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
52
54 10, 20, 40, 60,
55 10, 20, 40, 60,
56 10, 20, 40, 60,
57 10, 20,
58 10, 20,
59 };
60
61 /* number of samples of silence to feed to the resampler
62 * at the beginning */
64 4, 8, 11, 11, 11
65 };
66
68 {
69 if (config < 4)
70 return 8000;
71 else if (config < 8)
72 return 12000;
73 return 16000;
74 }
75
77 const float *in1, const float *in2,
79 {
80 int i;
81 for (i = 0; i <
len; i++)
82 out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
83 }
84
86 {
88 int ret, i;
92 if (ret < 0)
93 return ret;
94 else if (ret != nb_samples) {
96 ret);
98 }
99
100 if (celt_size) {
101 if (celt_size != nb_samples) {
104 }
109 nb_samples);
110 }
111 }
112
119 }
120
121 s->
out[0] += nb_samples;
122 s->
out[1] += nb_samples;
123 s->
out_size -= nb_samples *
sizeof(float);
124
125 return 0;
126 }
127
129 {
130 static const float delay[16] = { 0.0 };
132 int ret;
133
136 if (ret < 0) {
138 return ret;
139 }
140
144 if (ret < 0) {
146 "Error feeding initial silence to the resampler.\n");
147 return ret;
148 }
149
150 return 0;
151 }
152
154 {
156 if (ret < 0)
159
164 if (ret < 0)
166
167 return 0;
170 return ret;
171 }
172
174 {
176 int redundancy = 0;
177 int redundancy_size, redundancy_pos;
178 int ret, i, consumed;
180
182 if (ret < 0)
183 return ret;
184
185 /* decode the silk frame */
189 if (ret < 0)
190 return ret;
191 }
192
197 if (samples < 0) {
199 return samples;
200 }
204 if (samples < 0) {
206 return samples;
207 }
210 } else
212
213 // decode redundancy information
218 redundancy = 1;
219
220 if (redundancy) {
222
225 else
226 redundancy_size = size - (consumed + 7) / 8;
227 size -= redundancy_size;
228 if (size < 0) {
231 }
232
233 if (redundancy_pos) {
235 if (ret < 0)
236 return ret;
238 }
239 }
240
241 /* decode the CELT frame */
243 float *out_tmp[2] = { s->
out[0], s->
out[1] };
246 int celt_output_samples = samples;
248
249 if (delay_samples) {
252
255 delay_samples);
256 out_tmp[i] += delay_samples;
257 }
258 celt_output_samples -= delay_samples;
259 } else {
261 "Spurious CELT delay samples present.\n");
265 }
266 }
267
269
275 if (ret < 0)
276 return ret;
277
280 void *delaybuf[2] = { s->
celt_output[0] + celt_output_samples,
282
286 celt_output_samples);
287 }
288
290 if (ret < 0)
291 return ret;
292 }
293 } else
295
302 }
303 if (redundancy) {
304 if (!redundancy_pos) {
307 if (ret < 0)
308 return ret;
309
312 s->
out[i] + samples - 120 + delayed_samples,
315 if (delayed_samples)
317 }
318 } else {
323 s->
out[i] + 120 + delayed_samples,
325 }
326 }
327 }
328
329 return samples;
330 }
331
335 int nb_samples)
336 {
337 int output_samples = 0;
338 int flush_needed = 0;
339 int i, j, ret;
340
344
345 /* check if we need to flush the resampler */
347 if (buf) {
348 int64_t cur_samplerate;
351 } else {
353 }
354 }
355
356 if (!buf && !flush_needed)
357 return 0;
358
359 /* use dummy output buffers if the channel is not mapped to anything */
369 }
370
371 /* flush the resampler if necessary */
372 if (flush_needed) {
374 if (ret < 0) {
376 return ret;
377 }
381
382 if (!buf)
384 }
385
386 /* decode all the frames in the packet */
390
391 if (samples < 0) {
394 return samples;
395
399 }
400 output_samples += samples;
401
403 s->
out[j] += samples;
404 s->
out_size -= samples *
sizeof(
float);
405 }
406
410
411 return output_samples;
412 }
413
415 int *got_frame_ptr,
AVPacket *avpkt)
416 {
420 int buf_size = avpkt->
size;
421 int coded_samples = 0;
422 int decoded_samples = INT_MAX;
423 int delayed_samples = 0;
424 int i, ret;
425
426 /* calculate the number of delayed samples */
431 delayed_samples =
FFMAX(delayed_samples,
433 }
434
435 /* decode the header of the first sub-packet to find out the sample count */
436 if (buf) {
439 if (ret < 0) {
441 return ret;
442 }
445 }
446
447 frame->
nb_samples = coded_samples + delayed_samples;
448
449 /* no input or buffered data => nothing to do */
451 *got_frame_ptr = 0;
452 return 0;
453 }
454
455 /* setup the data buffers */
457 if (ret < 0)
458 return ret;
460
462 for (i = 0; i < avctx->
channels; i++) {
466 }
467
468 /* read the data from the sync buffers */
470 float **
out = c->
out + 2 * i;
472
473 float sync_dummy[32];
474 int out_dummy = (!out[0]) | ((!out[1]) << 1);
475
476 if (!out[0])
477 out[0] = sync_dummy;
478 if (!out[1])
479 out[1] = sync_dummy;
482
484 if (ret < 0)
485 return ret;
486
487 if (out_dummy & 1)
489 else
490 out[0] += ret;
491 if (out_dummy & 2)
493 else
494 out[1] += ret;
495
497 }
498
499 /* decode each sub-packet */
502
503 if (i && buf) {
505 if (ret < 0) {
507 return ret;
508 }
511 "Mismatching coded sample count in substream %d.\n", i);
513 }
514
516 }
517
520 if (ret < 0)
521 return ret;
523 decoded_samples =
FFMIN(decoded_samples, ret);
524
527 }
528
529 /* buffer the extra samples */
532 if (buffer_samples) {
535 buf[0] += decoded_samples;
536 buf[1] += decoded_samples;
538 if (ret < 0)
539 return ret;
540 }
541 }
542
543 for (i = 0; i < avctx->
channels; i++) {
545
546 /* handle copied channels */
553 }
554
555 if (c->
gain_i && decoded_samples > 0) {
559 }
560 }
561
563 *got_frame_ptr = !!decoded_samples;
564
566 }
567
569 {
571 int i;
572
575
578
582
584
587 }
588 }
589
591 {
593 int i;
594
597
600
603
606 }
607
609
613 }
618
620
623
624 return 0;
625 }
626
628 {
630 int ret, i, j;
631
634
638
639 /* find out the channel configuration */
641 if (ret < 0) {
643 return ret;
644 }
645
646 /* allocate and init each independent decoder */
656 }
657
661
663
665
670 }
671
673
677
685
687 if (ret < 0)
689
691 if (ret < 0)
693
699 }
700
706 }
707 }
708
709 return 0;
712 return ret;
713 }
714
715 #define OFFSET(x) offsetof(OpusContext, x)
716 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
718 {
"apply_phase_inv",
"Apply intensity stereo phase inversion",
OFFSET(apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
AD },
720 };
721
727 };
728
741 };
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 av_cold int opus_decode_close(AVCodecContext *avctx)
int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, int channels, int frame_size, int start_band, int end_band)
#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]
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
static const AVOption opus_options[]
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
const char * av_default_item_name(void *ptr)
Return the context name.
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
#define AV_CH_LAYOUT_STEREO
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Macro definitions for various function/variable attributes.
const uint8_t ff_celt_band_end[]
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.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#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.
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
bitstream reader API header.
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
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.
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void ff_celt_flush(CeltFrame *f)
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
const char * name
Name of the codec implementation.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
static SDL_Window * window
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
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
void ff_celt_free(CeltFrame **f)
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
static const AVClass opus_class
#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.
void ff_silk_flush(SilkContext *s)
main external API structure.
const float ff_celt_window2[120]
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame...
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)
Describe the class of an AVClass context structure.
void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
const VDPAUPixFmtMap * map
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)
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).
OpusStreamContext * streams
int packet_size
packet size
OpusRangeCoder redundancy_rc
int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
int channels
number of audio channels
int frame_offset[MAX_FRAMES]
frame offsets
enum OpusBandwidth bandwidth
bandwidth
static av_cold void opus_decode_flush(AVCodecContext *ctx)
float * redundancy_output[2]
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
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.
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
void * av_mallocz_array(size_t nmemb, size_t size)