FFmpeg coverage


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