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