FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
opus.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 <stdint.h>
28 
29 #include "libavutil/error.h"
30 #include "libavutil/internal.h"
31 
32 #include "opus.h"
33 #include "vorbis.h"
34 
35 static const uint16_t opus_frame_duration[32] = {
36  480, 960, 1920, 2880,
37  480, 960, 1920, 2880,
38  480, 960, 1920, 2880,
39  480, 960,
40  480, 960,
41  120, 240, 480, 960,
42  120, 240, 480, 960,
43  120, 240, 480, 960,
44  120, 240, 480, 960,
45 };
46 
47 /**
48  * Read a 1- or 2-byte frame length
49  */
50 static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
51 {
52  int val;
53 
54  if (*ptr >= end)
55  return AVERROR_INVALIDDATA;
56  val = *(*ptr)++;
57  if (val >= 252) {
58  if (*ptr >= end)
59  return AVERROR_INVALIDDATA;
60  val += 4 * *(*ptr)++;
61  }
62  return val;
63 }
64 
65 /**
66  * Read a multi-byte length (used for code 3 packet padding size)
67  */
68 static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
69 {
70  int val = 0;
71  int next;
72 
73  while (1) {
74  if (*ptr >= end || val > INT_MAX - 254)
75  return AVERROR_INVALIDDATA;
76  next = *(*ptr)++;
77  val += next;
78  if (next < 255)
79  break;
80  else
81  val--;
82  }
83  return val;
84 }
85 
86 /**
87  * Parse Opus packet info from raw packet data
88  */
89 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
90  int self_delimiting)
91 {
92  const uint8_t *ptr = buf;
93  const uint8_t *end = buf + buf_size;
94  int padding = 0;
95  int frame_bytes, i;
96 
97  if (buf_size < 1)
98  goto fail;
99 
100  /* TOC byte */
101  i = *ptr++;
102  pkt->code = (i ) & 0x3;
103  pkt->stereo = (i >> 2) & 0x1;
104  pkt->config = (i >> 3) & 0x1F;
105 
106  /* code 2 and code 3 packets have at least 1 byte after the TOC */
107  if (pkt->code >= 2 && buf_size < 2)
108  goto fail;
109 
110  switch (pkt->code) {
111  case 0:
112  /* 1 frame */
113  pkt->frame_count = 1;
114  pkt->vbr = 0;
115 
116  if (self_delimiting) {
117  int len = xiph_lacing_16bit(&ptr, end);
118  if (len < 0 || len > end - ptr)
119  goto fail;
120  end = ptr + len;
121  buf_size = end - buf;
122  }
123 
124  frame_bytes = end - ptr;
125  if (frame_bytes > MAX_FRAME_SIZE)
126  goto fail;
127  pkt->frame_offset[0] = ptr - buf;
128  pkt->frame_size[0] = frame_bytes;
129  break;
130  case 1:
131  /* 2 frames, equal size */
132  pkt->frame_count = 2;
133  pkt->vbr = 0;
134 
135  if (self_delimiting) {
136  int len = xiph_lacing_16bit(&ptr, end);
137  if (len < 0 || 2 * len > end - ptr)
138  goto fail;
139  end = ptr + 2 * len;
140  buf_size = end - buf;
141  }
142 
143  frame_bytes = end - ptr;
144  if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
145  goto fail;
146  pkt->frame_offset[0] = ptr - buf;
147  pkt->frame_size[0] = frame_bytes >> 1;
148  pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
149  pkt->frame_size[1] = frame_bytes >> 1;
150  break;
151  case 2:
152  /* 2 frames, different sizes */
153  pkt->frame_count = 2;
154  pkt->vbr = 1;
155 
156  /* read 1st frame size */
157  frame_bytes = xiph_lacing_16bit(&ptr, end);
158  if (frame_bytes < 0)
159  goto fail;
160 
161  if (self_delimiting) {
162  int len = xiph_lacing_16bit(&ptr, end);
163  if (len < 0 || len + frame_bytes > end - ptr)
164  goto fail;
165  end = ptr + frame_bytes + len;
166  buf_size = end - buf;
167  }
168 
169  pkt->frame_offset[0] = ptr - buf;
170  pkt->frame_size[0] = frame_bytes;
171 
172  /* calculate 2nd frame size */
173  frame_bytes = end - ptr - pkt->frame_size[0];
174  if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
175  goto fail;
176  pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
177  pkt->frame_size[1] = frame_bytes;
178  break;
179  case 3:
180  /* 1 to 48 frames, can be different sizes */
181  i = *ptr++;
182  pkt->frame_count = (i ) & 0x3F;
183  padding = (i >> 6) & 0x01;
184  pkt->vbr = (i >> 7) & 0x01;
185 
186  if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
187  goto fail;
188 
189  /* read padding size */
190  if (padding) {
191  padding = xiph_lacing_full(&ptr, end);
192  if (padding < 0)
193  goto fail;
194  }
195 
196  /* read frame sizes */
197  if (pkt->vbr) {
198  /* for VBR, all frames except the final one have their size coded
199  in the bitstream. the last frame size is implicit. */
200  int total_bytes = 0;
201  for (i = 0; i < pkt->frame_count - 1; i++) {
202  frame_bytes = xiph_lacing_16bit(&ptr, end);
203  if (frame_bytes < 0)
204  goto fail;
205  pkt->frame_size[i] = frame_bytes;
206  total_bytes += frame_bytes;
207  }
208 
209  if (self_delimiting) {
210  int len = xiph_lacing_16bit(&ptr, end);
211  if (len < 0 || len + total_bytes + padding > end - ptr)
212  goto fail;
213  end = ptr + total_bytes + len + padding;
214  buf_size = end - buf;
215  }
216 
217  frame_bytes = end - ptr - padding;
218  if (total_bytes > frame_bytes)
219  goto fail;
220  pkt->frame_offset[0] = ptr - buf;
221  for (i = 1; i < pkt->frame_count; i++)
222  pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
223  pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
224  } else {
225  /* for CBR, the remaining packet bytes are divided evenly between
226  the frames */
227  if (self_delimiting) {
228  frame_bytes = xiph_lacing_16bit(&ptr, end);
229  if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
230  goto fail;
231  end = ptr + pkt->frame_count * frame_bytes + padding;
232  buf_size = end - buf;
233  } else {
234  frame_bytes = end - ptr - padding;
235  if (frame_bytes % pkt->frame_count ||
236  frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
237  goto fail;
238  frame_bytes /= pkt->frame_count;
239  }
240 
241  pkt->frame_offset[0] = ptr - buf;
242  pkt->frame_size[0] = frame_bytes;
243  for (i = 1; i < pkt->frame_count; i++) {
244  pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
245  pkt->frame_size[i] = frame_bytes;
246  }
247  }
248  }
249 
250  pkt->packet_size = buf_size;
251  pkt->data_size = pkt->packet_size - padding;
252 
253  /* total packet duration cannot be larger than 120ms */
255  if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR)
256  goto fail;
257 
258  /* set mode and bandwidth */
259  if (pkt->config < 12) {
260  pkt->mode = OPUS_MODE_SILK;
261  pkt->bandwidth = pkt->config >> 2;
262  } else if (pkt->config < 16) {
263  pkt->mode = OPUS_MODE_HYBRID;
264  pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
265  } else {
266  pkt->mode = OPUS_MODE_CELT;
267  pkt->bandwidth = (pkt->config - 16) >> 2;
268  /* skip mediumband */
269  if (pkt->bandwidth)
270  pkt->bandwidth++;
271  }
272 
273  return 0;
274 
275 fail:
276  memset(pkt, 0, sizeof(*pkt));
277  return AVERROR_INVALIDDATA;
278 }
279 
280 static int channel_reorder_vorbis(int nb_channels, int channel_idx)
281 {
282  return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
283 }
284 
285 static int channel_reorder_unknown(int nb_channels, int channel_idx)
286 {
287  return channel_idx;
288 }
289 
291  OpusContext *s)
292 {
293  static const uint8_t default_channel_map[2] = { 0, 1 };
294 
295  int (*channel_reorder)(int, int) = channel_reorder_unknown;
296 
297  const uint8_t *extradata, *channel_map;
298  int extradata_size;
299  int version, channels, map_type, streams, stereo_streams, i, j;
300  uint64_t layout;
301 
302  if (!avctx->extradata) {
303  if (avctx->channels > 2) {
304  av_log(avctx, AV_LOG_ERROR,
305  "Multichannel configuration without extradata.\n");
306  return AVERROR(EINVAL);
307  }
308  extradata = opus_default_extradata;
309  extradata_size = sizeof(opus_default_extradata);
310  } else {
311  extradata = avctx->extradata;
312  extradata_size = avctx->extradata_size;
313  }
314 
315  if (extradata_size < 19) {
316  av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
317  extradata_size);
318  return AVERROR_INVALIDDATA;
319  }
320 
321  version = extradata[8];
322  if (version > 15) {
323  avpriv_request_sample(avctx, "Extradata version %d", version);
324  return AVERROR_PATCHWELCOME;
325  }
326 
327  avctx->delay = AV_RL16(extradata + 10);
328 
329  channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
330  if (!channels) {
331  av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\n");
332  return AVERROR_INVALIDDATA;
333  }
334 
335  s->gain_i = AV_RL16(extradata + 16);
336  if (s->gain_i)
337  s->gain = ff_exp10(s->gain_i / (20.0 * 256));
338 
339  map_type = extradata[18];
340  if (!map_type) {
341  if (channels > 2) {
342  av_log(avctx, AV_LOG_ERROR,
343  "Channel mapping 0 is only specified for up to 2 channels\n");
344  return AVERROR_INVALIDDATA;
345  }
346  layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
347  streams = 1;
348  stereo_streams = channels - 1;
349  channel_map = default_channel_map;
350  } else if (map_type == 1 || map_type == 255) {
351  if (extradata_size < 21 + channels) {
352  av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
353  extradata_size);
354  return AVERROR_INVALIDDATA;
355  }
356 
357  streams = extradata[19];
358  stereo_streams = extradata[20];
359  if (!streams || stereo_streams > streams ||
360  streams + stereo_streams > 255) {
361  av_log(avctx, AV_LOG_ERROR,
362  "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
363  return AVERROR_INVALIDDATA;
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");
370  return AVERROR_INVALIDDATA;
371  }
372  layout = ff_vorbis_channel_layouts[channels - 1];
373  channel_reorder = channel_reorder_vorbis;
374  } else
375  layout = 0;
376 
377  channel_map = extradata + 21;
378  } else {
379  avpriv_request_sample(avctx, "Mapping type %d", map_type);
380  return AVERROR_PATCHWELCOME;
381  }
382 
383  s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));
384  if (!s->channel_maps)
385  return AVERROR(ENOMEM);
386 
387  for (i = 0; i < channels; i++) {
388  ChannelMap *map = &s->channel_maps[i];
389  uint8_t idx = channel_map[channel_reorder(channels, i)];
390 
391  if (idx == 255) {
392  map->silence = 1;
393  continue;
394  } else if (idx >= streams + stereo_streams) {
395  av_log(avctx, AV_LOG_ERROR,
396  "Invalid channel map for output channel %d: %d\n", i, idx);
397  return AVERROR_INVALIDDATA;
398  }
399 
400  /* check that we din't see this index yet */
401  map->copy = 0;
402  for (j = 0; j < i; j++)
403  if (channel_map[channel_reorder(channels, j)] == idx) {
404  map->copy = 1;
405  map->copy_idx = j;
406  break;
407  }
408 
409  if (idx < 2 * stereo_streams) {
410  map->stream_idx = idx / 2;
411  map->channel_idx = idx & 1;
412  } else {
413  map->stream_idx = idx - stereo_streams;
414  map->channel_idx = 0;
415  }
416  }
417 
418  avctx->channels = channels;
419  avctx->channel_layout = layout;
420  s->nb_streams = streams;
421  s->nb_stereo_streams = stereo_streams;
422 
423  return 0;
424 }
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: opus.c:89
static int channel_reorder_vorbis(int nb_channels, int channel_idx)
Definition: opus.c:280
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define MAX_FRAME_SIZE
Definition: 8svx.c:60
int frame_count
frame count
Definition: opus.h:114
int nb_stereo_streams
Definition: opus.h:187
static int channel_reorder_unknown(int nb_channels, int channel_idx)
Definition: opus.c:285
float gain
Definition: opus.h:191
int version
Definition: avisynth_c.h:629
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int vbr
vbr flag
Definition: opus.h:111
#define MAX_PACKET_DUR
Definition: opus.h:39
const int * channel_map
channel index (or -1 if muted channel) map
int16_t gain_i
Definition: opus.h:190
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
#define av_cold
Definition: attributes.h:82
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
int copy
Definition: opus.h:166
#define av_log(a,...)
ChannelMap * channel_maps
Definition: opus.h:193
int nb_streams
Definition: opus.h:186
static const uint8_t opus_default_extradata[30]
Definition: opus.h:67
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
error code definitions
#define AVERROR(e)
Definition: error.h:43
#define fail()
Definition: checkasm.h:80
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2338
common internal API header
int frame_size[MAX_FRAMES]
frame sizes
Definition: opus.h:116
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:117
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int silence
Definition: opus.h:171
static int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
Read a 1- or 2-byte frame length.
Definition: opus.c:50
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: opus.c:68
main external API structure.
Definition: avcodec.h:1532
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1648
int config
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:112
enum OpusMode mode
mode
Definition: opus.h:118
int copy_idx
Definition: opus.h:168
int stereo
whether this packet is mono or stereo
Definition: opus.h:110
int data_size
size of the useful data – packet size - padding
Definition: opus.h:108
int channel_idx
Definition: opus.h:161
#define MAX_FRAMES
Definition: diracdec.c:49
int stream_idx
Definition: opus.h:160
static const uint16_t opus_frame_duration[32]
Definition: opus.c:35
int packet_size
packet size
Definition: opus.h:107
int len
int channels
number of audio channels
Definition: avcodec.h:2288
int frame_offset[MAX_FRAMES]
frame offsets
Definition: opus.h:115
enum OpusBandwidth bandwidth
bandwidth
Definition: opus.h:119
uint64_t layout
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:290
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: internal.h:306
int nb_channels
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:25
int code
packet code: specifies the frame layout
Definition: opus.h:109
#define AV_CH_LAYOUT_MONO
int delay
Codec delay.
Definition: avcodec.h:1694