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 if (avctx->extradata && avctx->extradata_size > 0)
94 s->version_b = avctx->extradata[0];
96 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
97 // audio is already interleaved for the RDFT format variant
98 sample_rate *= avctx->channels;
101 frame_len_bits += av_log2(avctx->channels);
103 s->channels = avctx->channels;
106 s->frame_len = 1 << frame_len_bits;
107 s->overlap_len = s->frame_len / 16;
108 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
109 sample_rate_half = (sample_rate + 1) / 2;
110 s->root = 2.0 / sqrt(s->frame_len);
112 /* calculate number of bands */
113 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
114 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
117 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
119 return AVERROR(ENOMEM);
121 /* populate bands data */
123 for (i = 1; i < s->num_bands; i++)
124 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
125 s->bands[s->num_bands] = s->frame_len;
128 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
130 for (i = 0; i < s->channels; i++)
131 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
133 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
134 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
135 else if (CONFIG_BINKAUDIO_DCT_DECODER)
136 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
143 static float get_float(GetBitContext *gb)
145 int power = get_bits(gb, 5);
146 float f = ldexpf(get_bits_long(gb, 23), power - 23);
152 static const uint8_t rle_length_tab[16] = {
153 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
157 * Decode Bink Audio block
158 * @param[out] out Output buffer (must contain s->block_size elements)
160 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
165 GetBitContext *gb = &s->gb;
170 for (ch = 0; ch < s->channels; ch++) {
171 FFTSample *coeffs = s->coeffs_ptr[ch];
173 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
174 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
176 coeffs[0] = get_float(gb) * s->root;
177 coeffs[1] = get_float(gb) * s->root;
180 for (i = 0; i < s->num_bands; i++) {
181 /* constant is result of 0.066399999/log10(M_E) */
182 int value = get_bits(gb, 8);
183 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
189 // parse coefficients
191 while (i < s->frame_len) {
194 } else if (get_bits1(gb)) {
195 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
200 j = FFMIN(j, s->frame_len);
202 width = get_bits(gb, 4);
204 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
206 while (s->bands[k] < i)
210 if (s->bands[k] == i)
212 coeff = get_bits(gb, width);
215 coeffs[i] = -q * coeff;
217 coeffs[i] = q * coeff;
226 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
228 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
229 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
231 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
232 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
235 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
236 s->frame_len, s->channels);
239 int count = s->overlap_len * s->channels;
240 int shift = av_log2(count);
241 for (i = 0; i < count; i++) {
242 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
246 memcpy(s->previous, out + s->block_size,
247 s->overlap_len * s->channels * sizeof(*out));
252 static av_cold int decode_end(AVCodecContext *avctx)
254 BinkAudioContext * s = avctx->priv_data;
256 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
257 ff_rdft_end(&s->trans.rdft);
258 else if (CONFIG_BINKAUDIO_DCT_DECODER)
259 ff_dct_end(&s->trans.dct);
263 static void get_bits_align32(GetBitContext *s)
265 int n = (-get_bits_count(s)) & 31;
266 if (n) skip_bits(s, n);
269 static int decode_frame(AVCodecContext *avctx,
270 void *data, int *data_size,
273 BinkAudioContext *s = avctx->priv_data;
274 const uint8_t *buf = avpkt->data;
275 int buf_size = avpkt->size;
276 short *samples = data;
277 short *samples_end = (short*)((uint8_t*)data + *data_size);
279 GetBitContext *gb = &s->gb;
281 init_get_bits(gb, buf, buf_size * 8);
283 reported_size = get_bits_long(gb, 32);
284 while (get_bits_count(gb) / 8 < buf_size &&
285 samples + s->block_size <= samples_end) {
286 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
287 samples += s->block_size;
288 get_bits_align32(gb);
291 *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
295 AVCodec ff_binkaudio_rdft_decoder = {
298 CODEC_ID_BINKAUDIO_RDFT,
299 sizeof(BinkAudioContext),
304 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
307 AVCodec ff_binkaudio_dct_decoder = {
310 CODEC_ID_BINKAUDIO_DCT,
311 sizeof(BinkAudioContext),
316 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")