binkaudio: change short to int16_t
[libav.git] / libavcodec / binkaudio.c
CommitLineData
c0d3f516
PR
1/*
2 * Bink Audio decoder
ccfcddb3 3 * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
c0d3f516
PR
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
5 *
2912e87a 6 * This file is part of Libav.
c0d3f516 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
c0d3f516
PR
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
c0d3f516
PR
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
c0d3f516
PR
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
ba87f080 24 * @file
c0d3f516
PR
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"
0aded948
MR
35#include "dct.h"
36#include "rdft.h"
c73d99e6 37#include "fmtconvert.h"
ccfcddb3 38#include "libavutil/intfloat_readwrite.h"
1429224b 39
c0d3f516
PR
40extern const uint16_t ff_wma_critical_freqs[25];
41
9f48039a
JR
42static float quant_table[95];
43
c0d3f516
PR
44#define MAX_CHANNELS 2
45#define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
46
47typedef struct {
c0d3f516
PR
48 GetBitContext gb;
49 DSPContext dsp;
c73d99e6 50 FmtConvertContext fmt_conv;
ccfcddb3 51 int version_b; ///< Bink version 'b'
c0d3f516
PR
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;
9d35fa52 60 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
425a8435 61 DECLARE_ALIGNED(16, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
eaddd29e 62 DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
c0d3f516 63 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
eaddd29e 64 float *prev_ptr[MAX_CHANNELS]; ///< pointers to the overlap points in the coeffs array
4f4e1948 65 uint8_t *packet_buffer;
c0d3f516
PR
66 union {
67 RDFTContext rdft;
68 DCTContext dct;
69 } trans;
70} BinkAudioContext;
71
72
73static av_cold int decode_init(AVCodecContext *avctx)
74{
75 BinkAudioContext *s = avctx->priv_data;
76 int sample_rate = avctx->sample_rate;
77 int sample_rate_half;
78 int i;
79 int frame_len_bits;
80
c0d3f516 81 dsputil_init(&s->dsp, avctx);
c73d99e6 82 ff_fmt_convert_init(&s->fmt_conv, avctx);
c0d3f516
PR
83
84 /* determine frame length */
85 if (avctx->sample_rate < 22050) {
86 frame_len_bits = 9;
87 } else if (avctx->sample_rate < 44100) {
88 frame_len_bits = 10;
89 } else {
90 frame_len_bits = 11;
91 }
c0d3f516 92
9806fbd5 93 if (avctx->channels > MAX_CHANNELS) {
71f88b1f 94 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
c0d3f516
PR
95 return -1;
96 }
97
df64da3b 98 s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
ccfcddb3 99
c0d3f516
PR
100 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
101 // audio is already interleaved for the RDFT format variant
102 sample_rate *= avctx->channels;
c0d3f516 103 s->channels = 1;
ccfcddb3
PR
104 if (!s->version_b)
105 frame_len_bits += av_log2(avctx->channels);
c0d3f516
PR
106 } else {
107 s->channels = avctx->channels;
108 }
109
8a8c283e 110 s->frame_len = 1 << frame_len_bits;
c0d3f516
PR
111 s->overlap_len = s->frame_len / 16;
112 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
113 sample_rate_half = (sample_rate + 1) / 2;
114 s->root = 2.0 / sqrt(s->frame_len);
9f48039a
JR
115 for (i = 0; i < 95; i++) {
116 /* constant is result of 0.066399999/log10(M_E) */
117 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
118 }
c0d3f516
PR
119
120 /* calculate number of bands */
121 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
122 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
123 break;
124
125 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
126 if (!s->bands)
127 return AVERROR(ENOMEM);
128
129 /* populate bands data */
582ac86d 130 s->bands[0] = 2;
c0d3f516 131 for (i = 1; i < s->num_bands; i++)
582ac86d
PR
132 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
133 s->bands[s->num_bands] = s->frame_len;
c0d3f516
PR
134
135 s->first = 1;
5d6e4c16 136 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
c0d3f516 137
eaddd29e 138 for (i = 0; i < s->channels; i++) {
c0d3f516 139 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
eaddd29e
JR
140 s->prev_ptr[i] = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
141 }
c0d3f516 142
e0ae3591 143 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
41ea18fb 144 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
e0ae3591 145 else if (CONFIG_BINKAUDIO_DCT_DECODER)
c2b774a0 146 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
e0ae3591
DV
147 else
148 return -1;
c0d3f516
PR
149
150 return 0;
151}
152
153static float get_float(GetBitContext *gb)
154{
155 int power = get_bits(gb, 5);
156 float f = ldexpf(get_bits_long(gb, 23), power - 23);
157 if (get_bits1(gb))
158 f = -f;
159 return f;
160}
161
162static const uint8_t rle_length_tab[16] = {
163 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
164};
165
101ef19e
JR
166#define GET_BITS_SAFE(out, nbits) do { \
167 if (get_bits_left(gb) < nbits) \
168 return AVERROR_INVALIDDATA; \
169 out = get_bits(gb, nbits); \
170} while (0)
171
c0d3f516
PR
172/**
173 * Decode Bink Audio block
174 * @param[out] out Output buffer (must contain s->block_size elements)
101ef19e 175 * @return 0 on success, negative error code on failure
c0d3f516 176 */
425a8435 177static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
c0d3f516
PR
178{
179 int ch, i, j, k;
180 float q, quant[25];
181 int width, coeff;
182 GetBitContext *gb = &s->gb;
183
184 if (use_dct)
185 skip_bits(gb, 2);
186
187 for (ch = 0; ch < s->channels; ch++) {
188 FFTSample *coeffs = s->coeffs_ptr[ch];
ccfcddb3 189 if (s->version_b) {
101ef19e
JR
190 if (get_bits_left(gb) < 64)
191 return AVERROR_INVALIDDATA;
ccfcddb3
PR
192 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
193 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
194 } else {
101ef19e
JR
195 if (get_bits_left(gb) < 58)
196 return AVERROR_INVALIDDATA;
ccfcddb3
PR
197 coeffs[0] = get_float(gb) * s->root;
198 coeffs[1] = get_float(gb) * s->root;
199 }
c0d3f516 200
101ef19e
JR
201 if (get_bits_left(gb) < s->num_bands * 8)
202 return AVERROR_INVALIDDATA;
c0d3f516 203 for (i = 0; i < s->num_bands; i++) {
c0d3f516 204 int value = get_bits(gb, 8);
9f48039a 205 quant[i] = quant_table[FFMIN(value, 95)];
c0d3f516
PR
206 }
207
a304def1
PR
208 k = 0;
209 q = quant[0];
c0d3f516
PR
210
211 // parse coefficients
212 i = 2;
213 while (i < s->frame_len) {
ccfcddb3
PR
214 if (s->version_b) {
215 j = i + 16;
c0d3f516 216 } else {
101ef19e
JR
217 int v;
218 GET_BITS_SAFE(v, 1);
219 if (v) {
220 GET_BITS_SAFE(v, 4);
221 j = i + rle_length_tab[v] * 8;
222 } else {
223 j = i + 8;
224 }
c0d3f516
PR
225 }
226
227 j = FFMIN(j, s->frame_len);
228
101ef19e 229 GET_BITS_SAFE(width, 4);
c0d3f516
PR
230 if (width == 0) {
231 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
232 i = j;
582ac86d 233 while (s->bands[k] < i)
c0d3f516
PR
234 q = quant[k++];
235 } else {
236 while (i < j) {
582ac86d 237 if (s->bands[k] == i)
c0d3f516 238 q = quant[k++];
101ef19e 239 GET_BITS_SAFE(coeff, width);
c0d3f516 240 if (coeff) {
101ef19e
JR
241 int v;
242 GET_BITS_SAFE(v, 1);
243 if (v)
c0d3f516
PR
244 coeffs[i] = -q * coeff;
245 else
246 coeffs[i] = q * coeff;
247 } else {
248 coeffs[i] = 0.0f;
249 }
250 i++;
251 }
252 }
253 }
254
54063e37
PR
255 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
256 coeffs[0] /= 0.5;
26f548bb 257 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
54063e37
PR
258 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
259 }
e0ae3591 260 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
26f548bb 261 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
c0d3f516
PR
262 }
263
eaddd29e
JR
264 s->fmt_conv.float_to_int16_interleave(s->current,
265 (const float **)s->prev_ptr,
266 s->overlap_len, s->channels);
c73d99e6 267 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
eaddd29e
JR
268 s->frame_len - s->overlap_len,
269 s->channels);
c0d3f516
PR
270
271 if (!s->first) {
272 int count = s->overlap_len * s->channels;
273 int shift = av_log2(count);
274 for (i = 0; i < count; i++) {
275 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
276 }
277 }
278
eaddd29e
JR
279 memcpy(s->previous, s->current,
280 s->overlap_len * s->channels * sizeof(*s->previous));
c0d3f516
PR
281
282 s->first = 0;
101ef19e
JR
283
284 return 0;
c0d3f516
PR
285}
286
287static av_cold int decode_end(AVCodecContext *avctx)
288{
289 BinkAudioContext * s = avctx->priv_data;
290 av_freep(&s->bands);
4f4e1948 291 av_freep(&s->packet_buffer);
e0ae3591 292 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
c0d3f516 293 ff_rdft_end(&s->trans.rdft);
e0ae3591 294 else if (CONFIG_BINKAUDIO_DCT_DECODER)
c0d3f516
PR
295 ff_dct_end(&s->trans.dct);
296 return 0;
297}
298
299static void get_bits_align32(GetBitContext *s)
300{
301 int n = (-get_bits_count(s)) & 31;
302 if (n) skip_bits(s, n);
303}
304
305static int decode_frame(AVCodecContext *avctx,
306 void *data, int *data_size,
307 AVPacket *avpkt)
308{
309 BinkAudioContext *s = avctx->priv_data;
425a8435 310 int16_t *samples = data;
c0d3f516 311 GetBitContext *gb = &s->gb;
4f4e1948
JR
312 int out_size, consumed = 0;
313
314 if (!get_bits_left(gb)) {
315 uint8_t *buf;
316 /* handle end-of-stream */
317 if (!avpkt->size) {
318 *data_size = 0;
319 return 0;
320 }
321 if (avpkt->size < 4) {
322 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
323 return AVERROR_INVALIDDATA;
324 }
325 buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
326 if (!buf)
327 return AVERROR(ENOMEM);
328 s->packet_buffer = buf;
329 memcpy(s->packet_buffer, avpkt->data, avpkt->size);
330 init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
331 consumed = avpkt->size;
332
333 /* skip reported size */
334 skip_bits_long(gb, 32);
101ef19e
JR
335 }
336
4f4e1948
JR
337 out_size = s->block_size * av_get_bytes_per_sample(avctx->sample_fmt);
338 if (*data_size < out_size) {
339 av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
340 return AVERROR(EINVAL);
341 }
c0d3f516 342
4f4e1948
JR
343 if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT)) {
344 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
345 return AVERROR_INVALIDDATA;
c0d3f516 346 }
4f4e1948 347 get_bits_align32(gb);
c0d3f516 348
4f4e1948
JR
349 *data_size = out_size;
350 return consumed;
c0d3f516
PR
351}
352
d36beb3f 353AVCodec ff_binkaudio_rdft_decoder = {
ec6402b7
AK
354 .name = "binkaudio_rdft",
355 .type = AVMEDIA_TYPE_AUDIO,
356 .id = CODEC_ID_BINKAUDIO_RDFT,
357 .priv_data_size = sizeof(BinkAudioContext),
358 .init = decode_init,
359 .close = decode_end,
360 .decode = decode_frame,
4f4e1948 361 .capabilities = CODEC_CAP_DELAY,
c0d3f516
PR
362 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
363};
364
d36beb3f 365AVCodec ff_binkaudio_dct_decoder = {
ec6402b7
AK
366 .name = "binkaudio_dct",
367 .type = AVMEDIA_TYPE_AUDIO,
368 .id = CODEC_ID_BINKAUDIO_DCT,
369 .priv_data_size = sizeof(BinkAudioContext),
370 .init = decode_init,
371 .close = decode_end,
372 .decode = decode_frame,
4f4e1948 373 .capabilities = CODEC_CAP_DELAY,
c0d3f516
PR
374 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
375};