]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopusdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / 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/common.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/intreadwrite.h"
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "vorbis.h"
31 #include "mathops.h"
32
33 struct libopus_context {
34     OpusMSDecoder *dec;
35     AVFrame frame;
36     int pre_skip;
37 #ifndef OPUS_SET_GAIN
38     union { int i; double d; } gain;
39 #endif
40 };
41
42 static int opus_error_to_averror(int err)
43 {
44     switch (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);
53     }
54 }
55
56 #define OPUS_HEAD_SIZE 19
57
58 static av_cold int libopus_decode_init(AVCodecContext *avc)
59 {
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;
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     avc->channel_layout = avc->channels > 8 ? 0 :
68                           ff_vorbis_channel_layouts[avc->channels - 1];
69
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);
74     }
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;
81     } else {
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);
86         }
87         nb_streams = 1;
88         nb_coupled = avc->channels > 1;
89         mapping    = mapping_arr;
90     }
91
92     if (avc->channels > 2 && avc->channels <= 8) {
93         const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
94         int ch;
95
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;
100     }
101
102     opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
103                                                 nb_streams, nb_coupled,
104                                                 mapping, &ret);
105     if (!opus->dec) {
106         av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
107                opus_strerror(ret));
108         return opus_error_to_averror(ret);
109     }
110
111 #ifdef OPUS_SET_GAIN
112     ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
113     if (ret != OPUS_OK)
114         av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
115                opus_strerror(ret));
116 #else
117     {
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;
121         else
122             opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
123     }
124 #endif
125
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;
130     return 0;
131 }
132
133 static av_cold int libopus_decode_close(AVCodecContext *avc)
134 {
135     struct libopus_context *opus = avc->priv_data;
136
137     opus_multistream_decoder_destroy(opus->dec);
138     return 0;
139 }
140
141 #define MAX_FRAME_SIZE (960 * 6)
142
143 static int libopus_decode(AVCodecContext *avc, void *frame,
144                           int *got_frame_ptr, AVPacket *pkt)
145 {
146     struct libopus_context *opus = avc->priv_data;
147     int ret, nb_samples;
148
149     opus->frame.nb_samples = MAX_FRAME_SIZE;
150     ret = avc->get_buffer(avc, &opus->frame);
151     if (ret < 0) {
152         av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
153         return ret;
154     }
155
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);
160     else
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);
164
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);
169     }
170
171 #ifndef OPUS_SET_GAIN
172     {
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);
178         } else {
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);
182         }
183     }
184 #endif
185
186     opus->frame.nb_samples = nb_samples;
187     *(AVFrame *)frame = opus->frame;
188     *got_frame_ptr = 1;
189     return pkt->size;
190 }
191
192 static void libopus_flush(AVCodecContext *avc)
193 {
194     struct libopus_context *opus = avc->priv_data;
195
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;
200 }
201
202 AVCodec ff_libopus_decoder = {
203     .name           = "libopus",
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,
214                                                      AV_SAMPLE_FMT_S16,
215                                                      AV_SAMPLE_FMT_NONE },
216 };