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

James Zern jzern
Sat May 29 01:17:26 CEST 2010


On Thu, May 20, 2010 at 05:24, David Conrad <lessen42 at gmail.com> wrote:
> On May 19, 2010, at 3:26 PM, James Zern wrote:
>
>> On Wed, May 19, 2010 at 15:14, David Conrad <lessen42 at gmail.com> wrote:
>>> On May 19, 2010, at 2:51 PM, James Zern wrote:
>>>
>>>> Related thread: '[PATCH] VP8 de/encode via libvpx'
>>>> VP8 and associated WebM container made public today [1].
>>>>
>>>> The attached adds WebM de/muxing to matroska.
>>>> For both some extra ifdef's/checks were added to only allow VP8/vorbis
>>>> as that is the current supported configuration in web browsers. If
>>>> these are too busy in this respect they be cleaned up.
>>>
>>> Yes, the only #ifdef should be around the AVOutputFormat.
>>>
>>>> + ? ?if (!strcmp("webm", s->oformat->name)) {
>>>> + ? ? ? ?mkv->max_cluster_size = 500*1024*1024; ? // 500 MB
>>>> + ? ? ? ?mkv->max_cluster_pts = 30000; ? ? ? ? ? ?// 30 seconds
>>>> + ? ?} else {
>>>
>>> Why? Seeking can only seek to a cluster, then do a linear scan to find the needed packet, and 500 MB makes that incredibly inefficient.
>> This was added as a worst case, the recommendation is for keyframes on
>> the 2-5s range.
>
>>>> +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 == NULL)
>>>> + ? ? ? ?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_packet2(AVFormatContext *s, AVPacket *pkt)
>>>> +{
>>>
>>> webm and mkv shouldn't use separate write_packets, the interleaving changes should be done for mkv too. Maybe call the current mkv_write_packet mkv_write_packet_internal and move all cluster handling to the outer one.
>>>
>> OK. The thought initially was not to force the keyframe change on the
>> matroska muxer, but I think this can be reworked.
>
> If the conceptual goal is 1 cluster == 1 gop, the main reason I didn't do it for mkv is because I was worried about overhead. But after making the the streaming changes I think the overhead of this shouldn't be terribly important. At any rate, the only difference I want in muxing webm is the doctype and checks to not write unsupported elements (namely non-vp8+vorbis.)
>
The attached adds the webm doctype, audio packet buffering and
keyframe clustering decision.
-------------- next part --------------
Index: Changelog
===================================================================
--- Changelog	(revision 23368)
+++ 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 23368)
+++ 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 23368)
+++ 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 23368)
+++ 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,11 @@ 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 +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);
+                }
+
                 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
 
                 if (!native_id)
@@ -693,7 +705,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 +758,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 +877,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 +926,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 == NULL)
+        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 +967,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 +994,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 +1042,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 +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 = {
+    "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 +1094,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 23368)
+++ 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 23368)
+++ 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 23368)
+++ 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