[FFmpeg-devel] [PATCH] WebM mux/demux

James Zern jzern
Thu Jun 3 00:49:20 CEST 2010


On Fri, May 28, 2010 at 20:06, Diego Biurrun <diego at biurrun.de> wrote:
> On Fri, May 28, 2010 at 07:17:26PM -0400, James Zern wrote:
>> The attached adds the webm doctype, audio packet buffering and
>> keyframe clustering decision.
>
>> --- libavformat/matroskaenc.c (revision 23368)
>> +++ libavformat/matroskaenc.c (working copy)
>> @@ -597,6 +604,11 @@ static int mkv_write_tracks(AVFormatCont
>> ? ? ? ? ? ? ? ? ?break;
>>
>> ? ? ? ? ? ? ?case AVMEDIA_TYPE_AUDIO:
>> + ? ? ? ? ? ? ? ?if (codec->codec_id != CODEC_ID_VORBIS && !strcmp("webm", s->oformat->name)) {
>> + ? ? ? ? ? ? ? ? ? ?av_log(s, AV_LOG_ERROR, "Only VP8 video and Vorbis audio are supported for WebM.\n");
>> + ? ? ? ? ? ? ? ? ? ?return AVERROR(EINVAL);
>
> These long lines could easily be broken.
>
>> @@ -910,9 +926,39 @@ static int mkv_write_packet(AVFormatCont
>> +
>> + ? ?if (mkv->cur_audio_pkt.data == NULL)
>
> (!mkv->cur_audio_pkt.data)
>
Sorry was keeping with the format of the file, both updated. I left
the format string as one piece, but if you want to limit to 80
characters on new code I'll make another split.

>> @@ -989,7 +1063,23 @@ AVOutputFormat matroska_muxer = {
>> ? ? ?.codec_tag = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
>> ? ? ?.subtitle_codec = CODEC_ID_TEXT,
>> ?};
>> -
>> +#endif
>> +#if CONFIG_WEBM_MUXER
>> +AVOutputFormat webm_muxer = {
>> +};
>> +#endif
>> +#if CONFIG_MATROSKA_AUDIO_MUXER
>> ?AVOutputFormat matroska_audio_muxer = {
>
> Put an empty line between those preprocessor directives..
>
Done.
-------------- next part --------------
Index: Changelog
===================================================================
--- Changelog	(revision 23437)
+++ Changelog	(working copy)
@@ -4,7 +4,7 @@ releases are sorted from youngest to old
 
 version <next>:
 
-- WebM support in Matroska demuxer
+- WebM support in Matroska de/muxer
 - low overhead Ogg muxing
 - MMS-TCP support
 - VP8 decoding via libvpx
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile	(revision 23437)
+++ libavcodec/Makefile	(working copy)
@@ -512,6 +512,8 @@ OBJS-$(CONFIG_OGG_DEMUXER)             +
                                           dirac.o mpeg12data.o
 OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o flacdata.o flac.o
 OBJS-$(CONFIG_RTP_MUXER)               += mpegvideo.o
+OBJS-$(CONFIG_WEBM_MUXER)              += xiph.o mpeg4audio.o \
+                                          flacdec.o flacdata.o flac.o
 
 # external codec libraries
 OBJS-$(CONFIG_LIBDIRAC_DECODER)           += libdiracdec.o
Index: doc/general.texi
===================================================================
--- doc/general.texi	(revision 23437)
+++ doc/general.texi	(working copy)
@@ -236,7 +236,7 @@ library:
 @item VC-1 test bitstream       @tab X @tab X
 @item WAV                       @tab X @tab X
 @item WavPack                   @tab   @tab X
- at item WebM                      @tab   @tab X
+ at item WebM                      @tab X @tab X
 @item Wing Commander III movie  @tab   @tab X
     @tab Multimedia format used in Origin's Wing Commander III computer game.
 @item Westwood Studios audio    @tab   @tab X
Index: libavformat/matroskaenc.c
===================================================================
--- libavformat/matroskaenc.c	(revision 23437)
+++ libavformat/matroskaenc.c	(working copy)
@@ -81,6 +81,8 @@ typedef struct MatroskaMuxContext {
     mkv_track       *tracks;
 
     struct AVMD5    *md5_ctx;
+    unsigned int    audio_buffer_size;
+    AVPacket        cur_audio_pkt;
 } MatroskaMuxContext;
 
 
@@ -565,6 +567,13 @@ static int mkv_write_tracks(AVFormatCont
 
         switch (codec->codec_type) {
             case AVMEDIA_TYPE_VIDEO:
+                if (codec->codec_id != CODEC_ID_VP8 &&
+                    !strcmp("webm", s->oformat->name)) {
+                    av_log(s, AV_LOG_ERROR,
+                           "Only VP8 video and Vorbis audio are supported for WebM.\n");
+                    return AVERROR(EINVAL);
+                }
+
                 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
                 put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
 
@@ -597,6 +606,13 @@ static int mkv_write_tracks(AVFormatCont
                 break;
 
             case AVMEDIA_TYPE_AUDIO:
+                if (codec->codec_id != CODEC_ID_VORBIS &&
+                    !strcmp("webm", s->oformat->name)) {
+                    av_log(s, AV_LOG_ERROR,
+                           "Only VP8 video and Vorbis audio are supported for WebM.\n");
+                    return AVERROR(EINVAL);
+                }
+
                 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
 
                 if (!native_id)
@@ -693,7 +709,7 @@ static int mkv_write_header(AVFormatCont
     put_ebml_uint   (pb, EBML_ID_EBMLREADVERSION    ,           1);
     put_ebml_uint   (pb, EBML_ID_EBMLMAXIDLENGTH    ,           4);
     put_ebml_uint   (pb, EBML_ID_EBMLMAXSIZELENGTH  ,           8);
-    put_ebml_string (pb, EBML_ID_DOCTYPE            ,  "matroska");
+    put_ebml_string (pb, EBML_ID_DOCTYPE            , s->oformat->name);
     put_ebml_uint   (pb, EBML_ID_DOCTYPEVERSION     ,           2);
     put_ebml_uint   (pb, EBML_ID_DOCTYPEREADVERSION ,           2);
     end_ebml_master(pb, ebml_header);
@@ -746,6 +762,10 @@ static int mkv_write_header(AVFormatCont
     if (mkv->cues == NULL)
         return AVERROR(ENOMEM);
 
+    av_init_packet(&mkv->cur_audio_pkt);
+    mkv->cur_audio_pkt.size = 0;
+    mkv->audio_buffer_size  = 0;
+
     put_flush_packet(pb);
     return 0;
 }
@@ -861,7 +881,7 @@ static void mkv_flush_dynbuf(AVFormatCon
     mkv->dyn_bc = NULL;
 }
 
-static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
+static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 {
     MatroskaMuxContext *mkv = s->priv_data;
     ByteIOContext *pb = s->pb;
@@ -910,9 +930,39 @@ static int mkv_write_packet(AVFormatCont
         if (ret < 0) return ret;
     }
 
-    // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming
-    if ((url_is_streamed(s->pb) && (url_ftell(pb) > 32*1024 || ts > mkv->cluster_pts + 1000))
-        ||  url_ftell(pb) > mkv->cluster_pos + 5*1024*1024 || ts > mkv->cluster_pts + 5000) {
+    mkv->duration = FFMAX(mkv->duration, ts + duration);
+    return 0;
+}
+
+static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
+{
+    uint8_t *data           = mkv->cur_audio_pkt.data;
+    mkv->cur_audio_pkt      = *pkt;
+    mkv->cur_audio_pkt.data = av_fast_realloc(data, &mkv->audio_buffer_size, pkt->size);
+    if (!mkv->cur_audio_pkt.data)
+        return AVERROR(ENOMEM);
+
+    memcpy(mkv->cur_audio_pkt.data, pkt->data, pkt->size);
+    mkv->cur_audio_pkt.size = pkt->size;
+    return 0;
+}
+
+static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    MatroskaMuxContext *mkv = s->priv_data;
+    ByteIOContext *pb = url_is_streamed(s->pb) ? mkv->dyn_bc : s->pb;
+    AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
+    int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
+    int ret = 0;
+    int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
+
+    // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
+    // after 4k and on a keyframe for WebM
+    if (mkv->cluster_pos &&
+        ((url_is_streamed(s->pb) && (url_ftell(pb) > 32*1024 || ts > mkv->cluster_pts + 1000))
+         ||   url_ftell(pb) > mkv->cluster_pos + 5*1024*1024 || ts > mkv->cluster_pts + 5000
+         || (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
+             url_ftell(s->pb) > mkv->cluster_pos + 4*1024))) {
         av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
                " bytes, pts %" PRIu64 "\n", url_ftell(pb), ts);
         end_ebml_master(pb, mkv->cluster);
@@ -921,8 +971,24 @@ static int mkv_write_packet(AVFormatCont
             mkv_flush_dynbuf(s);
     }
 
-    mkv->duration = FFMAX(mkv->duration, ts + duration);
-    return 0;
+    // check if we have an audio packet cached
+    if ((codec->codec_type == AVMEDIA_TYPE_AUDIO || codec->codec_type == AVMEDIA_TYPE_VIDEO)
+        && mkv->cur_audio_pkt.size > 0) {
+        ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
+        mkv->cur_audio_pkt.size = 0;
+        if (ret < 0) {
+            av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
+            return ret;
+        }
+    }
+
+    // buffer an audio packet to ensure the packet containing the video
+    // keyframe's timecode is contained in the same cluster for WebM
+    if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
+        ret = mkv_copy_packet(mkv, pkt);
+    else
+        ret = mkv_write_packet_internal(s, pkt);
+    return ret;
 }
 
 static int mkv_write_trailer(AVFormatContext *s)
@@ -932,6 +998,16 @@ static int mkv_write_trailer(AVFormatCon
     int64_t currentpos, second_seekhead, cuespos;
     int ret;
 
+    // check if we have an audio packet cached
+    if (mkv->cur_audio_pkt.size > 0) {
+        ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
+        mkv->cur_audio_pkt.size = 0;
+        if (ret < 0) {
+            av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
+            return ret;
+        }
+    }
+
     if (mkv->dyn_bc) {
         end_ebml_master(mkv->dyn_bc, mkv->cluster);
         mkv_flush_dynbuf(s);
@@ -970,10 +1046,12 @@ static int mkv_write_trailer(AVFormatCon
     end_ebml_master(pb, mkv->segment);
     av_free(mkv->md5_ctx);
     av_free(mkv->tracks);
+    av_destruct_packet(&mkv->cur_audio_pkt);
     put_flush_packet(pb);
     return 0;
 }
 
+#if CONFIG_MATROSKA_MUXER
 AVOutputFormat matroska_muxer = {
     "matroska",
     NULL_IF_CONFIG_SMALL("Matroska file format"),
@@ -989,7 +1067,25 @@ AVOutputFormat matroska_muxer = {
     .codec_tag = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
     .subtitle_codec = CODEC_ID_TEXT,
 };
+#endif
+
+#if CONFIG_WEBM_MUXER
+AVOutputFormat webm_muxer = {
+    "webm",
+    NULL_IF_CONFIG_SMALL("WebM file format"),
+    "video/webm",
+    "webm",
+    sizeof(MatroskaMuxContext),
+    CODEC_ID_VORBIS,
+    CODEC_ID_VP8,
+    mkv_write_header,
+    mkv_write_packet,
+    mkv_write_trailer,
+    .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
+};
+#endif
 
+#if CONFIG_MATROSKA_AUDIO_MUXER
 AVOutputFormat matroska_audio_muxer = {
     "matroska",
     NULL_IF_CONFIG_SMALL("Matroska file format"),
@@ -1004,3 +1100,4 @@ AVOutputFormat matroska_audio_muxer = {
     .flags = AVFMT_GLOBALHEADER,
     .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
 };
+#endif
Index: libavformat/avformat.h
===================================================================
--- libavformat/avformat.h	(revision 23437)
+++ libavformat/avformat.h	(working copy)
@@ -22,7 +22,7 @@
 #define AVFORMAT_AVFORMAT_H
 
 #define LIBAVFORMAT_VERSION_MAJOR 52
-#define LIBAVFORMAT_VERSION_MINOR 67
+#define LIBAVFORMAT_VERSION_MINOR 68
 #define LIBAVFORMAT_VERSION_MICRO  0
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
Index: libavformat/Makefile
===================================================================
--- libavformat/Makefile	(revision 23437)
+++ libavformat/Makefile	(working copy)
@@ -258,6 +258,9 @@ OBJS-$(CONFIG_W64_DEMUXER)              
 OBJS-$(CONFIG_WAV_DEMUXER)               += wav.o riff.o raw.o
 OBJS-$(CONFIG_WAV_MUXER)                 += wav.o riff.o
 OBJS-$(CONFIG_WC3_DEMUXER)               += wc3movie.o
+OBJS-$(CONFIG_WEBM_MUXER)                += matroskaenc.o matroska.o \
+                                            riff.o isom.o avc.o \
+                                            flacenc_header.o
 OBJS-$(CONFIG_WSAUD_DEMUXER)             += westwood.o
 OBJS-$(CONFIG_WSVQA_DEMUXER)             += westwood.o
 OBJS-$(CONFIG_WV_DEMUXER)                += wv.o apetag.o id3v1.o
Index: libavformat/allformats.c
===================================================================
--- libavformat/allformats.c	(revision 23437)
+++ libavformat/allformats.c	(working copy)
@@ -206,6 +206,7 @@ void av_register_all(void)
     REGISTER_DEMUXER  (W64, w64);
     REGISTER_MUXDEMUX (WAV, wav);
     REGISTER_DEMUXER  (WC3, wc3);
+    REGISTER_MUXER    (WEBM, webm);
     REGISTER_DEMUXER  (WSAUD, wsaud);
     REGISTER_DEMUXER  (WSVQA, wsvqa);
     REGISTER_DEMUXER  (WV, wv);



More information about the ffmpeg-devel mailing list