1 /*
2 * Bink Audio decoder
3 * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Bink Audio decoder
26 *
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
29 */
30
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/intfloat.h"
33
34 #define BITSTREAM_READER_LE
35 #include "avcodec.h"
36 #include "dct.h"
37 #include "decode.h"
38 #include "get_bits.h"
39 #include "internal.h"
40 #include "rdft.h"
41 #include "wma_freqs.h"
42
43 static float quant_table[96];
44
45 #define MAX_CHANNELS 2
46 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
47
48 typedef struct BinkAudioContext {
49 GetBitContext gb;
50 int version_b; ///< Bink version 'b'
51 int first;
52 int channels;
53 int frame_len; ///< transform size (samples)
54 int overlap_len; ///< overlap size (samples)
55 int block_size;
56 int num_bands;
57 unsigned int *bands;
58 float root;
59 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
60 float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
61 AVPacket *pkt;
62 union {
63 RDFTContext rdft;
64 DCTContext dct;
65 } trans;
66 } BinkAudioContext;
67
68
decode_init(AVCodecContext * avctx)69 static av_cold int decode_init(AVCodecContext *avctx)
70 {
71 BinkAudioContext *s = avctx->priv_data;
72 int sample_rate = avctx->sample_rate;
73 int sample_rate_half;
74 int i;
75 int frame_len_bits;
76
77 /* determine frame length */
78 if (avctx->sample_rate < 22050) {
79 frame_len_bits = 9;
80 } else if (avctx->sample_rate < 44100) {
81 frame_len_bits = 10;
82 } else {
83 frame_len_bits = 11;
84 }
85
86 if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
87 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
88 return AVERROR_INVALIDDATA;
89 }
90 avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
91 AV_CH_LAYOUT_STEREO;
92
93 s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
94
95 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
96 // audio is already interleaved for the RDFT format variant
97 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
98 if (sample_rate > INT_MAX / avctx->channels)
99 return AVERROR_INVALIDDATA;
100 sample_rate *= avctx->channels;
101 s->channels = 1;
102 if (!s->version_b)
103 frame_len_bits += av_log2(avctx->channels);
104 } else {
105 s->channels = avctx->channels;
106 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
107 }
108
109 s->frame_len = 1 << frame_len_bits;
110 s->overlap_len = s->frame_len / 16;
111 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
112 sample_rate_half = (sample_rate + 1LL) / 2;
113 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
114 s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
115 else
116 s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
117 for (i = 0; i < 96; i++) {
118 /* constant is result of 0.066399999/log10(M_E) */
119 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
120 }
121
122 /* calculate number of bands */
123 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
124 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
125 break;
126
127 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
128 if (!s->bands)
129 return AVERROR(ENOMEM);
130
131 /* populate bands data */
132 s->bands[0] = 2;
133 for (i = 1; i < s->num_bands; i++)
134 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
135 s->bands[s->num_bands] = s->frame_len;
136
137 s->first = 1;
138
139 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
140 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
141 else if (CONFIG_BINKAUDIO_DCT_DECODER)
142 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
143 else
144 av_assert0(0);
145
146 s->pkt = av_packet_alloc();
147 if (!s->pkt)
148 return AVERROR(ENOMEM);
149
150 return 0;
151 }
152
get_float(GetBitContext * gb)153 static float get_float(GetBitContext *gb)
154 {
155 int power = get_bits(gb, 5);
156 float f = ldexpf(get_bits(gb, 23), power - 23);
157 if (get_bits1(gb))
158 f = -f;
159 return f;
160 }
161
162 static const uint8_t rle_length_tab[16] = {
163 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
164 };
165
166 /**
167 * Decode Bink Audio block
168 * @param[out] out Output buffer (must contain s->block_size elements)
169 * @return 0 on success, negative error code on failure
170 */
decode_block(BinkAudioContext * s,float ** out,int use_dct)171 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
172 {
173 int ch, i, j, k;
174 float q, quant[25];
175 int width, coeff;
176 GetBitContext *gb = &s->gb;
177
178 if (use_dct)
179 skip_bits(gb, 2);
180
181 for (ch = 0; ch < s->channels; ch++) {
182 FFTSample *coeffs = out[ch];
183
184 if (s->version_b) {
185 if (get_bits_left(gb) < 64)
186 return AVERROR_INVALIDDATA;
187 coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
188 coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
189 } else {
190 if (get_bits_left(gb) < 58)
191 return AVERROR_INVALIDDATA;
192 coeffs[0] = get_float(gb) * s->root;
193 coeffs[1] = get_float(gb) * s->root;
194 }
195
196 if (get_bits_left(gb) < s->num_bands * 8)
197 return AVERROR_INVALIDDATA;
198 for (i = 0; i < s->num_bands; i++) {
199 int value = get_bits(gb, 8);
200 quant[i] = quant_table[FFMIN(value, 95)];
201 }
202
203 k = 0;
204 q = quant[0];
205
206 // parse coefficients
207 i = 2;
208 while (i < s->frame_len) {
209 if (s->version_b) {
210 j = i + 16;
211 } else {
212 int v = get_bits1(gb);
213 if (v) {
214 v = get_bits(gb, 4);
215 j = i + rle_length_tab[v] * 8;
216 } else {
217 j = i + 8;
218 }
219 }
220
221 j = FFMIN(j, s->frame_len);
222
223 width = get_bits(gb, 4);
224 if (width == 0) {
225 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
226 i = j;
227 while (s->bands[k] < i)
228 q = quant[k++];
229 } else {
230 while (i < j) {
231 if (s->bands[k] == i)
232 q = quant[k++];
233 coeff = get_bits(gb, width);
234 if (coeff) {
235 int v;
236 v = get_bits1(gb);
237 if (v)
238 coeffs[i] = -q * coeff;
239 else
240 coeffs[i] = q * coeff;
241 } else {
242 coeffs[i] = 0.0f;
243 }
244 i++;
245 }
246 }
247 }
248
249 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
250 coeffs[0] /= 0.5;
251 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
252 }
253 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
254 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
255 }
256
257 for (ch = 0; ch < s->channels; ch++) {
258 int j;
259 int count = s->overlap_len * s->channels;
260 if (!s->first) {
261 j = ch;
262 for (i = 0; i < s->overlap_len; i++, j += s->channels)
263 out[ch][i] = (s->previous[ch][i] * (count - j) +
264 out[ch][i] * j) / count;
265 }
266 memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
267 s->overlap_len * sizeof(*s->previous[ch]));
268 }
269
270 s->first = 0;
271
272 return 0;
273 }
274
decode_end(AVCodecContext * avctx)275 static av_cold int decode_end(AVCodecContext *avctx)
276 {
277 BinkAudioContext * s = avctx->priv_data;
278 av_freep(&s->bands);
279 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
280 ff_rdft_end(&s->trans.rdft);
281 else if (CONFIG_BINKAUDIO_DCT_DECODER)
282 ff_dct_end(&s->trans.dct);
283
284 av_packet_free(&s->pkt);
285
286 return 0;
287 }
288
get_bits_align32(GetBitContext * s)289 static void get_bits_align32(GetBitContext *s)
290 {
291 int n = (-get_bits_count(s)) & 31;
292 if (n) skip_bits(s, n);
293 }
294
binkaudio_receive_frame(AVCodecContext * avctx,AVFrame * frame)295 static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
296 {
297 BinkAudioContext *s = avctx->priv_data;
298 GetBitContext *gb = &s->gb;
299 int ret;
300
301 if (!s->pkt->data) {
302 ret = ff_decode_get_packet(avctx, s->pkt);
303 if (ret < 0)
304 return ret;
305
306 if (s->pkt->size < 4) {
307 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
308 ret = AVERROR_INVALIDDATA;
309 goto fail;
310 }
311
312 ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
313 if (ret < 0)
314 goto fail;
315
316 /* skip reported size */
317 skip_bits_long(gb, 32);
318 }
319
320 /* get output buffer */
321 frame->nb_samples = s->frame_len;
322 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
323 return ret;
324
325 if (decode_block(s, (float **)frame->extended_data,
326 avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
327 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
328 return AVERROR_INVALIDDATA;
329 }
330 get_bits_align32(gb);
331 if (!get_bits_left(gb)) {
332 memset(gb, 0, sizeof(*gb));
333 av_packet_unref(s->pkt);
334 }
335
336 frame->nb_samples = s->block_size / avctx->channels;
337
338 return 0;
339 fail:
340 av_packet_unref(s->pkt);
341 return ret;
342 }
343
344 AVCodec ff_binkaudio_rdft_decoder = {
345 .name = "binkaudio_rdft",
346 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
347 .type = AVMEDIA_TYPE_AUDIO,
348 .id = AV_CODEC_ID_BINKAUDIO_RDFT,
349 .priv_data_size = sizeof(BinkAudioContext),
350 .init = decode_init,
351 .close = decode_end,
352 .receive_frame = binkaudio_receive_frame,
353 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
354 };
355
356 AVCodec ff_binkaudio_dct_decoder = {
357 .name = "binkaudio_dct",
358 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
359 .type = AVMEDIA_TYPE_AUDIO,
360 .id = AV_CODEC_ID_BINKAUDIO_DCT,
361 .priv_data_size = sizeof(BinkAudioContext),
362 .init = decode_init,
363 .close = decode_end,
364 .receive_frame = binkaudio_receive_frame,
365 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
366 };
367