r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / quicktime / libavcodec / mpegaudiodec.c
blobb2c0966aa08bf558afc18d099d475dc1dfdedefa
1 /*
2 * MPEG Audio decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //#define DEBUG
20 #include "avcodec.h"
21 #include "mpegaudio.h"
24 * TODO:
25 * - in low precision mode, use more 16 bit multiplies in synth filter
26 * - test lsf / mpeg25 extensively.
29 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
30 audio decoder */
31 #ifdef CONFIG_MPEGAUDIO_HP
32 #define USE_HIGHPRECISION
33 #endif
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
43 #define FRAC_ONE (1 << FRAC_BITS)
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)
52 #if FRAC_BITS <= 15
53 typedef INT16 MPA_INT;
54 #else
55 typedef INT32 MPA_INT;
56 #endif
58 /****************/
60 #define HEADER_SIZE 4
61 #define BACKSTEP_SIZE 512
63 typedef struct MPADecodeContext {
64 UINT8 inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
65 int inbuf_index;
66 UINT8 *inbuf_ptr, *inbuf;
67 int frame_size;
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;
72 int error_protection;
73 int layer;
74 int sample_rate;
75 int sample_rate_index; /* between 0 and 8 */
76 int bit_rate;
77 int old_frame_size;
78 GetBitContext gb;
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
90 } MPADecodeContext;
92 /* layer 3 "granule" */
93 typedef 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;
112 #define MODE_EXT_MS_STEREO 2
113 #define MODE_EXT_I_STEREO 1
115 /* layer 3 huffman tables */
116 typedef struct HuffTable {
117 int xsize;
118 const UINT8 *bits;
119 const UINT16 *codes;
120 } HuffTable;
122 #include "mpegaudiodectab.h"
124 /* vlc structure for decoding layer 3 huffman tables */
125 static VLC huff_vlc[16];
126 static UINT8 *huff_code_table[16];
127 static VLC huff_quad_vlc[2];
128 /* computed from band_size_long */
129 static UINT16 band_index_long[9][23];
130 /* XXX: free when all decoders are closed */
131 #define TABLE_4_3_SIZE (8191 + 16)
132 static INT8 *table_4_3_exp;
133 #if FRAC_BITS <= 15
134 static UINT16 *table_4_3_value;
135 #else
136 static UINT32 *table_4_3_value;
137 #endif
138 /* intensity stereo coef table */
139 static INT32 is_table[2][16];
140 static INT32 is_table_lsf[2][2][16];
141 static INT32 csa_table[8][2];
142 static INT32 mdct_win[8][36];
144 /* lower 2 bits: modulo 3, higher bits: shift */
145 static UINT16 scale_factor_modshift[64];
146 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
147 static INT32 scale_factor_mult[15][3];
148 /* mult table for layer 2 group quantization */
150 #define SCALE_GEN(v) \
151 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
153 static INT32 scale_factor_mult2[3][3] = {
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 */
159 /* 2^(n/4) */
160 static UINT32 scale_factor_mult3[4] = {
161 FIXR(1.0),
162 FIXR(1.18920711500272106671),
163 FIXR(1.41421356237309504880),
164 FIXR(1.68179283050742908605),
167 static MPA_INT window[512];
169 /* layer 1 unscaling */
170 /* n = number of bits of the mantissa minus 1 */
171 static inline int l1_unscale(int n, int mant, int scale_factor)
173 int shift, mod;
174 INT64 val;
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;
181 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
182 return (int)((val + (1LL << (shift - 1))) >> shift);
185 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
187 int shift, mod, val;
189 shift = scale_factor_modshift[scale_factor];
190 mod = shift & 3;
191 shift >>= 2;
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;
200 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
201 static inline int l3_unscale(int value, int exponent)
203 #if FRAC_BITS <= 15
204 unsigned int m;
205 #else
206 UINT64 m;
207 #endif
208 int e;
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
229 /* all integer n^(4/3) computation code */
230 #define DEV_ORDER 13
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)
237 static int dev_4_3_coefs[DEV_ORDER];
239 static int pow_mult3[3] = {
240 POW_FIX(1.0),
241 POW_FIX(1.25992104989487316476),
242 POW_FIX(1.58740105196819947474),
245 static void int_pow_init(void)
247 int i, a;
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;
256 /* return the mantissa and the binary exponent */
257 static int int_pow(int i, int *exp_ptr)
259 int e, er, eq, j;
260 int a, a1;
262 /* renormalize */
263 a = i;
264 e = POW_FRAC_BITS;
265 while (a < (1 << (POW_FRAC_BITS - 1))) {
266 a = a << 1;
267 e--;
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++;
283 /* convert to float */
284 while (a < POW_FRAC_ONE) {
285 a = a << 1;
286 eq--;
288 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
289 #if POW_FRAC_BITS > FRAC_BITS
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++;
296 #endif
297 *exp_ptr = eq;
298 return a;
301 static int decode_init(AVCodecContext * avctx)
303 MPADecodeContext *s = avctx->priv_data;
304 static int init=0;
305 int i, j, k;
307 if(!init) {
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 */
312 shift = (i / 3);
313 mod = i % 3;
314 scale_factor_modshift[i] = mod | (shift << 2);
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);
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);
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]);
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;
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];
351 int xsize, n, x, y;
352 UINT8 *code_table;
354 xsize = h->xsize;
355 n = xsize * xsize;
356 /* XXX: fail test */
357 init_vlc(&huff_vlc[i], 8, n,
358 h->bits, 1, 1, h->codes, 2, 2);
360 code_table = av_mallocz(n);
361 j = 0;
362 for(x=0;x<xsize;x++) {
363 for(y=0;y<xsize;y++)
364 code_table[j++] = (x << 4) | y;
366 huff_code_table[i] = code_table;
368 for(i=0;i<2;i++) {
369 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
370 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1);
373 for(i=0;i<9;i++) {
374 k = 0;
375 for(j=0;j<22;j++) {
376 band_index_long[i][j] = k;
377 k += band_size_long[i][j];
379 band_index_long[i][22] = k;
382 /* compute n ^ (4/3) and store it in mantissa/exp format */
383 if (!av_mallocz_static(&table_4_3_exp,
384 TABLE_4_3_SIZE * sizeof(table_4_3_exp[0])))
385 return -1;
386 if (!av_mallocz_static(&table_4_3_value,
387 TABLE_4_3_SIZE * sizeof(table_4_3_value[0])))
388 return -1;
390 int_pow_init();
391 for(i=1;i<TABLE_4_3_SIZE;i++) {
392 int e, m;
393 m = int_pow(i, &e);
394 #if 0
395 /* test code */
397 double f, fm;
398 int e1, m1;
399 f = pow((double)i, 4.0 / 3.0);
400 fm = frexp(f, &e1);
401 m1 = FIXR(2 * fm);
402 #if FRAC_BITS <= 15
403 if ((unsigned short)m1 != m1) {
404 m1 = m1 >> 1;
405 e1++;
407 #endif
408 e1--;
409 if (m != m1 || e != e1) {
410 printf("%4d: m=%x m1=%x e=%d e1=%d\n",
411 i, m, m1, e, e1);
414 #endif
415 /* normalized to FRAC_BITS */
416 table_4_3_value[i] = m;
417 table_4_3_exp[i] = e;
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);
429 is_table[0][i] = v;
430 is_table[1][6 - i] = v;
432 /* invalid values */
433 for(i=7;i<16;i++)
434 is_table[0][i] = is_table[1][i] = 0.0;
436 for(i=0;i<16;i++) {
437 double f;
438 int e, k;
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]);
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);
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;
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);
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);
478 for(i=0;i<12;i++)
479 mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
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];
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");
497 #endif
498 init = 1;
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;
510 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */;
512 /* cos(i*pi/64) */
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)
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)
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)
545 #define COS3_0 FIXR(0.54119610014619698439)
546 #define COS3_1 FIXR(1.30656296487637652785)
548 #define COS4_0 FIXR(0.70710678118654752439)
550 /* butterfly operator */
551 #define BF(a, b, c)\
553 tmp0 = tab[a] + tab[b];\
554 tmp1 = tab[a] - tab[b];\
555 tab[a] = tmp0;\
556 tab[b] = MULL(tmp1, c);\
559 #define BF1(a, b, c, d)\
561 BF(a, b, COS4_0);\
562 BF(c, d, -COS4_0);\
563 tab[c] += tab[d];\
566 #define BF2(a, b, c, d)\
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];\
576 #define ADD(a, b) tab[a] += tab[b]
578 /* DCT32 without 1/sqrt(2) coef zero scaling. */
579 static void dct32(INT32 *out, INT32 *tab)
581 int tmp0, tmp1;
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);
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);
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);
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);
626 BF(8, 15, -COS2_0);
627 BF(9, 14, -COS2_1);
628 BF(10, 13, -COS2_2);
629 BF(11, 12, -COS2_3);
631 BF(16, 23, COS2_0);
632 BF(17, 22, COS2_1);
633 BF(18, 21, COS2_2);
634 BF(19, 20, COS2_3);
636 BF(24, 31, -COS2_0);
637 BF(25, 30, -COS2_1);
638 BF(26, 29, -COS2_2);
639 BF(27, 28, -COS2_3);
641 /* pass 4 */
642 BF(0, 3, COS3_0);
643 BF(1, 2, COS3_1);
645 BF(4, 7, -COS3_0);
646 BF(5, 6, -COS3_1);
648 BF(8, 11, COS3_0);
649 BF(9, 10, COS3_1);
651 BF(12, 15, -COS3_0);
652 BF(13, 14, -COS3_1);
654 BF(16, 19, COS3_0);
655 BF(17, 18, COS3_1);
657 BF(20, 23, -COS3_0);
658 BF(21, 22, -COS3_1);
660 BF(24, 27, COS3_0);
661 BF(25, 26, COS3_1);
663 BF(28, 31, -COS3_0);
664 BF(29, 30, -COS3_1);
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);
676 /* pass 6 */
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);
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];
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);
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];
729 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
731 #if FRAC_BITS <= 15
733 #define OUT_SAMPLE(sum)\
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;\
745 #define SUM8(off, op) \
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];\
757 #else
759 #define OUT_SAMPLE(sum)\
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;\
771 #define SUM8(off, op) \
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]);\
783 #endif
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])
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
802 dct32(tmp, sb_samples);
804 offset = s1->synth_buf_offset[ch];
805 synth_buf = s1->synth_buf[ch] + offset;
807 for(j=0;j<32;j++) {
808 v = tmp[j];
809 #if FRAC_BITS <= 15
810 /* NOTE: can cause a loss in precision if very high amplitude
811 sound */
812 if (v > 32767)
813 v = 32767;
814 else if (v < -32768)
815 v = -32768;
816 #endif
817 synth_buf[j] = v;
819 /* copy to avoid wrap */
820 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
822 w = window;
823 for(j=0;j<16;j++) {
824 sum = 0;
825 p = synth_buf + 16 + j; /* 0-15 */
826 SUM8(0, +=);
827 p = synth_buf + 48 - j; /* 32-47 */
828 SUM8(32, -=);
829 OUT_SAMPLE(sum);
830 w++;
833 p = synth_buf + 32; /* 48 */
834 sum = 0;
835 SUM8(32, -=);
836 OUT_SAMPLE(sum);
837 w++;
839 for(j=17;j<32;j++) {
840 sum = 0;
841 p = synth_buf + 48 - j; /* 17-31 */
842 SUM8(0, -=);
843 p = synth_buf + 16 + j; /* 49-63 */
844 SUM8(32, -=);
845 OUT_SAMPLE(sum);
846 w++;
848 offset = (offset - 32) & 511;
849 s1->synth_buf_offset[ch] = offset;
852 /* cos(pi*i/24) */
853 #define C1 FIXR(0.99144486137381041114)
854 #define C3 FIXR(0.92387953251128675612)
855 #define C5 FIXR(0.79335334029123516458)
856 #define C7 FIXR(0.60876142900872063941)
857 #define C9 FIXR(0.38268343236508977173)
858 #define C11 FIXR(0.13052619222005159154)
860 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
861 cases. */
862 static void imdct12(int *out, int *in)
864 int tmp;
865 INT64 in1_3, in1_9, in4_3, in4_9;
867 in1_3 = MUL64(in[1], C3);
868 in1_9 = MUL64(in[1], C9);
869 in4_3 = MUL64(in[4], C3);
870 in4_9 = MUL64(in[4], C9);
872 tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) +
873 MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
874 out[0] = tmp;
875 out[5] = -tmp;
876 tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 +
877 MUL64(in[2] + in[5], C3) - in4_9);
878 out[1] = tmp;
879 out[4] = -tmp;
880 tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
881 MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
882 out[2] = tmp;
883 out[3] = -tmp;
884 tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) +
885 MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
886 out[6] = tmp;
887 out[11] = tmp;
888 tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 +
889 MUL64(in[2] + in[5], C9) + in4_3);
890 out[7] = tmp;
891 out[10] = tmp;
892 tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
893 MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
894 out[8] = tmp;
895 out[9] = tmp;
898 #undef C1
899 #undef C3
900 #undef C5
901 #undef C7
902 #undef C9
903 #undef C11
905 /* cos(pi*i/18) */
906 #define C1 FIXR(0.98480775301220805936)
907 #define C2 FIXR(0.93969262078590838405)
908 #define C3 FIXR(0.86602540378443864676)
909 #define C4 FIXR(0.76604444311897803520)
910 #define C5 FIXR(0.64278760968653932632)
911 #define C6 FIXR(0.5)
912 #define C7 FIXR(0.34202014332566873304)
913 #define C8 FIXR(0.17364817766693034885)
915 /* 0.5 / cos(pi*(2*i+1)/36) */
916 static const int icos36[9] = {
917 FIXR(0.50190991877167369479),
918 FIXR(0.51763809020504152469),
919 FIXR(0.55168895948124587824),
920 FIXR(0.61038729438072803416),
921 FIXR(0.70710678118654752439),
922 FIXR(0.87172339781054900991),
923 FIXR(1.18310079157624925896),
924 FIXR(1.93185165257813657349),
925 FIXR(5.73685662283492756461),
928 static const int icos72[18] = {
929 /* 0.5 / cos(pi*(2*i+19)/72) */
930 FIXR(0.74009361646113053152),
931 FIXR(0.82133981585229078570),
932 FIXR(0.93057949835178895673),
933 FIXR(1.08284028510010010928),
934 FIXR(1.30656296487637652785),
935 FIXR(1.66275476171152078719),
936 FIXR(2.31011315767264929558),
937 FIXR(3.83064878777019433457),
938 FIXR(11.46279281302667383546),
940 /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
941 FIXR(-0.67817085245462840086),
942 FIXR(-0.63023620700513223342),
943 FIXR(-0.59284452371708034528),
944 FIXR(-0.56369097343317117734),
945 FIXR(-0.54119610014619698439),
946 FIXR(-0.52426456257040533932),
947 FIXR(-0.51213975715725461845),
948 FIXR(-0.50431448029007636036),
949 FIXR(-0.50047634258165998492),
952 /* using Lee like decomposition followed by hand coded 9 points DCT */
953 static void imdct36(int *out, int *in)
955 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
956 int tmp[18], *tmp1, *in1;
957 INT64 in3_3, in6_6;
959 for(i=17;i>=1;i--)
960 in[i] += in[i-1];
961 for(i=17;i>=3;i-=2)
962 in[i] += in[i-2];
964 for(j=0;j<2;j++) {
965 tmp1 = tmp + j;
966 in1 = in + j;
968 in3_3 = MUL64(in1[2*3], C3);
969 in6_6 = MUL64(in1[2*6], C6);
971 tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 +
972 MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
973 tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) +
974 MUL64(in1[2*4], C4) + in6_6 +
975 MUL64(in1[2*8], C8));
976 tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
977 tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) -
978 in1[2*6] + in1[2*0];
979 tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 -
980 MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
981 tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) -
982 MUL64(in1[2*4], C2) + in6_6 +
983 MUL64(in1[2*8], C4));
984 tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 +
985 MUL64(in1[2*5], C1) -
986 MUL64(in1[2*7], C5));
987 tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) +
988 MUL64(in1[2*4], C8) + in6_6 -
989 MUL64(in1[2*8], C2));
990 tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
993 i = 0;
994 for(j=0;j<4;j++) {
995 t0 = tmp[i];
996 t1 = tmp[i + 2];
997 s0 = t1 + t0;
998 s2 = t1 - t0;
1000 t2 = tmp[i + 1];
1001 t3 = tmp[i + 3];
1002 s1 = MULL(t3 + t2, icos36[j]);
1003 s3 = MULL(t3 - t2, icos36[8 - j]);
1005 t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1006 t1 = MULL(s0 - s1, icos72[8 - j]);
1007 out[18 + 9 + j] = t0;
1008 out[18 + 8 - j] = t0;
1009 out[9 + j] = -t1;
1010 out[8 - j] = t1;
1012 t0 = MULL(s2 + s3, icos72[9+j]);
1013 t1 = MULL(s2 - s3, icos72[j]);
1014 out[18 + 9 + (8 - j)] = t0;
1015 out[18 + j] = t0;
1016 out[9 + (8 - j)] = -t1;
1017 out[j] = t1;
1018 i += 4;
1021 s0 = tmp[16];
1022 s1 = MULL(tmp[17], icos36[4]);
1023 t0 = MULL(s0 + s1, icos72[9 + 4]);
1024 t1 = MULL(s0 - s1, icos72[4]);
1025 out[18 + 9 + 4] = t0;
1026 out[18 + 8 - 4] = t0;
1027 out[9 + 4] = -t1;
1028 out[8 - 4] = t1;
1031 /* fast header check for resync */
1032 static int check_header(UINT32 header)
1034 /* header */
1035 if ((header & 0xffe00000) != 0xffe00000)
1036 return -1;
1037 /* layer check */
1038 if (((header >> 17) & 3) == 0)
1039 return -1;
1040 /* bit rate */
1041 if (((header >> 12) & 0xf) == 0xf)
1042 return -1;
1043 /* frequency */
1044 if (((header >> 10) & 3) == 3)
1045 return -1;
1046 return 0;
1049 /* header + layer + bitrate + freq + lsf/mpeg25 */
1050 #define SAME_HEADER_MASK \
1051 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1053 /* header decoding. MUST check the header before because no
1054 consistency check is done there. Return 1 if free format found and
1055 that the frame size must be computed externally */
1056 static int decode_header(MPADecodeContext *s, UINT32 header)
1058 int sample_rate, frame_size, mpeg25, padding;
1059 int sample_rate_index, bitrate_index;
1060 if (header & (1<<20)) {
1061 s->lsf = (header & (1<<19)) ? 0 : 1;
1062 mpeg25 = 0;
1063 } else {
1064 s->lsf = 1;
1065 mpeg25 = 1;
1068 s->layer = 4 - ((header >> 17) & 3);
1069 /* extract frequency */
1070 sample_rate_index = (header >> 10) & 3;
1071 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
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 s->sample_rate = sample_rate;
1077 bitrate_index = (header >> 12) & 0xf;
1078 padding = (header >> 9) & 1;
1079 //extension = (header >> 8) & 1;
1080 s->mode = (header >> 6) & 3;
1081 s->mode_ext = (header >> 4) & 3;
1082 //copyright = (header >> 3) & 1;
1083 //original = (header >> 2) & 1;
1084 //emphasis = header & 3;
1086 if (s->mode == MPA_MONO)
1087 s->nb_channels = 1;
1088 else
1089 s->nb_channels = 2;
1091 if (bitrate_index != 0) {
1092 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1093 s->bit_rate = frame_size * 1000;
1094 switch(s->layer) {
1095 case 1:
1096 frame_size = (frame_size * 12000) / sample_rate;
1097 frame_size = (frame_size + padding) * 4;
1098 break;
1099 case 2:
1100 frame_size = (frame_size * 144000) / sample_rate;
1101 frame_size += padding;
1102 break;
1103 default:
1104 case 3:
1105 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1106 frame_size += padding;
1107 break;
1109 s->frame_size = frame_size;
1110 } else {
1111 /* if no frame size computed, signal it */
1112 if (!s->free_format_frame_size)
1113 return 1;
1114 /* free format: compute bitrate and real frame size from the
1115 frame size we extracted by reading the bitstream */
1116 s->frame_size = s->free_format_frame_size;
1117 switch(s->layer) {
1118 case 1:
1119 s->frame_size += padding * 4;
1120 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1121 break;
1122 case 2:
1123 s->frame_size += padding;
1124 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1125 break;
1126 default:
1127 case 3:
1128 s->frame_size += padding;
1129 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1130 break;
1134 #if defined(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-");
1144 printf("stereo");
1145 } else {
1146 printf("mono");
1148 printf("\n");
1149 #endif
1150 return 0;
1153 /* return the number of decoded frames */
1154 static int mp_decode_layer1(MPADecodeContext *s)
1156 int bound, i, v, n, ch, j, mant;
1157 UINT8 allocation[MPA_MAX_CHANNELS][SBLIMIT];
1158 UINT8 scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1160 if (s->mode == MPA_JSTEREO)
1161 bound = (s->mode_ext + 1) * 4;
1162 else
1163 bound = SBLIMIT;
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);
1171 for(i=bound;i<SBLIMIT;i++) {
1172 allocation[0][i] = get_bits(&s->gb, 4);
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);
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);
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;
1200 s->sb_samples[ch][j][i] = v;
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;
1217 return 12;
1220 /* bitrate is in kb/s */
1221 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1223 int ch_bitrate, table;
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;
1239 return table;
1242 static int mp_decode_layer2(MPADecodeContext *s)
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;
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];
1258 if (s->mode == MPA_JSTEREO)
1259 bound = (s->mode_ext + 1) * 4;
1260 else
1261 bound = sblimit;
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);
1271 j += 1 << bit_alloc_bits;
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;
1281 #ifdef DEBUG
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");
1289 #endif
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);
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;
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(" -");
1341 printf("\n");
1343 #endif
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;
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;
1382 /* next subband in alloc table */
1383 j += 1 << bit_alloc_bits;
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);
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;
1432 /* next subband in alloc table */
1433 j += 1 << bit_alloc_bits;
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;
1445 return 3 * 12;
1449 * Seek back in the stream for backstep bytes (at most 511 bytes)
1451 static void seek_to_maindata(MPADecodeContext *s, long backstep)
1453 UINT8 *ptr;
1455 /* compute current position in stream */
1456 ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1458 /* copy old data before current one */
1459 ptr -= backstep;
1460 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1461 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1462 /* init get bits again */
1463 init_get_bits(&s->gb, ptr, s->frame_size + backstep);
1465 /* prepare next buffer */
1466 s->inbuf_index ^= 1;
1467 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1468 s->old_frame_size = s->frame_size;
1471 static inline void lsf_sf_expand(int *slen,
1472 int sf, int n1, int n2, int n3)
1474 if (n3) {
1475 slen[3] = sf % n3;
1476 sf /= n3;
1477 } else {
1478 slen[3] = 0;
1480 if (n2) {
1481 slen[2] = sf % n2;
1482 sf /= n2;
1483 } else {
1484 slen[2] = 0;
1486 slen[1] = sf % n1;
1487 sf /= n1;
1488 slen[0] = sf;
1491 static void exponents_from_scale_factors(MPADecodeContext *s,
1492 GranuleDef *g,
1493 INT16 *exponents)
1495 const UINT8 *bstab, *pretab;
1496 int len, i, j, k, l, v0, shift, gain, gains[3];
1497 INT16 *exp_ptr;
1499 exp_ptr = exponents;
1500 gain = g->global_gain - 210;
1501 shift = g->scalefac_scale + 1;
1503 bstab = band_size_long[s->sample_rate_index];
1504 pretab = mpa_pretab[g->preflag];
1505 for(i=0;i<g->long_end;i++) {
1506 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1507 len = bstab[i];
1508 for(j=len;j>0;j--)
1509 *exp_ptr++ = v0;
1512 if (g->short_start < 13) {
1513 bstab = band_size_short[s->sample_rate_index];
1514 gains[0] = gain - (g->subblock_gain[0] << 3);
1515 gains[1] = gain - (g->subblock_gain[1] << 3);
1516 gains[2] = gain - (g->subblock_gain[2] << 3);
1517 k = g->long_end;
1518 for(i=g->short_start;i<13;i++) {
1519 len = bstab[i];
1520 for(l=0;l<3;l++) {
1521 v0 = gains[l] - (g->scale_factors[k++] << shift);
1522 for(j=len;j>0;j--)
1523 *exp_ptr++ = v0;
1529 /* handle n = 0 too */
1530 static inline int get_bitsz(GetBitContext *s, int n)
1532 if (n == 0)
1533 return 0;
1534 else
1535 return get_bits(s, n);
1538 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1539 INT16 *exponents, int end_pos)
1541 int s_index;
1542 int linbits, code, x, y, l, v, i, j, k, pos;
1543 GetBitContext last_gb;
1544 VLC *vlc;
1545 UINT8 *code_table;
1547 /* low frequencies (called big values) */
1548 s_index = 0;
1549 for(i=0;i<3;i++) {
1550 j = g->region_size[i];
1551 if (j == 0)
1552 continue;
1553 /* select vlc table */
1554 k = g->table_select[i];
1555 l = mpa_huff_data[k][0];
1556 linbits = mpa_huff_data[k][1];
1557 vlc = &huff_vlc[l];
1558 code_table = huff_code_table[l];
1560 /* read huffcode and compute each couple */
1561 for(;j>0;j--) {
1562 if (get_bits_count(&s->gb) >= end_pos)
1563 break;
1564 if (code_table) {
1565 code = get_vlc(&s->gb, vlc);
1566 if (code < 0)
1567 return -1;
1568 y = code_table[code];
1569 x = y >> 4;
1570 y = y & 0x0f;
1571 } else {
1572 x = 0;
1573 y = 0;
1575 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1576 i, g->region_size[i] - j, x, y, exponents[s_index]);
1577 if (x) {
1578 if (x == 15)
1579 x += get_bitsz(&s->gb, linbits);
1580 v = l3_unscale(x, exponents[s_index]);
1581 if (get_bits1(&s->gb))
1582 v = -v;
1583 } else {
1584 v = 0;
1586 g->sb_hybrid[s_index++] = v;
1587 if (y) {
1588 if (y == 15)
1589 y += get_bitsz(&s->gb, linbits);
1590 v = l3_unscale(y, exponents[s_index]);
1591 if (get_bits1(&s->gb))
1592 v = -v;
1593 } else {
1594 v = 0;
1596 g->sb_hybrid[s_index++] = v;
1600 /* high frequencies */
1601 vlc = &huff_quad_vlc[g->count1table_select];
1602 last_gb.buffer = NULL;
1603 while (s_index <= 572) {
1604 pos = get_bits_count(&s->gb);
1605 if (pos >= end_pos) {
1606 if (pos > end_pos && last_gb.buffer != NULL) {
1607 /* some encoders generate an incorrect size for this
1608 part. We must go back into the data */
1609 s_index -= 4;
1610 s->gb = last_gb;
1612 break;
1614 last_gb= s->gb;
1616 code = get_vlc(&s->gb, vlc);
1617 dprintf("t=%d code=%d\n", g->count1table_select, code);
1618 if (code < 0)
1619 return -1;
1620 for(i=0;i<4;i++) {
1621 if (code & (8 >> i)) {
1622 /* non zero value. Could use a hand coded function for
1623 'one' value */
1624 v = l3_unscale(1, exponents[s_index]);
1625 if(get_bits1(&s->gb))
1626 v = -v;
1627 } else {
1628 v = 0;
1630 g->sb_hybrid[s_index++] = v;
1633 while (s_index < 576)
1634 g->sb_hybrid[s_index++] = 0;
1635 return 0;
1638 /* Reorder short blocks from bitstream order to interleaved order. It
1639 would be faster to do it in parsing, but the code would be far more
1640 complicated */
1641 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1643 int i, j, k, len;
1644 INT32 *ptr, *dst, *ptr1;
1645 INT32 tmp[576];
1647 if (g->block_type != 2)
1648 return;
1650 if (g->switch_point) {
1651 if (s->sample_rate_index != 8) {
1652 ptr = g->sb_hybrid + 36;
1653 } else {
1654 ptr = g->sb_hybrid + 48;
1656 } else {
1657 ptr = g->sb_hybrid;
1660 for(i=g->short_start;i<13;i++) {
1661 len = band_size_short[s->sample_rate_index][i];
1662 ptr1 = ptr;
1663 for(k=0;k<3;k++) {
1664 dst = tmp + k;
1665 for(j=len;j>0;j--) {
1666 *dst = *ptr++;
1667 dst += 3;
1670 memcpy(ptr1, tmp, len * 3 * sizeof(INT32));
1674 #define ISQRT2 FIXR(0.70710678118654752440)
1676 static void compute_stereo(MPADecodeContext *s,
1677 GranuleDef *g0, GranuleDef *g1)
1679 int i, j, k, l;
1680 INT32 v1, v2;
1681 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1682 INT32 (*is_tab)[16];
1683 INT32 *tab0, *tab1;
1684 int non_zero_found_short[3];
1686 /* intensity stereo */
1687 if (s->mode_ext & MODE_EXT_I_STEREO) {
1688 if (!s->lsf) {
1689 is_tab = is_table;
1690 sf_max = 7;
1691 } else {
1692 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1693 sf_max = 16;
1696 tab0 = g0->sb_hybrid + 576;
1697 tab1 = g1->sb_hybrid + 576;
1699 non_zero_found_short[0] = 0;
1700 non_zero_found_short[1] = 0;
1701 non_zero_found_short[2] = 0;
1702 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1703 for(i = 12;i >= g1->short_start;i--) {
1704 /* for last band, use previous scale factor */
1705 if (i != 11)
1706 k -= 3;
1707 len = band_size_short[s->sample_rate_index][i];
1708 for(l=2;l>=0;l--) {
1709 tab0 -= len;
1710 tab1 -= len;
1711 if (!non_zero_found_short[l]) {
1712 /* test if non zero band. if so, stop doing i-stereo */
1713 for(j=0;j<len;j++) {
1714 if (tab1[j] != 0) {
1715 non_zero_found_short[l] = 1;
1716 goto found1;
1719 sf = g1->scale_factors[k + l];
1720 if (sf >= sf_max)
1721 goto found1;
1723 v1 = is_tab[0][sf];
1724 v2 = is_tab[1][sf];
1725 for(j=0;j<len;j++) {
1726 tmp0 = tab0[j];
1727 tab0[j] = MULL(tmp0, v1);
1728 tab1[j] = MULL(tmp0, v2);
1730 } else {
1731 found1:
1732 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1733 /* lower part of the spectrum : do ms stereo
1734 if enabled */
1735 for(j=0;j<len;j++) {
1736 tmp0 = tab0[j];
1737 tmp1 = tab1[j];
1738 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1739 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1746 non_zero_found = non_zero_found_short[0] |
1747 non_zero_found_short[1] |
1748 non_zero_found_short[2];
1750 for(i = g1->long_end - 1;i >= 0;i--) {
1751 len = band_size_long[s->sample_rate_index][i];
1752 tab0 -= len;
1753 tab1 -= len;
1754 /* test if non zero band. if so, stop doing i-stereo */
1755 if (!non_zero_found) {
1756 for(j=0;j<len;j++) {
1757 if (tab1[j] != 0) {
1758 non_zero_found = 1;
1759 goto found2;
1762 /* for last band, use previous scale factor */
1763 k = (i == 21) ? 20 : i;
1764 sf = g1->scale_factors[k];
1765 if (sf >= sf_max)
1766 goto found2;
1767 v1 = is_tab[0][sf];
1768 v2 = is_tab[1][sf];
1769 for(j=0;j<len;j++) {
1770 tmp0 = tab0[j];
1771 tab0[j] = MULL(tmp0, v1);
1772 tab1[j] = MULL(tmp0, v2);
1774 } else {
1775 found2:
1776 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1777 /* lower part of the spectrum : do ms stereo
1778 if enabled */
1779 for(j=0;j<len;j++) {
1780 tmp0 = tab0[j];
1781 tmp1 = tab1[j];
1782 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1783 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1788 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1789 /* ms stereo ONLY */
1790 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1791 global gain */
1792 tab0 = g0->sb_hybrid;
1793 tab1 = g1->sb_hybrid;
1794 for(i=0;i<576;i++) {
1795 tmp0 = tab0[i];
1796 tmp1 = tab1[i];
1797 tab0[i] = tmp0 + tmp1;
1798 tab1[i] = tmp0 - tmp1;
1803 static void compute_antialias(MPADecodeContext *s,
1804 GranuleDef *g)
1806 INT32 *ptr, *p0, *p1, *csa;
1807 int n, tmp0, tmp1, i, j;
1809 /* we antialias only "long" bands */
1810 if (g->block_type == 2) {
1811 if (!g->switch_point)
1812 return;
1813 /* XXX: check this for 8000Hz case */
1814 n = 1;
1815 } else {
1816 n = SBLIMIT - 1;
1819 ptr = g->sb_hybrid + 18;
1820 for(i = n;i > 0;i--) {
1821 p0 = ptr - 1;
1822 p1 = ptr;
1823 csa = &csa_table[0][0];
1824 for(j=0;j<8;j++) {
1825 tmp0 = *p0;
1826 tmp1 = *p1;
1827 *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1828 *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1829 p0--;
1830 p1++;
1831 csa += 2;
1833 ptr += 18;
1837 static void compute_imdct(MPADecodeContext *s,
1838 GranuleDef *g,
1839 INT32 *sb_samples,
1840 INT32 *mdct_buf)
1842 INT32 *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
1843 INT32 in[6];
1844 INT32 out[36];
1845 INT32 out2[12];
1846 int i, j, k, mdct_long_end, v, sblimit;
1848 /* find last non zero block */
1849 ptr = g->sb_hybrid + 576;
1850 ptr1 = g->sb_hybrid + 2 * 18;
1851 while (ptr >= ptr1) {
1852 ptr -= 6;
1853 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1854 if (v != 0)
1855 break;
1857 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1859 if (g->block_type == 2) {
1860 /* XXX: check for 8000 Hz */
1861 if (g->switch_point)
1862 mdct_long_end = 2;
1863 else
1864 mdct_long_end = 0;
1865 } else {
1866 mdct_long_end = sblimit;
1869 buf = mdct_buf;
1870 ptr = g->sb_hybrid;
1871 for(j=0;j<mdct_long_end;j++) {
1872 imdct36(out, ptr);
1873 /* apply window & overlap with previous buffer */
1874 out_ptr = sb_samples + j;
1875 /* select window */
1876 if (g->switch_point && j < 2)
1877 win1 = mdct_win[0];
1878 else
1879 win1 = mdct_win[g->block_type];
1880 /* select frequency inversion */
1881 win = win1 + ((4 * 36) & -(j & 1));
1882 for(i=0;i<18;i++) {
1883 *out_ptr = MULL(out[i], win[i]) + buf[i];
1884 buf[i] = MULL(out[i + 18], win[i + 18]);
1885 out_ptr += SBLIMIT;
1887 ptr += 18;
1888 buf += 18;
1890 for(j=mdct_long_end;j<sblimit;j++) {
1891 for(i=0;i<6;i++) {
1892 out[i] = 0;
1893 out[6 + i] = 0;
1894 out[30+i] = 0;
1896 /* select frequency inversion */
1897 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1898 buf2 = out + 6;
1899 for(k=0;k<3;k++) {
1900 /* reorder input for short mdct */
1901 ptr1 = ptr + k;
1902 for(i=0;i<6;i++) {
1903 in[i] = *ptr1;
1904 ptr1 += 3;
1906 imdct12(out2, in);
1907 /* apply 12 point window and do small overlap */
1908 for(i=0;i<6;i++) {
1909 buf2[i] = MULL(out2[i], win[i]) + buf2[i];
1910 buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
1912 buf2 += 6;
1914 /* overlap */
1915 out_ptr = sb_samples + j;
1916 for(i=0;i<18;i++) {
1917 *out_ptr = out[i] + buf[i];
1918 buf[i] = out[i + 18];
1919 out_ptr += SBLIMIT;
1921 ptr += 18;
1922 buf += 18;
1924 /* zero bands */
1925 for(j=sblimit;j<SBLIMIT;j++) {
1926 /* overlap */
1927 out_ptr = sb_samples + j;
1928 for(i=0;i<18;i++) {
1929 *out_ptr = buf[i];
1930 buf[i] = 0;
1931 out_ptr += SBLIMIT;
1933 buf += 18;
1937 #if defined(DEBUG)
1938 void sample_dump(int fnum, INT32 *tab, int n)
1940 static FILE *files[16], *f;
1941 char buf[512];
1942 int i;
1943 INT32 v;
1945 f = files[fnum];
1946 if (!f) {
1947 sprintf(buf, "/tmp/out%d.%s.pcm",
1948 fnum,
1949 #ifdef USE_HIGHPRECISION
1950 "hp"
1951 #else
1952 "lp"
1953 #endif
1955 f = fopen(buf, "w");
1956 if (!f)
1957 return;
1958 files[fnum] = f;
1961 if (fnum == 0) {
1962 static int pos = 0;
1963 printf("pos=%d\n", pos);
1964 for(i=0;i<n;i++) {
1965 printf(" %0.4f", (double)tab[i] / FRAC_ONE);
1966 if ((i % 18) == 17)
1967 printf("\n");
1969 pos += n;
1971 for(i=0;i<n;i++) {
1972 /* normalize to 23 frac bits */
1973 v = tab[i] << (23 - FRAC_BITS);
1974 fwrite(&v, 1, sizeof(INT32), f);
1977 #endif
1980 /* main layer3 decoding function */
1981 static int mp_decode_layer3(MPADecodeContext *s)
1983 int nb_granules, main_data_begin, private_bits;
1984 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
1985 GranuleDef granules[2][2], *g;
1986 INT16 exponents[576];
1988 /* read side info */
1989 if (s->lsf) {
1990 main_data_begin = get_bits(&s->gb, 8);
1991 if (s->nb_channels == 2)
1992 private_bits = get_bits(&s->gb, 2);
1993 else
1994 private_bits = get_bits(&s->gb, 1);
1995 nb_granules = 1;
1996 } else {
1997 main_data_begin = get_bits(&s->gb, 9);
1998 if (s->nb_channels == 2)
1999 private_bits = get_bits(&s->gb, 3);
2000 else
2001 private_bits = get_bits(&s->gb, 5);
2002 nb_granules = 2;
2003 for(ch=0;ch<s->nb_channels;ch++) {
2004 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2005 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2009 for(gr=0;gr<nb_granules;gr++) {
2010 for(ch=0;ch<s->nb_channels;ch++) {
2011 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2012 g = &granules[ch][gr];
2013 g->part2_3_length = get_bits(&s->gb, 12);
2014 g->big_values = get_bits(&s->gb, 9);
2015 g->global_gain = get_bits(&s->gb, 8);
2016 /* if MS stereo only is selected, we precompute the
2017 1/sqrt(2) renormalization factor */
2018 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2019 MODE_EXT_MS_STEREO)
2020 g->global_gain -= 2;
2021 if (s->lsf)
2022 g->scalefac_compress = get_bits(&s->gb, 9);
2023 else
2024 g->scalefac_compress = get_bits(&s->gb, 4);
2025 blocksplit_flag = get_bits(&s->gb, 1);
2026 if (blocksplit_flag) {
2027 g->block_type = get_bits(&s->gb, 2);
2028 if (g->block_type == 0)
2029 return -1;
2030 g->switch_point = get_bits(&s->gb, 1);
2031 for(i=0;i<2;i++)
2032 g->table_select[i] = get_bits(&s->gb, 5);
2033 for(i=0;i<3;i++)
2034 g->subblock_gain[i] = get_bits(&s->gb, 3);
2035 /* compute huffman coded region sizes */
2036 if (g->block_type == 2)
2037 g->region_size[0] = (36 / 2);
2038 else {
2039 if (s->sample_rate_index <= 2)
2040 g->region_size[0] = (36 / 2);
2041 else if (s->sample_rate_index != 8)
2042 g->region_size[0] = (54 / 2);
2043 else
2044 g->region_size[0] = (108 / 2);
2046 g->region_size[1] = (576 / 2);
2047 } else {
2048 int region_address1, region_address2, l;
2049 g->block_type = 0;
2050 g->switch_point = 0;
2051 for(i=0;i<3;i++)
2052 g->table_select[i] = get_bits(&s->gb, 5);
2053 /* compute huffman coded region sizes */
2054 region_address1 = get_bits(&s->gb, 4);
2055 region_address2 = get_bits(&s->gb, 3);
2056 dprintf("region1=%d region2=%d\n",
2057 region_address1, region_address2);
2058 g->region_size[0] =
2059 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2060 l = region_address1 + region_address2 + 2;
2061 /* should not overflow */
2062 if (l > 22)
2063 l = 22;
2064 g->region_size[1] =
2065 band_index_long[s->sample_rate_index][l] >> 1;
2067 /* convert region offsets to region sizes and truncate
2068 size to big_values */
2069 g->region_size[2] = (576 / 2);
2070 j = 0;
2071 for(i=0;i<3;i++) {
2072 k = g->region_size[i];
2073 if (k > g->big_values)
2074 k = g->big_values;
2075 g->region_size[i] = k - j;
2076 j = k;
2079 /* compute band indexes */
2080 if (g->block_type == 2) {
2081 if (g->switch_point) {
2082 /* if switched mode, we handle the 36 first samples as
2083 long blocks. For 8000Hz, we handle the 48 first
2084 exponents as long blocks (XXX: check this!) */
2085 if (s->sample_rate_index <= 2)
2086 g->long_end = 8;
2087 else if (s->sample_rate_index != 8)
2088 g->long_end = 6;
2089 else
2090 g->long_end = 4; /* 8000 Hz */
2092 if (s->sample_rate_index != 8)
2093 g->short_start = 3;
2094 else
2095 g->short_start = 2;
2096 } else {
2097 g->long_end = 0;
2098 g->short_start = 0;
2100 } else {
2101 g->short_start = 13;
2102 g->long_end = 22;
2105 g->preflag = 0;
2106 if (!s->lsf)
2107 g->preflag = get_bits(&s->gb, 1);
2108 g->scalefac_scale = get_bits(&s->gb, 1);
2109 g->count1table_select = get_bits(&s->gb, 1);
2110 dprintf("block_type=%d switch_point=%d\n",
2111 g->block_type, g->switch_point);
2115 /* now we get bits from the main_data_begin offset */
2116 dprintf("seekback: %d\n", main_data_begin);
2117 seek_to_maindata(s, main_data_begin);
2119 for(gr=0;gr<nb_granules;gr++) {
2120 for(ch=0;ch<s->nb_channels;ch++) {
2121 g = &granules[ch][gr];
2123 bits_pos = get_bits_count(&s->gb);
2125 if (!s->lsf) {
2126 UINT8 *sc;
2127 int slen, slen1, slen2;
2129 /* MPEG1 scale factors */
2130 slen1 = slen_table[0][g->scalefac_compress];
2131 slen2 = slen_table[1][g->scalefac_compress];
2132 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2133 if (g->block_type == 2) {
2134 n = g->switch_point ? 17 : 18;
2135 j = 0;
2136 for(i=0;i<n;i++)
2137 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2138 for(i=0;i<18;i++)
2139 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2140 for(i=0;i<3;i++)
2141 g->scale_factors[j++] = 0;
2142 } else {
2143 sc = granules[ch][0].scale_factors;
2144 j = 0;
2145 for(k=0;k<4;k++) {
2146 n = (k == 0 ? 6 : 5);
2147 if ((g->scfsi & (0x8 >> k)) == 0) {
2148 slen = (k < 2) ? slen1 : slen2;
2149 for(i=0;i<n;i++)
2150 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2151 } else {
2152 /* simply copy from last granule */
2153 for(i=0;i<n;i++) {
2154 g->scale_factors[j] = sc[j];
2155 j++;
2159 g->scale_factors[j++] = 0;
2161 #if defined(DEBUG)
2163 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2164 g->scfsi, gr, ch);
2165 for(i=0;i<j;i++)
2166 printf(" %d", g->scale_factors[i]);
2167 printf("\n");
2169 #endif
2170 } else {
2171 int tindex, tindex2, slen[4], sl, sf;
2173 /* LSF scale factors */
2174 if (g->block_type == 2) {
2175 tindex = g->switch_point ? 2 : 1;
2176 } else {
2177 tindex = 0;
2179 sf = g->scalefac_compress;
2180 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2181 /* intensity stereo case */
2182 sf >>= 1;
2183 if (sf < 180) {
2184 lsf_sf_expand(slen, sf, 6, 6, 0);
2185 tindex2 = 3;
2186 } else if (sf < 244) {
2187 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2188 tindex2 = 4;
2189 } else {
2190 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2191 tindex2 = 5;
2193 } else {
2194 /* normal case */
2195 if (sf < 400) {
2196 lsf_sf_expand(slen, sf, 5, 4, 4);
2197 tindex2 = 0;
2198 } else if (sf < 500) {
2199 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2200 tindex2 = 1;
2201 } else {
2202 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2203 tindex2 = 2;
2204 g->preflag = 1;
2208 j = 0;
2209 for(k=0;k<4;k++) {
2210 n = lsf_nsf_table[tindex2][tindex][k];
2211 sl = slen[k];
2212 for(i=0;i<n;i++)
2213 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2215 /* XXX: should compute exact size */
2216 for(;j<40;j++)
2217 g->scale_factors[j] = 0;
2218 #if defined(DEBUG)
2220 printf("gr=%d ch=%d scale_factors:\n",
2221 gr, ch);
2222 for(i=0;i<40;i++)
2223 printf(" %d", g->scale_factors[i]);
2224 printf("\n");
2226 #endif
2229 exponents_from_scale_factors(s, g, exponents);
2231 /* read Huffman coded residue */
2232 if (huffman_decode(s, g, exponents,
2233 bits_pos + g->part2_3_length) < 0)
2234 return -1;
2235 #if defined(DEBUG)
2236 sample_dump(0, g->sb_hybrid, 576);
2237 #endif
2239 /* skip extension bits */
2240 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2241 if (bits_left < 0) {
2242 dprintf("bits_left=%d\n", bits_left);
2243 return -1;
2245 while (bits_left >= 16) {
2246 skip_bits(&s->gb, 16);
2247 bits_left -= 16;
2249 if (bits_left > 0)
2250 skip_bits(&s->gb, bits_left);
2251 } /* ch */
2253 if (s->nb_channels == 2)
2254 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2256 for(ch=0;ch<s->nb_channels;ch++) {
2257 g = &granules[ch][gr];
2259 reorder_block(s, g);
2260 #if defined(DEBUG)
2261 sample_dump(0, g->sb_hybrid, 576);
2262 #endif
2263 compute_antialias(s, g);
2264 #if defined(DEBUG)
2265 sample_dump(1, g->sb_hybrid, 576);
2266 #endif
2267 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2268 #if defined(DEBUG)
2269 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2270 #endif
2272 } /* gr */
2273 return nb_granules * 18;
2276 static int mp_decode_frame(MPADecodeContext *s,
2277 short *samples)
2279 int i, nb_frames, ch;
2280 short *samples_ptr;
2282 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2283 s->inbuf_ptr - s->inbuf - HEADER_SIZE);
2285 /* skip error protection field */
2286 if (s->error_protection)
2287 get_bits(&s->gb, 16);
2289 dprintf("frame %d:\n", s->frame_count);
2290 switch(s->layer) {
2291 case 1:
2292 nb_frames = mp_decode_layer1(s);
2293 break;
2294 case 2:
2295 nb_frames = mp_decode_layer2(s);
2296 break;
2297 case 3:
2298 default:
2299 nb_frames = mp_decode_layer3(s);
2300 break;
2302 #if defined(DEBUG)
2303 for(i=0;i<nb_frames;i++) {
2304 for(ch=0;ch<s->nb_channels;ch++) {
2305 int j;
2306 printf("%d-%d:", i, ch);
2307 for(j=0;j<SBLIMIT;j++)
2308 printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2309 printf("\n");
2312 #endif
2313 /* apply the synthesis filter */
2314 for(ch=0;ch<s->nb_channels;ch++) {
2315 samples_ptr = samples + ch;
2316 for(i=0;i<nb_frames;i++) {
2317 synth_filter(s, ch, samples_ptr, s->nb_channels,
2318 s->sb_samples[ch][i]);
2319 samples_ptr += 32 * s->nb_channels;
2322 #ifdef DEBUG
2323 s->frame_count++;
2324 #endif
2325 return nb_frames * 32 * sizeof(short) * s->nb_channels;
2328 static int decode_frame(AVCodecContext * avctx,
2329 void *data, int *data_size,
2330 UINT8 * buf, int buf_size)
2332 MPADecodeContext *s = avctx->priv_data;
2333 UINT32 header;
2334 UINT8 *buf_ptr;
2335 int len, out_size;
2336 short *out_samples = data;
2338 *data_size = 0;
2339 buf_ptr = buf;
2340 while (buf_size > 0) {
2341 len = s->inbuf_ptr - s->inbuf;
2342 if (s->frame_size == 0) {
2343 /* special case for next header for first frame in free
2344 format case (XXX: find a simpler method) */
2345 if (s->free_format_next_header != 0) {
2346 s->inbuf[0] = s->free_format_next_header >> 24;
2347 s->inbuf[1] = s->free_format_next_header >> 16;
2348 s->inbuf[2] = s->free_format_next_header >> 8;
2349 s->inbuf[3] = s->free_format_next_header;
2350 s->inbuf_ptr = s->inbuf + 4;
2351 s->free_format_next_header = 0;
2352 goto got_header;
2354 /* no header seen : find one. We need at least HEADER_SIZE
2355 bytes to parse it */
2356 len = HEADER_SIZE - len;
2357 if (len > buf_size)
2358 len = buf_size;
2359 if (len > 0) {
2360 memcpy(s->inbuf_ptr, buf_ptr, len);
2361 buf_ptr += len;
2362 buf_size -= len;
2363 s->inbuf_ptr += len;
2365 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2366 got_header:
2367 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2368 (s->inbuf[2] << 8) | s->inbuf[3];
2370 if (check_header(header) < 0) {
2371 /* no sync found : move by one byte (inefficient, but simple!) */
2372 memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2373 s->inbuf_ptr--;
2374 dprintf("skip %x\n", header);
2375 /* reset free format frame size to give a chance
2376 to get a new bitrate */
2377 s->free_format_frame_size = 0;
2378 } else {
2379 if (decode_header(s, header) == 1) {
2380 /* free format: prepare to compute frame size */
2381 s->frame_size = -1;
2383 /* update codec info */
2384 avctx->sample_rate = s->sample_rate;
2385 avctx->channels = s->nb_channels;
2386 avctx->bit_rate = s->bit_rate;
2387 avctx->frame_size = s->frame_size;
2390 } else if (s->frame_size == -1) {
2391 /* free format : find next sync to compute frame size */
2392 len = MPA_MAX_CODED_FRAME_SIZE - len;
2393 if (len > buf_size)
2394 len = buf_size;
2395 if (len == 0) {
2396 /* frame too long: resync */
2397 s->frame_size = 0;
2398 memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2399 s->inbuf_ptr--;
2400 } else {
2401 UINT8 *p, *pend;
2402 UINT32 header1;
2403 int padding;
2405 memcpy(s->inbuf_ptr, buf_ptr, len);
2406 /* check for header */
2407 p = s->inbuf_ptr - 3;
2408 pend = s->inbuf_ptr + len - 4;
2409 while (p <= pend) {
2410 header = (p[0] << 24) | (p[1] << 16) |
2411 (p[2] << 8) | p[3];
2412 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2413 (s->inbuf[2] << 8) | s->inbuf[3];
2414 /* check with high probability that we have a
2415 valid header */
2416 if ((header & SAME_HEADER_MASK) ==
2417 (header1 & SAME_HEADER_MASK)) {
2418 /* header found: update pointers */
2419 len = (p + 4) - s->inbuf_ptr;
2420 buf_ptr += len;
2421 buf_size -= len;
2422 s->inbuf_ptr = p;
2423 /* compute frame size */
2424 s->free_format_next_header = header;
2425 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2426 padding = (header1 >> 9) & 1;
2427 if (s->layer == 1)
2428 s->free_format_frame_size -= padding * 4;
2429 else
2430 s->free_format_frame_size -= padding;
2431 dprintf("free frame size=%d padding=%d\n",
2432 s->free_format_frame_size, padding);
2433 decode_header(s, header1);
2434 goto next_data;
2436 p++;
2438 /* not found: simply increase pointers */
2439 buf_ptr += len;
2440 s->inbuf_ptr += len;
2441 buf_size -= len;
2443 } else if (len < s->frame_size) {
2444 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2445 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2446 len = s->frame_size - len;
2447 if (len > buf_size)
2448 len = buf_size;
2449 memcpy(s->inbuf_ptr, buf_ptr, len);
2450 buf_ptr += len;
2451 s->inbuf_ptr += len;
2452 buf_size -= len;
2453 } else {
2454 out_size = mp_decode_frame(s, out_samples);
2455 s->inbuf_ptr = s->inbuf;
2456 s->frame_size = 0;
2457 *data_size = out_size;
2458 break;
2460 next_data:
2463 return buf_ptr - buf;
2466 AVCodec mp2_decoder =
2468 "mp2",
2469 CODEC_TYPE_AUDIO,
2470 CODEC_ID_MP2,
2471 sizeof(MPADecodeContext),
2472 decode_init,
2473 NULL,
2474 NULL,
2475 decode_frame,
2478 AVCodec mp3_decoder =
2480 "mp3",
2481 CODEC_TYPE_AUDIO,
2482 CODEC_ID_MP3LAME,
2483 sizeof(MPADecodeContext),
2484 decode_init,
2485 NULL,
2486 NULL,
2487 decode_frame,
2490 #undef C1
2491 #undef C2
2492 #undef C3
2493 #undef C4
2494 #undef C5
2495 #undef C6
2496 #undef C7
2497 #undef C8
2498 #undef FRAC_BITS
2499 #undef HEADER_SIZE