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