Browse code

avformat/oggenc: add vp8 muxing support

Addresses ticket #5687

Signed-off-by: James Almer <jamrial@gmail.com>

James Almer authored on 2016/07/21 10:29:54
Showing 5 changed files
... ...
@@ -6,6 +6,7 @@ version <next>:
6 6
 - tee protocol
7 7
 - Changed metadata print option to accept general urls
8 8
 - Alias muxer for Ogg Video (.ogv)
9
+- VP8 in Ogg muxing
9 10
 
10 11
 
11 12
 version 3.1:
... ...
@@ -54,6 +54,8 @@ typedef struct OGGStreamContext {
54 54
     int kfgshift;
55 55
     int64_t last_kf_pts;
56 56
     int vrev;
57
+    /* for VP8 granule */
58
+    int isvp8;
57 59
     int eos;
58 60
     unsigned page_count; ///< number of page buffered
59 61
     OGGPage page; ///< current page
... ...
@@ -146,7 +148,8 @@ static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
146 146
 
147 147
 static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
148 148
 {
149
-    return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
149
+    return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
150
+           (oggstream->isvp8    && !((granule >> 3) & 0x07ffffff));
150 151
 }
151 152
 
152 153
 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
154 154
     if (oggstream->kfgshift)
155 155
         return (granule>>oggstream->kfgshift) +
156 156
             (granule & ((1<<oggstream->kfgshift)-1));
157
+    else if (oggstream->isvp8)
158
+        return granule >> 32;
157 159
     else
158 160
         return granule;
159 161
 }
... ...
@@ -219,11 +224,11 @@ static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
219 219
     int i, segments, len, flush = 0;
220 220
 
221 221
     // Handles VFR by flushing page because this frame needs to have a timestamp
222
-    // For theora, keyframes also need to have a timestamp to correctly mark
222
+    // For theora and VP8, keyframes also need to have a timestamp to correctly mark
223 223
     // them as such, otherwise seeking will not work correctly at the very
224 224
     // least with old libogg versions.
225 225
     // Do not try to flush header packets though, that will create broken files.
226
-    if (st->codecpar->codec_id == AV_CODEC_ID_THEORA && !header &&
226
+    if ((st->codecpar->codec_id == AV_CODEC_ID_THEORA || st->codecpar->codec_id == AV_CODEC_ID_VP8) && !header &&
227 227
         (ogg_granule_to_timestamp(oggstream, granule) >
228 228
          ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
229 229
          ogg_key_granule(oggstream, granule))) {
... ...
@@ -405,6 +410,57 @@ static int ogg_build_opus_headers(AVCodecParameters *par,
405 405
     return 0;
406 406
 }
407 407
 
408
+#define VP8_HEADER_SIZE 26
409
+
410
+static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st,
411
+                                 OGGStreamContext *oggstream, int bitexact)
412
+{
413
+    AVCodecParameters *par = st->codecpar;
414
+    uint8_t *p;
415
+
416
+    /* first packet: VP8 header */
417
+    p = av_mallocz(VP8_HEADER_SIZE);
418
+    if (!p)
419
+        return AVERROR(ENOMEM);
420
+    oggstream->header[0] = p;
421
+    oggstream->header_len[0] = VP8_HEADER_SIZE;
422
+    bytestream_put_byte(&p, 0x4f); // HDRID
423
+    bytestream_put_buffer(&p, "VP80", 4); // Identifier
424
+    bytestream_put_byte(&p, 1); // HDRTYP
425
+    bytestream_put_byte(&p, 1); // VMAJ
426
+    bytestream_put_byte(&p, 0); // VMIN
427
+    bytestream_put_be16(&p, par->width);
428
+    bytestream_put_be16(&p, par->height);
429
+    bytestream_put_be24(&p, par->sample_aspect_ratio.num);
430
+    bytestream_put_be24(&p, par->sample_aspect_ratio.den);
431
+    if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
432
+        // OggVP8 requires pts to increase by 1 per visible frame, so use the least common
433
+        // multiple framerate if available.
434
+        av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
435
+               st->time_base.num, st->time_base.den,
436
+               st->r_frame_rate.den, st->r_frame_rate.num);
437
+        avpriv_set_pts_info(st, 64, st->r_frame_rate.den, st->r_frame_rate.num);
438
+    }
439
+    bytestream_put_be32(&p, st->time_base.den);
440
+    bytestream_put_be32(&p, st->time_base.num);
441
+
442
+    /* optional second packet: VorbisComment */
443
+    if (av_dict_get(st->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
444
+        p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0);
445
+        if (!p)
446
+            return AVERROR(ENOMEM);
447
+        oggstream->header[1] = p;
448
+        bytestream_put_byte(&p, 0x4f); // HDRID
449
+        bytestream_put_buffer(&p, "VP80", 4); // Identifier
450
+        bytestream_put_byte(&p, 2); // HDRTYP
451
+        bytestream_put_byte(&p, 0x20);
452
+    }
453
+
454
+    oggstream->isvp8 = 1;
455
+
456
+    return 0;
457
+}
458
+
408 459
 static void ogg_write_pages(AVFormatContext *s, int flush)
409 460
 {
410 461
     OGGContext *ogg = s->priv_data;
... ...
@@ -452,12 +508,14 @@ static int ogg_write_header(AVFormatContext *s)
452 452
             st->codecpar->codec_id != AV_CODEC_ID_THEORA &&
453 453
             st->codecpar->codec_id != AV_CODEC_ID_SPEEX  &&
454 454
             st->codecpar->codec_id != AV_CODEC_ID_FLAC   &&
455
-            st->codecpar->codec_id != AV_CODEC_ID_OPUS) {
455
+            st->codecpar->codec_id != AV_CODEC_ID_OPUS   &&
456
+            st->codecpar->codec_id != AV_CODEC_ID_VP8) {
456 457
             av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
457 458
             return AVERROR(EINVAL);
458 459
         }
459 460
 
460
-        if (!st->codecpar->extradata || !st->codecpar->extradata_size) {
461
+        if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
462
+            st->codecpar->codec_id != AV_CODEC_ID_VP8) {
461 463
             av_log(s, AV_LOG_ERROR, "No extradata present\n");
462 464
             return AVERROR_INVALIDDATA;
463 465
         }
... ...
@@ -508,6 +566,14 @@ static int ogg_write_header(AVFormatContext *s)
508 508
                 av_freep(&st->priv_data);
509 509
                 return err;
510 510
             }
511
+        } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
512
+            int err = ogg_build_vp8_headers(s, st, oggstream,
513
+                                            s->flags & AVFMT_FLAG_BITEXACT);
514
+            if (err) {
515
+                av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
516
+                av_freep(&st->priv_data);
517
+                return err;
518
+            }
511 519
         } else {
512 520
             uint8_t *p;
513 521
             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)
600 600
                   av_rescale_q(st->codecpar->initial_padding,
601 601
                                (AVRational){ 1, st->codecpar->sample_rate },
602 602
                                st->time_base);
603
-    else
603
+    else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
604
+        int64_t pts, invcnt, dist;
605
+        int visible;
606
+
607
+        visible = (pkt->data[0] >> 4) & 1;
608
+        pts     = pkt->pts + pkt->duration;
609
+        invcnt  = (oggstream->last_granule >> 30) & 3;
610
+        invcnt  = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
611
+        dist    = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
612
+
613
+        granule = (pts << 32) | (invcnt << 30) | (dist << 3);
614
+    } else
604 615
         granule = pkt->pts + pkt->duration;
605 616
 
606 617
     if (oggstream->page.start_granule == AV_NOPTS_VALUE)
... ...
@@ -653,7 +730,8 @@ static int ogg_write_trailer(AVFormatContext *s)
653 653
         OGGStreamContext *oggstream = st->priv_data;
654 654
         if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
655 655
             st->codecpar->codec_id == AV_CODEC_ID_SPEEX ||
656
-            st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
656
+            st->codecpar->codec_id == AV_CODEC_ID_OPUS ||
657
+            st->codecpar->codec_id == AV_CODEC_ID_VP8) {
657 658
             av_freep(&oggstream->header[0]);
658 659
         }
659 660
         av_freep(&oggstream->header[1]);
... ...
@@ -686,7 +764,7 @@ AVOutputFormat ff_ogg_muxer = {
686 686
     .write_header      = ogg_write_header,
687 687
     .write_packet      = ogg_write_packet,
688 688
     .write_trailer     = ogg_write_trailer,
689
-    .flags             = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
689
+    .flags             = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
690 690
     .priv_class        = &ogg_muxer_class,
691 691
 };
692 692
 #endif
... ...
@@ -718,11 +796,12 @@ AVOutputFormat ff_ogv_muxer = {
718 718
     .priv_data_size    = sizeof(OGGContext),
719 719
     .audio_codec       = CONFIG_LIBVORBIS_ENCODER ?
720 720
                          AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
721
-    .video_codec       = AV_CODEC_ID_THEORA,
721
+    .video_codec       = CONFIG_LIBTHEORA_ENCODER ?
722
+                         AV_CODEC_ID_THEORA : AV_CODEC_ID_VP8,
722 723
     .write_header      = ogg_write_header,
723 724
     .write_packet      = ogg_write_packet,
724 725
     .write_trailer     = ogg_write_trailer,
725
-    .flags             = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
726
+    .flags             = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
726 727
     .priv_class        = &ogv_muxer_class,
727 728
 };
728 729
 #endif
... ...
@@ -67,6 +67,7 @@ FATE_AVCONV += $(FATE_LAVF)
67 67
 fate-lavf:     $(FATE_LAVF)
68 68
 
69 69
 FATE_LAVF_FATE-$(call ALLYES, MATROSKA_DEMUXER   OGG_MUXER)          += ogg_vp3
70
+FATE_LAVF_FATE-$(call ALLYES, MATROSKA_DEMUXER   OGV_MUXER)          += ogg_vp8
70 71
 FATE_LAVF_FATE-$(call ALLYES, MOV_DEMUXER        LATM_MUXER)         += latm
71 72
 FATE_LAVF_FATE-$(call ALLYES, MP3_DEMUXER        MP3_MUXER)          += mp3
72 73
 FATE_LAVF_FATE-$(call ALLYES, MOV_DEMUXER        MOV_MUXER)          += mov_qtrle_mace6
... ...
@@ -165,6 +165,10 @@ DEC_OPTS="$DEC_OPTS -idct auto"
165 165
 do_lavf_fate ogg "vp3/coeff_level64.mkv"
166 166
 fi
167 167
 
168
+if [ -n "$do_ogg_vp8" ] ; then
169
+do_lavf_fate ogv "vp8/RRSF49-short.webm" "-acodec copy"
170
+fi
171
+
168 172
 if [ -n "$do_mov_qtrle_mace6" ] ; then
169 173
 DEC_OPTS="$DEC_OPTS -idct auto"
170 174
 do_lavf_fate mov "qtrle/Animation-16Greys.mov"
171 175
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+c56d8dce728d46d4f0ab4c7cc9f86abc *./tests/data/lavf-fate/lavf.ogv
1
+95009 ./tests/data/lavf-fate/lavf.ogv
2
+./tests/data/lavf-fate/lavf.ogv CRC=0x8c067a66