]> git.sesse.net Git - ffmpeg/blob - libavcodec/opus.h
dvbsubdec: check memory allocations and propagate errors
[ffmpeg] / libavcodec / opus.h
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
32 #include "libavresample/avresample.h"
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 {
74     OPUS_MODE_SILK,
75     OPUS_MODE_HYBRID,
76     OPUS_MODE_CELT
77 };
78
79 enum OpusBandwidth {
80     OPUS_BANDWIDTH_NARROWBAND,
81     OPUS_BANDWIDTH_MEDIUMBAND,
82     OPUS_BANDWIDTH_WIDEBAND,
83     OPUS_BANDWIDTH_SUPERWIDEBAND,
84     OPUS_BANDWIDTH_FULLBAND
85 };
86
87 typedef struct RawBitsContext {
88     const uint8_t *position;
89     unsigned int bytes;
90     unsigned int cachelen;
91     unsigned int cacheval;
92 } RawBitsContext;
93
94 typedef struct OpusRangeCoder {
95     GetBitContext gb;
96     RawBitsContext rb;
97     unsigned int range;
98     unsigned int value;
99     unsigned int total_read_bits;
100 } OpusRangeCoder;
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 {
123     AVCodecContext *avctx;
124     int output_channels;
125
126     OpusRangeCoder rc;
127     OpusRangeCoder redundancy_rc;
128     SilkContext *silk;
129     CeltContext *celt;
130     AVFloatDSPContext *fdsp;
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;
145     int    out_dummy_allocated_size;
146
147     AVAudioResampleContext *avr;
148     AVAudioFifo *celt_delay;
149     int silk_samplerate;
150     /* number of samples we still want to get from the resampler */
151     int delayed_samples;
152
153     OpusPacket packet;
154
155     int redundancy_idx;
156 } OpusStreamContext;
157
158 // a mapping between an opus stream and an output channel
159 typedef struct ChannelMap {
160     int stream_idx;
161     int channel_idx;
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 {
175     OpusStreamContext *streams;
176     int             nb_streams;
177     int      nb_stereo_streams;
178
179     AVFloatDSPContext fdsp;
180     int16_t gain_i;
181     float   gain;
182
183     ChannelMap *channel_maps;
184 } OpusContext;
185
186 static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
187 {
188     while (rc->range <= 1<<23) {
189         rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1);
190         rc->range          <<= 8;
191         rc->total_read_bits += 8;
192     }
193 }
194
195 static av_always_inline void opus_rc_update(OpusRangeCoder *rc, unsigned int scale,
196                                           unsigned int low, unsigned int high,
197                                           unsigned int total)
198 {
199     rc->value -= scale * (total - high);
200     rc->range  = low ? scale * (high - low)
201                       : rc->range - scale * (total - high);
202     opus_rc_normalize(rc);
203 }
204
205 static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf)
206 {
207     unsigned int k, scale, total, symbol, low, high;
208
209     total = *cdf++;
210
211     scale   = rc->range / total;
212     symbol = rc->value / scale + 1;
213     symbol = total - FFMIN(symbol, total);
214
215     for (k = 0; cdf[k] <= symbol; k++);
216     high = cdf[k];
217     low  = k ? cdf[k-1] : 0;
218
219     opus_rc_update(rc, scale, low, high, total);
220
221     return k;
222 }
223
224 static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
225 {
226     unsigned int k, scale;
227     scale = rc->range >> bits; // in this case, scale = symbol
228
229     if (rc->value >= scale) {
230         rc->value -= scale;
231         rc->range -= scale;
232         k = 0;
233     } else {
234         rc->range = scale;
235         k = 1;
236     }
237     opus_rc_normalize(rc);
238     return k;
239 }
240
241 /**
242  * CELT: estimate bits of entropy that have thus far been consumed for the
243  *       current CELT frame, to integer and fractional (1/8th bit) precision
244  */
245 static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc)
246 {
247     return rc->total_read_bits - av_log2(rc->range) - 1;
248 }
249
250 static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc)
251 {
252     unsigned int i, total_bits, rcbuffer, range;
253
254     total_bits = rc->total_read_bits << 3;
255     rcbuffer   = av_log2(rc->range) + 1;
256     range      = rc->range >> (rcbuffer-16);
257
258     for (i = 0; i < 3; i++) {
259         int bit;
260         range = range * range >> 15;
261         bit = range >> 16;
262         rcbuffer = rcbuffer << 1 | bit;
263         range >>= bit;
264     }
265
266     return total_bits - rcbuffer;
267 }
268
269 /**
270  * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
271  */
272 static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigned int count)
273 {
274     unsigned int value = 0;
275
276     while (rc->rb.bytes && rc->rb.cachelen < count) {
277         rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
278         rc->rb.cachelen += 8;
279         rc->rb.bytes--;
280     }
281
282     value = rc->rb.cacheval & ((1<<count)-1);
283     rc->rb.cacheval    >>= count;
284     rc->rb.cachelen     -= count;
285     rc->total_read_bits += count;
286
287     return value;
288 }
289
290 /**
291  * CELT: read a uniform distribution
292  */
293 static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size)
294 {
295     unsigned int bits, k, scale, total;
296
297     bits  = opus_ilog(size - 1);
298     total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
299
300     scale  = rc->range / total;
301     k      = rc->value / scale + 1;
302     k      = total - FFMIN(k, total);
303     opus_rc_update(rc, scale, k, k + 1, total);
304
305     if (bits > 8) {
306         k = k << (bits - 8) | opus_getrawbits(rc, bits - 8);
307         return FFMIN(k, size - 1);
308     } else
309         return k;
310 }
311
312 static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay)
313 {
314     /* extends the range coder to model a Laplace distribution */
315     int value = 0;
316     unsigned int scale, low = 0, center;
317
318     scale  = rc->range >> 15;
319     center = rc->value / scale + 1;
320     center = (1 << 15) - FFMIN(center, 1 << 15);
321
322     if (center >= symbol) {
323         value++;
324         low = symbol;
325         symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
326
327         while (symbol > 1 && center >= low + 2 * symbol) {
328             value++;
329             symbol *= 2;
330             low    += symbol;
331             symbol  = (((symbol - 2) * decay) >> 15) + 1;
332         }
333
334         if (symbol <= 1) {
335             int distance = (center - low) >> 1;
336             value += distance;
337             low   += 2 * distance;
338         }
339
340         if (center < low + symbol)
341             value *= -1;
342         else
343             low += symbol;
344     }
345
346     opus_rc_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
347
348     return value;
349 }
350
351 static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0)
352 {
353     /* Use a probability of 3 up to itheta=8192 and then use 1 after */
354     unsigned int k, scale, symbol, total = (k0+1)*3 + k0;
355     scale  = rc->range / total;
356     symbol = rc->value / scale + 1;
357     symbol = total - FFMIN(symbol, total);
358
359     k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
360
361     opus_rc_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
362                    (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
363     return k;
364 }
365
366 static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn)
367 {
368     unsigned int k, scale, symbol, total, low, center;
369
370     total = ((qn>>1) + 1) * ((qn>>1) + 1);
371     scale   = rc->range / total;
372     center = rc->value / scale + 1;
373     center = total - FFMIN(center, total);
374
375     if (center < total >> 1) {
376         k      = (ff_sqrt(8 * center + 1) - 1) >> 1;
377         low    = k * (k + 1) >> 1;
378         symbol = k + 1;
379     } else {
380         k      = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
381         low    = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
382         symbol = qn + 1 - k;
383     }
384
385     opus_rc_update(rc, scale, low, low + symbol, total);
386
387     return k;
388 }
389
390 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
391                          int self_delimited);
392
393 int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s);
394
395 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
396 void ff_silk_free(SilkContext **ps);
397 void ff_silk_flush(SilkContext *s);
398
399 /**
400  * Decode the LP layer of one Opus frame (which may correspond to several SILK
401  * frames).
402  */
403 int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc,
404                               float *output[2],
405                               enum OpusBandwidth bandwidth, int coded_channels,
406                               int duration_ms);
407
408 int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels);
409
410 void ff_celt_free(CeltContext **s);
411
412 void ff_celt_flush(CeltContext *s);
413
414 int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc,
415                          float **output, int coded_channels, int frame_size,
416                          int startband,  int endband);
417
418 extern const float ff_celt_window2[120];
419
420 #endif /* AVCODEC_OPUS_H */