FFmpeg
Loading...
Searching...
No Matches
dec.c
Go to the documentation of this file.
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
41#include "libavutil/ffmath.h"
42#include "libavutil/float_dsp.h"
43#include "libavutil/frame.h"
45#include "libavutil/mem.h"
47#include "libavutil/opt.h"
48
50
51#include "libavcodec/avcodec.h"
53#include "libavcodec/decode.h"
54
55#include "opus.h"
56#include "tab.h"
57#include "celt.h"
58#include "parse.h"
59#include "rc.h"
60#include "silk.h"
61
62static const uint16_t silk_frame_duration_ms[16] = {
63 10, 20, 40, 60,
64 10, 20, 40, 60,
65 10, 20, 40, 60,
66 10, 20,
67 10, 20,
68};
69
70/* number of samples of silence to feed to the resampler
71 * at the beginning */
72static const int silk_resample_delay[] = {
73 4, 8, 11, 11, 11
74};
75
76/* filter size of the resampler for each bandwidth, chosen so that the
77 * resampler needs no more input samples of lookahead than the silence
78 * above provides, so it does not hold back output samples */
79static const int silk_resample_filter_size[] = {
80 8, 16, 16, 16, 16
81};
82
83typedef struct OpusStreamContext {
86
87 /* number of decoded samples for this stream */
89 /* current output buffers for this stream */
90 float *out[2];
92 /* Buffer with samples from this stream for synchronizing
93 * the streams when they have different resampling delays */
95
101
102 float silk_buf[2][960];
103 float *silk_output[2];
104 DECLARE_ALIGNED(32, float, celt_buf)[2][960];
105 float *celt_output[2];
106
107 DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
109
110 /* buffers for the next samples to be decoded */
111 float *cur_out[2];
113
114 float *out_dummy;
116
120 /* number of samples we still want to get from the resampler */
122
124
127
139
140static int get_silk_samplerate(int config)
141{
142 if (config < 4)
143 return 8000;
144 else if (config < 8)
145 return 12000;
146 return 16000;
147}
148
149static void opus_fade(float *out,
150 const float *in1, const float *in2,
151 const float *window, int len)
152{
153 int i;
154 for (i = 0; i < len; i++)
155 out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
156}
157
158static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
159{
160 int celt_size = av_audio_fifo_size(s->celt_delay);
161 int ret, i;
162 ret = swr_convert(s->swr,
163 (uint8_t**)s->cur_out, nb_samples,
164 NULL, 0);
165 if (ret < 0)
166 return ret;
167 else if (ret != nb_samples) {
168 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
169 ret);
170 return AVERROR_BUG;
171 }
172
173 if (celt_size) {
174 if (celt_size != nb_samples) {
175 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
176 return AVERROR_BUG;
177 }
178 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
179 for (i = 0; i < s->output_channels; i++) {
180 s->fdsp->vector_fmac_scalar(s->cur_out[i],
181 s->celt_output[i], 1.0,
182 nb_samples);
183 }
184 }
185
186 if (s->redundancy_idx) {
187 for (i = 0; i < s->output_channels; i++)
188 opus_fade(s->cur_out[i], s->cur_out[i],
189 s->redundancy_output[i] + 120 + s->redundancy_idx,
190 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
191 s->redundancy_idx = 0;
192 }
193
194 for (i = 0; i < s->output_channels; i++)
195 s->cur_out[i] += nb_samples;
196 s->remaining_out_size -= nb_samples * sizeof(float);
197
198 return 0;
199}
200
202{
203 static const float delay[16] = { 0.0 };
204 const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
205 int ret;
206
207 av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
208 av_opt_set_int(s->swr, "filter_size",
209 silk_resample_filter_size[s->packet.bandwidth], 0);
210 ret = swr_init(s->swr);
211 if (ret < 0) {
212 av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
213 return ret;
214 }
215
216 ret = swr_convert(s->swr,
217 NULL, 0,
218 delayptr, silk_resample_delay[s->packet.bandwidth]);
219 if (ret < 0) {
220 av_log(s->avctx, AV_LOG_ERROR,
221 "Error feeding initial silence to the resampler.\n");
222 return ret;
223 }
224
225 return 0;
226}
227
228static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
229{
230 int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
231 if (ret < 0)
232 goto fail;
233 ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
234
235 ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
236 s->redundancy_output,
237 s->packet.stereo + 1, 240,
238 0, ff_celt_band_end[s->packet.bandwidth]);
239 if (ret < 0)
240 goto fail;
241
242 return 0;
243fail:
244 av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
245 return ret;
246}
247
248static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
249{
250 int samples = s->packet.frame_duration;
251 int redundancy = 0;
252 int redundancy_size, redundancy_pos;
253 int ret, i, consumed;
254 int delayed_samples = s->delayed_samples;
255
257 if (ret < 0)
258 return ret;
259
260 /* decode the silk frame */
261 if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
262 if (!swr_is_initialized(s->swr)) {
264 if (ret < 0)
265 return ret;
266 }
267
268 samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
269 FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
270 s->packet.stereo + 1,
271 silk_frame_duration_ms[s->packet.config]);
272 if (samples < 0) {
273 av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
274 return samples;
275 }
276 samples = swr_convert(s->swr,
277 (uint8_t**)s->cur_out, s->packet.frame_duration,
278 (const uint8_t**)s->silk_output, samples);
279 if (samples < 0) {
280 av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
281 return samples;
282 }
283 av_assert2((samples & 7) == 0);
284 s->delayed_samples += s->packet.frame_duration - samples;
285 } else
286 ff_silk_flush(s->silk);
287
288 // decode redundancy information
289 consumed = opus_rc_tell(&s->rc);
290 if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
291 redundancy = ff_opus_rc_dec_log(&s->rc, 12);
292 else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
293 redundancy = 1;
294
295 if (redundancy) {
296 redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
297
298 if (s->packet.mode == OPUS_MODE_HYBRID)
299 redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
300 else
301 redundancy_size = size - (consumed + 7) / 8;
302 size -= redundancy_size;
303 if (size < 0) {
304 av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
305 return AVERROR_INVALIDDATA;
306 }
307
308 if (redundancy_pos) {
309 ret = opus_decode_redundancy(s, data + size, redundancy_size);
310 if (ret < 0)
311 return ret;
312 ff_celt_flush(s->celt);
313 }
314 }
315
316 /* decode the CELT frame */
317 if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
318 float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
319 float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
320 out_tmp : s->celt_output;
321 int celt_output_samples = samples;
322 int delay_samples = av_audio_fifo_size(s->celt_delay);
323
324 if (delay_samples) {
325 if (s->packet.mode == OPUS_MODE_HYBRID) {
326 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
327
328 for (i = 0; i < s->output_channels; i++) {
329 s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
330 delay_samples);
331 out_tmp[i] += delay_samples;
332 }
333 celt_output_samples -= delay_samples;
334 } else {
335 av_log(s->avctx, AV_LOG_WARNING,
336 "Spurious CELT delay samples present.\n");
337 av_audio_fifo_reset(s->celt_delay);
338 if (s->avctx->err_recognition & AV_EF_EXPLODE)
339 return AVERROR_BUG;
340 }
341 }
342
344
345 ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
346 s->packet.stereo + 1,
347 s->packet.frame_duration,
348 (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
349 ff_celt_band_end[s->packet.bandwidth]);
350 if (ret < 0)
351 return ret;
352
353 if (s->packet.mode == OPUS_MODE_HYBRID) {
354 int celt_delay = s->packet.frame_duration - celt_output_samples;
355 void *delaybuf[2];
356
357 for (i = 0; i < s->output_channels; i++) {
358 delaybuf[i] = s->celt_output[i] + celt_output_samples;
359 s->fdsp->vector_fmac_scalar(out_tmp[i],
360 s->celt_output[i], 1.0,
361 celt_output_samples);
362 }
363
364 ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
365 if (ret < 0)
366 return ret;
367 }
368 } else
369 ff_celt_flush(s->celt);
370
371 if (s->redundancy_idx) {
372 for (i = 0; i < s->output_channels; i++)
373 opus_fade(s->cur_out[i], s->cur_out[i],
374 s->redundancy_output[i] + 120 + s->redundancy_idx,
375 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
376 s->redundancy_idx = 0;
377 }
378 if (redundancy) {
379 if (!redundancy_pos) {
380 ff_celt_flush(s->celt);
381 ret = opus_decode_redundancy(s, data + size, redundancy_size);
382 if (ret < 0)
383 return ret;
384
385 for (i = 0; i < s->output_channels; i++) {
386 opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
387 s->cur_out[i] + samples - 120 + delayed_samples,
388 s->redundancy_output[i] + 120,
390 if (delayed_samples)
391 s->redundancy_idx = 120 - delayed_samples;
392 }
393 } else {
394 for (i = 0; i < s->output_channels; i++) {
395 memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
396 opus_fade(s->cur_out[i] + 120 + delayed_samples,
397 s->redundancy_output[i] + 120,
398 s->cur_out[i] + 120 + delayed_samples,
399 ff_celt_window2, 120);
400 }
401 }
402 }
403
404 return samples;
405}
406
407static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
408{
409 int output_samples = 0;
410 int flush_needed = 0;
411 int i, j, ret;
412
413 s->cur_out[0] = s->out[0];
414 s->cur_out[1] = s->out[1];
415 s->remaining_out_size = s->out_size;
416
417 /* check if we need to flush the resampler */
418 if (swr_is_initialized(s->swr)) {
419 if (buf) {
420 int64_t cur_samplerate;
421 av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
422 flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
423 } else {
424 flush_needed = !!s->delayed_samples;
425 }
426 }
427
428 if (!buf && !flush_needed)
429 return 0;
430
431 /* use dummy output buffers if the channel is not mapped to anything */
432 if (!s->cur_out[0] ||
433 (s->output_channels == 2 && !s->cur_out[1])) {
434 av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
435 s->remaining_out_size);
436 if (!s->out_dummy)
437 return AVERROR(ENOMEM);
438 if (!s->cur_out[0])
439 s->cur_out[0] = s->out_dummy;
440 if (!s->cur_out[1])
441 s->cur_out[1] = s->out_dummy;
442 }
443
444 /* flush the resampler if necessary */
445 if (flush_needed) {
446 ret = opus_flush_resample(s, s->delayed_samples);
447 if (ret < 0) {
448 av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
449 return ret;
450 }
451 swr_close(s->swr);
452 output_samples += s->delayed_samples;
453 s->delayed_samples = 0;
454
455 if (!buf)
456 goto finish;
457 }
458
459 /* decode all the frames in the packet */
460 for (i = 0; i < s->packet.frame_count; i++) {
461 int size = s->packet.frame_size[i];
462 int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
463
464 if (samples < 0) {
465 av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
466 if (s->avctx->err_recognition & AV_EF_EXPLODE)
467 return samples;
468
469 for (j = 0; j < s->output_channels; j++)
470 memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
471 samples = s->packet.frame_duration;
472 }
473 output_samples += samples;
474
475 for (j = 0; j < s->output_channels; j++)
476 s->cur_out[j] += samples;
477 s->remaining_out_size -= samples * sizeof(float);
478 }
479
480finish:
481 s->cur_out[0] = s->cur_out[1] = NULL;
482 s->remaining_out_size = 0;
483
484 return output_samples;
485}
486
487/**
488 * Append the samples the resampler holds back to the output of the
489 * sub-packet that just decoded, leaving the stream's output state as it was.
490 */
492{
493 float *out[2] = { s->out[0], s->out[1] };
494 int out_size = s->out_size;
495 int ret;
496
497 for (int i = 0; i < 2; i++)
498 if (s->out[i])
499 s->out[i] += decoded_samples;
500 s->out_size -= decoded_samples * sizeof(float);
501
503
504 s->out[0] = out[0];
505 s->out[1] = out[1];
506 s->out_size = out_size;
507
508 return ret < 0 ? ret : decoded_samples + ret;
509}
510
511/**
512 * Whether a packet ends the sequence it belongs to: it is flagged so, or
513 * samples are to be discarded from the end of its output. Nothing can
514 * follow the latter, so the samples held back belong before that padding.
515 */
517{
518 const uint8_t *sd;
519 size_t sd_size;
520
521 if (pkt->flags & AV_PKT_FLAG_EOS)
522 return 1;
523
525 return sd && sd_size >= 8 && AV_RL32(sd + 4);
526}
527
529{
530 for (int i = 0; i < c->p.nb_streams; i++) {
531 OpusStreamContext *s = &c->streams[i];
532
533 ff_silk_free(&s->silk);
534 ff_celt_free(&s->celt);
535
536 av_freep(&s->out_dummy);
537 s->out_dummy_allocated_size = 0;
538
539 av_audio_fifo_free(s->sync_buffer);
540 av_audio_fifo_free(s->celt_delay);
541 swr_free(&s->swr);
542 }
543
544 av_freep(&c->streams);
545
546 c->p.nb_streams = 0;
547
548 av_freep(&c->p.channel_maps);
549}
550
552{
554
556 av_freep(&c->fdsp);
557
558 return 0;
559}
560
562 const uint8_t *header, int header_size)
563{
565 int ret;
566
567 /* find out the channel configuration */
568 ret = ff_opus_parse_extradata(avctx, &c->p, header, header_size);
569 if (ret < 0)
570 return ret;
571 if (c->p.gain_i)
572 c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
573
574 /* allocate and init each independent decoder */
575 c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
576 if (!c->streams) {
577 c->p.nb_streams = 0;
578 return AVERROR(ENOMEM);
579 }
580
581 for (int i = 0; i < c->p.nb_streams; i++) {
582 OpusStreamContext *s = &c->streams[i];
584
585 s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
586
587 s->avctx = avctx;
588
589 for (int j = 0; j < s->output_channels; j++) {
590 s->silk_output[j] = s->silk_buf[j];
591 s->celt_output[j] = s->celt_buf[j];
592 s->redundancy_output[j] = s->redundancy_buf[j];
593 }
594
595 s->fdsp = c->fdsp;
596
597 s->swr =swr_alloc();
598 if (!s->swr)
599 return AVERROR(ENOMEM);
600
601 layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
603 av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
604 av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
605 av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
606 av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
607 av_opt_set_int(s->swr, "out_sample_rate", 48000, 0);
608
609 ret = ff_silk_init(avctx, &s->silk, s->output_channels);
610 if (ret < 0)
611 return ret;
612
613 ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
614 if (ret < 0)
615 return ret;
616
617 s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
618 s->output_channels, 1024);
619 if (!s->celt_delay)
620 return AVERROR(ENOMEM);
621
622 s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
623 s->output_channels, 32);
624 if (!s->sync_buffer)
625 return AVERROR(ENOMEM);
626 }
627
628 return 0;
629}
630
632{
634
636 if (!avctx->sample_rate)
637 avctx->sample_rate = 48000;
638
639 c->fdsp = avpriv_float_dsp_alloc(0);
640 if (!c->fdsp)
641 return AVERROR(ENOMEM);
642
644}
645
647 int *got_frame_ptr, AVPacket *avpkt)
648{
650 const uint8_t *buf = avpkt->data;
651 int buf_size = avpkt->size;
652 int coded_samples = 0;
653 int decoded_samples = INT_MAX;
654 int delayed_samples = 0;
655 const uint8_t *new_header;
656 size_t new_header_size;
657 int i, ret;
658
659 /* A new header starts a new logical stream, decoded from a fresh state. */
661 &new_header_size);
662 if (new_header) {
663 if (new_header_size > INT_MAX)
664 return AVERROR_INVALIDDATA;
666 ret = opus_init_streams(avctx, new_header, new_header_size);
667 if (ret < 0) {
669 return ret;
670 }
671 }
672 if (!c->streams)
673 return AVERROR_INVALIDDATA;
674
675 /* calculate the number of delayed samples */
676 for (int i = 0; i < c->p.nb_streams; i++) {
677 OpusStreamContext *s = &c->streams[i];
678 s->out[0] =
679 s->out[1] = NULL;
680 int fifo_samples = av_audio_fifo_size(s->sync_buffer);
682 s->delayed_samples + fifo_samples);
683 }
684
685 /* decode the header of the first sub-packet to find out the sample count */
686 if (buf) {
687 OpusPacket *pkt = &c->streams[0].packet;
688 ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
689 if (ret < 0) {
690 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
691 return ret;
692 }
693 coded_samples += pkt->frame_count * pkt->frame_duration;
694 c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
695 }
696
697 frame->sample_rate = 48000;
698 frame->nb_samples = coded_samples + delayed_samples;
699
700 /* no input or buffered data => nothing to do */
701 if (!frame->nb_samples) {
702 *got_frame_ptr = 0;
703 return 0;
704 }
705
706 /* setup the data buffers */
708 if (ret < 0)
709 return ret;
710 frame->nb_samples = 0;
711
712 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
713 ChannelMap *map = &c->p.channel_maps[i];
714 if (!map->copy)
715 c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
716 }
717
718 /* read the data from the sync buffers */
719 for (int i = 0; i < c->p.nb_streams; i++) {
720 OpusStreamContext *s = &c->streams[i];
721 float **out = s->out;
722 int sync_size = av_audio_fifo_size(s->sync_buffer);
723
724 float sync_dummy[32];
725 int out_dummy = (!out[0]) | ((!out[1]) << 1);
726
727 if (!out[0])
728 out[0] = sync_dummy;
729 if (!out[1])
730 out[1] = sync_dummy;
731 if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
732 return AVERROR_BUG;
733
734 ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
735 if (ret < 0)
736 return ret;
737
738 if (out_dummy & 1)
739 out[0] = NULL;
740 else
741 out[0] += ret;
742 if (out_dummy & 2)
743 out[1] = NULL;
744 else
745 out[1] += ret;
746
747 s->out_size = frame->linesize[0] - ret * sizeof(float);
748 }
749
750 /* decode each sub-packet */
751 for (int i = 0; i < c->p.nb_streams; i++) {
752 OpusStreamContext *s = &c->streams[i];
753
754 if (i && buf) {
755 ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->p.nb_streams - 1);
756 if (ret < 0) {
757 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
758 return ret;
759 }
760 if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
762 "Mismatching coded sample count in substream %d.\n", i);
763 return AVERROR_INVALIDDATA;
764 }
765
766 s->silk_samplerate = get_silk_samplerate(s->packet.config);
767 }
768
769 ret = opus_decode_subpacket(&c->streams[i], buf);
770 if (ret >= 0 && buf && opus_packet_ends_sequence(avpkt))
772 if (ret < 0)
773 return ret;
774 s->decoded_samples = ret;
776
777 if (!buf)
778 continue;
779
780 buf += s->packet.packet_size;
781 buf_size -= s->packet.packet_size;
782 }
783
784 /* buffer the extra samples */
785 for (int i = 0; i < c->p.nb_streams; i++) {
786 OpusStreamContext *s = &c->streams[i];
787 int buffer_samples = s->decoded_samples - decoded_samples;
788 if (buffer_samples) {
789 float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
790 s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
791 buf[0] += decoded_samples;
792 buf[1] += decoded_samples;
793 ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
794 if (ret < 0)
795 return ret;
796 }
797 }
798
799 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
800 ChannelMap *map = &c->p.channel_maps[i];
801
802 /* handle copied channels */
803 if (map->copy) {
804 memcpy(frame->extended_data[i],
805 frame->extended_data[map->copy_idx],
806 frame->linesize[0]);
807 } else if (map->silence) {
808 memset(frame->extended_data[i], 0, frame->linesize[0]);
809 }
810
811 if (c->p.gain_i && decoded_samples > 0) {
812 c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
813 (float*)frame->extended_data[i],
814 c->gain, FFALIGN(decoded_samples, 8));
815 }
816 }
817
818 frame->nb_samples = decoded_samples;
819 *got_frame_ptr = !!decoded_samples;
820
821 return avpkt->size;
822}
823
825{
826 OpusContext *c = ctx->priv_data;
827
828 for (int i = 0; i < c->p.nb_streams; i++) {
829 OpusStreamContext *s = &c->streams[i];
830
831 memset(&s->packet, 0, sizeof(s->packet));
832 s->delayed_samples = 0;
833
834 av_audio_fifo_reset(s->celt_delay);
835 swr_close(s->swr);
836
837 av_audio_fifo_reset(s->sync_buffer);
838
839 ff_silk_flush(s->silk);
840 ff_celt_flush(s->celt);
841 }
842}
843
844#define OFFSET(x) offsetof(OpusContext, x)
845#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
846static const AVOption opus_options[] = {
847 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
848 { NULL },
849};
850
851static const AVClass opus_class = {
852 .class_name = "Opus Decoder",
853 .item_name = av_default_item_name,
854 .option = opus_options,
855 .version = LIBAVUTIL_VERSION_INT,
856};
857
859 .p.name = "opus",
860 CODEC_LONG_NAME("Opus"),
861 .p.priv_class = &opus_class,
862 .p.type = AVMEDIA_TYPE_AUDIO,
863 .p.id = AV_CODEC_ID_OPUS,
864 .priv_data_size = sizeof(OpusContext),
868 .flush = opus_decode_flush,
870 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
871};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFCodec ff_opus_decoder
Definition dec.c:858
static FILE * out
static int out_size
static AVFormatContext * ctx
static void finish(void)
static int decoded_samples(uint32_t skip, uint32_t discard)
Audio FIFO Buffer.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
void ff_celt_free(CeltFrame **f)
Definition dec_celt.c:530
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
Definition dec_celt.c:547
int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, int coded_channels, int frame_size, int startband, int endband)
Definition dec_celt.c:323
void ff_celt_flush(CeltFrame *f)
Definition dec_celt.c:499
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1814
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
#define AD
Definition evrcdec.c:920
internal math functions header
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition ffmath.h:44
int ret
Definition ffplay.c:391
static SDL_Window * window
Definition ffplay.c:421
reference-counted frame API
#define fail
Definition test.h:479
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_OPUS
Definition codec_id.h:519
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
#define AV_PKT_FLAG_EOS
The packet ends a sequence: the next packet, if any, does not continue its decoder state.
Definition packet.h:685
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition audio_fifo.c:62
void av_audio_fifo_reset(AVAudioFifo *af)
Reset the AVAudioFifo buffer.
Definition audio_fifo.c:212
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition audio_fifo.c:119
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
Definition audio_fifo.c:175
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition audio_fifo.c:48
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition audio_fifo.c:222
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:661
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition options.c:148
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition swresample.c:137
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
Definition swresample.c:152
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
Definition swresample.c:747
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
Definition swresample.c:751
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition swresample.c:156
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition opt.c:1362
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition opt.c:951
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition opt.c:1086
const VDPAUPixFmtMap * map
#define AV_RL32(p)
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
uint64_t layout
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
const char data[16]
Definition mxf.c:155
AVOptions.
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
Definition dec.c:407
static int opus_packet_ends_sequence(const AVPacket *pkt)
Whether a packet ends the sequence it belongs to: it is flagged so, or samples are to be discarded fr...
Definition dec.c:516
static int get_silk_samplerate(int config)
Definition dec.c:140
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition dec.c:228
static const AVClass opus_class
Definition dec.c:851
static int opus_drain_subpacket(OpusStreamContext *s, int decoded_samples)
Append the samples the resampler holds back to the output of the sub-packet that just decoded,...
Definition dec.c:491
static const int silk_resample_delay[]
Definition dec.c:72
static const int silk_resample_filter_size[]
Definition dec.c:79
static const AVOption opus_options[]
Definition dec.c:846
static int opus_init_resample(OpusStreamContext *s)
Definition dec.c:201
static const uint16_t silk_frame_duration_ms[16]
Definition dec.c:62
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition dec.c:158
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition dec.c:631
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition dec.c:149
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition dec.c:824
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition dec.c:646
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition dec.c:551
#define OFFSET(x)
Definition dec.c:844
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition dec.c:248
static av_cold int opus_init_streams(AVCodecContext *avctx, const uint8_t *header, int header_size)
Definition dec.c:561
static av_cold void opus_free_streams(OpusContext *c)
Definition dec.c:528
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
Definition parse.c:85
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s, const uint8_t *header, int header_size)
Definition parse.c:286
@ OPUS_BANDWIDTH_WIDEBAND
Definition opus.h:52
@ OPUS_MODE_SILK
Definition opus.h:42
@ OPUS_MODE_HYBRID
Definition opus.h:43
@ OPUS_MODE_CELT
Definition opus.h:44
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
Definition rc.c:182
void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
Definition rc.c:352
int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Definition rc.c:338
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition rc.c:114
static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame,...
Definition rc.h:62
static const uint8_t header[24]
Definition sdr2.c:68
int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, float *output[2], enum OpusBandwidth bandwidth, int coded_channels, int duration_ms)
Decode the LP layer of one Opus frame (which may correspond to several SILK frames).
Definition silk.c:786
int ff_silk_init(void *logctx, SilkContext **ps, int output_channels)
Definition silk.c:879
void ff_silk_flush(SilkContext *s)
Definition silk.c:871
void ff_silk_free(SilkContext **ps)
Definition silk.c:866
#define FF_ARRAY_ELEMS(a)
Context for an Audio FIFO Buffer.
Definition audio_fifo.c:37
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1059
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
int sample_rate
samples per second
Definition avcodec.h:1044
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
float gain
Definition dec.c:135
struct OpusStreamContext * streams
Definition dec.c:131
OpusParseContext p
Definition dec.c:137
AVClass * av_class
Definition dec.c:129
AVFloatDSPContext * fdsp
Definition dec.c:134
int apply_phase_inv
Definition dec.c:132
float * redundancy_output[2]
Definition dec.c:108
int silk_samplerate
Definition dec.c:119
AVAudioFifo * celt_delay
Definition dec.c:118
OpusRangeCoder redundancy_rc
Definition dec.c:97
float * celt_output[2]
Definition dec.c:105
OpusRangeCoder rc
Definition dec.c:96
CeltFrame * celt
Definition dec.c:99
AVCodecContext * avctx
Definition dec.c:84
AVFloatDSPContext * fdsp
Definition dec.c:100
SilkContext * silk
Definition dec.c:98
int decoded_samples
Definition dec.c:88
int remaining_out_size
Definition dec.c:112
float celt_buf[2][960]
Definition dec.c:104
int out_dummy_allocated_size
Definition dec.c:115
OpusPacket packet
Definition dec.c:123
float redundancy_buf[2][960]
Definition dec.c:107
int output_channels
Definition dec.c:85
int delayed_samples
Definition dec.c:121
float silk_buf[2][960]
Definition dec.c:102
AVAudioFifo * sync_buffer
Definition dec.c:94
SwrContext * swr
Definition dec.c:117
float * cur_out[2]
Definition dec.c:111
float * out[2]
Definition dec.c:90
int redundancy_idx
Definition dec.c:125
float * out_dummy
Definition dec.c:114
float * silk_output[2]
Definition dec.c:103
The libswresample context.
libswresample public header
const uint8_t ff_celt_band_end[]
Definition tab.c:29
const float ff_celt_window2[120]
Definition tab.c:1200
#define av_freep(p)
#define av_log(a,...)
int size
int len
static double c[64]