binkaudio: store interleaved overlap samples in BinkAudioContext.
[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 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 union {
66 RDFTContext rdft;
67 DCTContext dct;
68 } trans;
69 } BinkAudioContext;
70
71
72 static av_cold int decode_init(AVCodecContext *avctx)
73 {
74 BinkAudioContext *s = avctx->priv_data;
75 int sample_rate = avctx->sample_rate;
76 int sample_rate_half;
77 int i;
78 int frame_len_bits;
79
80 dsputil_init(&s->dsp, avctx);
81 ff_fmt_convert_init(&s->fmt_conv, avctx);
82
83 /* determine frame length */
84 if (avctx->sample_rate < 22050) {
85 frame_len_bits = 9;
86 } else if (avctx->sample_rate < 44100) {
87 frame_len_bits = 10;
88 } else {
89 frame_len_bits = 11;
90 }
91
92 if (avctx->channels > MAX_CHANNELS) {
93 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
94 return -1;
95 }
96
97 s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
98
99 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
100 // audio is already interleaved for the RDFT format variant
101 sample_rate *= avctx->channels;
102 s->channels = 1;
103 if (!s->version_b)
104 frame_len_bits += av_log2(avctx->channels);
105 } else {
106 s->channels = avctx->channels;
107 }
108
109 s->frame_len = 1 << frame_len_bits;
110 s->overlap_len = s->frame_len / 16;
111 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
112 sample_rate_half = (sample_rate + 1) / 2;
113 s->root = 2.0 / sqrt(s->frame_len);
114 for (i = 0; i < 95; i++) {
115 /* constant is result of 0.066399999/log10(M_E) */
116 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
117 }
118
119 /* calculate number of bands */
120 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
121 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
122 break;
123
124 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
125 if (!s->bands)
126 return AVERROR(ENOMEM);
127
128 /* populate bands data */
129 s->bands[0] = 2;
130 for (i = 1; i < s->num_bands; i++)
131 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
132 s->bands[s->num_bands] = s->frame_len;
133
134 s->first = 1;
135 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
136
137 for (i = 0; i < s->channels; i++) {
138 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
139 s->prev_ptr[i] = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
140 }
141
142 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
143 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
144 else if (CONFIG_BINKAUDIO_DCT_DECODER)
145 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
146 else
147 return -1;
148
149 return 0;
150 }
151
152 static float get_float(GetBitContext *gb)
153 {
154 int power = get_bits(gb, 5);
155 float f = ldexpf(get_bits_long(gb, 23), power - 23);
156 if (get_bits1(gb))
157 f = -f;
158 return f;
159 }
160
161 static const uint8_t rle_length_tab[16] = {
162 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
163 };
164
165 #define GET_BITS_SAFE(out, nbits) do { \
166 if (get_bits_left(gb) < nbits) \
167 return AVERROR_INVALIDDATA; \
168 out = get_bits(gb, nbits); \
169 } while (0)
170
171 /**
172 * Decode Bink Audio block
173 * @param[out] out Output buffer (must contain s->block_size elements)
174 * @return 0 on success, negative error code on failure
175 */
176 static int decode_block(BinkAudioContext *s, short *out, int use_dct)
177 {
178 int ch, i, j, k;
179 float q, quant[25];
180 int width, coeff;
181 GetBitContext *gb = &s->gb;
182
183 if (use_dct)
184 skip_bits(gb, 2);
185
186 for (ch = 0; ch < s->channels; ch++) {
187 FFTSample *coeffs = s->coeffs_ptr[ch];
188 if (s->version_b) {
189 if (get_bits_left(gb) < 64)
190 return AVERROR_INVALIDDATA;
191 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
192 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
193 } else {
194 if (get_bits_left(gb) < 58)
195 return AVERROR_INVALIDDATA;
196 coeffs[0] = get_float(gb) * s->root;
197 coeffs[1] = get_float(gb) * s->root;
198 }
199
200 if (get_bits_left(gb) < s->num_bands * 8)
201 return AVERROR_INVALIDDATA;
202 for (i = 0; i < s->num_bands; i++) {
203 int value = get_bits(gb, 8);
204 quant[i] = quant_table[FFMIN(value, 95)];
205 }
206
207 k = 0;
208 q = quant[0];
209
210 // parse coefficients
211 i = 2;
212 while (i < s->frame_len) {
213 if (s->version_b) {
214 j = i + 16;
215 } else {
216 int v;
217 GET_BITS_SAFE(v, 1);
218 if (v) {
219 GET_BITS_SAFE(v, 4);
220 j = i + rle_length_tab[v] * 8;
221 } else {
222 j = i + 8;
223 }
224 }
225
226 j = FFMIN(j, s->frame_len);
227
228 GET_BITS_SAFE(width, 4);
229 if (width == 0) {
230 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
231 i = j;
232 while (s->bands[k] < i)
233 q = quant[k++];
234 } else {
235 while (i < j) {
236 if (s->bands[k] == i)
237 q = quant[k++];
238 GET_BITS_SAFE(coeff, width);
239 if (coeff) {
240 int v;
241 GET_BITS_SAFE(v, 1);
242 if (v)
243 coeffs[i] = -q * coeff;
244 else
245 coeffs[i] = q * coeff;
246 } else {
247 coeffs[i] = 0.0f;
248 }
249 i++;
250 }
251 }
252 }
253
254 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
255 coeffs[0] /= 0.5;
256 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
257 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
258 }
259 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
260 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
261 }
262
263 s->fmt_conv.float_to_int16_interleave(s->current,
264 (const float **)s->prev_ptr,
265 s->overlap_len, s->channels);
266 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
267 s->frame_len - s->overlap_len,
268 s->channels);
269
270 if (!s->first) {
271 int count = s->overlap_len * s->channels;
272 int shift = av_log2(count);
273 for (i = 0; i < count; i++) {
274 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
275 }
276 }
277
278 memcpy(s->previous, s->current,
279 s->overlap_len * s->channels * sizeof(*s->previous));
280
281 s->first = 0;
282
283 return 0;
284 }
285
286 static av_cold int decode_end(AVCodecContext *avctx)
287 {
288 BinkAudioContext * s = avctx->priv_data;
289 av_freep(&s->bands);
290 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
291 ff_rdft_end(&s->trans.rdft);
292 else if (CONFIG_BINKAUDIO_DCT_DECODER)
293 ff_dct_end(&s->trans.dct);
294 return 0;
295 }
296
297 static void get_bits_align32(GetBitContext *s)
298 {
299 int n = (-get_bits_count(s)) & 31;
300 if (n) skip_bits(s, n);
301 }
302
303 static int decode_frame(AVCodecContext *avctx,
304 void *data, int *data_size,
305 AVPacket *avpkt)
306 {
307 BinkAudioContext *s = avctx->priv_data;
308 const uint8_t *buf = avpkt->data;
309 int buf_size = avpkt->size;
310 short *samples = data;
311 short *samples_end = (short*)((uint8_t*)data + *data_size);
312 int reported_size;
313 GetBitContext *gb = &s->gb;
314
315 if (buf_size < 4) {
316 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
317 return AVERROR_INVALIDDATA;
318 }
319
320 init_get_bits(gb, buf, buf_size * 8);
321
322 reported_size = get_bits_long(gb, 32);
323 while (samples + s->block_size <= samples_end) {
324 if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT))
325 break;
326 samples += s->block_size;
327 get_bits_align32(gb);
328 }
329
330 *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
331 return buf_size;
332 }
333
334 AVCodec ff_binkaudio_rdft_decoder = {
335 .name = "binkaudio_rdft",
336 .type = AVMEDIA_TYPE_AUDIO,
337 .id = CODEC_ID_BINKAUDIO_RDFT,
338 .priv_data_size = sizeof(BinkAudioContext),
339 .init = decode_init,
340 .close = decode_end,
341 .decode = decode_frame,
342 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
343 };
344
345 AVCodec ff_binkaudio_dct_decoder = {
346 .name = "binkaudio_dct",
347 .type = AVMEDIA_TYPE_AUDIO,
348 .id = CODEC_ID_BINKAUDIO_DCT,
349 .priv_data_size = sizeof(BinkAudioContext),
350 .init = decode_init,
351 .close = decode_end,
352 .decode = decode_frame,
353 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
354 };