2012-06-24 09:38:18 +00:00
|
|
|
/*
|
|
|
|
* Opus parser for Ogg
|
|
|
|
* Copyright (c) 2012 Nicolas George
|
|
|
|
*
|
|
|
|
* 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 <string.h>
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "oggdec.h"
|
|
|
|
|
|
|
|
struct oggopus_private {
|
|
|
|
int need_comments;
|
|
|
|
unsigned pre_skip;
|
|
|
|
int64_t cur_dts;
|
|
|
|
};
|
|
|
|
|
2014-02-05 15:36:13 +00:00
|
|
|
#define OPUS_SEEK_PREROLL_MS 80
|
2012-06-24 09:38:18 +00:00
|
|
|
#define OPUS_HEAD_SIZE 19
|
|
|
|
|
|
|
|
static int opus_header(AVFormatContext *avf, int idx)
|
|
|
|
{
|
2012-06-24 09:38:18 +00:00
|
|
|
struct ogg *ogg = avf->priv_data;
|
|
|
|
struct ogg_stream *os = &ogg->streams[idx];
|
|
|
|
AVStream *st = avf->streams[idx];
|
2012-06-24 09:38:18 +00:00
|
|
|
struct oggopus_private *priv = os->private;
|
2012-06-24 09:38:18 +00:00
|
|
|
uint8_t *packet = os->buf + os->pstart;
|
2012-06-24 09:38:18 +00:00
|
|
|
|
|
|
|
if (!priv) {
|
|
|
|
priv = os->private = av_mallocz(sizeof(*priv));
|
|
|
|
if (!priv)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2013-10-13 07:07:59 +00:00
|
|
|
|
2012-06-24 09:38:18 +00:00
|
|
|
if (os->flags & OGG_FLAG_BOS) {
|
|
|
|
if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
|
|
|
|
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->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_OPUS;
|
|
|
|
st->codecpar->channels = AV_RL8(packet + 9);
|
2016-04-10 19:58:15 +00:00
|
|
|
|
2012-06-24 09:38:18 +00:00
|
|
|
priv->pre_skip = AV_RL16(packet + 10);
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->initial_padding = priv->pre_skip;
|
2013-10-13 07:07:59 +00:00
|
|
|
/*orig_sample_rate = AV_RL32(packet + 12);*/
|
|
|
|
/*gain = AV_RL16(packet + 16);*/
|
|
|
|
/*channel_map = AV_RL8 (packet + 18);*/
|
2012-06-24 09:38:18 +00:00
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
if (ff_alloc_extradata(st->codecpar, os->psize))
|
2012-06-24 09:38:18 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2012-06-24 09:38:18 +00:00
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
memcpy(st->codecpar->extradata, packet, os->psize);
|
2012-06-24 09:38:18 +00:00
|
|
|
|
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->sample_rate = 48000;
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->seek_preroll = av_rescale(OPUS_SEEK_PREROLL_MS,
|
|
|
|
st->codecpar->sample_rate, 1000);
|
2012-06-24 09:38:18 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, 48000);
|
|
|
|
priv->need_comments = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->need_comments) {
|
|
|
|
if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
|
|
|
|
return AVERROR_INVALIDDATA;
|
2014-08-12 21:03:55 +00:00
|
|
|
ff_vorbis_stream_comment(avf, st, packet + 8, os->psize - 8);
|
2012-06-24 09:38:18 +00:00
|
|
|
priv->need_comments--;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-06-24 09:38:18 +00:00
|
|
|
|
2012-06-24 09:38:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-11 03:15:38 +00:00
|
|
|
static int opus_duration(uint8_t *src, int size)
|
|
|
|
{
|
|
|
|
unsigned nb_frames = 1;
|
|
|
|
unsigned toc = src[0];
|
|
|
|
unsigned toc_config = toc >> 3;
|
|
|
|
unsigned toc_count = toc & 3;
|
|
|
|
unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
|
|
|
|
toc_config < 16 ? 480 << (toc_config & 1) :
|
|
|
|
120 << (toc_config & 3);
|
|
|
|
if (toc_count == 3) {
|
|
|
|
if (size<2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
nb_frames = src[1] & 0x3F;
|
|
|
|
} else if (toc_count) {
|
|
|
|
nb_frames = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame_size * nb_frames;
|
|
|
|
}
|
|
|
|
|
2012-06-24 09:38:18 +00:00
|
|
|
static int opus_packet(AVFormatContext *avf, int idx)
|
|
|
|
{
|
2012-06-24 09:38:18 +00:00
|
|
|
struct ogg *ogg = avf->priv_data;
|
|
|
|
struct ogg_stream *os = &ogg->streams[idx];
|
|
|
|
AVStream *st = avf->streams[idx];
|
2012-06-24 09:38:18 +00:00
|
|
|
struct oggopus_private *priv = os->private;
|
2012-06-24 09:38:18 +00:00
|
|
|
uint8_t *packet = os->buf + os->pstart;
|
2013-12-12 06:34:19 +00:00
|
|
|
int ret;
|
2012-06-24 09:38:18 +00:00
|
|
|
|
|
|
|
if (!os->psize)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-05-27 22:50:25 +00:00
|
|
|
if (os->granule > (1LL << 62)) {
|
2016-05-10 21:12:58 +00:00
|
|
|
av_log(avf, AV_LOG_ERROR, "Unsupported huge granule pos %"PRId64 "\n", os->granule);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2012-06-24 09:38:18 +00:00
|
|
|
|
2013-12-11 03:38:40 +00:00
|
|
|
if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
|
|
|
|
int seg, d;
|
|
|
|
int duration;
|
|
|
|
uint8_t *last_pkt = os->buf + os->pstart;
|
|
|
|
uint8_t *next_pkt = last_pkt;
|
|
|
|
|
|
|
|
duration = 0;
|
|
|
|
seg = os->segp;
|
|
|
|
d = opus_duration(last_pkt, os->psize);
|
|
|
|
if (d < 0) {
|
|
|
|
os->pflags |= AV_PKT_FLAG_CORRUPT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
duration += d;
|
|
|
|
last_pkt = next_pkt = next_pkt + os->psize;
|
|
|
|
for (; seg < os->nsegs; seg++) {
|
|
|
|
next_pkt += os->segments[seg];
|
2013-12-31 19:04:54 +00:00
|
|
|
if (os->segments[seg] < 255 && next_pkt != last_pkt) {
|
|
|
|
int d = opus_duration(last_pkt, next_pkt - last_pkt);
|
|
|
|
if (d > 0)
|
|
|
|
duration += d;
|
|
|
|
last_pkt = next_pkt;
|
|
|
|
}
|
2013-12-11 03:38:40 +00:00
|
|
|
}
|
|
|
|
os->lastpts =
|
|
|
|
os->lastdts = os->granule - duration;
|
|
|
|
}
|
2012-06-24 09:38:18 +00:00
|
|
|
|
2013-12-12 06:34:19 +00:00
|
|
|
if ((ret = opus_duration(packet, os->psize)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
os->pduration = ret;
|
2012-06-24 09:38:18 +00:00
|
|
|
if (os->lastpts != AV_NOPTS_VALUE) {
|
|
|
|
if (st->start_time == AV_NOPTS_VALUE)
|
|
|
|
st->start_time = os->lastpts;
|
|
|
|
priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
|
|
|
|
}
|
2012-06-24 09:38:18 +00:00
|
|
|
|
2012-06-24 09:38:18 +00:00
|
|
|
priv->cur_dts += os->pduration;
|
|
|
|
if ((os->flags & OGG_FLAG_EOS)) {
|
|
|
|
int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
|
|
|
|
skip = FFMIN(skip, os->pduration);
|
|
|
|
if (skip > 0) {
|
|
|
|
os->pduration = skip < os->pduration ? os->pduration - skip : 1;
|
2013-09-12 17:24:18 +00:00
|
|
|
os->end_trimming = skip;
|
2013-09-16 20:47:21 +00:00
|
|
|
av_log(avf, AV_LOG_DEBUG,
|
|
|
|
"Last packet was truncated to %d due to end trimming.\n",
|
2012-06-24 09:38:18 +00:00
|
|
|
os->pduration);
|
|
|
|
}
|
|
|
|
}
|
2012-06-24 09:38:18 +00:00
|
|
|
|
2012-06-24 09:38:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ogg_codec ff_opus_codec = {
|
2012-06-24 09:38:18 +00:00
|
|
|
.name = "Opus",
|
|
|
|
.magic = "OpusHead",
|
|
|
|
.magicsize = 8,
|
|
|
|
.header = opus_header,
|
|
|
|
.packet = opus_packet,
|
2013-10-13 16:24:08 +00:00
|
|
|
.nb_header = 1,
|
2012-06-24 09:38:18 +00:00
|
|
|
};
|