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"
28 #include "libavutil/opt.h"
36 struct libopus_context {
41 union { int i; double d; } gain;
43 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
48 #define OPUS_HEAD_SIZE 19
50 static av_cold int libopus_decode_init(AVCodecContext *avc)
52 struct libopus_context *opus = avc->priv_data;
53 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
54 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
56 avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
57 if (avc->channels <= 0) {
58 av_log(avc, AV_LOG_WARNING,
59 "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
63 avc->sample_rate = 48000;
64 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
65 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
66 avc->channel_layout = avc->channels > 8 ? 0 :
67 ff_vorbis_channel_layouts[avc->channels - 1];
69 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
70 opus->pre_skip = AV_RL16(avc->extradata + 10);
71 gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
72 channel_map = AV_RL8 (avc->extradata + 18);
74 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
75 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
76 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
77 if (nb_streams + nb_coupled != avc->channels)
78 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
79 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
81 if (avc->channels > 2 || channel_map) {
82 av_log(avc, AV_LOG_ERROR,
83 "No channel mapping for %d channels.\n", avc->channels);
84 return AVERROR(EINVAL);
87 nb_coupled = avc->channels > 1;
88 mapping = mapping_arr;
91 if (avc->channels > 2 && avc->channels <= 8) {
92 const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
95 /* Remap channels from Vorbis order to ffmpeg order */
96 for (ch = 0; ch < avc->channels; ch++)
97 mapping_arr[ch] = mapping[vorbis_offset[ch]];
98 mapping = mapping_arr;
101 opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
102 nb_streams, nb_coupled,
105 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
107 return ff_opus_error_to_averror(ret);
111 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
113 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
117 double gain_lin = ff_exp10(gain_db / (20.0 * 256));
118 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
119 opus->gain.d = gain_lin;
121 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
125 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
126 ret = opus_multistream_decoder_ctl(opus->dec,
127 OPUS_SET_PHASE_INVERSION_DISABLED(!opus->apply_phase_inv));
129 av_log(avc, AV_LOG_WARNING,
130 "Unable to set phase inversion: %s\n",
134 /* Decoder delay (in samples) at 48kHz */
135 avc->delay = avc->internal->skip_samples = opus->pre_skip;
140 static av_cold int libopus_decode_close(AVCodecContext *avc)
142 struct libopus_context *opus = avc->priv_data;
145 opus_multistream_decoder_destroy(opus->dec);
151 #define MAX_FRAME_SIZE (960 * 6)
153 static int libopus_decode(AVCodecContext *avc, void *data,
154 int *got_frame_ptr, AVPacket *pkt)
156 struct libopus_context *opus = avc->priv_data;
157 AVFrame *frame = data;
160 frame->nb_samples = MAX_FRAME_SIZE;
161 if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
164 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
165 nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
166 (opus_int16 *)frame->data[0],
167 frame->nb_samples, 0);
169 nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
170 (float *)frame->data[0],
171 frame->nb_samples, 0);
173 if (nb_samples < 0) {
174 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
175 opus_strerror(nb_samples));
176 return ff_opus_error_to_averror(nb_samples);
179 #ifndef OPUS_SET_GAIN
181 int i = avc->channels * nb_samples;
182 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
183 float *pcm = (float *)frame->data[0];
184 for (; i > 0; i--, pcm++)
185 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
187 int16_t *pcm = (int16_t *)frame->data[0];
188 for (; i > 0; i--, pcm++)
189 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
194 frame->nb_samples = nb_samples;
200 static void libopus_flush(AVCodecContext *avc)
202 struct libopus_context *opus = avc->priv_data;
204 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
205 /* The stream can have been extracted by a tool that is not Opus-aware.
206 Therefore, any packet can become the first of the stream. */
207 avc->internal->skip_samples = opus->pre_skip;
211 #define OFFSET(x) offsetof(struct libopus_context, x)
212 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
213 static const AVOption libopusdec_options[] = {
214 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
215 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
220 static const AVClass libopusdec_class = {
221 .class_name = "libopusdec",
222 .item_name = av_default_item_name,
223 .option = libopusdec_options,
224 .version = LIBAVUTIL_VERSION_INT,
228 AVCodec ff_libopus_decoder = {
230 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
231 .type = AVMEDIA_TYPE_AUDIO,
232 .id = AV_CODEC_ID_OPUS,
233 .priv_data_size = sizeof(struct libopus_context),
234 .init = libopus_decode_init,
235 .close = libopus_decode_close,
236 .decode = libopus_decode,
237 .flush = libopus_flush,
238 .capabilities = AV_CODEC_CAP_DR1,
239 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
240 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
242 AV_SAMPLE_FMT_NONE },
243 .priv_class = &libopusdec_class,
244 .wrapper_name = "libopus",