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 s->cur_out[0] += nb_samples;
187 s->cur_out[1] += 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
246 ret = ff_opus_rc_dec_init(&s->rc, data, size);
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)) {
253 ret = opus_init_resample(s);
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] = { s->celt_output[0] + celt_output_samples,
346 s->celt_output[1] + celt_output_samples };
347
348 for (i = 0; i < s->output_channels; i++) {
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
478 int *got_frame_ptr, AVPacket *avpkt)
479{
481 const uint8_t *buf = avpkt->data;
482 int buf_size = avpkt->size;
483 int coded_samples = 0;
484 int decoded_samples = INT_MAX;
485 int delayed_samples = 0;
486 int i, ret;
487
488 /* calculate the number of delayed samples */
489 for (int i = 0; i < c->p.nb_streams; i++) {
490 OpusStreamContext *s = &c->streams[i];
491 s->out[0] =
492 s->out[1] = NULL;
493 int fifo_samples = av_audio_fifo_size(s->sync_buffer);
495 s->delayed_samples + fifo_samples);
496 }
497
498 /* decode the header of the first sub-packet to find out the sample count */
499 if (buf) {
500 OpusPacket *pkt = &c->streams[0].packet;
501 ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
502 if (ret < 0) {
503 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
504 return ret;
505 }
506 coded_samples += pkt->frame_count * pkt->frame_duration;
507 c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
508 }
509
510 frame->nb_samples = coded_samples + delayed_samples;
511
512 /* no input or buffered data => nothing to do */
513 if (!frame->nb_samples) {
514 *got_frame_ptr = 0;
515 return 0;
516 }
517
518 /* setup the data buffers */
519 ret = ff_get_buffer(avctx, frame, 0);
520 if (ret < 0)
521 return ret;
522 frame->nb_samples = 0;
523
524 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
525 ChannelMap *map = &c->p.channel_maps[i];
526 if (!map->copy)
527 c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
528 }
529
530 /* read the data from the sync buffers */
531 for (int i = 0; i < c->p.nb_streams; i++) {
532 OpusStreamContext *s = &c->streams[i];
533 float **out = s->out;
534 int sync_size = av_audio_fifo_size(s->sync_buffer);
535
536 float sync_dummy[32];
537 int out_dummy = (!out[0]) | ((!out[1]) << 1);
538
539 if (!out[0])
540 out[0] = sync_dummy;
541 if (!out[1])
542 out[1] = sync_dummy;
543 if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
544 return AVERROR_BUG;
545
546 ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
547 if (ret < 0)
548 return ret;
549
550 if (out_dummy & 1)
551 out[0] = NULL;
552 else
553 out[0] += ret;
554 if (out_dummy & 2)
555 out[1] = NULL;
556 else
557 out[1] += ret;
558
559 s->out_size = frame->linesize[0] - ret * sizeof(float);
560 }
561
562 /* decode each sub-packet */
563 for (int i = 0; i < c->p.nb_streams; i++) {
564 OpusStreamContext *s = &c->streams[i];
565
566 if (i && buf) {
567 ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->p.nb_streams - 1);
568 if (ret < 0) {
569 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
570 return ret;
571 }
572 if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
574 "Mismatching coded sample count in substream %d.\n", i);
575 return AVERROR_INVALIDDATA;
576 }
577
578 s->silk_samplerate = get_silk_samplerate(s->packet.config);
579 }
580
581 ret = opus_decode_subpacket(&c->streams[i], buf);
582 if (ret < 0)
583 return ret;
584 s->decoded_samples = ret;
586
587 if (!buf)
588 continue;
589
590 buf += s->packet.packet_size;
591 buf_size -= s->packet.packet_size;
592 }
593
594 /* buffer the extra samples */
595 for (int i = 0; i < c->p.nb_streams; i++) {
596 OpusStreamContext *s = &c->streams[i];
597 int buffer_samples = s->decoded_samples - decoded_samples;
598 if (buffer_samples) {
599 float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
600 s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
601 buf[0] += decoded_samples;
602 buf[1] += decoded_samples;
603 ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
604 if (ret < 0)
605 return ret;
606 }
607 }
608
609 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
610 ChannelMap *map = &c->p.channel_maps[i];
611
612 /* handle copied channels */
613 if (map->copy) {
614 memcpy(frame->extended_data[i],
615 frame->extended_data[map->copy_idx],
616 frame->linesize[0]);
617 } else if (map->silence) {
618 memset(frame->extended_data[i], 0, frame->linesize[0]);
619 }
620
621 if (c->p.gain_i && decoded_samples > 0) {
622 c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
623 (float*)frame->extended_data[i],
624 c->gain, FFALIGN(decoded_samples, 8));
625 }
626 }
627
628 frame->nb_samples = decoded_samples;
629 *got_frame_ptr = !!decoded_samples;
630
631 return avpkt->size;
632}
633
635{
636 OpusContext *c = ctx->priv_data;
637
638 for (int i = 0; i < c->p.nb_streams; i++) {
639 OpusStreamContext *s = &c->streams[i];
640
641 memset(&s->packet, 0, sizeof(s->packet));
642 s->delayed_samples = 0;
643
644 av_audio_fifo_reset(s->celt_delay);
645 swr_close(s->swr);
646
647 av_audio_fifo_reset(s->sync_buffer);
648
649 ff_silk_flush(s->silk);
650 ff_celt_flush(s->celt);
651 }
652}
653
655{
657
658 for (int i = 0; i < c->p.nb_streams; i++) {
659 OpusStreamContext *s = &c->streams[i];
660
661 ff_silk_free(&s->silk);
662 ff_celt_free(&s->celt);
663
664 av_freep(&s->out_dummy);
665 s->out_dummy_allocated_size = 0;
666
667 av_audio_fifo_free(s->sync_buffer);
668 av_audio_fifo_free(s->celt_delay);
669 swr_free(&s->swr);
670 }
671
672 av_freep(&c->streams);
673
674 c->p.nb_streams = 0;
675
676 av_freep(&c->p.channel_maps);
677 av_freep(&c->fdsp);
678
679 return 0;
680}
681
683{
685 int ret;
686
688 avctx->sample_rate = 48000;
689
690 c->fdsp = avpriv_float_dsp_alloc(0);
691 if (!c->fdsp)
692 return AVERROR(ENOMEM);
693
694 /* find out the channel configuration */
695 ret = ff_opus_parse_extradata(avctx, &c->p);
696 if (ret < 0)
697 return ret;
698 if (c->p.gain_i)
699 c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
700
701 /* allocate and init each independent decoder */
702 c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
703 if (!c->streams) {
704 c->p.nb_streams = 0;
705 return AVERROR(ENOMEM);
706 }
707
708 for (int i = 0; i < c->p.nb_streams; i++) {
709 OpusStreamContext *s = &c->streams[i];
711
712 s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
713
714 s->avctx = avctx;
715
716 for (int j = 0; j < s->output_channels; j++) {
717 s->silk_output[j] = s->silk_buf[j];
718 s->celt_output[j] = s->celt_buf[j];
719 s->redundancy_output[j] = s->redundancy_buf[j];
720 }
721
722 s->fdsp = c->fdsp;
723
724 s->swr =swr_alloc();
725 if (!s->swr)
726 return AVERROR(ENOMEM);
727
728 layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
730 av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
731 av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
732 av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
733 av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
734 av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
735 av_opt_set_int(s->swr, "filter_size", 16, 0);
736
737 ret = ff_silk_init(avctx, &s->silk, s->output_channels);
738 if (ret < 0)
739 return ret;
740
741 ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
742 if (ret < 0)
743 return ret;
744
745 s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
746 s->output_channels, 1024);
747 if (!s->celt_delay)
748 return AVERROR(ENOMEM);
749
750 s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
751 s->output_channels, 32);
752 if (!s->sync_buffer)
753 return AVERROR(ENOMEM);
754 }
755
756 return 0;
757}
758
759#define OFFSET(x) offsetof(OpusContext, x)
760#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
761static const AVOption opus_options[] = {
762 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
763 { NULL },
764};
765
766static const AVClass opus_class = {
767 .class_name = "Opus Decoder",
768 .item_name = av_default_item_name,
769 .option = opus_options,
770 .version = LIBAVUTIL_VERSION_INT,
771};
772
774 .p.name = "opus",
775 CODEC_LONG_NAME("Opus"),
776 .p.priv_class = &opus_class,
777 .p.type = AVMEDIA_TYPE_AUDIO,
778 .p.id = AV_CODEC_ID_OPUS,
779 .priv_data_size = sizeof(OpusContext),
783 .flush = opus_decode_flush,
785 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
786};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_opus_decoder
Definition dec.c:773
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
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:1777
#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:608
#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:42
static SDL_Window * window
Definition ffplay.c:365
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:513
#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:241
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:555
@ 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:721
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:725
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:1345
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition opt.c:934
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition opt.c:1069
const VDPAUPixFmtMap * map
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:264
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:766
static const int silk_resample_delay[]
Definition dec.c:71
static const AVOption opus_options[]
Definition dec.c:761
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:682
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:634
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition dec.c:477
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition dec.c:654
#define OFFSET(x)
Definition dec.c:759
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition dec.c:238
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)
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
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:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
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,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static void finish(void)
Definition movenc.c:374
int size
int len
static double c[64]