Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenlayer2.c
Go to the documentation of this file.
00001 /* 00002 layer2.c: the layer 2 decoder, root of mpg123 00003 00004 copyright 1994-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 mpg123 started as mp2 decoder a long time ago... 00009 part of this file is required for layer 1, too. 00010 */ 00011 00012 00013 #include "mpg123lib_intern.h" 00014 #ifndef NO_LAYER2 00015 #include "l2tables.h" 00016 #endif 00017 #include "getbits.h" 00018 00019 #ifndef NO_LAYER12 /* Stuff needed for layer I and II. */ 00020 00021 static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */ 00022 static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */ 00023 static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */ 00024 00025 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES) 00026 #include "l12_integer_tables.h" 00027 #else 00028 static const double mulmul[27] = 00029 { 00030 0.0 , -2.0/3.0 , 2.0/3.0 , 00031 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 , 00032 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 , 00033 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 , 00034 -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 , 00035 -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 00036 }; 00037 #endif 00038 00039 void init_layer12(void) 00040 { 00041 const int base[3][9] = 00042 { 00043 { 1 , 0, 2 , } , 00044 { 17, 18, 0 , 19, 20 , } , 00045 { 21, 1, 22, 23, 0, 24, 25, 2, 26 } 00046 }; 00047 int i,j,k,l,len; 00048 const int tablen[3] = { 3 , 5 , 9 }; 00049 int *itable; 00050 int *tables[3] = { grp_3tab , grp_5tab , grp_9tab }; 00051 00052 for(i=0;i<3;i++) 00053 { 00054 itable = tables[i]; 00055 len = tablen[i]; 00056 for(j=0;j<len;j++) 00057 for(k=0;k<len;k++) 00058 for(l=0;l<len;l++) 00059 { 00060 *itable++ = base[i][l]; 00061 *itable++ = base[i][k]; 00062 *itable++ = base[i][j]; 00063 } 00064 } 00065 } 00066 00067 void init_layer12_stuff(mpg123_handle *fr, real* (*init_table)(mpg123_handle *fr, real *table, int m)) 00068 { 00069 int k; 00070 real *table; 00071 for(k=0;k<27;k++) 00072 { 00073 table = init_table(fr, fr->muls[k], k); 00074 *table++ = 0.0; 00075 } 00076 } 00077 00078 real* init_layer12_table(mpg123_handle *fr, real *table, int m) 00079 { 00080 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES) 00081 int i; 00082 for(i=0;i<63;i++) 00083 *table++ = layer12_table[m][i]; 00084 #else 00085 int i,j; 00086 for(j=3,i=0;i<63;i++,j--) 00087 *table++ = DOUBLE_TO_REAL_SCALE_LAYER12(mulmul[m] * pow(2.0,(double) j / 3.0)); 00088 #endif 00089 00090 return table; 00091 } 00092 00093 #ifdef OPT_MMXORSSE 00094 real* init_layer12_table_mmx(mpg123_handle *fr, real *table, int m) 00095 { 00096 int i,j; 00097 if(!fr->p.down_sample) 00098 { 00099 for(j=3,i=0;i<63;i++,j--) 00100 *table++ = DOUBLE_TO_REAL(16384 * mulmul[m] * pow(2.0,(double) j / 3.0)); 00101 } 00102 else 00103 { 00104 for(j=3,i=0;i<63;i++,j--) 00105 *table++ = DOUBLE_TO_REAL(mulmul[m] * pow(2.0,(double) j / 3.0)); 00106 } 00107 return table; 00108 } 00109 #endif 00110 00111 #endif /* NO_LAYER12 */ 00112 00113 /* The rest is the actual decoding of layer II data. */ 00114 00115 #ifndef NO_LAYER2 00116 00117 void II_step_one(unsigned int *bit_alloc,int *scale,mpg123_handle *fr) 00118 { 00119 int stereo = fr->stereo-1; 00120 int sblimit = fr->II_sblimit; 00121 int jsbound = fr->jsbound; 00122 int sblimit2 = fr->II_sblimit<<stereo; 00123 const struct al_table *alloc1 = fr->alloc; 00124 int i; 00125 unsigned int scfsi_buf[64]; 00126 unsigned int *scfsi,*bita; 00127 int sc,step; 00128 00129 bita = bit_alloc; 00130 if(stereo) 00131 { 00132 for(i=jsbound;i;i--,alloc1+=(1<<step)) 00133 { 00134 step=alloc1->bits; 00135 *bita++ = (char) getbits(fr, step); 00136 *bita++ = (char) getbits(fr, step); 00137 } 00138 for(i=sblimit-jsbound;i;i--,alloc1+=(1<<step)) 00139 { 00140 step=alloc1->bits; 00141 bita[0] = (char) getbits(fr, step); 00142 bita[1] = bita[0]; 00143 bita+=2; 00144 } 00145 bita = bit_alloc; 00146 scfsi=scfsi_buf; 00147 00148 for(i=sblimit2;i;i--) 00149 if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2); 00150 } 00151 else /* mono */ 00152 { 00153 for(i=sblimit;i;i--,alloc1+=(1<<step)) 00154 { 00155 step=alloc1->bits; 00156 *bita++ = (char) getbits(fr, step); 00157 } 00158 bita = bit_alloc; 00159 scfsi=scfsi_buf; 00160 for(i=sblimit;i;i--) 00161 if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2); 00162 } 00163 00164 bita = bit_alloc; 00165 scfsi=scfsi_buf; 00166 for(i=sblimit2;i;i--) 00167 if(*bita++) 00168 switch(*scfsi++) 00169 { 00170 case 0: 00171 *scale++ = getbits_fast(fr, 6); 00172 *scale++ = getbits_fast(fr, 6); 00173 *scale++ = getbits_fast(fr, 6); 00174 break; 00175 case 1 : 00176 *scale++ = sc = getbits_fast(fr, 6); 00177 *scale++ = sc; 00178 *scale++ = getbits_fast(fr, 6); 00179 break; 00180 case 2: 00181 *scale++ = sc = getbits_fast(fr, 6); 00182 *scale++ = sc; 00183 *scale++ = sc; 00184 break; 00185 default: /* case 3 */ 00186 *scale++ = getbits_fast(fr, 6); 00187 *scale++ = sc = getbits_fast(fr, 6); 00188 *scale++ = sc; 00189 break; 00190 } 00191 } 00192 00193 00194 void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,mpg123_handle *fr,int x1) 00195 { 00196 int i,j,k,ba; 00197 int stereo = fr->stereo; 00198 int sblimit = fr->II_sblimit; 00199 int jsbound = fr->jsbound; 00200 const struct al_table *alloc2,*alloc1 = fr->alloc; 00201 unsigned int *bita=bit_alloc; 00202 int d1,step; 00203 00204 for(i=0;i<jsbound;i++,alloc1+=(1<<step)) 00205 { 00206 step = alloc1->bits; 00207 for(j=0;j<stereo;j++) 00208 { 00209 if( (ba=*bita++) ) 00210 { 00211 k=(alloc2 = alloc1+ba)->bits; 00212 if( (d1=alloc2->d) < 0) 00213 { 00214 real cm=fr->muls[k][scale[x1]]; 00215 fraction[j][0][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); 00216 fraction[j][1][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); 00217 fraction[j][2][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); 00218 } 00219 else 00220 { 00221 const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; 00222 unsigned int idx,*tab,m=scale[x1]; 00223 idx = (unsigned int) getbits(fr, k); 00224 tab = (unsigned int *) (table[d1] + idx + idx + idx); 00225 fraction[j][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]); 00226 fraction[j][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]); 00227 fraction[j][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m]); 00228 } 00229 scale+=3; 00230 } 00231 else 00232 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0); 00233 } 00234 } 00235 00236 for(i=jsbound;i<sblimit;i++,alloc1+=(1<<step)) 00237 { 00238 step = alloc1->bits; 00239 bita++; /* channel 1 and channel 2 bitalloc are the same */ 00240 if( (ba=*bita++) ) 00241 { 00242 k=(alloc2 = alloc1+ba)->bits; 00243 if( (d1=alloc2->d) < 0) 00244 { 00245 real cm; 00246 cm=fr->muls[k][scale[x1+3]]; 00247 fraction[0][0][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); 00248 fraction[0][1][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); 00249 fraction[0][2][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); 00250 fraction[1][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm); 00251 fraction[1][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm); 00252 fraction[1][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm); 00253 cm=fr->muls[k][scale[x1]]; 00254 fraction[0][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm); 00255 fraction[0][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm); 00256 fraction[0][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm); 00257 } 00258 else 00259 { 00260 const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; 00261 unsigned int idx,*tab,m1,m2; 00262 m1 = scale[x1]; m2 = scale[x1+3]; 00263 idx = (unsigned int) getbits(fr, k); 00264 tab = (unsigned int *) (table[d1] + idx + idx + idx); 00265 fraction[0][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]); 00266 fraction[0][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]); 00267 fraction[0][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m2]); 00268 } 00269 scale+=6; 00270 } 00271 else 00272 { 00273 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = 00274 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = DOUBLE_TO_REAL(0.0); 00275 } 00276 /* 00277 Historic comment... 00278 should we use individual scalefac for channel 2 or 00279 is the current way the right one , where we just copy channel 1 to 00280 channel 2 ?? 00281 The current 'strange' thing is, that we throw away the scalefac 00282 values for the second channel ...!! 00283 -> changed .. now we use the scalefac values of channel one !! 00284 */ 00285 } 00286 00287 if(sblimit > (fr->down_sample_sblimit) ) 00288 sblimit = fr->down_sample_sblimit; 00289 00290 for(i=sblimit;i<SBLIMIT;i++) 00291 for (j=0;j<stereo;j++) 00292 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0); 00293 } 00294 00295 00296 static void II_select_table(mpg123_handle *fr) 00297 { 00298 const int translate[3][2][16] = 00299 { 00300 { 00301 { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 }, 00302 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } 00303 }, 00304 { 00305 { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 }, 00306 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } 00307 }, 00308 { 00309 { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 }, 00310 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } 00311 } 00312 }; 00313 00314 int table,sblim; 00315 const struct al_table *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 }; 00316 const int sblims[5] = { 27 , 30 , 8, 12 , 30 }; 00317 00318 if(fr->sampling_frequency >= 3) /* Or equivalent: (fr->lsf == 1) */ 00319 table = 4; 00320 else 00321 table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index]; 00322 00323 sblim = sblims[table]; 00324 fr->alloc = tables[table]; 00325 fr->II_sblimit = sblim; 00326 } 00327 00328 00329 int do_layer2(mpg123_handle *fr) 00330 { 00331 int clip=0; 00332 int i,j; 00333 int stereo = fr->stereo; 00334 /* pick_table clears unused subbands */ 00335 /* replacement for real fraction[2][4][SBLIMIT], needs alignment. */ 00336 real (*fraction)[4][SBLIMIT] = fr->layer2.fraction; 00337 unsigned int bit_alloc[64]; 00338 int scale[192]; 00339 int single = fr->single; 00340 00341 II_select_table(fr); 00342 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : fr->II_sblimit; 00343 00344 if(fr->jsbound > fr->II_sblimit) 00345 { 00346 fprintf(stderr, "Truncating stereo boundary to sideband limit.\n"); 00347 fr->jsbound=fr->II_sblimit; 00348 } 00349 00350 /* TODO: What happens with mono mixing, actually? */ 00351 if(stereo == 1 || single == SINGLE_MIX) /* also, mix not really handled */ 00352 single = SINGLE_LEFT; 00353 00354 II_step_one(bit_alloc, scale, fr); 00355 00356 for(i=0;i<SCALE_BLOCK;i++) 00357 { 00358 II_step_two(bit_alloc,fraction,scale,fr,i>>2); 00359 for(j=0;j<3;j++) 00360 { 00361 if(single != SINGLE_STEREO) 00362 clip += (fr->synth_mono)(fraction[single][j], fr); 00363 else 00364 clip += (fr->synth_stereo)(fraction[0][j], fraction[1][j], fr); 00365 } 00366 } 00367 00368 return clip; 00369 } 00370 00371 #endif /* NO_LAYER2 */ Generated on Sun May 27 2012 04:34:09 for ReactOS by
1.7.6.1
|