blob: e6ca61a78f3fc250f1e371f310241f2b6dc1095d
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 | |
28 | #include "avcodec.h" |
29 | #include "internal.h" |
30 | #include "vorbis.h" |
31 | #include "mathops.h" |
32 | #include "libopus.h" |
33 | |
34 | struct libopus_context { |
35 | OpusMSDecoder *dec; |
36 | int pre_skip; |
37 | #ifndef OPUS_SET_GAIN |
38 | union { int i; double d; } gain; |
39 | #endif |
40 | }; |
41 | |
42 | #define OPUS_HEAD_SIZE 19 |
43 | |
44 | static av_cold int libopus_decode_init(AVCodecContext *avc) |
45 | { |
46 | struct libopus_context *opus = avc->priv_data; |
47 | int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled; |
48 | uint8_t mapping_arr[8] = { 0, 1 }, *mapping; |
49 | |
50 | avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2; |
51 | if (avc->channels <= 0) { |
52 | av_log(avc, AV_LOG_WARNING, |
53 | "Invalid number of channels %d, defaulting to stereo\n", avc->channels); |
54 | avc->channels = 2; |
55 | } |
56 | |
57 | avc->sample_rate = 48000; |
58 | avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ? |
59 | AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16; |
60 | avc->channel_layout = avc->channels > 8 ? 0 : |
61 | ff_vorbis_channel_layouts[avc->channels - 1]; |
62 | |
63 | if (avc->extradata_size >= OPUS_HEAD_SIZE) { |
64 | opus->pre_skip = AV_RL16(avc->extradata + 10); |
65 | gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16); |
66 | channel_map = AV_RL8 (avc->extradata + 18); |
67 | } |
68 | if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) { |
69 | nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0]; |
70 | nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1]; |
71 | if (nb_streams + nb_coupled != avc->channels) |
72 | av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n"); |
73 | mapping = avc->extradata + OPUS_HEAD_SIZE + 2; |
74 | } else { |
75 | if (avc->channels > 2 || channel_map) { |
76 | av_log(avc, AV_LOG_ERROR, |
77 | "No channel mapping for %d channels.\n", avc->channels); |
78 | return AVERROR(EINVAL); |
79 | } |
80 | nb_streams = 1; |
81 | nb_coupled = avc->channels > 1; |
82 | mapping = mapping_arr; |
83 | } |
84 | |
85 | if (avc->channels > 2 && avc->channels <= 8) { |
86 | const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1]; |
87 | int ch; |
88 | |
89 | /* Remap channels from Vorbis order to ffmpeg order */ |
90 | for (ch = 0; ch < avc->channels; ch++) |
91 | mapping_arr[ch] = mapping[vorbis_offset[ch]]; |
92 | mapping = mapping_arr; |
93 | } |
94 | |
95 | opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels, |
96 | nb_streams, nb_coupled, |
97 | mapping, &ret); |
98 | if (!opus->dec) { |
99 | av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n", |
100 | opus_strerror(ret)); |
101 | return ff_opus_error_to_averror(ret); |
102 | } |
103 | |
104 | #ifdef OPUS_SET_GAIN |
105 | ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db)); |
106 | if (ret != OPUS_OK) |
107 | av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n", |
108 | opus_strerror(ret)); |
109 | #else |
110 | { |
111 | double gain_lin = ff_exp10(gain_db / (20.0 * 256)); |
112 | if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) |
113 | opus->gain.d = gain_lin; |
114 | else |
115 | opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX); |
116 | } |
117 | #endif |
118 | |
119 | /* Decoder delay (in samples) at 48kHz */ |
120 | avc->delay = avc->internal->skip_samples = opus->pre_skip; |
121 | |
122 | return 0; |
123 | } |
124 | |
125 | static av_cold int libopus_decode_close(AVCodecContext *avc) |
126 | { |
127 | struct libopus_context *opus = avc->priv_data; |
128 | |
129 | opus_multistream_decoder_destroy(opus->dec); |
130 | return 0; |
131 | } |
132 | |
133 | #define MAX_FRAME_SIZE (960 * 6) |
134 | |
135 | static int libopus_decode(AVCodecContext *avc, void *data, |
136 | int *got_frame_ptr, AVPacket *pkt) |
137 | { |
138 | struct libopus_context *opus = avc->priv_data; |
139 | AVFrame *frame = data; |
140 | int ret, nb_samples; |
141 | |
142 | frame->nb_samples = MAX_FRAME_SIZE; |
143 | if ((ret = ff_get_buffer(avc, frame, 0)) < 0) |
144 | return ret; |
145 | |
146 | if (avc->sample_fmt == AV_SAMPLE_FMT_S16) |
147 | nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size, |
148 | (opus_int16 *)frame->data[0], |
149 | frame->nb_samples, 0); |
150 | else |
151 | nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size, |
152 | (float *)frame->data[0], |
153 | frame->nb_samples, 0); |
154 | |
155 | if (nb_samples < 0) { |
156 | av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n", |
157 | opus_strerror(nb_samples)); |
158 | return ff_opus_error_to_averror(nb_samples); |
159 | } |
160 | |
161 | #ifndef OPUS_SET_GAIN |
162 | { |
163 | int i = avc->channels * nb_samples; |
164 | if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) { |
165 | float *pcm = (float *)frame->data[0]; |
166 | for (; i > 0; i--, pcm++) |
167 | *pcm = av_clipf(*pcm * opus->gain.d, -1, 1); |
168 | } else { |
169 | int16_t *pcm = (int16_t *)frame->data[0]; |
170 | for (; i > 0; i--, pcm++) |
171 | *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16); |
172 | } |
173 | } |
174 | #endif |
175 | |
176 | frame->nb_samples = nb_samples; |
177 | *got_frame_ptr = 1; |
178 | |
179 | return pkt->size; |
180 | } |
181 | |
182 | static void libopus_flush(AVCodecContext *avc) |
183 | { |
184 | struct libopus_context *opus = avc->priv_data; |
185 | |
186 | opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE); |
187 | /* The stream can have been extracted by a tool that is not Opus-aware. |
188 | Therefore, any packet can become the first of the stream. */ |
189 | avc->internal->skip_samples = opus->pre_skip; |
190 | } |
191 | |
192 | AVCodec ff_libopus_decoder = { |
193 | .name = "libopus", |
194 | .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"), |
195 | .type = AVMEDIA_TYPE_AUDIO, |
196 | .id = AV_CODEC_ID_OPUS, |
197 | .priv_data_size = sizeof(struct libopus_context), |
198 | .init = libopus_decode_init, |
199 | .close = libopus_decode_close, |
200 | .decode = libopus_decode, |
201 | .flush = libopus_flush, |
202 | .capabilities = AV_CODEC_CAP_DR1, |
203 | .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT, |
204 | AV_SAMPLE_FMT_S16, |
205 | AV_SAMPLE_FMT_NONE }, |
206 | }; |
207 |