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