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 #define MAX_CHANNELS 2
43 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
48 FmtConvertContext fmt_conv;
49 int version_b; ///< Bink version 'b'
52 int frame_len; ///< transform size (samples)
53 int overlap_len; ///< overlap size (samples)
58 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
59 DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
60 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
68 static av_cold int decode_init(AVCodecContext *avctx)
70 BinkAudioContext *s = avctx->priv_data;
71 int sample_rate = avctx->sample_rate;
76 dsputil_init(&s->dsp, avctx);
77 ff_fmt_convert_init(&s->fmt_conv, avctx);
79 /* determine frame length */
80 if (avctx->sample_rate < 22050) {
82 } else if (avctx->sample_rate < 44100) {
88 if (avctx->channels > MAX_CHANNELS) {
89 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
93 s->version_b = avctx->codec_tag == MKTAG('B','I','K','b');
95 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
96 // audio is already interleaved for the RDFT format variant
97 sample_rate *= avctx->channels;
100 frame_len_bits += av_log2(avctx->channels);
102 s->channels = avctx->channels;
105 s->frame_len = 1 << frame_len_bits;
106 s->overlap_len = s->frame_len / 16;
107 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
108 sample_rate_half = (sample_rate + 1) / 2;
109 s->root = 2.0 / sqrt(s->frame_len);
111 /* calculate number of bands */
112 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
113 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
116 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
118 return AVERROR(ENOMEM);
120 /* populate bands data */
122 for (i = 1; i < s->num_bands; i++)
123 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
124 s->bands[s->num_bands] = s->frame_len;
127 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
129 for (i = 0; i < s->channels; i++)
130 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
132 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
133 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
134 else if (CONFIG_BINKAUDIO_DCT_DECODER)
135 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
142 static float get_float(GetBitContext *gb)
144 int power = get_bits(gb, 5);
145 float f = ldexpf(get_bits_long(gb, 23), power - 23);
151 static const uint8_t rle_length_tab[16] = {
152 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
156 * Decode Bink Audio block
157 * @param[out] out Output buffer (must contain s->block_size elements)
159 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
164 GetBitContext *gb = &s->gb;
169 for (ch = 0; ch < s->channels; ch++) {
170 FFTSample *coeffs = s->coeffs_ptr[ch];
172 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
173 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
175 coeffs[0] = get_float(gb) * s->root;
176 coeffs[1] = get_float(gb) * s->root;
179 for (i = 0; i < s->num_bands; i++) {
180 /* constant is result of 0.066399999/log10(M_E) */
181 int value = get_bits(gb, 8);
182 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
188 // parse coefficients
190 while (i < s->frame_len) {
193 } else if (get_bits1(gb)) {
194 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
199 j = FFMIN(j, s->frame_len);
201 width = get_bits(gb, 4);
203 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
205 while (s->bands[k] < i)
209 if (s->bands[k] == i)
211 coeff = get_bits(gb, width);
214 coeffs[i] = -q * coeff;
216 coeffs[i] = q * coeff;
225 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
227 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
228 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
230 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
231 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
234 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
235 s->frame_len, s->channels);
238 int count = s->overlap_len * s->channels;
239 int shift = av_log2(count);
240 for (i = 0; i < count; i++) {
241 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
245 memcpy(s->previous, out + s->block_size,
246 s->overlap_len * s->channels * sizeof(*out));
251 static av_cold int decode_end(AVCodecContext *avctx)
253 BinkAudioContext * s = avctx->priv_data;
255 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
256 ff_rdft_end(&s->trans.rdft);
257 else if (CONFIG_BINKAUDIO_DCT_DECODER)
258 ff_dct_end(&s->trans.dct);
262 static void get_bits_align32(GetBitContext *s)
264 int n = (-get_bits_count(s)) & 31;
265 if (n) skip_bits(s, n);
268 static int decode_frame(AVCodecContext *avctx,
269 void *data, int *data_size,
272 BinkAudioContext *s = avctx->priv_data;
273 const uint8_t *buf = avpkt->data;
274 int buf_size = avpkt->size;
275 short *samples = data;
276 short *samples_end = (short*)((uint8_t*)data + *data_size);
278 GetBitContext *gb = &s->gb;
280 init_get_bits(gb, buf, buf_size * 8);
282 reported_size = get_bits_long(gb, 32);
283 while (get_bits_count(gb) / 8 < buf_size &&
284 samples + s->block_size <= samples_end) {
285 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
286 samples += s->block_size;
287 get_bits_align32(gb);
290 *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
294 AVCodec ff_binkaudio_rdft_decoder = {
297 CODEC_ID_BINKAUDIO_RDFT,
298 sizeof(BinkAudioContext),
303 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
306 AVCodec ff_binkaudio_dct_decoder = {
309 CODEC_ID_BINKAUDIO_DCT,
310 sizeof(BinkAudioContext),
315 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")