Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenlayer3.c
Go to the documentation of this file.
00001 /* 00002 layer3.c: the layer 3 decoder 00003 00004 copyright 1995-2009 by the mpg123 project - free software under the terms of the LGPL 2.1 00005 see COPYING and AUTHORS files in distribution or http://mpg123.org 00006 initially written by Michael Hipp 00007 00008 Dear visitor: 00009 If you feel you don't understand fully the works of this file, your feeling might be correct. 00010 00011 Optimize-TODO: put short bands into the band-field without the stride of 3 reals 00012 Length-optimze: unify long and short band code where it is possible 00013 00014 The int-vs-pointer situation has to be cleaned up. 00015 */ 00016 00017 #include "mpg123lib_intern.h" 00018 #include "huffman.h" 00019 #include "getbits.h" 00020 #include "debug.h" 00021 00022 /* define CUT_SFB21 if you want to cut-off the frequency above 16kHz */ 00023 #if 0 00024 #define CUT_SFB21 00025 #endif 00026 00027 #ifdef REAL_IS_FIXED 00028 #define NEW_DCT9 00029 #include "l3_integer_tables.h" 00030 #else 00031 /* static one-time calculated tables... or so */ 00032 static real ispow[8207]; 00033 static real aa_ca[8],aa_cs[8]; 00034 static real win[4][36]; 00035 static real win1[4][36]; 00036 real COS9[9]; /* dct36_3dnow wants to use that */ 00037 static real COS6_1,COS6_2; 00038 real tfcos36[9]; /* dct36_3dnow wants to use that */ 00039 static real tfcos12[3]; 00040 #define NEW_DCT9 00041 #ifdef NEW_DCT9 00042 static real cos9[3],cos18[3]; 00043 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16]; 00044 static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16]; 00045 #endif 00046 #endif 00047 00048 /* Decoder state data, living on the stack of do_layer3. */ 00049 00050 struct gr_info_s 00051 { 00052 int scfsi; 00053 unsigned part2_3_length; 00054 unsigned big_values; 00055 unsigned scalefac_compress; 00056 unsigned block_type; 00057 unsigned mixed_block_flag; 00058 unsigned table_select[3]; 00059 /* 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. */ 00060 int maxband[3]; 00061 int maxbandl; 00062 unsigned maxb; 00063 unsigned region1start; 00064 unsigned region2start; 00065 unsigned preflag; 00066 unsigned scalefac_scale; 00067 unsigned count1table_select; 00068 real *full_gain[3]; 00069 real *pow2gain; 00070 }; 00071 00072 struct III_sideinfo 00073 { 00074 unsigned main_data_begin; 00075 unsigned private_bits; 00076 /* Hm, funny... struct inside struct... */ 00077 struct { struct gr_info_s gr[2]; } ch[2]; 00078 }; 00079 00080 struct bandInfoStruct 00081 { 00082 int longIdx[23]; 00083 int longDiff[22]; 00084 int shortIdx[14]; 00085 int shortDiff[13]; 00086 }; 00087 00088 /* Techy details about our friendly MPEG data. Fairly constant over the years;-) */ 00089 const struct bandInfoStruct bandInfo[9] = 00090 { 00091 { /* MPEG 1.0 */ 00092 {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576}, 00093 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158}, 00094 {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}, 00095 {4,4,4,4,6,8,10,12,14,18,22,30,56} 00096 }, 00097 { 00098 {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576}, 00099 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192}, 00100 {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}, 00101 {4,4,4,4,6,6,10,12,14,16,20,26,66} 00102 }, 00103 { 00104 {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576}, 00105 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26}, 00106 {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}, 00107 {4,4,4,4,6,8,12,16,20,26,34,42,12} 00108 }, 00109 { /* MPEG 2.0 */ 00110 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 00111 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } , 00112 {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} , 00113 {4,4,4,6,6,8,10,14,18,26,32,42,18 } 00114 }, 00115 { /* Twiddling 3 values here (not just 330->332!) fixed bug 1895025. */ 00116 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576}, 00117 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 }, 00118 {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}, 00119 {4,4,4,6,8,10,12,14,18,24,32,44,12 } 00120 }, 00121 { 00122 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 00123 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 }, 00124 {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}, 00125 {4,4,4,6,8,10,12,14,18,24,30,40,18 } 00126 }, 00127 { /* MPEG 2.5 */ 00128 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 00129 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, 00130 {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, 00131 {4,4,4,6,8,10,12,14,18,24,30,40,18} 00132 }, 00133 { 00134 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 00135 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, 00136 {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, 00137 {4,4,4,6,8,10,12,14,18,24,30,40,18} 00138 }, 00139 { 00140 {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576}, 00141 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2}, 00142 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576}, 00143 {8,8,8,12,16,20,24,28,36,2,2,2,26} 00144 } 00145 }; 00146 00147 static int mapbuf0[9][152]; 00148 static int mapbuf1[9][156]; 00149 static int mapbuf2[9][44]; 00150 static int *map[9][3]; 00151 static int *mapend[9][3]; 00152 00153 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */ 00154 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */ 00155 00156 /* Some helpers used in init_layer3 */ 00157 00158 #ifdef OPT_MMXORSSE 00159 real init_layer3_gainpow2_mmx(mpg123_handle *fr, int i) 00160 { 00161 if(!fr->p.down_sample) return DOUBLE_TO_REAL(16384.0 * pow((double)2.0,-0.25 * (double) (i+210) )); 00162 else return DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210))); 00163 } 00164 #endif 00165 00166 real init_layer3_gainpow2(mpg123_handle *fr, int i) 00167 { 00168 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES) 00169 return gainpow2[i+256]; 00170 #else 00171 return DOUBLE_TO_REAL_SCALE_LAYER3(pow((double)2.0,-0.25 * (double) (i+210)),i+256); 00172 #endif 00173 } 00174 00175 00176 /* init tables for layer-3 ... specific with the downsampling... */ 00177 void init_layer3(void) 00178 { 00179 int i,j,k,l; 00180 00181 #if !defined(REAL_IS_FIXED) || !defined(PRECALC_TABLES) 00182 for(i=0;i<8207;i++) 00183 ispow[i] = DOUBLE_TO_REAL_POW43(pow((double)i,(double)4.0/3.0)); 00184 00185 for(i=0;i<8;i++) 00186 { 00187 const double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037}; 00188 double sq = sqrt(1.0+Ci[i]*Ci[i]); 00189 aa_cs[i] = DOUBLE_TO_REAL(1.0/sq); 00190 aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq); 00191 } 00192 00193 for(i=0;i<18;i++) 00194 { 00195 win[0][i] = win[1][i] = 00196 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) ); 00197 win[0][i+18] = win[3][i+18] = 00198 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) ); 00199 } 00200 for(i=0;i<6;i++) 00201 { 00202 win[1][i+18] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 )); 00203 win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 )); 00204 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 )); 00205 win[1][i+30] = win[3][i] = DOUBLE_TO_REAL(0.0); 00206 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 )); 00207 } 00208 00209 for(i=0;i<9;i++) 00210 COS9[i] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i)); 00211 00212 for(i=0;i<9;i++) 00213 tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 )); 00214 00215 for(i=0;i<3;i++) 00216 tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 )); 00217 00218 COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1)); 00219 COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2)); 00220 00221 #ifdef NEW_DCT9 00222 cos9[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0)); 00223 cos9[1] = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0)); 00224 cos9[2] = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0)); 00225 cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0)); 00226 cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0)); 00227 cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0)); 00228 #endif 00229 00230 for(i=0;i<12;i++) 00231 { 00232 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 )); 00233 } 00234 00235 for(i=0;i<16;i++) 00236 { 00237 double t = tan( (double) i * M_PI / 12.0 ); 00238 tan1_1[i] = DOUBLE_TO_REAL_15(t / (1.0+t)); 00239 tan2_1[i] = DOUBLE_TO_REAL_15(1.0 / (1.0 + t)); 00240 tan1_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 * t / (1.0+t)); 00241 tan2_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 / (1.0 + t)); 00242 00243 for(j=0;j<2;j++) 00244 { 00245 double base = pow(2.0,-0.25*(j+1.0)); 00246 double p1=1.0,p2=1.0; 00247 if(i > 0) 00248 { 00249 if( i & 1 ) p1 = pow(base,(i+1.0)*0.5); 00250 else p2 = pow(base,i*0.5); 00251 } 00252 pow1_1[j][i] = DOUBLE_TO_REAL_15(p1); 00253 pow2_1[j][i] = DOUBLE_TO_REAL_15(p2); 00254 pow1_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p1); 00255 pow2_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p2); 00256 } 00257 } 00258 #endif 00259 00260 for(j=0;j<4;j++) 00261 { 00262 const int len[4] = { 36,36,12,36 }; 00263 for(i=0;i<len[j];i+=2) win1[j][i] = + win[j][i]; 00264 00265 for(i=1;i<len[j];i+=2) win1[j][i] = - win[j][i]; 00266 } 00267 00268 for(j=0;j<9;j++) 00269 { 00270 const struct bandInfoStruct *bi = &bandInfo[j]; 00271 int *mp; 00272 int cb,lwin; 00273 const int *bdf; 00274 00275 mp = map[j][0] = mapbuf0[j]; 00276 bdf = bi->longDiff; 00277 for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) 00278 { 00279 *mp++ = (*bdf) >> 1; 00280 *mp++ = i; 00281 *mp++ = 3; 00282 *mp++ = cb; 00283 } 00284 bdf = bi->shortDiff+3; 00285 for(cb=3;cb<13;cb++) 00286 { 00287 int l = (*bdf++) >> 1; 00288 for(lwin=0;lwin<3;lwin++) 00289 { 00290 *mp++ = l; 00291 *mp++ = i + lwin; 00292 *mp++ = lwin; 00293 *mp++ = cb; 00294 } 00295 i += 6*l; 00296 } 00297 mapend[j][0] = mp; 00298 00299 mp = map[j][1] = mapbuf1[j]; 00300 bdf = bi->shortDiff+0; 00301 for(i=0,cb=0;cb<13;cb++) 00302 { 00303 int l = (*bdf++) >> 1; 00304 for(lwin=0;lwin<3;lwin++) 00305 { 00306 *mp++ = l; 00307 *mp++ = i + lwin; 00308 *mp++ = lwin; 00309 *mp++ = cb; 00310 } 00311 i += 6*l; 00312 } 00313 mapend[j][1] = mp; 00314 00315 mp = map[j][2] = mapbuf2[j]; 00316 bdf = bi->longDiff; 00317 for(cb = 0; cb < 22 ; cb++) 00318 { 00319 *mp++ = (*bdf++) >> 1; 00320 *mp++ = cb; 00321 } 00322 mapend[j][2] = mp; 00323 } 00324 00325 /* Now for some serious loopings! */ 00326 for(i=0;i<5;i++) 00327 for(j=0;j<6;j++) 00328 for(k=0;k<6;k++) 00329 { 00330 int n = k + j * 6 + i * 36; 00331 i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12); 00332 } 00333 for(i=0;i<4;i++) 00334 for(j=0;j<4;j++) 00335 for(k=0;k<4;k++) 00336 { 00337 int n = k + j * 4 + i * 16; 00338 i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12); 00339 } 00340 for(i=0;i<4;i++) 00341 for(j=0;j<3;j++) 00342 { 00343 int n = j + i * 3; 00344 i_slen2[n+244] = i|(j<<3) | (5<<12); 00345 n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15); 00346 } 00347 for(i=0;i<5;i++) 00348 for(j=0;j<5;j++) 00349 for(k=0;k<4;k++) 00350 for(l=0;l<4;l++) 00351 { 00352 int n = l + k * 4 + j * 16 + i * 80; 00353 n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12); 00354 } 00355 for(i=0;i<5;i++) 00356 for(j=0;j<5;j++) 00357 for(k=0;k<4;k++) 00358 { 00359 int n = k + j * 4 + i * 20; 00360 n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12); 00361 } 00362 } 00363 00364 00365 void init_layer3_stuff(mpg123_handle *fr, real (*gainpow2)(mpg123_handle *fr, int i)) 00366 { 00367 int i,j; 00368 00369 for(i=-256;i<118+4;i++) fr->gainpow2[i+256] = gainpow2(fr,i); 00370 00371 for(j=0;j<9;j++) 00372 { 00373 for(i=0;i<23;i++) 00374 { 00375 fr->longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1; 00376 if(fr->longLimit[j][i] > (fr->down_sample_sblimit) ) 00377 fr->longLimit[j][i] = fr->down_sample_sblimit; 00378 } 00379 for(i=0;i<14;i++) 00380 { 00381 fr->shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1; 00382 if(fr->shortLimit[j][i] > (fr->down_sample_sblimit) ) 00383 fr->shortLimit[j][i] = fr->down_sample_sblimit; 00384 } 00385 } 00386 } 00387 00388 /* 00389 Observe! 00390 Now come the actualy decoding routines. 00391 */ 00392 00393 /* read additional side information (for MPEG 1 and MPEG 2) */ 00394 static int III_get_side_info(mpg123_handle *fr, struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single) 00395 { 00396 int ch, gr; 00397 int powdiff = (single == SINGLE_MIX) ? 4 : 0; 00398 00399 const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } }; 00400 const int *tab = tabs[fr->lsf]; 00401 00402 si->main_data_begin = getbits(fr, tab[1]); 00403 00404 if(si->main_data_begin > fr->bitreservoir) 00405 { 00406 if(VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num); 00407 00408 /* overwrite main_data_begin for the really available bit reservoir */ 00409 backbits(fr, tab[1]); 00410 if(fr->lsf == 0) 00411 { 00412 fr->wordpointer[0] = (unsigned char) (fr->bitreservoir >> 1); 00413 fr->wordpointer[1] = (unsigned char) ((fr->bitreservoir & 1) << 7); 00414 } 00415 else fr->wordpointer[0] = (unsigned char) fr->bitreservoir; 00416 00417 /* zero "side-info" data for a silence-frame 00418 without touching audio data used as bit reservoir for following frame */ 00419 memset(fr->wordpointer+2, 0, fr->ssize-2); 00420 00421 /* reread the new bit reservoir offset */ 00422 si->main_data_begin = getbits(fr, tab[1]); 00423 } 00424 00425 /* Keep track of the available data bytes for the bit reservoir. 00426 Think: Substract the 2 crc bytes in parser already? */ 00427 fr->bitreservoir = fr->bitreservoir + fr->framesize - fr->ssize - (fr->error_protection ? 2 : 0); 00428 /* Limit the reservoir to the max for MPEG 1.0 or 2.x . */ 00429 if(fr->bitreservoir > (unsigned int) (fr->lsf == 0 ? 511 : 255)) 00430 fr->bitreservoir = (fr->lsf == 0 ? 511 : 255); 00431 00432 /* Now back into less commented territory. It's code. It works. */ 00433 00434 if (stereo == 1) 00435 si->private_bits = getbits_fast(fr, tab[2]); 00436 else 00437 si->private_bits = getbits_fast(fr, tab[3]); 00438 00439 if(!fr->lsf) for(ch=0; ch<stereo; ch++) 00440 { 00441 si->ch[ch].gr[0].scfsi = -1; 00442 si->ch[ch].gr[1].scfsi = getbits_fast(fr, 4); 00443 } 00444 00445 for (gr=0; gr<tab[0]; gr++) 00446 for (ch=0; ch<stereo; ch++) 00447 { 00448 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]); 00449 00450 gr_info->part2_3_length = getbits(fr, 12); 00451 gr_info->big_values = getbits(fr, 9); 00452 if(gr_info->big_values > 288) 00453 { 00454 if(NOQUIET) error("big_values too large!"); 00455 gr_info->big_values = 288; 00456 } 00457 gr_info->pow2gain = fr->gainpow2+256 - getbits_fast(fr, 8) + powdiff; 00458 if(ms_stereo) gr_info->pow2gain += 2; 00459 00460 gr_info->scalefac_compress = getbits(fr, tab[4]); 00461 00462 if(get1bit(fr)) 00463 { /* window switch flag */ 00464 int i; 00465 gr_info->block_type = getbits_fast(fr, 2); 00466 gr_info->mixed_block_flag = get1bit(fr); 00467 gr_info->table_select[0] = getbits_fast(fr, 5); 00468 gr_info->table_select[1] = getbits_fast(fr, 5); 00469 /* 00470 table_select[2] not needed, because there is no region2, 00471 but to satisfy some verification tools we set it either. 00472 */ 00473 gr_info->table_select[2] = 0; 00474 for(i=0;i<3;i++) 00475 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(fr, 3)<<3); 00476 00477 if(gr_info->block_type == 0) 00478 { 00479 if(NOQUIET) error("Blocktype == 0 and window-switching == 1 not allowed."); 00480 return 1; 00481 } 00482 00483 /* region_count/start parameters are implicit in this case. */ 00484 if( (!fr->lsf || (gr_info->block_type == 2)) && !fr->mpeg25) 00485 { 00486 gr_info->region1start = 36>>1; 00487 gr_info->region2start = 576>>1; 00488 } 00489 else 00490 { 00491 if(fr->mpeg25) 00492 { 00493 int r0c,r1c; 00494 if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5; 00495 else r0c = 7; 00496 00497 r1c = 20 - r0c; 00498 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; 00499 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 00500 } 00501 else 00502 { 00503 gr_info->region1start = 54>>1; 00504 gr_info->region2start = 576>>1; 00505 } 00506 } 00507 } 00508 else 00509 { 00510 int i,r0c,r1c; 00511 for (i=0; i<3; i++) 00512 gr_info->table_select[i] = getbits_fast(fr, 5); 00513 00514 r0c = getbits_fast(fr, 4); 00515 r1c = getbits_fast(fr, 3); 00516 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; 00517 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 00518 00519 if(r0c + r1c + 2 > 22) gr_info->region2start = 576>>1; 00520 else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 00521 00522 gr_info->block_type = 0; 00523 gr_info->mixed_block_flag = 0; 00524 } 00525 if(!fr->lsf) gr_info->preflag = get1bit(fr); 00526 00527 gr_info->scalefac_scale = get1bit(fr); 00528 gr_info->count1table_select = get1bit(fr); 00529 } 00530 return 0; 00531 } 00532 00533 00534 /* read scalefactors */ 00535 static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr) 00536 { 00537 const unsigned char slen[2][16] = 00538 { 00539 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4}, 00540 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3} 00541 }; 00542 int numbits; 00543 int num0 = slen[0][gr_info->scalefac_compress]; 00544 int num1 = slen[1][gr_info->scalefac_compress]; 00545 00546 if(gr_info->block_type == 2) 00547 { 00548 int i=18; 00549 numbits = (num0 + num1) * 18; 00550 00551 if(gr_info->mixed_block_flag) 00552 { 00553 for (i=8;i;i--) 00554 *scf++ = getbits_fast(fr, num0); 00555 00556 i = 9; 00557 numbits -= num0; /* num0 * 17 + num1 * 18 */ 00558 } 00559 00560 for(;i;i--) *scf++ = getbits_fast(fr, num0); 00561 00562 for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1); 00563 00564 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */ 00565 } 00566 else 00567 { 00568 int i; 00569 int scfsi = gr_info->scfsi; 00570 00571 if(scfsi < 0) 00572 { /* scfsi < 0 => granule == 0 */ 00573 for(i=11;i;i--) *scf++ = getbits_fast(fr, num0); 00574 00575 for(i=10;i;i--) *scf++ = getbits_fast(fr, num1); 00576 00577 numbits = (num0 + num1) * 10 + num0; 00578 *scf++ = 0; 00579 } 00580 else 00581 { 00582 numbits = 0; 00583 if(!(scfsi & 0x8)) 00584 { 00585 for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0); 00586 00587 numbits += num0 * 6; 00588 } 00589 else scf += 6; 00590 00591 if(!(scfsi & 0x4)) 00592 { 00593 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0); 00594 00595 numbits += num0 * 5; 00596 } 00597 else scf += 5; 00598 00599 if(!(scfsi & 0x2)) 00600 { 00601 for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1); 00602 00603 numbits += num1 * 5; 00604 } 00605 else scf += 5; 00606 00607 if(!(scfsi & 0x1)) 00608 { 00609 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1); 00610 00611 numbits += num1 * 5; 00612 } 00613 else scf += 5; 00614 00615 *scf++ = 0; /* no l[21] in original sources */ 00616 } 00617 } 00618 return numbits; 00619 } 00620 00621 00622 static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo) 00623 { 00624 const unsigned char *pnt; 00625 int i,j,n=0,numbits=0; 00626 unsigned int slen; 00627 00628 const unsigned char stab[3][6][4] = 00629 { 00630 { 00631 { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0}, 00632 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} 00633 }, 00634 { 00635 { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0}, 00636 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} 00637 }, 00638 { 00639 { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0}, 00640 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} 00641 } 00642 }; 00643 00644 if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */ 00645 slen = i_slen2[gr_info->scalefac_compress>>1]; 00646 else 00647 slen = n_slen2[gr_info->scalefac_compress]; 00648 00649 gr_info->preflag = (slen>>15) & 0x1; 00650 00651 n = 0; 00652 if( gr_info->block_type == 2 ) 00653 { 00654 n++; 00655 if(gr_info->mixed_block_flag) n++; 00656 } 00657 00658 pnt = stab[n][(slen>>12)&0x7]; 00659 00660 for(i=0;i<4;i++) 00661 { 00662 int num = slen & 0x7; 00663 slen >>= 3; 00664 if(num) 00665 { 00666 for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num); 00667 00668 numbits += pnt[i] * num; 00669 } 00670 else 00671 for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0; 00672 } 00673 00674 n = (n << 1) + 1; 00675 for(i=0;i<n;i++) *scf++ = 0; 00676 00677 return numbits; 00678 } 00679 00680 static const int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}; 00681 static const int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 00682 00683 /* 00684 Dequantize samples 00685 ...includes Huffman decoding 00686 */ 00687 00688 /* 24 is enough because tab13 has max. a 19 bit huffvector */ 00689 #define BITSHIFT ((sizeof(long)-1)*8) 00690 #define REFRESH_MASK \ 00691 while(num < BITSHIFT) { \ 00692 mask |= ((unsigned long)getbyte(fr))<<(BITSHIFT-num); \ 00693 num += 8; \ 00694 part2remain -= 8; } 00695 00696 static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits) 00697 { 00698 int shift = 1 + gr_info->scalefac_scale; 00699 real *xrpnt = (real *) xr; 00700 int l[3],l3; 00701 int part2remain = gr_info->part2_3_length - part2bits; 00702 int *me; 00703 #ifdef REAL_IS_FIXED 00704 int gainpow2_scale_idx = 378; 00705 #endif 00706 00707 /* mhipp tree has this split up a bit... */ 00708 int num=getbitoffset(fr); 00709 long mask; 00710 /* We must split this, because for num==0 the shift is undefined if you do it in one step. */ 00711 mask = ((unsigned long) getbits(fr, num))<<BITSHIFT; 00712 mask <<= 8-num; 00713 part2remain -= num; 00714 00715 { 00716 int bv = gr_info->big_values; 00717 int region1 = gr_info->region1start; 00718 int region2 = gr_info->region2start; 00719 if(region1 > region2) 00720 { 00721 /* 00722 That's not optimal: it fixes a segfault with fuzzed data, but also apparently triggers where it shouldn't, see bug 1641196. 00723 The benefit of not crashing / having this security risk is bigger than these few frames of a lame-3.70 file that aren't audible anyway. 00724 But still, I want to know if indeed this check or the old lame is at fault. 00725 */ 00726 if(NOQUIET) error("You got some really nasty file there... region1>region2!"); 00727 return 1; 00728 } 00729 l3 = ((576>>1)-bv)>>1; 00730 00731 /* we may lose the 'odd' bit here !! check this later again */ 00732 if(bv <= region1) 00733 { 00734 l[0] = bv; 00735 l[1] = 0; 00736 l[2] = 0; 00737 } 00738 else 00739 { 00740 l[0] = region1; 00741 if(bv <= region2) 00742 { 00743 l[1] = bv - l[0]; 00744 l[2] = 0; 00745 } 00746 else 00747 { 00748 l[1] = region2 - l[0]; 00749 l[2] = bv - region2; 00750 } 00751 } 00752 } 00753 00754 if(gr_info->block_type == 2) 00755 { 00756 /* decoding with short or mixed mode BandIndex table */ 00757 int i,max[4]; 00758 int step=0,lwin=3,cb=0; 00759 register real v = 0.0; 00760 register int *m,mc; 00761 00762 if(gr_info->mixed_block_flag) 00763 { 00764 max[3] = -1; 00765 max[0] = max[1] = max[2] = 2; 00766 m = map[sfreq][0]; 00767 me = mapend[sfreq][0]; 00768 } 00769 else 00770 { 00771 max[0] = max[1] = max[2] = max[3] = -1; 00772 /* max[3] not really needed in this case */ 00773 m = map[sfreq][1]; 00774 me = mapend[sfreq][1]; 00775 } 00776 00777 mc = 0; 00778 for(i=0;i<2;i++) 00779 { 00780 int lp = l[i]; 00781 struct newhuff *h = ht+gr_info->table_select[i]; 00782 for(;lp;lp--,mc--) 00783 { 00784 register int x,y; 00785 if( (!mc) ) 00786 { 00787 mc = *m++; 00788 xrpnt = ((real *) xr) + (*m++); 00789 lwin = *m++; 00790 cb = *m++; 00791 if(lwin == 3) 00792 { 00793 #ifdef REAL_IS_FIXED 00794 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 00795 #endif 00796 v = gr_info->pow2gain[(*scf++) << shift]; 00797 step = 1; 00798 } 00799 else 00800 { 00801 #ifdef REAL_IS_FIXED 00802 gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2); 00803 #endif 00804 v = gr_info->full_gain[lwin][(*scf++) << shift]; 00805 step = 3; 00806 } 00807 } 00808 { 00809 register short *val = h->table; 00810 REFRESH_MASK; 00811 while((y=*val++)<0) 00812 { 00813 if (mask < 0) val -= y; 00814 00815 num--; 00816 mask <<= 1; 00817 } 00818 x = y >> 4; 00819 y &= 0xf; 00820 } 00821 if(x == 15 && h->linbits) 00822 { 00823 max[lwin] = cb; 00824 REFRESH_MASK; 00825 x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); 00826 num -= h->linbits+1; 00827 mask <<= h->linbits; 00828 if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 00829 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 00830 00831 mask <<= 1; 00832 } 00833 else if(x) 00834 { 00835 max[lwin] = cb; 00836 if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 00837 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 00838 00839 num--; 00840 mask <<= 1; 00841 } 00842 else *xrpnt = DOUBLE_TO_REAL(0.0); 00843 00844 xrpnt += step; 00845 if(y == 15 && h->linbits) 00846 { 00847 max[lwin] = cb; 00848 REFRESH_MASK; 00849 y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); 00850 num -= h->linbits+1; 00851 mask <<= h->linbits; 00852 if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 00853 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 00854 00855 mask <<= 1; 00856 } 00857 else if(y) 00858 { 00859 max[lwin] = cb; 00860 if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 00861 else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 00862 00863 num--; 00864 mask <<= 1; 00865 } 00866 else *xrpnt = DOUBLE_TO_REAL(0.0); 00867 00868 xrpnt += step; 00869 } 00870 } 00871 00872 for(;l3 && (part2remain+num > 0);l3--) 00873 { 00874 struct newhuff* h; 00875 register short* val; 00876 register short a; 00877 /* 00878 This is only a humble hack to prevent a special segfault. 00879 More insight into the real workings is still needed. 00880 Especially why there are (valid?) files that make xrpnt exceed the array with 4 bytes without segfaulting, more seems to be really bad, though. 00881 */ 00882 #ifdef DEBUG 00883 if(!(xrpnt < &xr[SBLIMIT][0])) 00884 { 00885 if(VERBOSE) debug2("attempted soft xrpnt overflow (%p !< %p) ?", (void*) xrpnt, (void*) &xr[SBLIMIT][0]); 00886 } 00887 #endif 00888 if(!(xrpnt < &xr[SBLIMIT][0]+5)) 00889 { 00890 if(NOQUIET) error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) &xr[SBLIMIT][0]); 00891 return 2; 00892 } 00893 h = htc+gr_info->count1table_select; 00894 val = h->table; 00895 00896 REFRESH_MASK; 00897 while((a=*val++)<0) 00898 { 00899 if(mask < 0) val -= a; 00900 00901 num--; 00902 mask <<= 1; 00903 } 00904 if(part2remain+num <= 0) 00905 { 00906 num -= part2remain+num; 00907 break; 00908 } 00909 00910 for(i=0;i<4;i++) 00911 { 00912 if(!(i & 1)) 00913 { 00914 if(!mc) 00915 { 00916 mc = *m++; 00917 xrpnt = ((real *) xr) + (*m++); 00918 lwin = *m++; 00919 cb = *m++; 00920 if(lwin == 3) 00921 { 00922 #ifdef REAL_IS_FIXED 00923 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 00924 #endif 00925 v = gr_info->pow2gain[(*scf++) << shift]; 00926 step = 1; 00927 } 00928 else 00929 { 00930 #ifdef REAL_IS_FIXED 00931 gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2); 00932 #endif 00933 v = gr_info->full_gain[lwin][(*scf++) << shift]; 00934 step = 3; 00935 } 00936 } 00937 mc--; 00938 } 00939 if( (a & (0x8>>i)) ) 00940 { 00941 max[lwin] = cb; 00942 if(part2remain+num <= 0) 00943 break; 00944 00945 if(mask < 0) *xrpnt = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 00946 else *xrpnt = REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 00947 00948 num--; 00949 mask <<= 1; 00950 } 00951 else *xrpnt = DOUBLE_TO_REAL(0.0); 00952 00953 xrpnt += step; 00954 } 00955 } 00956 00957 if(lwin < 3) 00958 { /* short band? */ 00959 while(1) 00960 { 00961 for(;mc > 0;mc--) 00962 { 00963 *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */ 00964 *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; 00965 } 00966 if(m >= me) 00967 break; 00968 00969 mc = *m++; 00970 xrpnt = ((real *) xr) + *m++; 00971 if(*m++ == 0) 00972 break; /* optimize: field will be set to zero at the end of the function */ 00973 00974 m++; /* cb */ 00975 } 00976 } 00977 00978 gr_info->maxband[0] = max[0]+1; 00979 gr_info->maxband[1] = max[1]+1; 00980 gr_info->maxband[2] = max[2]+1; 00981 gr_info->maxbandl = max[3]+1; 00982 00983 { 00984 int rmax = max[0] > max[1] ? max[0] : max[1]; 00985 rmax = (rmax > max[2] ? rmax : max[2]) + 1; 00986 gr_info->maxb = rmax ? fr->shortLimit[sfreq][rmax] : fr->longLimit[sfreq][max[3]+1]; 00987 } 00988 00989 } 00990 else 00991 { 00992 /* decoding with 'long' BandIndex table (block_type != 2) */ 00993 const int *pretab = gr_info->preflag ? pretab1 : pretab2; 00994 int i,max = -1; 00995 int cb = 0; 00996 int *m = map[sfreq][2]; 00997 register real v = 0.0; 00998 int mc = 0; 00999 01000 /* long hash table values */ 01001 for(i=0;i<3;i++) 01002 { 01003 int lp = l[i]; 01004 struct newhuff *h = ht+gr_info->table_select[i]; 01005 01006 for(;lp;lp--,mc--) 01007 { 01008 int x,y; 01009 if(!mc) 01010 { 01011 mc = *m++; 01012 cb = *m++; 01013 #ifdef CUT_SFB21 01014 if(cb == 21) 01015 v = 0.0; 01016 else 01017 #endif 01018 { 01019 #ifdef REAL_IS_FIXED 01020 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 01021 #endif 01022 v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift]; 01023 } 01024 } 01025 { 01026 register short *val = h->table; 01027 REFRESH_MASK; 01028 while((y=*val++)<0) 01029 { 01030 if (mask < 0) val -= y; 01031 01032 num--; 01033 mask <<= 1; 01034 } 01035 x = y >> 4; 01036 y &= 0xf; 01037 } 01038 01039 if(x == 15 && h->linbits) 01040 { 01041 max = cb; 01042 REFRESH_MASK; 01043 x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); 01044 num -= h->linbits+1; 01045 mask <<= h->linbits; 01046 if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 01047 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 01048 01049 mask <<= 1; 01050 } 01051 else if(x) 01052 { 01053 max = cb; 01054 if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); 01055 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); 01056 num--; 01057 01058 mask <<= 1; 01059 } 01060 else *xrpnt++ = DOUBLE_TO_REAL(0.0); 01061 01062 if(y == 15 && h->linbits) 01063 { 01064 max = cb; 01065 REFRESH_MASK; 01066 y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); 01067 num -= h->linbits+1; 01068 mask <<= h->linbits; 01069 if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 01070 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 01071 01072 mask <<= 1; 01073 } 01074 else if(y) 01075 { 01076 max = cb; 01077 if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); 01078 else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); 01079 01080 num--; 01081 mask <<= 1; 01082 } 01083 else *xrpnt++ = DOUBLE_TO_REAL(0.0); 01084 } 01085 } 01086 01087 /* short (count1table) values */ 01088 for(;l3 && (part2remain+num > 0);l3--) 01089 { 01090 struct newhuff *h = htc+gr_info->count1table_select; 01091 register short *val = h->table,a; 01092 01093 REFRESH_MASK; 01094 while((a=*val++)<0) 01095 { 01096 if (mask < 0) val -= a; 01097 01098 num--; 01099 mask <<= 1; 01100 } 01101 if(part2remain+num <= 0) 01102 { 01103 num -= part2remain+num; 01104 break; 01105 } 01106 01107 for(i=0;i<4;i++) 01108 { 01109 if(!(i & 1)) 01110 { 01111 if(!mc) 01112 { 01113 mc = *m++; 01114 cb = *m++; 01115 #ifdef CUT_SFB21 01116 if(cb == 21) 01117 v = 0.0; 01118 else 01119 #endif 01120 { 01121 #ifdef REAL_IS_FIXED 01122 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); 01123 #endif 01124 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift]; 01125 } 01126 } 01127 mc--; 01128 } 01129 if( (a & (0x8>>i)) ) 01130 { 01131 max = cb; 01132 if(part2remain+num <= 0) 01133 break; 01134 01135 if(mask < 0) *xrpnt++ = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 01136 else *xrpnt++ = REAL_SCALE_LAYER3(v, gainpow2_scale_idx); 01137 01138 num--; 01139 mask <<= 1; 01140 } 01141 else *xrpnt++ = DOUBLE_TO_REAL(0.0); 01142 } 01143 } 01144 01145 gr_info->maxbandl = max+1; 01146 gr_info->maxb = fr->longLimit[sfreq][gr_info->maxbandl]; 01147 } 01148 01149 part2remain += num; 01150 backbits(fr, num); 01151 num = 0; 01152 01153 while(xrpnt < &xr[SBLIMIT][0]) 01154 *xrpnt++ = DOUBLE_TO_REAL(0.0); 01155 01156 while( part2remain > 16 ) 01157 { 01158 skipbits(fr, 16); /* Dismiss stuffing Bits */ 01159 part2remain -= 16; 01160 } 01161 if(part2remain > 0) skipbits(fr, part2remain); 01162 else if(part2remain < 0) 01163 { 01164 debug1("Can't rewind stream by %d bits!",-part2remain); 01165 return 1; /* -> error */ 01166 } 01167 return 0; 01168 } 01169 01170 01171 /* calculate real channel values for Joint-I-Stereo-mode */ 01172 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) 01173 { 01174 real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf; 01175 const struct bandInfoStruct *bi = &bandInfo[sfreq]; 01176 01177 const real *tab1,*tab2; 01178 01179 #if 1 01180 int tab; 01181 /* TODO: optimize as static */ 01182 const real *tabs[3][2][2] = 01183 { 01184 { { tan1_1,tan2_1 } , { tan1_2,tan2_2 } }, 01185 { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } }, 01186 { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } } 01187 }; 01188 01189 tab = lsf + (gr_info->scalefac_compress & lsf); 01190 tab1 = tabs[tab][ms_stereo][0]; 01191 tab2 = tabs[tab][ms_stereo][1]; 01192 #else 01193 if(lsf) 01194 { 01195 int p = gr_info->scalefac_compress & 0x1; 01196 if(ms_stereo) 01197 { 01198 tab1 = pow1_2[p]; 01199 tab2 = pow2_2[p]; 01200 } 01201 else 01202 { 01203 tab1 = pow1_1[p]; 01204 tab2 = pow2_1[p]; 01205 } 01206 } 01207 else 01208 { 01209 if(ms_stereo) 01210 { 01211 tab1 = tan1_2; 01212 tab2 = tan2_2; 01213 } 01214 else 01215 { 01216 tab1 = tan1_1; 01217 tab2 = tan2_1; 01218 } 01219 } 01220 #endif 01221 01222 if(gr_info->block_type == 2) 01223 { 01224 int lwin,do_l = 0; 01225 if( gr_info->mixed_block_flag ) do_l = 1; 01226 01227 for(lwin=0;lwin<3;lwin++) 01228 { /* process each window */ 01229 /* get first band with zero values */ 01230 int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */ 01231 if(sfb > 3) do_l = 0; 01232 01233 for(;sfb<12;sfb++) 01234 { 01235 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 01236 if(is_p != 7) 01237 { 01238 real t1,t2; 01239 sb = bi->shortDiff[sfb]; 01240 idx = bi->shortIdx[sfb] + lwin; 01241 t1 = tab1[is_p]; t2 = tab2[is_p]; 01242 for (; sb > 0; sb--,idx+=3) 01243 { 01244 real v = xr[0][idx]; 01245 xr[0][idx] = REAL_MUL_15(v, t1); 01246 xr[1][idx] = REAL_MUL_15(v, t2); 01247 } 01248 } 01249 } 01250 01251 #if 1 01252 /* in the original: copy 10 to 11 , here: copy 11 to 12 01253 maybe still wrong??? (copy 12 to 13?) */ 01254 is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 01255 sb = bi->shortDiff[12]; 01256 idx = bi->shortIdx[12] + lwin; 01257 #else 01258 is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 01259 sb = bi->shortDiff[11]; 01260 idx = bi->shortIdx[11] + lwin; 01261 #endif 01262 if(is_p != 7) 01263 { 01264 real t1,t2; 01265 t1 = tab1[is_p]; t2 = tab2[is_p]; 01266 for( ; sb > 0; sb--,idx+=3 ) 01267 { 01268 real v = xr[0][idx]; 01269 xr[0][idx] = REAL_MUL_15(v, t1); 01270 xr[1][idx] = REAL_MUL_15(v, t2); 01271 } 01272 } 01273 } /* end for(lwin; .. ; . ) */ 01274 01275 /* also check l-part, if ALL bands in the three windows are 'empty' and mode = mixed_mode */ 01276 if(do_l) 01277 { 01278 int sfb = gr_info->maxbandl; 01279 int idx; 01280 if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */ 01281 01282 idx = bi->longIdx[sfb]; 01283 for( ; sfb<8; sfb++ ) 01284 { 01285 int sb = bi->longDiff[sfb]; 01286 int is_p = scalefac[sfb]; /* scale: 0-15 */ 01287 if(is_p != 7) 01288 { 01289 real t1,t2; 01290 t1 = tab1[is_p]; t2 = tab2[is_p]; 01291 for( ; sb > 0; sb--,idx++) 01292 { 01293 real v = xr[0][idx]; 01294 xr[0][idx] = REAL_MUL_15(v, t1); 01295 xr[1][idx] = REAL_MUL_15(v, t2); 01296 } 01297 } 01298 else idx += sb; 01299 } 01300 } 01301 } 01302 else 01303 { /* ((gr_info->block_type != 2)) */ 01304 int sfb = gr_info->maxbandl; 01305 int is_p,idx; 01306 if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */ 01307 01308 idx = bi->longIdx[sfb]; 01309 for ( ; sfb<21; sfb++) 01310 { 01311 int sb = bi->longDiff[sfb]; 01312 is_p = scalefac[sfb]; /* scale: 0-15 */ 01313 if(is_p != 7) 01314 { 01315 real t1,t2; 01316 t1 = tab1[is_p]; t2 = tab2[is_p]; 01317 for( ; sb > 0; sb--,idx++) 01318 { 01319 real v = xr[0][idx]; 01320 xr[0][idx] = REAL_MUL_15(v, t1); 01321 xr[1][idx] = REAL_MUL_15(v, t2); 01322 } 01323 } 01324 else idx += sb; 01325 } 01326 01327 is_p = scalefac[20]; 01328 if(is_p != 7) 01329 { /* copy l-band 20 to l-band 21 */ 01330 int sb; 01331 real t1 = tab1[is_p],t2 = tab2[is_p]; 01332 01333 for( sb = bi->longDiff[21]; sb > 0; sb--,idx++ ) 01334 { 01335 real v = xr[0][idx]; 01336 xr[0][idx] = REAL_MUL_15(v, t1); 01337 xr[1][idx] = REAL_MUL_15(v, t2); 01338 } 01339 } 01340 } 01341 } 01342 01343 01344 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info) 01345 { 01346 int sblim; 01347 01348 if(gr_info->block_type == 2) 01349 { 01350 if(!gr_info->mixed_block_flag) return; 01351 01352 sblim = 1; 01353 } 01354 else sblim = gr_info->maxb-1; 01355 01356 /* 31 alias-reduction operations between each pair of sub-bands */ 01357 /* with 8 butterflies between each pair */ 01358 01359 { 01360 int sb; 01361 real *xr1=(real *) xr[1]; 01362 01363 for(sb=sblim; sb; sb--,xr1+=10) 01364 { 01365 int ss; 01366 real *cs=aa_cs,*ca=aa_ca; 01367 real *xr2 = xr1; 01368 01369 for(ss=7;ss>=0;ss--) 01370 { /* upper and lower butterfly inputs */ 01371 register real bu = *--xr2,bd = *xr1; 01372 *xr2 = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca); 01373 *xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++); 01374 } 01375 } 01376 } 01377 } 01378 01379 /* 01380 This is an optimized DCT from Jeff Tsay's maplay 1.2+ package. 01381 Saved one multiplication by doing the 'twiddle factor' stuff 01382 together with the window mul. (MH) 01383 01384 This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the 01385 9 point IDCT needs to be reduced further. Unfortunately, I don't 01386 know how to do that, because 9 is not an even number. - Jeff. 01387 01388 Original Message: 01389 01390 9 Point Inverse Discrete Cosine Transform 01391 01392 This piece of code is Copyright 1997 Mikko Tommila and is freely usable 01393 by anybody. The algorithm itself is of course in the public domain. 01394 01395 Again derived heuristically from the 9-point WFTA. 01396 01397 The algorithm is optimized (?) for speed, not for small rounding errors or 01398 good readability. 01399 01400 36 additions, 11 multiplications 01401 01402 Again this is very likely sub-optimal. 01403 01404 The code is optimized to use a minimum number of temporary variables, 01405 so it should compile quite well even on 8-register Intel x86 processors. 01406 This makes the code quite obfuscated and very difficult to understand. 01407 01408 References: 01409 [1] S. Winograd: "On Computing the Discrete Fourier Transform", 01410 Mathematics of Computation, Volume 32, Number 141, January 1978, 01411 Pages 175-199 01412 */ 01413 01414 /* Calculation of the inverse MDCT 01415 used to be static without 3dnow - does that really matter? */ 01416 void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf) 01417 { 01418 #ifdef NEW_DCT9 01419 real tmp[18]; 01420 #endif 01421 01422 { 01423 register real *in = inbuf; 01424 01425 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14]; 01426 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11]; 01427 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8]; 01428 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5]; 01429 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2]; 01430 in[2] +=in[1]; in[1] +=in[0]; 01431 01432 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9]; 01433 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1]; 01434 01435 01436 #ifdef NEW_DCT9 01437 #if 1 01438 { 01439 real t3; 01440 { 01441 real t0, t1, t2; 01442 01443 t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4])); 01444 t1 = REAL_MUL(COS6_2, in[12]); 01445 01446 t3 = in[0]; 01447 t2 = t3 - t1 - t1; 01448 tmp[1] = tmp[7] = t2 - t0; 01449 tmp[4] = t2 + t0 + t0; 01450 t3 += t1; 01451 01452 t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2])); 01453 tmp[1] -= t2; 01454 tmp[7] += t2; 01455 } 01456 { 01457 real t0, t1, t2; 01458 01459 t0 = REAL_MUL(cos9[0], (in[4] + in[8] )); 01460 t1 = REAL_MUL(cos9[1], (in[8] - in[16])); 01461 t2 = REAL_MUL(cos9[2], (in[4] + in[16])); 01462 01463 tmp[2] = tmp[6] = t3 - t0 - t2; 01464 tmp[0] = tmp[8] = t3 + t0 + t1; 01465 tmp[3] = tmp[5] = t3 - t1 + t2; 01466 } 01467 } 01468 { 01469 real t1, t2, t3; 01470 01471 t1 = REAL_MUL(cos18[0], (in[2] + in[10])); 01472 t2 = REAL_MUL(cos18[1], (in[10] - in[14])); 01473 t3 = REAL_MUL(COS6_1, in[6]); 01474 01475 { 01476 real t0 = t1 + t2 + t3; 01477 tmp[0] += t0; 01478 tmp[8] -= t0; 01479 } 01480 01481 t2 -= t3; 01482 t1 -= t3; 01483 01484 t3 = REAL_MUL(cos18[2], (in[2] + in[14])); 01485 01486 t1 += t3; 01487 tmp[3] += t1; 01488 tmp[5] -= t1; 01489 01490 t2 -= t3; 01491 tmp[2] += t2; 01492 tmp[6] -= t2; 01493 } 01494 01495 #else 01496 { 01497 real t0, t1, t2, t3, t4, t5, t6, t7; 01498 01499 t1 = REAL_MUL(COS6_2, in[12]); 01500 t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4])); 01501 01502 t3 = in[0] + t1; 01503 t4 = in[0] - t1 - t1; 01504 t5 = t4 - t2; 01505 tmp[4] = t4 + t2 + t2; 01506 01507 t0 = REAL_MUL(cos9[0], (in[4] + in[8])); 01508 t1 = REAL_MUL(cos9[1], (in[8] - in[16])); 01509 01510 t2 = REAL_MUL(cos9[2], (in[4] + in[16])); 01511 01512 t6 = t3 - t0 - t2; 01513 t0 += t3 + t1; 01514 t3 += t2 - t1; 01515 01516 t2 = REAL_MUL(cos18[0], (in[2] + in[10])); 01517 t4 = REAL_MUL(cos18[1], (in[10] - in[14])); 01518 t7 = REAL_MUL(COS6_1, in[6]); 01519 01520 t1 = t2 + t4 + t7; 01521 tmp[0] = t0 + t1; 01522 tmp[8] = t0 - t1; 01523 t1 = REAL_MUL(cos18[2], (in[2] + in[14])); 01524 t2 += t1 - t7; 01525 01526 tmp[3] = t3 + t2; 01527 t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2])); 01528 tmp[5] = t3 - t2; 01529 01530 t4 -= t1 + t7; 01531 01532 tmp[1] = t5 - t0; 01533 tmp[7] = t5 + t0; 01534 tmp[2] = t6 + t4; 01535 tmp[6] = t6 - t4; 01536 } 01537 #endif 01538 01539 { 01540 real t0, t1, t2, t3, t4, t5, t6, t7; 01541 01542 t1 = REAL_MUL(COS6_2, in[13]); 01543 t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5])); 01544 01545 t3 = in[1] + t1; 01546 t4 = in[1] - t1 - t1; 01547 t5 = t4 - t2; 01548 01549 t0 = REAL_MUL(cos9[0], (in[5] + in[9])); 01550 t1 = REAL_MUL(cos9[1], (in[9] - in[17])); 01551 01552 tmp[13] = REAL_MUL((t4 + t2 + t2), tfcos36[17-13]); 01553 t2 = REAL_MUL(cos9[2], (in[5] + in[17])); 01554 01555 t6 = t3 - t0 - t2; 01556 t0 += t3 + t1; 01557 t3 += t2 - t1; 01558 01559 t2 = REAL_MUL(cos18[0], (in[3] + in[11])); 01560 t4 = REAL_MUL(cos18[1], (in[11] - in[15])); 01561 t7 = REAL_MUL(COS6_1, in[7]); 01562 01563 t1 = t2 + t4 + t7; 01564 tmp[17] = REAL_MUL((t0 + t1), tfcos36[17-17]); 01565 tmp[9] = REAL_MUL((t0 - t1), tfcos36[17-9]); 01566 t1 = REAL_MUL(cos18[2], (in[3] + in[15])); 01567 t2 += t1 - t7; 01568 01569 tmp[14] = REAL_MUL((t3 + t2), tfcos36[17-14]); 01570 t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3])); 01571 tmp[12] = REAL_MUL((t3 - t2), tfcos36[17-12]); 01572 01573 t4 -= t1 + t7; 01574 01575 tmp[16] = REAL_MUL((t5 - t0), tfcos36[17-16]); 01576 tmp[10] = REAL_MUL((t5 + t0), tfcos36[17-10]); 01577 tmp[15] = REAL_MUL((t6 + t4), tfcos36[17-15]); 01578 tmp[11] = REAL_MUL((t6 - t4), tfcos36[17-11]); 01579 } 01580 01581 #define MACRO(v) { \ 01582 real tmpval; \ 01583 tmpval = tmp[(v)] + tmp[17-(v)]; \ 01584 out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \ 01585 out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \ 01586 tmpval = tmp[(v)] - tmp[17-(v)]; \ 01587 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \ 01588 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); } 01589 01590 { 01591 register real *out2 = o2; 01592 register real *w = wintab; 01593 register real *out1 = o1; 01594 register real *ts = tsbuf; 01595 01596 MACRO(0); 01597 MACRO(1); 01598 MACRO(2); 01599 MACRO(3); 01600 MACRO(4); 01601 MACRO(5); 01602 MACRO(6); 01603 MACRO(7); 01604 MACRO(8); 01605 } 01606 01607 #else 01608 01609 { 01610 01611 #define MACRO0(v) { \ 01612 real tmp; \ 01613 out2[9+(v)] = REAL_MUL((tmp = sum0 + sum1), w[27+(v)]); \ 01614 out2[8-(v)] = REAL_MUL(tmp, w[26-(v)]); } \ 01615 sum0 -= sum1; \ 01616 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(sum0, w[8-(v)]); \ 01617 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(sum0, w[9+(v)]); 01618 #define MACRO1(v) { \ 01619 real sum0,sum1; \ 01620 sum0 = tmp1a + tmp2a; \ 01621 sum1 = REAL_MUL((tmp1b + tmp2b), tfcos36[(v)]); \ 01622 MACRO0(v); } 01623 #define MACRO2(v) { \ 01624 real sum0,sum1; \ 01625 sum0 = tmp2a - tmp1a; \ 01626 sum1 = REAL_MUL((tmp2b - tmp1b), tfcos36[(v)]); \ 01627 MACRO0(v); } 01628 01629 register const real *c = COS9; 01630 register real *out2 = o2; 01631 register real *w = wintab; 01632 register real *out1 = o1; 01633 register real *ts = tsbuf; 01634 01635 real ta33,ta66,tb33,tb66; 01636 01637 ta33 = REAL_MUL(in[2*3+0], c[3]); 01638 ta66 = REAL_MUL(in[2*6+0], c[6]); 01639 tb33 = REAL_MUL(in[2*3+1], c[3]); 01640 tb66 = REAL_MUL(in[2*6+1], c[6]); 01641 01642 { 01643 real tmp1a,tmp2a,tmp1b,tmp2b; 01644 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]); 01645 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]); 01646 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]); 01647 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]); 01648 01649 MACRO1(0); 01650 MACRO2(8); 01651 } 01652 01653 { 01654 real tmp1a,tmp2a,tmp1b,tmp2b; 01655 tmp1a = REAL_MUL(( in[2*1+0] - in[2*5+0] - in[2*7+0] ), c[3]); 01656 tmp1b = REAL_MUL(( in[2*1+1] - in[2*5+1] - in[2*7+1] ), c[3]); 01657 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]; 01658 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]; 01659 01660 MACRO1(1); 01661 MACRO2(7); 01662 } 01663 01664 { 01665 real tmp1a,tmp2a,tmp1b,tmp2b; 01666 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]); 01667 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]); 01668 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]); 01669 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]); 01670 01671 MACRO1(2); 01672 MACRO2(6); 01673 } 01674 01675 { 01676 real tmp1a,tmp2a,tmp1b,tmp2b; 01677 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]); 01678 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]); 01679 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]); 01680 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]); 01681 01682 MACRO1(3); 01683 MACRO2(5); 01684 } 01685 01686 { 01687 real sum0,sum1; 01688 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0]; 01689 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]); 01690 MACRO0(4); 01691 } 01692 } 01693 #endif 01694 01695 } 01696 } 01697 01698 01699 /* new DCT12 */ 01700 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts) 01701 { 01702 #define DCT12_PART1 \ 01703 in5 = in[5*3]; \ 01704 in5 += (in4 = in[4*3]); \ 01705 in4 += (in3 = in[3*3]); \ 01706 in3 += (in2 = in[2*3]); \ 01707 in2 += (in1 = in[1*3]); \ 01708 in1 += (in0 = in[0*3]); \ 01709 \ 01710 in5 += in3; in3 += in1; \ 01711 \ 01712 in2 = REAL_MUL(in2, COS6_1); \ 01713 in3 = REAL_MUL(in3, COS6_1); 01714 01715 #define DCT12_PART2 \ 01716 in0 += REAL_MUL(in4, COS6_2); \ 01717 \ 01718 in4 = in0 + in2; \ 01719 in0 -= in2; \ 01720 \ 01721 in1 += REAL_MUL(in5, COS6_2); \ 01722 \ 01723 in5 = REAL_MUL((in1 + in3), tfcos12[0]); \ 01724 in1 = REAL_MUL((in1 - in3), tfcos12[2]); \ 01725 \ 01726 in3 = in4 + in5; \ 01727 in4 -= in5; \ 01728 \ 01729 in2 = in0 + in1; \ 01730 in0 -= in1; 01731 01732 { 01733 real in0,in1,in2,in3,in4,in5; 01734 register real *out1 = rawout1; 01735 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2]; 01736 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5]; 01737 01738 DCT12_PART1 01739 01740 { 01741 real tmp0,tmp1 = (in0 - in4); 01742 { 01743 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); 01744 tmp0 = tmp1 + tmp2; 01745 tmp1 -= tmp2; 01746 } 01747 ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]); 01748 ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]); 01749 ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]); 01750 ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]); 01751 } 01752 01753 DCT12_PART2 01754 01755 ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]); 01756 ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]); 01757 ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]); 01758 ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]); 01759 01760 ts[(6 +0)*SBLIMIT] = out1[6+0] + REAL_MUL(in0, wi[0]); 01761 ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]); 01762 ts[(6 +2)*SBLIMIT] = out1[6+2] + REAL_MUL(in4, wi[2]); 01763 ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]); 01764 } 01765 01766 in++; 01767 01768 { 01769 real in0,in1,in2,in3,in4,in5; 01770 register real *out2 = rawout2; 01771 01772 DCT12_PART1 01773 01774 { 01775 real tmp0,tmp1 = (in0 - in4); 01776 { 01777 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); 01778 tmp0 = tmp1 + tmp2; 01779 tmp1 -= tmp2; 01780 } 01781 out2[5-1] = REAL_MUL(tmp0, wi[11-1]); 01782 out2[0+1] = REAL_MUL(tmp0, wi[6+1]); 01783 ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]); 01784 ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]); 01785 } 01786 01787 DCT12_PART2 01788 01789 out2[5-0] = REAL_MUL(in2, wi[11-0]); 01790 out2[0+0] = REAL_MUL(in2, wi[6+0]); 01791 out2[0+2] = REAL_MUL(in3, wi[6+2]); 01792 out2[5-2] = REAL_MUL(in3, wi[11-2]); 01793 01794 ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]); 01795 ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]); 01796 ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]); 01797 ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]); 01798 } 01799 01800 in++; 01801 01802 { 01803 real in0,in1,in2,in3,in4,in5; 01804 register real *out2 = rawout2; 01805 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0; 01806 01807 DCT12_PART1 01808 01809 { 01810 real tmp0,tmp1 = (in0 - in4); 01811 { 01812 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); 01813 tmp0 = tmp1 + tmp2; 01814 tmp1 -= tmp2; 01815 } 01816 out2[11-1] = REAL_MUL(tmp0, wi[11-1]); 01817 out2[6 +1] = REAL_MUL(tmp0, wi[6+1]); 01818 out2[0+1] += REAL_MUL(tmp1, wi[1]); 01819 out2[5-1] += REAL_MUL(tmp1, wi[5-1]); 01820 } 01821 01822 DCT12_PART2 01823 01824 out2[11-0] = REAL_MUL(in2, wi[11-0]); 01825 out2[6 +0] = REAL_MUL(in2, wi[6+0]); 01826 out2[6 +2] = REAL_MUL(in3, wi[6+2]); 01827 out2[11-2] = REAL_MUL(in3, wi[11-2]); 01828 01829 out2[0+0] += REAL_MUL(in0, wi[0]); 01830 out2[5-0] += REAL_MUL(in0, wi[5-0]); 01831 out2[0+2] += REAL_MUL(in4, wi[2]); 01832 out2[5-2] += REAL_MUL(in4, wi[5-2]); 01833 } 01834 } 01835 01836 01837 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info, mpg123_handle *fr) 01838 { 01839 real (*block)[2][SBLIMIT*SSLIMIT] = fr->hybrid_block; 01840 int *blc = fr->hybrid_blc; 01841 01842 real *tspnt = (real *) tsOut; 01843 real *rawout1,*rawout2; 01844 int bt = 0; 01845 size_t sb = 0; 01846 01847 { 01848 int b = blc[ch]; 01849 rawout1=block[b][ch]; 01850 b=-b+1; 01851 rawout2=block[b][ch]; 01852 blc[ch] = b; 01853 } 01854 01855 if(gr_info->mixed_block_flag) 01856 { 01857 sb = 2; 01858 opt_dct36(fr)(fsIn[0],rawout1,rawout2,win[0],tspnt); 01859 opt_dct36(fr)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1); 01860 rawout1 += 36; rawout2 += 36; tspnt += 2; 01861 } 01862 01863 bt = gr_info->block_type; 01864 if(bt == 2) 01865 { 01866 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) 01867 { 01868 dct12(fsIn[sb] ,rawout1 ,rawout2 ,win[2] ,tspnt); 01869 dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1); 01870 } 01871 } 01872 else 01873 { 01874 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) 01875 { 01876 opt_dct36(fr)(fsIn[sb],rawout1,rawout2,win[bt],tspnt); 01877 opt_dct36(fr)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1); 01878 } 01879 } 01880 01881 for(;sb<SBLIMIT;sb++,tspnt++) 01882 { 01883 int i; 01884 for(i=0;i<SSLIMIT;i++) 01885 { 01886 tspnt[i*SBLIMIT] = *rawout1++; 01887 *rawout2++ = DOUBLE_TO_REAL(0.0); 01888 } 01889 } 01890 } 01891 01892 01893 /* And at the end... the main layer3 handler */ 01894 int do_layer3(mpg123_handle *fr) 01895 { 01896 int gr, ch, ss,clip=0; 01897 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */ 01898 struct III_sideinfo sideinfo; 01899 int stereo = fr->stereo; 01900 int single = fr->single; 01901 int ms_stereo,i_stereo; 01902 int sfreq = fr->sampling_frequency; 01903 int stereo1,granules; 01904 01905 if(stereo == 1) 01906 { /* stream is mono */ 01907 stereo1 = 1; 01908 single = SINGLE_LEFT; 01909 } 01910 else if(single != SINGLE_STEREO) /* stream is stereo, but force to mono */ 01911 stereo1 = 1; 01912 else 01913 stereo1 = 2; 01914 01915 if(fr->mode == MPG_MD_JOINT_STEREO) 01916 { 01917 ms_stereo = (fr->mode_ext & 0x2)>>1; 01918 i_stereo = fr->mode_ext & 0x1; 01919 } 01920 else ms_stereo = i_stereo = 0; 01921 01922 granules = fr->lsf ? 1 : 2; 01923 01924 /* quick hack to keep the music playing */ 01925 /* after having seen this nasty test file... */ 01926 if(III_get_side_info(fr, &sideinfo,stereo,ms_stereo,sfreq,single)) 01927 { 01928 if(NOQUIET) error("bad frame - unable to get valid sideinfo"); 01929 return clip; 01930 } 01931 01932 set_pointer(fr,sideinfo.main_data_begin); 01933 01934 for(gr=0;gr<granules;gr++) 01935 { 01936 /* hybridIn[2][SBLIMIT][SSLIMIT] */ 01937 real (*hybridIn)[SBLIMIT][SSLIMIT] = fr->layer3.hybrid_in; 01938 /* hybridOut[2][SSLIMIT][SBLIMIT] */ 01939 real (*hybridOut)[SSLIMIT][SBLIMIT] = fr->layer3.hybrid_out; 01940 01941 { 01942 struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]); 01943 long part2bits; 01944 if(fr->lsf) 01945 part2bits = III_get_scale_factors_2(fr, scalefacs[0],gr_info,0); 01946 else 01947 part2bits = III_get_scale_factors_1(fr, scalefacs[0],gr_info,0,gr); 01948 01949 if(III_dequantize_sample(fr, hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits)) 01950 { 01951 if(VERBOSE2) error("dequantization failed!"); 01952 return clip; 01953 } 01954 } 01955 01956 if(stereo == 2) 01957 { 01958 struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]); 01959 long part2bits; 01960 if(fr->lsf) 01961 part2bits = III_get_scale_factors_2(fr, scalefacs[1],gr_info,i_stereo); 01962 else 01963 part2bits = III_get_scale_factors_1(fr, scalefacs[1],gr_info,1,gr); 01964 01965 if(III_dequantize_sample(fr, hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits)) 01966 { 01967 if(VERBOSE2) error("dequantization failed!"); 01968 return clip; 01969 } 01970 01971 if(ms_stereo) 01972 { 01973 int i; 01974 unsigned int maxb = sideinfo.ch[0].gr[gr].maxb; 01975 if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb; 01976 01977 for(i=0;i<SSLIMIT*(int)maxb;i++) 01978 { 01979 real tmp0 = ((real *)hybridIn[0])[i]; 01980 real tmp1 = ((real *)hybridIn[1])[i]; 01981 ((real *)hybridIn[0])[i] = tmp0 + tmp1; 01982 ((real *)hybridIn[1])[i] = tmp0 - tmp1; 01983 } 01984 } 01985 01986 if(i_stereo) III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf); 01987 01988 if(ms_stereo || i_stereo || (single == SINGLE_MIX) ) 01989 { 01990 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb) 01991 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb; 01992 else 01993 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb; 01994 } 01995 01996 switch(single) 01997 { 01998 case SINGLE_MIX: 01999 { 02000 register int i; 02001 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; 02002 for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++,in0++) 02003 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */ 02004 } 02005 break; 02006 case SINGLE_RIGHT: 02007 { 02008 register int i; 02009 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; 02010 for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++) 02011 *in0++ = *in1++; 02012 } 02013 break; 02014 } 02015 } 02016 02017 for(ch=0;ch<stereo1;ch++) 02018 { 02019 struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]); 02020 III_antialias(hybridIn[ch],gr_info); 02021 III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr); 02022 } 02023 02024 #ifdef OPT_I486 02025 if(single != SINGLE_STEREO || fr->af.encoding != MPG123_ENC_SIGNED_16 || fr->down_sample != 0) 02026 { 02027 #endif 02028 for(ss=0;ss<SSLIMIT;ss++) 02029 { 02030 if(single != SINGLE_STEREO) 02031 clip += (fr->synth_mono)(hybridOut[0][ss], fr); 02032 else 02033 clip += (fr->synth_stereo)(hybridOut[0][ss], hybridOut[1][ss], fr); 02034 02035 } 02036 #ifdef OPT_I486 02037 } else 02038 { 02039 /* Only stereo, 16 bits benefit from the 486 optimization. */ 02040 ss=0; 02041 while(ss < SSLIMIT) 02042 { 02043 int n; 02044 n=(fr->buffer.size - fr->buffer.fill) / (2*2*32); 02045 if(n > (SSLIMIT-ss)) n=SSLIMIT-ss; 02046 02047 /* Clip counting makes no sense with this function. */ 02048 absynth_1to1_i486(hybridOut[0][ss], 0, fr, n); 02049 absynth_1to1_i486(hybridOut[1][ss], 1, fr, n); 02050 ss+=n; 02051 fr->buffer.fill+=(2*2*32)*n; 02052 } 02053 } 02054 #endif 02055 } 02056 02057 return clip; 02058 } Generated on Tue May 22 2012 04:38:12 for ReactOS by
1.7.6.1
|