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