1/*
2 * Opus decoder using libopus
3 * Copyright (c) 2012 Nicolas George
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include <opus.h>
23#include <opus_multistream.h>
24
25#include "libavutil/internal.h"
26#include "libavutil/intreadwrite.h"
27#include "libavutil/ffmath.h"
28#include "libavutil/opt.h"
29
30#include "avcodec.h"
31#include "codec_internal.h"
32#include "internal.h"
33#include "vorbis.h"
34#include "mathops.h"
35#include "libopus.h"
36
37struct libopus_context {
38    AVClass *class;
39    OpusMSDecoder *dec;
40    int pre_skip;
41#ifndef OPUS_SET_GAIN
42    union { int i; double d; } gain;
43#endif
44#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
45    int apply_phase_inv;
46#endif
47};
48
49#define OPUS_HEAD_SIZE 19
50
51static av_cold int libopus_decode_init(AVCodecContext *avc)
52{
53    struct libopus_context *opus = avc->priv_data;
54    int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled, channels;
55    uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
56
57    channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->ch_layout.nb_channels == 1) ? 1 : 2;
58    if (channels <= 0) {
59        av_log(avc, AV_LOG_WARNING,
60               "Invalid number of channels %d, defaulting to stereo\n", channels);
61        channels = 2;
62    }
63
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    av_channel_layout_uninit(&avc->ch_layout);
68    if (channels > 8) {
69        avc->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
70        avc->ch_layout.nb_channels = channels;
71    } else {
72        av_channel_layout_copy(&avc->ch_layout, &ff_vorbis_ch_layouts[channels - 1]);
73    }
74
75    if (avc->extradata_size >= OPUS_HEAD_SIZE) {
76        opus->pre_skip = AV_RL16(avc->extradata + 10);
77        gain_db     = sign_extend(AV_RL16(avc->extradata + 16), 16);
78        channel_map = AV_RL8 (avc->extradata + 18);
79    }
80    if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + channels) {
81        nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
82        nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
83        if (nb_streams + nb_coupled != channels)
84            av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
85        mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
86    } else {
87        if (channels > 2 || channel_map) {
88            av_log(avc, AV_LOG_ERROR,
89                   "No channel mapping for %d channels.\n", channels);
90            return AVERROR(EINVAL);
91        }
92        nb_streams = 1;
93        nb_coupled = channels > 1;
94        mapping    = mapping_arr;
95    }
96
97    if (channels > 2 && channels <= 8) {
98        const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[channels - 1];
99        int ch;
100
101        /* Remap channels from Vorbis order to ffmpeg order */
102        for (ch = 0; ch < channels; ch++)
103            mapping_arr[ch] = mapping[vorbis_offset[ch]];
104        mapping = mapping_arr;
105    }
106
107    opus->dec = opus_multistream_decoder_create(avc->sample_rate, channels,
108                                                nb_streams, nb_coupled,
109                                                mapping, &ret);
110    if (!opus->dec) {
111        av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
112               opus_strerror(ret));
113        return ff_opus_error_to_averror(ret);
114    }
115
116#ifdef OPUS_SET_GAIN
117    ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
118    if (ret != OPUS_OK)
119        av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
120               opus_strerror(ret));
121#else
122    {
123        double gain_lin = ff_exp10(gain_db / (20.0 * 256));
124        if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
125            opus->gain.d = gain_lin;
126        else
127            opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
128    }
129#endif
130
131#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
132    ret = opus_multistream_decoder_ctl(opus->dec,
133                                       OPUS_SET_PHASE_INVERSION_DISABLED(!opus->apply_phase_inv));
134    if (ret != OPUS_OK)
135        av_log(avc, AV_LOG_WARNING,
136               "Unable to set phase inversion: %s\n",
137               opus_strerror(ret));
138#endif
139
140    /* Decoder delay (in samples) at 48kHz */
141    avc->delay = avc->internal->skip_samples = opus->pre_skip;
142
143    return 0;
144}
145
146static av_cold int libopus_decode_close(AVCodecContext *avc)
147{
148    struct libopus_context *opus = avc->priv_data;
149
150    if (opus->dec) {
151        opus_multistream_decoder_destroy(opus->dec);
152        opus->dec = NULL;
153    }
154    return 0;
155}
156
157#define MAX_FRAME_SIZE (960 * 6)
158
159static int libopus_decode(AVCodecContext *avc, AVFrame *frame,
160                          int *got_frame_ptr, AVPacket *pkt)
161{
162    struct libopus_context *opus = avc->priv_data;
163    int ret, nb_samples;
164
165    frame->nb_samples = MAX_FRAME_SIZE;
166    if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
167        return ret;
168
169    if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
170        nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
171                                             (opus_int16 *)frame->data[0],
172                                             frame->nb_samples, 0);
173    else
174        nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
175                                                   (float *)frame->data[0],
176                                                   frame->nb_samples, 0);
177
178    if (nb_samples < 0) {
179        av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
180               opus_strerror(nb_samples));
181        return ff_opus_error_to_averror(nb_samples);
182    }
183
184#ifndef OPUS_SET_GAIN
185    {
186        int i = avc->ch_layout.nb_channels * nb_samples;
187        if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
188            float *pcm = (float *)frame->data[0];
189            for (; i > 0; i--, pcm++)
190                *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
191        } else {
192            int16_t *pcm = (int16_t *)frame->data[0];
193            for (; i > 0; i--, pcm++)
194                *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
195        }
196    }
197#endif
198
199    frame->nb_samples = nb_samples;
200    *got_frame_ptr    = 1;
201
202    return pkt->size;
203}
204
205static void libopus_flush(AVCodecContext *avc)
206{
207    struct libopus_context *opus = avc->priv_data;
208
209    opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
210    /* The stream can have been extracted by a tool that is not Opus-aware.
211       Therefore, any packet can become the first of the stream. */
212    avc->internal->skip_samples = opus->pre_skip;
213}
214
215
216#define OFFSET(x) offsetof(struct libopus_context, x)
217#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
218static const AVOption libopusdec_options[] = {
219#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
220    { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
221#endif
222    { NULL },
223};
224
225static const AVClass libopusdec_class = {
226    .class_name = "libopusdec",
227    .item_name  = av_default_item_name,
228    .option     = libopusdec_options,
229    .version    = LIBAVUTIL_VERSION_INT,
230};
231
232
233const FFCodec ff_libopus_decoder = {
234    .p.name         = "libopus",
235    .p.long_name    = NULL_IF_CONFIG_SMALL("libopus Opus"),
236    .p.type         = AVMEDIA_TYPE_AUDIO,
237    .p.id           = AV_CODEC_ID_OPUS,
238    .priv_data_size = sizeof(struct libopus_context),
239    .init           = libopus_decode_init,
240    .close          = libopus_decode_close,
241    FF_CODEC_DECODE_CB(libopus_decode),
242    .flush          = libopus_flush,
243    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
244    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
245    .p.sample_fmts  = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
246                                                     AV_SAMPLE_FMT_S16,
247                                                     AV_SAMPLE_FMT_NONE },
248    .p.priv_class   = &libopusdec_class,
249    .p.wrapper_name = "libopus",
250};
251