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