1 /* 2 layer3.c: the layer 3 decoder 3 4 copyright 1995-2017 by the mpg123 project - free software under the terms of the LGPL 2.1 5 see COPYING and AUTHORS files in distribution or http://mpg123.org 6 initially written by Michael Hipp 7 8 Dear visitor: 9 If you feel you don't understand fully the works of this file, your feeling might be correct. 10 11 Optimize-TODO: put short bands into the band-field without the stride of 3 reals 12 Length-optimze: unify long and short band code where it is possible 13 14 The int-vs-pointer situation has to be cleaned up. 15 */ 16 17 #include "mpg123lib_intern.h" 18 #ifdef USE_NEW_HUFFTABLE 19 #include "newhuffman.h" 20 #else 21 #include "huffman.h" 22 #endif 23 #include "getbits.h" 24 #include "debug.h" 25 26 27 28 /* define CUT_SFB21 if you want to cut-off the frequency above 16kHz */ 29 #if 0 30 #define CUT_SFB21 31 #endif 32 33 #ifdef REAL_IS_FIXED 34 #define NEW_DCT9 35 #include "l3_integer_tables.h" 36 #else 37 /* static one-time calculated tables... or so */ 38 static real ispow[8207]; 39 static real aa_ca[8],aa_cs[8]; 40 static ALIGNED(16) real win[4][36]; 41 static ALIGNED(16) real win1[4][36]; 42 real COS9[9]; /* dct36_3dnow wants to use that */ 43 static real COS6_1,COS6_2; 44 real tfcos36[9]; /* dct36_3dnow wants to use that */ 45 static real tfcos12[3]; 46 #define NEW_DCT9 47 #ifdef NEW_DCT9 48 static real cos9[3],cos18[3]; 49 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16]; 50 static real pow1_1[2][32],pow2_1[2][32],pow1_2[2][32],pow2_2[2][32]; 51 #endif 52 #endif 53 54 /* Decoder state data, living on the stack of do_layer3. */ 55 56 struct gr_info_s 57 { 58 int scfsi; 59 unsigned part2_3_length; 60 unsigned big_values; 61 unsigned scalefac_compress; 62 unsigned block_type; 63 unsigned mixed_block_flag; 64 unsigned table_select[3]; 65 /* Making those two signed int as workaround for open64/pathscale/sun compilers, and also for consistency, since they're worked on together with other signed variables. */ 66 int maxband[3]; 67 int maxbandl; 68 unsigned maxb; 69 unsigned region1start; 70 unsigned region2start; 71 unsigned preflag; 72 unsigned scalefac_scale; 73 unsigned count1table_select; 74 real *full_gain[3]; 75 real *pow2gain; 76 }; 77 78 struct III_sideinfo 79 { 80 unsigned main_data_begin; 81 unsigned private_bits; 82 /* Hm, funny... struct inside struct... */ 83 struct { struct gr_info_s gr[2]; } ch[2]; 84 }; 85 86 struct bandInfoStruct 87 { 88 unsigned short longIdx[23]; 89 unsigned char longDiff[22]; 90 unsigned short shortIdx[14]; 91 unsigned char shortDiff[13]; 92 }; 93 94 /* Techy details about our friendly MPEG data. Fairly constant over the years;-) */ 95 static const struct bandInfoStruct bandInfo[9] = 96 { 97 { /* MPEG 1.0 */ 98 {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576}, 99 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158}, 100 {0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3}, 101 {4,4,4,4,6,8,10,12,14,18,22,30,56} 102 }, 103 { 104 {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576}, 105 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192}, 106 {0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3}, 107 {4,4,4,4,6,6,10,12,14,16,20,26,66} 108 }, 109 { 110 {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576}, 111 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26}, 112 {0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3}, 113 {4,4,4,4,6,8,12,16,20,26,34,42,12} 114 }, 115 { /* MPEG 2.0 */ 116 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 117 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } , 118 {0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} , 119 {4,4,4,6,6,8,10,14,18,26,32,42,18 } 120 }, 121 { /* Twiddling 3 values here (not just 330->332!) fixed bug 1895025. */ 122 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576}, 123 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 }, 124 {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3}, 125 {4,4,4,6,8,10,12,14,18,24,32,44,12 } 126 }, 127 { 128 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 129 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 }, 130 {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3}, 131 {4,4,4,6,8,10,12,14,18,24,30,40,18 } 132 }, 133 { /* MPEG 2.5 */ 134 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 135 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, 136 {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, 137 {4,4,4,6,8,10,12,14,18,24,30,40,18} 138 }, 139 { 140 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 141 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, 142 {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, 143 {4,4,4,6,8,10,12,14,18,24,30,40,18} 144 }, 145 { 146 {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576}, 147 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2}, 148 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576}, 149 {8,8,8,12,16,20,24,28,36,2,2,2,26} 150 } 151 }; 152 153 static int mapbuf0[9][152]; 154 static int mapbuf1[9][156]; 155 static int mapbuf2[9][44]; 156 static int *map[9][3]; 157 static int *mapend[9][3]; 158 159 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */ 160 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */ 161 162 /* Some helpers used in init_layer3 */ 163 164 #ifdef OPT_MMXORSSE 165 real init_layer3_gainpow2_mmx(mpg123_handle *fr, int i) 166 { 167 if(!fr->p.down_sample) return DOUBLE_TO_REAL(16384.0 * pow((double)2.0,-0.25 * (double) (i+210) )); 168 else return DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210))); 169 } 170 #endif 171 172 real init_layer3_gainpow2(mpg123_handle *fr, int i) 173 { 174 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES) 175 return gainpow2[i+256]; 176 #else 177 return DOUBLE_TO_REAL_SCALE_LAYER3(pow((double)2.0,-0.25 * (double) (i+210)),i+256); 178 #endif 179 } 180 181 182 /* init tables for layer-3 ... specific with the downsampling... */ 183 void init_layer3(void) 184 { 185 int i,j,k,l; 186 187 #if !defined(REAL_IS_FIXED) || !defined(PRECALC_TABLES) 188 for(i=0;i<8207;i++) 189 ispow[i] = DOUBLE_TO_REAL_POW43(pow((double)i,(double)4.0/3.0)); 190 191 for(i=0;i<8;i++) 192 { 193 const double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037}; 194 double sq = sqrt(1.0+Ci[i]*Ci[i]); 195 aa_cs[i] = DOUBLE_TO_REAL(1.0/sq); 196 aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq); 197 } 198 199 for(i=0;i<18;i++) 200 { 201 win[0][i] = win[1][i] = 202 DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+0) +1)) / cos(M_PI * (double)(2*(i+0) +19) / 72.0) ); 203 win[0][i+18] = win[3][i+18] = 204 DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+18)+1)) / cos(M_PI * (double)(2*(i+18)+19) / 72.0) ); 205 } 206 for(i=0;i<6;i++) 207 { 208 win[1][i+18] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 )); 209 win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 )); 210 win[1][i+24] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 )); 211 win[1][i+30] = win[3][i] = DOUBLE_TO_REAL(0.0); 212 win[3][i+6 ] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1 ) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 )); 213 } 214 215 for(i=0;i<9;i++) 216 COS9[i] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i)); 217 218 for(i=0;i<9;i++) 219 tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 )); 220 221 for(i=0;i<3;i++) 222 tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 )); 223 224 COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1)); 225 COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2)); 226 227 #ifdef NEW_DCT9 228 cos9[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0)); 229 cos9[1] = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0)); 230 cos9[2] = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0)); 231 cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0)); 232 cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0)); 233 cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0)); 234 #endif 235 236 for(i=0;i<12;i++) 237 { 238 win[2][i] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 )); 239 } 240 241 for(i=0;i<16;i++) 242 { 243 double t = tan( (double) i * M_PI / 12.0 ); 244 tan1_1[i] = DOUBLE_TO_REAL_15(t / (1.0+t)); 245 tan2_1[i] = DOUBLE_TO_REAL_15(1.0 / (1.0 + t)); 246 tan1_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 * t / (1.0+t)); 247 tan2_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 / (1.0 + t)); 248 } 249 250 for(i=0;i<32;i++) 251 { 252 for(j=0;j<2;j++) 253 { 254 double base = pow(2.0,-0.25*(j+1.0)); 255 double p1=1.0,p2=1.0; 256 if(i > 0) 257 { 258 if( i & 1 ) p1 = pow(base,(i+1.0)*0.5); 259 else p2 = pow(base,i*0.5); 260 } 261 pow1_1[j][i] = DOUBLE_TO_REAL_15(p1); 262 pow2_1[j][i] = DOUBLE_TO_REAL_15(p2); 263 pow1_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p1); 264 pow2_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p2); 265 } 266 } 267 #endif 268 269 for(j=0;j<4;j++) 270 { 271 const int len[4] = { 36,36,12,36 }; 272 for(i=0;i<len[j];i+=2) win1[j][i] = + win[j][i]; 273 274 for(i=1;i<len[j];i+=2) win1[j][i] = - win[j][i]; 275 } 276 277 for(j=0;j<9;j++) 278 { 279 const struct bandInfoStruct *bi = &bandInfo[j]; 280 int *mp; 281 int cb,lwin; 282 const unsigned char *bdf; 283 int switch_idx; 284 285 mp = map[j][0] = mapbuf0[j]; 286 bdf = bi->longDiff; 287 switch_idx = (j < 3) ? 8 : 6; 288 for(i=0,cb = 0; cb < switch_idx ; cb++,i+=*bdf++) 289 { 290 *mp++ = (*bdf) >> 1; 291 *mp++ = i; 292 *mp++ = 3; 293 *mp++ = cb; 294 } 295 bdf = bi->shortDiff+3; 296 for(cb=3;cb<13;cb++) 297 { 298 int l = (*bdf++) >> 1; 299 for(lwin=0;lwin<3;lwin++) 300 { 301 *mp++ = l; 302 *mp++ = i + lwin; 303 *mp++ = lwin; 304 *mp++ = cb; 305 } 306 i += 6*l; 307 } 308 mapend[j][0] = mp; 309 310 mp = map[j][1] = mapbuf1[j]; 311 bdf = bi->shortDiff+0; 312 for(i=0,cb=0;cb<13;cb++) 313 { 314 int l = (*bdf++) >> 1; 315 for(lwin=0;lwin<3;lwin++) 316 { 317 *mp++ = l; 318 *mp++ = i + lwin; 319 *mp++ = lwin; 320 *mp++ = cb; 321 } 322 i += 6*l; 323 } 324 mapend[j][1] = mp; 325 326 mp = map[j][2] = mapbuf2[j]; 327 bdf = bi->longDiff; 328 for(cb = 0; cb < 22 ; cb++) 329 { 330 *mp++ = (*bdf++) >> 1; 331 *mp++ = cb; 332 } 333 mapend[j][2] = mp; 334 } 335 336 /* Now for some serious loopings! */ 337 for(i=0;i<5;i++) 338 for(j=0;j<6;j++) 339 for(k=0;k<6;k++) 340 { 341 int n = k + j * 6 + i * 36; 342 i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12); 343 } 344 for(i=0;i<4;i++) 345 for(j=0;j<4;j++) 346 for(k=0;k<4;k++) 347 { 348 int n = k + j * 4 + i * 16; 349 i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12); 350 } 351 for(i=0;i<4;i++) 352 for(j=0;j<3;j++) 353 { 354 int n = j + i * 3; 355 i_slen2[n+244] = i|(j<<3) | (5<<12); 356 n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15); 357 } 358 for(i=0;i<5;i++) 359 for(j=0;j<5;j++) 360 for(k=0;k<4;k++) 361 for(l=0;l<4;l++) 362 { 363 int n = l + k * 4 + j * 16 + i * 80; 364 n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12); 365 } 366 for(i=0;i<5;i++) 367 for(j=0;j<5;j++) 368 for(k=0;k<4;k++) 369 { 370 int n = k + j * 4 + i * 20; 371 n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12); 372 } 373 } 374 375 376 void init_layer3_stuff(mpg123_handle *fr, real (*gainpow2)(mpg123_handle *fr, int i)) 377 { 378 int i,j; 379 380 for(i=-256;i<118+4;i++) fr->gainpow2[i+256] = gainpow2(fr,i); 381 382 for(j=0;j<9;j++) 383 { 384 for(i=0;i<23;i++) 385 { 386 fr->longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1; 387 if(fr->longLimit[j][i] > (fr->down_sample_sblimit) ) 388 fr->longLimit[j][i] = fr->down_sample_sblimit; 389 } 390 for(i=0;i<14;i++) 391 { 392 fr->shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1; 393 if(fr->shortLimit[j][i] > (fr->down_sample_sblimit) ) 394 fr->shortLimit[j][i] = fr->down_sample_sblimit; 395 } 396 } 397 } 398 399 /* 400 Observe! 401 Now come the actualy decoding routines. 402 */ 403 404 /* read additional side information (for MPEG 1 and MPEG 2) */ 405 static int III_get_side_info(mpg123_handle *fr, struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single) 406 { 407 int ch, gr; 408 int powdiff = (single == SINGLE_MIX) ? 4 : 0; 409 410 const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } }; 411 const int *tab = tabs[fr->lsf]; 412 413 { /* First ensure we got enough bits available. */ 414 unsigned int needbits = 0; 415 needbits += tab[1]; /* main_data_begin */ 416 needbits += stereo == 1 ? tab[2] : tab[3]; /* private */ 417 if(!fr->lsf) 418 needbits += stereo*4; /* scfsi */ 419 /* For each granule for each channel ... */ 420 needbits += tab[0]*stereo*(29+tab[4]+1+22+(!fr->lsf?1:0)+2); 421 if(fr->bits_avail < needbits) \ 422 { 423 if(NOQUIET) 424 error2( "%u bits for side info needed, only %li available" 425 , needbits, fr->bits_avail ); 426 return 1; 427 } 428 } 429 430 si->main_data_begin = getbits(fr, tab[1]); 431 432 if(si->main_data_begin > fr->bitreservoir) 433 { 434 if(!fr->to_ignore && VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num); 435 436 /* overwrite main_data_begin for the really available bit reservoir */ 437 backbits(fr, tab[1]); 438 if(fr->lsf == 0) 439 { 440 fr->wordpointer[0] = (unsigned char) (fr->bitreservoir >> 1); 441 fr->wordpointer[1] = (unsigned char) ((fr->bitreservoir & 1) << 7); 442 } 443 else fr->wordpointer[0] = (unsigned char) fr->bitreservoir; 444 445 /* zero "side-info" data for a silence-frame 446 without touching audio data used as bit reservoir for following frame */ 447 memset(fr->wordpointer+2, 0, fr->ssize-2); 448 449 /* reread the new bit reservoir offset */ 450 si->main_data_begin = getbits(fr, tab[1]); 451 } 452 453 /* Keep track of the available data bytes for the bit reservoir. 454 CRC is included in ssize already. */ 455 fr->bitreservoir = fr->bitreservoir + fr->framesize - fr->ssize; 456 457 /* Limit the reservoir to the max for MPEG 1.0 or 2.x . */ 458 if(fr->bitreservoir > (unsigned int) (fr->lsf == 0 ? 511 : 255)) 459 fr->bitreservoir = (fr->lsf == 0 ? 511 : 255); 460 461 /* Now back into less commented territory. It's code. It works. */ 462 463 if (stereo == 1) 464 si->private_bits = getbits(fr, tab[2]); 465 else 466 si->private_bits = getbits(fr, tab[3]); 467 468 if(!fr->lsf) for(ch=0; ch<stereo; ch++) 469 { 470 si->ch[ch].gr[0].scfsi = -1; 471 si->ch[ch].gr[1].scfsi = getbits(fr, 4); 472 } 473 474 for (gr=0; gr<tab[0]; gr++) 475 for (ch=0; ch<stereo; ch++) 476 { 477 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]); 478 unsigned int qss; 479 gr_info->part2_3_length = getbits(fr, 12); 480 gr_info->big_values = getbits(fr, 9); 481 if(gr_info->big_values > 288) 482 { 483 if(NOQUIET) error("big_values too large!"); 484 gr_info->big_values = 288; 485 } 486 qss = getbits_fast(fr, 8); 487 gr_info->pow2gain = fr->gainpow2+256 - qss + powdiff; 488 if(ms_stereo) 489 gr_info->pow2gain += 2; 490 #ifndef NO_MOREINFO 491 if(fr->pinfo) 492 fr->pinfo->qss[gr][ch] = qss; 493 #endif 494 gr_info->scalefac_compress = getbits(fr, tab[4]); 495 if(gr_info->part2_3_length == 0) 496 { 497 if(gr_info->scalefac_compress > 0 && VERBOSE2) 498 error1( "scalefac_compress should be zero instead of %i" 499 , gr_info->scalefac_compress ); 500 gr_info->scalefac_compress = 0; 501 } 502 503 /* 22 bits for if/else block */ 504 if(getbits(fr,1)) 505 { /* window switch flag */ 506 int i; 507 gr_info->block_type = getbits_fast(fr, 2); 508 gr_info->mixed_block_flag = get1bit(fr); 509 gr_info->table_select[0] = getbits_fast(fr, 5); 510 gr_info->table_select[1] = getbits_fast(fr, 5); 511 /* 512 table_select[2] not needed, because there is no region2, 513 but to satisfy some verification tools we set it either. 514 */ 515 gr_info->table_select[2] = 0; 516 for(i=0;i<3;i++) 517 { 518 unsigned int sbg = (getbits_fast(fr, 3)<<3); 519 gr_info->full_gain[i] = gr_info->pow2gain + sbg; 520 #ifndef NO_MOREINFO 521 if(fr->pinfo) 522 fr->pinfo->sub_gain[gr][ch][i] = sbg / 8; 523 #endif 524 } 525 526 if(gr_info->block_type == 0) 527 { 528 if(NOQUIET) error("Blocktype == 0 and window-switching == 1 not allowed."); 529 return 1; 530 } 531 532 /* region_count/start parameters are implicit in this case. */ 533 if( (!fr->lsf || (gr_info->block_type == 2)) && !fr->mpeg25) 534 { 535 gr_info->region1start = 36>>1; 536 gr_info->region2start = 576>>1; 537 } 538 else 539 { 540 if(fr->mpeg25) 541 { 542 int r0c,r1c; 543 if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5; 544 else r0c = 7; 545 546 /* r0c+1+r1c+1 == 22, always. */ 547 r1c = 20 - r0c; 548 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; 549 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 550 } 551 else 552 { 553 gr_info->region1start = 54>>1; 554 gr_info->region2start = 576>>1; 555 } 556 } 557 } 558 else 559 { 560 int i,r0c,r1c; 561 for (i=0; i<3; i++) 562 gr_info->table_select[i] = getbits_fast(fr, 5); 563 564 r0c = getbits_fast(fr, 4); /* 0 .. 15 */ 565 r1c = getbits_fast(fr, 3); /* 0 .. 7 */ 566 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; 567 568 /* max(r0c+r1c+2) = 15+7+2 = 24 */ 569 if(r0c+1+r1c+1 > 22) gr_info->region2start = 576>>1; 570 else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 571 572 gr_info->block_type = 0; 573 gr_info->mixed_block_flag = 0; 574 } 575 if(!fr->lsf) gr_info->preflag = get1bit(fr); 576 577 gr_info->scalefac_scale = get1bit(fr); 578 gr_info->count1table_select = get1bit(fr); 579 } 580 return 0; 581 } 582 583 584 /* read scalefactors */ 585 static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr) 586 { 587 const unsigned char slen[2][16] = 588 { 589 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4}, 590 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3} 591 }; 592 int numbits; 593 int num0 = slen[0][gr_info->scalefac_compress]; 594 int num1 = slen[1][gr_info->scalefac_compress]; 595 596 if(gr_info->part2_3_length == 0) 597 { 598 int i; 599 for(i=0;i<39;i++) 600 *scf++ = 0; 601 return 0; 602 } 603 604 if(gr_info->block_type == 2) 605 { 606 int i=18; 607 numbits = (num0 + num1) * 18 /* num0 * (17+1?) + num1 * 18 */ 608 - (gr_info->mixed_block_flag ? num0 : 0); 609 if(numbits > gr_info->part2_3_length) 610 return -1; 611 612 if(gr_info->mixed_block_flag) 613 { 614 for (i=8;i;i--) 615 *scf++ = getbits_fast(fr, num0); 616 617 i = 9; 618 } 619 620 for(;i;i--) *scf++ = getbits_fast(fr, num0); 621 622 for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1); 623 624 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */ 625 } 626 else 627 { 628 int i; 629 int scfsi = gr_info->scfsi; 630 631 if(scfsi < 0) 632 { /* scfsi < 0 => granule == 0 */ 633 numbits = (num0 + num1) * 10 + num0; 634 if(numbits > gr_info->part2_3_length) 635 return -1; 636 637 for(i=11;i;i--) *scf++ = getbits_fast(fr, num0); 638 639 for(i=10;i;i--) *scf++ = getbits_fast(fr, num1); 640 641 *scf++ = 0; 642 } 643 else 644 { 645 numbits = !(scfsi & 0x8) * num0 * 6 646 + !(scfsi & 0x4) * num0 * 5 647 + !(scfsi & 0x2) * num1 * 5 648 + !(scfsi & 0x1) * num1 * 5; 649 if(numbits > gr_info->part2_3_length) 650 return -1; 651 652 if(!(scfsi & 0x8)) 653 { 654 for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0); 655 } 656 else scf += 6; 657 658 if(!(scfsi & 0x4)) 659 { 660 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0); 661 } 662 else scf += 5; 663 664 if(!(scfsi & 0x2)) 665 { 666 for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1); 667 } 668 else scf += 5; 669 670 if(!(scfsi & 0x1)) 671 { 672 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1); 673 } 674 else scf += 5; 675 676 *scf++ = 0; /* no l[21] in original sources */ 677 } 678 } 679 680 return numbits; 681 } 682 683 684 static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo) 685 { 686 const unsigned char *pnt; 687 int i,j,n=0,numbits=0; 688 unsigned int slen, slen2; 689 690 const unsigned char stab[3][6][4] = 691 { 692 { 693 { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0}, 694 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} 695 }, 696 { 697 { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0}, 698 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} 699 }, 700 { 701 { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0}, 702 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} 703 } 704 }; 705 706 if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */ 707 slen = i_slen2[gr_info->scalefac_compress>>1]; 708 else 709 slen = n_slen2[gr_info->scalefac_compress]; 710 711 gr_info->preflag = (slen>>15) & 0x1; 712 713 n = 0; 714 if( gr_info->block_type == 2 ) 715 { 716 n++; 717 if(gr_info->mixed_block_flag) n++; 718 } 719 720 pnt = stab[n][(slen>>12)&0x7]; 721 722 if(gr_info->part2_3_length == 0) 723 { 724 for(i=0;i<39;i++) 725 *scf++ = 0; 726 return 0; 727 } 728 729 slen2 = slen; 730 for(i=0;i<4;i++) 731 { 732 int num = slen2 & 0x7; 733 slen2 >>= 3; 734 if(num) 735 numbits += pnt[i] * num; 736 } 737 if(numbits > gr_info->part2_3_length) 738 return -1; 739 740 for(i=0;i<4;i++) 741 { 742 int num = slen & 0x7; 743 slen >>= 3; 744 if(num) 745 { 746 for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num); 747 } 748 else 749 for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0; 750 } 751 752 n = (n << 1) + 1; 753 for(i=0;i<n;i++) *scf++ = 0; 754 755 return numbits; 756 } 757 758 static unsigned char pretab_choice[2][22] = 759 { 760 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 761 {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0} 762 }; 763 764 /* 765 Dequantize samples 766 ...includes Huffman decoding 767 */ 768 769 /* 24 is enough because tab13 has max. a 19 bit huffvector */ 770 /* The old code played games with shifting signed integers around in not quite */ 771 /* legal ways. Also, it used long where just 32 bits are required. This could */ 772 /* be good or bad on 64 bit architectures ... anyway, making clear that */ 773 /* 32 bits suffice is a benefit. */ 774 #if 0 775 /* To reconstruct old code, use this: */ 776 #define MASK_STYPE long 777 #define MASK_UTYPE unsigned long 778 #define MASK_TYPE MASK_STYPE 779 #define MSB_MASK (mask < 0) 780 #else 781 /* This should be more proper: */ 782 #define MASK_STYPE int32_t 783 #define MASK_UTYPE uint32_t 784 #define MASK_TYPE MASK_UTYPE 785 #define MSB_MASK ((MASK_UTYPE)mask & (MASK_UTYPE)1<<(sizeof(MASK_TYPE)*8-1)) 786 #endif 787 #define BITSHIFT ((sizeof(MASK_TYPE)-1)*8) 788 #define REFRESH_MASK \ 789 while(num < BITSHIFT) { \ 790 mask |= ((MASK_UTYPE)getbyte(fr))<<(BITSHIFT-num); \ 791 num += 8; \ 792 part2remain -= 8; } 793 /* Complicated way of checking for msb value. This used to be (mask < 0). */ 794 795 static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits) 796 { 797 int shift = 1 + gr_info->scalefac_scale; 798 real *xrpnt = (real *) xr; 799 int l[3],l3; 800 int part2remain = gr_info->part2_3_length - part2bits; 801 int *me; 802 #ifdef REAL_IS_FIXED 803 int gainpow2_scale_idx = 378; 804 #endif 805 806 /* Assumption: If there is some part2_3_length at all, there should be 807 enough of it to work with properly. In case of zero length we silently 808 zero things. */ 809 if(gr_info->part2_3_length > 0) 810 { 811 812 /* mhipp tree has this split up a bit... */ 813 int num=getbitoffset(fr); 814 MASK_TYPE mask; 815 /* We must split this, because for num==0 the shift is undefined if you do it in one step. */ 816 mask = ((MASK_UTYPE) getbits(fr, num))<<BITSHIFT; 817 mask <<= 8-num; 818 part2remain -= num; 819 820 /* Bitindex is zero now, we are allowed to use getbyte(). */ 821 822 { 823 int bv = gr_info->big_values; 824 int region1 = gr_info->region1start; 825 int region2 = gr_info->region2start; 826 l3 = ((576>>1)-bv)>>1; 827 828 /* we may lose the 'odd' bit here !! check this later again */ 829 if(bv <= region1) 830 { 831 l[0] = bv; 832 l[1] = 0; 833 l[2] = 0; 834 } 835 else 836 { 837 l[0] = region1; 838 if(bv <= region2) 839 { 840 l[1] = bv - l[0]; 841 l[2] = 0; 842 } 843 else 844 { 845 l[1] = region2 - l[0]; 846 l[2] = bv - region2; 847 } 848 } 849 } 850 851 #define CHECK_XRPNT if(xrpnt >= &xr[SBLIMIT][0]) \ 852 { \ 853 if(NOQUIET) \ 854 error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) &xr[SBLIMIT][0]); \ 855 return 1; \ 856 } 857 858 if(gr_info->block_type == 2) 859 { 860 /* decoding with short or mixed mode BandIndex table */ 861 int i,max[4]; 862 int step=0,lwin=3,cb=0; 863 register real v = 0.0; 864 register int *m,mc; 865 866 if(gr_info->mixed_block_flag) 867 { 868 max[3] = -1; 869 max[0] = max[1] = max[2] = 2; 870 m = map[sfreq][0]; 871 me = mapend[sfreq][0]; 872 } 873 else 874 { 875 max[0] = max[1] = max[2] = max[3] = -1; 876 /* max[3] not really needed in this case */ 877 m = map[sfreq][1]; 878 me = mapend[sfreq][1]; 879 } 880 881 mc = 0; 882 for(i=0;i<2;i++) 883 { 884 int lp = l[i]; 885 const struct newhuff *h = ht+gr_info->table_select[i]; 886 for(;lp;lp--,mc--) 887 { 888 register MASK_STYPE x,y; 889 if( (!mc) ) 890 { 891 mc = *m++; 892 //fprintf(stderr, "%i setting xrpnt = xr + %i (%ld)\n", __LINE__, *m, xrpnt-(real*)xr); 893 xrpnt = ((real *) xr) + (*m++); 894 lwin = *m++; 895 cb = *m++; 896 if(lwin == 3) 897 { 898 #ifdef REAL_IS_FIXED 899 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 900 #endif 901 v = gr_info->pow2gain[(*scf++) << shift]; 902 step = 1; 903 } 904 else 905 { 906 #ifdef REAL_IS_FIXED 907 gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2); 908 #endif 909 v = gr_info->full_gain[lwin][(*scf++) << shift]; 910 step = 3; 911 } 912 } 913 { 914 const short *val = h->table; 915 REFRESH_MASK; 916 #ifdef USE_NEW_HUFFTABLE 917 while((y=val[(MASK_UTYPE)mask>>(BITSHIFT+4)])<0) 918 { 919 val -= y; 920 num -= 4; 921 mask <<= 4; 922 } 923 num -= (y >> 8); 924 mask <<= (y >> 8); 925 x = (y >> 4) & 0xf; 926 y &= 0xf; 927 #else 928 while((y=*val++)<0) 929 { 930 if (MSB_MASK) val -= y; 931 932 num--; 933 mask <<= 1; 934 } 935 x = y >> 4; 936 y &= 0xf; 937 #endif 938 } 939 CHECK_XRPNT; 940 if(x == 15 && h->linbits) 941 { 942 max[lwin] = cb; 943 REFRESH_MASK; 944 x += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits); 945 num -= h->linbits+1; 946 mask <<= h->linbits; 947 if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 948 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 949 950 mask <<= 1; 951 } 952 else if(x) 953 { 954 max[lwin] = cb; 955 if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 956 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 957 958 num--; 959 mask <<= 1; 960 } 961 else *xrpnt = DOUBLE_TO_REAL(0.0); 962 963 xrpnt += step; 964 CHECK_XRPNT; 965 if(y == 15 && h->linbits) 966 { 967 max[lwin] = cb; 968 REFRESH_MASK; 969 y += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits); 970 num -= h->linbits+1; 971 mask <<= h->linbits; 972 if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 973 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 974 975 mask <<= 1; 976 } 977 else if(y) 978 { 979 max[lwin] = cb; 980 if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 981 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 982 983 num--; 984 mask <<= 1; 985 } 986 else *xrpnt = DOUBLE_TO_REAL(0.0); 987 988 xrpnt += step; 989 } 990 } 991 992 for(;l3 && (part2remain+num > 0);l3--) 993 { 994 const struct newhuff* h; 995 const short* val; 996 register short a; 997 998 h = htc+gr_info->count1table_select; 999 val = h->table; 1000 1001 REFRESH_MASK; 1002 while((a=*val++)<0) 1003 { 1004 if(MSB_MASK) val -= a; 1005 1006 num--; 1007 mask <<= 1; 1008 } 1009 if(part2remain+num <= 0) 1010 { 1011 num -= part2remain+num; 1012 break; 1013 } 1014 1015 for(i=0;i<4;i++) 1016 { 1017 if(!(i & 1)) 1018 { 1019 if(!mc) 1020 { 1021 mc = *m++; 1022 //fprintf(stderr, "%i setting xrpnt = xr + %i (%ld)\n", __LINE__, *m, xrpnt-(real*)xr); 1023 xrpnt = ((real *) xr) + (*m++); 1024 lwin = *m++; 1025 cb = *m++; 1026 if(lwin == 3) 1027 { 1028 #ifdef REAL_IS_FIXED 1029 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 1030 #endif 1031 v = gr_info->pow2gain[(*scf++) << shift]; 1032 step = 1; 1033 } 1034 else 1035 { 1036 #ifdef REAL_IS_FIXED 1037 gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2); 1038 #endif 1039 v = gr_info->full_gain[lwin][(*scf++) << shift]; 1040 step = 3; 1041 } 1042 } 1043 mc--; 1044 } 1045 CHECK_XRPNT; 1046 if( (a & (0x8>>i)) ) 1047 { 1048 max[lwin] = cb; 1049 if(part2remain+num <= 0) 1050 break; 1051 1052 if(MSB_MASK) *xrpnt = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 1053 else *xrpnt = REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 1054 1055 num--; 1056 mask <<= 1; 1057 } 1058 else *xrpnt = DOUBLE_TO_REAL(0.0); 1059 1060 xrpnt += step; 1061 } 1062 } 1063 1064 if(lwin < 3) 1065 { /* short band? */ 1066 while(1) 1067 { 1068 for(;mc > 0;mc--) 1069 { 1070 CHECK_XRPNT; 1071 *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */ 1072 *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; 1073 } 1074 if(m >= me) 1075 break; 1076 1077 mc = *m++; 1078 xrpnt = ((real *) xr) + *m++; 1079 if(*m++ == 0) 1080 break; /* optimize: field will be set to zero at the end of the function */ 1081 1082 m++; /* cb */ 1083 } 1084 } 1085 1086 gr_info->maxband[0] = max[0]+1; 1087 gr_info->maxband[1] = max[1]+1; 1088 gr_info->maxband[2] = max[2]+1; 1089 gr_info->maxbandl = max[3]+1; 1090 1091 { 1092 int rmax = max[0] > max[1] ? max[0] : max[1]; 1093 rmax = (rmax > max[2] ? rmax : max[2]) + 1; 1094 gr_info->maxb = rmax ? fr->shortLimit[sfreq][rmax] : fr->longLimit[sfreq][max[3]+1]; 1095 } 1096 1097 } 1098 else 1099 { 1100 /* decoding with 'long' BandIndex table (block_type != 2) */ 1101 const unsigned char *pretab = pretab_choice[gr_info->preflag]; 1102 int i,max = -1; 1103 int cb = 0; 1104 int *m = map[sfreq][2]; 1105 register real v = 0.0; 1106 int mc = 0; 1107 1108 /* long hash table values */ 1109 for(i=0;i<3;i++) 1110 { 1111 int lp = l[i]; 1112 const struct newhuff *h = ht+gr_info->table_select[i]; 1113 1114 for(;lp;lp--,mc--) 1115 { 1116 MASK_STYPE x,y; 1117 if(!mc) 1118 { 1119 mc = *m++; 1120 cb = *m++; 1121 #ifdef CUT_SFB21 1122 if(cb == 21) 1123 v = 0.0; 1124 else 1125 #endif 1126 { 1127 #ifdef REAL_IS_FIXED 1128 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 1129 #endif 1130 v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift]; 1131 } 1132 } 1133 { 1134 const short *val = h->table; 1135 REFRESH_MASK; 1136 #ifdef USE_NEW_HUFFTABLE 1137 while((y=val[(MASK_UTYPE)mask>>(BITSHIFT+4)])<0) 1138 { 1139 val -= y; 1140 num -= 4; 1141 mask <<= 4; 1142 } 1143 num -= (y >> 8); 1144 mask <<= (y >> 8); 1145 x = (y >> 4) & 0xf; 1146 y &= 0xf; 1147 #else 1148 while((y=*val++)<0) 1149 { 1150 if (MSB_MASK) val -= y; 1151 1152 num--; 1153 mask <<= 1; 1154 } 1155 x = y >> 4; 1156 y &= 0xf; 1157 #endif 1158 } 1159 1160 CHECK_XRPNT; 1161 if(x == 15 && h->linbits) 1162 { 1163 max = cb; 1164 REFRESH_MASK; 1165 x += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits); 1166 num -= h->linbits+1; 1167 mask <<= h->linbits; 1168 if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 1169 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 1170 1171 mask <<= 1; 1172 } 1173 else if(x) 1174 { 1175 max = cb; 1176 if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 1177 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 1178 num--; 1179 1180 mask <<= 1; 1181 } 1182 else *xrpnt++ = DOUBLE_TO_REAL(0.0); 1183 1184 CHECK_XRPNT; 1185 if(y == 15 && h->linbits) 1186 { 1187 max = cb; 1188 REFRESH_MASK; 1189 y += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits); 1190 num -= h->linbits+1; 1191 mask <<= h->linbits; 1192 if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 1193 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 1194 1195 mask <<= 1; 1196 } 1197 else if(y) 1198 { 1199 max = cb; 1200 if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 1201 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 1202 1203 num--; 1204 mask <<= 1; 1205 } 1206 else *xrpnt++ = DOUBLE_TO_REAL(0.0); 1207 } 1208 } 1209 1210 /* short (count1table) values */ 1211 for(;l3 && (part2remain+num > 0);l3--) 1212 { 1213 const struct newhuff *h = htc+gr_info->count1table_select; 1214 const short *val = h->table; 1215 register short a; 1216 1217 REFRESH_MASK; 1218 while((a=*val++)<0) 1219 { 1220 if (MSB_MASK) val -= a; 1221 1222 num--; 1223 mask <<= 1; 1224 } 1225 if(part2remain+num <= 0) 1226 { 1227 num -= part2remain+num; 1228 break; 1229 } 1230 1231 for(i=0;i<4;i++) 1232 { 1233 if(!(i & 1)) 1234 { 1235 if(!mc) 1236 { 1237 mc = *m++; 1238 cb = *m++; 1239 #ifdef CUT_SFB21 1240 if(cb == 21) 1241 v = 0.0; 1242 else 1243 #endif 1244 { 1245 #ifdef REAL_IS_FIXED 1246 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 1247 #endif 1248 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift]; 1249 } 1250 } 1251 mc--; 1252 } 1253 CHECK_XRPNT; 1254 if( (a & (0x8>>i)) ) 1255 { 1256 max = cb; 1257 if(part2remain+num <= 0) 1258 break; 1259 1260 if(MSB_MASK) *xrpnt++ = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 1261 else *xrpnt++ = REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 1262 1263 num--; 1264 mask <<= 1; 1265 } 1266 else *xrpnt++ = DOUBLE_TO_REAL(0.0); 1267 } 1268 } 1269 1270 gr_info->maxbandl = max+1; 1271 gr_info->maxb = fr->longLimit[sfreq][gr_info->maxbandl]; 1272 } 1273 1274 part2remain += num; 1275 backbits(fr, num); 1276 num = 0; 1277 1278 } 1279 else 1280 { 1281 part2remain = 0; 1282 /* Not entirely sure what good values are, must be > 0. */ 1283 gr_info->maxband[0] = 1284 gr_info->maxband[1] = 1285 gr_info->maxband[2] = 1286 gr_info->maxbandl = 1; /* sfb=maxband[lwin]*3 + lwin - mixed_block_flag must be >= 0 */ 1287 gr_info->maxb = 1; 1288 } 1289 1290 while(xrpnt < &xr[SBLIMIT][0]) 1291 *xrpnt++ = DOUBLE_TO_REAL(0.0); 1292 1293 while( part2remain > 16 ) 1294 { 1295 skipbits(fr, 16); /* Dismiss stuffing Bits */ 1296 part2remain -= 16; 1297 } 1298 if(part2remain > 0) skipbits(fr, part2remain); 1299 else if(part2remain < 0) 1300 { 1301 if(VERBOSE2) 1302 error1("Can't rewind stream by %d bits!",-part2remain); 1303 return 1; /* -> error */ 1304 } 1305 return 0; 1306 } 1307 1308 1309 /* calculate real channel values for Joint-I-Stereo-mode */ 1310 static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac, struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf) 1311 { 1312 real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf; 1313 const struct bandInfoStruct *bi = &bandInfo[sfreq]; 1314 1315 const real *tab1,*tab2; 1316 1317 #if 1 1318 int tab; 1319 /* TODO: optimize as static */ 1320 const real *tabs[3][2][2] = 1321 { 1322 { { tan1_1,tan2_1 } , { tan1_2,tan2_2 } }, 1323 { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } }, 1324 { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } } 1325 }; 1326 1327 tab = lsf + (gr_info->scalefac_compress & lsf); 1328 tab1 = tabs[tab][ms_stereo][0]; 1329 tab2 = tabs[tab][ms_stereo][1]; 1330 #else 1331 if(lsf) 1332 { 1333 int p = gr_info->scalefac_compress & 0x1; 1334 if(ms_stereo) 1335 { 1336 tab1 = pow1_2[p]; 1337 tab2 = pow2_2[p]; 1338 } 1339 else 1340 { 1341 tab1 = pow1_1[p]; 1342 tab2 = pow2_1[p]; 1343 } 1344 } 1345 else 1346 { 1347 if(ms_stereo) 1348 { 1349 tab1 = tan1_2; 1350 tab2 = tan2_2; 1351 } 1352 else 1353 { 1354 tab1 = tan1_1; 1355 tab2 = tan2_1; 1356 } 1357 } 1358 #endif 1359 1360 if(gr_info->block_type == 2) 1361 { 1362 int lwin,do_l = 0; 1363 if( gr_info->mixed_block_flag ) do_l = 1; 1364 1365 for(lwin=0;lwin<3;lwin++) 1366 { /* process each window */ 1367 /* get first band with zero values */ 1368 int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */ 1369 if(sfb > 3) do_l = 0; 1370 1371 for(;sfb<12;sfb++) 1372 { 1373 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 1374 if(is_p != 7) 1375 { 1376 real t1,t2; 1377 sb = bi->shortDiff[sfb]; 1378 idx = bi->shortIdx[sfb] + lwin; 1379 t1 = tab1[is_p]; t2 = tab2[is_p]; 1380 for (; sb > 0; sb--,idx+=3) 1381 { 1382 real v = xr[0][idx]; 1383 xr[0][idx] = REAL_MUL_15(v, t1); 1384 xr[1][idx] = REAL_MUL_15(v, t2); 1385 } 1386 } 1387 } 1388 1389 #if 1 1390 /* in the original: copy 10 to 11 , here: copy 11 to 12 1391 maybe still wrong??? (copy 12 to 13?) */ 1392 is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 1393 sb = bi->shortDiff[12]; 1394 idx = bi->shortIdx[12] + lwin; 1395 #else 1396 is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 1397 sb = bi->shortDiff[11]; 1398 idx = bi->shortIdx[11] + lwin; 1399 #endif 1400 if(is_p != 7) 1401 { 1402 real t1,t2; 1403 t1 = tab1[is_p]; t2 = tab2[is_p]; 1404 for( ; sb > 0; sb--,idx+=3 ) 1405 { 1406 real v = xr[0][idx]; 1407 xr[0][idx] = REAL_MUL_15(v, t1); 1408 xr[1][idx] = REAL_MUL_15(v, t2); 1409 } 1410 } 1411 } /* end for(lwin; .. ; . ) */ 1412 1413 /* also check l-part, if ALL bands in the three windows are 'empty' and mode = mixed_mode */ 1414 if(do_l) 1415 { 1416 int sfb = gr_info->maxbandl; 1417 int idx; 1418 if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */ 1419 1420 idx = bi->longIdx[sfb]; 1421 for( ; sfb<8; sfb++ ) 1422 { 1423 int sb = bi->longDiff[sfb]; 1424 int is_p = scalefac[sfb]; /* scale: 0-15 */ 1425 if(is_p != 7) 1426 { 1427 real t1,t2; 1428 t1 = tab1[is_p]; t2 = tab2[is_p]; 1429 for( ; sb > 0; sb--,idx++) 1430 { 1431 real v = xr[0][idx]; 1432 xr[0][idx] = REAL_MUL_15(v, t1); 1433 xr[1][idx] = REAL_MUL_15(v, t2); 1434 } 1435 } 1436 else idx += sb; 1437 } 1438 } 1439 } 1440 else 1441 { /* ((gr_info->block_type != 2)) */ 1442 int sfb = gr_info->maxbandl; 1443 int is_p,idx; 1444 if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */ 1445 1446 idx = bi->longIdx[sfb]; 1447 for ( ; sfb<21; sfb++) 1448 { 1449 int sb = bi->longDiff[sfb]; 1450 is_p = scalefac[sfb]; /* scale: 0-15 */ 1451 if(is_p != 7) 1452 { 1453 real t1,t2; 1454 t1 = tab1[is_p]; t2 = tab2[is_p]; 1455 for( ; sb > 0; sb--,idx++) 1456 { 1457 real v = xr[0][idx]; 1458 xr[0][idx] = REAL_MUL_15(v, t1); 1459 xr[1][idx] = REAL_MUL_15(v, t2); 1460 } 1461 } 1462 else idx += sb; 1463 } 1464 1465 is_p = scalefac[20]; 1466 if(is_p != 7) 1467 { /* copy l-band 20 to l-band 21 */ 1468 int sb; 1469 real t1 = tab1[is_p],t2 = tab2[is_p]; 1470 1471 for( sb = bi->longDiff[21]; sb > 0; sb--,idx++ ) 1472 { 1473 real v = xr[0][idx]; 1474 xr[0][idx] = REAL_MUL_15(v, t1); 1475 xr[1][idx] = REAL_MUL_15(v, t2); 1476 } 1477 } 1478 } 1479 } 1480 1481 1482 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info) 1483 { 1484 int sblim; 1485 1486 if(gr_info->block_type == 2) 1487 { 1488 if(!gr_info->mixed_block_flag) return; 1489 1490 sblim = 1; 1491 } 1492 else sblim = gr_info->maxb-1; 1493 1494 /* 31 alias-reduction operations between each pair of sub-bands */ 1495 /* with 8 butterflies between each pair */ 1496 1497 { 1498 int sb; 1499 real *xr1=(real *) xr[1]; 1500 1501 for(sb=sblim; sb; sb--,xr1+=10) 1502 { 1503 int ss; 1504 real *cs=aa_cs,*ca=aa_ca; 1505 real *xr2 = xr1; 1506 1507 for(ss=7;ss>=0;ss--) 1508 { /* upper and lower butterfly inputs */ 1509 register real bu = *--xr2,bd = *xr1; 1510 *xr2 = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca); 1511 *xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++); 1512 } 1513 } 1514 } 1515 } 1516 1517 /* 1518 This is an optimized DCT from Jeff Tsay's maplay 1.2+ package. 1519 Saved one multiplication by doing the 'twiddle factor' stuff 1520 together with the window mul. (MH) 1521 1522 This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the 1523 9 point IDCT needs to be reduced further. Unfortunately, I don't 1524 know how to do that, because 9 is not an even number. - Jeff. 1525 1526 Original Message: 1527 1528 9 Point Inverse Discrete Cosine Transform 1529 1530 This piece of code is Copyright 1997 Mikko Tommila and is freely usable 1531 by anybody. The algorithm itself is of course in the public domain. 1532 1533 Again derived heuristically from the 9-point WFTA. 1534 1535 The algorithm is optimized (?) for speed, not for small rounding errors or 1536 good readability. 1537 1538 36 additions, 11 multiplications 1539 1540 Again this is very likely sub-optimal. 1541 1542 The code is optimized to use a minimum number of temporary variables, 1543 so it should compile quite well even on 8-register Intel x86 processors. 1544 This makes the code quite obfuscated and very difficult to understand. 1545 1546 References: 1547 [1] S. Winograd: "On Computing the Discrete Fourier Transform", 1548 Mathematics of Computation, Volume 32, Number 141, January 1978, 1549 Pages 175-199 1550 */ 1551 1552 /* Calculation of the inverse MDCT 1553 used to be static without 3dnow - does that really matter? */ 1554 void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf) 1555 { 1556 #ifdef NEW_DCT9 1557 real tmp[18]; 1558 #endif 1559 1560 { 1561 register real *in = inbuf; 1562 1563 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14]; 1564 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11]; 1565 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8]; 1566 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5]; 1567 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2]; 1568 in[2] +=in[1]; in[1] +=in[0]; 1569 1570 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9]; 1571 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1]; 1572 1573 1574 #ifdef NEW_DCT9 1575 #if 1 1576 { 1577 real t3; 1578 { 1579 real t0, t1, t2; 1580 1581 t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4])); 1582 t1 = REAL_MUL(COS6_2, in[12]); 1583 1584 t3 = in[0]; 1585 t2 = t3 - t1 - t1; 1586 tmp[1] = tmp[7] = t2 - t0; 1587 tmp[4] = t2 + t0 + t0; 1588 t3 += t1; 1589 1590 t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2])); 1591 tmp[1] -= t2; 1592 tmp[7] += t2; 1593 } 1594 { 1595 real t0, t1, t2; 1596 1597 t0 = REAL_MUL(cos9[0], (in[4] + in[8] )); 1598 t1 = REAL_MUL(cos9[1], (in[8] - in[16])); 1599 t2 = REAL_MUL(cos9[2], (in[4] + in[16])); 1600 1601 tmp[2] = tmp[6] = t3 - t0 - t2; 1602 tmp[0] = tmp[8] = t3 + t0 + t1; 1603 tmp[3] = tmp[5] = t3 - t1 + t2; 1604 } 1605 } 1606 { 1607 real t1, t2, t3; 1608 1609 t1 = REAL_MUL(cos18[0], (in[2] + in[10])); 1610 t2 = REAL_MUL(cos18[1], (in[10] - in[14])); 1611 t3 = REAL_MUL(COS6_1, in[6]); 1612 1613 { 1614 real t0 = t1 + t2 + t3; 1615 tmp[0] += t0; 1616 tmp[8] -= t0; 1617 } 1618 1619 t2 -= t3; 1620 t1 -= t3; 1621 1622 t3 = REAL_MUL(cos18[2], (in[2] + in[14])); 1623 1624 t1 += t3; 1625 tmp[3] += t1; 1626 tmp[5] -= t1; 1627 1628 t2 -= t3; 1629 tmp[2] += t2; 1630 tmp[6] -= t2; 1631 } 1632 1633 #else 1634 { 1635 real t0, t1, t2, t3, t4, t5, t6, t7; 1636 1637 t1 = REAL_MUL(COS6_2, in[12]); 1638 t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4])); 1639 1640 t3 = in[0] + t1; 1641 t4 = in[0] - t1 - t1; 1642 t5 = t4 - t2; 1643 tmp[4] = t4 + t2 + t2; 1644 1645 t0 = REAL_MUL(cos9[0], (in[4] + in[8])); 1646 t1 = REAL_MUL(cos9[1], (in[8] - in[16])); 1647 1648 t2 = REAL_MUL(cos9[2], (in[4] + in[16])); 1649 1650 t6 = t3 - t0 - t2; 1651 t0 += t3 + t1; 1652 t3 += t2 - t1; 1653 1654 t2 = REAL_MUL(cos18[0], (in[2] + in[10])); 1655 t4 = REAL_MUL(cos18[1], (in[10] - in[14])); 1656 t7 = REAL_MUL(COS6_1, in[6]); 1657 1658 t1 = t2 + t4 + t7; 1659 tmp[0] = t0 + t1; 1660 tmp[8] = t0 - t1; 1661 t1 = REAL_MUL(cos18[2], (in[2] + in[14])); 1662 t2 += t1 - t7; 1663 1664 tmp[3] = t3 + t2; 1665 t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2])); 1666 tmp[5] = t3 - t2; 1667 1668 t4 -= t1 + t7; 1669 1670 tmp[1] = t5 - t0; 1671 tmp[7] = t5 + t0; 1672 tmp[2] = t6 + t4; 1673 tmp[6] = t6 - t4; 1674 } 1675 #endif 1676 1677 { 1678 real t0, t1, t2, t3, t4, t5, t6, t7; 1679 1680 t1 = REAL_MUL(COS6_2, in[13]); 1681 t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5])); 1682 1683 t3 = in[1] + t1; 1684 t4 = in[1] - t1 - t1; 1685 t5 = t4 - t2; 1686 1687 t0 = REAL_MUL(cos9[0], (in[5] + in[9])); 1688 t1 = REAL_MUL(cos9[1], (in[9] - in[17])); 1689 1690 tmp[13] = REAL_MUL((t4 + t2 + t2), tfcos36[17-13]); 1691 t2 = REAL_MUL(cos9[2], (in[5] + in[17])); 1692 1693 t6 = t3 - t0 - t2; 1694 t0 += t3 + t1; 1695 t3 += t2 - t1; 1696 1697 t2 = REAL_MUL(cos18[0], (in[3] + in[11])); 1698 t4 = REAL_MUL(cos18[1], (in[11] - in[15])); 1699 t7 = REAL_MUL(COS6_1, in[7]); 1700 1701 t1 = t2 + t4 + t7; 1702 tmp[17] = REAL_MUL((t0 + t1), tfcos36[17-17]); 1703 tmp[9] = REAL_MUL((t0 - t1), tfcos36[17-9]); 1704 t1 = REAL_MUL(cos18[2], (in[3] + in[15])); 1705 t2 += t1 - t7; 1706 1707 tmp[14] = REAL_MUL((t3 + t2), tfcos36[17-14]); 1708 t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3])); 1709 tmp[12] = REAL_MUL((t3 - t2), tfcos36[17-12]); 1710 1711 t4 -= t1 + t7; 1712 1713 tmp[16] = REAL_MUL((t5 - t0), tfcos36[17-16]); 1714 tmp[10] = REAL_MUL((t5 + t0), tfcos36[17-10]); 1715 tmp[15] = REAL_MUL((t6 + t4), tfcos36[17-15]); 1716 tmp[11] = REAL_MUL((t6 - t4), tfcos36[17-11]); 1717 } 1718 1719 #define MACRO(v) { \ 1720 real tmpval; \ 1721 tmpval = tmp[(v)] + tmp[17-(v)]; \ 1722 out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \ 1723 out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \ 1724 tmpval = tmp[(v)] - tmp[17-(v)]; \ 1725 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \ 1726 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); } 1727 1728 { 1729 register real *out2 = o2; 1730 register real *w = wintab; 1731 register real *out1 = o1; 1732 register real *ts = tsbuf; 1733 1734 MACRO(0); 1735 MACRO(1); 1736 MACRO(2); 1737 MACRO(3); 1738 MACRO(4); 1739 MACRO(5); 1740 MACRO(6); 1741 MACRO(7); 1742 MACRO(8); 1743 } 1744 1745 #else 1746 1747 { 1748 1749 #define MACRO0(v) { \ 1750 real tmp; \ 1751 out2[9+(v)] = REAL_MUL((tmp = sum0 + sum1), w[27+(v)]); \ 1752 out2[8-(v)] = REAL_MUL(tmp, w[26-(v)]); } \ 1753 sum0 -= sum1; \ 1754 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(sum0, w[8-(v)]); \ 1755 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(sum0, w[9+(v)]); 1756 #define MACRO1(v) { \ 1757 real sum0,sum1; \ 1758 sum0 = tmp1a + tmp2a; \ 1759 sum1 = REAL_MUL((tmp1b + tmp2b), tfcos36[(v)]); \ 1760 MACRO0(v); } 1761 #define MACRO2(v) { \ 1762 real sum0,sum1; \ 1763 sum0 = tmp2a - tmp1a; \ 1764 sum1 = REAL_MUL((tmp2b - tmp1b), tfcos36[(v)]); \ 1765 MACRO0(v); } 1766 1767 register const real *c = COS9; 1768 register real *out2 = o2; 1769 register real *w = wintab; 1770 register real *out1 = o1; 1771 register real *ts = tsbuf; 1772 1773 real ta33,ta66,tb33,tb66; 1774 1775 ta33 = REAL_MUL(in[2*3+0], c[3]); 1776 ta66 = REAL_MUL(in[2*6+0], c[6]); 1777 tb33 = REAL_MUL(in[2*3+1], c[3]); 1778 tb66 = REAL_MUL(in[2*6+1], c[6]); 1779 1780 { 1781 real tmp1a,tmp2a,tmp1b,tmp2b; 1782 tmp1a = REAL_MUL(in[2*1+0], c[1]) + ta33 + REAL_MUL(in[2*5+0], c[5]) + REAL_MUL(in[2*7+0], c[7]); 1783 tmp1b = REAL_MUL(in[2*1+1], c[1]) + tb33 + REAL_MUL(in[2*5+1], c[5]) + REAL_MUL(in[2*7+1], c[7]); 1784 tmp2a = REAL_MUL(in[2*2+0], c[2]) + REAL_MUL(in[2*4+0], c[4]) + ta66 + REAL_MUL(in[2*8+0], c[8]); 1785 tmp2b = REAL_MUL(in[2*2+1], c[2]) + REAL_MUL(in[2*4+1], c[4]) + tb66 + REAL_MUL(in[2*8+1], c[8]); 1786 1787 MACRO1(0); 1788 MACRO2(8); 1789 } 1790 1791 { 1792 real tmp1a,tmp2a,tmp1b,tmp2b; 1793 tmp1a = REAL_MUL(( in[2*1+0] - in[2*5+0] - in[2*7+0] ), c[3]); 1794 tmp1b = REAL_MUL(( in[2*1+1] - in[2*5+1] - in[2*7+1] ), c[3]); 1795 tmp2a = REAL_MUL(( in[2*2+0] - in[2*4+0] - in[2*8+0] ), c[6]) - in[2*6+0] + in[2*0+0]; 1796 tmp2b = REAL_MUL(( in[2*2+1] - in[2*4+1] - in[2*8+1] ), c[6]) - in[2*6+1] + in[2*0+1]; 1797 1798 MACRO1(1); 1799 MACRO2(7); 1800 } 1801 1802 { 1803 real tmp1a,tmp2a,tmp1b,tmp2b; 1804 tmp1a = REAL_MUL(in[2*1+0], c[5]) - ta33 - REAL_MUL(in[2*5+0], c[7]) + REAL_MUL(in[2*7+0], c[1]); 1805 tmp1b = REAL_MUL(in[2*1+1], c[5]) - tb33 - REAL_MUL(in[2*5+1], c[7]) + REAL_MUL(in[2*7+1], c[1]); 1806 tmp2a = - REAL_MUL(in[2*2+0], c[8]) - REAL_MUL(in[2*4+0], c[2]) + ta66 + REAL_MUL(in[2*8+0], c[4]); 1807 tmp2b = - REAL_MUL(in[2*2+1], c[8]) - REAL_MUL(in[2*4+1], c[2]) + tb66 + REAL_MUL(in[2*8+1], c[4]); 1808 1809 MACRO1(2); 1810 MACRO2(6); 1811 } 1812 1813 { 1814 real tmp1a,tmp2a,tmp1b,tmp2b; 1815 tmp1a = REAL_MUL(in[2*1+0], c[7]) - ta33 + REAL_MUL(in[2*5+0], c[1]) - REAL_MUL(in[2*7+0], c[5]); 1816 tmp1b = REAL_MUL(in[2*1+1], c[7]) - tb33 + REAL_MUL(in[2*5+1], c[1]) - REAL_MUL(in[2*7+1], c[5]); 1817 tmp2a = - REAL_MUL(in[2*2+0], c[4]) + REAL_MUL(in[2*4+0], c[8]) + ta66 - REAL_MUL(in[2*8+0], c[2]); 1818 tmp2b = - REAL_MUL(in[2*2+1], c[4]) + REAL_MUL(in[2*4+1], c[8]) + tb66 - REAL_MUL(in[2*8+1], c[2]); 1819 1820 MACRO1(3); 1821 MACRO2(5); 1822 } 1823 1824 { 1825 real sum0,sum1; 1826 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0]; 1827 sum1 = REAL_MUL((in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ), tfcos36[4]); 1828 MACRO0(4); 1829 } 1830 } 1831 #endif 1832 1833 } 1834 } 1835 1836 1837 /* new DCT12 */ 1838 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts) 1839 { 1840 #define DCT12_PART1 \ 1841 in5 = in[5*3]; \ 1842 in5 += (in4 = in[4*3]); \ 1843 in4 += (in3 = in[3*3]); \ 1844 in3 += (in2 = in[2*3]); \ 1845 in2 += (in1 = in[1*3]); \ 1846 in1 += (in0 = in[0*3]); \ 1847 \ 1848 in5 += in3; in3 += in1; \ 1849 \ 1850 in2 = REAL_MUL(in2, COS6_1); \ 1851 in3 = REAL_MUL(in3, COS6_1); 1852 1853 #define DCT12_PART2 \ 1854 in0 += REAL_MUL(in4, COS6_2); \ 1855 \ 1856 in4 = in0 + in2; \ 1857 in0 -= in2; \ 1858 \ 1859 in1 += REAL_MUL(in5, COS6_2); \ 1860 \ 1861 in5 = REAL_MUL((in1 + in3), tfcos12[0]); \ 1862 in1 = REAL_MUL((in1 - in3), tfcos12[2]); \ 1863 \ 1864 in3 = in4 + in5; \ 1865 in4 -= in5; \ 1866 \ 1867 in2 = in0 + in1; \ 1868 in0 -= in1; 1869 1870 { 1871 real in0,in1,in2,in3,in4,in5; 1872 register real *out1 = rawout1; 1873 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2]; 1874 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5]; 1875 1876 DCT12_PART1 1877 1878 { 1879 real tmp0,tmp1 = (in0 - in4); 1880 { 1881 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); 1882 tmp0 = tmp1 + tmp2; 1883 tmp1 -= tmp2; 1884 } 1885 ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]); 1886 ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]); 1887 ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]); 1888 ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]); 1889 } 1890 1891 DCT12_PART2 1892 1893 ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]); 1894 ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]); 1895 ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]); 1896 ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]); 1897 1898 ts[(6 +0)*SBLIMIT] = out1[6+0] + REAL_MUL(in0, wi[0]); 1899 ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]); 1900 ts[(6 +2)*SBLIMIT] = out1[6+2] + REAL_MUL(in4, wi[2]); 1901 ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]); 1902 } 1903 1904 in++; 1905 1906 { 1907 real in0,in1,in2,in3,in4,in5; 1908 register real *out2 = rawout2; 1909 1910 DCT12_PART1 1911 1912 { 1913 real tmp0,tmp1 = (in0 - in4); 1914 { 1915 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); 1916 tmp0 = tmp1 + tmp2; 1917 tmp1 -= tmp2; 1918 } 1919 out2[5-1] = REAL_MUL(tmp0, wi[11-1]); 1920 out2[0+1] = REAL_MUL(tmp0, wi[6+1]); 1921 ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]); 1922 ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]); 1923 } 1924 1925 DCT12_PART2 1926 1927 out2[5-0] = REAL_MUL(in2, wi[11-0]); 1928 out2[0+0] = REAL_MUL(in2, wi[6+0]); 1929 out2[0+2] = REAL_MUL(in3, wi[6+2]); 1930 out2[5-2] = REAL_MUL(in3, wi[11-2]); 1931 1932 ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]); 1933 ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]); 1934 ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]); 1935 ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]); 1936 } 1937 1938 in++; 1939 1940 { 1941 real in0,in1,in2,in3,in4,in5; 1942 register real *out2 = rawout2; 1943 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0; 1944 1945 DCT12_PART1 1946 1947 { 1948 real tmp0,tmp1 = (in0 - in4); 1949 { 1950 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); 1951 tmp0 = tmp1 + tmp2; 1952 tmp1 -= tmp2; 1953 } 1954 out2[11-1] = REAL_MUL(tmp0, wi[11-1]); 1955 out2[6 +1] = REAL_MUL(tmp0, wi[6+1]); 1956 out2[0+1] += REAL_MUL(tmp1, wi[1]); 1957 out2[5-1] += REAL_MUL(tmp1, wi[5-1]); 1958 } 1959 1960 DCT12_PART2 1961 1962 out2[11-0] = REAL_MUL(in2, wi[11-0]); 1963 out2[6 +0] = REAL_MUL(in2, wi[6+0]); 1964 out2[6 +2] = REAL_MUL(in3, wi[6+2]); 1965 out2[11-2] = REAL_MUL(in3, wi[11-2]); 1966 1967 out2[0+0] += REAL_MUL(in0, wi[0]); 1968 out2[5-0] += REAL_MUL(in0, wi[5-0]); 1969 out2[0+2] += REAL_MUL(in4, wi[2]); 1970 out2[5-2] += REAL_MUL(in4, wi[5-2]); 1971 } 1972 } 1973 1974 1975 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info, mpg123_handle *fr) 1976 { 1977 real (*block)[2][SBLIMIT*SSLIMIT] = fr->hybrid_block; 1978 int *blc = fr->hybrid_blc; 1979 1980 real *tspnt = (real *) tsOut; 1981 real *rawout1,*rawout2; 1982 int bt = 0; 1983 size_t sb = 0; 1984 1985 { 1986 int b = blc[ch]; 1987 rawout1=block[b][ch]; 1988 b=-b+1; 1989 rawout2=block[b][ch]; 1990 blc[ch] = b; 1991 } 1992 1993 if(gr_info->mixed_block_flag) 1994 { 1995 sb = 2; 1996 opt_dct36(fr)(fsIn[0],rawout1,rawout2,win[0],tspnt); 1997 opt_dct36(fr)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1); 1998 rawout1 += 36; rawout2 += 36; tspnt += 2; 1999 } 2000 2001 bt = gr_info->block_type; 2002 if(bt == 2) 2003 { 2004 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) 2005 { 2006 dct12(fsIn[sb] ,rawout1 ,rawout2 ,win[2] ,tspnt); 2007 dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1); 2008 } 2009 } 2010 else 2011 { 2012 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) 2013 { 2014 opt_dct36(fr)(fsIn[sb],rawout1,rawout2,win[bt],tspnt); 2015 opt_dct36(fr)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1); 2016 } 2017 } 2018 2019 for(;sb<SBLIMIT;sb++,tspnt++) 2020 { 2021 int i; 2022 for(i=0;i<SSLIMIT;i++) 2023 { 2024 tspnt[i*SBLIMIT] = *rawout1++; 2025 *rawout2++ = DOUBLE_TO_REAL(0.0); 2026 } 2027 } 2028 } 2029 2030 #ifndef NO_MOREINFO 2031 static void fill_pinfo_side(mpg123_handle *fr, struct III_sideinfo *si, int gr, int stereo1) 2032 { 2033 int i, sb; 2034 float ifqstep; /* Why not double? */ 2035 int ch, ss;; 2036 2037 for(ch = 0; ch < stereo1; ++ch) 2038 { 2039 struct gr_info_s *gr_infos = &(si->ch[ch].gr[gr]); 2040 fr->pinfo->big_values[gr][ch] = gr_infos->big_values; 2041 fr->pinfo->scalefac_scale[gr][ch] = gr_infos->scalefac_scale; 2042 fr->pinfo->mixed[gr][ch] = gr_infos->mixed_block_flag; 2043 fr->pinfo->blocktype[gr][ch] = gr_infos->block_type; 2044 fr->pinfo->mainbits[gr][ch] = gr_infos->part2_3_length; 2045 fr->pinfo->preflag[gr][ch] = gr_infos->preflag; 2046 if(gr == 1) 2047 fr->pinfo->scfsi[ch] = gr_infos->scfsi; 2048 } 2049 2050 for(ch = 0; ch < stereo1; ++ch) 2051 { 2052 struct gr_info_s *gr_infos = &(si->ch[ch].gr[gr]); 2053 ifqstep = (fr->pinfo->scalefac_scale[gr][ch] == 0) ? .5 : 1.0; 2054 if(2 == gr_infos->block_type) 2055 { 2056 for(i = 0; i < 3; ++i) 2057 { 2058 for(sb = 0; sb < 12; ++sb) 2059 { 2060 int j = 3 * sb + i; 2061 /* 2062 is_p = scalefac[sfb*3+lwin-gr_infos->mixed_block_flag]; 2063 */ 2064 /* scalefac was copied into pinfo->sfb_s[] before */ 2065 fr->pinfo->sfb_s[gr][ch][j] = -ifqstep * 2066 fr->pinfo->sfb_s[gr][ch][j - gr_infos->mixed_block_flag]; 2067 fr->pinfo->sfb_s[gr][ch][j] -= 2 * 2068 (fr->pinfo->sub_gain[gr][ch][i]); 2069 } 2070 fr->pinfo->sfb_s[gr][ch][3 * sb + i] = 2071 -2 * (fr->pinfo->sub_gain[gr][ch][i]); 2072 } 2073 } else 2074 { 2075 for(sb = 0; sb < 21; ++sb) 2076 { 2077 /* scalefac was copied into pinfo->sfb[] before */ 2078 fr->pinfo->sfb[gr][ch][sb] = fr->pinfo->sfb_s[gr][ch][sb]; 2079 if (gr_infos->preflag) 2080 fr->pinfo->sfb[gr][ch][sb] += pretab_choice[1][sb]; 2081 fr->pinfo->sfb[gr][ch][sb] *= -ifqstep; 2082 } 2083 fr->pinfo->sfb[gr][ch][21] = 0; 2084 } 2085 } 2086 2087 2088 for(ch = 0; ch < stereo1; ++ch) 2089 { 2090 int j = 0; 2091 for(sb = 0; sb < SBLIMIT; ++sb) 2092 for (ss = 0; ss < SSLIMIT; ++ss, ++j) 2093 fr->pinfo->xr[gr][ch][j] = fr->layer3.hybrid_in[ch][sb][ss]; 2094 } 2095 } 2096 #endif 2097 2098 /* And at the end... the main layer3 handler */ 2099 int do_layer3(mpg123_handle *fr) 2100 { 2101 int gr, ch, ss,clip=0; 2102 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */ 2103 struct III_sideinfo sideinfo; 2104 int stereo = fr->stereo; 2105 int single = fr->single; 2106 int ms_stereo,i_stereo; 2107 int sfreq = fr->sampling_frequency; 2108 int stereo1,granules; 2109 2110 if(stereo == 1) 2111 { /* stream is mono */ 2112 stereo1 = 1; 2113 single = SINGLE_LEFT; 2114 } 2115 else if(single != SINGLE_STEREO) /* stream is stereo, but force to mono */ 2116 stereo1 = 1; 2117 else 2118 stereo1 = 2; 2119 2120 if(fr->mode == MPG_MD_JOINT_STEREO) 2121 { 2122 ms_stereo = (fr->mode_ext & 0x2)>>1; 2123 i_stereo = fr->mode_ext & 0x1; 2124 } 2125 else ms_stereo = i_stereo = 0; 2126 2127 granules = fr->lsf ? 1 : 2; 2128 2129 /* quick hack to keep the music playing */ 2130 /* after having seen this nasty test file... */ 2131 if(III_get_side_info(fr, &sideinfo,stereo,ms_stereo,sfreq,single)) 2132 { 2133 if(NOQUIET) error("bad frame - unable to get valid sideinfo"); 2134 return clip; 2135 } 2136 2137 set_pointer(fr, 1, sideinfo.main_data_begin); 2138 #ifndef NO_MOREINFO 2139 if(fr->pinfo) 2140 { 2141 fr->pinfo->maindata = sideinfo.main_data_begin; 2142 fr->pinfo->padding = fr->padding; 2143 } 2144 #endif 2145 for(gr=0;gr<granules;gr++) 2146 { 2147 /* hybridIn[2][SBLIMIT][SSLIMIT] */ 2148 real (*hybridIn)[SBLIMIT][SSLIMIT] = fr->layer3.hybrid_in; 2149 /* hybridOut[2][SSLIMIT][SBLIMIT] */ 2150 real (*hybridOut)[SSLIMIT][SBLIMIT] = fr->layer3.hybrid_out; 2151 2152 { 2153 struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]); 2154 long part2bits; 2155 if(gr_info->part2_3_length > fr->bits_avail) 2156 { 2157 if(NOQUIET) 2158 error2( 2159 "part2_3_length (%u) too large for available bit count (%li)" 2160 , gr_info->part2_3_length, fr->bits_avail ); 2161 return clip; 2162 } 2163 if(fr->lsf) 2164 part2bits = III_get_scale_factors_2(fr, scalefacs[0],gr_info,0); 2165 else 2166 part2bits = III_get_scale_factors_1(fr, scalefacs[0],gr_info,0,gr); 2167 2168 if(part2bits < 0) 2169 { 2170 if(VERBOSE2) 2171 error("not enough bits for scale factors"); 2172 return clip; 2173 } 2174 2175 #ifndef NO_MOREINFO 2176 if(fr->pinfo) 2177 { 2178 int i; 2179 fr->pinfo->sfbits[gr][0] = part2bits; 2180 for(i=0; i<39; ++i) 2181 fr->pinfo->sfb_s[gr][0][i] = scalefacs[0][i]; 2182 } 2183 #endif 2184 2185 if(III_dequantize_sample(fr, hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits)) 2186 { 2187 if(NOQUIET) 2188 error("dequantization failed!"); 2189 return clip; 2190 } 2191 if(fr->bits_avail < 0) 2192 { 2193 if(NOQUIET) 2194 error("bit deficit after dequant"); 2195 return clip; 2196 } 2197 } 2198 2199 if(stereo == 2) 2200 { 2201 struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]); 2202 long part2bits; 2203 if(fr->lsf) 2204 part2bits = III_get_scale_factors_2(fr, scalefacs[1],gr_info,i_stereo); 2205 else 2206 part2bits = III_get_scale_factors_1(fr, scalefacs[1],gr_info,1,gr); 2207 2208 if(part2bits < 0) 2209 { 2210 if(VERBOSE2) 2211 error("not enough bits for scale factors"); 2212 return clip; 2213 } 2214 2215 #ifndef NO_MOREINFO 2216 if(fr->pinfo) 2217 { 2218 int i; 2219 fr->pinfo->sfbits[gr][1] = part2bits; 2220 for(i=0; i<39; ++i) 2221 fr->pinfo->sfb_s[gr][1][i] = scalefacs[1][i]; 2222 } 2223 #endif 2224 2225 if(III_dequantize_sample(fr, hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits)) 2226 { 2227 if(NOQUIET) 2228 error("dequantization failed!"); 2229 return clip; 2230 } 2231 if(fr->bits_avail < 0) 2232 { 2233 if(NOQUIET) 2234 error("bit deficit after dequant"); 2235 return clip; 2236 } 2237 2238 if(ms_stereo) 2239 { 2240 int i; 2241 unsigned int maxb = sideinfo.ch[0].gr[gr].maxb; 2242 if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb; 2243 2244 for(i=0;i<SSLIMIT*(int)maxb;i++) 2245 { 2246 real tmp0 = ((real *)hybridIn[0])[i]; 2247 real tmp1 = ((real *)hybridIn[1])[i]; 2248 ((real *)hybridIn[0])[i] = tmp0 + tmp1; 2249 ((real *)hybridIn[1])[i] = tmp0 - tmp1; 2250 } 2251 } 2252 2253 if(i_stereo) III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf); 2254 2255 if(ms_stereo || i_stereo || (single == SINGLE_MIX) ) 2256 { 2257 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb) 2258 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb; 2259 else 2260 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb; 2261 } 2262 2263 switch(single) 2264 { 2265 case SINGLE_MIX: 2266 { 2267 register int i; 2268 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; 2269 for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++,in0++) 2270 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */ 2271 } 2272 break; 2273 case SINGLE_RIGHT: 2274 { 2275 register int i; 2276 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; 2277 for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++) 2278 *in0++ = *in1++; 2279 } 2280 break; 2281 } 2282 } 2283 2284 #ifndef NO_MOREINFO 2285 if(fr->pinfo) 2286 fill_pinfo_side(fr, &sideinfo, gr, stereo1); 2287 #endif 2288 2289 for(ch=0;ch<stereo1;ch++) 2290 { 2291 struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]); 2292 III_antialias(hybridIn[ch],gr_info); 2293 III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr); 2294 } 2295 2296 #ifdef OPT_I486 2297 if(single != SINGLE_STEREO || fr->af.encoding != MPG123_ENC_SIGNED_16 || fr->down_sample != 0) 2298 { 2299 #endif 2300 for(ss=0;ss<SSLIMIT;ss++) 2301 { 2302 if(single != SINGLE_STEREO) 2303 clip += (fr->synth_mono)(hybridOut[0][ss], fr); 2304 else 2305 clip += (fr->synth_stereo)(hybridOut[0][ss], hybridOut[1][ss], fr); 2306 2307 } 2308 #ifdef OPT_I486 2309 } else 2310 { 2311 /* Only stereo, 16 bits benefit from the 486 optimization. */ 2312 ss=0; 2313 while(ss < SSLIMIT) 2314 { 2315 int n; 2316 n=(fr->buffer.size - fr->buffer.fill) / (2*2*32); 2317 if(n > (SSLIMIT-ss)) n=SSLIMIT-ss; 2318 2319 /* Clip counting makes no sense with this function. */ 2320 absynth_1to1_i486(hybridOut[0][ss], 0, fr, n); 2321 absynth_1to1_i486(hybridOut[1][ss], 1, fr, n); 2322 ss+=n; 2323 fr->buffer.fill+=(2*2*32)*n; 2324 } 2325 } 2326 #endif 2327 } 2328 2329 return clip; 2330 } 2331