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"
27 #include "libavutil/ffmath.h"
35 struct libopus_context {
39 union { int i; double d; } gain;
43 #define OPUS_HEAD_SIZE 19
45 static av_cold int libopus_decode_init(AVCodecContext *avc)
47 struct libopus_context *opus = avc->priv_data;
48 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
49 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
51 avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
52 if (avc->channels <= 0) {
53 av_log(avc, AV_LOG_WARNING,
54 "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
58 avc->sample_rate = 48000;
59 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
60 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
62 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
63 opus->pre_skip = AV_RL16(avc->extradata + 10);
64 gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
65 channel_map = AV_RL8 (avc->extradata + 18);
67 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
68 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
69 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
70 if (nb_streams + nb_coupled != avc->channels)
71 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
72 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
74 if (avc->channels > 2 || channel_map) {
75 av_log(avc, AV_LOG_ERROR,
76 "No channel mapping for %d channels.\n", avc->channels);
77 return AVERROR(EINVAL);
80 nb_coupled = avc->channels > 1;
81 mapping = mapping_arr;
84 if (channel_map == 1) {
85 avc->channel_layout = avc->channels > 8 ? 0 :
86 ff_vorbis_channel_layouts[avc->channels - 1];
87 if (avc->channels > 2 && avc->channels <= 8) {
88 const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
91 /* Remap channels from Vorbis order to ffmpeg order */
92 for (ch = 0; ch < avc->channels; ch++)
93 mapping_arr[ch] = mapping[vorbis_offset[ch]];
94 mapping = mapping_arr;
96 } else if (channel_map == 2) {
97 int ambisonic_order = ff_sqrt(avc->channels) - 1;
98 if (avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) &&
99 avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) + 2) {
100 av_log(avc, AV_LOG_ERROR,
101 "Channel mapping 2 is only specified for channel counts"
102 " which can be written as (n + 1)^2 or (n + 2)^2 + 2"
103 " for nonnegative integer n\n");
104 return AVERROR_INVALIDDATA;
106 if (avc->channels > 227) {
107 av_log(avc, AV_LOG_ERROR, "Too many channels\n");
108 return AVERROR_INVALIDDATA;
110 avc->channel_layout = 0;
112 avc->channel_layout = 0;
115 opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
116 nb_streams, nb_coupled,
119 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
121 return ff_opus_error_to_averror(ret);
125 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
127 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
131 double gain_lin = ff_exp10(gain_db / (20.0 * 256));
132 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
133 opus->gain.d = gain_lin;
135 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
139 /* Decoder delay (in samples) at 48kHz */
140 avc->delay = avc->internal->skip_samples = opus->pre_skip;
145 static av_cold int libopus_decode_close(AVCodecContext *avc)
147 struct libopus_context *opus = avc->priv_data;
149 opus_multistream_decoder_destroy(opus->dec);
153 #define MAX_FRAME_SIZE (960 * 6)
155 static int libopus_decode(AVCodecContext *avc, void *data,
156 int *got_frame_ptr, AVPacket *pkt)
158 struct libopus_context *opus = avc->priv_data;
159 AVFrame *frame = data;
162 frame->nb_samples = MAX_FRAME_SIZE;
163 if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
166 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
167 nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
168 (opus_int16 *)frame->data[0],
169 frame->nb_samples, 0);
171 nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
172 (float *)frame->data[0],
173 frame->nb_samples, 0);
175 if (nb_samples < 0) {
176 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
177 opus_strerror(nb_samples));
178 return ff_opus_error_to_averror(nb_samples);
181 #ifndef OPUS_SET_GAIN
183 int i = avc->channels * nb_samples;
184 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
185 float *pcm = (float *)frame->data[0];
186 for (; i > 0; i--, pcm++)
187 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
189 int16_t *pcm = (int16_t *)frame->data[0];
190 for (; i > 0; i--, pcm++)
191 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
196 frame->nb_samples = nb_samples;
202 static void libopus_flush(AVCodecContext *avc)
204 struct libopus_context *opus = avc->priv_data;
206 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
207 /* The stream can have been extracted by a tool that is not Opus-aware.
208 Therefore, any packet can become the first of the stream. */
209 avc->internal->skip_samples = opus->pre_skip;
212 AVCodec ff_libopus_decoder = {
214 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
215 .type = AVMEDIA_TYPE_AUDIO,
216 .id = AV_CODEC_ID_OPUS,
217 .priv_data_size = sizeof(struct libopus_context),
218 .init = libopus_decode_init,
219 .close = libopus_decode_close,
220 .decode = libopus_decode,
221 .flush = libopus_flush,
222 .capabilities = AV_CODEC_CAP_DR1,
223 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
225 AV_SAMPLE_FMT_NONE },