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"
44#include "libavutil/mem.h"
46#include "libavutil/opt.h"
47
49
50#include "libavcodec/avcodec.h"
52#include "libavcodec/decode.h"
53
54#include "opus.h"
55#include "tab.h"
56#include "celt.h"
57#include "parse.h"
58#include "rc.h"
59#include "silk.h"
60
61static const uint16_t silk_frame_duration_ms[16] = {
62 10, 20, 40, 60,
63 10, 20, 40, 60,
64 10, 20, 40, 60,
65 10, 20,
66 10, 20,
67};
68
69/* number of samples of silence to feed to the resampler
70 * at the beginning */
71static const int silk_resample_delay[] = {
72 4, 8, 11, 11, 11
73};
74
75typedef struct OpusStreamContext {
78
79 /* number of decoded samples for this stream */
81 /* current output buffers for this stream */
82 float *out[2];
84 /* Buffer with samples from this stream for synchronizing
85 * the streams when they have different resampling delays */
87
93
94 float silk_buf[2][960];
95 float *silk_output[2];
96 DECLARE_ALIGNED(32, float, celt_buf)[2][960];
97 float *celt_output[2];
98
99 DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
101
102 /* buffers for the next samples to be decoded */
103 float *cur_out[2];
105
106 float *out_dummy;
108
112 /* number of samples we still want to get from the resampler */
114
116
119
131
132static int get_silk_samplerate(int config)
133{
134 if (config < 4)
135 return 8000;
136 else if (config < 8)
137 return 12000;
138 return 16000;
139}
140
141static void opus_fade(float *out,
142 const float *in1, const float *in2,
143 const float *window, int len)
144{
145 int i;
146 for (i = 0; i < len; i++)
147 out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
148}
149
150static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
151{
152 int celt_size = av_audio_fifo_size(s->celt_delay);
153 int ret, i;
154 ret = swr_convert(s->swr,
155 (uint8_t**)s->cur_out, nb_samples,
156 NULL, 0);
157 if (ret < 0)
158 return ret;
159 else if (ret != nb_samples) {
160 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
161 ret);
162 return AVERROR_BUG;
163 }
164
165 if (celt_size) {
166 if (celt_size != nb_samples) {
167 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
168 return AVERROR_BUG;
169 }
170 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
171 for (i = 0; i < s->output_channels; i++) {
172 s->fdsp->vector_fmac_scalar(s->cur_out[i],
173 s->celt_output[i], 1.0,
174 nb_samples);
175 }
176 }
177
178 if (s->redundancy_idx) {
179 for (i = 0; i < s->output_channels; i++)
180 opus_fade(s->cur_out[i], s->cur_out[i],
181 s->redundancy_output[i] + 120 + s->redundancy_idx,
182 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
183 s->redundancy_idx = 0;
184 }
185
186 for (i = 0; i < s->output_channels; i++)
187 s->cur_out[i] += nb_samples;
188 s->remaining_out_size -= nb_samples * sizeof(float);
189
190 return 0;
191}
192
194{
195 static const float delay[16] = { 0.0 };
196 const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
197 int ret;
198
199 av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
200 ret = swr_init(s->swr);
201 if (ret < 0) {
202 av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
203 return ret;
204 }
205
206 ret = swr_convert(s->swr,
207 NULL, 0,
208 delayptr, silk_resample_delay[s->packet.bandwidth]);
209 if (ret < 0) {
210 av_log(s->avctx, AV_LOG_ERROR,
211 "Error feeding initial silence to the resampler.\n");
212 return ret;
213 }
214
215 return 0;
216}
217
218static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
219{
220 int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
221 if (ret < 0)
222 goto fail;
223 ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
224
225 ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
226 s->redundancy_output,
227 s->packet.stereo + 1, 240,
228 0, ff_celt_band_end[s->packet.bandwidth]);
229 if (ret < 0)
230 goto fail;
231
232 return 0;
233fail:
234 av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
235 return ret;
236}
237
238static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
239{
240 int samples = s->packet.frame_duration;
241 int redundancy = 0;
242 int redundancy_size, redundancy_pos;
243 int ret, i, consumed;
244 int delayed_samples = s->delayed_samples;
245
247 if (ret < 0)
248 return ret;
249
250 /* decode the silk frame */
251 if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
252 if (!swr_is_initialized(s->swr)) {
254 if (ret < 0)
255 return ret;
256 }
257
258 samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
259 FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
260 s->packet.stereo + 1,
261 silk_frame_duration_ms[s->packet.config]);
262 if (samples < 0) {
263 av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
264 return samples;
265 }
266 samples = swr_convert(s->swr,
267 (uint8_t**)s->cur_out, s->packet.frame_duration,
268 (const uint8_t**)s->silk_output, samples);
269 if (samples < 0) {
270 av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
271 return samples;
272 }
273 av_assert2((samples & 7) == 0);
274 s->delayed_samples += s->packet.frame_duration - samples;
275 } else
276 ff_silk_flush(s->silk);
277
278 // decode redundancy information
279 consumed = opus_rc_tell(&s->rc);
280 if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
281 redundancy = ff_opus_rc_dec_log(&s->rc, 12);
282 else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
283 redundancy = 1;
284
285 if (redundancy) {
286 redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
287
288 if (s->packet.mode == OPUS_MODE_HYBRID)
289 redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
290 else
291 redundancy_size = size - (consumed + 7) / 8;
292 size -= redundancy_size;
293 if (size < 0) {
294 av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
295 return AVERROR_INVALIDDATA;
296 }
297
298 if (redundancy_pos) {
299 ret = opus_decode_redundancy(s, data + size, redundancy_size);
300 if (ret < 0)
301 return ret;
302 ff_celt_flush(s->celt);
303 }
304 }
305
306 /* decode the CELT frame */
307 if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
308 float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
309 float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
310 out_tmp : s->celt_output;
311 int celt_output_samples = samples;
312 int delay_samples = av_audio_fifo_size(s->celt_delay);
313
314 if (delay_samples) {
315 if (s->packet.mode == OPUS_MODE_HYBRID) {
316 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
317
318 for (i = 0; i < s->output_channels; i++) {
319 s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
320 delay_samples);
321 out_tmp[i] += delay_samples;
322 }
323 celt_output_samples -= delay_samples;
324 } else {
325 av_log(s->avctx, AV_LOG_WARNING,
326 "Spurious CELT delay samples present.\n");
327 av_audio_fifo_reset(s->celt_delay);
328 if (s->avctx->err_recognition & AV_EF_EXPLODE)
329 return AVERROR_BUG;
330 }
331 }
332
334
335 ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
336 s->packet.stereo + 1,
337 s->packet.frame_duration,
338 (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
339 ff_celt_band_end[s->packet.bandwidth]);
340 if (ret < 0)
341 return ret;
342
343 if (s->packet.mode == OPUS_MODE_HYBRID) {
344 int celt_delay = s->packet.frame_duration - celt_output_samples;
345 void *delaybuf[2];
346
347 for (i = 0; i < s->output_channels; i++) {
348 delaybuf[i] = s->celt_output[i] + celt_output_samples;
349 s->fdsp->vector_fmac_scalar(out_tmp[i],
350 s->celt_output[i], 1.0,
351 celt_output_samples);
352 }
353
354 ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
355 if (ret < 0)
356 return ret;
357 }
358 } else
359 ff_celt_flush(s->celt);
360
361 if (s->redundancy_idx) {
362 for (i = 0; i < s->output_channels; i++)
363 opus_fade(s->cur_out[i], s->cur_out[i],
364 s->redundancy_output[i] + 120 + s->redundancy_idx,
365 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
366 s->redundancy_idx = 0;
367 }
368 if (redundancy) {
369 if (!redundancy_pos) {
370 ff_celt_flush(s->celt);
371 ret = opus_decode_redundancy(s, data + size, redundancy_size);
372 if (ret < 0)
373 return ret;
374
375 for (i = 0; i < s->output_channels; i++) {
376 opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
377 s->cur_out[i] + samples - 120 + delayed_samples,
378 s->redundancy_output[i] + 120,
380 if (delayed_samples)
381 s->redundancy_idx = 120 - delayed_samples;
382 }
383 } else {
384 for (i = 0; i < s->output_channels; i++) {
385 memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
386 opus_fade(s->cur_out[i] + 120 + delayed_samples,
387 s->redundancy_output[i] + 120,
388 s->cur_out[i] + 120 + delayed_samples,
389 ff_celt_window2, 120);
390 }
391 }
392 }
393
394 return samples;
395}
396
397static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
398{
399 int output_samples = 0;
400 int flush_needed = 0;
401 int i, j, ret;
402
403 s->cur_out[0] = s->out[0];
404 s->cur_out[1] = s->out[1];
405 s->remaining_out_size = s->out_size;
406
407 /* check if we need to flush the resampler */
408 if (swr_is_initialized(s->swr)) {
409 if (buf) {
410 int64_t cur_samplerate;
411 av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
412 flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
413 } else {
414 flush_needed = !!s->delayed_samples;
415 }
416 }
417
418 if (!buf && !flush_needed)
419 return 0;
420
421 /* use dummy output buffers if the channel is not mapped to anything */
422 if (!s->cur_out[0] ||
423 (s->output_channels == 2 && !s->cur_out[1])) {
424 av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
425 s->remaining_out_size);
426 if (!s->out_dummy)
427 return AVERROR(ENOMEM);
428 if (!s->cur_out[0])
429 s->cur_out[0] = s->out_dummy;
430 if (!s->cur_out[1])
431 s->cur_out[1] = s->out_dummy;
432 }
433
434 /* flush the resampler if necessary */
435 if (flush_needed) {
436 ret = opus_flush_resample(s, s->delayed_samples);
437 if (ret < 0) {
438 av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
439 return ret;
440 }
441 swr_close(s->swr);
442 output_samples += s->delayed_samples;
443 s->delayed_samples = 0;
444
445 if (!buf)
446 goto finish;
447 }
448
449 /* decode all the frames in the packet */
450 for (i = 0; i < s->packet.frame_count; i++) {
451 int size = s->packet.frame_size[i];
452 int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
453
454 if (samples < 0) {
455 av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
456 if (s->avctx->err_recognition & AV_EF_EXPLODE)
457 return samples;
458
459 for (j = 0; j < s->output_channels; j++)
460 memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
461 samples = s->packet.frame_duration;
462 }
463 output_samples += samples;
464
465 for (j = 0; j < s->output_channels; j++)
466 s->cur_out[j] += samples;
467 s->remaining_out_size -= samples * sizeof(float);
468 }
469
470finish:
471 s->cur_out[0] = s->cur_out[1] = NULL;
472 s->remaining_out_size = 0;
473
474 return output_samples;
475}
476
477/**
478 * Append the samples the resampler holds back to the output of the
479 * sub-packet that just decoded, leaving the stream's output state as it was.
480 */
482{
483 float *out[2] = { s->out[0], s->out[1] };
484 int out_size = s->out_size;
485 int ret;
486
487 for (int i = 0; i < 2; i++)
488 if (s->out[i])
489 s->out[i] += decoded_samples;
490 s->out_size -= decoded_samples * sizeof(float);
491
493
494 s->out[0] = out[0];
495 s->out[1] = out[1];
496 s->out_size = out_size;
497
498 return ret < 0 ? ret : decoded_samples + ret;
499}
500
502{
503 for (int i = 0; i < c->p.nb_streams; i++) {
504 OpusStreamContext *s = &c->streams[i];
505
506 ff_silk_free(&s->silk);
507 ff_celt_free(&s->celt);
508
509 av_freep(&s->out_dummy);
510 s->out_dummy_allocated_size = 0;
511
512 av_audio_fifo_free(s->sync_buffer);
513 av_audio_fifo_free(s->celt_delay);
514 swr_free(&s->swr);
515 }
516
517 av_freep(&c->streams);
518
519 c->p.nb_streams = 0;
520
521 av_freep(&c->p.channel_maps);
522}
523
525{
527
529 av_freep(&c->fdsp);
530
531 return 0;
532}
533
535 const uint8_t *header, int header_size)
536{
538 int ret;
539
540 /* find out the channel configuration */
541 ret = ff_opus_parse_extradata(avctx, &c->p, header, header_size);
542 if (ret < 0)
543 return ret;
544 if (c->p.gain_i)
545 c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
546
547 /* allocate and init each independent decoder */
548 c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
549 if (!c->streams) {
550 c->p.nb_streams = 0;
551 return AVERROR(ENOMEM);
552 }
553
554 for (int i = 0; i < c->p.nb_streams; i++) {
555 OpusStreamContext *s = &c->streams[i];
557
558 s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
559
560 s->avctx = avctx;
561
562 for (int j = 0; j < s->output_channels; j++) {
563 s->silk_output[j] = s->silk_buf[j];
564 s->celt_output[j] = s->celt_buf[j];
565 s->redundancy_output[j] = s->redundancy_buf[j];
566 }
567
568 s->fdsp = c->fdsp;
569
570 s->swr =swr_alloc();
571 if (!s->swr)
572 return AVERROR(ENOMEM);
573
574 layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
576 av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
577 av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
578 av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
579 av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
580 av_opt_set_int(s->swr, "out_sample_rate", 48000, 0);
581 av_opt_set_int(s->swr, "filter_size", 16, 0);
582
583 ret = ff_silk_init(avctx, &s->silk, s->output_channels);
584 if (ret < 0)
585 return ret;
586
587 ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
588 if (ret < 0)
589 return ret;
590
591 s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
592 s->output_channels, 1024);
593 if (!s->celt_delay)
594 return AVERROR(ENOMEM);
595
596 s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
597 s->output_channels, 32);
598 if (!s->sync_buffer)
599 return AVERROR(ENOMEM);
600 }
601
602 return 0;
603}
604
606{
608
610 if (!avctx->sample_rate)
611 avctx->sample_rate = 48000;
612
613 c->fdsp = avpriv_float_dsp_alloc(0);
614 if (!c->fdsp)
615 return AVERROR(ENOMEM);
616
618}
619
621 int *got_frame_ptr, AVPacket *avpkt)
622{
624 const uint8_t *buf = avpkt->data;
625 int buf_size = avpkt->size;
626 int coded_samples = 0;
627 int decoded_samples = INT_MAX;
628 int delayed_samples = 0;
629 const uint8_t *new_header;
630 size_t new_header_size;
631 int i, ret;
632
633 /* A new header starts a new logical stream, decoded from a fresh state. */
635 &new_header_size);
636 if (new_header) {
637 if (new_header_size > INT_MAX)
638 return AVERROR_INVALIDDATA;
640 ret = opus_init_streams(avctx, new_header, new_header_size);
641 if (ret < 0) {
643 return ret;
644 }
645 }
646 if (!c->streams)
647 return AVERROR_INVALIDDATA;
648
649 /* calculate the number of delayed samples */
650 for (int i = 0; i < c->p.nb_streams; i++) {
651 OpusStreamContext *s = &c->streams[i];
652 s->out[0] =
653 s->out[1] = NULL;
654 int fifo_samples = av_audio_fifo_size(s->sync_buffer);
656 s->delayed_samples + fifo_samples);
657 }
658
659 /* decode the header of the first sub-packet to find out the sample count */
660 if (buf) {
661 OpusPacket *pkt = &c->streams[0].packet;
662 ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
663 if (ret < 0) {
664 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
665 return ret;
666 }
667 coded_samples += pkt->frame_count * pkt->frame_duration;
668 c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
669 }
670
671 frame->sample_rate = 48000;
672 frame->nb_samples = coded_samples + delayed_samples;
673
674 /* no input or buffered data => nothing to do */
675 if (!frame->nb_samples) {
676 *got_frame_ptr = 0;
677 return 0;
678 }
679
680 /* setup the data buffers */
682 if (ret < 0)
683 return ret;
684 frame->nb_samples = 0;
685
686 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
687 ChannelMap *map = &c->p.channel_maps[i];
688 if (!map->copy)
689 c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
690 }
691
692 /* read the data from the sync buffers */
693 for (int i = 0; i < c->p.nb_streams; i++) {
694 OpusStreamContext *s = &c->streams[i];
695 float **out = s->out;
696 int sync_size = av_audio_fifo_size(s->sync_buffer);
697
698 float sync_dummy[32];
699 int out_dummy = (!out[0]) | ((!out[1]) << 1);
700
701 if (!out[0])
702 out[0] = sync_dummy;
703 if (!out[1])
704 out[1] = sync_dummy;
705 if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
706 return AVERROR_BUG;
707
708 ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
709 if (ret < 0)
710 return ret;
711
712 if (out_dummy & 1)
713 out[0] = NULL;
714 else
715 out[0] += ret;
716 if (out_dummy & 2)
717 out[1] = NULL;
718 else
719 out[1] += ret;
720
721 s->out_size = frame->linesize[0] - ret * sizeof(float);
722 }
723
724 /* decode each sub-packet */
725 for (int i = 0; i < c->p.nb_streams; i++) {
726 OpusStreamContext *s = &c->streams[i];
727
728 if (i && buf) {
729 ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->p.nb_streams - 1);
730 if (ret < 0) {
731 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
732 return ret;
733 }
734 if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
736 "Mismatching coded sample count in substream %d.\n", i);
737 return AVERROR_INVALIDDATA;
738 }
739
740 s->silk_samplerate = get_silk_samplerate(s->packet.config);
741 }
742
743 ret = opus_decode_subpacket(&c->streams[i], buf);
744 if (ret >= 0 && buf && (avpkt->flags & AV_PKT_FLAG_EOS))
746 if (ret < 0)
747 return ret;
748 s->decoded_samples = ret;
750
751 if (!buf)
752 continue;
753
754 buf += s->packet.packet_size;
755 buf_size -= s->packet.packet_size;
756 }
757
758 /* buffer the extra samples */
759 for (int i = 0; i < c->p.nb_streams; i++) {
760 OpusStreamContext *s = &c->streams[i];
761 int buffer_samples = s->decoded_samples - decoded_samples;
762 if (buffer_samples) {
763 float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
764 s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
765 buf[0] += decoded_samples;
766 buf[1] += decoded_samples;
767 ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
768 if (ret < 0)
769 return ret;
770 }
771 }
772
773 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
774 ChannelMap *map = &c->p.channel_maps[i];
775
776 /* handle copied channels */
777 if (map->copy) {
778 memcpy(frame->extended_data[i],
779 frame->extended_data[map->copy_idx],
780 frame->linesize[0]);
781 } else if (map->silence) {
782 memset(frame->extended_data[i], 0, frame->linesize[0]);
783 }
784
785 if (c->p.gain_i && decoded_samples > 0) {
786 c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
787 (float*)frame->extended_data[i],
788 c->gain, FFALIGN(decoded_samples, 8));
789 }
790 }
791
792 frame->nb_samples = decoded_samples;
793 *got_frame_ptr = !!decoded_samples;
794
795 return avpkt->size;
796}
797
799{
800 OpusContext *c = ctx->priv_data;
801
802 for (int i = 0; i < c->p.nb_streams; i++) {
803 OpusStreamContext *s = &c->streams[i];
804
805 memset(&s->packet, 0, sizeof(s->packet));
806 s->delayed_samples = 0;
807
808 av_audio_fifo_reset(s->celt_delay);
809 swr_close(s->swr);
810
811 av_audio_fifo_reset(s->sync_buffer);
812
813 ff_silk_flush(s->silk);
814 ff_celt_flush(s->celt);
815 }
816}
817
818#define OFFSET(x) offsetof(OpusContext, x)
819#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
820static const AVOption opus_options[] = {
821 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
822 { NULL },
823};
824
825static const AVClass opus_class = {
826 .class_name = "Opus Decoder",
827 .item_name = av_default_item_name,
828 .option = opus_options,
829 .version = LIBAVUTIL_VERSION_INT,
830};
831
833 .p.name = "opus",
834 CODEC_LONG_NAME("Opus"),
835 .p.priv_class = &opus_class,
836 .p.type = AVMEDIA_TYPE_AUDIO,
837 .p.id = AV_CODEC_ID_OPUS,
838 .priv_data_size = sizeof(OpusContext),
842 .flush = opus_decode_flush,
844 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
845};
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:832
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_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
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:149
AVOptions.
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
Definition dec.c:397
static int get_silk_samplerate(int config)
Definition dec.c:132
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition dec.c:218
static const AVClass opus_class
Definition dec.c:825
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:481
static const int silk_resample_delay[]
Definition dec.c:71
static const AVOption opus_options[]
Definition dec.c:820
static int opus_init_resample(OpusStreamContext *s)
Definition dec.c:193
static const uint16_t silk_frame_duration_ms[16]
Definition dec.c:61
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition dec.c:150
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition dec.c:605
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition dec.c:141
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition dec.c:798
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition dec.c:620
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition dec.c:524
#define OFFSET(x)
Definition dec.c:818
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition dec.c:238
static av_cold int opus_init_streams(AVCodecContext *avctx, const uint8_t *header, int header_size)
Definition dec.c:534
static av_cold void opus_free_streams(OpusContext *c)
Definition dec.c:501
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 flags
A combination of AV_PKT_FLAG values.
Definition packet.h:616
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
float gain
Definition dec.c:127
struct OpusStreamContext * streams
Definition dec.c:123
OpusParseContext p
Definition dec.c:129
AVClass * av_class
Definition dec.c:121
AVFloatDSPContext * fdsp
Definition dec.c:126
int apply_phase_inv
Definition dec.c:124
float * redundancy_output[2]
Definition dec.c:100
int silk_samplerate
Definition dec.c:111
AVAudioFifo * celt_delay
Definition dec.c:110
OpusRangeCoder redundancy_rc
Definition dec.c:89
float * celt_output[2]
Definition dec.c:97
OpusRangeCoder rc
Definition dec.c:88
CeltFrame * celt
Definition dec.c:91
AVCodecContext * avctx
Definition dec.c:76
AVFloatDSPContext * fdsp
Definition dec.c:92
SilkContext * silk
Definition dec.c:90
int decoded_samples
Definition dec.c:80
int remaining_out_size
Definition dec.c:104
float celt_buf[2][960]
Definition dec.c:96
int out_dummy_allocated_size
Definition dec.c:107
OpusPacket packet
Definition dec.c:115
float redundancy_buf[2][960]
Definition dec.c:99
int output_channels
Definition dec.c:77
int delayed_samples
Definition dec.c:113
float silk_buf[2][960]
Definition dec.c:94
AVAudioFifo * sync_buffer
Definition dec.c:86
SwrContext * swr
Definition dec.c:109
float * cur_out[2]
Definition dec.c:103
float * out[2]
Definition dec.c:82
int redundancy_idx
Definition dec.c:117
float * out_dummy
Definition dec.c:106
float * silk_output[2]
Definition dec.c:95
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]