3 * Copyright (c) 2007-2010 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
36 #include "fmtconvert.h"
38 extern const uint16_t ff_wma_critical_freqs[25];
40 #define MAX_CHANNELS 2
41 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
44 AVCodecContext *avctx;
47 FmtConvertContext fmt_conv;
50 int frame_len; ///< transform size (samples)
51 int overlap_len; ///< overlap size (samples)
56 DECLARE_ALIGNED(16, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
57 DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
58 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
66 static av_cold int decode_init(AVCodecContext *avctx)
68 BinkAudioContext *s = avctx->priv_data;
69 int sample_rate = avctx->sample_rate;
75 dsputil_init(&s->dsp, avctx);
76 ff_fmt_convert_init(&s->fmt_conv, avctx);
78 /* determine frame length */
79 if (avctx->sample_rate < 22050) {
81 } else if (avctx->sample_rate < 44100) {
86 s->frame_len = 1 << frame_len_bits;
88 if (s->channels > MAX_CHANNELS) {
89 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
93 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
94 // audio is already interleaved for the RDFT format variant
95 sample_rate *= avctx->channels;
96 s->frame_len *= avctx->channels;
98 if (avctx->channels == 2)
101 s->channels = avctx->channels;
104 s->overlap_len = s->frame_len / 16;
105 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
106 sample_rate_half = (sample_rate + 1) / 2;
107 s->root = 2.0 / sqrt(s->frame_len);
109 /* calculate number of bands */
110 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
111 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
114 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
116 return AVERROR(ENOMEM);
118 /* populate bands data */
120 for (i = 1; i < s->num_bands; i++)
121 s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
122 s->bands[s->num_bands] = s->frame_len / 2;
125 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
127 for (i = 0; i < s->channels; i++)
128 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
130 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
131 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
132 else if (CONFIG_BINKAUDIO_DCT_DECODER)
133 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
140 static float get_float(GetBitContext *gb)
142 int power = get_bits(gb, 5);
143 float f = ldexpf(get_bits_long(gb, 23), power - 23);
149 static const uint8_t rle_length_tab[16] = {
150 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
154 * Decode Bink Audio block
155 * @param[out] out Output buffer (must contain s->block_size elements)
157 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
162 GetBitContext *gb = &s->gb;
167 for (ch = 0; ch < s->channels; ch++) {
168 FFTSample *coeffs = s->coeffs_ptr[ch];
170 coeffs[0] = get_float(gb) * s->root;
171 coeffs[1] = get_float(gb) * s->root;
173 for (i = 0; i < s->num_bands; i++) {
174 /* constant is result of 0.066399999/log10(M_E) */
175 int value = get_bits(gb, 8);
176 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
180 for (k = 0; s->bands[k] < 1; k++) {
184 // parse coefficients
186 while (i < s->frame_len) {
188 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
193 j = FFMIN(j, s->frame_len);
195 width = get_bits(gb, 4);
197 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
199 while (s->bands[k] * 2 < i)
203 if (s->bands[k] * 2 == i)
205 coeff = get_bits(gb, width);
208 coeffs[i] = -q * coeff;
210 coeffs[i] = q * coeff;
219 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
221 ff_dct_calc (&s->trans.dct, coeffs);
222 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
224 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
225 ff_rdft_calc(&s->trans.rdft, coeffs);
228 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
229 s->frame_len, s->channels);
232 int count = s->overlap_len * s->channels;
233 int shift = av_log2(count);
234 for (i = 0; i < count; i++) {
235 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
239 memcpy(s->previous, out + s->block_size,
240 s->overlap_len * s->channels * sizeof(*out));
245 static av_cold int decode_end(AVCodecContext *avctx)
247 BinkAudioContext * s = avctx->priv_data;
249 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
250 ff_rdft_end(&s->trans.rdft);
251 else if (CONFIG_BINKAUDIO_DCT_DECODER)
252 ff_dct_end(&s->trans.dct);
256 static void get_bits_align32(GetBitContext *s)
258 int n = (-get_bits_count(s)) & 31;
259 if (n) skip_bits(s, n);
262 static int decode_frame(AVCodecContext *avctx,
263 void *data, int *data_size,
266 BinkAudioContext *s = avctx->priv_data;
267 const uint8_t *buf = avpkt->data;
268 int buf_size = avpkt->size;
269 short *samples = data;
270 short *samples_end = (short*)((uint8_t*)data + *data_size);
272 GetBitContext *gb = &s->gb;
274 init_get_bits(gb, buf, buf_size * 8);
276 reported_size = get_bits_long(gb, 32);
277 while (get_bits_count(gb) / 8 < buf_size &&
278 samples + s->block_size <= samples_end) {
279 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
280 samples += s->block_size;
281 get_bits_align32(gb);
284 *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
288 AVCodec ff_binkaudio_rdft_decoder = {
291 CODEC_ID_BINKAUDIO_RDFT,
292 sizeof(BinkAudioContext),
297 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
300 AVCodec ff_binkaudio_dct_decoder = {
303 CODEC_ID_BINKAUDIO_DCT,
304 sizeof(BinkAudioContext),
309 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")