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>
27 #include "libavutil/avassert.h"
28 #include "libavutil/intreadwrite.h"
30 struct libopus_context {
35 union { int i; double d; } gain;
39 static int ff_opus_error_to_averror(int err)
42 case OPUS_BAD_ARG: return AVERROR(EINVAL);
43 case OPUS_BUFFER_TOO_SMALL: return AVERROR_BUFFER_TOO_SMALL;
44 case OPUS_INTERNAL_ERROR: return AVERROR(EFAULT);
45 case OPUS_INVALID_PACKET: return AVERROR_INVALIDDATA;
46 case OPUS_UNIMPLEMENTED: return AVERROR(ENOSYS);
47 case OPUS_INVALID_STATE: return AVERROR_EXTERNAL;
48 case OPUS_ALLOC_FAIL: return AVERROR(ENOMEM);
49 default: return AVERROR(EINVAL);
53 static inline void reorder(uint8_t *data, unsigned channels, unsigned bps,
54 unsigned samples, const uint8_t *map)
59 av_assert1(channels * bps <= sizeof(tmp));
60 for (; samples > 0; samples--) {
61 for (i = 0; i < channels; i++)
62 memcpy(tmp + bps * i, data + bps * map[i], bps);
63 memcpy(data, tmp, bps * channels);
64 data += bps * channels;
68 #define OPUS_HEAD_SIZE 19
70 static av_cold int libopus_dec_init(AVCodecContext *avc)
72 struct libopus_context *opus = avc->priv_data;
73 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
74 uint8_t mapping_stereo[] = { 0, 1 }, *mapping;
76 avc->sample_rate = 48000;
77 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
78 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
79 avc->channel_layout = avc->channels > 8 ? 0 :
80 ff_vorbis_channel_layouts[avc->channels - 1];
82 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
83 opus->pre_skip = AV_RL16(avc->extradata + 10);
84 gain_db = AV_RL16(avc->extradata + 16);
85 channel_map = AV_RL8 (avc->extradata + 18);
86 gain_db -= (gain_db & 0x8000) << 1; /* signed */
88 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
89 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
90 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
91 if (nb_streams + nb_coupled != avc->channels)
92 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
93 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
95 if (avc->channels > 2 || channel_map) {
96 av_log(avc, AV_LOG_ERROR,
97 "No channel mapping for %d channels.\n", avc->channels);
98 return AVERROR(EINVAL);
101 nb_coupled = avc->channels > 1;
102 mapping = mapping_stereo;
105 opus->dec = opus_multistream_decoder_create(
106 avc->sample_rate, avc->channels,
107 nb_streams, nb_coupled, mapping, &ret);
109 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
111 return ff_opus_error_to_averror(ret);
115 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
117 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
121 double gain_lin = pow(10, gain_db / (20.0 * 256));
122 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
123 opus->gain.d = gain_lin;
125 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
129 avc->internal->skip_samples = opus->pre_skip;
130 avcodec_get_frame_defaults(&opus->frame);
131 avc->coded_frame = &opus->frame;
135 static av_cold int libopus_dec_close(AVCodecContext *avc)
137 struct libopus_context *opus = avc->priv_data;
139 opus_multistream_decoder_destroy(opus->dec);
143 #define MAX_FRAME_SIZE (960*6)
145 static int libopus_dec_decode(AVCodecContext *avc, void *frame,
146 int *got_frame_ptr, AVPacket *pkt)
148 struct libopus_context *opus = avc->priv_data;
151 opus->frame.nb_samples = MAX_FRAME_SIZE;
152 ret = avc->get_buffer(avc, &opus->frame);
154 av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
158 nb_samples = avc->sample_fmt == AV_SAMPLE_FMT_S16 ?
159 opus_multistream_decode (opus->dec, pkt->data, pkt->size,
160 (void *)opus->frame.data[0],
161 opus->frame.nb_samples, 0) :
162 opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
163 (void *)opus->frame.data[0],
164 opus->frame.nb_samples, 0);
165 if (nb_samples < 0) {
166 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
167 opus_strerror(nb_samples));
168 return ff_opus_error_to_averror(nb_samples);
171 if (avc->channels > 3 && avc->channels <= 8) {
172 const uint8_t *m = ff_vorbis_channel_layout_offsets[avc->channels - 1];
173 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
174 reorder(opus->frame.data[0], avc->channels, 2, nb_samples, m);
176 reorder(opus->frame.data[0], avc->channels, 4, nb_samples, m);
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 *)opus->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 *)opus->frame.data[0];
188 for (; i > 0; i--, pcm++)
189 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
194 opus->frame.nb_samples = nb_samples;
195 *(AVFrame *)frame = opus->frame;
200 static void libopus_dec_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;
210 AVCodec ff_libopus_decoder = {
212 .type = AVMEDIA_TYPE_AUDIO,
214 .priv_data_size = sizeof(struct libopus_context),
215 .init = libopus_dec_init,
216 .close = libopus_dec_close,
217 .decode = libopus_dec_decode,
218 .flush = libopus_dec_flush,
219 .capabilities = CODEC_CAP_DR1,
220 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),