Gigabeat S: Make MIN_BRIGHTNESS_SETTING to be 1, not 0; the backlight already has...
[kugel-rb.git] / apps / codecs / libwma / wmadeci.c
blobd362c6d1e96638b05775ae3658d797f09c74e66d
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.h"
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 typedef struct CoefVLCTable
45 int n; /* total number of codes */
46 const uint32_t *huffcodes; /* VLC bit values */
47 const uint8_t *huffbits; /* VLC bit size */
48 const uint16_t *levels; /* table to build run/level tables */
50 CoefVLCTable;
52 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
54 fixed32 coefsarray[MAX_CHANNELS][BLOCK_MAX_SIZE] IBSS_ATTR;
55 /*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
56 fixed32 frame_out_buf[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] IBSS_ATTR_WMA_LARGE_IRAM;
58 //static variables that replace malloced stuff
59 fixed32 stat0[2048], stat1[1024], stat2[512], stat3[256], stat4[128]; //these are the MDCT reconstruction windows
61 uint16_t *runtabarray[2], *levtabarray[2]; //these are VLC lookup tables
63 uint16_t runtab0[1336], runtab1[1336], levtab0[1336], levtab1[1336]; //these could be made smaller since only one can be 1336
65 #define VLCBUF1SIZE 4598
66 #define VLCBUF2SIZE 3574
67 #define VLCBUF3SIZE 360
68 #define VLCBUF4SIZE 540
70 /*putting these in IRAM actually makes PP slower*/
72 VLC_TYPE vlcbuf1[VLCBUF1SIZE][2];
73 VLC_TYPE vlcbuf2[VLCBUF2SIZE][2];
74 VLC_TYPE vlcbuf3[VLCBUF3SIZE][2];
75 VLC_TYPE vlcbuf4[VLCBUF4SIZE][2];
79 #include "wmadata.h" // PJJ
84 * Helper functions for wma_window.
89 #ifdef CPU_ARM
90 static inline
91 void vector_fmul_add_add(fixed32 *dst, const fixed32 *data,
92 const fixed32 *window, int n)
94 /* Block sizes are always power of two */
95 asm volatile (
96 "0:"
97 "ldmia %[d]!, {r0, r1};"
98 "ldmia %[w]!, {r4, r5};"
99 /* consume the first data and window value so we can use those
100 * registers again */
101 "smull r8, r9, r0, r4;"
102 "ldmia %[dst], {r0, r4};"
103 "add r0, r0, r9, lsl #1;" /* *dst=*dst+(r9<<1)*/
104 "smull r8, r9, r1, r5;"
105 "add r1, r4, r9, lsl #1;"
106 "stmia %[dst]!, {r0, r1};"
107 "subs %[n], %[n], #2;"
108 "bne 0b;"
109 : [d] "+r" (data), [w] "+r" (window), [dst] "+r" (dst), [n] "+r" (n)
110 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
113 static inline
114 void vector_fmul_reverse(fixed32 *dst, const fixed32 *src0, const fixed32 *src1,
115 int len)
117 /* Block sizes are always power of two */
118 asm volatile (
119 "add %[s1], %[s1], %[n], lsl #2;"
120 "0:"
121 "ldmia %[s0]!, {r0, r1};"
122 "ldmdb %[s1]!, {r4, r5};"
123 "smull r8, r9, r0, r5;"
124 "mov r0, r9, lsl #1;"
125 "smull r8, r9, r1, r4;"
126 "mov r1, r9, lsl #1;"
127 "stmia %[dst]!, {r0, r1};"
128 "subs %[n], %[n], #2;"
129 "bne 0b;"
130 : [s0] "+r" (src0), [s1] "+r" (src1), [dst] "+r" (dst), [n] "+r" (len)
131 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
134 #elif defined(CPU_COLDFIRE)
136 static inline
137 void vector_fmul_add_add(fixed32 *dst, const fixed32 *data,
138 const fixed32 *window, int n)
140 /* Block sizes are always power of two. Smallest block is always way bigger
141 * than four too.*/
142 asm volatile (
143 "0:"
144 "movem.l (%[d]), %%d0-%%d3;"
145 "movem.l (%[w]), %%d4-%%d5/%%a0-%%a1;"
146 "mac.l %%d0, %%d4, %%acc0;"
147 "mac.l %%d1, %%d5, %%acc1;"
148 "mac.l %%d2, %%a0, %%acc2;"
149 "mac.l %%d3, %%a1, %%acc3;"
150 "lea.l (16, %[d]), %[d];"
151 "lea.l (16, %[w]), %[w];"
152 "movclr.l %%acc0, %%d0;"
153 "movclr.l %%acc1, %%d1;"
154 "movclr.l %%acc2, %%d2;"
155 "movclr.l %%acc3, %%d3;"
156 "movem.l (%[dst]), %%d4-%%d5/%%a0-%%a1;"
157 "add.l %%d4, %%d0;"
158 "add.l %%d5, %%d1;"
159 "add.l %%a0, %%d2;"
160 "add.l %%a1, %%d3;"
161 "movem.l %%d0-%%d3, (%[dst]);"
162 "lea.l (16, %[dst]), %[dst];"
163 "subq.l #4, %[n];"
164 "jne 0b;"
165 : [d] "+a" (data), [w] "+a" (window), [dst] "+a" (dst), [n] "+d" (n)
166 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
169 static inline
170 void vector_fmul_reverse(fixed32 *dst, const fixed32 *src0, const fixed32 *src1,
171 int len)
173 /* Block sizes are always power of two. Smallest block is always way bigger
174 * than four too.*/
175 asm volatile (
176 "lea.l (-16, %[s1], %[n]*4), %[s1];"
177 "0:"
178 "movem.l (%[s0]), %%d0-%%d3;"
179 "movem.l (%[s1]), %%d4-%%d5/%%a0-%%a1;"
180 "mac.l %%d0, %%a1, %%acc0;"
181 "mac.l %%d1, %%a0, %%acc1;"
182 "mac.l %%d2, %%d5, %%acc2;"
183 "mac.l %%d3, %%d4, %%acc3;"
184 "lea.l (16, %[s0]), %[s0];"
185 "lea.l (-16, %[s1]), %[s1];"
186 "movclr.l %%acc0, %%d0;"
187 "movclr.l %%acc1, %%d1;"
188 "movclr.l %%acc2, %%d2;"
189 "movclr.l %%acc3, %%d3;"
190 "movem.l %%d0-%%d3, (%[dst]);"
191 "lea.l (16, %[dst]), %[dst];"
192 "subq.l #4, %[n];"
193 "jne 0b;"
194 : [s0] "+a" (src0), [s1] "+a" (src1), [dst] "+a" (dst), [n] "+d" (len)
195 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
198 #else
200 static inline void vector_fmul_add_add(fixed32 *dst, const fixed32 *src0, const fixed32 *src1, int len){
201 int i;
202 for(i=0; i<len; i++)
203 dst[i] = fixmul32b(src0[i], src1[i]) + dst[i];
206 static inline void vector_fmul_reverse(fixed32 *dst, const fixed32 *src0, const fixed32 *src1, int len){
207 int i;
208 src1 += len-1;
209 for(i=0; i<len; i++)
210 dst[i] = fixmul32b(src0[i], src1[-i]);
213 #endif
216 * Apply MDCT window and add into output.
218 * We ensure that when the windows overlap their squared sum
219 * is always 1 (MDCT reconstruction rule).
221 * The Vorbis I spec has a great diagram explaining this process.
222 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
224 static void wma_window(WMADecodeContext *s, fixed32 *in, fixed32 *out)
226 //float *in = s->output;
227 int block_len, bsize, n;
229 /* left part */
230 /*previous block was larger, so we'll use the size of the current block to set the window size*/
231 if (s->block_len_bits <= s->prev_block_len_bits) {
232 block_len = s->block_len;
233 bsize = s->frame_len_bits - s->block_len_bits;
235 vector_fmul_add_add(out, in, s->windows[bsize], block_len);
237 } else {
238 /*previous block was smaller or the same size, so use it's size to set the window length*/
239 block_len = 1 << s->prev_block_len_bits;
240 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
241 n = (s->block_len - block_len) / 2;
242 bsize = s->frame_len_bits - s->prev_block_len_bits;
244 vector_fmul_add_add(out+n, in+n, s->windows[bsize], block_len);
246 memcpy(out+n+block_len, in+n+block_len, n*sizeof(fixed32));
248 /* Advance to the end of the current block and prepare to window it for the next block.
249 * Since the window function needs to be reversed, we do it backwards starting with the
250 * last sample and moving towards the first
252 out += s->block_len;
253 in += s->block_len;
255 /* right part */
256 if (s->block_len_bits <= s->next_block_len_bits) {
257 block_len = s->block_len;
258 bsize = s->frame_len_bits - s->block_len_bits;
260 vector_fmul_reverse(out, in, s->windows[bsize], block_len);
262 } else {
263 block_len = 1 << s->next_block_len_bits;
264 n = (s->block_len - block_len) / 2;
265 bsize = s->frame_len_bits - s->next_block_len_bits;
267 memcpy(out, in, n*sizeof(fixed32));
269 vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
271 memset(out+n+block_len, 0, n*sizeof(fixed32));
278 /* XXX: use same run/length optimization as mpeg decoders */
279 static void init_coef_vlc(VLC *vlc,
280 uint16_t **prun_table, uint16_t **plevel_table,
281 const CoefVLCTable *vlc_table, int tab)
283 int n = vlc_table->n;
284 const uint8_t *table_bits = vlc_table->huffbits;
285 const uint32_t *table_codes = vlc_table->huffcodes;
286 const uint16_t *levels_table = vlc_table->levels;
287 uint16_t *run_table, *level_table;
288 const uint16_t *p;
289 int i, l, j, level;
292 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
294 run_table = runtabarray[tab];
295 level_table= levtabarray[tab];
297 p = levels_table;
298 i = 2;
299 level = 1;
300 while (i < n)
302 l = *p++;
303 for(j=0;j<l;++j)
305 run_table[i] = j;
306 level_table[i] = level;
307 ++i;
309 ++level;
311 *prun_table = run_table;
312 *plevel_table = level_table;
315 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
317 //WMADecodeContext *s = avctx->priv_data;
318 int i, flags1, flags2;
319 fixed32 *window;
320 uint8_t *extradata;
321 fixed64 bps1;
322 fixed32 high_freq;
323 fixed64 bps;
324 int sample_rate1;
325 int coef_vlc_table;
326 // int filehandle;
327 #ifdef CPU_COLDFIRE
328 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
329 #endif
331 s->sample_rate = wfx->rate;
332 s->nb_channels = wfx->channels;
333 s->bit_rate = wfx->bitrate;
334 s->block_align = wfx->blockalign;
336 s->coefs = &coefsarray;
337 s->frame_out = &frame_out_buf;
339 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
340 s->version = 1;
341 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2 ) {
342 s->version = 2;
343 } else {
344 /*one of those other wma flavors that don't have GPLed decoders */
345 return -1;
348 /* extract flag infos */
349 flags1 = 0;
350 flags2 = 0;
351 extradata = wfx->data;
352 if (s->version == 1 && wfx->datalen >= 4) {
353 flags1 = extradata[0] | (extradata[1] << 8);
354 flags2 = extradata[2] | (extradata[3] << 8);
355 }else if (s->version == 2 && wfx->datalen >= 6){
356 flags1 = extradata[0] | (extradata[1] << 8) |
357 (extradata[2] << 16) | (extradata[3] << 24);
358 flags2 = extradata[4] | (extradata[5] << 8);
360 s->use_exp_vlc = flags2 & 0x0001;
361 s->use_bit_reservoir = flags2 & 0x0002;
362 s->use_variable_block_len = flags2 & 0x0004;
364 /* compute MDCT block size */
365 if (s->sample_rate <= 16000){
366 s->frame_len_bits = 9;
367 }else if (s->sample_rate <= 22050 ||
368 (s->sample_rate <= 32000 && s->version == 1)){
369 s->frame_len_bits = 10;
370 }else{
371 s->frame_len_bits = 11;
373 s->frame_len = 1 << s->frame_len_bits;
374 if (s-> use_variable_block_len)
376 int nb_max, nb;
377 nb = ((flags2 >> 3) & 3) + 1;
378 if ((s->bit_rate / s->nb_channels) >= 32000)
380 nb += 2;
382 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
383 if (nb > nb_max)
384 nb = nb_max;
385 s->nb_block_sizes = nb + 1;
387 else
389 s->nb_block_sizes = 1;
392 /* init rate dependant parameters */
393 s->use_noise_coding = 1;
394 high_freq = itofix64(s->sample_rate) >> 1;
397 /* if version 2, then the rates are normalized */
398 sample_rate1 = s->sample_rate;
399 if (s->version == 2)
401 if (sample_rate1 >= 44100)
402 sample_rate1 = 44100;
403 else if (sample_rate1 >= 22050)
404 sample_rate1 = 22050;
405 else if (sample_rate1 >= 16000)
406 sample_rate1 = 16000;
407 else if (sample_rate1 >= 11025)
408 sample_rate1 = 11025;
409 else if (sample_rate1 >= 8000)
410 sample_rate1 = 8000;
413 fixed64 tmp = itofix64(s->bit_rate);
414 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
415 bps = fixdiv64(tmp, tmp2);
416 fixed64 tim = bps * s->frame_len;
417 fixed64 tmpi = fixdiv64(tim,itofix64(8));
418 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
420 /* compute high frequency value and choose if noise coding should
421 be activated */
422 bps1 = bps;
423 if (s->nb_channels == 2)
424 bps1 = fixmul32(bps,0x1999a);
425 if (sample_rate1 == 44100)
427 if (bps1 >= 0x9c29)
428 s->use_noise_coding = 0;
429 else
430 high_freq = fixmul32(high_freq,0x6666);
432 else if (sample_rate1 == 22050)
434 if (bps1 >= 0x128f6)
435 s->use_noise_coding = 0;
436 else if (bps1 >= 0xb852)
437 high_freq = fixmul32(high_freq,0xb333);
438 else
439 high_freq = fixmul32(high_freq,0x999a);
441 else if (sample_rate1 == 16000)
443 if (bps > 0x8000)
444 high_freq = fixmul32(high_freq,0x8000);
445 else
446 high_freq = fixmul32(high_freq,0x4ccd);
448 else if (sample_rate1 == 11025)
450 high_freq = fixmul32(high_freq,0xb333);
452 else if (sample_rate1 == 8000)
454 if (bps <= 0xa000)
456 high_freq = fixmul32(high_freq,0x8000);
458 else if (bps > 0xc000)
460 s->use_noise_coding = 0;
462 else
464 high_freq = fixmul32(high_freq,0xa666);
467 else
469 if (bps >= 0xcccd)
471 high_freq = fixmul32(high_freq,0xc000);
473 else if (bps >= 0x999a)
475 high_freq = fixmul32(high_freq,0x999a);
477 else
479 high_freq = fixmul32(high_freq,0x8000);
483 /* compute the scale factor band sizes for each MDCT block size */
485 int a, b, pos, lpos, k, block_len, i, j, n;
486 const uint8_t *table;
488 if (s->version == 1)
490 s->coefs_start = 3;
492 else
494 s->coefs_start = 0;
496 for(k = 0; k < s->nb_block_sizes; ++k)
498 block_len = s->frame_len >> k;
500 if (s->version == 1)
502 lpos = 0;
503 for(i=0;i<25;++i)
505 a = wma_critical_freqs[i];
506 b = s->sample_rate;
507 pos = ((block_len * 2 * a) + (b >> 1)) / b;
508 if (pos > block_len)
509 pos = block_len;
510 s->exponent_bands[0][i] = pos - lpos;
511 if (pos >= block_len)
513 ++i;
514 break;
516 lpos = pos;
518 s->exponent_sizes[0] = i;
520 else
522 /* hardcoded tables */
523 table = NULL;
524 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
525 if (a < 3)
527 if (s->sample_rate >= 44100)
528 table = exponent_band_44100[a];
529 else if (s->sample_rate >= 32000)
530 table = exponent_band_32000[a];
531 else if (s->sample_rate >= 22050)
532 table = exponent_band_22050[a];
534 if (table)
536 n = *table++;
537 for(i=0;i<n;++i)
538 s->exponent_bands[k][i] = table[i];
539 s->exponent_sizes[k] = n;
541 else
543 j = 0;
544 lpos = 0;
545 for(i=0;i<25;++i)
547 a = wma_critical_freqs[i];
548 b = s->sample_rate;
549 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
550 pos <<= 2;
551 if (pos > block_len)
552 pos = block_len;
553 if (pos > lpos)
554 s->exponent_bands[k][j++] = pos - lpos;
555 if (pos >= block_len)
556 break;
557 lpos = pos;
559 s->exponent_sizes[k] = j;
563 /* max number of coefs */
564 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
565 /* high freq computation */
567 fixed32 tmp1 = high_freq*2; /* high_freq is a fixed32!*/
568 fixed32 tmp2=itofix32(s->sample_rate>>1);
569 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
572 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
573 s->sample_rate + 0.5);*/
575 n = s->exponent_sizes[k];
576 j = 0;
577 pos = 0;
578 for(i=0;i<n;++i)
580 int start, end;
581 start = pos;
582 pos += s->exponent_bands[k][i];
583 end = pos;
584 if (start < s->high_band_start[k])
585 start = s->high_band_start[k];
586 if (end > s->coefs_end[k])
587 end = s->coefs_end[k];
588 if (end > start)
589 s->exponent_high_bands[k][j++] = end - start;
591 s->exponent_high_sizes[k] = j;
595 /*Not using the ffmpeg IMDCT anymore*/
597 /* mdct_init_global();
599 for(i = 0; i < s->nb_block_sizes; ++i)
601 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
606 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
607 * In the worst case you can have 5 window sizes, 128 doubling up 2048
608 * Smaller windows are handled differently.
609 * Since we don't have malloc, just statically allocate this
611 fixed32 *temp[5];
612 temp[0] = stat0;
613 temp[1] = stat1;
614 temp[2] = stat2;
615 temp[3] = stat3;
616 temp[4] = stat4;
618 /* init MDCT windows : simple sinus window */
619 for(i = 0; i < s->nb_block_sizes; i++)
621 int n, j;
622 fixed32 alpha;
623 n = 1 << (s->frame_len_bits - i);
624 //window = av_malloc(sizeof(fixed32) * n);
625 window = temp[i];
627 //fixed32 n2 = itofix32(n<<1); //2x the window length
628 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
630 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
631 alpha = (1<<15)>>(s->frame_len_bits - i+1); /* this calculates 0.5/(2*n) */
632 for(j=0;j<n;++j)
634 fixed32 j2 = itofix32(j) + 0x8000;
635 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0); //alpha between 0 and pi/2
638 s->windows[i] = window;
642 s->reset_block_lengths = 1;
644 if (s->use_noise_coding)
646 /* init the noise generator */
647 if (s->use_exp_vlc)
649 s->noise_mult = 0x51f;
650 s->noise_table = noisetable_exp;
652 else
654 s->noise_mult = 0xa3d;
655 /* LSP values are simply 2x the EXP values */
656 for (i=0;i<NOISE_TAB_SIZE;++i)
657 noisetable_exp[i] = noisetable_exp[i]<< 1;
658 s->noise_table = noisetable_exp;
660 #if 0
662 unsigned int seed;
663 fixed32 norm;
664 seed = 1;
665 norm = 0; // PJJ: near as makes any diff to 0!
666 for (i=0;i<NOISE_TAB_SIZE;++i)
668 seed = seed * 314159 + 1;
669 s->noise_table[i] = itofix32((int)seed) * norm;
672 #endif
674 s->hgain_vlc.table = vlcbuf4;
675 s->hgain_vlc.table_allocated = VLCBUF4SIZE;
676 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
677 hgain_huffbits, 1, 1,
678 hgain_huffcodes, 2, 2, 0);
681 if (s->use_exp_vlc)
684 s->exp_vlc.table = vlcbuf3;
685 s->exp_vlc.table_allocated = VLCBUF3SIZE;
687 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
688 scale_huffbits, 1, 1,
689 scale_huffcodes, 4, 4, 0);
691 else
693 wma_lsp_to_curve_init(s, s->frame_len);
696 /* choose the VLC tables for the coefficients */
697 coef_vlc_table = 2;
698 if (s->sample_rate >= 32000)
700 if (bps1 < 0xb852)
701 coef_vlc_table = 0;
702 else if (bps1 < 0x128f6)
703 coef_vlc_table = 1;
706 runtabarray[0] = runtab0; runtabarray[1] = runtab1;
707 levtabarray[0] = levtab0; levtabarray[1] = levtab1;
709 s->coef_vlc[0].table = vlcbuf1;
710 s->coef_vlc[0].table_allocated = VLCBUF1SIZE;
711 s->coef_vlc[1].table = vlcbuf2;
712 s->coef_vlc[1].table_allocated = VLCBUF2SIZE;
715 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
716 &coef_vlcs[coef_vlc_table * 2], 0);
717 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
718 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
720 s->last_superframe_len = 0;
721 s->last_bitoffset = 0;
723 return 0;
727 /* compute x^-0.25 with an exponent and mantissa table. We use linear
728 interpolation to reduce the mantissa table size at a small speed
729 expense (linear interpolation approximately doubles the number of
730 bits of precision). */
731 static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
733 union {
734 float f;
735 unsigned int v;
736 } u, t;
737 unsigned int e, m;
738 fixed32 a, b;
740 u.f = fixtof64(x);
741 e = u.v >> 23;
742 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
743 /* build interpolation scale: 1 <= t < 2. */
744 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
745 a = s->lsp_pow_m_table1[m];
746 b = s->lsp_pow_m_table2[m];
748 /* lsp_pow_e_table contains 32.32 format */
749 /* TODO: Since we're unlikely have value that cover the whole
750 * IEEE754 range, we probably don't need to have all possible exponents */
752 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
755 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
757 fixed32 wdel, a, b, temp, temp2;
758 int i, m;
760 wdel = fixdiv32(M_PI_F, itofix32(frame_len));
761 temp = fixdiv32(itofix32(1), itofix32(frame_len));
762 for (i=0; i<frame_len; ++i)
764 /* TODO: can probably reuse the trig_init values here */
765 fsincos((temp*i)<<15, &temp2);
766 /* get 3 bits headroom + 1 bit from not doubleing the values */
767 s->lsp_cos_table[i] = temp2>>3;
770 /* NOTE: these two tables are needed to avoid two operations in
771 pow_m1_4 */
772 b = itofix32(1);
773 int ix = 0;
775 /*double check this later*/
776 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
778 m = (1 << LSP_POW_BITS) + i;
779 a = pow_a_table[ix++]<<4;
780 s->lsp_pow_m_table1[i] = 2 * a - b;
781 s->lsp_pow_m_table2[i] = b - a;
782 b = a;
787 /* NOTE: We use the same code as Vorbis here */
788 /* XXX: optimize it further with SSE/3Dnow */
789 static void wma_lsp_to_curve(WMADecodeContext *s,
790 fixed32 *out,
791 fixed32 *val_max_ptr,
792 int n,
793 fixed32 *lsp)
795 int i, j;
796 fixed32 p, q, w, v, val_max, temp, temp2;
798 val_max = 0;
799 for(i=0;i<n;++i)
801 /* shift by 2 now to reduce rounding error,
802 * we can renormalize right before pow_m1_4
805 p = 0x8000<<5;
806 q = 0x8000<<5;
807 w = s->lsp_cos_table[i];
809 for (j=1;j<NB_LSP_COEFS;j+=2)
811 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
812 temp2 = ((w - (lsp[j - 1]<<11)));
813 temp = q;
815 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
816 q = fixmul32b(q, temp2 )<<4;
817 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
820 /* 2 in 5.27 format is 0x10000000 */
821 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
822 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
824 v = (p + q) >>9; /* p/q end up as 16.16 */
825 v = pow_m1_4(s, v);
826 if (v > val_max)
827 val_max = v;
828 out[i] = v;
831 *val_max_ptr = val_max;
834 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
835 static void decode_exp_lsp(WMADecodeContext *s, int ch)
837 fixed32 lsp_coefs[NB_LSP_COEFS];
838 int val, i;
840 for (i = 0; i < NB_LSP_COEFS; ++i)
842 if (i == 0 || i >= 8)
843 val = get_bits(&s->gb, 3);
844 else
845 val = get_bits(&s->gb, 4);
846 lsp_coefs[i] = lsp_codebook[i][val];
849 wma_lsp_to_curve(s,
850 s->exponents[ch],
851 &s->max_exponent[ch],
852 s->block_len,
853 lsp_coefs);
856 /* decode exponents coded with VLC codes */
857 static int decode_exp_vlc(WMADecodeContext *s, int ch)
859 int last_exp, n, code;
860 const uint16_t *ptr, *band_ptr;
861 fixed32 v, max_scale;
862 fixed32 *q,*q_end;
864 /*accommodate the 60 negative indices */
865 const fixed32 *pow_10_to_yover16_ptr = &pow_10_to_yover16[61];
867 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
868 ptr = band_ptr;
869 q = s->exponents[ch];
870 q_end = q + s->block_len;
871 max_scale = 0;
874 if (s->version == 1) //wmav1 only
876 last_exp = get_bits(&s->gb, 5) + 10;
877 /* XXX: use a table */
878 v = pow_10_to_yover16_ptr[last_exp];
879 max_scale = v;
880 n = *ptr++;
883 *q++ = v;
885 while (--n);
886 } else {
887 last_exp = 36;
890 while (q < q_end)
892 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
893 if (code < 0)
895 return -1;
897 /* NOTE: this offset is the same as MPEG4 AAC ! */
898 last_exp += code - 60;
899 /* XXX: use a table */
900 v = pow_10_to_yover16_ptr[last_exp];
901 if (v > max_scale)
903 max_scale = v;
905 n = *ptr++;
908 *q++ = v;
911 while (--n);
914 s->max_exponent[ch] = max_scale;
915 return 0;
918 /* return 0 if OK. return 1 if last block of frame. return -1 if
919 unrecorrable error. */
920 static int wma_decode_block(WMADecodeContext *s, int32_t *scratch_buffer)
922 int n, v, a, ch, code, bsize;
923 int coef_nb_bits, total_gain;
924 int nb_coefs[MAX_CHANNELS];
925 fixed32 mdct_norm;
927 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
929 /* compute current block length */
930 if (s->use_variable_block_len)
932 n = av_log2(s->nb_block_sizes - 1) + 1;
934 if (s->reset_block_lengths)
936 s->reset_block_lengths = 0;
937 v = get_bits(&s->gb, n);
938 if (v >= s->nb_block_sizes)
940 return -2;
942 s->prev_block_len_bits = s->frame_len_bits - v;
943 v = get_bits(&s->gb, n);
944 if (v >= s->nb_block_sizes)
946 return -3;
948 s->block_len_bits = s->frame_len_bits - v;
950 else
952 /* update block lengths */
953 s->prev_block_len_bits = s->block_len_bits;
954 s->block_len_bits = s->next_block_len_bits;
956 v = get_bits(&s->gb, n);
958 if (v >= s->nb_block_sizes)
960 // rb->splash(HZ*4, "v was %d", v); //5, 7
961 return -4; //this is it
963 else{
964 //rb->splash(HZ, "passed v block (%d)!", v);
966 s->next_block_len_bits = s->frame_len_bits - v;
968 else
970 /* fixed block len */
971 s->next_block_len_bits = s->frame_len_bits;
972 s->prev_block_len_bits = s->frame_len_bits;
973 s->block_len_bits = s->frame_len_bits;
975 /* now check if the block length is coherent with the frame length */
976 s->block_len = 1 << s->block_len_bits;
978 if ((s->block_pos + s->block_len) > s->frame_len)
980 return -5; //oddly 32k sample from tracker fails here
983 if (s->nb_channels == 2)
985 s->ms_stereo = get_bits(&s->gb, 1);
987 v = 0;
988 for (ch = 0; ch < s->nb_channels; ++ch)
990 a = get_bits(&s->gb, 1);
991 s->channel_coded[ch] = a;
992 v |= a;
994 /* if no channel coded, no need to go further */
995 /* XXX: fix potential framing problems */
996 if (!v)
998 goto next;
1001 bsize = s->frame_len_bits - s->block_len_bits;
1003 /* read total gain and extract corresponding number of bits for
1004 coef escape coding */
1005 total_gain = 1;
1006 for(;;)
1008 a = get_bits(&s->gb, 7);
1009 total_gain += a;
1010 if (a != 127)
1012 break;
1016 if (total_gain < 15)
1017 coef_nb_bits = 13;
1018 else if (total_gain < 32)
1019 coef_nb_bits = 12;
1020 else if (total_gain < 40)
1021 coef_nb_bits = 11;
1022 else if (total_gain < 45)
1023 coef_nb_bits = 10;
1024 else
1025 coef_nb_bits = 9;
1027 /* compute number of coefficients */
1028 n = s->coefs_end[bsize] - s->coefs_start;
1030 for(ch = 0; ch < s->nb_channels; ++ch)
1032 nb_coefs[ch] = n;
1034 /* complex coding */
1035 if (s->use_noise_coding)
1038 for(ch = 0; ch < s->nb_channels; ++ch)
1040 if (s->channel_coded[ch])
1042 int i, n, a;
1043 n = s->exponent_high_sizes[bsize];
1044 for(i=0;i<n;++i)
1046 a = get_bits(&s->gb, 1);
1047 s->high_band_coded[ch][i] = a;
1048 /* if noise coding, the coefficients are not transmitted */
1049 if (a)
1050 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
1054 for(ch = 0; ch < s->nb_channels; ++ch)
1056 if (s->channel_coded[ch])
1058 int i, n, val, code;
1060 n = s->exponent_high_sizes[bsize];
1061 val = (int)0x80000000;
1062 for(i=0;i<n;++i)
1064 if (s->high_band_coded[ch][i])
1066 if (val == (int)0x80000000)
1068 val = get_bits(&s->gb, 7) - 19;
1070 else
1072 //code = get_vlc(&s->gb, &s->hgain_vlc);
1073 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
1074 if (code < 0)
1076 return -6;
1078 val += code - 18;
1080 s->high_band_values[ch][i] = val;
1087 /* exponents can be reused in short blocks. */
1088 if ((s->block_len_bits == s->frame_len_bits) || get_bits(&s->gb, 1))
1090 for(ch = 0; ch < s->nb_channels; ++ch)
1092 if (s->channel_coded[ch])
1094 if (s->use_exp_vlc)
1096 if (decode_exp_vlc(s, ch) < 0)
1098 return -7;
1101 else
1103 decode_exp_lsp(s, ch);
1105 s->exponents_bsize[ch] = bsize;
1110 /* parse spectral coefficients : just RLE encoding */
1111 for(ch = 0; ch < s->nb_channels; ++ch)
1113 if (s->channel_coded[ch])
1115 VLC *coef_vlc;
1116 int level, run, sign, tindex;
1117 int16_t *ptr, *eptr;
1118 const int16_t *level_table, *run_table;
1120 /* special VLC tables are used for ms stereo because
1121 there is potentially less energy there */
1122 tindex = (ch == 1 && s->ms_stereo);
1123 coef_vlc = &s->coef_vlc[tindex];
1124 run_table = s->run_table[tindex];
1125 level_table = s->level_table[tindex];
1126 /* XXX: optimize */
1127 ptr = &s->coefs1[ch][0];
1128 eptr = ptr + nb_coefs[ch];
1129 memset(ptr, 0, s->block_len * sizeof(int16_t));
1131 for(;;)
1133 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
1134 //code = get_vlc(&s->gb, coef_vlc);
1135 if (code < 0)
1137 return -8;
1139 if (code == 1)
1141 /* EOB */
1142 break;
1144 else if (code == 0)
1146 /* escape */
1147 level = get_bits(&s->gb, coef_nb_bits);
1148 /* NOTE: this is rather suboptimal. reading
1149 block_len_bits would be better */
1150 run = get_bits(&s->gb, s->frame_len_bits);
1152 else
1154 /* normal code */
1155 run = run_table[code];
1156 level = level_table[code];
1158 sign = get_bits(&s->gb, 1);
1159 if (!sign)
1160 level = -level;
1161 ptr += run;
1162 if (ptr >= eptr)
1164 break;
1166 *ptr++ = level;
1169 /* NOTE: EOB can be omitted */
1170 if (ptr >= eptr)
1171 break;
1174 if (s->version == 1 && s->nb_channels >= 2)
1176 align_get_bits(&s->gb);
1181 int n4 = s->block_len >> 1;
1184 mdct_norm = 0x10000>>(s->block_len_bits-1);
1186 if (s->version == 1)
1188 mdct_norm *= fixtoi32(fixsqrt32(itofix32(n4)));
1193 /* finally compute the MDCT coefficients */
1194 for(ch = 0; ch < s->nb_channels; ++ch)
1196 if (s->channel_coded[ch])
1198 int16_t *coefs1;
1199 fixed32 *exponents, *exp_ptr;
1200 fixed32 *coefs, atemp;
1201 fixed64 mult;
1202 fixed64 mult1;
1203 fixed32 noise, temp1, temp2, mult2;
1204 int i, j, n, n1, last_high_band, esize;
1205 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
1207 //total_gain, coefs1, mdctnorm are lossless
1209 coefs1 = s->coefs1[ch];
1210 exponents = s->exponents[ch];
1211 esize = s->exponents_bsize[ch];
1212 coefs = (*(s->coefs))[ch];
1214 n=0;
1217 * The calculation of coefs has a shift right by 2 built in. This prepares samples
1218 * for the Tremor IMDCT which uses a slightly different fixed format then the ffmpeg one.
1219 * If the old ffmpeg imdct is used, each shift storing into coefs should be reduced by 1.
1220 * See SVN logs for details.
1224 if (s->use_noise_coding)
1226 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1228 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1229 mult = mult* mdct_norm; //what the hell? This is actually fixed64*2^16!
1230 mult1 = mult;
1232 /* very low freqs : noise */
1233 for(i = 0;i < s->coefs_start; ++i)
1235 *coefs++ = fixmul32( (fixmul32(s->noise_table[s->noise_index],(*exponents++))>>4),Fixed32From64(mult1)) >>2;
1236 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1239 n1 = s->exponent_high_sizes[bsize];
1241 /* compute power of high bands */
1242 exp_ptr = exponents +
1243 s->high_band_start[bsize] -
1244 s->coefs_start;
1245 last_high_band = 0; /* avoid warning */
1246 for (j=0;j<n1;++j)
1248 n = s->exponent_high_bands[s->frame_len_bits -
1249 s->block_len_bits][j];
1250 if (s->high_band_coded[ch][j])
1252 fixed32 e2, v;
1253 e2 = 0;
1254 for(i = 0;i < n; ++i)
1256 /*v is noramlized later on so its fixed format is irrelevant*/
1257 v = exp_ptr[i]>>4;
1258 e2 += fixmul32(v, v)>>3;
1260 exp_power[j] = e2/n; /*n is an int...*/
1261 last_high_band = j;
1263 exp_ptr += n;
1266 /* main freqs and high freqs */
1267 for(j=-1;j<n1;++j)
1269 if (j < 0)
1271 n = s->high_band_start[bsize] -
1272 s->coefs_start;
1274 else
1276 n = s->exponent_high_bands[s->frame_len_bits -
1277 s->block_len_bits][j];
1279 if (j >= 0 && s->high_band_coded[ch][j])
1281 /* use noise with specified power */
1282 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1283 mult1 = (fixed64)fixsqrt32(tmp);
1284 /* XXX: use a table */
1285 /*mult1 is 48.16, pow_table is 48.16*/
1286 mult1 = mult1 * pow_table[s->high_band_values[ch][j]+20] >> PRECISION;
1288 /*this step has a fairly high degree of error for some reason*/
1289 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1291 mult1 = mult1*mdct_norm>>PRECISION;
1292 for(i = 0;i < n; ++i)
1294 noise = s->noise_table[s->noise_index];
1295 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1296 *coefs++ = fixmul32((fixmul32(*exponents,noise)>>4),Fixed32From64(mult1)) >>2;
1297 ++exponents;
1300 else
1302 /* coded values + small noise */
1303 for(i = 0;i < n; ++i)
1305 // PJJ: check code path
1306 noise = s->noise_table[s->noise_index];
1307 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1309 /*don't forget to renormalize the noise*/
1310 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1311 temp2 = fixmul32(*exponents, mult>>18);
1312 *coefs++ = fixmul32(temp1, temp2);
1313 ++exponents;
1318 /* very high freqs : noise */
1319 n = s->block_len - s->coefs_end[bsize];
1320 mult2 = fixmul32(mult>>16,exponents[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1321 for (i = 0; i < n; ++i)
1323 /*renormalize the noise product and then reduce to 14.18 precison*/
1324 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>6;
1326 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1329 else
1331 /*Noise coding not used, simply convert from exp to fixed representation*/
1333 fixed32 mult3 = (fixed32)(fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch])));
1334 mult3 = fixmul32(mult3, mdct_norm);
1336 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1337 for(i=0; i<s->coefs_start; i++)
1338 *coefs++=0;
1340 n = nb_coefs[ch];
1342 /* XXX: optimize more, unrolling this loop in asm might be a good idea */
1344 for(i = 0;i < n; ++i)
1346 atemp = (coefs1[i] * mult3)>>2; /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1347 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1349 n = s->block_len - s->coefs_end[bsize];
1350 memset(coefs, 0, n*sizeof(fixed32));
1357 if (s->ms_stereo && s->channel_coded[1])
1359 fixed32 a, b;
1360 int i;
1361 fixed32 (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1363 /* nominal case for ms stereo: we do it before mdct */
1364 /* no need to optimize this case because it should almost
1365 never happen */
1366 if (!s->channel_coded[0])
1368 memset((*(s->coefs))[0], 0, sizeof(fixed32) * s->block_len);
1369 s->channel_coded[0] = 1;
1372 for(i = 0; i < s->block_len; ++i)
1374 a = (*coefs)[0][i];
1375 b = (*coefs)[1][i];
1376 (*coefs)[0][i] = a + b;
1377 (*coefs)[1][i] = a - b;
1381 for(ch = 0; ch < s->nb_channels; ++ch)
1383 if (s->channel_coded[ch])
1385 int n4, index, n;
1387 n = s->block_len;
1388 n4 = s->block_len >>1;
1390 /*faster IMDCT from Vorbis*/
1391 mdct_backward( (1 << (s->block_len_bits+1)), (int32_t*)(*(s->coefs))[ch], (int32_t*)scratch_buffer);
1393 /*slower but more easily understood IMDCT from FFMPEG*/
1394 //ff_imdct_calc(&s->mdct_ctx[bsize],
1395 // output,
1396 // (*(s->coefs))[ch]);
1399 /* add in the frame */
1400 index = (s->frame_len / 2) + s->block_pos - n4;
1401 wma_window(s, scratch_buffer, &((*s->frame_out)[ch][index]));
1405 /* specific fast case for ms-stereo : add to second
1406 channel if it is not coded */
1407 if (s->ms_stereo && !s->channel_coded[1])
1409 wma_window(s, scratch_buffer, &((*s->frame_out)[1][index]));
1413 next:
1414 /* update block number */
1415 ++s->block_num;
1416 s->block_pos += s->block_len;
1417 if (s->block_pos >= s->frame_len)
1419 return 1;
1421 else
1423 return 0;
1427 /* decode a frame of frame_len samples */
1428 static int wma_decode_frame(WMADecodeContext *s, int32_t *samples)
1430 int ret, i, n, ch, incr;
1431 int32_t *ptr;
1432 fixed32 *iptr;
1434 /* read each block */
1435 s->block_num = 0;
1436 s->block_pos = 0;
1439 for(;;)
1441 ret = wma_decode_block(s, samples);
1442 if (ret < 0)
1445 DEBUGF("wma_decode_block failed with code %d\n", ret);
1446 return -1;
1448 if (ret)
1450 break;
1454 /* return frame with full 30-bit precision */
1455 n = s->frame_len;
1456 incr = s->nb_channels;
1457 for(ch = 0; ch < s->nb_channels; ++ch)
1459 ptr = samples + ch;
1460 iptr = &((*s->frame_out)[ch][0]);
1462 for (i=0;i<n;++i)
1464 *ptr = (*iptr++);
1465 ptr += incr;
1468 memmove(&((*s->frame_out)[ch][0]), &((*s->frame_out)[ch][s->frame_len]),
1469 s->frame_len * sizeof(fixed32));
1472 return 0;
1475 /* Initialise the superframe decoding */
1477 int wma_decode_superframe_init(WMADecodeContext* s,
1478 uint8_t *buf, /*input*/
1479 int buf_size)
1481 if (buf_size==0)
1483 s->last_superframe_len = 0;
1484 return 0;
1487 s->current_frame = 0;
1489 init_get_bits(&s->gb, buf, buf_size*8);
1491 if (s->use_bit_reservoir)
1493 /* read super frame header */
1494 get_bits(&s->gb, 4); /* super frame index */
1495 s->nb_frames = get_bits(&s->gb, 4);
1497 if (s->last_superframe_len == 0)
1498 s->nb_frames --;
1499 else if (s->nb_frames == 0)
1500 s->nb_frames++;
1502 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1503 } else {
1504 s->nb_frames = 1;
1507 return 1;
1511 /* Decode a single frame in the current superframe - return -1 if
1512 there was a decoding error, or the number of samples decoded.
1515 int wma_decode_superframe_frame(WMADecodeContext* s,
1516 int32_t* samples, /*output*/
1517 uint8_t *buf, /*input*/
1518 int buf_size)
1520 int pos, len;
1521 uint8_t *q;
1522 int done = 0;
1523 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1525 if (s->last_superframe_len > 0)
1527 /* add s->bit_offset bits to last frame */
1528 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1529 MAX_CODED_SUPERFRAME_SIZE)
1531 DEBUGF("superframe size too large error\n");
1532 goto fail;
1534 q = s->last_superframe + s->last_superframe_len;
1535 len = s->bit_offset;
1536 while (len > 0)
1538 *q++ = (get_bits)(&s->gb, 8);
1539 len -= 8;
1541 if (len > 0)
1543 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1546 /* XXX: s->bit_offset bits into last frame */
1547 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1548 /* skip unused bits */
1549 if (s->last_bitoffset > 0)
1550 skip_bits(&s->gb, s->last_bitoffset);
1552 /* this frame is stored in the last superframe and in the
1553 current one */
1554 if (wma_decode_frame(s, samples) < 0)
1556 goto fail;
1558 done = 1;
1561 /* read each frame starting from s->bit_offset */
1562 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1563 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1564 len = pos & 7;
1565 if (len > 0)
1566 skip_bits(&s->gb, len);
1568 s->reset_block_lengths = 1;
1571 /* If we haven't decoded a frame yet, do it now */
1572 if (!done)
1574 if (wma_decode_frame(s, samples) < 0)
1576 goto fail;
1580 s->current_frame++;
1582 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1584 /* we copy the end of the frame in the last frame buffer */
1585 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1586 s->last_bitoffset = pos & 7;
1587 pos >>= 3;
1588 len = buf_size - pos;
1589 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1591 DEBUGF("superframe size too large error after decodeing\n");
1592 goto fail;
1594 s->last_superframe_len = len;
1595 memcpy(s->last_superframe, buf + pos, len);
1598 return s->frame_len;
1600 fail:
1601 /* when error, we reset the bit reservoir */
1603 s->last_superframe_len = 0;
1604 return -1;