Move sine windows to a separate file
[libav.git] / libavcodec / wma.c
CommitLineData
59686073
MN
1/*
2 * WMA compatible codec
2912e87a 3 * Copyright (c) 2002-2007 The Libav Project
59686073 4 *
2912e87a 5 * This file is part of Libav.
59686073 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
59686073
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
59686073
MN
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
59686073
MN
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
4538729a 23#include "sinewin.h"
59686073
MN
24#include "wma.h"
25#include "wmadata.h"
26
27#undef NDEBUG
28#include <assert.h>
29
30/* XXX: use same run/length optimization as mpeg decoders */
31//FIXME maybe split decode / encode or pass flag
ae1374ce 32static void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
076a9dea 33 float **plevel_table, uint16_t **pint_table,
59686073
MN
34 const CoefVLCTable *vlc_table)
35{
36 int n = vlc_table->n;
ae1374ce
DB
37 const uint8_t *table_bits = vlc_table->huffbits;
38 const uint32_t *table_codes = vlc_table->huffcodes;
59686073
MN
39 const uint16_t *levels_table = vlc_table->levels;
40 uint16_t *run_table, *level_table, *int_table;
076a9dea 41 float *flevel_table;
59686073
MN
42 int i, l, j, k, level;
43
44 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
45
ae1374ce 46 run_table = av_malloc(n * sizeof(uint16_t));
59686073 47 level_table = av_malloc(n * sizeof(uint16_t));
076a9dea 48 flevel_table= av_malloc(n * sizeof(*flevel_table));
ae1374ce 49 int_table = av_malloc(n * sizeof(uint16_t));
59686073
MN
50 i = 2;
51 level = 1;
52 k = 0;
53 while (i < n) {
ae1374ce 54 int_table[k] = i;
59686073 55 l = levels_table[k++];
ae1374ce
DB
56 for (j = 0; j < l; j++) {
57 run_table[i] = j;
59686073 58 level_table[i] = level;
076a9dea 59 flevel_table[i]= level;
59686073
MN
60 i++;
61 }
62 level++;
63 }
ae1374ce 64 *prun_table = run_table;
076a9dea 65 *plevel_table = flevel_table;
ae1374ce 66 *pint_table = int_table;
076a9dea 67 av_free(level_table);
59686073
MN
68}
69
cee4bb89
SS
70/**
71 *@brief Get the samples per frame for this stream.
72 *@param sample_rate output sample_rate
73 *@param version wma version
74 *@param decode_flags codec compression features
75 *@return log2 of the number of output samples per frame
76 */
77int av_cold ff_wma_get_frame_len_bits(int sample_rate, int version,
78 unsigned int decode_flags)
79{
80
81 int frame_len_bits;
82
e8c7f81c 83 if (sample_rate <= 16000) {
cee4bb89 84 frame_len_bits = 9;
e8c7f81c
SS
85 } else if (sample_rate <= 22050 ||
86 (sample_rate <= 32000 && version == 1)) {
cee4bb89 87 frame_len_bits = 10;
e8c7f81c 88 } else if (sample_rate <= 48000) {
cee4bb89 89 frame_len_bits = 11;
523c09ca
SS
90 } else if (sample_rate <= 96000) {
91 frame_len_bits = 12;
92 } else {
93 frame_len_bits = 13;
94 }
95
96 if (version == 3) {
97 int tmp = decode_flags & 0x6;
98 if (tmp == 0x2) {
99 ++frame_len_bits;
100 } else if (tmp == 0x4) {
101 --frame_len_bits;
102 } else if (tmp == 0x6) {
103 frame_len_bits -= 2;
104 }
105 }
cee4bb89
SS
106
107 return frame_len_bits;
108}
109
ae1374ce 110int ff_wma_init(AVCodecContext *avctx, int flags2)
59686073 111{
911b9faf 112 WMACodecContext *s = avctx->priv_data;
59686073 113 int i;
59686073
MN
114 float bps1, high_freq;
115 volatile float bps;
116 int sample_rate1;
117 int coef_vlc_table;
118
ae1374ce
DB
119 if ( avctx->sample_rate <= 0 || avctx->sample_rate > 50000
120 || avctx->channels <= 0 || avctx->channels > 8
121 || avctx->bit_rate <= 0)
47b777ce
MN
122 return -1;
123
59686073
MN
124 s->sample_rate = avctx->sample_rate;
125 s->nb_channels = avctx->channels;
ae1374ce 126 s->bit_rate = avctx->bit_rate;
59686073
MN
127 s->block_align = avctx->block_align;
128
129 dsputil_init(&s->dsp, avctx);
c73d99e6 130 ff_fmt_convert_init(&s->fmt_conv, avctx);
59686073
MN
131
132 if (avctx->codec->id == CODEC_ID_WMAV1) {
133 s->version = 1;
134 } else {
135 s->version = 2;
136 }
137
138 /* compute MDCT block size */
cee4bb89
SS
139 s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0);
140
59686073
MN
141 s->frame_len = 1 << s->frame_len_bits;
142 if (s->use_variable_block_len) {
143 int nb_max, nb;
144 nb = ((flags2 >> 3) & 3) + 1;
145 if ((s->bit_rate / s->nb_channels) >= 32000)
146 nb += 2;
147 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
148 if (nb > nb_max)
149 nb = nb_max;
150 s->nb_block_sizes = nb + 1;
151 } else {
152 s->nb_block_sizes = 1;
153 }
154
eafcac6a 155 /* init rate dependent parameters */
59686073
MN
156 s->use_noise_coding = 1;
157 high_freq = s->sample_rate * 0.5;
158
159 /* if version 2, then the rates are normalized */
160 sample_rate1 = s->sample_rate;
161 if (s->version == 2) {
e8c7f81c 162 if (sample_rate1 >= 44100) {
59686073 163 sample_rate1 = 44100;
e8c7f81c 164 } else if (sample_rate1 >= 22050) {
59686073 165 sample_rate1 = 22050;
e8c7f81c 166 } else if (sample_rate1 >= 16000) {
59686073 167 sample_rate1 = 16000;
e8c7f81c 168 } else if (sample_rate1 >= 11025) {
59686073 169 sample_rate1 = 11025;
e8c7f81c 170 } else if (sample_rate1 >= 8000) {
59686073 171 sample_rate1 = 8000;
e8c7f81c 172 }
59686073
MN
173 }
174
175 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
176 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
177
178 /* compute high frequency value and choose if noise coding should
179 be activated */
180 bps1 = bps;
181 if (s->nb_channels == 2)
182 bps1 = bps * 1.6;
183 if (sample_rate1 == 44100) {
e8c7f81c 184 if (bps1 >= 0.61) {
59686073 185 s->use_noise_coding = 0;
e8c7f81c 186 } else {
59686073 187 high_freq = high_freq * 0.4;
e8c7f81c 188 }
59686073 189 } else if (sample_rate1 == 22050) {
e8c7f81c 190 if (bps1 >= 1.16) {
59686073 191 s->use_noise_coding = 0;
e8c7f81c 192 } else if (bps1 >= 0.72) {
59686073 193 high_freq = high_freq * 0.7;
e8c7f81c 194 } else {
59686073 195 high_freq = high_freq * 0.6;
e8c7f81c 196 }
59686073 197 } else if (sample_rate1 == 16000) {
e8c7f81c 198 if (bps > 0.5) {
59686073 199 high_freq = high_freq * 0.5;
e8c7f81c 200 } else {
59686073 201 high_freq = high_freq * 0.3;
e8c7f81c 202 }
59686073
MN
203 } else if (sample_rate1 == 11025) {
204 high_freq = high_freq * 0.7;
205 } else if (sample_rate1 == 8000) {
206 if (bps <= 0.625) {
207 high_freq = high_freq * 0.5;
208 } else if (bps > 0.75) {
209 s->use_noise_coding = 0;
210 } else {
211 high_freq = high_freq * 0.65;
212 }
213 } else {
214 if (bps >= 0.8) {
215 high_freq = high_freq * 0.75;
216 } else if (bps >= 0.6) {
217 high_freq = high_freq * 0.6;
218 } else {
219 high_freq = high_freq * 0.5;
220 }
221 }
dfd2a005
LB
222 av_dlog(s->avctx, "flags2=0x%x\n", flags2);
223 av_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
ae1374ce
DB
224 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
225 s->block_align);
dfd2a005 226 av_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
ae1374ce 227 bps, bps1, high_freq, s->byte_offset_bits);
dfd2a005 228 av_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
ae1374ce 229 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
59686073
MN
230
231 /* compute the scale factor band sizes for each MDCT block size */
232 {
233 int a, b, pos, lpos, k, block_len, i, j, n;
234 const uint8_t *table;
235
236 if (s->version == 1) {
237 s->coefs_start = 3;
238 } else {
239 s->coefs_start = 0;
240 }
ae1374ce 241 for (k = 0; k < s->nb_block_sizes; k++) {
59686073
MN
242 block_len = s->frame_len >> k;
243
244 if (s->version == 1) {
245 lpos = 0;
ae1374ce 246 for (i = 0; i < 25; i++) {
a7e6328c 247 a = ff_wma_critical_freqs[i];
59686073 248 b = s->sample_rate;
ae1374ce 249 pos = ((block_len * 2 * a) + (b >> 1)) / b;
59686073
MN
250 if (pos > block_len)
251 pos = block_len;
252 s->exponent_bands[0][i] = pos - lpos;
253 if (pos >= block_len) {
254 i++;
255 break;
256 }
257 lpos = pos;
258 }
259 s->exponent_sizes[0] = i;
260 } else {
261 /* hardcoded tables */
262 table = NULL;
263 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
264 if (a < 3) {
e8c7f81c 265 if (s->sample_rate >= 44100) {
59686073 266 table = exponent_band_44100[a];
e8c7f81c 267 } else if (s->sample_rate >= 32000) {
59686073 268 table = exponent_band_32000[a];
e8c7f81c 269 } else if (s->sample_rate >= 22050) {
59686073 270 table = exponent_band_22050[a];
e8c7f81c 271 }
59686073
MN
272 }
273 if (table) {
274 n = *table++;
ae1374ce 275 for (i = 0; i < n; i++)
59686073
MN
276 s->exponent_bands[k][i] = table[i];
277 s->exponent_sizes[k] = n;
278 } else {
279 j = 0;
280 lpos = 0;
ae1374ce 281 for (i = 0; i < 25; i++) {
a7e6328c 282 a = ff_wma_critical_freqs[i];
59686073 283 b = s->sample_rate;
ae1374ce 284 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
59686073
MN
285 pos <<= 2;
286 if (pos > block_len)
287 pos = block_len;
288 if (pos > lpos)
289 s->exponent_bands[k][j++] = pos - lpos;
290 if (pos >= block_len)
291 break;
292 lpos = pos;
293 }
294 s->exponent_sizes[k] = j;
295 }
296 }
297
298 /* max number of coefs */
299 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
300 /* high freq computation */
301 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
302 s->sample_rate + 0.5);
303 n = s->exponent_sizes[k];
304 j = 0;
305 pos = 0;
ae1374ce 306 for (i = 0; i < n; i++) {
59686073
MN
307 int start, end;
308 start = pos;
309 pos += s->exponent_bands[k][i];
310 end = pos;
311 if (start < s->high_band_start[k])
312 start = s->high_band_start[k];
313 if (end > s->coefs_end[k])
314 end = s->coefs_end[k];
315 if (end > start)
316 s->exponent_high_bands[k][j++] = end - start;
317 }
318 s->exponent_high_sizes[k] = j;
319#if 0
a9c9a240 320 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
ae1374ce
DB
321 s->frame_len >> k,
322 s->coefs_end[k],
323 s->high_band_start[k],
324 s->exponent_high_sizes[k]);
325 for (j = 0; j < s->exponent_high_sizes[k]; j++)
a9c9a240
MB
326 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
327 tprintf(s->avctx, "\n");
59686073
MN
328#endif
329 }
330 }
331
332#ifdef TRACE
333 {
334 int i, j;
ae1374ce 335 for (i = 0; i < s->nb_block_sizes; i++) {
a9c9a240 336 tprintf(s->avctx, "%5d: n=%2d:",
ae1374ce
DB
337 s->frame_len >> i,
338 s->exponent_sizes[i]);
339 for (j = 0; j < s->exponent_sizes[i]; j++)
a9c9a240
MB
340 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
341 tprintf(s->avctx, "\n");
59686073
MN
342 }
343 }
344#endif
345
346 /* init MDCT windows : simple sinus window */
ae1374ce 347 for (i = 0; i < s->nb_block_sizes; i++) {
14b86070 348 ff_init_ff_sine_windows(s->frame_len_bits - i);
6776061b 349 s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
59686073
MN
350 }
351
352 s->reset_block_lengths = 1;
353
354 if (s->use_noise_coding) {
355
356 /* init the noise generator */
e8c7f81c 357 if (s->use_exp_vlc) {
59686073 358 s->noise_mult = 0.02;
e8c7f81c 359 } else {
59686073 360 s->noise_mult = 0.04;
e8c7f81c 361 }
59686073
MN
362
363#ifdef TRACE
ae1374ce 364 for (i = 0; i < NOISE_TAB_SIZE; i++)
59686073
MN
365 s->noise_table[i] = 1.0 * s->noise_mult;
366#else
367 {
368 unsigned int seed;
369 float norm;
370 seed = 1;
371 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
ae1374ce 372 for (i = 0; i < NOISE_TAB_SIZE; i++) {
59686073
MN
373 seed = seed * 314159 + 1;
374 s->noise_table[i] = (float)((int)seed) * norm;
375 }
376 }
377#endif
378 }
379
380 /* choose the VLC tables for the coefficients */
381 coef_vlc_table = 2;
382 if (s->sample_rate >= 32000) {
e8c7f81c 383 if (bps1 < 0.72) {
59686073 384 coef_vlc_table = 0;
e8c7f81c 385 } else if (bps1 < 1.16) {
59686073 386 coef_vlc_table = 1;
e8c7f81c 387 }
59686073
MN
388 }
389 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
390 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
391 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
392 s->coef_vlcs[0]);
393 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
394 s->coef_vlcs[1]);
395
396 return 0;
397}
398
ae1374ce
DB
399int ff_wma_total_gain_to_bits(int total_gain)
400{
59686073
MN
401 if (total_gain < 15) return 13;
402 else if (total_gain < 32) return 12;
403 else if (total_gain < 40) return 11;
404 else if (total_gain < 45) return 10;
405 else return 9;
406}
407
408int ff_wma_end(AVCodecContext *avctx)
409{
911b9faf 410 WMACodecContext *s = avctx->priv_data;
59686073
MN
411 int i;
412
ae1374ce 413 for (i = 0; i < s->nb_block_sizes; i++)
59686073 414 ff_mdct_end(&s->mdct_ctx[i]);
59686073
MN
415
416 if (s->use_exp_vlc) {
417 free_vlc(&s->exp_vlc);
418 }
419 if (s->use_noise_coding) {
420 free_vlc(&s->hgain_vlc);
421 }
ae1374ce 422 for (i = 0; i < 2; i++) {
59686073
MN
423 free_vlc(&s->coef_vlc[i]);
424 av_free(s->run_table[i]);
425 av_free(s->level_table[i]);
2c79288d 426 av_free(s->int_table[i]);
59686073
MN
427 }
428
429 return 0;
430}
c0e9b2e8
SS
431
432/**
891bd2e5 433 * Decode an uncompressed coefficient.
503f9c0b 434 * @param gb GetBitContext
891bd2e5
SS
435 * @return the decoded coefficient
436 */
437unsigned int ff_wma_get_large_val(GetBitContext* gb)
438{
439 /** consumes up to 34 bits */
440 int n_bits = 8;
441 /** decode length */
442 if (get_bits1(gb)) {
443 n_bits += 8;
444 if (get_bits1(gb)) {
445 n_bits += 8;
446 if (get_bits1(gb)) {
447 n_bits += 7;
448 }
449 }
450 }
451 return get_bits_long(gb, n_bits);
452}
453
454/**
c0e9b2e8
SS
455 * Decode run level compressed coefficients.
456 * @param avctx codec context
457 * @param gb bitstream reader context
458 * @param vlc vlc table for get_vlc2
459 * @param level_table level codes
460 * @param run_table run codes
461 * @param version 0 for wma1,2 1 for wmapro
462 * @param ptr output buffer
463 * @param offset offset in the output buffer
464 * @param num_coefs number of input coefficents
465 * @param block_len input buffer length (2^n)
466 * @param frame_len_bits number of bits for escaped run codes
467 * @param coef_nb_bits number of bits for escaped level codes
468 * @return 0 on success, -1 otherwise
469 */
470int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
471 VLC *vlc,
076a9dea 472 const float *level_table, const uint16_t *run_table,
9970c61b 473 int version, WMACoef *ptr, int offset,
c0e9b2e8
SS
474 int num_coefs, int block_len, int frame_len_bits,
475 int coef_nb_bits)
476{
4df8bdee 477 int code, level, sign;
076a9dea
MR
478 const uint32_t *ilvl = (const uint32_t*)level_table;
479 uint32_t *iptr = (uint32_t*)ptr;
4df8bdee
SS
480 const unsigned int coef_mask = block_len - 1;
481 for (; offset < num_coefs; offset++) {
c0e9b2e8 482 code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
4df8bdee
SS
483 if (code > 1) {
484 /** normal code */
485 offset += run_table[code];
076a9dea
MR
486 sign = get_bits1(gb) - 1;
487 iptr[offset & coef_mask] = ilvl[code] ^ sign<<31;
4df8bdee 488 } else if (code == 1) {
4c7615c4 489 /** EOB */
c0e9b2e8 490 break;
4df8bdee 491 } else {
4c7615c4 492 /** escape */
891bd2e5 493 if (!version) {
4d67e067 494 level = get_bits(gb, coef_nb_bits);
4c7615c4
SS
495 /** NOTE: this is rather suboptimal. reading
496 block_len_bits would be better */
4df8bdee 497 offset += get_bits(gb, frame_len_bits);
891bd2e5
SS
498 } else {
499 level = ff_wma_get_large_val(gb);
500 /** escape decode */
501 if (get_bits1(gb)) {
502 if (get_bits1(gb)) {
503 if (get_bits1(gb)) {
504 av_log(avctx,AV_LOG_ERROR,
505 "broken escape sequence\n");
506 return -1;
507 } else
4df8bdee 508 offset += get_bits(gb, frame_len_bits) + 4;
891bd2e5 509 } else
4df8bdee
SS
510 offset += get_bits(gb, 2) + 1;
511 }
891bd2e5 512 }
076a9dea
MR
513 sign = get_bits1(gb) - 1;
514 ptr[offset & coef_mask] = (level^sign) - sign;
c0e9b2e8 515 }
c0e9b2e8 516 }
4df8bdee
SS
517 /** NOTE: EOB can be omitted */
518 if (offset > num_coefs) {
519 av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
520 return -1;
521 }
522
c0e9b2e8
SS
523 return 0;
524}
525