avformat/dash: move reused API to common file and header file
authorSteven Liu <lq@chinaffmpeg.org>
Fri, 1 Sep 2017 02:20:56 +0000 (10:20 +0800)
committerSteven Liu <lq@chinaffmpeg.org>
Fri, 1 Sep 2017 02:20:56 +0000 (10:20 +0800)
move from dashenc, move DASHTmplId and dash_fill_tmpl_params to
dash.c, they will be used by dash demuxer and dash muxer.

v2 fixed:
1. rename common file from dashcomm.* to dash.*
Suggested-by: Hendrik Leppkes <h.leppkes@gmail.com>
v3 fixed:
1. rename header file pre defined
2. add ff_ prefix for the internal API
Suggested-by: James Almer <jamrial@gmail.com>
Suggested-by: Timo Rothenpieler <timo@rothenpieler.org>
Reviewed-by: wm4 <nfxjfg@googlemail.com>
Signed-off-by: Steven Liu <lq@onvideo.cn>
libavformat/Makefile
libavformat/dash.c [new file with mode: 0644]
libavformat/dash.h [new file with mode: 0644]
libavformat/dashenc.c

index 36f5839..de954af 100644 (file)
@@ -132,7 +132,7 @@ OBJS-$(CONFIG_CONCAT_DEMUXER)            += concatdec.o
 OBJS-$(CONFIG_CRC_MUXER)                 += crcenc.o
 OBJS-$(CONFIG_DATA_DEMUXER)              += rawdec.o
 OBJS-$(CONFIG_DATA_MUXER)                += rawenc.o
-OBJS-$(CONFIG_DASH_MUXER)                += dashenc.o
+OBJS-$(CONFIG_DASH_MUXER)                += dash.o dashenc.o
 OBJS-$(CONFIG_DAUD_DEMUXER)              += dauddec.o
 OBJS-$(CONFIG_DAUD_MUXER)                += daudenc.o
 OBJS-$(CONFIG_DCSTR_DEMUXER)             += dcstr.o
diff --git a/libavformat/dash.c b/libavformat/dash.c
new file mode 100644 (file)
index 0000000..b0b1ed4
--- /dev/null
@@ -0,0 +1,136 @@
+#include "config.h"
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
+#include "libavutil/rational.h"
+#include "libavutil/time_internal.h"
+
+#include "avc.h"
+#include "avformat.h"
+#include "avio_internal.h"
+#include "internal.h"
+#include "isom.h"
+#include "os_support.h"
+#include "url.h"
+#include "dash.h"
+
+static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
+                                    size_t format_tag_size, const char **ptr) {
+    const char *next_ptr;
+    DASHTmplId id_type = DASH_TMPL_ID_UNDEFINED;
+
+    if (av_strstart(identifier, "$$", &next_ptr)) {
+        id_type = DASH_TMPL_ID_ESCAPE;
+        *ptr = next_ptr;
+    } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
+        id_type = DASH_TMPL_ID_REP_ID;
+        // default to basic format, as $RepresentationID$ identifiers
+        // are not allowed to have custom format-tags.
+        av_strlcpy(format_tag, "%d", format_tag_size);
+        *ptr = next_ptr;
+    } else { // the following identifiers may have an explicit format_tag
+        if (av_strstart(identifier, "$Number", &next_ptr))
+            id_type = DASH_TMPL_ID_NUMBER;
+        else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
+            id_type = DASH_TMPL_ID_BANDWIDTH;
+        else if (av_strstart(identifier, "$Time", &next_ptr))
+            id_type = DASH_TMPL_ID_TIME;
+        else
+            id_type = DASH_TMPL_ID_UNDEFINED;
+
+        // next parse the dash format-tag and generate a c-string format tag
+        // (next_ptr now points at the first '%' at the beginning of the format-tag)
+        if (id_type != DASH_TMPL_ID_UNDEFINED) {
+            const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
+            if (next_ptr[0] == '$') { // no dash format-tag
+                snprintf(format_tag, format_tag_size, "%%%s", number_format);
+                *ptr = &next_ptr[1];
+            } else {
+                const char *width_ptr;
+                // only tolerate single-digit width-field (i.e. up to 9-digit width)
+                if (av_strstart(next_ptr, "%0", &width_ptr) &&
+                    av_isdigit(width_ptr[0]) &&
+                    av_strstart(&width_ptr[1], "d$", &next_ptr)) {
+                    // yes, we're using a format tag to build format_tag.
+                    snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
+                    *ptr = next_ptr;
+                } else {
+                    av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
+                                                 "closing '$' character or a format-string like '%%0[width]d', "
+                                                 "where width must be a single digit\n", next_ptr);
+                    id_type = DASH_TMPL_ID_UNDEFINED;
+                }
+            }
+        }
+    }
+    return id_type;
+}
+
+void ff_dash_fill_tmpl_params(char *dst, size_t buffer_size,
+                                  const char *template, int rep_id,
+                                  int number, int bit_rate,
+                                  int64_t time) {
+    int dst_pos = 0;
+    const char *t_cur = template;
+    while (dst_pos < buffer_size - 1 && *t_cur) {
+        char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
+        int n = 0;
+        DASHTmplId id_type;
+        const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
+        if (t_next) {
+            int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
+            av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
+            // advance
+            dst_pos += num_copy_bytes;
+            t_cur = t_next;
+        } else { // no more DASH identifiers to substitute - just copy the rest over and break
+            av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
+            break;
+        }
+
+        if (dst_pos >= buffer_size - 1 || !*t_cur)
+            break;
+
+        // t_cur is now pointing to a '$' character
+        id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
+        switch (id_type) {
+        case DASH_TMPL_ID_ESCAPE:
+            av_strlcpy(&dst[dst_pos], "$", 2);
+            n = 1;
+            break;
+        case DASH_TMPL_ID_REP_ID:
+            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
+            break;
+        case DASH_TMPL_ID_NUMBER:
+            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
+            break;
+        case DASH_TMPL_ID_BANDWIDTH:
+            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
+            break;
+        case DASH_TMPL_ID_TIME:
+            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
+            break;
+        case DASH_TMPL_ID_UNDEFINED:
+            // copy over one byte and advance
+            av_strlcpy(&dst[dst_pos], t_cur, 2);
+            n = 1;
+            t_next = &t_cur[1];
+            break;
+        }
+        // t_next points just past the processed identifier
+        // n is the number of bytes that were attempted to be written to dst
+        // (may have failed to write all because buffer_size).
+
+        // advance
+        dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
+        t_cur = t_next;
+    }
+}
+
+
diff --git a/libavformat/dash.h b/libavformat/dash.h
new file mode 100644 (file)
index 0000000..c6d9d2c
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * MPEG-DASH ISO BMFF segmenter
+ * Copyright (c) 2014 Martin Storsjo
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFORMAT_DASH_H
+#define AVFORMAT_DASH_H
+#include "avformat.h"
+
+// See ISO/IEC 23009-1:2014 5.3.9.4.4
+typedef enum {
+    DASH_TMPL_ID_UNDEFINED = -1,
+    DASH_TMPL_ID_ESCAPE,
+    DASH_TMPL_ID_REP_ID,
+    DASH_TMPL_ID_NUMBER,
+    DASH_TMPL_ID_BANDWIDTH,
+    DASH_TMPL_ID_TIME,
+} DASHTmplId;
+
+
+void ff_dash_fill_tmpl_params(char *dst, size_t buffer_size, const char *template, int rep_id, int number, int bit_rate, int64_t time);
+
+#endif /* AVFORMAT_DASH_H */
index bdb332c..6471649 100644 (file)
 #include "isom.h"
 #include "os_support.h"
 #include "url.h"
-
-// See ISO/IEC 23009-1:2014 5.3.9.4.4
-typedef enum {
-    DASH_TMPL_ID_UNDEFINED = -1,
-    DASH_TMPL_ID_ESCAPE,
-    DASH_TMPL_ID_REP_ID,
-    DASH_TMPL_ID_NUMBER,
-    DASH_TMPL_ID_BANDWIDTH,
-    DASH_TMPL_ID_TIME,
-} DASHTmplId;
+#include "dash.h"
 
 typedef struct Segment {
     char file[1024];
@@ -259,119 +250,6 @@ static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext
     }
 }
 
-static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
-                                    size_t format_tag_size, const char **ptr) {
-    const char *next_ptr;
-    DASHTmplId id_type = DASH_TMPL_ID_UNDEFINED;
-
-    if (av_strstart(identifier, "$$", &next_ptr)) {
-        id_type = DASH_TMPL_ID_ESCAPE;
-        *ptr = next_ptr;
-    } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
-        id_type = DASH_TMPL_ID_REP_ID;
-        // default to basic format, as $RepresentationID$ identifiers
-        // are not allowed to have custom format-tags.
-        av_strlcpy(format_tag, "%d", format_tag_size);
-        *ptr = next_ptr;
-    } else { // the following identifiers may have an explicit format_tag
-        if (av_strstart(identifier, "$Number", &next_ptr))
-            id_type = DASH_TMPL_ID_NUMBER;
-        else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
-            id_type = DASH_TMPL_ID_BANDWIDTH;
-        else if (av_strstart(identifier, "$Time", &next_ptr))
-            id_type = DASH_TMPL_ID_TIME;
-        else
-            id_type = DASH_TMPL_ID_UNDEFINED;
-
-        // next parse the dash format-tag and generate a c-string format tag
-        // (next_ptr now points at the first '%' at the beginning of the format-tag)
-        if (id_type != DASH_TMPL_ID_UNDEFINED) {
-            const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
-            if (next_ptr[0] == '$') { // no dash format-tag
-                snprintf(format_tag, format_tag_size, "%%%s", number_format);
-                *ptr = &next_ptr[1];
-            } else {
-                const char *width_ptr;
-                // only tolerate single-digit width-field (i.e. up to 9-digit width)
-                if (av_strstart(next_ptr, "%0", &width_ptr) &&
-                    av_isdigit(width_ptr[0]) &&
-                    av_strstart(&width_ptr[1], "d$", &next_ptr)) {
-                    // yes, we're using a format tag to build format_tag.
-                    snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
-                    *ptr = next_ptr;
-                } else {
-                    av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
-                                                 "closing '$' character or a format-string like '%%0[width]d', "
-                                                 "where width must be a single digit\n", next_ptr);
-                    id_type = DASH_TMPL_ID_UNDEFINED;
-                }
-            }
-        }
-    }
-    return id_type;
-}
-
-static void dash_fill_tmpl_params(char *dst, size_t buffer_size,
-                                  const char *template, int rep_id,
-                                  int number, int bit_rate,
-                                  int64_t time) {
-    int dst_pos = 0;
-    const char *t_cur = template;
-    while (dst_pos < buffer_size - 1 && *t_cur) {
-        char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
-        int n = 0;
-        DASHTmplId id_type;
-        const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
-        if (t_next) {
-            int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
-            av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
-            // advance
-            dst_pos += num_copy_bytes;
-            t_cur = t_next;
-        } else { // no more DASH identifiers to substitute - just copy the rest over and break
-            av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
-            break;
-        }
-
-        if (dst_pos >= buffer_size - 1 || !*t_cur)
-            break;
-
-        // t_cur is now pointing to a '$' character
-        id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
-        switch (id_type) {
-        case DASH_TMPL_ID_ESCAPE:
-            av_strlcpy(&dst[dst_pos], "$", 2);
-            n = 1;
-            break;
-        case DASH_TMPL_ID_REP_ID:
-            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
-            break;
-        case DASH_TMPL_ID_NUMBER:
-            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
-            break;
-        case DASH_TMPL_ID_BANDWIDTH:
-            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
-            break;
-        case DASH_TMPL_ID_TIME:
-            n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
-            break;
-        case DASH_TMPL_ID_UNDEFINED:
-            // copy over one byte and advance
-            av_strlcpy(&dst[dst_pos], t_cur, 2);
-            n = 1;
-            t_next = &t_cur[1];
-            break;
-        }
-        // t_next points just past the processed identifier
-        // n is the number of bytes that were attempted to be written to dst
-        // (may have failed to write all because buffer_size).
-
-        // advance
-        dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
-        t_cur = t_next;
-    }
-}
-
 static char *xmlescape(const char *str) {
     int outlen = strlen(str)*3/2 + 6;
     char *out = av_realloc(NULL, outlen + 1);
@@ -660,11 +538,11 @@ static int dash_init(AVFormatContext *s)
 
         if (c->single_file) {
             if (c->single_file_name)
-                dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
+                ff_dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
             else
                 snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
         } else {
-            dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
+            ff_dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
         }
         snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
         ret = s->io_open(s, &os->out, filename, AVIO_FLAG_WRITE, NULL);
@@ -865,7 +743,7 @@ static int dash_flush(AVFormatContext *s, int final, int stream)
         start_pos = avio_tell(os->ctx->pb);
 
         if (!c->single_file) {
-            dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
+            ff_dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
             snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
             snprintf(temp_path, sizeof(temp_path), use_rename ? "%s.tmp" : "%s", full_path);
             ret = s->io_open(s, &os->out, temp_path, AVIO_FLAG_WRITE, NULL);