ffmpeg / libavcodec / mpegaudiodec.c @ 4d49a5a7
History  View  Annotate  Download (78.8 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

24 
* MPEG Audio decoder.

25 
*/

26  
27 
#include "avcodec.h" 
28 
#include "get_bits.h" 
29 
#include "dsputil.h" 
30  
31 
/*

32 
* TODO:

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

34 
*  test lsf / mpeg25 extensively.

35 
*/

36  
37 
#include "mpegaudio.h" 
38 
#include "mpegaudiodecheader.h" 
39  
40 
#include "mathops.h" 
41  
42 
#if CONFIG_FLOAT

43 
# define SHR(a,b) ((a)*(1.0f/(1<<(b)))) 
44 
# define compute_antialias compute_antialias_float

45 
# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5)) 
46 
# define FIXR(x) ((float)(x)) 
47 
# define FIXHR(x) ((float)(x)) 
48 
# define MULH3(x, y, s) ((s)*(y)*(x))

49 
# define MULLx(x, y, s) ((y)*(x))

50 
# define RENAME(a) a ## _float 
51 
#else

52 
# define SHR(a,b) ((a)>>(b))

53 
# define compute_antialias compute_antialias_integer

54 
/* WARNING: only correct for posititive numbers */

55 
# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5)) 
56 
# define FIXR(a) ((int)((a) * FRAC_ONE + 0.5)) 
57 
# define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) 
58 
# define MULH3(x, y, s) MULH((s)*(x), y)

59 
# define MULLx(x, y, s) MULL(x,y,s)

60 
# define RENAME(a) a

61 
#endif

62  
63 
/****************/

64  
65 
#define HEADER_SIZE 4 
66  
67 
#include "mpegaudiodata.h" 
68 
#include "mpegaudiodectab.h" 
69  
70 
static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g); 
71 
static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g); 
72 
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window, 
73 
int *dither_state, OUT_INT *samples, int incr); 
74  
75 
/* vlc structure for decoding layer 3 huffman tables */

76 
static VLC huff_vlc[16]; 
77 
static VLC_TYPE huff_vlc_tables[

78 
0+128+128+128+130+128+154+166+ 
79 
142+204+190+170+542+460+662+414 
80 
][2];

81 
static const int huff_vlc_tables_sizes[16] = { 
82 
0, 128, 128, 128, 130, 128, 154, 166, 
83 
142, 204, 190, 170, 542, 460, 662, 414 
84 
}; 
85 
static VLC huff_quad_vlc[2]; 
86 
static VLC_TYPE huff_quad_vlc_tables[128+16][2]; 
87 
static const int huff_quad_vlc_tables_sizes[2] = { 
88 
128, 16 
89 
}; 
90 
/* computed from band_size_long */

91 
static uint16_t band_index_long[9][23]; 
92 
#include "mpegaudio_tablegen.h" 
93 
/* intensity stereo coef table */

94 
static INTFLOAT is_table[2][16]; 
95 
static INTFLOAT is_table_lsf[2][2][16]; 
96 
static int32_t csa_table[8][4]; 
97 
static float csa_table_float[8][4]; 
98 
static INTFLOAT mdct_win[8][36]; 
99  
100 
/* lower 2 bits: modulo 3, higher bits: shift */

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

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

105  
106 
#define SCALE_GEN(v) \

107 
{ FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) } 
108  
109 
static const int32_t scale_factor_mult2[3][3] = { 
110 
SCALE_GEN(4.0 / 3.0), /* 3 steps */ 
111 
SCALE_GEN(4.0 / 5.0), /* 5 steps */ 
112 
SCALE_GEN(4.0 / 9.0), /* 9 steps */ 
113 
}; 
114  
115 
DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512]; 
116  
117 
/**

118 
* Convert region offsets to region sizes and truncate

119 
* size to big_values.

120 
*/

121 
static void ff_region_offset2size(GranuleDef *g){ 
122 
int i, k, j=0; 
123 
g>region_size[2] = (576 / 2); 
124 
for(i=0;i<3;i++) { 
125 
k = FFMIN(g>region_size[i], g>big_values); 
126 
g>region_size[i] = k  j; 
127 
j = k; 
128 
} 
129 
} 
130  
131 
static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){ 
132 
if (g>block_type == 2) 
133 
g>region_size[0] = (36 / 2); 
134 
else {

135 
if (s>sample_rate_index <= 2) 
136 
g>region_size[0] = (36 / 2); 
137 
else if (s>sample_rate_index != 8) 
138 
g>region_size[0] = (54 / 2); 
139 
else

140 
g>region_size[0] = (108 / 2); 
141 
} 
142 
g>region_size[1] = (576 / 2); 
143 
} 
144  
145 
static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){ 
146 
int l;

147 
g>region_size[0] =

148 
band_index_long[s>sample_rate_index][ra1 + 1] >> 1; 
149 
/* should not overflow */

150 
l = FFMIN(ra1 + ra2 + 2, 22); 
151 
g>region_size[1] =

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

153 
} 
154  
155 
static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){ 
156 
if (g>block_type == 2) { 
157 
if (g>switch_point) {

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

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

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

161 
if (s>sample_rate_index <= 2) 
162 
g>long_end = 8;

163 
else if (s>sample_rate_index != 8) 
164 
g>long_end = 6;

165 
else

166 
g>long_end = 4; /* 8000 Hz */ 
167  
168 
g>short_start = 2 + (s>sample_rate_index != 8); 
169 
} else {

170 
g>long_end = 0;

171 
g>short_start = 0;

172 
} 
173 
} else {

174 
g>short_start = 13;

175 
g>long_end = 22;

176 
} 
177 
} 
178  
179 
/* layer 1 unscaling */

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

181 
static inline int l1_unscale(int n, int mant, int scale_factor) 
182 
{ 
183 
int shift, mod;

184 
int64_t val; 
185  
186 
shift = scale_factor_modshift[scale_factor]; 
187 
mod = shift & 3;

188 
shift >>= 2;

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

192 
return (int)((val + (1LL << (shift  1))) >> shift); 
193 
} 
194  
195 
static inline int l2_unscale_group(int steps, int mant, int scale_factor) 
196 
{ 
197 
int shift, mod, val;

198  
199 
shift = scale_factor_modshift[scale_factor]; 
200 
mod = shift & 3;

201 
shift >>= 2;

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

205 
if (shift > 0) 
206 
val = (val + (1 << (shift  1))) >> shift; 
207 
return val;

208 
} 
209  
210 
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */

211 
static inline int l3_unscale(int value, int exponent) 
212 
{ 
213 
unsigned int m; 
214 
int e;

215  
216 
e = table_4_3_exp [4*value + (exponent&3)]; 
217 
m = table_4_3_value[4*value + (exponent&3)]; 
218 
e = (exponent >> 2);

219 
assert(e>=1);

220 
if (e > 31) 
221 
return 0; 
222 
m = (m + (1 << (e1))) >> e; 
223  
224 
return m;

225 
} 
226  
227 
/* all integer n^(4/3) computation code */

228 
#define DEV_ORDER 13 
229  
230 
#define POW_FRAC_BITS 24 
231 
#define POW_FRAC_ONE (1 << POW_FRAC_BITS) 
232 
#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE)) 
233 
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)

234  
235 
static int dev_4_3_coefs[DEV_ORDER]; 
236  
237 
#if 0 /* unused */

238 
static int pow_mult3[3] = {

239 
POW_FIX(1.0),

240 
POW_FIX(1.25992104989487316476),

241 
POW_FIX(1.58740105196819947474),

242 
};

243 
#endif

244  
245 
static av_cold void int_pow_init(void) 
246 
{ 
247 
int i, a;

248  
249 
a = POW_FIX(1.0); 
250 
for(i=0;i<DEV_ORDER;i++) { 
251 
a = POW_MULL(a, POW_FIX(4.0 / 3.0)  i * POW_FIX(1.0)) / (i + 1); 
252 
dev_4_3_coefs[i] = a; 
253 
} 
254 
} 
255  
256 
#if 0 /* unused, remove? */

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

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

259 
{

260 
int e, er, eq, j;

261 
int a, a1;

262 

263 
/* renormalize */

264 
a = i;

265 
e = POW_FRAC_BITS;

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

267 
a = a << 1;

268 
e;

269 
}

270 
a = (1 << POW_FRAC_BITS);

271 
a1 = 0;

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

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

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

275 
/* exponent compute (exact) */

276 
e = e * 4;

277 
er = e % 3;

278 
eq = e / 3;

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

280 
while (a >= 2 * POW_FRAC_ONE) {

281 
a = a >> 1;

282 
eq++;

283 
}

284 
/* convert to float */

285 
while (a < POW_FRAC_ONE) {

286 
a = a << 1;

287 
eq;

288 
}

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

290 
#if POW_FRAC_BITS > FRAC_BITS

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

292 
/* correct overflow */

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

294 
a = a >> 1;

295 
eq++;

296 
}

297 
#endif

298 
*exp_ptr = eq; 
299 
return a;

300 
} 
301 
#endif

302  
303 
static av_cold int decode_init(AVCodecContext * avctx) 
304 
{ 
305 
MPADecodeContext *s = avctx>priv_data; 
306 
static int init=0; 
307 
int i, j, k;

308  
309 
s>avctx = avctx; 
310 
s>apply_window_mp3 = apply_window_mp3_c; 
311  
312 
avctx>sample_fmt= OUT_FMT; 
313 
s>error_recognition= avctx>error_recognition; 
314  
315 
if (!init && !avctx>parse_only) {

316 
int offset;

317  
318 
/* scale factors table for layer 1/2 */

319 
for(i=0;i<64;i++) { 
320 
int shift, mod;

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

322 
shift = (i / 3);

323 
mod = i % 3;

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

325 
} 
326  
327 
/* scale factor multiply for layer 1 */

328 
for(i=0;i<15;i++) { 
329 
int n, norm;

330 
n = i + 2;

331 
norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n)  1); 
332 
scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS); 
333 
scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS); 
334 
scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS); 
335 
dprintf(avctx, "%d: norm=%x s=%x %x %x\n",

336 
i, norm, 
337 
scale_factor_mult[i][0],

338 
scale_factor_mult[i][1],

339 
scale_factor_mult[i][2]);

340 
} 
341  
342 
RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window)); 
343  
344 
/* huffman decode tables */

345 
offset = 0;

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

348 
int xsize, x, y;

349 
uint8_t tmp_bits [512];

350 
uint16_t tmp_codes[512];

351  
352 
memset(tmp_bits , 0, sizeof(tmp_bits )); 
353 
memset(tmp_codes, 0, sizeof(tmp_codes)); 
354  
355 
xsize = h>xsize; 
356  
357 
j = 0;

358 
for(x=0;x<xsize;x++) { 
359 
for(y=0;y<xsize;y++){ 
360 
tmp_bits [(x << 5)  y  ((x&&y)<<4)]= h>bits [j ]; 
361 
tmp_codes[(x << 5)  y  ((x&&y)<<4)]= h>codes[j++]; 
362 
} 
363 
} 
364  
365 
/* XXX: fail test */

366 
huff_vlc[i].table = huff_vlc_tables+offset; 
367 
huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i]; 
368 
init_vlc(&huff_vlc[i], 7, 512, 
369 
tmp_bits, 1, 1, tmp_codes, 2, 2, 
370 
INIT_VLC_USE_NEW_STATIC); 
371 
offset += huff_vlc_tables_sizes[i]; 
372 
} 
373 
assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables)); 
374  
375 
offset = 0;

376 
for(i=0;i<2;i++) { 
377 
huff_quad_vlc[i].table = huff_quad_vlc_tables+offset; 
378 
huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i]; 
379 
init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, 
380 
mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 
381 
INIT_VLC_USE_NEW_STATIC); 
382 
offset += huff_quad_vlc_tables_sizes[i]; 
383 
} 
384 
assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables)); 
385  
386 
for(i=0;i<9;i++) { 
387 
k = 0;

388 
for(j=0;j<22;j++) { 
389 
band_index_long[i][j] = k; 
390 
k += band_size_long[i][j]; 
391 
} 
392 
band_index_long[i][22] = k;

393 
} 
394  
395 
/* compute n ^ (4/3) and store it in mantissa/exp format */

396  
397 
int_pow_init(); 
398 
mpegaudio_tableinit(); 
399  
400 
for(i=0;i<7;i++) { 
401 
float f;

402 
INTFLOAT v; 
403 
if (i != 6) { 
404 
f = tan((double)i * M_PI / 12.0); 
405 
v = FIXR(f / (1.0 + f)); 
406 
} else {

407 
v = FIXR(1.0); 
408 
} 
409 
is_table[0][i] = v;

410 
is_table[1][6  i] = v; 
411 
} 
412 
/* invalid values */

413 
for(i=7;i<16;i++) 
414 
is_table[0][i] = is_table[1][i] = 0.0; 
415  
416 
for(i=0;i<16;i++) { 
417 
double f;

418 
int e, k;

419  
420 
for(j=0;j<2;j++) { 
421 
e = (j + 1) * ((i + 1) >> 1); 
422 
f = pow(2.0, e / 4.0); 
423 
k = i & 1;

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

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

427 
i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]); 
428 
} 
429 
} 
430  
431 
for(i=0;i<8;i++) { 
432 
float ci, cs, ca;

433 
ci = ci_table[i]; 
434 
cs = 1.0 / sqrt(1.0 + ci * ci); 
435 
ca = cs * ci; 
436 
csa_table[i][0] = FIXHR(cs/4); 
437 
csa_table[i][1] = FIXHR(ca/4); 
438 
csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4); 
439 
csa_table[i][3] = FIXHR(ca/4)  FIXHR(cs/4); 
440 
csa_table_float[i][0] = cs;

441 
csa_table_float[i][1] = ca;

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

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

444 
} 
445  
446 
/* compute mdct windows */

447 
for(i=0;i<36;i++) { 
448 
for(j=0; j<4; j++){ 
449 
double d;

450  
451 
if(j==2 && i%3 != 1) 
452 
continue;

453  
454 
d= sin(M_PI * (i + 0.5) / 36.0); 
455 
if(j==1){ 
456 
if (i>=30) d= 0; 
457 
else if(i>=24) d= sin(M_PI * (i  18 + 0.5) / 12.0); 
458 
else if(i>=18) d= 1; 
459 
}else if(j==3){ 
460 
if (i< 6) d= 0; 
461 
else if(i< 12) d= sin(M_PI * (i  6 + 0.5) / 12.0); 
462 
else if(i< 18) d= 1; 
463 
} 
464 
//merge last stage of imdct into the window coefficients

465 
d*= 0.5 / cos(M_PI*(2*i + 19)/72); 
466  
467 
if(j==2) 
468 
mdct_win[j][i/3] = FIXHR((d / (1<<5))); 
469 
else

470 
mdct_win[j][i ] = FIXHR((d / (1<<5))); 
471 
} 
472 
} 
473  
474 
/* NOTE: we do frequency inversion adter the MDCT by changing

475 
the sign of the right window coefs */

476 
for(j=0;j<4;j++) { 
477 
for(i=0;i<36;i+=2) { 
478 
mdct_win[j + 4][i] = mdct_win[j][i];

479 
mdct_win[j + 4][i + 1] = mdct_win[j][i + 1]; 
480 
} 
481 
} 
482  
483 
init = 1;

484 
} 
485  
486 
if (avctx>codec_id == CODEC_ID_MP3ADU)

487 
s>adu_mode = 1;

488 
return 0; 
489 
} 
490  
491 
/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6  j))) */

492  
493 
/* cos(i*pi/64) */

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

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

533 
{\ 
534 
tmp0 = val##a + val##b;\ 
535 
tmp1 = val##a  val##b;\ 
536 
val##a = tmp0;\ 
537 
val##b = MULH3(tmp1, c, 1<<(s));\ 
538 
} 
539  
540 
#define BF0(a, b, c, s)\

541 
{\ 
542 
tmp0 = tab[a] + tab[b];\ 
543 
tmp1 = tab[a]  tab[b];\ 
544 
val##a = tmp0;\ 
545 
val##b = MULH3(tmp1, c, 1<<(s));\ 
546 
} 
547  
548 
#define BF1(a, b, c, d)\

549 
{\ 
550 
BF(a, b, COS4_0, 1);\

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

552 
val##c += val##d;\ 
553 
} 
554  
555 
#define BF2(a, b, c, d)\

556 
{\ 
557 
BF(a, b, COS4_0, 1);\

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

559 
val##c += val##d;\ 
560 
val##a += val##c;\ 
561 
val##c += val##b;\ 
562 
val##b += val##d;\ 
563 
} 
564  
565 
#define ADD(a, b) val##a += val##b 
566  
567 
/* DCT32 without 1/sqrt(2) coef zero scaling. */

568 
static void dct32(INTFLOAT *out, const INTFLOAT *tab) 
569 
{ 
570 
INTFLOAT tmp0, tmp1; 
571  
572 
INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 , 
573 
val8 , val9 , val10, val11, val12, val13, val14, val15, 
574 
val16, val17, val18, val19, val20, val21, val22, val23, 
575 
val24, val25, val26, val27, val28, val29, val30, val31; 
576  
577 
/* pass 1 */

578 
BF0( 0, 31, COS0_0 , 1); 
579 
BF0(15, 16, COS0_15, 5); 
580 
/* pass 2 */

581 
BF( 0, 15, COS1_0 , 1); 
582 
BF(16, 31,COS1_0 , 1); 
583 
/* pass 1 */

584 
BF0( 7, 24, COS0_7 , 1); 
585 
BF0( 8, 23, COS0_8 , 1); 
586 
/* pass 2 */

587 
BF( 7, 8, COS1_7 , 4); 
588 
BF(23, 24,COS1_7 , 4); 
589 
/* pass 3 */

590 
BF( 0, 7, COS2_0 , 1); 
591 
BF( 8, 15,COS2_0 , 1); 
592 
BF(16, 23, COS2_0 , 1); 
593 
BF(24, 31,COS2_0 , 1); 
594 
/* pass 1 */

595 
BF0( 3, 28, COS0_3 , 1); 
596 
BF0(12, 19, COS0_12, 2); 
597 
/* pass 2 */

598 
BF( 3, 12, COS1_3 , 1); 
599 
BF(19, 28,COS1_3 , 1); 
600 
/* pass 1 */

601 
BF0( 4, 27, COS0_4 , 1); 
602 
BF0(11, 20, COS0_11, 2); 
603 
/* pass 2 */

604 
BF( 4, 11, COS1_4 , 1); 
605 
BF(20, 27,COS1_4 , 1); 
606 
/* pass 3 */

607 
BF( 3, 4, COS2_3 , 3); 
608 
BF(11, 12,COS2_3 , 3); 
609 
BF(19, 20, COS2_3 , 3); 
610 
BF(27, 28,COS2_3 , 3); 
611 
/* pass 4 */

612 
BF( 0, 3, COS3_0 , 1); 
613 
BF( 4, 7,COS3_0 , 1); 
614 
BF( 8, 11, COS3_0 , 1); 
615 
BF(12, 15,COS3_0 , 1); 
616 
BF(16, 19, COS3_0 , 1); 
617 
BF(20, 23,COS3_0 , 1); 
618 
BF(24, 27, COS3_0 , 1); 
619 
BF(28, 31,COS3_0 , 1); 
620  
621  
622  
623 
/* pass 1 */

624 
BF0( 1, 30, COS0_1 , 1); 
625 
BF0(14, 17, COS0_14, 3); 
626 
/* pass 2 */

627 
BF( 1, 14, COS1_1 , 1); 
628 
BF(17, 30,COS1_1 , 1); 
629 
/* pass 1 */

630 
BF0( 6, 25, COS0_6 , 1); 
631 
BF0( 9, 22, COS0_9 , 1); 
632 
/* pass 2 */

633 
BF( 6, 9, COS1_6 , 2); 
634 
BF(22, 25,COS1_6 , 2); 
635 
/* pass 3 */

636 
BF( 1, 6, COS2_1 , 1); 
637 
BF( 9, 14,COS2_1 , 1); 
638 
BF(17, 22, COS2_1 , 1); 
639 
BF(25, 30,COS2_1 , 1); 
640  
641 
/* pass 1 */

642 
BF0( 2, 29, COS0_2 , 1); 
643 
BF0(13, 18, COS0_13, 3); 
644 
/* pass 2 */

645 
BF( 2, 13, COS1_2 , 1); 
646 
BF(18, 29,COS1_2 , 1); 
647 
/* pass 1 */

648 
BF0( 5, 26, COS0_5 , 1); 
649 
BF0(10, 21, COS0_10, 1); 
650 
/* pass 2 */

651 
BF( 5, 10, COS1_5 , 2); 
652 
BF(21, 26,COS1_5 , 2); 
653 
/* pass 3 */

654 
BF( 2, 5, COS2_2 , 1); 
655 
BF(10, 13,COS2_2 , 1); 
656 
BF(18, 21, COS2_2 , 1); 
657 
BF(26, 29,COS2_2 , 1); 
658 
/* pass 4 */

659 
BF( 1, 2, COS3_1 , 2); 
660 
BF( 5, 6,COS3_1 , 2); 
661 
BF( 9, 10, COS3_1 , 2); 
662 
BF(13, 14,COS3_1 , 2); 
663 
BF(17, 18, COS3_1 , 2); 
664 
BF(21, 22,COS3_1 , 2); 
665 
BF(25, 26, COS3_1 , 2); 
666 
BF(29, 30,COS3_1 , 2); 
667  
668 
/* pass 5 */

669 
BF1( 0, 1, 2, 3); 
670 
BF2( 4, 5, 6, 7); 
671 
BF1( 8, 9, 10, 11); 
672 
BF2(12, 13, 14, 15); 
673 
BF1(16, 17, 18, 19); 
674 
BF2(20, 21, 22, 23); 
675 
BF1(24, 25, 26, 27); 
676 
BF2(28, 29, 30, 31); 
677  
678 
/* pass 6 */

679  
680 
ADD( 8, 12); 
681 
ADD(12, 10); 
682 
ADD(10, 14); 
683 
ADD(14, 9); 
684 
ADD( 9, 13); 
685 
ADD(13, 11); 
686 
ADD(11, 15); 
687  
688 
out[ 0] = val0;

689 
out[16] = val1;

690 
out[ 8] = val2;

691 
out[24] = val3;

692 
out[ 4] = val4;

693 
out[20] = val5;

694 
out[12] = val6;

695 
out[28] = val7;

696 
out[ 2] = val8;

697 
out[18] = val9;

698 
out[10] = val10;

699 
out[26] = val11;

700 
out[ 6] = val12;

701 
out[22] = val13;

702 
out[14] = val14;

703 
out[30] = val15;

704  
705 
ADD(24, 28); 
706 
ADD(28, 26); 
707 
ADD(26, 30); 
708 
ADD(30, 25); 
709 
ADD(25, 29); 
710 
ADD(29, 27); 
711 
ADD(27, 31); 
712  
713 
out[ 1] = val16 + val24;

714 
out[17] = val17 + val25;

715 
out[ 9] = val18 + val26;

716 
out[25] = val19 + val27;

717 
out[ 5] = val20 + val28;

718 
out[21] = val21 + val29;

719 
out[13] = val22 + val30;

720 
out[29] = val23 + val31;

721 
out[ 3] = val24 + val20;

722 
out[19] = val25 + val21;

723 
out[11] = val26 + val22;

724 
out[27] = val27 + val23;

725 
out[ 7] = val28 + val18;

726 
out[23] = val29 + val19;

727 
out[15] = val30 + val17;

728 
out[31] = val31;

729 
} 
730  
731 
#if CONFIG_FLOAT

732 
static inline float round_sample(float *sum) 
733 
{ 
734 
float sum1=*sum;

735 
*sum = 0;

736 
return sum1;

737 
} 
738  
739 
/* signed 16x16 > 32 multiply add accumulate */

740 
#define MACS(rt, ra, rb) rt+=(ra)*(rb)

741  
742 
/* signed 16x16 > 32 multiply */

743 
#define MULS(ra, rb) ((ra)*(rb))

744  
745 
#define MLSS(rt, ra, rb) rt=(ra)*(rb)

746  
747 
#elif FRAC_BITS <= 15 
748  
749 
static inline int round_sample(int *sum) 
750 
{ 
751 
int sum1;

752 
sum1 = (*sum) >> OUT_SHIFT; 
753 
*sum &= (1<<OUT_SHIFT)1; 
754 
return av_clip(sum1, OUT_MIN, OUT_MAX);

755 
} 
756  
757 
/* signed 16x16 > 32 multiply add accumulate */

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

759  
760 
/* signed 16x16 > 32 multiply */

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

762  
763 
#define MLSS(rt, ra, rb) MLS16(rt, ra, rb)

764  
765 
#else

766  
767 
static inline int round_sample(int64_t *sum) 
768 
{ 
769 
int sum1;

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

771 
*sum &= (1<<OUT_SHIFT)1; 
772 
return av_clip(sum1, OUT_MIN, OUT_MAX);

773 
} 
774  
775 
# define MULS(ra, rb) MUL64(ra, rb)

776 
# define MACS(rt, ra, rb) MAC64(rt, ra, rb)

777 
# define MLSS(rt, ra, rb) MLS64(rt, ra, rb)

778 
#endif

779  
780 
#define SUM8(op, sum, w, p) \

781 
{ \ 
782 
op(sum, (w)[0 * 64], (p)[0 * 64]); \ 
783 
op(sum, (w)[1 * 64], (p)[1 * 64]); \ 
784 
op(sum, (w)[2 * 64], (p)[2 * 64]); \ 
785 
op(sum, (w)[3 * 64], (p)[3 * 64]); \ 
786 
op(sum, (w)[4 * 64], (p)[4 * 64]); \ 
787 
op(sum, (w)[5 * 64], (p)[5 * 64]); \ 
788 
op(sum, (w)[6 * 64], (p)[6 * 64]); \ 
789 
op(sum, (w)[7 * 64], (p)[7 * 64]); \ 
790 
} 
791  
792 
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \

793 
{ \ 
794 
INTFLOAT tmp;\ 
795 
tmp = p[0 * 64];\ 
796 
op1(sum1, (w1)[0 * 64], tmp);\ 
797 
op2(sum2, (w2)[0 * 64], tmp);\ 
798 
tmp = p[1 * 64];\ 
799 
op1(sum1, (w1)[1 * 64], tmp);\ 
800 
op2(sum2, (w2)[1 * 64], tmp);\ 
801 
tmp = p[2 * 64];\ 
802 
op1(sum1, (w1)[2 * 64], tmp);\ 
803 
op2(sum2, (w2)[2 * 64], tmp);\ 
804 
tmp = p[3 * 64];\ 
805 
op1(sum1, (w1)[3 * 64], tmp);\ 
806 
op2(sum2, (w2)[3 * 64], tmp);\ 
807 
tmp = p[4 * 64];\ 
808 
op1(sum1, (w1)[4 * 64], tmp);\ 
809 
op2(sum2, (w2)[4 * 64], tmp);\ 
810 
tmp = p[5 * 64];\ 
811 
op1(sum1, (w1)[5 * 64], tmp);\ 
812 
op2(sum2, (w2)[5 * 64], tmp);\ 
813 
tmp = p[6 * 64];\ 
814 
op1(sum1, (w1)[6 * 64], tmp);\ 
815 
op2(sum2, (w2)[6 * 64], tmp);\ 
816 
tmp = p[7 * 64];\ 
817 
op1(sum1, (w1)[7 * 64], tmp);\ 
818 
op2(sum2, (w2)[7 * 64], tmp);\ 
819 
} 
820  
821 
void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)

822 
{ 
823 
int i;

824  
825 
/* max = 18760, max sum over all 16 coefs : 44736 */

826 
for(i=0;i<257;i++) { 
827 
INTFLOAT v; 
828 
v = ff_mpa_enwindow[i]; 
829 
#if CONFIG_FLOAT

830 
v *= 1.0 / (1LL<<(16 + FRAC_BITS)); 
831 
#elif WFRAC_BITS < 16 
832 
v = (v + (1 << (16  WFRAC_BITS  1))) >> (16  WFRAC_BITS); 
833 
#endif

834 
window[i] = v; 
835 
if ((i & 63) != 0) 
836 
v = v; 
837 
if (i != 0) 
838 
window[512  i] = v;

839 
} 
840 
} 
841  
842 
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window, 
843 
int *dither_state, OUT_INT *samples, int incr) 
844 
{ 
845 
register const MPA_INT *w, *w2, *p; 
846 
int j;

847 
OUT_INT *samples2; 
848 
#if CONFIG_FLOAT

849 
float sum, sum2;

850 
#elif FRAC_BITS <= 15 
851 
int sum, sum2;

852 
#else

853 
int64_t sum, sum2; 
854 
#endif

855  
856 
/* copy to avoid wrap */

857 
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf)); 
858  
859 
samples2 = samples + 31 * incr;

860 
w = window; 
861 
w2 = window + 31;

862  
863 
sum = *dither_state; 
864 
p = synth_buf + 16;

865 
SUM8(MACS, sum, w, p); 
866 
p = synth_buf + 48;

867 
SUM8(MLSS, sum, w + 32, p);

868 
*samples = round_sample(&sum); 
869 
samples += incr; 
870 
w++; 
871  
872 
/* we calculate two samples at the same time to avoid one memory

873 
access per two sample */

874 
for(j=1;j<16;j++) { 
875 
sum2 = 0;

876 
p = synth_buf + 16 + j;

877 
SUM8P2(sum, MACS, sum2, MLSS, w, w2, p); 
878 
p = synth_buf + 48  j;

879 
SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p); 
880  
881 
*samples = round_sample(&sum); 
882 
samples += incr; 
883 
sum += sum2; 
884 
*samples2 = round_sample(&sum); 
885 
samples2 = incr; 
886 
w++; 
887 
w2; 
888 
} 
889  
890 
p = synth_buf + 32;

891 
SUM8(MLSS, sum, w + 32, p);

892 
*samples = round_sample(&sum); 
893 
*dither_state= sum; 
894 
} 
895  
896  
897 
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:

898 
32 samples. */

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

900 
#if CONFIG_FLOAT

901 
void ff_mpa_synth_filter_float(MPADecodeContext *s, float *synth_buf_ptr, 
902 
int *synth_buf_offset,

903 
float *window, int *dither_state, 
904 
float *samples, int incr, 
905 
float sb_samples[SBLIMIT])

906 
{ 
907 
float *synth_buf;

908 
int offset;

909  
910 
offset = *synth_buf_offset; 
911 
synth_buf = synth_buf_ptr + offset; 
912  
913 
dct32(synth_buf, sb_samples); 
914 
s>apply_window_mp3(synth_buf, window, dither_state, samples, incr); 
915  
916 
offset = (offset  32) & 511; 
917 
*synth_buf_offset = offset; 
918 
} 
919 
#else

920 
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, 
921 
MPA_INT *window, int *dither_state,

922 
OUT_INT *samples, int incr,

923 
INTFLOAT sb_samples[SBLIMIT]) 
924 
{ 
925 
register MPA_INT *synth_buf;

926 
int offset;

927 
#if FRAC_BITS <= 15 
928 
int32_t tmp[32];

929 
#endif

930  
931 
offset = *synth_buf_offset; 
932 
synth_buf = synth_buf_ptr + offset; 
933  
934 
#if FRAC_BITS <= 15 && !CONFIG_FLOAT 
935 
dct32(tmp, sb_samples); 
936 
for(j=0;j<32;j++) { 
937 
/* NOTE: can cause a loss in precision if very high amplitude

938 
sound */

939 
synth_buf[j] = av_clip_int16(tmp[j]); 
940 
} 
941 
#else

942 
dct32(synth_buf, sb_samples); 
943 
#endif

944  
945 
apply_window_mp3_c(synth_buf, window, dither_state, samples, incr); 
946  
947 
offset = (offset  32) & 511; 
948 
*synth_buf_offset = offset; 
949 
} 
950 
#endif

951  
952 
#define C3 FIXHR(0.86602540378443864676/2) 
953  
954 
/* 0.5 / cos(pi*(2*i+1)/36) */

955 
static const INTFLOAT icos36[9] = { 
956 
FIXR(0.50190991877167369479), 
957 
FIXR(0.51763809020504152469), //0 
958 
FIXR(0.55168895948124587824), 
959 
FIXR(0.61038729438072803416), 
960 
FIXR(0.70710678118654752439), //1 
961 
FIXR(0.87172339781054900991), 
962 
FIXR(1.18310079157624925896), 
963 
FIXR(1.93185165257813657349), //2 
964 
FIXR(5.73685662283492756461), 
965 
}; 
966  
967 
/* 0.5 / cos(pi*(2*i+1)/36) */

968 
static const INTFLOAT icos36h[9] = { 
969 
FIXHR(0.50190991877167369479/2), 
970 
FIXHR(0.51763809020504152469/2), //0 
971 
FIXHR(0.55168895948124587824/2), 
972 
FIXHR(0.61038729438072803416/2), 
973 
FIXHR(0.70710678118654752439/2), //1 
974 
FIXHR(0.87172339781054900991/2), 
975 
FIXHR(1.18310079157624925896/4), 
976 
FIXHR(1.93185165257813657349/4), //2 
977 
// FIXHR(5.73685662283492756461),

978 
}; 
979  
980 
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious

981 
cases. */

982 
static void imdct12(INTFLOAT *out, INTFLOAT *in) 
983 
{ 
984 
INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2; 
985  
986 
in0= in[0*3]; 
987 
in1= in[1*3] + in[0*3]; 
988 
in2= in[2*3] + in[1*3]; 
989 
in3= in[3*3] + in[2*3]; 
990 
in4= in[4*3] + in[3*3]; 
991 
in5= in[5*3] + in[4*3]; 
992 
in5 += in3; 
993 
in3 += in1; 
994  
995 
in2= MULH3(in2, C3, 2);

996 
in3= MULH3(in3, C3, 4);

997  
998 
t1 = in0  in4; 
999 
t2 = MULH3(in1  in5, icos36h[4], 2); 
1000  
1001 
out[ 7]=

1002 
out[10]= t1 + t2;

1003 
out[ 1]=

1004 
out[ 4]= t1  t2;

1005  
1006 
in0 += SHR(in4, 1);

1007 
in4 = in0 + in2; 
1008 
in5 += 2*in1;

1009 
in1 = MULH3(in5 + in3, icos36h[1], 1); 
1010 
out[ 8]=

1011 
out[ 9]= in4 + in1;

1012 
out[ 2]=

1013 
out[ 3]= in4  in1;

1014  
1015 
in0 = in2; 
1016 
in5 = MULH3(in5  in3, icos36h[7], 2); 
1017 
out[ 0]=

1018 
out[ 5]= in0  in5;

1019 
out[ 6]=

1020 
out[11]= in0 + in5;

1021 
} 
1022  
1023 
/* cos(pi*i/18) */

1024 
#define C1 FIXHR(0.98480775301220805936/2) 
1025 
#define C2 FIXHR(0.93969262078590838405/2) 
1026 
#define C3 FIXHR(0.86602540378443864676/2) 
1027 
#define C4 FIXHR(0.76604444311897803520/2) 
1028 
#define C5 FIXHR(0.64278760968653932632/2) 
1029 
#define C6 FIXHR(0.5/2) 
1030 
#define C7 FIXHR(0.34202014332566873304/2) 
1031 
#define C8 FIXHR(0.17364817766693034885/2) 
1032  
1033  
1034 
/* using Lee like decomposition followed by hand coded 9 points DCT */

1035 
static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win) 
1036 
{ 
1037 
int i, j;

1038 
INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3; 
1039 
INTFLOAT tmp[18], *tmp1, *in1;

1040  
1041 
for(i=17;i>=1;i) 
1042 
in[i] += in[i1];

1043 
for(i=17;i>=3;i=2) 
1044 
in[i] += in[i2];

1045  
1046 
for(j=0;j<2;j++) { 
1047 
tmp1 = tmp + j; 
1048 
in1 = in + j; 
1049  
1050 
t2 = in1[2*4] + in1[2*8]  in1[2*2]; 
1051  
1052 
t3 = in1[2*0] + SHR(in1[2*6],1); 
1053 
t1 = in1[2*0]  in1[2*6]; 
1054 
tmp1[ 6] = t1  SHR(t2,1); 
1055 
tmp1[16] = t1 + t2;

1056  
1057 
t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2); 
1058 
t1 = MULH3(in1[2*4]  in1[2*8] , 2*C8, 1); 
1059 
t2 = MULH3(in1[2*2] + in1[2*8] , C4, 2); 
1060  
1061 
tmp1[10] = t3  t0  t2;

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

1063 
tmp1[14] = t3 + t2  t1;

1064  
1065 
tmp1[ 4] = MULH3(in1[2*5] + in1[2*7]  in1[2*1], C3, 2); 
1066 
t2 = MULH3(in1[2*1] + in1[2*5], C1, 2); 
1067 
t3 = MULH3(in1[2*5]  in1[2*7], 2*C7, 1); 
1068 
t0 = MULH3(in1[2*3], C3, 2); 
1069  
1070 
t1 = MULH3(in1[2*1] + in1[2*7], C5, 2); 
1071  
1072 
tmp1[ 0] = t2 + t3 + t0;

1073 
tmp1[12] = t2 + t1  t0;

1074 
tmp1[ 8] = t3  t1  t0;

1075 
} 
1076  
1077 
i = 0;

1078 
for(j=0;j<4;j++) { 
1079 
t0 = tmp[i]; 
1080 
t1 = tmp[i + 2];

1081 
s0 = t1 + t0; 
1082 
s2 = t1  t0; 
1083  
1084 
t2 = tmp[i + 1];

1085 
t3 = tmp[i + 3];

1086 
s1 = MULH3(t3 + t2, icos36h[j], 2);

1087 
s3 = MULLx(t3  t2, icos36[8  j], FRAC_BITS);

1088  
1089 
t0 = s0 + s1; 
1090 
t1 = s0  s1; 
1091 
out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j]; 
1092 
out[(8  j)*SBLIMIT] = MULH3(t1, win[8  j], 1) + buf[8  j]; 
1093 
buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1); 
1094 
buf[8  j] = MULH3(t0, win[18 + 8  j], 1); 
1095  
1096 
t0 = s2 + s3; 
1097 
t1 = s2  s3; 
1098 
out[(9 + 8  j)*SBLIMIT] = MULH3(t1, win[9 + 8  j], 1) + buf[9 + 8  j]; 
1099 
out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];

1100 
buf[9 + 8  j] = MULH3(t0, win[18 + 9 + 8  j], 1); 
1101 
buf[ + j] = MULH3(t0, win[18 + j], 1); 
1102 
i += 4;

1103 
} 
1104  
1105 
s0 = tmp[16];

1106 
s1 = MULH3(tmp[17], icos36h[4], 2); 
1107 
t0 = s0 + s1; 
1108 
t1 = s0  s1; 
1109 
out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4]; 
1110 
out[(8  4)*SBLIMIT] = MULH3(t1, win[8  4], 1) + buf[8  4]; 
1111 
buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1); 
1112 
buf[8  4] = MULH3(t0, win[18 + 8  4], 1); 
1113 
} 
1114  
1115 
/* return the number of decoded frames */

1116 
static int mp_decode_layer1(MPADecodeContext *s) 
1117 
{ 
1118 
int bound, i, v, n, ch, j, mant;

1119 
uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT]; 
1120 
uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; 
1121  
1122 
if (s>mode == MPA_JSTEREO)

1123 
bound = (s>mode_ext + 1) * 4; 
1124 
else

1125 
bound = SBLIMIT; 
1126  
1127 
/* allocation bits */

1128 
for(i=0;i<bound;i++) { 
1129 
for(ch=0;ch<s>nb_channels;ch++) { 
1130 
allocation[ch][i] = get_bits(&s>gb, 4);

1131 
} 
1132 
} 
1133 
for(i=bound;i<SBLIMIT;i++) {

1134 
allocation[0][i] = get_bits(&s>gb, 4); 
1135 
} 
1136  
1137 
/* scale factors */

1138 
for(i=0;i<bound;i++) { 
1139 
for(ch=0;ch<s>nb_channels;ch++) { 
1140 
if (allocation[ch][i])

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

1142 
} 
1143 
} 
1144 
for(i=bound;i<SBLIMIT;i++) {

1145 
if (allocation[0][i]) { 
1146 
scale_factors[0][i] = get_bits(&s>gb, 6); 
1147 
scale_factors[1][i] = get_bits(&s>gb, 6); 
1148 
} 
1149 
} 
1150  
1151 
/* compute samples */

1152 
for(j=0;j<12;j++) { 
1153 
for(i=0;i<bound;i++) { 
1154 
for(ch=0;ch<s>nb_channels;ch++) { 
1155 
n = allocation[ch][i]; 
1156 
if (n) {

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

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

1160 
v = 0;

1161 
} 
1162 
s>sb_samples[ch][j][i] = v; 
1163 
} 
1164 
} 
1165 
for(i=bound;i<SBLIMIT;i++) {

1166 
n = allocation[0][i];

1167 
if (n) {

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

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

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

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

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

1173 
} else {

1174 
s>sb_samples[0][j][i] = 0; 
1175 
s>sb_samples[1][j][i] = 0; 
1176 
} 
1177 
} 
1178 
} 
1179 
return 12; 
1180 
} 
1181  
1182 
static int mp_decode_layer2(MPADecodeContext *s) 
1183 
{ 
1184 
int sblimit; /* number of used subbands */ 
1185 
const unsigned char *alloc_table; 
1186 
int table, bit_alloc_bits, i, j, ch, bound, v;

1187 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; 
1188 
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; 
1189 
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; 
1190 
int scale, qindex, bits, steps, k, l, m, b;

1191  
1192 
/* select decoding table */

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

1194 
s>sample_rate, s>lsf); 
1195 
sblimit = ff_mpa_sblimit_table[table]; 
1196 
alloc_table = ff_mpa_alloc_tables[table]; 
1197  
1198 
if (s>mode == MPA_JSTEREO)

1199 
bound = (s>mode_ext + 1) * 4; 
1200 
else

1201 
bound = sblimit; 
1202  
1203 
dprintf(s>avctx, "bound=%d sblimit=%d\n", bound, sblimit);

1204  
1205 
/* sanity check */

1206 
if( bound > sblimit ) bound = sblimit;

1207  
1208 
/* parse bit allocation */

1209 
j = 0;

1210 
for(i=0;i<bound;i++) { 
1211 
bit_alloc_bits = alloc_table[j]; 
1212 
for(ch=0;ch<s>nb_channels;ch++) { 
1213 
bit_alloc[ch][i] = get_bits(&s>gb, bit_alloc_bits); 
1214 
} 
1215 
j += 1 << bit_alloc_bits;

1216 
} 
1217 
for(i=bound;i<sblimit;i++) {

1218 
bit_alloc_bits = alloc_table[j]; 
1219 
v = get_bits(&s>gb, bit_alloc_bits); 
1220 
bit_alloc[0][i] = v;

1221 
bit_alloc[1][i] = v;

1222 
j += 1 << bit_alloc_bits;

1223 
} 
1224  
1225 
/* scale codes */

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

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

1230 
} 
1231 
} 
1232  
1233 
/* scale factors */

1234 
for(i=0;i<sblimit;i++) { 
1235 
for(ch=0;ch<s>nb_channels;ch++) { 
1236 
if (bit_alloc[ch][i]) {

1237 
sf = scale_factors[ch][i]; 
1238 
switch(scale_code[ch][i]) {

1239 
default:

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

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

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

1255 
case 3: 
1256 
sf[0] = get_bits(&s>gb, 6); 
1257 
sf[2] = get_bits(&s>gb, 6); 
1258 
sf[1] = sf[2]; 
1259 
break;

1260 
} 
1261 
} 
1262 
} 
1263 
} 
1264  
1265 
/* samples */

1266 
for(k=0;k<3;k++) { 
1267 
for(l=0;l<12;l+=3) { 
1268 
j = 0;

1269 
for(i=0;i<bound;i++) { 
1270 
bit_alloc_bits = alloc_table[j]; 
1271 
for(ch=0;ch<s>nb_channels;ch++) { 
1272 
b = bit_alloc[ch][i]; 
1273 
if (b) {

1274 
scale = scale_factors[ch][i][k]; 
1275 
qindex = alloc_table[j+b]; 
1276 
bits = ff_mpa_quant_bits[qindex]; 
1277 
if (bits < 0) { 
1278 
/* 3 values at the same time */

1279 
v = get_bits(&s>gb, bits); 
1280 
steps = ff_mpa_quant_steps[qindex]; 
1281 
s>sb_samples[ch][k * 12 + l + 0][i] = 
1282 
l2_unscale_group(steps, v % steps, scale); 
1283 
v = v / steps; 
1284 
s>sb_samples[ch][k * 12 + l + 1][i] = 
1285 
l2_unscale_group(steps, v % steps, scale); 
1286 
v = v / steps; 
1287 
s>sb_samples[ch][k * 12 + l + 2][i] = 
1288 
l2_unscale_group(steps, v, scale); 
1289 
} else {

1290 
for(m=0;m<3;m++) { 
1291 
v = get_bits(&s>gb, bits); 
1292 
v = l1_unscale(bits  1, v, scale);

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

1294 
} 
1295 
} 
1296 
} else {

1297 
s>sb_samples[ch][k * 12 + l + 0][i] = 0; 
1298 
s>sb_samples[ch][k * 12 + l + 1][i] = 0; 
1299 
s>sb_samples[ch][k * 12 + l + 2][i] = 0; 
1300 
} 
1301 
} 
1302 
/* next subband in alloc table */

1303 
j += 1 << bit_alloc_bits;

1304 
} 
1305 
/* XXX: find a way to avoid this duplication of code */

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

1307 
bit_alloc_bits = alloc_table[j]; 
1308 
b = bit_alloc[0][i];

1309 
if (b) {

1310 
int mant, scale0, scale1;

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

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

1313 
qindex = alloc_table[j+b]; 
1314 
bits = ff_mpa_quant_bits[qindex]; 
1315 
if (bits < 0) { 
1316 
/* 3 values at the same time */

1317 
v = get_bits(&s>gb, bits); 
1318 
steps = ff_mpa_quant_steps[qindex]; 
1319 
mant = v % steps; 
1320 
v = v / steps; 
1321 
s>sb_samples[0][k * 12 + l + 0][i] = 
1322 
l2_unscale_group(steps, mant, scale0); 
1323 
s>sb_samples[1][k * 12 + l + 0][i] = 
1324 
l2_unscale_group(steps, mant, scale1); 
1325 
mant = v % steps; 
1326 
v = v / steps; 
1327 
s>sb_samples[0][k * 12 + l + 1][i] = 
1328 
l2_unscale_group(steps, mant, scale0); 
1329 
s>sb_samples[1][k * 12 + l + 1][i] = 
1330 
l2_unscale_group(steps, mant, scale1); 
1331 
s>sb_samples[0][k * 12 + l + 2][i] = 
1332 
l2_unscale_group(steps, v, scale0); 
1333 
s>sb_samples[1][k * 12 + l + 2][i] = 
1334 
l2_unscale_group(steps, v, scale1); 
1335 
} else {

1336 
for(m=0;m<3;m++) { 
1337 
mant = get_bits(&s>gb, bits); 
1338 
s>sb_samples[0][k * 12 + l + m][i] = 
1339 
l1_unscale(bits  1, mant, scale0);

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

1342 
} 
1343 
} 
1344 
} else {

1345 
s>sb_samples[0][k * 12 + l + 0][i] = 0; 
1346 
s>sb_samples[0][k * 12 + l + 1][i] = 0; 
1347 
s>sb_samples[0][k * 12 + l + 2][i] = 0; 
1348 
s>sb_samples[1][k * 12 + l + 0][i] = 0; 
1349 
s>sb_samples[1][k * 12 + l + 1][i] = 0; 
1350 
s>sb_samples[1][k * 12 + l + 2][i] = 0; 
1351 
} 
1352 
/* next subband in alloc table */

1353 
j += 1 << bit_alloc_bits;

1354 
} 
1355 
/* fill remaining samples to zero */

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

1357 
for(ch=0;ch<s>nb_channels;ch++) { 
1358 
s>sb_samples[ch][k * 12 + l + 0][i] = 0; 
1359 
s>sb_samples[ch][k * 12 + l + 1][i] = 0; 
1360 
s>sb_samples[ch][k * 12 + l + 2][i] = 0; 
1361 
} 
1362 
} 
1363 
} 
1364 
} 
1365 
return 3 * 12; 
1366 
} 
1367  
1368 
#define SPLIT(dst,sf,n)\

1369 
if(n==3){\ 
1370 
int m= (sf*171)>>9;\ 
1371 
dst= sf  3*m;\

1372 
sf=m;\ 
1373 
}else if(n==4){\ 
1374 
dst= sf&3;\

1375 
sf>>=2;\

1376 
}else if(n==5){\ 
1377 
int m= (sf*205)>>10;\ 
1378 
dst= sf  5*m;\

1379 
sf=m;\ 
1380 
}else if(n==6){\ 
1381 
int m= (sf*171)>>10;\ 
1382 
dst= sf  6*m;\

1383 
sf=m;\ 
1384 
}else{\

1385 
dst=0;\

1386 
} 
1387  
1388 
static av_always_inline void lsf_sf_expand(int *slen, 
1389 
int sf, int n1, int n2, int n3) 
1390 
{ 
1391 
SPLIT(slen[3], sf, n3)

1392 
SPLIT(slen[2], sf, n2)

1393 
SPLIT(slen[1], sf, n1)

1394 
slen[0] = sf;

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

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

1407 
shift = g>scalefac_scale + 1;

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

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

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

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

1441 
return get_bits(s, n);

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

1447 
s>gb= s>in_gb; 
1448 
s>in_gb.buffer=NULL;

1449 
assert((get_bits_count(&s>gb) & 7) == 0); 
1450 
skip_bits_long(&s>gb, *pos  *end_pos); 
1451 
*end_pos2= 
1452 
*end_pos= *end_pos2 + get_bits_count(&s>gb)  *pos; 
1453 
*pos= get_bits_count(&s>gb); 
1454 
} 
1455 
} 
1456  
1457 
/* Following is a optimized code for

1458 
INTFLOAT v = *src

1459 
if(get_bits1(&s>gb))

1460 
v = v;

1461 
*dst = v;

1462 
*/

1463 
#if CONFIG_FLOAT

1464 
#define READ_FLIP_SIGN(dst,src)\

1465 
v = AV_RN32A(src) ^ (get_bits1(&s>gb)<<31);\

1466 
AV_WN32A(dst, v); 
1467 
#else

1468 
#define READ_FLIP_SIGN(dst,src)\

1469 
v= get_bits1(&s>gb);\ 
1470 
*(dst) = (*(src) ^ v)  v; 
1471 
#endif

1472  
1473 
static int huffman_decode(MPADecodeContext *s, GranuleDef *g, 
1474 
int16_t *exponents, int end_pos2)

1475 
{ 
1476 
int s_index;

1477 
int i;

1478 
int last_pos, bits_left;

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

1481  
1482 
/* low frequencies (called big values) */

1483 
s_index = 0;

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

1486 
j = g>region_size[i]; 
1487 
if (j == 0) 
1488 
continue;

1489 
/* select vlc table */

1490 
k = g>table_select[i]; 
1491 
l = mpa_huff_data[k][0];

1492 
linbits = mpa_huff_data[k][1];

1493 
vlc = &huff_vlc[l]; 
1494  
1495 
if(!l){

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

1498 
continue;

1499 
} 
1500  
1501 
/* read huffcode and compute each couple */

1502 
for(;j>0;j) { 
1503 
int exponent, x, y;

1504 
int v;

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

1506  
1507 
if (pos >= end_pos){

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

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

1511 
if(pos >= end_pos)

1512 
break;

1513 
} 
1514 
y = get_vlc2(&s>gb, vlc>table, 7, 3); 
1515  
1516 
if(!y){

1517 
g>sb_hybrid[s_index ] = 
1518 
g>sb_hybrid[s_index+1] = 0; 
1519 
s_index += 2;

1520 
continue;

1521 
} 
1522  
1523 
exponent= exponents[s_index]; 
1524  
1525 
dprintf(s>avctx, "region=%d n=%d x=%d y=%d exp=%d\n",

1526 
i, g>region_size[i]  j, x, y, exponent); 
1527 
if(y&16){ 
1528 
x = y >> 5;

1529 
y = y & 0x0f;

1530 
if (x < 15){ 
1531 
READ_FLIP_SIGN(g>sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x) 
1532 
}else{

1533 
x += get_bitsz(&s>gb, linbits); 
1534 
v = l3_unscale(x, exponent); 
1535 
if (get_bits1(&s>gb))

1536 
v = v; 
1537 
g>sb_hybrid[s_index] = v; 
1538 
} 
1539 
if (y < 15){ 
1540 
READ_FLIP_SIGN(g>sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)

1541 
}else{

1542 
y += get_bitsz(&s>gb, linbits); 
1543 
v = l3_unscale(y, exponent); 
1544 
if (get_bits1(&s>gb))

1545 
v = v; 
1546 
g>sb_hybrid[s_index+1] = v;

1547 
} 
1548 
}else{

1549 
x = y >> 5;

1550 
y = y & 0x0f;

1551 
x += y; 
1552 
if (x < 15){ 
1553 
READ_FLIP_SIGN(g>sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x) 
1554 
}else{

1555 
x += get_bitsz(&s>gb, linbits); 
1556 
v = l3_unscale(x, exponent); 
1557 
if (get_bits1(&s>gb))

1558 
v = v; 
1559 
g>sb_hybrid[s_index+!!y] = v; 
1560 
} 
1561 
g>sb_hybrid[s_index+ !y] = 0;

1562 
} 
1563 
s_index+=2;

1564 
} 
1565 
} 
1566  
1567 
/* high frequencies */

1568 
vlc = &huff_quad_vlc[g>count1table_select]; 
1569 
last_pos=0;

1570 
while (s_index <= 572) { 
1571 
int pos, code;

1572 
pos = get_bits_count(&s>gb); 
1573 
if (pos >= end_pos) {

1574 
if (pos > end_pos2 && last_pos){

1575 
/* some encoders generate an incorrect size for this

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

1577 
s_index = 4;

1578 
skip_bits_long(&s>gb, last_pos  pos); 
1579 
av_log(s>avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos  pos, end_pospos, end_pos2pos);

1580 
if(s>error_recognition >= FF_ER_COMPLIANT)

1581 
s_index=0;

1582 
break;

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

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

1587 
if(pos >= end_pos)

1588 
break;

1589 
} 
1590 
last_pos= pos; 
1591  
1592 
code = get_vlc2(&s>gb, vlc>table, vlc>bits, 1);

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

1594 
g>sb_hybrid[s_index+0]=

1595 
g>sb_hybrid[s_index+1]=

1596 
g>sb_hybrid[s_index+2]=

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

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

1601 
int pos= s_index+idxtab[code];

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

1603 
READ_FLIP_SIGN(g>sb_hybrid+pos, RENAME(exp_table)+exponents[pos]) 
1604 
} 
1605 
s_index+=4;

1606 
} 
1607 
/* skip extension bits */

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

1610 
if (bits_left < 0 && s>error_recognition >= FF_ER_COMPLIANT) { 
1611 
av_log(s>avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);

1612 
s_index=0;

1613 
}else if(bits_left > 0 && s>error_recognition >= FF_ER_AGGRESSIVE){ 
1614 
av_log(s>avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);

1615 
s_index=0;

1616 
} 
1617 
memset(&g>sb_hybrid[s_index], 0, sizeof(*g>sb_hybrid)*(576  s_index)); 
1618 
skip_bits_long(&s>gb, bits_left); 
1619  
1620 
i= get_bits_count(&s>gb); 
1621 
switch_buffer(s, &i, &end_pos, &end_pos2); 
1622  
1623 
return 0; 
1624 
} 
1625  
1626 
/* Reorder short blocks from bitstream order to interleaved order. It

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

1628 
complicated */

1629 
static void reorder_block(MPADecodeContext *s, GranuleDef *g) 
1630 
{ 
1631 
int i, j, len;

1632 
INTFLOAT *ptr, *dst, *ptr1; 
1633 
INTFLOAT tmp[576];

1634  
1635 
if (g>block_type != 2) 
1636 
return;

1637  
1638 
if (g>switch_point) {

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

1641 
} else {

1642 
ptr = g>sb_hybrid + 48;

1643 
} 
1644 
} else {

1645 
ptr = g>sb_hybrid; 
1646 
} 
1647  
1648 
for(i=g>short_start;i<13;i++) { 
1649 
len = band_size_short[s>sample_rate_index][i]; 
1650 
ptr1 = ptr; 
1651 
dst = tmp; 
1652 
for(j=len;j>0;j) { 
1653 
*dst++ = ptr[0*len];

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

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

1656 
ptr++; 
1657 
} 
1658 
ptr+=2*len;

1659 
memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1)); 
1660 
} 
1661 
} 
1662  
1663 
#define ISQRT2 FIXR(0.70710678118654752440) 
1664  
1665 
static void compute_stereo(MPADecodeContext *s, 
1666 
GranuleDef *g0, GranuleDef *g1) 
1667 
{ 
1668 
int i, j, k, l;

1669 
int sf_max, sf, len, non_zero_found;

1670 
INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;

1671 
int non_zero_found_short[3]; 
1672  
1673 
/* intensity stereo */

1674 
if (s>mode_ext & MODE_EXT_I_STEREO) {

1675 
if (!s>lsf) {

1676 
is_tab = is_table; 
1677 
sf_max = 7;

1678 
} else {

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

1680 
sf_max = 16;

1681 
} 
1682  
1683 
tab0 = g0>sb_hybrid + 576;

1684 
tab1 = g1>sb_hybrid + 576;

1685  
1686 
non_zero_found_short[0] = 0; 
1687 
non_zero_found_short[1] = 0; 
1688 
non_zero_found_short[2] = 0; 
1689 
k = (13  g1>short_start) * 3 + g1>long_end  3; 
1690 
for(i = 12;i >= g1>short_start;i) { 
1691 
/* for last band, use previous scale factor */

1692 
if (i != 11) 
1693 
k = 3;

1694 
len = band_size_short[s>sample_rate_index][i]; 
1695 
for(l=2;l>=0;l) { 
1696 
tab0 = len; 
1697 
tab1 = len; 
1698 
if (!non_zero_found_short[l]) {

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

1700 
for(j=0;j<len;j++) { 
1701 
if (tab1[j] != 0) { 
1702 
non_zero_found_short[l] = 1;

1703 
goto found1;

1704 
} 
1705 
} 
1706 
sf = g1>scale_factors[k + l]; 
1707 
if (sf >= sf_max)

1708 
goto found1;

1709  
1710 
v1 = is_tab[0][sf];

1711 
v2 = is_tab[1][sf];

1712 
for(j=0;j<len;j++) { 
1713 
tmp0 = tab0[j]; 
1714 
tab0[j] = MULLx(tmp0, v1, FRAC_BITS); 
1715 
tab1[j] = MULLx(tmp0, v2, FRAC_BITS); 
1716 
} 
1717 
} else {

1718 
found1:

1719 
if (s>mode_ext & MODE_EXT_MS_STEREO) {

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

1721 
if enabled */

1722 
for(j=0;j<len;j++) { 
1723 
tmp0 = tab0[j]; 
1724 
tmp1 = tab1[j]; 
1725 
tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS); 
1726 
tab1[j] = MULLx(tmp0  tmp1, ISQRT2, FRAC_BITS); 
1727 
} 
1728 
} 
1729 
} 
1730 
} 
1731 
} 
1732  
1733 
non_zero_found = non_zero_found_short[0] 

1734 
non_zero_found_short[1] 

1735 
non_zero_found_short[2];

1736  
1737 
for(i = g1>long_end  1;i >= 0;i) { 
1738 
len = band_size_long[s>sample_rate_index][i]; 
1739 
tab0 = len; 
1740 
tab1 = len; 
1741 
/* test if non zero band. if so, stop doing istereo */

1742 
if (!non_zero_found) {

1743 
for(j=0;j<len;j++) { 
1744 
if (tab1[j] != 0) { 
1745 
non_zero_found = 1;

1746 
goto found2;

1747 
} 
1748 
} 
1749 
/* for last band, use previous scale factor */

1750 
k = (i == 21) ? 20 : i; 
1751 
sf = g1>scale_factors[k]; 
1752 
if (sf >= sf_max)

1753 
goto found2;

1754 
v1 = is_tab[0][sf];

1755 
v2 = is_tab[1][sf];

1756 
for(j=0;j<len;j++) { 
1757 
tmp0 = tab0[j]; 
1758 
tab0[j] = MULLx(tmp0, v1, FRAC_BITS); 
1759 
tab1[j] = MULLx(tmp0, v2, FRAC_BITS); 
1760 
} 
1761 
} else {

1762 
found2:

1763 
if (s>mode_ext & MODE_EXT_MS_STEREO) {

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

1765 
if enabled */

1766 
for(j=0;j<len;j++) { 
1767 
tmp0 = tab0[j]; 
1768 
tmp1 = tab1[j]; 
1769 
tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS); 
1770 
tab1[j] = MULLx(tmp0  tmp1, ISQRT2, FRAC_BITS); 
1771 
} 
1772 
} 
1773 
} 
1774 
} 
1775 
} else if (s>mode_ext & MODE_EXT_MS_STEREO) { 
1776 
/* ms stereo ONLY */

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

1778 
global gain */

1779 
tab0 = g0>sb_hybrid; 
1780 
tab1 = g1>sb_hybrid; 
1781 
for(i=0;i<576;i++) { 
1782 
tmp0 = tab0[i]; 
1783 
tmp1 = tab1[i]; 
1784 
tab0[i] = tmp0 + tmp1; 
1785 
tab1[i] = tmp0  tmp1; 
1786 
} 
1787 
} 
1788 
} 
1789  
1790 
static void compute_antialias_integer(MPADecodeContext *s, 
1791 
GranuleDef *g) 
1792 
{ 
1793 
int32_t *ptr, *csa; 
1794 
int n, i;

1795  
1796 
/* we antialias only "long" bands */

1797 
if (g>block_type == 2) { 
1798 
if (!g>switch_point)

1799 
return;

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

1801 
n = 1;

1802 
} else {

1803 
n = SBLIMIT  1;

1804 
} 
1805  
1806 
ptr = g>sb_hybrid + 18;

1807 
for(i = n;i > 0;i) { 
1808 
int tmp0, tmp1, tmp2;

1809 
csa = &csa_table[0][0]; 
1810 
#define INT_AA(j) \

1811 
tmp0 = ptr[1j];\

1812 
tmp1 = ptr[ j];\ 
1813 
tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\ 
1814 
ptr[1j] = 4*(tmp2  MULH(tmp1, csa[2+4*j]));\ 
1815 
ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j])); 
1816  
1817 
INT_AA(0)

1818 
INT_AA(1)

1819 
INT_AA(2)

1820 
INT_AA(3)

1821 
INT_AA(4)

1822 
INT_AA(5)

1823 
INT_AA(6)

1824 
INT_AA(7)

1825  
1826 
ptr += 18;

1827 
} 
1828 
} 
1829  
1830 
static void compute_antialias_float(MPADecodeContext *s, 
1831 
GranuleDef *g) 
1832 
{ 
1833 
float *ptr;

1834 
int n, i;

1835  
1836 
/* we antialias only "long" bands */

1837 
if (g>block_type == 2) { 
1838 
if (!g>switch_point)

1839 
return;

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

1841 
n = 1;

1842 
} else {

1843 
n = SBLIMIT  1;

1844 
} 
1845  
1846 
ptr = g>sb_hybrid + 18;

1847 
for(i = n;i > 0;i) { 
1848 
float tmp0, tmp1;

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

1851 
tmp0= ptr[1j];\

1852 
tmp1= ptr[ j];\ 
1853 
ptr[1j] = tmp0 * csa[0+4*j]  tmp1 * csa[1+4*j];\ 
1854 
ptr[ j] = tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]; 
1855  
1856 
FLOAT_AA(0)

1857 
FLOAT_AA(1)

1858 
FLOAT_AA(2)

1859 
FLOAT_AA(3)

1860 
FLOAT_AA(4)

1861 
FLOAT_AA(5)

1862 
FLOAT_AA(6)

1863 
FLOAT_AA(7)

1864  
1865 
ptr += 18;

1866 
} 
1867 
} 
1868  
1869 
static void compute_imdct(MPADecodeContext *s, 
1870 
GranuleDef *g, 
1871 
INTFLOAT *sb_samples, 
1872 
INTFLOAT *mdct_buf) 
1873 
{ 
1874 
INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1; 
1875 
INTFLOAT out2[12];

1876 
int i, j, mdct_long_end, sblimit;

1877  
1878 
/* find last non zero block */

1879 
ptr = g>sb_hybrid + 576;

1880 
ptr1 = g>sb_hybrid + 2 * 18; 
1881 
while (ptr >= ptr1) {

1882 
int32_t *p; 
1883 
ptr = 6;

1884 
p= (int32_t*)ptr; 
1885 
if(p[0]  p[1]  p[2]  p[3]  p[4]  p[5]) 
1886 
break;

1887 
} 
1888 
sblimit = ((ptr  g>sb_hybrid) / 18) + 1; 
1889  
1890 
if (g>block_type == 2) { 
1891 
/* XXX: check for 8000 Hz */

1892 
if (g>switch_point)

1893 
mdct_long_end = 2;

1894 
else

1895 
mdct_long_end = 0;

1896 
} else {

1897 
mdct_long_end = sblimit; 
1898 
} 
1899  
1900 
buf = mdct_buf; 
1901 
ptr = g>sb_hybrid; 
1902 
for(j=0;j<mdct_long_end;j++) { 
1903 
/* apply window & overlap with previous buffer */

1904 
out_ptr = sb_samples + j; 
1905 
/* select window */

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

1908 
else

1909 
win1 = mdct_win[g>block_type]; 
1910 
/* select frequency inversion */

1911 
win = win1 + ((4 * 36) & (j & 1)); 
1912 
imdct36(out_ptr, buf, ptr, win); 
1913 
out_ptr += 18*SBLIMIT;

1914 
ptr += 18;

1915 
buf += 18;

1916 
} 
1917 
for(j=mdct_long_end;j<sblimit;j++) {

1918 
/* select frequency inversion */

1919 
win = mdct_win[2] + ((4 * 36) & (j & 1)); 
1920 
out_ptr = sb_samples + j; 
1921  
1922 
for(i=0; i<6; i++){ 
1923 
*out_ptr = buf[i]; 
1924 
out_ptr += SBLIMIT; 
1925 
} 
1926 
imdct12(out2, ptr + 0);

1927 
for(i=0;i<6;i++) { 
1928 
*out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1]; 
1929 
buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1); 
1930 
out_ptr += SBLIMIT; 
1931 
} 
1932 
imdct12(out2, ptr + 1);

1933 
for(i=0;i<6;i++) { 
1934 
*out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2]; 
1935 
buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1); 
1936 
out_ptr += SBLIMIT; 
1937 
} 
1938 
imdct12(out2, ptr + 2);

1939 
for(i=0;i<6;i++) { 
1940 
buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0]; 
1941 
buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1); 
1942 
buf[i + 6*2] = 0; 
1943 
} 
1944 
ptr += 18;

1945 
buf += 18;

1946 
} 
1947 
/* zero bands */

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

1949 
/* overlap */

1950 
out_ptr = sb_samples + j; 
1951 
for(i=0;i<18;i++) { 
1952 
*out_ptr = buf[i]; 
1953 
buf[i] = 0;

1954 
out_ptr += SBLIMIT; 
1955 
} 
1956 
buf += 18;

1957 
} 
1958 
} 
1959  
1960 
/* main layer3 decoding function */

1961 
static int mp_decode_layer3(MPADecodeContext *s) 
1962 
{ 
1963 
int nb_granules, main_data_begin, private_bits;

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

1965 
GranuleDef *g; 
1966 
int16_t exponents[576]; //FIXME try INTFLOAT 
1967  
1968 
/* read side info */

1969 
if (s>lsf) {

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

1971 
private_bits = get_bits(&s>gb, s>nb_channels); 
1972 
nb_granules = 1;

1973 
} else {

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

1975 
if (s>nb_channels == 2) 
1976 
private_bits = get_bits(&s>gb, 3);

1977 
else

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

1979 
nb_granules = 2;

1980 
for(ch=0;ch<s>nb_channels;ch++) { 
1981 
s>granules[ch][0].scfsi = 0;/* all scale factors are transmitted */ 
1982 
s>granules[ch][1].scfsi = get_bits(&s>gb, 4); 
1983 
} 
1984 
} 
1985  
1986 
for(gr=0;gr<nb_granules;gr++) { 
1987 
for(ch=0;ch<s>nb_channels;ch++) { 
1988 
dprintf(s>avctx, "gr=%d ch=%d: side_info\n", gr, ch);

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

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

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

1994 
return 1; 
1995 
} 
1996  
1997 
g>global_gain = get_bits(&s>gb, 8);

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

1999 
1/sqrt(2) renormalization factor */

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

2001 
MODE_EXT_MS_STEREO) 
2002 
g>global_gain = 2;

2003 
if (s>lsf)

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

2005 
else

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

2007 
blocksplit_flag = get_bits1(&s>gb); 
2008 
if (blocksplit_flag) {

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

2010 
if (g>block_type == 0){ 
2011 
av_log(s>avctx, AV_LOG_ERROR, "invalid block type\n");

2012 
return 1; 
2013 
} 
2014 
g>switch_point = get_bits1(&s>gb); 
2015 
for(i=0;i<2;i++) 
2016 
g>table_select[i] = get_bits(&s>gb, 5);

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

2019 
ff_init_short_region(s, g); 
2020 
} else {

2021 
int region_address1, region_address2;

2022 
g>block_type = 0;

2023 
g>switch_point = 0;

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

2026 
/* compute huffman coded region sizes */

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

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

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

2030 
region_address1, region_address2); 
2031 
ff_init_long_region(s, g, region_address1, region_address2); 
2032 
} 
2033 
ff_region_offset2size(g); 
2034 
ff_compute_band_indexes(s, g); 
2035  
2036 
g>preflag = 0;

2037 
if (!s>lsf)

2038 
g>preflag = get_bits1(&s>gb); 
2039 
g>scalefac_scale = get_bits1(&s>gb); 
2040 
g>count1table_select = get_bits1(&s>gb); 
2041 
dprintf(s>avctx, "block_type=%d switch_point=%d\n",

2042 
g>block_type, g>switch_point); 
2043 
} 
2044 
} 
2045  
2046 
if (!s>adu_mode) {

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

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

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

2052  
2053 
memcpy(s>last_buf + s>last_buf_size, ptr, EXTRABYTES); 
2054 
s>in_gb= s>gb; 
2055 
init_get_bits(&s>gb, s>last_buf, s>last_buf_size*8);

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

2057 
} 
2058  
2059 
for(gr=0;gr<nb_granules;gr++) { 
2060 
for(ch=0;ch<s>nb_channels;ch++) { 
2061 
g = &s>granules[ch][gr]; 
2062 
if(get_bits_count(&s>gb)<0){ 
2063 
av_log(s>avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",

2064 
main_data_begin, s>last_buf_size, gr); 
2065 
skip_bits_long(&s>gb, g>part2_3_length); 
2066 
memset(g>sb_hybrid, 0, sizeof(g>sb_hybrid)); 
2067 
if(get_bits_count(&s>gb) >= s>gb.size_in_bits && s>in_gb.buffer){

2068 
skip_bits_long(&s>in_gb, get_bits_count(&s>gb)  s>gb.size_in_bits); 
2069 
s>gb= s>in_gb; 
2070 
s>in_gb.buffer=NULL;

2071 
} 
2072 
continue;

2073 
} 
2074  
2075 
bits_pos = get_bits_count(&s>gb); 
2076  
2077 
if (!s>lsf) {

2078 
uint8_t *sc; 
2079 
int slen, slen1, slen2;

2080  
2081 
/* MPEG1 scale factors */

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

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

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

2085 
if (g>block_type == 2) { 
2086 
n = g>switch_point ? 17 : 18; 
2087 
j = 0;

2088 
if(slen1){

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

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

2094 
} 
2095 
if(slen2){

2096 
for(i=0;i<18;i++) 
2097 
g>scale_factors[j++] = get_bits(&s>gb, slen2); 
2098 
for(i=0;i<3;i++) 
2099 
g>scale_factors[j++] = 0;

2100 
}else{

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

2103 
} 
2104 
} else {

2105 
sc = s>granules[ch][0].scale_factors;

2106 
j = 0;

2107 
for(k=0;k<4;k++) { 
2108 
n = (k == 0 ? 6 : 5); 
2109 
if ((g>scfsi & (0x8 >> k)) == 0) { 
2110 
slen = (k < 2) ? slen1 : slen2;

2111 
if(slen){

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

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

2117 
} 
2118 
} else {

2119 
/* simply copy from last granule */

2120 
for(i=0;i<n;i++) { 
2121 
g>scale_factors[j] = sc[j]; 
2122 
j++; 
2123 
} 
2124 
} 
2125 
} 
2126 
g>scale_factors[j++] = 0;

2127 
} 
2128 
} else {

2129 
int tindex, tindex2, slen[4], sl, sf; 
2130  
2131 
/* LSF scale factors */

2132 
if (g>block_type == 2) { 
2133 
tindex = g>switch_point ? 2 : 1; 
2134 
} else {

2135 
tindex = 0;

2136 
} 
2137 
sf = g>scalefac_compress; 
2138 
if ((s>mode_ext & MODE_EXT_I_STEREO) && ch == 1) { 
2139 
/* intensity stereo case */

2140 
sf >>= 1;

2141 
if (sf < 180) { 
2142 
lsf_sf_expand(slen, sf, 6, 6, 0); 
2143 
tindex2 = 3;

2144 
} else if (sf < 244) { 
2145 
lsf_sf_expand(slen, sf  180, 4, 4, 0); 
2146 
tindex2 = 4;

2147 
} else {

2148 
lsf_sf_expand(slen, sf  244, 3, 0, 0); 
2149 
tindex2 = 5;

2150 
} 
2151 
} else {

2152 
/* normal case */

2153 
if (sf < 400) { 
2154 
lsf_sf_expand(slen, sf, 5, 4, 4); 
2155 
tindex2 = 0;

2156 
} else if (sf < 500) { 
2157 
lsf_sf_expand(slen, sf  400, 5, 4, 0); 
2158 
tindex2 = 1;

2159 
} else {

2160 
lsf_sf_expand(slen, sf  500, 3, 0, 0); 
2161 
tindex2 = 2;

2162 
g>preflag = 1;

2163 
} 
2164 
} 
2165  
2166 
j = 0;

2167 
for(k=0;k<4;k++) { 
2168 
n = lsf_nsf_table[tindex2][tindex][k]; 
2169 
sl = slen[k]; 
2170 
if(sl){

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

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

2176 
} 
2177 
} 
2178 
/* XXX: should compute exact size */

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

2181 
} 
2182  
2183 
exponents_from_scale_factors(s, g, exponents); 
2184  
2185 
/* read Huffman coded residue */

2186 
huffman_decode(s, g, exponents, bits_pos + g>part2_3_length); 
2187 
} /* ch */

2188  
2189 
if (s>nb_channels == 2) 
2190 
compute_stereo(s, &s>granules[0][gr], &s>granules[1][gr]); 
2191  
2192 
for(ch=0;ch<s>nb_channels;ch++) { 
2193 
g = &s>granules[ch][gr]; 
2194  
2195 
reorder_block(s, g); 
2196 
compute_antialias(s, g); 
2197 
compute_imdct(s, g, &s>sb_samples[ch][18 * gr][0], s>mdct_buf[ch]); 
2198 
} 
2199 
} /* gr */

2200 
if(get_bits_count(&s>gb)<0) 
2201 
skip_bits_long(&s>gb, get_bits_count(&s>gb)); 
2202 
return nb_granules * 18; 
2203 
} 
2204  
2205 
static int mp_decode_frame(MPADecodeContext *s, 
2206 
OUT_INT *samples, const uint8_t *buf, int buf_size) 
2207 
{ 
2208 
int i, nb_frames, ch;

2209 
OUT_INT *samples_ptr; 
2210  
2211 
init_get_bits(&s>gb, buf + HEADER_SIZE, (buf_size  HEADER_SIZE)*8);

2212  
2213 
/* skip error protection field */

2214 
if (s>error_protection)

2215 
skip_bits(&s>gb, 16);

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

2218 
switch(s>layer) {

2219 
case 1: 
2220 
s>avctx>frame_size = 384;

2221 
nb_frames = mp_decode_layer1(s); 
2222 
break;

2223 
case 2: 
2224 
s>avctx>frame_size = 1152;

2225 
nb_frames = mp_decode_layer2(s); 
2226 
break;

2227 
case 3: 
2228 
s>avctx>frame_size = s>lsf ? 576 : 1152; 
2229 
default:

2230 
nb_frames = mp_decode_layer3(s); 
2231  
2232 
s>last_buf_size=0;

2233 
if(s>in_gb.buffer){

2234 
align_get_bits(&s>gb); 
2235 
i= get_bits_left(&s>gb)>>3;

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

2238 
s>last_buf_size=i; 
2239 
}else

2240 
av_log(s>avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);

2241 
s>gb= s>in_gb; 
2242 
s>in_gb.buffer= NULL;

2243 
} 
2244  
2245 
align_get_bits(&s>gb); 
2246 
assert((get_bits_count(&s>gb) & 7) == 0); 
2247 
i= get_bits_left(&s>gb)>>3;

2248  
2249 
if(i<0  i > BACKSTEP_SIZE  nb_frames<0){ 
2250 
if(i<0) 
2251 
av_log(s>avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);

2252 
i= FFMIN(BACKSTEP_SIZE, buf_size  HEADER_SIZE); 
2253 
} 
2254 
assert(i <= buf_size  HEADER_SIZE && i>= 0);

2255 
memcpy(s>last_buf + s>last_buf_size, s>gb.buffer + buf_size  HEADER_SIZE  i, i); 
2256 
s>last_buf_size += i; 
2257  
2258 
break;

2259 
} 
2260  
2261 
/* apply the synthesis filter */

2262 
for(ch=0;ch<s>nb_channels;ch++) { 
2263 
samples_ptr = samples + ch; 
2264 
for(i=0;i<nb_frames;i++) { 
2265 
RENAME(ff_mpa_synth_filter)( 
2266 
#if CONFIG_FLOAT

2267 
s, 
2268 
#endif

2269 
s>synth_buf[ch], &(s>synth_buf_offset[ch]), 
2270 
RENAME(ff_mpa_synth_window), &s>dither_state, 
2271 
samples_ptr, s>nb_channels, 
2272 
s>sb_samples[ch][i]); 
2273 
samples_ptr += 32 * s>nb_channels;

2274 
} 
2275 
} 
2276  
2277 
return nb_frames * 32 * sizeof(OUT_INT) * s>nb_channels; 
2278 
} 
2279  
2280 
static int decode_frame(AVCodecContext * avctx, 
2281 
void *data, int *data_size, 
2282 
AVPacket *avpkt) 
2283 
{ 
2284 
const uint8_t *buf = avpkt>data;

2285 
int buf_size = avpkt>size;

2286 
MPADecodeContext *s = avctx>priv_data; 
2287 
uint32_t header; 
2288 
int out_size;

2289 
OUT_INT *out_samples = data; 
2290  
2291 
if(buf_size < HEADER_SIZE)

2292 
return 1; 
2293  
2294 
header = AV_RB32(buf); 
2295 
if(ff_mpa_check_header(header) < 0){ 
2296 
av_log(avctx, AV_LOG_ERROR, "Header missing\n");

2297 
return 1; 
2298 
} 
2299  
2300 
if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) { 
2301 
/* free format: prepare to compute frame size */

2302 
s>frame_size = 1;

2303 
return 1; 
2304 
} 
2305 
/* update codec info */

2306 
avctx>channels = s>nb_channels; 
2307 
avctx>bit_rate = s>bit_rate; 
2308 
avctx>sub_id = s>layer; 
2309  
2310 
if(*data_size < 1152*avctx>channels*sizeof(OUT_INT)) 
2311 
return 1; 
2312 
*data_size = 0;

2313  
2314 
if(s>frame_size<=0  s>frame_size > buf_size){ 
2315 
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");

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

2319 
buf_size= s>frame_size; 
2320 
} 
2321  
2322 
out_size = mp_decode_frame(s, out_samples, buf, buf_size); 
2323 
if(out_size>=0){ 
2324 
*data_size = out_size; 
2325 
avctx>sample_rate = s>sample_rate; 
2326 
//FIXME maybe move the other codec info stuff from above here too

2327 
}else

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

2330 
return buf_size;

2331 
} 
2332  
2333 
static void flush(AVCodecContext *avctx){ 
2334 
MPADecodeContext *s = avctx>priv_data; 
2335 
memset(s>synth_buf, 0, sizeof(s>synth_buf)); 
2336 
s>last_buf_size= 0;

2337 
} 
2338  
2339 
#if CONFIG_MP3ADU_DECODER

2340 
static int decode_frame_adu(AVCodecContext * avctx, 
2341 
void *data, int *data_size, 
2342 
AVPacket *avpkt) 
2343 
{ 
2344 
const uint8_t *buf = avpkt>data;

2345 
int buf_size = avpkt>size;

2346 
MPADecodeContext *s = avctx>priv_data; 
2347 
uint32_t header; 
2348 
int len, out_size;

2349 
OUT_INT *out_samples = data; 
2350  
2351 
len = buf_size; 
2352  
2353 
// Discard too short frames

2354 
if (buf_size < HEADER_SIZE) {

2355 
*data_size = 0;

2356 
return buf_size;

2357 
} 
2358  
2359  
2360 
if (len > MPA_MAX_CODED_FRAME_SIZE)

2361 
len = MPA_MAX_CODED_FRAME_SIZE; 
2362  
2363 
// Get header and restore sync word

2364 
header = AV_RB32(buf)  0xffe00000;

2365  
2366 
if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame 
2367 
*data_size = 0;

2368 
return buf_size;

2369 
} 
2370  
2371 
ff_mpegaudio_decode_header((MPADecodeHeader *)s, header); 
2372 
/* update codec info */

2373 
avctx>sample_rate = s>sample_rate; 
2374 
avctx>channels = s>nb_channels; 
2375 
avctx>bit_rate = s>bit_rate; 
2376 
avctx>sub_id = s>layer; 
2377  
2378 
s>frame_size = len; 
2379  
2380 
if (avctx>parse_only) {

2381 
out_size = buf_size; 
2382 
} else {

2383 
out_size = mp_decode_frame(s, out_samples, buf, buf_size); 
2384 
} 
2385  
2386 
*data_size = out_size; 
2387 
return buf_size;

2388 
} 
2389 
#endif /* CONFIG_MP3ADU_DECODER */ 
2390  
2391 
#if CONFIG_MP3ON4_DECODER

2392  
2393 
/**

2394 
* Context for MP3On4 decoder

2395 
*/

2396 
typedef struct MP3On4DecodeContext { 
2397 
int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) 
2398 
int syncword; ///< syncword patch 
2399 
const uint8_t *coff; ///< channels offsets in output buffer 
2400 
MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance 
2401 
} MP3On4DecodeContext; 
2402  
2403 
#include "mpeg4audio.h" 
2404  
2405 
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */

2406 
static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */ 
2407 
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */

2408 
static const uint8_t chan_offset[8][5] = { 
2409 
{0},

2410 
{0}, // C 
2411 
{0}, // FLR 
2412 
{2,0}, // C FLR 
2413 
{2,0,3}, // C FLR BS 
2414 
{4,0,2}, // C FLR BLRS 
2415 
{4,0,2,5}, // C FLR BLRS LFE 
2416 
{4,0,2,6,5}, // C FLR BLRS BLR LFE 
2417 
}; 
2418  
2419  
2420 
static int decode_init_mp3on4(AVCodecContext * avctx) 
2421 
{ 
2422 
MP3On4DecodeContext *s = avctx>priv_data; 
2423 
MPEG4AudioConfig cfg; 
2424 
int i;

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

2428 
return 1; 
2429 
} 
2430  
2431 
ff_mpeg4audio_get_config(&cfg, avctx>extradata, avctx>extradata_size); 
2432 
if (!cfg.chan_config  cfg.chan_config > 7) { 
2433 
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");

2434 
return 1; 
2435 
} 
2436 
s>frames = mp3Frames[cfg.chan_config]; 
2437 
s>coff = chan_offset[cfg.chan_config]; 
2438 
avctx>channels = ff_mpeg4audio_channels[cfg.chan_config]; 
2439  
2440 
if (cfg.sample_rate < 16000) 
2441 
s>syncword = 0xffe00000;

2442 
else

2443 
s>syncword = 0xfff00000;

2444  
2445 
/* Init the first mp3 decoder in standard way, so that all tables get builded

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

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

2448 
* Other decoders will be initialized here copying data from the first context

2449 
*/

2450 
// Allocate zeroed memory for the first decoder context

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

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

2454 
decode_init(avctx); 
2455 
// Restore mp3on4 context pointer

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

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

2461 
*/

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

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

2465 
s>mp3decctx[i]>avctx = avctx; 
2466 
} 
2467  
2468 
return 0; 
2469 
} 
2470  
2471  
2472 
static av_cold int decode_close_mp3on4(AVCodecContext * avctx) 
2473 
{ 
2474 
MP3On4DecodeContext *s = avctx>priv_data; 
2475 
int i;

2476  
2477 
for (i = 0; i < s>frames; i++) 
2478 
if (s>mp3decctx[i])

2479 
av_free(s>mp3decctx[i]); 
2480  
2481 
return 0; 
2482 
} 
2483  
2484  
2485 
static int decode_frame_mp3on4(AVCodecContext * avctx, 
2486 
void *data, int *data_size, 
2487 
AVPacket *avpkt) 
2488 
{ 
2489 
const uint8_t *buf = avpkt>data;

2490 
int buf_size = avpkt>size;

2491 
MP3On4DecodeContext *s = avctx>priv_data; 
2492 
MPADecodeContext *m; 
2493 
int fsize, len = buf_size, out_size = 0; 
2494 
uint32_t header; 
2495 
OUT_INT *out_samples = data; 
2496 
OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS]; 
2497 
OUT_INT *outptr, *bp; 
2498 
int fr, j, n;

2499  
2500 
if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s>frames * sizeof(OUT_INT)) 
2501 
return 1; 
2502  
2503 
*data_size = 0;

2504 
// Discard too short frames

2505 
if (buf_size < HEADER_SIZE)

2506 
return 1; 
2507  
2508 
// If only one decoder interleave is not needed

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

2510  
2511 
avctx>bit_rate = 0;

2512  
2513 
for (fr = 0; fr < s>frames; fr++) { 
2514 
fsize = AV_RB16(buf) >> 4;

2515 
fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE); 
2516 
m = s>mp3decctx[fr]; 
2517 
assert (m != NULL);

2518  
2519 
header = (AV_RB32(buf) & 0x000fffff)  s>syncword; // patch header 
2520  
2521 
if (ff_mpa_check_header(header) < 0) // Bad header, discard block 
2522 
break;

2523  
2524 
ff_mpegaudio_decode_header((MPADecodeHeader *)m, header); 
2525 
out_size += mp_decode_frame(m, outptr, buf, fsize); 
2526 
buf += fsize; 
2527 
len = fsize; 
2528  
2529 
if(s>frames > 1) { 
2530 
n = m>avctx>frame_size*m>nb_channels; 
2531 
/* interleave output data */

2532 
bp = out_samples + s>coff[fr]; 
2533 
if(m>nb_channels == 1) { 
2534 
for(j = 0; j < n; j++) { 
2535 
*bp = decoded_buf[j]; 
2536 
bp += avctx>channels; 
2537 
} 
2538 
} else {

2539 
for(j = 0; j < n; j++) { 
2540 
bp[0] = decoded_buf[j++];

2541 
bp[1] = decoded_buf[j];

2542 
bp += avctx>channels; 
2543 
} 
2544 
} 
2545 
} 
2546 
avctx>bit_rate += m>bit_rate; 
2547 
} 
2548  
2549 
/* update codec info */

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

2551  
2552 
*data_size = out_size; 
2553 
return buf_size;

2554 
} 
2555 
#endif /* CONFIG_MP3ON4_DECODER */ 
2556  
2557 
#if !CONFIG_FLOAT

2558 
#if CONFIG_MP1_DECODER

2559 
AVCodec mp1_decoder = 
2560 
{ 
2561 
"mp1",

2562 
AVMEDIA_TYPE_AUDIO, 
2563 
CODEC_ID_MP1, 
2564 
sizeof(MPADecodeContext),

2565 
decode_init, 
2566 
NULL,

2567 
NULL,

2568 
decode_frame, 
2569 
CODEC_CAP_PARSE_ONLY, 
2570 
.flush= flush, 
2571 
.long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),

2572 
}; 
2573 
#endif

2574 
#if CONFIG_MP2_DECODER

2575 
AVCodec mp2_decoder = 
2576 
{ 
2577 
"mp2",

2578 
AVMEDIA_TYPE_AUDIO, 
2579 
CODEC_ID_MP2, 
2580 
sizeof(MPADecodeContext),

2581 
decode_init, 
2582 
NULL,

2583 
NULL,

2584 
decode_frame, 
2585 
CODEC_CAP_PARSE_ONLY, 
2586 
.flush= flush, 
2587 
.long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),

2588 
}; 
2589 
#endif

2590 
#if CONFIG_MP3_DECODER

2591 
AVCodec mp3_decoder = 
2592 
{ 
2593 
"mp3",

2594 
AVMEDIA_TYPE_AUDIO, 
2595 
CODEC_ID_MP3, 
2596 
sizeof(MPADecodeContext),

2597 
decode_init, 
2598 
NULL,

2599 
NULL,

2600 
decode_frame, 
2601 
CODEC_CAP_PARSE_ONLY, 
2602 
.flush= flush, 
2603 
.long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),

2604 
}; 
2605 
#endif

2606 
#if CONFIG_MP3ADU_DECODER

2607 
AVCodec mp3adu_decoder = 
2608 
{ 
2609 
"mp3adu",

2610 
AVMEDIA_TYPE_AUDIO, 
2611 
CODEC_ID_MP3ADU, 
2612 
sizeof(MPADecodeContext),

2613 
decode_init, 
2614 
NULL,

2615 
NULL,

2616 
decode_frame_adu, 
2617 
CODEC_CAP_PARSE_ONLY, 
2618 
.flush= flush, 
2619 
.long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),

2620 
}; 
2621 
#endif

2622 
#if CONFIG_MP3ON4_DECODER

2623 
AVCodec mp3on4_decoder = 
2624 
{ 
2625 
"mp3on4",

2626 
AVMEDIA_TYPE_AUDIO, 
2627 
CODEC_ID_MP3ON4, 
2628 
sizeof(MP3On4DecodeContext),

2629 
decode_init_mp3on4, 
2630 
NULL,

2631 
decode_close_mp3on4, 
2632 
decode_frame_mp3on4, 
2633 
.flush= flush, 
2634 
.long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),

2635 
}; 
2636 
#endif

2637 
#endif
