FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opusdec.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 282 391 72.1%
Branches: 155 234 66.2%

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