2 * Opus decoder using libopus
3 * Copyright (c) 2012 Nicolas George
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <opus_multistream.h>
25 #include "libavutil/avassert.h"
26 #include "libavutil/intreadwrite.h"
33 struct libopus_context {
37 union { int i; double d; } gain;
41 #define OPUS_HEAD_SIZE 19
43 static av_cold int libopus_decode_init(AVCodecContext *avc)
45 struct libopus_context *opus = avc->priv_data;
46 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
47 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
49 avc->sample_rate = 48000;
50 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
51 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
52 avc->channel_layout = avc->channels > 8 ? 0 :
53 ff_vorbis_channel_layouts[avc->channels - 1];
55 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
56 opus->pre_skip = AV_RL16(avc->extradata + 10);
57 gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
58 channel_map = AV_RL8 (avc->extradata + 18);
60 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
61 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
62 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
63 if (nb_streams + nb_coupled != avc->channels)
64 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
65 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
67 if (avc->channels > 2 || channel_map) {
68 av_log(avc, AV_LOG_ERROR,
69 "No channel mapping for %d channels.\n", avc->channels);
70 return AVERROR(EINVAL);
73 nb_coupled = avc->channels > 1;
74 mapping = mapping_arr;
77 if (avc->channels > 2 && avc->channels <= 8) {
78 const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
81 /* Remap channels from vorbis order to ffmpeg order */
82 for (ch = 0; ch < avc->channels; ch++)
83 mapping_arr[ch] = mapping[vorbis_offset[ch]];
84 mapping = mapping_arr;
87 opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
88 nb_streams, nb_coupled,
91 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
93 return ff_opus_error_to_averror(ret);
97 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
99 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
103 double gain_lin = pow(10, gain_db / (20.0 * 256));
104 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
105 opus->gain.d = gain_lin;
107 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
111 avc->internal->skip_samples = opus->pre_skip;
112 avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
117 static av_cold int libopus_decode_close(AVCodecContext *avc)
119 struct libopus_context *opus = avc->priv_data;
121 opus_multistream_decoder_destroy(opus->dec);
125 #define MAX_FRAME_SIZE (960 * 6)
127 static int libopus_decode(AVCodecContext *avc, void *data,
128 int *got_frame_ptr, AVPacket *pkt)
130 struct libopus_context *opus = avc->priv_data;
131 AVFrame *frame = data;
134 frame->nb_samples = MAX_FRAME_SIZE;
135 ret = ff_get_buffer(avc, frame);
137 av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
141 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
142 nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
143 (opus_int16 *)frame->data[0],
144 frame->nb_samples, 0);
146 nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
147 (float *)frame->data[0],
148 frame->nb_samples, 0);
150 if (nb_samples < 0) {
151 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
152 opus_strerror(nb_samples));
153 return ff_opus_error_to_averror(nb_samples);
156 #ifndef OPUS_SET_GAIN
158 int i = avc->channels * nb_samples;
159 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
160 float *pcm = (float *)frame->data[0];
161 for (; i > 0; i--, pcm++)
162 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
164 int16_t *pcm = (int16_t *)frame->data[0];
165 for (; i > 0; i--, pcm++)
166 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
171 frame->nb_samples = nb_samples;
177 static void libopus_flush(AVCodecContext *avc)
179 struct libopus_context *opus = avc->priv_data;
181 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
182 /* The stream can have been extracted by a tool that is not Opus-aware.
183 Therefore, any packet can become the first of the stream. */
184 avc->internal->skip_samples = opus->pre_skip;
187 AVCodec ff_libopus_decoder = {
189 .type = AVMEDIA_TYPE_AUDIO,
190 .id = AV_CODEC_ID_OPUS,
191 .priv_data_size = sizeof(struct libopus_context),
192 .init = libopus_decode_init,
193 .close = libopus_decode_close,
194 .decode = libopus_decode,
195 .flush = libopus_flush,
196 .capabilities = CODEC_CAP_DR1,
197 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
198 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
200 AV_SAMPLE_FMT_NONE },