* Bad buffer management bug. Forgot to take care of the case
[libav.git] / libavcodec / mpegaudiodec.c
CommitLineData
de6d9b64
FB
1/*
2 * MPEG Audio decoder
ff4ec49e 3 * Copyright (c) 2001, 2002 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
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.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
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
de6d9b64 18 */
239c2f4c 19//#define DEBUG
de6d9b64 20#include "avcodec.h"
239c2f4c 21#include "mpegaudio.h"
de6d9b64
FB
22
23/*
239c2f4c
FB
24 * TODO:
25 * - in low precision mode, use more 16 bit multiplies in synth filter
26 * - test lsf / mpeg25 extensively.
de6d9b64
FB
27 */
28
239c2f4c
FB
29/* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
30 audio decoder */
31//#define USE_HIGHPRECISION
32
33#ifdef USE_HIGHPRECISION
34#define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
35#define WFRAC_BITS 16 /* fractional bits for window */
36#else
37#define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
38#define WFRAC_BITS 14 /* fractional bits for window */
39#endif
40
41#define FRAC_ONE (1 << FRAC_BITS)
42
43#define MULL(a,b) (((INT64)(a) * (INT64)(b)) >> FRAC_BITS)
44#define MUL64(a,b) ((INT64)(a) * (INT64)(b))
45#define FIX(a) ((int)((a) * FRAC_ONE))
46/* WARNING: only correct for posititive numbers */
47#define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
48#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
49
50#if FRAC_BITS <= 15
51typedef INT16 MPA_INT;
52#else
53typedef INT32 MPA_INT;
54#endif
55
56/****************/
57
de6d9b64
FB
58#define HEADER_SIZE 4
59#define BACKSTEP_SIZE 512
60
61typedef struct MPADecodeContext {
239c2f4c 62 UINT8 inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
de6d9b64
FB
63 int inbuf_index;
64 UINT8 *inbuf_ptr, *inbuf;
65 int frame_size;
239c2f4c
FB
66 int free_format_frame_size; /* frame size in case of free format
67 (zero if currently unknown) */
68 /* next header (used in free format parsing) */
69 UINT32 free_format_next_header;
de6d9b64
FB
70 int error_protection;
71 int layer;
72 int sample_rate;
239c2f4c 73 int sample_rate_index; /* between 0 and 8 */
de6d9b64
FB
74 int bit_rate;
75 int old_frame_size;
76 GetBitContext gb;
239c2f4c
FB
77 int nb_channels;
78 int mode;
79 int mode_ext;
80 int lsf;
81 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2];
82 int synth_buf_offset[MPA_MAX_CHANNELS];
83 INT32 sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT];
84 INT32 mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
85#ifdef DEBUG
86 int frame_count;
87#endif
de6d9b64
FB
88} MPADecodeContext;
89
239c2f4c
FB
90/* layer 3 "granule" */
91typedef struct GranuleDef {
92 UINT8 scfsi;
93 int part2_3_length;
94 int big_values;
95 int global_gain;
96 int scalefac_compress;
97 UINT8 block_type;
98 UINT8 switch_point;
99 int table_select[3];
100 int subblock_gain[3];
101 UINT8 scalefac_scale;
102 UINT8 count1table_select;
103 int region_size[3]; /* number of huffman codes in each region */
104 int preflag;
105 int short_start, long_end; /* long/short band indexes */
106 UINT8 scale_factors[40];
107 INT32 sb_hybrid[SBLIMIT * 18]; /* 576 samples */
108} GranuleDef;
de6d9b64 109
239c2f4c
FB
110#define MODE_EXT_MS_STEREO 2
111#define MODE_EXT_I_STEREO 1
112
113/* layer 3 huffman tables */
114typedef struct HuffTable {
115 int xsize;
116 const UINT8 *bits;
117 const UINT16 *codes;
118} HuffTable;
119
120#include "mpegaudiodectab.h"
121
122/* vlc structure for decoding layer 3 huffman tables */
123static VLC huff_vlc[16];
124static UINT8 *huff_code_table[16];
125static VLC huff_quad_vlc[2];
126/* computed from band_size_long */
127static UINT16 band_index_long[9][23];
128/* XXX: free when all decoders are closed */
129#define TABLE_4_3_SIZE (8191 + 16)
747a67fb 130static INT8 *table_4_3_exp;
239c2f4c
FB
131#if FRAC_BITS <= 15
132static UINT16 *table_4_3_value;
133#else
134static UINT32 *table_4_3_value;
135#endif
136/* intensity stereo coef table */
137static INT32 is_table[2][16];
138static INT32 is_table_lsf[2][2][16];
139static INT32 csa_table[8][2];
140static INT32 mdct_win[8][36];
141
142/* lower 2 bits: modulo 3, higher bits: shift */
143static UINT16 scale_factor_modshift[64];
144/* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
145static INT32 scale_factor_mult[15][3];
146/* mult table for layer 2 group quantization */
147
148#define SCALE_GEN(v) \
149{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
150
151static INT32 scale_factor_mult2[3][3] = {
152 SCALE_GEN(1.0 / 3.0), /* 3 steps */
153 SCALE_GEN(1.0 / 5.0), /* 5 steps */
154 SCALE_GEN(1.0 / 9.0), /* 9 steps */
155};
156
157/* 2^(n/4) */
158static UINT32 scale_factor_mult3[4] = {
159 FIXR(1.0),
160 FIXR(1.18920711500272106671),
161 FIXR(1.41421356237309504880),
162 FIXR(1.68179283050742908605),
de6d9b64
FB
163};
164
239c2f4c
FB
165static MPA_INT window[512];
166
167/* layer 1 unscaling */
168/* n = number of bits of the mantissa minus 1 */
169static inline int l1_unscale(int n, int mant, int scale_factor)
170{
171 int shift, mod;
172 INT64 val;
173
174 shift = scale_factor_modshift[scale_factor];
175 mod = shift & 3;
176 shift >>= 2;
177 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
178 shift += n;
179 return (int)((val + (1 << (shift - 1))) >> shift);
180}
181
182static inline int l2_unscale_group(int steps, int mant, int scale_factor)
183{
184 int shift, mod, val;
185
186 shift = scale_factor_modshift[scale_factor];
187 mod = shift & 3;
188 shift >>= 2;
189 /* XXX: store the result directly */
190 val = (2 * (mant - (steps >> 1))) * scale_factor_mult2[steps >> 2][mod];
191 return (val + (1 << (shift - 1))) >> shift;
192}
193
194/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
195static inline int l3_unscale(int value, int exponent)
196{
197#if FRAC_BITS <= 15
198 unsigned int m;
199#else
200 UINT64 m;
201#endif
202 int e;
203
204 e = table_4_3_exp[value];
205 e += (exponent >> 2);
206 e = FRAC_BITS - e;
207#if FRAC_BITS <= 15
208 if (e > 31)
209 e = 31;
210#endif
211 m = table_4_3_value[value];
212#if FRAC_BITS <= 15
213 m = (m * scale_factor_mult3[exponent & 3]);
214 m = (m + (1 << (e-1))) >> e;
215 return m;
216#else
217 m = MUL64(m, scale_factor_mult3[exponent & 3]);
218 m = (m + (UINT64_C(1) << (e-1))) >> e;
219 return m;
220#endif
221}
222
f9ed4f88
FB
223/* all integer n^(4/3) computation code */
224#define DEV_ORDER 13
225
226#define POW_FRAC_BITS 24
227#define POW_FRAC_ONE (1 << POW_FRAC_BITS)
228#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
229#define POW_MULL(a,b) (((INT64)(a) * (INT64)(b)) >> POW_FRAC_BITS)
230
231static int dev_4_3_coefs[DEV_ORDER];
232
233static int pow_mult3[3] = {
234 POW_FIX(1.0),
235 POW_FIX(1.25992104989487316476),
236 POW_FIX(1.58740105196819947474),
237};
238
239static void int_pow_init(void)
240{
241 int i, a;
242
243 a = POW_FIX(1.0);
244 for(i=0;i<DEV_ORDER;i++) {
245 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
246 dev_4_3_coefs[i] = a;
247 }
248}
249
250/* return the mantissa and the binary exponent */
251static int int_pow(int i, int *exp_ptr)
252{
253 int e, er, eq, j;
254 int a, a1;
255
256 /* renormalize */
257 a = i;
258 e = POW_FRAC_BITS;
259 while (a < (1 << (POW_FRAC_BITS - 1))) {
260 a = a << 1;
261 e--;
262 }
263 a -= (1 << POW_FRAC_BITS);
264 a1 = 0;
265 for(j = DEV_ORDER - 1; j >= 0; j--)
266 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
267 a = (1 << POW_FRAC_BITS) + a1;
268 /* exponent compute (exact) */
269 e = e * 4;
270 er = e % 3;
271 eq = e / 3;
272 a = POW_MULL(a, pow_mult3[er]);
273 while (a >= 2 * POW_FRAC_ONE) {
274 a = a >> 1;
275 eq++;
276 }
277 /* convert to float */
278 while (a < POW_FRAC_ONE) {
279 a = a << 1;
280 eq--;
281 }
59d3e367
FB
282 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
283#if (POW_FRAC_BITS - 1) > FRAC_BITS
284 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
285 /* correct overflow */
286 if (a >= 2 * (1 << FRAC_BITS)) {
287 a = a >> 1;
288 eq++;
289 }
290#endif
f9ed4f88 291 *exp_ptr = eq;
f9ed4f88 292 return a;
f9ed4f88 293}
de6d9b64
FB
294
295static int decode_init(AVCodecContext * avctx)
296{
297 MPADecodeContext *s = avctx->priv_data;
de6d9b64 298 static int init;
239c2f4c 299 int i, j, k;
de6d9b64
FB
300
301 if(!init) {
239c2f4c
FB
302 /* scale factors table for layer 1/2 */
303 for(i=0;i<64;i++) {
304 int shift, mod;
305 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
306 shift = (i / 3) - 1;
307 mod = i % 3;
308#if FRAC_BITS <= 15
309 if (shift > 31)
310 shift = 31;
311#endif
312 scale_factor_modshift[i] = mod | (shift << 2);
313 }
314
315 /* scale factor multiply for layer 1 */
316 for(i=0;i<15;i++) {
317 int n, norm;
318 n = i + 2;
319 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
320 scale_factor_mult[i][0] = MULL(FIXR(1.0), norm);
321 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259), norm);
322 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249), norm);
323 dprintf("%d: norm=%x s=%x %x %x\n",
324 i, norm,
325 scale_factor_mult[i][0],
326 scale_factor_mult[i][1],
327 scale_factor_mult[i][2]);
328 }
329
330 /* window */
331 /* max = 18760, max sum over all 16 coefs : 44736 */
332 for(i=0;i<257;i++) {
333 int v;
334 v = mpa_enwindow[i];
335#if WFRAC_BITS < 16
336 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
337#endif
338 window[i] = v;
339 if ((i & 63) != 0)
340 v = -v;
341 if (i != 0)
342 window[512 - i] = v;
343 }
344
345 /* huffman decode tables */
346 huff_code_table[0] = NULL;
347 for(i=1;i<16;i++) {
348 const HuffTable *h = &mpa_huff_tables[i];
349 int xsize, n, x, y;
350 UINT8 *code_table;
351
352 xsize = h->xsize;
353 n = xsize * xsize;
354 /* XXX: fail test */
355 init_vlc(&huff_vlc[i], 8, n,
356 h->bits, 1, 1, h->codes, 2, 2);
357
358 code_table = av_mallocz(n);
359 j = 0;
360 for(x=0;x<xsize;x++) {
361 for(y=0;y<xsize;y++)
362 code_table[j++] = (x << 4) | y;
363 }
364 huff_code_table[i] = code_table;
365 }
366 for(i=0;i<2;i++) {
367 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
368 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1);
369 }
370
371 for(i=0;i<9;i++) {
372 k = 0;
373 for(j=0;j<22;j++) {
374 band_index_long[i][j] = k;
375 k += band_size_long[i][j];
376 }
377 band_index_long[i][22] = k;
378 }
379
380 /* compute n ^ (4/3) and store it in mantissa/exp format */
381 table_4_3_exp = av_mallocz(TABLE_4_3_SIZE *
382 sizeof(table_4_3_exp[0]));
383 if (!table_4_3_exp)
384 return -1;
385 table_4_3_value = av_mallocz(TABLE_4_3_SIZE *
386 sizeof(table_4_3_value[0]));
387 if (!table_4_3_value) {
f9ed4f88 388 av_free(table_4_3_exp);
239c2f4c
FB
389 return -1;
390 }
391
f9ed4f88 392 int_pow_init();
239c2f4c 393 for(i=1;i<TABLE_4_3_SIZE;i++) {
239c2f4c 394 int e, m;
f9ed4f88 395 m = int_pow(i, &e);
f9ed4f88
FB
396#if 0
397 /* test code */
398 {
399 double f, fm;
400 int e1, m1;
401 f = pow((double)i, 4.0 / 3.0);
402 fm = frexp(f, &e1);
403 m1 = FIXR(2 * fm);
404#if FRAC_BITS <= 15
59d3e367
FB
405 if ((unsigned short)m1 != m1) {
406 m1 = m1 >> 1;
407 e1++;
408 }
f9ed4f88
FB
409#endif
410 e1--;
411 if (m != m1 || e != e1) {
412 printf("%4d: m=%x m1=%x e=%d e1=%d\n",
413 i, m, m1, e, e1);
414 }
415 }
416#endif
239c2f4c
FB
417 /* normalized to FRAC_BITS */
418 table_4_3_value[i] = m;
59d3e367 419 table_4_3_exp[i] = e;
239c2f4c 420 }
239c2f4c
FB
421
422 for(i=0;i<7;i++) {
423 float f;
424 int v;
425 if (i != 6) {
426 f = tan((double)i * M_PI / 12.0);
427 v = FIXR(f / (1.0 + f));
428 } else {
429 v = FIXR(1.0);
430 }
431 is_table[0][i] = v;
432 is_table[1][6 - i] = v;
433 }
434 /* invalid values */
435 for(i=7;i<16;i++)
436 is_table[0][i] = is_table[1][i] = 0.0;
437
438 for(i=0;i<16;i++) {
439 double f;
440 int e, k;
441
442 for(j=0;j<2;j++) {
443 e = -(j + 1) * ((i + 1) >> 1);
444 f = pow(2.0, e / 4.0);
445 k = i & 1;
446 is_table_lsf[j][k ^ 1][i] = FIXR(f);
447 is_table_lsf[j][k][i] = FIXR(1.0);
448 dprintf("is_table_lsf %d %d: %x %x\n",
449 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
450 }
451 }
452
453 for(i=0;i<8;i++) {
454 float ci, cs, ca;
455 ci = ci_table[i];
456 cs = 1.0 / sqrt(1.0 + ci * ci);
457 ca = cs * ci;
458 csa_table[i][0] = FIX(cs);
459 csa_table[i][1] = FIX(ca);
460 }
461
462 /* compute mdct windows */
463 for(i=0;i<36;i++) {
464 int v;
465 v = FIXR(sin(M_PI * (i + 0.5) / 36.0));
466 mdct_win[0][i] = v;
467 mdct_win[1][i] = v;
468 mdct_win[3][i] = v;
469 }
470 for(i=0;i<6;i++) {
471 mdct_win[1][18 + i] = FIXR(1.0);
472 mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0));
473 mdct_win[1][30 + i] = FIXR(0.0);
474
475 mdct_win[3][i] = FIXR(0.0);
476 mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
477 mdct_win[3][12 + i] = FIXR(1.0);
478 }
479
480 for(i=0;i<12;i++)
481 mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
482
483 /* NOTE: we do frequency inversion adter the MDCT by changing
484 the sign of the right window coefs */
485 for(j=0;j<4;j++) {
486 for(i=0;i<36;i+=2) {
487 mdct_win[j + 4][i] = mdct_win[j][i];
488 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
489 }
490 }
491
492#if defined(DEBUG)
493 for(j=0;j<8;j++) {
494 printf("win%d=\n", j);
495 for(i=0;i<36;i++)
496 printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
497 printf("\n");
498 }
499#endif
de6d9b64 500 init = 1;
de6d9b64
FB
501 }
502
503 s->inbuf_index = 0;
504 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
505 s->inbuf_ptr = s->inbuf;
239c2f4c
FB
506#ifdef DEBUG
507 s->frame_count = 0;
508#endif
de6d9b64
FB
509 return 0;
510}
511
239c2f4c
FB
512/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */;
513
514/* cos(i*pi/64) */
515
516#define COS0_0 FIXR(0.50060299823519630134)
517#define COS0_1 FIXR(0.50547095989754365998)
518#define COS0_2 FIXR(0.51544730992262454697)
519#define COS0_3 FIXR(0.53104259108978417447)
520#define COS0_4 FIXR(0.55310389603444452782)
521#define COS0_5 FIXR(0.58293496820613387367)
522#define COS0_6 FIXR(0.62250412303566481615)
523#define COS0_7 FIXR(0.67480834145500574602)
524#define COS0_8 FIXR(0.74453627100229844977)
525#define COS0_9 FIXR(0.83934964541552703873)
526#define COS0_10 FIXR(0.97256823786196069369)
527#define COS0_11 FIXR(1.16943993343288495515)
528#define COS0_12 FIXR(1.48416461631416627724)
529#define COS0_13 FIXR(2.05778100995341155085)
530#define COS0_14 FIXR(3.40760841846871878570)
531#define COS0_15 FIXR(10.19000812354805681150)
532
533#define COS1_0 FIXR(0.50241928618815570551)
534#define COS1_1 FIXR(0.52249861493968888062)
535#define COS1_2 FIXR(0.56694403481635770368)
536#define COS1_3 FIXR(0.64682178335999012954)
537#define COS1_4 FIXR(0.78815462345125022473)
538#define COS1_5 FIXR(1.06067768599034747134)
539#define COS1_6 FIXR(1.72244709823833392782)
540#define COS1_7 FIXR(5.10114861868916385802)
541
542#define COS2_0 FIXR(0.50979557910415916894)
543#define COS2_1 FIXR(0.60134488693504528054)
544#define COS2_2 FIXR(0.89997622313641570463)
545#define COS2_3 FIXR(2.56291544774150617881)
546
547#define COS3_0 FIXR(0.54119610014619698439)
548#define COS3_1 FIXR(1.30656296487637652785)
549
550#define COS4_0 FIXR(0.70710678118654752439)
551
552/* butterfly operator */
553#define BF(a, b, c)\
554{\
555 tmp0 = tab[a] + tab[b];\
556 tmp1 = tab[a] - tab[b];\
557 tab[a] = tmp0;\
558 tab[b] = MULL(tmp1, c);\
559}
560
561#define BF1(a, b, c, d)\
562{\
563 BF(a, b, COS4_0);\
564 BF(c, d, -COS4_0);\
565 tab[c] += tab[d];\
566}
567
568#define BF2(a, b, c, d)\
569{\
570 BF(a, b, COS4_0);\
571 BF(c, d, -COS4_0);\
572 tab[c] += tab[d];\
573 tab[a] += tab[c];\
574 tab[c] += tab[b];\
575 tab[b] += tab[d];\
576}
577
578#define ADD(a, b) tab[a] += tab[b]
579
580/* DCT32 without 1/sqrt(2) coef zero scaling. */
581static void dct32(INT32 *out, INT32 *tab)
582{
583 int tmp0, tmp1;
584
585 /* pass 1 */
586 BF(0, 31, COS0_0);
587 BF(1, 30, COS0_1);
588 BF(2, 29, COS0_2);
589 BF(3, 28, COS0_3);
590 BF(4, 27, COS0_4);
591 BF(5, 26, COS0_5);
592 BF(6, 25, COS0_6);
593 BF(7, 24, COS0_7);
594 BF(8, 23, COS0_8);
595 BF(9, 22, COS0_9);
596 BF(10, 21, COS0_10);
597 BF(11, 20, COS0_11);
598 BF(12, 19, COS0_12);
599 BF(13, 18, COS0_13);
600 BF(14, 17, COS0_14);
601 BF(15, 16, COS0_15);
602
603 /* pass 2 */
604 BF(0, 15, COS1_0);
605 BF(1, 14, COS1_1);
606 BF(2, 13, COS1_2);
607 BF(3, 12, COS1_3);
608 BF(4, 11, COS1_4);
609 BF(5, 10, COS1_5);
610 BF(6, 9, COS1_6);
611 BF(7, 8, COS1_7);
612
613 BF(16, 31, -COS1_0);
614 BF(17, 30, -COS1_1);
615 BF(18, 29, -COS1_2);
616 BF(19, 28, -COS1_3);
617 BF(20, 27, -COS1_4);
618 BF(21, 26, -COS1_5);
619 BF(22, 25, -COS1_6);
620 BF(23, 24, -COS1_7);
621
622 /* pass 3 */
623 BF(0, 7, COS2_0);
624 BF(1, 6, COS2_1);
625 BF(2, 5, COS2_2);
626 BF(3, 4, COS2_3);
627
628 BF(8, 15, -COS2_0);
629 BF(9, 14, -COS2_1);
630 BF(10, 13, -COS2_2);
631 BF(11, 12, -COS2_3);
632
633 BF(16, 23, COS2_0);
634 BF(17, 22, COS2_1);
635 BF(18, 21, COS2_2);
636 BF(19, 20, COS2_3);
637
638 BF(24, 31, -COS2_0);
639 BF(25, 30, -COS2_1);
640 BF(26, 29, -COS2_2);
641 BF(27, 28, -COS2_3);
642
643 /* pass 4 */
644 BF(0, 3, COS3_0);
645 BF(1, 2, COS3_1);
646
647 BF(4, 7, -COS3_0);
648 BF(5, 6, -COS3_1);
649
650 BF(8, 11, COS3_0);
651 BF(9, 10, COS3_1);
652
653 BF(12, 15, -COS3_0);
654 BF(13, 14, -COS3_1);
655
656 BF(16, 19, COS3_0);
657 BF(17, 18, COS3_1);
658
659 BF(20, 23, -COS3_0);
660 BF(21, 22, -COS3_1);
661
662 BF(24, 27, COS3_0);
663 BF(25, 26, COS3_1);
664
665 BF(28, 31, -COS3_0);
666 BF(29, 30, -COS3_1);
667
668 /* pass 5 */
669 BF1(0, 1, 2, 3);
670 BF2(4, 5, 6, 7);
671 BF1(8, 9, 10, 11);
672 BF2(12, 13, 14, 15);
673 BF1(16, 17, 18, 19);
674 BF2(20, 21, 22, 23);
675 BF1(24, 25, 26, 27);
676 BF2(28, 29, 30, 31);
677
678 /* pass 6 */
679
680 ADD( 8, 12);
681 ADD(12, 10);
682 ADD(10, 14);
683 ADD(14, 9);
684 ADD( 9, 13);
685 ADD(13, 11);
686 ADD(11, 15);
687
688 out[ 0] = tab[0];
689 out[16] = tab[1];
690 out[ 8] = tab[2];
691 out[24] = tab[3];
692 out[ 4] = tab[4];
693 out[20] = tab[5];
694 out[12] = tab[6];
695 out[28] = tab[7];
696 out[ 2] = tab[8];
697 out[18] = tab[9];
698 out[10] = tab[10];
699 out[26] = tab[11];
700 out[ 6] = tab[12];
701 out[22] = tab[13];
702 out[14] = tab[14];
703 out[30] = tab[15];
704
705 ADD(24, 28);
706 ADD(28, 26);
707 ADD(26, 30);
708 ADD(30, 25);
709 ADD(25, 29);
710 ADD(29, 27);
711 ADD(27, 31);
712
713 out[ 1] = tab[16] + tab[24];
714 out[17] = tab[17] + tab[25];
715 out[ 9] = tab[18] + tab[26];
716 out[25] = tab[19] + tab[27];
717 out[ 5] = tab[20] + tab[28];
718 out[21] = tab[21] + tab[29];
719 out[13] = tab[22] + tab[30];
720 out[29] = tab[23] + tab[31];
721 out[ 3] = tab[24] + tab[20];
722 out[19] = tab[25] + tab[21];
723 out[11] = tab[26] + tab[22];
724 out[27] = tab[27] + tab[23];
725 out[ 7] = tab[28] + tab[18];
726 out[23] = tab[29] + tab[19];
727 out[15] = tab[30] + tab[17];
728 out[31] = tab[31];
729}
730
731#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
732
733#if FRAC_BITS <= 15
734
735#define OUT_SAMPLE(sum)\
736{\
737 int sum1;\
738 sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;\
739 if (sum1 < -32768)\
740 sum1 = -32768;\
741 else if (sum1 > 32767)\
742 sum1 = 32767;\
743 *samples = sum1;\
744 samples += incr;\
745}
746
747#define SUM8(off, op) \
748{ \
749 sum op w[0 * 64 + off] * p[0 * 64];\
750 sum op w[1 * 64 + off] * p[1 * 64];\
751 sum op w[2 * 64 + off] * p[2 * 64];\
752 sum op w[3 * 64 + off] * p[3 * 64];\
753 sum op w[4 * 64 + off] * p[4 * 64];\
754 sum op w[5 * 64 + off] * p[5 * 64];\
755 sum op w[6 * 64 + off] * p[6 * 64];\
756 sum op w[7 * 64 + off] * p[7 * 64];\
757}
758
759#else
760
761#define OUT_SAMPLE(sum)\
762{\
763 int sum1;\
764 sum1 = (int)((sum + (INT64_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);\
765 if (sum1 < -32768)\
766 sum1 = -32768;\
767 else if (sum1 > 32767)\
768 sum1 = 32767;\
769 *samples = sum1;\
770 samples += incr;\
771}
772
773#define SUM8(off, op) \
774{ \
775 sum op MUL64(w[0 * 64 + off], p[0 * 64]);\
776 sum op MUL64(w[1 * 64 + off], p[1 * 64]);\
777 sum op MUL64(w[2 * 64 + off], p[2 * 64]);\
778 sum op MUL64(w[3 * 64 + off], p[3 * 64]);\
779 sum op MUL64(w[4 * 64 + off], p[4 * 64]);\
780 sum op MUL64(w[5 * 64 + off], p[5 * 64]);\
781 sum op MUL64(w[6 * 64 + off], p[6 * 64]);\
782 sum op MUL64(w[7 * 64 + off], p[7 * 64]);\
783}
784
785#endif
786
787/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
788 32 samples. */
789/* XXX: optimize by avoiding ring buffer usage */
790static void synth_filter(MPADecodeContext *s1,
791 int ch, INT16 *samples, int incr,
792 INT32 sb_samples[SBLIMIT])
793{
794 INT32 tmp[32];
795 register MPA_INT *synth_buf, *p;
796 register MPA_INT *w;
797 int j, offset, v;
798#if FRAC_BITS <= 15
799 int sum;
800#else
801 INT64 sum;
802#endif
803
804 dct32(tmp, sb_samples);
805
806 offset = s1->synth_buf_offset[ch];
807 synth_buf = s1->synth_buf[ch] + offset;
808
809 for(j=0;j<32;j++) {
810 v = tmp[j];
811#if FRAC_BITS <= 15
812 if (v > 32767)
813 v = 32767;
814 else if (v < -32768)
815 v = -32768;
816#endif
817 synth_buf[j] = v;
818 }
819 /* copy to avoid wrap */
820 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
821
822 w = window;
823 for(j=0;j<16;j++) {
824 sum = 0;
825 p = synth_buf + 16 + j; /* 0-15 */
826 SUM8(0, +=);
827 p = synth_buf + 48 - j; /* 32-47 */
828 SUM8(32, -=);
829 OUT_SAMPLE(sum);
830 w++;
831 }
832
833 p = synth_buf + 32; /* 48 */
834 sum = 0;
835 SUM8(32, -=);
836 OUT_SAMPLE(sum);
837 w++;
838
839 for(j=17;j<32;j++) {
840 sum = 0;
841 p = synth_buf + 48 - j; /* 17-31 */
842 SUM8(0, -=);
843 p = synth_buf + 16 + j; /* 49-63 */
844 SUM8(32, -=);
845 OUT_SAMPLE(sum);
846 w++;
847 }
848 offset = (offset - 32) & 511;
849 s1->synth_buf_offset[ch] = offset;
850}
851
852/* cos(pi*i/24) */
853#define C1 FIXR(0.99144486137381041114)
854#define C3 FIXR(0.92387953251128675612)
855#define C5 FIXR(0.79335334029123516458)
856#define C7 FIXR(0.60876142900872063941)
857#define C9 FIXR(0.38268343236508977173)
858#define C11 FIXR(0.13052619222005159154)
859
860/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
861 cases. */
862static void imdct12(int *out, int *in)
863{
864 int tmp;
865 INT64 in1_3, in1_9, in4_3, in4_9;
866
867 in1_3 = MUL64(in[1], C3);
868 in1_9 = MUL64(in[1], C9);
869 in4_3 = MUL64(in[4], C3);
870 in4_9 = MUL64(in[4], C9);
871
872 tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) +
873 MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
874 out[0] = tmp;
875 out[5] = -tmp;
876 tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 +
877 MUL64(in[2] + in[5], C3) - in4_9);
878 out[1] = tmp;
879 out[4] = -tmp;
880 tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
881 MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
882 out[2] = tmp;
883 out[3] = -tmp;
884 tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) +
885 MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
886 out[6] = tmp;
887 out[11] = tmp;
888 tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 +
889 MUL64(in[2] + in[5], C9) + in4_3);
890 out[7] = tmp;
891 out[10] = tmp;
892 tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
893 MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
894 out[8] = tmp;
895 out[9] = tmp;
896}
897
898#undef C1
899#undef C3
900#undef C5
901#undef C7
902#undef C9
903#undef C11
904
905/* cos(pi*i/18) */
906#define C1 FIXR(0.98480775301220805936)
907#define C2 FIXR(0.93969262078590838405)
908#define C3 FIXR(0.86602540378443864676)
909#define C4 FIXR(0.76604444311897803520)
910#define C5 FIXR(0.64278760968653932632)
911#define C6 FIXR(0.5)
912#define C7 FIXR(0.34202014332566873304)
913#define C8 FIXR(0.17364817766693034885)
914
915/* 0.5 / cos(pi*(2*i+1)/36) */
916static const int icos36[9] = {
917 FIXR(0.50190991877167369479),
918 FIXR(0.51763809020504152469),
919 FIXR(0.55168895948124587824),
920 FIXR(0.61038729438072803416),
921 FIXR(0.70710678118654752439),
922 FIXR(0.87172339781054900991),
923 FIXR(1.18310079157624925896),
924 FIXR(1.93185165257813657349),
925 FIXR(5.73685662283492756461),
926};
927
928static const int icos72[18] = {
929 /* 0.5 / cos(pi*(2*i+19)/72) */
930 FIXR(0.74009361646113053152),
931 FIXR(0.82133981585229078570),
932 FIXR(0.93057949835178895673),
933 FIXR(1.08284028510010010928),
934 FIXR(1.30656296487637652785),
935 FIXR(1.66275476171152078719),
936 FIXR(2.31011315767264929558),
937 FIXR(3.83064878777019433457),
938 FIXR(11.46279281302667383546),
939
940 /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
941 FIXR(-0.67817085245462840086),
942 FIXR(-0.63023620700513223342),
943 FIXR(-0.59284452371708034528),
944 FIXR(-0.56369097343317117734),
945 FIXR(-0.54119610014619698439),
946 FIXR(-0.52426456257040533932),
947 FIXR(-0.51213975715725461845),
948 FIXR(-0.50431448029007636036),
949 FIXR(-0.50047634258165998492),
950};
951
952/* using Lee like decomposition followed by hand coded 9 points DCT */
953static void imdct36(int *out, int *in)
954{
955 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
956 int tmp[18], *tmp1, *in1;
957 INT64 in3_3, in6_6;
958
959 for(i=17;i>=1;i--)
960 in[i] += in[i-1];
961 for(i=17;i>=3;i-=2)
962 in[i] += in[i-2];
963
964 for(j=0;j<2;j++) {
965 tmp1 = tmp + j;
966 in1 = in + j;
967
968 in3_3 = MUL64(in1[2*3], C3);
969 in6_6 = MUL64(in1[2*6], C6);
970
971 tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 +
972 MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
973 tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) +
974 MUL64(in1[2*4], C4) + in6_6 +
975 MUL64(in1[2*8], C8));
976 tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
977 tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) -
978 in1[2*6] + in1[2*0];
979 tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 -
980 MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
981 tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) -
982 MUL64(in1[2*4], C2) + in6_6 +
983 MUL64(in1[2*8], C4));
984 tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 +
985 MUL64(in1[2*5], C1) -
986 MUL64(in1[2*7], C5));
987 tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) +
988 MUL64(in1[2*4], C8) + in6_6 -
989 MUL64(in1[2*8], C2));
990 tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
991 }
992
993 i = 0;
994 for(j=0;j<4;j++) {
995 t0 = tmp[i];
996 t1 = tmp[i + 2];
997 s0 = t1 + t0;
998 s2 = t1 - t0;
999
1000 t2 = tmp[i + 1];
1001 t3 = tmp[i + 3];
1002 s1 = MULL(t3 + t2, icos36[j]);
1003 s3 = MULL(t3 - t2, icos36[8 - j]);
1004
1005 t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1006 t1 = MULL(s0 - s1, icos72[8 - j]);
1007 out[18 + 9 + j] = t0;
1008 out[18 + 8 - j] = t0;
1009 out[9 + j] = -t1;
1010 out[8 - j] = t1;
1011
1012 t0 = MULL(s2 + s3, icos72[9+j]);
1013 t1 = MULL(s2 - s3, icos72[j]);
1014 out[18 + 9 + (8 - j)] = t0;
1015 out[18 + j] = t0;
1016 out[9 + (8 - j)] = -t1;
1017 out[j] = t1;
1018 i += 4;
1019 }
1020
1021 s0 = tmp[16];
1022 s1 = MULL(tmp[17], icos36[4]);
1023 t0 = MULL(s0 + s1, icos72[9 + 4]);
1024 t1 = MULL(s0 - s1, icos72[4]);
1025 out[18 + 9 + 4] = t0;
1026 out[18 + 8 - 4] = t0;
1027 out[9 + 4] = -t1;
1028 out[8 - 4] = t1;
1029}
1030
de6d9b64
FB
1031/* fast header check for resync */
1032static int check_header(UINT32 header)
1033{
1034 /* header */
1035 if ((header & 0xffe00000) != 0xffe00000)
1036 return -1;
1037 /* layer check */
1038 if (((header >> 17) & 3) == 0)
1039 return -1;
239c2f4c
FB
1040 /* bit rate */
1041 if (((header >> 12) & 0xf) == 0xf)
de6d9b64
FB
1042 return -1;
1043 /* frequency */
1044 if (((header >> 10) & 3) == 3)
1045 return -1;
1046 return 0;
1047}
1048
239c2f4c
FB
1049/* header + layer + bitrate + freq + lsf/mpeg25 */
1050#define SAME_HEADER_MASK \
1051 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1052
de6d9b64 1053/* header decoding. MUST check the header before because no
239c2f4c
FB
1054 consistency check is done there. Return 1 if free format found and
1055 that the frame size must be computed externally */
1056static int decode_header(MPADecodeContext *s, UINT32 header)
de6d9b64 1057{
239c2f4c
FB
1058 int sample_rate, frame_size, mpeg25, padding;
1059 int sample_rate_index, bitrate_index;
de6d9b64 1060 if (header & (1<<20)) {
239c2f4c
FB
1061 s->lsf = (header & (1<<19)) ? 0 : 1;
1062 mpeg25 = 0;
de6d9b64 1063 } else {
239c2f4c
FB
1064 s->lsf = 1;
1065 mpeg25 = 1;
de6d9b64
FB
1066 }
1067
1068 s->layer = 4 - ((header >> 17) & 3);
1069 /* extract frequency */
239c2f4c
FB
1070 sample_rate_index = (header >> 10) & 3;
1071 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
de5123dc
ZK
1072 if (sample_rate == 0)
1073 return 1;
239c2f4c
FB
1074 sample_rate_index += 3 * (s->lsf + mpeg25);
1075 s->sample_rate_index = sample_rate_index;
1076 s->error_protection = ((header >> 16) & 1) ^ 1;
de6d9b64 1077
239c2f4c
FB
1078 bitrate_index = (header >> 12) & 0xf;
1079 padding = (header >> 9) & 1;
1080 //extension = (header >> 8) & 1;
1081 s->mode = (header >> 6) & 3;
1082 s->mode_ext = (header >> 4) & 3;
1083 //copyright = (header >> 3) & 1;
1084 //original = (header >> 2) & 1;
1085 //emphasis = header & 3;
de6d9b64 1086
239c2f4c
FB
1087 if (s->mode == MPA_MONO)
1088 s->nb_channels = 1;
1089 else
1090 s->nb_channels = 2;
de6d9b64 1091
239c2f4c
FB
1092 if (bitrate_index != 0) {
1093 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1094 s->bit_rate = frame_size * 1000;
1095 switch(s->layer) {
1096 case 1:
1097 frame_size = (frame_size * 12000) / sample_rate;
1098 frame_size = (frame_size + padding) * 4;
1099 break;
1100 case 2:
1101 frame_size = (frame_size * 144000) / sample_rate;
1102 frame_size += padding;
1103 break;
1104 default:
1105 case 3:
1106 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1107 frame_size += padding;
1108 break;
1109 }
1110 s->frame_size = frame_size;
1111 } else {
1112 /* if no frame size computed, signal it */
1113 if (!s->free_format_frame_size)
1114 return 1;
1115 /* free format: compute bitrate and real frame size from the
1116 frame size we extracted by reading the bitstream */
1117 s->frame_size = s->free_format_frame_size;
1118 switch(s->layer) {
1119 case 1:
1120 s->frame_size += padding * 4;
1121 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1122 break;
1123 case 2:
1124 s->frame_size += padding;
1125 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1126 break;
1127 default:
1128 case 3:
1129 s->frame_size += padding;
1130 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1131 break;
1132 }
de6d9b64 1133 }
de6d9b64 1134 s->sample_rate = sample_rate;
239c2f4c 1135
fad9f495 1136#if defined(DEBUG)
239c2f4c
FB
1137 printf("layer%d, %d Hz, %d kbits/s, ",
1138 s->layer, s->sample_rate, s->bit_rate);
1139 if (s->nb_channels == 2) {
1140 if (s->layer == 3) {
1141 if (s->mode_ext & MODE_EXT_MS_STEREO)
1142 printf("ms-");
1143 if (s->mode_ext & MODE_EXT_I_STEREO)
1144 printf("i-");
1145 }
1146 printf("stereo");
1147 } else {
1148 printf("mono");
1149 }
1150 printf("\n");
de6d9b64 1151#endif
239c2f4c 1152 return 0;
de6d9b64
FB
1153}
1154
239c2f4c
FB
1155/* return the number of decoded frames */
1156static int mp_decode_layer1(MPADecodeContext *s)
de6d9b64 1157{
239c2f4c
FB
1158 int bound, i, v, n, ch, j, mant;
1159 UINT8 allocation[MPA_MAX_CHANNELS][SBLIMIT];
1160 UINT8 scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1161
1162 if (s->mode == MPA_JSTEREO)
1163 bound = (s->mode_ext + 1) * 4;
1164 else
1165 bound = SBLIMIT;
1166
1167 /* allocation bits */
1168 for(i=0;i<bound;i++) {
1169 for(ch=0;ch<s->nb_channels;ch++) {
1170 allocation[ch][i] = get_bits(&s->gb, 4);
1171 }
1172 }
1173 for(i=bound;i<SBLIMIT;i++) {
1174 allocation[0][i] = get_bits(&s->gb, 4);
1175 }
1176
1177 /* scale factors */
1178 for(i=0;i<bound;i++) {
1179 for(ch=0;ch<s->nb_channels;ch++) {
1180 if (allocation[ch][i])
1181 scale_factors[ch][i] = get_bits(&s->gb, 6);
1182 }
1183 }
1184 for(i=bound;i<SBLIMIT;i++) {
1185 if (allocation[0][i]) {
1186 scale_factors[0][i] = get_bits(&s->gb, 6);
1187 scale_factors[1][i] = get_bits(&s->gb, 6);
1188 }
1189 }
de6d9b64 1190
239c2f4c
FB
1191 /* compute samples */
1192 for(j=0;j<12;j++) {
1193 for(i=0;i<bound;i++) {
1194 for(ch=0;ch<s->nb_channels;ch++) {
1195 n = allocation[ch][i];
1196 if (n) {
1197 mant = get_bits(&s->gb, n + 1);
1198 v = l1_unscale(n, mant, scale_factors[ch][i]);
1199 } else {
1200 v = 0;
1201 }
1202 s->sb_samples[ch][j][i] = v;
1203 }
1204 }
1205 for(i=bound;i<SBLIMIT;i++) {
1206 n = allocation[0][i];
1207 if (n) {
1208 mant = get_bits(&s->gb, n + 1);
1209 v = l1_unscale(n, mant, scale_factors[0][i]);
1210 s->sb_samples[0][j][i] = v;
1211 v = l1_unscale(n, mant, scale_factors[1][i]);
1212 s->sb_samples[1][j][i] = v;
1213 } else {
1214 s->sb_samples[0][j][i] = 0;
1215 s->sb_samples[1][j][i] = 0;
1216 }
1217 }
1218 }
1219 return 12;
1220}
1221
1222/* bitrate is in kb/s */
1223int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1224{
1225 int ch_bitrate, table;
de6d9b64 1226
239c2f4c
FB
1227 ch_bitrate = bitrate / nb_channels;
1228 if (!lsf) {
1229 if ((freq == 48000 && ch_bitrate >= 56) ||
1230 (ch_bitrate >= 56 && ch_bitrate <= 80))
1231 table = 0;
1232 else if (freq != 48000 && ch_bitrate >= 96)
1233 table = 1;
1234 else if (freq != 32000 && ch_bitrate <= 48)
1235 table = 2;
1236 else
1237 table = 3;
1238 } else {
1239 table = 4;
1240 }
1241 return table;
1242}
de6d9b64 1243
239c2f4c
FB
1244static int mp_decode_layer2(MPADecodeContext *s)
1245{
1246 int sblimit; /* number of used subbands */
1247 const unsigned char *alloc_table;
1248 int table, bit_alloc_bits, i, j, ch, bound, v;
1249 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1250 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1251 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1252 int scale, qindex, bits, steps, k, l, m, b;
de6d9b64 1253
239c2f4c
FB
1254 /* select decoding table */
1255 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1256 s->sample_rate, s->lsf);
1257 sblimit = sblimit_table[table];
1258 alloc_table = alloc_tables[table];
1259
1260 if (s->mode == MPA_JSTEREO)
1261 bound = (s->mode_ext + 1) * 4;
1262 else
1263 bound = sblimit;
1264
1265 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1266 /* parse bit allocation */
1267 j = 0;
1268 for(i=0;i<bound;i++) {
1269 bit_alloc_bits = alloc_table[j];
1270 for(ch=0;ch<s->nb_channels;ch++) {
1271 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1272 }
1273 j += 1 << bit_alloc_bits;
1274 }
1275 for(i=bound;i<sblimit;i++) {
1276 bit_alloc_bits = alloc_table[j];
1277 v = get_bits(&s->gb, bit_alloc_bits);
1278 bit_alloc[0][i] = v;
1279 bit_alloc[1][i] = v;
1280 j += 1 << bit_alloc_bits;
de6d9b64 1281 }
239c2f4c
FB
1282
1283#ifdef DEBUG
1284 {
1285 for(ch=0;ch<s->nb_channels;ch++) {
1286 for(i=0;i<sblimit;i++)
1287 printf(" %d", bit_alloc[ch][i]);
1288 printf("\n");
1289 }
1290 }
1291#endif
1292
1293 /* scale codes */
1294 for(i=0;i<sblimit;i++) {
1295 for(ch=0;ch<s->nb_channels;ch++) {
1296 if (bit_alloc[ch][i])
1297 scale_code[ch][i] = get_bits(&s->gb, 2);
1298 }
1299 }
1300
1301 /* scale factors */
1302 for(i=0;i<sblimit;i++) {
1303 for(ch=0;ch<s->nb_channels;ch++) {
1304 if (bit_alloc[ch][i]) {
1305 sf = scale_factors[ch][i];
1306 switch(scale_code[ch][i]) {
1307 default:
1308 case 0:
1309 sf[0] = get_bits(&s->gb, 6);
1310 sf[1] = get_bits(&s->gb, 6);
1311 sf[2] = get_bits(&s->gb, 6);
1312 break;
1313 case 2:
1314 sf[0] = get_bits(&s->gb, 6);
1315 sf[1] = sf[0];
1316 sf[2] = sf[0];
1317 break;
1318 case 1:
1319 sf[0] = get_bits(&s->gb, 6);
1320 sf[2] = get_bits(&s->gb, 6);
1321 sf[1] = sf[0];
1322 break;
1323 case 3:
1324 sf[0] = get_bits(&s->gb, 6);
1325 sf[2] = get_bits(&s->gb, 6);
1326 sf[1] = sf[2];
1327 break;
1328 }
1329 }
1330 }
1331 }
1332
1333#ifdef DEBUG
1334 for(ch=0;ch<s->nb_channels;ch++) {
1335 for(i=0;i<sblimit;i++) {
1336 if (bit_alloc[ch][i]) {
1337 sf = scale_factors[ch][i];
1338 printf(" %d %d %d", sf[0], sf[1], sf[2]);
1339 } else {
1340 printf(" -");
1341 }
1342 }
1343 printf("\n");
1344 }
1345#endif
1346
1347 /* samples */
1348 for(k=0;k<3;k++) {
1349 for(l=0;l<12;l+=3) {
1350 j = 0;
1351 for(i=0;i<bound;i++) {
1352 bit_alloc_bits = alloc_table[j];
1353 for(ch=0;ch<s->nb_channels;ch++) {
1354 b = bit_alloc[ch][i];
1355 if (b) {
1356 scale = scale_factors[ch][i][k];
1357 qindex = alloc_table[j+b];
1358 bits = quant_bits[qindex];
1359 if (bits < 0) {
1360 /* 3 values at the same time */
1361 v = get_bits(&s->gb, -bits);
1362 steps = quant_steps[qindex];
1363 s->sb_samples[ch][k * 12 + l + 0][i] =
1364 l2_unscale_group(steps, v % steps, scale);
1365 v = v / steps;
1366 s->sb_samples[ch][k * 12 + l + 1][i] =
1367 l2_unscale_group(steps, v % steps, scale);
1368 v = v / steps;
1369 s->sb_samples[ch][k * 12 + l + 2][i] =
1370 l2_unscale_group(steps, v, scale);
1371 } else {
1372 for(m=0;m<3;m++) {
1373 v = get_bits(&s->gb, bits);
1374 v = l1_unscale(bits - 1, v, scale);
1375 s->sb_samples[ch][k * 12 + l + m][i] = v;
1376 }
1377 }
1378 } else {
1379 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1380 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1381 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1382 }
1383 }
1384 /* next subband in alloc table */
1385 j += 1 << bit_alloc_bits;
1386 }
1387 /* XXX: find a way to avoid this duplication of code */
1388 for(i=bound;i<sblimit;i++) {
1389 bit_alloc_bits = alloc_table[j];
1390 b = bit_alloc[0][i];
1391 if (b) {
1392 int mant, scale0, scale1;
1393 scale0 = scale_factors[0][i][k];
1394 scale1 = scale_factors[1][i][k];
1395 qindex = alloc_table[j+b];
1396 bits = quant_bits[qindex];
1397 if (bits < 0) {
1398 /* 3 values at the same time */
1399 v = get_bits(&s->gb, -bits);
1400 steps = quant_steps[qindex];
1401 mant = v % steps;
1402 v = v / steps;
1403 s->sb_samples[0][k * 12 + l + 0][i] =
1404 l2_unscale_group(steps, mant, scale0);
1405 s->sb_samples[1][k * 12 + l + 0][i] =
1406 l2_unscale_group(steps, mant, scale1);
1407 mant = v % steps;
1408 v = v / steps;
1409 s->sb_samples[0][k * 12 + l + 1][i] =
1410 l2_unscale_group(steps, mant, scale0);
1411 s->sb_samples[1][k * 12 + l + 1][i] =
1412 l2_unscale_group(steps, mant, scale1);
1413 s->sb_samples[0][k * 12 + l + 2][i] =
1414 l2_unscale_group(steps, v, scale0);
1415 s->sb_samples[1][k * 12 + l + 2][i] =
1416 l2_unscale_group(steps, v, scale1);
1417 } else {
1418 for(m=0;m<3;m++) {
1419 mant = get_bits(&s->gb, bits);
1420 s->sb_samples[0][k * 12 + l + m][i] =
1421 l1_unscale(bits - 1, mant, scale0);
1422 s->sb_samples[1][k * 12 + l + m][i] =
1423 l1_unscale(bits - 1, mant, scale1);
1424 }
1425 }
1426 } else {
1427 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1428 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1429 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1430 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1431 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1432 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1433 }
1434 /* next subband in alloc table */
1435 j += 1 << bit_alloc_bits;
1436 }
1437 /* fill remaining samples to zero */
1438 for(i=sblimit;i<SBLIMIT;i++) {
1439 for(ch=0;ch<s->nb_channels;ch++) {
1440 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1441 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1442 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1443 }
1444 }
1445 }
1446 }
1447 return 3 * 12;
de6d9b64
FB
1448}
1449
1450/*
239c2f4c 1451 * Seek back in the stream for backstep bytes (at most 511 bytes)
de6d9b64 1452 */
239c2f4c 1453static void seek_to_maindata(MPADecodeContext *s, long backstep)
de6d9b64
FB
1454{
1455 UINT8 *ptr;
1456
1457 /* compute current position in stream */
27a3e2c5
MN
1458#ifdef ALT_BITSTREAM_READER
1459 ptr = s->gb.buffer + (s->gb.index>>3);
1460#else
239c2f4c 1461 ptr = s->gb.buf_ptr - (s->gb.bit_cnt >> 3);
27a3e2c5 1462#endif
de6d9b64
FB
1463 /* copy old data before current one */
1464 ptr -= backstep;
239c2f4c
FB
1465 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1466 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
de6d9b64 1467 /* init get bits again */
239c2f4c 1468 init_get_bits(&s->gb, ptr, s->frame_size + backstep);
de6d9b64 1469
239c2f4c
FB
1470 /* prepare next buffer */
1471 s->inbuf_index ^= 1;
1472 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1473 s->old_frame_size = s->frame_size;
1474}
1475
1476static inline void lsf_sf_expand(int *slen,
1477 int sf, int n1, int n2, int n3)
1478{
1479 if (n3) {
1480 slen[3] = sf % n3;
1481 sf /= n3;
1482 } else {
1483 slen[3] = 0;
1484 }
1485 if (n2) {
1486 slen[2] = sf % n2;
1487 sf /= n2;
1488 } else {
1489 slen[2] = 0;
1490 }
1491 slen[1] = sf % n1;
1492 sf /= n1;
1493 slen[0] = sf;
1494}
1495
1496static void exponents_from_scale_factors(MPADecodeContext *s,
1497 GranuleDef *g,
1498 INT16 *exponents)
1499{
1500 const UINT8 *bstab, *pretab;
1501 int len, i, j, k, l, v0, shift, gain, gains[3];
1502 INT16 *exp_ptr;
1503
1504 exp_ptr = exponents;
1505 gain = g->global_gain - 210;
1506 shift = g->scalefac_scale + 1;
1507
1508 bstab = band_size_long[s->sample_rate_index];
1509 pretab = mpa_pretab[g->preflag];
1510 for(i=0;i<g->long_end;i++) {
1511 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1512 len = bstab[i];
1513 for(j=len;j>0;j--)
1514 *exp_ptr++ = v0;
1515 }
1516
1517 if (g->short_start < 13) {
1518 bstab = band_size_short[s->sample_rate_index];
1519 gains[0] = gain - (g->subblock_gain[0] << 3);
1520 gains[1] = gain - (g->subblock_gain[1] << 3);
1521 gains[2] = gain - (g->subblock_gain[2] << 3);
1522 k = g->long_end;
1523 for(i=g->short_start;i<13;i++) {
1524 len = bstab[i];
1525 for(l=0;l<3;l++) {
1526 v0 = gains[l] - (g->scale_factors[k++] << shift);
1527 for(j=len;j>0;j--)
1528 *exp_ptr++ = v0;
1529 }
1530 }
1531 }
1532}
1533
1534/* handle n = 0 too */
1535static inline int get_bitsz(GetBitContext *s, int n)
1536{
1537 if (n == 0)
1538 return 0;
1539 else
1540 return get_bits(s, n);
1541}
1542
1543static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1544 INT16 *exponents, int end_pos)
1545{
1546 int s_index;
1547 int linbits, code, x, y, l, v, i, j, k, pos;
1548 UINT8 *last_buf_ptr;
1549 UINT32 last_bit_buf;
1550 int last_bit_cnt;
1551 VLC *vlc;
1552 UINT8 *code_table;
1553
1554 /* low frequencies (called big values) */
1555 s_index = 0;
1556 for(i=0;i<3;i++) {
1557 j = g->region_size[i];
1558 if (j == 0)
1559 continue;
1560 /* select vlc table */
1561 k = g->table_select[i];
1562 l = mpa_huff_data[k][0];
1563 linbits = mpa_huff_data[k][1];
1564 vlc = &huff_vlc[l];
1565 code_table = huff_code_table[l];
1566
1567 /* read huffcode and compute each couple */
1568 for(;j>0;j--) {
1569 if (get_bits_count(&s->gb) >= end_pos)
1570 break;
1571 if (code_table) {
1572 code = get_vlc(&s->gb, vlc);
1573 if (code < 0)
1574 return -1;
1575 y = code_table[code];
1576 x = y >> 4;
1577 y = y & 0x0f;
1578 } else {
1579 x = 0;
1580 y = 0;
1581 }
1582 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1583 i, g->region_size[i] - j, x, y, exponents[s_index]);
1584 if (x) {
1585 if (x == 15)
1586 x += get_bitsz(&s->gb, linbits);
1587 v = l3_unscale(x, exponents[s_index]);
1588 if (get_bits1(&s->gb))
1589 v = -v;
1590 } else {
1591 v = 0;
1592 }
1593 g->sb_hybrid[s_index++] = v;
1594 if (y) {
1595 if (y == 15)
1596 y += get_bitsz(&s->gb, linbits);
1597 v = l3_unscale(y, exponents[s_index]);
1598 if (get_bits1(&s->gb))
1599 v = -v;
1600 } else {
1601 v = 0;
1602 }
1603 g->sb_hybrid[s_index++] = v;
1604 }
1605 }
1606
1607 /* high frequencies */
1608 vlc = &huff_quad_vlc[g->count1table_select];
1609 last_buf_ptr = NULL;
1610 last_bit_buf = 0;
1611 last_bit_cnt = 0;
1612 while (s_index <= 572) {
1613 pos = get_bits_count(&s->gb);
1614 if (pos >= end_pos) {
1615 if (pos > end_pos && last_buf_ptr != NULL) {
1616 /* some encoders generate an incorrect size for this
1617 part. We must go back into the data */
1618 s_index -= 4;
27a3e2c5
MN
1619#ifdef ALT_BITSTREAM_READER
1620 s->gb.buffer = last_buf_ptr;
1621 s->gb.index = last_bit_cnt;
1622#else
239c2f4c
FB
1623 s->gb.buf_ptr = last_buf_ptr;
1624 s->gb.bit_buf = last_bit_buf;
1625 s->gb.bit_cnt = last_bit_cnt;
27a3e2c5 1626#endif
239c2f4c
FB
1627 }
1628 break;
1629 }
27a3e2c5
MN
1630#ifdef ALT_BITSTREAM_READER
1631 last_buf_ptr = s->gb.buffer;
1632 last_bit_cnt = s->gb.index;
1633#else
239c2f4c
FB
1634 last_buf_ptr = s->gb.buf_ptr;
1635 last_bit_buf = s->gb.bit_buf;
1636 last_bit_cnt = s->gb.bit_cnt;
27a3e2c5 1637#endif
239c2f4c
FB
1638
1639 code = get_vlc(&s->gb, vlc);
1640 dprintf("t=%d code=%d\n", g->count1table_select, code);
1641 if (code < 0)
1642 return -1;
1643 for(i=0;i<4;i++) {
1644 if (code & (8 >> i)) {
1645 /* non zero value. Could use a hand coded function for
1646 'one' value */
1647 v = l3_unscale(1, exponents[s_index]);
1648 if(get_bits1(&s->gb))
1649 v = -v;
1650 } else {
1651 v = 0;
1652 }
1653 g->sb_hybrid[s_index++] = v;
1654 }
1655 }
1656 while (s_index < 576)
1657 g->sb_hybrid[s_index++] = 0;
de6d9b64
FB
1658 return 0;
1659}
1660
239c2f4c
FB
1661/* Reorder short blocks from bitstream order to interleaved order. It
1662 would be faster to do it in parsing, but the code would be far more
1663 complicated */
1664static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1665{
1666 int i, j, k, len;
1667 INT32 *ptr, *dst, *ptr1;
1668 INT32 tmp[576];
1669
1670 if (g->block_type != 2)
1671 return;
1672
1673 if (g->switch_point) {
1674 if (s->sample_rate_index != 8) {
1675 ptr = g->sb_hybrid + 36;
1676 } else {
1677 ptr = g->sb_hybrid + 48;
1678 }
1679 } else {
1680 ptr = g->sb_hybrid;
1681 }
1682
1683 for(i=g->short_start;i<13;i++) {
1684 len = band_size_short[s->sample_rate_index][i];
1685 ptr1 = ptr;
1686 for(k=0;k<3;k++) {
1687 dst = tmp + k;
1688 for(j=len;j>0;j--) {
1689 *dst = *ptr++;
1690 dst += 3;
1691 }
1692 }
1693 memcpy(ptr1, tmp, len * 3 * sizeof(INT32));
1694 }
1695}
1696
1697#define ISQRT2 FIXR(0.70710678118654752440)
1698
1699static void compute_stereo(MPADecodeContext *s,
1700 GranuleDef *g0, GranuleDef *g1)
1701{
1702 int i, j, k, l;
1703 INT32 v1, v2;
1704 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1705 INT32 (*is_tab)[16];
1706 INT32 *tab0, *tab1;
1707 int non_zero_found_short[3];
1708
1709 /* intensity stereo */
1710 if (s->mode_ext & MODE_EXT_I_STEREO) {
1711 if (!s->lsf) {
1712 is_tab = is_table;
1713 sf_max = 7;
1714 } else {
1715 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1716 sf_max = 16;
1717 }
1718
1719 tab0 = g0->sb_hybrid + 576;
1720 tab1 = g1->sb_hybrid + 576;
1721
1722 non_zero_found_short[0] = 0;
1723 non_zero_found_short[1] = 0;
1724 non_zero_found_short[2] = 0;
1725 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1726 for(i = 12;i >= g1->short_start;i--) {
1727 /* for last band, use previous scale factor */
1728 if (i != 11)
1729 k -= 3;
1730 len = band_size_short[s->sample_rate_index][i];
1731 for(l=2;l>=0;l--) {
1732 tab0 -= len;
1733 tab1 -= len;
1734 if (!non_zero_found_short[l]) {
1735 /* test if non zero band. if so, stop doing i-stereo */
1736 for(j=0;j<len;j++) {
1737 if (tab1[j] != 0) {
1738 non_zero_found_short[l] = 1;
1739 goto found1;
1740 }
1741 }
1742 sf = g1->scale_factors[k + l];
1743 if (sf >= sf_max)
1744 goto found1;
1745
1746 v1 = is_tab[0][sf];
1747 v2 = is_tab[1][sf];
1748 for(j=0;j<len;j++) {
1749 tmp0 = tab0[j];
1750 tab0[j] = MULL(tmp0, v1);
1751 tab1[j] = MULL(tmp0, v2);
1752 }
1753 } else {
1754 found1:
1755 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1756 /* lower part of the spectrum : do ms stereo
1757 if enabled */
1758 for(j=0;j<len;j++) {
1759 tmp0 = tab0[j];
1760 tmp1 = tab1[j];
1761 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1762 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1763 }
1764 }
1765 }
1766 }
1767 }
1768
1769 non_zero_found = non_zero_found_short[0] |
1770 non_zero_found_short[1] |
1771 non_zero_found_short[2];
1772
1773 for(i = g1->long_end - 1;i >= 0;i--) {
1774 len = band_size_long[s->sample_rate_index][i];
1775 tab0 -= len;
1776 tab1 -= len;
1777 /* test if non zero band. if so, stop doing i-stereo */
1778 if (!non_zero_found) {
1779 for(j=0;j<len;j++) {
1780 if (tab1[j] != 0) {
1781 non_zero_found = 1;
1782 goto found2;
1783 }
1784 }
1785 /* for last band, use previous scale factor */
1786 k = (i == 21) ? 20 : i;
1787 sf = g1->scale_factors[k];
1788 if (sf >= sf_max)
1789 goto found2;
1790 v1 = is_tab[0][sf];
1791 v2 = is_tab[1][sf];
1792 for(j=0;j<len;j++) {
1793 tmp0 = tab0[j];
1794 tab0[j] = MULL(tmp0, v1);
1795 tab1[j] = MULL(tmp0, v2);
1796 }
1797 } else {
1798 found2:
1799 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1800 /* lower part of the spectrum : do ms stereo
1801 if enabled */
1802 for(j=0;j<len;j++) {
1803 tmp0 = tab0[j];
1804 tmp1 = tab1[j];
1805 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1806 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1807 }
1808 }
1809 }
1810 }
1811 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1812 /* ms stereo ONLY */
1813 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1814 global gain */
1815 tab0 = g0->sb_hybrid;
1816 tab1 = g1->sb_hybrid;
1817 for(i=0;i<576;i++) {
1818 tmp0 = tab0[i];
1819 tmp1 = tab1[i];
1820 tab0[i] = tmp0 + tmp1;
1821 tab1[i] = tmp0 - tmp1;
1822 }
1823 }
1824}
1825
1826static void compute_antialias(MPADecodeContext *s,
1827 GranuleDef *g)
1828{
1829 INT32 *ptr, *p0, *p1, *csa;
1830 int n, tmp0, tmp1, i, j;
1831
1832 /* we antialias only "long" bands */
1833 if (g->block_type == 2) {
1834 if (!g->switch_point)
1835 return;
1836 /* XXX: check this for 8000Hz case */
1837 n = 1;
1838 } else {
1839 n = SBLIMIT - 1;
1840 }
1841
1842 ptr = g->sb_hybrid + 18;
1843 for(i = n;i > 0;i--) {
1844 p0 = ptr - 1;
1845 p1 = ptr;
1846 csa = &csa_table[0][0];
1847 for(j=0;j<8;j++) {
1848 tmp0 = *p0;
1849 tmp1 = *p1;
1850 *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1851 *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1852 p0--;
1853 p1++;
1854 csa += 2;
1855 }
1856 ptr += 18;
1857 }
1858}
1859
1860static void compute_imdct(MPADecodeContext *s,
1861 GranuleDef *g,
1862 INT32 *sb_samples,
1863 INT32 *mdct_buf)
1864{
1865 INT32 *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
1866 INT32 in[6];
1867 INT32 out[36];
1868 INT32 out2[12];
1869 int i, j, k, mdct_long_end, v, sblimit;
1870
1871 /* find last non zero block */
1872 ptr = g->sb_hybrid + 576;
1873 ptr1 = g->sb_hybrid + 2 * 18;
1874 while (ptr >= ptr1) {
1875 ptr -= 6;
1876 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1877 if (v != 0)
1878 break;
1879 }
1880 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1881
1882 if (g->block_type == 2) {
1883 /* XXX: check for 8000 Hz */
1884 if (g->switch_point)
1885 mdct_long_end = 2;
1886 else
1887 mdct_long_end = 0;
1888 } else {
1889 mdct_long_end = sblimit;
1890 }
1891
1892 buf = mdct_buf;
1893 ptr = g->sb_hybrid;
1894 for(j=0;j<mdct_long_end;j++) {
1895 imdct36(out, ptr);
1896 /* apply window & overlap with previous buffer */
1897 out_ptr = sb_samples + j;
1898 /* select window */
1899 if (g->switch_point && j < 2)
1900 win1 = mdct_win[0];
1901 else
1902 win1 = mdct_win[g->block_type];
1903 /* select frequency inversion */
1904 win = win1 + ((4 * 36) & -(j & 1));
1905 for(i=0;i<18;i++) {
1906 *out_ptr = MULL(out[i], win[i]) + buf[i];
1907 buf[i] = MULL(out[i + 18], win[i + 18]);
1908 out_ptr += SBLIMIT;
1909 }
1910 ptr += 18;
1911 buf += 18;
1912 }
1913 for(j=mdct_long_end;j<sblimit;j++) {
1914 for(i=0;i<6;i++) {
1915 out[i] = 0;
1916 out[6 + i] = 0;
1917 out[30+i] = 0;
1918 }
1919 /* select frequency inversion */
1920 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1921 buf2 = out + 6;
1922 for(k=0;k<3;k++) {
1923 /* reorder input for short mdct */
1924 ptr1 = ptr + k;
1925 for(i=0;i<6;i++) {
1926 in[i] = *ptr1;
1927 ptr1 += 3;
1928 }
1929 imdct12(out2, in);
1930 /* apply 12 point window and do small overlap */
1931 for(i=0;i<6;i++) {
1932 buf2[i] = MULL(out2[i], win[i]) + buf2[i];
1933 buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
1934 }
1935 buf2 += 6;
1936 }
1937 /* overlap */
1938 out_ptr = sb_samples + j;
1939 for(i=0;i<18;i++) {
1940 *out_ptr = out[i] + buf[i];
1941 buf[i] = out[i + 18];
1942 out_ptr += SBLIMIT;
1943 }
1944 ptr += 18;
1945 buf += 18;
1946 }
1947 /* zero bands */
1948 for(j=sblimit;j<SBLIMIT;j++) {
1949 /* overlap */
1950 out_ptr = sb_samples + j;
1951 for(i=0;i<18;i++) {
1952 *out_ptr = buf[i];
1953 buf[i] = 0;
1954 out_ptr += SBLIMIT;
1955 }
1956 buf += 18;
1957 }
1958}
1959
747a67fb 1960#if defined(DEBUG)
239c2f4c
FB
1961void sample_dump(int fnum, INT32 *tab, int n)
1962{
1963 static FILE *files[16], *f;
1964 char buf[512];
1965
1966 f = files[fnum];
1967 if (!f) {
1968 sprintf(buf, "/tmp/out%d.pcm", fnum);
1969 f = fopen(buf, "w");
1970 if (!f)
1971 return;
1972 files[fnum] = f;
1973 }
1974
1975 if (fnum == 0) {
1976 int i;
1977 static int pos = 0;
1978 printf("pos=%d\n", pos);
1979 for(i=0;i<n;i++) {
1980 printf(" %f", (double)tab[i] / 32768.0);
1981 if ((i % 18) == 17)
1982 printf("\n");
1983 }
1984 pos += n;
1985 }
1986
1987 fwrite(tab, 1, n * sizeof(INT32), f);
1988}
1989#endif
1990
1991
1992/* main layer3 decoding function */
1993static int mp_decode_layer3(MPADecodeContext *s)
1994{
1995 int nb_granules, main_data_begin, private_bits;
1996 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
1997 GranuleDef granules[2][2], *g;
1998 INT16 exponents[576];
1999
2000 /* read side info */
2001 if (s->lsf) {
2002 main_data_begin = get_bits(&s->gb, 8);
2003 if (s->nb_channels == 2)
2004 private_bits = get_bits(&s->gb, 2);
2005 else
2006 private_bits = get_bits(&s->gb, 1);
2007 nb_granules = 1;
2008 } else {
2009 main_data_begin = get_bits(&s->gb, 9);
2010 if (s->nb_channels == 2)
2011 private_bits = get_bits(&s->gb, 3);
2012 else
2013 private_bits = get_bits(&s->gb, 5);
2014 nb_granules = 2;
2015 for(ch=0;ch<s->nb_channels;ch++) {
2016 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2017 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2018 }
2019 }
2020
2021 for(gr=0;gr<nb_granules;gr++) {
2022 for(ch=0;ch<s->nb_channels;ch++) {
2023 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2024 g = &granules[ch][gr];
2025 g->part2_3_length = get_bits(&s->gb, 12);
2026 g->big_values = get_bits(&s->gb, 9);
2027 g->global_gain = get_bits(&s->gb, 8);
2028 /* if MS stereo only is selected, we precompute the
2029 1/sqrt(2) renormalization factor */
2030 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2031 MODE_EXT_MS_STEREO)
2032 g->global_gain -= 2;
2033 if (s->lsf)
2034 g->scalefac_compress = get_bits(&s->gb, 9);
2035 else
2036 g->scalefac_compress = get_bits(&s->gb, 4);
2037 blocksplit_flag = get_bits(&s->gb, 1);
2038 if (blocksplit_flag) {
2039 g->block_type = get_bits(&s->gb, 2);
2040 if (g->block_type == 0)
2041 return -1;
2042 g->switch_point = get_bits(&s->gb, 1);
2043 for(i=0;i<2;i++)
2044 g->table_select[i] = get_bits(&s->gb, 5);
2045 for(i=0;i<3;i++)
2046 g->subblock_gain[i] = get_bits(&s->gb, 3);
2047 /* compute huffman coded region sizes */
2048 if (g->block_type == 2)
2049 g->region_size[0] = (36 / 2);
2050 else {
2051 if (s->sample_rate_index <= 2)
2052 g->region_size[0] = (36 / 2);
2053 else if (s->sample_rate_index != 8)
2054 g->region_size[0] = (54 / 2);
2055 else
2056 g->region_size[0] = (108 / 2);
2057 }
2058 g->region_size[1] = (576 / 2);
2059 } else {
2060 int region_address1, region_address2, l;
2061 g->block_type = 0;
2062 g->switch_point = 0;
2063 for(i=0;i<3;i++)
2064 g->table_select[i] = get_bits(&s->gb, 5);
2065 /* compute huffman coded region sizes */
2066 region_address1 = get_bits(&s->gb, 4);
2067 region_address2 = get_bits(&s->gb, 3);
2068 dprintf("region1=%d region2=%d\n",
2069 region_address1, region_address2);
2070 g->region_size[0] =
2071 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2072 l = region_address1 + region_address2 + 2;
2073 /* should not overflow */
2074 if (l > 22)
2075 l = 22;
2076 g->region_size[1] =
2077 band_index_long[s->sample_rate_index][l] >> 1;
2078 }
2079 /* convert region offsets to region sizes and truncate
2080 size to big_values */
2081 g->region_size[2] = (576 / 2);
2082 j = 0;
2083 for(i=0;i<3;i++) {
2084 k = g->region_size[i];
2085 if (k > g->big_values)
2086 k = g->big_values;
2087 g->region_size[i] = k - j;
2088 j = k;
2089 }
2090
2091 /* compute band indexes */
2092 if (g->block_type == 2) {
2093 if (g->switch_point) {
2094 /* if switched mode, we handle the 36 first samples as
2095 long blocks. For 8000Hz, we handle the 48 first
2096 exponents as long blocks (XXX: check this!) */
2097 if (s->sample_rate_index <= 2)
2098 g->long_end = 8;
2099 else if (s->sample_rate_index != 8)
2100 g->long_end = 6;
2101 else
2102 g->long_end = 4; /* 8000 Hz */
2103
2104 if (s->sample_rate_index != 8)
2105 g->short_start = 3;
2106 else
2107 g->short_start = 2;
2108 } else {
2109 g->long_end = 0;
2110 g->short_start = 0;
2111 }
2112 } else {
2113 g->short_start = 13;
2114 g->long_end = 22;
2115 }
2116
2117 g->preflag = 0;
2118 if (!s->lsf)
2119 g->preflag = get_bits(&s->gb, 1);
2120 g->scalefac_scale = get_bits(&s->gb, 1);
2121 g->count1table_select = get_bits(&s->gb, 1);
2122 dprintf("block_type=%d switch_point=%d\n",
2123 g->block_type, g->switch_point);
2124 }
2125 }
2126
2127 /* now we get bits from the main_data_begin offset */
2128 dprintf("seekback: %d\n", main_data_begin);
2129 seek_to_maindata(s, main_data_begin);
2130
2131 for(gr=0;gr<nb_granules;gr++) {
2132 for(ch=0;ch<s->nb_channels;ch++) {
2133 g = &granules[ch][gr];
2134
2135 bits_pos = get_bits_count(&s->gb);
2136
2137 if (!s->lsf) {
2138 UINT8 *sc;
2139 int slen, slen1, slen2;
2140
2141 /* MPEG1 scale factors */
2142 slen1 = slen_table[0][g->scalefac_compress];
2143 slen2 = slen_table[1][g->scalefac_compress];
2144 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2145 if (g->block_type == 2) {
2146 n = g->switch_point ? 17 : 18;
2147 j = 0;
2148 for(i=0;i<n;i++)
2149 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2150 for(i=0;i<18;i++)
2151 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2152 for(i=0;i<3;i++)
2153 g->scale_factors[j++] = 0;
2154 } else {
2155 sc = granules[ch][0].scale_factors;
2156 j = 0;
2157 for(k=0;k<4;k++) {
2158 n = (k == 0 ? 6 : 5);
2159 if ((g->scfsi & (0x8 >> k)) == 0) {
2160 slen = (k < 2) ? slen1 : slen2;
2161 for(i=0;i<n;i++)
2162 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2163 } else {
2164 /* simply copy from last granule */
2165 for(i=0;i<n;i++) {
2166 g->scale_factors[j] = sc[j];
2167 j++;
2168 }
2169 }
2170 }
2171 g->scale_factors[j++] = 0;
2172 }
747a67fb 2173#if defined(DEBUG)
239c2f4c
FB
2174 {
2175 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2176 g->scfsi, gr, ch);
2177 for(i=0;i<j;i++)
2178 printf(" %d", g->scale_factors[i]);
2179 printf("\n");
2180 }
2181#endif
2182 } else {
2183 int tindex, tindex2, slen[4], sl, sf;
2184
2185 /* LSF scale factors */
2186 if (g->block_type == 2) {
2187 tindex = g->switch_point ? 2 : 1;
2188 } else {
2189 tindex = 0;
2190 }
2191 sf = g->scalefac_compress;
2192 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2193 /* intensity stereo case */
2194 sf >>= 1;
2195 if (sf < 180) {
2196 lsf_sf_expand(slen, sf, 6, 6, 0);
2197 tindex2 = 3;
2198 } else if (sf < 244) {
2199 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2200 tindex2 = 4;
2201 } else {
2202 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2203 tindex2 = 5;
2204 }
2205 } else {
2206 /* normal case */
2207 if (sf < 400) {
2208 lsf_sf_expand(slen, sf, 5, 4, 4);
2209 tindex2 = 0;
2210 } else if (sf < 500) {
2211 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2212 tindex2 = 1;
2213 } else {
2214 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2215 tindex2 = 2;
2216 g->preflag = 1;
2217 }
2218 }
2219
2220 j = 0;
2221 for(k=0;k<4;k++) {
2222 n = lsf_nsf_table[tindex2][tindex][k];
2223 sl = slen[k];
2224 for(i=0;i<n;i++)
2225 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2226 }
2227 /* XXX: should compute exact size */
2228 for(;j<40;j++)
2229 g->scale_factors[j] = 0;
747a67fb 2230#if defined(DEBUG)
239c2f4c
FB
2231 {
2232 printf("gr=%d ch=%d scale_factors:\n",
2233 gr, ch);
2234 for(i=0;i<40;i++)
2235 printf(" %d", g->scale_factors[i]);
2236 printf("\n");
2237 }
2238#endif
2239 }
2240
2241 exponents_from_scale_factors(s, g, exponents);
2242
2243 /* read Huffman coded residue */
2244 if (huffman_decode(s, g, exponents,
2245 bits_pos + g->part2_3_length) < 0)
2246 return -1;
747a67fb
FB
2247#if defined(DEBUG)
2248 sample_dump(0, g->sb_hybrid, 576);
239c2f4c
FB
2249#endif
2250
2251 /* skip extension bits */
2252 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2253 if (bits_left < 0) {
2254 dprintf("bits_left=%d\n", bits_left);
2255 return -1;
2256 }
2257 while (bits_left >= 16) {
2258 skip_bits(&s->gb, 16);
2259 bits_left -= 16;
2260 }
2261 if (bits_left > 0)
2262 skip_bits(&s->gb, bits_left);
2263 } /* ch */
2264
2265 if (s->nb_channels == 2)
2266 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2267
2268 for(ch=0;ch<s->nb_channels;ch++) {
2269 g = &granules[ch][gr];
2270
2271 reorder_block(s, g);
747a67fb 2272#if defined(DEBUG)
239c2f4c
FB
2273 sample_dump(0, g->sb_hybrid, 576);
2274#endif
2275 compute_antialias(s, g);
2276#ifdef DEBUG
2277 sample_dump(1, g->sb_hybrid, 576);
2278#endif
2279 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2280#ifdef DEBUG
2281 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2282#endif
2283 }
2284 } /* gr */
2285 return nb_granules * 18;
2286}
2287
2288static int mp_decode_frame(MPADecodeContext *s,
2289 short *samples)
2290{
2291 int i, nb_frames, ch;
2292 short *samples_ptr;
2293
2294 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2295 s->inbuf_ptr - s->inbuf - HEADER_SIZE);
2296
2297 /* skip error protection field */
2298 if (s->error_protection)
2299 get_bits(&s->gb, 16);
2300
2301 dprintf("frame %d:\n", s->frame_count);
2302 switch(s->layer) {
2303 case 1:
2304 nb_frames = mp_decode_layer1(s);
2305 break;
2306 case 2:
2307 nb_frames = mp_decode_layer2(s);
2308 break;
2309 case 3:
2310 default:
2311 nb_frames = mp_decode_layer3(s);
2312 break;
2313 }
2314#if defined(DEBUG)
2315 for(i=0;i<nb_frames;i++) {
2316 for(ch=0;ch<s->nb_channels;ch++) {
2317 int j;
2318 printf("%d-%d:", i, ch);
2319 for(j=0;j<SBLIMIT;j++)
2320 printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2321 printf("\n");
2322 }
2323 }
2324#endif
2325 /* apply the synthesis filter */
2326 for(ch=0;ch<s->nb_channels;ch++) {
2327 samples_ptr = samples + ch;
2328 for(i=0;i<nb_frames;i++) {
2329 synth_filter(s, ch, samples_ptr, s->nb_channels,
2330 s->sb_samples[ch][i]);
2331 samples_ptr += 32 * s->nb_channels;
2332 }
2333 }
2334#ifdef DEBUG
2335 s->frame_count++;
2336#endif
2337 return nb_frames * 32 * sizeof(short) * s->nb_channels;
2338}
2339
de6d9b64
FB
2340static int decode_frame(AVCodecContext * avctx,
2341 void *data, int *data_size,
2342 UINT8 * buf, int buf_size)
2343{
2344 MPADecodeContext *s = avctx->priv_data;
2345 UINT32 header;
2346 UINT8 *buf_ptr;
2347 int len, out_size;
2348 short *out_samples = data;
2349
2350 *data_size = 0;
2351 buf_ptr = buf;
2352 while (buf_size > 0) {
2353 len = s->inbuf_ptr - s->inbuf;
2354 if (s->frame_size == 0) {
239c2f4c
FB
2355 /* special case for next header for first frame in free
2356 format case (XXX: find a simpler method) */
2357 if (s->free_format_next_header != 0) {
2358 s->inbuf[0] = s->free_format_next_header >> 24;
2359 s->inbuf[1] = s->free_format_next_header >> 16;
2360 s->inbuf[2] = s->free_format_next_header >> 8;
2361 s->inbuf[3] = s->free_format_next_header;
2362 s->inbuf_ptr = s->inbuf + 4;
2363 s->free_format_next_header = 0;
2364 goto got_header;
2365 }
2366 /* no header seen : find one. We need at least HEADER_SIZE
2367 bytes to parse it */
de6d9b64
FB
2368 len = HEADER_SIZE - len;
2369 if (len > buf_size)
2370 len = buf_size;
92d24f49 2371 if (len > 0) {
2d83f323
ZK
2372 memcpy(s->inbuf_ptr, buf_ptr, len);
2373 buf_ptr += len;
2374 buf_size -= len;
c152c983
ZK
2375 s->inbuf_ptr += len;
2376 }
2377 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
239c2f4c 2378 got_header:
de6d9b64
FB
2379 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2380 (s->inbuf[2] << 8) | s->inbuf[3];
92d24f49 2381
de6d9b64
FB
2382 if (check_header(header) < 0) {
2383 /* no sync found : move by one byte (inefficient, but simple!) */
2d83f323 2384 memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
de6d9b64 2385 s->inbuf_ptr--;
239c2f4c
FB
2386 dprintf("skip %x\n", header);
2387 /* reset free format frame size to give a chance
2388 to get a new bitrate */
2389 s->free_format_frame_size = 0;
de6d9b64 2390 } else {
239c2f4c
FB
2391 if (decode_header(s, header) == 1) {
2392 /* free format: compute frame size */
2d83f323
ZK
2393 s->frame_size = -1;
2394 memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
de5123dc 2395 s->inbuf_ptr--;
239c2f4c
FB
2396 } else {
2397 /* update codec info */
2398 avctx->sample_rate = s->sample_rate;
2399 avctx->channels = s->nb_channels;
2d83f323 2400 avctx->bit_rate = s->bit_rate;
10bb7023 2401 avctx->frame_size = s->frame_size;
239c2f4c 2402 }
de6d9b64
FB
2403 }
2404 }
239c2f4c
FB
2405 } else if (s->frame_size == -1) {
2406 /* free format : find next sync to compute frame size */
2407 len = MPA_MAX_CODED_FRAME_SIZE - len;
2408 if (len > buf_size)
2409 len = buf_size;
2410 if (len == 0) {
2411 /* frame too long: resync */
2412 s->frame_size = 0;
2413 } else {
2414 UINT8 *p, *pend;
2415 UINT32 header1;
2416 int padding;
2417
2418 memcpy(s->inbuf_ptr, buf_ptr, len);
2419 /* check for header */
2420 p = s->inbuf_ptr - 3;
2421 pend = s->inbuf_ptr + len - 4;
2422 while (p <= pend) {
2423 header = (p[0] << 24) | (p[1] << 16) |
2424 (p[2] << 8) | p[3];
2425 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2426 (s->inbuf[2] << 8) | s->inbuf[3];
2427 /* check with high probability that we have a
2428 valid header */
2429 if ((header & SAME_HEADER_MASK) ==
2430 (header1 & SAME_HEADER_MASK)) {
2431 /* header found: update pointers */
2432 len = (p + 4) - s->inbuf_ptr;
2433 buf_ptr += len;
2434 buf_size -= len;
2435 s->inbuf_ptr = p;
2436 /* compute frame size */
2437 s->free_format_next_header = header;
2438 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2439 padding = (header1 >> 9) & 1;
2440 if (s->layer == 1)
2441 s->free_format_frame_size -= padding * 4;
2442 else
2443 s->free_format_frame_size -= padding;
2444 dprintf("free frame size=%d padding=%d\n",
2445 s->free_format_frame_size, padding);
2446 decode_header(s, header1);
2447 goto next_data;
2448 }
2449 p++;
2450 }
2451 /* not found: simply increase pointers */
2452 buf_ptr += len;
2453 s->inbuf_ptr += len;
2454 buf_size -= len;
2455 }
de6d9b64 2456 } else if (len < s->frame_size) {
de5123dc
ZK
2457 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2458 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
de6d9b64
FB
2459 len = s->frame_size - len;
2460 if (len > buf_size)
2461 len = buf_size;
de6d9b64
FB
2462 memcpy(s->inbuf_ptr, buf_ptr, len);
2463 buf_ptr += len;
2464 s->inbuf_ptr += len;
2465 buf_size -= len;
2466 } else {
2467 out_size = mp_decode_frame(s, out_samples);
2468 s->inbuf_ptr = s->inbuf;
2469 s->frame_size = 0;
2470 *data_size = out_size;
2471 break;
2472 }
239c2f4c 2473 next_data:
de6d9b64
FB
2474 }
2475 return buf_ptr - buf;
2476}
2477
4b1f4f23 2478AVCodec mp2_decoder =
de6d9b64 2479{
4b1f4f23 2480 "mp2",
de6d9b64
FB
2481 CODEC_TYPE_AUDIO,
2482 CODEC_ID_MP2,
2483 sizeof(MPADecodeContext),
2484 decode_init,
2485 NULL,
2486 NULL,
2487 decode_frame,
2488};
4b1f4f23
J
2489
2490AVCodec mp3_decoder =
2491{
2492 "mp3",
2493 CODEC_TYPE_AUDIO,
2494 CODEC_ID_MP3LAME,
2495 sizeof(MPADecodeContext),
2496 decode_init,
2497 NULL,
2498 NULL,
2499 decode_frame,
2500};
cd4af68a
ZK
2501
2502#undef C1
2503#undef C2
2504#undef C3
2505#undef C4
2506#undef C5
2507#undef C6
2508#undef C7
2509#undef C8
2510#undef FRAC_BITS
2511#undef HEADER_SIZE