comparison wmadec.c @ 783:b6eefd714bf3 libavcodec

added wma decoder
author bellard
date Mon, 28 Oct 2002 00:48:55 +0000
parents
children 18b3fd684767
comparison
equal deleted inserted replaced
782:dd7d5748d064 783:b6eefd714bf3
1 /*
2 * WMA compatible decoder
3 * Copyright (c) 2002 The FFmpeg Project.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avcodec.h"
20 #include "dsputil.h"
21
22 //#define DEBUG_PARAMS
23 //#define DEBUG_TRACE
24
25 /* size of blocks */
26 #define BLOCK_MIN_BITS 7
27 #define BLOCK_MAX_BITS 11
28 #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
29
30 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
31
32 /* XXX: find exact max size */
33 #define HIGH_BAND_MAX_SIZE 16
34
35 #define NB_LSP_COEFS 10
36
37 /* XXX: is it a suitable value ? */
38 #define MAX_CODED_SUPERFRAME_SIZE 4096
39
40 #define MAX_CHANNELS 2
41
42 #define NOISE_TAB_SIZE 8192
43
44 #define LSP_POW_BITS 7
45
46 typedef struct WMADecodeContext {
47 GetBitContext gb;
48 int sample_rate;
49 int nb_channels;
50 int bit_rate;
51 int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
52 int block_align;
53 int use_bit_reservoir;
54 int use_variable_block_len;
55 int use_exp_vlc; /* exponent coding: 0 = lsp, 1 = vlc + delta */
56 int use_noise_coding; /* true if perceptual noise is added */
57 int byte_offset_bits;
58 VLC exp_vlc;
59 int exponent_sizes[BLOCK_NB_SIZES];
60 uint16_t exponent_bands[BLOCK_NB_SIZES][25];
61 int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
62 int coefs_start; /* first coded coef */
63 int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
64 int exponent_high_sizes[BLOCK_NB_SIZES];
65 int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
66 VLC hgain_vlc;
67
68 /* coded values in high bands */
69 int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
70 int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
71
72 /* there are two possible tables for spectral coefficients */
73 VLC coef_vlc[2];
74 uint16_t *run_table[2];
75 uint16_t *level_table[2];
76 /* frame info */
77 int frame_len; /* frame length in samples */
78 int frame_len_bits; /* frame_len = 1 << frame_len_bits */
79 int nb_block_sizes; /* number of block sizes */
80 /* block info */
81 int reset_block_lengths;
82 int block_len_bits; /* log2 of current block length */
83 int next_block_len_bits; /* log2 of next block length */
84 int prev_block_len_bits; /* log2 of prev block length */
85 int block_len; /* block length in samples */
86 int block_num; /* block number in current frame */
87 int block_pos; /* current position in frame */
88 uint8_t ms_stereo; /* true if mid/side stereo mode */
89 uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
90 float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE];
91 float max_exponent[MAX_CHANNELS];
92 int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
93 float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE];
94 MDCTContext mdct_ctx[BLOCK_NB_SIZES];
95 float *windows[BLOCK_NB_SIZES];
96 FFTSample mdct_tmp[BLOCK_MAX_SIZE]; /* temporary storage for imdct */
97 /* output buffer for one frame and the last for IMDCT windowing */
98 float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
99 /* last frame info */
100 uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
101 int last_bitoffset;
102 int last_superframe_len;
103 float noise_table[NOISE_TAB_SIZE];
104 int noise_index;
105 float noise_mult; /* XXX: suppress that and integrate it in the noise array */
106 /* lsp_to_curve tables */
107 float lsp_cos_table[BLOCK_MAX_SIZE];
108 float lsp_pow_e_table[256];
109 float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
110 float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
111 } WMADecodeContext;
112
113 typedef struct CoefVLCTable {
114 int n; /* total number of codes */
115 const uint32_t *huffcodes; /* VLC bit values */
116 const uint8_t *huffbits; /* VLC bit size */
117 const uint16_t *levels; /* table to build run/level tables */
118 } CoefVLCTable;
119
120 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
121
122 #include "wmadata.h"
123
124 #ifdef DEBUG_TRACE
125
126 int frame_count;
127
128 static FILE *flog;
129
130 void trace(const char *fmt, ...)
131 {
132 va_list ap;
133
134
135 if (!flog) {
136 flog = fopen("/tmp/out.log", "w");
137 setlinebuf(flog);
138 }
139
140 va_start(ap, fmt);
141 vfprintf(flog, fmt, ap);
142 va_end(ap);
143 }
144
145 #define get_bits(s, n) get_bits_trace(s, n)
146 #define get_vlc(s, vlc) get_vlc_trace(s, vlc)
147
148 unsigned int get_bits_trace(GetBitContext *s, int n)
149 {
150 unsigned int val;
151 val = (get_bits)(s, n);
152 trace("get_bits(%d) : 0x%x\n", n, val);
153 return val;
154 }
155
156 static int get_vlc_trace(GetBitContext *s, VLC *vlc)
157 {
158 int code;
159 code = (get_vlc)(s, vlc);
160 trace("get_vlc() : %d\n", code);
161 return code;
162 }
163
164 static void dump_shorts(const char *name, const short *tab, int n)
165 {
166 int i;
167
168 trace("%s[%d]:\n", name, n);
169 for(i=0;i<n;i++) {
170 if ((i & 7) == 0)
171 trace("%4d: ", i);
172 trace(" %5d.0", tab[i]);
173 if ((i & 7) == 7)
174 trace("\n");
175 }
176 }
177
178 static void dump_floats(const char *name, int prec, const float *tab, int n)
179 {
180 int i;
181
182 trace("%s[%d]:\n", name, n);
183 for(i=0;i<n;i++) {
184 if ((i & 7) == 0)
185 trace("%4d: ", i);
186 trace(" %8.*f", prec, tab[i]);
187 if ((i & 7) == 7)
188 trace("\n");
189 }
190 if ((i & 7) != 0)
191 trace("\n");
192 }
193
194 #else
195
196 #define trace(fmt, ...)
197
198 #endif
199
200 /* XXX: use same run/length optimization as mpeg decoders */
201 static void init_coef_vlc(VLC *vlc,
202 uint16_t **prun_table, uint16_t **plevel_table,
203 const CoefVLCTable *vlc_table)
204 {
205 int n = vlc_table->n;
206 const uint8_t *table_bits = vlc_table->huffbits;
207 const uint32_t *table_codes = vlc_table->huffcodes;
208 const uint16_t *levels_table = vlc_table->levels;
209 uint16_t *run_table, *level_table;
210 const uint16_t *p;
211 int i, l, j, level;
212
213 init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
214
215 run_table = malloc(n * sizeof(uint16_t));
216 level_table = malloc(n * sizeof(uint16_t));
217 p = levels_table;
218 i = 2;
219 level = 1;
220 while (i < n) {
221 l = *p++;
222 for(j=0;j<l;j++) {
223 run_table[i] = j;
224 level_table[i] = level;
225 i++;
226 }
227 level++;
228 }
229 *prun_table = run_table;
230 *plevel_table = level_table;
231 }
232
233 static int wma_decode_init(AVCodecContext * avctx)
234 {
235 WMADecodeContext *s = avctx->priv_data;
236 int i, flags1, flags2;
237 float *window;
238 uint8_t *extradata;
239 float bps1, high_freq, bps;
240 int sample_rate1;
241 int coef_vlc_table;
242
243 s->sample_rate = avctx->sample_rate;
244 s->nb_channels = avctx->channels;
245 s->bit_rate = avctx->bit_rate;
246 s->block_align = avctx->block_align;
247
248 if (avctx->codec_id == CODEC_ID_WMAV1) {
249 s->version = 1;
250 } else {
251 s->version = 2;
252 }
253
254 /* extract flag infos */
255 flags1 = 0;
256 flags2 = 0;
257 extradata = avctx->extradata;
258 if (s->version == 1 && avctx->extradata_size >= 4) {
259 flags1 = extradata[0] | (extradata[1] << 8);
260 flags2 = extradata[2] | (extradata[3] << 8);
261 } else if (s->version == 2 && avctx->extradata_size >= 6) {
262 flags1 = extradata[0] | (extradata[1] << 8) |
263 (extradata[2] << 16) | (extradata[3] << 24);
264 flags2 = extradata[4] | (extradata[5] << 8);
265 }
266 s->use_exp_vlc = flags2 & 0x0001;
267 s->use_bit_reservoir = flags2 & 0x0002;
268 s->use_variable_block_len = flags2 & 0x0004;
269
270 /* compute MDCT block size */
271 if (s->sample_rate <= 16000) {
272 s->frame_len_bits = 9;
273 } else if (s->sample_rate <= 32000) {
274 s->frame_len_bits = 10;
275 } else {
276 s->frame_len_bits = 11;
277 }
278 s->frame_len = 1 << s->frame_len_bits;
279 if (s->use_variable_block_len) {
280 s->nb_block_sizes = s->frame_len_bits - BLOCK_MIN_BITS + 1;
281 } else {
282 s->nb_block_sizes = 1;
283 }
284
285 /* init rate dependant parameters */
286 s->use_noise_coding = 1;
287 high_freq = s->sample_rate * 0.5;
288
289 /* if version 2, then the rates are normalized */
290 sample_rate1 = s->sample_rate;
291 if (s->version == 2) {
292 if (sample_rate1 >= 44100)
293 sample_rate1 = 44100;
294 else if (sample_rate1 >= 22050)
295 sample_rate1 = 22050;
296 else if (sample_rate1 >= 16000)
297 sample_rate1 = 16000;
298 else if (sample_rate1 >= 11025)
299 sample_rate1 = 11025;
300 else if (sample_rate1 >= 8000)
301 sample_rate1 = 8000;
302 }
303
304 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
305 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0)) + 2;
306
307 /* compute high frequency value and choose if noise coding should
308 be activated */
309 bps1 = bps;
310 if (s->nb_channels == 2)
311 bps1 = bps * 1.6;
312 if (sample_rate1 == 44100) {
313 if (bps1 >= 0.61)
314 s->use_noise_coding = 0;
315 else
316 high_freq = high_freq * 0.4;
317 } else if (sample_rate1 == 22050) {
318 if (bps1 >= 1.16)
319 s->use_noise_coding = 0;
320 else if (bps1 >= 0.72)
321 high_freq = high_freq * 0.7;
322 else
323 high_freq = high_freq * 0.6;
324 } else if (sample_rate1 == 16000) {
325 if (bps > 0.5)
326 high_freq = high_freq * 0.5;
327 else
328 high_freq = high_freq * 0.3;
329 } else if (sample_rate1 == 11025) {
330 high_freq = high_freq * 0.7;
331 } else if (sample_rate1 == 8000) {
332 if (bps <= 0.625) {
333 high_freq = high_freq * 0.5;
334 } else if (bps > 0.75) {
335 s->use_noise_coding = 0;
336 } else {
337 high_freq = high_freq * 0.65;
338 }
339 } else {
340 if (bps >= 0.8) {
341 high_freq = high_freq * 0.75;
342 } else if (bps >= 0.6) {
343 high_freq = high_freq * 0.6;
344 } else {
345 high_freq = high_freq * 0.5;
346 }
347 }
348 #ifdef DEBUG_PARAMS
349 printf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
350 printf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
351 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
352 s->block_align);
353 printf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
354 bps, bps1, high_freq, s->byte_offset_bits);
355 printf("use_noise_coding=%d use_exp_vlc=%d\n",
356 s->use_noise_coding, s->use_exp_vlc);
357 #endif
358
359 /* compute the scale factor band sizes for each MDCT block size */
360 {
361 int a, b, pos, lpos, k, block_len, i, j, n;
362 const uint8_t *table;
363
364 if (s->version == 1) {
365 s->coefs_start = 3;
366 } else {
367 s->coefs_start = 0;
368 }
369 for(k = 0; k < s->nb_block_sizes; k++) {
370 block_len = s->frame_len >> k;
371
372 if (s->version == 1) {
373 lpos = 0;
374 for(i=0;i<25;i++) {
375 a = wma_critical_freqs[i];
376 b = s->sample_rate;
377 pos = ((block_len * 2 * a) + (b >> 1)) / b;
378 if (pos > block_len)
379 pos = block_len;
380 s->exponent_bands[0][i] = pos - lpos;
381 if (pos >= block_len) {
382 i++;
383 break;
384 }
385 lpos = pos;
386 }
387 s->exponent_sizes[0] = i;
388 } else {
389 /* hardcoded tables */
390 table = NULL;
391 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
392 if (a < 3) {
393 if (s->sample_rate >= 44100)
394 table = exponent_band_44100[a];
395 else if (s->sample_rate >= 32000)
396 table = exponent_band_32000[a];
397 else if (s->sample_rate >= 22050)
398 table = exponent_band_22050[a];
399 }
400 if (table) {
401 n = *table++;
402 for(i=0;i<n;i++)
403 s->exponent_bands[k][i] = table[i];
404 s->exponent_sizes[k] = n;
405 } else {
406 j = 0;
407 lpos = 0;
408 for(i=0;i<25;i++) {
409 a = wma_critical_freqs[i];
410 b = s->sample_rate;
411 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
412 pos <<= 2;
413 if (pos > block_len)
414 pos = block_len;
415 if (pos > lpos)
416 s->exponent_bands[k][j++] = pos - lpos;
417 if (pos >= block_len)
418 break;
419 lpos = pos;
420 }
421 s->exponent_sizes[k] = j;
422 }
423 }
424
425 /* max number of coefs */
426 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
427 /* high freq computation */
428 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
429 s->sample_rate + 0.5);
430 n = s->exponent_sizes[k];
431 j = 0;
432 pos = 0;
433 for(i=0;i<n;i++) {
434 int start, end;
435 start = pos;
436 pos += s->exponent_bands[k][i];
437 end = pos;
438 if (start < s->high_band_start[k])
439 start = s->high_band_start[k];
440 if (end > s->coefs_end[k])
441 end = s->coefs_end[k];
442 if (end > start)
443 s->exponent_high_bands[k][j++] = end - start;
444 }
445 s->exponent_high_sizes[k] = j;
446 #if 0
447 trace("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
448 s->frame_len >> k,
449 s->coefs_end[k],
450 s->high_band_start[k],
451 s->exponent_high_sizes[k]);
452 for(j=0;j<s->exponent_high_sizes[k];j++)
453 trace(" %d", s->exponent_high_bands[k][j]);
454 trace("\n");
455 #endif
456 }
457 }
458
459 #ifdef DEBUG_TRACE
460 {
461 int i, j;
462 for(i = 0; i < s->nb_block_sizes; i++) {
463 trace("%5d: n=%2d:",
464 s->frame_len >> i,
465 s->exponent_sizes[i]);
466 for(j=0;j<s->exponent_sizes[i];j++)
467 trace(" %d", s->exponent_bands[i][j]);
468 trace("\n");
469 }
470 }
471 #endif
472
473 /* init MDCT */
474 for(i = 0; i < s->nb_block_sizes; i++)
475 mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
476
477 /* init MDCT windows : simple sinus window */
478 for(i = 0; i < s->nb_block_sizes; i++) {
479 int n, j;
480 float alpha;
481 n = 1 << (s->frame_len_bits - i);
482 window = av_malloc(sizeof(float) * n);
483 alpha = M_PI / (2.0 * n);
484 for(j=0;j<n;j++) {
485 window[n - j - 1] = sin((j + 0.5) * alpha);
486 }
487 s->windows[i] = window;
488 }
489
490 s->reset_block_lengths = 1;
491
492 if (s->use_noise_coding) {
493
494 /* init the noise generator */
495 if (s->use_exp_vlc)
496 s->noise_mult = 0.02;
497 else
498 s->noise_mult = 0.04;
499
500 #if defined(DEBUG_TRACE)
501 for(i=0;i<NOISE_TAB_SIZE;i++)
502 s->noise_table[i] = 1.0 * s->noise_mult;
503 #else
504 {
505 unsigned int seed;
506 float norm;
507 seed = 1;
508 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
509 for(i=0;i<NOISE_TAB_SIZE;i++) {
510 seed = seed * 314159 + 1;
511 s->noise_table[i] = (float)((int)seed) * norm;
512 }
513 }
514 #endif
515 init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits),
516 hgain_huffbits, 1, 1,
517 hgain_huffcodes, 2, 2);
518 }
519
520 if (s->use_exp_vlc) {
521 init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits),
522 scale_huffbits, 1, 1,
523 scale_huffcodes, 4, 4);
524 } else {
525 wma_lsp_to_curve_init(s, s->frame_len);
526 }
527
528 /* choose the VLC tables for the coefficients */
529 coef_vlc_table = 2;
530 if (s->sample_rate >= 32000) {
531 if (bps1 < 0.72)
532 coef_vlc_table = 0;
533 else if (bps1 < 1.16)
534 coef_vlc_table = 1;
535 }
536
537 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
538 &coef_vlcs[coef_vlc_table * 2]);
539 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
540 &coef_vlcs[coef_vlc_table * 2 + 1]);
541 return 0;
542 }
543
544 /* interpolate values for a bigger or smaller block. The block must
545 have multiple sizes */
546 static void interpolate_array(float *scale, int old_size, int new_size)
547 {
548 int i, j, jincr, k;
549 float v;
550
551 if (new_size > old_size) {
552 jincr = new_size / old_size;
553 j = new_size;
554 for(i = old_size - 1; i >=0; i--) {
555 v = scale[i];
556 k = jincr;
557 do {
558 scale[--j] = v;
559 } while (--k);
560 }
561 } else if (new_size < old_size) {
562 j = 0;
563 jincr = old_size / new_size;
564 for(i = 0; i < new_size; i++) {
565 scale[i] = scale[j];
566 j += jincr;
567 }
568 }
569 }
570
571 /* compute x^-0.25 with an exponent and mantissa table. We use linear
572 interpolation to reduce the mantissa table size at a small speed
573 expense (linear interpolation approximately doubles the number of
574 bits of precision). */
575 static inline float pow_m1_4(WMADecodeContext *s, float x)
576 {
577 union {
578 float f;
579 unsigned int v;
580 } u, t;
581 unsigned int e, m;
582 float a, b;
583
584 u.f = x;
585 e = u.v >> 23;
586 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
587 /* build interpolation scale: 1 <= t < 2. */
588 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
589 a = s->lsp_pow_m_table1[m];
590 b = s->lsp_pow_m_table2[m];
591 return s->lsp_pow_e_table[e] * (a + b * t.f);
592 }
593
594 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
595 {
596 float wdel, a, b;
597 int i, e, m;
598
599 wdel = M_PI / frame_len;
600 for(i=0;i<frame_len;i++)
601 s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
602
603 /* tables for x^-0.25 computation */
604 for(i=0;i<256;i++) {
605 e = i - 126;
606 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
607 }
608
609 /* NOTE: these two tables are needed to avoid two operations in
610 pow_m1_4 */
611 b = 1.0;
612 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
613 m = (1 << LSP_POW_BITS) + i;
614 a = (float)m * (0.5 / (1 << LSP_POW_BITS));
615 a = pow(a, -0.25);
616 s->lsp_pow_m_table1[i] = 2 * a - b;
617 s->lsp_pow_m_table2[i] = b - a;
618 b = a;
619 }
620 #if 0
621 for(i=1;i<20;i++) {
622 float v, r1, r2;
623 v = 5.0 / i;
624 r1 = pow_m1_4(s, v);
625 r2 = pow(v,-0.25);
626 printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1);
627 }
628 #endif
629 }
630
631 /* NOTE: We use the same code as Vorbis here */
632 /* XXX: optimize it further with SSE/3Dnow */
633 static void wma_lsp_to_curve(WMADecodeContext *s,
634 float *out, float *val_max_ptr,
635 int n, float *lsp)
636 {
637 int i, j;
638 float p, q, w, v, val_max;
639
640 val_max = 0;
641 for(i=0;i<n;i++) {
642 p = 0.5f;
643 q = 0.5f;
644 w = s->lsp_cos_table[i];
645 for(j=1;j<NB_LSP_COEFS;j+=2){
646 q *= w - lsp[j - 1];
647 p *= w - lsp[j];
648 }
649 p *= p * (2.0f - w);
650 q *= q * (2.0f + w);
651 v = p + q;
652 v = pow_m1_4(s, v);
653 if (v > val_max)
654 val_max = v;
655 out[i] = v;
656 }
657 *val_max_ptr = val_max;
658 }
659
660 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
661 static void decode_exp_lsp(WMADecodeContext *s, int ch)
662 {
663 float lsp_coefs[NB_LSP_COEFS];
664 int val, i;
665
666 for(i = 0; i < NB_LSP_COEFS; i++) {
667 if (i == 0 || i >= 8)
668 val = get_bits(&s->gb, 3);
669 else
670 val = get_bits(&s->gb, 4);
671 lsp_coefs[i] = lsp_codebook[i][val];
672 }
673
674 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
675 s->block_len, lsp_coefs);
676 }
677
678 /* decode exponents coded with VLC codes */
679 static int decode_exp_vlc(WMADecodeContext *s, int ch)
680 {
681 int last_exp, n, code;
682 const uint16_t *ptr, *band_ptr;
683 float v, *q, max_scale, *q_end;
684
685 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
686 ptr = band_ptr;
687 q = s->exponents[ch];
688 q_end = q + s->block_len;
689 max_scale = 0;
690 if (s->version == 1) {
691 last_exp = get_bits(&s->gb, 5) + 10;
692 /* XXX: use a table */
693 v = pow(10, last_exp * (1.0 / 16.0));
694 max_scale = v;
695 n = *ptr++;
696 do {
697 *q++ = v;
698 } while (--n);
699 }
700 last_exp = 36;
701 while (q < q_end) {
702 code = get_vlc(&s->gb, &s->exp_vlc);
703 if (code < 0)
704 return -1;
705 /* NOTE: this offset is the same as MPEG4 AAC ! */
706 last_exp += code - 60;
707 /* XXX: use a table */
708 v = pow(10, last_exp * (1.0 / 16.0));
709 if (v > max_scale)
710 max_scale = v;
711 n = *ptr++;
712 do {
713 *q++ = v;
714 } while (--n);
715 }
716 s->max_exponent[ch] = max_scale;
717 return 0;
718 }
719
720 /* return 0 if OK. return 1 if last block of frame. return -1 if
721 unrecorrable error. */
722 static int wma_decode_block(WMADecodeContext *s)
723 {
724 int n, v, a, ch, code, bsize;
725 int coef_nb_bits, total_gain, parse_exponents;
726 float window[BLOCK_MAX_SIZE * 2];
727 int nb_coefs[MAX_CHANNELS];
728 float mdct_norm;
729
730 trace("***decode_block: %d:%d\n", frame_count - 1, s->block_num);
731
732 /* compute current block length */
733 if (s->use_variable_block_len) {
734 n = av_log2(s->nb_block_sizes - 1) + 1;
735
736 if (s->reset_block_lengths) {
737 s->reset_block_lengths = 0;
738 v = get_bits(&s->gb, n);
739 if (v >= s->nb_block_sizes)
740 return -1;
741 s->prev_block_len_bits = s->frame_len_bits - v;
742 v = get_bits(&s->gb, n);
743 if (v >= s->nb_block_sizes)
744 return -1;
745 s->block_len_bits = s->frame_len_bits - v;
746 } else {
747 /* update block lengths */
748 s->prev_block_len_bits = s->block_len_bits;
749 s->block_len_bits = s->next_block_len_bits;
750 }
751 v = get_bits(&s->gb, n);
752 if (v >= s->nb_block_sizes)
753 return -1;
754 s->next_block_len_bits = s->frame_len_bits - v;
755 } else {
756 /* fixed block len */
757 s->next_block_len_bits = s->frame_len_bits;
758 s->prev_block_len_bits = s->frame_len_bits;
759 s->block_len_bits = s->frame_len_bits;
760 }
761
762 /* now check if the block length is coherent with the frame length */
763 s->block_len = 1 << s->block_len_bits;
764 if ((s->block_pos + s->block_len) > s->frame_len)
765 return -1;
766
767 if (s->nb_channels == 2) {
768 s->ms_stereo = get_bits(&s->gb, 1);
769 }
770 v = 0;
771 for(ch = 0; ch < s->nb_channels; ch++) {
772 a = get_bits(&s->gb, 1);
773 s->channel_coded[ch] = a;
774 v |= a;
775 }
776 /* if no channel coded, no need to go further */
777 /* XXX: fix potential framing problems */
778 if (!v)
779 goto next;
780
781 bsize = s->frame_len_bits - s->block_len_bits;
782
783 /* read total gain and extract corresponding number of bits for
784 coef escape coding */
785 total_gain = 1;
786 for(;;) {
787 a = get_bits(&s->gb, 7);
788 total_gain += a;
789 if (a != 127)
790 break;
791 }
792
793 if (total_gain < 15)
794 coef_nb_bits = 13;
795 else if (total_gain < 32)
796 coef_nb_bits = 12;
797 else if (total_gain < 40)
798 coef_nb_bits = 11;
799 else if (total_gain < 45)
800 coef_nb_bits = 10;
801 else
802 coef_nb_bits = 9;
803
804 /* compute number of coefficients */
805 n = s->coefs_end[bsize] - s->coefs_start;
806 for(ch = 0; ch < s->nb_channels; ch++)
807 nb_coefs[ch] = n;
808
809 /* complex coding */
810 if (s->use_noise_coding) {
811
812 for(ch = 0; ch < s->nb_channels; ch++) {
813 if (s->channel_coded[ch]) {
814 int i, n, a;
815 n = s->exponent_high_sizes[bsize];
816 for(i=0;i<n;i++) {
817 a = get_bits(&s->gb, 1);
818 s->high_band_coded[ch][i] = a;
819 /* if noise coding, the coefficients are not transmitted */
820 if (a)
821 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
822 }
823 }
824 }
825 for(ch = 0; ch < s->nb_channels; ch++) {
826 if (s->channel_coded[ch]) {
827 int i, n, val, code;
828
829 n = s->exponent_high_sizes[bsize];
830 val = (int)0x80000000;
831 for(i=0;i<n;i++) {
832 if (s->high_band_coded[ch][i]) {
833 if (val == (int)0x80000000) {
834 val = get_bits(&s->gb, 7) - 19;
835 } else {
836 code = get_vlc(&s->gb, &s->hgain_vlc);
837 if (code < 0)
838 return -1;
839 val += code - 18;
840 }
841 s->high_band_values[ch][i] = val;
842 }
843 }
844 }
845 }
846 }
847
848 /* exposant can be interpolated in short blocks. */
849 parse_exponents = 1;
850 if (s->block_len_bits != s->frame_len_bits) {
851 parse_exponents = get_bits(&s->gb, 1);
852 }
853
854 if (parse_exponents) {
855 for(ch = 0; ch < s->nb_channels; ch++) {
856 if (s->channel_coded[ch]) {
857 if (s->use_exp_vlc) {
858 if (decode_exp_vlc(s, ch) < 0)
859 return -1;
860 } else {
861 decode_exp_lsp(s, ch);
862 }
863 }
864 }
865 } else {
866 for(ch = 0; ch < s->nb_channels; ch++) {
867 if (s->channel_coded[ch]) {
868 interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
869 s->block_len);
870 }
871 }
872 }
873
874 /* parse spectral coefficients : just RLE encoding */
875 for(ch = 0; ch < s->nb_channels; ch++) {
876 if (s->channel_coded[ch]) {
877 VLC *coef_vlc;
878 int level, run, sign, tindex;
879 int16_t *ptr, *eptr;
880 const int16_t *level_table, *run_table;
881
882 /* special VLC tables are used for ms stereo because
883 there is potentially less energy there */
884 tindex = (ch == 1 && s->ms_stereo);
885 coef_vlc = &s->coef_vlc[tindex];
886 run_table = s->run_table[tindex];
887 level_table = s->level_table[tindex];
888 /* XXX: optimize */
889 ptr = &s->coefs1[ch][0];
890 eptr = ptr + nb_coefs[ch];
891 memset(ptr, 0, s->block_len * sizeof(int16_t));
892 for(;;) {
893 code = get_vlc(&s->gb, coef_vlc);
894 if (code < 0)
895 return -1;
896 if (code == 1) {
897 /* EOB */
898 break;
899 } else if (code == 0) {
900 /* escape */
901 level = get_bits(&s->gb, coef_nb_bits);
902 /* NOTE: this is rather suboptimal. reading
903 block_len_bits would be better */
904 run = get_bits(&s->gb, s->frame_len_bits);
905 } else {
906 /* normal code */
907 run = run_table[code];
908 level = level_table[code];
909 }
910 sign = get_bits(&s->gb, 1);
911 if (!sign)
912 level = -level;
913 ptr += run;
914 if (ptr >= eptr)
915 return -1;
916 *ptr++ = level;
917 /* NOTE: EOB can be omitted */
918 if (ptr >= eptr)
919 break;
920 }
921 }
922 if (s->version == 1 && s->nb_channels >= 2) {
923 align_get_bits(&s->gb);
924 }
925 }
926
927 /* normalize */
928 {
929 int n4 = s->block_len / 2;
930 mdct_norm = 1.0 / (float)n4;
931 if (s->version == 1) {
932 mdct_norm *= sqrt(n4);
933 }
934 }
935
936 /* finally compute the MDCT coefficients */
937 for(ch = 0; ch < s->nb_channels; ch++) {
938 if (s->channel_coded[ch]) {
939 int16_t *coefs1;
940 float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
941 int i, j, n, n1, last_high_band;
942 float exp_power[HIGH_BAND_MAX_SIZE];
943
944 coefs1 = s->coefs1[ch];
945 exponents = s->exponents[ch];
946 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
947 mult *= mdct_norm;
948 coefs = s->coefs[ch];
949 if (s->use_noise_coding) {
950 mult1 = mult;
951 /* very low freqs : noise */
952 for(i = 0;i < s->coefs_start; i++) {
953 *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
954 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
955 }
956
957 n1 = s->exponent_high_sizes[bsize];
958
959 /* compute power of high bands */
960 exp_ptr = exponents +
961 s->high_band_start[bsize] -
962 s->coefs_start;
963 last_high_band = 0; /* avoid warning */
964 for(j=0;j<n1;j++) {
965 n = s->exponent_high_bands[s->frame_len_bits -
966 s->block_len_bits][j];
967 if (s->high_band_coded[ch][j]) {
968 float e2, v;
969 e2 = 0;
970 for(i = 0;i < n; i++) {
971 v = exp_ptr[i];
972 e2 += v * v;
973 }
974 exp_power[j] = e2 / n;
975 last_high_band = j;
976 trace("%d: power=%f (%d)\n", j, exp_power[j], n);
977 }
978 exp_ptr += n;
979 }
980
981 /* main freqs and high freqs */
982 for(j=-1;j<n1;j++) {
983 if (j < 0) {
984 n = s->high_band_start[bsize] -
985 s->coefs_start;
986 } else {
987 n = s->exponent_high_bands[s->frame_len_bits -
988 s->block_len_bits][j];
989 }
990 if (j >= 0 && s->high_band_coded[ch][j]) {
991 /* use noise with specified power */
992 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
993 /* XXX: use a table */
994 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
995 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
996 mult1 *= mdct_norm;
997 for(i = 0;i < n; i++) {
998 noise = s->noise_table[s->noise_index];
999 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1000 *coefs++ = (*exponents++) * noise * mult1;
1001 }
1002 } else {
1003 /* coded values + small noise */
1004 for(i = 0;i < n; i++) {
1005 noise = s->noise_table[s->noise_index];
1006 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1007 *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
1008 }
1009 }
1010 }
1011
1012 /* very high freqs : noise */
1013 n = s->block_len - s->coefs_end[bsize];
1014 mult1 = mult * exponents[-1];
1015 for(i = 0; i < n; i++) {
1016 *coefs++ = s->noise_table[s->noise_index] * mult1;
1017 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1018 }
1019 } else {
1020 /* XXX: optimize more */
1021 for(i = 0;i < s->coefs_start; i++)
1022 *coefs++ = 0.0;
1023 n = nb_coefs[ch];
1024 for(i = 0;i < n; i++) {
1025 *coefs++ = coefs1[i] * exponents[i] * mult;
1026 }
1027 n = s->block_len - s->coefs_end[bsize];
1028 for(i = 0;i < n; i++)
1029 *coefs++ = 0.0;
1030 }
1031 }
1032 }
1033
1034 #ifdef DEBUG_TRACE
1035 for(ch = 0; ch < s->nb_channels; ch++) {
1036 if (s->channel_coded[ch]) {
1037 dump_floats("exponents", 3, s->exponents[ch], s->block_len);
1038 dump_floats("coefs", 1, s->coefs[ch], s->block_len);
1039 }
1040 }
1041 #endif
1042
1043 if (s->ms_stereo && s->channel_coded[1]) {
1044 float a, b;
1045 int i;
1046
1047 /* nominal case for ms stereo: we do it before mdct */
1048 /* no need to optimize this case because it should almost
1049 never happen */
1050 if (!s->channel_coded[0]) {
1051 #ifdef DEBUG_TRACE
1052 trace("rare ms-stereo case happened\n");
1053 #endif
1054 memset(s->coefs[0], 0, sizeof(float) * s->block_len);
1055 s->channel_coded[0] = 1;
1056 }
1057
1058 for(i = 0; i < s->block_len; i++) {
1059 a = s->coefs[0][i];
1060 b = s->coefs[1][i];
1061 s->coefs[0][i] = a + b;
1062 s->coefs[1][i] = a - b;
1063 }
1064 }
1065
1066 /* build the window : we ensure that when the windows overlap
1067 their squared sum is always 1 (MDCT reconstruction rule) */
1068 /* XXX: merge with output */
1069 {
1070 int i, next_block_len, block_len, prev_block_len, n;
1071 float *wptr;
1072
1073 block_len = s->block_len;
1074 prev_block_len = 1 << s->prev_block_len_bits;
1075 next_block_len = 1 << s->next_block_len_bits;
1076
1077 /* right part */
1078 wptr = window + block_len;
1079 if (block_len <= next_block_len) {
1080 for(i=0;i<block_len;i++)
1081 *wptr++ = s->windows[bsize][i];
1082 } else {
1083 /* overlap */
1084 n = (block_len / 2) - (next_block_len / 2);
1085 for(i=0;i<n;i++)
1086 *wptr++ = 1.0;
1087 for(i=0;i<next_block_len;i++)
1088 *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
1089 for(i=0;i<n;i++)
1090 *wptr++ = 0.0;
1091 }
1092
1093 /* left part */
1094 wptr = window + block_len;
1095 if (block_len <= prev_block_len) {
1096 for(i=0;i<block_len;i++)
1097 *--wptr = s->windows[bsize][i];
1098 } else {
1099 /* overlap */
1100 n = (block_len / 2) - (prev_block_len / 2);
1101 for(i=0;i<n;i++)
1102 *--wptr = 1.0;
1103 for(i=0;i<prev_block_len;i++)
1104 *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
1105 for(i=0;i<n;i++)
1106 *--wptr = 0.0;
1107 }
1108 }
1109
1110
1111 for(ch = 0; ch < s->nb_channels; ch++) {
1112 if (s->channel_coded[ch]) {
1113 FFTSample output[BLOCK_MAX_SIZE * 2];
1114 float *ptr;
1115 int i, n4, index, n;
1116
1117 n = s->block_len;
1118 n4 = s->block_len / 2;
1119 imdct_calc(&s->mdct_ctx[bsize],
1120 output, s->coefs[ch], s->mdct_tmp);
1121
1122 /* XXX: optimize all that by build the window and
1123 multipying/adding at the same time */
1124 /* multiply by the window */
1125 for(i=0;i<n * 2;i++) {
1126 output[i] *= window[i];
1127 }
1128
1129 /* add in the frame */
1130 index = (s->frame_len / 2) + s->block_pos - n4;
1131 ptr = &s->frame_out[ch][index];
1132 for(i=0;i<n * 2;i++) {
1133 *ptr += output[i];
1134 ptr++;
1135 }
1136
1137 /* specific fast case for ms-stereo : add to second
1138 channel if it is not coded */
1139 if (s->ms_stereo && !s->channel_coded[1]) {
1140 ptr = &s->frame_out[1][index];
1141 for(i=0;i<n * 2;i++) {
1142 *ptr += output[i];
1143 ptr++;
1144 }
1145 }
1146 }
1147 }
1148 next:
1149 /* update block number */
1150 s->block_num++;
1151 s->block_pos += s->block_len;
1152 if (s->block_pos >= s->frame_len)
1153 return 1;
1154 else
1155 return 0;
1156 }
1157
1158 /* decode a frame of frame_len samples */
1159 static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
1160 {
1161 int ret, i, n, a, ch, incr;
1162 int16_t *ptr;
1163 float *iptr;
1164
1165 trace("***decode_frame: %d size=%d\n", frame_count++, s->frame_len);
1166
1167 /* read each block */
1168 s->block_num = 0;
1169 s->block_pos = 0;
1170 for(;;) {
1171 ret = wma_decode_block(s);
1172 if (ret < 0)
1173 return -1;
1174 if (ret)
1175 break;
1176 }
1177
1178 /* convert frame to integer */
1179 n = s->frame_len;
1180 incr = s->nb_channels;
1181 for(ch = 0; ch < s->nb_channels; ch++) {
1182 ptr = samples + ch;
1183 iptr = s->frame_out[ch];
1184
1185 for(i=0;i<n;i++) {
1186 a = lrint(*iptr++);
1187 if (a > 32767)
1188 a = 32767;
1189 else if (a < -32768)
1190 a = -32768;
1191 *ptr = a;
1192 ptr += incr;
1193 }
1194 /* prepare for next block */
1195 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
1196 s->frame_len * sizeof(float));
1197 /* XXX: suppress this */
1198 memset(&s->frame_out[ch][s->frame_len], 0,
1199 s->frame_len * sizeof(float));
1200 }
1201
1202 #ifdef DEBUG_TRACE
1203 dump_shorts("samples", samples, n * s->nb_channels);
1204 #endif
1205 return 0;
1206 }
1207
1208 static int wma_decode_superframe(AVCodecContext *avctx,
1209 void *data, int *data_size,
1210 UINT8 *buf, int buf_size)
1211 {
1212 WMADecodeContext *s = avctx->priv_data;
1213 int nb_frames, bit_offset, i, pos, len;
1214 uint8_t *q;
1215 int16_t *samples;
1216
1217 trace("***decode_superframe:\n");
1218
1219 samples = data;
1220
1221 init_get_bits(&s->gb, buf, buf_size);
1222
1223 if (s->use_bit_reservoir) {
1224 /* read super frame header */
1225 get_bits(&s->gb, 4); /* super frame index */
1226 nb_frames = get_bits(&s->gb, 4) - 1;
1227
1228 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1229
1230 if (s->last_superframe_len > 0) {
1231 // printf("skip=%d\n", s->last_bitoffset);
1232 /* add bit_offset bits to last frame */
1233 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
1234 MAX_CODED_SUPERFRAME_SIZE)
1235 return -1;
1236 q = s->last_superframe + s->last_superframe_len;
1237 len = bit_offset;
1238 while (len > 0) {
1239 *q++ = (get_bits)(&s->gb, 8);
1240 len -= 8;
1241 }
1242 if (len > 0) {
1243 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1244 }
1245
1246 /* XXX: bit_offset bits into last frame */
1247 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE);
1248 /* skip unused bits */
1249 if (s->last_bitoffset > 0)
1250 skip_bits(&s->gb, s->last_bitoffset);
1251 /* this frame is stored in the last superframe and in the
1252 current one */
1253 if (wma_decode_frame(s, samples) < 0)
1254 return -1;
1255 samples += s->nb_channels * s->frame_len;
1256 }
1257
1258 /* read each frame starting from bit_offset */
1259 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1260 init_get_bits(&s->gb, buf + (pos >> 3), MAX_CODED_SUPERFRAME_SIZE - (pos >> 3));
1261 len = pos & 7;
1262 if (len > 0)
1263 skip_bits(&s->gb, len);
1264
1265 s->reset_block_lengths = 1;
1266 for(i=0;i<nb_frames;i++) {
1267 if (wma_decode_frame(s, samples) < 0)
1268 return -1;
1269 samples += s->nb_channels * s->frame_len;
1270 }
1271
1272 /* we copy the end of the frame in the last frame buffer */
1273 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1274 s->last_bitoffset = pos & 7;
1275 pos >>= 3;
1276 len = buf_size - pos;
1277 if (len > MAX_CODED_SUPERFRAME_SIZE) {
1278 return -1;
1279 }
1280 s->last_superframe_len = len;
1281 memcpy(s->last_superframe, buf + pos, len);
1282 } else {
1283 /* single frame decode */
1284 if (wma_decode_frame(s, samples) < 0)
1285 return -1;
1286 samples += s->nb_channels * s->frame_len;
1287 }
1288 *data_size = (int8_t *)samples - (int8_t *)data;
1289 return s->block_align;
1290 }
1291
1292 static int wma_decode_end(AVCodecContext *avctx)
1293 {
1294 WMADecodeContext *s = avctx->priv_data;
1295 int i;
1296
1297 for(i = 0; i < s->nb_block_sizes; i++)
1298 mdct_end(&s->mdct_ctx[i]);
1299 for(i = 0; i < s->nb_block_sizes; i++)
1300 av_free(s->windows[i]);
1301
1302 if (s->use_exp_vlc) {
1303 free_vlc(&s->exp_vlc);
1304 }
1305 if (s->use_noise_coding) {
1306 free_vlc(&s->hgain_vlc);
1307 }
1308 for(i = 0;i < 2; i++) {
1309 free_vlc(&s->coef_vlc[i]);
1310 av_free(s->run_table[i]);
1311 av_free(s->level_table[i]);
1312 }
1313
1314 return 0;
1315 }
1316
1317 AVCodec wmav1_decoder =
1318 {
1319 "wmav1",
1320 CODEC_TYPE_AUDIO,
1321 CODEC_ID_WMAV1,
1322 sizeof(WMADecodeContext),
1323 wma_decode_init,
1324 NULL,
1325 wma_decode_end,
1326 wma_decode_superframe,
1327 };
1328
1329 AVCodec wmav2_decoder =
1330 {
1331 "wmav2",
1332 CODEC_TYPE_AUDIO,
1333 CODEC_ID_WMAV2,
1334 sizeof(WMADecodeContext),
1335 wma_decode_init,
1336 NULL,
1337 wma_decode_end,
1338 wma_decode_superframe,
1339 };