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