summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libwma/wmadeci.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libwma/wmadeci.c')
-rw-r--r--lib/rbcodec/codecs/libwma/wmadeci.c1445
1 files changed, 1445 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libwma/wmadeci.c b/lib/rbcodec/codecs/libwma/wmadeci.c
new file mode 100644
index 0000000000..d7a836dd97
--- /dev/null
+++ b/lib/rbcodec/codecs/libwma/wmadeci.c
@@ -0,0 +1,1445 @@
1/*
2 * WMA compatible decoder
3 * Copyright (c) 2002 The FFmpeg Project.
4 *
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.
9 *
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.
14 *
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
18 */
19
20/**
21 * @file wmadec.c
22 * WMA compatible decoder.
23 */
24
25#include <codecs.h>
26#include <codecs/lib/codeclib.h>
27#include <codecs/libasf/asf.h>
28#include "wmadec.h"
29#include "wmafixed.h"
30#include "wmadata.h"
31
32static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
33
34/*declarations of statically allocated variables used to remove malloc calls*/
35
36static fixed32 coefsarray[MAX_CHANNELS][BLOCK_MAX_SIZE] IBSS_ATTR MEM_ALIGN_ATTR;
37/*decode and window into IRAM on targets with at least 80KB of codec IRAM*/
38static fixed32 frame_out_buf[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] IBSS_ATTR_WMA_LARGE_IRAM MEM_ALIGN_ATTR;
39
40/*MDCT reconstruction windows*/
41static fixed32 stat0[2048] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
42static fixed32 stat1[1024] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
43static fixed32 stat2[ 512] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
44static fixed32 stat3[ 256] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
45static fixed32 stat4[ 128] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
46
47/*VLC lookup tables*/
48static uint16_t *runtabarray[2];
49static uint16_t *levtabarray[2];
50
51static uint16_t runtab_big[1336] MEM_ALIGN_ATTR;
52static uint16_t runtab_small[1072] MEM_ALIGN_ATTR;
53static uint16_t levtab_big[1336] MEM_ALIGN_ATTR;
54static uint16_t levtab_small[1072] MEM_ALIGN_ATTR;
55
56#define VLCBUF1SIZE 4598
57#define VLCBUF2SIZE 3574
58#define VLCBUF3SIZE 360
59#define VLCBUF4SIZE 540
60
61/*putting these in IRAM actually makes PP slower*/
62
63static VLC_TYPE vlcbuf1[VLCBUF1SIZE][2] IBSS_ATTR_WMA_XL_IRAM MEM_ALIGN_ATTR;
64static VLC_TYPE vlcbuf2[VLCBUF2SIZE][2] MEM_ALIGN_ATTR;
65/* This buffer gets reused for lsp tables */
66static VLC_TYPE vlcbuf3[VLCBUF3SIZE][2] MEM_ALIGN_ATTR;
67static VLC_TYPE vlcbuf4[VLCBUF4SIZE][2] MEM_ALIGN_ATTR;
68
69
70
71
72/**
73 * Apply MDCT window and add into output.
74 *
75 * We ensure that when the windows overlap their squared sum
76 * is always 1 (MDCT reconstruction rule).
77 *
78 * The Vorbis I spec has a great diagram explaining this process.
79 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
80 */
81 static void wma_window(WMADecodeContext *s, fixed32 *in, fixed32 *out)
82 {
83 //float *in = s->output;
84 int block_len, bsize, n;
85
86 /* left part */
87
88 /* previous block was larger, so we'll use the size of the current
89 * block to set the window size*/
90 if (s->block_len_bits <= s->prev_block_len_bits) {
91 block_len = s->block_len;
92 bsize = s->frame_len_bits - s->block_len_bits;
93
94 vector_fmul_add_add(out, in, s->windows[bsize], block_len);
95
96 } else {
97 /*previous block was smaller or the same size, so use it's size to set the window length*/
98 block_len = 1 << s->prev_block_len_bits;
99 /*find the middle of the two overlapped blocks, this will be the first overlapped sample*/
100 n = (s->block_len - block_len) / 2;
101 bsize = s->frame_len_bits - s->prev_block_len_bits;
102
103 vector_fmul_add_add(out+n, in+n, s->windows[bsize], block_len);
104
105 memcpy(out+n+block_len, in+n+block_len, n*sizeof(fixed32));
106 }
107 /* Advance to the end of the current block and prepare to window it for the next block.
108 * Since the window function needs to be reversed, we do it backwards starting with the
109 * last sample and moving towards the first
110 */
111 out += s->block_len;
112 in += s->block_len;
113
114 /* right part */
115 if (s->block_len_bits <= s->next_block_len_bits) {
116 block_len = s->block_len;
117 bsize = s->frame_len_bits - s->block_len_bits;
118
119 vector_fmul_reverse(out, in, s->windows[bsize], block_len);
120
121 } else {
122 block_len = 1 << s->next_block_len_bits;
123 n = (s->block_len - block_len) / 2;
124 bsize = s->frame_len_bits - s->next_block_len_bits;
125
126 memcpy(out, in, n*sizeof(fixed32));
127
128 vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
129
130 memset(out+n+block_len, 0, n*sizeof(fixed32));
131 }
132 }
133
134
135
136
137/* XXX: use same run/length optimization as mpeg decoders */
138static void init_coef_vlc(VLC *vlc,
139 uint16_t **prun_table, uint16_t **plevel_table,
140 const CoefVLCTable *vlc_table, int tab)
141{
142 int n = vlc_table->n;
143 const uint8_t *table_bits = vlc_table->huffbits;
144 const uint32_t *table_codes = vlc_table->huffcodes;
145 const uint16_t *levels_table = vlc_table->levels;
146 uint16_t *run_table, *level_table;
147 const uint16_t *p;
148 int i, l, j, level;
149
150
151 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, INIT_VLC_USE_NEW_STATIC);
152
153 run_table = runtabarray[tab];
154 level_table= levtabarray[tab];
155
156 p = levels_table;
157 i = 2;
158 level = 1;
159 while (i < n)
160 {
161 l = *p++;
162 for(j=0;j<l;++j)
163 {
164 run_table[i] = j;
165 level_table[i] = level;
166 ++i;
167 }
168 ++level;
169 }
170 *prun_table = run_table;
171 *plevel_table = level_table;
172}
173
174int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
175{
176
177 int i, flags2;
178 fixed32 *window;
179 uint8_t *extradata;
180 fixed64 bps1;
181 fixed32 high_freq;
182 fixed64 bps;
183 int sample_rate1;
184 int coef_vlc_table;
185 // int filehandle;
186 #ifdef CPU_COLDFIRE
187 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
188 #endif
189
190 /*clear stereo setting to avoid glitches when switching stereo->mono*/
191 s->channel_coded[0]=0;
192 s->channel_coded[1]=0;
193 s->ms_stereo=0;
194
195 s->sample_rate = wfx->rate;
196 s->nb_channels = wfx->channels;
197 s->bit_rate = wfx->bitrate;
198 s->block_align = wfx->blockalign;
199
200 s->coefs = &coefsarray;
201 s->frame_out = &frame_out_buf;
202
203 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
204 s->version = 1;
205 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2 ) {
206 s->version = 2;
207 } else {
208 /*one of those other wma flavors that don't have GPLed decoders */
209 return -1;
210 }
211
212 /* extract flag infos */
213 flags2 = 0;
214 extradata = wfx->data;
215 if (s->version == 1 && wfx->datalen >= 4) {
216 flags2 = extradata[2] | (extradata[3] << 8);
217 }else if (s->version == 2 && wfx->datalen >= 6){
218 flags2 = extradata[4] | (extradata[5] << 8);
219 }
220 s->use_exp_vlc = flags2 & 0x0001;
221 s->use_bit_reservoir = flags2 & 0x0002;
222 s->use_variable_block_len = flags2 & 0x0004;
223
224 /* compute MDCT block size */
225 if (s->sample_rate <= 16000){
226 s->frame_len_bits = 9;
227 }else if (s->sample_rate <= 22050 ||
228 (s->sample_rate <= 32000 && s->version == 1)){
229 s->frame_len_bits = 10;
230 }else{
231 s->frame_len_bits = 11;
232 }
233 s->frame_len = 1 << s->frame_len_bits;
234 if (s-> use_variable_block_len)
235 {
236 int nb_max, nb;
237 nb = ((flags2 >> 3) & 3) + 1;
238 if ((s->bit_rate / s->nb_channels) >= 32000)
239 {
240 nb += 2;
241 }
242 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
243 if (nb > nb_max)
244 nb = nb_max;
245 s->nb_block_sizes = nb + 1;
246 }
247 else
248 {
249 s->nb_block_sizes = 1;
250 }
251
252 /* init rate dependant parameters */
253 s->use_noise_coding = 1;
254 high_freq = itofix64(s->sample_rate) >> 1;
255
256
257 /* if version 2, then the rates are normalized */
258 sample_rate1 = s->sample_rate;
259 if (s->version == 2)
260 {
261 if (sample_rate1 >= 44100)
262 sample_rate1 = 44100;
263 else if (sample_rate1 >= 22050)
264 sample_rate1 = 22050;
265 else if (sample_rate1 >= 16000)
266 sample_rate1 = 16000;
267 else if (sample_rate1 >= 11025)
268 sample_rate1 = 11025;
269 else if (sample_rate1 >= 8000)
270 sample_rate1 = 8000;
271 }
272
273 fixed64 tmp = itofix64(s->bit_rate);
274 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
275 bps = fixdiv64(tmp, tmp2);
276 fixed64 tim = bps * s->frame_len;
277 fixed64 tmpi = fixdiv64(tim,itofix64(8));
278 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
279
280 /* compute high frequency value and choose if noise coding should
281 be activated */
282 bps1 = bps;
283 if (s->nb_channels == 2)
284 bps1 = fixmul32(bps,0x1999a);
285 if (sample_rate1 == 44100)
286 {
287 if (bps1 >= 0x9c29)
288 s->use_noise_coding = 0;
289 else
290 high_freq = fixmul32(high_freq,0x6666);
291 }
292 else if (sample_rate1 == 22050)
293 {
294 if (bps1 >= 0x128f6)
295 s->use_noise_coding = 0;
296 else if (bps1 >= 0xb852)
297 high_freq = fixmul32(high_freq,0xb333);
298 else
299 high_freq = fixmul32(high_freq,0x999a);
300 }
301 else if (sample_rate1 == 16000)
302 {
303 if (bps > 0x8000)
304 high_freq = fixmul32(high_freq,0x8000);
305 else
306 high_freq = fixmul32(high_freq,0x4ccd);
307 }
308 else if (sample_rate1 == 11025)
309 {
310 high_freq = fixmul32(high_freq,0xb333);
311 }
312 else if (sample_rate1 == 8000)
313 {
314 if (bps <= 0xa000)
315 {
316 high_freq = fixmul32(high_freq,0x8000);
317 }
318 else if (bps > 0xc000)
319 {
320 s->use_noise_coding = 0;
321 }
322 else
323 {
324 high_freq = fixmul32(high_freq,0xa666);
325 }
326 }
327 else
328 {
329 if (bps >= 0xcccd)
330 {
331 high_freq = fixmul32(high_freq,0xc000);
332 }
333 else if (bps >= 0x999a)
334 {
335 high_freq = fixmul32(high_freq,0x999a);
336 }
337 else
338 {
339 high_freq = fixmul32(high_freq,0x8000);
340 }
341 }
342
343 /* compute the scale factor band sizes for each MDCT block size */
344 {
345 int a, b, pos, lpos, k, block_len, i, j, n;
346 const uint8_t *table;
347
348 if (s->version == 1)
349 {
350 s->coefs_start = 3;
351 }
352 else
353 {
354 s->coefs_start = 0;
355 }
356 for(k = 0; k < s->nb_block_sizes; ++k)
357 {
358 block_len = s->frame_len >> k;
359
360 if (s->version == 1)
361 {
362 lpos = 0;
363 for(i=0;i<25;++i)
364 {
365 a = wma_critical_freqs[i];
366 b = s->sample_rate;
367 pos = ((block_len * 2 * a) + (b >> 1)) / b;
368 if (pos > block_len)
369 pos = block_len;
370 s->exponent_bands[0][i] = pos - lpos;
371 if (pos >= block_len)
372 {
373 ++i;
374 break;
375 }
376 lpos = pos;
377 }
378 s->exponent_sizes[0] = i;
379 }
380 else
381 {
382 /* hardcoded tables */
383 table = NULL;
384 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
385 if (a < 3)
386 {
387 if (s->sample_rate >= 44100)
388 table = exponent_band_44100[a];
389 else if (s->sample_rate >= 32000)
390 table = exponent_band_32000[a];
391 else if (s->sample_rate >= 22050)
392 table = exponent_band_22050[a];
393 }
394 if (table)
395 {
396 n = *table++;
397 for(i=0;i<n;++i)
398 s->exponent_bands[k][i] = table[i];
399 s->exponent_sizes[k] = n;
400 }
401 else
402 {
403 j = 0;
404 lpos = 0;
405 for(i=0;i<25;++i)
406 {
407 a = wma_critical_freqs[i];
408 b = s->sample_rate;
409 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
410 pos <<= 2;
411 if (pos > block_len)
412 pos = block_len;
413 if (pos > lpos)
414 s->exponent_bands[k][j++] = pos - lpos;
415 if (pos >= block_len)
416 break;
417 lpos = pos;
418 }
419 s->exponent_sizes[k] = j;
420 }
421 }
422
423 /* max number of coefs */
424 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
425 /* high freq computation */
426
427 fixed32 tmp1 = high_freq*2; /* high_freq is a fixed32!*/
428 fixed32 tmp2=itofix32(s->sample_rate>>1);
429 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
430
431 /*
432 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
433 s->sample_rate + 0.5);*/
434
435 n = s->exponent_sizes[k];
436 j = 0;
437 pos = 0;
438 for(i=0;i<n;++i)
439 {
440 int start, end;
441 start = pos;
442 pos += s->exponent_bands[k][i];
443 end = pos;
444 if (start < s->high_band_start[k])
445 start = s->high_band_start[k];
446 if (end > s->coefs_end[k])
447 end = s->coefs_end[k];
448 if (end > start)
449 s->exponent_high_bands[k][j++] = end - start;
450 }
451 s->exponent_high_sizes[k] = j;
452 }
453 }
454
455 /* ffmpeg uses malloc to only allocate as many window sizes as needed.
456 * However, we're really only interested in the worst case memory usage.
457 * In the worst case you can have 5 window sizes, 128 doubling up 2048
458 * Smaller windows are handled differently.
459 * Since we don't have malloc, just statically allocate this
460 */
461 fixed32 *temp[5];
462 temp[0] = stat0;
463 temp[1] = stat1;
464 temp[2] = stat2;
465 temp[3] = stat3;
466 temp[4] = stat4;
467
468 /* init MDCT windows : simple sinus window */
469 for(i = 0; i < s->nb_block_sizes; i++)
470 {
471 int n, j;
472 fixed32 alpha;
473 n = 1 << (s->frame_len_bits - i);
474 window = temp[i];
475
476 /* this calculates 0.5/(2*n) */
477 alpha = (1<<15)>>(s->frame_len_bits - i+1);
478 for(j=0;j<n;++j)
479 {
480 fixed32 j2 = itofix32(j) + 0x8000;
481 /*alpha between 0 and pi/2*/
482 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0);
483 }
484 s->windows[i] = window;
485
486 }
487
488 s->reset_block_lengths = 1;
489
490 if (s->use_noise_coding)
491 {
492 /* init the noise generator */
493 if (s->use_exp_vlc)
494 {
495 s->noise_mult = 0x51f;
496 s->noise_table = noisetable_exp;
497 }
498 else
499 {
500 s->noise_mult = 0xa3d;
501 /* LSP values are simply 2x the EXP values */
502 for (i=0;i<NOISE_TAB_SIZE;++i)
503 noisetable_exp[i] = noisetable_exp[i]<< 1;
504 s->noise_table = noisetable_exp;
505 }
506#if 0
507/* We use a lookup table computered in advance, so no need to do this*/
508 {
509 unsigned int seed;
510 fixed32 norm;
511 seed = 1;
512 norm = 0; // PJJ: near as makes any diff to 0!
513 for (i=0;i<NOISE_TAB_SIZE;++i)
514 {
515 seed = seed * 314159 + 1;
516 s->noise_table[i] = itofix32((int)seed) * norm;
517 }
518 }
519#endif
520
521 s->hgain_vlc.table = vlcbuf4;
522 s->hgain_vlc.table_allocated = VLCBUF4SIZE;
523 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
524 hgain_huffbits, 1, 1,
525 hgain_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC);
526 }
527
528 if (s->use_exp_vlc)
529 {
530
531 s->exp_vlc.table = vlcbuf3;
532 s->exp_vlc.table_allocated = VLCBUF3SIZE;
533
534 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
535 scale_huffbits, 1, 1,
536 scale_huffcodes, 4, 4, INIT_VLC_USE_NEW_STATIC);
537 }
538 else
539 {
540 wma_lsp_to_curve_init(s, s->frame_len);
541 }
542
543 /* choose the VLC tables for the coefficients */
544 coef_vlc_table = 2;
545 if (s->sample_rate >= 32000)
546 {
547 if (bps1 < 0xb852)
548 coef_vlc_table = 0;
549 else if (bps1 < 0x128f6)
550 coef_vlc_table = 1;
551 }
552
553 /* since the coef2 table is the biggest and that has index 2 in coef_vlcs
554 it's safe to always assign like this */
555 runtabarray[0] = runtab_big; runtabarray[1] = runtab_small;
556 levtabarray[0] = levtab_big; levtabarray[1] = levtab_small;
557
558 s->coef_vlc[0].table = vlcbuf1;
559 s->coef_vlc[0].table_allocated = VLCBUF1SIZE;
560 s->coef_vlc[1].table = vlcbuf2;
561 s->coef_vlc[1].table_allocated = VLCBUF2SIZE;
562
563
564 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
565 &coef_vlcs[coef_vlc_table * 2], 0);
566 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
567 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
568
569 s->last_superframe_len = 0;
570 s->last_bitoffset = 0;
571
572 return 0;
573}
574
575
576/* compute x^-0.25 with an exponent and mantissa table. We use linear
577 interpolation to reduce the mantissa table size at a small speed
578 expense (linear interpolation approximately doubles the number of
579 bits of precision). */
580static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
581{
582 union {
583 float f;
584 unsigned int v;
585 } u, t;
586 unsigned int e, m;
587 fixed32 a, b;
588
589 u.f = fixtof64(x);
590 e = u.v >> 23;
591 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
592 /* build interpolation scale: 1 <= t < 2. */
593 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
594 a = ((fixed32*)s->lsp_pow_m_table1)[m];
595 b = ((fixed32*)s->lsp_pow_m_table2)[m];
596
597 /* lsp_pow_e_table contains 32.32 format */
598 /* TODO: Since we're unlikely have value that cover the whole
599 * IEEE754 range, we probably don't need to have all possible exponents */
600
601 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
602}
603
604static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
605{
606 fixed32 wdel, a, b, temp2;
607 int i;
608
609 wdel = fixdiv32(itofix32(1), itofix32(frame_len));
610 for (i=0; i<frame_len; ++i)
611 {
612 /* TODO: can probably reuse the trig_init values here */
613 fsincos((wdel*i)<<15, &temp2);
614 /* get 3 bits headroom + 1 bit from not doubleing the values */
615 s->lsp_cos_table[i] = temp2>>3;
616
617 }
618 /* NOTE: these two tables are needed to avoid two operations in
619 pow_m1_4 */
620 b = itofix32(1);
621 int ix = 0;
622
623 s->lsp_pow_m_table1 = &vlcbuf3[0];
624 s->lsp_pow_m_table2 = &vlcbuf3[1<<LSP_POW_BITS];
625
626 /*double check this later*/
627 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
628 {
629 a = pow_a_table[ix++]<<4;
630 ((fixed32*)s->lsp_pow_m_table1)[i] = 2 * a - b;
631 ((fixed32*)s->lsp_pow_m_table2)[i] = b - a;
632 b = a;
633 }
634
635}
636
637/* NOTE: We use the same code as Vorbis here */
638/* XXX: optimize it further with SSE/3Dnow */
639static void wma_lsp_to_curve(WMADecodeContext *s,
640 fixed32 *out,
641 fixed32 *val_max_ptr,
642 int n,
643 fixed32 *lsp)
644{
645 int i, j;
646 fixed32 p, q, w, v, val_max, temp2;
647
648 val_max = 0;
649 for(i=0;i<n;++i)
650 {
651 /* shift by 2 now to reduce rounding error,
652 * we can renormalize right before pow_m1_4
653 */
654
655 p = 0x8000<<5;
656 q = 0x8000<<5;
657 w = s->lsp_cos_table[i];
658
659 for (j=1;j<NB_LSP_COEFS;j+=2)
660 {
661 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
662 temp2 = ((w - (lsp[j - 1]<<11)));
663
664 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
665 q = fixmul32b(q, temp2 )<<4;
666 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
667 }
668
669 /* 2 in 5.27 format is 0x10000000 */
670 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
671 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
672
673 v = (p + q) >>9; /* p/q end up as 16.16 */
674 v = pow_m1_4(s, v);
675 if (v > val_max)
676 val_max = v;
677 out[i] = v;
678 }
679
680 *val_max_ptr = val_max;
681}
682
683/* decode exponents coded with LSP coefficients (same idea as Vorbis)
684 * only used for low bitrate (< 16kbps) files
685 */
686static void decode_exp_lsp(WMADecodeContext *s, int ch)
687{
688 fixed32 lsp_coefs[NB_LSP_COEFS];
689 int val, i;
690
691 for (i = 0; i < NB_LSP_COEFS; ++i)
692 {
693 if (i == 0 || i >= 8)
694 val = get_bits(&s->gb, 3);
695 else
696 val = get_bits(&s->gb, 4);
697 lsp_coefs[i] = lsp_codebook[i][val];
698 }
699
700 wma_lsp_to_curve(s,
701 s->exponents[ch],
702 &s->max_exponent[ch],
703 s->block_len,
704 lsp_coefs);
705}
706
707/* decode exponents coded with VLC codes - used for bitrate >= 32kbps*/
708static int decode_exp_vlc(WMADecodeContext *s, int ch)
709{
710 int last_exp, n, code;
711 const uint16_t *ptr, *band_ptr;
712 fixed32 v, max_scale;
713 fixed32 *q,*q_end;
714
715 /*accommodate the 60 negative indices */
716 const fixed32 *pow_10_to_yover16_ptr = &pow_10_to_yover16[61];
717
718 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
719 ptr = band_ptr;
720 q = s->exponents[ch];
721 q_end = q + s->block_len;
722 max_scale = 0;
723
724
725 if (s->version == 1) //wmav1 only
726 {
727 last_exp = get_bits(&s->gb, 5) + 10;
728
729 v = pow_10_to_yover16_ptr[last_exp];
730 max_scale = v;
731 n = *ptr++;
732 switch (n & 3) do {
733 case 0: *q++ = v;
734 case 3: *q++ = v;
735 case 2: *q++ = v;
736 case 1: *q++ = v;
737 } while ((n -= 4) > 0);
738 } else {
739 last_exp = 36;
740 }
741
742 while (q < q_end)
743 {
744 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
745 if (code < 0)
746 {
747 return -1;
748 }
749 /* NOTE: this offset is the same as MPEG4 AAC ! */
750 last_exp += code - 60;
751
752 v = pow_10_to_yover16_ptr[last_exp];
753 if (v > max_scale)
754 {
755 max_scale = v;
756 }
757 n = *ptr++;
758 switch (n & 3) do {
759 case 0: *q++ = v;
760 case 3: *q++ = v;
761 case 2: *q++ = v;
762 case 1: *q++ = v;
763 } while ((n -= 4) > 0);
764 }
765
766 s->max_exponent[ch] = max_scale;
767 return 0;
768}
769
770/* return 0 if OK. return 1 if last block of frame. return -1 if
771 unrecorrable error. */
772static int wma_decode_block(WMADecodeContext *s)
773{
774 int n, v, a, ch, code, bsize;
775 int coef_nb_bits, total_gain;
776 int nb_coefs[MAX_CHANNELS];
777 fixed32 mdct_norm;
778
779 /*DEBUGF("***decode_block: %d (%d samples of %d in frame)\n", s->block_num, s->block_len, s->frame_len);*/
780
781 /* compute current block length */
782 if (s->use_variable_block_len)
783 {
784 n = av_log2(s->nb_block_sizes - 1) + 1;
785
786 if (s->reset_block_lengths)
787 {
788 s->reset_block_lengths = 0;
789 v = get_bits(&s->gb, n);
790 if (v >= s->nb_block_sizes)
791 {
792 return -2;
793 }
794 s->prev_block_len_bits = s->frame_len_bits - v;
795 v = get_bits(&s->gb, n);
796 if (v >= s->nb_block_sizes)
797 {
798 return -3;
799 }
800 s->block_len_bits = s->frame_len_bits - v;
801 }
802 else
803 {
804 /* update block lengths */
805 s->prev_block_len_bits = s->block_len_bits;
806 s->block_len_bits = s->next_block_len_bits;
807 }
808 v = get_bits(&s->gb, n);
809
810 if (v >= s->nb_block_sizes)
811 {
812 // rb->splash(HZ*4, "v was %d", v); //5, 7
813 return -4; //this is it
814 }
815 else{
816 //rb->splash(HZ, "passed v block (%d)!", v);
817 }
818 s->next_block_len_bits = s->frame_len_bits - v;
819 }
820 else
821 {
822 /* fixed block len */
823 s->next_block_len_bits = s->frame_len_bits;
824 s->prev_block_len_bits = s->frame_len_bits;
825 s->block_len_bits = s->frame_len_bits;
826 }
827 /* now check if the block length is coherent with the frame length */
828 s->block_len = 1 << s->block_len_bits;
829
830 if ((s->block_pos + s->block_len) > s->frame_len)
831 {
832 return -5; //oddly 32k sample from tracker fails here
833 }
834
835 if (s->nb_channels == 2)
836 {
837 s->ms_stereo = get_bits1(&s->gb);
838 }
839 v = 0;
840 for (ch = 0; ch < s->nb_channels; ++ch)
841 {
842 a = get_bits1(&s->gb);
843 s->channel_coded[ch] = a;
844 v |= a;
845 }
846 /* if no channel coded, no need to go further */
847 /* XXX: fix potential framing problems */
848 if (!v)
849 {
850 goto next;
851 }
852
853 bsize = s->frame_len_bits - s->block_len_bits;
854
855 /* read total gain and extract corresponding number of bits for
856 coef escape coding */
857 total_gain = 1;
858 for(;;)
859 {
860 a = get_bits(&s->gb, 7);
861 total_gain += a;
862 if (a != 127)
863 {
864 break;
865 }
866 }
867
868 if (total_gain < 15)
869 coef_nb_bits = 13;
870 else if (total_gain < 32)
871 coef_nb_bits = 12;
872 else if (total_gain < 40)
873 coef_nb_bits = 11;
874 else if (total_gain < 45)
875 coef_nb_bits = 10;
876 else
877 coef_nb_bits = 9;
878
879 /* compute number of coefficients */
880 n = s->coefs_end[bsize] - s->coefs_start;
881
882 for(ch = 0; ch < s->nb_channels; ++ch)
883 {
884 nb_coefs[ch] = n;
885 }
886 /* complex coding */
887 if (s->use_noise_coding)
888 {
889
890 for(ch = 0; ch < s->nb_channels; ++ch)
891 {
892 if (s->channel_coded[ch])
893 {
894 int i, n, a;
895 n = s->exponent_high_sizes[bsize];
896 for(i=0;i<n;++i)
897 {
898 a = get_bits1(&s->gb);
899 s->high_band_coded[ch][i] = a;
900 /* if noise coding, the coefficients are not transmitted */
901 if (a)
902 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
903 }
904 }
905 }
906 for(ch = 0; ch < s->nb_channels; ++ch)
907 {
908 if (s->channel_coded[ch])
909 {
910 int i, n, val, code;
911
912 n = s->exponent_high_sizes[bsize];
913 val = (int)0x80000000;
914 for(i=0;i<n;++i)
915 {
916 if (s->high_band_coded[ch][i])
917 {
918 if (val == (int)0x80000000)
919 {
920 val = get_bits(&s->gb, 7) - 19;
921 }
922 else
923 {
924 //code = get_vlc(&s->gb, &s->hgain_vlc);
925 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
926 if (code < 0)
927 {
928 return -6;
929 }
930 val += code - 18;
931 }
932 s->high_band_values[ch][i] = val;
933 }
934 }
935 }
936 }
937 }
938
939 /* exponents can be reused in short blocks. */
940 if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb))
941 {
942 for(ch = 0; ch < s->nb_channels; ++ch)
943 {
944 if (s->channel_coded[ch])
945 {
946 if (s->use_exp_vlc)
947 {
948 if (decode_exp_vlc(s, ch) < 0)
949 {
950 return -7;
951 }
952 }
953 else
954 {
955 decode_exp_lsp(s, ch);
956 }
957 s->exponents_bsize[ch] = bsize;
958 }
959 }
960 }
961
962 /* parse spectral coefficients : just RLE encoding */
963 for(ch = 0; ch < s->nb_channels; ++ch)
964 {
965 if (s->channel_coded[ch])
966 {
967 VLC *coef_vlc;
968 int level, run, sign, tindex;
969 int16_t *ptr, *eptr;
970 const int16_t *level_table, *run_table;
971
972 /* special VLC tables are used for ms stereo because
973 there is potentially less energy there */
974 tindex = (ch == 1 && s->ms_stereo);
975 coef_vlc = &s->coef_vlc[tindex];
976 run_table = s->run_table[tindex];
977 level_table = s->level_table[tindex];
978 /* XXX: optimize */
979 ptr = &s->coefs1[ch][0];
980 eptr = ptr + nb_coefs[ch];
981 memset(ptr, 0, s->block_len * sizeof(int16_t));
982
983 for(;;)
984 {
985 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
986
987 if (code < 0)
988 {
989 return -8;
990 }
991 if (code == 1)
992 {
993 /* EOB */
994 break;
995 }
996 else if (code == 0)
997 {
998 /* escape */
999 level = get_bits(&s->gb, coef_nb_bits);
1000 /* NOTE: this is rather suboptimal. reading
1001 block_len_bits would be better */
1002 run = get_bits(&s->gb, s->frame_len_bits);
1003 }
1004 else
1005 {
1006 /* normal code */
1007 run = run_table[code];
1008 level = level_table[code];
1009 }
1010 sign = get_bits1(&s->gb);
1011 if (!sign)
1012 level = -level;
1013 ptr += run;
1014 if (ptr >= eptr)
1015 {
1016 break;
1017 }
1018 *ptr++ = level;
1019
1020
1021 /* NOTE: EOB can be omitted */
1022 if (ptr >= eptr)
1023 break;
1024 }
1025 }
1026 if (s->version == 1 && s->nb_channels >= 2)
1027 {
1028 align_get_bits(&s->gb);
1029 }
1030 }
1031
1032 {
1033 int n4 = s->block_len >> 1;
1034
1035
1036 mdct_norm = 0x10000>>(s->block_len_bits-1);
1037
1038 if (s->version == 1)
1039 {
1040 mdct_norm *= fixtoi32(fixsqrt32(itofix32(n4)));
1041 }
1042 }
1043
1044
1045 /* finally compute the MDCT coefficients */
1046 for(ch = 0; ch < s->nb_channels; ++ch)
1047 {
1048 if (s->channel_coded[ch])
1049 {
1050 int16_t *coefs1;
1051 fixed32 *exponents;
1052 fixed32 *coefs, atemp;
1053 fixed64 mult;
1054 fixed64 mult1;
1055 fixed32 noise, temp1, temp2, mult2;
1056 int i, j, n, n1, last_high_band, esize;
1057 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
1058
1059 //total_gain, coefs1, mdctnorm are lossless
1060
1061 coefs1 = s->coefs1[ch];
1062 exponents = s->exponents[ch];
1063 esize = s->exponents_bsize[ch];
1064 coefs = (*(s->coefs))[ch];
1065 n=0;
1066
1067 /*
1068 * The calculation of coefs has a shift right by 2 built in. This
1069 * prepares samples for the Tremor IMDCT which uses a slightly
1070 * different fixed format then the ffmpeg one. If the old ffmpeg
1071 * imdct is used, each shift storing into coefs should be reduced
1072 * by 1.
1073 * See SVN logs for details.
1074 */
1075
1076
1077 if (s->use_noise_coding)
1078 {
1079 /*This case is only used for low bitrates (typically less then 32kbps)*/
1080
1081 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1082
1083 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1084 mult = mult* mdct_norm;
1085 mult1 = mult;
1086
1087 /* very low freqs : noise */
1088 for(i = 0;i < s->coefs_start; ++i)
1089 {
1090 *coefs++ = fixmul32( (fixmul32(s->noise_table[s->noise_index],
1091 exponents[i<<bsize>>esize])>>4),Fixed32From64(mult1)) >>2;
1092 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1093 }
1094
1095 n1 = s->exponent_high_sizes[bsize];
1096
1097 /* compute power of high bands */
1098 exponents = s->exponents[ch] +(s->high_band_start[bsize]<<bsize);
1099 last_high_band = 0; /* avoid warning */
1100 for (j=0;j<n1;++j)
1101 {
1102 n = s->exponent_high_bands[s->frame_len_bits -
1103 s->block_len_bits][j];
1104 if (s->high_band_coded[ch][j])
1105 {
1106 fixed32 e2, v;
1107 e2 = 0;
1108 for(i = 0;i < n; ++i)
1109 {
1110 /*v is normalized later on so its fixed format is irrelevant*/
1111 v = exponents[i<<bsize>>esize]>>4;
1112 e2 += fixmul32(v, v)>>3;
1113 }
1114 exp_power[j] = e2/n; /*n is an int...*/
1115 last_high_band = j;
1116 }
1117 exponents += n<<bsize;
1118 }
1119
1120 /* main freqs and high freqs */
1121 exponents = s->exponents[ch] + (s->coefs_start<<bsize);
1122 for(j=-1;j<n1;++j)
1123 {
1124 if (j < 0)
1125 {
1126 n = s->high_band_start[bsize] -
1127 s->coefs_start;
1128 }
1129 else
1130 {
1131 n = s->exponent_high_bands[s->frame_len_bits -
1132 s->block_len_bits][j];
1133 }
1134 if (j >= 0 && s->high_band_coded[ch][j])
1135 {
1136 /* use noise with specified power */
1137 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1138
1139 /*mult1 is 48.16, pow_table is 48.16*/
1140 mult1 = fixmul32(fixsqrt32(tmp),
1141 pow_table[s->high_band_values[ch][j]+20]) >> 16;
1142
1143 /*this step has a fairly high degree of error for some reason*/
1144 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1145 mult1 = mult1*mdct_norm>>PRECISION;
1146 for(i = 0;i < n; ++i)
1147 {
1148 noise = s->noise_table[s->noise_index];
1149 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1150 *coefs++ = fixmul32((fixmul32(exponents[i<<bsize>>esize],noise)>>4),
1151 Fixed32From64(mult1)) >>2;
1152
1153 }
1154 exponents += n<<bsize;
1155 }
1156 else
1157 {
1158 /* coded values + small noise */
1159 for(i = 0;i < n; ++i)
1160 {
1161 noise = s->noise_table[s->noise_index];
1162 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1163
1164 /*don't forget to renormalize the noise*/
1165 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1166 temp2 = fixmul32(exponents[i<<bsize>>esize], mult>>18);
1167 *coefs++ = fixmul32(temp1, temp2);
1168 }
1169 exponents += n<<bsize;
1170 }
1171 }
1172
1173 /* very high freqs : noise */
1174 n = s->block_len - s->coefs_end[bsize];
1175 mult2 = fixmul32(mult>>16,exponents[((-1<<bsize))>>esize]) ;
1176 for (i = 0; i < n; ++i)
1177 {
1178 /*renormalize the noise product and then reduce to 14.18 precison*/
1179 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>6;
1180
1181 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1182 }
1183 }
1184 else
1185 {
1186 /*Noise coding not used, simply convert from exp to fixed representation*/
1187
1188 fixed32 mult3 = (fixed32)(fixdiv64(pow_table[total_gain+20],
1189 Fixed32To64(s->max_exponent[ch])));
1190 mult3 = fixmul32(mult3, mdct_norm);
1191
1192 /*zero the first 3 coefficients for WMA V1, does nothing otherwise*/
1193 for(i=0; i<s->coefs_start; i++)
1194 *coefs++=0;
1195
1196 n = nb_coefs[ch];
1197
1198 /* XXX: optimize more, unrolling this loop in asm
1199 might be a good idea */
1200
1201 for(i = 0;i < n; ++i)
1202 {
1203 /*ffmpeg imdct needs 15.17, while tremor 14.18*/
1204 atemp = (coefs1[i] * mult3)>>2;
1205 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1206 }
1207 n = s->block_len - s->coefs_end[bsize];
1208 memset(coefs, 0, n*sizeof(fixed32));
1209 }
1210 }
1211 }
1212
1213
1214
1215 if (s->ms_stereo && s->channel_coded[1])
1216 {
1217 fixed32 a, b;
1218 int i;
1219 fixed32 (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1220
1221 /* nominal case for ms stereo: we do it before mdct */
1222 /* no need to optimize this case because it should almost
1223 never happen */
1224 if (!s->channel_coded[0])
1225 {
1226 memset((*(s->coefs))[0], 0, sizeof(fixed32) * s->block_len);
1227 s->channel_coded[0] = 1;
1228 }
1229
1230 for(i = 0; i < s->block_len; ++i)
1231 {
1232 a = (*coefs)[0][i];
1233 b = (*coefs)[1][i];
1234 (*coefs)[0][i] = a + b;
1235 (*coefs)[1][i] = a - b;
1236 }
1237 }
1238
1239 for(ch = 0; ch < s->nb_channels; ++ch)
1240 {
1241 /* BLOCK_MAX_SIZE is 2048 (samples) and MAX_CHANNELS is 2. */
1242 static uint32_t scratch_buf[BLOCK_MAX_SIZE * MAX_CHANNELS] IBSS_ATTR MEM_ALIGN_ATTR;
1243 if (s->channel_coded[ch])
1244 {
1245 int n4, index;
1246
1247 n4 = s->block_len >>1;
1248
1249 ff_imdct_calc((s->frame_len_bits - bsize + 1),
1250 scratch_buf,
1251 (*(s->coefs))[ch]);
1252
1253 /* add in the frame */
1254 index = (s->frame_len / 2) + s->block_pos - n4;
1255 wma_window(s, scratch_buf, &((*s->frame_out)[ch][index]));
1256
1257
1258
1259 /* specific fast case for ms-stereo : add to second
1260 channel if it is not coded */
1261 if (s->ms_stereo && !s->channel_coded[1])
1262 {
1263 wma_window(s, scratch_buf, &((*s->frame_out)[1][index]));
1264 }
1265 }
1266 }
1267next:
1268 /* update block number */
1269 ++s->block_num;
1270 s->block_pos += s->block_len;
1271 if (s->block_pos >= s->frame_len)
1272 {
1273 return 1;
1274 }
1275 else
1276 {
1277 return 0;
1278 }
1279}
1280
1281/* decode a frame of frame_len samples */
1282static int wma_decode_frame(WMADecodeContext *s)
1283{
1284 int ret;
1285
1286 /* read each block */
1287 s->block_num = 0;
1288 s->block_pos = 0;
1289
1290
1291 for(;;)
1292 {
1293 ret = wma_decode_block(s);
1294 if (ret < 0)
1295 {
1296
1297 DEBUGF("wma_decode_block failed with code %d\n", ret);
1298 return -1;
1299 }
1300 if (ret)
1301 {
1302 break;
1303 }
1304 }
1305
1306 return 0;
1307}
1308
1309/* Initialise the superframe decoding */
1310
1311int wma_decode_superframe_init(WMADecodeContext* s,
1312 const uint8_t *buf, /*input*/
1313 int buf_size)
1314{
1315 if (buf_size==0)
1316 {
1317 s->last_superframe_len = 0;
1318 return 0;
1319 }
1320
1321 s->current_frame = 0;
1322
1323 init_get_bits(&s->gb, buf, buf_size*8);
1324
1325 if (s->use_bit_reservoir)
1326 {
1327 /* read super frame header */
1328 skip_bits(&s->gb, 4); /* super frame index */
1329 s->nb_frames = get_bits(&s->gb, 4);
1330
1331 if (s->last_superframe_len == 0)
1332 s->nb_frames --;
1333 else if (s->nb_frames == 0)
1334 s->nb_frames++;
1335
1336 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1337 } else {
1338 s->nb_frames = 1;
1339 }
1340
1341 return 1;
1342}
1343
1344
1345/* Decode a single frame in the current superframe - return -1 if
1346 there was a decoding error, or the number of samples decoded.
1347*/
1348
1349int wma_decode_superframe_frame(WMADecodeContext* s,
1350 const uint8_t *buf, /*input*/
1351 int buf_size)
1352{
1353 int pos, len, ch;
1354 uint8_t *q;
1355 int done = 0;
1356
1357 for(ch = 0; ch < s->nb_channels; ch++)
1358 memmove(&((*s->frame_out)[ch][0]),
1359 &((*s->frame_out)[ch][s->frame_len]),
1360 s->frame_len * sizeof(fixed32));
1361
1362 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1363 {
1364 if (s->last_superframe_len > 0)
1365 {
1366 /* add s->bit_offset bits to last frame */
1367 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1368 MAX_CODED_SUPERFRAME_SIZE)
1369 {
1370 DEBUGF("superframe size too large error\n");
1371 goto fail;
1372 }
1373 q = s->last_superframe + s->last_superframe_len;
1374 len = s->bit_offset;
1375 while (len > 7)
1376 {
1377 *q++ = (get_bits)(&s->gb, 8);
1378 len -= 8;
1379 }
1380 if (len > 0)
1381 {
1382 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1383 }
1384
1385 /* XXX: s->bit_offset bits into last frame */
1386 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1387 /* skip unused bits */
1388 if (s->last_bitoffset > 0)
1389 skip_bits(&s->gb, s->last_bitoffset);
1390
1391 /* this frame is stored in the last superframe and in the
1392 current one */
1393 if (wma_decode_frame(s) < 0)
1394 {
1395 goto fail;
1396 }
1397 done = 1;
1398 }
1399
1400 /* read each frame starting from s->bit_offset */
1401 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1402 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1403 len = pos & 7;
1404 if (len > 0)
1405 skip_bits(&s->gb, len);
1406
1407 s->reset_block_lengths = 1;
1408 }
1409
1410 /* If we haven't decoded a frame yet, do it now */
1411 if (!done)
1412 {
1413 if (wma_decode_frame(s) < 0)
1414 {
1415 goto fail;
1416 }
1417 }
1418
1419 s->current_frame++;
1420
1421 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1422 {
1423 /* we copy the end of the frame in the last frame buffer */
1424 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1425 s->last_bitoffset = pos & 7;
1426 pos >>= 3;
1427 len = buf_size - pos;
1428 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1429 {
1430 DEBUGF("superframe size too large error after decoding\n");
1431 goto fail;
1432 }
1433 s->last_superframe_len = len;
1434 memcpy(s->last_superframe, buf + pos, len);
1435 }
1436
1437 return s->frame_len;
1438
1439fail:
1440 /* when error, we reset the bit reservoir */
1441
1442 s->last_superframe_len = 0;
1443 return -1;
1444}
1445