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
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/intfloat.h"
33 #include "libavutil/mem_internal.h"
35 #define BITSTREAM_READER_LE
42 #include "wma_freqs.h"
44 #define MAX_CHANNELS 2
45 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
47 typedef struct BinkAudioContext {
49 int version_b; ///< Bink version 'b'
52 int frame_len; ///< transform size (samples)
53 int overlap_len; ///< overlap size (samples)
57 unsigned int bands[26];
58 float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
59 float quant_table[96];
68 static av_cold int decode_init(AVCodecContext *avctx)
70 BinkAudioContext *s = avctx->priv_data;
71 int sample_rate = avctx->sample_rate;
76 /* determine frame length */
77 if (avctx->sample_rate < 22050) {
79 } else if (avctx->sample_rate < 44100) {
85 if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
86 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
87 return AVERROR_INVALIDDATA;
89 avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
92 s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
94 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
95 // audio is already interleaved for the RDFT format variant
96 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
97 if (sample_rate > INT_MAX / avctx->channels)
98 return AVERROR_INVALIDDATA;
99 sample_rate *= avctx->channels;
102 frame_len_bits += av_log2(avctx->channels);
104 s->channels = avctx->channels;
105 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
108 s->frame_len = 1 << frame_len_bits;
109 s->overlap_len = s->frame_len / 16;
110 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
111 sample_rate_half = (sample_rate + 1LL) / 2;
112 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
113 s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
115 s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
116 for (i = 0; i < 96; i++) {
117 /* constant is result of 0.066399999/log10(M_E) */
118 s->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 /* populate bands data */
128 for (i = 1; i < s->num_bands; i++)
129 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
130 s->bands[s->num_bands] = s->frame_len;
134 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
135 ret = ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
136 else if (CONFIG_BINKAUDIO_DCT_DECODER)
137 ret = ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
143 s->pkt = av_packet_alloc();
145 return AVERROR(ENOMEM);
150 static float get_float(GetBitContext *gb)
152 int power = get_bits(gb, 5);
153 float f = ldexpf(get_bits(gb, 23), power - 23);
159 static const uint8_t rle_length_tab[16] = {
160 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
164 * Decode Bink Audio block
165 * @param[out] out Output buffer (must contain s->block_size elements)
166 * @return 0 on success, negative error code on failure
168 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
173 GetBitContext *gb = &s->gb;
178 for (ch = 0; ch < s->channels; ch++) {
179 FFTSample *coeffs = out[ch];
182 if (get_bits_left(gb) < 64)
183 return AVERROR_INVALIDDATA;
184 coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
185 coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
187 if (get_bits_left(gb) < 58)
188 return AVERROR_INVALIDDATA;
189 coeffs[0] = get_float(gb) * s->root;
190 coeffs[1] = get_float(gb) * s->root;
193 if (get_bits_left(gb) < s->num_bands * 8)
194 return AVERROR_INVALIDDATA;
195 for (i = 0; i < s->num_bands; i++) {
196 int value = get_bits(gb, 8);
197 quant[i] = s->quant_table[FFMIN(value, 95)];
203 // parse coefficients
205 while (i < s->frame_len) {
209 int v = get_bits1(gb);
212 j = i + rle_length_tab[v] * 8;
218 j = FFMIN(j, s->frame_len);
220 width = get_bits(gb, 4);
222 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
224 while (s->bands[k] < i)
228 if (s->bands[k] == i)
230 coeff = get_bits(gb, width);
235 coeffs[i] = -q * coeff;
237 coeffs[i] = q * coeff;
246 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
248 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
250 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
251 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
254 for (ch = 0; ch < s->channels; ch++) {
256 int count = s->overlap_len * s->channels;
259 for (i = 0; i < s->overlap_len; i++, j += s->channels)
260 out[ch][i] = (s->previous[ch][i] * (count - j) +
261 out[ch][i] * j) / count;
263 memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
264 s->overlap_len * sizeof(*s->previous[ch]));
272 static av_cold int decode_end(AVCodecContext *avctx)
274 BinkAudioContext * s = avctx->priv_data;
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);
280 av_packet_free(&s->pkt);
285 static void get_bits_align32(GetBitContext *s)
287 int n = (-get_bits_count(s)) & 31;
288 if (n) skip_bits(s, n);
291 static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
293 BinkAudioContext *s = avctx->priv_data;
294 GetBitContext *gb = &s->gb;
298 ret = ff_decode_get_packet(avctx, s->pkt);
302 if (s->pkt->size < 4) {
303 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
304 ret = AVERROR_INVALIDDATA;
308 ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
312 /* skip reported size */
313 skip_bits_long(gb, 32);
316 /* get output buffer */
317 frame->nb_samples = s->frame_len;
318 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
321 if (decode_block(s, (float **)frame->extended_data,
322 avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
323 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
324 return AVERROR_INVALIDDATA;
326 get_bits_align32(gb);
327 if (!get_bits_left(gb)) {
328 memset(gb, 0, sizeof(*gb));
329 av_packet_unref(s->pkt);
332 frame->nb_samples = s->block_size / avctx->channels;
336 av_packet_unref(s->pkt);
340 const AVCodec ff_binkaudio_rdft_decoder = {
341 .name = "binkaudio_rdft",
342 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
343 .type = AVMEDIA_TYPE_AUDIO,
344 .id = AV_CODEC_ID_BINKAUDIO_RDFT,
345 .priv_data_size = sizeof(BinkAudioContext),
348 .receive_frame = binkaudio_receive_frame,
349 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
350 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
353 const AVCodec ff_binkaudio_dct_decoder = {
354 .name = "binkaudio_dct",
355 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
356 .type = AVMEDIA_TYPE_AUDIO,
357 .id = AV_CODEC_ID_BINKAUDIO_DCT,
358 .priv_data_size = sizeof(BinkAudioContext),
361 .receive_frame = binkaudio_receive_frame,
362 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
363 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,