[FFmpeg-devel] [PATCH 2/2] lavf: Add WebM DASH Manifest Muxer
Vignesh Venkatasubramanian
vigneshv at google.com
Tue Jul 1 00:44:37 CEST 2014
This patch adds the ability to generate WebM DASH manifest XML using
ffmpeg. A sample command line would be as follows:
ffmpeg \
-f webm_dash_manifest -i video1.webm \
-f webm_dash_manifest -i video2.webm \
-f webm_dash_manifest -i audio1.webm \
-f webm_dash_manifest -i audio2.webm \
-map 0 -map 1 -map 2 -map 3 \
-c copy \
-f webm_dash_manifest \
-adaptation_sets “id=0,streams=0,1 id=1,streams=2,3” \
manifest.xml
It works by exporting necessary fields as metadata tags in matroskadec
and use those values to write the appropriate XML fields as per the WebM
DASH Specification [1]. Some ideas are adopted from webm-tools project
[2].
[1]
https://sites.google.com/a/webmproject.org/wiki/adaptive-streaming/webm-dash-specification
[2]
https://chromium.googlesource.com/webm/webm-tools/+/master/webm_dash_manifest/
Signed-off-by: Vignesh Venkatasubramanian <vigneshv at google.com>
---
Changelog | 1 +
libavformat/Makefile | 11 +-
libavformat/allformats.c | 1 +
libavformat/version.h | 2 +-
libavformat/webmdashenc.c | 306 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 318 insertions(+), 3 deletions(-)
create mode 100644 libavformat/webmdashenc.c
diff --git a/Changelog b/Changelog
index 0346877..81ae8ab 100644
--- a/Changelog
+++ b/Changelog
@@ -30,6 +30,7 @@ version <next>:
- zoompan filter
- signalstats filter
- hqx filter (hq2x, hq3x, hq4x)
+- WebM DASH Manifest
version 2.2:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 46aac96..aa53d1c 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -212,7 +212,8 @@ OBJS-$(CONFIG_MATROSKA_DEMUXER) += matroskadec.o matroska.o \
flac_picture.o replaygain.o
OBJS-$(CONFIG_MATROSKA_MUXER) += matroskaenc.o matroska.o \
isom.o avc.o hevc.o \
- flacenc_header.o avlanguage.o vorbiscomment.o wv.o
+ flacenc_header.o avlanguage.o vorbiscomment.o wv.o \
+ webmdashenc.o
OBJS-$(CONFIG_MD5_MUXER) += md5enc.o
OBJS-$(CONFIG_MGSTS_DEMUXER) += mgsts.o
OBJS-$(CONFIG_MICRODVD_DEMUXER) += microdvddec.o subtitles.o
@@ -421,7 +422,13 @@ OBJS-$(CONFIG_WC3_DEMUXER) += wc3movie.o
OBJS-$(CONFIG_WEBM_MUXER) += matroskaenc.o matroska.o \
isom.o avc.o hevc.o \
flacenc_header.o avlanguage.o \
- wv.o vorbiscomment.o
+ wv.o vorbiscomment.o \
+ webmdashenc.o
+OBJS-$(CONFIG_WEBM_DASH_MANIFEST_DEMUXER)+= matroskadec.o matroska.o \
+ isom.o rmsipr.o flac_picture.o \
+ oggparsevorbis.o vorbiscomment.o \
+ flac_picture.o replaygain.o
+OBJS-$(CONFIG_WEBM_DASH_MANIFEST_MUXER) += webmdashenc.o matroska.o
OBJS-$(CONFIG_WEBVTT_DEMUXER) += webvttdec.o subtitles.o
OBJS-$(CONFIG_WEBVTT_MUXER) += webvttenc.o
OBJS-$(CONFIG_WSAUD_DEMUXER) += westwood_aud.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index dc5557c..53fc992 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -306,6 +306,7 @@ void av_register_all(void)
REGISTER_MUXDEMUX(WAV, wav);
REGISTER_DEMUXER (WC3, wc3);
REGISTER_MUXER (WEBM, webm);
+ REGISTER_MUXDEMUX(WEBM_DASH_MANIFEST, webm_dash_manifest);
REGISTER_MUXDEMUX(WEBVTT, webvtt);
REGISTER_DEMUXER (WSAUD, wsaud);
REGISTER_DEMUXER (WSVQA, wsvqa);
diff --git a/libavformat/version.h b/libavformat/version.h
index 67393e0..a5b24bf 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
#include "libavutil/version.h"
#define LIBAVFORMAT_VERSION_MAJOR 55
-#define LIBAVFORMAT_VERSION_MINOR 44
+#define LIBAVFORMAT_VERSION_MINOR 45
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
diff --git a/libavformat/webmdashenc.c b/libavformat/webmdashenc.c
new file mode 100644
index 0000000..d8c2a65
--- /dev/null
+++ b/libavformat/webmdashenc.c
@@ -0,0 +1,306 @@
+/*
+ * WebM DASH Manifest XML muxer
+ * Copyright (c) 2014 Vignesh Venkatasubramanian
+ *
+ * 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
+ */
+
+/*
+ * WebM DASH Specification:
+ * https://sites.google.com/a/webmproject.org/wiki/adaptive-streaming/webm-dash-specification
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include "avformat.h"
+#include "avio_internal.h"
+#include "matroska.h"
+
+#include "libavutil/avstring.h"
+#include "libavutil/dict.h"
+#include "libavutil/opt.h"
+
+typedef struct AdaptationSet {
+ char id[10];
+ int *streams;
+ int nb_streams;
+} AdaptationSet;
+
+typedef struct WebMDashMuxContext {
+ const AVClass *class;
+ char *adaptation_sets;
+ AdaptationSet *as;
+ int nb_as;
+} WebMDashMuxContext;
+
+static void write_string(AVFormatContext *s, const char *str, int should_free)
+{
+ avio_write(s->pb, str, strlen(str));
+ if (should_free)
+ av_free((char*)str);
+}
+
+static const char *get_codec_name(int codec_id)
+{
+ switch (codec_id) {
+ case AV_CODEC_ID_VP8:
+ return "vp8";
+ case AV_CODEC_ID_VP9:
+ return "vp9";
+ case AV_CODEC_ID_VORBIS:
+ return "vorbis";
+ case AV_CODEC_ID_OPUS:
+ return "opus";
+ }
+ return NULL;
+}
+
+static double get_duration(AVFormatContext *s)
+{
+ int i = 0;
+ double max = 0.0;
+ for (i = 0; i < s->nb_streams; i++) {
+ AVDictionaryEntry *duration = av_dict_get(s->streams[i]->metadata,
+ DURATION, NULL, 0);
+ if (atof(duration->value) > max) max = atof(duration->value);
+ }
+ return max / 1000;
+}
+
+static void write_header(AVFormatContext *s)
+{
+ double min_buffer_time = 1.0;
+ write_string(s, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", 0);
+ write_string(s, "<MPD\n", 0);
+ write_string(s, " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n", 0);
+ write_string(s, " xmlns=\"urn:mpeg:DASH:schema:MPD:2011\"\n", 0);
+ write_string(s, " xsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011\"\n", 0);
+ write_string(s, " type=\"static\"\n", 0);
+ write_string(s, av_asprintf(" mediaPresentationDuration=\"PT%gS\"\n", get_duration(s)), 1);
+ write_string(s, av_asprintf(" minBufferTime=\"PT%gS\"\n", min_buffer_time), 1);
+ write_string(s, " profiles=\"urn:webm:dash:profile:webm-on-demand:2012\"", 0);
+ write_string(s, ">\n", 0);
+}
+
+static void write_footer(AVFormatContext *s)
+{
+ write_string(s, "</MPD>", 0);
+}
+
+static int subsegment_alignment(AVFormatContext *s, AdaptationSet *as) {
+ int i;
+ AVDictionaryEntry *gold = av_dict_get(s->streams[as->streams[0]]->metadata,
+ CUE_TIMESTAMPS, NULL, 0);
+ for (i = 1; i < as->nb_streams; i++) {
+ AVDictionaryEntry *ts = av_dict_get(s->streams[as->streams[i]]->metadata,
+ CUE_TIMESTAMPS, NULL, 0);
+ if (strncmp(gold->value, ts->value, strlen(gold->value))) return 0;
+ }
+ return 1;
+}
+
+static int bitstream_switching(AVFormatContext *s, AdaptationSet *as) {
+ int i;
+ AVDictionaryEntry *gold_track_num = av_dict_get(s->streams[as->streams[0]]->metadata,
+ TRACK_NUMBER, NULL, 0);
+ AVCodecContext *gold_codec = s->streams[as->streams[0]]->codec;
+ for (i = 1; i < as->nb_streams; i++) {
+ AVDictionaryEntry *track_num = av_dict_get(s->streams[as->streams[i]]->metadata,
+ TRACK_NUMBER, NULL, 0);
+ AVCodecContext *codec = s->streams[as->streams[i]]->codec;
+ if (strncmp(gold_track_num->value, track_num->value, strlen(gold_track_num->value)) ||
+ gold_codec->codec_id != codec->codec_id ||
+ gold_codec->extradata_size != codec->extradata_size ||
+ memcmp(gold_codec->extradata, codec->extradata, codec->extradata_size)) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+static void write_adaptation_set(AVFormatContext *s, int as_index)
+{
+ WebMDashMuxContext *w = s->priv_data;
+ AdaptationSet *as = &w->as[as_index];
+ AVCodecContext *codec = s->streams[as->streams[0]]->codec;
+ int i;
+ char boolean[2][6] = { "false", "true" };
+ int subsegmentStartsWithSAP = 1;
+ AVDictionaryEntry *lang;
+ write_string(s, av_asprintf("<AdaptationSet id=\"%s\"", as->id), 1);
+ write_string(s, av_asprintf(" mimeType=\"%s/webm\"",
+ codec->codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio"), 1);
+ write_string(s, av_asprintf(" codecs=\"%s\"", get_codec_name(codec->codec_id)), 1);
+
+ lang = av_dict_get(s->streams[as->streams[0]]->metadata, "language", NULL, 0);
+ if (lang != NULL) write_string(s, av_asprintf(" lang=\"%s\"", lang->value), 1);
+
+ if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+ write_string(s, av_asprintf(" width=\"%d\"", codec->width), 1);
+ write_string(s, av_asprintf(" height=\"%d\"", codec->height), 1);
+ } else {
+ write_string(s, av_asprintf(" audioSamplingRate=\"%d\"", codec->sample_rate), 1);
+ }
+
+ write_string(s, av_asprintf(" bitstreamSwitching=\"%s\"", boolean[bitstream_switching(s, as)]), 1);
+ write_string(s, av_asprintf(" subsegmentAlignment=\"%s\"", boolean[subsegment_alignment(s, as)]), 1);
+
+ for (i = 0; i < as->nb_streams; i++) {
+ AVDictionaryEntry *kf = av_dict_get(s->streams[as->streams[i]]->metadata,
+ CLUSTER_KEYFRAME, NULL, 0);
+ if (!strncmp(kf->value, "0", 1)) subsegmentStartsWithSAP = 0;
+ }
+ write_string(s, av_asprintf(" subsegmentStartsWithSAP=\"%d\"", subsegmentStartsWithSAP), 1);
+ write_string(s, ">\n", 0);
+
+ for (i = 0; i < as->nb_streams; i++) {
+ AVStream *stream = s->streams[as->streams[i]];
+ AVDictionaryEntry *irange = av_dict_get(stream->metadata, INITIALIZATION_RANGE, NULL, 0);
+ AVDictionaryEntry *cues_start = av_dict_get(stream->metadata, CUES_START, NULL, 0);
+ AVDictionaryEntry *cues_end = av_dict_get(stream->metadata, CUES_END, NULL, 0);
+ AVDictionaryEntry *filename = av_dict_get(stream->metadata, FILENAME, NULL, 0);
+ AVDictionaryEntry *bandwidth = av_dict_get(stream->metadata, BANDWIDTH, NULL, 0);
+ write_string(s, av_asprintf("<Representation id=\"%d\"", i), 1);
+ write_string(s, av_asprintf(" bandwidth=\"%s\"", bandwidth->value), 1);
+ write_string(s, ">\n", 0);
+ write_string(s, av_asprintf("<BaseURL>%s</BaseURL>\n", filename->value), 1);
+ write_string(s, "<SegmentBase\n", 0);
+ write_string(s, av_asprintf(" indexRange=\"%s-%s\">\n", cues_start->value, cues_end->value), 1);
+ write_string(s, "<Initialization\n", 0);
+ write_string(s, av_asprintf(" range=\"0-%s\" />\n", irange->value), 1);
+ write_string(s, "</SegmentBase>\n", 0);
+ write_string(s, "</Representation>\n", 0);
+ }
+ write_string(s, "</AdaptationSet>\n", 0);
+}
+
+static int to_integer(char *p, int len)
+{
+ int ret;
+ char *q = (char*)av_malloc(sizeof(char) * len);
+ strncpy(q, p, len);
+ ret = atoi(q);
+ av_free(q);
+ return ret;
+}
+
+static int parse_adaptation_sets(AVFormatContext *s)
+{
+ WebMDashMuxContext *w = s->priv_data;
+ char *p = w->adaptation_sets;
+ char *q;
+ enum { new_set, parsed_id, parsing_streams } state;
+ // syntax id=0,streams=0,1,2 id=1,streams=3,4 and so on
+ state = new_set;
+ while (p < w->adaptation_sets + strlen(w->adaptation_sets)) {
+ if (*p == ' ')
+ continue;
+ else if (state == new_set && !strncmp(p, "id=", 3)) {
+ w->as = av_realloc(w->as, sizeof(*w->as) * ++w->nb_as);
+ w->as[w->nb_as - 1].nb_streams = 0;
+ w->as[w->nb_as - 1].streams = NULL;
+ p += 3; // consume "id="
+ q = w->as[w->nb_as - 1].id;
+ while (*p != ',') *q++ = *p++;
+ *q = 0;
+ p++;
+ state = parsed_id;
+ } else if (state == parsed_id && !strncmp(p, "streams=", 8)) {
+ p += 8; // consume "streams="
+ state = parsing_streams;
+ } else if (state == parsing_streams) {
+ struct AdaptationSet *as = &w->as[w->nb_as - 1];
+ q = p;
+ while (*q != '\0' && *q != ',' && *q != ' ') q++;
+ as->streams = av_realloc(as->streams, sizeof(*as->streams) * ++as->nb_streams);
+ as->streams[as->nb_streams - 1] = to_integer(p, q - p);
+ if (*q == '\0') break;
+ if (*q == ' ') state = new_set;
+ p = ++q;
+ } else {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static int webm_dash_manifest_write_header(AVFormatContext *s)
+{
+ int i;
+ double start = 0.0;
+ WebMDashMuxContext *w = s->priv_data;
+ parse_adaptation_sets(s);
+ write_header(s);
+ write_string(s, "<Period id=\"0\"", 0);
+ write_string(s, av_asprintf(" start=\"PT%gS\"", start), 1);
+ write_string(s, av_asprintf(" duration=\"PT%gS\"", get_duration(s)), 1);
+ write_string(s, " >\n", 0);
+
+ for (i = 0; i < w->nb_as; i++) {
+ write_adaptation_set(s, i);
+ }
+
+ write_string(s, "</Period>\n", 0);
+ write_footer(s);
+ return 0;
+}
+
+static int webm_dash_manifest_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ return 0;
+}
+
+static int webm_dash_manifest_write_trailer(AVFormatContext *s)
+{
+ WebMDashMuxContext *w = s->priv_data;
+ int i;
+ for (i = 0; i < w->nb_as; i++) {
+ av_free(w->as[i].streams);
+ }
+ av_free(w->as);
+ return 0;
+}
+
+#define OFFSET(x) offsetof(WebMDashMuxContext, x)
+static const AVOption options[] = {
+ { "adaptation_sets", "Adaptation sets. Syntax id=0,streams=0,1,2 id=1 streams=3,4 and so on", OFFSET(adaptation_sets), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
+ { NULL },
+};
+
+#if CONFIG_WEBM_DASH_MANIFEST_MUXER
+static const AVClass webm_dash_class = {
+ .class_name = "webm dash manifest muxer",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+AVOutputFormat ff_webm_dash_manifest_muxer = {
+ .name = "webm_dash_manifest",
+ .long_name = NULL_IF_CONFIG_SMALL("WebM DASH Manifest"),
+ .mime_type = "application/xml",
+ .extensions = "xml",
+ .priv_data_size = sizeof(WebMDashMuxContext),
+ .audio_codec = AV_CODEC_ID_VORBIS || AV_CODEC_ID_OPUS,
+ .video_codec = AV_CODEC_ID_VP8 || AV_CODEC_ID_VP9,
+ .write_header = webm_dash_manifest_write_header,
+ .write_packet = webm_dash_manifest_write_packet,
+ .write_trailer = webm_dash_manifest_write_trailer,
+ .priv_class = &webm_dash_class,
+};
+#endif
--
2.0.0.526.g5318336
More information about the ffmpeg-devel
mailing list