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