FFmpeg
parse.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Andrew D'Addesio
3  * Copyright (c) 2013-2014 Mozilla Corporation
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 /**
23  * @file
24  * Opus decoder/parser shared code
25  */
26 
27 #include "libavutil/attributes.h"
29 #include "libavutil/error.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/log.h"
32 #include "libavutil/mem.h"
33 
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "mathops.h"
37 #include "opus.h"
38 #include "parse.h"
39 #include "tab.h"
40 #include "vorbis_data.h"
41 
42 /**
43  * Read a 1- or 2-byte frame length
44  */
45 static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
46 {
47  int val;
48 
49  if (*ptr >= end)
50  return AVERROR_INVALIDDATA;
51  val = *(*ptr)++;
52  if (val >= 252) {
53  if (*ptr >= end)
54  return AVERROR_INVALIDDATA;
55  val += 4 * *(*ptr)++;
56  }
57  return val;
58 }
59 
60 /**
61  * Read a multi-byte length (used for code 3 packet padding size)
62  */
63 static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
64 {
65  int val = 0;
66  int next;
67 
68  while (1) {
69  if (*ptr >= end || val > INT_MAX - 254)
70  return AVERROR_INVALIDDATA;
71  next = *(*ptr)++;
72  val += next;
73  if (next < 255)
74  break;
75  else
76  val--;
77  }
78  return val;
79 }
80 
81 /**
82  * Parse Opus packet info from raw packet data
83  */
84 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
85  int self_delimiting)
86 {
87  const uint8_t *ptr = buf;
88  const uint8_t *end = buf + buf_size;
89  int padding = 0;
90  int frame_bytes, i;
91 
92  if (buf_size < 1)
93  goto fail;
94 
95  /* TOC byte */
96  i = *ptr++;
97  pkt->code = (i ) & 0x3;
98  pkt->stereo = (i >> 2) & 0x1;
99  pkt->config = (i >> 3) & 0x1F;
100 
101  /* code 2 and code 3 packets have at least 1 byte after the TOC */
102  if (pkt->code >= 2 && buf_size < 2)
103  goto fail;
104 
105  switch (pkt->code) {
106  case 0:
107  /* 1 frame */
108  pkt->frame_count = 1;
109  pkt->vbr = 0;
110 
111  if (self_delimiting) {
112  int len = xiph_lacing_16bit(&ptr, end);
113  if (len < 0 || len > end - ptr)
114  goto fail;
115  end = ptr + len;
116  buf_size = end - buf;
117  }
118 
119  frame_bytes = end - ptr;
120  if (frame_bytes > OPUS_MAX_FRAME_SIZE)
121  goto fail;
122  pkt->frame_offset[0] = ptr - buf;
123  pkt->frame_size[0] = frame_bytes;
124  break;
125  case 1:
126  /* 2 frames, equal size */
127  pkt->frame_count = 2;
128  pkt->vbr = 0;
129 
130  if (self_delimiting) {
131  int len = xiph_lacing_16bit(&ptr, end);
132  if (len < 0 || 2 * len > end - ptr)
133  goto fail;
134  end = ptr + 2 * len;
135  buf_size = end - buf;
136  }
137 
138  frame_bytes = end - ptr;
139  if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE)
140  goto fail;
141  pkt->frame_offset[0] = ptr - buf;
142  pkt->frame_size[0] = frame_bytes >> 1;
143  pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
144  pkt->frame_size[1] = frame_bytes >> 1;
145  break;
146  case 2:
147  /* 2 frames, different sizes */
148  pkt->frame_count = 2;
149  pkt->vbr = 1;
150 
151  /* read 1st frame size */
152  frame_bytes = xiph_lacing_16bit(&ptr, end);
153  if (frame_bytes < 0)
154  goto fail;
155 
156  if (self_delimiting) {
157  int len = xiph_lacing_16bit(&ptr, end);
158  if (len < 0 || len + frame_bytes > end - ptr)
159  goto fail;
160  end = ptr + frame_bytes + len;
161  buf_size = end - buf;
162  }
163 
164  pkt->frame_offset[0] = ptr - buf;
165  pkt->frame_size[0] = frame_bytes;
166 
167  /* calculate 2nd frame size */
168  frame_bytes = end - ptr - pkt->frame_size[0];
169  if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE)
170  goto fail;
171  pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
172  pkt->frame_size[1] = frame_bytes;
173  break;
174  case 3:
175  /* 1 to 48 frames, can be different sizes */
176  i = *ptr++;
177  pkt->frame_count = (i ) & 0x3F;
178  padding = (i >> 6) & 0x01;
179  pkt->vbr = (i >> 7) & 0x01;
180 
181  if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES)
182  goto fail;
183 
184  /* read padding size */
185  if (padding) {
186  padding = xiph_lacing_full(&ptr, end);
187  if (padding < 0)
188  goto fail;
189  }
190 
191  /* read frame sizes */
192  if (pkt->vbr) {
193  /* for VBR, all frames except the final one have their size coded
194  in the bitstream. the last frame size is implicit. */
195  int total_bytes = 0;
196  for (i = 0; i < pkt->frame_count - 1; i++) {
197  frame_bytes = xiph_lacing_16bit(&ptr, end);
198  if (frame_bytes < 0)
199  goto fail;
200  pkt->frame_size[i] = frame_bytes;
201  total_bytes += frame_bytes;
202  }
203 
204  if (self_delimiting) {
205  int len = xiph_lacing_16bit(&ptr, end);
206  if (len < 0 || len + total_bytes + padding > end - ptr)
207  goto fail;
208  end = ptr + total_bytes + len + padding;
209  buf_size = end - buf;
210  }
211 
212  frame_bytes = end - ptr - padding;
213  if (total_bytes > frame_bytes)
214  goto fail;
215  pkt->frame_offset[0] = ptr - buf;
216  for (i = 1; i < pkt->frame_count; i++)
217  pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
218  pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
219  } else {
220  /* for CBR, the remaining packet bytes are divided evenly between
221  the frames */
222  if (self_delimiting) {
223  frame_bytes = xiph_lacing_16bit(&ptr, end);
224  if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
225  goto fail;
226  end = ptr + pkt->frame_count * frame_bytes + padding;
227  buf_size = end - buf;
228  } else {
229  frame_bytes = end - ptr - padding;
230  if (frame_bytes % pkt->frame_count ||
231  frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE)
232  goto fail;
233  frame_bytes /= pkt->frame_count;
234  }
235 
236  pkt->frame_offset[0] = ptr - buf;
237  pkt->frame_size[0] = frame_bytes;
238  for (i = 1; i < pkt->frame_count; i++) {
239  pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
240  pkt->frame_size[i] = frame_bytes;
241  }
242  }
243  }
244 
245  pkt->packet_size = buf_size;
246  pkt->data_size = pkt->packet_size - padding;
247 
248  /* total packet duration cannot be larger than 120ms */
249  pkt->frame_duration = ff_opus_frame_duration[pkt->config];
250  if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR)
251  goto fail;
252 
253  /* set mode and bandwidth */
254  if (pkt->config < 12) {
255  pkt->mode = OPUS_MODE_SILK;
256  pkt->bandwidth = pkt->config >> 2;
257  } else if (pkt->config < 16) {
258  pkt->mode = OPUS_MODE_HYBRID;
259  pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
260  } else {
261  pkt->mode = OPUS_MODE_CELT;
262  pkt->bandwidth = (pkt->config - 16) >> 2;
263  /* skip medium band */
264  if (pkt->bandwidth)
265  pkt->bandwidth++;
266  }
267 
268  return 0;
269 
270 fail:
271  memset(pkt, 0, sizeof(*pkt));
272  return AVERROR_INVALIDDATA;
273 }
274 
275 static int channel_reorder_vorbis(int nb_channels, int channel_idx)
276 {
277  return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
278 }
279 
280 static int channel_reorder_unknown(int nb_channels, int channel_idx)
281 {
282  return channel_idx;
283 }
284 
287 {
288  static const uint8_t default_channel_map[2] = { 0, 1 };
289 
290  int (*channel_reorder)(int, int) = channel_reorder_unknown;
291  int channels = avctx->ch_layout.nb_channels;
292 
293  const uint8_t *extradata, *channel_map;
294  int extradata_size;
295  int version, map_type, streams, stereo_streams, i, j, ret;
296  AVChannelLayout layout = { 0 };
297 
298  if (!avctx->extradata) {
299  if (channels > 2) {
300  av_log(avctx, AV_LOG_ERROR,
301  "Multichannel configuration without extradata.\n");
302  return AVERROR(EINVAL);
303  }
304  extradata = opus_default_extradata;
305  extradata_size = sizeof(opus_default_extradata);
306  } else {
307  extradata = avctx->extradata;
308  extradata_size = avctx->extradata_size;
309  }
310 
311  if (extradata_size < 19) {
312  av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
313  extradata_size);
314  return AVERROR_INVALIDDATA;
315  }
316 
317  version = extradata[8];
318  if (version > 15) {
319  avpriv_request_sample(avctx, "Extradata version %d", version);
320  return AVERROR_PATCHWELCOME;
321  }
322 
323  avctx->delay = AV_RL16(extradata + 10);
324  if (avctx->internal)
325  avctx->internal->skip_samples = avctx->delay;
326 
327  channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2;
328  if (!channels) {
329  av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
330  return AVERROR_INVALIDDATA;
331  }
332 
333  s->gain_i = AV_RL16(extradata + 16);
334 
335  map_type = extradata[18];
336  if (!map_type) {
337  if (channels > 2) {
338  av_log(avctx, AV_LOG_ERROR,
339  "Channel mapping 0 is only specified for up to 2 channels\n");
341  goto fail;
342  }
345  streams = 1;
346  stereo_streams = channels - 1;
347  channel_map = default_channel_map;
348  } else if (map_type == 1 || map_type == 2 || map_type == 255) {
349  if (extradata_size < 21 + channels) {
350  av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
351  extradata_size);
353  goto fail;
354  }
355 
356  streams = extradata[19];
357  stereo_streams = extradata[20];
358  if (!streams || stereo_streams > streams ||
359  streams + stereo_streams > 255) {
360  av_log(avctx, AV_LOG_ERROR,
361  "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
363  goto fail;
364  }
365 
366  if (map_type == 1) {
367  if (channels > 8) {
368  av_log(avctx, AV_LOG_ERROR,
369  "Channel mapping 1 is only specified for up to 8 channels\n");
371  goto fail;
372  }
374  channel_reorder = channel_reorder_vorbis;
375  } else if (map_type == 2) {
376  int ambisonic_order = ff_sqrt(channels) - 1;
377  if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
378  channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
379  av_log(avctx, AV_LOG_ERROR,
380  "Channel mapping 2 is only specified for channel counts"
381  " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
382  " for nonnegative integer n\n");
384  goto fail;
385  }
386  if (channels > 227) {
387  av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
389  goto fail;
390  }
391 
393  layout.nb_channels = channels;
394  if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)))
395  layout.u.mask = AV_CH_LAYOUT_STEREO;
396  } else {
398  layout.nb_channels = channels;
399  }
400 
401  channel_map = extradata + 21;
402  } else {
403  avpriv_request_sample(avctx, "Mapping type %d", map_type);
404  return AVERROR_PATCHWELCOME;
405  }
406 
407  s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
408  if (!s->channel_maps) {
409  ret = AVERROR(ENOMEM);
410  goto fail;
411  }
412 
413  for (i = 0; i < channels; i++) {
414  ChannelMap *map = &s->channel_maps[i];
415  uint8_t idx = channel_map[channel_reorder(channels, i)];
416 
417  if (idx == 255) {
418  map->silence = 1;
419  continue;
420  } else if (idx >= streams + stereo_streams) {
421  av_log(avctx, AV_LOG_ERROR,
422  "Invalid channel map for output channel %d: %d\n", i, idx);
423  av_freep(&s->channel_maps);
425  goto fail;
426  }
427 
428  /* check that we did not see this index yet */
429  map->copy = 0;
430  for (j = 0; j < i; j++)
431  if (channel_map[channel_reorder(channels, j)] == idx) {
432  map->copy = 1;
433  map->copy_idx = j;
434  break;
435  }
436 
437  if (idx < 2 * stereo_streams) {
438  map->stream_idx = idx / 2;
439  map->channel_idx = idx & 1;
440  } else {
441  map->stream_idx = idx - stereo_streams;
442  map->channel_idx = 0;
443  }
444  }
445 
447  if (ret < 0)
448  goto fail;
449 
450  s->nb_streams = streams;
451  s->nb_stereo_streams = stereo_streams;
452 
453  return 0;
454 fail:
456  return ret;
457 }
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
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:395
opus_default_extradata
static const uint8_t opus_default_extradata[30]
Definition: opus.h:35
channel_reorder_vorbis
static int channel_reorder_vorbis(int nb_channels, int channel_idx)
Definition: parse.c:275
AVCodecInternal::skip_samples
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:125
OPUS_MAX_FRAME_SIZE
#define OPUS_MAX_FRAME_SIZE
Definition: opus.h:28
opus.h
channel_reorder_unknown
static int channel_reorder_unknown(int nb_channels, int channel_idx)
Definition: parse.c:280
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
vorbis_data.h
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:583
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1047
fail
#define fail()
Definition: checkasm.h:218
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ff_sqrt
#define ff_sqrt
Definition: mathops.h:220
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:218
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
ff_opus_frame_duration
const uint16_t ff_opus_frame_duration[32]
Definition: frame_duration_tab.c:21
av_cold
#define av_cold
Definition: attributes.h:106
parse.h
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:523
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_opus_parse_extradata
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s)
Definition: parse.c:285
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
channels
channels
Definition: aptx.h:31
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
channel_map
static const uint8_t channel_map[8][8]
Definition: atrac3plusdec.c:52
OPUS_MODE_CELT
@ OPUS_MODE_CELT
Definition: opus.h:44
AV_CHANNEL_ORDER_AMBISONIC
@ AV_CHANNEL_ORDER_AMBISONIC
The audio is represented as the decomposition of the sound field into spherical harmonics.
Definition: channel_layout.h:155
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:474
OPUS_MAX_PACKET_DUR
#define OPUS_MAX_PACKET_DUR
Definition: opus.h:30
OpusParseContext
Definition: parse.h:63
OPUS_BANDWIDTH_SUPERWIDEBAND
@ OPUS_BANDWIDTH_SUPERWIDEBAND
Definition: opus.h:53
error.h
xiph_lacing_16bit
static int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
Read a 1- or 2-byte frame length.
Definition: parse.c:45
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
xiph_lacing_full
static int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
Read a multi-byte length (used for code 3 packet padding size)
Definition: parse.c:63
attributes.h
version
version
Definition: libkvazaar.c:313
ff_vorbis_channel_layout_offsets
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:26
layout
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 layout
Definition: filter_design.txt:18
tab.h
log.h
OPUS_MAX_FRAMES
#define OPUS_MAX_FRAMES
Definition: opus.h:29
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:522
OPUS_MODE_HYBRID
@ OPUS_MODE_HYBRID
Definition: opus.h:43
len
int len
Definition: vorbis_enc_data.h:426
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:439
channel_layout.h
OPUS_MODE_SILK
@ OPUS_MODE_SILK
Definition: opus.h:42
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
OpusPacket
Definition: parse.h:31
ChannelMap
Definition: parse.h:48
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:449
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
ff_vorbis_ch_layouts
const AVChannelLayout ff_vorbis_ch_layouts[9]
Definition: vorbis_data.c:37
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:394
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
ff_opus_parse_packet
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
Definition: parse.c:84