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