FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
opus.h
Go to the documentation of this file.
1 /*
2  * Opus decoder/demuxer common functions
3  * Copyright (c) 2012 Andrew D'Addesio
4  * Copyright (c) 2013-2014 Mozilla Corporation
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef AVCODEC_OPUS_H
24 #define AVCODEC_OPUS_H
25 
26 #include <stdint.h>
27 
28 #include "libavutil/audio_fifo.h"
29 #include "libavutil/float_dsp.h"
30 #include "libavutil/frame.h"
31 
33 
34 #include "avcodec.h"
35 #include "get_bits.h"
36 
37 #define MAX_FRAME_SIZE 1275
38 #define MAX_FRAMES 48
39 #define MAX_PACKET_DUR 5760
40 
41 #define CELT_SHORT_BLOCKSIZE 120
42 #define CELT_OVERLAP CELT_SHORT_BLOCKSIZE
43 #define CELT_MAX_LOG_BLOCKS 3
44 #define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS))
45 #define CELT_MAX_BANDS 21
46 #define CELT_VECTORS 11
47 #define CELT_ALLOC_STEPS 6
48 #define CELT_FINE_OFFSET 21
49 #define CELT_MAX_FINE_BITS 8
50 #define CELT_NORM_SCALE 16384
51 #define CELT_QTHETA_OFFSET 4
52 #define CELT_QTHETA_OFFSET_TWOPHASE 16
53 #define CELT_DEEMPH_COEFF 0.85000610f
54 #define CELT_POSTFILTER_MINPERIOD 15
55 #define CELT_ENERGY_SILENCE (-28.0f)
56 
57 #define SILK_HISTORY 322
58 #define SILK_MAX_LPC 16
59 
60 #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
61 #define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15)
62 #define opus_ilog(i) (av_log2(i) + !!(i))
63 
64 #define OPUS_TS_HEADER 0x7FE0 // 0x3ff (11 bits)
65 #define OPUS_TS_MASK 0xFFE0 // top 11 bits
66 
67 static const uint8_t opus_default_extradata[30] = {
68  'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
69  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 };
72 
73 enum OpusMode {
77 };
78 
85 };
86 
87 typedef struct RawBitsContext {
88  const uint8_t *position;
89  unsigned int bytes;
90  unsigned int cachelen;
91  unsigned int cacheval;
93 
94 typedef struct OpusRangeCoder {
97  unsigned int range;
98  unsigned int value;
99  unsigned int total_read_bits;
101 
102 typedef struct SilkContext SilkContext;
103 
104 typedef struct CeltContext CeltContext;
105 
106 typedef struct OpusPacket {
107  int packet_size; /**< packet size */
108  int data_size; /**< size of the useful data -- packet size - padding */
109  int code; /**< packet code: specifies the frame layout */
110  int stereo; /**< whether this packet is mono or stereo */
111  int vbr; /**< vbr flag */
112  int config; /**< configuration: tells the audio mode,
113  ** bandwidth, and frame duration */
114  int frame_count; /**< frame count */
115  int frame_offset[MAX_FRAMES]; /**< frame offsets */
116  int frame_size[MAX_FRAMES]; /**< frame sizes */
117  int frame_duration; /**< frame duration, in samples @ 48kHz */
118  enum OpusMode mode; /**< mode */
119  enum OpusBandwidth bandwidth; /**< bandwidth */
120 } OpusPacket;
121 
122 typedef struct OpusStreamContext {
125 
131 
132  float silk_buf[2][960];
133  float *silk_output[2];
134  DECLARE_ALIGNED(32, float, celt_buf)[2][960];
135  float *celt_output[2];
136 
137  float redundancy_buf[2][960];
138  float *redundancy_output[2];
139 
140  /* data buffers for the final output data */
141  float *out[2];
142  int out_size;
143 
144  float *out_dummy;
146 
150  /* number of samples we still want to get from the resampler */
152 
154 
157 
158 // a mapping between an opus stream and an output channel
159 typedef struct ChannelMap {
162 
163  // when a single decoded channel is mapped to multiple output channels, we
164  // write to the first output directly and copy from it to the others
165  // this field is set to 1 for those copied output channels
166  int copy;
167  // this is the index of the output channel to copy from
168  int copy_idx;
169 
170  // this channel is silent
171  int silence;
172 } ChannelMap;
173 
174 typedef struct OpusContext {
176 
177  /* current output buffers for each streams */
178  float **out;
179  int *out_size;
180  /* Buffers for synchronizing the streams when they have different
181  * resampling delays */
183  /* number of decoded samples for each stream */
185 
188 
190  int16_t gain_i;
191  float gain;
192 
194 } OpusContext;
195 
197 {
198  while (rc->range <= 1<<23) {
199  rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1);
200  rc->range <<= 8;
201  rc->total_read_bits += 8;
202  }
203 }
204 
205 static av_always_inline void opus_rc_update(OpusRangeCoder *rc, unsigned int scale,
206  unsigned int low, unsigned int high,
207  unsigned int total)
208 {
209  rc->value -= scale * (total - high);
210  rc->range = low ? scale * (high - low)
211  : rc->range - scale * (total - high);
212  opus_rc_normalize(rc);
213 }
214 
215 static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf)
216 {
217  unsigned int k, scale, total, symbol, low, high;
218 
219  total = *cdf++;
220 
221  scale = rc->range / total;
222  symbol = rc->value / scale + 1;
223  symbol = total - FFMIN(symbol, total);
224 
225  for (k = 0; cdf[k] <= symbol; k++);
226  high = cdf[k];
227  low = k ? cdf[k-1] : 0;
228 
229  opus_rc_update(rc, scale, low, high, total);
230 
231  return k;
232 }
233 
234 static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
235 {
236  unsigned int k, scale;
237  scale = rc->range >> bits; // in this case, scale = symbol
238 
239  if (rc->value >= scale) {
240  rc->value -= scale;
241  rc->range -= scale;
242  k = 0;
243  } else {
244  rc->range = scale;
245  k = 1;
246  }
247  opus_rc_normalize(rc);
248  return k;
249 }
250 
251 /**
252  * CELT: estimate bits of entropy that have thus far been consumed for the
253  * current CELT frame, to integer and fractional (1/8th bit) precision
254  */
255 static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc)
256 {
257  return rc->total_read_bits - av_log2(rc->range) - 1;
258 }
259 
260 static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc)
261 {
262  unsigned int i, total_bits, rcbuffer, range;
263 
264  total_bits = rc->total_read_bits << 3;
265  rcbuffer = av_log2(rc->range) + 1;
266  range = rc->range >> (rcbuffer-16);
267 
268  for (i = 0; i < 3; i++) {
269  int bit;
270  range = range * range >> 15;
271  bit = range >> 16;
272  rcbuffer = rcbuffer << 1 | bit;
273  range >>= bit;
274  }
275 
276  return total_bits - rcbuffer;
277 }
278 
279 /**
280  * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
281  */
282 static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigned int count)
283 {
284  unsigned int value = 0;
285 
286  while (rc->rb.bytes && rc->rb.cachelen < count) {
287  rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
288  rc->rb.cachelen += 8;
289  rc->rb.bytes--;
290  }
291 
292  value = av_mod_uintp2(rc->rb.cacheval, count);
293  rc->rb.cacheval >>= count;
294  rc->rb.cachelen -= count;
295  rc->total_read_bits += count;
296 
297  return value;
298 }
299 
300 /**
301  * CELT: read a uniform distribution
302  */
303 static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size)
304 {
305  unsigned int bits, k, scale, total;
306 
307  bits = opus_ilog(size - 1);
308  total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
309 
310  scale = rc->range / total;
311  k = rc->value / scale + 1;
312  k = total - FFMIN(k, total);
313  opus_rc_update(rc, scale, k, k + 1, total);
314 
315  if (bits > 8) {
316  k = k << (bits - 8) | opus_getrawbits(rc, bits - 8);
317  return FFMIN(k, size - 1);
318  } else
319  return k;
320 }
321 
322 static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay)
323 {
324  /* extends the range coder to model a Laplace distribution */
325  int value = 0;
326  unsigned int scale, low = 0, center;
327 
328  scale = rc->range >> 15;
329  center = rc->value / scale + 1;
330  center = (1 << 15) - FFMIN(center, 1 << 15);
331 
332  if (center >= symbol) {
333  value++;
334  low = symbol;
335  symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
336 
337  while (symbol > 1 && center >= low + 2 * symbol) {
338  value++;
339  symbol *= 2;
340  low += symbol;
341  symbol = (((symbol - 2) * decay) >> 15) + 1;
342  }
343 
344  if (symbol <= 1) {
345  int distance = (center - low) >> 1;
346  value += distance;
347  low += 2 * distance;
348  }
349 
350  if (center < low + symbol)
351  value *= -1;
352  else
353  low += symbol;
354  }
355 
356  opus_rc_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
357 
358  return value;
359 }
360 
361 static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0)
362 {
363  /* Use a probability of 3 up to itheta=8192 and then use 1 after */
364  unsigned int k, scale, symbol, total = (k0+1)*3 + k0;
365  scale = rc->range / total;
366  symbol = rc->value / scale + 1;
367  symbol = total - FFMIN(symbol, total);
368 
369  k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
370 
371  opus_rc_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
372  (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
373  return k;
374 }
375 
376 static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn)
377 {
378  unsigned int k, scale, symbol, total, low, center;
379 
380  total = ((qn>>1) + 1) * ((qn>>1) + 1);
381  scale = rc->range / total;
382  center = rc->value / scale + 1;
383  center = total - FFMIN(center, total);
384 
385  if (center < total >> 1) {
386  k = (ff_sqrt(8 * center + 1) - 1) >> 1;
387  low = k * (k + 1) >> 1;
388  symbol = k + 1;
389  } else {
390  k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
391  low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
392  symbol = qn + 1 - k;
393  }
394 
395  opus_rc_update(rc, scale, low, low + symbol, total);
396 
397  return k;
398 }
399 
400 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
401  int self_delimited);
402 
404 
405 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
406 void ff_silk_free(SilkContext **ps);
408 
409 /**
410  * Decode the LP layer of one Opus frame (which may correspond to several SILK
411  * frames).
412  */
414  float *output[2],
415  enum OpusBandwidth bandwidth, int coded_channels,
416  int duration_ms);
417 
418 int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels);
419 
420 void ff_celt_free(CeltContext **s);
421 
423 
425  float **output, int coded_channels, int frame_size,
426  int startband, int endband);
427 
428 extern const float ff_celt_window2[120];
429 
430 #endif /* AVCODEC_OPUS_H */
void ff_celt_flush(CeltContext *s)
Definition: opus_celt.c:2147
const char * s
Definition: avisynth_c.h:631
AVAudioFifo ** sync_buffers
Definition: opus.h:182
int frame_count
frame count
Definition: opus.h:114
int nb_stereo_streams
Definition: opus.h:187
static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf)
Definition: opus.h:215
float redundancy_buf[2][960]
Definition: opus.h:137
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
int output_channels
Definition: opus.h:124
int delayed_samples
Definition: opus.h:151
float gain
Definition: opus.h:191
int av_log2(unsigned v)
Definition: intmath.c:26
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:53
RawBitsContext rb
Definition: opus.h:96
static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
Definition: opus.h:234
static AVPacket pkt
int vbr
vbr flag
Definition: opus.h:111
int16_t gain_i
Definition: opus.h:190
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimited)
Parse Opus packet info from raw packet data.
Definition: opus.c:89
unsigned int cacheval
Definition: opus.h:91
int * decoded_samples
Definition: opus.h:184
uint8_t bits
Definition: crc.c:296
uint8_t
unsigned int total_read_bits
Definition: opus.h:99
#define opus_ilog(i)
Definition: opus.h:62
int copy
Definition: opus.h:166
SilkContext * silk
Definition: opus.h:128
void ff_celt_free(CeltContext **s)
Definition: opus_celt.c:2174
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
float * silk_output[2]
Definition: opus.h:133
#define ff_sqrt
Definition: mathops.h:215
const float ff_celt_window2[120]
Definition: opus_celt.c:467
AVFloatDSPContext * fdsp
Definition: opus.h:130
ChannelMap * channel_maps
Definition: opus.h:193
libswresample public header
int nb_streams
Definition: opus.h:186
static const uint8_t opus_default_extradata[30]
Definition: opus.h:67
int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:290
unsigned int value
Definition: opus.h:98
The libswresample context.
AVFloatDSPContext * fdsp
Definition: opus.h:189
static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc)
Definition: opus.h:260
GLsizei count
Definition: opengl_enc.c:109
reference-counted frame API
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
static float distance(float x, float y, int band)
int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels)
Definition: opus_celt.c:2189
float * out[2]
Definition: opus.h:141
int frame_size[MAX_FRAMES]
frame sizes
Definition: opus.h:116
#define FFMIN(a, b)
Definition: common.h:96
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:117
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
float celt_buf[2][960]
Definition: opus.h:134
SwrContext * swr
Definition: opus.h:147
int out_dummy_allocated_size
Definition: opus.h:145
float silk_buf[2][960]
Definition: opus.h:132
int silence
Definition: opus.h:171
unsigned int bytes
Definition: opus.h:89
float * out_dummy
Definition: opus.h:144
unsigned int cachelen
Definition: opus.h:90
int frame_size
Definition: mxfenc.c:1821
Libavcodec external API header.
OpusPacket packet
Definition: opus.h:153
static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0)
Definition: opus.h:361
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:1567
main external API structure.
Definition: avcodec.h:1649
static av_always_inline void opus_rc_update(OpusRangeCoder *rc, unsigned int scale, unsigned int low, unsigned int high, unsigned int total)
Definition: opus.h:205
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:1575
GetBitContext gb
Definition: opus.h:95
void * buf
Definition: avisynth_c.h:553
int config
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:112
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:1562
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
AVCodecContext * avctx
Definition: opus.h:123
#define u(width,...)
float ** out
Definition: opus.h:178
int data_size
size of the useful data – packet size - padding
Definition: opus.h:108
int channel_idx
Definition: opus.h:161
CeltContext * celt
Definition: opus.h:129
int redundancy_idx
Definition: opus.h:155
unsigned int range
Definition: opus.h:97
static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn)
Definition: opus.h:376
static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigned int count)
CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise.
Definition: opus.h:282
static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
Definition: opus.h:196
float * celt_output[2]
Definition: opus.h:135
OpusRangeCoder rc
Definition: opus.h:126
int stream_idx
Definition: opus.h:160
int * out_size
Definition: opus.h:179
OpusBandwidth
Definition: opus.h:79
int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, float *output[2], enum OpusBandwidth bandwidth, int coded_channels, int duration_ms)
Decode the LP layer of one Opus frame (which may correspond to several SILK frames).
Definition: opus_silk.c:1498
static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size)
CELT: read a uniform distribution.
Definition: opus.h:303
int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, float **output, int coded_channels, int frame_size, int startband, int endband)
Definition: opus_celt.c:1977
OpusStreamContext * streams
Definition: opus.h:175
int packet_size
packet size
Definition: opus.h:107
OpusRangeCoder redundancy_rc
Definition: opus.h:127
Audio FIFO Buffer.
OpusMode
Definition: opus.h:73
int frame_offset[MAX_FRAMES]
frame offsets
Definition: opus.h:115
static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame...
Definition: opus.h:255
enum OpusBandwidth bandwidth
bandwidth
Definition: opus.h:119
float * redundancy_output[2]
Definition: opus.h:138
AVAudioFifo * celt_delay
Definition: opus.h:148
int silk_samplerate
Definition: opus.h:149
#define av_always_inline
Definition: attributes.h:39
const uint8_t * position
Definition: opus.h:88
static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay)
Definition: opus.h:322
int code
packet code: specifies the frame layout
Definition: opus.h:109
#define MAX_FRAMES
Definition: opus.h:38