Clean up VLC functions.
[kugel-rb.git] / apps / codecs / libwma / wmadeci.c
blobb867e5c6f192eb2fade13f057a066fe32abc6659
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 "asf.h"
28 #include "wmadec.h"
29 #include "wmafixed.c"
30 #include "bitstream.h"
33 #define VLCBITS 7 /*7 is the lowest without glitching*/
34 #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
36 #define EXPVLCBITS 7
37 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
39 #define HGAINVLCBITS 9
40 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
43 #ifdef CPU_ARM
44 static inline
45 void CMUL(fixed32 *x, fixed32 *y,
46 fixed32 a, fixed32 b,
47 fixed32 t, fixed32 v)
49 /* This version loses one bit of precision. Could be solved at the cost
50 * of 2 extra cycles if it becomes an issue. */
51 int x1, y1, l;
52 asm(
53 "smull %[l], %[y1], %[b], %[t] \n"
54 "smlal %[l], %[y1], %[a], %[v] \n"
55 "rsb %[b], %[b], #0 \n"
56 "smull %[l], %[x1], %[a], %[t] \n"
57 "smlal %[l], %[x1], %[b], %[v] \n"
58 : [l] "=&r" (l), [x1]"=&r" (x1), [y1]"=&r" (y1), [b] "+r" (b)
59 : [a] "r" (a), [t] "r" (t), [v] "r" (v)
60 : "cc"
62 *x = x1 << 1;
63 *y = y1 << 1;
65 #elif defined CPU_COLDFIRE
66 static inline
67 void CMUL(fixed32 *x, fixed32 *y,
68 fixed32 a, fixed32 b,
69 fixed32 t, fixed32 v)
71 asm volatile ("mac.l %[a], %[t], %%acc0;"
72 "msac.l %[b], %[v], %%acc0;"
73 "mac.l %[b], %[t], %%acc1;"
74 "mac.l %[a], %[v], %%acc1;"
75 "movclr.l %%acc0, %[a];"
76 "move.l %[a], (%[x]);"
77 "movclr.l %%acc1, %[a];"
78 "move.l %[a], (%[y]);"
79 : [a] "+&r" (a)
80 : [x] "a" (x), [y] "a" (y),
81 [b] "r" (b), [t] "r" (t), [v] "r" (v)
82 : "cc", "memory");
84 #else
85 // PJJ : reinstate macro
86 void CMUL(fixed32 *pre,
87 fixed32 *pim,
88 fixed32 are,
89 fixed32 aim,
90 fixed32 bre,
91 fixed32 bim)
93 //int64_t x,y;
94 fixed32 _aref = are;
95 fixed32 _aimf = aim;
96 fixed32 _bref = bre;
97 fixed32 _bimf = bim;
98 fixed32 _r1 = fixmul32b(_bref, _aref);
99 fixed32 _r2 = fixmul32b(_bimf, _aimf);
100 fixed32 _r3 = fixmul32b(_bref, _aimf);
101 fixed32 _r4 = fixmul32b(_bimf, _aref);
102 *pre = _r1 - _r2;
103 *pim = _r3 + _r4;
106 #endif
108 typedef struct CoefVLCTable
110 int n; /* total number of codes */
111 const uint32_t *huffcodes; /* VLC bit values */
112 const uint8_t *huffbits; /* VLC bit size */
113 const uint16_t *levels; /* table to build run/level tables */
115 CoefVLCTable;
117 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
118 int fft_calc(FFTContext *s, FFTComplex *z);
121 fixed32 coefsarray[MAX_CHANNELS][BLOCK_MAX_SIZE] IBSS_ATTR;
123 //static variables that replace malloced stuff
124 fixed32 stat0[2048], stat1[1024], stat2[512], stat3[256], stat4[128]; //these are the MDCT reconstruction windows
126 fixed32 *tcosarray[5], *tsinarray[5];
127 fixed32 tcos0[1024], tcos1[512], tcos2[256], tcos3[128], tcos4[64]; //these are the sin and cos rotations used by the MDCT
128 fixed32 tsin0[1024], tsin1[512], tsin2[256], tsin3[128], tsin4[64];
130 FFTComplex *exparray[5]; //these are the fft lookup tables
132 uint16_t *revarray[5];
134 FFTComplex exptab0[512] IBSS_ATTR;
135 uint16_t revtab0[1024];
137 uint16_t *runtabarray[2], *levtabarray[2]; //these are VLC lookup tables
139 uint16_t runtab0[1336], runtab1[1336], levtab0[1336], levtab1[1336]; //these could be made smaller since only one can be 1336
141 FFTComplex mdct_tmp[1] ; /* dummy var */
144 /*putting these in IRAM actually makes PP slower*/
145 VLC_TYPE vlcbuf1[2550][2];
146 VLC_TYPE vlcbuf2[2550][2];
147 VLC_TYPE vlcbuf3[360][2];
148 VLC_TYPE vlcbuf4[540][2];
152 #include "wmadata.h" // PJJ
155 /* butter fly op */
156 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
158 fixed32 ax, ay, bx, by;\
159 bx=pre1;\
160 by=pim1;\
161 ax=qre1;\
162 ay=qim1;\
163 pre = (bx + ax);\
164 pim = (by + ay);\
165 qre = (bx - ax);\
166 qim = (by - ay);\
170 int fft_calc_unscaled(FFTContext *s, FFTComplex *z)
172 int ln = s->nbits;
173 int j, np, np2;
174 int nblocks, nloops;
175 register FFTComplex *p, *q;
176 // FFTComplex *exptab = s->exptab;
177 int l;
178 fixed32 tmp_re, tmp_im;
179 int tabshift = 10-ln;
181 np = 1 << ln;
184 /* pass 0 */
186 p=&z[0];
187 j=(np >> 1);
190 BF(p[0].re, p[0].im, p[1].re, p[1].im,
191 p[0].re, p[0].im, p[1].re, p[1].im);
192 p+=2;
194 while (--j != 0);
196 /* pass 1 */
199 p=&z[0];
200 j=np >> 2;
201 if (s->inverse)
205 BF(p[0].re, p[0].im, p[2].re, p[2].im,
206 p[0].re, p[0].im, p[2].re, p[2].im);
207 BF(p[1].re, p[1].im, p[3].re, p[3].im,
208 p[1].re, p[1].im, -p[3].im, p[3].re);
209 p+=4;
211 while (--j != 0);
213 else
217 BF(p[0].re, p[0].im, p[2].re, p[2].im,
218 p[0].re, p[0].im, p[2].re, p[2].im);
219 BF(p[1].re, p[1].im, p[3].re, p[3].im,
220 p[1].re, p[1].im, p[3].im, -p[3].re);
221 p+=4;
223 while (--j != 0);
225 /* pass 2 .. ln-1 */
227 nblocks = np >> 3;
228 nloops = 1 << 2;
229 np2 = np >> 1;
232 p = z;
233 q = z + nloops;
234 for (j = 0; j < nblocks; ++j)
236 BF(p->re, p->im, q->re, q->im,
237 p->re, p->im, q->re, q->im);
239 p++;
240 q++;
241 for(l = nblocks; l < np2; l += nblocks)
243 CMUL(&tmp_re, &tmp_im, exptab0[(l<<tabshift)].re, exptab0[(l<<tabshift)].im, q->re, q->im);
244 //CMUL(&tmp_re, &tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
245 BF(p->re, p->im, q->re, q->im,
246 p->re, p->im, tmp_re, tmp_im);
247 p++;
248 q++;
251 p += nloops;
252 q += nloops;
254 nblocks = nblocks >> 1;
255 nloops = nloops << 1;
257 while (nblocks != 0);
258 return 0;
262 * init MDCT or IMDCT computation.
264 int ff_mdct_init(MDCTContext *s, int nbits, int inverse)
266 int n, n4, i;
267 // fixed32 alpha;
270 memset(s, 0, sizeof(*s));
271 n = 1 << nbits; //nbits ranges from 12 to 8 inclusive
272 s->nbits = nbits;
273 s->n = n;
274 n4 = n >> 2;
275 s->tcos = tcosarray[12-nbits];
276 s->tsin = tsinarray[12-nbits];
277 for(i=0;i<n4;i++)
279 //fixed32 pi2 = fixmul32(0x20000, M_PI_F);
280 fixed32 ip = itofix32(i) + 0x2000;
281 ip = ip >> nbits;
282 //ip = fixdiv32(ip,itofix32(n)); // PJJ optimize
283 //alpha = fixmul32(TWO_M_PI_F, ip);
284 //s->tcos[i] = -fixcos32(alpha); //alpha between 0 and pi/2
285 //s->tsin[i] = -fixsin32(alpha);
287 s->tsin[i] = - fsincos(ip<<16, &(s->tcos[i])); //I can't remember why this works, but it seems to agree for ~24 bits, maybe more!
288 s->tcos[i] *=-1;
290 (&s->fft)->nbits = nbits-2;
292 (&s->fft)->inverse = inverse;
294 return 0;
299 * Compute inverse MDCT of size N = 2^nbits
300 * @param output N samples
301 * @param input N/2 samples
302 * @param tmp N/2 samples
304 void ff_imdct_calc(MDCTContext *s,
305 fixed32 *output,
306 fixed32 *input)
308 int k, n8, n4, n2, n, j,scale;
309 const fixed32 *tcos = s->tcos;
310 const fixed32 *tsin = s->tsin;
311 const fixed32 *in1, *in2;
312 FFTComplex *z1 = (FFTComplex *)output;
313 FFTComplex *z2 = (FFTComplex *)input;
314 int revtabshift = 12 - s->nbits;
316 n = 1 << s->nbits;
318 n2 = n >> 1;
319 n4 = n >> 2;
320 n8 = n >> 3;
323 /* pre rotation */
324 in1 = input;
325 in2 = input + n2 - 1;
327 for(k = 0; k < n4; k++)
329 j=revtab0[k<<revtabshift];
330 CMUL(&z1[j].re, &z1[j].im, *in2, *in1, tcos[k], tsin[k]);
331 in1 += 2;
332 in2 -= 2;
335 scale = fft_calc_unscaled(&s->fft, z1);
337 /* post rotation + reordering */
339 for(k = 0; k < n4; k++)
341 CMUL(&z2[k].re, &z2[k].im, (z1[k].re), (z1[k].im), tcos[k], tsin[k]);
344 for(k = 0; k < n8; k++)
346 fixed32 r1,r2,r3,r4,r1n,r2n,r3n;
348 r1 = z2[n8 + k].im;
349 r1n = r1 * -1;
350 r2 = z2[n8-1-k].re;
351 r2n = r2 * -1;
352 r3 = z2[k+n8].re;
353 r3n = r3 * -1;
354 r4 = z2[n8-k-1].im;
356 output[2*k] = r1n;
357 output[n2-1-2*k] = r1;
359 output[2*k+1] = r2;
360 output[n2-1-2*k-1] = r2n;
362 output[n2 + 2*k]= r3n;
363 output[n-1- 2*k]= r3n;
365 output[n2 + 2*k+1]= r4;
366 output[n-2 - 2 * k] = r4;
376 * Helper functions for wma_window.
381 static inline void vector_fmul_add_add(fixed32 *dst, const fixed32 *src0, const fixed32 *src1, int len){
382 int i;
383 for(i=0; i<len; i++)
384 dst[i] = fixmul32b(src0[i], src1[i]) + dst[i];
387 static inline void vector_fmul_reverse(fixed32 *dst, const fixed32 *src0, const fixed32 *src1, int len){
388 int i;
389 src1 += len-1;
390 for(i=0; i<len; i++)
391 dst[i] = fixmul32b(src0[i], src1[-i]);
395 * Apply MDCT window and add into output.
397 * We ensure that when the windows overlap their squared sum
398 * is always 1 (MDCT reconstruction rule).
400 static void wma_window(WMADecodeContext *s, fixed32 *in, fixed32 *out)
402 //float *in = s->output;
403 int block_len, bsize, n;
405 /* left part */
406 if (s->block_len_bits <= s->prev_block_len_bits) {
407 block_len = s->block_len;
408 bsize = s->frame_len_bits - s->block_len_bits;
410 vector_fmul_add_add(out, in, s->windows[bsize], block_len);
412 } else {
413 block_len = 1 << s->prev_block_len_bits;
414 n = (s->block_len - block_len) / 2;
415 bsize = s->frame_len_bits - s->prev_block_len_bits;
417 vector_fmul_add_add(out+n, in+n, s->windows[bsize], block_len);
419 memcpy(out+n+block_len, in+n+block_len, n*sizeof(fixed32));
422 out += s->block_len;
423 in += s->block_len;
425 /* right part */
426 if (s->block_len_bits <= s->next_block_len_bits) {
427 block_len = s->block_len;
428 bsize = s->frame_len_bits - s->block_len_bits;
430 vector_fmul_reverse(out, in, s->windows[bsize], block_len);
432 } else {
433 block_len = 1 << s->next_block_len_bits;
434 n = (s->block_len - block_len) / 2;
435 bsize = s->frame_len_bits - s->next_block_len_bits;
437 memcpy(out, in, n*sizeof(fixed32));
439 vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
441 memset(out+n+block_len, 0, n*sizeof(fixed32));
448 /* XXX: use same run/length optimization as mpeg decoders */
449 static void init_coef_vlc(VLC *vlc,
450 uint16_t **prun_table, uint16_t **plevel_table,
451 const CoefVLCTable *vlc_table, int tab)
453 int n = vlc_table->n;
454 const uint8_t *table_bits = vlc_table->huffbits;
455 const uint32_t *table_codes = vlc_table->huffcodes;
456 const uint16_t *levels_table = vlc_table->levels;
457 uint16_t *run_table, *level_table;
458 const uint16_t *p;
459 int i, l, j, level;
462 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
464 run_table = runtabarray[tab];
465 level_table= levtabarray[tab];
467 p = levels_table;
468 i = 2;
469 level = 1;
470 while (i < n)
472 l = *p++;
473 for(j=0;j<l;++j)
475 run_table[i] = j;
476 level_table[i] = level;
477 ++i;
479 ++level;
481 *prun_table = run_table;
482 *plevel_table = level_table;
485 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
487 //WMADecodeContext *s = avctx->priv_data;
488 int i, m, j, flags1, flags2;
489 fixed32 *window;
490 uint8_t *extradata;
491 fixed64 bps1;
492 fixed32 high_freq;
493 fixed64 bps;
494 int sample_rate1;
495 int coef_vlc_table;
496 // int filehandle;
497 #ifdef CPU_COLDFIRE
498 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
499 #endif
501 s->sample_rate = wfx->rate;
502 s->nb_channels = wfx->channels;
503 s->bit_rate = wfx->bitrate;
504 s->block_align = wfx->blockalign;
506 s->coefs = &coefsarray;
508 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
509 s->version = 1;
510 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2 ) {
511 s->version = 2;
512 } else {
513 /*one of those other wma flavors that don't have GPLed decoders */
514 return -1;
517 /* extract flag infos */
518 flags1 = 0;
519 flags2 = 0;
520 extradata = wfx->data;
521 if (s->version == 1 && wfx->datalen >= 4) {
522 flags1 = extradata[0] | (extradata[1] << 8);
523 flags2 = extradata[2] | (extradata[3] << 8);
524 }else if (s->version == 2 && wfx->datalen >= 6){
525 flags1 = extradata[0] | (extradata[1] << 8) |
526 (extradata[2] << 16) | (extradata[3] << 24);
527 flags2 = extradata[4] | (extradata[5] << 8);
529 s->use_exp_vlc = flags2 & 0x0001;
530 s->use_bit_reservoir = flags2 & 0x0002;
531 s->use_variable_block_len = flags2 & 0x0004;
533 /* compute MDCT block size */
534 if (s->sample_rate <= 16000){
535 s->frame_len_bits = 9;
536 }else if (s->sample_rate <= 22050 ||
537 (s->sample_rate <= 32000 && s->version == 1)){
538 s->frame_len_bits = 10;
539 }else{
540 s->frame_len_bits = 11;
542 s->frame_len = 1 << s->frame_len_bits;
543 if (s-> use_variable_block_len)
545 int nb_max, nb;
546 nb = ((flags2 >> 3) & 3) + 1;
547 if ((s->bit_rate / s->nb_channels) >= 32000)
549 nb += 2;
551 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
552 if (nb > nb_max)
553 nb = nb_max;
554 s->nb_block_sizes = nb + 1;
556 else
558 s->nb_block_sizes = 1;
561 /* init rate dependant parameters */
562 s->use_noise_coding = 1;
563 high_freq = itofix64(s->sample_rate) >> 1;
566 /* if version 2, then the rates are normalized */
567 sample_rate1 = s->sample_rate;
568 if (s->version == 2)
570 if (sample_rate1 >= 44100)
571 sample_rate1 = 44100;
572 else if (sample_rate1 >= 22050)
573 sample_rate1 = 22050;
574 else if (sample_rate1 >= 16000)
575 sample_rate1 = 16000;
576 else if (sample_rate1 >= 11025)
577 sample_rate1 = 11025;
578 else if (sample_rate1 >= 8000)
579 sample_rate1 = 8000;
582 fixed64 tmp = itofix64(s->bit_rate);
583 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
584 bps = fixdiv64(tmp, tmp2);
585 fixed64 tim = bps * s->frame_len;
586 fixed64 tmpi = fixdiv64(tim,itofix64(8));
587 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
589 /* compute high frequency value and choose if noise coding should
590 be activated */
591 bps1 = bps;
592 if (s->nb_channels == 2)
593 bps1 = fixmul32(bps,0x1999a);
594 if (sample_rate1 == 44100)
596 if (bps1 >= 0x9c29)
597 s->use_noise_coding = 0;
598 else
599 high_freq = fixmul32(high_freq,0x6666);
601 else if (sample_rate1 == 22050)
603 if (bps1 >= 0x128f6)
604 s->use_noise_coding = 0;
605 else if (bps1 >= 0xb852)
606 high_freq = fixmul32(high_freq,0xb333);
607 else
608 high_freq = fixmul32(high_freq,0x999a);
610 else if (sample_rate1 == 16000)
612 if (bps > 0x8000)
613 high_freq = fixmul32(high_freq,0x8000);
614 else
615 high_freq = fixmul32(high_freq,0x4ccd);
617 else if (sample_rate1 == 11025)
619 high_freq = fixmul32(high_freq,0xb333);
621 else if (sample_rate1 == 8000)
623 if (bps <= 0xa000)
625 high_freq = fixmul32(high_freq,0x8000);
627 else if (bps > 0xc000)
629 s->use_noise_coding = 0;
631 else
633 high_freq = fixmul32(high_freq,0xa666);
636 else
638 if (bps >= 0xcccd)
640 high_freq = fixmul32(high_freq,0xc000);
642 else if (bps >= 0x999a)
644 high_freq = fixmul32(high_freq,0x999a);
646 else
648 high_freq = fixmul32(high_freq,0x8000);
652 /* compute the scale factor band sizes for each MDCT block size */
654 int a, b, pos, lpos, k, block_len, i, j, n;
655 const uint8_t *table;
657 if (s->version == 1)
659 s->coefs_start = 3;
661 else
663 s->coefs_start = 0;
665 for(k = 0; k < s->nb_block_sizes; ++k)
667 block_len = s->frame_len >> k;
669 if (s->version == 1)
671 lpos = 0;
672 for(i=0;i<25;++i)
674 a = wma_critical_freqs[i];
675 b = s->sample_rate;
676 pos = ((block_len * 2 * a) + (b >> 1)) / b;
677 if (pos > block_len)
678 pos = block_len;
679 s->exponent_bands[0][i] = pos - lpos;
680 if (pos >= block_len)
682 ++i;
683 break;
685 lpos = pos;
687 s->exponent_sizes[0] = i;
689 else
691 /* hardcoded tables */
692 table = NULL;
693 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
694 if (a < 3)
696 if (s->sample_rate >= 44100)
697 table = exponent_band_44100[a];
698 else if (s->sample_rate >= 32000)
699 table = exponent_band_32000[a];
700 else if (s->sample_rate >= 22050)
701 table = exponent_band_22050[a];
703 if (table)
705 n = *table++;
706 for(i=0;i<n;++i)
707 s->exponent_bands[k][i] = table[i];
708 s->exponent_sizes[k] = n;
710 else
712 j = 0;
713 lpos = 0;
714 for(i=0;i<25;++i)
716 a = wma_critical_freqs[i];
717 b = s->sample_rate;
718 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
719 pos <<= 2;
720 if (pos > block_len)
721 pos = block_len;
722 if (pos > lpos)
723 s->exponent_bands[k][j++] = pos - lpos;
724 if (pos >= block_len)
725 break;
726 lpos = pos;
728 s->exponent_sizes[k] = j;
732 /* max number of coefs */
733 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
734 /* high freq computation */
736 fixed32 tmp1 = high_freq*2; /* high_freq is a fixed32!*/
737 fixed32 tmp2=itofix32(s->sample_rate>>1);
738 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
741 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
742 s->sample_rate + 0.5);*/
744 n = s->exponent_sizes[k];
745 j = 0;
746 pos = 0;
747 for(i=0;i<n;++i)
749 int start, end;
750 start = pos;
751 pos += s->exponent_bands[k][i];
752 end = pos;
753 if (start < s->high_band_start[k])
754 start = s->high_band_start[k];
755 if (end > s->coefs_end[k])
756 end = s->coefs_end[k];
757 if (end > start)
758 s->exponent_high_bands[k][j++] = end - start;
760 s->exponent_high_sizes[k] = j;
764 /* init MDCT */
765 /*TODO: figure out how to fold this up into one array*/
766 tcosarray[0] = tcos0; tcosarray[1] = tcos1; tcosarray[2] = tcos2; tcosarray[3] = tcos3;tcosarray[4] = tcos4;
767 tsinarray[0] = tsin0; tsinarray[1] = tsin1; tsinarray[2] = tsin2; tsinarray[3] = tsin3;tsinarray[4] = tsin4;
769 /*these are folded up now*/
770 exparray[0] = exptab0; //exparray[1] = exptab1; exparray[2] = exptab2; exparray[3] = exptab3; exparray[4] = exptab4;
771 revarray[0]=revtab0; //revarray[1]=revtab1; revarray[2]=revtab2; revarray[3]=revtab3; revarray[4]=revtab4;
773 s->mdct_tmp = mdct_tmp; /* temporary storage for imdct */
774 for(i = 0; i < s->nb_block_sizes; ++i)
776 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
780 int i, n;
781 fixed32 c1, s1, s2;
783 n=1<<10;
784 s2 = 1 ? 1 : -1;
785 for(i=0;i<(n/2);++i)
787 fixed32 ifix = itofix32(i);
788 fixed32 nfix = itofix32(n);
789 fixed32 res = fixdiv32(ifix,nfix);
791 s1 = fsincos(res<<16, &c1);
793 exptab0[i].re = c1;
794 exptab0[i].im = s1*s2;
798 /* init the MDCT bit reverse table here rather then in fft_init */
800 for(i=0;i<1024;i++) /*hard coded to a 2048 bit rotation*/
801 { /*smaller sizes can reuse the largest*/
802 m=0;
803 for(j=0;j<10;j++)
805 m |= ((i >> j) & 1) << (10-j-1);
808 revtab0[i]=m;
811 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
812 * In the worst case you can have 5 window sizes, 128 doubling up 2048
813 * Smaller windows are handled differently.
814 * Since we don't have malloc, just statically allocate this
816 fixed32 *temp[5];
817 temp[0] = stat0;
818 temp[1] = stat1;
819 temp[2] = stat2;
820 temp[3] = stat3;
821 temp[4] = stat4;
823 /* init MDCT windows : simple sinus window */
824 for(i = 0; i < s->nb_block_sizes; i++)
826 int n, j;
827 fixed32 alpha;
828 n = 1 << (s->frame_len_bits - i);
829 //window = av_malloc(sizeof(fixed32) * n);
830 window = temp[i];
832 //fixed32 n2 = itofix32(n<<1); //2x the window length
833 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
835 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
836 alpha = (1<<15)>>(s->frame_len_bits - i+1); /* this calculates 0.5/(2*n) */
837 for(j=0;j<n;++j)
839 fixed32 j2 = itofix32(j) + 0x8000;
840 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0); //alpha between 0 and pi/2
843 //printf("created window\n");
844 s->windows[i] = window;
845 //printf("assigned window\n");
848 s->reset_block_lengths = 1;
850 if (s->use_noise_coding)
852 /* init the noise generator */
853 if (s->use_exp_vlc)
855 s->noise_mult = 0x51f;
856 s->noise_table = noisetable_exp;
858 else
860 s->noise_mult = 0xa3d;
861 /* LSP values are simply 2x the EXP values */
862 for (i=0;i<NOISE_TAB_SIZE;++i)
863 noisetable_exp[i] = noisetable_exp[i]<< 1;
864 s->noise_table = noisetable_exp;
866 #if 0
868 unsigned int seed;
869 fixed32 norm;
870 seed = 1;
871 norm = 0; // PJJ: near as makes any diff to 0!
872 for (i=0;i<NOISE_TAB_SIZE;++i)
874 seed = seed * 314159 + 1;
875 s->noise_table[i] = itofix32((int)seed) * norm;
878 #endif
880 s->hgain_vlc.table = vlcbuf4;
881 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
882 hgain_huffbits, 1, 1,
883 hgain_huffcodes, 2, 2, 0);
886 if (s->use_exp_vlc)
889 s->exp_vlc.table = vlcbuf3;
891 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
892 scale_huffbits, 1, 1,
893 scale_huffcodes, 4, 4, 0);
895 else
897 wma_lsp_to_curve_init(s, s->frame_len);
900 /* choose the VLC tables for the coefficients */
901 coef_vlc_table = 2;
902 if (s->sample_rate >= 32000)
904 if (bps1 < 0xb852)
905 coef_vlc_table = 0;
906 else if (bps1 < 0x128f6)
907 coef_vlc_table = 1;
910 runtabarray[0] = runtab0; runtabarray[1] = runtab1;
911 levtabarray[0] = levtab0; levtabarray[1] = levtab1;
913 s->coef_vlc[0].table = vlcbuf1;
914 s->coef_vlc[0].table_allocated = 24576/4;
915 s->coef_vlc[1].table = vlcbuf2;
916 s->coef_vlc[1].table_allocated = 14336/4;
919 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
920 &coef_vlcs[coef_vlc_table * 2], 0);
921 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
922 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
924 s->last_superframe_len = 0;
925 s->last_bitoffset = 0;
927 return 0;
931 /* compute x^-0.25 with an exponent and mantissa table. We use linear
932 interpolation to reduce the mantissa table size at a small speed
933 expense (linear interpolation approximately doubles the number of
934 bits of precision). */
935 static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
937 union {
938 float f;
939 unsigned int v;
940 } u, t;
941 unsigned int e, m;
942 fixed32 a, b;
944 u.f = fixtof64(x);
945 e = u.v >> 23;
946 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
947 /* build interpolation scale: 1 <= t < 2. */
948 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
949 a = s->lsp_pow_m_table1[m];
950 b = s->lsp_pow_m_table2[m];
952 /* lsp_pow_e_table contains 32.32 format */
953 /* TODO: Since we're unlikely have value that cover the whole
954 * IEEE754 range, we probably don't need to have all possible exponents */
956 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
959 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
961 fixed32 wdel, a, b, temp, temp2;
962 int i, m;
964 wdel = fixdiv32(M_PI_F, itofix32(frame_len));
965 temp = fixdiv32(itofix32(1), itofix32(frame_len));
966 for (i=0; i<frame_len; ++i)
968 /* TODO: can probably reuse the trig_init values here */
969 fsincos((temp*i)<<15, &temp2);
970 /* get 3 bits headroom + 1 bit from not doubleing the values */
971 s->lsp_cos_table[i] = temp2>>3;
974 /* NOTE: these two tables are needed to avoid two operations in
975 pow_m1_4 */
976 b = itofix32(1);
977 int ix = 0;
979 /*double check this later*/
980 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
982 m = (1 << LSP_POW_BITS) + i;
983 a = pow_a_table[ix++]<<4;
984 s->lsp_pow_m_table1[i] = 2 * a - b;
985 s->lsp_pow_m_table2[i] = b - a;
986 b = a;
991 /* NOTE: We use the same code as Vorbis here */
992 /* XXX: optimize it further with SSE/3Dnow */
993 static void wma_lsp_to_curve(WMADecodeContext *s,
994 fixed32 *out,
995 fixed32 *val_max_ptr,
996 int n,
997 fixed32 *lsp)
999 int i, j;
1000 fixed32 p, q, w, v, val_max, temp, temp2;
1002 val_max = 0;
1003 for(i=0;i<n;++i)
1005 /* shift by 2 now to reduce rounding error,
1006 * we can renormalize right before pow_m1_4
1009 p = 0x8000<<5;
1010 q = 0x8000<<5;
1011 w = s->lsp_cos_table[i];
1013 for (j=1;j<NB_LSP_COEFS;j+=2)
1015 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
1016 temp2 = ((w - (lsp[j - 1]<<11)));
1017 temp = q;
1019 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
1020 q = fixmul32b(q, temp2 )<<4;
1021 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
1024 /* 2 in 5.27 format is 0x10000000 */
1025 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
1026 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
1028 v = (p + q) >>9; /* p/q end up as 16.16 */
1029 v = pow_m1_4(s, v);
1030 if (v > val_max)
1031 val_max = v;
1032 out[i] = v;
1035 *val_max_ptr = val_max;
1038 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
1039 static void decode_exp_lsp(WMADecodeContext *s, int ch)
1041 fixed32 lsp_coefs[NB_LSP_COEFS];
1042 int val, i;
1044 for (i = 0; i < NB_LSP_COEFS; ++i)
1046 if (i == 0 || i >= 8)
1047 val = get_bits(&s->gb, 3);
1048 else
1049 val = get_bits(&s->gb, 4);
1050 lsp_coefs[i] = lsp_codebook[i][val];
1053 wma_lsp_to_curve(s,
1054 s->exponents[ch],
1055 &s->max_exponent[ch],
1056 s->block_len,
1057 lsp_coefs);
1060 /* decode exponents coded with VLC codes */
1061 static int decode_exp_vlc(WMADecodeContext *s, int ch)
1063 int last_exp, n, code;
1064 const uint16_t *ptr, *band_ptr;
1065 fixed32 v, max_scale;
1066 fixed32 *q,*q_end;
1068 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
1069 ptr = band_ptr;
1070 q = s->exponents[ch];
1071 q_end = q + s->block_len;
1072 max_scale = 0;
1075 if (s->version == 1) //wmav1 only
1077 last_exp = get_bits(&s->gb, 5) + 10;
1078 /* XXX: use a table */
1079 v = pow_10_to_yover16[last_exp];
1080 max_scale = v;
1081 n = *ptr++;
1084 *q++ = v;
1086 while (--n);
1088 last_exp = 36;
1090 while (q < q_end)
1092 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
1093 if (code < 0)
1095 return -1;
1097 /* NOTE: this offset is the same as MPEG4 AAC ! */
1098 last_exp += code - 60;
1099 /* XXX: use a table */
1100 v = pow_10_to_yover16[last_exp];
1101 if (v > max_scale)
1103 max_scale = v;
1105 n = *ptr++;
1108 *q++ = v;
1111 while (--n);
1114 s->max_exponent[ch] = max_scale;
1115 return 0;
1118 /* return 0 if OK. return 1 if last block of frame. return -1 if
1119 unrecorrable error. */
1120 static int wma_decode_block(WMADecodeContext *s)
1122 int n, v, a, ch, code, bsize;
1123 int coef_nb_bits, total_gain;
1124 int nb_coefs[MAX_CHANNELS];
1125 fixed32 mdct_norm;
1127 // printf("***decode_block: %d:%d (%d)\n", s->frame_count - 1, s->block_num, s->block_len);
1129 /* compute current block length */
1130 if (s->use_variable_block_len)
1132 n = av_log2(s->nb_block_sizes - 1) + 1;
1134 if (s->reset_block_lengths)
1136 s->reset_block_lengths = 0;
1137 v = get_bits(&s->gb, n);
1138 if (v >= s->nb_block_sizes)
1140 return -2;
1142 s->prev_block_len_bits = s->frame_len_bits - v;
1143 v = get_bits(&s->gb, n);
1144 if (v >= s->nb_block_sizes)
1146 return -3;
1148 s->block_len_bits = s->frame_len_bits - v;
1150 else
1152 /* update block lengths */
1153 s->prev_block_len_bits = s->block_len_bits;
1154 s->block_len_bits = s->next_block_len_bits;
1156 v = get_bits(&s->gb, n);
1158 if (v >= s->nb_block_sizes)
1160 // rb->splash(HZ*4, "v was %d", v); //5, 7
1161 return -4; //this is it
1163 else{
1164 //rb->splash(HZ, "passed v block (%d)!", v);
1166 s->next_block_len_bits = s->frame_len_bits - v;
1168 else
1170 /* fixed block len */
1171 s->next_block_len_bits = s->frame_len_bits;
1172 s->prev_block_len_bits = s->frame_len_bits;
1173 s->block_len_bits = s->frame_len_bits;
1175 /* now check if the block length is coherent with the frame length */
1176 s->block_len = 1 << s->block_len_bits;
1178 if ((s->block_pos + s->block_len) > s->frame_len)
1180 return -5;
1183 if (s->nb_channels == 2)
1185 s->ms_stereo = get_bits(&s->gb, 1);
1187 v = 0;
1188 for (ch = 0; ch < s->nb_channels; ++ch)
1190 a = get_bits(&s->gb, 1);
1191 s->channel_coded[ch] = a;
1192 v |= a;
1194 /* if no channel coded, no need to go further */
1195 /* XXX: fix potential framing problems */
1196 if (!v)
1198 goto next;
1201 bsize = s->frame_len_bits - s->block_len_bits;
1203 /* read total gain and extract corresponding number of bits for
1204 coef escape coding */
1205 total_gain = 1;
1206 for(;;)
1208 a = get_bits(&s->gb, 7);
1209 total_gain += a;
1210 if (a != 127)
1212 break;
1216 if (total_gain < 15)
1217 coef_nb_bits = 13;
1218 else if (total_gain < 32)
1219 coef_nb_bits = 12;
1220 else if (total_gain < 40)
1221 coef_nb_bits = 11;
1222 else if (total_gain < 45)
1223 coef_nb_bits = 10;
1224 else
1225 coef_nb_bits = 9;
1227 /* compute number of coefficients */
1228 n = s->coefs_end[bsize] - s->coefs_start;
1230 for(ch = 0; ch < s->nb_channels; ++ch)
1232 nb_coefs[ch] = n;
1234 /* complex coding */
1235 if (s->use_noise_coding)
1238 for(ch = 0; ch < s->nb_channels; ++ch)
1240 if (s->channel_coded[ch])
1242 int i, n, a;
1243 n = s->exponent_high_sizes[bsize];
1244 for(i=0;i<n;++i)
1246 a = get_bits(&s->gb, 1);
1247 s->high_band_coded[ch][i] = a;
1248 /* if noise coding, the coefficients are not transmitted */
1249 if (a)
1250 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
1254 for(ch = 0; ch < s->nb_channels; ++ch)
1256 if (s->channel_coded[ch])
1258 int i, n, val, code;
1260 n = s->exponent_high_sizes[bsize];
1261 val = (int)0x80000000;
1262 for(i=0;i<n;++i)
1264 if (s->high_band_coded[ch][i])
1266 if (val == (int)0x80000000)
1268 val = get_bits(&s->gb, 7) - 19;
1270 else
1272 //code = get_vlc(&s->gb, &s->hgain_vlc);
1273 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
1274 if (code < 0)
1276 return -6;
1278 val += code - 18;
1280 s->high_band_values[ch][i] = val;
1287 /* exponents can be reused in short blocks. */
1288 if ((s->block_len_bits == s->frame_len_bits) || get_bits(&s->gb, 1))
1290 for(ch = 0; ch < s->nb_channels; ++ch)
1292 if (s->channel_coded[ch])
1294 if (s->use_exp_vlc)
1296 if (decode_exp_vlc(s, ch) < 0)
1298 return -7;
1301 else
1303 decode_exp_lsp(s, ch);
1305 s->exponents_bsize[ch] = bsize;
1310 /* parse spectral coefficients : just RLE encoding */
1311 for(ch = 0; ch < s->nb_channels; ++ch)
1313 if (s->channel_coded[ch])
1315 VLC *coef_vlc;
1316 int level, run, sign, tindex;
1317 int16_t *ptr, *eptr;
1318 const int16_t *level_table, *run_table;
1320 /* special VLC tables are used for ms stereo because
1321 there is potentially less energy there */
1322 tindex = (ch == 1 && s->ms_stereo);
1323 coef_vlc = &s->coef_vlc[tindex];
1324 run_table = s->run_table[tindex];
1325 level_table = s->level_table[tindex];
1326 /* XXX: optimize */
1327 ptr = &s->coefs1[ch][0];
1328 eptr = ptr + nb_coefs[ch];
1329 memset(ptr, 0, s->block_len * sizeof(int16_t));
1331 for(;;)
1333 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
1334 //code = get_vlc(&s->gb, coef_vlc);
1335 if (code < 0)
1337 return -8;
1339 if (code == 1)
1341 /* EOB */
1342 break;
1344 else if (code == 0)
1346 /* escape */
1347 level = get_bits(&s->gb, coef_nb_bits);
1348 /* NOTE: this is rather suboptimal. reading
1349 block_len_bits would be better */
1350 run = get_bits(&s->gb, s->frame_len_bits);
1352 else
1354 /* normal code */
1355 run = run_table[code];
1356 level = level_table[code];
1358 sign = get_bits(&s->gb, 1);
1359 if (!sign)
1360 level = -level;
1361 ptr += run;
1362 if (ptr >= eptr)
1364 return -9;
1366 *ptr++ = level;
1369 /* NOTE: EOB can be omitted */
1370 if (ptr >= eptr)
1371 break;
1374 if (s->version == 1 && s->nb_channels >= 2)
1376 align_get_bits(&s->gb);
1381 int n4 = s->block_len >> 1;
1382 //mdct_norm = 0x10000;
1383 //mdct_norm = fixdiv32(mdct_norm,itofix32(n4));
1385 mdct_norm = 0x10000>>(s->block_len_bits-1); //theres no reason to do a divide by two in fixed precision ...
1387 if (s->version == 1)
1389 fixed32 tmp = fixsqrt32(itofix32(n4));
1390 mdct_norm *= tmp; // PJJ : exercise this path
1395 /* finally compute the MDCT coefficients */
1396 for(ch = 0; ch < s->nb_channels; ++ch)
1398 if (s->channel_coded[ch])
1400 int16_t *coefs1;
1401 fixed32 *exponents, *exp_ptr;
1402 fixed32 *coefs, atemp;
1403 fixed64 mult;
1404 fixed64 mult1;
1405 fixed32 noise, temp1, temp2, mult2;
1406 int i, j, n, n1, last_high_band, esize;
1407 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
1409 //total_gain, coefs1, mdctnorm are lossless
1411 coefs1 = s->coefs1[ch];
1412 exponents = s->exponents[ch];
1413 esize = s->exponents_bsize[ch];
1414 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1415 mult = fixmul64byfixed(mult, mdct_norm); //what the hell? This is actually fixed64*2^16!
1416 coefs = (*(s->coefs))[ch];
1418 n=0;
1420 if (s->use_noise_coding)
1422 mult1 = mult;
1424 /* very low freqs : noise */
1425 for(i = 0;i < s->coefs_start; ++i)
1427 *coefs++ = fixmul32( (fixmul32(s->noise_table[s->noise_index],(*exponents++))>>4),Fixed32From64(mult1)) >>1;
1428 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1431 n1 = s->exponent_high_sizes[bsize];
1433 /* compute power of high bands */
1434 exp_ptr = exponents +
1435 s->high_band_start[bsize] -
1436 s->coefs_start;
1437 last_high_band = 0; /* avoid warning */
1438 for (j=0;j<n1;++j)
1440 n = s->exponent_high_bands[s->frame_len_bits -
1441 s->block_len_bits][j];
1442 if (s->high_band_coded[ch][j])
1444 fixed32 e2, v;
1445 e2 = 0;
1446 for(i = 0;i < n; ++i)
1448 /*v is noramlized later on so its fixed format is irrelevant*/
1449 v = exp_ptr[i]>>4;
1450 e2 += fixmul32(v, v)>>3;
1452 exp_power[j] = e2/n; /*n is an int...*/
1453 last_high_band = j;
1455 exp_ptr += n;
1458 /* main freqs and high freqs */
1459 for(j=-1;j<n1;++j)
1461 if (j < 0)
1463 n = s->high_band_start[bsize] -
1464 s->coefs_start;
1466 else
1468 n = s->exponent_high_bands[s->frame_len_bits -
1469 s->block_len_bits][j];
1471 if (j >= 0 && s->high_band_coded[ch][j])
1473 /* use noise with specified power */
1474 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1475 mult1 = (fixed64)fixsqrt32(tmp);
1476 /* XXX: use a table */
1477 /*mult1 is 48.16, pow_table is 48.16*/
1478 mult1 = mult1 * pow_table[s->high_band_values[ch][j]+20] >> PRECISION;
1480 /*this step has a fairly high degree of error for some reason*/
1481 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1483 mult1 = mult1*mdct_norm>>PRECISION;
1484 for(i = 0;i < n; ++i)
1486 noise = s->noise_table[s->noise_index];
1487 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1488 *coefs++ = fixmul32((fixmul32(*exponents,noise)>>4),Fixed32From64(mult1)) >>1;
1489 ++exponents;
1492 else
1494 /* coded values + small noise */
1495 for(i = 0;i < n; ++i)
1497 // PJJ: check code path
1498 noise = s->noise_table[s->noise_index];
1499 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1501 /*don't forget to renormalize the noise*/
1502 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1503 temp2 = fixmul32(*exponents, mult>>17);
1504 *coefs++ = fixmul32(temp1, temp2);
1505 ++exponents;
1510 /* very high freqs : noise */
1511 n = s->block_len - s->coefs_end[bsize];
1512 mult2 = fixmul32(mult>>16,exponents[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1513 for (i = 0; i < n; ++i)
1515 /*renormalize the noise product and then reduce to 17.15 precison*/
1516 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>5;
1518 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1521 else
1524 /* XXX: optimize more */
1526 n = nb_coefs[ch];
1528 for(i = 0;i < n; ++i)
1531 * Previously the IMDCT was run in 17.15 precision to avoid overflow. However rare files could
1532 * overflow here as well, so switch to 17.15 now. As a bonus, this saves us a shift later on.
1536 atemp = (fixed32)(coefs1[i]*mult>>17);
1537 //this "works" in the sense that the mdcts converge
1538 //atemp= ftofix32(coefs1[i] * fixtof64(exponents[i]) * fixtof64(mult>>16));
1540 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1543 n = s->block_len - s->coefs_end[bsize];
1544 for(i = 0;i < n; ++i)
1545 *coefs++ = 0;
1552 if (s->ms_stereo && s->channel_coded[1])
1554 fixed32 a, b;
1555 int i;
1556 fixed32 (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1558 /* nominal case for ms stereo: we do it before mdct */
1559 /* no need to optimize this case because it should almost
1560 never happen */
1561 if (!s->channel_coded[0])
1563 memset((*(s->coefs))[0], 0, sizeof(fixed32) * s->block_len);
1564 s->channel_coded[0] = 1;
1567 for(i = 0; i < s->block_len; ++i)
1569 a = (*coefs)[0][i];
1570 b = (*coefs)[1][i];
1571 (*coefs)[0][i] = a + b;
1572 (*coefs)[1][i] = a - b;
1576 for(ch = 0; ch < s->nb_channels; ++ch)
1578 if (s->channel_coded[ch])
1580 static fixed32 output[BLOCK_MAX_SIZE * 2] IBSS_ATTR;
1582 int n4, index, n;
1584 n = s->block_len;
1585 n4 = s->block_len >>1;
1587 ff_imdct_calc(&s->mdct_ctx[bsize],
1588 output,
1589 (*(s->coefs))[ch]);
1592 /* add in the frame */
1593 index = (s->frame_len / 2) + s->block_pos - n4;
1595 wma_window(s, output, &s->frame_out[ch][index]);
1599 /* specific fast case for ms-stereo : add to second
1600 channel if it is not coded */
1601 if (s->ms_stereo && !s->channel_coded[1])
1603 wma_window(s, output, &s->frame_out[1][index]);
1607 next:
1608 /* update block number */
1609 ++s->block_num;
1610 s->block_pos += s->block_len;
1611 if (s->block_pos >= s->frame_len)
1613 return 1;
1615 else
1617 return 0;
1621 /* decode a frame of frame_len samples */
1622 static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
1624 int ret, i, n, a, ch, incr;
1625 int16_t *ptr;
1626 fixed32 *iptr;
1627 // rb->splash(HZ, "in wma_decode_frame");
1629 /* read each block */
1630 s->block_num = 0;
1631 s->block_pos = 0;
1634 for(;;)
1636 ret = wma_decode_block(s);
1637 if (ret < 0)
1640 //rb->splash(HZ*4, "wma_decode_block failed with ret %d", ret);
1641 return -1;
1643 if (ret)
1645 break;
1649 /* convert frame to integer */
1650 n = s->frame_len;
1651 incr = s->nb_channels;
1652 for(ch = 0; ch < s->nb_channels; ++ch)
1654 ptr = samples + ch;
1655 iptr = s->frame_out[ch];
1657 for (i=0;i<n;++i)
1659 a = fixtoi32(*iptr++)<<1; //ugly but good enough for now
1661 if (a > 32767)
1663 a = 32767;
1665 else if (a < -32768)
1667 a = -32768;
1669 *ptr = a;
1670 ptr += incr;
1672 /* prepare for next block */
1673 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
1674 s->frame_len * sizeof(fixed32));
1678 return 0;
1681 /* Initialise the superframe decoding */
1683 int wma_decode_superframe_init(WMADecodeContext* s,
1684 uint8_t *buf, /*input*/
1685 int buf_size)
1687 if (buf_size==0)
1689 s->last_superframe_len = 0;
1690 return 0;
1693 s->current_frame = 0;
1695 init_get_bits(&s->gb, buf, buf_size*8);
1697 if (s->use_bit_reservoir)
1699 /* read super frame header */
1700 get_bits(&s->gb, 4); /* super frame index */
1701 s->nb_frames = get_bits(&s->gb, 4);
1703 if (s->last_superframe_len == 0)
1704 s->nb_frames --;
1705 else if (s->nb_frames == 0)
1706 s->nb_frames++;
1708 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1709 } else {
1710 s->nb_frames = 1;
1713 return 1;
1717 /* Decode a single frame in the current superframe - return -1 if
1718 there was a decoding error, or the number of samples decoded.
1721 int wma_decode_superframe_frame(WMADecodeContext* s,
1722 int16_t* samples, /*output*/
1723 uint8_t *buf, /*input*/
1724 int buf_size)
1726 int pos, len;
1727 uint8_t *q;
1728 int done = 0;
1730 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1732 if (s->last_superframe_len > 0)
1734 /* add s->bit_offset bits to last frame */
1735 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1736 MAX_CODED_SUPERFRAME_SIZE)
1738 goto fail;
1740 q = s->last_superframe + s->last_superframe_len;
1741 len = s->bit_offset;
1742 while (len > 0)
1744 *q++ = (get_bits)(&s->gb, 8);
1745 len -= 8;
1747 if (len > 0)
1749 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1752 /* XXX: s->bit_offset bits into last frame */
1753 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1754 /* skip unused bits */
1755 if (s->last_bitoffset > 0)
1756 skip_bits(&s->gb, s->last_bitoffset);
1758 /* this frame is stored in the last superframe and in the
1759 current one */
1760 if (wma_decode_frame(s, samples) < 0)
1762 goto fail;
1764 done = 1;
1767 /* read each frame starting from s->bit_offset */
1768 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1769 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1770 len = pos & 7;
1771 if (len > 0)
1772 skip_bits(&s->gb, len);
1774 s->reset_block_lengths = 1;
1777 /* If we haven't decoded a frame yet, do it now */
1778 if (!done)
1780 if (wma_decode_frame(s, samples) < 0)
1782 goto fail;
1786 s->current_frame++;
1788 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1790 /* we copy the end of the frame in the last frame buffer */
1791 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1792 s->last_bitoffset = pos & 7;
1793 pos >>= 3;
1794 len = buf_size - pos;
1795 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1797 goto fail;
1799 s->last_superframe_len = len;
1800 memcpy(s->last_superframe, buf + pos, len);
1803 return s->frame_len;
1805 fail:
1806 /* when error, we reset the bit reservoir */
1807 s->last_superframe_len = 0;
1808 return -1;