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