3 * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
6 * This file is part of FFmpeg.
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.
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.
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
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
32 #define ALT_BITSTREAM_READER_LE
37 #include "fmtconvert.h"
38 #include "libavutil/intfloat_readwrite.h"
40 extern const uint16_t ff_wma_critical_freqs[25];
42 static float quant_table[96];
44 #define MAX_CHANNELS 2
45 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
50 FmtConvertContext fmt_conv;
51 int version_b; ///< Bink version 'b'
54 int frame_len; ///< transform size (samples)
55 int overlap_len; ///< overlap size (samples)
60 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
61 DECLARE_ALIGNED(16, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
62 DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
63 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
64 float *prev_ptr[MAX_CHANNELS]; ///< pointers to the overlap points in the coeffs array
65 uint8_t *packet_buffer;
73 static av_cold int decode_init(AVCodecContext *avctx)
75 BinkAudioContext *s = avctx->priv_data;
76 int sample_rate = avctx->sample_rate;
81 dsputil_init(&s->dsp, avctx);
82 ff_fmt_convert_init(&s->fmt_conv, avctx);
84 /* determine frame length */
85 if (avctx->sample_rate < 22050) {
87 } else if (avctx->sample_rate < 44100) {
93 if (avctx->channels > MAX_CHANNELS) {
94 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
98 s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
100 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
101 // audio is already interleaved for the RDFT format variant
102 sample_rate *= avctx->channels;
105 frame_len_bits += av_log2(avctx->channels);
107 s->channels = avctx->channels;
110 s->frame_len = 1 << frame_len_bits;
111 s->overlap_len = s->frame_len / 16;
112 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
113 sample_rate_half = (sample_rate + 1) / 2;
114 s->root = 2.0 / sqrt(s->frame_len);
115 for (i = 0; i < 96; i++) {
116 /* constant is result of 0.066399999/log10(M_E) */
117 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
120 /* calculate number of bands */
121 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
122 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
125 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
127 return AVERROR(ENOMEM);
129 /* populate bands data */
131 for (i = 1; i < s->num_bands; i++)
132 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
133 s->bands[s->num_bands] = s->frame_len;
136 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
138 for (i = 0; i < s->channels; i++) {
139 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
140 s->prev_ptr[i] = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
143 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
144 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
145 else if (CONFIG_BINKAUDIO_DCT_DECODER)
146 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
153 static float get_float(GetBitContext *gb)
155 int power = get_bits(gb, 5);
156 float f = ldexpf(get_bits_long(gb, 23), power - 23);
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
166 #define GET_BITS_SAFE(out, nbits) do { \
167 if (get_bits_left(gb) < nbits) \
168 return AVERROR_INVALIDDATA; \
169 out = get_bits(gb, nbits); \
173 * Decode Bink Audio block
174 * @param[out] out Output buffer (must contain s->block_size elements)
175 * @return 0 on success, negative error code on failure
177 static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
182 GetBitContext *gb = &s->gb;
187 for (ch = 0; ch < s->channels; ch++) {
188 FFTSample *coeffs = s->coeffs_ptr[ch];
190 if (get_bits_left(gb) < 64)
191 return AVERROR_INVALIDDATA;
192 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
193 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
195 if (get_bits_left(gb) < 58)
196 return AVERROR_INVALIDDATA;
197 coeffs[0] = get_float(gb) * s->root;
198 coeffs[1] = get_float(gb) * s->root;
201 if (get_bits_left(gb) < s->num_bands * 8)
202 return AVERROR_INVALIDDATA;
203 for (i = 0; i < s->num_bands; i++) {
204 int value = get_bits(gb, 8);
205 quant[i] = quant_table[FFMIN(value, 95)];
211 // parse coefficients
213 while (i < s->frame_len) {
221 j = i + rle_length_tab[v] * 8;
227 j = FFMIN(j, s->frame_len);
229 GET_BITS_SAFE(width, 4);
231 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
233 while (s->bands[k] < i)
237 if (s->bands[k] == i)
239 GET_BITS_SAFE(coeff, width);
244 coeffs[i] = -q * coeff;
246 coeffs[i] = q * coeff;
255 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
257 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
258 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
260 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
261 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
264 s->fmt_conv.float_to_int16_interleave(s->current,
265 (const float **)s->prev_ptr,
266 s->overlap_len, s->channels);
267 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
268 s->frame_len - s->overlap_len,
272 int count = s->overlap_len * s->channels;
273 int shift = av_log2(count);
274 for (i = 0; i < count; i++) {
275 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
279 memcpy(s->previous, s->current,
280 s->overlap_len * s->channels * sizeof(*s->previous));
287 static av_cold int decode_end(AVCodecContext *avctx)
289 BinkAudioContext * s = avctx->priv_data;
291 av_freep(&s->packet_buffer);
292 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
293 ff_rdft_end(&s->trans.rdft);
294 else if (CONFIG_BINKAUDIO_DCT_DECODER)
295 ff_dct_end(&s->trans.dct);
299 static void get_bits_align32(GetBitContext *s)
301 int n = (-get_bits_count(s)) & 31;
302 if (n) skip_bits(s, n);
305 static int decode_frame(AVCodecContext *avctx,
306 void *data, int *data_size,
309 BinkAudioContext *s = avctx->priv_data;
310 int16_t *samples = data;
311 GetBitContext *gb = &s->gb;
312 int out_size, consumed = 0;
314 if (!get_bits_left(gb)) {
316 /* handle end-of-stream */
321 if (avpkt->size < 4) {
322 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
323 return AVERROR_INVALIDDATA;
325 buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
327 return AVERROR(ENOMEM);
328 s->packet_buffer = buf;
329 memcpy(s->packet_buffer, avpkt->data, avpkt->size);
330 init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
331 consumed = avpkt->size;
333 /* skip reported size */
334 skip_bits_long(gb, 32);
337 out_size = s->block_size * av_get_bytes_per_sample(avctx->sample_fmt);
338 if (*data_size < out_size) {
339 av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
340 return AVERROR(EINVAL);
343 if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT)) {
344 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
345 return AVERROR_INVALIDDATA;
347 get_bits_align32(gb);
349 *data_size = out_size;
353 AVCodec ff_binkaudio_rdft_decoder = {
354 .name = "binkaudio_rdft",
355 .type = AVMEDIA_TYPE_AUDIO,
356 .id = CODEC_ID_BINKAUDIO_RDFT,
357 .priv_data_size = sizeof(BinkAudioContext),
360 .decode = decode_frame,
361 .capabilities = CODEC_CAP_DELAY,
362 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
365 AVCodec ff_binkaudio_dct_decoder = {
366 .name = "binkaudio_dct",
367 .type = AVMEDIA_TYPE_AUDIO,
368 .id = CODEC_ID_BINKAUDIO_DCT,
369 .priv_data_size = sizeof(BinkAudioContext),
372 .decode = decode_frame,
373 .capabilities = CODEC_CAP_DELAY,
374 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")