Fix a bug introduced in r27463 that caused the line spectral pair look up tables...
[kugel-rb.git] / apps / codecs / libwma / wmadeci.c
blobd7a836dd975a4f30295cbdd9ac6cb83133383d67
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.
25 #include <codecs.h>
26 #include <codecs/lib/codeclib.h>
27 #include <codecs/libasf/asf.h>
28 #include "wmadec.h"
29 #include "wmafixed.h"
30 #include "wmadata.h"
32 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
34 /*declarations of statically allocated variables used to remove malloc calls*/
36 static fixed32 coefsarray[MAX_CHANNELS][BLOCK_MAX_SIZE] IBSS_ATTR MEM_ALIGN_ATTR;
37 /*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
38 static fixed32 frame_out_buf[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] IBSS_ATTR_WMA_LARGE_IRAM MEM_ALIGN_ATTR;
40 /*MDCT reconstruction windows*/
41 static fixed32 stat0[2048] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
42 static fixed32 stat1[1024] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
43 static fixed32 stat2[ 512] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
44 static fixed32 stat3[ 256] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
45 static fixed32 stat4[ 128] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
47 /*VLC lookup tables*/
48 static uint16_t *runtabarray[2];
49 static uint16_t *levtabarray[2];
51 static uint16_t runtab_big[1336] MEM_ALIGN_ATTR;
52 static uint16_t runtab_small[1072] MEM_ALIGN_ATTR;
53 static uint16_t levtab_big[1336] MEM_ALIGN_ATTR;
54 static uint16_t levtab_small[1072] MEM_ALIGN_ATTR;
56 #define VLCBUF1SIZE 4598
57 #define VLCBUF2SIZE 3574
58 #define VLCBUF3SIZE 360
59 #define VLCBUF4SIZE 540
61 /*putting these in IRAM actually makes PP slower*/
63 static VLC_TYPE vlcbuf1[VLCBUF1SIZE][2] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
64 static VLC_TYPE vlcbuf2[VLCBUF2SIZE][2] MEM_ALIGN_ATTR;
65 /* This buffer gets reused for lsp tables */
66 static VLC_TYPE vlcbuf3[VLCBUF3SIZE][2] MEM_ALIGN_ATTR;
67 static VLC_TYPE vlcbuf4[VLCBUF4SIZE][2] MEM_ALIGN_ATTR;
72 /**
73 * Apply MDCT window and add into output.
75 * We ensure that when the windows overlap their squared sum
76 * is always 1 (MDCT reconstruction rule).
78 * The Vorbis I spec has a great diagram explaining this process.
79 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
81 static void wma_window(WMADecodeContext *s, fixed32 *in, fixed32 *out)
83 //float *in = s->output;
84 int block_len, bsize, n;
86 /* left part */
88 /* previous block was larger, so we'll use the size of the current
89 * block to set the window size*/
90 if (s->block_len_bits <= s->prev_block_len_bits) {
91 block_len = s->block_len;
92 bsize = s->frame_len_bits - s->block_len_bits;
94 vector_fmul_add_add(out, in, s->windows[bsize], block_len);
96 } else {
97 /*previous block was smaller or the same size, so use it's size to set the window length*/
98 block_len = 1 << s->prev_block_len_bits;
99 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
100 n = (s->block_len - block_len) / 2;
101 bsize = s->frame_len_bits - s->prev_block_len_bits;
103 vector_fmul_add_add(out+n, in+n, s->windows[bsize], block_len);
105 memcpy(out+n+block_len, in+n+block_len, n*sizeof(fixed32));
107 /* Advance to the end of the current block and prepare to window it for the next block.
108 * Since the window function needs to be reversed, we do it backwards starting with the
109 * last sample and moving towards the first
111 out += s->block_len;
112 in += s->block_len;
114 /* right part */
115 if (s->block_len_bits <= s->next_block_len_bits) {
116 block_len = s->block_len;
117 bsize = s->frame_len_bits - s->block_len_bits;
119 vector_fmul_reverse(out, in, s->windows[bsize], block_len);
121 } else {
122 block_len = 1 << s->next_block_len_bits;
123 n = (s->block_len - block_len) / 2;
124 bsize = s->frame_len_bits - s->next_block_len_bits;
126 memcpy(out, in, n*sizeof(fixed32));
128 vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
130 memset(out+n+block_len, 0, n*sizeof(fixed32));
137 /* XXX: use same run/length optimization as mpeg decoders */
138 static void init_coef_vlc(VLC *vlc,
139 uint16_t **prun_table, uint16_t **plevel_table,
140 const CoefVLCTable *vlc_table, int tab)
142 int n = vlc_table->n;
143 const uint8_t *table_bits = vlc_table->huffbits;
144 const uint32_t *table_codes = vlc_table->huffcodes;
145 const uint16_t *levels_table = vlc_table->levels;
146 uint16_t *run_table, *level_table;
147 const uint16_t *p;
148 int i, l, j, level;
151 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, INIT_VLC_USE_NEW_STATIC);
153 run_table = runtabarray[tab];
154 level_table= levtabarray[tab];
156 p = levels_table;
157 i = 2;
158 level = 1;
159 while (i < n)
161 l = *p++;
162 for(j=0;j<l;++j)
164 run_table[i] = j;
165 level_table[i] = level;
166 ++i;
168 ++level;
170 *prun_table = run_table;
171 *plevel_table = level_table;
174 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
177 int i, flags2;
178 fixed32 *window;
179 uint8_t *extradata;
180 fixed64 bps1;
181 fixed32 high_freq;
182 fixed64 bps;
183 int sample_rate1;
184 int coef_vlc_table;
185 // int filehandle;
186 #ifdef CPU_COLDFIRE
187 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
188 #endif
190 /*clear stereo setting to avoid glitches when switching stereo->mono*/
191 s->channel_coded[0]=0;
192 s->channel_coded[1]=0;
193 s->ms_stereo=0;
195 s->sample_rate = wfx->rate;
196 s->nb_channels = wfx->channels;
197 s->bit_rate = wfx->bitrate;
198 s->block_align = wfx->blockalign;
200 s->coefs = &coefsarray;
201 s->frame_out = &frame_out_buf;
203 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
204 s->version = 1;
205 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2 ) {
206 s->version = 2;
207 } else {
208 /*one of those other wma flavors that don't have GPLed decoders */
209 return -1;
212 /* extract flag infos */
213 flags2 = 0;
214 extradata = wfx->data;
215 if (s->version == 1 && wfx->datalen >= 4) {
216 flags2 = extradata[2] | (extradata[3] << 8);
217 }else if (s->version == 2 && wfx->datalen >= 6){
218 flags2 = extradata[4] | (extradata[5] << 8);
220 s->use_exp_vlc = flags2 & 0x0001;
221 s->use_bit_reservoir = flags2 & 0x0002;
222 s->use_variable_block_len = flags2 & 0x0004;
224 /* compute MDCT block size */
225 if (s->sample_rate <= 16000){
226 s->frame_len_bits = 9;
227 }else if (s->sample_rate <= 22050 ||
228 (s->sample_rate <= 32000 && s->version == 1)){
229 s->frame_len_bits = 10;
230 }else{
231 s->frame_len_bits = 11;
233 s->frame_len = 1 << s->frame_len_bits;
234 if (s-> use_variable_block_len)
236 int nb_max, nb;
237 nb = ((flags2 >> 3) & 3) + 1;
238 if ((s->bit_rate / s->nb_channels) >= 32000)
240 nb += 2;
242 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
243 if (nb > nb_max)
244 nb = nb_max;
245 s->nb_block_sizes = nb + 1;
247 else
249 s->nb_block_sizes = 1;
252 /* init rate dependant parameters */
253 s->use_noise_coding = 1;
254 high_freq = itofix64(s->sample_rate) >> 1;
257 /* if version 2, then the rates are normalized */
258 sample_rate1 = s->sample_rate;
259 if (s->version == 2)
261 if (sample_rate1 >= 44100)
262 sample_rate1 = 44100;
263 else if (sample_rate1 >= 22050)
264 sample_rate1 = 22050;
265 else if (sample_rate1 >= 16000)
266 sample_rate1 = 16000;
267 else if (sample_rate1 >= 11025)
268 sample_rate1 = 11025;
269 else if (sample_rate1 >= 8000)
270 sample_rate1 = 8000;
273 fixed64 tmp = itofix64(s->bit_rate);
274 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
275 bps = fixdiv64(tmp, tmp2);
276 fixed64 tim = bps * s->frame_len;
277 fixed64 tmpi = fixdiv64(tim,itofix64(8));
278 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
280 /* compute high frequency value and choose if noise coding should
281 be activated */
282 bps1 = bps;
283 if (s->nb_channels == 2)
284 bps1 = fixmul32(bps,0x1999a);
285 if (sample_rate1 == 44100)
287 if (bps1 >= 0x9c29)
288 s->use_noise_coding = 0;
289 else
290 high_freq = fixmul32(high_freq,0x6666);
292 else if (sample_rate1 == 22050)
294 if (bps1 >= 0x128f6)
295 s->use_noise_coding = 0;
296 else if (bps1 >= 0xb852)
297 high_freq = fixmul32(high_freq,0xb333);
298 else
299 high_freq = fixmul32(high_freq,0x999a);
301 else if (sample_rate1 == 16000)
303 if (bps > 0x8000)
304 high_freq = fixmul32(high_freq,0x8000);
305 else
306 high_freq = fixmul32(high_freq,0x4ccd);
308 else if (sample_rate1 == 11025)
310 high_freq = fixmul32(high_freq,0xb333);
312 else if (sample_rate1 == 8000)
314 if (bps <= 0xa000)
316 high_freq = fixmul32(high_freq,0x8000);
318 else if (bps > 0xc000)
320 s->use_noise_coding = 0;
322 else
324 high_freq = fixmul32(high_freq,0xa666);
327 else
329 if (bps >= 0xcccd)
331 high_freq = fixmul32(high_freq,0xc000);
333 else if (bps >= 0x999a)
335 high_freq = fixmul32(high_freq,0x999a);
337 else
339 high_freq = fixmul32(high_freq,0x8000);
343 /* compute the scale factor band sizes for each MDCT block size */
345 int a, b, pos, lpos, k, block_len, i, j, n;
346 const uint8_t *table;
348 if (s->version == 1)
350 s->coefs_start = 3;
352 else
354 s->coefs_start = 0;
356 for(k = 0; k < s->nb_block_sizes; ++k)
358 block_len = s->frame_len >> k;
360 if (s->version == 1)
362 lpos = 0;
363 for(i=0;i<25;++i)
365 a = wma_critical_freqs[i];
366 b = s->sample_rate;
367 pos = ((block_len * 2 * a) + (b >> 1)) / b;
368 if (pos > block_len)
369 pos = block_len;
370 s->exponent_bands[0][i] = pos - lpos;
371 if (pos >= block_len)
373 ++i;
374 break;
376 lpos = pos;
378 s->exponent_sizes[0] = i;
380 else
382 /* hardcoded tables */
383 table = NULL;
384 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
385 if (a < 3)
387 if (s->sample_rate >= 44100)
388 table = exponent_band_44100[a];
389 else if (s->sample_rate >= 32000)
390 table = exponent_band_32000[a];
391 else if (s->sample_rate >= 22050)
392 table = exponent_band_22050[a];
394 if (table)
396 n = *table++;
397 for(i=0;i<n;++i)
398 s->exponent_bands[k][i] = table[i];
399 s->exponent_sizes[k] = n;
401 else
403 j = 0;
404 lpos = 0;
405 for(i=0;i<25;++i)
407 a = wma_critical_freqs[i];
408 b = s->sample_rate;
409 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
410 pos <<= 2;
411 if (pos > block_len)
412 pos = block_len;
413 if (pos > lpos)
414 s->exponent_bands[k][j++] = pos - lpos;
415 if (pos >= block_len)
416 break;
417 lpos = pos;
419 s->exponent_sizes[k] = j;
423 /* max number of coefs */
424 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
425 /* high freq computation */
427 fixed32 tmp1 = high_freq*2; /* high_freq is a fixed32!*/
428 fixed32 tmp2=itofix32(s->sample_rate>>1);
429 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
432 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
433 s->sample_rate + 0.5);*/
435 n = s->exponent_sizes[k];
436 j = 0;
437 pos = 0;
438 for(i=0;i<n;++i)
440 int start, end;
441 start = pos;
442 pos += s->exponent_bands[k][i];
443 end = pos;
444 if (start < s->high_band_start[k])
445 start = s->high_band_start[k];
446 if (end > s->coefs_end[k])
447 end = s->coefs_end[k];
448 if (end > start)
449 s->exponent_high_bands[k][j++] = end - start;
451 s->exponent_high_sizes[k] = j;
455 /* ffmpeg uses malloc to only allocate as many window sizes as needed.
456 * However, we're really only interested in the worst case memory usage.
457 * In the worst case you can have 5 window sizes, 128 doubling up 2048
458 * Smaller windows are handled differently.
459 * Since we don't have malloc, just statically allocate this
461 fixed32 *temp[5];
462 temp[0] = stat0;
463 temp[1] = stat1;
464 temp[2] = stat2;
465 temp[3] = stat3;
466 temp[4] = stat4;
468 /* init MDCT windows : simple sinus window */
469 for(i = 0; i < s->nb_block_sizes; i++)
471 int n, j;
472 fixed32 alpha;
473 n = 1 << (s->frame_len_bits - i);
474 window = temp[i];
476 /* this calculates 0.5/(2*n) */
477 alpha = (1<<15)>>(s->frame_len_bits - i+1);
478 for(j=0;j<n;++j)
480 fixed32 j2 = itofix32(j) + 0x8000;
481 /*alpha between 0 and pi/2*/
482 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0);
484 s->windows[i] = window;
488 s->reset_block_lengths = 1;
490 if (s->use_noise_coding)
492 /* init the noise generator */
493 if (s->use_exp_vlc)
495 s->noise_mult = 0x51f;
496 s->noise_table = noisetable_exp;
498 else
500 s->noise_mult = 0xa3d;
501 /* LSP values are simply 2x the EXP values */
502 for (i=0;i<NOISE_TAB_SIZE;++i)
503 noisetable_exp[i] = noisetable_exp[i]<< 1;
504 s->noise_table = noisetable_exp;
506 #if 0
507 /* We use a lookup table computered in advance, so no need to do this*/
509 unsigned int seed;
510 fixed32 norm;
511 seed = 1;
512 norm = 0; // PJJ: near as makes any diff to 0!
513 for (i=0;i<NOISE_TAB_SIZE;++i)
515 seed = seed * 314159 + 1;
516 s->noise_table[i] = itofix32((int)seed) * norm;
519 #endif
521 s->hgain_vlc.table = vlcbuf4;
522 s->hgain_vlc.table_allocated = VLCBUF4SIZE;
523 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
524 hgain_huffbits, 1, 1,
525 hgain_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC);
528 if (s->use_exp_vlc)
531 s->exp_vlc.table = vlcbuf3;
532 s->exp_vlc.table_allocated = VLCBUF3SIZE;
534 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
535 scale_huffbits, 1, 1,
536 scale_huffcodes, 4, 4, INIT_VLC_USE_NEW_STATIC);
538 else
540 wma_lsp_to_curve_init(s, s->frame_len);
543 /* choose the VLC tables for the coefficients */
544 coef_vlc_table = 2;
545 if (s->sample_rate >= 32000)
547 if (bps1 < 0xb852)
548 coef_vlc_table = 0;
549 else if (bps1 < 0x128f6)
550 coef_vlc_table = 1;
553 /* since the coef2 table is the biggest and that has index 2 in coef_vlcs
554 it's safe to always assign like this */
555 runtabarray[0] = runtab_big; runtabarray[1] = runtab_small;
556 levtabarray[0] = levtab_big; levtabarray[1] = levtab_small;
558 s->coef_vlc[0].table = vlcbuf1;
559 s->coef_vlc[0].table_allocated = VLCBUF1SIZE;
560 s->coef_vlc[1].table = vlcbuf2;
561 s->coef_vlc[1].table_allocated = VLCBUF2SIZE;
564 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
565 &coef_vlcs[coef_vlc_table * 2], 0);
566 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
567 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
569 s->last_superframe_len = 0;
570 s->last_bitoffset = 0;
572 return 0;
576 /* compute x^-0.25 with an exponent and mantissa table. We use linear
577 interpolation to reduce the mantissa table size at a small speed
578 expense (linear interpolation approximately doubles the number of
579 bits of precision). */
580 static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
582 union {
583 float f;
584 unsigned int v;
585 } u, t;
586 unsigned int e, m;
587 fixed32 a, b;
589 u.f = fixtof64(x);
590 e = u.v >> 23;
591 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
592 /* build interpolation scale: 1 <= t < 2. */
593 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
594 a = ((fixed32*)s->lsp_pow_m_table1)[m];
595 b = ((fixed32*)s->lsp_pow_m_table2)[m];
597 /* lsp_pow_e_table contains 32.32 format */
598 /* TODO: Since we're unlikely have value that cover the whole
599 * IEEE754 range, we probably don't need to have all possible exponents */
601 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
604 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
606 fixed32 wdel, a, b, temp2;
607 int i;
609 wdel = fixdiv32(itofix32(1), itofix32(frame_len));
610 for (i=0; i<frame_len; ++i)
612 /* TODO: can probably reuse the trig_init values here */
613 fsincos((wdel*i)<<15, &temp2);
614 /* get 3 bits headroom + 1 bit from not doubleing the values */
615 s->lsp_cos_table[i] = temp2>>3;
618 /* NOTE: these two tables are needed to avoid two operations in
619 pow_m1_4 */
620 b = itofix32(1);
621 int ix = 0;
623 s->lsp_pow_m_table1 = &vlcbuf3[0];
624 s->lsp_pow_m_table2 = &vlcbuf3[1<<LSP_POW_BITS];
626 /*double check this later*/
627 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
629 a = pow_a_table[ix++]<<4;
630 ((fixed32*)s->lsp_pow_m_table1)[i] = 2 * a - b;
631 ((fixed32*)s->lsp_pow_m_table2)[i] = b - a;
632 b = a;
637 /* NOTE: We use the same code as Vorbis here */
638 /* XXX: optimize it further with SSE/3Dnow */
639 static void wma_lsp_to_curve(WMADecodeContext *s,
640 fixed32 *out,
641 fixed32 *val_max_ptr,
642 int n,
643 fixed32 *lsp)
645 int i, j;
646 fixed32 p, q, w, v, val_max, temp2;
648 val_max = 0;
649 for(i=0;i<n;++i)
651 /* shift by 2 now to reduce rounding error,
652 * we can renormalize right before pow_m1_4
655 p = 0x8000<<5;
656 q = 0x8000<<5;
657 w = s->lsp_cos_table[i];
659 for (j=1;j<NB_LSP_COEFS;j+=2)
661 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
662 temp2 = ((w - (lsp[j - 1]<<11)));
664 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
665 q = fixmul32b(q, temp2 )<<4;
666 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
669 /* 2 in 5.27 format is 0x10000000 */
670 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
671 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
673 v = (p + q) >>9; /* p/q end up as 16.16 */
674 v = pow_m1_4(s, v);
675 if (v > val_max)
676 val_max = v;
677 out[i] = v;
680 *val_max_ptr = val_max;
683 /* decode exponents coded with LSP coefficients (same idea as Vorbis)
684 * only used for low bitrate (< 16kbps) files
686 static void decode_exp_lsp(WMADecodeContext *s, int ch)
688 fixed32 lsp_coefs[NB_LSP_COEFS];
689 int val, i;
691 for (i = 0; i < NB_LSP_COEFS; ++i)
693 if (i == 0 || i >= 8)
694 val = get_bits(&s->gb, 3);
695 else
696 val = get_bits(&s->gb, 4);
697 lsp_coefs[i] = lsp_codebook[i][val];
700 wma_lsp_to_curve(s,
701 s->exponents[ch],
702 &s->max_exponent[ch],
703 s->block_len,
704 lsp_coefs);
707 /* decode exponents coded with VLC codes - used for bitrate >= 32kbps*/
708 static int decode_exp_vlc(WMADecodeContext *s, int ch)
710 int last_exp, n, code;
711 const uint16_t *ptr, *band_ptr;
712 fixed32 v, max_scale;
713 fixed32 *q,*q_end;
715 /*accommodate the 60 negative indices */
716 const fixed32 *pow_10_to_yover16_ptr = &pow_10_to_yover16[61];
718 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
719 ptr = band_ptr;
720 q = s->exponents[ch];
721 q_end = q + s->block_len;
722 max_scale = 0;
725 if (s->version == 1) //wmav1 only
727 last_exp = get_bits(&s->gb, 5) + 10;
729 v = pow_10_to_yover16_ptr[last_exp];
730 max_scale = v;
731 n = *ptr++;
732 switch (n & 3) do {
733 case 0: *q++ = v;
734 case 3: *q++ = v;
735 case 2: *q++ = v;
736 case 1: *q++ = v;
737 } while ((n -= 4) > 0);
738 } else {
739 last_exp = 36;
742 while (q < q_end)
744 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
745 if (code < 0)
747 return -1;
749 /* NOTE: this offset is the same as MPEG4 AAC ! */
750 last_exp += code - 60;
752 v = pow_10_to_yover16_ptr[last_exp];
753 if (v > max_scale)
755 max_scale = v;
757 n = *ptr++;
758 switch (n & 3) do {
759 case 0: *q++ = v;
760 case 3: *q++ = v;
761 case 2: *q++ = v;
762 case 1: *q++ = v;
763 } while ((n -= 4) > 0);
766 s->max_exponent[ch] = max_scale;
767 return 0;
770 /* return 0 if OK. return 1 if last block of frame. return -1 if
771 unrecorrable error. */
772 static int wma_decode_block(WMADecodeContext *s)
774 int n, v, a, ch, code, bsize;
775 int coef_nb_bits, total_gain;
776 int nb_coefs[MAX_CHANNELS];
777 fixed32 mdct_norm;
779 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
781 /* compute current block length */
782 if (s->use_variable_block_len)
784 n = av_log2(s->nb_block_sizes - 1) + 1;
786 if (s->reset_block_lengths)
788 s->reset_block_lengths = 0;
789 v = get_bits(&s->gb, n);
790 if (v >= s->nb_block_sizes)
792 return -2;
794 s->prev_block_len_bits = s->frame_len_bits - v;
795 v = get_bits(&s->gb, n);
796 if (v >= s->nb_block_sizes)
798 return -3;
800 s->block_len_bits = s->frame_len_bits - v;
802 else
804 /* update block lengths */
805 s->prev_block_len_bits = s->block_len_bits;
806 s->block_len_bits = s->next_block_len_bits;
808 v = get_bits(&s->gb, n);
810 if (v >= s->nb_block_sizes)
812 // rb->splash(HZ*4, "v was %d", v); //5, 7
813 return -4; //this is it
815 else{
816 //rb->splash(HZ, "passed v block (%d)!", v);
818 s->next_block_len_bits = s->frame_len_bits - v;
820 else
822 /* fixed block len */
823 s->next_block_len_bits = s->frame_len_bits;
824 s->prev_block_len_bits = s->frame_len_bits;
825 s->block_len_bits = s->frame_len_bits;
827 /* now check if the block length is coherent with the frame length */
828 s->block_len = 1 << s->block_len_bits;
830 if ((s->block_pos + s->block_len) > s->frame_len)
832 return -5; //oddly 32k sample from tracker fails here
835 if (s->nb_channels == 2)
837 s->ms_stereo = get_bits1(&s->gb);
839 v = 0;
840 for (ch = 0; ch < s->nb_channels; ++ch)
842 a = get_bits1(&s->gb);
843 s->channel_coded[ch] = a;
844 v |= a;
846 /* if no channel coded, no need to go further */
847 /* XXX: fix potential framing problems */
848 if (!v)
850 goto next;
853 bsize = s->frame_len_bits - s->block_len_bits;
855 /* read total gain and extract corresponding number of bits for
856 coef escape coding */
857 total_gain = 1;
858 for(;;)
860 a = get_bits(&s->gb, 7);
861 total_gain += a;
862 if (a != 127)
864 break;
868 if (total_gain < 15)
869 coef_nb_bits = 13;
870 else if (total_gain < 32)
871 coef_nb_bits = 12;
872 else if (total_gain < 40)
873 coef_nb_bits = 11;
874 else if (total_gain < 45)
875 coef_nb_bits = 10;
876 else
877 coef_nb_bits = 9;
879 /* compute number of coefficients */
880 n = s->coefs_end[bsize] - s->coefs_start;
882 for(ch = 0; ch < s->nb_channels; ++ch)
884 nb_coefs[ch] = n;
886 /* complex coding */
887 if (s->use_noise_coding)
890 for(ch = 0; ch < s->nb_channels; ++ch)
892 if (s->channel_coded[ch])
894 int i, n, a;
895 n = s->exponent_high_sizes[bsize];
896 for(i=0;i<n;++i)
898 a = get_bits1(&s->gb);
899 s->high_band_coded[ch][i] = a;
900 /* if noise coding, the coefficients are not transmitted */
901 if (a)
902 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
906 for(ch = 0; ch < s->nb_channels; ++ch)
908 if (s->channel_coded[ch])
910 int i, n, val, code;
912 n = s->exponent_high_sizes[bsize];
913 val = (int)0x80000000;
914 for(i=0;i<n;++i)
916 if (s->high_band_coded[ch][i])
918 if (val == (int)0x80000000)
920 val = get_bits(&s->gb, 7) - 19;
922 else
924 //code = get_vlc(&s->gb, &s->hgain_vlc);
925 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
926 if (code < 0)
928 return -6;
930 val += code - 18;
932 s->high_band_values[ch][i] = val;
939 /* exponents can be reused in short blocks. */
940 if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb))
942 for(ch = 0; ch < s->nb_channels; ++ch)
944 if (s->channel_coded[ch])
946 if (s->use_exp_vlc)
948 if (decode_exp_vlc(s, ch) < 0)
950 return -7;
953 else
955 decode_exp_lsp(s, ch);
957 s->exponents_bsize[ch] = bsize;
962 /* parse spectral coefficients : just RLE encoding */
963 for(ch = 0; ch < s->nb_channels; ++ch)
965 if (s->channel_coded[ch])
967 VLC *coef_vlc;
968 int level, run, sign, tindex;
969 int16_t *ptr, *eptr;
970 const int16_t *level_table, *run_table;
972 /* special VLC tables are used for ms stereo because
973 there is potentially less energy there */
974 tindex = (ch == 1 && s->ms_stereo);
975 coef_vlc = &s->coef_vlc[tindex];
976 run_table = s->run_table[tindex];
977 level_table = s->level_table[tindex];
978 /* XXX: optimize */
979 ptr = &s->coefs1[ch][0];
980 eptr = ptr + nb_coefs[ch];
981 memset(ptr, 0, s->block_len * sizeof(int16_t));
983 for(;;)
985 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
987 if (code < 0)
989 return -8;
991 if (code == 1)
993 /* EOB */
994 break;
996 else if (code == 0)
998 /* escape */
999 level = get_bits(&s->gb, coef_nb_bits);
1000 /* NOTE: this is rather suboptimal. reading
1001 block_len_bits would be better */
1002 run = get_bits(&s->gb, s->frame_len_bits);
1004 else
1006 /* normal code */
1007 run = run_table[code];
1008 level = level_table[code];
1010 sign = get_bits1(&s->gb);
1011 if (!sign)
1012 level = -level;
1013 ptr += run;
1014 if (ptr >= eptr)
1016 break;
1018 *ptr++ = level;
1021 /* NOTE: EOB can be omitted */
1022 if (ptr >= eptr)
1023 break;
1026 if (s->version == 1 && s->nb_channels >= 2)
1028 align_get_bits(&s->gb);
1033 int n4 = s->block_len >> 1;
1036 mdct_norm = 0x10000>>(s->block_len_bits-1);
1038 if (s->version == 1)
1040 mdct_norm *= fixtoi32(fixsqrt32(itofix32(n4)));
1045 /* finally compute the MDCT coefficients */
1046 for(ch = 0; ch < s->nb_channels; ++ch)
1048 if (s->channel_coded[ch])
1050 int16_t *coefs1;
1051 fixed32 *exponents;
1052 fixed32 *coefs, atemp;
1053 fixed64 mult;
1054 fixed64 mult1;
1055 fixed32 noise, temp1, temp2, mult2;
1056 int i, j, n, n1, last_high_band, esize;
1057 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
1059 //total_gain, coefs1, mdctnorm are lossless
1061 coefs1 = s->coefs1[ch];
1062 exponents = s->exponents[ch];
1063 esize = s->exponents_bsize[ch];
1064 coefs = (*(s->coefs))[ch];
1065 n=0;
1068 * The calculation of coefs has a shift right by 2 built in. This
1069 * prepares samples for the Tremor IMDCT which uses a slightly
1070 * different fixed format then the ffmpeg one. If the old ffmpeg
1071 * imdct is used, each shift storing into coefs should be reduced
1072 * by 1.
1073 * See SVN logs for details.
1077 if (s->use_noise_coding)
1079 /*This case is only used for low bitrates (typically less then 32kbps)*/
1081 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1083 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1084 mult = mult* mdct_norm;
1085 mult1 = mult;
1087 /* very low freqs : noise */
1088 for(i = 0;i < s->coefs_start; ++i)
1090 *coefs++ = fixmul32( (fixmul32(s->noise_table[s->noise_index],
1091 exponents[i<<bsize>>esize])>>4),Fixed32From64(mult1)) >>2;
1092 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1095 n1 = s->exponent_high_sizes[bsize];
1097 /* compute power of high bands */
1098 exponents = s->exponents[ch] +(s->high_band_start[bsize]<<bsize);
1099 last_high_band = 0; /* avoid warning */
1100 for (j=0;j<n1;++j)
1102 n = s->exponent_high_bands[s->frame_len_bits -
1103 s->block_len_bits][j];
1104 if (s->high_band_coded[ch][j])
1106 fixed32 e2, v;
1107 e2 = 0;
1108 for(i = 0;i < n; ++i)
1110 /*v is normalized later on so its fixed format is irrelevant*/
1111 v = exponents[i<<bsize>>esize]>>4;
1112 e2 += fixmul32(v, v)>>3;
1114 exp_power[j] = e2/n; /*n is an int...*/
1115 last_high_band = j;
1117 exponents += n<<bsize;
1120 /* main freqs and high freqs */
1121 exponents = s->exponents[ch] + (s->coefs_start<<bsize);
1122 for(j=-1;j<n1;++j)
1124 if (j < 0)
1126 n = s->high_band_start[bsize] -
1127 s->coefs_start;
1129 else
1131 n = s->exponent_high_bands[s->frame_len_bits -
1132 s->block_len_bits][j];
1134 if (j >= 0 && s->high_band_coded[ch][j])
1136 /* use noise with specified power */
1137 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1139 /*mult1 is 48.16, pow_table is 48.16*/
1140 mult1 = fixmul32(fixsqrt32(tmp),
1141 pow_table[s->high_band_values[ch][j]+20]) >> 16;
1143 /*this step has a fairly high degree of error for some reason*/
1144 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1145 mult1 = mult1*mdct_norm>>PRECISION;
1146 for(i = 0;i < n; ++i)
1148 noise = s->noise_table[s->noise_index];
1149 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1150 *coefs++ = fixmul32((fixmul32(exponents[i<<bsize>>esize],noise)>>4),
1151 Fixed32From64(mult1)) >>2;
1154 exponents += n<<bsize;
1156 else
1158 /* coded values + small noise */
1159 for(i = 0;i < n; ++i)
1161 noise = s->noise_table[s->noise_index];
1162 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1164 /*don't forget to renormalize the noise*/
1165 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1166 temp2 = fixmul32(exponents[i<<bsize>>esize], mult>>18);
1167 *coefs++ = fixmul32(temp1, temp2);
1169 exponents += n<<bsize;
1173 /* very high freqs : noise */
1174 n = s->block_len - s->coefs_end[bsize];
1175 mult2 = fixmul32(mult>>16,exponents[((-1<<bsize))>>esize]) ;
1176 for (i = 0; i < n; ++i)
1178 /*renormalize the noise product and then reduce to 14.18 precison*/
1179 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>6;
1181 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1184 else
1186 /*Noise coding not used, simply convert from exp to fixed representation*/
1188 fixed32 mult3 = (fixed32)(fixdiv64(pow_table[total_gain+20],
1189 Fixed32To64(s->max_exponent[ch])));
1190 mult3 = fixmul32(mult3, mdct_norm);
1192 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1193 for(i=0; i<s->coefs_start; i++)
1194 *coefs++=0;
1196 n = nb_coefs[ch];
1198 /* XXX: optimize more, unrolling this loop in asm
1199 might be a good idea */
1201 for(i = 0;i < n; ++i)
1203 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1204 atemp = (coefs1[i] * mult3)>>2;
1205 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1207 n = s->block_len - s->coefs_end[bsize];
1208 memset(coefs, 0, n*sizeof(fixed32));
1215 if (s->ms_stereo && s->channel_coded[1])
1217 fixed32 a, b;
1218 int i;
1219 fixed32 (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1221 /* nominal case for ms stereo: we do it before mdct */
1222 /* no need to optimize this case because it should almost
1223 never happen */
1224 if (!s->channel_coded[0])
1226 memset((*(s->coefs))[0], 0, sizeof(fixed32) * s->block_len);
1227 s->channel_coded[0] = 1;
1230 for(i = 0; i < s->block_len; ++i)
1232 a = (*coefs)[0][i];
1233 b = (*coefs)[1][i];
1234 (*coefs)[0][i] = a + b;
1235 (*coefs)[1][i] = a - b;
1239 for(ch = 0; ch < s->nb_channels; ++ch)
1241 /* BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2. */
1242 static uint32_t scratch_buf[BLOCK_MAX_SIZE * MAX_CHANNELS] IBSS_ATTR MEM_ALIGN_ATTR;
1243 if (s->channel_coded[ch])
1245 int n4, index;
1247 n4 = s->block_len >>1;
1249 ff_imdct_calc((s->frame_len_bits - bsize + 1),
1250 scratch_buf,
1251 (*(s->coefs))[ch]);
1253 /* add in the frame */
1254 index = (s->frame_len / 2) + s->block_pos - n4;
1255 wma_window(s, scratch_buf, &((*s->frame_out)[ch][index]));
1259 /* specific fast case for ms-stereo : add to second
1260 channel if it is not coded */
1261 if (s->ms_stereo && !s->channel_coded[1])
1263 wma_window(s, scratch_buf, &((*s->frame_out)[1][index]));
1267 next:
1268 /* update block number */
1269 ++s->block_num;
1270 s->block_pos += s->block_len;
1271 if (s->block_pos >= s->frame_len)
1273 return 1;
1275 else
1277 return 0;
1281 /* decode a frame of frame_len samples */
1282 static int wma_decode_frame(WMADecodeContext *s)
1284 int ret;
1286 /* read each block */
1287 s->block_num = 0;
1288 s->block_pos = 0;
1291 for(;;)
1293 ret = wma_decode_block(s);
1294 if (ret < 0)
1297 DEBUGF("wma_decode_block failed with code %d\n", ret);
1298 return -1;
1300 if (ret)
1302 break;
1306 return 0;
1309 /* Initialise the superframe decoding */
1311 int wma_decode_superframe_init(WMADecodeContext* s,
1312 const uint8_t *buf, /*input*/
1313 int buf_size)
1315 if (buf_size==0)
1317 s->last_superframe_len = 0;
1318 return 0;
1321 s->current_frame = 0;
1323 init_get_bits(&s->gb, buf, buf_size*8);
1325 if (s->use_bit_reservoir)
1327 /* read super frame header */
1328 skip_bits(&s->gb, 4); /* super frame index */
1329 s->nb_frames = get_bits(&s->gb, 4);
1331 if (s->last_superframe_len == 0)
1332 s->nb_frames --;
1333 else if (s->nb_frames == 0)
1334 s->nb_frames++;
1336 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1337 } else {
1338 s->nb_frames = 1;
1341 return 1;
1345 /* Decode a single frame in the current superframe - return -1 if
1346 there was a decoding error, or the number of samples decoded.
1349 int wma_decode_superframe_frame(WMADecodeContext* s,
1350 const uint8_t *buf, /*input*/
1351 int buf_size)
1353 int pos, len, ch;
1354 uint8_t *q;
1355 int done = 0;
1357 for(ch = 0; ch < s->nb_channels; ch++)
1358 memmove(&((*s->frame_out)[ch][0]),
1359 &((*s->frame_out)[ch][s->frame_len]),
1360 s->frame_len * sizeof(fixed32));
1362 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1364 if (s->last_superframe_len > 0)
1366 /* add s->bit_offset bits to last frame */
1367 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1368 MAX_CODED_SUPERFRAME_SIZE)
1370 DEBUGF("superframe size too large error\n");
1371 goto fail;
1373 q = s->last_superframe + s->last_superframe_len;
1374 len = s->bit_offset;
1375 while (len > 7)
1377 *q++ = (get_bits)(&s->gb, 8);
1378 len -= 8;
1380 if (len > 0)
1382 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1385 /* XXX: s->bit_offset bits into last frame */
1386 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1387 /* skip unused bits */
1388 if (s->last_bitoffset > 0)
1389 skip_bits(&s->gb, s->last_bitoffset);
1391 /* this frame is stored in the last superframe and in the
1392 current one */
1393 if (wma_decode_frame(s) < 0)
1395 goto fail;
1397 done = 1;
1400 /* read each frame starting from s->bit_offset */
1401 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1402 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1403 len = pos & 7;
1404 if (len > 0)
1405 skip_bits(&s->gb, len);
1407 s->reset_block_lengths = 1;
1410 /* If we haven't decoded a frame yet, do it now */
1411 if (!done)
1413 if (wma_decode_frame(s) < 0)
1415 goto fail;
1419 s->current_frame++;
1421 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1423 /* we copy the end of the frame in the last frame buffer */
1424 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1425 s->last_bitoffset = pos & 7;
1426 pos >>= 3;
1427 len = buf_size - pos;
1428 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1430 DEBUGF("superframe size too large error after decoding\n");
1431 goto fail;
1433 s->last_superframe_len = len;
1434 memcpy(s->last_superframe, buf + pos, len);
1437 return s->frame_len;
1439 fail:
1440 /* when error, we reset the bit reservoir */
1442 s->last_superframe_len = 0;
1443 return -1;