r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / wmadec.c
blob9ea685af1b4635137bd440a9d3a44fc24a07454d
1 /*
2 * WMA compatible decoder
3 * Copyright (c) 2002 The FFmpeg Project.
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.
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.
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
20 /**
21 * @file wmadec.c
22 * WMA compatible decoder.
23 * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
24 * WMA v1 is identified by audio format 0x160 in Microsoft media files
25 * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161.
27 * To use this decoder, a calling application must supply the extra data
28 * bytes provided with the WMA data. These are the extra, codec-specific
29 * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes
30 * to the decoder using the extradata[_size] fields in AVCodecContext. There
31 * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data.
34 #include "avcodec.h"
35 #include "bitstream.h"
36 #include "dsputil.h"
38 /* size of blocks */
39 #define BLOCK_MIN_BITS 7
40 #define BLOCK_MAX_BITS 11
41 #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
43 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
45 /* XXX: find exact max size */
46 #define HIGH_BAND_MAX_SIZE 16
48 #define NB_LSP_COEFS 10
50 /* XXX: is it a suitable value ? */
51 #define MAX_CODED_SUPERFRAME_SIZE 16384
53 #define MAX_CHANNELS 2
55 #define NOISE_TAB_SIZE 8192
57 #define LSP_POW_BITS 7
59 typedef struct WMADecodeContext {
60 GetBitContext gb;
61 int sample_rate;
62 int nb_channels;
63 int bit_rate;
64 int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
65 int block_align;
66 int use_bit_reservoir;
67 int use_variable_block_len;
68 int use_exp_vlc; /* exponent coding: 0 = lsp, 1 = vlc + delta */
69 int use_noise_coding; /* true if perceptual noise is added */
70 int byte_offset_bits;
71 VLC exp_vlc;
72 int exponent_sizes[BLOCK_NB_SIZES];
73 uint16_t exponent_bands[BLOCK_NB_SIZES][25];
74 int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
75 int coefs_start; /* first coded coef */
76 int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
77 int exponent_high_sizes[BLOCK_NB_SIZES];
78 int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
79 VLC hgain_vlc;
81 /* coded values in high bands */
82 int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
83 int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
85 /* there are two possible tables for spectral coefficients */
86 VLC coef_vlc[2];
87 uint16_t *run_table[2];
88 uint16_t *level_table[2];
89 /* frame info */
90 int frame_len; /* frame length in samples */
91 int frame_len_bits; /* frame_len = 1 << frame_len_bits */
92 int nb_block_sizes; /* number of block sizes */
93 /* block info */
94 int reset_block_lengths;
95 int block_len_bits; /* log2 of current block length */
96 int next_block_len_bits; /* log2 of next block length */
97 int prev_block_len_bits; /* log2 of prev block length */
98 int block_len; /* block length in samples */
99 int block_num; /* block number in current frame */
100 int block_pos; /* current position in frame */
101 uint8_t ms_stereo; /* true if mid/side stereo mode */
102 uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
103 float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
104 float max_exponent[MAX_CHANNELS];
105 int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
106 float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
107 MDCTContext mdct_ctx[BLOCK_NB_SIZES];
108 float *windows[BLOCK_NB_SIZES];
109 FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */
110 /* output buffer for one frame and the last for IMDCT windowing */
111 float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
112 /* last frame info */
113 uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
114 int last_bitoffset;
115 int last_superframe_len;
116 float noise_table[NOISE_TAB_SIZE];
117 int noise_index;
118 float noise_mult; /* XXX: suppress that and integrate it in the noise array */
119 /* lsp_to_curve tables */
120 float lsp_cos_table[BLOCK_MAX_SIZE];
121 float lsp_pow_e_table[256];
122 float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
123 float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
125 #ifdef TRACE
126 int frame_count;
127 #endif
128 } WMADecodeContext;
130 typedef struct CoefVLCTable {
131 int n; /* total number of codes */
132 const uint32_t *huffcodes; /* VLC bit values */
133 const uint8_t *huffbits; /* VLC bit size */
134 const uint16_t *levels; /* table to build run/level tables */
135 } CoefVLCTable;
137 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
139 #include "wmadata.h"
141 #ifdef TRACE
142 static void dump_shorts(const char *name, const short *tab, int n)
144 int i;
146 tprintf("%s[%d]:\n", name, n);
147 for(i=0;i<n;i++) {
148 if ((i & 7) == 0)
149 tprintf("%4d: ", i);
150 tprintf(" %5d.0", tab[i]);
151 if ((i & 7) == 7)
152 tprintf("\n");
156 static void dump_floats(const char *name, int prec, const float *tab, int n)
158 int i;
160 tprintf("%s[%d]:\n", name, n);
161 for(i=0;i<n;i++) {
162 if ((i & 7) == 0)
163 tprintf("%4d: ", i);
164 tprintf(" %8.*f", prec, tab[i]);
165 if ((i & 7) == 7)
166 tprintf("\n");
168 if ((i & 7) != 0)
169 tprintf("\n");
171 #endif
173 /* XXX: use same run/length optimization as mpeg decoders */
174 static void init_coef_vlc(VLC *vlc,
175 uint16_t **prun_table, uint16_t **plevel_table,
176 const CoefVLCTable *vlc_table)
178 int n = vlc_table->n;
179 const uint8_t *table_bits = vlc_table->huffbits;
180 const uint32_t *table_codes = vlc_table->huffcodes;
181 const uint16_t *levels_table = vlc_table->levels;
182 uint16_t *run_table, *level_table;
183 const uint16_t *p;
184 int i, l, j, level;
186 init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4, 0);
188 run_table = av_malloc(n * sizeof(uint16_t));
189 level_table = av_malloc(n * sizeof(uint16_t));
190 p = levels_table;
191 i = 2;
192 level = 1;
193 while (i < n) {
194 l = *p++;
195 for(j=0;j<l;j++) {
196 run_table[i] = j;
197 level_table[i] = level;
198 i++;
200 level++;
202 *prun_table = run_table;
203 *plevel_table = level_table;
206 static int wma_decode_init(AVCodecContext * avctx)
208 WMADecodeContext *s = avctx->priv_data;
209 int i, flags1, flags2;
210 float *window;
211 uint8_t *extradata;
212 float bps1, high_freq;
213 volatile float bps;
214 int sample_rate1;
215 int coef_vlc_table;
217 s->sample_rate = avctx->sample_rate;
218 s->nb_channels = avctx->channels;
219 s->bit_rate = avctx->bit_rate;
220 s->block_align = avctx->block_align;
222 if (avctx->codec->id == CODEC_ID_WMAV1) {
223 s->version = 1;
224 } else {
225 s->version = 2;
228 /* extract flag infos */
229 flags1 = 0;
230 flags2 = 0;
231 extradata = avctx->extradata;
232 if (s->version == 1 && avctx->extradata_size >= 4) {
233 flags1 = extradata[0] | (extradata[1] << 8);
234 flags2 = extradata[2] | (extradata[3] << 8);
235 } else if (s->version == 2 && avctx->extradata_size >= 6) {
236 flags1 = extradata[0] | (extradata[1] << 8) |
237 (extradata[2] << 16) | (extradata[3] << 24);
238 flags2 = extradata[4] | (extradata[5] << 8);
240 s->use_exp_vlc = flags2 & 0x0001;
241 s->use_bit_reservoir = flags2 & 0x0002;
242 s->use_variable_block_len = flags2 & 0x0004;
244 /* compute MDCT block size */
245 if (s->sample_rate <= 16000) {
246 s->frame_len_bits = 9;
247 } else if (s->sample_rate <= 22050 ||
248 (s->sample_rate <= 32000 && s->version == 1)) {
249 s->frame_len_bits = 10;
250 } else {
251 s->frame_len_bits = 11;
253 s->frame_len = 1 << s->frame_len_bits;
254 if (s->use_variable_block_len) {
255 int nb_max, nb;
256 nb = ((flags2 >> 3) & 3) + 1;
257 if ((s->bit_rate / s->nb_channels) >= 32000)
258 nb += 2;
259 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
260 if (nb > nb_max)
261 nb = nb_max;
262 s->nb_block_sizes = nb + 1;
263 } else {
264 s->nb_block_sizes = 1;
267 /* init rate dependant parameters */
268 s->use_noise_coding = 1;
269 high_freq = s->sample_rate * 0.5;
271 /* if version 2, then the rates are normalized */
272 sample_rate1 = s->sample_rate;
273 if (s->version == 2) {
274 if (sample_rate1 >= 44100)
275 sample_rate1 = 44100;
276 else if (sample_rate1 >= 22050)
277 sample_rate1 = 22050;
278 else if (sample_rate1 >= 16000)
279 sample_rate1 = 16000;
280 else if (sample_rate1 >= 11025)
281 sample_rate1 = 11025;
282 else if (sample_rate1 >= 8000)
283 sample_rate1 = 8000;
286 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
287 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0)) + 2;
289 /* compute high frequency value and choose if noise coding should
290 be activated */
291 bps1 = bps;
292 if (s->nb_channels == 2)
293 bps1 = bps * 1.6;
294 if (sample_rate1 == 44100) {
295 if (bps1 >= 0.61)
296 s->use_noise_coding = 0;
297 else
298 high_freq = high_freq * 0.4;
299 } else if (sample_rate1 == 22050) {
300 if (bps1 >= 1.16)
301 s->use_noise_coding = 0;
302 else if (bps1 >= 0.72)
303 high_freq = high_freq * 0.7;
304 else
305 high_freq = high_freq * 0.6;
306 } else if (sample_rate1 == 16000) {
307 if (bps > 0.5)
308 high_freq = high_freq * 0.5;
309 else
310 high_freq = high_freq * 0.3;
311 } else if (sample_rate1 == 11025) {
312 high_freq = high_freq * 0.7;
313 } else if (sample_rate1 == 8000) {
314 if (bps <= 0.625) {
315 high_freq = high_freq * 0.5;
316 } else if (bps > 0.75) {
317 s->use_noise_coding = 0;
318 } else {
319 high_freq = high_freq * 0.65;
321 } else {
322 if (bps >= 0.8) {
323 high_freq = high_freq * 0.75;
324 } else if (bps >= 0.6) {
325 high_freq = high_freq * 0.6;
326 } else {
327 high_freq = high_freq * 0.5;
330 dprintf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
331 dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
332 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
333 s->block_align);
334 dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
335 bps, bps1, high_freq, s->byte_offset_bits);
336 dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
337 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
339 /* compute the scale factor band sizes for each MDCT block size */
341 int a, b, pos, lpos, k, block_len, i, j, n;
342 const uint8_t *table;
344 if (s->version == 1) {
345 s->coefs_start = 3;
346 } else {
347 s->coefs_start = 0;
349 for(k = 0; k < s->nb_block_sizes; k++) {
350 block_len = s->frame_len >> k;
352 if (s->version == 1) {
353 lpos = 0;
354 for(i=0;i<25;i++) {
355 a = wma_critical_freqs[i];
356 b = s->sample_rate;
357 pos = ((block_len * 2 * a) + (b >> 1)) / b;
358 if (pos > block_len)
359 pos = block_len;
360 s->exponent_bands[0][i] = pos - lpos;
361 if (pos >= block_len) {
362 i++;
363 break;
365 lpos = pos;
367 s->exponent_sizes[0] = i;
368 } else {
369 /* hardcoded tables */
370 table = NULL;
371 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
372 if (a < 3) {
373 if (s->sample_rate >= 44100)
374 table = exponent_band_44100[a];
375 else if (s->sample_rate >= 32000)
376 table = exponent_band_32000[a];
377 else if (s->sample_rate >= 22050)
378 table = exponent_band_22050[a];
380 if (table) {
381 n = *table++;
382 for(i=0;i<n;i++)
383 s->exponent_bands[k][i] = table[i];
384 s->exponent_sizes[k] = n;
385 } else {
386 j = 0;
387 lpos = 0;
388 for(i=0;i<25;i++) {
389 a = wma_critical_freqs[i];
390 b = s->sample_rate;
391 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
392 pos <<= 2;
393 if (pos > block_len)
394 pos = block_len;
395 if (pos > lpos)
396 s->exponent_bands[k][j++] = pos - lpos;
397 if (pos >= block_len)
398 break;
399 lpos = pos;
401 s->exponent_sizes[k] = j;
405 /* max number of coefs */
406 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
407 /* high freq computation */
408 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
409 s->sample_rate + 0.5);
410 n = s->exponent_sizes[k];
411 j = 0;
412 pos = 0;
413 for(i=0;i<n;i++) {
414 int start, end;
415 start = pos;
416 pos += s->exponent_bands[k][i];
417 end = pos;
418 if (start < s->high_band_start[k])
419 start = s->high_band_start[k];
420 if (end > s->coefs_end[k])
421 end = s->coefs_end[k];
422 if (end > start)
423 s->exponent_high_bands[k][j++] = end - start;
425 s->exponent_high_sizes[k] = j;
426 #if 0
427 tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
428 s->frame_len >> k,
429 s->coefs_end[k],
430 s->high_band_start[k],
431 s->exponent_high_sizes[k]);
432 for(j=0;j<s->exponent_high_sizes[k];j++)
433 tprintf(" %d", s->exponent_high_bands[k][j]);
434 tprintf("\n");
435 #endif
439 #ifdef TRACE
441 int i, j;
442 for(i = 0; i < s->nb_block_sizes; i++) {
443 tprintf("%5d: n=%2d:",
444 s->frame_len >> i,
445 s->exponent_sizes[i]);
446 for(j=0;j<s->exponent_sizes[i];j++)
447 tprintf(" %d", s->exponent_bands[i][j]);
448 tprintf("\n");
451 #endif
453 /* init MDCT */
454 for(i = 0; i < s->nb_block_sizes; i++)
455 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
457 /* init MDCT windows : simple sinus window */
458 for(i = 0; i < s->nb_block_sizes; i++) {
459 int n, j;
460 float alpha;
461 n = 1 << (s->frame_len_bits - i);
462 window = av_malloc(sizeof(float) * n);
463 alpha = M_PI / (2.0 * n);
464 for(j=0;j<n;j++) {
465 window[n - j - 1] = sin((j + 0.5) * alpha);
467 s->windows[i] = window;
470 s->reset_block_lengths = 1;
472 if (s->use_noise_coding) {
474 /* init the noise generator */
475 if (s->use_exp_vlc)
476 s->noise_mult = 0.02;
477 else
478 s->noise_mult = 0.04;
480 #ifdef TRACE
481 for(i=0;i<NOISE_TAB_SIZE;i++)
482 s->noise_table[i] = 1.0 * s->noise_mult;
483 #else
485 unsigned int seed;
486 float norm;
487 seed = 1;
488 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
489 for(i=0;i<NOISE_TAB_SIZE;i++) {
490 seed = seed * 314159 + 1;
491 s->noise_table[i] = (float)((int)seed) * norm;
494 #endif
495 init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits),
496 hgain_huffbits, 1, 1,
497 hgain_huffcodes, 2, 2, 0);
500 if (s->use_exp_vlc) {
501 init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits),
502 scale_huffbits, 1, 1,
503 scale_huffcodes, 4, 4, 0);
504 } else {
505 wma_lsp_to_curve_init(s, s->frame_len);
508 /* choose the VLC tables for the coefficients */
509 coef_vlc_table = 2;
510 if (s->sample_rate >= 32000) {
511 if (bps1 < 0.72)
512 coef_vlc_table = 0;
513 else if (bps1 < 1.16)
514 coef_vlc_table = 1;
517 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
518 &coef_vlcs[coef_vlc_table * 2]);
519 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
520 &coef_vlcs[coef_vlc_table * 2 + 1]);
521 return 0;
524 /* interpolate values for a bigger or smaller block. The block must
525 have multiple sizes */
526 static void interpolate_array(float *scale, int old_size, int new_size)
528 int i, j, jincr, k;
529 float v;
531 if (new_size > old_size) {
532 jincr = new_size / old_size;
533 j = new_size;
534 for(i = old_size - 1; i >=0; i--) {
535 v = scale[i];
536 k = jincr;
537 do {
538 scale[--j] = v;
539 } while (--k);
541 } else if (new_size < old_size) {
542 j = 0;
543 jincr = old_size / new_size;
544 for(i = 0; i < new_size; i++) {
545 scale[i] = scale[j];
546 j += jincr;
551 /* compute x^-0.25 with an exponent and mantissa table. We use linear
552 interpolation to reduce the mantissa table size at a small speed
553 expense (linear interpolation approximately doubles the number of
554 bits of precision). */
555 static inline float pow_m1_4(WMADecodeContext *s, float x)
557 union {
558 float f;
559 unsigned int v;
560 } u, t;
561 unsigned int e, m;
562 float a, b;
564 u.f = x;
565 e = u.v >> 23;
566 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
567 /* build interpolation scale: 1 <= t < 2. */
568 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
569 a = s->lsp_pow_m_table1[m];
570 b = s->lsp_pow_m_table2[m];
571 return s->lsp_pow_e_table[e] * (a + b * t.f);
574 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
576 float wdel, a, b;
577 int i, e, m;
579 wdel = M_PI / frame_len;
580 for(i=0;i<frame_len;i++)
581 s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
583 /* tables for x^-0.25 computation */
584 for(i=0;i<256;i++) {
585 e = i - 126;
586 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
589 /* NOTE: these two tables are needed to avoid two operations in
590 pow_m1_4 */
591 b = 1.0;
592 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
593 m = (1 << LSP_POW_BITS) + i;
594 a = (float)m * (0.5 / (1 << LSP_POW_BITS));
595 a = pow(a, -0.25);
596 s->lsp_pow_m_table1[i] = 2 * a - b;
597 s->lsp_pow_m_table2[i] = b - a;
598 b = a;
600 #if 0
601 for(i=1;i<20;i++) {
602 float v, r1, r2;
603 v = 5.0 / i;
604 r1 = pow_m1_4(s, v);
605 r2 = pow(v,-0.25);
606 printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1);
608 #endif
611 /* NOTE: We use the same code as Vorbis here */
612 /* XXX: optimize it further with SSE/3Dnow */
613 static void wma_lsp_to_curve(WMADecodeContext *s,
614 float *out, float *val_max_ptr,
615 int n, float *lsp)
617 int i, j;
618 float p, q, w, v, val_max;
620 val_max = 0;
621 for(i=0;i<n;i++) {
622 p = 0.5f;
623 q = 0.5f;
624 w = s->lsp_cos_table[i];
625 for(j=1;j<NB_LSP_COEFS;j+=2){
626 q *= w - lsp[j - 1];
627 p *= w - lsp[j];
629 p *= p * (2.0f - w);
630 q *= q * (2.0f + w);
631 v = p + q;
632 v = pow_m1_4(s, v);
633 if (v > val_max)
634 val_max = v;
635 out[i] = v;
637 *val_max_ptr = val_max;
640 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
641 static void decode_exp_lsp(WMADecodeContext *s, int ch)
643 float lsp_coefs[NB_LSP_COEFS];
644 int val, i;
646 for(i = 0; i < NB_LSP_COEFS; i++) {
647 if (i == 0 || i >= 8)
648 val = get_bits(&s->gb, 3);
649 else
650 val = get_bits(&s->gb, 4);
651 lsp_coefs[i] = lsp_codebook[i][val];
654 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
655 s->block_len, lsp_coefs);
658 /* decode exponents coded with VLC codes */
659 static int decode_exp_vlc(WMADecodeContext *s, int ch)
661 int last_exp, n, code;
662 const uint16_t *ptr, *band_ptr;
663 float v, *q, max_scale, *q_end;
665 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
666 ptr = band_ptr;
667 q = s->exponents[ch];
668 q_end = q + s->block_len;
669 max_scale = 0;
670 if (s->version == 1) {
671 last_exp = get_bits(&s->gb, 5) + 10;
672 /* XXX: use a table */
673 v = pow(10, last_exp * (1.0 / 16.0));
674 max_scale = v;
675 n = *ptr++;
676 do {
677 *q++ = v;
678 } while (--n);
680 last_exp = 36;
681 while (q < q_end) {
682 code = get_vlc(&s->gb, &s->exp_vlc);
683 if (code < 0)
684 return -1;
685 /* NOTE: this offset is the same as MPEG4 AAC ! */
686 last_exp += code - 60;
687 /* XXX: use a table */
688 v = pow(10, last_exp * (1.0 / 16.0));
689 if (v > max_scale)
690 max_scale = v;
691 n = *ptr++;
692 do {
693 *q++ = v;
694 } while (--n);
696 s->max_exponent[ch] = max_scale;
697 return 0;
700 /* return 0 if OK. return 1 if last block of frame. return -1 if
701 unrecorrable error. */
702 static int wma_decode_block(WMADecodeContext *s)
704 int n, v, a, ch, code, bsize;
705 int coef_nb_bits, total_gain, parse_exponents;
706 float window[BLOCK_MAX_SIZE * 2];
707 // XXX: FIXME!! there's a bug somewhere which makes this mandatory under altivec
708 #ifdef HAVE_ALTIVEC
709 volatile int nb_coefs[MAX_CHANNELS] __attribute__((aligned(16)));
710 #else
711 int nb_coefs[MAX_CHANNELS];
712 #endif
713 float mdct_norm;
715 #ifdef TRACE
716 tprintf("***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
717 #endif
719 /* compute current block length */
720 if (s->use_variable_block_len) {
721 n = av_log2(s->nb_block_sizes - 1) + 1;
723 if (s->reset_block_lengths) {
724 s->reset_block_lengths = 0;
725 v = get_bits(&s->gb, n);
726 if (v >= s->nb_block_sizes)
727 return -1;
728 s->prev_block_len_bits = s->frame_len_bits - v;
729 v = get_bits(&s->gb, n);
730 if (v >= s->nb_block_sizes)
731 return -1;
732 s->block_len_bits = s->frame_len_bits - v;
733 } else {
734 /* update block lengths */
735 s->prev_block_len_bits = s->block_len_bits;
736 s->block_len_bits = s->next_block_len_bits;
738 v = get_bits(&s->gb, n);
739 if (v >= s->nb_block_sizes)
740 return -1;
741 s->next_block_len_bits = s->frame_len_bits - v;
742 } else {
743 /* fixed block len */
744 s->next_block_len_bits = s->frame_len_bits;
745 s->prev_block_len_bits = s->frame_len_bits;
746 s->block_len_bits = s->frame_len_bits;
749 /* now check if the block length is coherent with the frame length */
750 s->block_len = 1 << s->block_len_bits;
751 if ((s->block_pos + s->block_len) > s->frame_len)
752 return -1;
754 if (s->nb_channels == 2) {
755 s->ms_stereo = get_bits(&s->gb, 1);
757 v = 0;
758 for(ch = 0; ch < s->nb_channels; ch++) {
759 a = get_bits(&s->gb, 1);
760 s->channel_coded[ch] = a;
761 v |= a;
763 /* if no channel coded, no need to go further */
764 /* XXX: fix potential framing problems */
765 if (!v)
766 goto next;
768 bsize = s->frame_len_bits - s->block_len_bits;
770 /* read total gain and extract corresponding number of bits for
771 coef escape coding */
772 total_gain = 1;
773 for(;;) {
774 a = get_bits(&s->gb, 7);
775 total_gain += a;
776 if (a != 127)
777 break;
780 if (total_gain < 15)
781 coef_nb_bits = 13;
782 else if (total_gain < 32)
783 coef_nb_bits = 12;
784 else if (total_gain < 40)
785 coef_nb_bits = 11;
786 else if (total_gain < 45)
787 coef_nb_bits = 10;
788 else
789 coef_nb_bits = 9;
791 /* compute number of coefficients */
792 n = s->coefs_end[bsize] - s->coefs_start;
793 for(ch = 0; ch < s->nb_channels; ch++)
794 nb_coefs[ch] = n;
796 /* complex coding */
797 if (s->use_noise_coding) {
799 for(ch = 0; ch < s->nb_channels; ch++) {
800 if (s->channel_coded[ch]) {
801 int i, n, a;
802 n = s->exponent_high_sizes[bsize];
803 for(i=0;i<n;i++) {
804 a = get_bits(&s->gb, 1);
805 s->high_band_coded[ch][i] = a;
806 /* if noise coding, the coefficients are not transmitted */
807 if (a)
808 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
812 for(ch = 0; ch < s->nb_channels; ch++) {
813 if (s->channel_coded[ch]) {
814 int i, n, val, code;
816 n = s->exponent_high_sizes[bsize];
817 val = (int)0x80000000;
818 for(i=0;i<n;i++) {
819 if (s->high_band_coded[ch][i]) {
820 if (val == (int)0x80000000) {
821 val = get_bits(&s->gb, 7) - 19;
822 } else {
823 code = get_vlc(&s->gb, &s->hgain_vlc);
824 if (code < 0)
825 return -1;
826 val += code - 18;
828 s->high_band_values[ch][i] = val;
835 /* exposant can be interpolated in short blocks. */
836 parse_exponents = 1;
837 if (s->block_len_bits != s->frame_len_bits) {
838 parse_exponents = get_bits(&s->gb, 1);
841 if (parse_exponents) {
842 for(ch = 0; ch < s->nb_channels; ch++) {
843 if (s->channel_coded[ch]) {
844 if (s->use_exp_vlc) {
845 if (decode_exp_vlc(s, ch) < 0)
846 return -1;
847 } else {
848 decode_exp_lsp(s, ch);
852 } else {
853 for(ch = 0; ch < s->nb_channels; ch++) {
854 if (s->channel_coded[ch]) {
855 interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
856 s->block_len);
861 /* parse spectral coefficients : just RLE encoding */
862 for(ch = 0; ch < s->nb_channels; ch++) {
863 if (s->channel_coded[ch]) {
864 VLC *coef_vlc;
865 int level, run, sign, tindex;
866 int16_t *ptr, *eptr;
867 const int16_t *level_table, *run_table;
869 /* special VLC tables are used for ms stereo because
870 there is potentially less energy there */
871 tindex = (ch == 1 && s->ms_stereo);
872 coef_vlc = &s->coef_vlc[tindex];
873 run_table = s->run_table[tindex];
874 level_table = s->level_table[tindex];
875 /* XXX: optimize */
876 ptr = &s->coefs1[ch][0];
877 eptr = ptr + nb_coefs[ch];
878 memset(ptr, 0, s->block_len * sizeof(int16_t));
879 for(;;) {
880 code = get_vlc(&s->gb, coef_vlc);
881 if (code < 0)
882 return -1;
883 if (code == 1) {
884 /* EOB */
885 break;
886 } else if (code == 0) {
887 /* escape */
888 level = get_bits(&s->gb, coef_nb_bits);
889 /* NOTE: this is rather suboptimal. reading
890 block_len_bits would be better */
891 run = get_bits(&s->gb, s->frame_len_bits);
892 } else {
893 /* normal code */
894 run = run_table[code];
895 level = level_table[code];
897 sign = get_bits(&s->gb, 1);
898 if (!sign)
899 level = -level;
900 ptr += run;
901 if (ptr >= eptr)
902 return -1;
903 *ptr++ = level;
904 /* NOTE: EOB can be omitted */
905 if (ptr >= eptr)
906 break;
909 if (s->version == 1 && s->nb_channels >= 2) {
910 align_get_bits(&s->gb);
914 /* normalize */
916 int n4 = s->block_len / 2;
917 mdct_norm = 1.0 / (float)n4;
918 if (s->version == 1) {
919 mdct_norm *= sqrt(n4);
923 /* finally compute the MDCT coefficients */
924 for(ch = 0; ch < s->nb_channels; ch++) {
925 if (s->channel_coded[ch]) {
926 int16_t *coefs1;
927 float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
928 int i, j, n, n1, last_high_band;
929 float exp_power[HIGH_BAND_MAX_SIZE];
931 coefs1 = s->coefs1[ch];
932 exponents = s->exponents[ch];
933 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
934 mult *= mdct_norm;
935 coefs = s->coefs[ch];
936 if (s->use_noise_coding) {
937 mult1 = mult;
938 /* very low freqs : noise */
939 for(i = 0;i < s->coefs_start; i++) {
940 *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
941 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
944 n1 = s->exponent_high_sizes[bsize];
946 /* compute power of high bands */
947 exp_ptr = exponents +
948 s->high_band_start[bsize] -
949 s->coefs_start;
950 last_high_band = 0; /* avoid warning */
951 for(j=0;j<n1;j++) {
952 n = s->exponent_high_bands[s->frame_len_bits -
953 s->block_len_bits][j];
954 if (s->high_band_coded[ch][j]) {
955 float e2, v;
956 e2 = 0;
957 for(i = 0;i < n; i++) {
958 v = exp_ptr[i];
959 e2 += v * v;
961 exp_power[j] = e2 / n;
962 last_high_band = j;
963 tprintf("%d: power=%f (%d)\n", j, exp_power[j], n);
965 exp_ptr += n;
968 /* main freqs and high freqs */
969 for(j=-1;j<n1;j++) {
970 if (j < 0) {
971 n = s->high_band_start[bsize] -
972 s->coefs_start;
973 } else {
974 n = s->exponent_high_bands[s->frame_len_bits -
975 s->block_len_bits][j];
977 if (j >= 0 && s->high_band_coded[ch][j]) {
978 /* use noise with specified power */
979 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
980 /* XXX: use a table */
981 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
982 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
983 mult1 *= mdct_norm;
984 for(i = 0;i < n; i++) {
985 noise = s->noise_table[s->noise_index];
986 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
987 *coefs++ = (*exponents++) * noise * mult1;
989 } else {
990 /* coded values + small noise */
991 for(i = 0;i < n; i++) {
992 noise = s->noise_table[s->noise_index];
993 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
994 *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
999 /* very high freqs : noise */
1000 n = s->block_len - s->coefs_end[bsize];
1001 mult1 = mult * exponents[-1];
1002 for(i = 0; i < n; i++) {
1003 *coefs++ = s->noise_table[s->noise_index] * mult1;
1004 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1006 } else {
1007 /* XXX: optimize more */
1008 for(i = 0;i < s->coefs_start; i++)
1009 *coefs++ = 0.0;
1010 n = nb_coefs[ch];
1011 for(i = 0;i < n; i++) {
1012 *coefs++ = coefs1[i] * exponents[i] * mult;
1014 n = s->block_len - s->coefs_end[bsize];
1015 for(i = 0;i < n; i++)
1016 *coefs++ = 0.0;
1021 #ifdef TRACE
1022 for(ch = 0; ch < s->nb_channels; ch++) {
1023 if (s->channel_coded[ch]) {
1024 dump_floats("exponents", 3, s->exponents[ch], s->block_len);
1025 dump_floats("coefs", 1, s->coefs[ch], s->block_len);
1028 #endif
1030 if (s->ms_stereo && s->channel_coded[1]) {
1031 float a, b;
1032 int i;
1034 /* nominal case for ms stereo: we do it before mdct */
1035 /* no need to optimize this case because it should almost
1036 never happen */
1037 if (!s->channel_coded[0]) {
1038 tprintf("rare ms-stereo case happened\n");
1039 memset(s->coefs[0], 0, sizeof(float) * s->block_len);
1040 s->channel_coded[0] = 1;
1043 for(i = 0; i < s->block_len; i++) {
1044 a = s->coefs[0][i];
1045 b = s->coefs[1][i];
1046 s->coefs[0][i] = a + b;
1047 s->coefs[1][i] = a - b;
1051 /* build the window : we ensure that when the windows overlap
1052 their squared sum is always 1 (MDCT reconstruction rule) */
1053 /* XXX: merge with output */
1055 int i, next_block_len, block_len, prev_block_len, n;
1056 float *wptr;
1058 block_len = s->block_len;
1059 prev_block_len = 1 << s->prev_block_len_bits;
1060 next_block_len = 1 << s->next_block_len_bits;
1062 /* right part */
1063 wptr = window + block_len;
1064 if (block_len <= next_block_len) {
1065 for(i=0;i<block_len;i++)
1066 *wptr++ = s->windows[bsize][i];
1067 } else {
1068 /* overlap */
1069 n = (block_len / 2) - (next_block_len / 2);
1070 for(i=0;i<n;i++)
1071 *wptr++ = 1.0;
1072 for(i=0;i<next_block_len;i++)
1073 *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
1074 for(i=0;i<n;i++)
1075 *wptr++ = 0.0;
1078 /* left part */
1079 wptr = window + block_len;
1080 if (block_len <= prev_block_len) {
1081 for(i=0;i<block_len;i++)
1082 *--wptr = s->windows[bsize][i];
1083 } else {
1084 /* overlap */
1085 n = (block_len / 2) - (prev_block_len / 2);
1086 for(i=0;i<n;i++)
1087 *--wptr = 1.0;
1088 for(i=0;i<prev_block_len;i++)
1089 *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
1090 for(i=0;i<n;i++)
1091 *--wptr = 0.0;
1096 for(ch = 0; ch < s->nb_channels; ch++) {
1097 if (s->channel_coded[ch]) {
1098 FFTSample output[BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
1099 float *ptr;
1100 int i, n4, index, n;
1102 n = s->block_len;
1103 n4 = s->block_len / 2;
1104 ff_imdct_calc(&s->mdct_ctx[bsize],
1105 output, s->coefs[ch], s->mdct_tmp);
1107 /* XXX: optimize all that by build the window and
1108 multipying/adding at the same time */
1109 /* multiply by the window */
1110 for(i=0;i<n * 2;i++) {
1111 output[i] *= window[i];
1114 /* add in the frame */
1115 index = (s->frame_len / 2) + s->block_pos - n4;
1116 ptr = &s->frame_out[ch][index];
1117 for(i=0;i<n * 2;i++) {
1118 *ptr += output[i];
1119 ptr++;
1122 /* specific fast case for ms-stereo : add to second
1123 channel if it is not coded */
1124 if (s->ms_stereo && !s->channel_coded[1]) {
1125 ptr = &s->frame_out[1][index];
1126 for(i=0;i<n * 2;i++) {
1127 *ptr += output[i];
1128 ptr++;
1133 next:
1134 /* update block number */
1135 s->block_num++;
1136 s->block_pos += s->block_len;
1137 if (s->block_pos >= s->frame_len)
1138 return 1;
1139 else
1140 return 0;
1143 /* decode a frame of frame_len samples */
1144 static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
1146 int ret, i, n, a, ch, incr;
1147 int16_t *ptr;
1148 float *iptr;
1150 #ifdef TRACE
1151 tprintf("***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
1152 #endif
1154 /* read each block */
1155 s->block_num = 0;
1156 s->block_pos = 0;
1157 for(;;) {
1158 ret = wma_decode_block(s);
1159 if (ret < 0)
1160 return -1;
1161 if (ret)
1162 break;
1165 /* convert frame to integer */
1166 n = s->frame_len;
1167 incr = s->nb_channels;
1168 for(ch = 0; ch < s->nb_channels; ch++) {
1169 ptr = samples + ch;
1170 iptr = s->frame_out[ch];
1172 for(i=0;i<n;i++) {
1173 a = lrintf(*iptr++);
1174 if (a > 32767)
1175 a = 32767;
1176 else if (a < -32768)
1177 a = -32768;
1178 *ptr = a;
1179 ptr += incr;
1181 /* prepare for next block */
1182 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
1183 s->frame_len * sizeof(float));
1184 /* XXX: suppress this */
1185 memset(&s->frame_out[ch][s->frame_len], 0,
1186 s->frame_len * sizeof(float));
1189 #ifdef TRACE
1190 dump_shorts("samples", samples, n * s->nb_channels);
1191 #endif
1192 return 0;
1195 static int wma_decode_superframe(AVCodecContext *avctx,
1196 void *data, int *data_size,
1197 uint8_t *buf, int buf_size)
1199 WMADecodeContext *s = avctx->priv_data;
1200 int nb_frames, bit_offset, i, pos, len;
1201 uint8_t *q;
1202 int16_t *samples;
1204 tprintf("***decode_superframe:\n");
1206 if(buf_size==0){
1207 s->last_superframe_len = 0;
1208 return 0;
1211 samples = data;
1213 init_get_bits(&s->gb, buf, buf_size*8);
1215 if (s->use_bit_reservoir) {
1216 /* read super frame header */
1217 get_bits(&s->gb, 4); /* super frame index */
1218 nb_frames = get_bits(&s->gb, 4) - 1;
1220 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1222 if (s->last_superframe_len > 0) {
1223 // printf("skip=%d\n", s->last_bitoffset);
1224 /* add bit_offset bits to last frame */
1225 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
1226 MAX_CODED_SUPERFRAME_SIZE)
1227 goto fail;
1228 q = s->last_superframe + s->last_superframe_len;
1229 len = bit_offset;
1230 while (len > 0) {
1231 *q++ = (get_bits)(&s->gb, 8);
1232 len -= 8;
1234 if (len > 0) {
1235 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1238 /* XXX: bit_offset bits into last frame */
1239 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1240 /* skip unused bits */
1241 if (s->last_bitoffset > 0)
1242 skip_bits(&s->gb, s->last_bitoffset);
1243 /* this frame is stored in the last superframe and in the
1244 current one */
1245 if (wma_decode_frame(s, samples) < 0)
1246 goto fail;
1247 samples += s->nb_channels * s->frame_len;
1250 /* read each frame starting from bit_offset */
1251 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1252 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1253 len = pos & 7;
1254 if (len > 0)
1255 skip_bits(&s->gb, len);
1257 s->reset_block_lengths = 1;
1258 for(i=0;i<nb_frames;i++) {
1259 if (wma_decode_frame(s, samples) < 0)
1260 goto fail;
1261 samples += s->nb_channels * s->frame_len;
1264 /* we copy the end of the frame in the last frame buffer */
1265 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1266 s->last_bitoffset = pos & 7;
1267 pos >>= 3;
1268 len = buf_size - pos;
1269 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
1270 goto fail;
1272 s->last_superframe_len = len;
1273 memcpy(s->last_superframe, buf + pos, len);
1274 } else {
1275 /* single frame decode */
1276 if (wma_decode_frame(s, samples) < 0)
1277 goto fail;
1278 samples += s->nb_channels * s->frame_len;
1280 *data_size = (int8_t *)samples - (int8_t *)data;
1281 return s->block_align;
1282 fail:
1283 /* when error, we reset the bit reservoir */
1284 s->last_superframe_len = 0;
1285 return -1;
1288 static int wma_decode_end(AVCodecContext *avctx)
1290 WMADecodeContext *s = avctx->priv_data;
1291 int i;
1293 for(i = 0; i < s->nb_block_sizes; i++)
1294 ff_mdct_end(&s->mdct_ctx[i]);
1295 for(i = 0; i < s->nb_block_sizes; i++)
1296 av_free(s->windows[i]);
1298 if (s->use_exp_vlc) {
1299 free_vlc(&s->exp_vlc);
1301 if (s->use_noise_coding) {
1302 free_vlc(&s->hgain_vlc);
1304 for(i = 0;i < 2; i++) {
1305 free_vlc(&s->coef_vlc[i]);
1306 av_free(s->run_table[i]);
1307 av_free(s->level_table[i]);
1310 return 0;
1313 AVCodec wmav1_decoder =
1315 "wmav1",
1316 CODEC_TYPE_AUDIO,
1317 CODEC_ID_WMAV1,
1318 sizeof(WMADecodeContext),
1319 wma_decode_init,
1320 NULL,
1321 wma_decode_end,
1322 wma_decode_superframe,
1325 AVCodec wmav2_decoder =
1327 "wmav2",
1328 CODEC_TYPE_AUDIO,
1329 CODEC_ID_WMAV2,
1330 sizeof(WMADecodeContext),
1331 wma_decode_init,
1332 NULL,
1333 wma_decode_end,
1334 wma_decode_superframe,