binkaudio: change short to int16_t
[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, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
62 DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
63 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
64 float *prev_ptr[MAX_CHANNELS]; ///< pointers to the overlap points in the coeffs array
65 uint8_t *packet_buffer;
66 union {
67 RDFTContext rdft;
68 DCTContext dct;
69 } trans;
70 } BinkAudioContext;
71
72
73 static 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
81 dsputil_init(&s->dsp, avctx);
82 ff_fmt_convert_init(&s->fmt_conv, avctx);
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 }
92
93 if (avctx->channels > MAX_CHANNELS) {
94 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
95 return -1;
96 }
97
98 s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
99
100 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
101 // audio is already interleaved for the RDFT format variant
102 sample_rate *= avctx->channels;
103 s->channels = 1;
104 if (!s->version_b)
105 frame_len_bits += av_log2(avctx->channels);
106 } else {
107 s->channels = avctx->channels;
108 }
109
110 s->frame_len = 1 << frame_len_bits;
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);
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 }
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 */
130 s->bands[0] = 2;
131 for (i = 1; i < s->num_bands; i++)
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;
134
135 s->first = 1;
136 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
137
138 for (i = 0; i < s->channels; i++) {
139 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
140 s->prev_ptr[i] = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
141 }
142
143 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
144 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
145 else if (CONFIG_BINKAUDIO_DCT_DECODER)
146 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
147 else
148 return -1;
149
150 return 0;
151 }
152
153 static 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
162 static 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
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
172 /**
173 * Decode Bink Audio block
174 * @param[out] out Output buffer (must contain s->block_size elements)
175 * @return 0 on success, negative error code on failure
176 */
177 static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
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];
189 if (s->version_b) {
190 if (get_bits_left(gb) < 64)
191 return AVERROR_INVALIDDATA;
192 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
193 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
194 } else {
195 if (get_bits_left(gb) < 58)
196 return AVERROR_INVALIDDATA;
197 coeffs[0] = get_float(gb) * s->root;
198 coeffs[1] = get_float(gb) * s->root;
199 }
200
201 if (get_bits_left(gb) < s->num_bands * 8)
202 return AVERROR_INVALIDDATA;
203 for (i = 0; i < s->num_bands; i++) {
204 int value = get_bits(gb, 8);
205 quant[i] = quant_table[FFMIN(value, 95)];
206 }
207
208 k = 0;
209 q = quant[0];
210
211 // parse coefficients
212 i = 2;
213 while (i < s->frame_len) {
214 if (s->version_b) {
215 j = i + 16;
216 } else {
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 }
225 }
226
227 j = FFMIN(j, s->frame_len);
228
229 GET_BITS_SAFE(width, 4);
230 if (width == 0) {
231 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
232 i = j;
233 while (s->bands[k] < i)
234 q = quant[k++];
235 } else {
236 while (i < j) {
237 if (s->bands[k] == i)
238 q = quant[k++];
239 GET_BITS_SAFE(coeff, width);
240 if (coeff) {
241 int v;
242 GET_BITS_SAFE(v, 1);
243 if (v)
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
255 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
256 coeffs[0] /= 0.5;
257 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
258 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
259 }
260 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
261 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
262 }
263
264 s->fmt_conv.float_to_int16_interleave(s->current,
265 (const float **)s->prev_ptr,
266 s->overlap_len, s->channels);
267 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
268 s->frame_len - s->overlap_len,
269 s->channels);
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
279 memcpy(s->previous, s->current,
280 s->overlap_len * s->channels * sizeof(*s->previous));
281
282 s->first = 0;
283
284 return 0;
285 }
286
287 static av_cold int decode_end(AVCodecContext *avctx)
288 {
289 BinkAudioContext * s = avctx->priv_data;
290 av_freep(&s->bands);
291 av_freep(&s->packet_buffer);
292 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
293 ff_rdft_end(&s->trans.rdft);
294 else if (CONFIG_BINKAUDIO_DCT_DECODER)
295 ff_dct_end(&s->trans.dct);
296 return 0;
297 }
298
299 static void get_bits_align32(GetBitContext *s)
300 {
301 int n = (-get_bits_count(s)) & 31;
302 if (n) skip_bits(s, n);
303 }
304
305 static int decode_frame(AVCodecContext *avctx,
306 void *data, int *data_size,
307 AVPacket *avpkt)
308 {
309 BinkAudioContext *s = avctx->priv_data;
310 int16_t *samples = data;
311 GetBitContext *gb = &s->gb;
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);
335 }
336
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 }
342
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;
346 }
347 get_bits_align32(gb);
348
349 *data_size = out_size;
350 return consumed;
351 }
352
353 AVCodec ff_binkaudio_rdft_decoder = {
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,
361 .capabilities = CODEC_CAP_DELAY,
362 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
363 };
364
365 AVCodec ff_binkaudio_dct_decoder = {
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,
373 .capabilities = CODEC_CAP_DELAY,
374 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
375 };