ffmpeg / libavcodec / mpegaudiodec.c @ 4bd8e17c
History  View  Annotate  Download (81.1 KB)
1 
/*


2 
* MPEG Audio decoder

3 
* Copyright (c) 2001, 2002 Fabrice Bellard.

4 
*

5 
* This file is part of FFmpeg.

6 
*

7 
* FFmpeg is free software; you can redistribute it and/or

8 
* modify it under the terms of the GNU Lesser General Public

9 
* License as published by the Free Software Foundation; either

10 
* version 2.1 of the License, or (at your option) any later version.

11 
*

12 
* FFmpeg is distributed in the hope that it will be useful,

13 
* but WITHOUT ANY WARRANTY; without even the implied warranty of

14 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

15 
* Lesser General Public License for more details.

16 
*

17 
* You should have received a copy of the GNU Lesser General Public

18 
* License along with FFmpeg; if not, write to the Free Software

19 
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 021101301 USA

20 
*/

21  
22 
/**

23 
* @file mpegaudiodec.c

24 
* MPEG Audio decoder.

25 
*/

26  
27 
//#define DEBUG

28 
#include "avcodec.h" 
29 
#include "bitstream.h" 
30 
#include "dsputil.h" 
31  
32 
/*

33 
* TODO:

34 
*  in low precision mode, use more 16 bit multiplies in synth filter

35 
*  test lsf / mpeg25 extensively.

36 
*/

37  
38 
/* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg

39 
audio decoder */

40 
#ifdef CONFIG_MPEGAUDIO_HP

41 
# define USE_HIGHPRECISION

42 
#endif

43  
44 
#include "mpegaudio.h" 
45 
#include "mpegaudiodecheader.h" 
46  
47 
#include "mathops.h" 
48  
49 
#define FRAC_ONE (1 << FRAC_BITS) 
50  
51 
#define FIX(a) ((int)((a) * FRAC_ONE)) 
52 
/* WARNING: only correct for posititive numbers */

53 
#define FIXR(a) ((int)((a) * FRAC_ONE + 0.5)) 
54 
#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS) 
55  
56 
#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) 
57  
58 
/****************/

59  
60 
#define HEADER_SIZE 4 
61  
62 
/**

63 
* Context for MP3On4 decoder

64 
*/

65 
typedef struct MP3On4DecodeContext { 
66 
int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) 
67 
int chan_cfg; ///< channel config number 
68 
MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance 
69 
} MP3On4DecodeContext; 
70  
71 
/* layer 3 "granule" */

72 
typedef struct GranuleDef { 
73 
uint8_t scfsi; 
74 
int part2_3_length;

75 
int big_values;

76 
int global_gain;

77 
int scalefac_compress;

78 
uint8_t block_type; 
79 
uint8_t switch_point; 
80 
int table_select[3]; 
81 
int subblock_gain[3]; 
82 
uint8_t scalefac_scale; 
83 
uint8_t count1table_select; 
84 
int region_size[3]; /* number of huffman codes in each region */ 
85 
int preflag;

86 
int short_start, long_end; /* long/short band indexes */ 
87 
uint8_t scale_factors[40];

88 
int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */ 
89 
} GranuleDef; 
90  
91 
#define MODE_EXT_MS_STEREO 2 
92 
#define MODE_EXT_I_STEREO 1 
93  
94 
/* layer 3 huffman tables */

95 
typedef struct HuffTable { 
96 
int xsize;

97 
const uint8_t *bits;

98 
const uint16_t *codes;

99 
} HuffTable; 
100  
101 
#include "mpegaudiodata.h" 
102 
#include "mpegaudiodectab.h" 
103  
104 
static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g); 
105 
static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g); 
106  
107 
/* vlc structure for decoding layer 3 huffman tables */

108 
static VLC huff_vlc[16]; 
109 
static VLC huff_quad_vlc[2]; 
110 
/* computed from band_size_long */

111 
static uint16_t band_index_long[9][23]; 
112 
/* XXX: free when all decoders are closed */

113 
#define TABLE_4_3_SIZE (8191 + 16)*4 
114 
static int8_t table_4_3_exp[TABLE_4_3_SIZE];

115 
static uint32_t table_4_3_value[TABLE_4_3_SIZE];

116 
static uint32_t exp_table[512]; 
117 
static uint32_t expval_table[512][16]; 
118 
/* intensity stereo coef table */

119 
static int32_t is_table[2][16]; 
120 
static int32_t is_table_lsf[2][2][16]; 
121 
static int32_t csa_table[8][4]; 
122 
static float csa_table_float[8][4]; 
123 
static int32_t mdct_win[8][36]; 
124  
125 
/* lower 2 bits: modulo 3, higher bits: shift */

126 
static uint16_t scale_factor_modshift[64]; 
127 
/* [i][j]: 2^(j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2)  1) */

128 
static int32_t scale_factor_mult[15][3]; 
129 
/* mult table for layer 2 group quantization */

130  
131 
#define SCALE_GEN(v) \

132 
{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) } 
133  
134 
static const int32_t scale_factor_mult2[3][3] = { 
135 
SCALE_GEN(4.0 / 3.0), /* 3 steps */ 
136 
SCALE_GEN(4.0 / 5.0), /* 5 steps */ 
137 
SCALE_GEN(4.0 / 9.0), /* 9 steps */ 
138 
}; 
139  
140 
static DECLARE_ALIGNED_16(MPA_INT, window[512]); 
141  
142 
/* layer 1 unscaling */

143 
/* n = number of bits of the mantissa minus 1 */

144 
static inline int l1_unscale(int n, int mant, int scale_factor) 
145 
{ 
146 
int shift, mod;

147 
int64_t val; 
148  
149 
shift = scale_factor_modshift[scale_factor]; 
150 
mod = shift & 3;

151 
shift >>= 2;

152 
val = MUL64(mant + (1 << n) + 1, scale_factor_mult[n1][mod]); 
153 
shift += n; 
154 
/* NOTE: at this point, 1 <= shift >= 21 + 15 */

155 
return (int)((val + (1LL << (shift  1))) >> shift); 
156 
} 
157  
158 
static inline int l2_unscale_group(int steps, int mant, int scale_factor) 
159 
{ 
160 
int shift, mod, val;

161  
162 
shift = scale_factor_modshift[scale_factor]; 
163 
mod = shift & 3;

164 
shift >>= 2;

165  
166 
val = (mant  (steps >> 1)) * scale_factor_mult2[steps >> 2][mod]; 
167 
/* NOTE: at this point, 0 <= shift <= 21 */

168 
if (shift > 0) 
169 
val = (val + (1 << (shift  1))) >> shift; 
170 
return val;

171 
} 
172  
173 
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */

174 
static inline int l3_unscale(int value, int exponent) 
175 
{ 
176 
unsigned int m; 
177 
int e;

178  
179 
e = table_4_3_exp [4*value + (exponent&3)]; 
180 
m = table_4_3_value[4*value + (exponent&3)]; 
181 
e = (exponent >> 2);

182 
assert(e>=1);

183 
if (e > 31) 
184 
return 0; 
185 
m = (m + (1 << (e1))) >> e; 
186  
187 
return m;

188 
} 
189  
190 
/* all integer n^(4/3) computation code */

191 
#define DEV_ORDER 13 
192  
193 
#define POW_FRAC_BITS 24 
194 
#define POW_FRAC_ONE (1 << POW_FRAC_BITS) 
195 
#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE)) 
196 
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)

197  
198 
static int dev_4_3_coefs[DEV_ORDER]; 
199  
200 
#if 0 /* unused */

201 
static int pow_mult3[3] = {

202 
POW_FIX(1.0),

203 
POW_FIX(1.25992104989487316476),

204 
POW_FIX(1.58740105196819947474),

205 
};

206 
#endif

207  
208 
static void int_pow_init(void) 
209 
{ 
210 
int i, a;

211  
212 
a = POW_FIX(1.0); 
213 
for(i=0;i<DEV_ORDER;i++) { 
214 
a = POW_MULL(a, POW_FIX(4.0 / 3.0)  i * POW_FIX(1.0)) / (i + 1); 
215 
dev_4_3_coefs[i] = a; 
216 
} 
217 
} 
218  
219 
#if 0 /* unused, remove? */

220 
/* return the mantissa and the binary exponent */

221 
static int int_pow(int i, int *exp_ptr)

222 
{

223 
int e, er, eq, j;

224 
int a, a1;

225 

226 
/* renormalize */

227 
a = i;

228 
e = POW_FRAC_BITS;

229 
while (a < (1 << (POW_FRAC_BITS  1))) {

230 
a = a << 1;

231 
e;

232 
}

233 
a = (1 << POW_FRAC_BITS);

234 
a1 = 0;

235 
for(j = DEV_ORDER  1; j >= 0; j)

236 
a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);

237 
a = (1 << POW_FRAC_BITS) + a1;

238 
/* exponent compute (exact) */

239 
e = e * 4;

240 
er = e % 3;

241 
eq = e / 3;

242 
a = POW_MULL(a, pow_mult3[er]);

243 
while (a >= 2 * POW_FRAC_ONE) {

244 
a = a >> 1;

245 
eq++;

246 
}

247 
/* convert to float */

248 
while (a < POW_FRAC_ONE) {

249 
a = a << 1;

250 
eq;

251 
}

252 
/* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */

253 
#if POW_FRAC_BITS > FRAC_BITS

254 
a = (a + (1 << (POW_FRAC_BITS  FRAC_BITS  1))) >> (POW_FRAC_BITS  FRAC_BITS);

255 
/* correct overflow */

256 
if (a >= 2 * (1 << FRAC_BITS)) {

257 
a = a >> 1;

258 
eq++;

259 
}

260 
#endif

261 
*exp_ptr = eq; 
262 
return a;

263 
} 
264 
#endif

265  
266 
static int decode_init(AVCodecContext * avctx) 
267 
{ 
268 
MPADecodeContext *s = avctx>priv_data; 
269 
static int init=0; 
270 
int i, j, k;

271  
272 
s>avctx = avctx; 
273  
274 
#if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)

275 
avctx>sample_fmt= SAMPLE_FMT_S32; 
276 
#else

277 
avctx>sample_fmt= SAMPLE_FMT_S16; 
278 
#endif

279 
s>error_resilience= avctx>error_resilience; 
280  
281 
if(avctx>antialias_algo != FF_AA_FLOAT)

282 
s>compute_antialias= compute_antialias_integer; 
283 
else

284 
s>compute_antialias= compute_antialias_float; 
285  
286 
if (!init && !avctx>parse_only) {

287 
/* scale factors table for layer 1/2 */

288 
for(i=0;i<64;i++) { 
289 
int shift, mod;

290 
/* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */

291 
shift = (i / 3);

292 
mod = i % 3;

293 
scale_factor_modshift[i] = mod  (shift << 2);

294 
} 
295  
296 
/* scale factor multiply for layer 1 */

297 
for(i=0;i<15;i++) { 
298 
int n, norm;

299 
n = i + 2;

300 
norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n)  1); 
301 
scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm); 
302 
scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm); 
303 
scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm); 
304 
dprintf(avctx, "%d: norm=%x s=%x %x %x\n",

305 
i, norm, 
306 
scale_factor_mult[i][0],

307 
scale_factor_mult[i][1],

308 
scale_factor_mult[i][2]);

309 
} 
310  
311 
ff_mpa_synth_init(window); 
312  
313 
/* huffman decode tables */

314 
for(i=1;i<16;i++) { 
315 
const HuffTable *h = &mpa_huff_tables[i];

316 
int xsize, x, y;

317 
unsigned int n; 
318 
uint8_t tmp_bits [512];

319 
uint16_t tmp_codes[512];

320  
321 
memset(tmp_bits , 0, sizeof(tmp_bits )); 
322 
memset(tmp_codes, 0, sizeof(tmp_codes)); 
323  
324 
xsize = h>xsize; 
325 
n = xsize * xsize; 
326  
327 
j = 0;

328 
for(x=0;x<xsize;x++) { 
329 
for(y=0;y<xsize;y++){ 
330 
tmp_bits [(x << 5)  y  ((x&&y)<<4)]= h>bits [j ]; 
331 
tmp_codes[(x << 5)  y  ((x&&y)<<4)]= h>codes[j++]; 
332 
} 
333 
} 
334  
335 
/* XXX: fail test */

336 
init_vlc(&huff_vlc[i], 7, 512, 
337 
tmp_bits, 1, 1, tmp_codes, 2, 2, 1); 
338 
} 
339 
for(i=0;i<2;i++) { 
340 
init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, 
341 
mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1); 
342 
} 
343  
344 
for(i=0;i<9;i++) { 
345 
k = 0;

346 
for(j=0;j<22;j++) { 
347 
band_index_long[i][j] = k; 
348 
k += band_size_long[i][j]; 
349 
} 
350 
band_index_long[i][22] = k;

351 
} 
352  
353 
/* compute n ^ (4/3) and store it in mantissa/exp format */

354  
355 
int_pow_init(); 
356 
for(i=1;i<TABLE_4_3_SIZE;i++) { 
357 
double f, fm;

358 
int e, m;

359 
f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25); 
360 
fm = frexp(f, &e); 
361 
m = (uint32_t)(fm*(1LL<<31) + 0.5); 
362 
e+= FRAC_BITS  31 + 5  100; 
363  
364 
/* normalized to FRAC_BITS */

365 
table_4_3_value[i] = m; 
366 
// av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));

367 
table_4_3_exp[i] = e; 
368 
} 
369 
for(i=0; i<512*16; i++){ 
370 
int exponent= (i>>4); 
371 
double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent400)*0.25 + FRAC_BITS + 5); 
372 
expval_table[exponent][i&15]= llrint(f);

373 
if((i&15)==1) 
374 
exp_table[exponent]= llrint(f); 
375 
} 
376  
377 
for(i=0;i<7;i++) { 
378 
float f;

379 
int v;

380 
if (i != 6) { 
381 
f = tan((double)i * M_PI / 12.0); 
382 
v = FIXR(f / (1.0 + f)); 
383 
} else {

384 
v = FIXR(1.0); 
385 
} 
386 
is_table[0][i] = v;

387 
is_table[1][6  i] = v; 
388 
} 
389 
/* invalid values */

390 
for(i=7;i<16;i++) 
391 
is_table[0][i] = is_table[1][i] = 0.0; 
392  
393 
for(i=0;i<16;i++) { 
394 
double f;

395 
int e, k;

396  
397 
for(j=0;j<2;j++) { 
398 
e = (j + 1) * ((i + 1) >> 1); 
399 
f = pow(2.0, e / 4.0); 
400 
k = i & 1;

401 
is_table_lsf[j][k ^ 1][i] = FIXR(f);

402 
is_table_lsf[j][k][i] = FIXR(1.0); 
403 
dprintf(avctx, "is_table_lsf %d %d: %x %x\n",

404 
i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]); 
405 
} 
406 
} 
407  
408 
for(i=0;i<8;i++) { 
409 
float ci, cs, ca;

410 
ci = ci_table[i]; 
411 
cs = 1.0 / sqrt(1.0 + ci * ci); 
412 
ca = cs * ci; 
413 
csa_table[i][0] = FIXHR(cs/4); 
414 
csa_table[i][1] = FIXHR(ca/4); 
415 
csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4); 
416 
csa_table[i][3] = FIXHR(ca/4)  FIXHR(cs/4); 
417 
csa_table_float[i][0] = cs;

418 
csa_table_float[i][1] = ca;

419 
csa_table_float[i][2] = ca + cs;

420 
csa_table_float[i][3] = ca  cs;

421 
// printf("%d %d %d %d\n", FIX(cs), FIX(cs1), FIX(ca), FIX(cs)FIX(ca));

422 
// av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, cacs);

423 
} 
424  
425 
/* compute mdct windows */

426 
for(i=0;i<36;i++) { 
427 
for(j=0; j<4; j++){ 
428 
double d;

429  
430 
if(j==2 && i%3 != 1) 
431 
continue;

432  
433 
d= sin(M_PI * (i + 0.5) / 36.0); 
434 
if(j==1){ 
435 
if (i>=30) d= 0; 
436 
else if(i>=24) d= sin(M_PI * (i  18 + 0.5) / 12.0); 
437 
else if(i>=18) d= 1; 
438 
}else if(j==3){ 
439 
if (i< 6) d= 0; 
440 
else if(i< 12) d= sin(M_PI * (i  6 + 0.5) / 12.0); 
441 
else if(i< 18) d= 1; 
442 
} 
443 
//merge last stage of imdct into the window coefficients

444 
d*= 0.5 / cos(M_PI*(2*i + 19)/72); 
445  
446 
if(j==2) 
447 
mdct_win[j][i/3] = FIXHR((d / (1<<5))); 
448 
else

449 
mdct_win[j][i ] = FIXHR((d / (1<<5))); 
450 
// av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));

451 
} 
452 
} 
453  
454 
/* NOTE: we do frequency inversion adter the MDCT by changing

455 
the sign of the right window coefs */

456 
for(j=0;j<4;j++) { 
457 
for(i=0;i<36;i+=2) { 
458 
mdct_win[j + 4][i] = mdct_win[j][i];

459 
mdct_win[j + 4][i + 1] = mdct_win[j][i + 1]; 
460 
} 
461 
} 
462  
463 
#if defined(DEBUG)

464 
for(j=0;j<8;j++) { 
465 
av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);

466 
for(i=0;i<36;i++) 
467 
av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE); 
468 
av_log(avctx, AV_LOG_DEBUG, "\n");

469 
} 
470 
#endif

471 
init = 1;

472 
} 
473  
474 
#ifdef DEBUG

475 
s>frame_count = 0;

476 
#endif

477 
if (avctx>codec_id == CODEC_ID_MP3ADU)

478 
s>adu_mode = 1;

479 
return 0; 
480 
} 
481  
482 
/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6  j))) */

483  
484 
/* cos(i*pi/64) */

485  
486 
#define COS0_0 FIXHR(0.50060299823519630134/2) 
487 
#define COS0_1 FIXHR(0.50547095989754365998/2) 
488 
#define COS0_2 FIXHR(0.51544730992262454697/2) 
489 
#define COS0_3 FIXHR(0.53104259108978417447/2) 
490 
#define COS0_4 FIXHR(0.55310389603444452782/2) 
491 
#define COS0_5 FIXHR(0.58293496820613387367/2) 
492 
#define COS0_6 FIXHR(0.62250412303566481615/2) 
493 
#define COS0_7 FIXHR(0.67480834145500574602/2) 
494 
#define COS0_8 FIXHR(0.74453627100229844977/2) 
495 
#define COS0_9 FIXHR(0.83934964541552703873/2) 
496 
#define COS0_10 FIXHR(0.97256823786196069369/2) 
497 
#define COS0_11 FIXHR(1.16943993343288495515/4) 
498 
#define COS0_12 FIXHR(1.48416461631416627724/4) 
499 
#define COS0_13 FIXHR(2.05778100995341155085/8) 
500 
#define COS0_14 FIXHR(3.40760841846871878570/8) 
501 
#define COS0_15 FIXHR(10.19000812354805681150/32) 
502  
503 
#define COS1_0 FIXHR(0.50241928618815570551/2) 
504 
#define COS1_1 FIXHR(0.52249861493968888062/2) 
505 
#define COS1_2 FIXHR(0.56694403481635770368/2) 
506 
#define COS1_3 FIXHR(0.64682178335999012954/2) 
507 
#define COS1_4 FIXHR(0.78815462345125022473/2) 
508 
#define COS1_5 FIXHR(1.06067768599034747134/4) 
509 
#define COS1_6 FIXHR(1.72244709823833392782/4) 
510 
#define COS1_7 FIXHR(5.10114861868916385802/16) 
511  
512 
#define COS2_0 FIXHR(0.50979557910415916894/2) 
513 
#define COS2_1 FIXHR(0.60134488693504528054/2) 
514 
#define COS2_2 FIXHR(0.89997622313641570463/2) 
515 
#define COS2_3 FIXHR(2.56291544774150617881/8) 
516  
517 
#define COS3_0 FIXHR(0.54119610014619698439/2) 
518 
#define COS3_1 FIXHR(1.30656296487637652785/4) 
519  
520 
#define COS4_0 FIXHR(0.70710678118654752439/2) 
521  
522 
/* butterfly operator */

523 
#define BF(a, b, c, s)\

524 
{\ 
525 
tmp0 = tab[a] + tab[b];\ 
526 
tmp1 = tab[a]  tab[b];\ 
527 
tab[a] = tmp0;\ 
528 
tab[b] = MULH(tmp1<<(s), c);\ 
529 
} 
530  
531 
#define BF1(a, b, c, d)\

532 
{\ 
533 
BF(a, b, COS4_0, 1);\

534 
BF(c, d,COS4_0, 1);\

535 
tab[c] += tab[d];\ 
536 
} 
537  
538 
#define BF2(a, b, c, d)\

539 
{\ 
540 
BF(a, b, COS4_0, 1);\

541 
BF(c, d,COS4_0, 1);\

542 
tab[c] += tab[d];\ 
543 
tab[a] += tab[c];\ 
544 
tab[c] += tab[b];\ 
545 
tab[b] += tab[d];\ 
546 
} 
547  
548 
#define ADD(a, b) tab[a] += tab[b]

549  
550 
/* DCT32 without 1/sqrt(2) coef zero scaling. */

551 
static void dct32(int32_t *out, int32_t *tab) 
552 
{ 
553 
int tmp0, tmp1;

554  
555 
/* pass 1 */

556 
BF( 0, 31, COS0_0 , 1); 
557 
BF(15, 16, COS0_15, 5); 
558 
/* pass 2 */

559 
BF( 0, 15, COS1_0 , 1); 
560 
BF(16, 31,COS1_0 , 1); 
561 
/* pass 1 */

562 
BF( 7, 24, COS0_7 , 1); 
563 
BF( 8, 23, COS0_8 , 1); 
564 
/* pass 2 */

565 
BF( 7, 8, COS1_7 , 4); 
566 
BF(23, 24,COS1_7 , 4); 
567 
/* pass 3 */

568 
BF( 0, 7, COS2_0 , 1); 
569 
BF( 8, 15,COS2_0 , 1); 
570 
BF(16, 23, COS2_0 , 1); 
571 
BF(24, 31,COS2_0 , 1); 
572 
/* pass 1 */

573 
BF( 3, 28, COS0_3 , 1); 
574 
BF(12, 19, COS0_12, 2); 
575 
/* pass 2 */

576 
BF( 3, 12, COS1_3 , 1); 
577 
BF(19, 28,COS1_3 , 1); 
578 
/* pass 1 */

579 
BF( 4, 27, COS0_4 , 1); 
580 
BF(11, 20, COS0_11, 2); 
581 
/* pass 2 */

582 
BF( 4, 11, COS1_4 , 1); 
583 
BF(20, 27,COS1_4 , 1); 
584 
/* pass 3 */

585 
BF( 3, 4, COS2_3 , 3); 
586 
BF(11, 12,COS2_3 , 3); 
587 
BF(19, 20, COS2_3 , 3); 
588 
BF(27, 28,COS2_3 , 3); 
589 
/* pass 4 */

590 
BF( 0, 3, COS3_0 , 1); 
591 
BF( 4, 7,COS3_0 , 1); 
592 
BF( 8, 11, COS3_0 , 1); 
593 
BF(12, 15,COS3_0 , 1); 
594 
BF(16, 19, COS3_0 , 1); 
595 
BF(20, 23,COS3_0 , 1); 
596 
BF(24, 27, COS3_0 , 1); 
597 
BF(28, 31,COS3_0 , 1); 
598  
599  
600  
601 
/* pass 1 */

602 
BF( 1, 30, COS0_1 , 1); 
603 
BF(14, 17, COS0_14, 3); 
604 
/* pass 2 */

605 
BF( 1, 14, COS1_1 , 1); 
606 
BF(17, 30,COS1_1 , 1); 
607 
/* pass 1 */

608 
BF( 6, 25, COS0_6 , 1); 
609 
BF( 9, 22, COS0_9 , 1); 
610 
/* pass 2 */

611 
BF( 6, 9, COS1_6 , 2); 
612 
BF(22, 25,COS1_6 , 2); 
613 
/* pass 3 */

614 
BF( 1, 6, COS2_1 , 1); 
615 
BF( 9, 14,COS2_1 , 1); 
616 
BF(17, 22, COS2_1 , 1); 
617 
BF(25, 30,COS2_1 , 1); 
618  
619 
/* pass 1 */

620 
BF( 2, 29, COS0_2 , 1); 
621 
BF(13, 18, COS0_13, 3); 
622 
/* pass 2 */

623 
BF( 2, 13, COS1_2 , 1); 
624 
BF(18, 29,COS1_2 , 1); 
625 
/* pass 1 */

626 
BF( 5, 26, COS0_5 , 1); 
627 
BF(10, 21, COS0_10, 1); 
628 
/* pass 2 */

629 
BF( 5, 10, COS1_5 , 2); 
630 
BF(21, 26,COS1_5 , 2); 
631 
/* pass 3 */

632 
BF( 2, 5, COS2_2 , 1); 
633 
BF(10, 13,COS2_2 , 1); 
634 
BF(18, 21, COS2_2 , 1); 
635 
BF(26, 29,COS2_2 , 1); 
636 
/* pass 4 */

637 
BF( 1, 2, COS3_1 , 2); 
638 
BF( 5, 6,COS3_1 , 2); 
639 
BF( 9, 10, COS3_1 , 2); 
640 
BF(13, 14,COS3_1 , 2); 
641 
BF(17, 18, COS3_1 , 2); 
642 
BF(21, 22,COS3_1 , 2); 
643 
BF(25, 26, COS3_1 , 2); 
644 
BF(29, 30,COS3_1 , 2); 
645  
646 
/* pass 5 */

647 
BF1( 0, 1, 2, 3); 
648 
BF2( 4, 5, 6, 7); 
649 
BF1( 8, 9, 10, 11); 
650 
BF2(12, 13, 14, 15); 
651 
BF1(16, 17, 18, 19); 
652 
BF2(20, 21, 22, 23); 
653 
BF1(24, 25, 26, 27); 
654 
BF2(28, 29, 30, 31); 
655  
656 
/* pass 6 */

657  
658 
ADD( 8, 12); 
659 
ADD(12, 10); 
660 
ADD(10, 14); 
661 
ADD(14, 9); 
662 
ADD( 9, 13); 
663 
ADD(13, 11); 
664 
ADD(11, 15); 
665  
666 
out[ 0] = tab[0]; 
667 
out[16] = tab[1]; 
668 
out[ 8] = tab[2]; 
669 
out[24] = tab[3]; 
670 
out[ 4] = tab[4]; 
671 
out[20] = tab[5]; 
672 
out[12] = tab[6]; 
673 
out[28] = tab[7]; 
674 
out[ 2] = tab[8]; 
675 
out[18] = tab[9]; 
676 
out[10] = tab[10]; 
677 
out[26] = tab[11]; 
678 
out[ 6] = tab[12]; 
679 
out[22] = tab[13]; 
680 
out[14] = tab[14]; 
681 
out[30] = tab[15]; 
682  
683 
ADD(24, 28); 
684 
ADD(28, 26); 
685 
ADD(26, 30); 
686 
ADD(30, 25); 
687 
ADD(25, 29); 
688 
ADD(29, 27); 
689 
ADD(27, 31); 
690  
691 
out[ 1] = tab[16] + tab[24]; 
692 
out[17] = tab[17] + tab[25]; 
693 
out[ 9] = tab[18] + tab[26]; 
694 
out[25] = tab[19] + tab[27]; 
695 
out[ 5] = tab[20] + tab[28]; 
696 
out[21] = tab[21] + tab[29]; 
697 
out[13] = tab[22] + tab[30]; 
698 
out[29] = tab[23] + tab[31]; 
699 
out[ 3] = tab[24] + tab[20]; 
700 
out[19] = tab[25] + tab[21]; 
701 
out[11] = tab[26] + tab[22]; 
702 
out[27] = tab[27] + tab[23]; 
703 
out[ 7] = tab[28] + tab[18]; 
704 
out[23] = tab[29] + tab[19]; 
705 
out[15] = tab[30] + tab[17]; 
706 
out[31] = tab[31]; 
707 
} 
708  
709 
#if FRAC_BITS <= 15 
710  
711 
static inline int round_sample(int *sum) 
712 
{ 
713 
int sum1;

714 
sum1 = (*sum) >> OUT_SHIFT; 
715 
*sum &= (1<<OUT_SHIFT)1; 
716 
if (sum1 < OUT_MIN)

717 
sum1 = OUT_MIN; 
718 
else if (sum1 > OUT_MAX) 
719 
sum1 = OUT_MAX; 
720 
return sum1;

721 
} 
722  
723 
/* signed 16x16 > 32 multiply add accumulate */

724 
#define MACS(rt, ra, rb) MAC16(rt, ra, rb)

725  
726 
/* signed 16x16 > 32 multiply */

727 
#define MULS(ra, rb) MUL16(ra, rb)

728  
729 
#else

730  
731 
static inline int round_sample(int64_t *sum) 
732 
{ 
733 
int sum1;

734 
sum1 = (int)((*sum) >> OUT_SHIFT);

735 
*sum &= (1<<OUT_SHIFT)1; 
736 
if (sum1 < OUT_MIN)

737 
sum1 = OUT_MIN; 
738 
else if (sum1 > OUT_MAX) 
739 
sum1 = OUT_MAX; 
740 
return sum1;

741 
} 
742  
743 
# define MULS(ra, rb) MUL64(ra, rb)

744 
#endif

745  
746 
#define SUM8(sum, op, w, p) \

747 
{ \ 
748 
sum op MULS((w)[0 * 64], p[0 * 64]);\ 
749 
sum op MULS((w)[1 * 64], p[1 * 64]);\ 
750 
sum op MULS((w)[2 * 64], p[2 * 64]);\ 
751 
sum op MULS((w)[3 * 64], p[3 * 64]);\ 
752 
sum op MULS((w)[4 * 64], p[4 * 64]);\ 
753 
sum op MULS((w)[5 * 64], p[5 * 64]);\ 
754 
sum op MULS((w)[6 * 64], p[6 * 64]);\ 
755 
sum op MULS((w)[7 * 64], p[7 * 64]);\ 
756 
} 
757  
758 
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \

759 
{ \ 
760 
int tmp;\

761 
tmp = p[0 * 64];\ 
762 
sum1 op1 MULS((w1)[0 * 64], tmp);\ 
763 
sum2 op2 MULS((w2)[0 * 64], tmp);\ 
764 
tmp = p[1 * 64];\ 
765 
sum1 op1 MULS((w1)[1 * 64], tmp);\ 
766 
sum2 op2 MULS((w2)[1 * 64], tmp);\ 
767 
tmp = p[2 * 64];\ 
768 
sum1 op1 MULS((w1)[2 * 64], tmp);\ 
769 
sum2 op2 MULS((w2)[2 * 64], tmp);\ 
770 
tmp = p[3 * 64];\ 
771 
sum1 op1 MULS((w1)[3 * 64], tmp);\ 
772 
sum2 op2 MULS((w2)[3 * 64], tmp);\ 
773 
tmp = p[4 * 64];\ 
774 
sum1 op1 MULS((w1)[4 * 64], tmp);\ 
775 
sum2 op2 MULS((w2)[4 * 64], tmp);\ 
776 
tmp = p[5 * 64];\ 
777 
sum1 op1 MULS((w1)[5 * 64], tmp);\ 
778 
sum2 op2 MULS((w2)[5 * 64], tmp);\ 
779 
tmp = p[6 * 64];\ 
780 
sum1 op1 MULS((w1)[6 * 64], tmp);\ 
781 
sum2 op2 MULS((w2)[6 * 64], tmp);\ 
782 
tmp = p[7 * 64];\ 
783 
sum1 op1 MULS((w1)[7 * 64], tmp);\ 
784 
sum2 op2 MULS((w2)[7 * 64], tmp);\ 
785 
} 
786  
787 
void ff_mpa_synth_init(MPA_INT *window)

788 
{ 
789 
int i;

790  
791 
/* max = 18760, max sum over all 16 coefs : 44736 */

792 
for(i=0;i<257;i++) { 
793 
int v;

794 
v = ff_mpa_enwindow[i]; 
795 
#if WFRAC_BITS < 16 
796 
v = (v + (1 << (16  WFRAC_BITS  1))) >> (16  WFRAC_BITS); 
797 
#endif

798 
window[i] = v; 
799 
if ((i & 63) != 0) 
800 
v = v; 
801 
if (i != 0) 
802 
window[512  i] = v;

803 
} 
804 
} 
805  
806 
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:

807 
32 samples. */

808 
/* XXX: optimize by avoiding ring buffer usage */

809 
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, 
810 
MPA_INT *window, int *dither_state,

811 
OUT_INT *samples, int incr,

812 
int32_t sb_samples[SBLIMIT]) 
813 
{ 
814 
int32_t tmp[32];

815 
register MPA_INT *synth_buf;

816 
register const MPA_INT *w, *w2, *p; 
817 
int j, offset, v;

818 
OUT_INT *samples2; 
819 
#if FRAC_BITS <= 15 
820 
int sum, sum2;

821 
#else

822 
int64_t sum, sum2; 
823 
#endif

824  
825 
dct32(tmp, sb_samples); 
826  
827 
offset = *synth_buf_offset; 
828 
synth_buf = synth_buf_ptr + offset; 
829  
830 
for(j=0;j<32;j++) { 
831 
v = tmp[j]; 
832 
#if FRAC_BITS <= 15 
833 
/* NOTE: can cause a loss in precision if very high amplitude

834 
sound */

835 
if (v > 32767) 
836 
v = 32767;

837 
else if (v < 32768) 
838 
v = 32768;

839 
#endif

840 
synth_buf[j] = v; 
841 
} 
842 
/* copy to avoid wrap */

843 
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); 
844  
845 
samples2 = samples + 31 * incr;

846 
w = window; 
847 
w2 = window + 31;

848  
849 
sum = *dither_state; 
850 
p = synth_buf + 16;

851 
SUM8(sum, +=, w, p); 
852 
p = synth_buf + 48;

853 
SUM8(sum, =, w + 32, p);

854 
*samples = round_sample(&sum); 
855 
samples += incr; 
856 
w++; 
857  
858 
/* we calculate two samples at the same time to avoid one memory

859 
access per two sample */

860 
for(j=1;j<16;j++) { 
861 
sum2 = 0;

862 
p = synth_buf + 16 + j;

863 
SUM8P2(sum, +=, sum2, =, w, w2, p); 
864 
p = synth_buf + 48  j;

865 
SUM8P2(sum, =, sum2, =, w + 32, w2 + 32, p); 
866  
867 
*samples = round_sample(&sum); 
868 
samples += incr; 
869 
sum += sum2; 
870 
*samples2 = round_sample(&sum); 
871 
samples2 = incr; 
872 
w++; 
873 
w2; 
874 
} 
875  
876 
p = synth_buf + 32;

877 
SUM8(sum, =, w + 32, p);

878 
*samples = round_sample(&sum); 
879 
*dither_state= sum; 
880  
881 
offset = (offset  32) & 511; 
882 
*synth_buf_offset = offset; 
883 
} 
884  
885 
#define C3 FIXHR(0.86602540378443864676/2) 
886  
887 
/* 0.5 / cos(pi*(2*i+1)/36) */

888 
static const int icos36[9] = { 
889 
FIXR(0.50190991877167369479), 
890 
FIXR(0.51763809020504152469), //0 
891 
FIXR(0.55168895948124587824), 
892 
FIXR(0.61038729438072803416), 
893 
FIXR(0.70710678118654752439), //1 
894 
FIXR(0.87172339781054900991), 
895 
FIXR(1.18310079157624925896), 
896 
FIXR(1.93185165257813657349), //2 
897 
FIXR(5.73685662283492756461), 
898 
}; 
899  
900 
/* 0.5 / cos(pi*(2*i+1)/36) */

901 
static const int icos36h[9] = { 
902 
FIXHR(0.50190991877167369479/2), 
903 
FIXHR(0.51763809020504152469/2), //0 
904 
FIXHR(0.55168895948124587824/2), 
905 
FIXHR(0.61038729438072803416/2), 
906 
FIXHR(0.70710678118654752439/2), //1 
907 
FIXHR(0.87172339781054900991/2), 
908 
FIXHR(1.18310079157624925896/4), 
909 
FIXHR(1.93185165257813657349/4), //2 
910 
// FIXHR(5.73685662283492756461),

911 
}; 
912  
913 
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious

914 
cases. */

915 
static void imdct12(int *out, int *in) 
916 
{ 
917 
int in0, in1, in2, in3, in4, in5, t1, t2;

918  
919 
in0= in[0*3]; 
920 
in1= in[1*3] + in[0*3]; 
921 
in2= in[2*3] + in[1*3]; 
922 
in3= in[3*3] + in[2*3]; 
923 
in4= in[4*3] + in[3*3]; 
924 
in5= in[5*3] + in[4*3]; 
925 
in5 += in3; 
926 
in3 += in1; 
927  
928 
in2= MULH(2*in2, C3);

929 
in3= MULH(4*in3, C3);

930  
931 
t1 = in0  in4; 
932 
t2 = MULH(2*(in1  in5), icos36h[4]); 
933  
934 
out[ 7]=

935 
out[10]= t1 + t2;

936 
out[ 1]=

937 
out[ 4]= t1  t2;

938  
939 
in0 += in4>>1;

940 
in4 = in0 + in2; 
941 
in5 += 2*in1;

942 
in1 = MULH(in5 + in3, icos36h[1]);

943 
out[ 8]=

944 
out[ 9]= in4 + in1;

945 
out[ 2]=

946 
out[ 3]= in4  in1;

947  
948 
in0 = in2; 
949 
in5 = MULH(2*(in5  in3), icos36h[7]); 
950 
out[ 0]=

951 
out[ 5]= in0  in5;

952 
out[ 6]=

953 
out[11]= in0 + in5;

954 
} 
955  
956 
/* cos(pi*i/18) */

957 
#define C1 FIXHR(0.98480775301220805936/2) 
958 
#define C2 FIXHR(0.93969262078590838405/2) 
959 
#define C3 FIXHR(0.86602540378443864676/2) 
960 
#define C4 FIXHR(0.76604444311897803520/2) 
961 
#define C5 FIXHR(0.64278760968653932632/2) 
962 
#define C6 FIXHR(0.5/2) 
963 
#define C7 FIXHR(0.34202014332566873304/2) 
964 
#define C8 FIXHR(0.17364817766693034885/2) 
965  
966  
967 
/* using Lee like decomposition followed by hand coded 9 points DCT */

968 
static void imdct36(int *out, int *buf, int *in, int *win) 
969 
{ 
970 
int i, j, t0, t1, t2, t3, s0, s1, s2, s3;

971 
int tmp[18], *tmp1, *in1; 
972  
973 
for(i=17;i>=1;i) 
974 
in[i] += in[i1];

975 
for(i=17;i>=3;i=2) 
976 
in[i] += in[i2];

977  
978 
for(j=0;j<2;j++) { 
979 
tmp1 = tmp + j; 
980 
in1 = in + j; 
981 
#if 0

982 
//more accurate but slower

983 
int64_t t0, t1, t2, t3;

984 
t2 = in1[2*4] + in1[2*8]  in1[2*2];

985 

986 
t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;

987 
t1 = in1[2*0]  in1[2*6];

988 
tmp1[ 6] = t1  (t2>>1);

989 
tmp1[16] = t1 + t2;

990 

991 
t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);

992 
t1 = MUL64( in1[2*4]  in1[2*8] , 2*C8);

993 
t2 = MUL64(2*(in1[2*2] + in1[2*8]), C4);

994 

995 
tmp1[10] = (t3  t0  t2) >> 32;

996 
tmp1[ 2] = (t3 + t0 + t1) >> 32;

997 
tmp1[14] = (t3 + t2  t1) >> 32;

998 

999 
tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7]  in1[2*1]), C3);

1000 
t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);

1001 
t3 = MUL64( in1[2*5]  in1[2*7] , 2*C7);

1002 
t0 = MUL64(2*in1[2*3], C3);

1003 

1004 
t1 = MUL64(2*(in1[2*1] + in1[2*7]), C5);

1005 

1006 
tmp1[ 0] = (t2 + t3 + t0) >> 32;

1007 
tmp1[12] = (t2 + t1  t0) >> 32;

1008 
tmp1[ 8] = (t3  t1  t0) >> 32;

1009 
#else

1010 
t2 = in1[2*4] + in1[2*8]  in1[2*2]; 
1011  
1012 
t3 = in1[2*0] + (in1[2*6]>>1); 
1013 
t1 = in1[2*0]  in1[2*6]; 
1014 
tmp1[ 6] = t1  (t2>>1); 
1015 
tmp1[16] = t1 + t2;

1016  
1017 
t0 = MULH(2*(in1[2*2] + in1[2*4]), C2); 
1018 
t1 = MULH( in1[2*4]  in1[2*8] , 2*C8); 
1019 
t2 = MULH(2*(in1[2*2] + in1[2*8]), C4); 
1020  
1021 
tmp1[10] = t3  t0  t2;

1022 
tmp1[ 2] = t3 + t0 + t1;

1023 
tmp1[14] = t3 + t2  t1;

1024  
1025 
tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7]  in1[2*1]), C3); 
1026 
t2 = MULH(2*(in1[2*1] + in1[2*5]), C1); 
1027 
t3 = MULH( in1[2*5]  in1[2*7] , 2*C7); 
1028 
t0 = MULH(2*in1[2*3], C3); 
1029  
1030 
t1 = MULH(2*(in1[2*1] + in1[2*7]), C5); 
1031  
1032 
tmp1[ 0] = t2 + t3 + t0;

1033 
tmp1[12] = t2 + t1  t0;

1034 
tmp1[ 8] = t3  t1  t0;

1035 
#endif

1036 
} 
1037  
1038 
i = 0;

1039 
for(j=0;j<4;j++) { 
1040 
t0 = tmp[i]; 
1041 
t1 = tmp[i + 2];

1042 
s0 = t1 + t0; 
1043 
s2 = t1  t0; 
1044  
1045 
t2 = tmp[i + 1];

1046 
t3 = tmp[i + 3];

1047 
s1 = MULH(2*(t3 + t2), icos36h[j]);

1048 
s3 = MULL(t3  t2, icos36[8  j]);

1049  
1050 
t0 = s0 + s1; 
1051 
t1 = s0  s1; 
1052 
out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j]; 
1053 
out[(8  j)*SBLIMIT] = MULH(t1, win[8  j]) + buf[8  j]; 
1054 
buf[9 + j] = MULH(t0, win[18 + 9 + j]); 
1055 
buf[8  j] = MULH(t0, win[18 + 8  j]); 
1056  
1057 
t0 = s2 + s3; 
1058 
t1 = s2  s3; 
1059 
out[(9 + 8  j)*SBLIMIT] = MULH(t1, win[9 + 8  j]) + buf[9 + 8  j]; 
1060 
out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j]; 
1061 
buf[9 + 8  j] = MULH(t0, win[18 + 9 + 8  j]); 
1062 
buf[ + j] = MULH(t0, win[18 + j]);

1063 
i += 4;

1064 
} 
1065  
1066 
s0 = tmp[16];

1067 
s1 = MULH(2*tmp[17], icos36h[4]); 
1068 
t0 = s0 + s1; 
1069 
t1 = s0  s1; 
1070 
out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4]; 
1071 
out[(8  4)*SBLIMIT] = MULH(t1, win[8  4]) + buf[8  4]; 
1072 
buf[9 + 4] = MULH(t0, win[18 + 9 + 4]); 
1073 
buf[8  4] = MULH(t0, win[18 + 8  4]); 
1074 
} 
1075  
1076 
/* return the number of decoded frames */

1077 
static int mp_decode_layer1(MPADecodeContext *s) 
1078 
{ 
1079 
int bound, i, v, n, ch, j, mant;

1080 
uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT]; 
1081 
uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; 
1082  
1083 
if (s>mode == MPA_JSTEREO)

1084 
bound = (s>mode_ext + 1) * 4; 
1085 
else

1086 
bound = SBLIMIT; 
1087  
1088 
/* allocation bits */

1089 
for(i=0;i<bound;i++) { 
1090 
for(ch=0;ch<s>nb_channels;ch++) { 
1091 
allocation[ch][i] = get_bits(&s>gb, 4);

1092 
} 
1093 
} 
1094 
for(i=bound;i<SBLIMIT;i++) {

1095 
allocation[0][i] = get_bits(&s>gb, 4); 
1096 
} 
1097  
1098 
/* scale factors */

1099 
for(i=0;i<bound;i++) { 
1100 
for(ch=0;ch<s>nb_channels;ch++) { 
1101 
if (allocation[ch][i])

1102 
scale_factors[ch][i] = get_bits(&s>gb, 6);

1103 
} 
1104 
} 
1105 
for(i=bound;i<SBLIMIT;i++) {

1106 
if (allocation[0][i]) { 
1107 
scale_factors[0][i] = get_bits(&s>gb, 6); 
1108 
scale_factors[1][i] = get_bits(&s>gb, 6); 
1109 
} 
1110 
} 
1111  
1112 
/* compute samples */

1113 
for(j=0;j<12;j++) { 
1114 
for(i=0;i<bound;i++) { 
1115 
for(ch=0;ch<s>nb_channels;ch++) { 
1116 
n = allocation[ch][i]; 
1117 
if (n) {

1118 
mant = get_bits(&s>gb, n + 1);

1119 
v = l1_unscale(n, mant, scale_factors[ch][i]); 
1120 
} else {

1121 
v = 0;

1122 
} 
1123 
s>sb_samples[ch][j][i] = v; 
1124 
} 
1125 
} 
1126 
for(i=bound;i<SBLIMIT;i++) {

1127 
n = allocation[0][i];

1128 
if (n) {

1129 
mant = get_bits(&s>gb, n + 1);

1130 
v = l1_unscale(n, mant, scale_factors[0][i]);

1131 
s>sb_samples[0][j][i] = v;

1132 
v = l1_unscale(n, mant, scale_factors[1][i]);

1133 
s>sb_samples[1][j][i] = v;

1134 
} else {

1135 
s>sb_samples[0][j][i] = 0; 
1136 
s>sb_samples[1][j][i] = 0; 
1137 
} 
1138 
} 
1139 
} 
1140 
return 12; 
1141 
} 
1142  
1143 
/* bitrate is in kb/s */

1144 
int l2_select_table(int bitrate, int nb_channels, int freq, int lsf) 
1145 
{ 
1146 
int ch_bitrate, table;

1147  
1148 
ch_bitrate = bitrate / nb_channels; 
1149 
if (!lsf) {

1150 
if ((freq == 48000 && ch_bitrate >= 56)  
1151 
(ch_bitrate >= 56 && ch_bitrate <= 80)) 
1152 
table = 0;

1153 
else if (freq != 48000 && ch_bitrate >= 96) 
1154 
table = 1;

1155 
else if (freq != 32000 && ch_bitrate <= 48) 
1156 
table = 2;

1157 
else

1158 
table = 3;

1159 
} else {

1160 
table = 4;

1161 
} 
1162 
return table;

1163 
} 
1164  
1165 
static int mp_decode_layer2(MPADecodeContext *s) 
1166 
{ 
1167 
int sblimit; /* number of used subbands */ 
1168 
const unsigned char *alloc_table; 
1169 
int table, bit_alloc_bits, i, j, ch, bound, v;

1170 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; 
1171 
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; 
1172 
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; 
1173 
int scale, qindex, bits, steps, k, l, m, b;

1174  
1175 
/* select decoding table */

1176 
table = l2_select_table(s>bit_rate / 1000, s>nb_channels,

1177 
s>sample_rate, s>lsf); 
1178 
sblimit = ff_mpa_sblimit_table[table]; 
1179 
alloc_table = ff_mpa_alloc_tables[table]; 
1180  
1181 
if (s>mode == MPA_JSTEREO)

1182 
bound = (s>mode_ext + 1) * 4; 
1183 
else

1184 
bound = sblimit; 
1185  
1186 
dprintf(s>avctx, "bound=%d sblimit=%d\n", bound, sblimit);

1187  
1188 
/* sanity check */

1189 
if( bound > sblimit ) bound = sblimit;

1190  
1191 
/* parse bit allocation */

1192 
j = 0;

1193 
for(i=0;i<bound;i++) { 
1194 
bit_alloc_bits = alloc_table[j]; 
1195 
for(ch=0;ch<s>nb_channels;ch++) { 
1196 
bit_alloc[ch][i] = get_bits(&s>gb, bit_alloc_bits); 
1197 
} 
1198 
j += 1 << bit_alloc_bits;

1199 
} 
1200 
for(i=bound;i<sblimit;i++) {

1201 
bit_alloc_bits = alloc_table[j]; 
1202 
v = get_bits(&s>gb, bit_alloc_bits); 
1203 
bit_alloc[0][i] = v;

1204 
bit_alloc[1][i] = v;

1205 
j += 1 << bit_alloc_bits;

1206 
} 
1207  
1208 
#ifdef DEBUG

1209 
{ 
1210 
for(ch=0;ch<s>nb_channels;ch++) { 
1211 
for(i=0;i<sblimit;i++) 
1212 
dprintf(s>avctx, " %d", bit_alloc[ch][i]);

1213 
dprintf(s>avctx, "\n");

1214 
} 
1215 
} 
1216 
#endif

1217  
1218 
/* scale codes */

1219 
for(i=0;i<sblimit;i++) { 
1220 
for(ch=0;ch<s>nb_channels;ch++) { 
1221 
if (bit_alloc[ch][i])

1222 
scale_code[ch][i] = get_bits(&s>gb, 2);

1223 
} 
1224 
} 
1225  
1226 
/* scale factors */

1227 
for(i=0;i<sblimit;i++) { 
1228 
for(ch=0;ch<s>nb_channels;ch++) { 
1229 
if (bit_alloc[ch][i]) {

1230 
sf = scale_factors[ch][i]; 
1231 
switch(scale_code[ch][i]) {

1232 
default:

1233 
case 0: 
1234 
sf[0] = get_bits(&s>gb, 6); 
1235 
sf[1] = get_bits(&s>gb, 6); 
1236 
sf[2] = get_bits(&s>gb, 6); 
1237 
break;

1238 
case 2: 
1239 
sf[0] = get_bits(&s>gb, 6); 
1240 
sf[1] = sf[0]; 
1241 
sf[2] = sf[0]; 
1242 
break;

1243 
case 1: 
1244 
sf[0] = get_bits(&s>gb, 6); 
1245 
sf[2] = get_bits(&s>gb, 6); 
1246 
sf[1] = sf[0]; 
1247 
break;

1248 
case 3: 
1249 
sf[0] = get_bits(&s>gb, 6); 
1250 
sf[2] = get_bits(&s>gb, 6); 
1251 
sf[1] = sf[2]; 
1252 
break;

1253 
} 
1254 
} 
1255 
} 
1256 
} 
1257  
1258 
#ifdef DEBUG

1259 
for(ch=0;ch<s>nb_channels;ch++) { 
1260 
for(i=0;i<sblimit;i++) { 
1261 
if (bit_alloc[ch][i]) {

1262 
sf = scale_factors[ch][i]; 
1263 
dprintf(s>avctx, " %d %d %d", sf[0], sf[1], sf[2]); 
1264 
} else {

1265 
dprintf(s>avctx, " ");

1266 
} 
1267 
} 
1268 
dprintf(s>avctx, "\n");

1269 
} 
1270 
#endif

1271  
1272 
/* samples */

1273 
for(k=0;k<3;k++) { 
1274 
for(l=0;l<12;l+=3) { 
1275 
j = 0;

1276 
for(i=0;i<bound;i++) { 
1277 
bit_alloc_bits = alloc_table[j]; 
1278 
for(ch=0;ch<s>nb_channels;ch++) { 
1279 
b = bit_alloc[ch][i]; 
1280 
if (b) {

1281 
scale = scale_factors[ch][i][k]; 
1282 
qindex = alloc_table[j+b]; 
1283 
bits = ff_mpa_quant_bits[qindex]; 
1284 
if (bits < 0) { 
1285 
/* 3 values at the same time */

1286 
v = get_bits(&s>gb, bits); 
1287 
steps = ff_mpa_quant_steps[qindex]; 
1288 
s>sb_samples[ch][k * 12 + l + 0][i] = 
1289 
l2_unscale_group(steps, v % steps, scale); 
1290 
v = v / steps; 
1291 
s>sb_samples[ch][k * 12 + l + 1][i] = 
1292 
l2_unscale_group(steps, v % steps, scale); 
1293 
v = v / steps; 
1294 
s>sb_samples[ch][k * 12 + l + 2][i] = 
1295 
l2_unscale_group(steps, v, scale); 
1296 
} else {

1297 
for(m=0;m<3;m++) { 
1298 
v = get_bits(&s>gb, bits); 
1299 
v = l1_unscale(bits  1, v, scale);

1300 
s>sb_samples[ch][k * 12 + l + m][i] = v;

1301 
} 
1302 
} 
1303 
} else {

1304 
s>sb_samples[ch][k * 12 + l + 0][i] = 0; 
1305 
s>sb_samples[ch][k * 12 + l + 1][i] = 0; 
1306 
s>sb_samples[ch][k * 12 + l + 2][i] = 0; 
1307 
} 
1308 
} 
1309 
/* next subband in alloc table */

1310 
j += 1 << bit_alloc_bits;

1311 
} 
1312 
/* XXX: find a way to avoid this duplication of code */

1313 
for(i=bound;i<sblimit;i++) {

1314 
bit_alloc_bits = alloc_table[j]; 
1315 
b = bit_alloc[0][i];

1316 
if (b) {

1317 
int mant, scale0, scale1;

1318 
scale0 = scale_factors[0][i][k];

1319 
scale1 = scale_factors[1][i][k];

1320 
qindex = alloc_table[j+b]; 
1321 
bits = ff_mpa_quant_bits[qindex]; 
1322 
if (bits < 0) { 
1323 
/* 3 values at the same time */

1324 
v = get_bits(&s>gb, bits); 
1325 
steps = ff_mpa_quant_steps[qindex]; 
1326 
mant = v % steps; 
1327 
v = v / steps; 
1328 
s>sb_samples[0][k * 12 + l + 0][i] = 
1329 
l2_unscale_group(steps, mant, scale0); 
1330 
s>sb_samples[1][k * 12 + l + 0][i] = 
1331 
l2_unscale_group(steps, mant, scale1); 
1332 
mant = v % steps; 
1333 
v = v / steps; 
1334 
s>sb_samples[0][k * 12 + l + 1][i] = 
1335 
l2_unscale_group(steps, mant, scale0); 
1336 
s>sb_samples[1][k * 12 + l + 1][i] = 
1337 
l2_unscale_group(steps, mant, scale1); 
1338 
s>sb_samples[0][k * 12 + l + 2][i] = 
1339 
l2_unscale_group(steps, v, scale0); 
1340 
s>sb_samples[1][k * 12 + l + 2][i] = 
1341 
l2_unscale_group(steps, v, scale1); 
1342 
} else {

1343 
for(m=0;m<3;m++) { 
1344 
mant = get_bits(&s>gb, bits); 
1345 
s>sb_samples[0][k * 12 + l + m][i] = 
1346 
l1_unscale(bits  1, mant, scale0);

1347 
s>sb_samples[1][k * 12 + l + m][i] = 
1348 
l1_unscale(bits  1, mant, scale1);

1349 
} 
1350 
} 
1351 
} else {

1352 
s>sb_samples[0][k * 12 + l + 0][i] = 0; 
1353 
s>sb_samples[0][k * 12 + l + 1][i] = 0; 
1354 
s>sb_samples[0][k * 12 + l + 2][i] = 0; 
1355 
s>sb_samples[1][k * 12 + l + 0][i] = 0; 
1356 
s>sb_samples[1][k * 12 + l + 1][i] = 0; 
1357 
s>sb_samples[1][k * 12 + l + 2][i] = 0; 
1358 
} 
1359 
/* next subband in alloc table */

1360 
j += 1 << bit_alloc_bits;

1361 
} 
1362 
/* fill remaining samples to zero */

1363 
for(i=sblimit;i<SBLIMIT;i++) {

1364 
for(ch=0;ch<s>nb_channels;ch++) { 
1365 
s>sb_samples[ch][k * 12 + l + 0][i] = 0; 
1366 
s>sb_samples[ch][k * 12 + l + 1][i] = 0; 
1367 
s>sb_samples[ch][k * 12 + l + 2][i] = 0; 
1368 
} 
1369 
} 
1370 
} 
1371 
} 
1372 
return 3 * 12; 
1373 
} 
1374  
1375 
static inline void lsf_sf_expand(int *slen, 
1376 
int sf, int n1, int n2, int n3) 
1377 
{ 
1378 
if (n3) {

1379 
slen[3] = sf % n3;

1380 
sf /= n3; 
1381 
} else {

1382 
slen[3] = 0; 
1383 
} 
1384 
if (n2) {

1385 
slen[2] = sf % n2;

1386 
sf /= n2; 
1387 
} else {

1388 
slen[2] = 0; 
1389 
} 
1390 
slen[1] = sf % n1;

1391 
sf /= n1; 
1392 
slen[0] = sf;

1393 
} 
1394  
1395 
static void exponents_from_scale_factors(MPADecodeContext *s, 
1396 
GranuleDef *g, 
1397 
int16_t *exponents) 
1398 
{ 
1399 
const uint8_t *bstab, *pretab;

1400 
int len, i, j, k, l, v0, shift, gain, gains[3]; 
1401 
int16_t *exp_ptr; 
1402  
1403 
exp_ptr = exponents; 
1404 
gain = g>global_gain  210;

1405 
shift = g>scalefac_scale + 1;

1406  
1407 
bstab = band_size_long[s>sample_rate_index]; 
1408 
pretab = mpa_pretab[g>preflag]; 
1409 
for(i=0;i<g>long_end;i++) { 
1410 
v0 = gain  ((g>scale_factors[i] + pretab[i]) << shift) + 400;

1411 
len = bstab[i]; 
1412 
for(j=len;j>0;j) 
1413 
*exp_ptr++ = v0; 
1414 
} 
1415  
1416 
if (g>short_start < 13) { 
1417 
bstab = band_size_short[s>sample_rate_index]; 
1418 
gains[0] = gain  (g>subblock_gain[0] << 3); 
1419 
gains[1] = gain  (g>subblock_gain[1] << 3); 
1420 
gains[2] = gain  (g>subblock_gain[2] << 3); 
1421 
k = g>long_end; 
1422 
for(i=g>short_start;i<13;i++) { 
1423 
len = bstab[i]; 
1424 
for(l=0;l<3;l++) { 
1425 
v0 = gains[l]  (g>scale_factors[k++] << shift) + 400;

1426 
for(j=len;j>0;j) 
1427 
*exp_ptr++ = v0; 
1428 
} 
1429 
} 
1430 
} 
1431 
} 
1432  
1433 
/* handle n = 0 too */

1434 
static inline int get_bitsz(GetBitContext *s, int n) 
1435 
{ 
1436 
if (n == 0) 
1437 
return 0; 
1438 
else

1439 
return get_bits(s, n);

1440 
} 
1441  
1442  
1443 
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){ 
1444 
if(s>in_gb.buffer && *pos >= s>gb.size_in_bits){

1445 
s>gb= s>in_gb; 
1446 
s>in_gb.buffer=NULL;

1447 
assert((get_bits_count(&s>gb) & 7) == 0); 
1448 
skip_bits_long(&s>gb, *pos  *end_pos); 
1449 
*end_pos2= 
1450 
*end_pos= *end_pos2 + get_bits_count(&s>gb)  *pos; 
1451 
*pos= get_bits_count(&s>gb); 
1452 
} 
1453 
} 
1454  
1455 
static int huffman_decode(MPADecodeContext *s, GranuleDef *g, 
1456 
int16_t *exponents, int end_pos2)

1457 
{ 
1458 
int s_index;

1459 
int i;

1460 
int last_pos, bits_left;

1461 
VLC *vlc; 
1462 
int end_pos= FFMIN(end_pos2, s>gb.size_in_bits);

1463  
1464 
/* low frequencies (called big values) */

1465 
s_index = 0;

1466 
for(i=0;i<3;i++) { 
1467 
int j, k, l, linbits;

1468 
j = g>region_size[i]; 
1469 
if (j == 0) 
1470 
continue;

1471 
/* select vlc table */

1472 
k = g>table_select[i]; 
1473 
l = mpa_huff_data[k][0];

1474 
linbits = mpa_huff_data[k][1];

1475 
vlc = &huff_vlc[l]; 
1476  
1477 
if(!l){

1478 
memset(&g>sb_hybrid[s_index], 0, sizeof(*g>sb_hybrid)*2*j); 
1479 
s_index += 2*j;

1480 
continue;

1481 
} 
1482  
1483 
/* read huffcode and compute each couple */

1484 
for(;j>0;j) { 
1485 
int exponent, x, y, v;

1486 
int pos= get_bits_count(&s>gb);

1487  
1488 
if (pos >= end_pos){

1489 
// av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);

1490 
switch_buffer(s, &pos, &end_pos, &end_pos2); 
1491 
// av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);

1492 
if(pos >= end_pos)

1493 
break;

1494 
} 
1495 
y = get_vlc2(&s>gb, vlc>table, 7, 3); 
1496  
1497 
if(!y){

1498 
g>sb_hybrid[s_index ] = 
1499 
g>sb_hybrid[s_index+1] = 0; 
1500 
s_index += 2;

1501 
continue;

1502 
} 
1503  
1504 
exponent= exponents[s_index]; 
1505  
1506 
dprintf(s>avctx, "region=%d n=%d x=%d y=%d exp=%d\n",

1507 
i, g>region_size[i]  j, x, y, exponent); 
1508 
if(y&16){ 
1509 
x = y >> 5;

1510 
y = y & 0x0f;

1511 
if (x < 15){ 
1512 
v = expval_table[ exponent ][ x ]; 
1513 
// v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0  (exponent>>2), 31);

1514 
}else{

1515 
x += get_bitsz(&s>gb, linbits); 
1516 
v = l3_unscale(x, exponent); 
1517 
} 
1518 
if (get_bits1(&s>gb))

1519 
v = v; 
1520 
g>sb_hybrid[s_index] = v; 
1521 
if (y < 15){ 
1522 
v = expval_table[ exponent ][ y ]; 
1523 
}else{

1524 
y += get_bitsz(&s>gb, linbits); 
1525 
v = l3_unscale(y, exponent); 
1526 
} 
1527 
if (get_bits1(&s>gb))

1528 
v = v; 
1529 
g>sb_hybrid[s_index+1] = v;

1530 
}else{

1531 
x = y >> 5;

1532 
y = y & 0x0f;

1533 
x += y; 
1534 
if (x < 15){ 
1535 
v = expval_table[ exponent ][ x ]; 
1536 
}else{

1537 
x += get_bitsz(&s>gb, linbits); 
1538 
v = l3_unscale(x, exponent); 
1539 
} 
1540 
if (get_bits1(&s>gb))

1541 
v = v; 
1542 
g>sb_hybrid[s_index+!!y] = v; 
1543 
g>sb_hybrid[s_index+ !y] = 0;

1544 
} 
1545 
s_index+=2;

1546 
} 
1547 
} 
1548  
1549 
/* high frequencies */

1550 
vlc = &huff_quad_vlc[g>count1table_select]; 
1551 
last_pos=0;

1552 
while (s_index <= 572) { 
1553 
int pos, code;

1554 
pos = get_bits_count(&s>gb); 
1555 
if (pos >= end_pos) {

1556 
if (pos > end_pos2 && last_pos){

1557 
/* some encoders generate an incorrect size for this

1558 
part. We must go back into the data */

1559 
s_index = 4;

1560 
skip_bits_long(&s>gb, last_pos  pos); 
1561 
av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos  pos, end_pospos, end_pos2pos); 
1562 
if(s>error_resilience >= FF_ER_COMPLIANT)

1563 
s_index=0;

1564 
break;

1565 
} 
1566 
// av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);

1567 
switch_buffer(s, &pos, &end_pos, &end_pos2); 
1568 
// av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);

1569 
if(pos >= end_pos)

1570 
break;

1571 
} 
1572 
last_pos= pos; 
1573  
1574 
code = get_vlc2(&s>gb, vlc>table, vlc>bits, 1);

1575 
dprintf(s>avctx, "t=%d code=%d\n", g>count1table_select, code);

1576 
g>sb_hybrid[s_index+0]=

1577 
g>sb_hybrid[s_index+1]=

1578 
g>sb_hybrid[s_index+2]=

1579 
g>sb_hybrid[s_index+3]= 0; 
1580 
while(code){

1581 
static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0}; 
1582 
int v;

1583 
int pos= s_index+idxtab[code];

1584 
code ^= 8>>idxtab[code];

1585 
v = exp_table[ exponents[pos] ]; 
1586 
// v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0  (exponents[pos]>>2), 31);

1587 
if(get_bits1(&s>gb))

1588 
v = v; 
1589 
g>sb_hybrid[pos] = v; 
1590 
} 
1591 
s_index+=4;

1592 
} 
1593 
/* skip extension bits */

1594 
bits_left = end_pos2  get_bits_count(&s>gb); 
1595 
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s>in_gb.buffer);

1596 
if (bits_left < 0/*  bits_left > 500*/) { 
1597 
av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left); 
1598 
s_index=0;

1599 
}else if(bits_left > 0 && s>error_resilience >= FF_ER_AGGRESSIVE){ 
1600 
av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left); 
1601 
s_index=0;

1602 
} 
1603 
memset(&g>sb_hybrid[s_index], 0, sizeof(*g>sb_hybrid)*(576  s_index)); 
1604 
skip_bits_long(&s>gb, bits_left); 
1605  
1606 
i= get_bits_count(&s>gb); 
1607 
switch_buffer(s, &i, &end_pos, &end_pos2); 
1608  
1609 
return 0; 
1610 
} 
1611  
1612 
/* Reorder short blocks from bitstream order to interleaved order. It

1613 
would be faster to do it in parsing, but the code would be far more

1614 
complicated */

1615 
static void reorder_block(MPADecodeContext *s, GranuleDef *g) 
1616 
{ 
1617 
int i, j, len;

1618 
int32_t *ptr, *dst, *ptr1; 
1619 
int32_t tmp[576];

1620  
1621 
if (g>block_type != 2) 
1622 
return;

1623  
1624 
if (g>switch_point) {

1625 
if (s>sample_rate_index != 8) { 
1626 
ptr = g>sb_hybrid + 36;

1627 
} else {

1628 
ptr = g>sb_hybrid + 48;

1629 
} 
1630 
} else {

1631 
ptr = g>sb_hybrid; 
1632 
} 
1633  
1634 
for(i=g>short_start;i<13;i++) { 
1635 
len = band_size_short[s>sample_rate_index][i]; 
1636 
ptr1 = ptr; 
1637 
dst = tmp; 
1638 
for(j=len;j>0;j) { 
1639 
*dst++ = ptr[0*len];

1640 
*dst++ = ptr[1*len];

1641 
*dst++ = ptr[2*len];

1642 
ptr++; 
1643 
} 
1644 
ptr+=2*len;

1645 
memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1)); 
1646 
} 
1647 
} 
1648  
1649 
#define ISQRT2 FIXR(0.70710678118654752440) 
1650  
1651 
static void compute_stereo(MPADecodeContext *s, 
1652 
GranuleDef *g0, GranuleDef *g1) 
1653 
{ 
1654 
int i, j, k, l;

1655 
int32_t v1, v2; 
1656 
int sf_max, tmp0, tmp1, sf, len, non_zero_found;

1657 
int32_t (*is_tab)[16];

1658 
int32_t *tab0, *tab1; 
1659 
int non_zero_found_short[3]; 
1660  
1661 
/* intensity stereo */

1662 
if (s>mode_ext & MODE_EXT_I_STEREO) {

1663 
if (!s>lsf) {

1664 
is_tab = is_table; 
1665 
sf_max = 7;

1666 
} else {

1667 
is_tab = is_table_lsf[g1>scalefac_compress & 1];

1668 
sf_max = 16;

1669 
} 
1670  
1671 
tab0 = g0>sb_hybrid + 576;

1672 
tab1 = g1>sb_hybrid + 576;

1673  
1674 
non_zero_found_short[0] = 0; 
1675 
non_zero_found_short[1] = 0; 
1676 
non_zero_found_short[2] = 0; 
1677 
k = (13  g1>short_start) * 3 + g1>long_end  3; 
1678 
for(i = 12;i >= g1>short_start;i) { 
1679 
/* for last band, use previous scale factor */

1680 
if (i != 11) 
1681 
k = 3;

1682 
len = band_size_short[s>sample_rate_index][i]; 
1683 
for(l=2;l>=0;l) { 
1684 
tab0 = len; 
1685 
tab1 = len; 
1686 
if (!non_zero_found_short[l]) {

1687 
/* test if non zero band. if so, stop doing istereo */

1688 
for(j=0;j<len;j++) { 
1689 
if (tab1[j] != 0) { 
1690 
non_zero_found_short[l] = 1;

1691 
goto found1;

1692 
} 
1693 
} 
1694 
sf = g1>scale_factors[k + l]; 
1695 
if (sf >= sf_max)

1696 
goto found1;

1697  
1698 
v1 = is_tab[0][sf];

1699 
v2 = is_tab[1][sf];

1700 
for(j=0;j<len;j++) { 
1701 
tmp0 = tab0[j]; 
1702 
tab0[j] = MULL(tmp0, v1); 
1703 
tab1[j] = MULL(tmp0, v2); 
1704 
} 
1705 
} else {

1706 
found1:

1707 
if (s>mode_ext & MODE_EXT_MS_STEREO) {

1708 
/* lower part of the spectrum : do ms stereo

1709 
if enabled */

1710 
for(j=0;j<len;j++) { 
1711 
tmp0 = tab0[j]; 
1712 
tmp1 = tab1[j]; 
1713 
tab0[j] = MULL(tmp0 + tmp1, ISQRT2); 
1714 
tab1[j] = MULL(tmp0  tmp1, ISQRT2); 
1715 
} 
1716 
} 
1717 
} 
1718 
} 
1719 
} 
1720  
1721 
non_zero_found = non_zero_found_short[0] 

1722 
non_zero_found_short[1] 

1723 
non_zero_found_short[2];

1724  
1725 
for(i = g1>long_end  1;i >= 0;i) { 
1726 
len = band_size_long[s>sample_rate_index][i]; 
1727 
tab0 = len; 
1728 
tab1 = len; 
1729 
/* test if non zero band. if so, stop doing istereo */

1730 
if (!non_zero_found) {

1731 
for(j=0;j<len;j++) { 
1732 
if (tab1[j] != 0) { 
1733 
non_zero_found = 1;

1734 
goto found2;

1735 
} 
1736 
} 
1737 
/* for last band, use previous scale factor */

1738 
k = (i == 21) ? 20 : i; 
1739 
sf = g1>scale_factors[k]; 
1740 
if (sf >= sf_max)

1741 
goto found2;

1742 
v1 = is_tab[0][sf];

1743 
v2 = is_tab[1][sf];

1744 
for(j=0;j<len;j++) { 
1745 
tmp0 = tab0[j]; 
1746 
tab0[j] = MULL(tmp0, v1); 
1747 
tab1[j] = MULL(tmp0, v2); 
1748 
} 
1749 
} else {

1750 
found2:

1751 
if (s>mode_ext & MODE_EXT_MS_STEREO) {

1752 
/* lower part of the spectrum : do ms stereo

1753 
if enabled */

1754 
for(j=0;j<len;j++) { 
1755 
tmp0 = tab0[j]; 
1756 
tmp1 = tab1[j]; 
1757 
tab0[j] = MULL(tmp0 + tmp1, ISQRT2); 
1758 
tab1[j] = MULL(tmp0  tmp1, ISQRT2); 
1759 
} 
1760 
} 
1761 
} 
1762 
} 
1763 
} else if (s>mode_ext & MODE_EXT_MS_STEREO) { 
1764 
/* ms stereo ONLY */

1765 
/* NOTE: the 1/sqrt(2) normalization factor is included in the

1766 
global gain */

1767 
tab0 = g0>sb_hybrid; 
1768 
tab1 = g1>sb_hybrid; 
1769 
for(i=0;i<576;i++) { 
1770 
tmp0 = tab0[i]; 
1771 
tmp1 = tab1[i]; 
1772 
tab0[i] = tmp0 + tmp1; 
1773 
tab1[i] = tmp0  tmp1; 
1774 
} 
1775 
} 
1776 
} 
1777  
1778 
static void compute_antialias_integer(MPADecodeContext *s, 
1779 
GranuleDef *g) 
1780 
{ 
1781 
int32_t *ptr, *csa; 
1782 
int n, i;

1783  
1784 
/* we antialias only "long" bands */

1785 
if (g>block_type == 2) { 
1786 
if (!g>switch_point)

1787 
return;

1788 
/* XXX: check this for 8000Hz case */

1789 
n = 1;

1790 
} else {

1791 
n = SBLIMIT  1;

1792 
} 
1793  
1794 
ptr = g>sb_hybrid + 18;

1795 
for(i = n;i > 0;i) { 
1796 
int tmp0, tmp1, tmp2;

1797 
csa = &csa_table[0][0]; 
1798 
#define INT_AA(j) \

1799 
tmp0 = ptr[1j];\

1800 
tmp1 = ptr[ j];\ 
1801 
tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\ 
1802 
ptr[1j] = 4*(tmp2  MULH(tmp1, csa[2+4*j]));\ 
1803 
ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j])); 
1804  
1805 
INT_AA(0)

1806 
INT_AA(1)

1807 
INT_AA(2)

1808 
INT_AA(3)

1809 
INT_AA(4)

1810 
INT_AA(5)

1811 
INT_AA(6)

1812 
INT_AA(7)

1813  
1814 
ptr += 18;

1815 
} 
1816 
} 
1817  
1818 
static void compute_antialias_float(MPADecodeContext *s, 
1819 
GranuleDef *g) 
1820 
{ 
1821 
int32_t *ptr; 
1822 
int n, i;

1823  
1824 
/* we antialias only "long" bands */

1825 
if (g>block_type == 2) { 
1826 
if (!g>switch_point)

1827 
return;

1828 
/* XXX: check this for 8000Hz case */

1829 
n = 1;

1830 
} else {

1831 
n = SBLIMIT  1;

1832 
} 
1833  
1834 
ptr = g>sb_hybrid + 18;

1835 
for(i = n;i > 0;i) { 
1836 
float tmp0, tmp1;

1837 
float *csa = &csa_table_float[0][0]; 
1838 
#define FLOAT_AA(j)\

1839 
tmp0= ptr[1j];\

1840 
tmp1= ptr[ j];\ 
1841 
ptr[1j] = lrintf(tmp0 * csa[0+4*j]  tmp1 * csa[1+4*j]);\ 
1842 
ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]); 
1843  
1844 
FLOAT_AA(0)

1845 
FLOAT_AA(1)

1846 
FLOAT_AA(2)

1847 
FLOAT_AA(3)

1848 
FLOAT_AA(4)

1849 
FLOAT_AA(5)

1850 
FLOAT_AA(6)

1851 
FLOAT_AA(7)

1852  
1853 
ptr += 18;

1854 
} 
1855 
} 
1856  
1857 
static void compute_imdct(MPADecodeContext *s, 
1858 
GranuleDef *g, 
1859 
int32_t *sb_samples, 
1860 
int32_t *mdct_buf) 
1861 
{ 
1862 
int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1; 
1863 
int32_t out2[12];

1864 
int i, j, mdct_long_end, v, sblimit;

1865  
1866 
/* find last non zero block */

1867 
ptr = g>sb_hybrid + 576;

1868 
ptr1 = g>sb_hybrid + 2 * 18; 
1869 
while (ptr >= ptr1) {

1870 
ptr = 6;

1871 
v = ptr[0]  ptr[1]  ptr[2]  ptr[3]  ptr[4]  ptr[5]; 
1872 
if (v != 0) 
1873 
break;

1874 
} 
1875 
sblimit = ((ptr  g>sb_hybrid) / 18) + 1; 
1876  
1877 
if (g>block_type == 2) { 
1878 
/* XXX: check for 8000 Hz */

1879 
if (g>switch_point)

1880 
mdct_long_end = 2;

1881 
else

1882 
mdct_long_end = 0;

1883 
} else {

1884 
mdct_long_end = sblimit; 
1885 
} 
1886  
1887 
buf = mdct_buf; 
1888 
ptr = g>sb_hybrid; 
1889 
for(j=0;j<mdct_long_end;j++) { 
1890 
/* apply window & overlap with previous buffer */

1891 
out_ptr = sb_samples + j; 
1892 
/* select window */

1893 
if (g>switch_point && j < 2) 
1894 
win1 = mdct_win[0];

1895 
else

1896 
win1 = mdct_win[g>block_type]; 
1897 
/* select frequency inversion */

1898 
win = win1 + ((4 * 36) & (j & 1)); 
1899 
imdct36(out_ptr, buf, ptr, win); 
1900 
out_ptr += 18*SBLIMIT;

1901 
ptr += 18;

1902 
buf += 18;

1903 
} 
1904 
for(j=mdct_long_end;j<sblimit;j++) {

1905 
/* select frequency inversion */

1906 
win = mdct_win[2] + ((4 * 36) & (j & 1)); 
1907 
out_ptr = sb_samples + j; 
1908  
1909 
for(i=0; i<6; i++){ 
1910 
*out_ptr = buf[i]; 
1911 
out_ptr += SBLIMIT; 
1912 
} 
1913 
imdct12(out2, ptr + 0);

1914 
for(i=0;i<6;i++) { 
1915 
*out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1]; 
1916 
buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]); 
1917 
out_ptr += SBLIMIT; 
1918 
} 
1919 
imdct12(out2, ptr + 1);

1920 
for(i=0;i<6;i++) { 
1921 
*out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2]; 
1922 
buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]); 
1923 
out_ptr += SBLIMIT; 
1924 
} 
1925 
imdct12(out2, ptr + 2);

1926 
for(i=0;i<6;i++) { 
1927 
buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0]; 
1928 
buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]); 
1929 
buf[i + 6*2] = 0; 
1930 
} 
1931 
ptr += 18;

1932 
buf += 18;

1933 
} 
1934 
/* zero bands */

1935 
for(j=sblimit;j<SBLIMIT;j++) {

1936 
/* overlap */

1937 
out_ptr = sb_samples + j; 
1938 
for(i=0;i<18;i++) { 
1939 
*out_ptr = buf[i]; 
1940 
buf[i] = 0;

1941 
out_ptr += SBLIMIT; 
1942 
} 
1943 
buf += 18;

1944 
} 
1945 
} 
1946  
1947 
#if defined(DEBUG)

1948 
void sample_dump(int fnum, int32_t *tab, int n) 
1949 
{ 
1950 
static FILE *files[16], *f; 
1951 
char buf[512]; 
1952 
int i;

1953 
int32_t v; 
1954  
1955 
f = files[fnum]; 
1956 
if (!f) {

1957 
snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm", 
1958 
fnum, 
1959 
#ifdef USE_HIGHPRECISION

1960 
"hp"

1961 
#else

1962 
"lp"

1963 
#endif

1964 
); 
1965 
f = fopen(buf, "w");

1966 
if (!f)

1967 
return;

1968 
files[fnum] = f; 
1969 
} 
1970  
1971 
if (fnum == 0) { 
1972 
static int pos = 0; 
1973 
av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos); 
1974 
for(i=0;i<n;i++) { 
1975 
av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE); 
1976 
if ((i % 18) == 17) 
1977 
av_log(NULL, AV_LOG_DEBUG, "\n"); 
1978 
} 
1979 
pos += n; 
1980 
} 
1981 
for(i=0;i<n;i++) { 
1982 
/* normalize to 23 frac bits */

1983 
v = tab[i] << (23  FRAC_BITS);

1984 
fwrite(&v, 1, sizeof(int32_t), f); 
1985 
} 
1986 
} 
1987 
#endif

1988  
1989  
1990 
/* main layer3 decoding function */

1991 
static int mp_decode_layer3(MPADecodeContext *s) 
1992 
{ 
1993 
int nb_granules, main_data_begin, private_bits;

1994 
int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;

1995 
GranuleDef granules[2][2], *g; 
1996 
int16_t exponents[576];

1997  
1998 
/* read side info */

1999 
if (s>lsf) {

2000 
main_data_begin = get_bits(&s>gb, 8);

2001 
private_bits = get_bits(&s>gb, s>nb_channels); 
2002 
nb_granules = 1;

2003 
} else {

2004 
main_data_begin = get_bits(&s>gb, 9);

2005 
if (s>nb_channels == 2) 
2006 
private_bits = get_bits(&s>gb, 3);

2007 
else

2008 
private_bits = get_bits(&s>gb, 5);

2009 
nb_granules = 2;

2010 
for(ch=0;ch<s>nb_channels;ch++) { 
2011 
granules[ch][0].scfsi = 0; /* all scale factors are transmitted */ 
2012 
granules[ch][1].scfsi = get_bits(&s>gb, 4); 
2013 
} 
2014 
} 
2015  
2016 
for(gr=0;gr<nb_granules;gr++) { 
2017 
for(ch=0;ch<s>nb_channels;ch++) { 
2018 
dprintf(s>avctx, "gr=%d ch=%d: side_info\n", gr, ch);

2019 
g = &granules[ch][gr]; 
2020 
g>part2_3_length = get_bits(&s>gb, 12);

2021 
g>big_values = get_bits(&s>gb, 9);

2022 
if(g>big_values > 288){ 
2023 
av_log(s>avctx, AV_LOG_ERROR, "big_values too big\n");

2024 
return 1; 
2025 
} 
2026  
2027 
g>global_gain = get_bits(&s>gb, 8);

2028 
/* if MS stereo only is selected, we precompute the

2029 
1/sqrt(2) renormalization factor */

2030 
if ((s>mode_ext & (MODE_EXT_MS_STEREO  MODE_EXT_I_STEREO)) ==

2031 
MODE_EXT_MS_STEREO) 
2032 
g>global_gain = 2;

2033 
if (s>lsf)

2034 
g>scalefac_compress = get_bits(&s>gb, 9);

2035 
else

2036 
g>scalefac_compress = get_bits(&s>gb, 4);

2037 
blocksplit_flag = get_bits(&s>gb, 1);

2038 
if (blocksplit_flag) {

2039 
g>block_type = get_bits(&s>gb, 2);

2040 
if (g>block_type == 0){ 
2041 
av_log(NULL, AV_LOG_ERROR, "invalid block type\n"); 
2042 
return 1; 
2043 
} 
2044 
g>switch_point = get_bits(&s>gb, 1);

2045 
for(i=0;i<2;i++) 
2046 
g>table_select[i] = get_bits(&s>gb, 5);

2047 
for(i=0;i<3;i++) 
2048 
g>subblock_gain[i] = get_bits(&s>gb, 3);

2049 
/* compute huffman coded region sizes */

2050 
if (g>block_type == 2) 
2051 
g>region_size[0] = (36 / 2); 
2052 
else {

2053 
if (s>sample_rate_index <= 2) 
2054 
g>region_size[0] = (36 / 2); 
2055 
else if (s>sample_rate_index != 8) 
2056 
g>region_size[0] = (54 / 2); 
2057 
else

2058 
g>region_size[0] = (108 / 2); 
2059 
} 
2060 
g>region_size[1] = (576 / 2); 
2061 
} else {

2062 
int region_address1, region_address2, l;

2063 
g>block_type = 0;

2064 
g>switch_point = 0;

2065 
for(i=0;i<3;i++) 
2066 
g>table_select[i] = get_bits(&s>gb, 5);

2067 
/* compute huffman coded region sizes */

2068 
region_address1 = get_bits(&s>gb, 4);

2069 
region_address2 = get_bits(&s>gb, 3);

2070 
dprintf(s>avctx, "region1=%d region2=%d\n",

2071 
region_address1, region_address2); 
2072 
g>region_size[0] =

2073 
band_index_long[s>sample_rate_index][region_address1 + 1] >> 1; 
2074 
l = region_address1 + region_address2 + 2;

2075 
/* should not overflow */

2076 
if (l > 22) 
2077 
l = 22;

2078 
g>region_size[1] =

2079 
band_index_long[s>sample_rate_index][l] >> 1;

2080 
} 
2081 
/* convert region offsets to region sizes and truncate

2082 
size to big_values */

2083 
g>region_size[2] = (576 / 2); 
2084 
j = 0;

2085 
for(i=0;i<3;i++) { 
2086 
k = FFMIN(g>region_size[i], g>big_values); 
2087 
g>region_size[i] = k  j; 
2088 
j = k; 
2089 
} 
2090  
2091 
/* compute band indexes */

2092 
if (g>block_type == 2) { 
2093 
if (g>switch_point) {

2094 
/* if switched mode, we handle the 36 first samples as

2095 
long blocks. For 8000Hz, we handle the 48 first

2096 
exponents as long blocks (XXX: check this!) */

2097 
if (s>sample_rate_index <= 2) 
2098 
g>long_end = 8;

2099 
else if (s>sample_rate_index != 8) 
2100 
g>long_end = 6;

2101 
else

2102 
g>long_end = 4; /* 8000 Hz */ 
2103  
2104 
g>short_start = 2 + (s>sample_rate_index != 8); 
2105 
} else {

2106 
g>long_end = 0;

2107 
g>short_start = 0;

2108 
} 
2109 
} else {

2110 
g>short_start = 13;

2111 
g>long_end = 22;

2112 
} 
2113  
2114 
g>preflag = 0;

2115 
if (!s>lsf)

2116 
g>preflag = get_bits(&s>gb, 1);

2117 
g>scalefac_scale = get_bits(&s>gb, 1);

2118 
g>count1table_select = get_bits(&s>gb, 1);

2119 
dprintf(s>avctx, "block_type=%d switch_point=%d\n",

2120 
g>block_type, g>switch_point); 
2121 
} 
2122 
} 
2123  
2124 
if (!s>adu_mode) {

2125 
const uint8_t *ptr = s>gb.buffer + (get_bits_count(&s>gb)>>3); 
2126 
assert((get_bits_count(&s>gb) & 7) == 0); 
2127 
/* now we get bits from the main_data_begin offset */

2128 
dprintf(s>avctx, "seekback: %d\n", main_data_begin);

2129 
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s>last_buf_size);

2130  
2131 
memcpy(s>last_buf + s>last_buf_size, ptr, EXTRABYTES); 
2132 
s>in_gb= s>gb; 
2133 
init_get_bits(&s>gb, s>last_buf, s>last_buf_size*8);

2134 
skip_bits_long(&s>gb, 8*(s>last_buf_size  main_data_begin));

2135 
} 
2136  
2137 
for(gr=0;gr<nb_granules;gr++) { 
2138 
for(ch=0;ch<s>nb_channels;ch++) { 
2139 
g = &granules[ch][gr]; 
2140 
if(get_bits_count(&s>gb)<0){ 
2141 
av_log(NULL, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n", 
2142 
main_data_begin, s>last_buf_size, gr); 
2143 
skip_bits_long(&s>gb, g>part2_3_length); 
2144 
memset(g>sb_hybrid, 0, sizeof(g>sb_hybrid)); 
2145 
if(get_bits_count(&s>gb) >= s>gb.size_in_bits && s>in_gb.buffer){

2146 
skip_bits_long(&s>in_gb, get_bits_count(&s>gb)  s>gb.size_in_bits); 
2147 
s>gb= s>in_gb; 
2148 
s>in_gb.buffer=NULL;

2149 
} 
2150 
continue;

2151 
} 
2152  
2153 
bits_pos = get_bits_count(&s>gb); 
2154  
2155 
if (!s>lsf) {

2156 
uint8_t *sc; 
2157 
int slen, slen1, slen2;

2158  
2159 
/* MPEG1 scale factors */

2160 
slen1 = slen_table[0][g>scalefac_compress];

2161 
slen2 = slen_table[1][g>scalefac_compress];

2162 
dprintf(s>avctx, "slen1=%d slen2=%d\n", slen1, slen2);

2163 
if (g>block_type == 2) { 
2164 
n = g>switch_point ? 17 : 18; 
2165 
j = 0;

2166 
if(slen1){

2167 
for(i=0;i<n;i++) 
2168 
g>scale_factors[j++] = get_bits(&s>gb, slen1); 
2169 
}else{

2170 
for(i=0;i<n;i++) 
2171 
g>scale_factors[j++] = 0;

2172 
} 
2173 
if(slen2){

2174 
for(i=0;i<18;i++) 
2175 
g>scale_factors[j++] = get_bits(&s>gb, slen2); 
2176 
for(i=0;i<3;i++) 
2177 
g>scale_factors[j++] = 0;

2178 
}else{

2179 
for(i=0;i<21;i++) 
2180 
g>scale_factors[j++] = 0;

2181 
} 
2182 
} else {

2183 
sc = granules[ch][0].scale_factors;

2184 
j = 0;

2185 
for(k=0;k<4;k++) { 
2186 
n = (k == 0 ? 6 : 5); 
2187 
if ((g>scfsi & (0x8 >> k)) == 0) { 
2188 
slen = (k < 2) ? slen1 : slen2;

2189 
if(slen){

2190 
for(i=0;i<n;i++) 
2191 
g>scale_factors[j++] = get_bits(&s>gb, slen); 
2192 
}else{

2193 
for(i=0;i<n;i++) 
2194 
g>scale_factors[j++] = 0;

2195 
} 
2196 
} else {

2197 
/* simply copy from last granule */

2198 
for(i=0;i<n;i++) { 
2199 
g>scale_factors[j] = sc[j]; 
2200 
j++; 
2201 
} 
2202 
} 
2203 
} 
2204 
g>scale_factors[j++] = 0;

2205 
} 
2206 
#if defined(DEBUG)

2207 
{ 
2208 
dprintf(s>avctx, "scfsi=%x gr=%d ch=%d scale_factors:\n",

2209 
g>scfsi, gr, ch); 
2210 
for(i=0;i<j;i++) 
2211 
dprintf(s>avctx, " %d", g>scale_factors[i]);

2212 
dprintf(s>avctx, "\n");

2213 
} 
2214 
#endif

2215 
} else {

2216 
int tindex, tindex2, slen[4], sl, sf; 
2217  
2218 
/* LSF scale factors */

2219 
if (g>block_type == 2) { 
2220 
tindex = g>switch_point ? 2 : 1; 
2221 
} else {

2222 
tindex = 0;

2223 
} 
2224 
sf = g>scalefac_compress; 
2225 
if ((s>mode_ext & MODE_EXT_I_STEREO) && ch == 1) { 
2226 
/* intensity stereo case */

2227 
sf >>= 1;

2228 
if (sf < 180) { 
2229 
lsf_sf_expand(slen, sf, 6, 6, 0); 
2230 
tindex2 = 3;

2231 
} else if (sf < 244) { 
2232 
lsf_sf_expand(slen, sf  180, 4, 4, 0); 
2233 
tindex2 = 4;

2234 
} else {

2235 
lsf_sf_expand(slen, sf  244, 3, 0, 0); 
2236 
tindex2 = 5;

2237 
} 
2238 
} else {

2239 
/* normal case */

2240 
if (sf < 400) { 
2241 
lsf_sf_expand(slen, sf, 5, 4, 4); 
2242 
tindex2 = 0;

2243 
} else if (sf < 500) { 
2244 
lsf_sf_expand(slen, sf  400, 5, 4, 0); 
2245 
tindex2 = 1;

2246 
} else {

2247 
lsf_sf_expand(slen, sf  500, 3, 0, 0); 
2248 
tindex2 = 2;

2249 
g>preflag = 1;

2250 
} 
2251 
} 
2252  
2253 
j = 0;

2254 
for(k=0;k<4;k++) { 
2255 
n = lsf_nsf_table[tindex2][tindex][k]; 
2256 
sl = slen[k]; 
2257 
if(sl){

2258 
for(i=0;i<n;i++) 
2259 
g>scale_factors[j++] = get_bits(&s>gb, sl); 
2260 
}else{

2261 
for(i=0;i<n;i++) 
2262 
g>scale_factors[j++] = 0;

2263 
} 
2264 
} 
2265 
/* XXX: should compute exact size */

2266 
for(;j<40;j++) 
2267 
g>scale_factors[j] = 0;

2268 
#if defined(DEBUG)

2269 
{ 
2270 
dprintf(s>avctx, "gr=%d ch=%d scale_factors:\n",

2271 
gr, ch); 
2272 
for(i=0;i<40;i++) 
2273 
dprintf(s>avctx, " %d", g>scale_factors[i]);

2274 
dprintf(s>avctx, "\n");

2275 
} 
2276 
#endif

2277 
} 
2278  
2279 
exponents_from_scale_factors(s, g, exponents); 
2280  
2281 
/* read Huffman coded residue */

2282 
huffman_decode(s, g, exponents, bits_pos + g>part2_3_length); 
2283 
#if defined(DEBUG)

2284 
sample_dump(0, g>sb_hybrid, 576); 
2285 
#endif

2286 
} /* ch */

2287  
2288 
if (s>nb_channels == 2) 
2289 
compute_stereo(s, &granules[0][gr], &granules[1][gr]); 
2290  
2291 
for(ch=0;ch<s>nb_channels;ch++) { 
2292 
g = &granules[ch][gr]; 
2293  
2294 
reorder_block(s, g); 
2295 
#if defined(DEBUG)

2296 
sample_dump(0, g>sb_hybrid, 576); 
2297 
#endif

2298 
s>compute_antialias(s, g); 
2299 
#if defined(DEBUG)

2300 
sample_dump(1, g>sb_hybrid, 576); 
2301 
#endif

2302 
compute_imdct(s, g, &s>sb_samples[ch][18 * gr][0], s>mdct_buf[ch]); 
2303 
#if defined(DEBUG)

2304 
sample_dump(2, &s>sb_samples[ch][18 * gr][0], 576); 
2305 
#endif

2306 
} 
2307 
} /* gr */

2308 
if(get_bits_count(&s>gb)<0) 
2309 
skip_bits_long(&s>gb, get_bits_count(&s>gb)); 
2310 
return nb_granules * 18; 
2311 
} 
2312  
2313 
static int mp_decode_frame(MPADecodeContext *s, 
2314 
OUT_INT *samples, const uint8_t *buf, int buf_size) 
2315 
{ 
2316 
int i, nb_frames, ch;

2317 
OUT_INT *samples_ptr; 
2318  
2319 
init_get_bits(&s>gb, buf + HEADER_SIZE, (buf_size  HEADER_SIZE)*8);

2320  
2321 
/* skip error protection field */

2322 
if (s>error_protection)

2323 
get_bits(&s>gb, 16);

2324  
2325 
dprintf(s>avctx, "frame %d:\n", s>frame_count);

2326 
switch(s>layer) {

2327 
case 1: 
2328 
nb_frames = mp_decode_layer1(s); 
2329 
break;

2330 
case 2: 
2331 
nb_frames = mp_decode_layer2(s); 
2332 
break;

2333 
case 3: 
2334 
default:

2335 
nb_frames = mp_decode_layer3(s); 
2336  
2337 
s>last_buf_size=0;

2338 
if(s>in_gb.buffer){

2339 
align_get_bits(&s>gb); 
2340 
i= (s>gb.size_in_bits  get_bits_count(&s>gb))>>3;

2341 
if(i >= 0 && i <= BACKSTEP_SIZE){ 
2342 
memmove(s>last_buf, s>gb.buffer + (get_bits_count(&s>gb)>>3), i);

2343 
s>last_buf_size=i; 
2344 
}else

2345 
av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i); 
2346 
s>gb= s>in_gb; 
2347 
s>in_gb.buffer= NULL;

2348 
} 
2349  
2350 
align_get_bits(&s>gb); 
2351 
assert((get_bits_count(&s>gb) & 7) == 0); 
2352 
i= (s>gb.size_in_bits  get_bits_count(&s>gb))>>3;

2353  
2354 
if(i<0  i > BACKSTEP_SIZE  nb_frames<0){ 
2355 
av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i); 
2356 
i= FFMIN(BACKSTEP_SIZE, buf_size  HEADER_SIZE); 
2357 
} 
2358 
assert(i <= buf_size  HEADER_SIZE && i>= 0);

2359 
memcpy(s>last_buf + s>last_buf_size, s>gb.buffer + buf_size  HEADER_SIZE  i, i); 
2360 
s>last_buf_size += i; 
2361  
2362 
break;

2363 
} 
2364 
#if defined(DEBUG)

2365 
for(i=0;i<nb_frames;i++) { 
2366 
for(ch=0;ch<s>nb_channels;ch++) { 
2367 
int j;

2368 
dprintf(s>avctx, "%d%d:", i, ch);

2369 
for(j=0;j<SBLIMIT;j++) 
2370 
dprintf(s>avctx, " %0.6f", (double)s>sb_samples[ch][i][j] / FRAC_ONE); 
2371 
dprintf(s>avctx, "\n");

2372 
} 
2373 
} 
2374 
#endif

2375 
/* apply the synthesis filter */

2376 
for(ch=0;ch<s>nb_channels;ch++) { 
2377 
samples_ptr = samples + ch; 
2378 
for(i=0;i<nb_frames;i++) { 
2379 
ff_mpa_synth_filter(s>synth_buf[ch], &(s>synth_buf_offset[ch]), 
2380 
window, &s>dither_state, 
2381 
samples_ptr, s>nb_channels, 
2382 
s>sb_samples[ch][i]); 
2383 
samples_ptr += 32 * s>nb_channels;

2384 
} 
2385 
} 
2386 
#ifdef DEBUG

2387 
s>frame_count++; 
2388 
#endif

2389 
return nb_frames * 32 * sizeof(OUT_INT) * s>nb_channels; 
2390 
} 
2391  
2392 
static int decode_frame(AVCodecContext * avctx, 
2393 
void *data, int *data_size, 
2394 
uint8_t * buf, int buf_size)

2395 
{ 
2396 
MPADecodeContext *s = avctx>priv_data; 
2397 
uint32_t header; 
2398 
int out_size;

2399 
OUT_INT *out_samples = data; 
2400  
2401 
retry:

2402 
if(buf_size < HEADER_SIZE)

2403 
return 1; 
2404  
2405 
header = (buf[0] << 24)  (buf[1] << 16)  (buf[2] << 8)  buf[3]; 
2406 
if(ff_mpa_check_header(header) < 0){ 
2407 
buf++; 
2408 
// buf_size;

2409 
av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");

2410 
goto retry;

2411 
} 
2412  
2413 
if (decode_header(s, header) == 1) { 
2414 
/* free format: prepare to compute frame size */

2415 
s>frame_size = 1;

2416 
return 1; 
2417 
} 
2418 
/* update codec info */

2419 
avctx>channels = s>nb_channels; 
2420 
avctx>bit_rate = s>bit_rate; 
2421 
avctx>sub_id = s>layer; 
2422 
switch(s>layer) {

2423 
case 1: 
2424 
avctx>frame_size = 384;

2425 
break;

2426 
case 2: 
2427 
avctx>frame_size = 1152;

2428 
break;

2429 
case 3: 
2430 
if (s>lsf)

2431 
avctx>frame_size = 576;

2432 
else

2433 
avctx>frame_size = 1152;

2434 
break;

2435 
} 
2436  
2437 
if(s>frame_size<=0  s>frame_size > buf_size){ 
2438 
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");

2439 
return 1; 
2440 
}else if(s>frame_size < buf_size){ 
2441 
av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");

2442 
} 
2443  
2444 
out_size = mp_decode_frame(s, out_samples, buf, buf_size); 
2445 
if(out_size>=0){ 
2446 
*data_size = out_size; 
2447 
avctx>sample_rate = s>sample_rate; 
2448 
//FIXME maybe move the other codec info stuff from above here too

2449 
}else

2450 
av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return 1 / but also return the number of bytes consumed 
2451 
s>frame_size = 0;

2452 
return buf_size;

2453 
} 
2454  
2455 
static void flush(AVCodecContext *avctx){ 
2456 
MPADecodeContext *s = avctx>priv_data; 
2457 
s>last_buf_size= 0;

2458 
} 
2459  
2460 
#ifdef CONFIG_MP3ADU_DECODER

2461 
static int decode_frame_adu(AVCodecContext * avctx, 
2462 
void *data, int *data_size, 
2463 
uint8_t * buf, int buf_size)

2464 
{ 
2465 
MPADecodeContext *s = avctx>priv_data; 
2466 
uint32_t header; 
2467 
int len, out_size;

2468 
OUT_INT *out_samples = data; 
2469  
2470 
len = buf_size; 
2471  
2472 
// Discard too short frames

2473 
if (buf_size < HEADER_SIZE) {

2474 
*data_size = 0;

2475 
return buf_size;

2476 
} 
2477  
2478  
2479 
if (len > MPA_MAX_CODED_FRAME_SIZE)

2480 
len = MPA_MAX_CODED_FRAME_SIZE; 
2481  
2482 
// Get header and restore sync word

2483 
header = (buf[0] << 24)  (buf[1] << 16)  (buf[2] << 8)  buf[3]  0xffe00000; 
2484  
2485 
if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame 
2486 
*data_size = 0;

2487 
return buf_size;

2488 
} 
2489  
2490 
decode_header(s, header); 
2491 
/* update codec info */

2492 
avctx>sample_rate = s>sample_rate; 
2493 
avctx>channels = s>nb_channels; 
2494 
avctx>bit_rate = s>bit_rate; 
2495 
avctx>sub_id = s>layer; 
2496  
2497 
avctx>frame_size=s>frame_size = len; 
2498  
2499 
if (avctx>parse_only) {

2500 
out_size = buf_size; 
2501 
} else {

2502 
out_size = mp_decode_frame(s, out_samples, buf, buf_size); 
2503 
} 
2504  
2505 
*data_size = out_size; 
2506 
return buf_size;

2507 
} 
2508 
#endif /* CONFIG_MP3ADU_DECODER */ 
2509  
2510 
#ifdef CONFIG_MP3ON4_DECODER

2511 
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */

2512 
static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */ 
2513 
static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */ 
2514 
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */

2515 
static int chan_offset[9][5] = { 
2516 
{0},

2517 
{0}, // C 
2518 
{0}, // FLR 
2519 
{2,0}, // C FLR 
2520 
{2,0,3}, // C FLR BS 
2521 
{4,0,2}, // C FLR BLRS 
2522 
{4,0,2,5}, // C FLR BLRS LFE 
2523 
{4,0,2,6,5}, // C FLR BLRS BLR LFE 
2524 
{0,2} // FLR BLRS 
2525 
}; 
2526  
2527  
2528 
static int decode_init_mp3on4(AVCodecContext * avctx) 
2529 
{ 
2530 
MP3On4DecodeContext *s = avctx>priv_data; 
2531 
int i;

2532  
2533 
if ((avctx>extradata_size < 2)  (avctx>extradata == NULL)) { 
2534 
av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");

2535 
return 1; 
2536 
} 
2537  
2538 
s>chan_cfg = (((unsigned char *)avctx>extradata)[1] >> 3) & 0x0f; 
2539 
s>frames = mp3Frames[s>chan_cfg]; 
2540 
if(!s>frames) {

2541 
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");

2542 
return 1; 
2543 
} 
2544 
avctx>channels = mp3Channels[s>chan_cfg]; 
2545  
2546 
/* Init the first mp3 decoder in standard way, so that all tables get builded

2547 
* We replace avctx>priv_data with the context of the first decoder so that

2548 
* decode_init() does not have to be changed.

2549 
* Other decoders will be inited here copying data from the first context

2550 
*/

2551 
// Allocate zeroed memory for the first decoder context

2552 
s>mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext)); 
2553 
// Put decoder context in place to make init_decode() happy

2554 
avctx>priv_data = s>mp3decctx[0];

2555 
decode_init(avctx); 
2556 
// Restore mp3on4 context pointer

2557 
avctx>priv_data = s; 
2558 
s>mp3decctx[0]>adu_mode = 1; // Set adu mode 
2559  
2560 
/* Create a separate codec/context for each frame (first is already ok).

2561 
* Each frame is 1 or 2 channels  up to 5 frames allowed

2562 
*/

2563 
for (i = 1; i < s>frames; i++) { 
2564 
s>mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));

2565 
s>mp3decctx[i]>compute_antialias = s>mp3decctx[0]>compute_antialias;

2566 
s>mp3decctx[i]>adu_mode = 1;

2567 
s>mp3decctx[i]>avctx = avctx; 
2568 
} 
2569  
2570 
return 0; 
2571 
} 
2572  
2573  
2574 
static int decode_close_mp3on4(AVCodecContext * avctx) 
2575 
{ 
2576 
MP3On4DecodeContext *s = avctx>priv_data; 
2577 
int i;

2578  
2579 
for (i = 0; i < s>frames; i++) 
2580 
if (s>mp3decctx[i])

2581 
av_free(s>mp3decctx[i]); 
2582  
2583 
return 0; 
2584 
} 
2585  
2586  
2587 
static int decode_frame_mp3on4(AVCodecContext * avctx, 
2588 
void *data, int *data_size, 
2589 
uint8_t * buf, int buf_size)

2590 
{ 
2591 
MP3On4DecodeContext *s = avctx>priv_data; 
2592 
MPADecodeContext *m; 
2593 
int len, out_size = 0; 
2594 
uint32_t header; 
2595 
OUT_INT *out_samples = data; 
2596 
OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS]; 
2597 
OUT_INT *outptr, *bp; 
2598 
int fsize;

2599 
unsigned char *start2 = buf, *start; 
2600 
int fr, i, j, n;

2601 
int off = avctx>channels;

2602 
int *coff = chan_offset[s>chan_cfg];

2603  
2604 
len = buf_size; 
2605  
2606 
// Discard too short frames

2607 
if (buf_size < HEADER_SIZE) {

2608 
*data_size = 0;

2609 
return buf_size;

2610 
} 
2611  
2612 
// If only one decoder interleave is not needed

2613 
outptr = s>frames == 1 ? out_samples : decoded_buf;

2614  
2615 
for (fr = 0; fr < s>frames; fr++) { 
2616 
start = start2; 
2617 
fsize = (start[0] << 4)  (start[1] >> 4); 
2618 
start2 += fsize; 
2619 
if (fsize > len)

2620 
fsize = len; 
2621 
len = fsize; 
2622 
if (fsize > MPA_MAX_CODED_FRAME_SIZE)

2623 
fsize = MPA_MAX_CODED_FRAME_SIZE; 
2624 
m = s>mp3decctx[fr]; 
2625 
assert (m != NULL);

2626  
2627 
// Get header

2628 
header = (start[0] << 24)  (start[1] << 16)  (start[2] << 8)  start[3]  0xfff00000; 
2629  
2630 
if (ff_mpa_check_header(header) < 0) { // Bad header, discard block 
2631 
*data_size = 0;

2632 
return buf_size;

2633 
} 
2634  
2635 
decode_header(m, header); 
2636 
mp_decode_frame(m, decoded_buf, start, fsize); 
2637  
2638 
n = MPA_FRAME_SIZE * m>nb_channels; 
2639 
out_size += n * sizeof(OUT_INT);

2640 
if(s>frames > 1) { 
2641 
/* interleave output data */

2642 
bp = out_samples + coff[fr]; 
2643 
if(m>nb_channels == 1) { 
2644 
for(j = 0; j < n; j++) { 
2645 
*bp = decoded_buf[j]; 
2646 
bp += off; 
2647 
} 
2648 
} else {

2649 
for(j = 0; j < n; j++) { 
2650 
bp[0] = decoded_buf[j++];

2651 
bp[1] = decoded_buf[j];

2652 
bp += off; 
2653 
} 
2654 
} 
2655 
} 
2656 
} 
2657  
2658 
/* update codec info */

2659 
avctx>sample_rate = s>mp3decctx[0]>sample_rate;

2660 
avctx>frame_size= buf_size; 
2661 
avctx>bit_rate = 0;

2662 
for (i = 0; i < s>frames; i++) 
2663 
avctx>bit_rate += s>mp3decctx[i]>bit_rate; 
2664  
2665 
*data_size = out_size; 
2666 
return buf_size;

2667 
} 
2668 
#endif /* CONFIG_MP3ON4_DECODER */ 
2669  
2670 
#ifdef CONFIG_MP2_DECODER

2671 
AVCodec mp2_decoder = 
2672 
{ 
2673 
"mp2",

2674 
CODEC_TYPE_AUDIO, 
2675 
CODEC_ID_MP2, 
2676 
sizeof(MPADecodeContext),

2677 
decode_init, 
2678 
NULL,

2679 
NULL,

2680 
decode_frame, 
2681 
CODEC_CAP_PARSE_ONLY, 
2682 
}; 
2683 
#endif

2684 
#ifdef CONFIG_MP3_DECODER

2685 
AVCodec mp3_decoder = 
2686 
{ 
2687 
"mp3",

2688 
CODEC_TYPE_AUDIO, 
2689 
CODEC_ID_MP3, 
2690 
sizeof(MPADecodeContext),

2691 
decode_init, 
2692 
NULL,

2693 
NULL,

2694 
decode_frame, 
2695 
CODEC_CAP_PARSE_ONLY, 
2696 
.flush= flush, 
2697 
}; 
2698 
#endif

2699 
#ifdef CONFIG_MP3ADU_DECODER

2700 
AVCodec mp3adu_decoder = 
2701 
{ 
2702 
"mp3adu",

2703 
CODEC_TYPE_AUDIO, 
2704 
CODEC_ID_MP3ADU, 
2705 
sizeof(MPADecodeContext),

2706 
decode_init, 
2707 
NULL,

2708 
NULL,

2709 
decode_frame_adu, 
2710 
CODEC_CAP_PARSE_ONLY, 
2711 
.flush= flush, 
2712 
}; 
2713 
#endif

2714 
#ifdef CONFIG_MP3ON4_DECODER

2715 
AVCodec mp3on4_decoder = 
2716 
{ 
2717 
"mp3on4",

2718 
CODEC_TYPE_AUDIO, 
2719 
CODEC_ID_MP3ON4, 
2720 
sizeof(MP3On4DecodeContext),

2721 
decode_init_mp3on4, 
2722 
NULL,

2723 
decode_close_mp3on4, 
2724 
decode_frame_mp3on4, 
2725 
.flush= flush, 
2726 
}; 
2727 
#endif
