Fix for WMAv1 decoding suggested by Rafael Carre. I don't think anyone out there...
[Rockbox.git] / apps / codecs / libwma / wmadeci.c
blob20c22187b2e570786445471aa337e923893067a7
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;
56 //static variables that replace malloced stuff
57 fixed32 stat0[2048], stat1[1024], stat2[512], stat3[256], stat4[128]; //these are the MDCT reconstruction windows
59 uint16_t *runtabarray[2], *levtabarray[2]; //these are VLC lookup tables
61 uint16_t runtab0[1336], runtab1[1336], levtab0[1336], levtab1[1336]; //these could be made smaller since only one can be 1336
63 #define VLCBUF1SIZE 4598
64 #define VLCBUF2SIZE 3574
65 #define VLCBUF3SIZE 360
66 #define VLCBUF4SIZE 540
68 /*putting these in IRAM actually makes PP slower*/
70 VLC_TYPE vlcbuf1[VLCBUF1SIZE][2];
71 VLC_TYPE vlcbuf2[VLCBUF2SIZE][2];
72 VLC_TYPE vlcbuf3[VLCBUF3SIZE][2];
73 VLC_TYPE vlcbuf4[VLCBUF4SIZE][2];
77 #include "wmadata.h" // PJJ
82 * Helper functions for wma_window.
87 #ifdef CPU_ARM
88 static inline
89 void vector_fmul_add_add(fixed32 *dst, const fixed32 *data,
90 const fixed32 *window, int n)
92 /* Block sizes are always power of two */
93 asm volatile (
94 "0:"
95 "ldmia %[d]!, {r0, r1};"
96 "ldmia %[w]!, {r4, r5};"
97 /* consume the first data and window value so we can use those
98 * registers again */
99 "smull r8, r9, r0, r4;"
100 "ldmia %[dst], {r0, r4};"
101 "add r0, r0, r9, lsl #1;" /* *dst=*dst+(r9<<1)*/
102 "smull r8, r9, r1, r5;"
103 "add r1, r4, r9, lsl #1;"
104 "stmia %[dst]!, {r0, r1};"
105 "subs %[n], %[n], #2;"
106 "bne 0b;"
107 : [d] "+r" (data), [w] "+r" (window), [dst] "+r" (dst), [n] "+r" (n)
108 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
111 static inline
112 void vector_fmul_reverse(fixed32 *dst, const fixed32 *src0, const fixed32 *src1,
113 int len)
115 /* Block sizes are always power of two */
116 asm volatile (
117 "add %[s1], %[s1], %[n], lsl #2;"
118 "0:"
119 "ldmia %[s0]!, {r0, r1};"
120 "ldmdb %[s1]!, {r4, r5};"
121 "smull r8, r9, r0, r5;"
122 "mov r0, r9, lsl #1;"
123 "smull r8, r9, r1, r4;"
124 "mov r1, r9, lsl #1;"
125 "stmia %[dst]!, {r0, r1};"
126 "subs %[n], %[n], #2;"
127 "bne 0b;"
128 : [s0] "+r" (src0), [s1] "+r" (src1), [dst] "+r" (dst), [n] "+r" (len)
129 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
132 #elif defined(CPU_COLDFIRE)
134 static inline
135 void vector_fmul_add_add(fixed32 *dst, const fixed32 *data,
136 const fixed32 *window, int n)
138 /* Block sizes are always power of two. Smallest block is always way bigger
139 * than four too.*/
140 asm volatile (
141 "0:"
142 "movem.l (%[d]), %%d0-%%d3;"
143 "movem.l (%[w]), %%d4-%%d5/%%a0-%%a1;"
144 "mac.l %%d0, %%d4, %%acc0;"
145 "mac.l %%d1, %%d5, %%acc1;"
146 "mac.l %%d2, %%a0, %%acc2;"
147 "mac.l %%d3, %%a1, %%acc3;"
148 "lea.l (16, %[d]), %[d];"
149 "lea.l (16, %[w]), %[w];"
150 "movclr.l %%acc0, %%d0;"
151 "movclr.l %%acc1, %%d1;"
152 "movclr.l %%acc2, %%d2;"
153 "movclr.l %%acc3, %%d3;"
154 "add.l %%d0, (%[dst])+;"
155 "add.l %%d1, (%[dst])+;"
156 "add.l %%d2, (%[dst])+;"
157 "add.l %%d3, (%[dst])+;"
158 "subq.l #4, %[n];"
159 "jne 0b;"
160 : [d] "+a" (data), [w] "+a" (window), [dst] "+a" (dst), [n] "+d" (n)
161 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
164 static inline
165 void vector_fmul_reverse(fixed32 *dst, const fixed32 *src0, const fixed32 *src1,
166 int len)
168 /* Block sizes are always power of two. Smallest block is always way bigger
169 * than four too.*/
170 asm volatile (
171 "lea.l (-16, %[s1], %[n]*4), %[s1];"
172 "0:"
173 "movem.l (%[s0]), %%d0-%%d3;"
174 "movem.l (%[s1]), %%d4-%%d5/%%a0-%%a1;"
175 "mac.l %%d0, %%a1, %%acc0;"
176 "mac.l %%d1, %%a0, %%acc1;"
177 "mac.l %%d2, %%d5, %%acc2;"
178 "mac.l %%d3, %%d4, %%acc3;"
179 "lea.l (16, %[s0]), %[s0];"
180 "lea.l (-16, %[s1]), %[s1];"
181 "movclr.l %%acc0, %%d0;"
182 "movclr.l %%acc1, %%d1;"
183 "movclr.l %%acc2, %%d2;"
184 "movclr.l %%acc3, %%d3;"
185 "movem.l %%d0-%%d3, (%[dst]);"
186 "lea.l (16, %[dst]), %[dst];"
187 "subq.l #4, %[n];"
188 "jne 0b;"
189 : [s0] "+a" (src0), [s1] "+a" (src1), [dst] "+a" (dst), [n] "+d" (len)
190 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
193 #else
195 static inline void vector_fmul_add_add(fixed32 *dst, const fixed32 *src0, const fixed32 *src1, int len){
196 int i;
197 for(i=0; i<len; i++)
198 dst[i] = fixmul32b(src0[i], src1[i]) + dst[i];
201 static inline void vector_fmul_reverse(fixed32 *dst, const fixed32 *src0, const fixed32 *src1, int len){
202 int i;
203 src1 += len-1;
204 for(i=0; i<len; i++)
205 dst[i] = fixmul32b(src0[i], src1[-i]);
208 #endif
211 * Apply MDCT window and add into output.
213 * We ensure that when the windows overlap their squared sum
214 * is always 1 (MDCT reconstruction rule).
216 * The Vorbis I spec has a great diagram explaining this process.
217 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
219 static void wma_window(WMADecodeContext *s, fixed32 *in, fixed32 *out)
221 //float *in = s->output;
222 int block_len, bsize, n;
224 /* left part */
225 /*previous block was larger, so we'll use the size of the current block to set the window size*/
226 if (s->block_len_bits <= s->prev_block_len_bits) {
227 block_len = s->block_len;
228 bsize = s->frame_len_bits - s->block_len_bits;
230 vector_fmul_add_add(out, in, s->windows[bsize], block_len);
232 } else {
233 /*previous block was smaller or the same size, so use it's size to set the window length*/
234 block_len = 1 << s->prev_block_len_bits;
235 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
236 n = (s->block_len - block_len) / 2;
237 bsize = s->frame_len_bits - s->prev_block_len_bits;
239 vector_fmul_add_add(out+n, in+n, s->windows[bsize], block_len);
241 memcpy(out+n+block_len, in+n+block_len, n*sizeof(fixed32));
243 /* Advance to the end of the current block and prepare to window it for the next block.
244 * Since the window function needs to be reversed, we do it backwards starting with the
245 * last sample and moving towards the first
247 out += s->block_len;
248 in += s->block_len;
250 /* right part */
251 if (s->block_len_bits <= s->next_block_len_bits) {
252 block_len = s->block_len;
253 bsize = s->frame_len_bits - s->block_len_bits;
255 vector_fmul_reverse(out, in, s->windows[bsize], block_len);
257 } else {
258 block_len = 1 << s->next_block_len_bits;
259 n = (s->block_len - block_len) / 2;
260 bsize = s->frame_len_bits - s->next_block_len_bits;
262 memcpy(out, in, n*sizeof(fixed32));
264 vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
266 memset(out+n+block_len, 0, n*sizeof(fixed32));
273 /* XXX: use same run/length optimization as mpeg decoders */
274 static void init_coef_vlc(VLC *vlc,
275 uint16_t **prun_table, uint16_t **plevel_table,
276 const CoefVLCTable *vlc_table, int tab)
278 int n = vlc_table->n;
279 const uint8_t *table_bits = vlc_table->huffbits;
280 const uint32_t *table_codes = vlc_table->huffcodes;
281 const uint16_t *levels_table = vlc_table->levels;
282 uint16_t *run_table, *level_table;
283 const uint16_t *p;
284 int i, l, j, level;
287 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
289 run_table = runtabarray[tab];
290 level_table= levtabarray[tab];
292 p = levels_table;
293 i = 2;
294 level = 1;
295 while (i < n)
297 l = *p++;
298 for(j=0;j<l;++j)
300 run_table[i] = j;
301 level_table[i] = level;
302 ++i;
304 ++level;
306 *prun_table = run_table;
307 *plevel_table = level_table;
310 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
312 //WMADecodeContext *s = avctx->priv_data;
313 int i, flags1, flags2;
314 fixed32 *window;
315 uint8_t *extradata;
316 fixed64 bps1;
317 fixed32 high_freq;
318 fixed64 bps;
319 int sample_rate1;
320 int coef_vlc_table;
321 // int filehandle;
322 #ifdef CPU_COLDFIRE
323 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
324 #endif
326 s->sample_rate = wfx->rate;
327 s->nb_channels = wfx->channels;
328 s->bit_rate = wfx->bitrate;
329 s->block_align = wfx->blockalign;
331 s->coefs = &coefsarray;
333 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
334 s->version = 1;
335 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2 ) {
336 s->version = 2;
337 } else {
338 /*one of those other wma flavors that don't have GPLed decoders */
339 return -1;
342 /* extract flag infos */
343 flags1 = 0;
344 flags2 = 0;
345 extradata = wfx->data;
346 if (s->version == 1 && wfx->datalen >= 4) {
347 flags1 = extradata[0] | (extradata[1] << 8);
348 flags2 = extradata[2] | (extradata[3] << 8);
349 }else if (s->version == 2 && wfx->datalen >= 6){
350 flags1 = extradata[0] | (extradata[1] << 8) |
351 (extradata[2] << 16) | (extradata[3] << 24);
352 flags2 = extradata[4] | (extradata[5] << 8);
354 s->use_exp_vlc = flags2 & 0x0001;
355 s->use_bit_reservoir = flags2 & 0x0002;
356 s->use_variable_block_len = flags2 & 0x0004;
358 /* compute MDCT block size */
359 if (s->sample_rate <= 16000){
360 s->frame_len_bits = 9;
361 }else if (s->sample_rate <= 22050 ||
362 (s->sample_rate <= 32000 && s->version == 1)){
363 s->frame_len_bits = 10;
364 }else{
365 s->frame_len_bits = 11;
367 s->frame_len = 1 << s->frame_len_bits;
368 if (s-> use_variable_block_len)
370 int nb_max, nb;
371 nb = ((flags2 >> 3) & 3) + 1;
372 if ((s->bit_rate / s->nb_channels) >= 32000)
374 nb += 2;
376 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
377 if (nb > nb_max)
378 nb = nb_max;
379 s->nb_block_sizes = nb + 1;
381 else
383 s->nb_block_sizes = 1;
386 /* init rate dependant parameters */
387 s->use_noise_coding = 1;
388 high_freq = itofix64(s->sample_rate) >> 1;
391 /* if version 2, then the rates are normalized */
392 sample_rate1 = s->sample_rate;
393 if (s->version == 2)
395 if (sample_rate1 >= 44100)
396 sample_rate1 = 44100;
397 else if (sample_rate1 >= 22050)
398 sample_rate1 = 22050;
399 else if (sample_rate1 >= 16000)
400 sample_rate1 = 16000;
401 else if (sample_rate1 >= 11025)
402 sample_rate1 = 11025;
403 else if (sample_rate1 >= 8000)
404 sample_rate1 = 8000;
407 fixed64 tmp = itofix64(s->bit_rate);
408 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
409 bps = fixdiv64(tmp, tmp2);
410 fixed64 tim = bps * s->frame_len;
411 fixed64 tmpi = fixdiv64(tim,itofix64(8));
412 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
414 /* compute high frequency value and choose if noise coding should
415 be activated */
416 bps1 = bps;
417 if (s->nb_channels == 2)
418 bps1 = fixmul32(bps,0x1999a);
419 if (sample_rate1 == 44100)
421 if (bps1 >= 0x9c29)
422 s->use_noise_coding = 0;
423 else
424 high_freq = fixmul32(high_freq,0x6666);
426 else if (sample_rate1 == 22050)
428 if (bps1 >= 0x128f6)
429 s->use_noise_coding = 0;
430 else if (bps1 >= 0xb852)
431 high_freq = fixmul32(high_freq,0xb333);
432 else
433 high_freq = fixmul32(high_freq,0x999a);
435 else if (sample_rate1 == 16000)
437 if (bps > 0x8000)
438 high_freq = fixmul32(high_freq,0x8000);
439 else
440 high_freq = fixmul32(high_freq,0x4ccd);
442 else if (sample_rate1 == 11025)
444 high_freq = fixmul32(high_freq,0xb333);
446 else if (sample_rate1 == 8000)
448 if (bps <= 0xa000)
450 high_freq = fixmul32(high_freq,0x8000);
452 else if (bps > 0xc000)
454 s->use_noise_coding = 0;
456 else
458 high_freq = fixmul32(high_freq,0xa666);
461 else
463 if (bps >= 0xcccd)
465 high_freq = fixmul32(high_freq,0xc000);
467 else if (bps >= 0x999a)
469 high_freq = fixmul32(high_freq,0x999a);
471 else
473 high_freq = fixmul32(high_freq,0x8000);
477 /* compute the scale factor band sizes for each MDCT block size */
479 int a, b, pos, lpos, k, block_len, i, j, n;
480 const uint8_t *table;
482 if (s->version == 1)
484 s->coefs_start = 3;
486 else
488 s->coefs_start = 0;
490 for(k = 0; k < s->nb_block_sizes; ++k)
492 block_len = s->frame_len >> k;
494 if (s->version == 1)
496 lpos = 0;
497 for(i=0;i<25;++i)
499 a = wma_critical_freqs[i];
500 b = s->sample_rate;
501 pos = ((block_len * 2 * a) + (b >> 1)) / b;
502 if (pos > block_len)
503 pos = block_len;
504 s->exponent_bands[0][i] = pos - lpos;
505 if (pos >= block_len)
507 ++i;
508 break;
510 lpos = pos;
512 s->exponent_sizes[0] = i;
514 else
516 /* hardcoded tables */
517 table = NULL;
518 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
519 if (a < 3)
521 if (s->sample_rate >= 44100)
522 table = exponent_band_44100[a];
523 else if (s->sample_rate >= 32000)
524 table = exponent_band_32000[a];
525 else if (s->sample_rate >= 22050)
526 table = exponent_band_22050[a];
528 if (table)
530 n = *table++;
531 for(i=0;i<n;++i)
532 s->exponent_bands[k][i] = table[i];
533 s->exponent_sizes[k] = n;
535 else
537 j = 0;
538 lpos = 0;
539 for(i=0;i<25;++i)
541 a = wma_critical_freqs[i];
542 b = s->sample_rate;
543 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
544 pos <<= 2;
545 if (pos > block_len)
546 pos = block_len;
547 if (pos > lpos)
548 s->exponent_bands[k][j++] = pos - lpos;
549 if (pos >= block_len)
550 break;
551 lpos = pos;
553 s->exponent_sizes[k] = j;
557 /* max number of coefs */
558 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
559 /* high freq computation */
561 fixed32 tmp1 = high_freq*2; /* high_freq is a fixed32!*/
562 fixed32 tmp2=itofix32(s->sample_rate>>1);
563 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
566 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
567 s->sample_rate + 0.5);*/
569 n = s->exponent_sizes[k];
570 j = 0;
571 pos = 0;
572 for(i=0;i<n;++i)
574 int start, end;
575 start = pos;
576 pos += s->exponent_bands[k][i];
577 end = pos;
578 if (start < s->high_band_start[k])
579 start = s->high_band_start[k];
580 if (end > s->coefs_end[k])
581 end = s->coefs_end[k];
582 if (end > start)
583 s->exponent_high_bands[k][j++] = end - start;
585 s->exponent_high_sizes[k] = j;
589 mdct_init_global();
591 for(i = 0; i < s->nb_block_sizes; ++i)
593 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
596 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
597 * In the worst case you can have 5 window sizes, 128 doubling up 2048
598 * Smaller windows are handled differently.
599 * Since we don't have malloc, just statically allocate this
601 fixed32 *temp[5];
602 temp[0] = stat0;
603 temp[1] = stat1;
604 temp[2] = stat2;
605 temp[3] = stat3;
606 temp[4] = stat4;
608 /* init MDCT windows : simple sinus window */
609 for(i = 0; i < s->nb_block_sizes; i++)
611 int n, j;
612 fixed32 alpha;
613 n = 1 << (s->frame_len_bits - i);
614 //window = av_malloc(sizeof(fixed32) * n);
615 window = temp[i];
617 //fixed32 n2 = itofix32(n<<1); //2x the window length
618 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
620 //alpha = M_PI_F>>(s->frame_len_bits - i+1);
621 alpha = (1<<15)>>(s->frame_len_bits - i+1); /* this calculates 0.5/(2*n) */
622 for(j=0;j<n;++j)
624 fixed32 j2 = itofix32(j) + 0x8000;
625 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0); //alpha between 0 and pi/2
628 s->windows[i] = window;
632 s->reset_block_lengths = 1;
634 if (s->use_noise_coding)
636 /* init the noise generator */
637 if (s->use_exp_vlc)
639 s->noise_mult = 0x51f;
640 s->noise_table = noisetable_exp;
642 else
644 s->noise_mult = 0xa3d;
645 /* LSP values are simply 2x the EXP values */
646 for (i=0;i<NOISE_TAB_SIZE;++i)
647 noisetable_exp[i] = noisetable_exp[i]<< 1;
648 s->noise_table = noisetable_exp;
650 #if 0
652 unsigned int seed;
653 fixed32 norm;
654 seed = 1;
655 norm = 0; // PJJ: near as makes any diff to 0!
656 for (i=0;i<NOISE_TAB_SIZE;++i)
658 seed = seed * 314159 + 1;
659 s->noise_table[i] = itofix32((int)seed) * norm;
662 #endif
664 s->hgain_vlc.table = vlcbuf4;
665 s->hgain_vlc.table_allocated = VLCBUF4SIZE;
666 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
667 hgain_huffbits, 1, 1,
668 hgain_huffcodes, 2, 2, 0);
671 if (s->use_exp_vlc)
674 s->exp_vlc.table = vlcbuf3;
675 s->exp_vlc.table_allocated = VLCBUF3SIZE;
677 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
678 scale_huffbits, 1, 1,
679 scale_huffcodes, 4, 4, 0);
681 else
683 wma_lsp_to_curve_init(s, s->frame_len);
686 /* choose the VLC tables for the coefficients */
687 coef_vlc_table = 2;
688 if (s->sample_rate >= 32000)
690 if (bps1 < 0xb852)
691 coef_vlc_table = 0;
692 else if (bps1 < 0x128f6)
693 coef_vlc_table = 1;
696 runtabarray[0] = runtab0; runtabarray[1] = runtab1;
697 levtabarray[0] = levtab0; levtabarray[1] = levtab1;
699 s->coef_vlc[0].table = vlcbuf1;
700 s->coef_vlc[0].table_allocated = VLCBUF1SIZE;
701 s->coef_vlc[1].table = vlcbuf2;
702 s->coef_vlc[1].table_allocated = VLCBUF2SIZE;
705 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
706 &coef_vlcs[coef_vlc_table * 2], 0);
707 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
708 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
710 s->last_superframe_len = 0;
711 s->last_bitoffset = 0;
713 return 0;
717 /* compute x^-0.25 with an exponent and mantissa table. We use linear
718 interpolation to reduce the mantissa table size at a small speed
719 expense (linear interpolation approximately doubles the number of
720 bits of precision). */
721 static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
723 union {
724 float f;
725 unsigned int v;
726 } u, t;
727 unsigned int e, m;
728 fixed32 a, b;
730 u.f = fixtof64(x);
731 e = u.v >> 23;
732 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
733 /* build interpolation scale: 1 <= t < 2. */
734 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
735 a = s->lsp_pow_m_table1[m];
736 b = s->lsp_pow_m_table2[m];
738 /* lsp_pow_e_table contains 32.32 format */
739 /* TODO: Since we're unlikely have value that cover the whole
740 * IEEE754 range, we probably don't need to have all possible exponents */
742 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
745 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
747 fixed32 wdel, a, b, temp, temp2;
748 int i, m;
750 wdel = fixdiv32(M_PI_F, itofix32(frame_len));
751 temp = fixdiv32(itofix32(1), itofix32(frame_len));
752 for (i=0; i<frame_len; ++i)
754 /* TODO: can probably reuse the trig_init values here */
755 fsincos((temp*i)<<15, &temp2);
756 /* get 3 bits headroom + 1 bit from not doubleing the values */
757 s->lsp_cos_table[i] = temp2>>3;
760 /* NOTE: these two tables are needed to avoid two operations in
761 pow_m1_4 */
762 b = itofix32(1);
763 int ix = 0;
765 /*double check this later*/
766 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
768 m = (1 << LSP_POW_BITS) + i;
769 a = pow_a_table[ix++]<<4;
770 s->lsp_pow_m_table1[i] = 2 * a - b;
771 s->lsp_pow_m_table2[i] = b - a;
772 b = a;
777 /* NOTE: We use the same code as Vorbis here */
778 /* XXX: optimize it further with SSE/3Dnow */
779 static void wma_lsp_to_curve(WMADecodeContext *s,
780 fixed32 *out,
781 fixed32 *val_max_ptr,
782 int n,
783 fixed32 *lsp)
785 int i, j;
786 fixed32 p, q, w, v, val_max, temp, temp2;
788 val_max = 0;
789 for(i=0;i<n;++i)
791 /* shift by 2 now to reduce rounding error,
792 * we can renormalize right before pow_m1_4
795 p = 0x8000<<5;
796 q = 0x8000<<5;
797 w = s->lsp_cos_table[i];
799 for (j=1;j<NB_LSP_COEFS;j+=2)
801 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
802 temp2 = ((w - (lsp[j - 1]<<11)));
803 temp = q;
805 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
806 q = fixmul32b(q, temp2 )<<4;
807 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
810 /* 2 in 5.27 format is 0x10000000 */
811 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
812 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
814 v = (p + q) >>9; /* p/q end up as 16.16 */
815 v = pow_m1_4(s, v);
816 if (v > val_max)
817 val_max = v;
818 out[i] = v;
821 *val_max_ptr = val_max;
824 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
825 static void decode_exp_lsp(WMADecodeContext *s, int ch)
827 fixed32 lsp_coefs[NB_LSP_COEFS];
828 int val, i;
830 for (i = 0; i < NB_LSP_COEFS; ++i)
832 if (i == 0 || i >= 8)
833 val = get_bits(&s->gb, 3);
834 else
835 val = get_bits(&s->gb, 4);
836 lsp_coefs[i] = lsp_codebook[i][val];
839 wma_lsp_to_curve(s,
840 s->exponents[ch],
841 &s->max_exponent[ch],
842 s->block_len,
843 lsp_coefs);
846 /* decode exponents coded with VLC codes */
847 static int decode_exp_vlc(WMADecodeContext *s, int ch)
849 int last_exp, n, code;
850 const uint16_t *ptr, *band_ptr;
851 fixed32 v, max_scale;
852 fixed32 *q,*q_end;
854 /*accommodate the 60 negative indices */
855 const fixed32 *pow_10_to_yover16_ptr = &pow_10_to_yover16[61];
857 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
858 ptr = band_ptr;
859 q = s->exponents[ch];
860 q_end = q + s->block_len;
861 max_scale = 0;
864 if (s->version == 1) //wmav1 only
866 last_exp = get_bits(&s->gb, 5) + 10;
867 /* XXX: use a table */
868 v = pow_10_to_yover16_ptr[last_exp];
869 max_scale = v;
870 n = *ptr++;
873 *q++ = v;
875 while (--n);
877 last_exp = 36;
879 while (q < q_end)
881 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
882 if (code < 0)
884 return -1;
886 /* NOTE: this offset is the same as MPEG4 AAC ! */
887 last_exp += code - 60;
888 /* XXX: use a table */
889 v = pow_10_to_yover16_ptr[last_exp];
890 if (v > max_scale)
892 max_scale = v;
894 n = *ptr++;
897 *q++ = v;
900 while (--n);
903 s->max_exponent[ch] = max_scale;
904 return 0;
907 /* return 0 if OK. return 1 if last block of frame. return -1 if
908 unrecorrable error. */
909 static int wma_decode_block(WMADecodeContext *s)
911 int n, v, a, ch, code, bsize;
912 int coef_nb_bits, total_gain;
913 int nb_coefs[MAX_CHANNELS];
914 fixed32 mdct_norm;
916 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
918 /* compute current block length */
919 if (s->use_variable_block_len)
921 n = av_log2(s->nb_block_sizes - 1) + 1;
923 if (s->reset_block_lengths)
925 s->reset_block_lengths = 0;
926 v = get_bits(&s->gb, n);
927 if (v >= s->nb_block_sizes)
929 return -2;
931 s->prev_block_len_bits = s->frame_len_bits - v;
932 v = get_bits(&s->gb, n);
933 if (v >= s->nb_block_sizes)
935 return -3;
937 s->block_len_bits = s->frame_len_bits - v;
939 else
941 /* update block lengths */
942 s->prev_block_len_bits = s->block_len_bits;
943 s->block_len_bits = s->next_block_len_bits;
945 v = get_bits(&s->gb, n);
947 if (v >= s->nb_block_sizes)
949 // rb->splash(HZ*4, "v was %d", v); //5, 7
950 return -4; //this is it
952 else{
953 //rb->splash(HZ, "passed v block (%d)!", v);
955 s->next_block_len_bits = s->frame_len_bits - v;
957 else
959 /* fixed block len */
960 s->next_block_len_bits = s->frame_len_bits;
961 s->prev_block_len_bits = s->frame_len_bits;
962 s->block_len_bits = s->frame_len_bits;
964 /* now check if the block length is coherent with the frame length */
965 s->block_len = 1 << s->block_len_bits;
967 if ((s->block_pos + s->block_len) > s->frame_len)
969 return -5; //oddly 32k sample from tracker fails here
972 if (s->nb_channels == 2)
974 s->ms_stereo = get_bits(&s->gb, 1);
976 v = 0;
977 for (ch = 0; ch < s->nb_channels; ++ch)
979 a = get_bits(&s->gb, 1);
980 s->channel_coded[ch] = a;
981 v |= a;
983 /* if no channel coded, no need to go further */
984 /* XXX: fix potential framing problems */
985 if (!v)
987 goto next;
990 bsize = s->frame_len_bits - s->block_len_bits;
992 /* read total gain and extract corresponding number of bits for
993 coef escape coding */
994 total_gain = 1;
995 for(;;)
997 a = get_bits(&s->gb, 7);
998 total_gain += a;
999 if (a != 127)
1001 break;
1005 if (total_gain < 15)
1006 coef_nb_bits = 13;
1007 else if (total_gain < 32)
1008 coef_nb_bits = 12;
1009 else if (total_gain < 40)
1010 coef_nb_bits = 11;
1011 else if (total_gain < 45)
1012 coef_nb_bits = 10;
1013 else
1014 coef_nb_bits = 9;
1016 /* compute number of coefficients */
1017 n = s->coefs_end[bsize] - s->coefs_start;
1019 for(ch = 0; ch < s->nb_channels; ++ch)
1021 nb_coefs[ch] = n;
1023 /* complex coding */
1024 if (s->use_noise_coding)
1027 for(ch = 0; ch < s->nb_channels; ++ch)
1029 if (s->channel_coded[ch])
1031 int i, n, a;
1032 n = s->exponent_high_sizes[bsize];
1033 for(i=0;i<n;++i)
1035 a = get_bits(&s->gb, 1);
1036 s->high_band_coded[ch][i] = a;
1037 /* if noise coding, the coefficients are not transmitted */
1038 if (a)
1039 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
1043 for(ch = 0; ch < s->nb_channels; ++ch)
1045 if (s->channel_coded[ch])
1047 int i, n, val, code;
1049 n = s->exponent_high_sizes[bsize];
1050 val = (int)0x80000000;
1051 for(i=0;i<n;++i)
1053 if (s->high_band_coded[ch][i])
1055 if (val == (int)0x80000000)
1057 val = get_bits(&s->gb, 7) - 19;
1059 else
1061 //code = get_vlc(&s->gb, &s->hgain_vlc);
1062 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
1063 if (code < 0)
1065 return -6;
1067 val += code - 18;
1069 s->high_band_values[ch][i] = val;
1076 /* exponents can be reused in short blocks. */
1077 if ((s->block_len_bits == s->frame_len_bits) || get_bits(&s->gb, 1))
1079 for(ch = 0; ch < s->nb_channels; ++ch)
1081 if (s->channel_coded[ch])
1083 if (s->use_exp_vlc)
1085 if (decode_exp_vlc(s, ch) < 0)
1087 return -7;
1090 else
1092 decode_exp_lsp(s, ch);
1094 s->exponents_bsize[ch] = bsize;
1099 /* parse spectral coefficients : just RLE encoding */
1100 for(ch = 0; ch < s->nb_channels; ++ch)
1102 if (s->channel_coded[ch])
1104 VLC *coef_vlc;
1105 int level, run, sign, tindex;
1106 int16_t *ptr, *eptr;
1107 const int16_t *level_table, *run_table;
1109 /* special VLC tables are used for ms stereo because
1110 there is potentially less energy there */
1111 tindex = (ch == 1 && s->ms_stereo);
1112 coef_vlc = &s->coef_vlc[tindex];
1113 run_table = s->run_table[tindex];
1114 level_table = s->level_table[tindex];
1115 /* XXX: optimize */
1116 ptr = &s->coefs1[ch][0];
1117 eptr = ptr + nb_coefs[ch];
1118 memset(ptr, 0, s->block_len * sizeof(int16_t));
1120 for(;;)
1122 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
1123 //code = get_vlc(&s->gb, coef_vlc);
1124 if (code < 0)
1126 return -8;
1128 if (code == 1)
1130 /* EOB */
1131 break;
1133 else if (code == 0)
1135 /* escape */
1136 level = get_bits(&s->gb, coef_nb_bits);
1137 /* NOTE: this is rather suboptimal. reading
1138 block_len_bits would be better */
1139 run = get_bits(&s->gb, s->frame_len_bits);
1141 else
1143 /* normal code */
1144 run = run_table[code];
1145 level = level_table[code];
1147 sign = get_bits(&s->gb, 1);
1148 if (!sign)
1149 level = -level;
1150 ptr += run;
1151 if (ptr >= eptr)
1153 break;
1155 *ptr++ = level;
1158 /* NOTE: EOB can be omitted */
1159 if (ptr >= eptr)
1160 break;
1163 if (s->version == 1 && s->nb_channels >= 2)
1165 align_get_bits(&s->gb);
1170 int n4 = s->block_len >> 1;
1171 //mdct_norm = 0x10000;
1172 //mdct_norm = fixdiv32(mdct_norm,itofix32(n4));
1174 mdct_norm = 0x10000>>(s->block_len_bits-1); //theres no reason to do a divide by two in fixed precision ...
1176 if (s->version == 1)
1178 fixed32 tmp = fixtoi32(fixsqrt32(itofix32(n4)));
1179 mdct_norm *= tmp; // PJJ : exercise this path
1184 /* finally compute the MDCT coefficients */
1185 for(ch = 0; ch < s->nb_channels; ++ch)
1187 if (s->channel_coded[ch])
1189 int16_t *coefs1;
1190 fixed32 *exponents, *exp_ptr;
1191 fixed32 *coefs, atemp;
1192 fixed64 mult;
1193 fixed64 mult1;
1194 fixed32 noise, temp1, temp2, mult2;
1195 int i, j, n, n1, last_high_band, esize;
1196 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
1198 //total_gain, coefs1, mdctnorm are lossless
1200 coefs1 = s->coefs1[ch];
1201 exponents = s->exponents[ch];
1202 esize = s->exponents_bsize[ch];
1203 coefs = (*(s->coefs))[ch];
1205 n=0;
1208 * Previously the IMDCT was run in 17.15 precision to avoid overflow. However rare files could
1209 * overflow here as well, so switch to 17.15 during coefs calculation.
1213 if (s->use_noise_coding)
1215 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1217 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1218 mult = mult* mdct_norm; //what the hell? This is actually fixed64*2^16!
1219 mult1 = mult;
1221 /* very low freqs : noise */
1222 for(i = 0;i < s->coefs_start; ++i)
1224 *coefs++ = fixmul32( (fixmul32(s->noise_table[s->noise_index],(*exponents++))>>4),Fixed32From64(mult1)) >>1;
1225 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1228 n1 = s->exponent_high_sizes[bsize];
1230 /* compute power of high bands */
1231 exp_ptr = exponents +
1232 s->high_band_start[bsize] -
1233 s->coefs_start;
1234 last_high_band = 0; /* avoid warning */
1235 for (j=0;j<n1;++j)
1237 n = s->exponent_high_bands[s->frame_len_bits -
1238 s->block_len_bits][j];
1239 if (s->high_band_coded[ch][j])
1241 fixed32 e2, v;
1242 e2 = 0;
1243 for(i = 0;i < n; ++i)
1245 /*v is noramlized later on so its fixed format is irrelevant*/
1246 v = exp_ptr[i]>>4;
1247 e2 += fixmul32(v, v)>>3;
1249 exp_power[j] = e2/n; /*n is an int...*/
1250 last_high_band = j;
1252 exp_ptr += n;
1255 /* main freqs and high freqs */
1256 for(j=-1;j<n1;++j)
1258 if (j < 0)
1260 n = s->high_band_start[bsize] -
1261 s->coefs_start;
1263 else
1265 n = s->exponent_high_bands[s->frame_len_bits -
1266 s->block_len_bits][j];
1268 if (j >= 0 && s->high_band_coded[ch][j])
1270 /* use noise with specified power */
1271 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1272 mult1 = (fixed64)fixsqrt32(tmp);
1273 /* XXX: use a table */
1274 /*mult1 is 48.16, pow_table is 48.16*/
1275 mult1 = mult1 * pow_table[s->high_band_values[ch][j]+20] >> PRECISION;
1277 /*this step has a fairly high degree of error for some reason*/
1278 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1280 mult1 = mult1*mdct_norm>>PRECISION;
1281 for(i = 0;i < n; ++i)
1283 noise = s->noise_table[s->noise_index];
1284 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1285 *coefs++ = fixmul32((fixmul32(*exponents,noise)>>4),Fixed32From64(mult1)) >>1;
1286 ++exponents;
1289 else
1291 /* coded values + small noise */
1292 for(i = 0;i < n; ++i)
1294 // PJJ: check code path
1295 noise = s->noise_table[s->noise_index];
1296 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1298 /*don't forget to renormalize the noise*/
1299 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1300 temp2 = fixmul32(*exponents, mult>>17);
1301 *coefs++ = fixmul32(temp1, temp2);
1302 ++exponents;
1307 /* very high freqs : noise */
1308 n = s->block_len - s->coefs_end[bsize];
1309 mult2 = fixmul32(mult>>16,exponents[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1310 for (i = 0; i < n; ++i)
1312 /*renormalize the noise product and then reduce to 17.15 precison*/
1313 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>5;
1315 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1318 else
1320 /*Noise coding not used, simply convert from exp to fixed representation*/
1323 fixed32 mult3 = (fixed32)(fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch])));
1324 mult3 = fixmul32(mult3, mdct_norm);
1326 n = nb_coefs[ch];
1328 /* XXX: optimize more, unrolling this loop in asm might be a good idea */
1330 for(i = 0;i < n; ++i)
1332 atemp = (coefs1[i] * mult3)>>1;
1333 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1335 n = s->block_len - s->coefs_end[bsize];
1336 memset(coefs, 0, n*sizeof(fixed32));
1343 if (s->ms_stereo && s->channel_coded[1])
1345 fixed32 a, b;
1346 int i;
1347 fixed32 (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1349 /* nominal case for ms stereo: we do it before mdct */
1350 /* no need to optimize this case because it should almost
1351 never happen */
1352 if (!s->channel_coded[0])
1354 memset((*(s->coefs))[0], 0, sizeof(fixed32) * s->block_len);
1355 s->channel_coded[0] = 1;
1358 for(i = 0; i < s->block_len; ++i)
1360 a = (*coefs)[0][i];
1361 b = (*coefs)[1][i];
1362 (*coefs)[0][i] = a + b;
1363 (*coefs)[1][i] = a - b;
1367 for(ch = 0; ch < s->nb_channels; ++ch)
1369 if (s->channel_coded[ch])
1371 static fixed32 output[BLOCK_MAX_SIZE * 2] IBSS_ATTR;
1373 int n4, index, n;
1375 n = s->block_len;
1376 n4 = s->block_len >>1;
1378 ff_imdct_calc(&s->mdct_ctx[bsize],
1379 output,
1380 (*(s->coefs))[ch]);
1383 /* add in the frame */
1384 index = (s->frame_len / 2) + s->block_pos - n4;
1386 wma_window(s, output, &s->frame_out[ch][index]);
1390 /* specific fast case for ms-stereo : add to second
1391 channel if it is not coded */
1392 if (s->ms_stereo && !s->channel_coded[1])
1394 wma_window(s, output, &s->frame_out[1][index]);
1398 next:
1399 /* update block number */
1400 ++s->block_num;
1401 s->block_pos += s->block_len;
1402 if (s->block_pos >= s->frame_len)
1404 return 1;
1406 else
1408 return 0;
1412 /* decode a frame of frame_len samples */
1413 static int wma_decode_frame(WMADecodeContext *s, int32_t *samples)
1415 int ret, i, n, ch, incr;
1416 int32_t *ptr;
1417 fixed32 *iptr;
1418 // rb->splash(HZ, "in wma_decode_frame");
1420 /* read each block */
1421 s->block_num = 0;
1422 s->block_pos = 0;
1425 for(;;)
1427 ret = wma_decode_block(s);
1428 if (ret < 0)
1431 DEBUGF("wma_decode_block failed with code %d\n", ret);
1432 return -1;
1434 if (ret)
1436 break;
1440 /* return frame with full 30-bit precision */
1441 n = s->frame_len;
1442 incr = s->nb_channels;
1443 for(ch = 0; ch < s->nb_channels; ++ch)
1445 ptr = samples + ch;
1446 iptr = s->frame_out[ch];
1448 for (i=0;i<n;++i)
1450 *ptr = (*iptr++);
1451 ptr += incr;
1453 /* prepare for next block */
1454 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
1455 s->frame_len * sizeof(fixed32));
1459 return 0;
1462 /* Initialise the superframe decoding */
1464 int wma_decode_superframe_init(WMADecodeContext* s,
1465 uint8_t *buf, /*input*/
1466 int buf_size)
1468 if (buf_size==0)
1470 s->last_superframe_len = 0;
1471 return 0;
1474 s->current_frame = 0;
1476 init_get_bits(&s->gb, buf, buf_size*8);
1478 if (s->use_bit_reservoir)
1480 /* read super frame header */
1481 get_bits(&s->gb, 4); /* super frame index */
1482 s->nb_frames = get_bits(&s->gb, 4);
1484 if (s->last_superframe_len == 0)
1485 s->nb_frames --;
1486 else if (s->nb_frames == 0)
1487 s->nb_frames++;
1489 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1490 } else {
1491 s->nb_frames = 1;
1494 return 1;
1498 /* Decode a single frame in the current superframe - return -1 if
1499 there was a decoding error, or the number of samples decoded.
1502 int wma_decode_superframe_frame(WMADecodeContext* s,
1503 int32_t* samples, /*output*/
1504 uint8_t *buf, /*input*/
1505 int buf_size)
1507 int pos, len;
1508 uint8_t *q;
1509 int done = 0;
1510 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1512 if (s->last_superframe_len > 0)
1514 /* add s->bit_offset bits to last frame */
1515 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1516 MAX_CODED_SUPERFRAME_SIZE)
1518 DEBUGF("superframe size too large error\n");
1519 goto fail;
1521 q = s->last_superframe + s->last_superframe_len;
1522 len = s->bit_offset;
1523 while (len > 0)
1525 *q++ = (get_bits)(&s->gb, 8);
1526 len -= 8;
1528 if (len > 0)
1530 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1533 /* XXX: s->bit_offset bits into last frame */
1534 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1535 /* skip unused bits */
1536 if (s->last_bitoffset > 0)
1537 skip_bits(&s->gb, s->last_bitoffset);
1539 /* this frame is stored in the last superframe and in the
1540 current one */
1541 if (wma_decode_frame(s, samples) < 0)
1543 goto fail;
1545 done = 1;
1548 /* read each frame starting from s->bit_offset */
1549 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1550 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1551 len = pos & 7;
1552 if (len > 0)
1553 skip_bits(&s->gb, len);
1555 s->reset_block_lengths = 1;
1558 /* If we haven't decoded a frame yet, do it now */
1559 if (!done)
1561 if (wma_decode_frame(s, samples) < 0)
1563 goto fail;
1567 s->current_frame++;
1569 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1571 /* we copy the end of the frame in the last frame buffer */
1572 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1573 s->last_bitoffset = pos & 7;
1574 pos >>= 3;
1575 len = buf_size - pos;
1576 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1578 DEBUGF("superframe size too large error after decodeing\n");
1579 goto fail;
1581 s->last_superframe_len = len;
1582 memcpy(s->last_superframe, buf + pos, len);
1585 return s->frame_len;
1587 fail:
1588 /* when error, we reset the bit reservoir */
1590 s->last_superframe_len = 0;
1591 return -1;