examples/sfexamples/oggvorbiscodec/src/libvorbis/lib/envelope.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: PCM data envelope analysis 
00014  last mod: $Id: envelope.c 8921 2005-02-14 23:03:43Z msmith $
00015 
00016  ********************************************************************/
00017 
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include "ogg/ogg.h"
00023 #include "vorbis/codec.h"
00024 #include "codec_internal.h"
00025 
00026 #include "os.h"
00027 #include "scales.h"
00028 #include "envelope.h"
00029 #include "mdct.h"
00030 #include "misc.h"
00031 
00032 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
00033   codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
00034   vorbis_info_psy_global *gi=&ci->psy_g_param;
00035   int ch=vi->channels;
00036   int i,j;
00037   int n=e->winlength=128;
00038   e->searchstep=64; /* not random */
00039 
00040   e->minenergy=gi->preecho_minenergy;
00041   e->ch=ch;
00042   e->storage=128;
00043   e->cursor=ci->blocksizes[1]/2;
00044   e->mdct_win=(float*)_ogg_calloc(n,sizeof(*e->mdct_win));
00045   mdct_init(&e->mdct,n);
00046 
00047   for(i=0;i<n;i++){
00048     e->mdct_win[i]=sin(i/(n-1.)*M_PI);
00049     e->mdct_win[i]*=e->mdct_win[i];
00050   }
00051 
00052   /* magic follows */
00053   e->band[0].begin=2;  e->band[0].end=4;
00054   e->band[1].begin=4;  e->band[1].end=5;
00055   e->band[2].begin=6;  e->band[2].end=6;
00056   e->band[3].begin=9;  e->band[3].end=8;
00057   e->band[4].begin=13;  e->band[4].end=8;
00058   e->band[5].begin=17;  e->band[5].end=8;
00059   e->band[6].begin=22;  e->band[6].end=8;
00060 
00061   for(j=0;j<VE_BANDS;j++){
00062     n=e->band[j].end;
00063     e->band[j].window=(float*)_ogg_malloc(n*sizeof(*e->band[0].window));
00064     for(i=0;i<n;i++){
00065       e->band[j].window[i]=sin((i+.5)/n*M_PI);
00066       e->band[j].total+=e->band[j].window[i];
00067     }
00068     e->band[j].total=1./e->band[j].total;
00069   }
00070   
00071   e->filter=(envelope_filter_state*)_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
00072   e->mark=(int*)_ogg_calloc(e->storage,sizeof(*e->mark));
00073 
00074 }
00075 
00076 void _ve_envelope_clear(envelope_lookup *e){
00077   int i;
00078   mdct_clear(&e->mdct);
00079   for(i=0;i<VE_BANDS;i++)
00080     _ogg_free(e->band[i].window);
00081   _ogg_free(e->mdct_win);
00082   _ogg_free(e->filter);
00083   _ogg_free(e->mark);
00084   memset(e,0,sizeof(*e));
00085 }
00086 
00087 /* fairly straight threshhold-by-band based until we find something
00088    that works better and isn't patented. */
00089 
00090 static int _ve_amp(envelope_lookup *ve,
00091                    vorbis_info_psy_global *gi,
00092                    float *data,
00093                    envelope_band *bands,
00094                    envelope_filter_state *filters,
00095                    long pos){
00096   long n=ve->winlength;
00097   int ret=0;
00098   long i,j;
00099   float decay;
00100 
00101   /* we want to have a 'minimum bar' for energy, else we're just
00102      basing blocks on quantization noise that outweighs the signal
00103      itself (for low power signals) */
00104 
00105   float minV=ve->minenergy;
00106   float *vec= (float*)_ogg_malloc(n*sizeof(*vec));//alloca(n*sizeof(*vec)); //patch
00107   float *vec_cpy = vec ; //patch
00108   
00109   /* stretch is used to gradually lengthen the number of windows
00110      considered prevoius-to-potential-trigger */
00111   int stretch=max(VE_MINSTRETCH,ve->stretch/2);
00112   float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH);
00113   if(penalty<0.f)penalty=0.f;
00114   if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty;
00115   pos = pos; //warning
00116   /*_analysis_output_always("lpcm",seq2,data,n,0,0,
00117     totalshift+pos*ve->searchstep);*/
00118   
00119  /* window and transform */
00120   for(i=0;i<n;i++)
00121     vec[i]=data[i]*ve->mdct_win[i];
00122   mdct_forward(&ve->mdct,vec,vec);
00123   
00124   /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */
00125 
00126   /* near-DC spreading function; this has nothing to do with
00127      psychoacoustics, just sidelobe leakage and window size */
00128   {
00129     float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
00130     int ptr=filters->nearptr;
00131 
00132     /* the accumulation is regularly refreshed from scratch to avoid
00133        floating point creep */
00134     if(ptr==0){
00135       decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
00136       filters->nearDC_partialacc=temp;
00137     }else{
00138       decay=filters->nearDC_acc+=temp;
00139       filters->nearDC_partialacc+=temp;
00140     }
00141     filters->nearDC_acc-=filters->nearDC[ptr];
00142     filters->nearDC[ptr]=temp;
00143 
00144     decay*=(1./(VE_NEARDC+1));
00145     filters->nearptr++;
00146     if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
00147     decay=todB(&decay)*.5-15.f;
00148   }
00149   
00150   /* perform spreading and limiting, also smooth the spectrum.  yes,
00151      the MDCT results in all real coefficients, but it still *behaves*
00152      like real/imaginary pairs */
00153   for(i=0;i<n/2;i+=2){
00154     float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
00155     val=todB(&val)*.5f;
00156     if(val<decay)val=decay;
00157     if(val<minV)val=minV;
00158     vec[i>>1]=val;
00159     decay-=8.;
00160   }
00161 
00162   /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/
00163   
00164   /* perform preecho/postecho triggering by band */
00165   for(j=0;j<VE_BANDS;j++){
00166     float acc=0.;
00167     float valmax,valmin;
00168 
00169     /* accumulate amplitude */
00170     for(i=0;i<bands[j].end;i++)
00171       acc+=vec[i+bands[j].begin]*bands[j].window[i];
00172    
00173     acc*=bands[j].total;
00174 
00175     /* convert amplitude to delta */
00176     {
00177       int p,temp=filters[j].ampptr;
00178       float postmax,postmin,premax=-99999.f,premin=99999.f;
00179       
00180       p=temp;
00181       p--;
00182       if(p<0)p+=VE_AMP;
00183       postmax=max(acc,filters[j].ampbuf[p]);
00184       postmin=min(acc,filters[j].ampbuf[p]);
00185       
00186       for(i=0;i<stretch;i++){
00187         p--;
00188         if(p<0)p+=VE_AMP;
00189         premax=max(premax,filters[j].ampbuf[p]);
00190         premin=min(premin,filters[j].ampbuf[p]);
00191       }
00192       
00193       valmin=postmin-premin;
00194       valmax=postmax-premax;
00195 
00196       /*filters[j].markers[pos]=valmax;*/
00197       filters[j].ampbuf[temp]=acc;
00198       filters[j].ampptr++;
00199       if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
00200     }
00201 
00202     /* look at min/max, decide trigger */
00203     if(valmax>gi->preecho_thresh[j]+penalty){
00204       ret|=1;
00205       ret|=4;
00206     }
00207     if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
00208   }
00209   
00210   free(vec_cpy); //patch        
00211   return(ret);
00212 }
00213 
00214 #if 0
00215 static int seq=0;
00216 static ogg_int64_t totalshift=-1024;
00217 #endif
00218 
00219 long _ve_envelope_search(vorbis_dsp_state *v){
00220   vorbis_info *vi=v->vi;
00221   codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
00222   vorbis_info_psy_global *gi=&ci->psy_g_param;
00223   envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
00224   long i,j;
00225 
00226   int first=ve->current/ve->searchstep;
00227   int last=v->pcm_current/ve->searchstep-VE_WIN;
00228   if(first<0)first=0;
00229 
00230   /* make sure we have enough storage to match the PCM */
00231   if(last+VE_WIN+VE_POST>ve->storage){
00232     ve->storage=last+VE_WIN+VE_POST; /* be sure */
00233     ve->mark=(int*)_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
00234   }
00235 
00236   for(j=first;j<last;j++){
00237     int ret=0;
00238 
00239     ve->stretch++;
00240     if(ve->stretch>VE_MAXSTRETCH*2)
00241       ve->stretch=VE_MAXSTRETCH*2;
00242     
00243     for(i=0;i<ve->ch;i++){
00244       float *pcm=v->pcm[i]+ve->searchstep*(j);
00245       ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
00246     }
00247 
00248     ve->mark[j+VE_POST]=0;
00249     if(ret&1){
00250       ve->mark[j]=1;
00251       ve->mark[j+1]=1;
00252     }
00253 
00254     if(ret&2){
00255       ve->mark[j]=1;
00256       if(j>0)ve->mark[j-1]=1;
00257     }
00258 
00259     if(ret&4)ve->stretch=-1;
00260   }
00261 
00262   ve->current=last*ve->searchstep;
00263 
00264   {
00265     long centerW=v->centerW;
00266     long testW=
00267       centerW+
00268       ci->blocksizes[v->W]/4+
00269       ci->blocksizes[1]/2+
00270       ci->blocksizes[0]/4;
00271     
00272     j=ve->cursor;
00273     
00274     while(j<ve->current-(ve->searchstep)){/* account for postecho
00275                                              working back one window */
00276       if(j>=testW)return(1);
00277  
00278       ve->cursor=j;
00279 
00280       if(ve->mark[j/ve->searchstep]){
00281         if(j>centerW){
00282 
00283 #if 0
00284           if(j>ve->curmark){
00285             float *marker=alloca(v->pcm_current*sizeof(*marker));
00286             int l,m;
00287             memset(marker,0,sizeof(*marker)*v->pcm_current);
00288             fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
00289                     seq,
00290                     (totalshift+ve->cursor)/44100.,
00291                     (totalshift+j)/44100.);
00292             _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
00293             _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
00294 
00295             _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
00296             _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
00297             
00298             for(m=0;m<VE_BANDS;m++){
00299               char buf[80];
00300               sprintf(buf,"delL%d",m);
00301               for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
00302               _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
00303             }
00304 
00305             for(m=0;m<VE_BANDS;m++){
00306               char buf[80];
00307               sprintf(buf,"delR%d",m);
00308               for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
00309               _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
00310             }
00311 
00312             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->mark[l]*.4;
00313             _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift);
00314            
00315             
00316             seq++;
00317             
00318           }
00319 #endif
00320 
00321           ve->curmark=j;
00322           if(j>=testW)return(1);
00323           return(0);
00324         }
00325       }
00326       j+=ve->searchstep;
00327     }
00328   }
00329   
00330   return(-1);
00331 }
00332 
00333 int _ve_envelope_mark(vorbis_dsp_state *v){
00334   envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
00335   vorbis_info *vi=v->vi;
00336   codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
00337   long centerW=v->centerW;
00338   long beginW=centerW-ci->blocksizes[v->W]/4;
00339   long endW=centerW+ci->blocksizes[v->W]/4;
00340   if(v->W){
00341     beginW-=ci->blocksizes[v->lW]/4;
00342     endW+=ci->blocksizes[v->nW]/4;
00343   }else{
00344     beginW-=ci->blocksizes[0]/4;
00345     endW+=ci->blocksizes[0]/4;
00346   }
00347 
00348   if(ve->curmark>=beginW && ve->curmark<endW)return(1);
00349   {
00350     long first=beginW/ve->searchstep;
00351     long last=endW/ve->searchstep;
00352     long i;
00353     for(i=first;i<last;i++)
00354       if(ve->mark[i])return(1);
00355   }
00356   return(0);
00357 }
00358 
00359 void _ve_envelope_shift(envelope_lookup *e,long shift){
00360   int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
00361                                                      ahead of ve->current */
00362   int smallshift=shift/e->searchstep;
00363 
00364   memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
00365   
00366 #if 0
00367   for(i=0;i<VE_BANDS*e->ch;i++)
00368     memmove(e->filter[i].markers,
00369             e->filter[i].markers+smallshift,
00370             (1024-smallshift)*sizeof(*(*e->filter).markers));
00371   totalshift+=shift;
00372 #endif 
00373 
00374   e->current-=shift;
00375   if(e->curmark>=0)
00376     e->curmark-=shift;
00377   e->cursor-=shift;
00378 }
00379 
00380 
00381 
00382 
00383 
00384 

Generated by  doxygen 1.6.2