Merge commit '7295b7373862ee54903b33d6ef3335531dfa93ad'
authorRodger Combs <rodger.combs@gmail.com>
Tue, 26 Sep 2017 17:13:09 +0000 (14:13 -0300)
committerJames Almer <jamrial@gmail.com>
Tue, 26 Sep 2017 17:13:09 +0000 (14:13 -0300)
* commit '7295b7373862ee54903b33d6ef3335531dfa93ad':
  dashenc: add webm support

Merged-by: Rodger Combs <rodger.combs@gmail.com>
1  2 
libavformat/dashenc.c
libavformat/version.h

@@@ -375,15 -527,11 +411,15 @@@ static int write_adaptation_set(AVForma
              continue;
  
          if (as->media_type == AVMEDIA_TYPE_VIDEO) {
 -            avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/%s\" codecs=\"%s\"%s width=\"%d\" height=\"%d\">\n",
 +            AVStream *st = s->streams[i];
-             avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/mp4\" codecs=\"%s\"%s width=\"%d\" height=\"%d\"",
-                 i, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->width, s->streams[i]->codecpar->height);
++            avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/%s\" codecs=\"%s\"%s width=\"%d\" height=\"%d\"",
+                 i, os->format_name, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->width, s->streams[i]->codecpar->height);
 +            if (st->avg_frame_rate.num)
 +                avio_printf(out, " frameRate=\"%d/%d\"", st->avg_frame_rate.num, st->avg_frame_rate.den);
 +            avio_printf(out, ">\n");
          } else {
-             avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"audio/mp4\" codecs=\"%s\"%s audioSamplingRate=\"%d\">\n",
-                 i, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->sample_rate);
+             avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"audio/%s\" codecs=\"%s\"%s audioSamplingRate=\"%d\">\n",
+                 i, os->format_name, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->sample_rate);
              avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n",
                  s->streams[i]->codecpar->channels);
          }
@@@ -628,7 -766,15 +664,15 @@@ static int dict_copy_entry(AVDictionar
      return 0;
  }
  
 -static int dash_write_header(AVFormatContext *s)
+ static int dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
+ {
+     char valuestr[22];
+     snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
+     flags &= ~AV_DICT_DONT_STRDUP_VAL;
+     return av_dict_set(pm, key, valuestr, flags);
+ }
 +static int dash_init(AVFormatContext *s)
  {
      DASHContext *c = s->priv_data;
      int ret = 0, i;
      if (ptr)
          *ptr = '\0';
  
-     oformat = av_guess_format("mp4", NULL, NULL);
-     if (!oformat)
-         return AVERROR_MUXER_NOT_FOUND;
      c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
 -    if (!c->streams) {
 -        ret = AVERROR(ENOMEM);
 -        goto fail;
 -    }
 +    if (!c->streams)
 +        return AVERROR(ENOMEM);
  
      if ((ret = parse_adaptation_sets(s)) < 0)
 -        goto fail;
 +        return ret;
  
      for (i = 0; i < s->nb_streams; i++) {
          OutputStream *os = &c->streams[i];
          dict_copy_entry(&as->metadata, s->streams[i]->metadata, "role");
  
          ctx = avformat_alloc_context();
 -        if (!ctx) {
 -            ret = AVERROR(ENOMEM);
 -            goto fail;
 -        }
 +        if (!ctx)
 +            return AVERROR(ENOMEM);
+         // choose muxer based on codec: webm for VP8/9 and opus, mp4 otherwise
+         // note: os->format_name is also used as part of the mimetype of the
+         //       representation, e.g. video/<format_name>
+         if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_VP8 ||
+             s->streams[i]->codecpar->codec_id == AV_CODEC_ID_VP9 ||
+             s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
+             s->streams[i]->codecpar->codec_id == AV_CODEC_ID_VORBIS) {
+             snprintf(os->format_name, sizeof(os->format_name), "webm");
+         } else {
+             snprintf(os->format_name, sizeof(os->format_name), "mp4");
+         }
+         ctx->oformat = av_guess_format(os->format_name, NULL, NULL);
 -        if (!ctx->oformat) {
 -            ret = AVERROR_MUXER_NOT_FOUND;
 -            goto fail;
 -        }
++        if (!ctx->oformat)
++            return AVERROR_MUXER_NOT_FOUND;
          os->ctx = ctx;
-         ctx->oformat = oformat;
          ctx->interrupt_callback = s->interrupt_callback;
          ctx->opaque             = s->opaque;
          ctx->io_close           = s->io_close;
          snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
          ret = s->io_open(s, &os->out, filename, AVIO_FLAG_WRITE, NULL);
          if (ret < 0)
 -            goto fail;
 +            return ret;
          os->init_start_pos = 0;
  
-         av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
-         if ((ret = avformat_init_output(ctx, &opts)) < 0)
+         if (!strcmp(os->format_name, "mp4")) {
+             av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
+         } else {
+             dict_set_int(&opts, "cluster_time_limit", c->min_seg_duration / 1000, 0);
+             dict_set_int(&opts, "cluster_size_limit", 5 * 1024 * 1024, 0); // set a large cluster size limit
+         }
 -        if ((ret = avformat_write_header(ctx, &opts)) < 0) {
 -             goto fail;
 -        }
++        if ((ret = avformat_write_header(ctx, &opts)) < 0)
 +            return ret;
          os->ctx_inited = 1;
          avio_flush(ctx->pb);
          av_dict_free(&opts);
  
  #include "libavutil/version.h"
  
 -#define LIBAVFORMAT_VERSION_MAJOR 57
 -#define LIBAVFORMAT_VERSION_MINOR 10
 -#define LIBAVFORMAT_VERSION_MICRO  3
 +// Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 +// Also please add any ticket numbers that you believe might be affected here
 +#define LIBAVFORMAT_VERSION_MAJOR  57
 +#define LIBAVFORMAT_VERSION_MINOR  82
- #define LIBAVFORMAT_VERSION_MICRO 101
++#define LIBAVFORMAT_VERSION_MICRO 102
  
  #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                 LIBAVFORMAT_VERSION_MINOR, \