examples/SFExamples/oggvorbiscodec94/src/libvorbis/lib/psytune.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
00004  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00005  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00006  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00007  *                                                                  *
00008  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002             *
00009  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
00010  *                                                                  *
00011  ********************************************************************
00012 
00013  function: simple utility that runs audio through the psychoacoustics
00014            without encoding
00015  last mod: $Id: psytune.c 7187 2004-07-20 07:24:27Z xiphmont $
00016 
00017  ********************************************************************/
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <math.h>
00023 
00024 #include "vorbis/codec.h"
00025 #include "codec_internal.h"
00026 #include "os.h"
00027 #include "misc.h"
00028 #include "psy.h"
00029 #include "mdct.h"
00030 #include "smallft.h"
00031 #include "window.h"
00032 #include "scales.h"
00033 #include "lpc.h"
00034 #include "lsp.h"
00035 #include "masking.h"
00036 #include "registry.h"
00037 
00038 static vorbis_info_psy_global _psy_set0G={
00039   0,   /* decaydBpms */
00040   8,   /* lines per eighth octave */
00041   
00042   /* thresh sample period, preecho clamp trigger threshhold, range, minenergy */
00043   256, {26.f,26.f,26.f,30.f}, {-90.f,-90.f,-90.f,-90.f}, -90.f,
00044   -6.f, 
00045   
00046   0,
00047 
00048   0.,
00049   0.,
00050 };
00051 
00052 static vp_part _vp_part0[]={
00053   {    1,9e10f, 9e10f,       1.f,9999.f},
00054   { 9999,  .75f, 9e10f,       .5f,9999.f},
00055 /*{ 9999, 1.5f, 9e10f,       .5f,9999.f},*/
00056   {   18,9e10f, 9e10f,       .5f,  30.f},
00057   { 9999,9e10f, 9e10f,       .5f,  30.f}
00058 };
00059 
00060 static vp_couple _vp_couple0[]={
00061   {    1,  {9e10f,9e10f,0}, {   0.f,   0.f,0}, {   0.f, 0.f,0}, {0.f,0.f,0}},
00062   {   18,  {9e10f,9e10f,0}, {   0.f,   0.f,0}, {   0.f, 0.f,0}, {0.f,0.f,0}},
00063   { 9999,  {9e10f,9e10f,0}, {   0.f, 9e10f,0}, {   0.f,22.f,1}, {0.f,0.f,0}}
00064 };
00065 
00066 static vorbis_info_psy _psy_set0={
00067   ATH_Bark_dB_lineaggressive,
00068   
00069   -100.f,
00070   -140.f,
00071   6.f, /* floor master att */
00072 
00073   /*     0  1  2   3   4   5   6   7   8   9  10  11  12  13  14  15   16   */
00074   /* x: 63 88 125 175 250 350 500 700 1k 1.4k 2k 2.8k 4k 5.6k 8k 11.5k 16k Hz */
00075   /* y: 0 10 20 30 40 50 60 70 80 90 100 dB */
00076    1,  /* tonemaskp */
00077   0.f, /* tone master att */
00078   /*  0   10   20   30   40   50   60   70   80   90   100 */
00079   {
00080    {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*63*/
00081    {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*88*/
00082    {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*125*/
00083 
00084    {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*175*/
00085    {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*250*/
00086    {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*350*/
00087    {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*500*/
00088    {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*700*/
00089    {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1000*/
00090    {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1400*/
00091    {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2000*/
00092    {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2800*/
00093    {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*4000*/
00094 
00095    {-30.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*5600*/
00096 
00097    {-30.f,-30.f,-33.f,-35.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*8000*/
00098    {-30.f,-30.f,-33.f,-35.f,-40.f,-45.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*11500*/
00099    {-24.f,-24.f,-26.f,-32.f,-32.f,-42.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*16000*/
00100 
00101   },
00102 
00103   1,/* peakattp */
00104   {{-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*63*/
00105    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*88*/
00106    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*125*/
00107    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*175*/
00108    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*250*/
00109    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*350*/
00110    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*500*/
00111    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*700*/
00112    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1000*/
00113    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1400*/
00114    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2000*/
00115    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2800*/
00116    {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*4000*/
00117    {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*5600*/
00118    {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*8000*/
00119    {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*11500*/
00120    {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*16000*/
00121   },
00122 
00123   1,/*noisemaskp */
00124   -10.f,  /* suppress any noise curve over maxspec+n */
00125   .5f,   /* low window */
00126   .5f,   /* high window */
00127   10,
00128   10,
00129   25,
00130   {.000f, 0.f, /*63*/
00131    .000f, 0.f, /*88*/
00132    .000f, 0.f, /*125*/
00133    .000f, 0.f, /*175*/
00134    .000f, 0.f, /*250*/
00135    .000f, 0.f, /*350*/
00136    .000f, 0.f, /*500*/
00137    .000f, 0.f, /*700*/
00138    .000f, 0.f, /*1000*/
00139    .300f, 0.f, /*1400*/
00140    .300f, 0.f, /*2000*/
00141    .300f, 0.f, /*2800*/
00142    .500f, 0.f, /*4000*/
00143    .700f, 0.f, /*5600*/
00144    .850f, 0.f, /*8000*/
00145    .900f, 0.f, /*11500*/
00146    .900f, 1.f, /*16000*/
00147   },
00148  
00149   95.f,  /* even decade + 5 is important; saves an rint() later in a
00150             tight loop) */
00151   -44.,
00152 
00153   32,
00154   _vp_part0,_vp_couple0
00155 };
00156 
00157 static vorbis_info_floor1 _floor_set0={1,
00158                                         {0},
00159                                         
00160                                         {32},
00161                                         {0},
00162                                         {0},
00163                                         {{-1}},
00164 
00165                                         2,
00166                                         {0,1024,
00167 
00168                                          88,31,243,
00169 
00170                                          14,54,143,460,
00171                                          
00172                                          6,3,10, 22,18,26, 41,36,47, 
00173                                          69,61,78, 112,99,126, 185,162,211,  
00174                                          329,282,387, 672,553,825
00175                                          },
00176                                         
00177                                         60,30,400,
00178                                         20,8,1,18.,
00179                                         20,600,
00180                                         960};
00181 
00182 
00183 static vorbis_info_mapping0 mapping_info={1,{0,1},{0},{0},{0},0, 1, {0},{1}};
00184 static codec_setup_info codec_setup0={ {0,0}, 
00185                                        1,1,1,1,1,0,1,   
00186                                        {NULL},
00187                                        {0},{&mapping_info},
00188                                        {0},{NULL},
00189                                        {1},{&_floor_set0},
00190                                        {2},{NULL},
00191                                        {NULL},
00192                                        {&_psy_set0},
00193                                        &_psy_set0G};
00194                                        
00195 static int noisy=0;
00196 void analysis(char *base,int i,float *v,int n,int bark,int dB){
00197   if(noisy){
00198     int j;
00199     FILE *of;
00200     char buffer[80];
00201     sprintf(buffer,"%s_%d.m",base,i);
00202     of=fopen(buffer,"w");
00203 
00204     for(j=0;j<n;j++){
00205       if(dB && v[j]==0)
00206           fprintf(of,"\n\n");
00207       else{
00208         if(bark)
00209           fprintf(of,"%g ",toBARK(22050.f*j/n));
00210         else
00211           fprintf(of,"%g ",(float)j);
00212       
00213         if(dB){
00214           fprintf(of,"%g\n",todB(v+j));
00215         }else{
00216           fprintf(of,"%g\n",v[j]);
00217         }
00218       }
00219     }
00220     fclose(of);
00221   }
00222 }
00223 
00224 long frameno=0;
00225 
00226 /****************************************************************/
00227 
00228 int main(int argc,char *argv[]){
00229   int eos=0;
00230   float nonz=0.f;
00231   float acc=0.f;
00232   float tot=0.f;
00233   float ampmax=-9999,newmax;
00234   float local_ampmax[2];
00235 
00236   int framesize=2048;
00237   float ampmax_att_per_sec=-6.;
00238 
00239   float *pcm[2],*out[2],*window,*flr[2],*mask[2],*work[2];
00240   signed char *buffer,*buffer2;
00241   mdct_lookup m_look;
00242   drft_lookup f_look;
00243   vorbis_look_psy p_look;
00244   vorbis_look_psy_global *pg_look;
00245   vorbis_look_floor *floor_look;
00246   vorbis_info vi;
00247   long i,j,k;
00248 
00249   int ath=0;
00250   int decayp=0;
00251 
00252   argv++;
00253   while(*argv){
00254     if(*argv[0]=='-'){
00255       /* option */
00256       if(argv[0][1]=='v'){
00257         noisy=0;
00258       }
00259     }else
00260       if(*argv[0]=='+'){
00261         /* option */
00262         if(argv[0][1]=='v'){
00263           noisy=1;
00264         }
00265       }else
00266         framesize=atoi(argv[0]);
00267     argv++;
00268   }
00269   
00270   vi.channels=2;
00271   vi.codec_setup=&codec_setup0;
00272 
00273   pcm[0]=_ogg_malloc(framesize*sizeof(float));
00274   pcm[1]=_ogg_malloc(framesize*sizeof(float));
00275   out[0]=_ogg_calloc(framesize/2,sizeof(float));
00276   out[1]=_ogg_calloc(framesize/2,sizeof(float));
00277   work[0]=_ogg_calloc(framesize,sizeof(float));
00278   work[1]=_ogg_calloc(framesize,sizeof(float));
00279   flr[0]=_ogg_calloc(framesize/2,sizeof(float));
00280   flr[1]=_ogg_calloc(framesize/2,sizeof(float));
00281   buffer=_ogg_malloc(framesize*4);
00282   buffer2=buffer+framesize*2;
00283   window=_vorbis_window_create(0,framesize,framesize/2,framesize/2);
00284   mdct_init(&m_look,framesize);
00285   drft_init(&f_look,framesize);
00286   _vp_psy_init(&p_look,&_psy_set0,&_psy_set0G,framesize/2,44100);
00287   pg_look=_vp_global_look(&vi);
00288   floor_look=_floor_P[1]->look(NULL,NULL,&_floor_set0);
00289 
00290   /* we cheat on the WAV header; we just bypass 44 bytes and never
00291      verify that it matches 16bit/stereo/44.1kHz. */
00292   
00293   fread(buffer,1,44,stdin);
00294   fwrite(buffer,1,44,stdout);
00295   memset(buffer,0,framesize*2);
00296 
00297   analysis("window",0,window,framesize,0,0);
00298 
00299   fprintf(stderr,"Processing for frame size %d...\n",framesize);
00300 
00301   while(!eos){
00302     long bytes=fread(buffer2,1,framesize*2,stdin); 
00303     if(bytes<framesize*2)
00304       memset(buffer2+bytes,0,framesize*2-bytes);
00305     
00306     if(bytes!=0){
00307       int nonzero[2];
00308 
00309       /* uninterleave samples */
00310       for(i=0;i<framesize;i++){
00311         pcm[0][i]=((buffer[i*4+1]<<8)|
00312                       (0x00ff&(int)buffer[i*4]))/32768.f;
00313         pcm[1][i]=((buffer[i*4+3]<<8)|
00314                    (0x00ff&(int)buffer[i*4+2]))/32768.f;
00315       }
00316       
00317       {
00318         float secs=framesize/44100.;
00319         
00320         ampmax+=secs*ampmax_att_per_sec;
00321         if(ampmax<-9999)ampmax=-9999;
00322       }
00323 
00324       for(i=0;i<2;i++){
00325         float scale=4.f/framesize;
00326         float *fft=work[i];
00327         float *mdct=pcm[i];
00328         float *logmdct=mdct+framesize/2;
00329 
00330         analysis("pre",frameno+i,pcm[i],framesize,0,0);
00331         
00332         /* fft and mdct transforms  */
00333         for(j=0;j<framesize;j++)
00334           fft[j]=pcm[i][j]*=window[j];
00335         
00336         drft_forward(&f_look,fft);
00337 
00338         local_ampmax[i]=-9999.f;
00339         fft[0]*=scale;
00340         fft[0]=todB(fft);
00341         for(j=1;j<framesize-1;j+=2){
00342           float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
00343           temp=fft[(j+1)>>1]=todB(&temp);
00344           if(temp>local_ampmax[i])local_ampmax[i]=temp;
00345         }
00346         if(local_ampmax[i]>ampmax)ampmax=local_ampmax[i];
00347         
00348         mdct_forward(&m_look,pcm[i],mdct);
00349         for(j=0;j<framesize/2;j++)
00350           logmdct[j]=todB(mdct+j);
00351 
00352         analysis("mdct",frameno+i,logmdct,framesize/2,1,0);
00353         analysis("fft",frameno+i,fft,framesize/2,1,0);
00354       }
00355 
00356       for(i=0;i<2;i++){
00357         float amp;
00358         float *fft=work[i];
00359         float *logmax=fft;
00360         float *mdct=pcm[i];
00361         float *logmdct=mdct+framesize/2;
00362         float *mask=fft+framesize/2;
00363 
00364         /* floor psychoacoustics */
00365         _vp_compute_mask(&p_look,
00366                          pg_look,
00367                          i,
00368                          fft,
00369                          logmdct,
00370                          mask,
00371                          ampmax,
00372                          local_ampmax[i],
00373                          framesize/2);
00374 
00375         analysis("mask",frameno+i,mask,framesize/2,1,0);
00376 
00377         {
00378           vorbis_block vb;
00379           vorbis_dsp_state vd;
00380           memset(&vd,0,sizeof(vd));
00381           vd.vi=&vi;
00382           vb.vd=&vd;
00383           vb.pcmend=framesize;
00384 
00385           /* floor quantization/application */
00386           nonzero[i]=_floor_P[1]->forward(&vb,floor_look,
00387                                           mdct,
00388                                           logmdct,
00389                                           mask,
00390                                           logmax,
00391                                           
00392                                           flr[i]);
00393         }
00394 
00395         _vp_remove_floor(&p_look,
00396                          pg_look,
00397                          logmdct,
00398                          mdct,
00399                          flr[i],
00400                          pcm[i],
00401                          local_ampmax[i]);
00402 
00403         for(j=0;j<framesize/2;j++)
00404           if(fabs(pcm[i][j])>1500)
00405             fprintf(stderr,"%ld ",frameno+i);
00406         
00407         analysis("res",frameno+i,pcm[i],framesize/2,1,0);
00408         analysis("codedflr",frameno+i,flr[i],framesize/2,1,1);
00409       }
00410 
00411       /* residue prequantization */
00412       _vp_partition_prequant(&p_look,
00413                              &vi,
00414                              pcm,
00415                              nonzero);
00416         
00417       for(i=0;i<2;i++)
00418         analysis("quant",frameno+i,pcm[i],framesize/2,1,0);
00419 
00420       /* channel coupling / stereo quantization */
00421 
00422       _vp_couple(&p_look,
00423                  &mapping_info,
00424                  pcm,
00425                  nonzero);
00426   
00427       for(i=0;i<2;i++)
00428         analysis("coupled",frameno+i,pcm[i],framesize/2,1,0);
00429 
00430       /* decoupling */
00431       for(i=mapping_info.coupling_steps-1;i>=0;i--){
00432         float *pcmM=pcm[mapping_info.coupling_mag[i]];
00433         float *pcmA=pcm[mapping_info.coupling_ang[i]];
00434         
00435         for(j=0;j<framesize/2;j++){
00436           float mag=pcmM[j];
00437           float ang=pcmA[j];
00438           
00439           if(mag>0)
00440             if(ang>0){
00441               pcmM[j]=mag;
00442               pcmA[j]=mag-ang;
00443             }else{
00444               pcmA[j]=mag;
00445               pcmM[j]=mag+ang;
00446             }
00447           else
00448             if(ang>0){
00449               pcmM[j]=mag;
00450               pcmA[j]=mag+ang;
00451             }else{
00452               pcmA[j]=mag;
00453               pcmM[j]=mag-ang;
00454             }
00455         }
00456       }
00457     
00458       for(i=0;i<2;i++)
00459         analysis("decoupled",frameno+i,pcm[i],framesize/2,1,0);
00460 
00461       for(i=0;i<2;i++){
00462         float amp;
00463 
00464         for(j=0;j<framesize/2;j++)
00465           pcm[i][j]*=flr[i][j];
00466 
00467         analysis("final",frameno+i,pcm[i],framesize/2,1,1);
00468 
00469         /* take it back to time */
00470         mdct_backward(&m_look,pcm[i],pcm[i]);
00471 
00472         for(j=0;j<framesize/2;j++)
00473           out[i][j]+=pcm[i][j]*window[j];
00474 
00475         analysis("out",frameno+i,out[i],framesize/2,0,0);
00476 
00477 
00478       }
00479            
00480       /* write data.  Use the part of buffer we're about to shift out */
00481       for(i=0;i<2;i++){
00482         char  *ptr=buffer+i*2;
00483         float *mono=out[i];
00484         int flag=0;
00485         for(j=0;j<framesize/2;j++){
00486           int val=mono[j]*32767.;
00487           /* might as well guard against clipping */
00488           if(val>32767){
00489             if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
00490             flag=1;
00491             val=32767;
00492           }
00493           if(val<-32768){
00494             if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
00495             flag=1;
00496             val=-32768;
00497           }
00498           ptr[0]=val&0xff;
00499           ptr[1]=(val>>8)&0xff;
00500           ptr+=4;
00501         }
00502       }
00503  
00504       fprintf(stderr,"*");
00505       fwrite(buffer,1,framesize*2,stdout);
00506       memmove(buffer,buffer2,framesize*2);
00507 
00508       for(i=0;i<2;i++){
00509         for(j=0,k=framesize/2;j<framesize/2;j++,k++)
00510           out[i][j]=pcm[i][k]*window[k];
00511       }
00512       frameno+=2;
00513     }else
00514       eos=1;
00515   }
00516   fprintf(stderr,"average raw bits of entropy: %.03g/sample\n",acc/tot);
00517   fprintf(stderr,"average nonzero samples: %.03g/%d\n",nonz/tot*framesize/2,
00518           framesize/2);
00519   fprintf(stderr,"Done\n\n");
00520   return 0;
00521 }

Generated by  doxygen 1.6.2