00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifdef HAVE_CONFIG_H
00011 # include <config.h>
00012 #endif
00013
00014 #include "common.h"
00015 #include "layer2.h"
00016 #include "l2tables.h"
00017 #include "decode_i386.h"
00018
00019 #ifdef WITH_DMALLOC
00020 #include <dmalloc.h>
00021 #endif
00022
00023 static int grp_3tab[32 * 3] = { 0, };
00024 static int grp_5tab[128 * 3] = { 0, };
00025 static int grp_9tab[1024 * 3] = { 0, };
00026
00027 void init_layer2(void)
00028 {
00029 static const double mulmul[27] = {
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 static const int base[3][9] = {
00037 { 1 , 0, 2 , } ,
00038 { 17, 18, 0 , 19, 20 , } ,
00039 { 21, 1, 22, 23, 0, 24, 25, 2, 26 } };
00040 int i,j,k,l,len;
00041 real *table;
00042 static const int tablen[3] = { 3 , 5 , 9 };
00043 static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab };
00044
00045 for(i=0;i<3;i++)
00046 {
00047 itable = tables[i];
00048 len = tablen[i];
00049 for(j=0;j<len;j++)
00050 for(k=0;k<len;k++)
00051 for(l=0;l<len;l++)
00052 {
00053 *itable++ = base[i][l];
00054 *itable++ = base[i][k];
00055 *itable++ = base[i][j];
00056 }
00057 }
00058
00059 for(k=0;k<27;k++)
00060 {
00061 double m=mulmul[k];
00062 table = muls[k];
00063 for(j=3,i=0;i<63;i++,j--)
00064 *table++ = (real)(m * pow(2.0,(double) j / 3.0));
00065 *table++ = 0.0;
00066 }
00067 }
00068
00069
00070 static void
00071 II_step_one(PMPSTR mp, unsigned int *bit_alloc,int *scale,struct frame *fr)
00072 {
00073 int stereo = fr->stereo-1;
00074 int sblimit = fr->II_sblimit;
00075 int jsbound = fr->jsbound;
00076 int sblimit2 = fr->II_sblimit<<stereo;
00077 struct al_table2 *alloc1 = fr->alloc;
00078 int i;
00079 static unsigned int scfsi_buf[64];
00080 unsigned int *scfsi,*bita;
00081 int sc,step;
00082
00083 bita = bit_alloc;
00084 if(stereo)
00085 {
00086 for (i=jsbound;i;i--,alloc1+=(1<<step))
00087 {
00088 *bita++ = (char) getbits(mp,step=alloc1->bits);
00089 *bita++ = (char) getbits(mp,step);
00090 }
00091 for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
00092 {
00093 bita[0] = (char) getbits(mp,step=alloc1->bits);
00094 bita[1] = bita[0];
00095 bita+=2;
00096 }
00097 bita = bit_alloc;
00098 scfsi=scfsi_buf;
00099 for (i=sblimit2;i;i--)
00100 if (*bita++)
00101 *scfsi++ = (char) getbits_fast(mp,2);
00102 }
00103 else
00104 {
00105 for (i=sblimit;i;i--,alloc1+=(1<<step))
00106 *bita++ = (char) getbits(mp,step=alloc1->bits);
00107 bita = bit_alloc;
00108 scfsi=scfsi_buf;
00109 for (i=sblimit;i;i--)
00110 if (*bita++)
00111 *scfsi++ = (char) getbits_fast(mp,2);
00112 }
00113
00114 bita = bit_alloc;
00115 scfsi=scfsi_buf;
00116 for (i=sblimit2;i;i--)
00117 if (*bita++)
00118 switch (*scfsi++)
00119 {
00120 case 0:
00121 *scale++ = getbits_fast(mp,6);
00122 *scale++ = getbits_fast(mp,6);
00123 *scale++ = getbits_fast(mp,6);
00124 break;
00125 case 1 :
00126 *scale++ = sc = getbits_fast(mp,6);
00127 *scale++ = sc;
00128 *scale++ = getbits_fast(mp,6);
00129 break;
00130 case 2:
00131 *scale++ = sc = getbits_fast(mp,6);
00132 *scale++ = sc;
00133 *scale++ = sc;
00134 break;
00135 default:
00136 *scale++ = getbits_fast(mp,6);
00137 *scale++ = sc = getbits_fast(mp,6);
00138 *scale++ = sc;
00139 break;
00140 }
00141
00142 }
00143
00144 static void
00145 II_step_two(PMPSTR mp, unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,struct frame *fr,int x1)
00146 {
00147 int i,j,k,ba;
00148 int stereo = fr->stereo;
00149 int sblimit = fr->II_sblimit;
00150 int jsbound = fr->jsbound;
00151 struct al_table2 *alloc2,*alloc1 = fr->alloc;
00152 unsigned int *bita=bit_alloc;
00153 int d1,step;
00154
00155 for (i=0;i<jsbound;i++,alloc1+=(1<<step))
00156 {
00157 step = alloc1->bits;
00158 for (j=0;j<stereo;j++)
00159 {
00160 if ( (ba=*bita++) )
00161 {
00162 k=(alloc2 = alloc1+ba)->bits;
00163 if( (d1=alloc2->d) < 0)
00164 {
00165 real cm=muls[k][scale[x1]];
00166 fraction[j][0][i] = ((real) ((int)getbits(mp,k) + d1)) * cm;
00167 fraction[j][1][i] = ((real) ((int)getbits(mp,k) + d1)) * cm;
00168 fraction[j][2][i] = ((real) ((int)getbits(mp,k) + d1)) * cm;
00169 }
00170 else
00171 {
00172 static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
00173 unsigned int idx,*tab,m=scale[x1];
00174 idx = (unsigned int) getbits(mp,k);
00175 tab = (unsigned int *) (table[d1] + idx + idx + idx);
00176 fraction[j][0][i] = muls[*tab++][m];
00177 fraction[j][1][i] = muls[*tab++][m];
00178 fraction[j][2][i] = muls[*tab][m];
00179 }
00180 scale+=3;
00181 }
00182 else
00183 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
00184 }
00185 }
00186
00187 for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
00188 {
00189 step = alloc1->bits;
00190 bita++;
00191 if ( (ba=*bita++) )
00192 {
00193 k=(alloc2 = alloc1+ba)->bits;
00194 if( (d1=alloc2->d) < 0)
00195 {
00196 real cm;
00197 cm=muls[k][scale[x1+3]];
00198 fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(mp,k) + d1) ) * cm;
00199 fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(mp,k) + d1) ) * cm;
00200 fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(mp,k) + d1) ) * cm;
00201 cm=muls[k][scale[x1]];
00202 fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm;
00203 }
00204 else
00205 {
00206 static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
00207 unsigned int idx,*tab,m1,m2;
00208 m1 = scale[x1]; m2 = scale[x1+3];
00209 idx = (unsigned int) getbits(mp,k);
00210 tab = (unsigned int *) (table[d1] + idx + idx + idx);
00211 fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2];
00212 fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2];
00213 fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2];
00214 }
00215 scale+=6;
00216 }
00217 else {
00218 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
00219 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229 }
00230
00231
00232
00233
00234 for(i=sblimit;i<SBLIMIT;i++)
00235 for (j=0;j<stereo;j++)
00236 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
00237
00238 }
00239
00240 static void II_select_table(struct frame *fr)
00241 {
00242 static const int translate[3][2][16] =
00243 { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
00244 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
00245 { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
00246 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
00247 { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
00248 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
00249
00250 int table,sblim;
00251 static const struct al_table2 *tables[5] =
00252 { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
00253 static const int sblims[5] = { 27 , 30 , 8, 12 , 30 };
00254
00255 if(fr->lsf)
00256 table = 4;
00257 else
00258 table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
00259 sblim = sblims[table];
00260
00261 fr->alloc = (struct al_table2*)tables[table];
00262 fr->II_sblimit = sblim;
00263 }
00264
00265
00266 int do_layer2( PMPSTR mp,unsigned char *pcm_sample,int *pcm_point)
00267
00268 {
00269 int clip=0;
00270 int i,j;
00271 real fraction[2][4][SBLIMIT];
00272 unsigned int bit_alloc[64];
00273 int scale[192];
00274 struct frame *fr=&(mp->fr);
00275 int stereo = fr->stereo;
00276 int single = fr->single;
00277
00278 II_select_table(fr);
00279 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
00280 (fr->mode_ext<<2)+4 : fr->II_sblimit;
00281
00282 if(stereo == 1 || single == 3)
00283 single = 0;
00284
00285 II_step_one(mp,bit_alloc, scale, fr);
00286
00287 for (i=0;i<SCALE_BLOCK;i++)
00288 {
00289 II_step_two(mp,bit_alloc,fraction,scale,fr,i>>2);
00290 for (j=0;j<3;j++)
00291 {
00292 if(single >= 0)
00293 {
00294 clip += synth_1to1_mono(mp, fraction[single][j],pcm_sample,pcm_point);
00295 }
00296 else {
00297 int p1 = *pcm_point;
00298 clip += synth_1to1(mp, fraction[0][j],0,pcm_sample,&p1);
00299 clip += synth_1to1(mp, fraction[1][j],1,pcm_sample,pcm_point);
00300 }
00301 }
00302 }
00303
00304 return clip;
00305 }
00306
00307
00308