FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus/dec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 283 394 71.8%
Functions: 10 11 90.9%
Branches: 156 236 66.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 "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 26277 static int get_silk_samplerate(int config)
132 {
133
2/2
✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 24726 times.
26277 if (config < 4)
134 1551 return 8000;
135
2/2
✓ Branch 0 taken 1223 times.
✓ Branch 1 taken 23503 times.
24726 else if (config < 8)
136 1223 return 12000;
137 23503 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 34451 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
238 {
239 34451 int samples = s->packet.frame_duration;
240 34451 int redundancy = 0;
241 int redundancy_size, redundancy_pos;
242 int ret, i, consumed;
243 34451 int delayed_samples = s->delayed_samples;
244
245 34451 ret = ff_opus_rc_dec_init(&s->rc, data, size);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34451 times.
34451 if (ret < 0)
247 return ret;
248
249 /* decode the silk frame */
250
4/4
✓ Branch 0 taken 29917 times.
✓ Branch 1 taken 4534 times.
✓ Branch 2 taken 4989 times.
✓ Branch 3 taken 24928 times.
34451 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 24928 ff_silk_flush(s->silk);
276
277 // decode redundancy information
278 34451 consumed = opus_rc_tell(&s->rc);
279
4/4
✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 29462 times.
✓ Branch 2 taken 4955 times.
✓ Branch 3 taken 34 times.
34451 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 24962 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 4520 times.
29496 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 34421 times.
34451 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 24928 times.
✓ Branch 2 taken 4989 times.
✓ Branch 3 taken 4534 times.
64368 if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
307 29917 float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
308 59834 float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
309
2/2
✓ Branch 0 taken 24928 times.
✓ Branch 1 taken 4989 times.
29917 out_tmp : s->celt_output;
310 29917 int celt_output_samples = samples;
311 29917 int delay_samples = av_audio_fifo_size(s->celt_delay);
312
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29917 times.
29917 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_drain(s->celt_delay, delay_samples);
327 if (s->avctx->err_recognition & AV_EF_EXPLODE)
328 return AVERROR_BUG;
329 }
330 }
331
332 29917 ff_opus_rc_dec_raw_init(&s->rc, data + size, size);
333
334 29917 ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
335 29917 s->packet.stereo + 1,
336 s->packet.frame_duration,
337 29917 (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
338
2/2
✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 24928 times.
29917 ff_celt_band_end[s->packet.bandwidth]);
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29917 times.
29917 if (ret < 0)
340 return ret;
341
342
2/2
✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 24928 times.
29917 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 34451 times.
34451 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 34421 times.
34451 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 34451 return samples;
394 }
395
396 26278 static int opus_decode_subpacket(OpusStreamContext *s,
397 const uint8_t *buf, int buf_size,
398 int nb_samples)
399 {
400 26278 int output_samples = 0;
401 26278 int flush_needed = 0;
402 int i, j, ret;
403
404 26278 s->cur_out[0] = s->out[0];
405 26278 s->cur_out[1] = s->out[1];
406 26278 s->remaining_out_size = s->out_size;
407
408 /* check if we need to flush the resampler */
409
2/2
✓ Branch 1 taken 9061 times.
✓ Branch 2 taken 17217 times.
26278 if (swr_is_initialized(s->swr)) {
410
2/2
✓ Branch 0 taken 9060 times.
✓ Branch 1 taken 1 times.
9061 if (buf) {
411 int64_t cur_samplerate;
412 9060 av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
413
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);
414 } else {
415 1 flush_needed = !!s->delayed_samples;
416 }
417 }
418
419
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26277 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
26278 if (!buf && !flush_needed)
420 return 0;
421
422 /* use dummy output buffers if the channel is not mapped to anything */
423
1/2
✓ Branch 0 taken 26278 times.
✗ Branch 1 not taken.
26278 if (!s->cur_out[0] ||
424
3/4
✓ Branch 0 taken 23208 times.
✓ Branch 1 taken 3070 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23208 times.
26278 (s->output_channels == 2 && !s->cur_out[1])) {
425 av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
426 s->remaining_out_size);
427 if (!s->out_dummy)
428 return AVERROR(ENOMEM);
429 if (!s->cur_out[0])
430 s->cur_out[0] = s->out_dummy;
431 if (!s->cur_out[1])
432 s->cur_out[1] = s->out_dummy;
433 }
434
435 /* flush the resampler if necessary */
436
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 26261 times.
26278 if (flush_needed) {
437 17 ret = opus_flush_resample(s, s->delayed_samples);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0) {
439 av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
440 return ret;
441 }
442 17 swr_close(s->swr);
443 17 output_samples += s->delayed_samples;
444 17 s->delayed_samples = 0;
445
446
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
17 if (!buf)
447 1 goto finish;
448 }
449
450 /* decode all the frames in the packet */
451
2/2
✓ Branch 0 taken 34451 times.
✓ Branch 1 taken 26277 times.
60728 for (i = 0; i < s->packet.frame_count; i++) {
452 34451 int size = s->packet.frame_size[i];
453 34451 int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
454
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34451 times.
34451 if (samples < 0) {
456 av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
457 if (s->avctx->err_recognition & AV_EF_EXPLODE)
458 return samples;
459
460 for (j = 0; j < s->output_channels; j++)
461 memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
462 samples = s->packet.frame_duration;
463 }
464 34451 output_samples += samples;
465
466
2/2
✓ Branch 0 taken 65832 times.
✓ Branch 1 taken 34451 times.
100283 for (j = 0; j < s->output_channels; j++)
467 65832 s->cur_out[j] += samples;
468 34451 s->remaining_out_size -= samples * sizeof(float);
469 }
470
471 26277 finish:
472 26278 s->cur_out[0] = s->cur_out[1] = NULL;
473 26278 s->remaining_out_size = 0;
474
475 26278 return output_samples;
476 }
477
478 21695 static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame,
479 int *got_frame_ptr, AVPacket *avpkt)
480 {
481 21695 OpusContext *c = avctx->priv_data;
482 21695 const uint8_t *buf = avpkt->data;
483 21695 int buf_size = avpkt->size;
484 21695 int coded_samples = 0;
485 21695 int decoded_samples = INT_MAX;
486 21695 int delayed_samples = 0;
487 int i, ret;
488
489 /* calculate the number of delayed samples */
490
2/2
✓ Branch 0 taken 26295 times.
✓ Branch 1 taken 21695 times.
47990 for (int i = 0; i < c->p.nb_streams; i++) {
491 26295 OpusStreamContext *s = &c->streams[i];
492 26295 s->out[0] =
493 26295 s->out[1] = NULL;
494
1/2
✓ Branch 1 taken 26295 times.
✗ Branch 2 not taken.
26295 delayed_samples = FFMAX(delayed_samples,
495 s->delayed_samples + av_audio_fifo_size(s->sync_buffer));
496 }
497
498 /* decode the header of the first sub-packet to find out the sample count */
499
2/2
✓ Branch 0 taken 21680 times.
✓ Branch 1 taken 15 times.
21695 if (buf) {
500 21680 OpusPacket *pkt = &c->streams[0].packet;
501 21680 ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21680 times.
21680 if (ret < 0) {
503 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
504 return ret;
505 }
506 21680 coded_samples += pkt->frame_count * pkt->frame_duration;
507 21680 c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
508 }
509
510 21695 frame->nb_samples = coded_samples + delayed_samples;
511
512 /* no input or buffered data => nothing to do */
513
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 21681 times.
21695 if (!frame->nb_samples) {
514 14 *got_frame_ptr = 0;
515 14 return 0;
516 }
517
518 /* setup the data buffers */
519 21681 ret = ff_get_buffer(avctx, frame, 0);
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21681 times.
21681 if (ret < 0)
521 return ret;
522 21681 frame->nb_samples = 0;
523
524
2/2
✓ Branch 0 taken 49486 times.
✓ Branch 1 taken 21681 times.
71167 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
525 49486 ChannelMap *map = &c->p.channel_maps[i];
526
1/2
✓ Branch 0 taken 49486 times.
✗ Branch 1 not taken.
49486 if (!map->copy)
527 49486 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 26278 times.
✓ Branch 1 taken 21681 times.
47959 for (int i = 0; i < c->p.nb_streams; i++) {
532 26278 OpusStreamContext *s = &c->streams[i];
533 26278 float **out = s->out;
534 26278 int sync_size = av_audio_fifo_size(s->sync_buffer);
535
536 float sync_dummy[32];
537
2/2
✓ Branch 0 taken 3070 times.
✓ Branch 1 taken 23208 times.
26278 int out_dummy = (!out[0]) | ((!out[1]) << 1);
538
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26278 times.
26278 if (!out[0])
540 out[0] = sync_dummy;
541
2/2
✓ Branch 0 taken 3070 times.
✓ Branch 1 taken 23208 times.
26278 if (!out[1])
542 3070 out[1] = sync_dummy;
543
3/4
✓ Branch 0 taken 3070 times.
✓ Branch 1 taken 23208 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3070 times.
26278 if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
544 return AVERROR_BUG;
545
546 26278 ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26278 times.
26278 if (ret < 0)
548 return ret;
549
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26278 times.
26278 if (out_dummy & 1)
551 out[0] = NULL;
552 else
553 26278 out[0] += ret;
554
2/2
✓ Branch 0 taken 3070 times.
✓ Branch 1 taken 23208 times.
26278 if (out_dummy & 2)
555 3070 out[1] = NULL;
556 else
557 23208 out[1] += ret;
558
559 26278 s->out_size = frame->linesize[0] - ret * sizeof(float);
560 }
561
562 /* decode each sub-packet */
563
2/2
✓ Branch 0 taken 26278 times.
✓ Branch 1 taken 21681 times.
47959 for (int i = 0; i < c->p.nb_streams; i++) {
564 26278 OpusStreamContext *s = &c->streams[i];
565
566
3/4
✓ Branch 0 taken 4597 times.
✓ Branch 1 taken 21681 times.
✓ Branch 2 taken 4597 times.
✗ Branch 3 not taken.
26278 if (i && buf) {
567 4597 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 4597 times.
4597 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 4597 times.
4597 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 4597 s->silk_samplerate = get_silk_samplerate(s->packet.config);
579 }
580
581 26278 ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
582 coded_samples);
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26278 times.
26278 if (ret < 0)
584 return ret;
585 26278 s->decoded_samples = ret;
586 26278 decoded_samples = FFMIN(decoded_samples, ret);
587
588 26278 buf += s->packet.packet_size;
589 26278 buf_size -= s->packet.packet_size;
590 }
591
592 /* buffer the extra samples */
593
2/2
✓ Branch 0 taken 26278 times.
✓ Branch 1 taken 21681 times.
47959 for (int i = 0; i < c->p.nb_streams; i++) {
594 26278 OpusStreamContext *s = &c->streams[i];
595 26278 int buffer_samples = s->decoded_samples - decoded_samples;
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26278 times.
26278 if (buffer_samples) {
597 float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
598 s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
599 buf[0] += decoded_samples;
600 buf[1] += decoded_samples;
601 ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
602 if (ret < 0)
603 return ret;
604 }
605 }
606
607
2/2
✓ Branch 0 taken 49486 times.
✓ Branch 1 taken 21681 times.
71167 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
608 49486 ChannelMap *map = &c->p.channel_maps[i];
609
610 /* handle copied channels */
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49486 times.
49486 if (map->copy) {
612 memcpy(frame->extended_data[i],
613 frame->extended_data[map->copy_idx],
614 frame->linesize[0]);
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49486 times.
49486 } else if (map->silence) {
616 memset(frame->extended_data[i], 0, frame->linesize[0]);
617 }
618
619
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49486 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49486 if (c->p.gain_i && decoded_samples > 0) {
620 c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
621 (float*)frame->extended_data[i],
622 c->gain, FFALIGN(decoded_samples, 8));
623 }
624 }
625
626 21681 frame->nb_samples = decoded_samples;
627 21681 *got_frame_ptr = !!decoded_samples;
628
629 21681 return avpkt->size;
630 }
631
632 static av_cold void opus_decode_flush(AVCodecContext *ctx)
633 {
634 OpusContext *c = ctx->priv_data;
635
636 for (int i = 0; i < c->p.nb_streams; i++) {
637 OpusStreamContext *s = &c->streams[i];
638
639 memset(&s->packet, 0, sizeof(s->packet));
640 s->delayed_samples = 0;
641
642 av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
643 swr_close(s->swr);
644
645 av_audio_fifo_drain(s->sync_buffer, av_audio_fifo_size(s->sync_buffer));
646
647 ff_silk_flush(s->silk);
648 ff_celt_flush(s->celt);
649 }
650 }
651
652 72 static av_cold int opus_decode_close(AVCodecContext *avctx)
653 {
654 72 OpusContext *c = avctx->priv_data;
655
656
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 72 times.
158 for (int i = 0; i < c->p.nb_streams; i++) {
657 86 OpusStreamContext *s = &c->streams[i];
658
659 86 ff_silk_free(&s->silk);
660 86 ff_celt_free(&s->celt);
661
662 86 av_freep(&s->out_dummy);
663 86 s->out_dummy_allocated_size = 0;
664
665 86 av_audio_fifo_free(s->sync_buffer);
666 86 av_audio_fifo_free(s->celt_delay);
667 86 swr_free(&s->swr);
668 }
669
670 72 av_freep(&c->streams);
671
672 72 c->p.nb_streams = 0;
673
674 72 av_freep(&c->p.channel_maps);
675 72 av_freep(&c->fdsp);
676
677 72 return 0;
678 }
679
680 72 static av_cold int opus_decode_init(AVCodecContext *avctx)
681 {
682 72 OpusContext *c = avctx->priv_data;
683 int ret;
684
685 72 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
686 72 avctx->sample_rate = 48000;
687
688 72 c->fdsp = avpriv_float_dsp_alloc(0);
689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (!c->fdsp)
690 return AVERROR(ENOMEM);
691
692 /* find out the channel configuration */
693 72 ret = ff_opus_parse_extradata(avctx, &c->p);
694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (ret < 0)
695 return ret;
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (c->p.gain_i)
697 c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
698
699 /* allocate and init each independent decoder */
700 72 c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (!c->streams) {
702 c->p.nb_streams = 0;
703 return AVERROR(ENOMEM);
704 }
705
706
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 72 times.
158 for (int i = 0; i < c->p.nb_streams; i++) {
707 86 OpusStreamContext *s = &c->streams[i];
708 AVChannelLayout layout;
709
710
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 29 times.
86 s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
711
712 86 s->avctx = avctx;
713
714
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 86 times.
229 for (int j = 0; j < s->output_channels; j++) {
715 143 s->silk_output[j] = s->silk_buf[j];
716 143 s->celt_output[j] = s->celt_buf[j];
717 143 s->redundancy_output[j] = s->redundancy_buf[j];
718 }
719
720 86 s->fdsp = c->fdsp;
721
722 86 s->swr =swr_alloc();
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (!s->swr)
724 return AVERROR(ENOMEM);
725
726
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 57 times.
86 layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
727 (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
728 86 av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
729 86 av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
730 86 av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
731 86 av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
732 86 av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
733 86 av_opt_set_int(s->swr, "filter_size", 16, 0);
734
735 86 ret = ff_silk_init(avctx, &s->silk, s->output_channels);
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (ret < 0)
737 return ret;
738
739 86 ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (ret < 0)
741 return ret;
742
743 86 s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
744 s->output_channels, 1024);
745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (!s->celt_delay)
746 return AVERROR(ENOMEM);
747
748 86 s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
749 s->output_channels, 32);
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (!s->sync_buffer)
751 return AVERROR(ENOMEM);
752 }
753
754 72 return 0;
755 }
756
757 #define OFFSET(x) offsetof(OpusContext, x)
758 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
759 static const AVOption opus_options[] = {
760 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
761 { NULL },
762 };
763
764 static const AVClass opus_class = {
765 .class_name = "Opus Decoder",
766 .item_name = av_default_item_name,
767 .option = opus_options,
768 .version = LIBAVUTIL_VERSION_INT,
769 };
770
771 const FFCodec ff_opus_decoder = {
772 .p.name = "opus",
773 CODEC_LONG_NAME("Opus"),
774 .p.priv_class = &opus_class,
775 .p.type = AVMEDIA_TYPE_AUDIO,
776 .p.id = AV_CODEC_ID_OPUS,
777 .priv_data_size = sizeof(OpusContext),
778 .init = opus_decode_init,
779 .close = opus_decode_close,
780 FF_CODEC_DECODE_CB(opus_decode_packet),
781 .flush = opus_decode_flush,
782 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF,
783 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
784 };
785