]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopus_dec.c
Merge commit '0c00fd80ee4791bd70b634084307fc9f179e0412'
[ffmpeg] / libavcodec / libopus_dec.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 #include "avcodec.h"
25 #include "internal.h"
26 #include "vorbis.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/intreadwrite.h"
29
30 struct libopus_context {
31     OpusMSDecoder *dec;
32     AVFrame frame;
33     int pre_skip;
34 #ifndef OPUS_SET_GAIN
35     union { int i; double d; } gain;
36 #endif
37 };
38
39 static int ff_opus_error_to_averror(int err)
40 {
41     switch (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);
50     }
51 }
52
53 static inline void reorder(uint8_t *data, unsigned channels, unsigned bps,
54                            unsigned samples, const uint8_t *map)
55 {
56     uint8_t tmp[8 * 4];
57     unsigned i;
58
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;
65     }
66 }
67
68 #define OPUS_HEAD_SIZE 19
69
70 static av_cold int libopus_dec_init(AVCodecContext *avc)
71 {
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;
75
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];
81
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 */
87     }
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;
94     } else {
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);
99         }
100         nb_streams = 1;
101         nb_coupled = avc->channels > 1;
102         mapping = mapping_stereo;
103     }
104
105     opus->dec = opus_multistream_decoder_create(
106         avc->sample_rate, avc->channels,
107         nb_streams, nb_coupled, mapping, &ret);
108     if (!opus->dec) {
109         av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
110                opus_strerror(ret));
111         return ff_opus_error_to_averror(ret);
112     }
113
114 #ifdef OPUS_SET_GAIN
115     ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
116     if (ret != OPUS_OK)
117         av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
118                opus_strerror(ret));
119 #else
120     {
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;
124         else
125             opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
126     }
127 #endif
128
129     avc->internal->skip_samples = opus->pre_skip;
130     avcodec_get_frame_defaults(&opus->frame);
131     avc->coded_frame = &opus->frame;
132     return 0;
133 }
134
135 static av_cold int libopus_dec_close(AVCodecContext *avc)
136 {
137     struct libopus_context *opus = avc->priv_data;
138
139     opus_multistream_decoder_destroy(opus->dec);
140     return 0;
141 }
142
143 #define MAX_FRAME_SIZE (960*6)
144
145 static int libopus_dec_decode(AVCodecContext *avc, void *frame,
146                               int *got_frame_ptr, AVPacket *pkt)
147 {
148     struct libopus_context *opus = avc->priv_data;
149     int ret, nb_samples;
150
151     opus->frame.nb_samples = MAX_FRAME_SIZE;
152     ret = avc->get_buffer(avc, &opus->frame);
153     if (ret < 0) {
154         av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
155         return ret;
156     }
157
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);
169     }
170
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);
175         else
176             reorder(opus->frame.data[0], avc->channels, 4, nb_samples, m);
177     }
178
179 #ifndef OPUS_SET_GAIN
180     {
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);
186         } else {
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);
190         }
191     }
192 #endif
193
194     opus->frame.nb_samples = nb_samples;
195     *(AVFrame *)frame = opus->frame;
196     *got_frame_ptr = 1;
197     return pkt->size;
198 }
199
200 static void libopus_dec_flush(AVCodecContext *avc)
201 {
202     struct libopus_context *opus = avc->priv_data;
203
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;
208 }
209
210 AVCodec ff_libopus_decoder = {
211     .name           = "libopus",
212     .type           = AVMEDIA_TYPE_AUDIO,
213     .id             = AV_CODEC_ID_OPUS,
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"),
221 };