codec: ass: Don't force fonts that aren't shipped anymore in the winstore app
[vlc.git] / modules / codec / wmafixed / wmadeci.c
blobb0997cc65173fe269a4040627944f0fde5b5bc35
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 #define IBSS_ATTR
26 #define ICONST_ATTR
27 #define ICODE_ATTR
29 #ifdef NDEBUG
30 #include <stdio.h>
31 #undef WMA_DEBUG /* enable when debugging wma */
32 #endif
34 #include "asf.h"
35 #include "wmadec.h"
36 #include "wmafixed.h"
37 #include "bitstream.h"
38 #include <string.h> /* memcpy() */
40 #define VLCBITS 7 /*7 is the lowest without glitching*/
41 #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
43 #define EXPVLCBITS 7
44 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
46 #define HGAINVLCBITS 9
47 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
50 typedef struct CoefVLCTable
52 int n; /* total number of codes */
53 const uint32_t *huffcodes; /* VLC bit values */
54 const uint8_t *huffbits; /* VLC bit size */
55 const uint16_t *levels; /* table to build run/level tables */
57 CoefVLCTable;
59 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
61 int32_t coefsarray[MAX_CHANNELS][BLOCK_MAX_SIZE] IBSS_ATTR;
63 /* static variables that replace malloced stuff */
64 /* these are the MDCT reconstruction windows */
65 int32_t stat0[2048], stat1[1024], stat2[512], stat3[256], stat4[128];
67 /* these are VLC lookup tables */
68 uint16_t *runtabarray[2], *levtabarray[2];
70 /* these could be made smaller since only one can be 1336 */
71 uint16_t runtab0[1336], runtab1[1336], levtab0[1336], levtab1[1336];
73 #define VLCBUF1SIZE 4598
74 #define VLCBUF2SIZE 3574
75 #define VLCBUF3SIZE 360
76 #define VLCBUF4SIZE 540
78 /*putting these in IRAM actually makes PP slower*/
80 VLC_TYPE vlcbuf1[VLCBUF1SIZE][2];
81 VLC_TYPE vlcbuf2[VLCBUF2SIZE][2];
82 VLC_TYPE vlcbuf3[VLCBUF3SIZE][2];
83 VLC_TYPE vlcbuf4[VLCBUF4SIZE][2];
85 #include "wmadata.h" // PJJ
88 * Helper functions for wma_window.
93 #ifdef __arm__
94 static inline
95 void vector_fmul_add_add(int32_t *dst, const int32_t *data,
96 const int32_t *window, int n)
98 /* Block sizes are always power of two */
99 asm volatile (
100 "0:"
101 "ldmia %[d]!, {r0, r1};"
102 "ldmia %[w]!, {r4, r5};"
103 /* consume the first data and window value so we can use those
104 * registers again */
105 "smull r8, r9, r0, r4;"
106 "ldmia %[dst], {r0, r4};"
107 "add r0, r0, r9, lsl #1;" /* *dst=*dst+(r9<<1)*/
108 "smull r8, r9, r1, r5;"
109 "add r1, r4, r9, lsl #1;"
110 "stmia %[dst]!, {r0, r1};"
111 "subs %[n], %[n], #2;"
112 "bne 0b;"
113 : [d] "+r" (data), [w] "+r" (window), [dst] "+r" (dst), [n] "+r" (n)
114 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
117 static inline
118 void vector_fmul_reverse(int32_t *dst, const int32_t *src0, const int32_t *src1,
119 int len)
121 /* Block sizes are always power of two */
122 asm volatile (
123 "add %[s1], %[s1], %[n], lsl #2;"
124 "0:"
125 "ldmia %[s0]!, {r0, r1};"
126 "ldmdb %[s1]!, {r4, r5};"
127 "smull r8, r9, r0, r5;"
128 "mov r0, r9, lsl #1;"
129 "smull r8, r9, r1, r4;"
130 "mov r1, r9, lsl #1;"
131 "stmia %[dst]!, {r0, r1};"
132 "subs %[n], %[n], #2;"
133 "bne 0b;"
134 : [s0] "+r" (src0), [s1] "+r" (src1), [dst] "+r" (dst), [n] "+r" (len)
135 : : "r0", "r1", "r4", "r5", "r8", "r9", "memory", "cc");
138 #elif defined(CPU_COLDFIRE)
140 static inline
141 void vector_fmul_add_add(int32_t *dst, const int32_t *data,
142 const int32_t *window, int n)
144 /* Block sizes are always power of two. Smallest block is always way bigger
145 * than four too.*/
146 asm volatile (
147 "0:"
148 "movem.l (%[d]), %%d0-%%d3;"
149 "movem.l (%[w]), %%d4-%%d5/%%a0-%%a1;"
150 "mac.l %%d0, %%d4, %%acc0;"
151 "mac.l %%d1, %%d5, %%acc1;"
152 "mac.l %%d2, %%a0, %%acc2;"
153 "mac.l %%d3, %%a1, %%acc3;"
154 "lea.l (16, %[d]), %[d];"
155 "lea.l (16, %[w]), %[w];"
156 "movclr.l %%acc0, %%d0;"
157 "movclr.l %%acc1, %%d1;"
158 "movclr.l %%acc2, %%d2;"
159 "movclr.l %%acc3, %%d3;"
160 "add.l %%d0, (%[dst])+;"
161 "add.l %%d1, (%[dst])+;"
162 "add.l %%d2, (%[dst])+;"
163 "add.l %%d3, (%[dst])+;"
164 "subq.l #4, %[n];"
165 "jne 0b;"
166 : [d] "+a" (data), [w] "+a" (window), [dst] "+a" (dst), [n] "+d" (n)
167 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
170 static inline
171 void vector_fmul_reverse(int32_t *dst, const int32_t *src0, const int32_t *src1,
172 int len)
174 /* Block sizes are always power of two. Smallest block is always way bigger
175 * than four too.*/
176 asm volatile (
177 "lea.l (-16, %[s1], %[n]*4), %[s1];"
178 "0:"
179 "movem.l (%[s0]), %%d0-%%d3;"
180 "movem.l (%[s1]), %%d4-%%d5/%%a0-%%a1;"
181 "mac.l %%d0, %%a1, %%acc0;"
182 "mac.l %%d1, %%a0, %%acc1;"
183 "mac.l %%d2, %%d5, %%acc2;"
184 "mac.l %%d3, %%d4, %%acc3;"
185 "lea.l (16, %[s0]), %[s0];"
186 "lea.l (-16, %[s1]), %[s1];"
187 "movclr.l %%acc0, %%d0;"
188 "movclr.l %%acc1, %%d1;"
189 "movclr.l %%acc2, %%d2;"
190 "movclr.l %%acc3, %%d3;"
191 "movem.l %%d0-%%d3, (%[dst]);"
192 "lea.l (16, %[dst]), %[dst];"
193 "subq.l #4, %[n];"
194 "jne 0b;"
195 : [s0] "+a" (src0), [s1] "+a" (src1), [dst] "+a" (dst), [n] "+d" (len)
196 : : "d0", "d1", "d2", "d3", "d4", "d5", "a0", "a1", "memory", "cc");
199 #else
201 static inline void vector_fmul_add_add(int32_t *dst, const int32_t *src0, const int32_t *src1, int len){
202 int i;
203 for(i=0; i<len; i++)
204 dst[i] = fixmul32b(src0[i], src1[i]) + dst[i];
207 static inline void vector_fmul_reverse(int32_t *dst, const int32_t *src0, const int32_t *src1, int len){
208 int i;
209 src1 += len-1;
210 for(i=0; i<len; i++)
211 dst[i] = fixmul32b(src0[i], src1[-i]);
214 #endif
217 * Apply MDCT window and add into output.
219 * We ensure that when the windows overlap their squared sum
220 * is always 1 (MDCT reconstruction rule).
222 * The Vorbis I spec has a great diagram explaining this process.
223 * See section 1.3.2.3 of http://xiph.org/vorbis/doc/Vorbis_I_spec.html
225 static void wma_window(WMADecodeContext *s, int32_t *in, int32_t *out)
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) >> 1;
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(int32_t));
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) >> 1;
265 bsize = s->frame_len_bits - s->next_block_len_bits;
267 memcpy(out, in, n*sizeof(int32_t));
269 vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
271 memset(out+n+block_len, 0, n*sizeof(int32_t));
275 /* XXX: use same run/length optimization as mpeg decoders */
276 static void init_coef_vlc(VLC *vlc,
277 uint16_t **prun_table, uint16_t **plevel_table,
278 const CoefVLCTable *vlc_table, int tab)
280 int n = vlc_table->n;
281 const uint8_t *table_bits = vlc_table->huffbits;
282 const uint32_t *table_codes = vlc_table->huffcodes;
283 const uint16_t *levels_table = vlc_table->levels;
284 uint16_t *run_table, *level_table;
285 const uint16_t *p;
286 int i, l, j, level;
288 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
290 run_table = runtabarray[tab];
291 level_table= levtabarray[tab];
293 p = levels_table;
294 i = 2;
295 level = 1;
296 while (i < n)
298 l = *p++;
299 for(j=0;j<l;++j)
301 run_table[i] = j;
302 level_table[i] = level;
303 ++i;
305 ++level;
307 *prun_table = run_table;
308 *plevel_table = level_table;
311 int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
313 int i, flags1, flags2;
314 int32_t *window;
315 uint8_t *extradata;
316 int64_t bps1;
317 int32_t high_freq;
318 int64_t bps;
319 int sample_rate1;
320 int coef_vlc_table;
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 dependent parameters */
387 s->use_noise_coding = 1;
388 high_freq = itofix64(s->sample_rate) >> 1;
390 /* if version 2, then the rates are normalized */
391 sample_rate1 = s->sample_rate;
392 if (s->version == 2)
394 if (sample_rate1 >= 44100)
395 sample_rate1 = 44100;
396 else if (sample_rate1 >= 22050)
397 sample_rate1 = 22050;
398 else if (sample_rate1 >= 16000)
399 sample_rate1 = 16000;
400 else if (sample_rate1 >= 11025)
401 sample_rate1 = 11025;
402 else if (sample_rate1 >= 8000)
403 sample_rate1 = 8000;
406 int64_t tmp = itofix64(s->bit_rate);
407 int64_t tmp2 = itofix64(s->nb_channels * s->sample_rate);
408 bps = fixdiv64(tmp, tmp2);
409 int64_t tim = bps * s->frame_len;
410 int64_t tmpi = fixdiv64(tim,itofix64(8));
411 s->byte_offset_bits = av_log2(fixtoi64(tmpi+0x8000)) + 2;
413 /* compute high frequency value and choose if noise coding should
414 be activated */
415 bps1 = bps;
416 if (s->nb_channels == 2)
417 bps1 = fixmul32(bps,0x1999a);
418 if (sample_rate1 == 44100)
420 if (bps1 >= 0x9c29)
421 s->use_noise_coding = 0;
422 else
423 high_freq = fixmul32(high_freq,0x6666);
425 else if (sample_rate1 == 22050)
427 if (bps1 >= 0x128f6)
428 s->use_noise_coding = 0;
429 else if (bps1 >= 0xb852)
430 high_freq = fixmul32(high_freq,0xb333);
431 else
432 high_freq = fixmul32(high_freq,0x999a);
434 else if (sample_rate1 == 16000)
436 if (bps > 0x8000)
437 high_freq = fixmul32(high_freq,0x8000);
438 else
439 high_freq = fixmul32(high_freq,0x4ccd);
441 else if (sample_rate1 == 11025)
443 high_freq = fixmul32(high_freq,0xb333);
445 else if (sample_rate1 == 8000)
447 if (bps <= 0xa000)
449 high_freq = fixmul32(high_freq,0x8000);
451 else if (bps > 0xc000)
453 s->use_noise_coding = 0;
455 else
457 high_freq = fixmul32(high_freq,0xa666);
460 else
462 if (bps >= 0xcccd)
464 high_freq = fixmul32(high_freq,0xc000);
466 else if (bps >= 0x999a)
468 high_freq = fixmul32(high_freq,0x999a);
470 else
472 high_freq = fixmul32(high_freq,0x8000);
476 /* compute the scale factor band sizes for each MDCT block size */
478 int a, b, pos, lpos, k, block_len, i, j, n;
479 const uint8_t *table;
481 if (s->version == 1)
483 s->coefs_start = 3;
485 else
487 s->coefs_start = 0;
489 for(k = 0; k < s->nb_block_sizes; ++k)
491 block_len = s->frame_len >> k;
493 if (s->version == 1)
495 lpos = 0;
496 for(i=0;i<25;++i)
498 a = wma_critical_freqs[i];
499 b = s->sample_rate;
500 pos = ((block_len * 2 * a) + (b >> 1)) / b;
501 if (pos > block_len)
502 pos = block_len;
503 s->exponent_bands[0][i] = pos - lpos;
504 if (pos >= block_len)
506 ++i;
507 break;
509 lpos = pos;
511 s->exponent_sizes[0] = i;
513 else
515 /* hardcoded tables */
516 table = NULL;
517 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
518 if (a < 3)
520 if (s->sample_rate >= 44100)
521 table = exponent_band_44100[a];
522 else if (s->sample_rate >= 32000)
523 table = exponent_band_32000[a];
524 else if (s->sample_rate >= 22050)
525 table = exponent_band_22050[a];
527 if (table)
529 n = *table++;
530 for(i=0;i<n;++i)
531 s->exponent_bands[k][i] = table[i];
532 s->exponent_sizes[k] = n;
534 else
536 j = 0;
537 lpos = 0;
538 for(i=0;i<25;++i)
540 a = wma_critical_freqs[i];
541 b = s->sample_rate;
542 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
543 pos <<= 2;
544 if (pos > block_len)
545 pos = block_len;
546 if (pos > lpos)
547 s->exponent_bands[k][j++] = pos - lpos;
548 if (pos >= block_len)
549 break;
550 lpos = pos;
552 s->exponent_sizes[k] = j;
556 /* max number of coefs */
557 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
558 /* high freq computation */
560 int32_t tmp1 = high_freq*2; /* high_freq is a int32_t!*/
561 int32_t tmp2=itofix32(s->sample_rate>>1);
562 s->high_band_start[k] = fixtoi32( fixdiv32(tmp1, tmp2) * (block_len>>1) +0x8000);
565 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
566 s->sample_rate + 0.5);*/
568 n = s->exponent_sizes[k];
569 j = 0;
570 pos = 0;
571 for(i=0;i<n;++i)
573 int start, end;
574 start = pos;
575 pos += s->exponent_bands[k][i];
576 end = pos;
577 if (start < s->high_band_start[k])
578 start = s->high_band_start[k];
579 if (end > s->coefs_end[k])
580 end = s->coefs_end[k];
581 if (end > start)
582 s->exponent_high_bands[k][j++] = end - start;
584 s->exponent_high_sizes[k] = j;
588 mdct_init_global();
590 for(i = 0; i < s->nb_block_sizes; ++i)
592 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
595 /*ffmpeg uses malloc to only allocate as many window sizes as needed.
596 * 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 int32_t *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 int32_t alpha;
613 n = 1 << (s->frame_len_bits - i);
614 window = temp[i];
616 alpha = (1<<15)>>(s->frame_len_bits - i+1); /* this calculates 0.5/(2*n) */
617 for(j=0;j<n;++j)
619 int32_t j2 = itofix32(j) + 0x8000;
620 window[j] = fsincos(fixmul32(j2,alpha)<<16, 0); /* alpha between 0 and pi/2 */
623 s->windows[i] = window;
627 s->reset_block_lengths = 1;
629 if (s->use_noise_coding)
631 /* init the noise generator */
632 if (s->use_exp_vlc)
634 s->noise_mult = 0x51f;
635 s->noise_table = noisetable_exp;
637 else
639 s->noise_mult = 0xa3d;
640 /* LSP values are simply 2x the EXP values */
641 for (i=0;i<NOISE_TAB_SIZE;++i)
642 noisetable_exp[i] = noisetable_exp[i]<< 1;
643 s->noise_table = noisetable_exp;
645 #if 0
647 unsigned int seed;
648 int32_t norm;
649 seed = 1;
650 norm = 0; // PJJ: near as makes any diff to 0!
651 for (i=0;i<NOISE_TAB_SIZE;++i)
653 seed = seed * 314159 + 1;
654 s->noise_table[i] = itofix32((int)seed) * norm;
657 #endif
659 s->hgain_vlc.table = vlcbuf4;
660 s->hgain_vlc.table_allocated = VLCBUF4SIZE;
661 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
662 hgain_huffbits, 1, 1,
663 hgain_huffcodes, 2, 2, 0);
666 if (s->use_exp_vlc)
669 s->exp_vlc.table = vlcbuf3;
670 s->exp_vlc.table_allocated = VLCBUF3SIZE;
672 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
673 scale_huffbits, 1, 1,
674 scale_huffcodes, 4, 4, 0);
676 else
678 wma_lsp_to_curve_init(s, s->frame_len);
681 /* choose the VLC tables for the coefficients */
682 coef_vlc_table = 2;
683 if (s->sample_rate >= 32000)
685 if (bps1 < 0xb852)
686 coef_vlc_table = 0;
687 else if (bps1 < 0x128f6)
688 coef_vlc_table = 1;
691 runtabarray[0] = runtab0; runtabarray[1] = runtab1;
692 levtabarray[0] = levtab0; levtabarray[1] = levtab1;
694 s->coef_vlc[0].table = vlcbuf1;
695 s->coef_vlc[0].table_allocated = VLCBUF1SIZE;
696 s->coef_vlc[1].table = vlcbuf2;
697 s->coef_vlc[1].table_allocated = VLCBUF2SIZE;
699 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
700 &coef_vlcs[coef_vlc_table * 2], 0);
701 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
702 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
704 s->last_superframe_len = 0;
705 s->last_bitoffset = 0;
707 return 0;
711 /* compute x^-0.25 with an exponent and mantissa table. We use linear
712 interpolation to reduce the mantissa table size at a small speed
713 expense (linear interpolation approximately doubles the number of
714 bits of precision). */
715 static inline int32_t pow_m1_4(WMADecodeContext *s, int32_t x)
717 union {
718 float f;
719 unsigned int v;
720 } u, t;
721 unsigned int e, m;
722 int32_t a, b;
724 u.f = fixtof64(x);
725 e = u.v >> 23;
726 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
727 /* build interpolation scale: 1 <= t < 2. */
728 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
729 a = s->lsp_pow_m_table1[m];
730 b = s->lsp_pow_m_table2[m];
732 /* lsp_pow_e_table contains 32.32 format */
733 /* TODO: Since we're unlikely have value that cover the whole
734 * IEEE754 range, we probably don't need to have all possible exponents */
736 return (lsp_pow_e_table[e] * (a + fixmul32(b, ftofix32(t.f))) >>32);
739 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
741 int32_t wdel, a, b, temp, temp2;
742 int i, m;
744 wdel = fixdiv32(M_PI_F, itofix32(frame_len));
745 temp = fixdiv32(itofix32(1), itofix32(frame_len));
746 for (i=0; i<frame_len; ++i)
748 /* TODO: can probably reuse the trig_init values here */
749 fsincos((temp*i)<<15, &temp2);
750 /* get 3 bits headroom + 1 bit from not doubleing the values */
751 s->lsp_cos_table[i] = temp2>>3;
754 /* NOTE: these two tables are needed to avoid two operations in
755 pow_m1_4 */
756 b = itofix32(1);
757 int ix = 0;
759 /*double check this later*/
760 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
762 m = (1 << LSP_POW_BITS) + i;
763 a = pow_a_table[ix++]<<4;
764 s->lsp_pow_m_table1[i] = 2 * a - b;
765 s->lsp_pow_m_table2[i] = b - a;
766 b = a;
771 /* NOTE: We use the same code as Vorbis here */
772 /* XXX: optimize it further with SSE/3Dnow */
773 static void wma_lsp_to_curve(WMADecodeContext *s,
774 int32_t *out,
775 int32_t *val_max_ptr,
776 int n,
777 int32_t *lsp)
779 int i, j;
780 int32_t p, q, w, v, val_max, temp, temp2;
782 val_max = 0;
783 for(i=0;i<n;++i)
785 /* shift by 2 now to reduce rounding error,
786 * we can renormalize right before pow_m1_4
789 p = 0x8000<<5;
790 q = 0x8000<<5;
791 w = s->lsp_cos_table[i];
793 for (j=1;j<NB_LSP_COEFS;j+=2)
795 /* w is 5.27 format, lsp is in 16.16, temp2 becomes 5.27 format */
796 temp2 = ((w - (lsp[j - 1]<<11)));
797 temp = q;
799 /* q is 16.16 format, temp2 is 5.27, q becomes 16.16 */
800 q = fixmul32b(q, temp2 )<<4;
801 p = fixmul32b(p, (w - (lsp[j]<<11)))<<4;
804 /* 2 in 5.27 format is 0x10000000 */
805 p = fixmul32(p, fixmul32b(p, (0x10000000 - w)))<<3;
806 q = fixmul32(q, fixmul32b(q, (0x10000000 + w)))<<3;
808 v = (p + q) >>9; /* p/q end up as 16.16 */
809 v = pow_m1_4(s, v);
810 if (v > val_max)
811 val_max = v;
812 out[i] = v;
815 *val_max_ptr = val_max;
818 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
819 static void decode_exp_lsp(WMADecodeContext *s, int ch)
821 int32_t lsp_coefs[NB_LSP_COEFS];
822 int val, i;
824 for (i = 0; i < NB_LSP_COEFS; ++i)
826 if (i == 0 || i >= 8)
827 val = get_bits(&s->gb, 3);
828 else
829 val = get_bits(&s->gb, 4);
830 lsp_coefs[i] = lsp_codebook[i][val];
833 wma_lsp_to_curve(s,
834 s->exponents[ch],
835 &s->max_exponent[ch],
836 s->block_len,
837 lsp_coefs);
840 /* decode exponents coded with VLC codes */
841 static int decode_exp_vlc(WMADecodeContext *s, int ch)
843 int last_exp, n, code;
844 const uint16_t *ptr, *band_ptr;
845 int32_t v, max_scale;
846 int32_t *q,*q_end;
848 /*accommodate the 60 negative indices */
849 const int32_t *pow_10_to_yover16_ptr = &pow_10_to_yover16[61];
851 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
852 ptr = band_ptr;
853 q = s->exponents[ch];
854 q_end = q + s->block_len;
855 max_scale = 0;
857 if (s->version == 1) /* wmav1 only */
859 last_exp = get_bits(&s->gb, 5) + 10;
860 /* XXX: use a table */
861 v = pow_10_to_yover16_ptr[last_exp];
862 max_scale = v;
863 n = *ptr++;
866 *q++ = v;
868 while (--n);
870 else
871 last_exp = 36;
873 while (q < q_end)
875 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
876 if (code < 0)
878 return -1;
880 /* NOTE: this offset is the same as MPEG4 AAC ! */
881 last_exp += code - 60;
882 /* XXX: use a table */
883 v = pow_10_to_yover16_ptr[last_exp];
884 if (v > max_scale)
886 max_scale = v;
888 n = *ptr++;
891 *q++ = v;
894 while (--n);
897 s->max_exponent[ch] = max_scale;
898 return 0;
901 /* return 0 if OK. return 1 if last block of frame. return -1 if
902 unrecorrable error. */
903 static int wma_decode_block(WMADecodeContext *s)
905 int n, v, a, ch, code, bsize;
906 int coef_nb_bits, total_gain;
907 int nb_coefs[MAX_CHANNELS];
908 int32_t mdct_norm;
910 /* compute current block length */
911 if (s->use_variable_block_len)
913 n = av_log2(s->nb_block_sizes - 1) + 1;
915 if (s->reset_block_lengths)
917 s->reset_block_lengths = 0;
918 v = get_bits(&s->gb, n);
919 if (v >= s->nb_block_sizes)
921 return -2;
923 s->prev_block_len_bits = s->frame_len_bits - v;
924 v = get_bits(&s->gb, n);
925 if (v >= s->nb_block_sizes)
927 return -3;
929 s->block_len_bits = s->frame_len_bits - v;
931 else
933 /* update block lengths */
934 s->prev_block_len_bits = s->block_len_bits;
935 s->block_len_bits = s->next_block_len_bits;
937 v = get_bits(&s->gb, n);
939 if (v >= s->nb_block_sizes)
940 return -4;
942 s->next_block_len_bits = s->frame_len_bits - v;
944 else
946 /* fixed block len */
947 s->next_block_len_bits = s->frame_len_bits;
948 s->prev_block_len_bits = s->frame_len_bits;
949 s->block_len_bits = s->frame_len_bits;
951 /* now check if the block length is coherent with the frame length */
952 s->block_len = 1 << s->block_len_bits;
954 if ((s->block_pos + s->block_len) > s->frame_len)
956 return -5; /* oddly 32k sample from tracker fails here */
959 if (s->nb_channels == 2)
961 s->ms_stereo = get_bits(&s->gb, 1);
963 v = 0;
964 for (ch = 0; ch < s->nb_channels; ++ch)
966 a = get_bits(&s->gb, 1);
967 s->channel_coded[ch] = a;
968 v |= a;
970 /* if no channel coded, no need to go further */
971 /* XXX: fix potential framing problems */
972 if (!v)
974 goto next;
977 bsize = s->frame_len_bits - s->block_len_bits;
979 /* read total gain and extract corresponding number of bits for
980 coef escape coding */
981 total_gain = 1;
982 for(;;)
984 a = get_bits(&s->gb, 7);
985 total_gain += a;
986 if (a != 127)
988 break;
992 if (total_gain < 15)
993 coef_nb_bits = 13;
994 else if (total_gain < 32)
995 coef_nb_bits = 12;
996 else if (total_gain < 40)
997 coef_nb_bits = 11;
998 else if (total_gain < 45)
999 coef_nb_bits = 10;
1000 else
1001 coef_nb_bits = 9;
1003 /* compute number of coefficients */
1004 n = s->coefs_end[bsize] - s->coefs_start;
1006 for(ch = 0; ch < s->nb_channels; ++ch)
1008 nb_coefs[ch] = n;
1010 /* complex coding */
1011 if (s->use_noise_coding)
1014 for(ch = 0; ch < s->nb_channels; ++ch)
1016 if (s->channel_coded[ch])
1018 int i, n, a;
1019 n = s->exponent_high_sizes[bsize];
1020 for(i=0;i<n;++i)
1022 a = get_bits(&s->gb, 1);
1023 s->high_band_coded[ch][i] = a;
1024 /* if noise coding, the coefficients are not transmitted */
1025 if (a)
1026 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
1030 for(ch = 0; ch < s->nb_channels; ++ch)
1032 if (s->channel_coded[ch])
1034 int i, n, val, code;
1036 n = s->exponent_high_sizes[bsize];
1037 val = (int)0x80000000;
1038 for(i=0;i<n;++i)
1040 if (s->high_band_coded[ch][i])
1042 if (val == (int)0x80000000)
1044 val = get_bits(&s->gb, 7) - 19;
1046 else
1048 //code = get_vlc(&s->gb, &s->hgain_vlc);
1049 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
1050 if (code < 0)
1052 return -6;
1054 val += code - 18;
1056 s->high_band_values[ch][i] = val;
1063 /* exponents can be reused in short blocks. */
1064 if ((s->block_len_bits == s->frame_len_bits) || get_bits(&s->gb, 1))
1066 for(ch = 0; ch < s->nb_channels; ++ch)
1068 if (s->channel_coded[ch])
1070 if (s->use_exp_vlc)
1072 if (decode_exp_vlc(s, ch) < 0)
1074 return -7;
1077 else
1079 decode_exp_lsp(s, ch);
1081 s->exponents_bsize[ch] = bsize;
1086 /* parse spectral coefficients : just RLE encoding */
1087 for(ch = 0; ch < s->nb_channels; ++ch)
1089 if (s->channel_coded[ch])
1091 VLC *coef_vlc;
1092 int level, run, sign, tindex;
1093 int16_t *ptr, *eptr;
1094 const uint16_t *level_table, *run_table;
1096 /* special VLC tables are used for ms stereo because
1097 there is potentially less energy there */
1098 tindex = (ch == 1 && s->ms_stereo);
1099 coef_vlc = &s->coef_vlc[tindex];
1100 run_table = s->run_table[tindex];
1101 level_table = s->level_table[tindex];
1102 /* XXX: optimize */
1103 ptr = &s->coefs1[ch][0];
1104 eptr = ptr + nb_coefs[ch];
1105 memset(ptr, 0, s->block_len * sizeof(int16_t));
1107 for(;;)
1109 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
1110 //code = get_vlc(&s->gb, coef_vlc);
1111 if (code < 0)
1113 return -8;
1115 if (code == 1)
1117 /* EOB */
1118 break;
1120 else if (code == 0)
1122 /* escape */
1123 level = get_bits(&s->gb, coef_nb_bits);
1124 /* NOTE: this is rather suboptimal. reading
1125 block_len_bits would be better */
1126 run = get_bits(&s->gb, s->frame_len_bits);
1128 else
1130 /* normal code */
1131 run = run_table[code];
1132 level = level_table[code];
1134 sign = get_bits(&s->gb, 1);
1135 if (!sign)
1136 level = -level;
1137 ptr += run;
1138 if (ptr >= eptr)
1140 break;
1142 *ptr++ = level;
1145 /* NOTE: EOB can be omitted */
1146 if (ptr >= eptr)
1147 break;
1150 if (s->version == 1 && s->nb_channels >= 2)
1152 align_get_bits(&s->gb);
1157 int n4 = s->block_len >> 1;
1159 /* theres no reason to do a divide by two in fixed precision ... */
1160 mdct_norm = 0x10000>>(s->block_len_bits-1);
1162 if (s->version == 1)
1164 mdct_norm *= fixtoi32(fixsqrt32(itofix32(n4))); /* PJJ : exercise this path */
1168 /* finally compute the MDCT coefficients */
1169 for(ch = 0; ch < s->nb_channels; ++ch)
1171 if (s->channel_coded[ch])
1173 int16_t *coefs1;
1174 int32_t *exponents, *exp_ptr;
1175 int32_t *coefs, atemp;
1176 int64_t mult;
1177 int64_t mult1;
1178 int32_t noise, temp1, temp2, mult2;
1179 int i, j, n, n1, last_high_band, esize;
1180 int32_t exp_power[HIGH_BAND_MAX_SIZE];
1182 coefs1 = s->coefs1[ch];
1183 exponents = s->exponents[ch];
1184 esize = s->exponents_bsize[ch];
1185 coefs = (*(s->coefs))[ch];
1187 n=0;
1190 * Previously the IMDCT was run in 17.15 precision to avoid overflow. However rare files could
1191 * overflow here as well, so switch to 17.15 during coefs calculation.
1195 if (s->use_noise_coding)
1197 /*TODO: mult should be converted to 32 bit to speed up noise coding*/
1199 mult = fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch]));
1200 mult = mult* mdct_norm; //what the hell? This is actually int64_t*2^16!
1201 mult1 = mult;
1203 /* very low freqs : noise */
1204 for(i = 0;i < s->coefs_start; ++i)
1206 *coefs++ = fixmul32((fixmul32(s->noise_table[s->noise_index],
1207 (*exponents++))>>4),Fixed32From64(mult1)) >>1;
1208 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1211 n1 = s->exponent_high_sizes[bsize];
1213 /* compute power of high bands */
1214 exp_ptr = exponents +
1215 s->high_band_start[bsize] -
1216 s->coefs_start;
1217 last_high_band = 0; /* avoid warning */
1218 for (j=0;j<n1;++j)
1220 n = s->exponent_high_bands[s->frame_len_bits -
1221 s->block_len_bits][j];
1222 if (s->high_band_coded[ch][j])
1224 int32_t e2, v;
1225 e2 = 0;
1226 for(i = 0;i < n; ++i)
1228 /*v is noramlized later on so its fixed format is irrelevant*/
1229 v = exp_ptr[i]>>4;
1230 e2 += fixmul32(v, v)>>3;
1232 exp_power[j] = e2/n; /*n is an int...*/
1233 last_high_band = j;
1235 exp_ptr += n;
1238 /* main freqs and high freqs */
1239 for(j=-1;j<n1;++j)
1241 if (j < 0)
1243 n = s->high_band_start[bsize] -
1244 s->coefs_start;
1246 else
1248 n = s->exponent_high_bands[s->frame_len_bits -
1249 s->block_len_bits][j];
1251 if (j >= 0 && s->high_band_coded[ch][j])
1253 /* use noise with specified power */
1254 int32_t tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
1255 mult1 = (int64_t)fixsqrt32(tmp);
1256 /* XXX: use a table */
1257 /*mult1 is 48.16, pow_table is 48.16*/
1258 mult1 = mult1 * pow_table[s->high_band_values[ch][j]+20] >> PRECISION;
1260 /*this step has a fairly high degree of error for some reason*/
1261 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
1263 mult1 = mult1*mdct_norm>>PRECISION;
1264 for(i = 0;i < n; ++i)
1266 noise = s->noise_table[s->noise_index];
1267 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1268 *coefs++ = fixmul32((fixmul32(*exponents,noise)>>4),Fixed32From64(mult1)) >>1;
1269 ++exponents;
1272 else
1274 /* coded values + small noise */
1275 for(i = 0;i < n; ++i)
1277 // PJJ: check code path
1278 noise = s->noise_table[s->noise_index];
1279 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1281 /*don't forget to renormalize the noise*/
1282 temp1 = (((int32_t)*coefs1++)<<16) + (noise>>4);
1283 temp2 = fixmul32(*exponents, mult>>17);
1284 *coefs++ = fixmul32(temp1, temp2);
1285 ++exponents;
1290 /* very high freqs : noise */
1291 n = s->block_len - s->coefs_end[bsize];
1292 mult2 = fixmul32(mult>>16,exponents[-1]) ; /*the work around for 32.32 vars are getting stupid*/
1293 for (i = 0; i < n; ++i)
1295 /*renormalize the noise product and then reduce to 17.15 precison*/
1296 *coefs++ = fixmul32(s->noise_table[s->noise_index],mult2) >>5;
1298 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
1301 else
1303 /*Noise coding not used, simply convert from exp to fixed representation*/
1304 int32_t mult3 = (int32_t)(fixdiv64(pow_table[total_gain+20],Fixed32To64(s->max_exponent[ch])));
1305 mult3 = fixmul32(mult3, mdct_norm);
1307 n = nb_coefs[ch];
1309 /* XXX: optimize more, unrolling this loop in asm might be a good idea */
1310 for(i = 0;i < s->coefs_start; i++)
1311 *coefs++ = 0;
1312 for(i = 0;i < n; ++i)
1314 atemp = (coefs1[i] * mult3)>>1;
1315 *coefs++=fixmul32(atemp,exponents[i<<bsize>>esize]);
1317 n = s->block_len - s->coefs_end[bsize];
1318 memset(coefs, 0, n*sizeof(int32_t));
1323 if (s->ms_stereo && s->channel_coded[1])
1325 int32_t a, b;
1326 int i;
1327 int32_t (*coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE] = (s->coefs);
1329 /* nominal case for ms stereo: we do it before mdct */
1330 /* no need to optimize this case because it should almost
1331 never happen */
1332 if (!s->channel_coded[0])
1334 memset((*(s->coefs))[0], 0, sizeof(int32_t) * s->block_len);
1335 s->channel_coded[0] = 1;
1338 for(i = 0; i < s->block_len; ++i)
1340 a = (*coefs)[0][i];
1341 b = (*coefs)[1][i];
1342 (*coefs)[0][i] = a + b;
1343 (*coefs)[1][i] = a - b;
1347 for(ch = 0; ch < s->nb_channels; ++ch)
1349 if (s->channel_coded[ch])
1351 static int32_t output[BLOCK_MAX_SIZE * 2] IBSS_ATTR;
1353 int n4, index, n;
1355 n = s->block_len;
1356 n4 = s->block_len >>1;
1358 ff_imdct_calc(&s->mdct_ctx[bsize],
1359 output,
1360 (*(s->coefs))[ch]);
1362 /* add in the frame */
1363 index = (s->frame_len / 2) + s->block_pos - n4;
1365 wma_window(s, output, &s->frame_out[ch][index]);
1367 /* specific fast case for ms-stereo : add to second
1368 channel if it is not coded */
1369 if (s->ms_stereo && !s->channel_coded[1])
1371 wma_window(s, output, &s->frame_out[1][index]);
1376 next:
1377 /* update block number */
1378 ++s->block_num;
1379 s->block_pos += s->block_len;
1380 if (s->block_pos >= s->frame_len)
1382 return 1;
1384 else
1386 return 0;
1390 /* decode a frame of frame_len samples */
1391 static int wma_decode_frame(WMADecodeContext *s, int32_t *samples)
1393 int ret, i, n, ch, incr;
1394 int32_t *ptr;
1395 int32_t *iptr;
1397 /* read each block */
1398 s->block_num = 0;
1399 s->block_pos = 0;
1401 for(;;)
1403 ret = wma_decode_block(s);
1404 if (ret < 0)
1406 #ifdef WMA_DEBUG
1407 printf("wma_decode_block failed with code %d\n", ret);
1408 #endif
1409 return -1;
1411 if (ret)
1413 break;
1417 /* return frame with full 30-bit precision */
1418 n = s->frame_len;
1419 incr = s->nb_channels;
1420 for(ch = 0; ch < s->nb_channels; ++ch)
1422 ptr = samples + ch;
1423 iptr = s->frame_out[ch];
1425 for (i=0;i<n;++i)
1427 *ptr = (*iptr++);
1428 ptr += incr;
1430 /* prepare for next block */
1431 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
1432 s->frame_len * sizeof(int32_t));
1436 return 0;
1439 /* Initialise the superframe decoding */
1441 int wma_decode_superframe_init(WMADecodeContext* s,
1442 uint8_t *buf, /*input*/
1443 int buf_size)
1445 if (buf_size==0)
1447 s->last_superframe_len = 0;
1448 return 0;
1451 s->current_frame = 0;
1453 init_get_bits(&s->gb, buf, buf_size*8);
1455 if (s->use_bit_reservoir)
1457 /* read super frame header */
1458 get_bits(&s->gb, 4); /* super frame index */
1459 s->nb_frames = get_bits(&s->gb, 4);
1461 if (s->last_superframe_len == 0)
1462 s->nb_frames --;
1463 else if (s->nb_frames == 0)
1464 s->nb_frames++;
1466 s->bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
1467 } else {
1468 s->nb_frames = 1;
1471 return 1;
1475 /* Decode a single frame in the current superframe - return -1 if
1476 there was a decoding error, or the number of samples decoded.
1479 int wma_decode_superframe_frame(WMADecodeContext* s,
1480 int32_t* samples, /*output*/
1481 uint8_t *buf, /*input*/
1482 int buf_size)
1484 int pos, len;
1485 uint8_t *q;
1486 int done = 0;
1487 if ((s->use_bit_reservoir) && (s->current_frame == 0))
1489 if (s->last_superframe_len > 0)
1491 /* add s->bit_offset bits to last frame */
1492 if ((s->last_superframe_len + ((s->bit_offset + 7) >> 3)) >
1493 MAX_CODED_SUPERFRAME_SIZE)
1495 #ifdef WMA_DEBUG
1496 printf("superframe size too large error\n");
1497 #endif
1498 goto fail;
1500 q = s->last_superframe + s->last_superframe_len;
1501 len = s->bit_offset;
1502 while (len > 0)
1504 *q++ = (get_bits)(&s->gb, 8);
1505 len -= 8;
1507 if (len > 0)
1509 *q++ = (get_bits)(&s->gb, len) << (8 - len);
1512 /* XXX: s->bit_offset bits into last frame */
1513 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
1514 /* skip unused bits */
1515 if (s->last_bitoffset > 0)
1516 skip_bits(&s->gb, s->last_bitoffset);
1518 /* this frame is stored in the last superframe and in the
1519 current one */
1520 if (wma_decode_frame(s, samples) < 0)
1522 goto fail;
1524 done = 1;
1527 /* read each frame starting from s->bit_offset */
1528 pos = s->bit_offset + 4 + 4 + s->byte_offset_bits + 3;
1529 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
1530 len = pos & 7;
1531 if (len > 0)
1532 skip_bits(&s->gb, len);
1534 s->reset_block_lengths = 1;
1537 /* If we haven't decoded a frame yet, do it now */
1538 if (!done)
1540 if (wma_decode_frame(s, samples) < 0)
1542 goto fail;
1546 s->current_frame++;
1548 if ((s->use_bit_reservoir) && (s->current_frame == s->nb_frames))
1550 /* we copy the end of the frame in the last frame buffer */
1551 pos = get_bits_count(&s->gb) + ((s->bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
1552 s->last_bitoffset = pos & 7;
1553 pos >>= 3;
1554 len = buf_size - pos;
1555 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1557 #ifdef WMA_DEBUG
1558 printf("superframe size too large error after decodeing\n");
1559 #endif
1560 goto fail;
1562 s->last_superframe_len = len;
1563 memcpy(s->last_superframe, buf + pos, len);
1566 return s->frame_len;
1568 fail:
1569 /* when error, we reset the bit reservoir */
1571 s->last_superframe_len = 0;
1572 return -1;