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/internal.h"
26 #include "libavutil/intreadwrite.h"
34 struct libopus_context {
38 union { int i; double d; } gain;
42 #define OPUS_HEAD_SIZE 19
44 static av_cold int libopus_decode_init(AVCodecContext *avc)
46 struct libopus_context *opus = avc->priv_data;
47 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
48 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
50 avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
51 if (avc->channels <= 0) {
52 av_log(avc, AV_LOG_WARNING,
53 "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
57 avc->sample_rate = 48000;
58 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
59 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
61 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
62 opus->pre_skip = AV_RL16(avc->extradata + 10);
63 gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
64 channel_map = AV_RL8 (avc->extradata + 18);
66 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
67 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
68 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
69 if (nb_streams + nb_coupled != avc->channels)
70 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
71 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
73 if (avc->channels > 2 || channel_map) {
74 av_log(avc, AV_LOG_ERROR,
75 "No channel mapping for %d channels.\n", avc->channels);
76 return AVERROR(EINVAL);
79 nb_coupled = avc->channels > 1;
80 mapping = mapping_arr;
83 if (channel_map == 1) {
84 avc->channel_layout = avc->channels > 8 ? 0 :
85 ff_vorbis_channel_layouts[avc->channels - 1];
86 if (avc->channels > 2 && avc->channels <= 8) {
87 const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
90 /* Remap channels from Vorbis order to ffmpeg order */
91 for (ch = 0; ch < avc->channels; ch++)
92 mapping_arr[ch] = mapping[vorbis_offset[ch]];
93 mapping = mapping_arr;
95 } else if (channel_map == 2) {
96 int ambisonic_order = ff_sqrt(avc->channels) - 1;
97 if (avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) &&
98 avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) + 2) {
99 av_log(avc, AV_LOG_ERROR,
100 "Channel mapping 2 is only specified for channel counts"
101 " which can be written as (n + 1)^2 or (n + 2)^2 + 2"
102 " for nonnegative integer n\n");
103 return AVERROR_INVALIDDATA;
105 if (avc->channels > 227) {
106 av_log(avc, AV_LOG_ERROR, "Too many channels\n");
107 return AVERROR_INVALIDDATA;
109 avc->channel_layout = 0;
111 avc->channel_layout = 0;
114 opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
115 nb_streams, nb_coupled,
118 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
120 return ff_opus_error_to_averror(ret);
124 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
126 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
130 double gain_lin = ff_exp10(gain_db / (20.0 * 256));
131 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
132 opus->gain.d = gain_lin;
134 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
138 /* Decoder delay (in samples) at 48kHz */
139 avc->delay = avc->internal->skip_samples = opus->pre_skip;
144 static av_cold int libopus_decode_close(AVCodecContext *avc)
146 struct libopus_context *opus = avc->priv_data;
148 opus_multistream_decoder_destroy(opus->dec);
152 #define MAX_FRAME_SIZE (960 * 6)
154 static int libopus_decode(AVCodecContext *avc, void *data,
155 int *got_frame_ptr, AVPacket *pkt)
157 struct libopus_context *opus = avc->priv_data;
158 AVFrame *frame = data;
161 frame->nb_samples = MAX_FRAME_SIZE;
162 if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
165 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
166 nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
167 (opus_int16 *)frame->data[0],
168 frame->nb_samples, 0);
170 nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
171 (float *)frame->data[0],
172 frame->nb_samples, 0);
174 if (nb_samples < 0) {
175 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
176 opus_strerror(nb_samples));
177 return ff_opus_error_to_averror(nb_samples);
180 #ifndef OPUS_SET_GAIN
182 int i = avc->channels * nb_samples;
183 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
184 float *pcm = (float *)frame->data[0];
185 for (; i > 0; i--, pcm++)
186 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
188 int16_t *pcm = (int16_t *)frame->data[0];
189 for (; i > 0; i--, pcm++)
190 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
195 frame->nb_samples = nb_samples;
201 static void libopus_flush(AVCodecContext *avc)
203 struct libopus_context *opus = avc->priv_data;
205 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
206 /* The stream can have been extracted by a tool that is not Opus-aware.
207 Therefore, any packet can become the first of the stream. */
208 avc->internal->skip_samples = opus->pre_skip;
211 AVCodec ff_libopus_decoder = {
213 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
214 .type = AVMEDIA_TYPE_AUDIO,
215 .id = AV_CODEC_ID_OPUS,
216 .priv_data_size = sizeof(struct libopus_context),
217 .init = libopus_decode_init,
218 .close = libopus_decode_close,
219 .decode = libopus_decode,
220 .flush = libopus_flush,
221 .capabilities = AV_CODEC_CAP_DR1,
222 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
224 AV_SAMPLE_FMT_NONE },