2013-12-25 05:48:34 +00:00
|
|
|
/*
|
|
|
|
* On2 VP8 parser for Ogg
|
|
|
|
* Copyright (C) 2013 James Almer
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2014-12-12 20:57:43 +00:00
|
|
|
|
2013-12-25 05:48:34 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "oggdec.h"
|
|
|
|
|
|
|
|
#define VP8_HEADER_SIZE 26
|
|
|
|
|
|
|
|
static int vp8_header(AVFormatContext *s, int idx)
|
|
|
|
{
|
|
|
|
struct ogg *ogg = s->priv_data;
|
|
|
|
struct ogg_stream *os = ogg->streams + idx;
|
|
|
|
uint8_t *p = os->buf + os->pstart;
|
|
|
|
AVStream *st = s->streams[idx];
|
|
|
|
AVRational framerate;
|
|
|
|
|
|
|
|
if (os->psize < 7 || p[0] != 0x4f)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (p[5]){
|
|
|
|
case 0x01:
|
|
|
|
if (os->psize < VP8_HEADER_SIZE) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid OggVP8 header packet");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p[6] != 1) {
|
2014-12-12 20:57:43 +00:00
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"Unknown OggVP8 version %d.%d\n", p[6], p[7]);
|
2013-12-25 05:48:34 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->width = AV_RB16(p + 8);
|
|
|
|
st->codecpar->height = AV_RB16(p + 10);
|
2013-12-25 05:48:34 +00:00
|
|
|
st->sample_aspect_ratio.num = AV_RB24(p + 12);
|
|
|
|
st->sample_aspect_ratio.den = AV_RB24(p + 15);
|
2014-12-12 20:57:43 +00:00
|
|
|
framerate.num = AV_RB32(p + 18);
|
|
|
|
framerate.den = AV_RB32(p + 22);
|
2013-12-25 05:48:34 +00:00
|
|
|
|
2014-12-12 20:57:43 +00:00
|
|
|
avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_VP8;
|
2013-12-25 05:48:34 +00:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_HEADERS;
|
|
|
|
break;
|
|
|
|
case 0x02:
|
|
|
|
if (p[6] != 0x20)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2014-08-14 03:31:00 +00:00
|
|
|
ff_vorbis_stream_comment(s, st, p + 7, os->psize - 7);
|
2013-12-25 05:48:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unknown VP8 header type 0x%02X\n", p[5]);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-12 20:57:43 +00:00
|
|
|
static uint64_t vp8_gptopts(AVFormatContext *s, int idx,
|
|
|
|
uint64_t granule, int64_t *dts)
|
2013-12-25 05:48:34 +00:00
|
|
|
{
|
|
|
|
struct ogg *ogg = s->priv_data;
|
|
|
|
struct ogg_stream *os = ogg->streams + idx;
|
|
|
|
|
|
|
|
uint64_t pts = (granule >> 32);
|
|
|
|
uint32_t dist = (granule >> 3) & 0x07ffffff;
|
|
|
|
|
|
|
|
if (!dist)
|
|
|
|
os->pflags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
|
|
|
if (dts)
|
|
|
|
*dts = pts;
|
|
|
|
|
|
|
|
return pts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vp8_packet(AVFormatContext *s, int idx)
|
|
|
|
{
|
|
|
|
struct ogg *ogg = s->priv_data;
|
|
|
|
struct ogg_stream *os = ogg->streams + idx;
|
|
|
|
uint8_t *p = os->buf + os->pstart;
|
|
|
|
|
2014-12-12 20:57:43 +00:00
|
|
|
if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) &&
|
|
|
|
!(os->flags & OGG_FLAG_EOS)) {
|
2013-12-25 05:48:34 +00:00
|
|
|
int seg;
|
|
|
|
int duration;
|
|
|
|
uint8_t *last_pkt = p;
|
|
|
|
uint8_t *next_pkt;
|
|
|
|
|
|
|
|
seg = os->segp;
|
|
|
|
duration = (last_pkt[0] >> 4) & 1;
|
|
|
|
next_pkt = last_pkt += os->psize;
|
|
|
|
for (; seg < os->nsegs; seg++) {
|
|
|
|
if (os->segments[seg] < 255) {
|
|
|
|
duration += (last_pkt[0] >> 4) & 1;
|
|
|
|
last_pkt = next_pkt + os->segments[seg];
|
|
|
|
}
|
|
|
|
next_pkt += os->segments[seg];
|
|
|
|
}
|
2014-12-12 20:57:43 +00:00
|
|
|
os->lastpts =
|
|
|
|
os->lastdts = vp8_gptopts(s, idx, os->granule, NULL) - duration;
|
2013-12-25 05:48:34 +00:00
|
|
|
if(s->streams[idx]->start_time == AV_NOPTS_VALUE) {
|
|
|
|
s->streams[idx]->start_time = os->lastpts;
|
|
|
|
if (s->streams[idx]->duration)
|
|
|
|
s->streams[idx]->duration -= s->streams[idx]->start_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (os->psize > 0)
|
|
|
|
os->pduration = (p[0] >> 4) & 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ogg_codec ff_vp8_codec = {
|
|
|
|
.magic = "OVP80",
|
|
|
|
.magicsize = 5,
|
|
|
|
.header = vp8_header,
|
|
|
|
.packet = vp8_packet,
|
|
|
|
.gptopts = vp8_gptopts,
|
|
|
|
.nb_header = 1,
|
|
|
|
};
|