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/common.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/intreadwrite.h"
33 struct libopus_context {
38 union { int i; double d; } gain;
42 static int opus_error_to_averror(int err)
45 case OPUS_BAD_ARG: return AVERROR(EINVAL);
46 case OPUS_BUFFER_TOO_SMALL: return AVERROR_BUFFER_TOO_SMALL;
47 case OPUS_INTERNAL_ERROR: return AVERROR(EFAULT);
48 case OPUS_INVALID_PACKET: return AVERROR_INVALIDDATA;
49 case OPUS_UNIMPLEMENTED: return AVERROR(ENOSYS);
50 case OPUS_INVALID_STATE: return AVERROR_EXTERNAL;
51 case OPUS_ALLOC_FAIL: return AVERROR(ENOMEM);
52 default: return AVERROR(EINVAL);
56 #define OPUS_HEAD_SIZE 19
58 static av_cold int libopus_decode_init(AVCodecContext *avc)
60 struct libopus_context *opus = avc->priv_data;
61 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
62 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
64 avc->sample_rate = 48000;
65 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
66 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
67 avc->channel_layout = avc->channels > 8 ? 0 :
68 ff_vorbis_channel_layouts[avc->channels - 1];
70 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
71 opus->pre_skip = AV_RL16(avc->extradata + 10);
72 gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
73 channel_map = AV_RL8 (avc->extradata + 18);
75 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
76 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
77 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
78 if (nb_streams + nb_coupled != avc->channels)
79 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
80 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
82 if (avc->channels > 2 || channel_map) {
83 av_log(avc, AV_LOG_ERROR,
84 "No channel mapping for %d channels.\n", avc->channels);
85 return AVERROR(EINVAL);
88 nb_coupled = avc->channels > 1;
89 mapping = mapping_arr;
92 if (avc->channels > 2 && avc->channels <= 8) {
93 const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
96 /* Remap channels from vorbis order to ffmpeg order */
97 for (ch = 0; ch < avc->channels; ch++)
98 mapping_arr[ch] = mapping[vorbis_offset[ch]];
99 mapping = mapping_arr;
102 opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
103 nb_streams, nb_coupled,
106 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
108 return opus_error_to_averror(ret);
112 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
114 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
118 double gain_lin = pow(10, gain_db / (20.0 * 256));
119 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
120 opus->gain.d = gain_lin;
122 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
126 avc->internal->skip_samples = opus->pre_skip;
127 avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
128 avcodec_get_frame_defaults(&opus->frame);
129 avc->coded_frame = &opus->frame;
133 static av_cold int libopus_decode_close(AVCodecContext *avc)
135 struct libopus_context *opus = avc->priv_data;
137 opus_multistream_decoder_destroy(opus->dec);
141 #define MAX_FRAME_SIZE (960 * 6)
143 static int libopus_decode(AVCodecContext *avc, void *frame,
144 int *got_frame_ptr, AVPacket *pkt)
146 struct libopus_context *opus = avc->priv_data;
149 opus->frame.nb_samples = MAX_FRAME_SIZE;
150 ret = avc->get_buffer(avc, &opus->frame);
152 av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
156 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
157 nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
158 (opus_int16 *)opus->frame.data[0],
159 opus->frame.nb_samples, 0);
161 nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
162 (float *)opus->frame.data[0],
163 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 opus_error_to_averror(nb_samples);
171 #ifndef OPUS_SET_GAIN
173 int i = avc->channels * nb_samples;
174 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
175 float *pcm = (float *)opus->frame.data[0];
176 for (; i > 0; i--, pcm++)
177 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
179 int16_t *pcm = (int16_t *)opus->frame.data[0];
180 for (; i > 0; i--, pcm++)
181 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
186 opus->frame.nb_samples = nb_samples;
187 *(AVFrame *)frame = opus->frame;
192 static void libopus_flush(AVCodecContext *avc)
194 struct libopus_context *opus = avc->priv_data;
196 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
197 /* The stream can have been extracted by a tool that is not Opus-aware.
198 Therefore, any packet can become the first of the stream. */
199 avc->internal->skip_samples = opus->pre_skip;
202 AVCodec ff_libopus_decoder = {
204 .type = AVMEDIA_TYPE_AUDIO,
205 .id = AV_CODEC_ID_OPUS,
206 .priv_data_size = sizeof(struct libopus_context),
207 .init = libopus_decode_init,
208 .close = libopus_decode_close,
209 .decode = libopus_decode,
210 .flush = libopus_flush,
211 .capabilities = CODEC_CAP_DR1,
212 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
213 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
215 AV_SAMPLE_FMT_NONE },