FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus/dec.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 287 398 72.1%
Functions: 10 11 90.9%
Branches: 161 240 67.1%

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 26799 static int get_silk_samplerate(int config)
133 {
134
2/2
✓ Branch 0 taken 1598 times.
✓ Branch 1 taken 25201 times.
26799 if (config < 4)
135 1598 return 8000;
136
2/2
✓ Branch 0 taken 1223 times.
✓ Branch 1 taken 23978 times.
25201 else if (config < 8)
137 1223 return 12000;
138 23978 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 18 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
151 {
152 18 int celt_size = av_audio_fifo_size(s->celt_delay);
153 int ret, i;
154 18 ret = swr_convert(s->swr,
155 18 (uint8_t**)s->cur_out, nb_samples,
156 NULL, 0);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
158 ✗ return ret;
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 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 18 times.
18 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 14 times.
18 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
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 18 times.
53 for (i = 0; i < s->output_channels; i++)
187 35 s->cur_out[i] += nb_samples;
188 18 s->remaining_out_size -= nb_samples * sizeof(float);
189
190 18 return 0;
191 }
192
193 43 static int opus_init_resample(OpusStreamContext *s)
194 {
195 static const float delay[16] = { 0.0 };
196 43 const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
197 int ret;
198
199 43 av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
200 43 ret = swr_init(s->swr);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0) {
202 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
203 ✗ return ret;
204 }
205
206 43 ret = swr_convert(s->swr,
207 NULL, 0,
208 43 delayptr, silk_resample_delay[s->packet.bandwidth]);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 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 43 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 34973 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
239 {
240 34973 int samples = s->packet.frame_duration;
241 34973 int redundancy = 0;
242 int redundancy_size, redundancy_pos;
243 int ret, i, consumed;
244 34973 int delayed_samples = s->delayed_samples;
245
246 34973 ret = ff_opus_rc_dec_init(&s->rc, data, size);
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34973 times.
34973 if (ret < 0)
248 ✗ return ret;
249
250 /* decode the silk frame */
251
4/4
✓ Branch 0 taken 30392 times.
✓ Branch 1 taken 4581 times.
✓ Branch 2 taken 5013 times.
✓ Branch 3 taken 25379 times.
34973 if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
252
2/2
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 9551 times.
9594 if (!swr_is_initialized(s->swr)) {
253 43 ret = opus_init_resample(s);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
255 ✗ return ret;
256 }
257
258 9594 samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
259 9594 FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
260 9594 s->packet.stereo + 1,
261 9594 silk_frame_duration_ms[s->packet.config]);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9594 times.
9594 if (samples < 0) {
263 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
264 ✗ return samples;
265 }
266 9594 samples = swr_convert(s->swr,
267 9594 (uint8_t**)s->cur_out, s->packet.frame_duration,
268 9594 (const uint8_t**)s->silk_output, samples);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9594 times.
9594 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 9594 s->delayed_samples += s->packet.frame_duration - samples;
275 } else
276 25379 ff_silk_flush(s->silk);
277
278 // decode redundancy information
279 34973 consumed = opus_rc_tell(&s->rc);
280
4/4
✓ Branch 0 taken 5013 times.
✓ Branch 1 taken 29960 times.
✓ Branch 2 taken 4979 times.
✓ Branch 3 taken 34 times.
34973 if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
281 4979 redundancy = ff_opus_rc_dec_log(&s->rc, 12);
282
4/4
✓ Branch 0 taken 4581 times.
✓ Branch 1 taken 25413 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 4567 times.
29994 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 34943 times.
34973 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 9594 times.
✓ Branch 1 taken 25379 times.
✓ Branch 2 taken 5013 times.
✓ Branch 3 taken 4581 times.
65365 if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
308 30392 float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
309 60784 float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
310
2/2
✓ Branch 0 taken 25379 times.
✓ Branch 1 taken 5013 times.
30392 out_tmp : s->celt_output;
311 30392 int celt_output_samples = samples;
312 30392 int delay_samples = av_audio_fifo_size(s->celt_delay);
313
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30392 times.
30392 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 30392 ff_opus_rc_dec_raw_init(&s->rc, data + size, size);
334
335 30392 ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
336 30392 s->packet.stereo + 1,
337 s->packet.frame_duration,
338 30392 (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
339
2/2
✓ Branch 0 taken 5013 times.
✓ Branch 1 taken 25379 times.
30392 ff_celt_band_end[s->packet.bandwidth]);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30392 times.
30392 if (ret < 0)
341 ✗ return ret;
342
343
2/2
✓ Branch 0 taken 5013 times.
✓ Branch 1 taken 25379 times.
30392 if (s->packet.mode == OPUS_MODE_HYBRID) {
344 5013 int celt_delay = s->packet.frame_duration - celt_output_samples;
345 void *delaybuf[2];
346
347
2/2
✓ Branch 0 taken 10026 times.
✓ Branch 1 taken 5013 times.
15039 for (i = 0; i < s->output_channels; i++) {
348 10026 delaybuf[i] = s->celt_output[i] + celt_output_samples;
349 10026 s->fdsp->vector_fmac_scalar(out_tmp[i],
350 10026 s->celt_output[i], 1.0,
351 celt_output_samples);
352 }
353
354 5013 ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5013 times.
5013 if (ret < 0)
356 ✗ return ret;
357 }
358 } else
359 4581 ff_celt_flush(s->celt);
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34973 times.
34973 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 34943 times.
34973 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 34973 return samples;
395 }
396
397 26801 static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
398 {
399 26801 int output_samples = 0;
400 26801 int flush_needed = 0;
401 int i, j, ret;
402
403 26801 s->cur_out[0] = s->out[0];
404 26801 s->cur_out[1] = s->out[1];
405 26801 s->remaining_out_size = s->out_size;
406
407 /* check if we need to flush the resampler */
408
2/2
✓ Branch 1 taken 9125 times.
✓ Branch 2 taken 17676 times.
26801 if (swr_is_initialized(s->swr)) {
409
2/2
✓ Branch 0 taken 9123 times.
✓ Branch 1 taken 2 times.
9125 if (buf) {
410 int64_t cur_samplerate;
411 9123 av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
412
4/4
✓ Branch 0 taken 9113 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 9107 times.
9123 flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
413 } else {
414 2 flush_needed = !!s->delayed_samples;
415 }
416 }
417
418
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 26799 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
26801 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 26801 times.
✗ Branch 1 not taken.
26801 if (!s->cur_out[0] ||
423
3/4
✓ Branch 0 taken 23568 times.
✓ Branch 1 taken 3233 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23568 times.
26801 (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 18 times.
✓ Branch 1 taken 26783 times.
26801 if (flush_needed) {
436 18 ret = opus_flush_resample(s, s->delayed_samples);
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0) {
438 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
439 ✗ return ret;
440 }
441 18 swr_close(s->swr);
442 18 output_samples += s->delayed_samples;
443 18 s->delayed_samples = 0;
444
445
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (!buf)
446 2 goto finish;
447 }
448
449 /* decode all the frames in the packet */
450
2/2
✓ Branch 0 taken 34973 times.
✓ Branch 1 taken 26799 times.
61772 for (i = 0; i < s->packet.frame_count; i++) {
451 34973 int size = s->packet.frame_size[i];
452 34973 int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
453
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34973 times.
34973 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 34973 output_samples += samples;
464
465
2/2
✓ Branch 0 taken 66714 times.
✓ Branch 1 taken 34973 times.
101687 for (j = 0; j < s->output_channels; j++)
466 66714 s->cur_out[j] += samples;
467 34973 s->remaining_out_size -= samples * sizeof(float);
468 }
469
470 26799 finish:
471 26801 s->cur_out[0] = s->cur_out[1] = NULL;
472 26801 s->remaining_out_size = 0;
473
474 26801 return output_samples;
475 }
476
477 22116 static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame,
478 int *got_frame_ptr, AVPacket *avpkt)
479 {
480 22116 OpusContext *c = avctx->priv_data;
481 22116 const uint8_t *buf = avpkt->data;
482 22116 int buf_size = avpkt->size;
483 22116 int coded_samples = 0;
484 22116 int decoded_samples = INT_MAX;
485 22116 int delayed_samples = 0;
486 int i, ret;
487
488 /* calculate the number of delayed samples */
489
2/2
✓ Branch 0 taken 26821 times.
✓ Branch 1 taken 22116 times.
48937 for (int i = 0; i < c->p.nb_streams; i++) {
490 26821 OpusStreamContext *s = &c->streams[i];
491 26821 s->out[0] =
492 26821 s->out[1] = NULL;
493 26821 int fifo_samples = av_audio_fifo_size(s->sync_buffer);
494 26821 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 22097 times.
✓ Branch 1 taken 19 times.
22116 if (buf) {
500 22097 OpusPacket *pkt = &c->streams[0].packet;
501 22097 ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22097 times.
22097 if (ret < 0) {
503 ✗ av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
504 ✗ return ret;
505 }
506 22097 coded_samples += pkt->frame_count * pkt->frame_duration;
507 22097 c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
508 }
509
510 22116 frame->sample_rate = 48000;
511 22116 frame->nb_samples = coded_samples + delayed_samples;
512
513 /* no input or buffered data => nothing to do */
514
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 22099 times.
22116 if (!frame->nb_samples) {
515 17 *got_frame_ptr = 0;
516 17 return 0;
517 }
518
519 /* setup the data buffers */
520 22099 ret = ff_get_buffer(avctx, frame, 0);
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22099 times.
22099 if (ret < 0)
522 ✗ return ret;
523 22099 frame->nb_samples = 0;
524
525
2/2
✓ Branch 0 taken 50369 times.
✓ Branch 1 taken 22099 times.
72468 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
526 50369 ChannelMap *map = &c->p.channel_maps[i];
527
1/2
✓ Branch 0 taken 50369 times.
✗ Branch 1 not taken.
50369 if (!map->copy)
528 50369 c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
529 }
530
531 /* read the data from the sync buffers */
532
2/2
✓ Branch 0 taken 26801 times.
✓ Branch 1 taken 22099 times.
48900 for (int i = 0; i < c->p.nb_streams; i++) {
533 26801 OpusStreamContext *s = &c->streams[i];
534 26801 float **out = s->out;
535 26801 int sync_size = av_audio_fifo_size(s->sync_buffer);
536
537 float sync_dummy[32];
538
2/2
✓ Branch 0 taken 3233 times.
✓ Branch 1 taken 23568 times.
26801 int out_dummy = (!out[0]) | ((!out[1]) << 1);
539
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26801 times.
26801 if (!out[0])
541 ✗ out[0] = sync_dummy;
542
2/2
✓ Branch 0 taken 3233 times.
✓ Branch 1 taken 23568 times.
26801 if (!out[1])
543 3233 out[1] = sync_dummy;
544
3/4
✓ Branch 0 taken 3233 times.
✓ Branch 1 taken 23568 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3233 times.
26801 if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
545 ✗ return AVERROR_BUG;
546
547 26801 ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26801 times.
26801 if (ret < 0)
549 ✗ return ret;
550
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26801 times.
26801 if (out_dummy & 1)
552 ✗ out[0] = NULL;
553 else
554 26801 out[0] += ret;
555
2/2
✓ Branch 0 taken 3233 times.
✓ Branch 1 taken 23568 times.
26801 if (out_dummy & 2)
556 3233 out[1] = NULL;
557 else
558 23568 out[1] += ret;
559
560 26801 s->out_size = frame->linesize[0] - ret * sizeof(float);
561 }
562
563 /* decode each sub-packet */
564
2/2
✓ Branch 0 taken 26801 times.
✓ Branch 1 taken 22099 times.
48900 for (int i = 0; i < c->p.nb_streams; i++) {
565 26801 OpusStreamContext *s = &c->streams[i];
566
567
3/4
✓ Branch 0 taken 4702 times.
✓ Branch 1 taken 22099 times.
✓ Branch 2 taken 4702 times.
✗ Branch 3 not taken.
26801 if (i && buf) {
568 4702 ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->p.nb_streams - 1);
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4702 times.
4702 if (ret < 0) {
570 ✗ av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
571 ✗ return ret;
572 }
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4702 times.
4702 if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
574 ✗ av_log(avctx, AV_LOG_ERROR,
575 "Mismatching coded sample count in substream %d.\n", i);
576 ✗ return AVERROR_INVALIDDATA;
577 }
578
579 4702 s->silk_samplerate = get_silk_samplerate(s->packet.config);
580 }
581
582 26801 ret = opus_decode_subpacket(&c->streams[i], buf);
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26801 times.
26801 if (ret < 0)
584 ✗ return ret;
585 26801 s->decoded_samples = ret;
586 26801 decoded_samples = FFMIN(decoded_samples, ret);
587
588
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 26799 times.
26801 if (!buf)
589 2 continue;
590
591 26799 buf += s->packet.packet_size;
592 26799 buf_size -= s->packet.packet_size;
593 }
594
595 /* buffer the extra samples */
596
2/2
✓ Branch 0 taken 26801 times.
✓ Branch 1 taken 22099 times.
48900 for (int i = 0; i < c->p.nb_streams; i++) {
597 26801 OpusStreamContext *s = &c->streams[i];
598 26801 int buffer_samples = s->decoded_samples - decoded_samples;
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26801 times.
26801 if (buffer_samples) {
600 ✗ float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
601 ✗ s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
602 ✗ buf[0] += decoded_samples;
603 ✗ buf[1] += decoded_samples;
604 ✗ ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
605 ✗ if (ret < 0)
606 ✗ return ret;
607 }
608 }
609
610
2/2
✓ Branch 0 taken 50369 times.
✓ Branch 1 taken 22099 times.
72468 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
611 50369 ChannelMap *map = &c->p.channel_maps[i];
612
613 /* handle copied channels */
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50369 times.
50369 if (map->copy) {
615 ✗ memcpy(frame->extended_data[i],
616 ✗ frame->extended_data[map->copy_idx],
617 ✗ frame->linesize[0]);
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50369 times.
50369 } else if (map->silence) {
619 ✗ memset(frame->extended_data[i], 0, frame->linesize[0]);
620 }
621
622
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50369 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50369 if (c->p.gain_i && decoded_samples > 0) {
623 ✗ c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
624 ✗ (float*)frame->extended_data[i],
625 ✗ c->gain, FFALIGN(decoded_samples, 8));
626 }
627 }
628
629 22099 frame->nb_samples = decoded_samples;
630 22099 *got_frame_ptr = !!decoded_samples;
631
632 22099 return avpkt->size;
633 }
634
635 ✗ static av_cold void opus_decode_flush(AVCodecContext *ctx)
636 {
637 ✗ OpusContext *c = ctx->priv_data;
638
639 ✗ for (int i = 0; i < c->p.nb_streams; i++) {
640 ✗ OpusStreamContext *s = &c->streams[i];
641
642 ✗ memset(&s->packet, 0, sizeof(s->packet));
643 ✗ s->delayed_samples = 0;
644
645 ✗ av_audio_fifo_reset(s->celt_delay);
646 ✗ swr_close(s->swr);
647
648 ✗ av_audio_fifo_reset(s->sync_buffer);
649
650 ✗ ff_silk_flush(s->silk);
651 ✗ ff_celt_flush(s->celt);
652 }
653 ✗ }
654
655 155 static av_cold int opus_decode_close(AVCodecContext *avctx)
656 {
657 155 OpusContext *c = avctx->priv_data;
658
659
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 155 times.
368 for (int i = 0; i < c->p.nb_streams; i++) {
660 213 OpusStreamContext *s = &c->streams[i];
661
662 213 ff_silk_free(&s->silk);
663 213 ff_celt_free(&s->celt);
664
665 213 av_freep(&s->out_dummy);
666 213 s->out_dummy_allocated_size = 0;
667
668 213 av_audio_fifo_free(s->sync_buffer);
669 213 av_audio_fifo_free(s->celt_delay);
670 213 swr_free(&s->swr);
671 }
672
673 155 av_freep(&c->streams);
674
675 155 c->p.nb_streams = 0;
676
677 155 av_freep(&c->p.channel_maps);
678 155 av_freep(&c->fdsp);
679
680 155 return 0;
681 }
682
683 155 static av_cold int opus_decode_init(AVCodecContext *avctx)
684 {
685 155 OpusContext *c = avctx->priv_data;
686 int ret;
687
688 155 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
689
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 150 times.
155 if (!avctx->sample_rate)
690 5 avctx->sample_rate = 48000;
691
692 155 c->fdsp = avpriv_float_dsp_alloc(0);
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (!c->fdsp)
694 ✗ return AVERROR(ENOMEM);
695
696 /* find out the channel configuration */
697 155 ret = ff_opus_parse_extradata(avctx, &c->p);
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (ret < 0)
699 ✗ return ret;
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (c->p.gain_i)
701 ✗ c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
702
703 /* allocate and init each independent decoder */
704 155 c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (!c->streams) {
706 ✗ c->p.nb_streams = 0;
707 ✗ return AVERROR(ENOMEM);
708 }
709
710
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 155 times.
368 for (int i = 0; i < c->p.nb_streams; i++) {
711 213 OpusStreamContext *s = &c->streams[i];
712 AVChannelLayout layout;
713
714
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 98 times.
213 s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
715
716 213 s->avctx = avctx;
717
718
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 213 times.
541 for (int j = 0; j < s->output_channels; j++) {
719 328 s->silk_output[j] = s->silk_buf[j];
720 328 s->celt_output[j] = s->celt_buf[j];
721 328 s->redundancy_output[j] = s->redundancy_buf[j];
722 }
723
724 213 s->fdsp = c->fdsp;
725
726 213 s->swr =swr_alloc();
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (!s->swr)
728 ✗ return AVERROR(ENOMEM);
729
730
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 115 times.
213 layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
731 (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
732 213 av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
733 213 av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
734 213 av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
735 213 av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
736 213 av_opt_set_int(s->swr, "out_sample_rate", 48000, 0);
737 213 av_opt_set_int(s->swr, "filter_size", 16, 0);
738
739 213 ret = ff_silk_init(avctx, &s->silk, s->output_channels);
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (ret < 0)
741 ✗ return ret;
742
743 213 ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (ret < 0)
745 ✗ return ret;
746
747 213 s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
748 s->output_channels, 1024);
749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (!s->celt_delay)
750 ✗ return AVERROR(ENOMEM);
751
752 213 s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
753 s->output_channels, 32);
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (!s->sync_buffer)
755 ✗ return AVERROR(ENOMEM);
756 }
757
758 155 return 0;
759 }
760
761 #define OFFSET(x) offsetof(OpusContext, x)
762 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
763 static const AVOption opus_options[] = {
764 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
765 { NULL },
766 };
767
768 static const AVClass opus_class = {
769 .class_name = "Opus Decoder",
770 .item_name = av_default_item_name,
771 .option = opus_options,
772 .version = LIBAVUTIL_VERSION_INT,
773 };
774
775 const FFCodec ff_opus_decoder = {
776 .p.name = "opus",
777 CODEC_LONG_NAME("Opus"),
778 .p.priv_class = &opus_class,
779 .p.type = AVMEDIA_TYPE_AUDIO,
780 .p.id = AV_CODEC_ID_OPUS,
781 .priv_data_size = sizeof(OpusContext),
782 .init = opus_decode_init,
783 .close = opus_decode_close,
784 FF_CODEC_DECODE_CB(opus_decode_packet),
785 .flush = opus_decode_flush,
786 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF,
787 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
788 };
789