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 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)
50 int version_b; ///< Bink version 'b'
53 int frame_len; ///< transform size (samples)
54 int overlap_len; ///< overlap size (samples)
59 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
60 float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
61 uint8_t *packet_buffer;
69 static av_cold int decode_init(AVCodecContext *avctx)
71 BinkAudioContext *s = avctx->priv_data;
72 int sample_rate = avctx->sample_rate;
77 /* determine frame length */
78 if (avctx->sample_rate < 22050) {
80 } else if (avctx->sample_rate < 44100) {
86 if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
87 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
88 return AVERROR_INVALIDDATA;
91 s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
93 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
94 // audio is already interleaved for the RDFT format variant
95 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
96 sample_rate *= avctx->channels;
99 frame_len_bits += av_log2(avctx->channels);
101 s->channels = avctx->channels;
102 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
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 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
110 s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
112 s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
113 for (i = 0; i < 96; i++) {
114 /* constant is result of 0.066399999/log10(M_E) */
115 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
118 /* calculate number of bands */
119 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
120 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
123 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
125 return AVERROR(ENOMEM);
127 /* populate bands data */
129 for (i = 1; i < s->num_bands; i++)
130 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
131 s->bands[s->num_bands] = s->frame_len;
135 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
136 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
137 else if (CONFIG_BINKAUDIO_DCT_DECODER)
138 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
142 avcodec_get_frame_defaults(&s->frame);
143 avctx->coded_frame = &s->frame;
148 static float get_float(GetBitContext *gb)
150 int power = get_bits(gb, 5);
151 float f = ldexpf(get_bits_long(gb, 23), power - 23);
157 static const uint8_t rle_length_tab[16] = {
158 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
162 * Decode Bink Audio block
163 * @param[out] out Output buffer (must contain s->block_size elements)
164 * @return 0 on success, negative error code on failure
166 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
171 GetBitContext *gb = &s->gb;
176 for (ch = 0; ch < s->channels; ch++) {
177 FFTSample *coeffs = out[ch];
180 if (get_bits_left(gb) < 64)
181 return AVERROR_INVALIDDATA;
182 coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
183 coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
185 if (get_bits_left(gb) < 58)
186 return AVERROR_INVALIDDATA;
187 coeffs[0] = get_float(gb) * s->root;
188 coeffs[1] = get_float(gb) * s->root;
191 if (get_bits_left(gb) < s->num_bands * 8)
192 return AVERROR_INVALIDDATA;
193 for (i = 0; i < s->num_bands; i++) {
194 int value = get_bits(gb, 8);
195 quant[i] = quant_table[FFMIN(value, 95)];
201 // parse coefficients
203 while (i < s->frame_len) {
207 int v = get_bits1(gb);
210 j = i + rle_length_tab[v] * 8;
216 j = FFMIN(j, s->frame_len);
218 width = get_bits(gb, 4);
220 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
222 while (s->bands[k] < i)
226 if (s->bands[k] == i)
228 coeff = get_bits(gb, width);
233 coeffs[i] = -q * coeff;
235 coeffs[i] = q * coeff;
244 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
246 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
248 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
249 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
252 for (ch = 0; ch < s->channels; ch++) {
254 int count = s->overlap_len * s->channels;
257 for (i = 0; i < s->overlap_len; i++, j += s->channels)
258 out[ch][i] = (s->previous[ch][i] * (count - j) +
259 out[ch][i] * j) / count;
261 memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
262 s->overlap_len * sizeof(*s->previous[ch]));
270 static av_cold int decode_end(AVCodecContext *avctx)
272 BinkAudioContext * s = avctx->priv_data;
274 av_freep(&s->packet_buffer);
275 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
276 ff_rdft_end(&s->trans.rdft);
277 else if (CONFIG_BINKAUDIO_DCT_DECODER)
278 ff_dct_end(&s->trans.dct);
283 static void get_bits_align32(GetBitContext *s)
285 int n = (-get_bits_count(s)) & 31;
286 if (n) skip_bits(s, n);
289 static int decode_frame(AVCodecContext *avctx, void *data,
290 int *got_frame_ptr, AVPacket *avpkt)
292 BinkAudioContext *s = avctx->priv_data;
293 GetBitContext *gb = &s->gb;
294 int ret, consumed = 0;
296 if (!get_bits_left(gb)) {
298 /* handle end-of-stream */
303 if (avpkt->size < 4) {
304 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
305 return AVERROR_INVALIDDATA;
307 buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
309 return AVERROR(ENOMEM);
310 s->packet_buffer = buf;
311 memcpy(s->packet_buffer, avpkt->data, avpkt->size);
312 init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
313 consumed = avpkt->size;
315 /* skip reported size */
316 skip_bits_long(gb, 32);
319 /* get output buffer */
320 s->frame.nb_samples = s->frame_len;
321 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
322 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
326 if (decode_block(s, (float **)s->frame.extended_data,
327 avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
328 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
329 return AVERROR_INVALIDDATA;
331 get_bits_align32(gb);
333 s->frame.nb_samples = s->block_size / avctx->channels;
335 *(AVFrame *)data = s->frame;
340 AVCodec ff_binkaudio_rdft_decoder = {
341 .name = "binkaudio_rdft",
342 .type = AVMEDIA_TYPE_AUDIO,
343 .id = AV_CODEC_ID_BINKAUDIO_RDFT,
344 .priv_data_size = sizeof(BinkAudioContext),
347 .decode = decode_frame,
348 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
349 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
352 AVCodec ff_binkaudio_dct_decoder = {
353 .name = "binkaudio_dct",
354 .type = AVMEDIA_TYPE_AUDIO,
355 .id = AV_CODEC_ID_BINKAUDIO_DCT,
356 .priv_data_size = sizeof(BinkAudioContext),
359 .decode = decode_frame,
360 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
361 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")