blob: c064bf0e04326b7b47d47323b842d3a4ff536cf9 [file] [log] [blame]
Dave Chapmanc7282472007-07-03 09:25:36 +00001/*
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 "asf.h"
28#include "wmadec.h"
29
30/* These are for development and debugging and should not be changed unless
31you REALLY know what you are doing ;) */
32//#define IGNORE_OVERFLOW
33#define FAST_FILTERS
34#define PRECISION 16
35#define PRECISION64 16
36
37static fixed64 IntTo64(int x)
38{
39 fixed64 res = 0;
40 unsigned char *p = (unsigned char *)&res;
41
42#ifdef ROCKBOX_BIG_ENDIAN
43 p[5] = x & 0xff;
44 p[4] = (x & 0xff00)>>8;
45 p[3] = (x & 0xff0000)>>16;
46 p[2] = (x & 0xff000000)>>24;
47#else
48 p[2] = x & 0xff;
49 p[3] = (x & 0xff00)>>8;
50 p[4] = (x & 0xff0000)>>16;
51 p[5] = (x & 0xff000000)>>24;
52#endif
53 return res;
54}
55
56static int IntFrom64(fixed64 x)
57{
58 int res = 0;
59 unsigned char *p = (unsigned char *)&x;
60
61#ifdef ROCKBOX_BIG_ENDIAN
62 res = p[5] | (p[4]<<8) | (p[3]<<16) | (p[2]<<24);
63#else
64 res = p[2] | (p[3]<<8) | (p[4]<<16) | (p[5]<<24);
65#endif
66 return res;
67}
68
69static fixed32 Fixed32From64(fixed64 x)
70{
71 return x & 0xFFFFFFFF;
72}
73
74static fixed64 Fixed32To64(fixed32 x)
75{
76 return (fixed64)x;
77}
78#define fixtof64(x) (float)((float)(x) / (float)(1 << PRECISION64)) //does not work on int64_t!
79#define ftofix32(x) ((fixed32)((x) * (float)(1 << PRECISION) + ((x) < 0 ? -0.5 : 0.5)))
80#define itofix64(x) (IntTo64(x))
81#define itofix32(x) ((x) << PRECISION)
82#define fixtoi32(x) ((x) >> PRECISION)
83#define fixtoi64(x) (IntFrom64(x))
84
85#ifdef CPU_ARM
86#define fixmul32(x, y) \
87 ({ int32_t __hi; \
88 uint32_t __lo; \
89 int32_t __result; \
90 asm ("smull %0, %1, %3, %4\n\t" \
91 "movs %0, %0, lsr %5\n\t" \
92 "adc %2, %0, %1, lsl %6" \
93 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
94 : "%r" (x), "r" (y), \
95 "M" (PRECISION), "M" (32 - PRECISION) \
96 : "cc"); \
97 __result; \
98 })
Thom Johansen4aeab552007-07-03 19:36:26 +000099#elif defined(CPU_COLDFIRE)
Jens Arnold5717a0a2007-07-04 00:31:17 +0000100static inline int32_t fixmul32(int32_t x, int32_t y)
101{
102#if PRECISION != 16
103#warning Coldfire fixmul32() only works for PRECISION == 16
104#endif
105 int32_t t1;
106 asm (
107 "mac.l %[x], %[y], %%acc0 \n" /* multiply */
108 "mulu.l %[y], %[x] \n" /* get lower half, avoid emac stall */
109 "movclr.l %%acc0, %[t1] \n" /* get higher half */
110 "lsr.l #1, %[t1] \n"
111 "move.w %[t1], %[x] \n"
112 "swap %[x] \n"
113 : /* outputs */
114 [t1]"=&d"(t1),
115 [x] "+d" (x)
116 : /* inputs */
117 [y] "d" (y)
118 );
119 return x;
120}
Dave Chapmanc7282472007-07-03 09:25:36 +0000121#else
122fixed32 fixmul32(fixed32 x, fixed32 y)
123{
124 fixed64 temp;
125 float lol;
126 // int filehandle = rb->open("/mul.txt", O_WRONLY|O_CREAT|O_APPEND);
127 lol= fixtof64(x) * fixtof64(y);
128
129 temp = x;
130 temp *= y;
131
132 temp >>= PRECISION;
133
134 //rb->fdprintf(filehandle,"%d\n", (fixed32)temp);
135 //rb->close(filehandle);
136
137 return (fixed32)temp;
138}
139
140#endif
141//thanks MAD!
142
143
144//mgg: special fixmul32 that does a 16.16 x 1.31 multiply that returns a 16.16 value.
145//this is needed because the fft constants are all normalized to be less then 1 and can't fit into a 16 bit number
146#ifdef CPU_ARM
147
148# define fixmul32b(x, y) \
149 ({ int32_t __hi; \
150 uint32_t __lo; \
151 int32_t __result; \
152 asm ("smull %0, %1, %3, %4\n\t" \
153 "movs %0, %0, lsr %5\n\t" \
154 "adc %2, %0, %1, lsl %6" \
155 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
156 : "%r" (x), "r" (y), \
157 "M" (31), "M" (1) \
158 : "cc"); \
159 __result; \
160 })
Thom Johansen4aeab552007-07-03 19:36:26 +0000161#elif !defined(CPU_COLDFIRE)
Dave Chapmanc7282472007-07-03 09:25:36 +0000162static fixed32 fixmul32b(fixed32 x, fixed32 y)
163{
164 fixed64 temp;
165
166 temp = x;
167 temp *= y;
168
169 temp >>= 31; //16+31-16 = 31 bits
170
171 return (fixed32)temp;
172}
173
174#endif
175
176
177
178
179static fixed64 fixmul64byfixed(fixed64 x, fixed32 y)
180{
181
182 //return x * y;
183 return (x * y);
184 // return (fixed64) fixmul32(Fixed32From64(x),y);
185}
186
187
188fixed32 fixdiv32(fixed32 x, fixed32 y)
189{
190 fixed64 temp;
191
192 if(x == 0)
193 return 0;
194 if(y == 0)
195 return 0x7fffffff;
196 temp = x;
197 temp <<= PRECISION;
198 return (fixed32)(temp / y);
199}
200
201
202static fixed64 fixdiv64(fixed64 x, fixed64 y)
203{
204 fixed64 temp;
205
206 if(x == 0)
207 return 0;
208 if(y == 0)
209 return 0x07ffffffffffffffLL;
210 temp = x;
211 temp <<= PRECISION64;
212 return (fixed64)(temp / y);
213}
214
215static fixed32 fixsqrt32(fixed32 x)
216{
217
218 unsigned long r = 0, s, v = (unsigned long)x;
219
220#define STEP(k) s = r + (1 << k * 2); r >>= 1; \
221 if (s <= v) { v -= s; r |= (1 << k * 2); }
222
223 STEP(15);
224 STEP(14);
225 STEP(13);
226 STEP(12);
227 STEP(11);
228 STEP(10);
229 STEP(9);
230 STEP(8);
231 STEP(7);
232 STEP(6);
233 STEP(5);
234 STEP(4);
235 STEP(3);
236 STEP(2);
237 STEP(1);
238 STEP(0);
239
240 return (fixed32)(r << (PRECISION / 2));
241}
242
243
244__inline fixed32 fixsin32(fixed32 x)
245{
246
247 fixed64 x2, temp;
248 int sign = 1;
249
250 if(x < 0)
251 {
252 sign = -1;
253 x = -x;
254 }
255 while (x > 0x19220)
256 {
257 x -= M_PI_F;
258 sign = -sign;
259 }
260 if (x > 0x19220)
261 {
262 x = M_PI_F - x;
263 }
264 x2 = (fixed64)x * x;
265 x2 >>= PRECISION;
266 if(sign != 1)
267 {
268 x = -x;
269 }
270 /**
271 temp = ftofix32(-.0000000239f) * x2;
272 temp >>= PRECISION;
273 **/
274 temp = 0; // PJJ
275 //temp = (temp + 0x0) * x2; //MGG: this can't possibly do anything?
276 //temp >>= PRECISION;
277 temp = (temp - 0xd) * x2;
278 temp >>= PRECISION;
279 temp = (temp + 0x222) * x2;
280 temp >>= PRECISION;
281 temp = (temp - 0x2aab) * x2;
282 temp >>= PRECISION;
283 temp += 0x10000;
284 temp = temp * x;
285 temp >>= PRECISION;
286
287 return (fixed32)(temp);
288}
289
290__inline fixed32 fixcos32(fixed32 x)
291{
292 return fixsin32(x - (M_PI_F>>1))*-1;
293}
294
295
296/* Inverse gain of circular cordic rotation in s0.31 format. */
297static const long cordic_circular_gain = 0xb2458939; /* 0.607252929 */
298
299/* Table of values of atan(2^-i) in 0.32 format fractions of pi where pi = 0xffffffff / 2 */
300static const unsigned long atan_table[] = {
301 0x1fffffff, /* +0.785398163 (or pi/4) */
302 0x12e4051d, /* +0.463647609 */
303 0x09fb385b, /* +0.244978663 */
304 0x051111d4, /* +0.124354995 */
305 0x028b0d43, /* +0.062418810 */
306 0x0145d7e1, /* +0.031239833 */
307 0x00a2f61e, /* +0.015623729 */
308 0x00517c55, /* +0.007812341 */
309 0x0028be53, /* +0.003906230 */
310 0x00145f2e, /* +0.001953123 */
311 0x000a2f98, /* +0.000976562 */
312 0x000517cc, /* +0.000488281 */
313 0x00028be6, /* +0.000244141 */
314 0x000145f3, /* +0.000122070 */
315 0x0000a2f9, /* +0.000061035 */
316 0x0000517c, /* +0.000030518 */
317 0x000028be, /* +0.000015259 */
318 0x0000145f, /* +0.000007629 */
319 0x00000a2f, /* +0.000003815 */
320 0x00000517, /* +0.000001907 */
321 0x0000028b, /* +0.000000954 */
322 0x00000145, /* +0.000000477 */
323 0x000000a2, /* +0.000000238 */
324 0x00000051, /* +0.000000119 */
325 0x00000028, /* +0.000000060 */
326 0x00000014, /* +0.000000030 */
327 0x0000000a, /* +0.000000015 */
328 0x00000005, /* +0.000000007 */
329 0x00000002, /* +0.000000004 */
330 0x00000001, /* +0.000000002 */
331 0x00000000, /* +0.000000001 */
332 0x00000000, /* +0.000000000 */
333};
334
335/**
336 * Implements sin and cos using CORDIC rotation.
337 *
338 * @param phase has range from 0 to 0xffffffff, representing 0 and
339 * 2*pi respectively.
340 * @param cos return address for cos
341 * @return sin of phase, value is a signed value from LONG_MIN to LONG_MAX,
342 * representing -1 and 1 respectively.
343 *
344 * Gives at least 24 bits precision (last 2-8 bits or so are probably off)
345 */
346static long fsincos(unsigned long phase, fixed32 *cos)
347{
348 int32_t x, x1, y, y1;
349 unsigned long z, z1;
350 int i;
351
352 /* Setup initial vector */
353 x = cordic_circular_gain;
354 y = 0;
355 z = phase;
356
357 /* The phase has to be somewhere between 0..pi for this to work right */
358 if (z < 0xffffffff / 4) {
359 /* z in first quadrant, z += pi/2 to correct */
360 x = -x;
361 z += 0xffffffff / 4;
362 } else if (z < 3 * (0xffffffff / 4)) {
363 /* z in third quadrant, z -= pi/2 to correct */
364 z -= 0xffffffff / 4;
365 } else {
366 /* z in fourth quadrant, z -= 3pi/2 to correct */
367 x = -x;
368 z -= 3 * (0xffffffff / 4);
369 }
370
371 /* Each iteration adds roughly 1-bit of extra precision */
372 for (i = 0; i < 31; i++) {
373 x1 = x >> i;
374 y1 = y >> i;
375 z1 = atan_table[i];
376
377 /* Decided which direction to rotate vector. Pivot point is pi/2 */
378 if (z >= 0xffffffff / 4) {
379 x -= y1;
380 y += x1;
381 z -= z1;
382 } else {
383 x += y1;
384 y -= x1;
385 z += z1;
386 }
387 }
388
389 if (cos)
390 *cos = x;
391
392 return y;
393}
394
395
396
397/*
398__inline fixed32 fixasin32(fixed32 x)
399{
400 fixed64 temp;
401 int sign = 1;
402
403 if(x > 0x10000 || x < 0xffff0000)
404 {
405 return 0;
406 }
407 if(x < 0)
408 {
409 sign = -1;
410 x = -x;
411 }
412 temp = 0xffffffad * (fixed64)x;
413 temp >>= PRECISION;
414 temp = (temp + 0x1b5) * x;
415 temp >>= PRECISION;
416 temp = (temp - 0x460) * x;
417 temp >>= PRECISION;
418 temp = (temp + 0x7e9) * x;
419 temp >>= PRECISION;
420 temp = (temp - 0xcd8) * x;
421 temp >>= PRECISION;
422 temp = (temp + 0x16c7) * x;
423 temp >>= PRECISION;
424 temp = (temp - 0x36f0) * x;
425 temp >>= PRECISION;
426 temp = (temp + 0x19220) * fixsqrt32(0x10000 - x);
427 temp >>= PRECISION;
428
429 return sign * ((M_PI_F>>1) - (fixed32)temp);
430}
431*/
432
433#define ALT_BITSTREAM_READER
434
435#define unaligned32(a) (*(uint32_t*)(a))
436
437uint16_t bswap_16(uint16_t x)
438{
439 uint16_t hi = x & 0xff00;
440 uint16_t lo = x & 0x00ff;
441 return (hi >> 8) | (lo << 8);
442}
443
444uint32_t bswap_32(uint32_t x)
445{
446 uint32_t b1 = x & 0xff000000;
447 uint32_t b2 = x & 0x00ff0000;
448 uint32_t b3 = x & 0x0000ff00;
449 uint32_t b4 = x & 0x000000ff;
450 return (b1 >> 24) | (b2 >> 8) | (b3 << 8) | (b4 << 24);
451}
452
Thom Johansen4aeab552007-07-03 19:36:26 +0000453#ifdef CPU_COLDFIRE
454static inline
455void CMUL(fixed32 *x, fixed32 *y,
456 fixed32 a, fixed32 b,
457 fixed32 t, fixed32 v)
458{
459 asm volatile ("mac.l %[a], %[t], %%acc0;"
460 "msac.l %[b], %[v], %%acc0;"
461 "mac.l %[b], %[t], %%acc1;"
462 "mac.l %[a], %[v], %%acc1;"
463 "movclr.l %%acc0, %[a];"
464 "move.l %[a], (%[x]);"
465 "movclr.l %%acc1, %[a];"
466 "move.l %[a], (%[y]);"
467 : [a] "+&r" (a)
468 : [x] "a" (x), [y] "a" (y),
469 [b] "r" (b), [t] "r" (t), [v] "r" (v)
470 : "cc", "memory");
471}
472#else
Dave Chapmanc7282472007-07-03 09:25:36 +0000473// PJJ : reinstate macro
474void CMUL(fixed32 *pre,
475 fixed32 *pim,
476 fixed32 are,
477 fixed32 aim,
478 fixed32 bre,
479 fixed32 bim)
480{
481 //int64_t x,y;
482 fixed32 _aref = are;
483 fixed32 _aimf = aim;
484 fixed32 _bref = bre;
485 fixed32 _bimf = bim;
486 fixed32 _r1 = fixmul32b(_bref, _aref);
487 fixed32 _r2 = fixmul32b(_bimf, _aimf);
488 fixed32 _r3 = fixmul32b(_bref, _aimf);
489 fixed32 _r4 = fixmul32b(_bimf, _aref);
490 *pre = _r1 - _r2;
491 *pim = _r3 + _r4;
492
493}
Thom Johansen4aeab552007-07-03 19:36:26 +0000494#endif
Dave Chapmanc7282472007-07-03 09:25:36 +0000495
496typedef struct CoefVLCTable
497{
498 int n; /* total number of codes */
499 const uint32_t *huffcodes; /* VLC bit values */
500 const uint8_t *huffbits; /* VLC bit size */
501 const uint16_t *levels; /* table to build run/level tables */
502}
503CoefVLCTable;
504
505static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
506int fft_calc(FFTContext *s, FFTComplex *z);
507
508
509//static variables that replace malloced stuff
510fixed32 stat0[2048], stat1[1024], stat2[512], stat3[256], stat4[128]; //these are the MDCT reconstruction windows
511
512fixed32 *tcosarray[5], *tsinarray[5];
513fixed32 tcos0[1024], tcos1[512], tcos2[256], tcos3[128], tcos4[64]; //these are the sin and cos rotations used by the MDCT
514fixed32 tsin0[1024], tsin1[512], tsin2[256], tsin3[128], tsin4[64];
515
516FFTComplex *exparray[5]; //these are the fft lookup tables
517uint16_t *revarray[5];
518FFTComplex exptab0[512] IBSS_ATTR;//, exptab1[256], exptab2[128], exptab3[64], exptab4[32]; //folded these in!
519uint16_t revtab0[1024], revtab1[512], revtab2[256], revtab3[128], revtab4[64];
520
521uint16_t *runtabarray[2], *levtabarray[2]; //these are VLC lookup tables
522
523uint16_t runtab0[1336], runtab1[1336], levtab0[1336], levtab1[1336]; //these could be made smaller since only one can be 1336
524
525
526//may also be too large by ~ 1KB each?
527static VLC_TYPE vlcbuf1[6144][2];
528static VLC_TYPE vlcbuf2[3584][2];
529static VLC_TYPE vlcbuf3[1536][2] IBSS_ATTR; //small so lets try iram
530
531//fixed32 window[BLOCK_MAX_SIZE * 2];
532
533const fixed64 pow_table[] =
534 {
535 0x10000LL,0x11f3dLL,0x14249LL,0x1699cLL,0x195bcLL,0x1c73dLL,0x1fec9LL,0x23d1dLL,0x2830bLL,0x2d182LL,
536 0x3298bLL,0x38c53LL,0x3fb28LL,0x47783LL,0x5030aLL,0x59f98LL,0x64f40LL,0x71457LL,0x7f17bLL,0x8e99aLL,
537 0xa0000LL,0xb385eLL,0xc96d9LL,0xe2019LL,0xfd954LL,0x11c865LL,0x13f3dfLL,0x166320LL,0x191e6eLL,0x1c2f10LL,
538 0x1f9f6eLL,0x237b39LL,0x27cf8bLL,0x2cab1aLL,0x321e65LL,0x383bf0LL,0x3f1882LL,0x46cb6aLL,0x4f6eceLL,0x592006LL,
539 0x640000LL,0x7033acLL,0x7de47eLL,0x8d40f6LL,0x9e7d44LL,0xb1d3f4LL,0xc786b7LL,0xdfdf43LL,0xfb304bLL,0x119d69aLL,
540 0x13c3a4eLL,0x162d03aLL,0x18e1b70LL,0x1beaf00LL,0x1f52feeLL,0x2325760LL,0x276f514LL,0x2c3f220LL,0x31a5408LL,
541 0x37b403cLL,0x3e80000LL,0x46204b8LL,0x4eaece8LL,0x58489a0LL,0x630e4a8LL,0x6f24788LL,0x7cb4328LL,0x8beb8a0LL,
542 0x9cfe2f0LL,0xb026200LL,0xc5a4710LL,0xddc2240LL,0xf8d1260LL,0x1172d600LL,0x1393df60LL,0x15f769c0LL,0x18a592c0LL,
543 0x1ba77540LL,0x1f074840LL,0x22d08280LL,0x27100000LL,0x2bd42f40LL,0x312d4100LL,0x372d6000LL,0x3de8ee80LL,
544 0x4576cb80LL,0x4df09f80LL,0x57733600LL,0x621edd80LL,0x6e17d480LL,0x7b86c700LL,0x8a995700LL,0x9b82b800LL,
545 0xae7c5c00LL,0xc3c6b900LL,0xdbaa2200LL,0xf677bc00LL,0x1148a9400LL,0x13648d200LL,0x15c251800LL,0x186a00000LL,
546 0x1b649d800LL,0x1ebc48a00LL,0x227c5c000LL,0x26b195000LL,0x2b6a3f000LL,0x30b663c00LL,0x36a801c00LL,0x3d534a400LL,
547 0x44cee4800LL,0x4d343c800LL,0x569fd6000LL,0x6131b2800LL,0x6d0db9800LL,0x7a5c33800LL,0x894a55000LL,0x9a0ad6000LL,
548 0xacd69d000LL,0xc1ed84000LL,0xd9972f000LL,0xf42400000LL,0x111ee28000LL,0x1335ad6000LL,0x158db98000LL,0x182efd4000LL,
549 0x1b22676000LL,0x1e71fe6000LL,0x2229014000LL,0x26540e8000LL,0x2b014f0000LL,0x3040a5c000LL,0x3623e60000LL,0x3cbf0fc000LL,
550 0x4428940000LL,0x4c79a08000LL,0x55ce758000LL,0x6046c58000LL,0x6c06220000LL,0x7934728000LL,0x87fe7d0000LL,0x9896800000LL,
551 0xab34d90000LL,0xc018c60000LL,0xd7893f0000LL,0xf1d5e40000LL,0x10f580a0000LL,0x13073f00000LL,0x1559a0c0000LL,0x17f48900000LL,
552 0x1ae0d160000LL,0x1e286780000LL,0x21d66fc0000LL,0x25f769c0000LL,0x2a995c80000LL,0x2fcc0440000LL,0x35a10940000LL,
553 0x3c2c3b80000LL,0x4383d500000LL,0x4bc0c780000LL,0x54ff0e80000LL,0x5f5e1000000LL,0x6b010780000LL,0x780f7c00000LL,
554 0x86b5c800000LL,0x9725ae00000LL,0xa9970600000LL,0xbe487500000LL,0xd5804700000LL,0xef8d5a00000LL,0x10cc82e00000LL,
555 0x12d940c00000LL,0x152605c00000LL,0x17baa2200000LL,0x1a9fd9c00000LL,0x1ddf82a00000LL,0x2184a5c00000LL,0x259ba5400000LL,
556 0x2a3265400000LL,0x2f587cc00000LL,0x351f69000000LL,0x3b9aca000000LL,0x42e0a4800000LL,0x4b09ad800000LL,
557 0x54319d000000LL,0x5e778d000000LL,0x69fe64000000LL,0x76ed49800000LL,0x85702c000000LL,0x95b858000000LL,
558 0xa7fd1c000000LL,0xbc7c87000000LL,0xd37c3a000000LL,0xed4a55000000LL,0x10a3e82000000LL,0x12abb1a000000LL,
559 0x14f2e7a000000LL,0x1781474000000LL,0x1a5f7f4000000LL,0x1d974de000000LL,0x2133a18000000LL
560 };
561
562const fixed32 pow_10_to_yover16[] ICONST_ATTR=
563{
5640x10000,0x127a0,0x15562,0x18a39,0x1c73d,0x20db4,0x25f12,0x2bd09,0x3298b,0x3a6d9,0x4378b,0x4dea3,
5650x59f98,0x67e6b,0x77fbb,0x8a8de,0xa0000,0xb8c3e,0xd55d1,0xf6636,0x11c865,0x148906,0x17b6b8,0x1b625b,
5660x1f9f6e,0x248475,0x2a2b6e,0x30b25f,0x383bf0,0x40f02c,0x4afd4b,0x5698b0,0x640000,0x737a6b,0x855a26,
5670x99fe1f,0xb1d3f4,0xcd5a3e,0xed232b,0x111d78a,0x13c3a4e,0x16d2c94,0x1a5b24e,0x1e6f7b0,0x2325760,
5680x28961b4,0x2ede4ec,0x361f6dc,0x3e80000,0x482c830,0x5358580,0x603ed30,0x6f24788,0x8058670,0x9435fb0,
5690xab26b70,0xc5a4710,0xe43bdc0,0x1078f700,0x1305ace0,0x15f769c0,0x195dd100,0x1d4af120
570};
571
572const fixed32 pow_a_table[] =
573{
5740x1004,0x1008,0x100c,0x1010,0x1014,0x1018,0x101c,0x1021,0x1025,0x1029,0x102d,0x1031,0x1036,0x103a,
5750x103e,0x1043,0x1047,0x104b,0x1050,0x1054,0x1059,0x105d,0x1062,0x1066,0x106b,0x106f,0x1074,0x1078,
5760x107d,0x1082,0x1086,0x108b,0x1090,0x1095,0x1099,0x109e,0x10a3,0x10a8,0x10ad,0x10b2,0x10b7,0x10bc,
5770x10c1,0x10c6,0x10cb,0x10d0,0x10d5,0x10da,0x10df,0x10e5,0x10ea,0x10ef,0x10f5,0x10fa,0x10ff,0x1105,
5780x110a,0x1110,0x1115,0x111b,0x1120,0x1126,0x112c,0x1131,0x1137,0x113d,0x1143,0x1149,0x114f,0x1155,
5790x115a,0x1161,0x1167,0x116d,0x1173,0x1179,0x117f,0x1186,0x118c,0x1192,0x1199,0x119f,0x11a6,0x11ac,
5800x11b3,0x11b9,0x11c0,0x11c7,0x11ce,0x11d4,0x11db,0x11e2,0x11e9,0x11f0,0x11f8,0x11ff,0x1206,0x120d,
5810x1215,0x121c,0x1223,0x122b,0x1233,0x123a,0x1242,0x124a,0x1251,0x1259,0x1261,0x1269,0x1271,0x127a,
5820x1282,0x128a,0x1293,0x129b,0x12a4,0x12ac,0x12b5,0x12be,0x12c7,0x12d0,0x12d9,0x12e2,0x12eb,0x12f4,
5830x12fe,0x1307
584};
585
586const fixed64 lsp_pow_e_table[] =
587{
588 0xf333f9deLL, 0xf0518db9LL, 0x0LL, 0x7e656b4fLL, 0x7999fcefLL, 0xf828c6dcLL, 0x0LL,
589 0x3f32b5a7LL, 0x3cccfe78LL, 0xfc14636eLL, 0x0LL, 0x9f995ad4LL, 0x9e667f3cLL, 0xfe0a31b7LL,
590 0x0LL, 0x4fccad6aLL, 0x4f333f9eLL, 0x7f0518dcLL, 0x0LL, 0x27e656b5LL, 0x27999fcfLL,
591 0xbf828c6eLL, 0x0LL, 0x13f32b5aLL, 0x13cccfe7LL, 0xdfc14637LL, 0x0LL, 0x89f995adLL,
592 0x9e667f4LL, 0x6fe0a31bLL, 0x0LL, 0x44fccad7LL, 0x4f333faLL, 0x37f0518eLL, 0x0LL,
593 0xa27e656bLL, 0x827999fdLL, 0x1bf828c7LL, 0x0LL, 0xd13f32b6LL, 0x413cccfeLL, 0xdfc1463LL,
594 0x0LL, 0xe89f995bLL, 0xa09e667fLL, 0x6fe0a32LL, 0x0LL, 0x744fccadLL, 0x504f3340LL,
595 0x837f0519LL, 0x0LL, 0xba27e657LL, 0xa82799a0LL, 0xc1bf828cLL, 0x0LL, 0x5d13f32bLL,
596 0xd413ccd0LL, 0x60dfc146LL, 0x0LL, 0xae89f996LL, 0x6a09e668LL, 0x306fe0a3LL, 0x0LL,
597 0xd744fccbLL, 0xb504f334LL, 0x9837f052LL, 0x80000000LL, 0x6ba27e65LL, 0x5a82799aLL,
598 0x4c1bf829LL, 0x40000000LL, 0x35d13f33LL, 0x2d413ccdLL, 0x260dfc14LL, 0x20000000LL,
599 0x1ae89f99LL, 0x16a09e66LL, 0x1306fe0aLL, 0x10000000LL, 0xd744fcdLL, 0xb504f33LL,
600 0x9837f05LL, 0x8000000LL, 0x6ba27e6LL, 0x5a8279aLL, 0x4c1bf83LL, 0x4000000LL,
601 0x35d13f3LL, 0x2d413cdLL, 0x260dfc1LL, 0x2000000LL, 0x1ae89faLL, 0x16a09e6LL,
602 0x1306fe1LL, 0x1000000LL, 0xd744fdLL, 0xb504f3LL, 0x9837f0LL, 0x800000LL,
603 0x6ba27eLL, 0x5a827aLL, 0x4c1bf8LL, 0x400000LL, 0x35d13fLL, 0x2d413dLL,
604 0x260dfcLL, 0x200000LL, 0x1ae8a0LL, 0x16a09eLL, 0x1306feLL, 0x100000LL,
605 0xd7450LL, 0xb504fLL, 0x9837fLL, 0x80000LL, 0x6ba28LL, 0x5a828LL, 0x4c1c0LL,
606 0x40000LL, 0x35d14LL, 0x2d414LL, 0x260e0LL, 0x20000LL, 0x1ae8aLL, 0x16a0aLL,
607 0x13070LL, 0x10000LL, 0xd745LL, 0xb505LL, 0x9838LL, 0x8000LL, 0x6ba2LL,
608 0x5a82LL, 0x4c1cLL, 0x4000LL, 0x35d1LL, 0x2d41LL, 0x260eLL, 0x2000LL,
609 0x1ae9LL, 0x16a1LL, 0x1307LL, 0x1000LL, 0xd74LL, 0xb50LL, 0x983LL, 0x800LL,
610 0x6baLL, 0x5a8LL, 0x4c2LL, 0x400LL, 0x35dLL, 0x2d4LL, 0x261LL, 0x200LL, 0x1afLL,
611 0x16aLL, 0x130LL, 0x100LL, 0xd7LL, 0xb5LL, 0x98LL, 0x80LL, 0x6cLL, 0x5bLL,
612 0x4cLL, 0x40LL, 0x36LL, 0x2dLL, 0x26LL, 0x20LL, 0x1bLL, 0x17LL, 0x13LL,
613 0x10LL, 0xdLL, 0xbLL, 0xaLL, 0x8LL, 0x7LL, 0x6LL, 0x5LL, 0x4LL, 0x3LL,
614 0x3LL, 0x2LL, 0x2LL, 0x2LL, 0x1LL, 0x1LL, 0x1LL, 0x1LL, 0x1LL, 0x1LL,
615 0x1LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL,
616 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL,
617 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL,
618 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL,
619 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL,
620 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL, 0x0LL,
621 0x0LL, 0x0LL
622};
623
624#include "wmadata.h" // PJJ
625
626/**
627 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
628 * done
629 */
630int fft_inits(FFTContext *s, int nbits, int inverse)
631{
632 int i, j, m, n;
633 fixed32 alpha, c1, s1, ct, st;
634 int s2;
635
636 s->nbits = nbits;
637 n = 1 << nbits;
638
639 //s->exptab = exparray[10-nbits]; //not needed
640
641 //s->exptab = av_malloc((n >> 1) * sizeof(FFTComplex));
642 //if (!s->exptab)
643 // goto fail;
644 s->revtab = revarray[10-nbits];
645 //s->revtab = av_malloc(n * sizeof(uint16_t));
646 //if (!s->revtab)
647 // goto fail;
648 s->inverse = inverse;
649
650 s2 = inverse ? 1 : -1;
651
652 if(nbits == 10){ //we folded all these stupid tables into the nbits==10 table, so don't make it for the others!
653 //should probably just remove exptab building out of this function and do it higher up for neatness
654 for(i=0;i<(n/2);++i)
655 {
656 //we're going to redo this in CORDIC fixed format! Hold onto your butts
657
658 /*
659 input to cordic is from 0 ->2pi with 0->0 and 2^32-1 ->2pi
660 output, which is what we'll store the variables as is
661 -1->-2^31 and 1->2^31-1
662
663 */
664
665 fixed32 ifix = itofix32(i);
666 fixed32 nfix = itofix32(n);
667 fixed32 res = fixdiv32(ifix,nfix); //this is really bad here since nfix can be as large as 1024 !
668 //also, make this a shift, since its a fucking power of two divide
669 alpha = fixmul32(TWO_M_PI_F, res);
670 ct = fixcos32(alpha); //need to correct alpha for 0->2pi scale
671 st = fixsin32(alpha);// * s2;
672
673 s1 = fsincos(res<<16, &c1); //does sin and cos in one pass!
674
675 //I really have my doubts about the correctness of the alpha to cordic mapping here, but it seems to work well enough
676 //double check this later!
677
678 exptab0[i].re = c1;
679 exptab0[i].im = s1*s2;
680 }
681 }
682 // s->fft_calc = fft_calc;
683 s->exptab1 = NULL;
684
685
686 /* compute bit reverse table */
687
688 for(i=0;i<n;i++)
689 {
690 m=0;
691 for(j=0;j<nbits;j++)
692 {
693 m |= ((i >> j) & 1) << (nbits-j-1);
694
695 }
696
697 s->revtab[i]=m;
698 }
699 return 0;
700//fail:
701 // av_freep(&s->revtab);
702 // av_freep(&s->exptab);
703 // av_freep(&s->exptab1);
704 return -1;
705}
706
707/* butter fly op */
708#define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
709{\
710 fixed32 ax, ay, bx, by;\
711 bx=pre1;\
712 by=pim1;\
713 ax=qre1;\
714 ay=qim1;\
715 pre = (bx + ax);\
716 pim = (by + ay);\
717 qre = (bx - ax);\
718 qim = (by - ay);\
719}
720
721//this goddamn butterfly could overflow and i'd neve rknow it...
722//holy shit it was the fucking butterfly oh god this is the worst thing ever
723
724
725
726int fft_calc_unscaled(FFTContext *s, FFTComplex *z)
727{
728 int ln = s->nbits;
729 int j, np, np2;
730 int nblocks, nloops;
731 register FFTComplex *p, *q;
732 // FFTComplex *exptab = s->exptab;
733 int l;
734 fixed32 tmp_re, tmp_im;
735 int tabshift = 10-ln;
736
737 np = 1 << ln;
738
739
740 /* pass 0 */
741
742 p=&z[0];
743 j=(np >> 1);
744 do
745 {
746 BF(p[0].re, p[0].im, p[1].re, p[1].im,
747 p[0].re, p[0].im, p[1].re, p[1].im);
748 p+=2;
749 }
750 while (--j != 0);
751
752 /* pass 1 */
753
754
755 p=&z[0];
756 j=np >> 2;
757 if (s->inverse)
758 {
759 do
760 {
761 BF(p[0].re, p[0].im, p[2].re, p[2].im,
762 p[0].re, p[0].im, p[2].re, p[2].im);
763 BF(p[1].re, p[1].im, p[3].re, p[3].im,
764 p[1].re, p[1].im, -p[3].im, p[3].re);
765 p+=4;
766 }
767 while (--j != 0);
768 }
769 else
770 {
771 do
772 {
773 BF(p[0].re, p[0].im, p[2].re, p[2].im,
774 p[0].re, p[0].im, p[2].re, p[2].im);
775 BF(p[1].re, p[1].im, p[3].re, p[3].im,
776 p[1].re, p[1].im, p[3].im, -p[3].re);
777 p+=4;
778 }
779 while (--j != 0);
780 }
781 /* pass 2 .. ln-1 */
782
783 nblocks = np >> 3;
784 nloops = 1 << 2;
785 np2 = np >> 1;
786 do
787 {
788 p = z;
789 q = z + nloops;
790 for (j = 0; j < nblocks; ++j)
791 {
792 BF(p->re, p->im, q->re, q->im,
793 p->re, p->im, q->re, q->im);
794
795 p++;
796 q++;
797 for(l = nblocks; l < np2; l += nblocks)
798 {
799 CMUL(&tmp_re, &tmp_im, exptab0[(l<<tabshift)].re, exptab0[(l<<tabshift)].im, q->re, q->im);
800 //CMUL(&tmp_re, &tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
801 BF(p->re, p->im, q->re, q->im,
802 p->re, p->im, tmp_re, tmp_im);
803 p++;
804 q++;
805 }
806
807 p += nloops;
808 q += nloops;
809 }
810 nblocks = nblocks >> 1;
811 nloops = nloops << 1;
812 }
813 while (nblocks != 0);
814 return 0;
815}
816
817/*
818//needless since we're statically allocated
819void fft_end(FFTContext *s)
820{
821 // av_freep(&s->revtab);
822 // av_freep(&s->exptab);
823 // av_freep(&s->exptab1);
824}
825*/
826/* VLC decoding */
827
828#define GET_VLC(code, name, gb, table, bits, max_depth)\
829{\
830 int n, index, nb_bits;\
831\
832 index= SHOW_UBITS(name, gb, bits);\
833 code = table[index][0];\
834 n = table[index][1];\
835\
836 if(max_depth > 1 && n < 0){\
837 LAST_SKIP_BITS(name, gb, bits)\
838 UPDATE_CACHE(name, gb)\
839\
840 nb_bits = -n;\
841\
842 index= SHOW_UBITS(name, gb, nb_bits) + code;\
843 code = table[index][0];\
844 n = table[index][1];\
845 if(max_depth > 2 && n < 0){\
846 LAST_SKIP_BITS(name, gb, nb_bits)\
847 UPDATE_CACHE(name, gb)\
848\
849 nb_bits = -n;\
850\
851 index= SHOW_UBITS(name, gb, nb_bits) + code;\
852 code = table[index][0];\
853 n = table[index][1];\
854 }\
855 }\
856 SKIP_BITS(name, gb, n)\
857}
858
859
860//#define DEBUG_VLC
861
862#define GET_DATA(v, table, i, wrap, size) \
863{\
864 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
865 switch(size) {\
866 case 1:\
867 v = *(const uint8_t *)ptr;\
868 break;\
869 case 2:\
870 v = *(const uint16_t *)ptr;\
871 break;\
872 default:\
873 v = *(const uint32_t *)ptr;\
874 break;\
875 }\
876}
877
878// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
879static inline int get_vlc(GetBitContext *s, VLC *vlc)
880{
881 int code;
882 VLC_TYPE (*table)[2]= vlc->table;
883
884 OPEN_READER(re, s)
885 UPDATE_CACHE(re, s)
886
887 GET_VLC(code, re, s, table, vlc->bits, 3)
888
889 CLOSE_READER(re, s)
890 return code;
891}
892
893static int alloc_table(VLC *vlc, int size)
894{
895 int index;
896 index = vlc->table_size;
897 vlc->table_size += size;
898 if (vlc->table_size > vlc->table_allocated)
899 {
900 // rb->splash(HZ*10, "OH CRAP, TRIED TO REALLOC A STATIC VLC TABLE!");
901 vlc->table_allocated += (1 << vlc->bits);
902 // vlc->table = av_realloc(vlc->table,
903 // sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
904 if (!vlc->table)
905 return -1;
906 }
907 return index;
908}
909
910static int build_table(VLC *vlc, int table_nb_bits,
911 int nb_codes,
912 const void *bits, int bits_wrap, int bits_size,
913 const void *codes, int codes_wrap, int codes_size,
914 uint32_t code_prefix, int n_prefix)
915{
916 int i, j, k, n, table_size, table_index, nb, n1, index;
917 uint32_t code;
918 VLC_TYPE (*table)[2];
919
920 table_size = 1 << table_nb_bits;
921 table_index = alloc_table(vlc, table_size);
922 if (table_index < 0)
923 return -1;
924 table = &vlc->table[table_index];
925
926 for(i=0;i<table_size;i++)
927 {
928 table[i][1] = 0; //bits
929 table[i][0] = -1; //codes
930 }
931
932 /* first pass: map codes and compute auxillary table sizes */
933 for(i=0;i<nb_codes;i++)
934 {
935 GET_DATA(n, bits, i, bits_wrap, bits_size);
936 GET_DATA(code, codes, i, codes_wrap, codes_size);
937 /* we accept tables with holes */
938 if (n <= 0)
939 continue;
940 /* if code matches the prefix, it is in the table */
941 n -= n_prefix;
942 if (n > 0 && (code >> n) == code_prefix)
943 {
944 if (n <= table_nb_bits)
945 {
946 /* no need to add another table */
947 j = (code << (table_nb_bits - n)) & (table_size - 1);
948 nb = 1 << (table_nb_bits - n);
949 for(k=0;k<nb;k++)
950 {
951 if (table[j][1] /*bits*/ != 0)
952 {
953 // PJJ exit(-1);
954 }
955 table[j][1] = n; //bits
956 table[j][0] = i; //code
957 j++;
958 }
959 }
960 else
961 {
962 n -= table_nb_bits;
963 j = (code >> n) & ((1 << table_nb_bits) - 1);
964 /* compute table size */
965 n1 = -table[j][1]; //bits
966 if (n > n1)
967 n1 = n;
968 table[j][1] = -n1; //bits
969 }
970 }
971 }
972
973 /* second pass : fill auxillary tables recursively */
974 for(i=0;i<table_size;i++)
975 {
976 n = table[i][1]; //bits
977 if (n < 0)
978 {
979 n = -n;
980 if (n > table_nb_bits)
981 {
982 n = table_nb_bits;
983 table[i][1] = -n; //bits
984 }
985 index = build_table(vlc, n, nb_codes,
986 bits, bits_wrap, bits_size,
987 codes, codes_wrap, codes_size,
988 (code_prefix << table_nb_bits) | i,
989 n_prefix + table_nb_bits);
990 if (index < 0)
991 return -1;
992 /* note: realloc has been done, so reload tables */
993 table = &vlc->table[table_index];
994 table[i][0] = index; //code
995 }
996 }
997 return table_index;
998}
999
1000/* Build VLC decoding tables suitable for use with get_vlc().
1001
1002 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
1003 bigger it is, the faster is the decoding. But it should not be too
1004 big to save memory and L1 cache. '9' is a good compromise.
1005
1006 'nb_codes' : number of vlcs codes
1007
1008 'bits' : table which gives the size (in bits) of each vlc code.
1009
1010 'codes' : table which gives the bit pattern of of each vlc code.
1011
1012 'xxx_wrap' : give the number of bytes between each entry of the
1013 'bits' or 'codes' tables.
1014
1015 'xxx_size' : gives the number of bytes of each entry of the 'bits'
1016 or 'codes' tables.
1017
1018 'wrap' and 'size' allows to use any memory configuration and types
1019 (byte/word/long) to store the 'bits' and 'codes' tables.
1020*/
1021int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
1022 const void *bits, int bits_wrap, int bits_size,
1023 const void *codes, int codes_wrap, int codes_size)
1024{
1025 vlc->bits = nb_bits;
1026 // vlc->table = NULL;
1027 // vlc->table_allocated = 0;
1028 vlc->table_size = 0;
1029
1030 if (build_table(vlc, nb_bits, nb_codes,
1031 bits, bits_wrap, bits_size,
1032 codes, codes_wrap, codes_size,
1033 0, 0) < 0)
1034 {
1035 // av_free(vlc->table);
1036 return -1;
1037 }
1038 //dump_table("Tab 1",vlc->table[0],vlc->table_size);
1039 //dump_table("Tab 2",vlc->table[1],vlc->table_size);
1040 return 0;
1041}
1042
1043/**
1044 * init MDCT or IMDCT computation.
1045 */
1046int ff_mdct_init(MDCTContext *s, int nbits, int inverse)
1047{
1048 int n, n4, i;
1049 // fixed32 alpha;
1050
1051
1052 memset(s, 0, sizeof(*s));
1053 n = 1 << nbits; //nbits ranges from 12 to 8 inclusive
1054 s->nbits = nbits;
1055 s->n = n;
1056 n4 = n >> 2;
1057 s->tcos = tcosarray[12-nbits];
1058 s->tsin = tsinarray[12-nbits];
1059 //s->tcos = av_malloc(n4 * sizeof(fixed32)); //this allocates between 1024 and 64 elements
1060 //if (!s->tcos)
1061 // goto fail;
1062 //s->tsin = av_malloc(n4 * sizeof(fixed32));
1063 //if (!s->tsin)
1064 // goto fail;
1065//
1066 for(i=0;i<n4;i++)
1067 {
1068 //fixed32 pi2 = fixmul32(0x20000, M_PI_F);
1069 fixed32 ip = itofix32(i) + 0x2000;
1070 ip = ip >> nbits;
1071 //ip = fixdiv32(ip,itofix32(n)); // PJJ optimize
1072 //alpha = fixmul32(TWO_M_PI_F, ip);
1073 //s->tcos[i] = -fixcos32(alpha); //alpha between 0 and pi/2
1074 //s->tsin[i] = -fixsin32(alpha);
1075
1076 s->tsin[i] = - fsincos(ip<<16, &(s->tcos[i])); //I can't remember why this works, but it seems to agree for ~24 bits, maybe more!
1077 s->tcos[i] *=-1;
1078 }
1079 if (fft_inits(&s->fft, s->nbits - 2, inverse) < 0)
1080 goto fail;
1081 return 0;
1082fail:
1083// av_freep(&s->tcos);
1084// av_freep(&s->tsin);
1085 return -1;
1086}
1087
1088/**
1089 * Compute inverse MDCT of size N = 2^nbits
1090 * @param output N samples
1091 * @param input N/2 samples
1092 * @param tmp N/2 samples
1093 */
1094void ff_imdct_calc(MDCTContext *s,
1095 fixed32 *output,
1096 const fixed32 *input,
1097 FFTComplex *tmp)
1098{
1099 int k, n8, n4, n2, n, j,scale;
1100 const uint16_t *revtab = s->fft.revtab;
1101 const fixed32 *tcos = s->tcos;
1102 const fixed32 *tsin = s->tsin;
1103 const fixed32 *in1, *in2;
1104 FFTComplex *z = (FFTComplex *)tmp;
1105
1106 n = 1 << s->nbits;
1107
1108 n2 = n >> 1;
1109 n4 = n >> 2;
1110 n8 = n >> 3;
1111
1112
1113 /* pre rotation */
1114 in1 = input;
1115 in2 = input + n2 - 1;
1116
1117 for(k = 0; k < n4; k++)
1118 {
1119 j=revtab[k];
1120 CMUL(&z[j].re, &z[j].im, *in2, *in1, tcos[k], tsin[k]);
1121 in1 += 2;
1122 in2 -= 2;
1123 }
1124
1125 for(k = 0; k < n4; k++){
1126 z[k].re >>=1;
1127 z[k].im >>=1;
1128 }
1129
1130 //rb->splash(HZ, "in MDCT calc");
1131 scale = fft_calc_unscaled(&s->fft, z);
1132 // scale = fft_calc(&s->fft, z);
1133
1134 //rb->splash(HZ, "in MDCT calc2");
1135
1136 /* post rotation + reordering */
1137
1138 for(k = 0; k < n4; k++)
1139 {
1140 CMUL(&z[k].re, &z[k].im, (z[k].re), (z[k].im), tcos[k], tsin[k]);
1141 }
1142
1143 for(k = 0; k < n8; k++)
1144 {
1145 fixed32 r1,r2,r3,r4,r1n,r2n,r3n;
1146
1147 r1 = z[n8 + k].im;
1148 r1n = r1 * -1;
1149 r2 = z[n8-1-k].re;
1150 r2n = r2 * -1;
1151 r3 = z[k+n8].re;
1152 r3n = r3 * -1;
1153 r4 = z[n8-k-1].im;
1154
1155 output[2*k] = r1n;
1156 output[n2-1-2*k] = r1;
1157
1158 output[2*k+1] = r2;
1159 output[n2-1-2*k-1] = r2n;
1160
1161 output[n2 + 2*k]= r3n;
1162 output[n-1- 2*k]= r3n;
1163
1164 output[n2 + 2*k+1]= r4;
1165 output[n-2 - 2 * k] = r4;
1166 }
1167
1168
1169
1170
1171}
1172
1173void ff_mdct_end(MDCTContext *s)
1174{
1175 (void)s;
1176
1177 // av_freep(&s->tcos);
1178 // av_freep(&s->tsin);
1179 // fft_end(&s->fft);
1180}
1181
1182
1183
1184
1185/* XXX: use same run/length optimization as mpeg decoders */
1186static void init_coef_vlc(VLC *vlc,
1187 uint16_t **prun_table, uint16_t **plevel_table,
1188 const CoefVLCTable *vlc_table, int tab)
1189{
1190 int n = vlc_table->n;
1191 const uint8_t *table_bits = vlc_table->huffbits;
1192 const uint32_t *table_codes = vlc_table->huffcodes;
1193 const uint16_t *levels_table = vlc_table->levels;
1194 uint16_t *run_table, *level_table;
1195 const uint16_t *p;
1196 int i, l, j, level;
1197
1198
1199 init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
1200
1201 run_table = runtabarray[tab];
1202 //run_table = av_malloc(n * sizeof(uint16_t)); //max n should be 1336
1203
1204 level_table= levtabarray[tab];
1205 //level_table = av_malloc(n * sizeof(uint16_t));
1206 p = levels_table;
1207 i = 2;
1208 level = 1;
1209 while (i < n)
1210 {
1211 l = *p++;
1212 for(j=0;j<l;++j)
1213 {
1214 run_table[i] = j;
1215 level_table[i] = level;
1216 ++i;
1217 }
1218 ++level;
1219 }
1220 *prun_table = run_table;
1221 *plevel_table = level_table;
1222}
1223
1224int wma_decode_init(WMADecodeContext* s, asf_waveformatex_t *wfx)
1225{
1226 //WMADecodeContext *s = avctx->priv_data;
1227 int i, flags1, flags2;
1228 fixed32 *window;
1229 uint8_t *extradata;
1230 fixed64 bps1;
1231 fixed32 high_freq;
1232 fixed64 bps;
1233 int sample_rate1;
1234 int coef_vlc_table;
1235 // int filehandle;
Thom Johansen4aeab552007-07-03 19:36:26 +00001236 #ifdef CPU_COLDFIRE
1237 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1238 #endif
Dave Chapmanc7282472007-07-03 09:25:36 +00001239
1240 s->sample_rate = wfx->rate;
1241 s->nb_channels = wfx->channels;
1242 s->bit_rate = wfx->bitrate;
1243 s->block_align = wfx->blockalign;
1244
1245 if (wfx->codec_id == ASF_CODEC_ID_WMAV1){
1246 s->version = 1;
1247 }else{
1248 s->version = 2;
1249 }
1250
1251 /* extract flag infos */
1252 flags1 = 0;
1253 flags2 = 0;
1254 extradata = wfx->data;
1255 if (s->version == 1 && wfx->datalen >= 4) {
1256 flags1 = extradata[0] | (extradata[1] << 8);
1257 flags2 = extradata[2] | (extradata[3] << 8);
1258 }else if (s->version == 2 && wfx->datalen >= 6){
1259 flags1 = extradata[0] | (extradata[1] << 8) |
1260 (extradata[2] << 16) | (extradata[3] << 24);
1261 flags2 = extradata[4] | (extradata[5] << 8);
1262 }
1263 s->use_exp_vlc = flags2 & 0x0001;
1264 s->use_bit_reservoir = flags2 & 0x0002;
1265 s->use_variable_block_len = flags2 & 0x0004;
1266
1267 /* compute MDCT block size */
1268 if (s->sample_rate <= 16000){
1269 s->frame_len_bits = 9;
1270 }else if (s->sample_rate <= 22050 ||
1271 (s->sample_rate <= 32000 && s->version == 1)){
1272 s->frame_len_bits = 10;
1273 }else{
1274 s->frame_len_bits = 11;
1275 }
1276 s->frame_len = 1 << s->frame_len_bits;
1277 if (s-> use_variable_block_len)
1278 {
1279 int nb_max, nb;
1280 nb = ((flags2 >> 3) & 3) + 1;
1281 if ((s->bit_rate / s->nb_channels) >= 32000)
1282 {
1283 nb += 2;
1284 }
1285 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; //max is 11-7
1286 if (nb > nb_max)
1287 nb = nb_max;
1288 s->nb_block_sizes = nb + 1;
1289 }
1290 else
1291 {
1292 s->nb_block_sizes = 1;
1293 }
1294
1295 /* init rate dependant parameters */
1296 s->use_noise_coding = 1;
1297 high_freq = fixmul64byfixed(itofix64(s->sample_rate), 0x8000);
1298
1299
1300 /* if version 2, then the rates are normalized */
1301 sample_rate1 = s->sample_rate;
1302 if (s->version == 2)
1303 {
1304 if (sample_rate1 >= 44100)
1305 sample_rate1 = 44100;
1306 else if (sample_rate1 >= 22050)
1307 sample_rate1 = 22050;
1308 else if (sample_rate1 >= 16000)
1309 sample_rate1 = 16000;
1310 else if (sample_rate1 >= 11025)
1311 sample_rate1 = 11025;
1312 else if (sample_rate1 >= 8000)
1313 sample_rate1 = 8000;
1314 }
1315
1316 fixed64 tmp = itofix64(s->bit_rate);
1317 fixed64 tmp2 = itofix64(s->nb_channels * s->sample_rate);
1318 bps = fixdiv64(tmp, tmp2);
1319 fixed64 tim = fixmul64byfixed(bps, s->frame_len);
1320 fixed64 tmpi = fixdiv64(tim,itofix64(8));
1321 s->byte_offset_bits = av_log2(fixtoi64(tmpi)) + 2;
1322
1323 /* compute high frequency value and choose if noise coding should
1324 be activated */
1325 bps1 = bps;
1326 if (s->nb_channels == 2)
1327 bps1 = fixmul32(bps,0x1999a);
1328 if (sample_rate1 == 44100)
1329 {
1330 if (bps1 >= 0x9c29)
1331 s->use_noise_coding = 0;
1332 else
1333 high_freq = fixmul64byfixed(high_freq,0x6666);
1334 }
1335 else if (sample_rate1 == 22050)
1336 {
1337 if (bps1 >= 0x128f6)
1338 s->use_noise_coding = 0;
1339 else if (bps1 >= 0xb852)
1340 high_freq = fixmul64byfixed(high_freq,0xb333);
1341 else
1342 high_freq = fixmul64byfixed(high_freq,0x999a);
1343 }
1344 else if (sample_rate1 == 16000)
1345 {
1346 if (bps > 0x8000)
1347 high_freq = fixmul64byfixed(high_freq,0x8000);
1348 else
1349 high_freq = fixmul64byfixed(high_freq,0x4ccd);
1350 }
1351 else if (sample_rate1 == 11025)
1352 {
1353 high_freq = fixmul64byfixed(high_freq,0xb3333);
1354 }
1355 else if (sample_rate1 == 8000)
1356 {
1357 if (bps <= 0xa000)
1358 {
1359 high_freq = fixmul64byfixed(high_freq,0x8000);
1360 }
1361 else if (bps > 0xc000)
1362 {
1363 s->use_noise_coding = 0;
1364 }
1365 else
1366 {
1367 high_freq = fixmul64byfixed(high_freq,0xa666);
1368 }
1369 }
1370 else
1371 {
1372 if (bps >= 0xcccd)
1373 {
1374 high_freq = fixmul64byfixed(high_freq,0xc000);
1375 }
1376 else if (bps >= 0x999a)
1377 {
1378 high_freq = fixmul64byfixed(high_freq,0x999a);
1379 }
1380 else
1381 {
1382 high_freq = fixmul64byfixed(high_freq,0x8000);
1383 }
1384 }
1385
1386 /* compute the scale factor band sizes for each MDCT block size */
1387 {
1388 int a, b, pos, lpos, k, block_len, i, j, n;
1389 const uint8_t *table;
1390
1391 if (s->version == 1)
1392 {
1393 s->coefs_start = 3;
1394 }
1395 else
1396 {
1397 s->coefs_start = 0;
1398 }
1399 for(k = 0; k < s->nb_block_sizes; ++k)
1400 {
1401 block_len = s->frame_len >> k;
1402
1403 if (s->version == 1)
1404 {
1405 lpos = 0;
1406 for(i=0;i<25;++i)
1407 {
1408 a = wma_critical_freqs[i];
1409 b = s->sample_rate;
1410 pos = ((block_len * 2 * a) + (b >> 1)) / b;
1411 if (pos > block_len)
1412 pos = block_len;
1413 s->exponent_bands[0][i] = pos - lpos;
1414 if (pos >= block_len)
1415 {
1416 ++i;
1417 break;
1418 }
1419 lpos = pos;
1420 }
1421 s->exponent_sizes[0] = i;
1422 }
1423 else
1424 {
1425 /* hardcoded tables */
1426 table = NULL;
1427 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
1428 if (a < 3)
1429 {
1430 if (s->sample_rate >= 44100)
1431 table = exponent_band_44100[a];
1432 else if (s->sample_rate >= 32000)
1433 table = exponent_band_32000[a];
1434 else if (s->sample_rate >= 22050)
1435 table = exponent_band_22050[a];
1436 }
1437 if (table)
1438 {
1439 n = *table++;
1440 for(i=0;i<n;++i)
1441 s->exponent_bands[k][i] = table[i];
1442 s->exponent_sizes[k] = n;
1443 }
1444 else
1445 {
1446 j = 0;
1447 lpos = 0;
1448 for(i=0;i<25;++i)
1449 {
1450 a = wma_critical_freqs[i];
1451 b = s->sample_rate;
1452 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
1453 pos <<= 2;
1454 if (pos > block_len)
1455 pos = block_len;
1456 if (pos > lpos)
1457 s->exponent_bands[k][j++] = pos - lpos;
1458 if (pos >= block_len)
1459 break;
1460 lpos = pos;
1461 }
1462 s->exponent_sizes[k] = j;
1463 }
1464 }
1465
1466 /* max number of coefs */
1467 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
1468 /* high freq computation */
1469 fixed64 tmp = itofix64(block_len<<2);
1470 tmp = fixmul64byfixed(tmp,high_freq);
1471 fixed64 tmp2 = itofix64(s->sample_rate);
1472 tmp2 += 0x8000;
1473 s->high_band_start[k] = fixtoi64(fixdiv64(tmp,tmp2));
1474
1475 /*
1476 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
1477 s->sample_rate + 0.5);*/
1478
1479 n = s->exponent_sizes[k];
1480 j = 0;
1481 pos = 0;
1482 for(i=0;i<n;++i)
1483 {
1484 int start, end;
1485 start = pos;
1486 pos += s->exponent_bands[k][i];
1487 end = pos;
1488 if (start < s->high_band_start[k])
1489 start = s->high_band_start[k];
1490 if (end > s->coefs_end[k])
1491 end = s->coefs_end[k];
1492 if (end > start)
1493 s->exponent_high_bands[k][j++] = end - start;
1494 }
1495 s->exponent_high_sizes[k] = j;
1496 }
1497 }
1498
1499 /* init MDCT */
1500 tcosarray[0] = tcos0; tcosarray[1] = tcos1; tcosarray[2] = tcos2; tcosarray[3] = tcos3;tcosarray[4] = tcos4;
1501 tsinarray[0] = tsin0; tsinarray[1] = tsin1; tsinarray[2] = tsin2; tsinarray[3] = tsin3;tsinarray[4] = tsin4;
1502
1503 exparray[0] = exptab0; //exparray[1] = exptab1; exparray[2] = exptab2; exparray[3] = exptab3; exparray[4] = exptab4;
1504 revarray[0]=revtab0; revarray[1]=revtab1; revarray[2]=revtab2; revarray[3]=revtab3; revarray[4]=revtab4;
1505
1506 for(i = 0; i < s->nb_block_sizes; ++i)
1507 {
1508 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
1509 }
1510
1511 /*ffmpeg uses malloc to only allocate as many window sizes as needed. However, we're really only interested in the worst case memory usage.
1512 * In the worst case you can have 5 window sizes, 128 doubling up 2048
1513 * Smaller windows are handled differently.
1514 * Since we don't have malloc, just statically allocate this
1515 */
1516 fixed32 *temp[5];
1517 temp[0] = stat0;
1518 temp[1] = stat1;
1519 temp[2] = stat2;
1520 temp[3] = stat3;
1521 temp[4] = stat4;
1522
1523 /* init MDCT windows : simple sinus window */
1524 for(i = 0; i < s->nb_block_sizes; i++)
1525 {
1526 int n, j;
1527 fixed32 alpha;
1528 n = 1 << (s->frame_len_bits - i);
1529 //window = av_malloc(sizeof(fixed32) * n);
1530 window = temp[i];
1531
1532 //fixed32 n2 = itofix32(n<<1); //2x the window length
1533 //alpha = fixdiv32(M_PI_F, n2); //PI / (2x Window length) == PI<<(s->frame_len_bits - i+1)
1534 //printf("two values of alpha %16.10lf %16.10lf\n", fixtof64(alpha), fixtof64(M_PI_F>>(s->frame_len_bits - i+1)));
1535 alpha = M_PI_F>>(s->frame_len_bits - i+1);
1536 for(j=0;j<n;++j)
1537 {
1538 fixed32 j2 = itofix32(j) + 0x8000;
1539 window[n - j - 1] = fixsin32(fixmul32(j2,alpha)); //alpha between 0 and pi/2
1540
1541 }
1542 //printf("created window\n");
1543 s->windows[i] = window;
1544 //printf("assigned window\n");
1545 }
1546
1547 s->reset_block_lengths = 1;
1548
1549 if (s->use_noise_coding)
1550 {
1551 /* init the noise generator */
1552 if (s->use_exp_vlc)
1553 {
1554 s->noise_mult = 0x51f;
1555 }
1556 else
1557 {
1558 s->noise_mult = 0xa3d;
1559 }
1560
1561
1562 {
1563 unsigned int seed;
1564 fixed32 norm;
1565 seed = 1;
1566 norm = 0; // PJJ: near as makes any diff to 0!
1567 for (i=0;i<NOISE_TAB_SIZE;++i)
1568 {
1569 seed = seed * 314159 + 1;
1570 s->noise_table[i] = itofix32((int)seed) * norm;
1571 }
1572 }
1573
1574 init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits),
1575 hgain_huffbits, 1, 1,
1576 hgain_huffcodes, 2, 2);
1577 }
1578
1579 if (s->use_exp_vlc)
1580 {
1581 s->exp_vlc.table = vlcbuf3;
1582 s->exp_vlc.table_allocated = 1536;
1583 init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits),
1584 scale_huffbits, 1, 1,
1585 scale_huffcodes, 4, 4);
1586 }
1587 else
1588 {
1589 wma_lsp_to_curve_init(s, s->frame_len);
1590 }
1591
1592 /* choose the VLC tables for the coefficients */
1593 coef_vlc_table = 2;
1594 if (s->sample_rate >= 32000)
1595 {
1596 if (bps1 < 0xb852)
1597 coef_vlc_table = 0;
1598 else if (bps1 < 0x128f6)
1599 coef_vlc_table = 1;
1600 }
1601
1602 runtabarray[0] = runtab0; runtabarray[1] = runtab1;
1603 levtabarray[0] = levtab0; levtabarray[1] = levtab1;
1604
1605 s->coef_vlc[0].table = vlcbuf1;
1606 s->coef_vlc[0].table_allocated = 24576/4;
1607 s->coef_vlc[1].table = vlcbuf2;
1608 s->coef_vlc[1].table_allocated = 14336/4;
1609
1610 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
1611 &coef_vlcs[coef_vlc_table * 2], 0);
1612 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
1613 &coef_vlcs[coef_vlc_table * 2 + 1], 1);
1614
1615 //filehandle = rb->open("/log.txt", O_WRONLY|O_CREAT|O_APPEND);
1616
1617
1618
1619 //rb->fdprintf(filehandle,"In init:\n\nsample rate %d\nbit_rate %d\n version %d\n", s->sample_rate, s->bit_rate, s->version );
1620 //rb->fdprintf(filehandle,"use_noise_coding %d \nframe_len %d\nframe_len_bits %d\n", s->use_noise_coding, s->frame_len, s->frame_len_bits);
1621 //rb->fdprintf(filehandle,"use_bit_reservoir %d\n use_variable_block_len %d\n use_exp_vlc %d\n",s->use_bit_reservoir, s->use_variable_block_len, s->use_exp_vlc);
1622 //rb->fdprintf(filehandle,"use_noise_coding %d\n byte_offset_bits %d\n use_exp_vlc %d\n",s->use_noise_coding, s->byte_offset_bits, s->use_exp_vlc);
1623 //rb->close(filehandle);
1624
1625
1626
1627 return 0;
1628}
1629
1630/* interpolate values for a bigger or smaller block. The block must
1631 have multiple sizes */
1632static void interpolate_array(fixed32 *scale, int old_size, int new_size)
1633{
1634 int i, j, jincr, k;
1635 fixed32 v;
1636
1637
1638
1639 if (new_size > old_size)
1640 {
1641 jincr = new_size / old_size;
1642 j = new_size;
1643 for(i = old_size - 1; i >=0; --i)
1644 {
1645 v = scale[i];
1646 k = jincr;
1647 do
1648 {
1649 scale[--j] = v;
1650 }
1651 while (--k);
1652 }
1653 }
1654 else if (new_size < old_size)
1655 {
1656 j = 0;
1657 jincr = old_size / new_size;
1658 for(i = 0; i < new_size; ++i)
1659 {
1660 scale[i] = scale[j];
1661 j += jincr;
1662 }
1663 }
1664}
1665
1666/* compute x^-0.25 with an exponent and mantissa table. We use linear
1667 interpolation to reduce the mantissa table size at a small speed
1668 expense (linear interpolation approximately doubles the number of
1669 bits of precision). */
1670static inline fixed32 pow_m1_4(WMADecodeContext *s, fixed32 x)
1671{
1672 union {
1673 fixed64 f;
1674 unsigned int v;
1675 } u, t;
1676 unsigned int e, m;
1677 fixed64 a, b;
1678
1679 u.f = x;
1680 e = u.v >> 23;
1681 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
1682 /* build interpolation scale: 1 <= t < 2. */
1683 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
1684 a = s->lsp_pow_m_table1[m];
1685 b = s->lsp_pow_m_table2[m];
1686 return lsp_pow_e_table[e] * (a + b * t.f);
1687}
1688
1689static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
1690{
1691 fixed32 wdel, a, b;
1692 int i, m;
1693
1694 wdel = fixdiv32(M_PI_F, itofix32(frame_len));
1695 for (i=0; i<frame_len; ++i)
1696 {
1697 s->lsp_cos_table[i] = 0x20000 * fixcos32(wdel * i); //wdel*i between 0 and pi
1698
1699 }
1700
1701
1702 /* NOTE: these two tables are needed to avoid two operations in
1703 pow_m1_4 */
1704 b = itofix32(1);
1705 int ix = 0;
1706 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--)
1707 {
1708 m = (1 << LSP_POW_BITS) + i;
1709 a = m * (0x8000 / (1 << LSP_POW_BITS)); //PJJ
1710 a = pow_a_table[ix++]; // PJJ : further refinement
1711 s->lsp_pow_m_table1[i] = 2 * a - b;
1712 s->lsp_pow_m_table2[i] = b - a;
1713 b = a;
1714 }
1715}
1716
1717/* NOTE: We use the same code as Vorbis here */
1718/* XXX: optimize it further with SSE/3Dnow */
1719static void wma_lsp_to_curve(WMADecodeContext *s,
1720 fixed32 *out,
1721 fixed32 *val_max_ptr,
1722 int n,
1723 fixed32 *lsp)
1724{
1725 int i, j;
1726 fixed32 p, q, w, v, val_max;
1727
1728 val_max = 0;
1729 for(i=0;i<n;++i)
1730 {
1731 p = 0x8000;
1732 q = 0x8000;
1733 w = s->lsp_cos_table[i];
1734 for (j=1;j<NB_LSP_COEFS;j+=2)
1735 {
1736 q *= w - lsp[j - 1];
1737 p *= w - lsp[j];
1738 }
1739 p *= p * (0x20000 - w);
1740 q *= q * (0x20000 + w);
1741 v = p + q;
1742 v = pow_m1_4(s, v); // PJJ
1743 if (v > val_max)
1744 val_max = v;
1745 out[i] = v;
1746 }
1747 *val_max_ptr = val_max;
1748}
1749
1750/* decode exponents coded with LSP coefficients (same idea as Vorbis) */
1751static void decode_exp_lsp(WMADecodeContext *s, int ch)
1752{
1753 fixed32 lsp_coefs[NB_LSP_COEFS];
1754 int val, i;
1755
1756 for (i = 0; i < NB_LSP_COEFS; ++i)
1757 {
1758 if (i == 0 || i >= 8)
1759 val = get_bits(&s->gb, 3);
1760 else
1761 val = get_bits(&s->gb, 4);
1762 lsp_coefs[i] = lsp_codebook[i][val];
1763 }
1764
1765 wma_lsp_to_curve(s,
1766 s->exponents[ch],
1767 &s->max_exponent[ch],
1768 s->block_len,
1769 lsp_coefs);
1770}
1771
1772/* decode exponents coded with VLC codes */
1773static int decode_exp_vlc(WMADecodeContext *s, int ch)
1774{
1775 int last_exp, n, code;
1776 const uint16_t *ptr, *band_ptr;
1777 fixed32 v, max_scale;
1778 fixed32 *q,*q_end;
1779
1780 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
1781 ptr = band_ptr;
1782 q = s->exponents[ch];
1783 q_end = q + s->block_len;
1784 max_scale = 0;
1785
1786
1787 if (s->version == 1) //wmav1 only
1788 {
1789 last_exp = get_bits(&s->gb, 5) + 10;
1790 /* XXX: use a table */
1791 v = pow_10_to_yover16[last_exp];
1792 max_scale = v;
1793 n = *ptr++;
1794 do
1795 {
1796 *q++ = v;
1797 }
1798 while (--n);
1799 }
1800 last_exp = 36;
1801
1802 while (q < q_end)
1803 {
1804 code = get_vlc(&s->gb, &s->exp_vlc);
1805 if (code < 0)
1806 {
1807 return -1;
1808 }
1809 /* NOTE: this offset is the same as MPEG4 AAC ! */
1810 last_exp += code - 60;
1811 /* XXX: use a table */
1812 v = pow_10_to_yover16[last_exp];
1813 if (v > max_scale)
1814 {
1815 max_scale = v;
1816 }
1817 n = *ptr++;
1818 do
1819 {
1820 *q++ = v;
1821
1822 }
1823 while (--n);
1824 }
1825
1826 s->max_exponent[ch] = max_scale;
1827 return 0;
1828}
1829
1830/* return 0 if OK. return 1 if last block of frame. return -1 if
1831 unrecorrable error. */
1832static int wma_decode_block(WMADecodeContext *s)
1833{
1834 int n, v, a, ch, code, bsize;
1835 int coef_nb_bits, total_gain, parse_exponents;
1836 static fixed32 window[BLOCK_MAX_SIZE * 2]; //crap can't do this locally on the device! its big as the whole stack
1837 int nb_coefs[MAX_CHANNELS];
1838 fixed32 mdct_norm;
1839//int filehandle = rb->open("/mul.txt", O_WRONLY|O_CREAT|O_APPEND);
1840// rb->fdprintf(filehandle,"\nIn wma_decode_block:\n use_variable_block_len %d\n nb_block_sizes %d\n reset_block_lengths %d\n", s->use_variable_block_len, s->nb_block_sizes, s->reset_block_lengths );
1841
1842// printf("***decode_block: %d:%d (%d)\n", s->frame_count - 1, s->block_num, s->block_len);
1843 /* compute current block length */
1844 if (s->use_variable_block_len)
1845 {
1846 n = av_log2(s->nb_block_sizes - 1) + 1;
1847
1848 if (s->reset_block_lengths)
1849 {
1850 s->reset_block_lengths = 0;
1851 v = get_bits(&s->gb, n);
1852 if (v >= s->nb_block_sizes)
1853 {
1854 return -2;
1855 }
1856 s->prev_block_len_bits = s->frame_len_bits - v;
1857 v = get_bits(&s->gb, n);
1858 if (v >= s->nb_block_sizes)
1859 {
1860 return -3;
1861 }
1862 s->block_len_bits = s->frame_len_bits - v;
1863 }
1864 else
1865 {
1866 /* update block lengths */
1867 s->prev_block_len_bits = s->block_len_bits;
1868 s->block_len_bits = s->next_block_len_bits;
1869 }
1870 v = get_bits(&s->gb, n);
1871
1872 //rb->fdprintf(filehandle,"v %d \n prev_block_len_bits %d\n block_len_bits %d\n", v, s->prev_block_len_bits, s->block_len_bits);
1873 //rb->close(filehandle);
1874
1875 LOGF("v was %d", v);
1876 if (v >= s->nb_block_sizes)
1877 {
1878 // rb->splash(HZ*4, "v was %d", v); //5, 7
1879 return -4; //this is it
1880 }
1881 else{
1882 //rb->splash(HZ, "passed v block (%d)!", v);
1883 }
1884 s->next_block_len_bits = s->frame_len_bits - v;
1885 }
1886 else
1887 {
1888 /* fixed block len */
1889 s->next_block_len_bits = s->frame_len_bits;
1890 s->prev_block_len_bits = s->frame_len_bits;
1891 s->block_len_bits = s->frame_len_bits;
1892 }
1893 /* now check if the block length is coherent with the frame length */
1894 s->block_len = 1 << s->block_len_bits;
1895
1896 if ((s->block_pos + s->block_len) > s->frame_len)
1897 {
1898 return -5;
1899 }
1900
1901 if (s->nb_channels == 2)
1902 {
1903 s->ms_stereo = get_bits(&s->gb, 1);
1904 }
1905 v = 0;
1906 for (ch = 0; ch < s->nb_channels; ++ch)
1907 {
1908 a = get_bits(&s->gb, 1);
1909 s->channel_coded[ch] = a;
1910 v |= a;
1911 }
1912 /* if no channel coded, no need to go further */
1913 /* XXX: fix potential framing problems */
1914 if (!v)
1915 {
1916 goto next;
1917 }
1918
1919 bsize = s->frame_len_bits - s->block_len_bits;
1920
1921 /* read total gain and extract corresponding number of bits for
1922 coef escape coding */
1923 total_gain = 1;
1924 for(;;)
1925 {
1926 a = get_bits(&s->gb, 7);
1927 total_gain += a;
1928 if (a != 127)
1929 {
1930 break;
1931 }
1932 }
1933
1934 if (total_gain < 15)
1935 coef_nb_bits = 13;
1936 else if (total_gain < 32)
1937 coef_nb_bits = 12;
1938 else if (total_gain < 40)
1939 coef_nb_bits = 11;
1940 else if (total_gain < 45)
1941 coef_nb_bits = 10;
1942 else
1943 coef_nb_bits = 9;
1944 /* compute number of coefficients */
1945 n = s->coefs_end[bsize] - s->coefs_start;
1946
1947 for(ch = 0; ch < s->nb_channels; ++ch)
1948 {
1949 nb_coefs[ch] = n;
1950 }
1951 /* complex coding */
1952
1953 if (s->use_noise_coding)
1954 {
1955
1956 for(ch = 0; ch < s->nb_channels; ++ch)
1957 {
1958 if (s->channel_coded[ch])
1959 {
1960 int i, n, a;
1961 n = s->exponent_high_sizes[bsize];
1962 for(i=0;i<n;++i)
1963 {
1964 a = get_bits(&s->gb, 1);
1965 s->high_band_coded[ch][i] = a;
1966 /* if noise coding, the coefficients are not transmitted */
1967 if (a)
1968 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
1969 }
1970 }
1971 }
1972 for(ch = 0; ch < s->nb_channels; ++ch)
1973 {
1974 if (s->channel_coded[ch])
1975 {
1976 int i, n, val, code;
1977
1978 n = s->exponent_high_sizes[bsize];
1979 val = (int)0x80000000;
1980 for(i=0;i<n;++i)
1981 {
1982 if (s->high_band_coded[ch][i])
1983 {
1984 if (val == (int)0x80000000)
1985 {
1986 val = get_bits(&s->gb, 7) - 19;
1987 }
1988 else
1989 {
1990 code = get_vlc(&s->gb, &s->hgain_vlc);
1991 if (code < 0)
1992 {
1993 return -6;
1994 }
1995 val += code - 18;
1996 }
1997 s->high_band_values[ch][i] = val;
1998 }
1999 }
2000 }
2001 }
2002 }
2003
2004 /* exposant can be interpolated in short blocks. */
2005 parse_exponents = 1;
2006 if (s->block_len_bits != s->frame_len_bits)
2007 {
2008 parse_exponents = get_bits(&s->gb, 1);
2009 }
2010
2011 if (parse_exponents)
2012 {
2013
2014 for(ch = 0; ch < s->nb_channels; ++ch)
2015 {
2016 if (s->channel_coded[ch])
2017 {
2018 if (s->use_exp_vlc)
2019 {
2020 if (decode_exp_vlc(s, ch) < 0)
2021 {
2022 return -7;
2023 }
2024 }
2025 else
2026 {
2027 decode_exp_lsp(s, ch);
2028 }
2029 }
2030 }
2031 }
2032 else
2033 {
2034 for(ch = 0; ch < s->nb_channels; ++ch)
2035 {
2036 if (s->channel_coded[ch])
2037 {
2038 interpolate_array(s->exponents[ch],
2039 1 << s->prev_block_len_bits,
2040 s->block_len);
2041 }
2042 }
2043 }
2044//ok up to here!
2045//printf("got here!\n");
2046//rb->splash(HZ, "in wma_decode_block 2");
2047 /* parse spectral coefficients : just RLE encoding */
2048 for(ch = 0; ch < s->nb_channels; ++ch)
2049 {
2050 if (s->channel_coded[ch])
2051 {
2052 VLC *coef_vlc;
2053 int level, run, sign, tindex;
2054 int16_t *ptr, *eptr;
2055 const int16_t *level_table, *run_table;
2056
2057 /* special VLC tables are used for ms stereo because
2058 there is potentially less energy there */
2059 tindex = (ch == 1 && s->ms_stereo);
2060 coef_vlc = &s->coef_vlc[tindex];
2061 run_table = s->run_table[tindex];
2062 level_table = s->level_table[tindex];
2063 /* XXX: optimize */
2064 ptr = &s->coefs1[ch][0];
2065 eptr = ptr + nb_coefs[ch];
2066 memset(ptr, 0, s->block_len * sizeof(int16_t));
2067
2068
2069
2070 for(;;)
2071 {
2072 code = get_vlc(&s->gb, coef_vlc);
2073 if (code < 0)
2074 {
2075 return -8;
2076 }
2077 if (code == 1)
2078 {
2079 /* EOB */
2080 break;
2081 }
2082 else if (code == 0)
2083 {
2084 /* escape */
2085 level = get_bits(&s->gb, coef_nb_bits);
2086 /* NOTE: this is rather suboptimal. reading
2087 block_len_bits would be better */
2088 run = get_bits(&s->gb, s->frame_len_bits);
2089 }
2090 else
2091 {
2092 /* normal code */
2093 run = run_table[code];
2094 level = level_table[code];
2095 }
2096 sign = get_bits(&s->gb, 1);
2097 if (!sign)
2098 level = -level;
2099 ptr += run;
2100 if (ptr >= eptr)
2101 {
2102 return -9;
2103 }
2104 *ptr++ = level;
2105
2106
2107 /* NOTE: EOB can be omitted */
2108 if (ptr >= eptr)
2109 break;
2110 }
2111 }
2112 if (s->version == 1 && s->nb_channels >= 2)
2113 {
2114 align_get_bits(&s->gb);
2115 }
2116 }
2117
2118 {
2119 int n4 = s->block_len >> 1;
2120 //mdct_norm = 0x10000;
2121 //mdct_norm = fixdiv32(mdct_norm,itofix32(n4));
2122
2123 mdct_norm = 0x10000>>(s->block_len_bits-1); //theres no reason to do a divide by two in fixed precision ...
2124
2125 if (s->version == 1)
2126 {
2127 fixed32 tmp = fixsqrt32(itofix32(n4));
2128 mdct_norm *= tmp; // PJJ : exercise this path
2129 }
2130 }
2131
2132
2133
2134//rb->splash(HZ, "in wma_decode_block 3");
2135 /* finally compute the MDCT coefficients */
2136 for(ch = 0; ch < s->nb_channels; ++ch)
2137 {
2138 if (s->channel_coded[ch])
2139 {
2140 int16_t *coefs1;
2141 fixed32 *exponents, *exp_ptr;
2142 fixed32 *coefs, atemp;
2143 fixed64 mult;
2144 fixed64 mult1;
2145 fixed32 noise;
2146 int i, j, n, n1, last_high_band;
2147 fixed32 exp_power[HIGH_BAND_MAX_SIZE];
2148
2149 //double test, mul;
2150
2151 //total_gain, coefs1, mdctnorm are lossless
2152
2153 coefs1 = s->coefs1[ch];
2154 exponents = s->exponents[ch];
2155 mult = fixdiv64(pow_table[total_gain],Fixed32To64(s->max_exponent[ch]));
2156 // mul = fixtof64(pow_table[total_gain])/(s->block_len/2)/fixtof64(s->max_exponent[ch]);
2157
2158 mult = fixmul64byfixed(mult, mdct_norm); //what the hell? This is actually fixed64*2^16!
2159 coefs = s->coefs[ch]; //VLC exponenents are used to get MDCT coef here!
2160
2161 n=0;
2162
2163 if (s->use_noise_coding)
2164 {
2165 mult1 = mult;
2166
2167 /* very low freqs : noise */
2168 for(i = 0;i < s->coefs_start; ++i)
2169 {
2170 *coefs++ = fixmul32(fixmul32(s->noise_table[s->noise_index],(*exponents++)),Fixed32From64(mult1));
2171 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
2172 }
2173
2174 n1 = s->exponent_high_sizes[bsize];
2175
2176 /* compute power of high bands */
2177 exp_ptr = exponents +
2178 s->high_band_start[bsize] -
2179 s->coefs_start;
2180 last_high_band = 0; /* avoid warning */
2181 for (j=0;j<n1;++j)
2182 {
2183 n = s->exponent_high_bands[s->frame_len_bits -
2184 s->block_len_bits][j];
2185 if (s->high_band_coded[ch][j])
2186 {
2187 fixed32 e2, v;
2188 e2 = 0;
2189 for(i = 0;i < n; ++i)
2190 {
2191 v = exp_ptr[i];
2192 e2 += v * v;
2193 }
2194 exp_power[j] = fixdiv32(e2,n);
2195 last_high_band = j;
2196 }
2197 exp_ptr += n;
2198 }
2199
2200 /* main freqs and high freqs */
2201 for(j=-1;j<n1;++j)
2202 {
2203 if (j < 0)
2204 {
2205 n = s->high_band_start[bsize] -
2206 s->coefs_start;
2207 }
2208 else
2209 {
2210 n = s->exponent_high_bands[s->frame_len_bits -
2211 s->block_len_bits][j];
2212 }
2213 if (j >= 0 && s->high_band_coded[ch][j])
2214 {
2215 /* use noise with specified power */
2216 fixed32 tmp = fixdiv32(exp_power[j],exp_power[last_high_band]);
2217 mult1 = (fixed64)fixsqrt32(tmp);
2218 /* XXX: use a table */
2219 mult1 = mult1 * pow_table[s->high_band_values[ch][j]];
2220 mult1 = fixdiv64(mult1,fixmul32(s->max_exponent[ch],s->noise_mult));
2221 mult1 = fixmul64byfixed(mult1,mdct_norm);
2222 for(i = 0;i < n; ++i)
2223 {
2224 noise = s->noise_table[s->noise_index];
2225 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
2226 *coefs++ = fixmul32(fixmul32(*exponents,noise),Fixed32From64(mult1));
2227 ++exponents;
2228 }
2229 }
2230 else
2231 {
2232 /* coded values + small noise */
2233 for(i = 0;i < n; ++i)
2234 {
2235 // PJJ: check code path
2236 noise = s->noise_table[s->noise_index];
2237 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
2238 *coefs++ = fixmul32(fixmul32(((*coefs1++) + noise),*exponents),mult);
2239 ++exponents;
2240 }
2241 }
2242 }
2243
2244 /* very high freqs : noise */
2245 n = s->block_len - s->coefs_end[bsize];
2246 mult1 = fixmul32(mult,exponents[-1]);
2247 for (i = 0; i < n; ++i)
2248 {
2249 *coefs++ = fixmul32(s->noise_table[s->noise_index],Fixed32From64(mult1));
2250 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
2251 }
2252 }
2253 else
2254 {
2255
2256 /* XXX: optimize more */
2257 for(i = 0;i < s->coefs_start; ++i)
2258 *coefs++ = 0; //why do we do this step?!
2259 n = nb_coefs[ch];
2260
2261
2262
2263
2264 for(i = 0;i < n; ++i)
2265 {
2266
2267 atemp = (fixed32)(coefs1[i]*mult>>16);
2268 //atemp= ftofix32(coefs1[i] * fixtof64(exponents[i]) * fixtof64(mult>>16)); //this "works" in the sense that the mdcts converge
2269 *coefs++=fixmul32(atemp,exponents[i]); //this does not work
2270
2271
2272 //atemp = ftofix32( coefs1[i]*mul* fixtof64(exponents[i]) ); //this doesn't seem to help any at all.
2273// *coefs++=atemp;
2274
2275 } //coefs1 could underflow?
2276 n = s->block_len - s->coefs_end[bsize];
2277 for(i = 0;i < n; ++i)
2278 *coefs++ = 0;
2279 }
2280 }
2281 }
2282
2283
2284//rb->splash(HZ, "in wma_decode_block 3b");
2285 if (s->ms_stereo && s->channel_coded[1])
2286 {
2287 fixed32 a, b;
2288 int i;
2289
2290 /* nominal case for ms stereo: we do it before mdct */
2291 /* no need to optimize this case because it should almost
2292 never happen */
2293 if (!s->channel_coded[0])
2294 {
2295 memset(s->coefs[0], 0, sizeof(fixed32) * s->block_len);
2296 s->channel_coded[0] = 1;
2297 }
2298
2299 for(i = 0; i < s->block_len; ++i)
2300 {
2301 a = s->coefs[0][i];
2302 b = s->coefs[1][i];
2303 s->coefs[0][i] = a + b;
2304 s->coefs[1][i] = a - b;
2305 }
2306 }
2307//rb->splash(HZ, "in wma_decode_block 3c");
2308 /* build the window : we ensure that when the windows overlap
2309 their squared sum is always 1 (MDCT reconstruction rule) */
2310 /* XXX: merge with output */
2311 {
2312 int i, next_block_len, block_len, prev_block_len, n;
2313 fixed32 *wptr;
2314
2315 block_len = s->block_len;
2316 prev_block_len = 1 << s->prev_block_len_bits;
2317 next_block_len = 1 << s->next_block_len_bits;
2318 //rb->splash(HZ, "in wma_decode_block 3d"); //got here
2319 /* right part */
2320 wptr = window + block_len;
2321 if (block_len <= next_block_len)
2322 {
2323 for(i=0;i<block_len;++i)
2324 *wptr++ = s->windows[bsize][i];
2325 }
2326 else
2327 {
2328 /* overlap */
2329 n = (block_len / 2) - (next_block_len / 2);
2330 for(i=0;i<n;++i)
2331 *wptr++ = itofix32(1);
2332 for(i=0;i<next_block_len;++i)
2333 *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
2334 for(i=0;i<n;++i)
2335 *wptr++ = 0;
2336 }
2337//rb->splash(HZ, "in wma_decode_block 3e");
2338 /* left part */
2339 wptr = window + block_len;
2340 if (block_len <= prev_block_len)
2341 {
2342 for(i=0;i<block_len;++i)
2343 *--wptr = s->windows[bsize][i];
2344 }
2345 else
2346 {
2347 /* overlap */
2348 n = (block_len / 2) - (prev_block_len / 2);
2349 for(i=0;i<n;++i)
2350 *--wptr = itofix32(1);
2351 for(i=0;i<prev_block_len;++i)
2352 *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
2353 for(i=0;i<n;++i)
2354 *--wptr = 0;
2355 }
2356 }
2357
2358
2359 for(ch = 0; ch < s->nb_channels; ++ch)
2360 {
2361 if (s->channel_coded[ch])
2362 {
2363 static fixed32 output[BLOCK_MAX_SIZE * 2];
2364 fixed32 *ptr;
2365 int i, n4, index, n;
2366
2367 n = s->block_len;
2368 n4 = s->block_len >>1;
2369 //rb->splash(HZ, "in wma_decode_block 4");
2370 ff_imdct_calc(&s->mdct_ctx[bsize],
2371 output,
2372 s->coefs[ch],
2373 s->mdct_tmp);
2374
2375 /* XXX: optimize all that by build the window and
2376 multipying/adding at the same time */
2377 /* multiply by the window */
2378//already broken here!
2379
2380
2381
2382
2383
2384 for(i=0;i<n * 2;++i)
2385 {
2386
2387 output[i] = fixmul32(output[i], window[i]);
2388 //output[i] *= window[i];
2389
2390 }
2391
2392
2393 /* add in the frame */
2394 index = (s->frame_len / 2) + s->block_pos - n4;
2395 ptr = &s->frame_out[ch][index];
2396
2397 for(i=0;i<n * 2;++i)
2398 {
2399 *ptr += output[i];
2400 ++ptr;
2401
2402
2403 }
2404
2405
2406 /* specific fast case for ms-stereo : add to second
2407 channel if it is not coded */
2408 if (s->ms_stereo && !s->channel_coded[1])
2409 {
2410 ptr = &s->frame_out[1][index];
2411 for(i=0;i<n * 2;++i)
2412 {
2413 *ptr += output[i];
2414 ++ptr;
2415 }
2416 }
2417 }
2418 }
2419next:
2420 /* update block number */
2421 ++s->block_num;
2422 s->block_pos += s->block_len;
2423 if (s->block_pos >= s->frame_len)
2424 {
2425 return 1;
2426 }
2427 else
2428 {
2429 return 0;
2430 }
2431}
2432
2433/* decode a frame of frame_len samples */
2434static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
2435{
2436 int ret, i, n, a, ch, incr;
2437 int16_t *ptr;
2438 fixed32 *iptr;
2439 // rb->splash(HZ, "in wma_decode_frame");
2440
2441 /* read each block */
2442 s->block_num = 0;
2443 s->block_pos = 0;
2444
2445
2446 for(;;)
2447 {
2448 ret = wma_decode_block(s);
2449 if (ret < 0)
2450 {
2451 LOGF("wma_decode_block: %d",ret);
2452 //rb->splash(HZ*4, "wma_decode_block failed with ret %d", ret);
2453 return -1;
2454 }
2455 if (ret)
2456 {
2457 break;
2458 }
2459 }
2460
2461 /* convert frame to integer */
2462 n = s->frame_len;
2463 incr = s->nb_channels;
2464 for(ch = 0; ch < s->nb_channels; ++ch)
2465 {
2466 ptr = samples + ch;
2467 iptr = s->frame_out[ch];
2468
2469 for (i=0;i<n;++i)
2470 {
2471 a = fixtoi32(*iptr++)<<1; //ugly but good enough for now
2472
2473
2474
2475
2476
2477 if (a > 32767)
2478 {
2479 a = 32767;
2480 }
2481 else if (a < -32768)
2482 {
2483 a = -32768;
2484 }
2485 *ptr = a;
2486 ptr += incr;
2487 }
2488 /* prepare for next block */
2489 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
2490 s->frame_len * sizeof(fixed32));
2491 /* XXX: suppress this */
2492 memset(&s->frame_out[ch][s->frame_len], 0,
2493 s->frame_len * sizeof(fixed32));
2494 }
2495
2496 return 0;
2497}
2498
2499int wma_decode_superframe(WMADecodeContext* s,
2500 void *data,
2501 int *data_size,
2502 uint8_t *buf,
2503 int buf_size)
2504{
2505 //WMADecodeContext *s = avctx->priv_data;
2506 int nb_frames, bit_offset, i, pos, len;
2507 uint8_t *q;
2508 int16_t *samples;
2509
2510 if (buf_size==0)
2511 {
2512 s->last_superframe_len = 0;
2513 return 0;
2514 }
2515
2516 samples = data;
2517 init_get_bits(&s->gb, buf, buf_size*8);
2518 if (s->use_bit_reservoir)
2519 {
2520 /* read super frame header */
2521 get_bits(&s->gb, 4); /* super frame index */
2522 nb_frames = get_bits(&s->gb, 4) - 1;
2523
2524 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
2525 if (s->last_superframe_len > 0)
2526 {
2527 /* add bit_offset bits to last frame */
2528 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
2529 MAX_CODED_SUPERFRAME_SIZE)
2530 {
2531 goto fail;
2532 }
2533 q = s->last_superframe + s->last_superframe_len;
2534 len = bit_offset;
2535 while (len > 0)
2536 {
2537 *q++ = (get_bits)(&s->gb, 8);
2538 len -= 8;
2539 }
2540 if (len > 0)
2541 {
2542 *q++ = (get_bits)(&s->gb, len) << (8 - len);
2543 }
2544
2545 /* XXX: bit_offset bits into last frame */
2546 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
2547 /* skip unused bits */
2548 if (s->last_bitoffset > 0)
2549 skip_bits(&s->gb, s->last_bitoffset);
2550 /* this frame is stored in the last superframe and in the
2551 current one */
2552 if (wma_decode_frame(s, samples) < 0)
2553 {
2554 goto fail;
2555 }
2556 samples += s->nb_channels * s->frame_len;
2557 }
2558
2559 /* read each frame starting from bit_offset */
2560 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
2561 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
2562 len = pos & 7;
2563 if (len > 0)
2564 skip_bits(&s->gb, len);
2565
2566 s->reset_block_lengths = 1;
2567 for(i=0;i<nb_frames;++i)
2568 {
2569 if (wma_decode_frame(s, samples) < 0)
2570 {
2571 goto fail;
2572 }
2573 samples += s->nb_channels * s->frame_len;
2574 }
2575
2576 /* we copy the end of the frame in the last frame buffer */
2577 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
2578 s->last_bitoffset = pos & 7;
2579 pos >>= 3;
2580 len = buf_size - pos;
2581 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
2582 {
2583 goto fail;
2584 }
2585 s->last_superframe_len = len;
2586 memcpy(s->last_superframe, buf + pos, len);
2587 }
2588 else
2589 {
2590 /* single frame decode */
2591 if (wma_decode_frame(s, samples) < 0)
2592 {
2593 goto fail;
2594 }
2595 samples += s->nb_channels * s->frame_len;
2596 }
2597 *data_size = (int8_t *)samples - (int8_t *)data;
2598 return s->block_align;
2599fail:
2600 /* when error, we reset the bit reservoir */
2601 s->last_superframe_len = 0;
2602 return -1;
2603}
2604
2605/*void free_vlc(VLC *vlc)
2606{
2607 //av_free(vlc->table);
2608}
2609*/
2610int wma_decode_end(WMADecodeContext *s)
2611{
2612 (void)s;
2613/* WMADecodeContext *s = avctx->priv_data;
2614 int i;
2615
2616 for(i = 0; i < s->nb_block_sizes; ++i)
2617 ff_mdct_end(&s->mdct_ctx[i]);
2618 // for(i = 0; i < s->nb_block_sizes; ++i) //now statically allocated
2619 // av_free(s->windows[i]);
2620
2621 if (s->use_exp_vlc)
2622 {
2623 free_vlc(&s->exp_vlc);
2624 }
2625 if (s->use_noise_coding)
2626 {
2627 free_vlc(&s->hgain_vlc);
2628 }
2629 for(i = 0;i < 2; ++i)
2630 {
2631 // free_vlc(&s->coef_vlc[i]);
2632 // av_free(s->run_table[i]);
2633 // av_free(s->level_table[i]);
2634 }
2635*/
2636 return 0;
2637}