[FFmpeg-devel] [PATCH 3/3] avformat/oggenc: add vp8 muxing support

James Almer jamrial at gmail.com
Wed Jul 13 00:36:22 EEST 2016


Addresses ticket #5687

Signed-off-by: James Almer <jamrial at gmail.com>
---
AVFMT_TS_NONSTRICT is added to flags because invisible vp8 frames don't
increase the pts

 libavformat/oggenc.c        | 94 +++++++++++++++++++++++++++++++++++++++++----
 tests/fate/avformat.mak     |  1 +
 tests/lavf-regression.sh    |  4 ++
 tests/ref/lavf-fate/ogg_vp8 |  3 ++
 4 files changed, 94 insertions(+), 8 deletions(-)
 create mode 100644 tests/ref/lavf-fate/ogg_vp8

diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c
index 952261b..230dbf1 100644
--- a/libavformat/oggenc.c
+++ b/libavformat/oggenc.c
@@ -54,6 +54,8 @@ typedef struct OGGStreamContext {
     int kfgshift;
     int64_t last_kf_pts;
     int vrev;
+    /** for VP8 granule */
+    int isvp8;
     int eos;
     unsigned page_count; ///< number of page buffered
     OGGPage page; ///< current page
@@ -146,7 +148,8 @@ static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
 
 static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
 {
-    return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
+    return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
+           (oggstream->isvp8    && !((granule >> 3) & 0x07ffffff));
 }
 
 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
@@ -154,6 +157,8 @@ static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t gra
     if (oggstream->kfgshift)
         return (granule>>oggstream->kfgshift) +
             (granule & ((1<<oggstream->kfgshift)-1));
+    else if (oggstream->isvp8)
+        return granule >> 32;
     else
         return granule;
 }
@@ -219,11 +224,11 @@ static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
     int i, segments, len, flush = 0;
 
     // Handles VFR by flushing page because this frame needs to have a timestamp
-    // For theora, keyframes also need to have a timestamp to correctly mark
+    // For theora and VP8, keyframes also need to have a timestamp to correctly mark
     // them as such, otherwise seeking will not work correctly at the very
     // least with old libogg versions.
     // Do not try to flush header packets though, that will create broken files.
-    if (st->codecpar->codec_id == AV_CODEC_ID_THEORA && !header &&
+    if ((st->codecpar->codec_id == AV_CODEC_ID_THEORA || st->codecpar->codec_id == AV_CODEC_ID_VP8) && !header &&
         (ogg_granule_to_timestamp(oggstream, granule) >
          ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
          ogg_key_granule(oggstream, granule))) {
@@ -405,6 +410,57 @@ static int ogg_build_opus_headers(AVCodecParameters *par,
     return 0;
 }
 
+#define VP8_HEADER_SIZE 26
+
+static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st,
+                                 OGGStreamContext *oggstream, int bitexact)
+{
+    AVCodecParameters *par = st->codecpar;
+    uint8_t *p;
+
+    /* first packet: VP8 header */
+    p = av_mallocz(VP8_HEADER_SIZE);
+    if (!p)
+        return AVERROR(ENOMEM);
+    oggstream->header[0] = p;
+    oggstream->header_len[0] = VP8_HEADER_SIZE;
+    bytestream_put_byte(&p, 0x4f); // HDRID
+    bytestream_put_buffer(&p, "VP80", 4); // Identifier
+    bytestream_put_byte(&p, 1); // HDRTYP
+    bytestream_put_byte(&p, 1); // VMAJ
+    bytestream_put_byte(&p, 0); // VMIN
+    bytestream_put_be16(&p, par->width);
+    bytestream_put_be16(&p, par->height);
+    bytestream_put_be24(&p, par->sample_aspect_ratio.num);
+    bytestream_put_be24(&p, par->sample_aspect_ratio.den);
+    if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
+        // OggVP8 requires pts to increase by 1 per visible frame, so use the least common
+        // multiple framerate if available.
+        av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
+               st->time_base.num, st->time_base.den,
+               st->r_frame_rate.den, st->r_frame_rate.num);
+        avpriv_set_pts_info(st, 64, st->r_frame_rate.den, st->r_frame_rate.num);
+    }
+    bytestream_put_be32(&p, st->time_base.den);
+    bytestream_put_be32(&p, st->time_base.num);
+
+    /* optional second packet: VorbisComment */
+    if (av_dict_get(st->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
+        p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0);
+        if (!p)
+            return AVERROR(ENOMEM);
+        oggstream->header[1] = p;
+        bytestream_put_byte(&p, 0x4f); // HDRID
+        bytestream_put_buffer(&p, "VP80", 4); // Identifier
+        bytestream_put_byte(&p, 2); // HDRTYP
+        bytestream_put_byte(&p, 0x20);
+    }
+
+    oggstream->isvp8 = 1;
+
+    return 0;
+}
+
 static void ogg_write_pages(AVFormatContext *s, int flush)
 {
     OGGContext *ogg = s->priv_data;
@@ -452,12 +508,14 @@ static int ogg_write_header(AVFormatContext *s)
             st->codecpar->codec_id != AV_CODEC_ID_THEORA &&
             st->codecpar->codec_id != AV_CODEC_ID_SPEEX  &&
             st->codecpar->codec_id != AV_CODEC_ID_FLAC   &&
-            st->codecpar->codec_id != AV_CODEC_ID_OPUS) {
+            st->codecpar->codec_id != AV_CODEC_ID_OPUS   &&
+            st->codecpar->codec_id != AV_CODEC_ID_VP8) {
             av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
             return AVERROR(EINVAL);
         }
 
-        if (!st->codecpar->extradata || !st->codecpar->extradata_size) {
+        if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
+            st->codecpar->codec_id != AV_CODEC_ID_VP8) {
             av_log(s, AV_LOG_ERROR, "No extradata present\n");
             return AVERROR_INVALIDDATA;
         }
@@ -508,6 +566,14 @@ static int ogg_write_header(AVFormatContext *s)
                 av_freep(&st->priv_data);
                 return err;
             }
+        } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
+            int err = ogg_build_vp8_headers(s, st, oggstream,
+                                            s->flags & AVFMT_FLAG_BITEXACT);
+            if (err) {
+                av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
+                av_freep(&st->priv_data);
+                return err;
+            }
         } else {
             uint8_t *p;
             const char *cstr = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
@@ -600,7 +666,18 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
                   av_rescale_q(st->codecpar->initial_padding,
                                (AVRational){ 1, st->codecpar->sample_rate },
                                st->time_base);
-    else
+    else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
+        int64_t pts, invcnt, dist;
+        int visible;
+
+        visible = (pkt->data[0] >> 4) & 1;
+        pts     = pkt->pts + pkt->duration;
+        invcnt  = (oggstream->last_granule >> 30) & 3;
+        invcnt  = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
+        dist    = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
+
+        granule = (pts << 32) | (invcnt << 30) | (dist << 3);
+    } else
         granule = pkt->pts + pkt->duration;
 
     if (oggstream->page.start_granule == AV_NOPTS_VALUE)
@@ -653,7 +730,8 @@ static int ogg_write_trailer(AVFormatContext *s)
         OGGStreamContext *oggstream = st->priv_data;
         if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
             st->codecpar->codec_id == AV_CODEC_ID_SPEEX ||
-            st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
+            st->codecpar->codec_id == AV_CODEC_ID_OPUS ||
+            st->codecpar->codec_id == AV_CODEC_ID_VP8) {
             av_freep(&oggstream->header[0]);
         }
         av_freep(&oggstream->header[1]);
@@ -683,7 +761,7 @@ AVOutputFormat ff_ogg_muxer = {
     .write_header      = ogg_write_header,
     .write_packet      = ogg_write_packet,
     .write_trailer     = ogg_write_trailer,
-    .flags             = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
+    .flags             = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
     .priv_class        = &ogg_muxer_class,
 };
 #endif
diff --git a/tests/fate/avformat.mak b/tests/fate/avformat.mak
index bcfe2e3..3f69d00 100644
--- a/tests/fate/avformat.mak
+++ b/tests/fate/avformat.mak
@@ -67,6 +67,7 @@ FATE_AVCONV += $(FATE_LAVF)
 fate-lavf:     $(FATE_LAVF)
 
 FATE_LAVF_FATE-$(call ALLYES, MATROSKA_DEMUXER   OGG_MUXER)          += ogg_vp3
+FATE_LAVF_FATE-$(call ALLYES, MATROSKA_DEMUXER   OGG_MUXER)          += ogg_vp8
 FATE_LAVF_FATE-$(call ALLYES, MOV_DEMUXER        LATM_MUXER)         += latm
 FATE_LAVF_FATE-$(call ALLYES, MP3_DEMUXER        MP3_MUXER)          += mp3
 FATE_LAVF_FATE-$(call ALLYES, MOV_DEMUXER        MOV_MUXER)          += mov_qtrle_mace6
diff --git a/tests/lavf-regression.sh b/tests/lavf-regression.sh
index f390dd9..0e20513 100755
--- a/tests/lavf-regression.sh
+++ b/tests/lavf-regression.sh
@@ -165,6 +165,10 @@ DEC_OPTS="$DEC_OPTS -idct auto"
 do_lavf_fate ogg "vp3/coeff_level64.mkv"
 fi
 
+if [ -n "$do_ogg_vp8" ] ; then
+do_lavf_fate ogv "vp8/RRSF49-short.webm" "-acodec copy"
+fi
+
 if [ -n "$do_mov_qtrle_mace6" ] ; then
 DEC_OPTS="$DEC_OPTS -idct auto"
 do_lavf_fate mov "qtrle/Animation-16Greys.mov"
diff --git a/tests/ref/lavf-fate/ogg_vp8 b/tests/ref/lavf-fate/ogg_vp8
new file mode 100644
index 0000000..78131f3
--- /dev/null
+++ b/tests/ref/lavf-fate/ogg_vp8
@@ -0,0 +1,3 @@
+c56d8dce728d46d4f0ab4c7cc9f86abc *./tests/data/lavf-fate/lavf.ogv
+95009 ./tests/data/lavf-fate/lavf.ogv
+./tests/data/lavf-fate/lavf.ogv CRC=0x8c067a66
-- 
2.9.0



More information about the ffmpeg-devel mailing list