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