Wrap some of the more ridiculously long lines introduced in the previous revision...
[kugel-rb.git] / apps / codecs / libwma / wmadeci.c
blob2697db0d8d463f07d129b4399f2ac55f700ffed2
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 /*clear stereo setting to avoid glitches when switching stereo->mono*/
332 s->channel_coded[0]=0;
333 s->channel_coded[1]=0;
334 s->ms_stereo=0;
336 s->sample_rate = wfx->rate;
337 s->nb_channels = wfx->channels;
338 s->bit_rate = wfx->bitrate;
339 s->block_align = wfx->blockalign;
341 s->coefs = &coefsarray;
342 s->frame_out = &frame_out_buf;
344 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
345 s->version = 1;
346 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2 ) {
347 s->version = 2;
348 } else {
349 /*one of those other wma flavors that don't have GPLed decoders */
350 return -1;
353 /* extract flag infos */
354 flags1 = 0;
355 flags2 = 0;
356 extradata = wfx->data;
357 if (s->version == 1 && wfx->datalen >= 4) {
358 flags1 = extradata[0] | (extradata[1] << 8);
359 flags2 = extradata[2] | (extradata[3] << 8);
360 }else if (s->version == 2 && wfx->datalen >= 6){
361 flags1 = extradata[0] | (extradata[1] << 8) |
362 (extradata[2] << 16) | (extradata[3] << 24);
363 flags2 = extradata[4] | (extradata[5] << 8);
365 s->use_exp_vlc = flags2 & 0x0001;
366 s->use_bit_reservoir = flags2 & 0x0002;
367 s->use_variable_block_len = flags2 & 0x0004;
369 /* compute MDCT block size */
370 if (s->sample_rate <= 16000){
371 s->frame_len_bits = 9;
372 }else if (s->sample_rate <= 22050 ||
373 (s->sample_rate <= 32000 && s->version == 1)){
374 s->frame_len_bits = 10;
375 }else{
376 s->frame_len_bits = 11;
378 s->frame_len = 1 << s->frame_len_bits;
379 if (s-> use_variable_block_len)
381 int nb_max, nb;
382 nb = ((flags2 >> 3) & 3) + 1;
383 if ((s->bit_rate / s->nb_channels) >= 32000)
385 nb += 2;
387 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
388 if (nb > nb_max)
389 nb = nb_max;
390 s->nb_block_sizes = nb + 1;
392 else
394 s->nb_block_sizes = 1;
397 /* init rate dependant parameters */
398 s->use_noise_coding = 1;
399 high_freq = itofix64(s->sample_rate) >> 1;
402 /* if version 2, then the rates are normalized */
403 sample_rate1 = s->sample_rate;
404 if (s->version == 2)
406 if (sample_rate1 >= 44100)
407 sample_rate1 = 44100;
408 else if (sample_rate1 >= 22050)
409 sample_rate1 = 22050;
410 else if (sample_rate1 >= 16000)
411 sample_rate1 = 16000;
412 else if (sample_rate1 >= 11025)
413 sample_rate1 = 11025;
414 else if (sample_rate1 >= 8000)
415 sample_rate1 = 8000;
418 fixed64 tmp = itofix64(s->bit_rate);
419 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
420 bps = fixdiv64(tmp, tmp2);
421 fixed64 tim = bps * s->frame_len;
422 fixed64 tmpi = fixdiv64(tim,itofix64(8));
423 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
425 /* compute high frequency value and choose if noise coding should
426 be activated */
427 bps1 = bps;
428 if (s->nb_channels == 2)
429 bps1 = fixmul32(bps,0x1999a);
430 if (sample_rate1 == 44100)
432 if (bps1 >= 0x9c29)
433 s->use_noise_coding = 0;
434 else
435 high_freq = fixmul32(high_freq,0x6666);
437 else if (sample_rate1 == 22050)
439 if (bps1 >= 0x128f6)
440 s->use_noise_coding = 0;
441 else if (bps1 >= 0xb852)
442 high_freq = fixmul32(high_freq,0xb333);
443 else
444 high_freq = fixmul32(high_freq,0x999a);
446 else if (sample_rate1 == 16000)
448 if (bps > 0x8000)
449 high_freq = fixmul32(high_freq,0x8000);
450 else
451 high_freq = fixmul32(high_freq,0x4ccd);
453 else if (sample_rate1 == 11025)
455 high_freq = fixmul32(high_freq,0xb333);
457 else if (sample_rate1 == 8000)
459 if (bps <= 0xa000)
461 high_freq = fixmul32(high_freq,0x8000);
463 else if (bps > 0xc000)
465 s->use_noise_coding = 0;
467 else
469 high_freq = fixmul32(high_freq,0xa666);
472 else
474 if (bps >= 0xcccd)
476 high_freq = fixmul32(high_freq,0xc000);
478 else if (bps >= 0x999a)
480 high_freq = fixmul32(high_freq,0x999a);
482 else
484 high_freq = fixmul32(high_freq,0x8000);
488 /* compute the scale factor band sizes for each MDCT block size */
490 int a, b, pos, lpos, k, block_len, i, j, n;
491 const uint8_t *table;
493 if (s->version == 1)
495 s->coefs_start = 3;
497 else
499 s->coefs_start = 0;
501 for(k = 0; k < s->nb_block_sizes; ++k)
503 block_len = s->frame_len >> k;
505 if (s->version == 1)
507 lpos = 0;
508 for(i=0;i<25;++i)
510 a = wma_critical_freqs[i];
511 b = s->sample_rate;
512 pos = ((block_len * 2 * a) + (b >> 1)) / b;
513 if (pos > block_len)
514 pos = block_len;
515 s->exponent_bands[0][i] = pos - lpos;
516 if (pos >= block_len)
518 ++i;
519 break;
521 lpos = pos;
523 s->exponent_sizes[0] = i;
525 else
527 /* hardcoded tables */
528 table = NULL;
529 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
530 if (a < 3)
532 if (s->sample_rate >= 44100)
533 table = exponent_band_44100[a];
534 else if (s->sample_rate >= 32000)
535 table = exponent_band_32000[a];
536 else if (s->sample_rate >= 22050)
537 table = exponent_band_22050[a];
539 if (table)
541 n = *table++;
542 for(i=0;i<n;++i)
543 s->exponent_bands[k][i] = table[i];
544 s->exponent_sizes[k] = n;
546 else
548 j = 0;
549 lpos = 0;
550 for(i=0;i<25;++i)
552 a = wma_critical_freqs[i];
553 b = s->sample_rate;
554 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
555 pos <<= 2;
556 if (pos > block_len)
557 pos = block_len;
558 if (pos > lpos)
559 s->exponent_bands[k][j++] = pos - lpos;
560 if (pos >= block_len)
561 break;
562 lpos = pos;
564 s->exponent_sizes[k] = j;
568 /* max number of coefs */
569 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
570 /* high freq computation */
572 fixed32 tmp1 = high_freq*2; /* high_freq is a fixed32!*/
573 fixed32 tmp2=itofix32(s->sample_rate>>1);
574 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
577 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
578 s->sample_rate + 0.5);*/
580 n = s->exponent_sizes[k];
581 j = 0;
582 pos = 0;
583 for(i=0;i<n;++i)
585 int start, end;
586 start = pos;
587 pos += s->exponent_bands[k][i];
588 end = pos;
589 if (start < s->high_band_start[k])
590 start = s->high_band_start[k];
591 if (end > s->coefs_end[k])
592 end = s->coefs_end[k];
593 if (end > start)
594 s->exponent_high_bands[k][j++] = end - start;
596 s->exponent_high_sizes[k] = j;
600 /*Not using the ffmpeg IMDCT anymore*/
602 /* mdct_init_global();
604 for(i = 0; i < s->nb_block_sizes; ++i)
606 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
611 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
612 * In the worst case you can have 5 window sizes, 128 doubling up 2048
613 * Smaller windows are handled differently.
614 * Since we don't have malloc, just statically allocate this
616 fixed32 *temp[5];
617 temp[0] = stat0;
618 temp[1] = stat1;
619 temp[2] = stat2;
620 temp[3] = stat3;
621 temp[4] = stat4;
623 /* init MDCT windows : simple sinus window */
624 for(i = 0; i < s->nb_block_sizes; i++)
626 int n, j;
627 fixed32 alpha;
628 n = 1 << (s->frame_len_bits - i);
629 //window = av_malloc(sizeof(fixed32) * n);
630 window = temp[i];
632 //fixed32 n2 = itofix32(n<<1); //2x the window length
633 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
635 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
636 alpha = (1<<15)>>(s->frame_len_bits - i+1); /* this calculates 0.5/(2*n) */
637 for(j=0;j<n;++j)
639 fixed32 j2 = itofix32(j) + 0x8000;
640 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0); //alpha between 0 and pi/2
643 s->windows[i] = window;
647 s->reset_block_lengths = 1;
649 if (s->use_noise_coding)
651 /* init the noise generator */
652 if (s->use_exp_vlc)
654 s->noise_mult = 0x51f;
655 s->noise_table = noisetable_exp;
657 else
659 s->noise_mult = 0xa3d;
660 /* LSP values are simply 2x the EXP values */
661 for (i=0;i<NOISE_TAB_SIZE;++i)
662 noisetable_exp[i] = noisetable_exp[i]<< 1;
663 s->noise_table = noisetable_exp;
665 #if 0
667 unsigned int seed;
668 fixed32 norm;
669 seed = 1;
670 norm = 0; // PJJ: near as makes any diff to 0!
671 for (i=0;i<NOISE_TAB_SIZE;++i)
673 seed = seed * 314159 + 1;
674 s->noise_table[i] = itofix32((int)seed) * norm;
677 #endif
679 s->hgain_vlc.table = vlcbuf4;
680 s->hgain_vlc.table_allocated = VLCBUF4SIZE;
681 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
682 hgain_huffbits, 1, 1,
683 hgain_huffcodes, 2, 2, 0);
686 if (s->use_exp_vlc)
689 s->exp_vlc.table = vlcbuf3;
690 s->exp_vlc.table_allocated = VLCBUF3SIZE;
692 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
693 scale_huffbits, 1, 1,
694 scale_huffcodes, 4, 4, 0);
696 else
698 wma_lsp_to_curve_init(s, s->frame_len);
701 /* choose the VLC tables for the coefficients */
702 coef_vlc_table = 2;
703 if (s->sample_rate >= 32000)
705 if (bps1 < 0xb852)
706 coef_vlc_table = 0;
707 else if (bps1 < 0x128f6)
708 coef_vlc_table = 1;
711 runtabarray[0] = runtab0; runtabarray[1] = runtab1;
712 levtabarray[0] = levtab0; levtabarray[1] = levtab1;
714 s->coef_vlc[0].table = vlcbuf1;
715 s->coef_vlc[0].table_allocated = VLCBUF1SIZE;
716 s->coef_vlc[1].table = vlcbuf2;
717 s->coef_vlc[1].table_allocated = VLCBUF2SIZE;
720 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
721 &coef_vlcs[coef_vlc_table * 2], 0);
722 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
723 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
725 s->last_superframe_len = 0;
726 s->last_bitoffset = 0;
728 return 0;
732 /* compute x^-0.25 with an exponent and mantissa table. We use linear
733 interpolation to reduce the mantissa table size at a small speed
734 expense (linear interpolation approximately doubles the number of
735 bits of precision). */
736 static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
738 union {
739 float f;
740 unsigned int v;
741 } u, t;
742 unsigned int e, m;
743 fixed32 a, b;
745 u.f = fixtof64(x);
746 e = u.v >> 23;
747 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
748 /* build interpolation scale: 1 <= t < 2. */
749 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
750 a = s->lsp_pow_m_table1[m];
751 b = s->lsp_pow_m_table2[m];
753 /* lsp_pow_e_table contains 32.32 format */
754 /* TODO: Since we're unlikely have value that cover the whole
755 * IEEE754 range, we probably don't need to have all possible exponents */
757 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
760 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
762 fixed32 wdel, a, b, temp, temp2;
763 int i, m;
765 wdel = fixdiv32(M_PI_F, itofix32(frame_len));
766 temp = fixdiv32(itofix32(1), itofix32(frame_len));
767 for (i=0; i<frame_len; ++i)
769 /* TODO: can probably reuse the trig_init values here */
770 fsincos((temp*i)<<15, &temp2);
771 /* get 3 bits headroom + 1 bit from not doubleing the values */
772 s->lsp_cos_table[i] = temp2>>3;
775 /* NOTE: these two tables are needed to avoid two operations in
776 pow_m1_4 */
777 b = itofix32(1);
778 int ix = 0;
780 /*double check this later*/
781 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
783 m = (1 << LSP_POW_BITS) + i;
784 a = pow_a_table[ix++]<<4;
785 s->lsp_pow_m_table1[i] = 2 * a - b;
786 s->lsp_pow_m_table2[i] = b - a;
787 b = a;
792 /* NOTE: We use the same code as Vorbis here */
793 /* XXX: optimize it further with SSE/3Dnow */
794 static void wma_lsp_to_curve(WMADecodeContext *s,
795 fixed32 *out,
796 fixed32 *val_max_ptr,
797 int n,
798 fixed32 *lsp)
800 int i, j;
801 fixed32 p, q, w, v, val_max, temp, temp2;
803 val_max = 0;
804 for(i=0;i<n;++i)
806 /* shift by 2 now to reduce rounding error,
807 * we can renormalize right before pow_m1_4
810 p = 0x8000<<5;
811 q = 0x8000<<5;
812 w = s->lsp_cos_table[i];
814 for (j=1;j<NB_LSP_COEFS;j+=2)
816 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
817 temp2 = ((w - (lsp[j - 1]<<11)));
818 temp = q;
820 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
821 q = fixmul32b(q, temp2 )<<4;
822 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
825 /* 2 in 5.27 format is 0x10000000 */
826 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
827 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
829 v = (p + q) >>9; /* p/q end up as 16.16 */
830 v = pow_m1_4(s, v);
831 if (v > val_max)
832 val_max = v;
833 out[i] = v;
836 *val_max_ptr = val_max;
839 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
840 static void decode_exp_lsp(WMADecodeContext *s, int ch)
842 fixed32 lsp_coefs[NB_LSP_COEFS];
843 int val, i;
845 for (i = 0; i < NB_LSP_COEFS; ++i)
847 if (i == 0 || i >= 8)
848 val = get_bits(&s->gb, 3);
849 else
850 val = get_bits(&s->gb, 4);
851 lsp_coefs[i] = lsp_codebook[i][val];
854 wma_lsp_to_curve(s,
855 s->exponents[ch],
856 &s->max_exponent[ch],
857 s->block_len,
858 lsp_coefs);
861 /* decode exponents coded with VLC codes */
862 static int decode_exp_vlc(WMADecodeContext *s, int ch)
864 int last_exp, n, code;
865 const uint16_t *ptr, *band_ptr;
866 fixed32 v, max_scale;
867 fixed32 *q,*q_end;
869 /*accommodate the 60 negative indices */
870 const fixed32 *pow_10_to_yover16_ptr = &pow_10_to_yover16[61];
872 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
873 ptr = band_ptr;
874 q = s->exponents[ch];
875 q_end = q + s->block_len;
876 max_scale = 0;
879 if (s->version == 1) //wmav1 only
881 last_exp = get_bits(&s->gb, 5) + 10;
882 /* XXX: use a table */
883 v = pow_10_to_yover16_ptr[last_exp];
884 max_scale = v;
885 n = *ptr++;
888 *q++ = v;
890 while (--n);
891 } else {
892 last_exp = 36;
895 while (q < q_end)
897 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
898 if (code < 0)
900 return -1;
902 /* NOTE: this offset is the same as MPEG4 AAC ! */
903 last_exp += code - 60;
904 /* XXX: use a table */
905 v = pow_10_to_yover16_ptr[last_exp];
906 if (v > max_scale)
908 max_scale = v;
910 n = *ptr++;
913 *q++ = v;
916 while (--n);
919 s->max_exponent[ch] = max_scale;
920 return 0;
923 /* return 0 if OK. return 1 if last block of frame. return -1 if
924 unrecorrable error. */
925 static int wma_decode_block(WMADecodeContext *s, int32_t *scratch_buffer)
927 int n, v, a, ch, code, bsize;
928 int coef_nb_bits, total_gain;
929 int nb_coefs[MAX_CHANNELS];
930 fixed32 mdct_norm;
932 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
934 /* compute current block length */
935 if (s->use_variable_block_len)
937 n = av_log2(s->nb_block_sizes - 1) + 1;
939 if (s->reset_block_lengths)
941 s->reset_block_lengths = 0;
942 v = get_bits(&s->gb, n);
943 if (v >= s->nb_block_sizes)
945 return -2;
947 s->prev_block_len_bits = s->frame_len_bits - v;
948 v = get_bits(&s->gb, n);
949 if (v >= s->nb_block_sizes)
951 return -3;
953 s->block_len_bits = s->frame_len_bits - v;
955 else
957 /* update block lengths */
958 s->prev_block_len_bits = s->block_len_bits;
959 s->block_len_bits = s->next_block_len_bits;
961 v = get_bits(&s->gb, n);
963 if (v >= s->nb_block_sizes)
965 // rb->splash(HZ*4, "v was %d", v); //5, 7
966 return -4; //this is it
968 else{
969 //rb->splash(HZ, "passed v block (%d)!", v);
971 s->next_block_len_bits = s->frame_len_bits - v;
973 else
975 /* fixed block len */
976 s->next_block_len_bits = s->frame_len_bits;
977 s->prev_block_len_bits = s->frame_len_bits;
978 s->block_len_bits = s->frame_len_bits;
980 /* now check if the block length is coherent with the frame length */
981 s->block_len = 1 << s->block_len_bits;
983 if ((s->block_pos + s->block_len) > s->frame_len)
985 return -5; //oddly 32k sample from tracker fails here
988 if (s->nb_channels == 2)
990 s->ms_stereo = get_bits(&s->gb, 1);
992 v = 0;
993 for (ch = 0; ch < s->nb_channels; ++ch)
995 a = get_bits(&s->gb, 1);
996 s->channel_coded[ch] = a;
997 v |= a;
999 /* if no channel coded, no need to go further */
1000 /* XXX: fix potential framing problems */
1001 if (!v)
1003 goto next;
1006 bsize = s->frame_len_bits - s->block_len_bits;
1008 /* read total gain and extract corresponding number of bits for
1009 coef escape coding */
1010 total_gain = 1;
1011 for(;;)
1013 a = get_bits(&s->gb, 7);
1014 total_gain += a;
1015 if (a != 127)
1017 break;
1021 if (total_gain < 15)
1022 coef_nb_bits = 13;
1023 else if (total_gain < 32)
1024 coef_nb_bits = 12;
1025 else if (total_gain < 40)
1026 coef_nb_bits = 11;
1027 else if (total_gain < 45)
1028 coef_nb_bits = 10;
1029 else
1030 coef_nb_bits = 9;
1032 /* compute number of coefficients */
1033 n = s->coefs_end[bsize] - s->coefs_start;
1035 for(ch = 0; ch < s->nb_channels; ++ch)
1037 nb_coefs[ch] = n;
1039 /* complex coding */
1040 if (s->use_noise_coding)
1043 for(ch = 0; ch < s->nb_channels; ++ch)
1045 if (s->channel_coded[ch])
1047 int i, n, a;
1048 n = s->exponent_high_sizes[bsize];
1049 for(i=0;i<n;++i)
1051 a = get_bits(&s->gb, 1);
1052 s->high_band_coded[ch][i] = a;
1053 /* if noise coding, the coefficients are not transmitted */
1054 if (a)
1055 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
1059 for(ch = 0; ch < s->nb_channels; ++ch)
1061 if (s->channel_coded[ch])
1063 int i, n, val, code;
1065 n = s->exponent_high_sizes[bsize];
1066 val = (int)0x80000000;
1067 for(i=0;i<n;++i)
1069 if (s->high_band_coded[ch][i])
1071 if (val == (int)0x80000000)
1073 val = get_bits(&s->gb, 7) - 19;
1075 else
1077 //code = get_vlc(&s->gb, &s->hgain_vlc);
1078 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
1079 if (code < 0)
1081 return -6;
1083 val += code - 18;
1085 s->high_band_values[ch][i] = val;
1092 /* exponents can be reused in short blocks. */
1093 if ((s->block_len_bits == s->frame_len_bits) || get_bits(&s->gb, 1))
1095 for(ch = 0; ch < s->nb_channels; ++ch)
1097 if (s->channel_coded[ch])
1099 if (s->use_exp_vlc)
1101 if (decode_exp_vlc(s, ch) < 0)
1103 return -7;
1106 else
1108 decode_exp_lsp(s, ch);
1110 s->exponents_bsize[ch] = bsize;
1115 /* parse spectral coefficients : just RLE encoding */
1116 for(ch = 0; ch < s->nb_channels; ++ch)
1118 if (s->channel_coded[ch])
1120 VLC *coef_vlc;
1121 int level, run, sign, tindex;
1122 int16_t *ptr, *eptr;
1123 const int16_t *level_table, *run_table;
1125 /* special VLC tables are used for ms stereo because
1126 there is potentially less energy there */
1127 tindex = (ch == 1 && s->ms_stereo);
1128 coef_vlc = &s->coef_vlc[tindex];
1129 run_table = s->run_table[tindex];
1130 level_table = s->level_table[tindex];
1131 /* XXX: optimize */
1132 ptr = &s->coefs1[ch][0];
1133 eptr = ptr + nb_coefs[ch];
1134 memset(ptr, 0, s->block_len * sizeof(int16_t));
1136 for(;;)
1138 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
1139 //code = get_vlc(&s->gb, coef_vlc);
1140 if (code < 0)
1142 return -8;
1144 if (code == 1)
1146 /* EOB */
1147 break;
1149 else if (code == 0)
1151 /* escape */
1152 level = get_bits(&s->gb, coef_nb_bits);
1153 /* NOTE: this is rather suboptimal. reading
1154 block_len_bits would be better */
1155 run = get_bits(&s->gb, s->frame_len_bits);
1157 else
1159 /* normal code */
1160 run = run_table[code];
1161 level = level_table[code];
1163 sign = get_bits(&s->gb, 1);
1164 if (!sign)
1165 level = -level;
1166 ptr += run;
1167 if (ptr >= eptr)
1169 break;
1171 *ptr++ = level;
1174 /* NOTE: EOB can be omitted */
1175 if (ptr >= eptr)
1176 break;
1179 if (s->version == 1 && s->nb_channels >= 2)
1181 align_get_bits(&s->gb);
1186 int n4 = s->block_len >> 1;
1189 mdct_norm = 0x10000>>(s->block_len_bits-1);
1191 if (s->version == 1)
1193 mdct_norm *= fixtoi32(fixsqrt32(itofix32(n4)));
1198 /* finally compute the MDCT coefficients */
1199 for(ch = 0; ch < s->nb_channels; ++ch)
1201 if (s->channel_coded[ch])
1203 int16_t *coefs1;
1204 fixed32 *exponents;
1205 fixed32 *coefs, atemp;
1206 fixed64 mult;
1207 fixed64 mult1;
1208 fixed32 noise, temp1, temp2, mult2;
1209 int i, j, n, n1, last_high_band, esize;
1210 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
1212 //total_gain, coefs1, mdctnorm are lossless
1214 coefs1 = s->coefs1[ch];
1215 exponents = s->exponents[ch];
1216 esize = s->exponents_bsize[ch];
1217 coefs = (*(s->coefs))[ch];
1218 n=0;
1221 * The calculation of coefs has a shift right by 2 built in. This
1222 * prepares samples for the Tremor IMDCT which uses a slightly
1223 * different fixed format then the ffmpeg one. If the old ffmpeg
1224 * imdct is used, each shift storing into coefs should be reduced
1225 * by 1.
1226 * See SVN logs for details.
1230 if (s->use_noise_coding)
1232 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1234 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1235 mult = mult* mdct_norm;
1236 mult1 = mult;
1238 /* very low freqs : noise */
1239 for(i = 0;i < s->coefs_start; ++i)
1241 *coefs++ = fixmul32( (fixmul32(s->noise_table[s->noise_index],
1242 exponents[i<<bsize>>esize])>>4),Fixed32From64(mult1)) >>2;
1243 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1246 n1 = s->exponent_high_sizes[bsize];
1248 /* compute power of high bands */
1249 exponents = s->exponents[ch] +(s->high_band_start[bsize]<<bsize);
1250 last_high_band = 0; /* avoid warning */
1251 for (j=0;j<n1;++j)
1253 n = s->exponent_high_bands[s->frame_len_bits -
1254 s->block_len_bits][j];
1255 if (s->high_band_coded[ch][j])
1257 fixed32 e2, v;
1258 e2 = 0;
1259 for(i = 0;i < n; ++i)
1261 /*v is noramlized later on so its fixed format is irrelevant*/
1262 v = exponents[i<<bsize>>esize]>>4;
1263 e2 += fixmul32(v, v)>>3;
1265 exp_power[j] = e2/n; /*n is an int...*/
1266 last_high_band = j;
1268 exponents += n<<bsize;
1271 /* main freqs and high freqs */
1272 exponents = s->exponents[ch] + (s->coefs_start<<bsize);
1273 for(j=-1;j<n1;++j)
1275 if (j < 0)
1277 n = s->high_band_start[bsize] -
1278 s->coefs_start;
1280 else
1282 n = s->exponent_high_bands[s->frame_len_bits -
1283 s->block_len_bits][j];
1285 if (j >= 0 && s->high_band_coded[ch][j])
1287 /* use noise with specified power */
1288 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1290 /*mult1 is 48.16, pow_table is 48.16*/
1291 mult1 = fixmul32(fixsqrt32(tmp),
1292 pow_table[s->high_band_values[ch][j]+20]) >> 16;
1294 /*this step has a fairly high degree of error for some reason*/
1295 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1296 mult1 = mult1*mdct_norm>>PRECISION;
1297 for(i = 0;i < n; ++i)
1299 noise = s->noise_table[s->noise_index];
1300 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1301 *coefs++ = fixmul32((fixmul32(exponents[i<<bsize>>esize],noise)>>4),
1302 Fixed32From64(mult1)) >>2;
1305 exponents += n<<bsize;
1307 else
1309 /* coded values + small noise */
1310 for(i = 0;i < n; ++i)
1312 noise = s->noise_table[s->noise_index];
1313 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1315 /*don't forget to renormalize the noise*/
1316 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1317 temp2 = fixmul32(exponents[i<<bsize>>esize], mult>>18);
1318 *coefs++ = fixmul32(temp1, temp2);
1320 exponents += n<<bsize;
1324 /* very high freqs : noise */
1325 n = s->block_len - s->coefs_end[bsize];
1326 mult2 = fixmul32(mult>>16,exponents[((-1<<bsize))>>esize]) ;
1327 for (i = 0; i < n; ++i)
1329 /*renormalize the noise product and then reduce to 14.18 precison*/
1330 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>6;
1332 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1335 else
1337 /*Noise coding not used, simply convert from exp to fixed representation*/
1339 fixed32 mult3 = (fixed32)(fixdiv64(pow_table[total_gain+20],
1340 Fixed32To64(s->max_exponent[ch])));
1341 mult3 = fixmul32(mult3, mdct_norm);
1343 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1344 for(i=0; i<s->coefs_start; i++)
1345 *coefs++=0;
1347 n = nb_coefs[ch];
1349 /* XXX: optimize more, unrolling this loop in asm
1350 might be a good idea */
1352 for(i = 0;i < n; ++i)
1354 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1355 atemp = (coefs1[i] * mult3)>>2;
1356 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1358 n = s->block_len - s->coefs_end[bsize];
1359 memset(coefs, 0, n*sizeof(fixed32));
1366 if (s->ms_stereo && s->channel_coded[1])
1368 fixed32 a, b;
1369 int i;
1370 fixed32 (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1372 /* nominal case for ms stereo: we do it before mdct */
1373 /* no need to optimize this case because it should almost
1374 never happen */
1375 if (!s->channel_coded[0])
1377 memset((*(s->coefs))[0], 0, sizeof(fixed32) * s->block_len);
1378 s->channel_coded[0] = 1;
1381 for(i = 0; i < s->block_len; ++i)
1383 a = (*coefs)[0][i];
1384 b = (*coefs)[1][i];
1385 (*coefs)[0][i] = a + b;
1386 (*coefs)[1][i] = a - b;
1390 for(ch = 0; ch < s->nb_channels; ++ch)
1392 if (s->channel_coded[ch])
1394 int n4, index, n;
1396 n = s->block_len;
1397 n4 = s->block_len >>1;
1399 /*faster IMDCT from Vorbis*/
1400 mdct_backward( (1 << (s->block_len_bits+1)), (int32_t*)(*(s->coefs))[ch], (int32_t*)scratch_buffer);
1402 /*slower but more easily understood IMDCT from FFMPEG*/
1403 //ff_imdct_calc(&s->mdct_ctx[bsize],
1404 // output,
1405 // (*(s->coefs))[ch]);
1408 /* add in the frame */
1409 index = (s->frame_len / 2) + s->block_pos - n4;
1410 wma_window(s, scratch_buffer, &((*s->frame_out)[ch][index]));
1414 /* specific fast case for ms-stereo : add to second
1415 channel if it is not coded */
1416 if (s->ms_stereo && !s->channel_coded[1])
1418 wma_window(s, scratch_buffer, &((*s->frame_out)[1][index]));
1422 next:
1423 /* update block number */
1424 ++s->block_num;
1425 s->block_pos += s->block_len;
1426 if (s->block_pos >= s->frame_len)
1428 return 1;
1430 else
1432 return 0;
1436 /* decode a frame of frame_len samples */
1437 static int wma_decode_frame(WMADecodeContext *s, int32_t *samples)
1439 int ret, i, n, ch, incr;
1440 int32_t *ptr;
1441 fixed32 *iptr;
1443 /* read each block */
1444 s->block_num = 0;
1445 s->block_pos = 0;
1448 for(;;)
1450 ret = wma_decode_block(s, samples);
1451 if (ret < 0)
1454 DEBUGF("wma_decode_block failed with code %d\n", ret);
1455 return -1;
1457 if (ret)
1459 break;
1463 /* return frame with full 30-bit precision */
1464 n = s->frame_len;
1465 incr = s->nb_channels;
1466 for(ch = 0; ch < s->nb_channels; ++ch)
1468 ptr = samples + ch;
1469 iptr = &((*s->frame_out)[ch][0]);
1471 for (i=0;i<n;++i)
1473 *ptr = (*iptr++);
1474 ptr += incr;
1477 memmove(&((*s->frame_out)[ch][0]), &((*s->frame_out)[ch][s->frame_len]),
1478 s->frame_len * sizeof(fixed32));
1481 return 0;
1484 /* Initialise the superframe decoding */
1486 int wma_decode_superframe_init(WMADecodeContext* s,
1487 uint8_t *buf, /*input*/
1488 int buf_size)
1490 if (buf_size==0)
1492 s->last_superframe_len = 0;
1493 return 0;
1496 s->current_frame = 0;
1498 init_get_bits(&s->gb, buf, buf_size*8);
1500 if (s->use_bit_reservoir)
1502 /* read super frame header */
1503 get_bits(&s->gb, 4); /* super frame index */
1504 s->nb_frames = get_bits(&s->gb, 4);
1506 if (s->last_superframe_len == 0)
1507 s->nb_frames --;
1508 else if (s->nb_frames == 0)
1509 s->nb_frames++;
1511 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1512 } else {
1513 s->nb_frames = 1;
1516 return 1;
1520 /* Decode a single frame in the current superframe - return -1 if
1521 there was a decoding error, or the number of samples decoded.
1524 int wma_decode_superframe_frame(WMADecodeContext* s,
1525 int32_t* samples, /*output*/
1526 uint8_t *buf, /*input*/
1527 int buf_size)
1529 int pos, len;
1530 uint8_t *q;
1531 int done = 0;
1532 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1534 if (s->last_superframe_len > 0)
1536 /* add s->bit_offset bits to last frame */
1537 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1538 MAX_CODED_SUPERFRAME_SIZE)
1540 DEBUGF("superframe size too large error\n");
1541 goto fail;
1543 q = s->last_superframe + s->last_superframe_len;
1544 len = s->bit_offset;
1545 while (len > 0)
1547 *q++ = (get_bits)(&s->gb, 8);
1548 len -= 8;
1550 if (len > 0)
1552 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1555 /* XXX: s->bit_offset bits into last frame */
1556 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1557 /* skip unused bits */
1558 if (s->last_bitoffset > 0)
1559 skip_bits(&s->gb, s->last_bitoffset);
1561 /* this frame is stored in the last superframe and in the
1562 current one */
1563 if (wma_decode_frame(s, samples) < 0)
1565 goto fail;
1567 done = 1;
1570 /* read each frame starting from s->bit_offset */
1571 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1572 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1573 len = pos & 7;
1574 if (len > 0)
1575 skip_bits(&s->gb, len);
1577 s->reset_block_lengths = 1;
1580 /* If we haven't decoded a frame yet, do it now */
1581 if (!done)
1583 if (wma_decode_frame(s, samples) < 0)
1585 goto fail;
1589 s->current_frame++;
1591 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1593 /* we copy the end of the frame in the last frame buffer */
1594 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1595 s->last_bitoffset = pos & 7;
1596 pos >>= 3;
1597 len = buf_size - pos;
1598 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1600 DEBUGF("superframe size too large error after decodeing\n");
1601 goto fail;
1603 s->last_superframe_len = len;
1604 memcpy(s->last_superframe, buf + pos, len);
1607 return s->frame_len;
1609 fail:
1610 /* when error, we reset the bit reservoir */
1612 s->last_superframe_len = 0;
1613 return -1;