[FFmpeg-devel] [PATCH 2/2] lavf: Add WebM DASH Manifest Muxer

Vignesh Venkatasubramanian vigneshv at google.com
Mon Jul 7 18:37:39 CEST 2014


On Mon, Jun 30, 2014 at 4:26 PM, Timothy Gu <timothygu99 at gmail.com> wrote:
> On Mon, Jun 30, 2014 at 3:44 PM, Vignesh Venkatasubramanian
> <vigneshv at google.com> wrote:
>
>> 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
>
> WebM DASH Manifest muxer
>

will update.

>> 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 },
>
> I don't get the help message. Is it supposed to be:
> "id=0,streams=0,1,2 id=1,streams=3,4"
> or "id=0 streams=0,1,2 id=1 streams=3,4"?
>
> Also this needs an doc/muxers.texi update, preferably with some
> real-world examples.
>

the purpose of this parameter is to combine streams into adaptation
sets with a unique id. so it would go like this: "id=1,streams=0,1,2
id=2,streams=3,4". This means that: streams 0, 1 and 2 belong to the
same adaptation set with the unique identifier 1; streams 3 and 4
belong to the same adaptation set with unique identifier 2. it is sort
of a parameter within a parameter. please let me know if it makes
sense now.

i will add some examples in doc/muxers.texi.

>> +    { NULL },
>> +};
>> +
>> +#if CONFIG_WEBM_DASH_MANIFEST_MUXER
>> +static const AVClass webm_dash_class = {
>> +    .class_name = "webm dash manifest muxer",
>
> Proper capitalization: WebM DASH Manifest muxer
>

i thought this should always be lower case. my bad, will update.

>> +    .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,
>
> I don't think this will work. audio_ and video_codec are for default
> codecs. I'm not 100% sure what to do if what you want is "valid
> codecs", writing a query_codec callback maybe?
>

i think i misunderstood the purpose of these values. what i was trying
to do is make this muxer accept only streams which has codec value to
be one of this. i will figure out the right way to do that and update
it.

>> +    .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
>>
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel at ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel



-- 
Vignesh


More information about the ffmpeg-devel mailing list