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 Libav.
8 * Libav 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 * Libav 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 Libav; 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 BITSTREAM_READER_LE
37 #include "fmtconvert.h"
38 #include "libavutil/intfloat.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)
51 FmtConvertContext fmt_conv;
52 int version_b; ///< Bink version 'b'
55 int frame_len; ///< transform size (samples)
56 int overlap_len; ///< overlap size (samples)
61 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
62 DECLARE_ALIGNED(16, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
63 DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
64 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
65 float *prev_ptr[MAX_CHANNELS]; ///< pointers to the overlap points in the coeffs array
66 uint8_t *packet_buffer;
74 static av_cold int decode_init(AVCodecContext *avctx)
76 BinkAudioContext *s = avctx->priv_data;
77 int sample_rate = avctx->sample_rate;
82 ff_dsputil_init(&s->dsp, avctx);
83 ff_fmt_convert_init(&s->fmt_conv, avctx);
85 /* determine frame length */
86 if (avctx->sample_rate < 22050) {
88 } else if (avctx->sample_rate < 44100) {
94 if (avctx->channels > MAX_CHANNELS) {
95 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
99 s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
101 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
102 // audio is already interleaved for the RDFT format variant
103 sample_rate *= avctx->channels;
106 frame_len_bits += av_log2(avctx->channels);
108 s->channels = avctx->channels;
111 s->frame_len = 1 << frame_len_bits;
112 s->overlap_len = s->frame_len / 16;
113 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
114 sample_rate_half = (sample_rate + 1) / 2;
115 s->root = 2.0 / sqrt(s->frame_len);
116 for (i = 0; i < 96; i++) {
117 /* constant is result of 0.066399999/log10(M_E) */
118 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
121 /* calculate number of bands */
122 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
123 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
126 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
128 return AVERROR(ENOMEM);
130 /* populate bands data */
132 for (i = 1; i < s->num_bands; i++)
133 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
134 s->bands[s->num_bands] = s->frame_len;
137 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
139 for (i = 0; i < s->channels; i++) {
140 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
141 s->prev_ptr[i] = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
144 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
145 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
146 else if (CONFIG_BINKAUDIO_DCT_DECODER)
147 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
151 avcodec_get_frame_defaults(&s->frame);
152 avctx->coded_frame = &s->frame;
157 static float get_float(GetBitContext *gb)
159 int power = get_bits(gb, 5);
160 float f = ldexpf(get_bits_long(gb, 23), power - 23);
166 static const uint8_t rle_length_tab[16] = {
167 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
170 #define GET_BITS_SAFE(out, nbits) do { \
171 if (get_bits_left(gb) < nbits) \
172 return AVERROR_INVALIDDATA; \
173 out = get_bits(gb, nbits); \
177 * Decode Bink Audio block
178 * @param[out] out Output buffer (must contain s->block_size elements)
179 * @return 0 on success, negative error code on failure
181 static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
186 GetBitContext *gb = &s->gb;
191 for (ch = 0; ch < s->channels; ch++) {
192 FFTSample *coeffs = s->coeffs_ptr[ch];
194 if (get_bits_left(gb) < 64)
195 return AVERROR_INVALIDDATA;
196 coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
197 coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
199 if (get_bits_left(gb) < 58)
200 return AVERROR_INVALIDDATA;
201 coeffs[0] = get_float(gb) * s->root;
202 coeffs[1] = get_float(gb) * s->root;
205 if (get_bits_left(gb) < s->num_bands * 8)
206 return AVERROR_INVALIDDATA;
207 for (i = 0; i < s->num_bands; i++) {
208 int value = get_bits(gb, 8);
209 quant[i] = quant_table[FFMIN(value, 95)];
215 // parse coefficients
217 while (i < s->frame_len) {
225 j = i + rle_length_tab[v] * 8;
231 j = FFMIN(j, s->frame_len);
233 GET_BITS_SAFE(width, 4);
235 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
237 while (s->bands[k] < i)
241 if (s->bands[k] == i)
243 GET_BITS_SAFE(coeff, width);
248 coeffs[i] = -q * coeff;
250 coeffs[i] = q * coeff;
259 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
261 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
262 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
264 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
265 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
268 s->fmt_conv.float_to_int16_interleave(s->current,
269 (const float **)s->prev_ptr,
270 s->overlap_len, s->channels);
271 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
272 s->frame_len - s->overlap_len,
276 int count = s->overlap_len * s->channels;
277 int shift = av_log2(count);
278 for (i = 0; i < count; i++) {
279 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
283 memcpy(s->previous, s->current,
284 s->overlap_len * s->channels * sizeof(*s->previous));
291 static av_cold int decode_end(AVCodecContext *avctx)
293 BinkAudioContext * s = avctx->priv_data;
295 av_freep(&s->packet_buffer);
296 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
297 ff_rdft_end(&s->trans.rdft);
298 else if (CONFIG_BINKAUDIO_DCT_DECODER)
299 ff_dct_end(&s->trans.dct);
304 static void get_bits_align32(GetBitContext *s)
306 int n = (-get_bits_count(s)) & 31;
307 if (n) skip_bits(s, n);
310 static int decode_frame(AVCodecContext *avctx, void *data,
311 int *got_frame_ptr, AVPacket *avpkt)
313 BinkAudioContext *s = avctx->priv_data;
315 GetBitContext *gb = &s->gb;
316 int ret, consumed = 0;
318 if (!get_bits_left(gb)) {
320 /* handle end-of-stream */
325 if (avpkt->size < 4) {
326 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
327 return AVERROR_INVALIDDATA;
329 buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
331 return AVERROR(ENOMEM);
332 s->packet_buffer = buf;
333 memcpy(s->packet_buffer, avpkt->data, avpkt->size);
334 init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
335 consumed = avpkt->size;
337 /* skip reported size */
338 skip_bits_long(gb, 32);
341 /* get output buffer */
342 s->frame.nb_samples = s->block_size / avctx->channels;
343 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
344 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
347 samples = (int16_t *)s->frame.data[0];
349 if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT)) {
350 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
351 return AVERROR_INVALIDDATA;
353 get_bits_align32(gb);
356 *(AVFrame *)data = s->frame;
361 AVCodec ff_binkaudio_rdft_decoder = {
362 .name = "binkaudio_rdft",
363 .type = AVMEDIA_TYPE_AUDIO,
364 .id = CODEC_ID_BINKAUDIO_RDFT,
365 .priv_data_size = sizeof(BinkAudioContext),
368 .decode = decode_frame,
369 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
370 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
373 AVCodec ff_binkaudio_dct_decoder = {
374 .name = "binkaudio_dct",
375 .type = AVMEDIA_TYPE_AUDIO,
376 .id = CODEC_ID_BINKAUDIO_DCT,
377 .priv_data_size = sizeof(BinkAudioContext),
380 .decode = decode_frame,
381 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
382 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")