ffmpeg / libavcodec / mpegaudiodec.c @ 4a69055b
History  View  Annotate  Download (80.2 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 
/* WARNING: only correct for posititive numbers */

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

56  
57 
#define HEADER_SIZE 4 
58  
59 
/**

60 
* Context for MP3On4 decoder

61 
*/

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

69 
typedef struct GranuleDef { 
70 
uint8_t scfsi; 
71 
int part2_3_length;

72 
int big_values;

73 
int global_gain;

74 
int scalefac_compress;

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

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

85 
int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */ 
86 
} GranuleDef; 
87  
88 
#include "mpegaudiodata.h" 
89 
#include "mpegaudiodectab.h" 
90  
91 
static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g); 
92 
static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g); 
93  
94 
/* vlc structure for decoding layer 3 huffman tables */

95 
static VLC huff_vlc[16]; 
96 
static VLC huff_quad_vlc[2]; 
97 
/* computed from band_size_long */

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

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

102 
static uint32_t table_4_3_value[TABLE_4_3_SIZE];

103 
static uint32_t exp_table[512]; 
104 
static uint32_t expval_table[512][16]; 
105 
/* intensity stereo coef table */

106 
static int32_t is_table[2][16]; 
107 
static int32_t is_table_lsf[2][2][16]; 
108 
static int32_t csa_table[8][4]; 
109 
static float csa_table_float[8][4]; 
110 
static int32_t mdct_win[8][36]; 
111  
112 
/* lower 2 bits: modulo 3, higher bits: shift */

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

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

117  
118 
#define SCALE_GEN(v) \

119 
{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) } 
120  
121 
static const int32_t scale_factor_mult2[3][3] = { 
122 
SCALE_GEN(4.0 / 3.0), /* 3 steps */ 
123 
SCALE_GEN(4.0 / 5.0), /* 5 steps */ 
124 
SCALE_GEN(4.0 / 9.0), /* 9 steps */ 
125 
}; 
126  
127 
static DECLARE_ALIGNED_16(MPA_INT, window[512]); 
128  
129 
/* layer 1 unscaling */

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

131 
static inline int l1_unscale(int n, int mant, int scale_factor) 
132 
{ 
133 
int shift, mod;

134 
int64_t val; 
135  
136 
shift = scale_factor_modshift[scale_factor]; 
137 
mod = shift & 3;

138 
shift >>= 2;

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

142 
return (int)((val + (1LL << (shift  1))) >> shift); 
143 
} 
144  
145 
static inline int l2_unscale_group(int steps, int mant, int scale_factor) 
146 
{ 
147 
int shift, mod, val;

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

151 
shift >>= 2;

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

155 
if (shift > 0) 
156 
val = (val + (1 << (shift  1))) >> shift; 
157 
return val;

158 
} 
159  
160 
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */

161 
static inline int l3_unscale(int value, int exponent) 
162 
{ 
163 
unsigned int m; 
164 
int e;

165  
166 
e = table_4_3_exp [4*value + (exponent&3)]; 
167 
m = table_4_3_value[4*value + (exponent&3)]; 
168 
e = (exponent >> 2);

169 
assert(e>=1);

170 
if (e > 31) 
171 
return 0; 
172 
m = (m + (1 << (e1))) >> e; 
173  
174 
return m;

175 
} 
176  
177 
/* all integer n^(4/3) computation code */

178 
#define DEV_ORDER 13 
179  
180 
#define POW_FRAC_BITS 24 
181 
#define POW_FRAC_ONE (1 << POW_FRAC_BITS) 
182 
#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE)) 
183 
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)

184  
185 
static int dev_4_3_coefs[DEV_ORDER]; 
186  
187 
#if 0 /* unused */

188 
static int pow_mult3[3] = {

189 
POW_FIX(1.0),

190 
POW_FIX(1.25992104989487316476),

191 
POW_FIX(1.58740105196819947474),

192 
};

193 
#endif

194  
195 
static void int_pow_init(void) 
196 
{ 
197 
int i, a;

198  
199 
a = POW_FIX(1.0); 
200 
for(i=0;i<DEV_ORDER;i++) { 
201 
a = POW_MULL(a, POW_FIX(4.0 / 3.0)  i * POW_FIX(1.0)) / (i + 1); 
202 
dev_4_3_coefs[i] = a; 
203 
} 
204 
} 
205  
206 
#if 0 /* unused, remove? */

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

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

209 
{

210 
int e, er, eq, j;

211 
int a, a1;

212 

213 
/* renormalize */

214 
a = i;

215 
e = POW_FRAC_BITS;

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

217 
a = a << 1;

218 
e;

219 
}

220 
a = (1 << POW_FRAC_BITS);

221 
a1 = 0;

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

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

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

225 
/* exponent compute (exact) */

226 
e = e * 4;

227 
er = e % 3;

228 
eq = e / 3;

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

230 
while (a >= 2 * POW_FRAC_ONE) {

231 
a = a >> 1;

232 
eq++;

233 
}

234 
/* convert to float */

235 
while (a < POW_FRAC_ONE) {

236 
a = a << 1;

237 
eq;

238 
}

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

240 
#if POW_FRAC_BITS > FRAC_BITS

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

242 
/* correct overflow */

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

244 
a = a >> 1;

245 
eq++;

246 
}

247 
#endif

248 
*exp_ptr = eq; 
249 
return a;

250 
} 
251 
#endif

252  
253 
static int decode_init(AVCodecContext * avctx) 
254 
{ 
255 
MPADecodeContext *s = avctx>priv_data; 
256 
static int init=0; 
257 
int i, j, k;

258  
259 
s>avctx = avctx; 
260  
261 
#if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)

262 
avctx>sample_fmt= SAMPLE_FMT_S32; 
263 
#else

264 
avctx>sample_fmt= SAMPLE_FMT_S16; 
265 
#endif

266 
s>error_resilience= avctx>error_resilience; 
267  
268 
if(avctx>antialias_algo != FF_AA_FLOAT)

269 
s>compute_antialias= compute_antialias_integer; 
270 
else

271 
s>compute_antialias= compute_antialias_float; 
272  
273 
if (!init && !avctx>parse_only) {

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

275 
for(i=0;i<64;i++) { 
276 
int shift, mod;

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

278 
shift = (i / 3);

279 
mod = i % 3;

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

281 
} 
282  
283 
/* scale factor multiply for layer 1 */

284 
for(i=0;i<15;i++) { 
285 
int n, norm;

286 
n = i + 2;

287 
norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n)  1); 
288 
scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm); 
289 
scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm); 
290 
scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm); 
291 
dprintf(avctx, "%d: norm=%x s=%x %x %x\n",

292 
i, norm, 
293 
scale_factor_mult[i][0],

294 
scale_factor_mult[i][1],

295 
scale_factor_mult[i][2]);

296 
} 
297  
298 
ff_mpa_synth_init(window); 
299  
300 
/* huffman decode tables */

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

303 
int xsize, x, y;

304 
unsigned int n; 
305 
uint8_t tmp_bits [512];

306 
uint16_t tmp_codes[512];

307  
308 
memset(tmp_bits , 0, sizeof(tmp_bits )); 
309 
memset(tmp_codes, 0, sizeof(tmp_codes)); 
310  
311 
xsize = h>xsize; 
312 
n = xsize * xsize; 
313  
314 
j = 0;

315 
for(x=0;x<xsize;x++) { 
316 
for(y=0;y<xsize;y++){ 
317 
tmp_bits [(x << 5)  y  ((x&&y)<<4)]= h>bits [j ]; 
318 
tmp_codes[(x << 5)  y  ((x&&y)<<4)]= h>codes[j++]; 
319 
} 
320 
} 
321  
322 
/* XXX: fail test */

323 
init_vlc(&huff_vlc[i], 7, 512, 
324 
tmp_bits, 1, 1, tmp_codes, 2, 2, 1); 
325 
} 
326 
for(i=0;i<2;i++) { 
327 
init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, 
328 
mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1); 
329 
} 
330  
331 
for(i=0;i<9;i++) { 
332 
k = 0;

333 
for(j=0;j<22;j++) { 
334 
band_index_long[i][j] = k; 
335 
k += band_size_long[i][j]; 
336 
} 
337 
band_index_long[i][22] = k;

338 
} 
339  
340 
/* compute n ^ (4/3) and store it in mantissa/exp format */

341  
342 
int_pow_init(); 
343 
for(i=1;i<TABLE_4_3_SIZE;i++) { 
344 
double f, fm;

345 
int e, m;

346 
f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25); 
347 
fm = frexp(f, &e); 
348 
m = (uint32_t)(fm*(1LL<<31) + 0.5); 
349 
e+= FRAC_BITS  31 + 5  100; 
350  
351 
/* normalized to FRAC_BITS */

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

354 
table_4_3_exp[i] = e; 
355 
} 
356 
for(i=0; i<512*16; i++){ 
357 
int exponent= (i>>4); 
358 
double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent400)*0.25 + FRAC_BITS + 5); 
359 
expval_table[exponent][i&15]= llrint(f);

360 
if((i&15)==1) 
361 
exp_table[exponent]= llrint(f); 
362 
} 
363  
364 
for(i=0;i<7;i++) { 
365 
float f;

366 
int v;

367 
if (i != 6) { 
368 
f = tan((double)i * M_PI / 12.0); 
369 
v = FIXR(f / (1.0 + f)); 
370 
} else {

371 
v = FIXR(1.0); 
372 
} 
373 
is_table[0][i] = v;

374 
is_table[1][6  i] = v; 
375 
} 
376 
/* invalid values */

377 
for(i=7;i<16;i++) 
378 
is_table[0][i] = is_table[1][i] = 0.0; 
379  
380 
for(i=0;i<16;i++) { 
381 
double f;

382 
int e, k;

383  
384 
for(j=0;j<2;j++) { 
385 
e = (j + 1) * ((i + 1) >> 1); 
386 
f = pow(2.0, e / 4.0); 
387 
k = i & 1;

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

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

391 
i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]); 
392 
} 
393 
} 
394  
395 
for(i=0;i<8;i++) { 
396 
float ci, cs, ca;

397 
ci = ci_table[i]; 
398 
cs = 1.0 / sqrt(1.0 + ci * ci); 
399 
ca = cs * ci; 
400 
csa_table[i][0] = FIXHR(cs/4); 
401 
csa_table[i][1] = FIXHR(ca/4); 
402 
csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4); 
403 
csa_table[i][3] = FIXHR(ca/4)  FIXHR(cs/4); 
404 
csa_table_float[i][0] = cs;

405 
csa_table_float[i][1] = ca;

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

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

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

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

410 
} 
411  
412 
/* compute mdct windows */

413 
for(i=0;i<36;i++) { 
414 
for(j=0; j<4; j++){ 
415 
double d;

416  
417 
if(j==2 && i%3 != 1) 
418 
continue;

419  
420 
d= sin(M_PI * (i + 0.5) / 36.0); 
421 
if(j==1){ 
422 
if (i>=30) d= 0; 
423 
else if(i>=24) d= sin(M_PI * (i  18 + 0.5) / 12.0); 
424 
else if(i>=18) d= 1; 
425 
}else if(j==3){ 
426 
if (i< 6) d= 0; 
427 
else if(i< 12) d= sin(M_PI * (i  6 + 0.5) / 12.0); 
428 
else if(i< 18) d= 1; 
429 
} 
430 
//merge last stage of imdct into the window coefficients

431 
d*= 0.5 / cos(M_PI*(2*i + 19)/72); 
432  
433 
if(j==2) 
434 
mdct_win[j][i/3] = FIXHR((d / (1<<5))); 
435 
else

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

438 
} 
439 
} 
440  
441 
/* NOTE: we do frequency inversion adter the MDCT by changing

442 
the sign of the right window coefs */

443 
for(j=0;j<4;j++) { 
444 
for(i=0;i<36;i+=2) { 
445 
mdct_win[j + 4][i] = mdct_win[j][i];

446 
mdct_win[j + 4][i + 1] = mdct_win[j][i + 1]; 
447 
} 
448 
} 
449  
450 
#if defined(DEBUG)

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

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

456 
} 
457 
#endif

458 
init = 1;

459 
} 
460  
461 
#ifdef DEBUG

462 
s>frame_count = 0;

463 
#endif

464 
if (avctx>codec_id == CODEC_ID_MP3ADU)

465 
s>adu_mode = 1;

466 
return 0; 
467 
} 
468  
469 
/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6  j))) */

470  
471 
/* cos(i*pi/64) */

472  
473 
#define COS0_0 FIXHR(0.50060299823519630134/2) 
474 
#define COS0_1 FIXHR(0.50547095989754365998/2) 
475 
#define COS0_2 FIXHR(0.51544730992262454697/2) 
476 
#define COS0_3 FIXHR(0.53104259108978417447/2) 
477 
#define COS0_4 FIXHR(0.55310389603444452782/2) 
478 
#define COS0_5 FIXHR(0.58293496820613387367/2) 
479 
#define COS0_6 FIXHR(0.62250412303566481615/2) 
480 
#define COS0_7 FIXHR(0.67480834145500574602/2) 
481 
#define COS0_8 FIXHR(0.74453627100229844977/2) 
482 
#define COS0_9 FIXHR(0.83934964541552703873/2) 
483 
#define COS0_10 FIXHR(0.97256823786196069369/2) 
484 
#define COS0_11 FIXHR(1.16943993343288495515/4) 
485 
#define COS0_12 FIXHR(1.48416461631416627724/4) 
486 
#define COS0_13 FIXHR(2.05778100995341155085/8) 
487 
#define COS0_14 FIXHR(3.40760841846871878570/8) 
488 
#define COS0_15 FIXHR(10.19000812354805681150/32) 
489  
490 
#define COS1_0 FIXHR(0.50241928618815570551/2) 
491 
#define COS1_1 FIXHR(0.52249861493968888062/2) 
492 
#define COS1_2 FIXHR(0.56694403481635770368/2) 
493 
#define COS1_3 FIXHR(0.64682178335999012954/2) 
494 
#define COS1_4 FIXHR(0.78815462345125022473/2) 
495 
#define COS1_5 FIXHR(1.06067768599034747134/4) 
496 
#define COS1_6 FIXHR(1.72244709823833392782/4) 
497 
#define COS1_7 FIXHR(5.10114861868916385802/16) 
498  
499 
#define COS2_0 FIXHR(0.50979557910415916894/2) 
500 
#define COS2_1 FIXHR(0.60134488693504528054/2) 
501 
#define COS2_2 FIXHR(0.89997622313641570463/2) 
502 
#define COS2_3 FIXHR(2.56291544774150617881/8) 
503  
504 
#define COS3_0 FIXHR(0.54119610014619698439/2) 
505 
#define COS3_1 FIXHR(1.30656296487637652785/4) 
506  
507 
#define COS4_0 FIXHR(0.70710678118654752439/2) 
508  
509 
/* butterfly operator */

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

511 
{\ 
512 
tmp0 = tab[a] + tab[b];\ 
513 
tmp1 = tab[a]  tab[b];\ 
514 
tab[a] = tmp0;\ 
515 
tab[b] = MULH(tmp1<<(s), c);\ 
516 
} 
517  
518 
#define BF1(a, b, c, d)\

519 
{\ 
520 
BF(a, b, COS4_0, 1);\

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

522 
tab[c] += tab[d];\ 
523 
} 
524  
525 
#define BF2(a, b, c, d)\

526 
{\ 
527 
BF(a, b, COS4_0, 1);\

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

529 
tab[c] += tab[d];\ 
530 
tab[a] += tab[c];\ 
531 
tab[c] += tab[b];\ 
532 
tab[b] += tab[d];\ 
533 
} 
534  
535 
#define ADD(a, b) tab[a] += tab[b]

536  
537 
/* DCT32 without 1/sqrt(2) coef zero scaling. */

538 
static void dct32(int32_t *out, int32_t *tab) 
539 
{ 
540 
int tmp0, tmp1;

541  
542 
/* pass 1 */

543 
BF( 0, 31, COS0_0 , 1); 
544 
BF(15, 16, COS0_15, 5); 
545 
/* pass 2 */

546 
BF( 0, 15, COS1_0 , 1); 
547 
BF(16, 31,COS1_0 , 1); 
548 
/* pass 1 */

549 
BF( 7, 24, COS0_7 , 1); 
550 
BF( 8, 23, COS0_8 , 1); 
551 
/* pass 2 */

552 
BF( 7, 8, COS1_7 , 4); 
553 
BF(23, 24,COS1_7 , 4); 
554 
/* pass 3 */

555 
BF( 0, 7, COS2_0 , 1); 
556 
BF( 8, 15,COS2_0 , 1); 
557 
BF(16, 23, COS2_0 , 1); 
558 
BF(24, 31,COS2_0 , 1); 
559 
/* pass 1 */

560 
BF( 3, 28, COS0_3 , 1); 
561 
BF(12, 19, COS0_12, 2); 
562 
/* pass 2 */

563 
BF( 3, 12, COS1_3 , 1); 
564 
BF(19, 28,COS1_3 , 1); 
565 
/* pass 1 */

566 
BF( 4, 27, COS0_4 , 1); 
567 
BF(11, 20, COS0_11, 2); 
568 
/* pass 2 */

569 
BF( 4, 11, COS1_4 , 1); 
570 
BF(20, 27,COS1_4 , 1); 
571 
/* pass 3 */

572 
BF( 3, 4, COS2_3 , 3); 
573 
BF(11, 12,COS2_3 , 3); 
574 
BF(19, 20, COS2_3 , 3); 
575 
BF(27, 28,COS2_3 , 3); 
576 
/* pass 4 */

577 
BF( 0, 3, COS3_0 , 1); 
578 
BF( 4, 7,COS3_0 , 1); 
579 
BF( 8, 11, COS3_0 , 1); 
580 
BF(12, 15,COS3_0 , 1); 
581 
BF(16, 19, COS3_0 , 1); 
582 
BF(20, 23,COS3_0 , 1); 
583 
BF(24, 27, COS3_0 , 1); 
584 
BF(28, 31,COS3_0 , 1); 
585  
586  
587  
588 
/* pass 1 */

589 
BF( 1, 30, COS0_1 , 1); 
590 
BF(14, 17, COS0_14, 3); 
591 
/* pass 2 */

592 
BF( 1, 14, COS1_1 , 1); 
593 
BF(17, 30,COS1_1 , 1); 
594 
/* pass 1 */

595 
BF( 6, 25, COS0_6 , 1); 
596 
BF( 9, 22, COS0_9 , 1); 
597 
/* pass 2 */

598 
BF( 6, 9, COS1_6 , 2); 
599 
BF(22, 25,COS1_6 , 2); 
600 
/* pass 3 */

601 
BF( 1, 6, COS2_1 , 1); 
602 
BF( 9, 14,COS2_1 , 1); 
603 
BF(17, 22, COS2_1 , 1); 
604 
BF(25, 30,COS2_1 , 1); 
605  
606 
/* pass 1 */

607 
BF( 2, 29, COS0_2 , 1); 
608 
BF(13, 18, COS0_13, 3); 
609 
/* pass 2 */

610 
BF( 2, 13, COS1_2 , 1); 
611 
BF(18, 29,COS1_2 , 1); 
612 
/* pass 1 */

613 
BF( 5, 26, COS0_5 , 1); 
614 
BF(10, 21, COS0_10, 1); 
615 
/* pass 2 */

616 
BF( 5, 10, COS1_5 , 2); 
617 
BF(21, 26,COS1_5 , 2); 
618 
/* pass 3 */

619 
BF( 2, 5, COS2_2 , 1); 
620 
BF(10, 13,COS2_2 , 1); 
621 
BF(18, 21, COS2_2 , 1); 
622 
BF(26, 29,COS2_2 , 1); 
623 
/* pass 4 */

624 
BF( 1, 2, COS3_1 , 2); 
625 
BF( 5, 6,COS3_1 , 2); 
626 
BF( 9, 10, COS3_1 , 2); 
627 
BF(13, 14,COS3_1 , 2); 
628 
BF(17, 18, COS3_1 , 2); 
629 
BF(21, 22,COS3_1 , 2); 
630 
BF(25, 26, COS3_1 , 2); 
631 
BF(29, 30,COS3_1 , 2); 
632  
633 
/* pass 5 */

634 
BF1( 0, 1, 2, 3); 
635 
BF2( 4, 5, 6, 7); 
636 
BF1( 8, 9, 10, 11); 
637 
BF2(12, 13, 14, 15); 
638 
BF1(16, 17, 18, 19); 
639 
BF2(20, 21, 22, 23); 
640 
BF1(24, 25, 26, 27); 
641 
BF2(28, 29, 30, 31); 
642  
643 
/* pass 6 */

644  
645 
ADD( 8, 12); 
646 
ADD(12, 10); 
647 
ADD(10, 14); 
648 
ADD(14, 9); 
649 
ADD( 9, 13); 
650 
ADD(13, 11); 
651 
ADD(11, 15); 
652  
653 
out[ 0] = tab[0]; 
654 
out[16] = tab[1]; 
655 
out[ 8] = tab[2]; 
656 
out[24] = tab[3]; 
657 
out[ 4] = tab[4]; 
658 
out[20] = tab[5]; 
659 
out[12] = tab[6]; 
660 
out[28] = tab[7]; 
661 
out[ 2] = tab[8]; 
662 
out[18] = tab[9]; 
663 
out[10] = tab[10]; 
664 
out[26] = tab[11]; 
665 
out[ 6] = tab[12]; 
666 
out[22] = tab[13]; 
667 
out[14] = tab[14]; 
668 
out[30] = tab[15]; 
669  
670 
ADD(24, 28); 
671 
ADD(28, 26); 
672 
ADD(26, 30); 
673 
ADD(30, 25); 
674 
ADD(25, 29); 
675 
ADD(29, 27); 
676 
ADD(27, 31); 
677  
678 
out[ 1] = tab[16] + tab[24]; 
679 
out[17] = tab[17] + tab[25]; 
680 
out[ 9] = tab[18] + tab[26]; 
681 
out[25] = tab[19] + tab[27]; 
682 
out[ 5] = tab[20] + tab[28]; 
683 
out[21] = tab[21] + tab[29]; 
684 
out[13] = tab[22] + tab[30]; 
685 
out[29] = tab[23] + tab[31]; 
686 
out[ 3] = tab[24] + tab[20]; 
687 
out[19] = tab[25] + tab[21]; 
688 
out[11] = tab[26] + tab[22]; 
689 
out[27] = tab[27] + tab[23]; 
690 
out[ 7] = tab[28] + tab[18]; 
691 
out[23] = tab[29] + tab[19]; 
692 
out[15] = tab[30] + tab[17]; 
693 
out[31] = tab[31]; 
694 
} 
695  
696 
#if FRAC_BITS <= 15 
697  
698 
static inline int round_sample(int *sum) 
699 
{ 
700 
int sum1;

701 
sum1 = (*sum) >> OUT_SHIFT; 
702 
*sum &= (1<<OUT_SHIFT)1; 
703 
if (sum1 < OUT_MIN)

704 
sum1 = OUT_MIN; 
705 
else if (sum1 > OUT_MAX) 
706 
sum1 = OUT_MAX; 
707 
return sum1;

708 
} 
709  
710 
/* signed 16x16 > 32 multiply add accumulate */

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

712  
713 
/* signed 16x16 > 32 multiply */

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

715  
716 
#else

717  
718 
static inline int round_sample(int64_t *sum) 
719 
{ 
720 
int sum1;

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

722 
*sum &= (1<<OUT_SHIFT)1; 
723 
if (sum1 < OUT_MIN)

724 
sum1 = OUT_MIN; 
725 
else if (sum1 > OUT_MAX) 
726 
sum1 = OUT_MAX; 
727 
return sum1;

728 
} 
729  
730 
# define MULS(ra, rb) MUL64(ra, rb)

731 
#endif

732  
733 
#define SUM8(sum, op, w, p) \

734 
{ \ 
735 
sum op MULS((w)[0 * 64], p[0 * 64]);\ 
736 
sum op MULS((w)[1 * 64], p[1 * 64]);\ 
737 
sum op MULS((w)[2 * 64], p[2 * 64]);\ 
738 
sum op MULS((w)[3 * 64], p[3 * 64]);\ 
739 
sum op MULS((w)[4 * 64], p[4 * 64]);\ 
740 
sum op MULS((w)[5 * 64], p[5 * 64]);\ 
741 
sum op MULS((w)[6 * 64], p[6 * 64]);\ 
742 
sum op MULS((w)[7 * 64], p[7 * 64]);\ 
743 
} 
744  
745 
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \

746 
{ \ 
747 
int tmp;\

748 
tmp = p[0 * 64];\ 
749 
sum1 op1 MULS((w1)[0 * 64], tmp);\ 
750 
sum2 op2 MULS((w2)[0 * 64], tmp);\ 
751 
tmp = p[1 * 64];\ 
752 
sum1 op1 MULS((w1)[1 * 64], tmp);\ 
753 
sum2 op2 MULS((w2)[1 * 64], tmp);\ 
754 
tmp = p[2 * 64];\ 
755 
sum1 op1 MULS((w1)[2 * 64], tmp);\ 
756 
sum2 op2 MULS((w2)[2 * 64], tmp);\ 
757 
tmp = p[3 * 64];\ 
758 
sum1 op1 MULS((w1)[3 * 64], tmp);\ 
759 
sum2 op2 MULS((w2)[3 * 64], tmp);\ 
760 
tmp = p[4 * 64];\ 
761 
sum1 op1 MULS((w1)[4 * 64], tmp);\ 
762 
sum2 op2 MULS((w2)[4 * 64], tmp);\ 
763 
tmp = p[5 * 64];\ 
764 
sum1 op1 MULS((w1)[5 * 64], tmp);\ 
765 
sum2 op2 MULS((w2)[5 * 64], tmp);\ 
766 
tmp = p[6 * 64];\ 
767 
sum1 op1 MULS((w1)[6 * 64], tmp);\ 
768 
sum2 op2 MULS((w2)[6 * 64], tmp);\ 
769 
tmp = p[7 * 64];\ 
770 
sum1 op1 MULS((w1)[7 * 64], tmp);\ 
771 
sum2 op2 MULS((w2)[7 * 64], tmp);\ 
772 
} 
773  
774 
void ff_mpa_synth_init(MPA_INT *window)

775 
{ 
776 
int i;

777  
778 
/* max = 18760, max sum over all 16 coefs : 44736 */

779 
for(i=0;i<257;i++) { 
780 
int v;

781 
v = ff_mpa_enwindow[i]; 
782 
#if WFRAC_BITS < 16 
783 
v = (v + (1 << (16  WFRAC_BITS  1))) >> (16  WFRAC_BITS); 
784 
#endif

785 
window[i] = v; 
786 
if ((i & 63) != 0) 
787 
v = v; 
788 
if (i != 0) 
789 
window[512  i] = v;

790 
} 
791 
} 
792  
793 
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:

794 
32 samples. */

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

796 
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, 
797 
MPA_INT *window, int *dither_state,

798 
OUT_INT *samples, int incr,

799 
int32_t sb_samples[SBLIMIT]) 
800 
{ 
801 
int32_t tmp[32];

802 
register MPA_INT *synth_buf;

803 
register const MPA_INT *w, *w2, *p; 
804 
int j, offset, v;

805 
OUT_INT *samples2; 
806 
#if FRAC_BITS <= 15 
807 
int sum, sum2;

808 
#else

809 
int64_t sum, sum2; 
810 
#endif

811  
812 
dct32(tmp, sb_samples); 
813  
814 
offset = *synth_buf_offset; 
815 
synth_buf = synth_buf_ptr + offset; 
816  
817 
for(j=0;j<32;j++) { 
818 
v = tmp[j]; 
819 
#if FRAC_BITS <= 15 
820 
/* NOTE: can cause a loss in precision if very high amplitude

821 
sound */

822 
v = av_clip_int16(v); 
823 
#endif

824 
synth_buf[j] = v; 
825 
} 
826 
/* copy to avoid wrap */

827 
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); 
828  
829 
samples2 = samples + 31 * incr;

830 
w = window; 
831 
w2 = window + 31;

832  
833 
sum = *dither_state; 
834 
p = synth_buf + 16;

835 
SUM8(sum, +=, w, p); 
836 
p = synth_buf + 48;

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

838 
*samples = round_sample(&sum); 
839 
samples += incr; 
840 
w++; 
841  
842 
/* we calculate two samples at the same time to avoid one memory

843 
access per two sample */

844 
for(j=1;j<16;j++) { 
845 
sum2 = 0;

846 
p = synth_buf + 16 + j;

847 
SUM8P2(sum, +=, sum2, =, w, w2, p); 
848 
p = synth_buf + 48  j;

849 
SUM8P2(sum, =, sum2, =, w + 32, w2 + 32, p); 
850  
851 
*samples = round_sample(&sum); 
852 
samples += incr; 
853 
sum += sum2; 
854 
*samples2 = round_sample(&sum); 
855 
samples2 = incr; 
856 
w++; 
857 
w2; 
858 
} 
859  
860 
p = synth_buf + 32;

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

862 
*samples = round_sample(&sum); 
863 
*dither_state= sum; 
864  
865 
offset = (offset  32) & 511; 
866 
*synth_buf_offset = offset; 
867 
} 
868  
869 
#define C3 FIXHR(0.86602540378443864676/2) 
870  
871 
/* 0.5 / cos(pi*(2*i+1)/36) */

872 
static const int icos36[9] = { 
873 
FIXR(0.50190991877167369479), 
874 
FIXR(0.51763809020504152469), //0 
875 
FIXR(0.55168895948124587824), 
876 
FIXR(0.61038729438072803416), 
877 
FIXR(0.70710678118654752439), //1 
878 
FIXR(0.87172339781054900991), 
879 
FIXR(1.18310079157624925896), 
880 
FIXR(1.93185165257813657349), //2 
881 
FIXR(5.73685662283492756461), 
882 
}; 
883  
884 
/* 0.5 / cos(pi*(2*i+1)/36) */

885 
static const int icos36h[9] = { 
886 
FIXHR(0.50190991877167369479/2), 
887 
FIXHR(0.51763809020504152469/2), //0 
888 
FIXHR(0.55168895948124587824/2), 
889 
FIXHR(0.61038729438072803416/2), 
890 
FIXHR(0.70710678118654752439/2), //1 
891 
FIXHR(0.87172339781054900991/2), 
892 
FIXHR(1.18310079157624925896/4), 
893 
FIXHR(1.93185165257813657349/4), //2 
894 
// FIXHR(5.73685662283492756461),

895 
}; 
896  
897 
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious

898 
cases. */

899 
static void imdct12(int *out, int *in) 
900 
{ 
901 
int in0, in1, in2, in3, in4, in5, t1, t2;

902  
903 
in0= in[0*3]; 
904 
in1= in[1*3] + in[0*3]; 
905 
in2= in[2*3] + in[1*3]; 
906 
in3= in[3*3] + in[2*3]; 
907 
in4= in[4*3] + in[3*3]; 
908 
in5= in[5*3] + in[4*3]; 
909 
in5 += in3; 
910 
in3 += in1; 
911  
912 
in2= MULH(2*in2, C3);

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

914  
915 
t1 = in0  in4; 
916 
t2 = MULH(2*(in1  in5), icos36h[4]); 
917  
918 
out[ 7]=

919 
out[10]= t1 + t2;

920 
out[ 1]=

921 
out[ 4]= t1  t2;

922  
923 
in0 += in4>>1;

924 
in4 = in0 + in2; 
925 
in5 += 2*in1;

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

927 
out[ 8]=

928 
out[ 9]= in4 + in1;

929 
out[ 2]=

930 
out[ 3]= in4  in1;

931  
932 
in0 = in2; 
933 
in5 = MULH(2*(in5  in3), icos36h[7]); 
934 
out[ 0]=

935 
out[ 5]= in0  in5;

936 
out[ 6]=

937 
out[11]= in0 + in5;

938 
} 
939  
940 
/* cos(pi*i/18) */

941 
#define C1 FIXHR(0.98480775301220805936/2) 
942 
#define C2 FIXHR(0.93969262078590838405/2) 
943 
#define C3 FIXHR(0.86602540378443864676/2) 
944 
#define C4 FIXHR(0.76604444311897803520/2) 
945 
#define C5 FIXHR(0.64278760968653932632/2) 
946 
#define C6 FIXHR(0.5/2) 
947 
#define C7 FIXHR(0.34202014332566873304/2) 
948 
#define C8 FIXHR(0.17364817766693034885/2) 
949  
950  
951 
/* using Lee like decomposition followed by hand coded 9 points DCT */

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

955 
int tmp[18], *tmp1, *in1; 
956  
957 
for(i=17;i>=1;i) 
958 
in[i] += in[i1];

959 
for(i=17;i>=3;i=2) 
960 
in[i] += in[i2];

961  
962 
for(j=0;j<2;j++) { 
963 
tmp1 = tmp + j; 
964 
in1 = in + j; 
965 
#if 0

966 
//more accurate but slower

967 
int64_t t0, t1, t2, t3;

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

969 

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

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

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

973 
tmp1[16] = t1 + t2;

974 

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

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

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

978 

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

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

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

982 

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

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

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

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

987 

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

989 

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

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

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

993 
#else

994 
t2 = in1[2*4] + in1[2*8]  in1[2*2]; 
995  
996 
t3 = in1[2*0] + (in1[2*6]>>1); 
997 
t1 = in1[2*0]  in1[2*6]; 
998 
tmp1[ 6] = t1  (t2>>1); 
999 
tmp1[16] = t1 + t2;

1000  
1001 
t0 = MULH(2*(in1[2*2] + in1[2*4]), C2); 
1002 
t1 = MULH( in1[2*4]  in1[2*8] , 2*C8); 
1003 
t2 = MULH(2*(in1[2*2] + in1[2*8]), C4); 
1004  
1005 
tmp1[10] = t3  t0  t2;

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

1007 
tmp1[14] = t3 + t2  t1;

1008  
1009 
tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7]  in1[2*1]), C3); 
1010 
t2 = MULH(2*(in1[2*1] + in1[2*5]), C1); 
1011 
t3 = MULH( in1[2*5]  in1[2*7] , 2*C7); 
1012 
t0 = MULH(2*in1[2*3], C3); 
1013  
1014 
t1 = MULH(2*(in1[2*1] + in1[2*7]), C5); 
1015  
1016 
tmp1[ 0] = t2 + t3 + t0;

1017 
tmp1[12] = t2 + t1  t0;

1018 
tmp1[ 8] = t3  t1  t0;

1019 
#endif

1020 
} 
1021  
1022 
i = 0;

1023 
for(j=0;j<4;j++) { 
1024 
t0 = tmp[i]; 
1025 
t1 = tmp[i + 2];

1026 
s0 = t1 + t0; 
1027 
s2 = t1  t0; 
1028  
1029 
t2 = tmp[i + 1];

1030 
t3 = tmp[i + 3];

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

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

1033  
1034 
t0 = s0 + s1; 
1035 
t1 = s0  s1; 
1036 
out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j]; 
1037 
out[(8  j)*SBLIMIT] = MULH(t1, win[8  j]) + buf[8  j]; 
1038 
buf[9 + j] = MULH(t0, win[18 + 9 + j]); 
1039 
buf[8  j] = MULH(t0, win[18 + 8  j]); 
1040  
1041 
t0 = s2 + s3; 
1042 
t1 = s2  s3; 
1043 
out[(9 + 8  j)*SBLIMIT] = MULH(t1, win[9 + 8  j]) + buf[9 + 8  j]; 
1044 
out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j]; 
1045 
buf[9 + 8  j] = MULH(t0, win[18 + 9 + 8  j]); 
1046 
buf[ + j] = MULH(t0, win[18 + j]);

1047 
i += 4;

1048 
} 
1049  
1050 
s0 = tmp[16];

1051 
s1 = MULH(2*tmp[17], icos36h[4]); 
1052 
t0 = s0 + s1; 
1053 
t1 = s0  s1; 
1054 
out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4]; 
1055 
out[(8  4)*SBLIMIT] = MULH(t1, win[8  4]) + buf[8  4]; 
1056 
buf[9 + 4] = MULH(t0, win[18 + 9 + 4]); 
1057 
buf[8  4] = MULH(t0, win[18 + 8  4]); 
1058 
} 
1059  
1060 
/* return the number of decoded frames */

1061 
static int mp_decode_layer1(MPADecodeContext *s) 
1062 
{ 
1063 
int bound, i, v, n, ch, j, mant;

1064 
uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT]; 
1065 
uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; 
1066  
1067 
if (s>mode == MPA_JSTEREO)

1068 
bound = (s>mode_ext + 1) * 4; 
1069 
else

1070 
bound = SBLIMIT; 
1071  
1072 
/* allocation bits */

1073 
for(i=0;i<bound;i++) { 
1074 
for(ch=0;ch<s>nb_channels;ch++) { 
1075 
allocation[ch][i] = get_bits(&s>gb, 4);

1076 
} 
1077 
} 
1078 
for(i=bound;i<SBLIMIT;i++) {

1079 
allocation[0][i] = get_bits(&s>gb, 4); 
1080 
} 
1081  
1082 
/* scale factors */

1083 
for(i=0;i<bound;i++) { 
1084 
for(ch=0;ch<s>nb_channels;ch++) { 
1085 
if (allocation[ch][i])

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

1087 
} 
1088 
} 
1089 
for(i=bound;i<SBLIMIT;i++) {

1090 
if (allocation[0][i]) { 
1091 
scale_factors[0][i] = get_bits(&s>gb, 6); 
1092 
scale_factors[1][i] = get_bits(&s>gb, 6); 
1093 
} 
1094 
} 
1095  
1096 
/* compute samples */

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

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

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

1105 
v = 0;

1106 
} 
1107 
s>sb_samples[ch][j][i] = v; 
1108 
} 
1109 
} 
1110 
for(i=bound;i<SBLIMIT;i++) {

1111 
n = allocation[0][i];

1112 
if (n) {

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

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

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

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

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

1118 
} else {

1119 
s>sb_samples[0][j][i] = 0; 
1120 
s>sb_samples[1][j][i] = 0; 
1121 
} 
1122 
} 
1123 
} 
1124 
return 12; 
1125 
} 
1126  
1127 
static int mp_decode_layer2(MPADecodeContext *s) 
1128 
{ 
1129 
int sblimit; /* number of used subbands */ 
1130 
const unsigned char *alloc_table; 
1131 
int table, bit_alloc_bits, i, j, ch, bound, v;

1132 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; 
1133 
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; 
1134 
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; 
1135 
int scale, qindex, bits, steps, k, l, m, b;

1136  
1137 
/* select decoding table */

1138 
table = ff_mpa_l2_select_table(s>bit_rate / 1000, s>nb_channels,

1139 
s>sample_rate, s>lsf); 
1140 
sblimit = ff_mpa_sblimit_table[table]; 
1141 
alloc_table = ff_mpa_alloc_tables[table]; 
1142  
1143 
if (s>mode == MPA_JSTEREO)

1144 
bound = (s>mode_ext + 1) * 4; 
1145 
else

1146 
bound = sblimit; 
1147  
1148 
dprintf(s>avctx, "bound=%d sblimit=%d\n", bound, sblimit);

1149  
1150 
/* sanity check */

1151 
if( bound > sblimit ) bound = sblimit;

1152  
1153 
/* parse bit allocation */

1154 
j = 0;

1155 
for(i=0;i<bound;i++) { 
1156 
bit_alloc_bits = alloc_table[j]; 
1157 
for(ch=0;ch<s>nb_channels;ch++) { 
1158 
bit_alloc[ch][i] = get_bits(&s>gb, bit_alloc_bits); 
1159 
} 
1160 
j += 1 << bit_alloc_bits;

1161 
} 
1162 
for(i=bound;i<sblimit;i++) {

1163 
bit_alloc_bits = alloc_table[j]; 
1164 
v = get_bits(&s>gb, bit_alloc_bits); 
1165 
bit_alloc[0][i] = v;

1166 
bit_alloc[1][i] = v;

1167 
j += 1 << bit_alloc_bits;

1168 
} 
1169  
1170 
#ifdef DEBUG

1171 
{ 
1172 
for(ch=0;ch<s>nb_channels;ch++) { 
1173 
for(i=0;i<sblimit;i++) 
1174 
dprintf(s>avctx, " %d", bit_alloc[ch][i]);

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

1176 
} 
1177 
} 
1178 
#endif

1179  
1180 
/* scale codes */

1181 
for(i=0;i<sblimit;i++) { 
1182 
for(ch=0;ch<s>nb_channels;ch++) { 
1183 
if (bit_alloc[ch][i])

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

1185 
} 
1186 
} 
1187  
1188 
/* scale factors */

1189 
for(i=0;i<sblimit;i++) { 
1190 
for(ch=0;ch<s>nb_channels;ch++) { 
1191 
if (bit_alloc[ch][i]) {

1192 
sf = scale_factors[ch][i]; 
1193 
switch(scale_code[ch][i]) {

1194 
default:

1195 
case 0: 
1196 
sf[0] = get_bits(&s>gb, 6); 
1197 
sf[1] = get_bits(&s>gb, 6); 
1198 
sf[2] = get_bits(&s>gb, 6); 
1199 
break;

1200 
case 2: 
1201 
sf[0] = get_bits(&s>gb, 6); 
1202 
sf[1] = sf[0]; 
1203 
sf[2] = sf[0]; 
1204 
break;

1205 
case 1: 
1206 
sf[0] = get_bits(&s>gb, 6); 
1207 
sf[2] = get_bits(&s>gb, 6); 
1208 
sf[1] = sf[0]; 
1209 
break;

1210 
case 3: 
1211 
sf[0] = get_bits(&s>gb, 6); 
1212 
sf[2] = get_bits(&s>gb, 6); 
1213 
sf[1] = sf[2]; 
1214 
break;

1215 
} 
1216 
} 
1217 
} 
1218 
} 
1219  
1220 
#ifdef DEBUG

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

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

1227 
dprintf(s>avctx, " ");

1228 
} 
1229 
} 
1230 
dprintf(s>avctx, "\n");

1231 
} 
1232 
#endif

1233  
1234 
/* samples */

1235 
for(k=0;k<3;k++) { 
1236 
for(l=0;l<12;l+=3) { 
1237 
j = 0;

1238 
for(i=0;i<bound;i++) { 
1239 
bit_alloc_bits = alloc_table[j]; 
1240 
for(ch=0;ch<s>nb_channels;ch++) { 
1241 
b = bit_alloc[ch][i]; 
1242 
if (b) {

1243 
scale = scale_factors[ch][i][k]; 
1244 
qindex = alloc_table[j+b]; 
1245 
bits = ff_mpa_quant_bits[qindex]; 
1246 
if (bits < 0) { 
1247 
/* 3 values at the same time */

1248 
v = get_bits(&s>gb, bits); 
1249 
steps = ff_mpa_quant_steps[qindex]; 
1250 
s>sb_samples[ch][k * 12 + l + 0][i] = 
1251 
l2_unscale_group(steps, v % steps, scale); 
1252 
v = v / steps; 
1253 
s>sb_samples[ch][k * 12 + l + 1][i] = 
1254 
l2_unscale_group(steps, v % steps, scale); 
1255 
v = v / steps; 
1256 
s>sb_samples[ch][k * 12 + l + 2][i] = 
1257 
l2_unscale_group(steps, v, scale); 
1258 
} else {

1259 
for(m=0;m<3;m++) { 
1260 
v = get_bits(&s>gb, bits); 
1261 
v = l1_unscale(bits  1, v, scale);

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

1263 
} 
1264 
} 
1265 
} else {

1266 
s>sb_samples[ch][k * 12 + l + 0][i] = 0; 
1267 
s>sb_samples[ch][k * 12 + l + 1][i] = 0; 
1268 
s>sb_samples[ch][k * 12 + l + 2][i] = 0; 
1269 
} 
1270 
} 
1271 
/* next subband in alloc table */

1272 
j += 1 << bit_alloc_bits;

1273 
} 
1274 
/* XXX: find a way to avoid this duplication of code */

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

1276 
bit_alloc_bits = alloc_table[j]; 
1277 
b = bit_alloc[0][i];

1278 
if (b) {

1279 
int mant, scale0, scale1;

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

1281 
scale1 = scale_factors[1][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 
mant = v % steps; 
1289 
v = v / steps; 
1290 
s>sb_samples[0][k * 12 + l + 0][i] = 
1291 
l2_unscale_group(steps, mant, scale0); 
1292 
s>sb_samples[1][k * 12 + l + 0][i] = 
1293 
l2_unscale_group(steps, mant, scale1); 
1294 
mant = v % steps; 
1295 
v = v / steps; 
1296 
s>sb_samples[0][k * 12 + l + 1][i] = 
1297 
l2_unscale_group(steps, mant, scale0); 
1298 
s>sb_samples[1][k * 12 + l + 1][i] = 
1299 
l2_unscale_group(steps, mant, scale1); 
1300 
s>sb_samples[0][k * 12 + l + 2][i] = 
1301 
l2_unscale_group(steps, v, scale0); 
1302 
s>sb_samples[1][k * 12 + l + 2][i] = 
1303 
l2_unscale_group(steps, v, scale1); 
1304 
} else {

1305 
for(m=0;m<3;m++) { 
1306 
mant = get_bits(&s>gb, bits); 
1307 
s>sb_samples[0][k * 12 + l + m][i] = 
1308 
l1_unscale(bits  1, mant, scale0);

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

1311 
} 
1312 
} 
1313 
} else {

1314 
s>sb_samples[0][k * 12 + l + 0][i] = 0; 
1315 
s>sb_samples[0][k * 12 + l + 1][i] = 0; 
1316 
s>sb_samples[0][k * 12 + l + 2][i] = 0; 
1317 
s>sb_samples[1][k * 12 + l + 0][i] = 0; 
1318 
s>sb_samples[1][k * 12 + l + 1][i] = 0; 
1319 
s>sb_samples[1][k * 12 + l + 2][i] = 0; 
1320 
} 
1321 
/* next subband in alloc table */

1322 
j += 1 << bit_alloc_bits;

1323 
} 
1324 
/* fill remaining samples to zero */

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

1326 
for(ch=0;ch<s>nb_channels;ch++) { 
1327 
s>sb_samples[ch][k * 12 + l + 0][i] = 0; 
1328 
s>sb_samples[ch][k * 12 + l + 1][i] = 0; 
1329 
s>sb_samples[ch][k * 12 + l + 2][i] = 0; 
1330 
} 
1331 
} 
1332 
} 
1333 
} 
1334 
return 3 * 12; 
1335 
} 
1336  
1337 
static inline void lsf_sf_expand(int *slen, 
1338 
int sf, int n1, int n2, int n3) 
1339 
{ 
1340 
if (n3) {

1341 
slen[3] = sf % n3;

1342 
sf /= n3; 
1343 
} else {

1344 
slen[3] = 0; 
1345 
} 
1346 
if (n2) {

1347 
slen[2] = sf % n2;

1348 
sf /= n2; 
1349 
} else {

1350 
slen[2] = 0; 
1351 
} 
1352 
slen[1] = sf % n1;

1353 
sf /= n1; 
1354 
slen[0] = sf;

1355 
} 
1356  
1357 
static void exponents_from_scale_factors(MPADecodeContext *s, 
1358 
GranuleDef *g, 
1359 
int16_t *exponents) 
1360 
{ 
1361 
const uint8_t *bstab, *pretab;

1362 
int len, i, j, k, l, v0, shift, gain, gains[3]; 
1363 
int16_t *exp_ptr; 
1364  
1365 
exp_ptr = exponents; 
1366 
gain = g>global_gain  210;

1367 
shift = g>scalefac_scale + 1;

1368  
1369 
bstab = band_size_long[s>sample_rate_index]; 
1370 
pretab = mpa_pretab[g>preflag]; 
1371 
for(i=0;i<g>long_end;i++) { 
1372 
v0 = gain  ((g>scale_factors[i] + pretab[i]) << shift) + 400;

1373 
len = bstab[i]; 
1374 
for(j=len;j>0;j) 
1375 
*exp_ptr++ = v0; 
1376 
} 
1377  
1378 
if (g>short_start < 13) { 
1379 
bstab = band_size_short[s>sample_rate_index]; 
1380 
gains[0] = gain  (g>subblock_gain[0] << 3); 
1381 
gains[1] = gain  (g>subblock_gain[1] << 3); 
1382 
gains[2] = gain  (g>subblock_gain[2] << 3); 
1383 
k = g>long_end; 
1384 
for(i=g>short_start;i<13;i++) { 
1385 
len = bstab[i]; 
1386 
for(l=0;l<3;l++) { 
1387 
v0 = gains[l]  (g>scale_factors[k++] << shift) + 400;

1388 
for(j=len;j>0;j) 
1389 
*exp_ptr++ = v0; 
1390 
} 
1391 
} 
1392 
} 
1393 
} 
1394  
1395 
/* handle n = 0 too */

1396 
static inline int get_bitsz(GetBitContext *s, int n) 
1397 
{ 
1398 
if (n == 0) 
1399 
return 0; 
1400 
else

1401 
return get_bits(s, n);

1402 
} 
1403  
1404  
1405 
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){ 
1406 
if(s>in_gb.buffer && *pos >= s>gb.size_in_bits){

1407 
s>gb= s>in_gb; 
1408 
s>in_gb.buffer=NULL;

1409 
assert((get_bits_count(&s>gb) & 7) == 0); 
1410 
skip_bits_long(&s>gb, *pos  *end_pos); 
1411 
*end_pos2= 
1412 
*end_pos= *end_pos2 + get_bits_count(&s>gb)  *pos; 
1413 
*pos= get_bits_count(&s>gb); 
1414 
} 
1415 
} 
1416  
1417 
static int huffman_decode(MPADecodeContext *s, GranuleDef *g, 
1418 
int16_t *exponents, int end_pos2)

1419 
{ 
1420 
int s_index;

1421 
int i;

1422 
int last_pos, bits_left;

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

1425  
1426 
/* low frequencies (called big values) */

1427 
s_index = 0;

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

1430 
j = g>region_size[i]; 
1431 
if (j == 0) 
1432 
continue;

1433 
/* select vlc table */

1434 
k = g>table_select[i]; 
1435 
l = mpa_huff_data[k][0];

1436 
linbits = mpa_huff_data[k][1];

1437 
vlc = &huff_vlc[l]; 
1438  
1439 
if(!l){

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

1442 
continue;

1443 
} 
1444  
1445 
/* read huffcode and compute each couple */

1446 
for(;j>0;j) { 
1447 
int exponent, x, y, v;

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

1449  
1450 
if (pos >= end_pos){

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

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

1454 
if(pos >= end_pos)

1455 
break;

1456 
} 
1457 
y = get_vlc2(&s>gb, vlc>table, 7, 3); 
1458  
1459 
if(!y){

1460 
g>sb_hybrid[s_index ] = 
1461 
g>sb_hybrid[s_index+1] = 0; 
1462 
s_index += 2;

1463 
continue;

1464 
} 
1465  
1466 
exponent= exponents[s_index]; 
1467  
1468 
dprintf(s>avctx, "region=%d n=%d x=%d y=%d exp=%d\n",

1469 
i, g>region_size[i]  j, x, y, exponent); 
1470 
if(y&16){ 
1471 
x = y >> 5;

1472 
y = y & 0x0f;

1473 
if (x < 15){ 
1474 
v = expval_table[ exponent ][ x ]; 
1475 
// v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0  (exponent>>2), 31);

1476 
}else{

1477 
x += get_bitsz(&s>gb, linbits); 
1478 
v = l3_unscale(x, exponent); 
1479 
} 
1480 
if (get_bits1(&s>gb))

1481 
v = v; 
1482 
g>sb_hybrid[s_index] = v; 
1483 
if (y < 15){ 
1484 
v = expval_table[ exponent ][ y ]; 
1485 
}else{

1486 
y += get_bitsz(&s>gb, linbits); 
1487 
v = l3_unscale(y, exponent); 
1488 
} 
1489 
if (get_bits1(&s>gb))

1490 
v = v; 
1491 
g>sb_hybrid[s_index+1] = v;

1492 
}else{

1493 
x = y >> 5;

1494 
y = y & 0x0f;

1495 
x += y; 
1496 
if (x < 15){ 
1497 
v = expval_table[ exponent ][ x ]; 
1498 
}else{

1499 
x += get_bitsz(&s>gb, linbits); 
1500 
v = l3_unscale(x, exponent); 
1501 
} 
1502 
if (get_bits1(&s>gb))

1503 
v = v; 
1504 
g>sb_hybrid[s_index+!!y] = v; 
1505 
g>sb_hybrid[s_index+ !y] = 0;

1506 
} 
1507 
s_index+=2;

1508 
} 
1509 
} 
1510  
1511 
/* high frequencies */

1512 
vlc = &huff_quad_vlc[g>count1table_select]; 
1513 
last_pos=0;

1514 
while (s_index <= 572) { 
1515 
int pos, code;

1516 
pos = get_bits_count(&s>gb); 
1517 
if (pos >= end_pos) {

1518 
if (pos > end_pos2 && last_pos){

1519 
/* some encoders generate an incorrect size for this

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

1521 
s_index = 4;

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

1525 
s_index=0;

1526 
break;

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

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

1531 
if(pos >= end_pos)

1532 
break;

1533 
} 
1534 
last_pos= pos; 
1535  
1536 
code = get_vlc2(&s>gb, vlc>table, vlc>bits, 1);

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

1538 
g>sb_hybrid[s_index+0]=

1539 
g>sb_hybrid[s_index+1]=

1540 
g>sb_hybrid[s_index+2]=

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

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

1545 
int pos= s_index+idxtab[code];

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

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

1549 
if(get_bits1(&s>gb))

1550 
v = v; 
1551 
g>sb_hybrid[pos] = v; 
1552 
} 
1553 
s_index+=4;

1554 
} 
1555 
/* skip extension bits */

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

1558 
if (bits_left < 0/*  bits_left > 500*/) { 
1559 
av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left); 
1560 
s_index=0;

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

1564 
} 
1565 
memset(&g>sb_hybrid[s_index], 0, sizeof(*g>sb_hybrid)*(576  s_index)); 
1566 
skip_bits_long(&s>gb, bits_left); 
1567  
1568 
i= get_bits_count(&s>gb); 
1569 
switch_buffer(s, &i, &end_pos, &end_pos2); 
1570  
1571 
return 0; 
1572 
} 
1573  
1574 
/* Reorder short blocks from bitstream order to interleaved order. It

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

1576 
complicated */

1577 
static void reorder_block(MPADecodeContext *s, GranuleDef *g) 
1578 
{ 
1579 
int i, j, len;

1580 
int32_t *ptr, *dst, *ptr1; 
1581 
int32_t tmp[576];

1582  
1583 
if (g>block_type != 2) 
1584 
return;

1585  
1586 
if (g>switch_point) {

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

1589 
} else {

1590 
ptr = g>sb_hybrid + 48;

1591 
} 
1592 
} else {

1593 
ptr = g>sb_hybrid; 
1594 
} 
1595  
1596 
for(i=g>short_start;i<13;i++) { 
1597 
len = band_size_short[s>sample_rate_index][i]; 
1598 
ptr1 = ptr; 
1599 
dst = tmp; 
1600 
for(j=len;j>0;j) { 
1601 
*dst++ = ptr[0*len];

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

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

1604 
ptr++; 
1605 
} 
1606 
ptr+=2*len;

1607 
memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1)); 
1608 
} 
1609 
} 
1610  
1611 
#define ISQRT2 FIXR(0.70710678118654752440) 
1612  
1613 
static void compute_stereo(MPADecodeContext *s, 
1614 
GranuleDef *g0, GranuleDef *g1) 
1615 
{ 
1616 
int i, j, k, l;

1617 
int32_t v1, v2; 
1618 
int sf_max, tmp0, tmp1, sf, len, non_zero_found;

1619 
int32_t (*is_tab)[16];

1620 
int32_t *tab0, *tab1; 
1621 
int non_zero_found_short[3]; 
1622  
1623 
/* intensity stereo */

1624 
if (s>mode_ext & MODE_EXT_I_STEREO) {

1625 
if (!s>lsf) {

1626 
is_tab = is_table; 
1627 
sf_max = 7;

1628 
} else {

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

1630 
sf_max = 16;

1631 
} 
1632  
1633 
tab0 = g0>sb_hybrid + 576;

1634 
tab1 = g1>sb_hybrid + 576;

1635  
1636 
non_zero_found_short[0] = 0; 
1637 
non_zero_found_short[1] = 0; 
1638 
non_zero_found_short[2] = 0; 
1639 
k = (13  g1>short_start) * 3 + g1>long_end  3; 
1640 
for(i = 12;i >= g1>short_start;i) { 
1641 
/* for last band, use previous scale factor */

1642 
if (i != 11) 
1643 
k = 3;

1644 
len = band_size_short[s>sample_rate_index][i]; 
1645 
for(l=2;l>=0;l) { 
1646 
tab0 = len; 
1647 
tab1 = len; 
1648 
if (!non_zero_found_short[l]) {

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

1650 
for(j=0;j<len;j++) { 
1651 
if (tab1[j] != 0) { 
1652 
non_zero_found_short[l] = 1;

1653 
goto found1;

1654 
} 
1655 
} 
1656 
sf = g1>scale_factors[k + l]; 
1657 
if (sf >= sf_max)

1658 
goto found1;

1659  
1660 
v1 = is_tab[0][sf];

1661 
v2 = is_tab[1][sf];

1662 
for(j=0;j<len;j++) { 
1663 
tmp0 = tab0[j]; 
1664 
tab0[j] = MULL(tmp0, v1); 
1665 
tab1[j] = MULL(tmp0, v2); 
1666 
} 
1667 
} else {

1668 
found1:

1669 
if (s>mode_ext & MODE_EXT_MS_STEREO) {

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

1671 
if enabled */

1672 
for(j=0;j<len;j++) { 
1673 
tmp0 = tab0[j]; 
1674 
tmp1 = tab1[j]; 
1675 
tab0[j] = MULL(tmp0 + tmp1, ISQRT2); 
1676 
tab1[j] = MULL(tmp0  tmp1, ISQRT2); 
1677 
} 
1678 
} 
1679 
} 
1680 
} 
1681 
} 
1682  
1683 
non_zero_found = non_zero_found_short[0] 

1684 
non_zero_found_short[1] 

1685 
non_zero_found_short[2];

1686  
1687 
for(i = g1>long_end  1;i >= 0;i) { 
1688 
len = band_size_long[s>sample_rate_index][i]; 
1689 
tab0 = len; 
1690 
tab1 = len; 
1691 
/* test if non zero band. if so, stop doing istereo */

1692 
if (!non_zero_found) {

1693 
for(j=0;j<len;j++) { 
1694 
if (tab1[j] != 0) { 
1695 
non_zero_found = 1;

1696 
goto found2;

1697 
} 
1698 
} 
1699 
/* for last band, use previous scale factor */

1700 
k = (i == 21) ? 20 : i; 
1701 
sf = g1>scale_factors[k]; 
1702 
if (sf >= sf_max)

1703 
goto found2;

1704 
v1 = is_tab[0][sf];

1705 
v2 = is_tab[1][sf];

1706 
for(j=0;j<len;j++) { 
1707 
tmp0 = tab0[j]; 
1708 
tab0[j] = MULL(tmp0, v1); 
1709 
tab1[j] = MULL(tmp0, v2); 
1710 
} 
1711 
} else {

1712 
found2:

1713 
if (s>mode_ext & MODE_EXT_MS_STEREO) {

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

1715 
if enabled */

1716 
for(j=0;j<len;j++) { 
1717 
tmp0 = tab0[j]; 
1718 
tmp1 = tab1[j]; 
1719 
tab0[j] = MULL(tmp0 + tmp1, ISQRT2); 
1720 
tab1[j] = MULL(tmp0  tmp1, ISQRT2); 
1721 
} 
1722 
} 
1723 
} 
1724 
} 
1725 
} else if (s>mode_ext & MODE_EXT_MS_STEREO) { 
1726 
/* ms stereo ONLY */

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

1728 
global gain */

1729 
tab0 = g0>sb_hybrid; 
1730 
tab1 = g1>sb_hybrid; 
1731 
for(i=0;i<576;i++) { 
1732 
tmp0 = tab0[i]; 
1733 
tmp1 = tab1[i]; 
1734 
tab0[i] = tmp0 + tmp1; 
1735 
tab1[i] = tmp0  tmp1; 
1736 
} 
1737 
} 
1738 
} 
1739  
1740 
static void compute_antialias_integer(MPADecodeContext *s, 
1741 
GranuleDef *g) 
1742 
{ 
1743 
int32_t *ptr, *csa; 
1744 
int n, i;

1745  
1746 
/* we antialias only "long" bands */

1747 
if (g>block_type == 2) { 
1748 
if (!g>switch_point)

1749 
return;

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

1751 
n = 1;

1752 
} else {

1753 
n = SBLIMIT  1;

1754 
} 
1755  
1756 
ptr = g>sb_hybrid + 18;

1757 
for(i = n;i > 0;i) { 
1758 
int tmp0, tmp1, tmp2;

1759 
csa = &csa_table[0][0]; 
1760 
#define INT_AA(j) \

1761 
tmp0 = ptr[1j];\

1762 
tmp1 = ptr[ j];\ 
1763 
tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\ 
1764 
ptr[1j] = 4*(tmp2  MULH(tmp1, csa[2+4*j]));\ 
1765 
ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j])); 
1766  
1767 
INT_AA(0)

1768 
INT_AA(1)

1769 
INT_AA(2)

1770 
INT_AA(3)

1771 
INT_AA(4)

1772 
INT_AA(5)

1773 
INT_AA(6)

1774 
INT_AA(7)

1775  
1776 
ptr += 18;

1777 
} 
1778 
} 
1779  
1780 
static void compute_antialias_float(MPADecodeContext *s, 
1781 
GranuleDef *g) 
1782 
{ 
1783 
int32_t *ptr; 
1784 
int n, i;

1785  
1786 
/* we antialias only "long" bands */

1787 
if (g>block_type == 2) { 
1788 
if (!g>switch_point)

1789 
return;

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

1791 
n = 1;

1792 
} else {

1793 
n = SBLIMIT  1;

1794 
} 
1795  
1796 
ptr = g>sb_hybrid + 18;

1797 
for(i = n;i > 0;i) { 
1798 
float tmp0, tmp1;

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

1801 
tmp0= ptr[1j];\

1802 
tmp1= ptr[ j];\ 
1803 
ptr[1j] = lrintf(tmp0 * csa[0+4*j]  tmp1 * csa[1+4*j]);\ 
1804 
ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]); 
1805  
1806 
FLOAT_AA(0)

1807 
FLOAT_AA(1)

1808 
FLOAT_AA(2)

1809 
FLOAT_AA(3)

1810 
FLOAT_AA(4)

1811 
FLOAT_AA(5)

1812 
FLOAT_AA(6)

1813 
FLOAT_AA(7)

1814  
1815 
ptr += 18;

1816 
} 
1817 
} 
1818  
1819 
static void compute_imdct(MPADecodeContext *s, 
1820 
GranuleDef *g, 
1821 
int32_t *sb_samples, 
1822 
int32_t *mdct_buf) 
1823 
{ 
1824 
int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1; 
1825 
int32_t out2[12];

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

1827  
1828 
/* find last non zero block */

1829 
ptr = g>sb_hybrid + 576;

1830 
ptr1 = g>sb_hybrid + 2 * 18; 
1831 
while (ptr >= ptr1) {

1832 
ptr = 6;

1833 
v = ptr[0]  ptr[1]  ptr[2]  ptr[3]  ptr[4]  ptr[5]; 
1834 
if (v != 0) 
1835 
break;

1836 
} 
1837 
sblimit = ((ptr  g>sb_hybrid) / 18) + 1; 
1838  
1839 
if (g>block_type == 2) { 
1840 
/* XXX: check for 8000 Hz */

1841 
if (g>switch_point)

1842 
mdct_long_end = 2;

1843 
else

1844 
mdct_long_end = 0;

1845 
} else {

1846 
mdct_long_end = sblimit; 
1847 
} 
1848  
1849 
buf = mdct_buf; 
1850 
ptr = g>sb_hybrid; 
1851 
for(j=0;j<mdct_long_end;j++) { 
1852 
/* apply window & overlap with previous buffer */

1853 
out_ptr = sb_samples + j; 
1854 
/* select window */

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

1857 
else

1858 
win1 = mdct_win[g>block_type]; 
1859 
/* select frequency inversion */

1860 
win = win1 + ((4 * 36) & (j & 1)); 
1861 
imdct36(out_ptr, buf, ptr, win); 
1862 
out_ptr += 18*SBLIMIT;

1863 
ptr += 18;

1864 
buf += 18;

1865 
} 
1866 
for(j=mdct_long_end;j<sblimit;j++) {

1867 
/* select frequency inversion */

1868 
win = mdct_win[2] + ((4 * 36) & (j & 1)); 
1869 
out_ptr = sb_samples + j; 
1870  
1871 
for(i=0; i<6; i++){ 
1872 
*out_ptr = buf[i]; 
1873 
out_ptr += SBLIMIT; 
1874 
} 
1875 
imdct12(out2, ptr + 0);

1876 
for(i=0;i<6;i++) { 
1877 
*out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1]; 
1878 
buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]); 
1879 
out_ptr += SBLIMIT; 
1880 
} 
1881 
imdct12(out2, ptr + 1);

1882 
for(i=0;i<6;i++) { 
1883 
*out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2]; 
1884 
buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]); 
1885 
out_ptr += SBLIMIT; 
1886 
} 
1887 
imdct12(out2, ptr + 2);

1888 
for(i=0;i<6;i++) { 
1889 
buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0]; 
1890 
buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]); 
1891 
buf[i + 6*2] = 0; 
1892 
} 
1893 
ptr += 18;

1894 
buf += 18;

1895 
} 
1896 
/* zero bands */

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

1898 
/* overlap */

1899 
out_ptr = sb_samples + j; 
1900 
for(i=0;i<18;i++) { 
1901 
*out_ptr = buf[i]; 
1902 
buf[i] = 0;

1903 
out_ptr += SBLIMIT; 
1904 
} 
1905 
buf += 18;

1906 
} 
1907 
} 
1908  
1909 
#if defined(DEBUG)

1910 
void sample_dump(int fnum, int32_t *tab, int n) 
1911 
{ 
1912 
static FILE *files[16], *f; 
1913 
char buf[512]; 
1914 
int i;

1915 
int32_t v; 
1916  
1917 
f = files[fnum]; 
1918 
if (!f) {

1919 
snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm", 
1920 
fnum, 
1921 
#ifdef USE_HIGHPRECISION

1922 
"hp"

1923 
#else

1924 
"lp"

1925 
#endif

1926 
); 
1927 
f = fopen(buf, "w");

1928 
if (!f)

1929 
return;

1930 
files[fnum] = f; 
1931 
} 
1932  
1933 
if (fnum == 0) { 
1934 
static int pos = 0; 
1935 
av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos); 
1936 
for(i=0;i<n;i++) { 
1937 
av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE); 
1938 
if ((i % 18) == 17) 
1939 
av_log(NULL, AV_LOG_DEBUG, "\n"); 
1940 
} 
1941 
pos += n; 
1942 
} 
1943 
for(i=0;i<n;i++) { 
1944 
/* normalize to 23 frac bits */

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

1946 
fwrite(&v, 1, sizeof(int32_t), f); 
1947 
} 
1948 
} 
1949 
#endif

1950  
1951  
1952 
/* main layer3 decoding function */

1953 
static int mp_decode_layer3(MPADecodeContext *s) 
1954 
{ 
1955 
int nb_granules, main_data_begin, private_bits;

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

1957 
GranuleDef granules[2][2], *g; 
1958 
int16_t exponents[576];

1959  
1960 
/* read side info */

1961 
if (s>lsf) {

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

1963 
private_bits = get_bits(&s>gb, s>nb_channels); 
1964 
nb_granules = 1;

1965 
} else {

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

1967 
if (s>nb_channels == 2) 
1968 
private_bits = get_bits(&s>gb, 3);

1969 
else

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

1971 
nb_granules = 2;

1972 
for(ch=0;ch<s>nb_channels;ch++) { 
1973 
granules[ch][0].scfsi = 0; /* all scale factors are transmitted */ 
1974 
granules[ch][1].scfsi = get_bits(&s>gb, 4); 
1975 
} 
1976 
} 
1977  
1978 
for(gr=0;gr<nb_granules;gr++) { 
1979 
for(ch=0;ch<s>nb_channels;ch++) { 
1980 
dprintf(s>avctx, "gr=%d ch=%d: side_info\n", gr, ch);

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

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

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

1986 
return 1; 
1987 
} 
1988  
1989 
g>global_gain = get_bits(&s>gb, 8);

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

1991 
1/sqrt(2) renormalization factor */

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

1993 
MODE_EXT_MS_STEREO) 
1994 
g>global_gain = 2;

1995 
if (s>lsf)

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

1997 
else

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

1999 
blocksplit_flag = get_bits1(&s>gb); 
2000 
if (blocksplit_flag) {

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

2002 
if (g>block_type == 0){ 
2003 
av_log(NULL, AV_LOG_ERROR, "invalid block type\n"); 
2004 
return 1; 
2005 
} 
2006 
g>switch_point = get_bits1(&s>gb); 
2007 
for(i=0;i<2;i++) 
2008 
g>table_select[i] = get_bits(&s>gb, 5);

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

2011 
/* compute huffman coded region sizes */

2012 
if (g>block_type == 2) 
2013 
g>region_size[0] = (36 / 2); 
2014 
else {

2015 
if (s>sample_rate_index <= 2) 
2016 
g>region_size[0] = (36 / 2); 
2017 
else if (s>sample_rate_index != 8) 
2018 
g>region_size[0] = (54 / 2); 
2019 
else

2020 
g>region_size[0] = (108 / 2); 
2021 
} 
2022 
g>region_size[1] = (576 / 2); 
2023 
} else {

2024 
int region_address1, region_address2, l;

2025 
g>block_type = 0;

2026 
g>switch_point = 0;

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

2029 
/* compute huffman coded region sizes */

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

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

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

2033 
region_address1, region_address2); 
2034 
g>region_size[0] =

2035 
band_index_long[s>sample_rate_index][region_address1 + 1] >> 1; 
2036 
l = region_address1 + region_address2 + 2;

2037 
/* should not overflow */

2038 
if (l > 22) 
2039 
l = 22;

2040 
g>region_size[1] =

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

2042 
} 
2043 
/* convert region offsets to region sizes and truncate

2044 
size to big_values */

2045 
g>region_size[2] = (576 / 2); 
2046 
j = 0;

2047 
for(i=0;i<3;i++) { 
2048 
k = FFMIN(g>region_size[i], g>big_values); 
2049 
g>region_size[i] = k  j; 
2050 
j = k; 
2051 
} 
2052  
2053 
/* compute band indexes */

2054 
if (g>block_type == 2) { 
2055 
if (g>switch_point) {

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

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

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

2059 
if (s>sample_rate_index <= 2) 
2060 
g>long_end = 8;

2061 
else if (s>sample_rate_index != 8) 
2062 
g>long_end = 6;

2063 
else

2064 
g>long_end = 4; /* 8000 Hz */ 
2065  
2066 
g>short_start = 2 + (s>sample_rate_index != 8); 
2067 
} else {

2068 
g>long_end = 0;

2069 
g>short_start = 0;

2070 
} 
2071 
} else {

2072 
g>short_start = 13;

2073 
g>long_end = 22;

2074 
} 
2075  
2076 
g>preflag = 0;

2077 
if (!s>lsf)

2078 
g>preflag = get_bits1(&s>gb); 
2079 
g>scalefac_scale = get_bits1(&s>gb); 
2080 
g>count1table_select = get_bits1(&s>gb); 
2081 
dprintf(s>avctx, "block_type=%d switch_point=%d\n",

2082 
g>block_type, g>switch_point); 
2083 
} 
2084 
} 
2085  
2086 
if (!s>adu_mode) {

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

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

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

2092  
2093 
memcpy(s>last_buf + s>last_buf_size, ptr, EXTRABYTES); 
2094 
s>in_gb= s>gb; 
2095 
init_get_bits(&s>gb, s>last_buf, s>last_buf_size*8);

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

2097 
} 
2098  
2099 
for(gr=0;gr<nb_granules;gr++) { 
2100 
for(ch=0;ch<s>nb_channels;ch++) { 
2101 
g = &granules[ch][gr]; 
2102 
if(get_bits_count(&s>gb)<0){ 
2103 
av_log(NULL, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n", 
2104 
main_data_begin, s>last_buf_size, gr); 
2105 
skip_bits_long(&s>gb, g>part2_3_length); 
2106 
memset(g>sb_hybrid, 0, sizeof(g>sb_hybrid)); 
2107 
if(get_bits_count(&s>gb) >= s>gb.size_in_bits && s>in_gb.buffer){

2108 
skip_bits_long(&s>in_gb, get_bits_count(&s>gb)  s>gb.size_in_bits); 
2109 
s>gb= s>in_gb; 
2110 
s>in_gb.buffer=NULL;

2111 
} 
2112 
continue;

2113 
} 
2114  
2115 
bits_pos = get_bits_count(&s>gb); 
2116  
2117 
if (!s>lsf) {

2118 
uint8_t *sc; 
2119 
int slen, slen1, slen2;

2120  
2121 
/* MPEG1 scale factors */

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

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

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

2125 
if (g>block_type == 2) { 
2126 
n = g>switch_point ? 17 : 18; 
2127 
j = 0;

2128 
if(slen1){

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

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

2134 
} 
2135 
if(slen2){

2136 
for(i=0;i<18;i++) 
2137 
g>scale_factors[j++] = get_bits(&s>gb, slen2); 
2138 
for(i=0;i<3;i++) 
2139 
g>scale_factors[j++] = 0;

2140 
}else{

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

2143 
} 
2144 
} else {

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

2146 
j = 0;

2147 
for(k=0;k<4;k++) { 
2148 
n = (k == 0 ? 6 : 5); 
2149 
if ((g>scfsi & (0x8 >> k)) == 0) { 
2150 
slen = (k < 2) ? slen1 : slen2;

2151 
if(slen){

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

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

2157 
} 
2158 
} else {

2159 
/* simply copy from last granule */

2160 
for(i=0;i<n;i++) { 
2161 
g>scale_factors[j] = sc[j]; 
2162 
j++; 
2163 
} 
2164 
} 
2165 
} 
2166 
g>scale_factors[j++] = 0;

2167 
} 
2168 
#if defined(DEBUG)

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

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

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

2175 
} 
2176 
#endif

2177 
} else {

2178 
int tindex, tindex2, slen[4], sl, sf; 
2179  
2180 
/* LSF scale factors */

2181 
if (g>block_type == 2) { 
2182 
tindex = g>switch_point ? 2 : 1; 
2183 
} else {

2184 
tindex = 0;

2185 
} 
2186 
sf = g>scalefac_compress; 
2187 
if ((s>mode_ext & MODE_EXT_I_STEREO) && ch == 1) { 
2188 
/* intensity stereo case */

2189 
sf >>= 1;

2190 
if (sf < 180) { 
2191 
lsf_sf_expand(slen, sf, 6, 6, 0); 
2192 
tindex2 = 3;

2193 
} else if (sf < 244) { 
2194 
lsf_sf_expand(slen, sf  180, 4, 4, 0); 
2195 
tindex2 = 4;

2196 
} else {

2197 
lsf_sf_expand(slen, sf  244, 3, 0, 0); 
2198 
tindex2 = 5;

2199 
} 
2200 
} else {

2201 
/* normal case */

2202 
if (sf < 400) { 
2203 
lsf_sf_expand(slen, sf, 5, 4, 4); 
2204 
tindex2 = 0;

2205 
} else if (sf < 500) { 
2206 
lsf_sf_expand(slen, sf  400, 5, 4, 0); 
2207 
tindex2 = 1;

2208 
} else {

2209 
lsf_sf_expand(slen, sf  500, 3, 0, 0); 
2210 
tindex2 = 2;

2211 
g>preflag = 1;

2212 
} 
2213 
} 
2214  
2215 
j = 0;

2216 
for(k=0;k<4;k++) { 
2217 
n = lsf_nsf_table[tindex2][tindex][k]; 
2218 
sl = slen[k]; 
2219 
if(sl){

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

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

2225 
} 
2226 
} 
2227 
/* XXX: should compute exact size */

2228 
for(;j<40;j++) 
2229 
g>scale_factors[j] = 0;

2230 
#if defined(DEBUG)

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

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

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

2237 
} 
2238 
#endif

2239 
} 
2240  
2241 
exponents_from_scale_factors(s, g, exponents); 
2242  
2243 
/* read Huffman coded residue */

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

2246 
sample_dump(0, g>sb_hybrid, 576); 
2247 
#endif

2248 
} /* ch */

2249  
2250 
if (s>nb_channels == 2) 
2251 
compute_stereo(s, &granules[0][gr], &granules[1][gr]); 
2252  
2253 
for(ch=0;ch<s>nb_channels;ch++) { 
2254 
g = &granules[ch][gr]; 
2255  
2256 
reorder_block(s, g); 
2257 
#if defined(DEBUG)

2258 
sample_dump(0, g>sb_hybrid, 576); 
2259 
#endif

2260 
s>compute_antialias(s, g); 
2261 
#if defined(DEBUG)

2262 
sample_dump(1, g>sb_hybrid, 576); 
2263 
#endif

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

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

2268 
} 
2269 
} /* gr */

2270 
if(get_bits_count(&s>gb)<0) 
2271 
skip_bits_long(&s>gb, get_bits_count(&s>gb)); 
2272 
return nb_granules * 18; 
2273 
} 
2274  
2275 
static int mp_decode_frame(MPADecodeContext *s, 
2276 
OUT_INT *samples, const uint8_t *buf, int buf_size) 
2277 
{ 
2278 
int i, nb_frames, ch;

2279 
OUT_INT *samples_ptr; 
2280  
2281 
init_get_bits(&s>gb, buf + HEADER_SIZE, (buf_size  HEADER_SIZE)*8);

2282  
2283 
/* skip error protection field */

2284 
if (s>error_protection)

2285 
skip_bits(&s>gb, 16);

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

2288 
switch(s>layer) {

2289 
case 1: 
2290 
nb_frames = mp_decode_layer1(s); 
2291 
break;

2292 
case 2: 
2293 
nb_frames = mp_decode_layer2(s); 
2294 
break;

2295 
case 3: 
2296 
default:

2297 
nb_frames = mp_decode_layer3(s); 
2298  
2299 
s>last_buf_size=0;

2300 
if(s>in_gb.buffer){

2301 
align_get_bits(&s>gb); 
2302 
i= (s>gb.size_in_bits  get_bits_count(&s>gb))>>3;

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

2305 
s>last_buf_size=i; 
2306 
}else

2307 
av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i); 
2308 
s>gb= s>in_gb; 
2309 
s>in_gb.buffer= NULL;

2310 
} 
2311  
2312 
align_get_bits(&s>gb); 
2313 
assert((get_bits_count(&s>gb) & 7) == 0); 
2314 
i= (s>gb.size_in_bits  get_bits_count(&s>gb))>>3;

2315  
2316 
if(i<0  i > BACKSTEP_SIZE  nb_frames<0){ 
2317 
av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i); 
2318 
i= FFMIN(BACKSTEP_SIZE, buf_size  HEADER_SIZE); 
2319 
} 
2320 
assert(i <= buf_size  HEADER_SIZE && i>= 0);

2321 
memcpy(s>last_buf + s>last_buf_size, s>gb.buffer + buf_size  HEADER_SIZE  i, i); 
2322 
s>last_buf_size += i; 
2323  
2324 
break;

2325 
} 
2326 
#if defined(DEBUG)

2327 
for(i=0;i<nb_frames;i++) { 
2328 
for(ch=0;ch<s>nb_channels;ch++) { 
2329 
int j;

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

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

2334 
} 
2335 
} 
2336 
#endif

2337 
/* apply the synthesis filter */

2338 
for(ch=0;ch<s>nb_channels;ch++) { 
2339 
samples_ptr = samples + ch; 
2340 
for(i=0;i<nb_frames;i++) { 
2341 
ff_mpa_synth_filter(s>synth_buf[ch], &(s>synth_buf_offset[ch]), 
2342 
window, &s>dither_state, 
2343 
samples_ptr, s>nb_channels, 
2344 
s>sb_samples[ch][i]); 
2345 
samples_ptr += 32 * s>nb_channels;

2346 
} 
2347 
} 
2348 
#ifdef DEBUG

2349 
s>frame_count++; 
2350 
#endif

2351 
return nb_frames * 32 * sizeof(OUT_INT) * s>nb_channels; 
2352 
} 
2353  
2354 
static int decode_frame(AVCodecContext * avctx, 
2355 
void *data, int *data_size, 
2356 
uint8_t * buf, int buf_size)

2357 
{ 
2358 
MPADecodeContext *s = avctx>priv_data; 
2359 
uint32_t header; 
2360 
int out_size;

2361 
OUT_INT *out_samples = data; 
2362  
2363 
retry:

2364 
if(buf_size < HEADER_SIZE)

2365 
return 1; 
2366  
2367 
header = AV_RB32(buf); 
2368 
if(ff_mpa_check_header(header) < 0){ 
2369 
buf++; 
2370 
// buf_size;

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

2372 
goto retry;

2373 
} 
2374  
2375 
if (ff_mpegaudio_decode_header(s, header) == 1) { 
2376 
/* free format: prepare to compute frame size */

2377 
s>frame_size = 1;

2378 
return 1; 
2379 
} 
2380 
/* update codec info */

2381 
avctx>channels = s>nb_channels; 
2382 
avctx>bit_rate = s>bit_rate; 
2383 
avctx>sub_id = s>layer; 
2384 
switch(s>layer) {

2385 
case 1: 
2386 
avctx>frame_size = 384;

2387 
break;

2388 
case 2: 
2389 
avctx>frame_size = 1152;

2390 
break;

2391 
case 3: 
2392 
if (s>lsf)

2393 
avctx>frame_size = 576;

2394 
else

2395 
avctx>frame_size = 1152;

2396 
break;

2397 
} 
2398  
2399 
if(s>frame_size<=0  s>frame_size > buf_size){ 
2400 
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");

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

2404 
buf_size= s>frame_size; 
2405 
} 
2406  
2407 
out_size = mp_decode_frame(s, out_samples, buf, buf_size); 
2408 
if(out_size>=0){ 
2409 
*data_size = out_size; 
2410 
avctx>sample_rate = s>sample_rate; 
2411 
//FIXME maybe move the other codec info stuff from above here too

2412 
}else

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

2415 
return buf_size;

2416 
} 
2417  
2418 
static void flush(AVCodecContext *avctx){ 
2419 
MPADecodeContext *s = avctx>priv_data; 
2420 
s>last_buf_size= 0;

2421 
} 
2422  
2423 
#ifdef CONFIG_MP3ADU_DECODER

2424 
static int decode_frame_adu(AVCodecContext * avctx, 
2425 
void *data, int *data_size, 
2426 
uint8_t * buf, int buf_size)

2427 
{ 
2428 
MPADecodeContext *s = avctx>priv_data; 
2429 
uint32_t header; 
2430 
int len, out_size;

2431 
OUT_INT *out_samples = data; 
2432  
2433 
len = buf_size; 
2434  
2435 
// Discard too short frames

2436 
if (buf_size < HEADER_SIZE) {

2437 
*data_size = 0;

2438 
return buf_size;

2439 
} 
2440  
2441  
2442 
if (len > MPA_MAX_CODED_FRAME_SIZE)

2443 
len = MPA_MAX_CODED_FRAME_SIZE; 
2444  
2445 
// Get header and restore sync word

2446 
header = AV_RB32(buf)  0xffe00000;

2447  
2448 
if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame 
2449 
*data_size = 0;

2450 
return buf_size;

2451 
} 
2452  
2453 
ff_mpegaudio_decode_header(s, header); 
2454 
/* update codec info */

2455 
avctx>sample_rate = s>sample_rate; 
2456 
avctx>channels = s>nb_channels; 
2457 
avctx>bit_rate = s>bit_rate; 
2458 
avctx>sub_id = s>layer; 
2459  
2460 
avctx>frame_size=s>frame_size = len; 
2461  
2462 
if (avctx>parse_only) {

2463 
out_size = buf_size; 
2464 
} else {

2465 
out_size = mp_decode_frame(s, out_samples, buf, buf_size); 
2466 
} 
2467  
2468 
*data_size = out_size; 
2469 
return buf_size;

2470 
} 
2471 
#endif /* CONFIG_MP3ADU_DECODER */ 
2472  
2473 
#ifdef CONFIG_MP3ON4_DECODER

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

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

2478 
static int chan_offset[9][5] = { 
2479 
{0},

2480 
{0}, // C 
2481 
{0}, // FLR 
2482 
{2,0}, // C FLR 
2483 
{2,0,3}, // C FLR BS 
2484 
{4,0,2}, // C FLR BLRS 
2485 
{4,0,2,5}, // C FLR BLRS LFE 
2486 
{4,0,2,6,5}, // C FLR BLRS BLR LFE 
2487 
{0,2} // FLR BLRS 
2488 
}; 
2489  
2490  
2491 
static int decode_init_mp3on4(AVCodecContext * avctx) 
2492 
{ 
2493 
MP3On4DecodeContext *s = avctx>priv_data; 
2494 
int i;

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

2498 
return 1; 
2499 
} 
2500  
2501 
s>chan_cfg = (((unsigned char *)avctx>extradata)[1] >> 3) & 0x0f; 
2502 
s>frames = mp3Frames[s>chan_cfg]; 
2503 
if(!s>frames) {

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

2505 
return 1; 
2506 
} 
2507 
avctx>channels = mp3Channels[s>chan_cfg]; 
2508  
2509 
/* Init the first mp3 decoder in standard way, so that all tables get builded

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

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

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

2513 
*/

2514 
// Allocate zeroed memory for the first decoder context

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

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

2518 
decode_init(avctx); 
2519 
// Restore mp3on4 context pointer

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

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

2525 
*/

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

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

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

2530 
s>mp3decctx[i]>avctx = avctx; 
2531 
} 
2532  
2533 
return 0; 
2534 
} 
2535  
2536  
2537 
static int decode_close_mp3on4(AVCodecContext * avctx) 
2538 
{ 
2539 
MP3On4DecodeContext *s = avctx>priv_data; 
2540 
int i;

2541  
2542 
for (i = 0; i < s>frames; i++) 
2543 
if (s>mp3decctx[i])

2544 
av_free(s>mp3decctx[i]); 
2545  
2546 
return 0; 
2547 
} 
2548  
2549  
2550 
static int decode_frame_mp3on4(AVCodecContext * avctx, 
2551 
void *data, int *data_size, 
2552 
uint8_t * buf, int buf_size)

2553 
{ 
2554 
MP3On4DecodeContext *s = avctx>priv_data; 
2555 
MPADecodeContext *m; 
2556 
int len, out_size = 0; 
2557 
uint32_t header; 
2558 
OUT_INT *out_samples = data; 
2559 
OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS]; 
2560 
OUT_INT *outptr, *bp; 
2561 
int fsize;

2562 
unsigned char *start2 = buf, *start; 
2563 
int fr, i, j, n;

2564 
int off = avctx>channels;

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

2566  
2567 
len = buf_size; 
2568  
2569 
// Discard too short frames

2570 
if (buf_size < HEADER_SIZE) {

2571 
*data_size = 0;

2572 
return buf_size;

2573 
} 
2574  
2575 
// If only one decoder interleave is not needed

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

2577  
2578 
for (fr = 0; fr < s>frames; fr++) { 
2579 
start = start2; 
2580 
fsize = (start[0] << 4)  (start[1] >> 4); 
2581 
start2 += fsize; 
2582 
if (fsize > len)

2583 
fsize = len; 
2584 
len = fsize; 
2585 
if (fsize > MPA_MAX_CODED_FRAME_SIZE)

2586 
fsize = MPA_MAX_CODED_FRAME_SIZE; 
2587 
m = s>mp3decctx[fr]; 
2588 
assert (m != NULL);

2589  
2590 
// Get header

2591 
header = AV_RB32(start)  0xfff00000;

2592  
2593 
if (ff_mpa_check_header(header) < 0) { // Bad header, discard block 
2594 
*data_size = 0;

2595 
return buf_size;

2596 
} 
2597  
2598 
ff_mpegaudio_decode_header(m, header); 
2599 
mp_decode_frame(m, decoded_buf, start, fsize); 
2600  
2601 
n = MPA_FRAME_SIZE * m>nb_channels; 
2602 
out_size += n * sizeof(OUT_INT);

2603 
if(s>frames > 1) { 
2604 
/* interleave output data */

2605 
bp = out_samples + coff[fr]; 
2606 
if(m>nb_channels == 1) { 
2607 
for(j = 0; j < n; j++) { 
2608 
*bp = decoded_buf[j]; 
2609 
bp += off; 
2610 
} 
2611 
} else {

2612 
for(j = 0; j < n; j++) { 
2613 
bp[0] = decoded_buf[j++];

2614 
bp[1] = decoded_buf[j];

2615 
bp += off; 
2616 
} 
2617 
} 
2618 
} 
2619 
} 
2620  
2621 
/* update codec info */

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

2623 
avctx>frame_size= buf_size; 
2624 
avctx>bit_rate = 0;

2625 
for (i = 0; i < s>frames; i++) 
2626 
avctx>bit_rate += s>mp3decctx[i]>bit_rate; 
2627  
2628 
*data_size = out_size; 
2629 
return buf_size;

2630 
} 
2631 
#endif /* CONFIG_MP3ON4_DECODER */ 
2632  
2633 
#ifdef CONFIG_MP2_DECODER

2634 
AVCodec mp2_decoder = 
2635 
{ 
2636 
"mp2",

2637 
CODEC_TYPE_AUDIO, 
2638 
CODEC_ID_MP2, 
2639 
sizeof(MPADecodeContext),

2640 
decode_init, 
2641 
NULL,

2642 
NULL,

2643 
decode_frame, 
2644 
CODEC_CAP_PARSE_ONLY, 
2645 
.flush= flush, 
2646 
}; 
2647 
#endif

2648 
#ifdef CONFIG_MP3_DECODER

2649 
AVCodec mp3_decoder = 
2650 
{ 
2651 
"mp3",

2652 
CODEC_TYPE_AUDIO, 
2653 
CODEC_ID_MP3, 
2654 
sizeof(MPADecodeContext),

2655 
decode_init, 
2656 
NULL,

2657 
NULL,

2658 
decode_frame, 
2659 
CODEC_CAP_PARSE_ONLY, 
2660 
.flush= flush, 
2661 
}; 
2662 
#endif

2663 
#ifdef CONFIG_MP3ADU_DECODER

2664 
AVCodec mp3adu_decoder = 
2665 
{ 
2666 
"mp3adu",

2667 
CODEC_TYPE_AUDIO, 
2668 
CODEC_ID_MP3ADU, 
2669 
sizeof(MPADecodeContext),

2670 
decode_init, 
2671 
NULL,

2672 
NULL,

2673 
decode_frame_adu, 
2674 
CODEC_CAP_PARSE_ONLY, 
2675 
.flush= flush, 
2676 
}; 
2677 
#endif

2678 
#ifdef CONFIG_MP3ON4_DECODER

2679 
AVCodec mp3on4_decoder = 
2680 
{ 
2681 
"mp3on4",

2682 
CODEC_TYPE_AUDIO, 
2683 
CODEC_ID_MP3ON4, 
2684 
sizeof(MP3On4DecodeContext),

2685 
decode_init_mp3on4, 
2686 
NULL,

2687 
decode_close_mp3on4, 
2688 
decode_frame_mp3on4, 
2689 
.flush= flush, 
2690 
}; 
2691 
#endif
