1 /*
2 * Opus decoder
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 /**
24 * @file
25 * Opus decoder
26 * @author Andrew D'Addesio, Anton Khirnov
27 *
28 * Codec homepage: http://opus-codec.org/
29 * Specification: http://tools.ietf.org/html/rfc6716
30 * Ogg Opus specification: https://tools.ietf.org/html/draft-ietf-codec-oggopus-03
31 *
32 * Ogg-contained .opus files can be produced with opus-tools:
33 * http://git.xiph.org/?p=opus-tools.git
34 */
35
36 #include <stdint.h>
37
42
44
50
52 10, 20, 40, 60,
53 10, 20, 40, 60,
54 10, 20, 40, 60,
55 10, 20,
56 10, 20,
57 };
58
59 /* number of samples of silence to feed to the resampler
60 * at the beginning */
62 4, 8, 11, 11, 11
63 };
64
66
68 {
69 if (config < 4)
70 return 8000;
71 else if (config < 8)
72 return 12000;
73 return 16000;
74 }
75
76 /**
77 * Range decoder
78 */
80 {
82 if (ret < 0)
83 return ret;
84
89
90 return 0;
91 }
92
94 unsigned int bytes)
95 {
100 }
101
103 const float *in1, const float *in2,
104 const float *window,
int len)
105 {
106 int i;
107 for (i = 0; i <
len; i++)
108 out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
109 }
110
112 {
114 int ret, i;
118 if (ret < 0)
119 return ret;
120 else if (ret != nb_samples) {
122 ret);
124 }
125
126 if (celt_size) {
127 if (celt_size != nb_samples) {
130 }
135 nb_samples);
136 }
137 }
138
145 }
146
147 s->
out[0] += nb_samples;
148 s->
out[1] += nb_samples;
149 s->
out_size -= nb_samples *
sizeof(float);
150
151 return 0;
152 }
153
155 {
156 static const float delay[16] = { 0.0 };
158 int ret;
159
162 if (ret < 0) {
164 return ret;
165 }
166
170 if (ret < 0) {
172 "Error feeding initial silence to the resampler.\n");
173 return ret;
174 }
175
176 return 0;
177 }
178
180 {
181 int ret;
183
187
189 if (ret < 0)
192
197 if (ret < 0)
199
200 return 0;
203 return ret;
204 }
205
207 {
209 int redundancy = 0;
210 int redundancy_size, redundancy_pos;
211 int ret, i, consumed;
213
215 if (ret < 0)
216 return ret;
217
218 /* decode the silk frame */
222 if (ret < 0)
223 return ret;
224 }
225
230 if (samples < 0) {
232 return samples;
233 }
237 if (samples < 0) {
239 return samples;
240 }
243 } else
245
246 // decode redundancy information
251 redundancy = 1;
252
253 if (redundancy) {
255
258 else
259 redundancy_size = size - (consumed + 7) / 8;
260 size -= redundancy_size;
261 if (size < 0) {
264 }
265
266 if (redundancy_pos) {
268 if (ret < 0)
269 return ret;
271 }
272 }
273
274 /* decode the CELT frame */
276 float *out_tmp[2] = { s->
out[0], s->
out[1] };
279 int celt_output_samples = samples;
281
282 if (delay_samples) {
285
288 delay_samples);
289 out_tmp[i] += delay_samples;
290 }
291 celt_output_samples -= delay_samples;
292 } else {
294 "Spurious CELT delay samples present.\n");
298 }
299 }
300
302
308 if (ret < 0)
309 return ret;
310
313 void *delaybuf[2] = { s->
celt_output[0] + celt_output_samples,
315
319 celt_output_samples);
320 }
321
323 if (ret < 0)
324 return ret;
325 }
326 } else
328
335 }
336 if (redundancy) {
337 if (!redundancy_pos) {
340 if (ret < 0)
341 return ret;
342
345 s->
out[i] + samples - 120 + delayed_samples,
348 if (delayed_samples)
350 }
351 } else {
356 s->
out[i] + 120 + delayed_samples,
358 }
359 }
360 }
361
362 return samples;
363 }
364
368 int nb_samples)
369 {
370 int output_samples = 0;
371 int flush_needed = 0;
372 int i, j, ret;
373
377
378 /* check if we need to flush the resampler */
380 if (buf) {
381 int64_t cur_samplerate;
384 } else {
386 }
387 }
388
389 if (!buf && !flush_needed)
390 return 0;
391
392 /* use dummy output buffers if the channel is not mapped to anything */
402 }
403
404 /* flush the resampler if necessary */
405 if (flush_needed) {
407 if (ret < 0) {
409 return ret;
410 }
414
415 if (!buf)
417 }
418
419 /* decode all the frames in the packet */
423
424 if (samples < 0) {
427 return samples;
428
432 }
433 output_samples += samples;
434
436 s->
out[j] += samples;
437 s->
out_size -= samples *
sizeof(
float);
438 }
439
443
444 return output_samples;
445 }
446
448 int *got_frame_ptr,
AVPacket *avpkt)
449 {
453 int buf_size = avpkt->
size;
454 int coded_samples = 0;
455 int decoded_samples = INT_MAX;
456 int delayed_samples = 0;
457 int i, ret;
458
459 /* calculate the number of delayed samples */
464 delayed_samples =
FFMAX(delayed_samples,
466 }
467
468 /* decode the header of the first sub-packet to find out the sample count */
469 if (buf) {
472 if (ret < 0) {
474 return ret;
475 }
478 }
479
480 frame->
nb_samples = coded_samples + delayed_samples;
481
482 /* no input or buffered data => nothing to do */
484 *got_frame_ptr = 0;
485 return 0;
486 }
487
488 /* setup the data buffers */
490 if (ret < 0)
491 return ret;
493
495 for (i = 0; i < avctx->
channels; i++) {
499 }
500
501 /* read the data from the sync buffers */
503 float **
out = c->
out + 2 * i;
505
506 float sync_dummy[32];
507 int out_dummy = (!out[0]) | ((!out[1]) << 1);
508
509 if (!out[0])
510 out[0] = sync_dummy;
511 if (!out[1])
512 out[1] = sync_dummy;
515
517 if (ret < 0)
518 return ret;
519
520 if (out_dummy & 1)
522 else
523 out[0] += ret;
524 if (out_dummy & 2)
526 else
527 out[1] += ret;
528
530 }
531
532 /* decode each sub-packet */
535
536 if (i && buf) {
538 if (ret < 0) {
540 return ret;
541 }
544 "Mismatching coded sample count in substream %d.\n", i);
546 }
547
549 }
550
553 if (ret < 0)
554 return ret;
556 decoded_samples =
FFMIN(decoded_samples, ret);
557
560 }
561
562 /* buffer the extra samples */
565 if (buffer_samples) {
568 buf[0] += decoded_samples;
569 buf[1] += decoded_samples;
571 if (ret < 0)
572 return ret;
573 }
574 }
575
576 for (i = 0; i < avctx->
channels; i++) {
578
579 /* handle copied channels */
586 }
587
588 if (c->
gain_i && decoded_samples > 0) {
592 }
593 }
594
596 *got_frame_ptr = !!decoded_samples;
597
599 }
600
602 {
604 int i;
605
608
611
615
617
620 }
621 }
622
624 {
626 int i;
627
630
633
636
639 }
640
642
646 }
651
653
656
657 return 0;
658 }
659
661 {
663 int ret, i, j;
664
667
671
672 /* find out the channel configuration */
674 if (ret < 0) {
677 return ret;
678 }
679
680 /* allocate and init each independent decoder */
690 }
691
695
697
699
704 }
705
707
711
719
721 if (ret < 0)
723
725 if (ret < 0)
727
733 }
734
740 }
741 }
742
743 return 0;
746 return ret;
747 }
748
760 };
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.
static const uint8_t celt_band_end[]
static av_cold int opus_decode_close(AVCodecContext *avctx)
void ff_celt_flush(CeltContext *s)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
This structure describes decoded (raw) audio or video data.
AVAudioFifo ** sync_buffers
ptrdiff_t const GLvoid * data
static void flush(AVCodecContext *avctx)
static const uint16_t silk_frame_duration_ms[16]
int frame_count
frame count
float redundancy_buf[2][960]
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, unsigned int bytes)
#define AV_LOG_WARNING
Something somehow does not look correct.
static av_cold int init(AVCodecContext *avctx)
static FFServerConfig config
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
#define AV_CH_LAYOUT_STEREO
Macro definitions for various function/variable attributes.
void(* vector_fmac_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float and add to destination vector.
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
enum AVSampleFormat sample_fmt
audio sample format
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
unsigned int total_read_bits
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
void ff_celt_free(CeltContext **s)
bitstream reader API header.
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
const float ff_celt_window2[120]
static av_cold int opus_decode_init(AVCodecContext *avctx)
ChannelMap * channel_maps
libswresample public header
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
const char * name
Name of the codec implementation.
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels)
audio channel layout utility functions
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
int frame_size[MAX_FRAMES]
frame sizes
int frame_duration
frame duration, in samples @ 48kHz
int out_dummy_allocated_size
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
#define AV_EF_EXPLODE
abort decoding on minor error detection
#define FF_ARRAY_ELEMS(a)
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
static int get_silk_samplerate(int config)
Libavcodec external API header.
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
void ff_silk_flush(SilkContext *s)
main external API structure.
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
int config
configuration: tells the audio mode, bandwidth, and frame duration
void ff_silk_free(SilkContext **ps)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
int stereo
whether this packet is mono or stereo
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count, const uint8_t *in_arg[SWR_CH_MAX], int in_count)
int data_size
size of the useful data – packet size - padding
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
static int opus_decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
static int opus_init_resample(OpusStreamContext *s)
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf, int buf_size, float **out, int out_size, int nb_samples)
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
common internal api header.
static const int silk_resample_delay[]
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).
static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size)
CELT: read a uniform distribution.
int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, float **output, int coded_channels, int frame_size, int startband, int endband)
OpusStreamContext * streams
int packet_size
packet size
OpusRangeCoder redundancy_rc
int channels
number of audio channels
int frame_offset[MAX_FRAMES]
frame offsets
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...
enum OpusBandwidth bandwidth
bandwidth
static av_cold void opus_decode_flush(AVCodecContext *ctx)
static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Range decoder.
float * redundancy_output[2]
static void * av_mallocz_array(size_t nmemb, size_t size)
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
uint8_t ** extended_data
pointers to the data planes/channels.
#define AV_CH_LAYOUT_MONO
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.