3 * Copyright (c) 2012 Andrew D'Addesio
4 * Copyright (c) 2013-2014 Mozilla Corporation
6 * This file is part of FFmpeg.
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.
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.
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
26 * @author Andrew D'Addesio, Anton Khirnov
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
32 * Ogg-contained .opus files can be produced with opus-tools:
33 * http://git.xiph.org/?p=opus-tools.git
38 #include "libavutil/attributes.h"
39 #include "libavutil/audio_fifo.h"
40 #include "libavutil/channel_layout.h"
41 #include "libavutil/opt.h"
43 #include "libswresample/swresample.h"
51 #include "opus_celt.h"
53 static const uint16_t silk_frame_duration_ms[16] = {
61 /* number of samples of silence to feed to the resampler
63 static const int silk_resample_delay[] = {
67 static int get_silk_samplerate(int config)
76 static void opus_fade(float *out,
77 const float *in1, const float *in2,
78 const float *window, int len)
81 for (i = 0; i < len; i++)
82 out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
85 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
87 int celt_size = av_audio_fifo_size(s->celt_delay);
89 ret = swr_convert(s->swr,
90 (uint8_t**)s->out, nb_samples,
94 else if (ret != nb_samples) {
95 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
101 if (celt_size != nb_samples) {
102 av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
105 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
106 for (i = 0; i < s->output_channels; i++) {
107 s->fdsp->vector_fmac_scalar(s->out[i],
108 s->celt_output[i], 1.0,
113 if (s->redundancy_idx) {
114 for (i = 0; i < s->output_channels; i++)
115 opus_fade(s->out[i], s->out[i],
116 s->redundancy_output[i] + 120 + s->redundancy_idx,
117 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
118 s->redundancy_idx = 0;
121 s->out[0] += nb_samples;
122 s->out[1] += nb_samples;
123 s->out_size -= nb_samples * sizeof(float);
128 static int opus_init_resample(OpusStreamContext *s)
130 static const float delay[16] = { 0.0 };
131 const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
134 av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
135 ret = swr_init(s->swr);
137 av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
141 ret = swr_convert(s->swr,
143 delayptr, silk_resample_delay[s->packet.bandwidth]);
145 av_log(s->avctx, AV_LOG_ERROR,
146 "Error feeding initial silence to the resampler.\n");
153 static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
156 enum OpusBandwidth bw = s->packet.bandwidth;
158 if (s->packet.mode == OPUS_MODE_SILK &&
159 bw == OPUS_BANDWIDTH_MEDIUMBAND)
160 bw = OPUS_BANDWIDTH_WIDEBAND;
162 ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
165 ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
167 ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
168 s->redundancy_output,
169 s->packet.stereo + 1, 240,
170 0, ff_celt_band_end[s->packet.bandwidth]);
176 av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
180 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
182 int samples = s->packet.frame_duration;
184 int redundancy_size, redundancy_pos;
185 int ret, i, consumed;
186 int delayed_samples = s->delayed_samples;
188 ret = ff_opus_rc_dec_init(&s->rc, data, size);
192 /* decode the silk frame */
193 if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
194 if (!swr_is_initialized(s->swr)) {
195 ret = opus_init_resample(s);
200 samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
201 FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
202 s->packet.stereo + 1,
203 silk_frame_duration_ms[s->packet.config]);
205 av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
208 samples = swr_convert(s->swr,
209 (uint8_t**)s->out, s->packet.frame_duration,
210 (const uint8_t**)s->silk_output, samples);
212 av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
215 av_assert2((samples & 7) == 0);
216 s->delayed_samples += s->packet.frame_duration - samples;
218 ff_silk_flush(s->silk);
220 // decode redundancy information
221 consumed = opus_rc_tell(&s->rc);
222 if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
223 redundancy = ff_opus_rc_dec_log(&s->rc, 12);
224 else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
228 redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
230 if (s->packet.mode == OPUS_MODE_HYBRID)
231 redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
233 redundancy_size = size - (consumed + 7) / 8;
234 size -= redundancy_size;
236 av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
237 return AVERROR_INVALIDDATA;
240 if (redundancy_pos) {
241 ret = opus_decode_redundancy(s, data + size, redundancy_size);
244 ff_celt_flush(s->celt);
248 /* decode the CELT frame */
249 if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
250 float *out_tmp[2] = { s->out[0], s->out[1] };
251 float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
252 out_tmp : s->celt_output;
253 int celt_output_samples = samples;
254 int delay_samples = av_audio_fifo_size(s->celt_delay);
257 if (s->packet.mode == OPUS_MODE_HYBRID) {
258 av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
260 for (i = 0; i < s->output_channels; i++) {
261 s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
263 out_tmp[i] += delay_samples;
265 celt_output_samples -= delay_samples;
267 av_log(s->avctx, AV_LOG_WARNING,
268 "Spurious CELT delay samples present.\n");
269 av_audio_fifo_drain(s->celt_delay, delay_samples);
270 if (s->avctx->err_recognition & AV_EF_EXPLODE)
275 ff_opus_rc_dec_raw_init(&s->rc, data + size, size);
277 ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
278 s->packet.stereo + 1,
279 s->packet.frame_duration,
280 (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
281 ff_celt_band_end[s->packet.bandwidth]);
285 if (s->packet.mode == OPUS_MODE_HYBRID) {
286 int celt_delay = s->packet.frame_duration - celt_output_samples;
287 void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
288 s->celt_output[1] + celt_output_samples };
290 for (i = 0; i < s->output_channels; i++) {
291 s->fdsp->vector_fmac_scalar(out_tmp[i],
292 s->celt_output[i], 1.0,
293 celt_output_samples);
296 ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
301 ff_celt_flush(s->celt);
303 if (s->redundancy_idx) {
304 for (i = 0; i < s->output_channels; i++)
305 opus_fade(s->out[i], s->out[i],
306 s->redundancy_output[i] + 120 + s->redundancy_idx,
307 ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
308 s->redundancy_idx = 0;
311 if (!redundancy_pos) {
312 ff_celt_flush(s->celt);
313 ret = opus_decode_redundancy(s, data + size, redundancy_size);
317 for (i = 0; i < s->output_channels; i++) {
318 opus_fade(s->out[i] + samples - 120 + delayed_samples,
319 s->out[i] + samples - 120 + delayed_samples,
320 s->redundancy_output[i] + 120,
321 ff_celt_window2, 120 - delayed_samples);
323 s->redundancy_idx = 120 - delayed_samples;
326 for (i = 0; i < s->output_channels; i++) {
327 memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
328 opus_fade(s->out[i] + 120 + delayed_samples,
329 s->redundancy_output[i] + 120,
330 s->out[i] + 120 + delayed_samples,
331 ff_celt_window2, 120);
339 static int opus_decode_subpacket(OpusStreamContext *s,
340 const uint8_t *buf, int buf_size,
341 float **out, int out_size,
344 int output_samples = 0;
345 int flush_needed = 0;
350 s->out_size = out_size;
352 /* check if we need to flush the resampler */
353 if (swr_is_initialized(s->swr)) {
355 int64_t cur_samplerate;
356 av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
357 flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
359 flush_needed = !!s->delayed_samples;
363 if (!buf && !flush_needed)
366 /* use dummy output buffers if the channel is not mapped to anything */
368 (s->output_channels == 2 && !s->out[1])) {
369 av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size, s->out_size);
371 return AVERROR(ENOMEM);
373 s->out[0] = s->out_dummy;
375 s->out[1] = s->out_dummy;
378 /* flush the resampler if necessary */
380 ret = opus_flush_resample(s, s->delayed_samples);
382 av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
386 output_samples += s->delayed_samples;
387 s->delayed_samples = 0;
393 /* decode all the frames in the packet */
394 for (i = 0; i < s->packet.frame_count; i++) {
395 int size = s->packet.frame_size[i];
396 int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
399 av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
400 if (s->avctx->err_recognition & AV_EF_EXPLODE)
403 for (j = 0; j < s->output_channels; j++)
404 memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
405 samples = s->packet.frame_duration;
407 output_samples += samples;
409 for (j = 0; j < s->output_channels; j++)
410 s->out[j] += samples;
411 s->out_size -= samples * sizeof(float);
415 s->out[0] = s->out[1] = NULL;
418 return output_samples;
421 static int opus_decode_packet(AVCodecContext *avctx, void *data,
422 int *got_frame_ptr, AVPacket *avpkt)
424 OpusContext *c = avctx->priv_data;
425 AVFrame *frame = data;
426 const uint8_t *buf = avpkt->data;
427 int buf_size = avpkt->size;
428 int coded_samples = 0;
429 int decoded_samples = INT_MAX;
430 int delayed_samples = 0;
433 /* calculate the number of delayed samples */
434 for (i = 0; i < c->nb_streams; i++) {
435 OpusStreamContext *s = &c->streams[i];
438 delayed_samples = FFMAX(delayed_samples,
439 s->delayed_samples + av_audio_fifo_size(c->sync_buffers[i]));
442 /* decode the header of the first sub-packet to find out the sample count */
444 OpusPacket *pkt = &c->streams[0].packet;
445 ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
447 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
450 coded_samples += pkt->frame_count * pkt->frame_duration;
451 c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
454 frame->nb_samples = coded_samples + delayed_samples;
456 /* no input or buffered data => nothing to do */
457 if (!frame->nb_samples) {
462 /* setup the data buffers */
463 ret = ff_get_buffer(avctx, frame, 0);
466 frame->nb_samples = 0;
468 memset(c->out, 0, c->nb_streams * 2 * sizeof(*c->out));
469 for (i = 0; i < avctx->channels; i++) {
470 ChannelMap *map = &c->channel_maps[i];
472 c->out[2 * map->stream_idx + map->channel_idx] = (float*)frame->extended_data[i];
475 /* read the data from the sync buffers */
476 for (i = 0; i < c->nb_streams; i++) {
477 float **out = c->out + 2 * i;
478 int sync_size = av_audio_fifo_size(c->sync_buffers[i]);
480 float sync_dummy[32];
481 int out_dummy = (!out[0]) | ((!out[1]) << 1);
487 if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
490 ret = av_audio_fifo_read(c->sync_buffers[i], (void**)out, sync_size);
503 c->out_size[i] = frame->linesize[0] - ret * sizeof(float);
506 /* decode each sub-packet */
507 for (i = 0; i < c->nb_streams; i++) {
508 OpusStreamContext *s = &c->streams[i];
511 ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
513 av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
516 if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
517 av_log(avctx, AV_LOG_ERROR,
518 "Mismatching coded sample count in substream %d.\n", i);
519 return AVERROR_INVALIDDATA;
522 s->silk_samplerate = get_silk_samplerate(s->packet.config);
525 ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
526 c->out + 2 * i, c->out_size[i], coded_samples);
529 c->decoded_samples[i] = ret;
530 decoded_samples = FFMIN(decoded_samples, ret);
532 buf += s->packet.packet_size;
533 buf_size -= s->packet.packet_size;
536 /* buffer the extra samples */
537 for (i = 0; i < c->nb_streams; i++) {
538 int buffer_samples = c->decoded_samples[i] - decoded_samples;
539 if (buffer_samples) {
540 float *buf[2] = { c->out[2 * i + 0] ? c->out[2 * i + 0] : (float*)frame->extended_data[0],
541 c->out[2 * i + 1] ? c->out[2 * i + 1] : (float*)frame->extended_data[0] };
542 buf[0] += decoded_samples;
543 buf[1] += decoded_samples;
544 ret = av_audio_fifo_write(c->sync_buffers[i], (void**)buf, buffer_samples);
550 for (i = 0; i < avctx->channels; i++) {
551 ChannelMap *map = &c->channel_maps[i];
553 /* handle copied channels */
555 memcpy(frame->extended_data[i],
556 frame->extended_data[map->copy_idx],
558 } else if (map->silence) {
559 memset(frame->extended_data[i], 0, frame->linesize[0]);
562 if (c->gain_i && decoded_samples > 0) {
563 c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
564 (float*)frame->extended_data[i],
565 c->gain, FFALIGN(decoded_samples, 8));
569 frame->nb_samples = decoded_samples;
570 *got_frame_ptr = !!decoded_samples;
575 static av_cold void opus_decode_flush(AVCodecContext *ctx)
577 OpusContext *c = ctx->priv_data;
580 for (i = 0; i < c->nb_streams; i++) {
581 OpusStreamContext *s = &c->streams[i];
583 memset(&s->packet, 0, sizeof(s->packet));
584 s->delayed_samples = 0;
587 av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
590 av_audio_fifo_drain(c->sync_buffers[i], av_audio_fifo_size(c->sync_buffers[i]));
592 ff_silk_flush(s->silk);
593 ff_celt_flush(s->celt);
597 static av_cold int opus_decode_close(AVCodecContext *avctx)
599 OpusContext *c = avctx->priv_data;
602 for (i = 0; i < c->nb_streams; i++) {
603 OpusStreamContext *s = &c->streams[i];
605 ff_silk_free(&s->silk);
606 ff_celt_free(&s->celt);
608 av_freep(&s->out_dummy);
609 s->out_dummy_allocated_size = 0;
611 av_audio_fifo_free(s->celt_delay);
615 av_freep(&c->streams);
617 if (c->sync_buffers) {
618 for (i = 0; i < c->nb_streams; i++)
619 av_audio_fifo_free(c->sync_buffers[i]);
621 av_freep(&c->sync_buffers);
622 av_freep(&c->decoded_samples);
624 av_freep(&c->out_size);
628 av_freep(&c->channel_maps);
634 static av_cold int opus_decode_init(AVCodecContext *avctx)
636 OpusContext *c = avctx->priv_data;
639 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
640 avctx->sample_rate = 48000;
642 c->fdsp = avpriv_float_dsp_alloc(0);
644 return AVERROR(ENOMEM);
646 /* find out the channel configuration */
647 ret = ff_opus_parse_extradata(avctx, c);
653 /* allocate and init each independent decoder */
654 c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
655 c->out = av_mallocz_array(c->nb_streams, 2 * sizeof(*c->out));
656 c->out_size = av_mallocz_array(c->nb_streams, sizeof(*c->out_size));
657 c->sync_buffers = av_mallocz_array(c->nb_streams, sizeof(*c->sync_buffers));
658 c->decoded_samples = av_mallocz_array(c->nb_streams, sizeof(*c->decoded_samples));
659 if (!c->streams || !c->sync_buffers || !c->decoded_samples || !c->out || !c->out_size) {
661 ret = AVERROR(ENOMEM);
665 for (i = 0; i < c->nb_streams; i++) {
666 OpusStreamContext *s = &c->streams[i];
669 s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
673 for (j = 0; j < s->output_channels; j++) {
674 s->silk_output[j] = s->silk_buf[j];
675 s->celt_output[j] = s->celt_buf[j];
676 s->redundancy_output[j] = s->redundancy_buf[j];
685 layout = (s->output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
686 av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
687 av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
688 av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
689 av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
690 av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
691 av_opt_set_int(s->swr, "filter_size", 16, 0);
693 ret = ff_silk_init(avctx, &s->silk, s->output_channels);
697 ret = ff_celt_init(avctx, &s->celt, s->output_channels);
701 s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
702 s->output_channels, 1024);
703 if (!s->celt_delay) {
704 ret = AVERROR(ENOMEM);
708 c->sync_buffers[i] = av_audio_fifo_alloc(avctx->sample_fmt,
709 s->output_channels, 32);
710 if (!c->sync_buffers[i]) {
711 ret = AVERROR(ENOMEM);
718 opus_decode_close(avctx);
722 AVCodec ff_opus_decoder = {
724 .long_name = NULL_IF_CONFIG_SMALL("Opus"),
725 .type = AVMEDIA_TYPE_AUDIO,
726 .id = AV_CODEC_ID_OPUS,
727 .priv_data_size = sizeof(OpusContext),
728 .init = opus_decode_init,
729 .close = opus_decode_close,
730 .decode = opus_decode_packet,
731 .flush = opus_decode_flush,
732 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,