00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "bitstream.h"
00029 #include "dsputil.h"
00030
00031
00032
00033
00034
00035
00036
00037 #include "mpegaudio.h"
00038 #include "mpegaudiodecheader.h"
00039
00040 #include "mathops.h"
00041
00042
00043 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
00044 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
00045
00046 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
00047
00048
00049
00050 #define HEADER_SIZE 4
00051
00052
00053 typedef struct GranuleDef {
00054 uint8_t scfsi;
00055 int part2_3_length;
00056 int big_values;
00057 int global_gain;
00058 int scalefac_compress;
00059 uint8_t block_type;
00060 uint8_t switch_point;
00061 int table_select[3];
00062 int subblock_gain[3];
00063 uint8_t scalefac_scale;
00064 uint8_t count1table_select;
00065 int region_size[3];
00066 int preflag;
00067 int short_start, long_end;
00068 uint8_t scale_factors[40];
00069 int32_t sb_hybrid[SBLIMIT * 18];
00070 } GranuleDef;
00071
00072 #include "mpegaudiodata.h"
00073 #include "mpegaudiodectab.h"
00074
00075 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
00076 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
00077
00078
00079 static VLC huff_vlc[16];
00080 static VLC_TYPE huff_vlc_tables[
00081 0+128+128+128+130+128+154+166+
00082 142+204+190+170+542+460+662+414
00083 ][2];
00084 static const int huff_vlc_tables_sizes[16] = {
00085 0, 128, 128, 128, 130, 128, 154, 166,
00086 142, 204, 190, 170, 542, 460, 662, 414
00087 };
00088 static VLC huff_quad_vlc[2];
00089 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
00090 static const int huff_quad_vlc_tables_sizes[2] = {
00091 128, 16
00092 };
00093
00094 static uint16_t band_index_long[9][23];
00095
00096 #define TABLE_4_3_SIZE (8191 + 16)*4
00097 static int8_t table_4_3_exp[TABLE_4_3_SIZE];
00098 static uint32_t table_4_3_value[TABLE_4_3_SIZE];
00099 static uint32_t exp_table[512];
00100 static uint32_t expval_table[512][16];
00101
00102 static int32_t is_table[2][16];
00103 static int32_t is_table_lsf[2][2][16];
00104 static int32_t csa_table[8][4];
00105 static float csa_table_float[8][4];
00106 static int32_t mdct_win[8][36];
00107
00108
00109 static uint16_t scale_factor_modshift[64];
00110
00111 static int32_t scale_factor_mult[15][3];
00112
00113
00114 #define SCALE_GEN(v) \
00115 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
00116
00117 static const int32_t scale_factor_mult2[3][3] = {
00118 SCALE_GEN(4.0 / 3.0),
00119 SCALE_GEN(4.0 / 5.0),
00120 SCALE_GEN(4.0 / 9.0),
00121 };
00122
00123 static DECLARE_ALIGNED_16(MPA_INT, window[512]);
00124
00129 void ff_region_offset2size(GranuleDef *g){
00130 int i, k, j=0;
00131 g->region_size[2] = (576 / 2);
00132 for(i=0;i<3;i++) {
00133 k = FFMIN(g->region_size[i], g->big_values);
00134 g->region_size[i] = k - j;
00135 j = k;
00136 }
00137 }
00138
00139 void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
00140 if (g->block_type == 2)
00141 g->region_size[0] = (36 / 2);
00142 else {
00143 if (s->sample_rate_index <= 2)
00144 g->region_size[0] = (36 / 2);
00145 else if (s->sample_rate_index != 8)
00146 g->region_size[0] = (54 / 2);
00147 else
00148 g->region_size[0] = (108 / 2);
00149 }
00150 g->region_size[1] = (576 / 2);
00151 }
00152
00153 void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
00154 int l;
00155 g->region_size[0] =
00156 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
00157
00158 l = FFMIN(ra1 + ra2 + 2, 22);
00159 g->region_size[1] =
00160 band_index_long[s->sample_rate_index][l] >> 1;
00161 }
00162
00163 void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
00164 if (g->block_type == 2) {
00165 if (g->switch_point) {
00166
00167
00168
00169 if (s->sample_rate_index <= 2)
00170 g->long_end = 8;
00171 else if (s->sample_rate_index != 8)
00172 g->long_end = 6;
00173 else
00174 g->long_end = 4;
00175
00176 g->short_start = 2 + (s->sample_rate_index != 8);
00177 } else {
00178 g->long_end = 0;
00179 g->short_start = 0;
00180 }
00181 } else {
00182 g->short_start = 13;
00183 g->long_end = 22;
00184 }
00185 }
00186
00187
00188
00189 static inline int l1_unscale(int n, int mant, int scale_factor)
00190 {
00191 int shift, mod;
00192 int64_t val;
00193
00194 shift = scale_factor_modshift[scale_factor];
00195 mod = shift & 3;
00196 shift >>= 2;
00197 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
00198 shift += n;
00199
00200 return (int)((val + (1LL << (shift - 1))) >> shift);
00201 }
00202
00203 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
00204 {
00205 int shift, mod, val;
00206
00207 shift = scale_factor_modshift[scale_factor];
00208 mod = shift & 3;
00209 shift >>= 2;
00210
00211 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
00212
00213 if (shift > 0)
00214 val = (val + (1 << (shift - 1))) >> shift;
00215 return val;
00216 }
00217
00218
00219 static inline int l3_unscale(int value, int exponent)
00220 {
00221 unsigned int m;
00222 int e;
00223
00224 e = table_4_3_exp [4*value + (exponent&3)];
00225 m = table_4_3_value[4*value + (exponent&3)];
00226 e -= (exponent >> 2);
00227 assert(e>=1);
00228 if (e > 31)
00229 return 0;
00230 m = (m + (1 << (e-1))) >> e;
00231
00232 return m;
00233 }
00234
00235
00236 #define DEV_ORDER 13
00237
00238 #define POW_FRAC_BITS 24
00239 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
00240 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
00241 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
00242
00243 static int dev_4_3_coefs[DEV_ORDER];
00244
00245 #if 0
00246 static int pow_mult3[3] = {
00247 POW_FIX(1.0),
00248 POW_FIX(1.25992104989487316476),
00249 POW_FIX(1.58740105196819947474),
00250 };
00251 #endif
00252
00253 static av_cold void int_pow_init(void)
00254 {
00255 int i, a;
00256
00257 a = POW_FIX(1.0);
00258 for(i=0;i<DEV_ORDER;i++) {
00259 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
00260 dev_4_3_coefs[i] = a;
00261 }
00262 }
00263
00264 #if 0
00265
00266 static int int_pow(int i, int *exp_ptr)
00267 {
00268 int e, er, eq, j;
00269 int a, a1;
00270
00271
00272 a = i;
00273 e = POW_FRAC_BITS;
00274 while (a < (1 << (POW_FRAC_BITS - 1))) {
00275 a = a << 1;
00276 e--;
00277 }
00278 a -= (1 << POW_FRAC_BITS);
00279 a1 = 0;
00280 for(j = DEV_ORDER - 1; j >= 0; j--)
00281 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
00282 a = (1 << POW_FRAC_BITS) + a1;
00283
00284 e = e * 4;
00285 er = e % 3;
00286 eq = e / 3;
00287 a = POW_MULL(a, pow_mult3[er]);
00288 while (a >= 2 * POW_FRAC_ONE) {
00289 a = a >> 1;
00290 eq++;
00291 }
00292
00293 while (a < POW_FRAC_ONE) {
00294 a = a << 1;
00295 eq--;
00296 }
00297
00298 #if POW_FRAC_BITS > FRAC_BITS
00299 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
00300
00301 if (a >= 2 * (1 << FRAC_BITS)) {
00302 a = a >> 1;
00303 eq++;
00304 }
00305 #endif
00306 *exp_ptr = eq;
00307 return a;
00308 }
00309 #endif
00310
00311 static av_cold int decode_init(AVCodecContext * avctx)
00312 {
00313 MPADecodeContext *s = avctx->priv_data;
00314 static int init=0;
00315 int i, j, k;
00316
00317 s->avctx = avctx;
00318
00319 avctx->sample_fmt= OUT_FMT;
00320 s->error_recognition= avctx->error_recognition;
00321
00322 if(avctx->antialias_algo != FF_AA_FLOAT)
00323 s->compute_antialias= compute_antialias_integer;
00324 else
00325 s->compute_antialias= compute_antialias_float;
00326
00327 if (!init && !avctx->parse_only) {
00328 int offset;
00329
00330
00331 for(i=0;i<64;i++) {
00332 int shift, mod;
00333
00334 shift = (i / 3);
00335 mod = i % 3;
00336 scale_factor_modshift[i] = mod | (shift << 2);
00337 }
00338
00339
00340 for(i=0;i<15;i++) {
00341 int n, norm;
00342 n = i + 2;
00343 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
00344 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm, FRAC_BITS);
00345 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm, FRAC_BITS);
00346 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm, FRAC_BITS);
00347 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
00348 i, norm,
00349 scale_factor_mult[i][0],
00350 scale_factor_mult[i][1],
00351 scale_factor_mult[i][2]);
00352 }
00353
00354 ff_mpa_synth_init(window);
00355
00356
00357 offset = 0;
00358 for(i=1;i<16;i++) {
00359 const HuffTable *h = &mpa_huff_tables[i];
00360 int xsize, x, y;
00361 unsigned int n;
00362 uint8_t tmp_bits [512];
00363 uint16_t tmp_codes[512];
00364
00365 memset(tmp_bits , 0, sizeof(tmp_bits ));
00366 memset(tmp_codes, 0, sizeof(tmp_codes));
00367
00368 xsize = h->xsize;
00369 n = xsize * xsize;
00370
00371 j = 0;
00372 for(x=0;x<xsize;x++) {
00373 for(y=0;y<xsize;y++){
00374 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
00375 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
00376 }
00377 }
00378
00379
00380 huff_vlc[i].table = huff_vlc_tables+offset;
00381 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
00382 init_vlc(&huff_vlc[i], 7, 512,
00383 tmp_bits, 1, 1, tmp_codes, 2, 2,
00384 INIT_VLC_USE_NEW_STATIC);
00385 offset += huff_vlc_tables_sizes[i];
00386 }
00387 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
00388
00389 offset = 0;
00390 for(i=0;i<2;i++) {
00391 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
00392 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
00393 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
00394 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
00395 INIT_VLC_USE_NEW_STATIC);
00396 offset += huff_quad_vlc_tables_sizes[i];
00397 }
00398 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
00399
00400 for(i=0;i<9;i++) {
00401 k = 0;
00402 for(j=0;j<22;j++) {
00403 band_index_long[i][j] = k;
00404 k += band_size_long[i][j];
00405 }
00406 band_index_long[i][22] = k;
00407 }
00408
00409
00410
00411 int_pow_init();
00412 for(i=1;i<TABLE_4_3_SIZE;i++) {
00413 double f, fm;
00414 int e, m;
00415 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
00416 fm = frexp(f, &e);
00417 m = (uint32_t)(fm*(1LL<<31) + 0.5);
00418 e+= FRAC_BITS - 31 + 5 - 100;
00419
00420
00421 table_4_3_value[i] = m;
00422 table_4_3_exp[i] = -e;
00423 }
00424 for(i=0; i<512*16; i++){
00425 int exponent= (i>>4);
00426 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
00427 expval_table[exponent][i&15]= llrint(f);
00428 if((i&15)==1)
00429 exp_table[exponent]= llrint(f);
00430 }
00431
00432 for(i=0;i<7;i++) {
00433 float f;
00434 int v;
00435 if (i != 6) {
00436 f = tan((double)i * M_PI / 12.0);
00437 v = FIXR(f / (1.0 + f));
00438 } else {
00439 v = FIXR(1.0);
00440 }
00441 is_table[0][i] = v;
00442 is_table[1][6 - i] = v;
00443 }
00444
00445 for(i=7;i<16;i++)
00446 is_table[0][i] = is_table[1][i] = 0.0;
00447
00448 for(i=0;i<16;i++) {
00449 double f;
00450 int e, k;
00451
00452 for(j=0;j<2;j++) {
00453 e = -(j + 1) * ((i + 1) >> 1);
00454 f = pow(2.0, e / 4.0);
00455 k = i & 1;
00456 is_table_lsf[j][k ^ 1][i] = FIXR(f);
00457 is_table_lsf[j][k][i] = FIXR(1.0);
00458 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
00459 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
00460 }
00461 }
00462
00463 for(i=0;i<8;i++) {
00464 float ci, cs, ca;
00465 ci = ci_table[i];
00466 cs = 1.0 / sqrt(1.0 + ci * ci);
00467 ca = cs * ci;
00468 csa_table[i][0] = FIXHR(cs/4);
00469 csa_table[i][1] = FIXHR(ca/4);
00470 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
00471 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
00472 csa_table_float[i][0] = cs;
00473 csa_table_float[i][1] = ca;
00474 csa_table_float[i][2] = ca + cs;
00475 csa_table_float[i][3] = ca - cs;
00476 }
00477
00478
00479 for(i=0;i<36;i++) {
00480 for(j=0; j<4; j++){
00481 double d;
00482
00483 if(j==2 && i%3 != 1)
00484 continue;
00485
00486 d= sin(M_PI * (i + 0.5) / 36.0);
00487 if(j==1){
00488 if (i>=30) d= 0;
00489 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
00490 else if(i>=18) d= 1;
00491 }else if(j==3){
00492 if (i< 6) d= 0;
00493 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
00494 else if(i< 18) d= 1;
00495 }
00496
00497 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
00498
00499 if(j==2)
00500 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
00501 else
00502 mdct_win[j][i ] = FIXHR((d / (1<<5)));
00503 }
00504 }
00505
00506
00507
00508 for(j=0;j<4;j++) {
00509 for(i=0;i<36;i+=2) {
00510 mdct_win[j + 4][i] = mdct_win[j][i];
00511 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
00512 }
00513 }
00514
00515 init = 1;
00516 }
00517
00518 if (avctx->codec_id == CODEC_ID_MP3ADU)
00519 s->adu_mode = 1;
00520 return 0;
00521 }
00522
00523
00524
00525
00526
00527 #define COS0_0 FIXHR(0.50060299823519630134/2)
00528 #define COS0_1 FIXHR(0.50547095989754365998/2)
00529 #define COS0_2 FIXHR(0.51544730992262454697/2)
00530 #define COS0_3 FIXHR(0.53104259108978417447/2)
00531 #define COS0_4 FIXHR(0.55310389603444452782/2)
00532 #define COS0_5 FIXHR(0.58293496820613387367/2)
00533 #define COS0_6 FIXHR(0.62250412303566481615/2)
00534 #define COS0_7 FIXHR(0.67480834145500574602/2)
00535 #define COS0_8 FIXHR(0.74453627100229844977/2)
00536 #define COS0_9 FIXHR(0.83934964541552703873/2)
00537 #define COS0_10 FIXHR(0.97256823786196069369/2)
00538 #define COS0_11 FIXHR(1.16943993343288495515/4)
00539 #define COS0_12 FIXHR(1.48416461631416627724/4)
00540 #define COS0_13 FIXHR(2.05778100995341155085/8)
00541 #define COS0_14 FIXHR(3.40760841846871878570/8)
00542 #define COS0_15 FIXHR(10.19000812354805681150/32)
00543
00544 #define COS1_0 FIXHR(0.50241928618815570551/2)
00545 #define COS1_1 FIXHR(0.52249861493968888062/2)
00546 #define COS1_2 FIXHR(0.56694403481635770368/2)
00547 #define COS1_3 FIXHR(0.64682178335999012954/2)
00548 #define COS1_4 FIXHR(0.78815462345125022473/2)
00549 #define COS1_5 FIXHR(1.06067768599034747134/4)
00550 #define COS1_6 FIXHR(1.72244709823833392782/4)
00551 #define COS1_7 FIXHR(5.10114861868916385802/16)
00552
00553 #define COS2_0 FIXHR(0.50979557910415916894/2)
00554 #define COS2_1 FIXHR(0.60134488693504528054/2)
00555 #define COS2_2 FIXHR(0.89997622313641570463/2)
00556 #define COS2_3 FIXHR(2.56291544774150617881/8)
00557
00558 #define COS3_0 FIXHR(0.54119610014619698439/2)
00559 #define COS3_1 FIXHR(1.30656296487637652785/4)
00560
00561 #define COS4_0 FIXHR(0.70710678118654752439/2)
00562
00563
00564 #define BF(a, b, c, s)\
00565 {\
00566 tmp0 = tab[a] + tab[b];\
00567 tmp1 = tab[a] - tab[b];\
00568 tab[a] = tmp0;\
00569 tab[b] = MULH(tmp1<<(s), c);\
00570 }
00571
00572 #define BF1(a, b, c, d)\
00573 {\
00574 BF(a, b, COS4_0, 1);\
00575 BF(c, d,-COS4_0, 1);\
00576 tab[c] += tab[d];\
00577 }
00578
00579 #define BF2(a, b, c, d)\
00580 {\
00581 BF(a, b, COS4_0, 1);\
00582 BF(c, d,-COS4_0, 1);\
00583 tab[c] += tab[d];\
00584 tab[a] += tab[c];\
00585 tab[c] += tab[b];\
00586 tab[b] += tab[d];\
00587 }
00588
00589 #define ADD(a, b) tab[a] += tab[b]
00590
00591
00592 static void dct32(int32_t *out, int32_t *tab)
00593 {
00594 int tmp0, tmp1;
00595
00596
00597 BF( 0, 31, COS0_0 , 1);
00598 BF(15, 16, COS0_15, 5);
00599
00600 BF( 0, 15, COS1_0 , 1);
00601 BF(16, 31,-COS1_0 , 1);
00602
00603 BF( 7, 24, COS0_7 , 1);
00604 BF( 8, 23, COS0_8 , 1);
00605
00606 BF( 7, 8, COS1_7 , 4);
00607 BF(23, 24,-COS1_7 , 4);
00608
00609 BF( 0, 7, COS2_0 , 1);
00610 BF( 8, 15,-COS2_0 , 1);
00611 BF(16, 23, COS2_0 , 1);
00612 BF(24, 31,-COS2_0 , 1);
00613
00614 BF( 3, 28, COS0_3 , 1);
00615 BF(12, 19, COS0_12, 2);
00616
00617 BF( 3, 12, COS1_3 , 1);
00618 BF(19, 28,-COS1_3 , 1);
00619
00620 BF( 4, 27, COS0_4 , 1);
00621 BF(11, 20, COS0_11, 2);
00622
00623 BF( 4, 11, COS1_4 , 1);
00624 BF(20, 27,-COS1_4 , 1);
00625
00626 BF( 3, 4, COS2_3 , 3);
00627 BF(11, 12,-COS2_3 , 3);
00628 BF(19, 20, COS2_3 , 3);
00629 BF(27, 28,-COS2_3 , 3);
00630
00631 BF( 0, 3, COS3_0 , 1);
00632 BF( 4, 7,-COS3_0 , 1);
00633 BF( 8, 11, COS3_0 , 1);
00634 BF(12, 15,-COS3_0 , 1);
00635 BF(16, 19, COS3_0 , 1);
00636 BF(20, 23,-COS3_0 , 1);
00637 BF(24, 27, COS3_0 , 1);
00638 BF(28, 31,-COS3_0 , 1);
00639
00640
00641
00642
00643 BF( 1, 30, COS0_1 , 1);
00644 BF(14, 17, COS0_14, 3);
00645
00646 BF( 1, 14, COS1_1 , 1);
00647 BF(17, 30,-COS1_1 , 1);
00648
00649 BF( 6, 25, COS0_6 , 1);
00650 BF( 9, 22, COS0_9 , 1);
00651
00652 BF( 6, 9, COS1_6 , 2);
00653 BF(22, 25,-COS1_6 , 2);
00654
00655 BF( 1, 6, COS2_1 , 1);
00656 BF( 9, 14,-COS2_1 , 1);
00657 BF(17, 22, COS2_1 , 1);
00658 BF(25, 30,-COS2_1 , 1);
00659
00660
00661 BF( 2, 29, COS0_2 , 1);
00662 BF(13, 18, COS0_13, 3);
00663
00664 BF( 2, 13, COS1_2 , 1);
00665 BF(18, 29,-COS1_2 , 1);
00666
00667 BF( 5, 26, COS0_5 , 1);
00668 BF(10, 21, COS0_10, 1);
00669
00670 BF( 5, 10, COS1_5 , 2);
00671 BF(21, 26,-COS1_5 , 2);
00672
00673 BF( 2, 5, COS2_2 , 1);
00674 BF(10, 13,-COS2_2 , 1);
00675 BF(18, 21, COS2_2 , 1);
00676 BF(26, 29,-COS2_2 , 1);
00677
00678 BF( 1, 2, COS3_1 , 2);
00679 BF( 5, 6,-COS3_1 , 2);
00680 BF( 9, 10, COS3_1 , 2);
00681 BF(13, 14,-COS3_1 , 2);
00682 BF(17, 18, COS3_1 , 2);
00683 BF(21, 22,-COS3_1 , 2);
00684 BF(25, 26, COS3_1 , 2);
00685 BF(29, 30,-COS3_1 , 2);
00686
00687
00688 BF1( 0, 1, 2, 3);
00689 BF2( 4, 5, 6, 7);
00690 BF1( 8, 9, 10, 11);
00691 BF2(12, 13, 14, 15);
00692 BF1(16, 17, 18, 19);
00693 BF2(20, 21, 22, 23);
00694 BF1(24, 25, 26, 27);
00695 BF2(28, 29, 30, 31);
00696
00697
00698
00699 ADD( 8, 12);
00700 ADD(12, 10);
00701 ADD(10, 14);
00702 ADD(14, 9);
00703 ADD( 9, 13);
00704 ADD(13, 11);
00705 ADD(11, 15);
00706
00707 out[ 0] = tab[0];
00708 out[16] = tab[1];
00709 out[ 8] = tab[2];
00710 out[24] = tab[3];
00711 out[ 4] = tab[4];
00712 out[20] = tab[5];
00713 out[12] = tab[6];
00714 out[28] = tab[7];
00715 out[ 2] = tab[8];
00716 out[18] = tab[9];
00717 out[10] = tab[10];
00718 out[26] = tab[11];
00719 out[ 6] = tab[12];
00720 out[22] = tab[13];
00721 out[14] = tab[14];
00722 out[30] = tab[15];
00723
00724 ADD(24, 28);
00725 ADD(28, 26);
00726 ADD(26, 30);
00727 ADD(30, 25);
00728 ADD(25, 29);
00729 ADD(29, 27);
00730 ADD(27, 31);
00731
00732 out[ 1] = tab[16] + tab[24];
00733 out[17] = tab[17] + tab[25];
00734 out[ 9] = tab[18] + tab[26];
00735 out[25] = tab[19] + tab[27];
00736 out[ 5] = tab[20] + tab[28];
00737 out[21] = tab[21] + tab[29];
00738 out[13] = tab[22] + tab[30];
00739 out[29] = tab[23] + tab[31];
00740 out[ 3] = tab[24] + tab[20];
00741 out[19] = tab[25] + tab[21];
00742 out[11] = tab[26] + tab[22];
00743 out[27] = tab[27] + tab[23];
00744 out[ 7] = tab[28] + tab[18];
00745 out[23] = tab[29] + tab[19];
00746 out[15] = tab[30] + tab[17];
00747 out[31] = tab[31];
00748 }
00749
00750 #if FRAC_BITS <= 15
00751
00752 static inline int round_sample(int *sum)
00753 {
00754 int sum1;
00755 sum1 = (*sum) >> OUT_SHIFT;
00756 *sum &= (1<<OUT_SHIFT)-1;
00757 if (sum1 < OUT_MIN)
00758 sum1 = OUT_MIN;
00759 else if (sum1 > OUT_MAX)
00760 sum1 = OUT_MAX;
00761 return sum1;
00762 }
00763
00764
00765 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
00766
00767
00768 #define MULS(ra, rb) MUL16(ra, rb)
00769
00770 #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
00771
00772 #else
00773
00774 static inline int round_sample(int64_t *sum)
00775 {
00776 int sum1;
00777 sum1 = (int)((*sum) >> OUT_SHIFT);
00778 *sum &= (1<<OUT_SHIFT)-1;
00779 if (sum1 < OUT_MIN)
00780 sum1 = OUT_MIN;
00781 else if (sum1 > OUT_MAX)
00782 sum1 = OUT_MAX;
00783 return sum1;
00784 }
00785
00786 # define MULS(ra, rb) MUL64(ra, rb)
00787 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
00788 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
00789 #endif
00790
00791 #define SUM8(op, sum, w, p) \
00792 { \
00793 op(sum, (w)[0 * 64], p[0 * 64]); \
00794 op(sum, (w)[1 * 64], p[1 * 64]); \
00795 op(sum, (w)[2 * 64], p[2 * 64]); \
00796 op(sum, (w)[3 * 64], p[3 * 64]); \
00797 op(sum, (w)[4 * 64], p[4 * 64]); \
00798 op(sum, (w)[5 * 64], p[5 * 64]); \
00799 op(sum, (w)[6 * 64], p[6 * 64]); \
00800 op(sum, (w)[7 * 64], p[7 * 64]); \
00801 }
00802
00803 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
00804 { \
00805 int tmp;\
00806 tmp = p[0 * 64];\
00807 op1(sum1, (w1)[0 * 64], tmp);\
00808 op2(sum2, (w2)[0 * 64], tmp);\
00809 tmp = p[1 * 64];\
00810 op1(sum1, (w1)[1 * 64], tmp);\
00811 op2(sum2, (w2)[1 * 64], tmp);\
00812 tmp = p[2 * 64];\
00813 op1(sum1, (w1)[2 * 64], tmp);\
00814 op2(sum2, (w2)[2 * 64], tmp);\
00815 tmp = p[3 * 64];\
00816 op1(sum1, (w1)[3 * 64], tmp);\
00817 op2(sum2, (w2)[3 * 64], tmp);\
00818 tmp = p[4 * 64];\
00819 op1(sum1, (w1)[4 * 64], tmp);\
00820 op2(sum2, (w2)[4 * 64], tmp);\
00821 tmp = p[5 * 64];\
00822 op1(sum1, (w1)[5 * 64], tmp);\
00823 op2(sum2, (w2)[5 * 64], tmp);\
00824 tmp = p[6 * 64];\
00825 op1(sum1, (w1)[6 * 64], tmp);\
00826 op2(sum2, (w2)[6 * 64], tmp);\
00827 tmp = p[7 * 64];\
00828 op1(sum1, (w1)[7 * 64], tmp);\
00829 op2(sum2, (w2)[7 * 64], tmp);\
00830 }
00831
00832 void av_cold ff_mpa_synth_init(MPA_INT *window)
00833 {
00834 int i;
00835
00836
00837 for(i=0;i<257;i++) {
00838 int v;
00839 v = ff_mpa_enwindow[i];
00840 #if WFRAC_BITS < 16
00841 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
00842 #endif
00843 window[i] = v;
00844 if ((i & 63) != 0)
00845 v = -v;
00846 if (i != 0)
00847 window[512 - i] = v;
00848 }
00849 }
00850
00851
00852
00853
00854 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
00855 MPA_INT *window, int *dither_state,
00856 OUT_INT *samples, int incr,
00857 int32_t sb_samples[SBLIMIT])
00858 {
00859 int32_t tmp[32];
00860 register MPA_INT *synth_buf;
00861 register const MPA_INT *w, *w2, *p;
00862 int j, offset, v;
00863 OUT_INT *samples2;
00864 #if FRAC_BITS <= 15
00865 int sum, sum2;
00866 #else
00867 int64_t sum, sum2;
00868 #endif
00869
00870 dct32(tmp, sb_samples);
00871
00872 offset = *synth_buf_offset;
00873 synth_buf = synth_buf_ptr + offset;
00874
00875 for(j=0;j<32;j++) {
00876 v = tmp[j];
00877 #if FRAC_BITS <= 15
00878
00879
00880 v = av_clip_int16(v);
00881 #endif
00882 synth_buf[j] = v;
00883 }
00884
00885 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
00886
00887 samples2 = samples + 31 * incr;
00888 w = window;
00889 w2 = window + 31;
00890
00891 sum = *dither_state;
00892 p = synth_buf + 16;
00893 SUM8(MACS, sum, w, p);
00894 p = synth_buf + 48;
00895 SUM8(MLSS, sum, w + 32, p);
00896 *samples = round_sample(&sum);
00897 samples += incr;
00898 w++;
00899
00900
00901
00902 for(j=1;j<16;j++) {
00903 sum2 = 0;
00904 p = synth_buf + 16 + j;
00905 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
00906 p = synth_buf + 48 - j;
00907 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
00908
00909 *samples = round_sample(&sum);
00910 samples += incr;
00911 sum += sum2;
00912 *samples2 = round_sample(&sum);
00913 samples2 -= incr;
00914 w++;
00915 w2--;
00916 }
00917
00918 p = synth_buf + 32;
00919 SUM8(MLSS, sum, w + 32, p);
00920 *samples = round_sample(&sum);
00921 *dither_state= sum;
00922
00923 offset = (offset - 32) & 511;
00924 *synth_buf_offset = offset;
00925 }
00926
00927 #define C3 FIXHR(0.86602540378443864676/2)
00928
00929
00930 static const int icos36[9] = {
00931 FIXR(0.50190991877167369479),
00932 FIXR(0.51763809020504152469),
00933 FIXR(0.55168895948124587824),
00934 FIXR(0.61038729438072803416),
00935 FIXR(0.70710678118654752439),
00936 FIXR(0.87172339781054900991),
00937 FIXR(1.18310079157624925896),
00938 FIXR(1.93185165257813657349),
00939 FIXR(5.73685662283492756461),
00940 };
00941
00942
00943 static const int icos36h[9] = {
00944 FIXHR(0.50190991877167369479/2),
00945 FIXHR(0.51763809020504152469/2),
00946 FIXHR(0.55168895948124587824/2),
00947 FIXHR(0.61038729438072803416/2),
00948 FIXHR(0.70710678118654752439/2),
00949 FIXHR(0.87172339781054900991/2),
00950 FIXHR(1.18310079157624925896/4),
00951 FIXHR(1.93185165257813657349/4),
00952
00953 };
00954
00955
00956
00957 static void imdct12(int *out, int *in)
00958 {
00959 int in0, in1, in2, in3, in4, in5, t1, t2;
00960
00961 in0= in[0*3];
00962 in1= in[1*3] + in[0*3];
00963 in2= in[2*3] + in[1*3];
00964 in3= in[3*3] + in[2*3];
00965 in4= in[4*3] + in[3*3];
00966 in5= in[5*3] + in[4*3];
00967 in5 += in3;
00968 in3 += in1;
00969
00970 in2= MULH(2*in2, C3);
00971 in3= MULH(4*in3, C3);
00972
00973 t1 = in0 - in4;
00974 t2 = MULH(2*(in1 - in5), icos36h[4]);
00975
00976 out[ 7]=
00977 out[10]= t1 + t2;
00978 out[ 1]=
00979 out[ 4]= t1 - t2;
00980
00981 in0 += in4>>1;
00982 in4 = in0 + in2;
00983 in5 += 2*in1;
00984 in1 = MULH(in5 + in3, icos36h[1]);
00985 out[ 8]=
00986 out[ 9]= in4 + in1;
00987 out[ 2]=
00988 out[ 3]= in4 - in1;
00989
00990 in0 -= in2;
00991 in5 = MULH(2*(in5 - in3), icos36h[7]);
00992 out[ 0]=
00993 out[ 5]= in0 - in5;
00994 out[ 6]=
00995 out[11]= in0 + in5;
00996 }
00997
00998
00999 #define C1 FIXHR(0.98480775301220805936/2)
01000 #define C2 FIXHR(0.93969262078590838405/2)
01001 #define C3 FIXHR(0.86602540378443864676/2)
01002 #define C4 FIXHR(0.76604444311897803520/2)
01003 #define C5 FIXHR(0.64278760968653932632/2)
01004 #define C6 FIXHR(0.5/2)
01005 #define C7 FIXHR(0.34202014332566873304/2)
01006 #define C8 FIXHR(0.17364817766693034885/2)
01007
01008
01009
01010 static void imdct36(int *out, int *buf, int *in, int *win)
01011 {
01012 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
01013 int tmp[18], *tmp1, *in1;
01014
01015 for(i=17;i>=1;i--)
01016 in[i] += in[i-1];
01017 for(i=17;i>=3;i-=2)
01018 in[i] += in[i-2];
01019
01020 for(j=0;j<2;j++) {
01021 tmp1 = tmp + j;
01022 in1 = in + j;
01023 #if 0
01024
01025 int64_t t0, t1, t2, t3;
01026 t2 = in1[2*4] + in1[2*8] - in1[2*2];
01027
01028 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
01029 t1 = in1[2*0] - in1[2*6];
01030 tmp1[ 6] = t1 - (t2>>1);
01031 tmp1[16] = t1 + t2;
01032
01033 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
01034 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
01035 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
01036
01037 tmp1[10] = (t3 - t0 - t2) >> 32;
01038 tmp1[ 2] = (t3 + t0 + t1) >> 32;
01039 tmp1[14] = (t3 + t2 - t1) >> 32;
01040
01041 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
01042 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
01043 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
01044 t0 = MUL64(2*in1[2*3], C3);
01045
01046 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
01047
01048 tmp1[ 0] = (t2 + t3 + t0) >> 32;
01049 tmp1[12] = (t2 + t1 - t0) >> 32;
01050 tmp1[ 8] = (t3 - t1 - t0) >> 32;
01051 #else
01052 t2 = in1[2*4] + in1[2*8] - in1[2*2];
01053
01054 t3 = in1[2*0] + (in1[2*6]>>1);
01055 t1 = in1[2*0] - in1[2*6];
01056 tmp1[ 6] = t1 - (t2>>1);
01057 tmp1[16] = t1 + t2;
01058
01059 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
01060 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
01061 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
01062
01063 tmp1[10] = t3 - t0 - t2;
01064 tmp1[ 2] = t3 + t0 + t1;
01065 tmp1[14] = t3 + t2 - t1;
01066
01067 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
01068 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
01069 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
01070 t0 = MULH(2*in1[2*3], C3);
01071
01072 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
01073
01074 tmp1[ 0] = t2 + t3 + t0;
01075 tmp1[12] = t2 + t1 - t0;
01076 tmp1[ 8] = t3 - t1 - t0;
01077 #endif
01078 }
01079
01080 i = 0;
01081 for(j=0;j<4;j++) {
01082 t0 = tmp[i];
01083 t1 = tmp[i + 2];
01084 s0 = t1 + t0;
01085 s2 = t1 - t0;
01086
01087 t2 = tmp[i + 1];
01088 t3 = tmp[i + 3];
01089 s1 = MULH(2*(t3 + t2), icos36h[j]);
01090 s3 = MULL(t3 - t2, icos36[8 - j], FRAC_BITS);
01091
01092 t0 = s0 + s1;
01093 t1 = s0 - s1;
01094 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
01095 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
01096 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
01097 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
01098
01099 t0 = s2 + s3;
01100 t1 = s2 - s3;
01101 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
01102 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
01103 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
01104 buf[ + j] = MULH(t0, win[18 + j]);
01105 i += 4;
01106 }
01107
01108 s0 = tmp[16];
01109 s1 = MULH(2*tmp[17], icos36h[4]);
01110 t0 = s0 + s1;
01111 t1 = s0 - s1;
01112 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
01113 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
01114 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
01115 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
01116 }
01117
01118
01119 static int mp_decode_layer1(MPADecodeContext *s)
01120 {
01121 int bound, i, v, n, ch, j, mant;
01122 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
01123 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
01124
01125 if (s->mode == MPA_JSTEREO)
01126 bound = (s->mode_ext + 1) * 4;
01127 else
01128 bound = SBLIMIT;
01129
01130
01131 for(i=0;i<bound;i++) {
01132 for(ch=0;ch<s->nb_channels;ch++) {
01133 allocation[ch][i] = get_bits(&s->gb, 4);
01134 }
01135 }
01136 for(i=bound;i<SBLIMIT;i++) {
01137 allocation[0][i] = get_bits(&s->gb, 4);
01138 }
01139
01140
01141 for(i=0;i<bound;i++) {
01142 for(ch=0;ch<s->nb_channels;ch++) {
01143 if (allocation[ch][i])
01144 scale_factors[ch][i] = get_bits(&s->gb, 6);
01145 }
01146 }
01147 for(i=bound;i<SBLIMIT;i++) {
01148 if (allocation[0][i]) {
01149 scale_factors[0][i] = get_bits(&s->gb, 6);
01150 scale_factors[1][i] = get_bits(&s->gb, 6);
01151 }
01152 }
01153
01154
01155 for(j=0;j<12;j++) {
01156 for(i=0;i<bound;i++) {
01157 for(ch=0;ch<s->nb_channels;ch++) {
01158 n = allocation[ch][i];
01159 if (n) {
01160 mant = get_bits(&s->gb, n + 1);
01161 v = l1_unscale(n, mant, scale_factors[ch][i]);
01162 } else {
01163 v = 0;
01164 }
01165 s->sb_samples[ch][j][i] = v;
01166 }
01167 }
01168 for(i=bound;i<SBLIMIT;i++) {
01169 n = allocation[0][i];
01170 if (n) {
01171 mant = get_bits(&s->gb, n + 1);
01172 v = l1_unscale(n, mant, scale_factors[0][i]);
01173 s->sb_samples[0][j][i] = v;
01174 v = l1_unscale(n, mant, scale_factors[1][i]);
01175 s->sb_samples[1][j][i] = v;
01176 } else {
01177 s->sb_samples[0][j][i] = 0;
01178 s->sb_samples[1][j][i] = 0;
01179 }
01180 }
01181 }
01182 return 12;
01183 }
01184
01185 static int mp_decode_layer2(MPADecodeContext *s)
01186 {
01187 int sblimit;
01188 const unsigned char *alloc_table;
01189 int table, bit_alloc_bits, i, j, ch, bound, v;
01190 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
01191 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
01192 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
01193 int scale, qindex, bits, steps, k, l, m, b;
01194
01195
01196 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
01197 s->sample_rate, s->lsf);
01198 sblimit = ff_mpa_sblimit_table[table];
01199 alloc_table = ff_mpa_alloc_tables[table];
01200
01201 if (s->mode == MPA_JSTEREO)
01202 bound = (s->mode_ext + 1) * 4;
01203 else
01204 bound = sblimit;
01205
01206 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
01207
01208
01209 if( bound > sblimit ) bound = sblimit;
01210
01211
01212 j = 0;
01213 for(i=0;i<bound;i++) {
01214 bit_alloc_bits = alloc_table[j];
01215 for(ch=0;ch<s->nb_channels;ch++) {
01216 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
01217 }
01218 j += 1 << bit_alloc_bits;
01219 }
01220 for(i=bound;i<sblimit;i++) {
01221 bit_alloc_bits = alloc_table[j];
01222 v = get_bits(&s->gb, bit_alloc_bits);
01223 bit_alloc[0][i] = v;
01224 bit_alloc[1][i] = v;
01225 j += 1 << bit_alloc_bits;
01226 }
01227
01228
01229 for(i=0;i<sblimit;i++) {
01230 for(ch=0;ch<s->nb_channels;ch++) {
01231 if (bit_alloc[ch][i])
01232 scale_code[ch][i] = get_bits(&s->gb, 2);
01233 }
01234 }
01235
01236
01237 for(i=0;i<sblimit;i++) {
01238 for(ch=0;ch<s->nb_channels;ch++) {
01239 if (bit_alloc[ch][i]) {
01240 sf = scale_factors[ch][i];
01241 switch(scale_code[ch][i]) {
01242 default:
01243 case 0:
01244 sf[0] = get_bits(&s->gb, 6);
01245 sf[1] = get_bits(&s->gb, 6);
01246 sf[2] = get_bits(&s->gb, 6);
01247 break;
01248 case 2:
01249 sf[0] = get_bits(&s->gb, 6);
01250 sf[1] = sf[0];
01251 sf[2] = sf[0];
01252 break;
01253 case 1:
01254 sf[0] = get_bits(&s->gb, 6);
01255 sf[2] = get_bits(&s->gb, 6);
01256 sf[1] = sf[0];
01257 break;
01258 case 3:
01259 sf[0] = get_bits(&s->gb, 6);
01260 sf[2] = get_bits(&s->gb, 6);
01261 sf[1] = sf[2];
01262 break;
01263 }
01264 }
01265 }
01266 }
01267
01268
01269 for(k=0;k<3;k++) {
01270 for(l=0;l<12;l+=3) {
01271 j = 0;
01272 for(i=0;i<bound;i++) {
01273 bit_alloc_bits = alloc_table[j];
01274 for(ch=0;ch<s->nb_channels;ch++) {
01275 b = bit_alloc[ch][i];
01276 if (b) {
01277 scale = scale_factors[ch][i][k];
01278 qindex = alloc_table[j+b];
01279 bits = ff_mpa_quant_bits[qindex];
01280 if (bits < 0) {
01281
01282 v = get_bits(&s->gb, -bits);
01283 steps = ff_mpa_quant_steps[qindex];
01284 s->sb_samples[ch][k * 12 + l + 0][i] =
01285 l2_unscale_group(steps, v % steps, scale);
01286 v = v / steps;
01287 s->sb_samples[ch][k * 12 + l + 1][i] =
01288 l2_unscale_group(steps, v % steps, scale);
01289 v = v / steps;
01290 s->sb_samples[ch][k * 12 + l + 2][i] =
01291 l2_unscale_group(steps, v, scale);
01292 } else {
01293 for(m=0;m<3;m++) {
01294 v = get_bits(&s->gb, bits);
01295 v = l1_unscale(bits - 1, v, scale);
01296 s->sb_samples[ch][k * 12 + l + m][i] = v;
01297 }
01298 }
01299 } else {
01300 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
01301 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
01302 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
01303 }
01304 }
01305
01306 j += 1 << bit_alloc_bits;
01307 }
01308
01309 for(i=bound;i<sblimit;i++) {
01310 bit_alloc_bits = alloc_table[j];
01311 b = bit_alloc[0][i];
01312 if (b) {
01313 int mant, scale0, scale1;
01314 scale0 = scale_factors[0][i][k];
01315 scale1 = scale_factors[1][i][k];
01316 qindex = alloc_table[j+b];
01317 bits = ff_mpa_quant_bits[qindex];
01318 if (bits < 0) {
01319
01320 v = get_bits(&s->gb, -bits);
01321 steps = ff_mpa_quant_steps[qindex];
01322 mant = v % steps;
01323 v = v / steps;
01324 s->sb_samples[0][k * 12 + l + 0][i] =
01325 l2_unscale_group(steps, mant, scale0);
01326 s->sb_samples[1][k * 12 + l + 0][i] =
01327 l2_unscale_group(steps, mant, scale1);
01328 mant = v % steps;
01329 v = v / steps;
01330 s->sb_samples[0][k * 12 + l + 1][i] =
01331 l2_unscale_group(steps, mant, scale0);
01332 s->sb_samples[1][k * 12 + l + 1][i] =
01333 l2_unscale_group(steps, mant, scale1);
01334 s->sb_samples[0][k * 12 + l + 2][i] =
01335 l2_unscale_group(steps, v, scale0);
01336 s->sb_samples[1][k * 12 + l + 2][i] =
01337 l2_unscale_group(steps, v, scale1);
01338 } else {
01339 for(m=0;m<3;m++) {
01340 mant = get_bits(&s->gb, bits);
01341 s->sb_samples[0][k * 12 + l + m][i] =
01342 l1_unscale(bits - 1, mant, scale0);
01343 s->sb_samples[1][k * 12 + l + m][i] =
01344 l1_unscale(bits - 1, mant, scale1);
01345 }
01346 }
01347 } else {
01348 s->sb_samples[0][k * 12 + l + 0][i] = 0;
01349 s->sb_samples[0][k * 12 + l + 1][i] = 0;
01350 s->sb_samples[0][k * 12 + l + 2][i] = 0;
01351 s->sb_samples[1][k * 12 + l + 0][i] = 0;
01352 s->sb_samples[1][k * 12 + l + 1][i] = 0;
01353 s->sb_samples[1][k * 12 + l + 2][i] = 0;
01354 }
01355
01356 j += 1 << bit_alloc_bits;
01357 }
01358
01359 for(i=sblimit;i<SBLIMIT;i++) {
01360 for(ch=0;ch<s->nb_channels;ch++) {
01361 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
01362 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
01363 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
01364 }
01365 }
01366 }
01367 }
01368 return 3 * 12;
01369 }
01370
01371 static inline void lsf_sf_expand(int *slen,
01372 int sf, int n1, int n2, int n3)
01373 {
01374 if (n3) {
01375 slen[3] = sf % n3;
01376 sf /= n3;
01377 } else {
01378 slen[3] = 0;
01379 }
01380 if (n2) {
01381 slen[2] = sf % n2;
01382 sf /= n2;
01383 } else {
01384 slen[2] = 0;
01385 }
01386 slen[1] = sf % n1;
01387 sf /= n1;
01388 slen[0] = sf;
01389 }
01390
01391 static void exponents_from_scale_factors(MPADecodeContext *s,
01392 GranuleDef *g,
01393 int16_t *exponents)
01394 {
01395 const uint8_t *bstab, *pretab;
01396 int len, i, j, k, l, v0, shift, gain, gains[3];
01397 int16_t *exp_ptr;
01398
01399 exp_ptr = exponents;
01400 gain = g->global_gain - 210;
01401 shift = g->scalefac_scale + 1;
01402
01403 bstab = band_size_long[s->sample_rate_index];
01404 pretab = mpa_pretab[g->preflag];
01405 for(i=0;i<g->long_end;i++) {
01406 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
01407 len = bstab[i];
01408 for(j=len;j>0;j--)
01409 *exp_ptr++ = v0;
01410 }
01411
01412 if (g->short_start < 13) {
01413 bstab = band_size_short[s->sample_rate_index];
01414 gains[0] = gain - (g->subblock_gain[0] << 3);
01415 gains[1] = gain - (g->subblock_gain[1] << 3);
01416 gains[2] = gain - (g->subblock_gain[2] << 3);
01417 k = g->long_end;
01418 for(i=g->short_start;i<13;i++) {
01419 len = bstab[i];
01420 for(l=0;l<3;l++) {
01421 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
01422 for(j=len;j>0;j--)
01423 *exp_ptr++ = v0;
01424 }
01425 }
01426 }
01427 }
01428
01429
01430 static inline int get_bitsz(GetBitContext *s, int n)
01431 {
01432 if (n == 0)
01433 return 0;
01434 else
01435 return get_bits(s, n);
01436 }
01437
01438
01439 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
01440 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
01441 s->gb= s->in_gb;
01442 s->in_gb.buffer=NULL;
01443 assert((get_bits_count(&s->gb) & 7) == 0);
01444 skip_bits_long(&s->gb, *pos - *end_pos);
01445 *end_pos2=
01446 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
01447 *pos= get_bits_count(&s->gb);
01448 }
01449 }
01450
01451 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
01452 int16_t *exponents, int end_pos2)
01453 {
01454 int s_index;
01455 int i;
01456 int last_pos, bits_left;
01457 VLC *vlc;
01458 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
01459
01460
01461 s_index = 0;
01462 for(i=0;i<3;i++) {
01463 int j, k, l, linbits;
01464 j = g->region_size[i];
01465 if (j == 0)
01466 continue;
01467
01468 k = g->table_select[i];
01469 l = mpa_huff_data[k][0];
01470 linbits = mpa_huff_data[k][1];
01471 vlc = &huff_vlc[l];
01472
01473 if(!l){
01474 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
01475 s_index += 2*j;
01476 continue;
01477 }
01478
01479
01480 for(;j>0;j--) {
01481 int exponent, x, y, v;
01482 int pos= get_bits_count(&s->gb);
01483
01484 if (pos >= end_pos){
01485
01486 switch_buffer(s, &pos, &end_pos, &end_pos2);
01487
01488 if(pos >= end_pos)
01489 break;
01490 }
01491 y = get_vlc2(&s->gb, vlc->table, 7, 3);
01492
01493 if(!y){
01494 g->sb_hybrid[s_index ] =
01495 g->sb_hybrid[s_index+1] = 0;
01496 s_index += 2;
01497 continue;
01498 }
01499
01500 exponent= exponents[s_index];
01501
01502 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
01503 i, g->region_size[i] - j, x, y, exponent);
01504 if(y&16){
01505 x = y >> 5;
01506 y = y & 0x0f;
01507 if (x < 15){
01508 v = expval_table[ exponent ][ x ];
01509
01510 }else{
01511 x += get_bitsz(&s->gb, linbits);
01512 v = l3_unscale(x, exponent);
01513 }
01514 if (get_bits1(&s->gb))
01515 v = -v;
01516 g->sb_hybrid[s_index] = v;
01517 if (y < 15){
01518 v = expval_table[ exponent ][ y ];
01519 }else{
01520 y += get_bitsz(&s->gb, linbits);
01521 v = l3_unscale(y, exponent);
01522 }
01523 if (get_bits1(&s->gb))
01524 v = -v;
01525 g->sb_hybrid[s_index+1] = v;
01526 }else{
01527 x = y >> 5;
01528 y = y & 0x0f;
01529 x += y;
01530 if (x < 15){
01531 v = expval_table[ exponent ][ x ];
01532 }else{
01533 x += get_bitsz(&s->gb, linbits);
01534 v = l3_unscale(x, exponent);
01535 }
01536 if (get_bits1(&s->gb))
01537 v = -v;
01538 g->sb_hybrid[s_index+!!y] = v;
01539 g->sb_hybrid[s_index+ !y] = 0;
01540 }
01541 s_index+=2;
01542 }
01543 }
01544
01545
01546 vlc = &huff_quad_vlc[g->count1table_select];
01547 last_pos=0;
01548 while (s_index <= 572) {
01549 int pos, code;
01550 pos = get_bits_count(&s->gb);
01551 if (pos >= end_pos) {
01552 if (pos > end_pos2 && last_pos){
01553
01554
01555 s_index -= 4;
01556 skip_bits_long(&s->gb, last_pos - pos);
01557 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
01558 if(s->error_recognition >= FF_ER_COMPLIANT)
01559 s_index=0;
01560 break;
01561 }
01562
01563 switch_buffer(s, &pos, &end_pos, &end_pos2);
01564
01565 if(pos >= end_pos)
01566 break;
01567 }
01568 last_pos= pos;
01569
01570 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
01571 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
01572 g->sb_hybrid[s_index+0]=
01573 g->sb_hybrid[s_index+1]=
01574 g->sb_hybrid[s_index+2]=
01575 g->sb_hybrid[s_index+3]= 0;
01576 while(code){
01577 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
01578 int v;
01579 int pos= s_index+idxtab[code];
01580 code ^= 8>>idxtab[code];
01581 v = exp_table[ exponents[pos] ];
01582
01583 if(get_bits1(&s->gb))
01584 v = -v;
01585 g->sb_hybrid[pos] = v;
01586 }
01587 s_index+=4;
01588 }
01589
01590 bits_left = end_pos2 - get_bits_count(&s->gb);
01591
01592 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
01593 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
01594 s_index=0;
01595 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
01596 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
01597 s_index=0;
01598 }
01599 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
01600 skip_bits_long(&s->gb, bits_left);
01601
01602 i= get_bits_count(&s->gb);
01603 switch_buffer(s, &i, &end_pos, &end_pos2);
01604
01605 return 0;
01606 }
01607
01608
01609
01610
01611 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
01612 {
01613 int i, j, len;
01614 int32_t *ptr, *dst, *ptr1;
01615 int32_t tmp[576];
01616
01617 if (g->block_type != 2)
01618 return;
01619
01620 if (g->switch_point) {
01621 if (s->sample_rate_index != 8) {
01622 ptr = g->sb_hybrid + 36;
01623 } else {
01624 ptr = g->sb_hybrid + 48;
01625 }
01626 } else {
01627 ptr = g->sb_hybrid;
01628 }
01629
01630 for(i=g->short_start;i<13;i++) {
01631 len = band_size_short[s->sample_rate_index][i];
01632 ptr1 = ptr;
01633 dst = tmp;
01634 for(j=len;j>0;j--) {
01635 *dst++ = ptr[0*len];
01636 *dst++ = ptr[1*len];
01637 *dst++ = ptr[2*len];
01638 ptr++;
01639 }
01640 ptr+=2*len;
01641 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
01642 }
01643 }
01644
01645 #define ISQRT2 FIXR(0.70710678118654752440)
01646
01647 static void compute_stereo(MPADecodeContext *s,
01648 GranuleDef *g0, GranuleDef *g1)
01649 {
01650 int i, j, k, l;
01651 int32_t v1, v2;
01652 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
01653 int32_t (*is_tab)[16];
01654 int32_t *tab0, *tab1;
01655 int non_zero_found_short[3];
01656
01657
01658 if (s->mode_ext & MODE_EXT_I_STEREO) {
01659 if (!s->lsf) {
01660 is_tab = is_table;
01661 sf_max = 7;
01662 } else {
01663 is_tab = is_table_lsf[g1->scalefac_compress & 1];
01664 sf_max = 16;
01665 }
01666
01667 tab0 = g0->sb_hybrid + 576;
01668 tab1 = g1->sb_hybrid + 576;
01669
01670 non_zero_found_short[0] = 0;
01671 non_zero_found_short[1] = 0;
01672 non_zero_found_short[2] = 0;
01673 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
01674 for(i = 12;i >= g1->short_start;i--) {
01675
01676 if (i != 11)
01677 k -= 3;
01678 len = band_size_short[s->sample_rate_index][i];
01679 for(l=2;l>=0;l--) {
01680 tab0 -= len;
01681 tab1 -= len;
01682 if (!non_zero_found_short[l]) {
01683
01684 for(j=0;j<len;j++) {
01685 if (tab1[j] != 0) {
01686 non_zero_found_short[l] = 1;
01687 goto found1;
01688 }
01689 }
01690 sf = g1->scale_factors[k + l];
01691 if (sf >= sf_max)
01692 goto found1;
01693
01694 v1 = is_tab[0][sf];
01695 v2 = is_tab[1][sf];
01696 for(j=0;j<len;j++) {
01697 tmp0 = tab0[j];
01698 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
01699 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
01700 }
01701 } else {
01702 found1:
01703 if (s->mode_ext & MODE_EXT_MS_STEREO) {
01704
01705
01706 for(j=0;j<len;j++) {
01707 tmp0 = tab0[j];
01708 tmp1 = tab1[j];
01709 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
01710 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
01711 }
01712 }
01713 }
01714 }
01715 }
01716
01717 non_zero_found = non_zero_found_short[0] |
01718 non_zero_found_short[1] |
01719 non_zero_found_short[2];
01720
01721 for(i = g1->long_end - 1;i >= 0;i--) {
01722 len = band_size_long[s->sample_rate_index][i];
01723 tab0 -= len;
01724 tab1 -= len;
01725
01726 if (!non_zero_found) {
01727 for(j=0;j<len;j++) {
01728 if (tab1[j] != 0) {
01729 non_zero_found = 1;
01730 goto found2;
01731 }
01732 }
01733
01734 k = (i == 21) ? 20 : i;
01735 sf = g1->scale_factors[k];
01736 if (sf >= sf_max)
01737 goto found2;
01738 v1 = is_tab[0][sf];
01739 v2 = is_tab[1][sf];
01740 for(j=0;j<len;j++) {
01741 tmp0 = tab0[j];
01742 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
01743 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
01744 }
01745 } else {
01746 found2:
01747 if (s->mode_ext & MODE_EXT_MS_STEREO) {
01748
01749
01750 for(j=0;j<len;j++) {
01751 tmp0 = tab0[j];
01752 tmp1 = tab1[j];
01753 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
01754 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
01755 }
01756 }
01757 }
01758 }
01759 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
01760
01761
01762
01763 tab0 = g0->sb_hybrid;
01764 tab1 = g1->sb_hybrid;
01765 for(i=0;i<576;i++) {
01766 tmp0 = tab0[i];
01767 tmp1 = tab1[i];
01768 tab0[i] = tmp0 + tmp1;
01769 tab1[i] = tmp0 - tmp1;
01770 }
01771 }
01772 }
01773
01774 static void compute_antialias_integer(MPADecodeContext *s,
01775 GranuleDef *g)
01776 {
01777 int32_t *ptr, *csa;
01778 int n, i;
01779
01780
01781 if (g->block_type == 2) {
01782 if (!g->switch_point)
01783 return;
01784
01785 n = 1;
01786 } else {
01787 n = SBLIMIT - 1;
01788 }
01789
01790 ptr = g->sb_hybrid + 18;
01791 for(i = n;i > 0;i--) {
01792 int tmp0, tmp1, tmp2;
01793 csa = &csa_table[0][0];
01794 #define INT_AA(j) \
01795 tmp0 = ptr[-1-j];\
01796 tmp1 = ptr[ j];\
01797 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
01798 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
01799 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
01800
01801 INT_AA(0)
01802 INT_AA(1)
01803 INT_AA(2)
01804 INT_AA(3)
01805 INT_AA(4)
01806 INT_AA(5)
01807 INT_AA(6)
01808 INT_AA(7)
01809
01810 ptr += 18;
01811 }
01812 }
01813
01814 static void compute_antialias_float(MPADecodeContext *s,
01815 GranuleDef *g)
01816 {
01817 int32_t *ptr;
01818 int n, i;
01819
01820
01821 if (g->block_type == 2) {
01822 if (!g->switch_point)
01823 return;
01824
01825 n = 1;
01826 } else {
01827 n = SBLIMIT - 1;
01828 }
01829
01830 ptr = g->sb_hybrid + 18;
01831 for(i = n;i > 0;i--) {
01832 float tmp0, tmp1;
01833 float *csa = &csa_table_float[0][0];
01834 #define FLOAT_AA(j)\
01835 tmp0= ptr[-1-j];\
01836 tmp1= ptr[ j];\
01837 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
01838 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
01839
01840 FLOAT_AA(0)
01841 FLOAT_AA(1)
01842 FLOAT_AA(2)
01843 FLOAT_AA(3)
01844 FLOAT_AA(4)
01845 FLOAT_AA(5)
01846 FLOAT_AA(6)
01847 FLOAT_AA(7)
01848
01849 ptr += 18;
01850 }
01851 }
01852
01853 static void compute_imdct(MPADecodeContext *s,
01854 GranuleDef *g,
01855 int32_t *sb_samples,
01856 int32_t *mdct_buf)
01857 {
01858 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
01859 int32_t out2[12];
01860 int i, j, mdct_long_end, v, sblimit;
01861
01862
01863 ptr = g->sb_hybrid + 576;
01864 ptr1 = g->sb_hybrid + 2 * 18;
01865 while (ptr >= ptr1) {
01866 ptr -= 6;
01867 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
01868 if (v != 0)
01869 break;
01870 }
01871 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
01872
01873 if (g->block_type == 2) {
01874
01875 if (g->switch_point)
01876 mdct_long_end = 2;
01877 else
01878 mdct_long_end = 0;
01879 } else {
01880 mdct_long_end = sblimit;
01881 }
01882
01883 buf = mdct_buf;
01884 ptr = g->sb_hybrid;
01885 for(j=0;j<mdct_long_end;j++) {
01886
01887 out_ptr = sb_samples + j;
01888
01889 if (g->switch_point && j < 2)
01890 win1 = mdct_win[0];
01891 else
01892 win1 = mdct_win[g->block_type];
01893
01894 win = win1 + ((4 * 36) & -(j & 1));
01895 imdct36(out_ptr, buf, ptr, win);
01896 out_ptr += 18*SBLIMIT;
01897 ptr += 18;
01898 buf += 18;
01899 }
01900 for(j=mdct_long_end;j<sblimit;j++) {
01901
01902 win = mdct_win[2] + ((4 * 36) & -(j & 1));
01903 out_ptr = sb_samples + j;
01904
01905 for(i=0; i<6; i++){
01906 *out_ptr = buf[i];
01907 out_ptr += SBLIMIT;
01908 }
01909 imdct12(out2, ptr + 0);
01910 for(i=0;i<6;i++) {
01911 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
01912 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
01913 out_ptr += SBLIMIT;
01914 }
01915 imdct12(out2, ptr + 1);
01916 for(i=0;i<6;i++) {
01917 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
01918 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
01919 out_ptr += SBLIMIT;
01920 }
01921 imdct12(out2, ptr + 2);
01922 for(i=0;i<6;i++) {
01923 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
01924 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
01925 buf[i + 6*2] = 0;
01926 }
01927 ptr += 18;
01928 buf += 18;
01929 }
01930
01931 for(j=sblimit;j<SBLIMIT;j++) {
01932
01933 out_ptr = sb_samples + j;
01934 for(i=0;i<18;i++) {
01935 *out_ptr = buf[i];
01936 buf[i] = 0;
01937 out_ptr += SBLIMIT;
01938 }
01939 buf += 18;
01940 }
01941 }
01942
01943
01944 static int mp_decode_layer3(MPADecodeContext *s)
01945 {
01946 int nb_granules, main_data_begin, private_bits;
01947 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
01948 GranuleDef granules[2][2], *g;
01949 int16_t exponents[576];
01950
01951
01952 if (s->lsf) {
01953 main_data_begin = get_bits(&s->gb, 8);
01954 private_bits = get_bits(&s->gb, s->nb_channels);
01955 nb_granules = 1;
01956 } else {
01957 main_data_begin = get_bits(&s->gb, 9);
01958 if (s->nb_channels == 2)
01959 private_bits = get_bits(&s->gb, 3);
01960 else
01961 private_bits = get_bits(&s->gb, 5);
01962 nb_granules = 2;
01963 for(ch=0;ch<s->nb_channels;ch++) {
01964 granules[ch][0].scfsi = 0;
01965 granules[ch][1].scfsi = get_bits(&s->gb, 4);
01966 }
01967 }
01968
01969 for(gr=0;gr<nb_granules;gr++) {
01970 for(ch=0;ch<s->nb_channels;ch++) {
01971 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
01972 g = &granules[ch][gr];
01973 g->part2_3_length = get_bits(&s->gb, 12);
01974 g->big_values = get_bits(&s->gb, 9);
01975 if(g->big_values > 288){
01976 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
01977 return -1;
01978 }
01979
01980 g->global_gain = get_bits(&s->gb, 8);
01981
01982
01983 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
01984 MODE_EXT_MS_STEREO)
01985 g->global_gain -= 2;
01986 if (s->lsf)
01987 g->scalefac_compress = get_bits(&s->gb, 9);
01988 else
01989 g->scalefac_compress = get_bits(&s->gb, 4);
01990 blocksplit_flag = get_bits1(&s->gb);
01991 if (blocksplit_flag) {
01992 g->block_type = get_bits(&s->gb, 2);
01993 if (g->block_type == 0){
01994 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
01995 return -1;
01996 }
01997 g->switch_point = get_bits1(&s->gb);
01998 for(i=0;i<2;i++)
01999 g->table_select[i] = get_bits(&s->gb, 5);
02000 for(i=0;i<3;i++)
02001 g->subblock_gain[i] = get_bits(&s->gb, 3);
02002 ff_init_short_region(s, g);
02003 } else {
02004 int region_address1, region_address2;
02005 g->block_type = 0;
02006 g->switch_point = 0;
02007 for(i=0;i<3;i++)
02008 g->table_select[i] = get_bits(&s->gb, 5);
02009
02010 region_address1 = get_bits(&s->gb, 4);
02011 region_address2 = get_bits(&s->gb, 3);
02012 dprintf(s->avctx, "region1=%d region2=%d\n",
02013 region_address1, region_address2);
02014 ff_init_long_region(s, g, region_address1, region_address2);
02015 }
02016 ff_region_offset2size(g);
02017 ff_compute_band_indexes(s, g);
02018
02019 g->preflag = 0;
02020 if (!s->lsf)
02021 g->preflag = get_bits1(&s->gb);
02022 g->scalefac_scale = get_bits1(&s->gb);
02023 g->count1table_select = get_bits1(&s->gb);
02024 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
02025 g->block_type, g->switch_point);
02026 }
02027 }
02028
02029 if (!s->adu_mode) {
02030 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
02031 assert((get_bits_count(&s->gb) & 7) == 0);
02032
02033 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
02034
02035
02036 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
02037 s->in_gb= s->gb;
02038 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
02039 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
02040 }
02041
02042 for(gr=0;gr<nb_granules;gr++) {
02043 for(ch=0;ch<s->nb_channels;ch++) {
02044 g = &granules[ch][gr];
02045 if(get_bits_count(&s->gb)<0){
02046 av_log(s->avctx, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n",
02047 main_data_begin, s->last_buf_size, gr);
02048 skip_bits_long(&s->gb, g->part2_3_length);
02049 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
02050 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
02051 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
02052 s->gb= s->in_gb;
02053 s->in_gb.buffer=NULL;
02054 }
02055 continue;
02056 }
02057
02058 bits_pos = get_bits_count(&s->gb);
02059
02060 if (!s->lsf) {
02061 uint8_t *sc;
02062 int slen, slen1, slen2;
02063
02064
02065 slen1 = slen_table[0][g->scalefac_compress];
02066 slen2 = slen_table[1][g->scalefac_compress];
02067 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
02068 if (g->block_type == 2) {
02069 n = g->switch_point ? 17 : 18;
02070 j = 0;
02071 if(slen1){
02072 for(i=0;i<n;i++)
02073 g->scale_factors[j++] = get_bits(&s->gb, slen1);
02074 }else{
02075 for(i=0;i<n;i++)
02076 g->scale_factors[j++] = 0;
02077 }
02078 if(slen2){
02079 for(i=0;i<18;i++)
02080 g->scale_factors[j++] = get_bits(&s->gb, slen2);
02081 for(i=0;i<3;i++)
02082 g->scale_factors[j++] = 0;
02083 }else{
02084 for(i=0;i<21;i++)
02085 g->scale_factors[j++] = 0;
02086 }
02087 } else {
02088 sc = granules[ch][0].scale_factors;
02089 j = 0;
02090 for(k=0;k<4;k++) {
02091 n = (k == 0 ? 6 : 5);
02092 if ((g->scfsi & (0x8 >> k)) == 0) {
02093 slen = (k < 2) ? slen1 : slen2;
02094 if(slen){
02095 for(i=0;i<n;i++)
02096 g->scale_factors[j++] = get_bits(&s->gb, slen);
02097 }else{
02098 for(i=0;i<n;i++)
02099 g->scale_factors[j++] = 0;
02100 }
02101 } else {
02102
02103 for(i=0;i<n;i++) {
02104 g->scale_factors[j] = sc[j];
02105 j++;
02106 }
02107 }
02108 }
02109 g->scale_factors[j++] = 0;
02110 }
02111 } else {
02112 int tindex, tindex2, slen[4], sl, sf;
02113
02114
02115 if (g->block_type == 2) {
02116 tindex = g->switch_point ? 2 : 1;
02117 } else {
02118 tindex = 0;
02119 }
02120 sf = g->scalefac_compress;
02121 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
02122
02123 sf >>= 1;
02124 if (sf < 180) {
02125 lsf_sf_expand(slen, sf, 6, 6, 0);
02126 tindex2 = 3;
02127 } else if (sf < 244) {
02128 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
02129 tindex2 = 4;
02130 } else {
02131 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
02132 tindex2 = 5;
02133 }
02134 } else {
02135
02136 if (sf < 400) {
02137 lsf_sf_expand(slen, sf, 5, 4, 4);
02138 tindex2 = 0;
02139 } else if (sf < 500) {
02140 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
02141 tindex2 = 1;
02142 } else {
02143 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
02144 tindex2 = 2;
02145 g->preflag = 1;
02146 }
02147 }
02148
02149 j = 0;
02150 for(k=0;k<4;k++) {
02151 n = lsf_nsf_table[tindex2][tindex][k];
02152 sl = slen[k];
02153 if(sl){
02154 for(i=0;i<n;i++)
02155 g->scale_factors[j++] = get_bits(&s->gb, sl);
02156 }else{
02157 for(i=0;i<n;i++)
02158 g->scale_factors[j++] = 0;
02159 }
02160 }
02161
02162 for(;j<40;j++)
02163 g->scale_factors[j] = 0;
02164 }
02165
02166 exponents_from_scale_factors(s, g, exponents);
02167
02168
02169 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
02170 }
02171
02172 if (s->nb_channels == 2)
02173 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
02174
02175 for(ch=0;ch<s->nb_channels;ch++) {
02176 g = &granules[ch][gr];
02177
02178 reorder_block(s, g);
02179 s->compute_antialias(s, g);
02180 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
02181 }
02182 }
02183 if(get_bits_count(&s->gb)<0)
02184 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
02185 return nb_granules * 18;
02186 }
02187
02188 static int mp_decode_frame(MPADecodeContext *s,
02189 OUT_INT *samples, const uint8_t *buf, int buf_size)
02190 {
02191 int i, nb_frames, ch;
02192 OUT_INT *samples_ptr;
02193
02194 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
02195
02196
02197 if (s->error_protection)
02198 skip_bits(&s->gb, 16);
02199
02200 dprintf(s->avctx, "frame %d:\n", s->frame_count);
02201 switch(s->layer) {
02202 case 1:
02203 s->avctx->frame_size = 384;
02204 nb_frames = mp_decode_layer1(s);
02205 break;
02206 case 2:
02207 s->avctx->frame_size = 1152;
02208 nb_frames = mp_decode_layer2(s);
02209 break;
02210 case 3:
02211 s->avctx->frame_size = s->lsf ? 576 : 1152;
02212 default:
02213 nb_frames = mp_decode_layer3(s);
02214
02215 s->last_buf_size=0;
02216 if(s->in_gb.buffer){
02217 align_get_bits(&s->gb);
02218 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
02219 if(i >= 0 && i <= BACKSTEP_SIZE){
02220 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
02221 s->last_buf_size=i;
02222 }else
02223 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
02224 s->gb= s->in_gb;
02225 s->in_gb.buffer= NULL;
02226 }
02227
02228 align_get_bits(&s->gb);
02229 assert((get_bits_count(&s->gb) & 7) == 0);
02230 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
02231
02232 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
02233 if(i<0)
02234 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
02235 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
02236 }
02237 assert(i <= buf_size - HEADER_SIZE && i>= 0);
02238 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
02239 s->last_buf_size += i;
02240
02241 break;
02242 }
02243
02244
02245 for(ch=0;ch<s->nb_channels;ch++) {
02246 samples_ptr = samples + ch;
02247 for(i=0;i<nb_frames;i++) {
02248 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
02249 window, &s->dither_state,
02250 samples_ptr, s->nb_channels,
02251 s->sb_samples[ch][i]);
02252 samples_ptr += 32 * s->nb_channels;
02253 }
02254 }
02255
02256 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
02257 }
02258
02259 static int decode_frame(AVCodecContext * avctx,
02260 void *data, int *data_size,
02261 const uint8_t * buf, int buf_size)
02262 {
02263 MPADecodeContext *s = avctx->priv_data;
02264 uint32_t header;
02265 int out_size;
02266 OUT_INT *out_samples = data;
02267
02268 retry:
02269 if(buf_size < HEADER_SIZE)
02270 return -1;
02271
02272 header = AV_RB32(buf);
02273 if(ff_mpa_check_header(header) < 0){
02274 buf++;
02275
02276 av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
02277 goto retry;
02278 }
02279
02280 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
02281
02282 s->frame_size = -1;
02283 return -1;
02284 }
02285
02286 avctx->channels = s->nb_channels;
02287 avctx->bit_rate = s->bit_rate;
02288 avctx->sub_id = s->layer;
02289
02290 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
02291 return -1;
02292 *data_size = 0;
02293
02294 if(s->frame_size<=0 || s->frame_size > buf_size){
02295 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
02296 return -1;
02297 }else if(s->frame_size < buf_size){
02298 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
02299 buf_size= s->frame_size;
02300 }
02301
02302 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
02303 if(out_size>=0){
02304 *data_size = out_size;
02305 avctx->sample_rate = s->sample_rate;
02306
02307 }else
02308 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n");
02309 s->frame_size = 0;
02310 return buf_size;
02311 }
02312
02313 static void flush(AVCodecContext *avctx){
02314 MPADecodeContext *s = avctx->priv_data;
02315 memset(s->synth_buf, 0, sizeof(s->synth_buf));
02316 s->last_buf_size= 0;
02317 }
02318
02319 #if CONFIG_MP3ADU_DECODER
02320 static int decode_frame_adu(AVCodecContext * avctx,
02321 void *data, int *data_size,
02322 const uint8_t * buf, int buf_size)
02323 {
02324 MPADecodeContext *s = avctx->priv_data;
02325 uint32_t header;
02326 int len, out_size;
02327 OUT_INT *out_samples = data;
02328
02329 len = buf_size;
02330
02331
02332 if (buf_size < HEADER_SIZE) {
02333 *data_size = 0;
02334 return buf_size;
02335 }
02336
02337
02338 if (len > MPA_MAX_CODED_FRAME_SIZE)
02339 len = MPA_MAX_CODED_FRAME_SIZE;
02340
02341
02342 header = AV_RB32(buf) | 0xffe00000;
02343
02344 if (ff_mpa_check_header(header) < 0) {
02345 *data_size = 0;
02346 return buf_size;
02347 }
02348
02349 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
02350
02351 avctx->sample_rate = s->sample_rate;
02352 avctx->channels = s->nb_channels;
02353 avctx->bit_rate = s->bit_rate;
02354 avctx->sub_id = s->layer;
02355
02356 s->frame_size = len;
02357
02358 if (avctx->parse_only) {
02359 out_size = buf_size;
02360 } else {
02361 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
02362 }
02363
02364 *data_size = out_size;
02365 return buf_size;
02366 }
02367 #endif
02368
02369 #if CONFIG_MP3ON4_DECODER
02370
02374 typedef struct MP3On4DecodeContext {
02375 int frames;
02376 int syncword;
02377 const uint8_t *coff;
02378 MPADecodeContext *mp3decctx[5];
02379 } MP3On4DecodeContext;
02380
02381 #include "mpeg4audio.h"
02382
02383
02384 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5};
02385
02386 static const uint8_t chan_offset[8][5] = {
02387 {0},
02388 {0},
02389 {0},
02390 {2,0},
02391 {2,0,3},
02392 {4,0,2},
02393 {4,0,2,5},
02394 {4,0,2,6,5},
02395 };
02396
02397
02398 static int decode_init_mp3on4(AVCodecContext * avctx)
02399 {
02400 MP3On4DecodeContext *s = avctx->priv_data;
02401 MPEG4AudioConfig cfg;
02402 int i;
02403
02404 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
02405 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
02406 return -1;
02407 }
02408
02409 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
02410 if (!cfg.chan_config || cfg.chan_config > 7) {
02411 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
02412 return -1;
02413 }
02414 s->frames = mp3Frames[cfg.chan_config];
02415 s->coff = chan_offset[cfg.chan_config];
02416 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
02417
02418 if (cfg.sample_rate < 16000)
02419 s->syncword = 0xffe00000;
02420 else
02421 s->syncword = 0xfff00000;
02422
02423
02424
02425
02426
02427
02428
02429 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
02430
02431 avctx->priv_data = s->mp3decctx[0];
02432 decode_init(avctx);
02433
02434 avctx->priv_data = s;
02435 s->mp3decctx[0]->adu_mode = 1;
02436
02437
02438
02439
02440 for (i = 1; i < s->frames; i++) {
02441 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
02442 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
02443 s->mp3decctx[i]->adu_mode = 1;
02444 s->mp3decctx[i]->avctx = avctx;
02445 }
02446
02447 return 0;
02448 }
02449
02450
02451 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
02452 {
02453 MP3On4DecodeContext *s = avctx->priv_data;
02454 int i;
02455
02456 for (i = 0; i < s->frames; i++)
02457 if (s->mp3decctx[i])
02458 av_free(s->mp3decctx[i]);
02459
02460 return 0;
02461 }
02462
02463
02464 static int decode_frame_mp3on4(AVCodecContext * avctx,
02465 void *data, int *data_size,
02466 const uint8_t * buf, int buf_size)
02467 {
02468 MP3On4DecodeContext *s = avctx->priv_data;
02469 MPADecodeContext *m;
02470 int fsize, len = buf_size, out_size = 0;
02471 uint32_t header;
02472 OUT_INT *out_samples = data;
02473 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
02474 OUT_INT *outptr, *bp;
02475 int fr, j, n;
02476
02477 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
02478 return -1;
02479
02480 *data_size = 0;
02481
02482 if (buf_size < HEADER_SIZE)
02483 return -1;
02484
02485
02486 outptr = s->frames == 1 ? out_samples : decoded_buf;
02487
02488 avctx->bit_rate = 0;
02489
02490 for (fr = 0; fr < s->frames; fr++) {
02491 fsize = AV_RB16(buf) >> 4;
02492 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
02493 m = s->mp3decctx[fr];
02494 assert (m != NULL);
02495
02496 header = (AV_RB32(buf) & 0x000fffff) | s->syncword;
02497
02498 if (ff_mpa_check_header(header) < 0)
02499 break;
02500
02501 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
02502 out_size += mp_decode_frame(m, outptr, buf, fsize);
02503 buf += fsize;
02504 len -= fsize;
02505
02506 if(s->frames > 1) {
02507 n = m->avctx->frame_size*m->nb_channels;
02508
02509 bp = out_samples + s->coff[fr];
02510 if(m->nb_channels == 1) {
02511 for(j = 0; j < n; j++) {
02512 *bp = decoded_buf[j];
02513 bp += avctx->channels;
02514 }
02515 } else {
02516 for(j = 0; j < n; j++) {
02517 bp[0] = decoded_buf[j++];
02518 bp[1] = decoded_buf[j];
02519 bp += avctx->channels;
02520 }
02521 }
02522 }
02523 avctx->bit_rate += m->bit_rate;
02524 }
02525
02526
02527 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
02528
02529 *data_size = out_size;
02530 return buf_size;
02531 }
02532 #endif
02533
02534 #if CONFIG_MP1_DECODER
02535 AVCodec mp1_decoder =
02536 {
02537 "mp1",
02538 CODEC_TYPE_AUDIO,
02539 CODEC_ID_MP1,
02540 sizeof(MPADecodeContext),
02541 decode_init,
02542 NULL,
02543 NULL,
02544 decode_frame,
02545 CODEC_CAP_PARSE_ONLY,
02546 .flush= flush,
02547 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
02548 };
02549 #endif
02550 #if CONFIG_MP2_DECODER
02551 AVCodec mp2_decoder =
02552 {
02553 "mp2",
02554 CODEC_TYPE_AUDIO,
02555 CODEC_ID_MP2,
02556 sizeof(MPADecodeContext),
02557 decode_init,
02558 NULL,
02559 NULL,
02560 decode_frame,
02561 CODEC_CAP_PARSE_ONLY,
02562 .flush= flush,
02563 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
02564 };
02565 #endif
02566 #if CONFIG_MP3_DECODER
02567 AVCodec mp3_decoder =
02568 {
02569 "mp3",
02570 CODEC_TYPE_AUDIO,
02571 CODEC_ID_MP3,
02572 sizeof(MPADecodeContext),
02573 decode_init,
02574 NULL,
02575 NULL,
02576 decode_frame,
02577 CODEC_CAP_PARSE_ONLY,
02578 .flush= flush,
02579 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
02580 };
02581 #endif
02582 #if CONFIG_MP3ADU_DECODER
02583 AVCodec mp3adu_decoder =
02584 {
02585 "mp3adu",
02586 CODEC_TYPE_AUDIO,
02587 CODEC_ID_MP3ADU,
02588 sizeof(MPADecodeContext),
02589 decode_init,
02590 NULL,
02591 NULL,
02592 decode_frame_adu,
02593 CODEC_CAP_PARSE_ONLY,
02594 .flush= flush,
02595 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
02596 };
02597 #endif
02598 #if CONFIG_MP3ON4_DECODER
02599 AVCodec mp3on4_decoder =
02600 {
02601 "mp3on4",
02602 CODEC_TYPE_AUDIO,
02603 CODEC_ID_MP3ON4,
02604 sizeof(MP3On4DecodeContext),
02605 decode_init_mp3on4,
02606 NULL,
02607 decode_close_mp3on4,
02608 decode_frame_mp3on4,
02609 .flush= flush,
02610 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
02611 };
02612 #endif