binkaudio: pre-calculate quantization factors
[libav.git] / libavcodec / binkaudio.c
1 /*
2 * Bink Audio decoder
3 * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
5 *
6 * This file is part of Libav.
7 *
8 * Libav 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.
12 *
13 * Libav 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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Bink Audio decoder
26 *
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
29 */
30
31 #include "avcodec.h"
32 #define ALT_BITSTREAM_READER_LE
33 #include "get_bits.h"
34 #include "dsputil.h"
35 #include "dct.h"
36 #include "rdft.h"
37 #include "fmtconvert.h"
38 #include "libavutil/intfloat_readwrite.h"
39
40 extern const uint16_t ff_wma_critical_freqs[25];
41
42 static float quant_table[95];
43
44 #define MAX_CHANNELS 2
45 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
46
47 typedef struct {
48 GetBitContext gb;
49 DSPContext dsp;
50 FmtConvertContext fmt_conv;
51 int version_b; ///< Bink version 'b'
52 int first;
53 int channels;
54 int frame_len; ///< transform size (samples)
55 int overlap_len; ///< overlap size (samples)
56 int block_size;
57 int num_bands;
58 unsigned int *bands;
59 float root;
60 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
61 DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
62 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
63 union {
64 RDFTContext rdft;
65 DCTContext dct;
66 } trans;
67 } BinkAudioContext;
68
69
70 static av_cold int decode_init(AVCodecContext *avctx)
71 {
72 BinkAudioContext *s = avctx->priv_data;
73 int sample_rate = avctx->sample_rate;
74 int sample_rate_half;
75 int i;
76 int frame_len_bits;
77
78 dsputil_init(&s->dsp, avctx);
79 ff_fmt_convert_init(&s->fmt_conv, avctx);
80
81 /* determine frame length */
82 if (avctx->sample_rate < 22050) {
83 frame_len_bits = 9;
84 } else if (avctx->sample_rate < 44100) {
85 frame_len_bits = 10;
86 } else {
87 frame_len_bits = 11;
88 }
89
90 if (avctx->channels > MAX_CHANNELS) {
91 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
92 return -1;
93 }
94
95 s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
96
97 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
98 // audio is already interleaved for the RDFT format variant
99 sample_rate *= avctx->channels;
100 s->channels = 1;
101 if (!s->version_b)
102 frame_len_bits += av_log2(avctx->channels);
103 } else {
104 s->channels = avctx->channels;
105 }
106
107 s->frame_len = 1 << frame_len_bits;
108 s->overlap_len = s->frame_len / 16;
109 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
110 sample_rate_half = (sample_rate + 1) / 2;
111 s->root = 2.0 / sqrt(s->frame_len);
112 for (i = 0; i < 95; i++) {
113 /* constant is result of 0.066399999/log10(M_E) */
114 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
115 }
116
117 /* calculate number of bands */
118 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
119 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
120 break;
121
122 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
123 if (!s->bands)
124 return AVERROR(ENOMEM);
125
126 /* populate bands data */
127 s->bands[0] = 2;
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;
131
132 s->first = 1;
133 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
134
135 for (i = 0; i < s->channels; i++)
136 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
137
138 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
139 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
140 else if (CONFIG_BINKAUDIO_DCT_DECODER)
141 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
142 else
143 return -1;
144
145 return 0;
146 }
147
148 static float get_float(GetBitContext *gb)
149 {
150 int power = get_bits(gb, 5);
151 float f = ldexpf(get_bits_long(gb, 23), power - 23);
152 if (get_bits1(gb))
153 f = -f;
154 return f;
155 }
156
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
159 };
160
161 #define GET_BITS_SAFE(out, nbits) do { \
162 if (get_bits_left(gb) < nbits) \
163 return AVERROR_INVALIDDATA; \
164 out = get_bits(gb, nbits); \
165 } while (0)
166
167 /**
168 * Decode Bink Audio block
169 * @param[out] out Output buffer (must contain s->block_size elements)
170 * @return 0 on success, negative error code on failure
171 */
172 static int decode_block(BinkAudioContext *s, short *out, int use_dct)
173 {
174 int ch, i, j, k;
175 float q, quant[25];
176 int width, coeff;
177 GetBitContext *gb = &s->gb;
178
179 if (use_dct)
180 skip_bits(gb, 2);
181
182 for (ch = 0; ch < s->channels; ch++) {
183 FFTSample *coeffs = s->coeffs_ptr[ch];
184 if (s->version_b) {
185 if (get_bits_left(gb) < 64)
186 return AVERROR_INVALIDDATA;
187 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
188 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
189 } else {
190 if (get_bits_left(gb) < 58)
191 return AVERROR_INVALIDDATA;
192 coeffs[0] = get_float(gb) * s->root;
193 coeffs[1] = get_float(gb) * s->root;
194 }
195
196 if (get_bits_left(gb) < s->num_bands * 8)
197 return AVERROR_INVALIDDATA;
198 for (i = 0; i < s->num_bands; i++) {
199 int value = get_bits(gb, 8);
200 quant[i] = quant_table[FFMIN(value, 95)];
201 }
202
203 k = 0;
204 q = quant[0];
205
206 // parse coefficients
207 i = 2;
208 while (i < s->frame_len) {
209 if (s->version_b) {
210 j = i + 16;
211 } else {
212 int v;
213 GET_BITS_SAFE(v, 1);
214 if (v) {
215 GET_BITS_SAFE(v, 4);
216 j = i + rle_length_tab[v] * 8;
217 } else {
218 j = i + 8;
219 }
220 }
221
222 j = FFMIN(j, s->frame_len);
223
224 GET_BITS_SAFE(width, 4);
225 if (width == 0) {
226 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
227 i = j;
228 while (s->bands[k] < i)
229 q = quant[k++];
230 } else {
231 while (i < j) {
232 if (s->bands[k] == i)
233 q = quant[k++];
234 GET_BITS_SAFE(coeff, width);
235 if (coeff) {
236 int v;
237 GET_BITS_SAFE(v, 1);
238 if (v)
239 coeffs[i] = -q * coeff;
240 else
241 coeffs[i] = q * coeff;
242 } else {
243 coeffs[i] = 0.0f;
244 }
245 i++;
246 }
247 }
248 }
249
250 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
251 coeffs[0] /= 0.5;
252 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
253 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
254 }
255 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
256 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
257 }
258
259 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
260 s->frame_len, s->channels);
261
262 if (!s->first) {
263 int count = s->overlap_len * s->channels;
264 int shift = av_log2(count);
265 for (i = 0; i < count; i++) {
266 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
267 }
268 }
269
270 memcpy(s->previous, out + s->block_size,
271 s->overlap_len * s->channels * sizeof(*out));
272
273 s->first = 0;
274
275 return 0;
276 }
277
278 static av_cold int decode_end(AVCodecContext *avctx)
279 {
280 BinkAudioContext * s = avctx->priv_data;
281 av_freep(&s->bands);
282 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
283 ff_rdft_end(&s->trans.rdft);
284 else if (CONFIG_BINKAUDIO_DCT_DECODER)
285 ff_dct_end(&s->trans.dct);
286 return 0;
287 }
288
289 static void get_bits_align32(GetBitContext *s)
290 {
291 int n = (-get_bits_count(s)) & 31;
292 if (n) skip_bits(s, n);
293 }
294
295 static int decode_frame(AVCodecContext *avctx,
296 void *data, int *data_size,
297 AVPacket *avpkt)
298 {
299 BinkAudioContext *s = avctx->priv_data;
300 const uint8_t *buf = avpkt->data;
301 int buf_size = avpkt->size;
302 short *samples = data;
303 short *samples_end = (short*)((uint8_t*)data + *data_size);
304 int reported_size;
305 GetBitContext *gb = &s->gb;
306
307 if (buf_size < 4) {
308 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
309 return AVERROR_INVALIDDATA;
310 }
311
312 init_get_bits(gb, buf, buf_size * 8);
313
314 reported_size = get_bits_long(gb, 32);
315 while (samples + s->block_size <= samples_end) {
316 if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT))
317 break;
318 samples += s->block_size;
319 get_bits_align32(gb);
320 }
321
322 *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
323 return buf_size;
324 }
325
326 AVCodec ff_binkaudio_rdft_decoder = {
327 .name = "binkaudio_rdft",
328 .type = AVMEDIA_TYPE_AUDIO,
329 .id = CODEC_ID_BINKAUDIO_RDFT,
330 .priv_data_size = sizeof(BinkAudioContext),
331 .init = decode_init,
332 .close = decode_end,
333 .decode = decode_frame,
334 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
335 };
336
337 AVCodec ff_binkaudio_dct_decoder = {
338 .name = "binkaudio_dct",
339 .type = AVMEDIA_TYPE_AUDIO,
340 .id = CODEC_ID_BINKAUDIO_DCT,
341 .priv_data_size = sizeof(BinkAudioContext),
342 .init = decode_init,
343 .close = decode_end,
344 .decode = decode_frame,
345 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
346 };