Browse code

oggdec: add support for VP8 demuxing

Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

James Almer authored on 2013/12/25 14:48:34
Showing 6 changed files
... ...
@@ -16,6 +16,7 @@ version <next>
16 16
 - stereoscopic 3d metadata handling
17 17
 - WebP encoding via libwebp
18 18
 - ATRAC3+ decoder
19
+- VP8 in Ogg demuxing
19 20
 
20 21
 
21 22
 version 2.1:
... ...
@@ -261,6 +261,7 @@ OBJS-$(CONFIG_OGG_DEMUXER)               += oggdec.o         \
261 261
                                             oggparsespeex.o  \
262 262
                                             oggparsetheora.o \
263 263
                                             oggparsevorbis.o \
264
+                                            oggparsevp8.o    \
264 265
                                             vorbiscomment.o  \
265 266
                                             flac_picture.o
266 267
 OBJS-$(CONFIG_OGG_MUXER)                 += oggenc.o \
... ...
@@ -48,6 +48,7 @@ static const struct ogg_codec * const ogg_codecs[] = {
48 48
     &ff_flac_codec,
49 49
     &ff_celt_codec,
50 50
     &ff_opus_codec,
51
+    &ff_vp8_codec,
51 52
     &ff_old_dirac_codec,
52 53
     &ff_old_flac_codec,
53 54
     &ff_ogm_video_codec,
... ...
@@ -125,6 +125,7 @@ extern const struct ogg_codec ff_skeleton_codec;
125 125
 extern const struct ogg_codec ff_speex_codec;
126 126
 extern const struct ogg_codec ff_theora_codec;
127 127
 extern const struct ogg_codec ff_vorbis_codec;
128
+extern const struct ogg_codec ff_vp8_codec;
128 129
 
129 130
 int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size);
130 131
 
131 132
new file mode 100644
... ...
@@ -0,0 +1,137 @@
0
+/*
1
+ * On2 VP8 parser for Ogg
2
+ * Copyright (C) 2013 James Almer
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/intreadwrite.h"
22
+#include "avformat.h"
23
+#include "internal.h"
24
+#include "oggdec.h"
25
+
26
+#define VP8_HEADER_SIZE 26
27
+
28
+static int vp8_header(AVFormatContext *s, int idx)
29
+{
30
+    struct ogg *ogg = s->priv_data;
31
+    struct ogg_stream *os = ogg->streams + idx;
32
+    uint8_t *p = os->buf + os->pstart;
33
+    AVStream *st = s->streams[idx];
34
+    AVRational framerate;
35
+
36
+    if (os->psize < 7 || p[0] != 0x4f)
37
+        return 0;
38
+
39
+    switch (p[5]){
40
+    case 0x01:
41
+        if (os->psize < VP8_HEADER_SIZE) {
42
+            av_log(s, AV_LOG_ERROR, "Invalid OggVP8 header packet");
43
+            return AVERROR_INVALIDDATA;
44
+        }
45
+
46
+        if (p[6] != 1) {
47
+            av_log(s, AV_LOG_WARNING, "Unknown OggVP8 version %d.%d\n", p[6], p[7]);
48
+            return AVERROR_INVALIDDATA;
49
+        }
50
+
51
+        st->codec->width            = AV_RB16(p +  8);
52
+        st->codec->height           = AV_RB16(p + 10);
53
+        st->sample_aspect_ratio.num = AV_RB24(p + 12);
54
+        st->sample_aspect_ratio.den = AV_RB24(p + 15);
55
+        framerate.den               = AV_RB32(p + 18);
56
+        framerate.num               = AV_RB32(p + 22);
57
+
58
+        avpriv_set_pts_info(st, 64, framerate.num, framerate.den);
59
+        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
60
+        st->codec->codec_id   = AV_CODEC_ID_VP8;
61
+        st->need_parsing      = AVSTREAM_PARSE_HEADERS;
62
+        break;
63
+    case 0x02:
64
+        if (p[6] != 0x20)
65
+            return AVERROR_INVALIDDATA;
66
+        ff_vorbis_comment(s, &st->metadata, p + 7, os->psize - 7);
67
+        break;
68
+    default:
69
+        av_log(s, AV_LOG_ERROR, "Unknown VP8 header type 0x%02X\n", p[5]);
70
+        return AVERROR_INVALIDDATA;
71
+    }
72
+
73
+    return 1;
74
+}
75
+
76
+static uint64_t vp8_gptopts(AVFormatContext *s, int idx, uint64_t granule, int64_t *dts)
77
+{
78
+    struct ogg *ogg = s->priv_data;
79
+    struct ogg_stream *os = ogg->streams + idx;
80
+
81
+    uint64_t pts  = (granule >> 32);
82
+    uint32_t dist = (granule >>  3) & 0x07ffffff;
83
+
84
+    if (!dist)
85
+        os->pflags |= AV_PKT_FLAG_KEY;
86
+
87
+    if (dts)
88
+        *dts = pts;
89
+
90
+    return pts;
91
+}
92
+
93
+static int vp8_packet(AVFormatContext *s, int idx)
94
+{
95
+    struct ogg *ogg = s->priv_data;
96
+    struct ogg_stream *os = ogg->streams + idx;
97
+    uint8_t *p = os->buf + os->pstart;
98
+
99
+    if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
100
+        int seg;
101
+        int duration;
102
+        uint8_t *last_pkt = p;
103
+        uint8_t *next_pkt;
104
+
105
+        seg = os->segp;
106
+        duration = (last_pkt[0] >> 4) & 1;
107
+        next_pkt = last_pkt += os->psize;
108
+        for (; seg < os->nsegs; seg++) {
109
+            if (os->segments[seg] < 255) {
110
+                duration += (last_pkt[0] >> 4) & 1;
111
+                last_pkt  = next_pkt + os->segments[seg];
112
+            }
113
+            next_pkt += os->segments[seg];
114
+        }
115
+        os->lastpts = os->lastdts = vp8_gptopts(s, idx, os->granule, NULL) - duration;
116
+        if(s->streams[idx]->start_time == AV_NOPTS_VALUE) {
117
+            s->streams[idx]->start_time = os->lastpts;
118
+            if (s->streams[idx]->duration)
119
+                s->streams[idx]->duration -= s->streams[idx]->start_time;
120
+        }
121
+    }
122
+
123
+    if (os->psize > 0)
124
+        os->pduration = (p[0] >> 4) & 1;
125
+
126
+    return 0;
127
+}
128
+
129
+const struct ogg_codec ff_vp8_codec = {
130
+    .magic     = "OVP80",
131
+    .magicsize = 5,
132
+    .header    = vp8_header,
133
+    .packet    = vp8_packet,
134
+    .gptopts   = vp8_gptopts,
135
+    .nb_header = 1,
136
+};
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 55
33 33
 #define LIBAVFORMAT_VERSION_MINOR 22
34
-#define LIBAVFORMAT_VERSION_MICRO 100
34
+#define LIBAVFORMAT_VERSION_MICRO 101
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \