FFmpeg
oggparseopus.c
Go to the documentation of this file.
1 /*
2  * Opus parser for Ogg
3  * Copyright (c) 2012 Nicolas George
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include <string.h>
23 
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "oggdec.h"
29 
32  unsigned pre_skip;
34 };
35 
36 #define OPUS_SEEK_PREROLL_MS 80
37 #define OPUS_HEAD_SIZE 19
38 
39 static int opus_header(AVFormatContext *avf, int idx)
40 {
41  struct ogg *ogg = avf->priv_data;
42  struct ogg_stream *os = &ogg->streams[idx];
43  AVStream *st = avf->streams[idx];
44  struct oggopus_private *priv = os->private;
45  uint8_t *packet = os->buf + os->pstart;
46  int ret;
47 
48  if (!priv) {
49  priv = os->private = av_mallocz(sizeof(*priv));
50  if (!priv)
51  return AVERROR(ENOMEM);
52  }
53 
54  if (os->flags & OGG_FLAG_BOS) {
55  if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
56  return AVERROR_INVALIDDATA;
59  st->codecpar->ch_layout.nb_channels = AV_RL8(packet + 9);
60 
61  priv->pre_skip = AV_RL16(packet + 10);
62  st->codecpar->initial_padding = priv->pre_skip;
63  os->start_trimming = priv->pre_skip;
64  /*orig_sample_rate = AV_RL32(packet + 12);*/
65  /*gain = AV_RL16(packet + 16);*/
66  /*channel_map = AV_RL8 (packet + 18);*/
67 
68  if ((ret = ff_alloc_extradata(st->codecpar, os->psize)) < 0)
69  return ret;
70 
71  memcpy(st->codecpar->extradata, packet, os->psize);
72 
73  st->codecpar->sample_rate = 48000;
75  st->codecpar->sample_rate, 1000);
76  avpriv_set_pts_info(st, 64, 1, 48000);
77  priv->need_comments = 1;
78  return 1;
79  }
80 
81  if (priv->need_comments) {
82  if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
83  return AVERROR_INVALIDDATA;
84  ff_vorbis_stream_comment(avf, st, packet + 8, os->psize - 8);
85  priv->need_comments--;
86  return 1;
87  }
88 
89  return 0;
90 }
91 
92 static int opus_duration(uint8_t *src, int size)
93 {
94  unsigned nb_frames = 1;
95  unsigned toc = src[0];
96  unsigned toc_config = toc >> 3;
97  unsigned toc_count = toc & 3;
98  unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
99  toc_config < 16 ? 480 << (toc_config & 1) :
100  120 << (toc_config & 3);
101  if (toc_count == 3) {
102  if (size<2)
103  return AVERROR_INVALIDDATA;
104  nb_frames = src[1] & 0x3F;
105  } else if (toc_count) {
106  nb_frames = 2;
107  }
108 
109  return frame_size * nb_frames;
110 }
111 
112 static int opus_packet(AVFormatContext *avf, int idx)
113 {
114  struct ogg *ogg = avf->priv_data;
115  struct ogg_stream *os = &ogg->streams[idx];
116  AVStream *st = avf->streams[idx];
117  struct oggopus_private *priv = os->private;
118  uint8_t *packet = os->buf + os->pstart;
119  int ret;
120 
121  if (!os->psize)
122  return AVERROR_INVALIDDATA;
123  if (os->granule > (1LL << 62)) {
124  av_log(avf, AV_LOG_ERROR, "Unsupported huge granule pos %"PRId64 "\n", os->granule);
125  return AVERROR_INVALIDDATA;
126  }
127 
128  if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
129  int seg, d;
130  int duration;
131  uint8_t *last_pkt = os->buf + os->pstart;
132  uint8_t *next_pkt = last_pkt;
133 
134  duration = 0;
135  seg = os->segp;
136  d = opus_duration(last_pkt, os->psize);
137  if (d < 0) {
139  return 0;
140  }
141  duration += d;
142  last_pkt = next_pkt = next_pkt + os->psize;
143  for (; seg < os->nsegs; seg++) {
144  next_pkt += os->segments[seg];
145  if (os->segments[seg] < 255 && next_pkt != last_pkt) {
146  int d = opus_duration(last_pkt, next_pkt - last_pkt);
147  if (d > 0)
148  duration += d;
149  last_pkt = next_pkt;
150  }
151  }
152  os->lastpts =
153  os->lastdts = os->granule - duration;
154  }
155 
156  if ((ret = opus_duration(packet, os->psize)) < 0)
157  return ret;
158 
159  os->pduration = ret;
160  if (os->lastpts != AV_NOPTS_VALUE) {
161  if (st->start_time == AV_NOPTS_VALUE)
162  st->start_time = os->lastpts;
163  priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
164  }
165 
166  priv->cur_dts += os->pduration;
167  if ((os->flags & OGG_FLAG_EOS)) {
168  int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
169  skip = FFMIN(skip, os->pduration);
170  if (skip > 0) {
171  os->pduration = skip < os->pduration ? os->pduration - skip : 1;
172  os->end_trimming = skip;
173  av_log(avf, AV_LOG_DEBUG,
174  "Last packet was truncated to %d due to end trimming.\n",
175  os->pduration);
176  }
177  }
178 
179  return 0;
180 }
181 
182 const struct ogg_codec ff_opus_codec = {
183  .name = "Opus",
184  .magic = "OpusHead",
185  .magicsize = 8,
186  .header = opus_header,
187  .packet = opus_packet,
188  .nb_header = 1,
189 };
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
ogg_stream::segp
int segp
Definition: oggdec.h:78
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ogg_stream::lastpts
int64_t lastpts
Definition: oggdec.h:71
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
int64_t
long long int64_t
Definition: coverity.c:34
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1368
ogg_stream::granule
uint64_t granule
Definition: oggdec.h:69
AVCodecParameters::seek_preroll
int seek_preroll
Audio only.
Definition: codec_par.h:214
ogg_stream::start_trimming
int start_trimming
set the number of packets to drop from the start
Definition: oggdec.h:86
ogg_stream::buf
uint8_t * buf
Definition: oggdec.h:61
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
ogg_stream::nsegs
int nsegs
Definition: oggdec.h:78
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
AV_RL8
#define AV_RL8(x)
Definition: intreadwrite.h:394
ogg
Definition: oggdec.h:101
OPUS_SEEK_PREROLL_MS
#define OPUS_SEEK_PREROLL_MS
Definition: oggparseopus.c:36
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:867
oggopus_private::cur_dts
int64_t cur_dts
Definition: oggparseopus.c:33
ogg_stream::lastdts
int64_t lastdts
Definition: oggdec.h:72
ff_opus_codec
const struct ogg_codec ff_opus_codec
Definition: oggparseopus.c:182
ogg_stream::pstart
unsigned int pstart
Definition: oggdec.h:64
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:595
duration
int64_t duration
Definition: movenc.c:65
OPUS_HEAD_SIZE
#define OPUS_HEAD_SIZE
Definition: oggparseopus.c:37
intreadwrite.h
frame_size
int frame_size
Definition: mxfenc.c:2429
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
oggopus_private::pre_skip
unsigned pre_skip
Definition: oggparseopus.c:32
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
ogg_stream::flags
int flags
Definition: oggdec.h:75
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
ogg::streams
struct ogg_stream * streams
Definition: oggdec.h:102
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
ogg_stream::private
void * private
Definition: oggdec.h:90
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
ogg_codec::name
const int8_t * name
Definition: oggdec.h:33
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:506
oggopus_private::need_comments
int need_comments
Definition: oggparseopus.c:31
oggopus_private
Definition: oggparseopus.c:30
ogg_stream::pflags
unsigned int pflags
Definition: oggdec.h:66
ogg_stream
Definition: oggdec.h:60
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avformat.h
oggdec.h
ff_vorbis_stream_comment
int ff_vorbis_stream_comment(AVFormatContext *as, AVStream *st, const uint8_t *buf, int size)
Parse Vorbis comments and add metadata to an AVStream.
Definition: oggparsevorbis.c:74
ogg_stream::segments
uint8_t segments[255]
Definition: oggdec.h:79
mem.h
OGG_FLAG_EOS
#define OGG_FLAG_EOS
Definition: oggdec.h:112
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
ogg_stream::psize
unsigned int psize
Definition: oggdec.h:65
OGG_FLAG_BOS
#define OGG_FLAG_BOS
Definition: oggdec.h:111
ogg_codec
Copyright (C) 2005 Michael Ahlberg, Måns Rullgård.
Definition: oggdec.h:30
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
opus_packet
static int opus_packet(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:112
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:797
ogg_stream::end_trimming
int end_trimming
set the number of packets to drop from the end
Definition: oggdec.h:87
opus_duration
static int opus_duration(uint8_t *src, int size)
Definition: oggparseopus.c:92
ogg_stream::pduration
unsigned int pduration
Definition: oggdec.h:67
AVCodecParameters::initial_padding
int initial_padding
Audio only.
Definition: codec_par.h:203
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
opus_header
static int opus_header(AVFormatContext *avf, int idx)
Definition: oggparseopus.c:39
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:375
src
#define src
Definition: vp8dsp.c:248
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:227