FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus/dec.c
Date: 2025-07-18 13:13:50
Exec Total Coverage
Lines: 286 397 72.0%
Functions: 10 11 90.9%
Branches: 157 236 66.5%

Line Branch Exec Source
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
38 #include "libavutil/attributes.h"
39 #include "libavutil/audio_fifo.h"
40 #include "libavutil/channel_layout.h"
41 #include "libavutil/ffmath.h"
42 #include "libavutil/float_dsp.h"
43 #include "libavutil/frame.h"
44 #include "libavutil/mem.h"
45 #include "libavutil/mem_internal.h"
46 #include "libavutil/opt.h"
47
48 #include "libswresample/swresample.h"
49
50 #include "avcodec.h"
51 #include "codec_internal.h"
52 #include "decode.h"
53 #include "opus.h"
54 #include "tab.h"
55 #include "celt.h"
56 #include "parse.h"
57 #include "rc.h"
58 #include "silk.h"
59
60 static const uint16_t silk_frame_duration_ms[16] = {
61 10, 20, 40, 60,
62 10, 20, 40, 60,
63 10, 20, 40, 60,
64 10, 20,
65 10, 20,
66 };
67
68 /* number of samples of silence to feed to the resampler
69 * at the beginning */
70 static const int silk_resample_delay[] = {
71 4, 8, 11, 11, 11
72 };
73
74 typedef struct OpusStreamContext {
75 AVCodecContext *avctx;
76 int output_channels;
77
78 /* number of decoded samples for this stream */
79 int decoded_samples;
80 /* current output buffers for this stream */
81 float *out[2];
82 int out_size;
83 /* Buffer with samples from this stream for synchronizing
84 * the streams when they have different resampling delays */
85 AVAudioFifo *sync_buffer;
86
87 OpusRangeCoder rc;
88 OpusRangeCoder redundancy_rc;
89 SilkContext *silk;
90 CeltFrame *celt;
91 AVFloatDSPContext *fdsp;
92
93 float silk_buf[2][960];
94 float *silk_output[2];
95 DECLARE_ALIGNED(32, float, celt_buf)[2][960];
96 float *celt_output[2];
97
98 DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
99 float *redundancy_output[2];
100
101 /* buffers for the next samples to be decoded */
102 float *cur_out[2];
103 int remaining_out_size;
104
105 float *out_dummy;
106 int out_dummy_allocated_size;
107
108 SwrContext *swr;
109 AVAudioFifo *celt_delay;
110 int silk_samplerate;
111 /* number of samples we still want to get from the resampler */
112 int delayed_samples;
113
114 OpusPacket packet;
115
116 int redundancy_idx;
117 } OpusStreamContext;
118
119 typedef struct OpusContext {
120 AVClass *av_class;
121
122 struct OpusStreamContext *streams;
123 int apply_phase_inv;
124
125 AVFloatDSPContext *fdsp;
126 float gain;
127
128 OpusParseContext p;
129 } OpusContext;
130
131 26302 static int get_silk_samplerate(int config)
132 {
133
2/2
✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 24751 times.
26302 if (config < 4)
134 1551 return 8000;
135
2/2
✓ Branch 0 taken 1223 times.
✓ Branch 1 taken 23528 times.
24751 else if (config < 8)
136 1223 return 12000;
137 23528 return 16000;
138 }
139
140 68 static void opus_fade(float *out,
141 const float *in1, const float *in2,
142 const float *window, int len)
143 {
144 int i;
145
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 68 times.
7268 for (i = 0; i < len; i++)
146 7200 out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
147 68 }
148
149 17 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
150 {
151 17 int celt_size = av_audio_fifo_size(s->celt_delay);
152 int ret, i;
153 17 ret = swr_convert(s->swr,
154 17 (uint8_t**)s->cur_out, nb_samples,
155 NULL, 0);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
157 return ret;
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 else if (ret != nb_samples) {
159 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
160 ret);
161 return AVERROR_BUG;
162 }
163
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (celt_size) {
165 if (celt_size != nb_samples) {
166 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
167 return AVERROR_BUG;
168 }
169 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
170 for (i = 0; i < s->output_channels; i++) {
171 s->fdsp->vector_fmac_scalar(s->cur_out[i],
172 s->celt_output[i], 1.0,
173 nb_samples);
174 }
175 }
176
177
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 13 times.
17 if (s->redundancy_idx) {
178
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < s->output_channels; i++)
179 8 opus_fade(s->cur_out[i], s->cur_out[i],
180 8 s->redundancy_output[i] + 120 + s->redundancy_idx,
181 8 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
182 4 s->redundancy_idx = 0;
183 }
184
185 17 s->cur_out[0] += nb_samples;
186 17 s->cur_out[1] += nb_samples;
187 17 s->remaining_out_size -= nb_samples * sizeof(float);
188
189 17 return 0;
190 }
191
192 35 static int opus_init_resample(OpusStreamContext *s)
193 {
194 static const float delay[16] = { 0.0 };
195 35 const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
196 int ret;
197
198 35 av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
199 35 ret = swr_init(s->swr);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0) {
201 av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
202 return ret;
203 }
204
205 35 ret = swr_convert(s->swr,
206 NULL, 0,
207 35 delayptr, silk_resample_delay[s->packet.bandwidth]);
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0) {
209 av_log(s->avctx, AV_LOG_ERROR,
210 "Error feeding initial silence to the resampler.\n");
211 return ret;
212 }
213
214 35 return 0;
215 }
216
217 30 static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
218 {
219 30 int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
221 goto fail;
222 30 ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
223
224 30 ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
225 30 s->redundancy_output,
226 30 s->packet.stereo + 1, 240,
227 30 0, ff_celt_band_end[s->packet.bandwidth]);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
229 goto fail;
230
231 30 return 0;
232 fail:
233 av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
234 return ret;
235 }
236
237 34476 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
238 {
239 34476 int samples = s->packet.frame_duration;
240 34476 int redundancy = 0;
241 int redundancy_size, redundancy_pos;
242 int ret, i, consumed;
243 34476 int delayed_samples = s->delayed_samples;
244
245 34476 ret = ff_opus_rc_dec_init(&s->rc, data, size);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34476 times.
34476 if (ret < 0)
247 return ret;
248
249 /* decode the silk frame */
250
4/4
✓ Branch 0 taken 29942 times.
✓ Branch 1 taken 4534 times.
✓ Branch 2 taken 4989 times.
✓ Branch 3 taken 24953 times.
34476 if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
251
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 9488 times.
9523 if (!swr_is_initialized(s->swr)) {
252 35 ret = opus_init_resample(s);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0)
254 return ret;
255 }
256
257 9523 samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
258 9523 FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
259 9523 s->packet.stereo + 1,
260 9523 silk_frame_duration_ms[s->packet.config]);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9523 times.
9523 if (samples < 0) {
262 av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
263 return samples;
264 }
265 9523 samples = swr_convert(s->swr,
266 9523 (uint8_t**)s->cur_out, s->packet.frame_duration,
267 9523 (const uint8_t**)s->silk_output, samples);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9523 times.
9523 if (samples < 0) {
269 av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
270 return samples;
271 }
272 av_assert2((samples & 7) == 0);
273 9523 s->delayed_samples += s->packet.frame_duration - samples;
274 } else
275 24953 ff_silk_flush(s->silk);
276
277 // decode redundancy information
278 34476 consumed = opus_rc_tell(&s->rc);
279
4/4
✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 29487 times.
✓ Branch 2 taken 4955 times.
✓ Branch 3 taken 34 times.
34476 if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
280 4955 redundancy = ff_opus_rc_dec_log(&s->rc, 12);
281
4/4
✓ Branch 0 taken 4534 times.
✓ Branch 1 taken 24987 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 4520 times.
29521 else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
282 14 redundancy = 1;
283
284
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 34446 times.
34476 if (redundancy) {
285 30 redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
286
287
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 14 times.
30 if (s->packet.mode == OPUS_MODE_HYBRID)
288 16 redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
289 else
290 14 redundancy_size = size - (consumed + 7) / 8;
291 30 size -= redundancy_size;
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (size < 0) {
293 av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
294 return AVERROR_INVALIDDATA;
295 }
296
297
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 16 times.
30 if (redundancy_pos) {
298 14 ret = opus_decode_redundancy(s, data + size, redundancy_size);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0)
300 return ret;
301 14 ff_celt_flush(s->celt);
302 }
303 }
304
305 /* decode the CELT frame */
306
4/4
✓ Branch 0 taken 9523 times.
✓ Branch 1 taken 24953 times.
✓ Branch 2 taken 4989 times.
✓ Branch 3 taken 4534 times.
64418 if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
307 29942 float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
308 59884 float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
309
2/2
✓ Branch 0 taken 24953 times.
✓ Branch 1 taken 4989 times.
29942 out_tmp : s->celt_output;
310 29942 int celt_output_samples = samples;
311 29942 int delay_samples = av_audio_fifo_size(s->celt_delay);
312
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29942 times.
29942 if (delay_samples) {
314 if (s->packet.mode == OPUS_MODE_HYBRID) {
315 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
316
317 for (i = 0; i < s->output_channels; i++) {
318 s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
319 delay_samples);
320 out_tmp[i] += delay_samples;
321 }
322 celt_output_samples -= delay_samples;
323 } else {
324 av_log(s->avctx, AV_LOG_WARNING,
325 "Spurious CELT delay samples present.\n");
326 av_audio_fifo_reset(s->celt_delay);
327 if (s->avctx->err_recognition & AV_EF_EXPLODE)
328 return AVERROR_BUG;
329 }
330 }
331
332 29942 ff_opus_rc_dec_raw_init(&s->rc, data + size, size);
333
334 29942 ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
335 29942 s->packet.stereo + 1,
336 s->packet.frame_duration,
337 29942 (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
338
2/2
✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 24953 times.
29942 ff_celt_band_end[s->packet.bandwidth]);
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29942 times.
29942 if (ret < 0)
340 return ret;
341
342
2/2
✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 24953 times.
29942 if (s->packet.mode == OPUS_MODE_HYBRID) {
343 4989 int celt_delay = s->packet.frame_duration - celt_output_samples;
344 4989 void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
345 4989 s->celt_output[1] + celt_output_samples };
346
347
2/2
✓ Branch 0 taken 9978 times.
✓ Branch 1 taken 4989 times.
14967 for (i = 0; i < s->output_channels; i++) {
348 9978 s->fdsp->vector_fmac_scalar(out_tmp[i],
349 9978 s->celt_output[i], 1.0,
350 celt_output_samples);
351 }
352
353 4989 ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4989 times.
4989 if (ret < 0)
355 return ret;
356 }
357 } else
358 4534 ff_celt_flush(s->celt);
359
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34476 times.
34476 if (s->redundancy_idx) {
361 for (i = 0; i < s->output_channels; i++)
362 opus_fade(s->cur_out[i], s->cur_out[i],
363 s->redundancy_output[i] + 120 + s->redundancy_idx,
364 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
365 s->redundancy_idx = 0;
366 }
367
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 34446 times.
34476 if (redundancy) {
368
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 14 times.
30 if (!redundancy_pos) {
369 16 ff_celt_flush(s->celt);
370 16 ret = opus_decode_redundancy(s, data + size, redundancy_size);
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
372 return ret;
373
374
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (i = 0; i < s->output_channels; i++) {
375 32 opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
376 32 s->cur_out[i] + samples - 120 + delayed_samples,
377 32 s->redundancy_output[i] + 120,
378 ff_celt_window2, 120 - delayed_samples);
379
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 24 times.
32 if (delayed_samples)
380 8 s->redundancy_idx = 120 - delayed_samples;
381 }
382 } else {
383
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 for (i = 0; i < s->output_channels; i++) {
384 28 memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
385 28 opus_fade(s->cur_out[i] + 120 + delayed_samples,
386 28 s->redundancy_output[i] + 120,
387 28 s->cur_out[i] + 120 + delayed_samples,
388 ff_celt_window2, 120);
389 }
390 }
391 }
392
393 34476 return samples;
394 }
395
396 26303 static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
397 {
398 26303 int output_samples = 0;
399 26303 int flush_needed = 0;
400 int i, j, ret;
401
402 26303 s->cur_out[0] = s->out[0];
403 26303 s->cur_out[1] = s->out[1];
404 26303 s->remaining_out_size = s->out_size;
405
406 /* check if we need to flush the resampler */
407
2/2
✓ Branch 1 taken 9061 times.
✓ Branch 2 taken 17242 times.
26303 if (swr_is_initialized(s->swr)) {
408
2/2
✓ Branch 0 taken 9060 times.
✓ Branch 1 taken 1 times.
9061 if (buf) {
409 int64_t cur_samplerate;
410 9060 av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
411
4/4
✓ Branch 0 taken 9050 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 9044 times.
9060 flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
412 } else {
413 1 flush_needed = !!s->delayed_samples;
414 }
415 }
416
417
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26302 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
26303 if (!buf && !flush_needed)
418 return 0;
419
420 /* use dummy output buffers if the channel is not mapped to anything */
421
1/2
✓ Branch 0 taken 26303 times.
✗ Branch 1 not taken.
26303 if (!s->cur_out[0] ||
422
3/4
✓ Branch 0 taken 23214 times.
✓ Branch 1 taken 3089 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23214 times.
26303 (s->output_channels == 2 && !s->cur_out[1])) {
423 av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
424 s->remaining_out_size);
425 if (!s->out_dummy)
426 return AVERROR(ENOMEM);
427 if (!s->cur_out[0])
428 s->cur_out[0] = s->out_dummy;
429 if (!s->cur_out[1])
430 s->cur_out[1] = s->out_dummy;
431 }
432
433 /* flush the resampler if necessary */
434
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 26286 times.
26303 if (flush_needed) {
435 17 ret = opus_flush_resample(s, s->delayed_samples);
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0) {
437 av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
438 return ret;
439 }
440 17 swr_close(s->swr);
441 17 output_samples += s->delayed_samples;
442 17 s->delayed_samples = 0;
443
444
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
17 if (!buf)
445 1 goto finish;
446 }
447
448 /* decode all the frames in the packet */
449
2/2
✓ Branch 0 taken 34476 times.
✓ Branch 1 taken 26302 times.
60778 for (i = 0; i < s->packet.frame_count; i++) {
450 34476 int size = s->packet.frame_size[i];
451 34476 int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
452
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34476 times.
34476 if (samples < 0) {
454 av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
455 if (s->avctx->err_recognition & AV_EF_EXPLODE)
456 return samples;
457
458 for (j = 0; j < s->output_channels; j++)
459 memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
460 samples = s->packet.frame_duration;
461 }
462 34476 output_samples += samples;
463
464
2/2
✓ Branch 0 taken 65863 times.
✓ Branch 1 taken 34476 times.
100339 for (j = 0; j < s->output_channels; j++)
465 65863 s->cur_out[j] += samples;
466 34476 s->remaining_out_size -= samples * sizeof(float);
467 }
468
469 26302 finish:
470 26303 s->cur_out[0] = s->cur_out[1] = NULL;
471 26303 s->remaining_out_size = 0;
472
473 26303 return output_samples;
474 }
475
476 21711 static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame,
477 int *got_frame_ptr, AVPacket *avpkt)
478 {
479 21711 OpusContext *c = avctx->priv_data;
480 21711 const uint8_t *buf = avpkt->data;
481 21711 int buf_size = avpkt->size;
482 21711 int coded_samples = 0;
483 21711 int decoded_samples = INT_MAX;
484 21711 int delayed_samples = 0;
485 int i, ret;
486
487 /* calculate the number of delayed samples */
488
2/2
✓ Branch 0 taken 26320 times.
✓ Branch 1 taken 21711 times.
48031 for (int i = 0; i < c->p.nb_streams; i++) {
489 26320 OpusStreamContext *s = &c->streams[i];
490 26320 s->out[0] =
491 26320 s->out[1] = NULL;
492 26320 int fifo_samples = av_audio_fifo_size(s->sync_buffer);
493 26320 delayed_samples = FFMAX(delayed_samples,
494 s->delayed_samples + fifo_samples);
495 }
496
497 /* decode the header of the first sub-packet to find out the sample count */
498
2/2
✓ Branch 0 taken 21696 times.
✓ Branch 1 taken 15 times.
21711 if (buf) {
499 21696 OpusPacket *pkt = &c->streams[0].packet;
500 21696 ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21696 times.
21696 if (ret < 0) {
502 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
503 return ret;
504 }
505 21696 coded_samples += pkt->frame_count * pkt->frame_duration;
506 21696 c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
507 }
508
509 21711 frame->nb_samples = coded_samples + delayed_samples;
510
511 /* no input or buffered data => nothing to do */
512
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 21697 times.
21711 if (!frame->nb_samples) {
513 14 *got_frame_ptr = 0;
514 14 return 0;
515 }
516
517 /* setup the data buffers */
518 21697 ret = ff_get_buffer(avctx, frame, 0);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21697 times.
21697 if (ret < 0)
520 return ret;
521 21697 frame->nb_samples = 0;
522
523
2/2
✓ Branch 0 taken 49517 times.
✓ Branch 1 taken 21697 times.
71214 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
524 49517 ChannelMap *map = &c->p.channel_maps[i];
525
1/2
✓ Branch 0 taken 49517 times.
✗ Branch 1 not taken.
49517 if (!map->copy)
526 49517 c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
527 }
528
529 /* read the data from the sync buffers */
530
2/2
✓ Branch 0 taken 26303 times.
✓ Branch 1 taken 21697 times.
48000 for (int i = 0; i < c->p.nb_streams; i++) {
531 26303 OpusStreamContext *s = &c->streams[i];
532 26303 float **out = s->out;
533 26303 int sync_size = av_audio_fifo_size(s->sync_buffer);
534
535 float sync_dummy[32];
536
2/2
✓ Branch 0 taken 3089 times.
✓ Branch 1 taken 23214 times.
26303 int out_dummy = (!out[0]) | ((!out[1]) << 1);
537
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26303 times.
26303 if (!out[0])
539 out[0] = sync_dummy;
540
2/2
✓ Branch 0 taken 3089 times.
✓ Branch 1 taken 23214 times.
26303 if (!out[1])
541 3089 out[1] = sync_dummy;
542
3/4
✓ Branch 0 taken 3089 times.
✓ Branch 1 taken 23214 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3089 times.
26303 if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
543 return AVERROR_BUG;
544
545 26303 ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26303 times.
26303 if (ret < 0)
547 return ret;
548
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26303 times.
26303 if (out_dummy & 1)
550 out[0] = NULL;
551 else
552 26303 out[0] += ret;
553
2/2
✓ Branch 0 taken 3089 times.
✓ Branch 1 taken 23214 times.
26303 if (out_dummy & 2)
554 3089 out[1] = NULL;
555 else
556 23214 out[1] += ret;
557
558 26303 s->out_size = frame->linesize[0] - ret * sizeof(float);
559 }
560
561 /* decode each sub-packet */
562
2/2
✓ Branch 0 taken 26303 times.
✓ Branch 1 taken 21697 times.
48000 for (int i = 0; i < c->p.nb_streams; i++) {
563 26303 OpusStreamContext *s = &c->streams[i];
564
565
3/4
✓ Branch 0 taken 4606 times.
✓ Branch 1 taken 21697 times.
✓ Branch 2 taken 4606 times.
✗ Branch 3 not taken.
26303 if (i && buf) {
566 4606 ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->p.nb_streams - 1);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4606 times.
4606 if (ret < 0) {
568 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
569 return ret;
570 }
571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4606 times.
4606 if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
572 av_log(avctx, AV_LOG_ERROR,
573 "Mismatching coded sample count in substream %d.\n", i);
574 return AVERROR_INVALIDDATA;
575 }
576
577 4606 s->silk_samplerate = get_silk_samplerate(s->packet.config);
578 }
579
580 26303 ret = opus_decode_subpacket(&c->streams[i], buf);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26303 times.
26303 if (ret < 0)
582 return ret;
583 26303 s->decoded_samples = ret;
584 26303 decoded_samples = FFMIN(decoded_samples, ret);
585
586
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26302 times.
26303 if (!buf)
587 1 continue;
588
589 26302 buf += s->packet.packet_size;
590 26302 buf_size -= s->packet.packet_size;
591 }
592
593 /* buffer the extra samples */
594
2/2
✓ Branch 0 taken 26303 times.
✓ Branch 1 taken 21697 times.
48000 for (int i = 0; i < c->p.nb_streams; i++) {
595 26303 OpusStreamContext *s = &c->streams[i];
596 26303 int buffer_samples = s->decoded_samples - decoded_samples;
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26303 times.
26303 if (buffer_samples) {
598 float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
599 s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
600 buf[0] += decoded_samples;
601 buf[1] += decoded_samples;
602 ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
603 if (ret < 0)
604 return ret;
605 }
606 }
607
608
2/2
✓ Branch 0 taken 49517 times.
✓ Branch 1 taken 21697 times.
71214 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
609 49517 ChannelMap *map = &c->p.channel_maps[i];
610
611 /* handle copied channels */
612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49517 times.
49517 if (map->copy) {
613 memcpy(frame->extended_data[i],
614 frame->extended_data[map->copy_idx],
615 frame->linesize[0]);
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49517 times.
49517 } else if (map->silence) {
617 memset(frame->extended_data[i], 0, frame->linesize[0]);
618 }
619
620
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49517 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49517 if (c->p.gain_i && decoded_samples > 0) {
621 c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
622 (float*)frame->extended_data[i],
623 c->gain, FFALIGN(decoded_samples, 8));
624 }
625 }
626
627 21697 frame->nb_samples = decoded_samples;
628 21697 *got_frame_ptr = !!decoded_samples;
629
630 21697 return avpkt->size;
631 }
632
633 static av_cold void opus_decode_flush(AVCodecContext *ctx)
634 {
635 OpusContext *c = ctx->priv_data;
636
637 for (int i = 0; i < c->p.nb_streams; i++) {
638 OpusStreamContext *s = &c->streams[i];
639
640 memset(&s->packet, 0, sizeof(s->packet));
641 s->delayed_samples = 0;
642
643 av_audio_fifo_reset(s->celt_delay);
644 swr_close(s->swr);
645
646 av_audio_fifo_reset(s->sync_buffer);
647
648 ff_silk_flush(s->silk);
649 ff_celt_flush(s->celt);
650 }
651 }
652
653 78 static av_cold int opus_decode_close(AVCodecContext *avctx)
654 {
655 78 OpusContext *c = avctx->priv_data;
656
657
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 78 times.
182 for (int i = 0; i < c->p.nb_streams; i++) {
658 104 OpusStreamContext *s = &c->streams[i];
659
660 104 ff_silk_free(&s->silk);
661 104 ff_celt_free(&s->celt);
662
663 104 av_freep(&s->out_dummy);
664 104 s->out_dummy_allocated_size = 0;
665
666 104 av_audio_fifo_free(s->sync_buffer);
667 104 av_audio_fifo_free(s->celt_delay);
668 104 swr_free(&s->swr);
669 }
670
671 78 av_freep(&c->streams);
672
673 78 c->p.nb_streams = 0;
674
675 78 av_freep(&c->p.channel_maps);
676 78 av_freep(&c->fdsp);
677
678 78 return 0;
679 }
680
681 78 static av_cold int opus_decode_init(AVCodecContext *avctx)
682 {
683 78 OpusContext *c = avctx->priv_data;
684 int ret;
685
686 78 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
687 78 avctx->sample_rate = 48000;
688
689 78 c->fdsp = avpriv_float_dsp_alloc(0);
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!c->fdsp)
691 return AVERROR(ENOMEM);
692
693 /* find out the channel configuration */
694 78 ret = ff_opus_parse_extradata(avctx, &c->p);
695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
696 return ret;
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (c->p.gain_i)
698 c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
699
700 /* allocate and init each independent decoder */
701 78 c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!c->streams) {
703 c->p.nb_streams = 0;
704 return AVERROR(ENOMEM);
705 }
706
707
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 78 times.
182 for (int i = 0; i < c->p.nb_streams; i++) {
708 104 OpusStreamContext *s = &c->streams[i];
709 AVChannelLayout layout;
710
711
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 39 times.
104 s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
712
713 104 s->avctx = avctx;
714
715
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 104 times.
273 for (int j = 0; j < s->output_channels; j++) {
716 169 s->silk_output[j] = s->silk_buf[j];
717 169 s->celt_output[j] = s->celt_buf[j];
718 169 s->redundancy_output[j] = s->redundancy_buf[j];
719 }
720
721 104 s->fdsp = c->fdsp;
722
723 104 s->swr =swr_alloc();
724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (!s->swr)
725 return AVERROR(ENOMEM);
726
727
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 65 times.
104 layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
728 (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
729 104 av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
730 104 av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
731 104 av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
732 104 av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
733 104 av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
734 104 av_opt_set_int(s->swr, "filter_size", 16, 0);
735
736 104 ret = ff_silk_init(avctx, &s->silk, s->output_channels);
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (ret < 0)
738 return ret;
739
740 104 ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (ret < 0)
742 return ret;
743
744 104 s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
745 s->output_channels, 1024);
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (!s->celt_delay)
747 return AVERROR(ENOMEM);
748
749 104 s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
750 s->output_channels, 32);
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (!s->sync_buffer)
752 return AVERROR(ENOMEM);
753 }
754
755 78 return 0;
756 }
757
758 #define OFFSET(x) offsetof(OpusContext, x)
759 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
760 static const AVOption opus_options[] = {
761 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
762 { NULL },
763 };
764
765 static const AVClass opus_class = {
766 .class_name = "Opus Decoder",
767 .item_name = av_default_item_name,
768 .option = opus_options,
769 .version = LIBAVUTIL_VERSION_INT,
770 };
771
772 const FFCodec ff_opus_decoder = {
773 .p.name = "opus",
774 CODEC_LONG_NAME("Opus"),
775 .p.priv_class = &opus_class,
776 .p.type = AVMEDIA_TYPE_AUDIO,
777 .p.id = AV_CODEC_ID_OPUS,
778 .priv_data_size = sizeof(OpusContext),
779 .init = opus_decode_init,
780 .close = opus_decode_close,
781 FF_CODEC_DECODE_CB(opus_decode_packet),
782 .flush = opus_decode_flush,
783 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF,
784 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
785 };
786