Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 enum OpusMode {
68 };
69 
76 };
77 
78 typedef struct RawBitsContext {
79  const uint8_t *position;
80  unsigned int bytes;
81  unsigned int cachelen;
82  unsigned int cacheval;
84 
85 typedef struct OpusRangeCoder {
88  unsigned int range;
89  unsigned int value;
90  unsigned int total_read_bits;
92 
93 typedef struct SilkContext SilkContext;
94 
95 typedef struct CeltContext CeltContext;
96 
97 typedef struct OpusPacket {
99  int data_size;
100  int code;
101  int stereo;
102  int vbr;
103  int config;
106  int frame_offset[MAX_FRAMES];
109  enum OpusMode mode;
110  enum OpusBandwidth bandwidth;
111 } OpusPacket;
112 
113 typedef struct OpusStreamContext {
116 
122 
123  float silk_buf[2][960];
124  float *silk_output[2];
125  DECLARE_ALIGNED(32, float, celt_buf)[2][960];
126  float *celt_output[2];
127 
128  float redundancy_buf[2][960];
129  float *redundancy_output[2];
130 
131  /* data buffers for the final output data */
132  float *out[2];
133  int out_size;
134 
135  float *out_dummy;
137 
141  /* number of samples we still want to get from the resampler */
143 
145 
148 
149 // a mapping between an opus stream and an output channel
150 typedef struct ChannelMap {
153 
154  // when a single decoded channel is mapped to multiple output channels, we
155  // write to the first output directly and copy from it to the others
156  // this field is set to 1 for those copied output channels
157  int copy;
158  // this is the index of the output channel to copy from
159  int copy_idx;
160 
161  // this channel is silent
162  int silence;
163 } ChannelMap;
164 
165 typedef struct OpusContext {
167 
168  /* current output buffers for each streams */
169  float **out;
170  int *out_size;
171  /* Buffers for synchronizing the streams when they have different
172  * resampling delays */
174  /* number of decoded samples for each stream */
176 
179 
181  int16_t gain_i;
182  float gain;
183 
185 } OpusContext;
186 
188 {
189  while (rc->range <= 1<<23) {
190  rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1);
191  rc->range <<= 8;
192  rc->total_read_bits += 8;
193  }
194 }
195 
196 static av_always_inline void opus_rc_update(OpusRangeCoder *rc, unsigned int scale,
197  unsigned int low, unsigned int high,
198  unsigned int total)
199 {
200  rc->value -= scale * (total - high);
201  rc->range = low ? scale * (high - low)
202  : rc->range - scale * (total - high);
203  opus_rc_normalize(rc);
204 }
205 
206 static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf)
207 {
208  unsigned int k, scale, total, symbol, low, high;
209 
210  total = *cdf++;
211 
212  scale = rc->range / total;
213  symbol = rc->value / scale + 1;
214  symbol = total - FFMIN(symbol, total);
215 
216  for (k = 0; cdf[k] <= symbol; k++);
217  high = cdf[k];
218  low = k ? cdf[k-1] : 0;
219 
220  opus_rc_update(rc, scale, low, high, total);
221 
222  return k;
223 }
224 
225 static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
226 {
227  unsigned int k, scale;
228  scale = rc->range >> bits; // in this case, scale = symbol
229 
230  if (rc->value >= scale) {
231  rc->value -= scale;
232  rc->range -= scale;
233  k = 0;
234  } else {
235  rc->range = scale;
236  k = 1;
237  }
238  opus_rc_normalize(rc);
239  return k;
240 }
241 
246 static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc)
247 {
248  return rc->total_read_bits - av_log2(rc->range) - 1;
249 }
250 
251 static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc)
252 {
253  unsigned int i, total_bits, rcbuffer, range;
254 
255  total_bits = rc->total_read_bits << 3;
256  rcbuffer = av_log2(rc->range) + 1;
257  range = rc->range >> (rcbuffer-16);
258 
259  for (i = 0; i < 3; i++) {
260  int bit;
261  range = range * range >> 15;
262  bit = range >> 16;
263  rcbuffer = rcbuffer << 1 | bit;
264  range >>= bit;
265  }
266 
267  return total_bits - rcbuffer;
268 }
269 
273 static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigned int count)
274 {
275  unsigned int value = 0;
276 
277  while (rc->rb.bytes && rc->rb.cachelen < count) {
278  rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
279  rc->rb.cachelen += 8;
280  rc->rb.bytes--;
281  }
282 
283  value = rc->rb.cacheval & ((1<<count)-1);
284  rc->rb.cacheval >>= count;
285  rc->rb.cachelen -= count;
286  rc->total_read_bits += count;
287 
288  return value;
289 }
290 
294 static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size)
295 {
296  unsigned int bits, k, scale, total;
297 
298  bits = opus_ilog(size - 1);
299  total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
300 
301  scale = rc->range / total;
302  k = rc->value / scale + 1;
303  k = total - FFMIN(k, total);
304  opus_rc_update(rc, scale, k, k + 1, total);
305 
306  if (bits > 8) {
307  k = k << (bits - 8) | opus_getrawbits(rc, bits - 8);
308  return FFMIN(k, size - 1);
309  } else
310  return k;
311 }
312 
313 static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay)
314 {
315  /* extends the range coder to model a Laplace distribution */
316  int value = 0;
317  unsigned int scale, low = 0, center;
318 
319  scale = rc->range >> 15;
320  center = rc->value / scale + 1;
321  center = (1 << 15) - FFMIN(center, 1 << 15);
322 
323  if (center >= symbol) {
324  value++;
325  low = symbol;
326  symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
327 
328  while (symbol > 1 && center >= low + 2 * symbol) {
329  value++;
330  symbol *= 2;
331  low += symbol;
332  symbol = (((symbol - 2) * decay) >> 15) + 1;
333  }
334 
335  if (symbol <= 1) {
336  int distance = (center - low) >> 1;
337  value += distance;
338  low += 2 * distance;
339  }
340 
341  if (center < low + symbol)
342  value *= -1;
343  else
344  low += symbol;
345  }
346 
347  opus_rc_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
348 
349  return value;
350 }
351 
352 static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0)
353 {
354  /* Use a probability of 3 up to itheta=8192 and then use 1 after */
355  unsigned int k, scale, symbol, total = (k0+1)*3 + k0;
356  scale = rc->range / total;
357  symbol = rc->value / scale + 1;
358  symbol = total - FFMIN(symbol, total);
359 
360  k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
361 
362  opus_rc_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
363  (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
364  return k;
365 }
366 
367 static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn)
368 {
369  unsigned int k, scale, symbol, total, low, center;
370 
371  total = ((qn>>1) + 1) * ((qn>>1) + 1);
372  scale = rc->range / total;
373  center = rc->value / scale + 1;
374  center = total - FFMIN(center, total);
375 
376  if (center < total >> 1) {
377  k = (ff_sqrt(8 * center + 1) - 1) >> 1;
378  low = k * (k + 1) >> 1;
379  symbol = k + 1;
380  } else {
381  k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
382  low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
383  symbol = qn + 1 - k;
384  }
385 
386  opus_rc_update(rc, scale, low, low + symbol, total);
387 
388  return k;
389 }
390 
391 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
392  int self_delimited);
393 
395 
396 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
397 void ff_silk_free(SilkContext **ps);
398 void ff_silk_flush(SilkContext *s);
399 
405  float *output[2],
406  enum OpusBandwidth bandwidth, int coded_channels,
407  int duration_ms);
408 
409 int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels);
410 
411 void ff_celt_free(CeltContext **s);
412 
413 void ff_celt_flush(CeltContext *s);
414 
416  float **output, int coded_channels, int frame_size,
417  int startband, int endband);
418 
419 extern const float ff_celt_window2[120];
420 
421 #endif /* AVCODEC_OPUS_H */
AVAudioResampleContext * avr
Definition: opus.h:138
void ff_celt_flush(CeltContext *s)
Definition: opus_celt.c:2146
int size
AVAudioFifo ** sync_buffers
Definition: opus.h:173
int frame_count
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:105
int nb_stereo_streams
Definition: opus.h:178
static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf)
Definition: opus.h:206
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
int output_channels
Definition: opus.h:115
int delayed_samples
Definition: opus.h:142
float gain
Definition: opus.h:182
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:58
RawBitsContext rb
Definition: opus.h:87
static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
Definition: opus.h:225
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
int vbr
whether this packet is mono or stereo
Definition: opus.h:102
int16_t gain_i
Definition: opus.h:181
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:88
unsigned int cacheval
Definition: opus.h:82
int * decoded_samples
Definition: opus.h:175
uint8_t bits
Definition: crc.c:251
uint8_t
unsigned int total_read_bits
Definition: opus.h:90
#define opus_ilog(i)
Definition: opus.h:62
int copy
Definition: opus.h:157
SilkContext * silk
Definition: opus.h:119
void ff_celt_free(CeltContext **s)
Definition: opus_celt.c:2173
bitstream reader API header.
const float ff_celt_window2[120]
Definition: opus_celt.c:466
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
AVFloatDSPContext * fdsp
Definition: opus.h:121
ChannelMap * channel_maps
Definition: opus.h:184
int nb_streams
Definition: opus.h:177
int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:289
unsigned int value
Definition: opus.h:89
static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc)
Definition: opus.h:251
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:2187
external API header
#define FFMIN(a, b)
Definition: common.h:57
int frame_duration
frame sizes
Definition: opus.h:108
int out_dummy_allocated_size
Definition: opus.h:136
int silence
Definition: opus.h:162
unsigned int bytes
Definition: opus.h:80
float * out_dummy
Definition: opus.h:135
unsigned int cachelen
Definition: opus.h:81
Libavcodec external API header.
OpusPacket packet
Definition: opus.h:144
static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0)
Definition: opus.h:352
static av_const unsigned int ff_sqrt(unsigned int a)
Definition: mathops.h:202
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:1567
main external API structure.
Definition: avcodec.h:1050
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:196
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:1575
GetBitContext gb
Definition: opus.h:86
int config
vbr flag
Definition: opus.h:103
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:1562
int copy_idx
Definition: opus.h:159
int stereo
packet code: specifies the frame layout
Definition: opus.h:101
AVCodecContext * avctx
Definition: opus.h:114
float ** out
Definition: opus.h:169
int data_size
packet size
Definition: opus.h:99
int channel_idx
Definition: opus.h:152
CeltContext * celt
Definition: opus.h:120
int redundancy_idx
Definition: opus.h:146
unsigned int range
Definition: opus.h:88
static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn)
Definition: opus.h:367
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:273
static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
Definition: opus.h:187
OpusRangeCoder rc
Definition: opus.h:117
int stream_idx
Definition: opus.h:151
AVFloatDSPContext fdsp
Definition: opus.h:180
int * out_size
Definition: opus.h:170
OpusBandwidth
Definition: opus.h:70
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:294
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:1976
OpusStreamContext * streams
Definition: opus.h:166
int packet_size
Definition: opus.h:98
OpusRangeCoder redundancy_rc
Definition: opus.h:118
Audio FIFO Buffer.
OpusMode
Definition: opus.h:64
#define av_log2
Definition: intmath.h:85
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:246
AVAudioFifo * celt_delay
Definition: opus.h:139
int silk_samplerate
Definition: opus.h:140
#define av_always_inline
Definition: attributes.h:40
const uint8_t * position
Definition: opus.h:79
static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay)
Definition: opus.h:313
int code
size of the useful data – packet size - padding
Definition: opus.h:100
#define MAX_FRAMES
Definition: opus.h:38