2 * On2 VP8 parser for Ogg
3 * Copyright (C) 2013 James Almer
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
27 #define VP8_HEADER_SIZE 26
29 static int vp8_header(AVFormatContext
*s
, int idx
)
31 struct ogg
*ogg
= s
->priv_data
;
32 struct ogg_stream
*os
= ogg
->streams
+ idx
;
33 uint8_t *p
= os
->buf
+ os
->pstart
;
34 AVStream
*st
= s
->streams
[idx
];
37 if (os
->psize
< 7 || p
[0] != 0x4f)
42 if (os
->psize
< VP8_HEADER_SIZE
) {
43 av_log(s
, AV_LOG_ERROR
, "Invalid OggVP8 header packet");
44 return AVERROR_INVALIDDATA
;
48 av_log(s
, AV_LOG_WARNING
, "Unknown OggVP8 version %d.%d\n", p
[6], p
[7]);
49 return AVERROR_INVALIDDATA
;
52 st
->codec
->width
= AV_RB16(p
+ 8);
53 st
->codec
->height
= AV_RB16(p
+ 10);
54 st
->sample_aspect_ratio
.num
= AV_RB24(p
+ 12);
55 st
->sample_aspect_ratio
.den
= AV_RB24(p
+ 15);
56 framerate
.den
= AV_RB32(p
+ 18);
57 framerate
.num
= AV_RB32(p
+ 22);
59 avpriv_set_pts_info(st
, 64, framerate
.num
, framerate
.den
);
60 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
61 st
->codec
->codec_id
= AV_CODEC_ID_VP8
;
62 st
->need_parsing
= AVSTREAM_PARSE_HEADERS
;
66 return AVERROR_INVALIDDATA
;
67 ff_vorbis_stream_comment(s
, st
, p
+ 7, os
->psize
- 7);
70 av_log(s
, AV_LOG_ERROR
, "Unknown VP8 header type 0x%02X\n", p
[5]);
71 return AVERROR_INVALIDDATA
;
77 static uint64_t vp8_gptopts(AVFormatContext
*s
, int idx
, uint64_t granule
, int64_t *dts
)
79 struct ogg
*ogg
= s
->priv_data
;
80 struct ogg_stream
*os
= ogg
->streams
+ idx
;
82 uint64_t pts
= (granule
>> 32);
83 uint32_t dist
= (granule
>> 3) & 0x07ffffff;
86 os
->pflags
|= AV_PKT_FLAG_KEY
;
94 static int vp8_packet(AVFormatContext
*s
, int idx
)
96 struct ogg
*ogg
= s
->priv_data
;
97 struct ogg_stream
*os
= ogg
->streams
+ idx
;
98 uint8_t *p
= os
->buf
+ os
->pstart
;
100 if ((!os
->lastpts
|| os
->lastpts
== AV_NOPTS_VALUE
) && !(os
->flags
& OGG_FLAG_EOS
)) {
103 uint8_t *last_pkt
= p
;
107 duration
= (last_pkt
[0] >> 4) & 1;
108 next_pkt
= last_pkt
+= os
->psize
;
109 for (; seg
< os
->nsegs
; seg
++) {
110 if (os
->segments
[seg
] < 255) {
111 duration
+= (last_pkt
[0] >> 4) & 1;
112 last_pkt
= next_pkt
+ os
->segments
[seg
];
114 next_pkt
+= os
->segments
[seg
];
116 os
->lastpts
= os
->lastdts
= vp8_gptopts(s
, idx
, os
->granule
, NULL
) - duration
;
117 if(s
->streams
[idx
]->start_time
== AV_NOPTS_VALUE
) {
118 s
->streams
[idx
]->start_time
= os
->lastpts
;
119 if (s
->streams
[idx
]->duration
)
120 s
->streams
[idx
]->duration
-= s
->streams
[idx
]->start_time
;
125 os
->pduration
= (p
[0] >> 4) & 1;
130 const struct ogg_codec ff_vp8_codec
= {
133 .header
= vp8_header
,
134 .packet
= vp8_packet
,
135 .gptopts
= vp8_gptopts
,