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
24 * @file libavcodec/binkaudio.c
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
32 #define ALT_BITSTREAM_READER_LE
35 extern const uint16_t ff_wma_critical_freqs[25];
37 #define MAX_CHANNELS 2
38 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
41 AVCodecContext *avctx;
46 int frame_len; ///< transform size (samples)
47 int overlap_len; ///< overlap size (samples)
52 DECLARE_ALIGNED_16(FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
53 DECLARE_ALIGNED_16(short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
54 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
62 static av_cold int decode_init(AVCodecContext *avctx)
64 BinkAudioContext *s = avctx->priv_data;
65 int sample_rate = avctx->sample_rate;
71 dsputil_init(&s->dsp, avctx);
73 /* determine frame length */
74 if (avctx->sample_rate < 22050) {
76 } else if (avctx->sample_rate < 44100) {
81 s->frame_len = 1 << frame_len_bits;
83 if (s->channels > MAX_CHANNELS) {
84 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
88 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
89 // audio is already interleaved for the RDFT format variant
90 sample_rate *= avctx->channels;
91 s->frame_len *= avctx->channels;
93 if (avctx->channels == 2)
96 s->channels = avctx->channels;
99 s->overlap_len = s->frame_len / 16;
100 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
101 sample_rate_half = (sample_rate + 1) / 2;
102 s->root = 2.0 / sqrt(s->frame_len);
104 /* calculate number of bands */
105 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
106 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
109 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
111 return AVERROR(ENOMEM);
113 /* populate bands data */
115 for (i = 1; i < s->num_bands; i++)
116 s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
117 s->bands[s->num_bands] = s->frame_len / 2;
120 avctx->sample_fmt = SAMPLE_FMT_S16;
122 for (i = 0; i < s->channels; i++)
123 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
125 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
126 ff_rdft_init(&s->trans.rdft, frame_len_bits, IRIDFT);
127 else if (CONFIG_BINKAUDIO_DCT_DECODER)
128 ff_dct_init(&s->trans.dct, frame_len_bits, 0);
135 static float get_float(GetBitContext *gb)
137 int power = get_bits(gb, 5);
138 float f = ldexpf(get_bits_long(gb, 23), power - 23);
144 static const uint8_t rle_length_tab[16] = {
145 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
149 * Decode Bink Audio block
150 * @param[out] out Output buffer (must contain s->block_size elements)
152 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
157 GetBitContext *gb = &s->gb;
162 for (ch = 0; ch < s->channels; ch++) {
163 FFTSample *coeffs = s->coeffs_ptr[ch];
165 coeffs[0] = get_float(gb) * s->root;
166 coeffs[1] = get_float(gb) * s->root;
168 for (i = 0; i < s->num_bands; i++) {
169 /* constant is result of 0.066399999/log10(M_E) */
170 int value = get_bits(gb, 8);
171 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
175 for (k = 0; s->bands[k] < 1; k++) {
179 // parse coefficients
181 while (i < s->frame_len) {
183 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
188 j = FFMIN(j, s->frame_len);
190 width = get_bits(gb, 4);
192 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
194 while (s->bands[k] * 2 < i)
198 if (s->bands[k] * 2 == i)
200 coeff = get_bits(gb, width);
203 coeffs[i] = -q * coeff;
205 coeffs[i] = q * coeff;
214 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct)
215 ff_dct_calc (&s->trans.dct, coeffs);
216 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
217 ff_rdft_calc(&s->trans.rdft, coeffs);
220 s->dsp.float_to_int16_interleave(out, (const float **)s->coeffs_ptr, s->frame_len, s->channels);
223 int count = s->overlap_len * s->channels;
224 int shift = av_log2(count);
225 for (i = 0; i < count; i++) {
226 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
230 memcpy(s->previous, out + s->block_size,
231 s->overlap_len * s->channels * sizeof(*out));
236 static av_cold int decode_end(AVCodecContext *avctx)
238 BinkAudioContext * s = avctx->priv_data;
240 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
241 ff_rdft_end(&s->trans.rdft);
242 else if (CONFIG_BINKAUDIO_DCT_DECODER)
243 ff_dct_end(&s->trans.dct);
247 static void get_bits_align32(GetBitContext *s)
249 int n = (-get_bits_count(s)) & 31;
250 if (n) skip_bits(s, n);
253 static int decode_frame(AVCodecContext *avctx,
254 void *data, int *data_size,
257 BinkAudioContext *s = avctx->priv_data;
258 const uint8_t *buf = avpkt->data;
259 int buf_size = avpkt->size;
260 short *samples = data;
261 short *samples_end = (short*)((uint8_t*)data + *data_size);
263 GetBitContext *gb = &s->gb;
265 init_get_bits(gb, buf, buf_size * 8);
267 reported_size = get_bits_long(gb, 32);
268 while (get_bits_count(gb) / 8 < buf_size &&
269 samples + s->block_size <= samples_end) {
270 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
271 samples += s->block_size;
272 get_bits_align32(gb);
275 *data_size = (uint8_t*)samples - (uint8_t*)data;
276 if (reported_size != *data_size) {
277 av_log(avctx, AV_LOG_WARNING, "reported data size (%d) does not match output data size (%d)\n",
278 reported_size, *data_size);
283 AVCodec binkaudio_rdft_decoder = {
286 CODEC_ID_BINKAUDIO_RDFT,
287 sizeof(BinkAudioContext),
292 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
295 AVCodec binkaudio_dct_decoder = {
298 CODEC_ID_BINKAUDIO_DCT,
299 sizeof(BinkAudioContext),
304 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")