Build CMake install target for libogg dependency on AppVeyor
[platform/upstream/libvorbis.git] / lib / envelope.c
index 1999793..944a0a0 100644 (file)
@@ -5,13 +5,13 @@
  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  *                                                                  *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
- * by the XIPHOPHORUS Company http://www.xiph.org/                  *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
+ * by the Xiph.Org Foundation http://www.xiph.org/                  *
  *                                                                  *
  ********************************************************************
 
- function: PCM data envelope analysis 
- last mod: $Id: envelope.c,v 1.48 2002/04/01 00:49:41 xiphmont Exp $
+ function: PCM data envelope analysis
+ last mod: $Id$
 
  ********************************************************************/
 
@@ -51,12 +51,12 @@ void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
 
   /* magic follows */
   e->band[0].begin=2;  e->band[0].end=4;
-  e->band[1].begin=4;  e->band[1].end=8;
-  e->band[2].begin=6;  e->band[2].end=10;
-  e->band[3].begin=12; e->band[3].end=12;
-  e->band[4].begin=18; e->band[4].end=16;
-  e->band[5].begin=26; e->band[5].end=20;
-  e->band[6].begin=36; e->band[6].end=24;
+  e->band[1].begin=4;  e->band[1].end=5;
+  e->band[2].begin=6;  e->band[2].end=6;
+  e->band[3].begin=9;  e->band[3].end=8;
+  e->band[4].begin=13;  e->band[4].end=8;
+  e->band[5].begin=17;  e->band[5].end=8;
+  e->band[6].begin=22;  e->band[6].end=8;
 
   for(j=0;j<VE_BANDS;j++){
     n=e->band[j].end;
@@ -67,7 +67,7 @@ void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
     }
     e->band[j].total=1./e->band[j].total;
   }
-  
+
   e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
   e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));
 
@@ -88,11 +88,10 @@ void _ve_envelope_clear(envelope_lookup *e){
    that works better and isn't patented. */
 
 static int _ve_amp(envelope_lookup *ve,
-                  vorbis_info_psy_global *gi,
-                  float *data,
-                  envelope_band *bands,
-                  envelope_filter_state *filters,
-                  long pos){
+                   vorbis_info_psy_global *gi,
+                   float *data,
+                   envelope_band *bands,
+                   envelope_filter_state *filters){
   long n=ve->winlength;
   int ret=0;
   long i,j;
@@ -111,15 +110,15 @@ static int _ve_amp(envelope_lookup *ve,
   float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH);
   if(penalty<0.f)penalty=0.f;
   if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty;
-  
+
   /*_analysis_output_always("lpcm",seq2,data,n,0,0,
     totalshift+pos*ve->searchstep);*/
-  
+
  /* window and transform */
   for(i=0;i<n;i++)
     vec[i]=data[i]*ve->mdct_win[i];
   mdct_forward(&ve->mdct,vec,vec);
-  
+
   /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */
 
   /* near-DC spreading function; this has nothing to do with
@@ -145,7 +144,7 @@ static int _ve_amp(envelope_lookup *ve,
     if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
     decay=todB(&decay)*.5-15.f;
   }
-  
+
   /* perform spreading and limiting, also smooth the spectrum.  yes,
      the MDCT results in all real coefficients, but it still *behaves*
      like real/imaginary pairs */
@@ -159,7 +158,7 @@ static int _ve_amp(envelope_lookup *ve,
   }
 
   /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/
-  
+
   /* perform preecho/postecho triggering by band */
   for(j=0;j<VE_BANDS;j++){
     float acc=0.;
@@ -168,30 +167,31 @@ static int _ve_amp(envelope_lookup *ve,
     /* accumulate amplitude */
     for(i=0;i<bands[j].end;i++)
       acc+=vec[i+bands[j].begin]*bands[j].window[i];
-   
+
     acc*=bands[j].total;
 
     /* convert amplitude to delta */
     {
       int p,this=filters[j].ampptr;
       float postmax,postmin,premax=-99999.f,premin=99999.f;
-      
+
       p=this;
       p--;
       if(p<0)p+=VE_AMP;
       postmax=max(acc,filters[j].ampbuf[p]);
       postmin=min(acc,filters[j].ampbuf[p]);
-      
+
       for(i=0;i<stretch;i++){
-       p--;
-       if(p<0)p+=VE_AMP;
-       premax=max(premax,filters[j].ampbuf[p]);
-       premin=min(premin,filters[j].ampbuf[p]);
+        p--;
+        if(p<0)p+=VE_AMP;
+        premax=max(premax,filters[j].ampbuf[p]);
+        premin=min(premin,filters[j].ampbuf[p]);
       }
-      
+
       valmin=postmin-premin;
       valmax=postmax-premax;
 
+      /*filters[j].markers[pos]=valmax;*/
       filters[j].ampbuf[this]=acc;
       filters[j].ampptr++;
       if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
@@ -204,15 +204,20 @@ static int _ve_amp(envelope_lookup *ve,
     }
     if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
   }
+
   return(ret);
 }
 
+#if 0
+static int seq=0;
+static ogg_int64_t totalshift=-1024;
+#endif
+
 long _ve_envelope_search(vorbis_dsp_state *v){
   vorbis_info *vi=v->vi;
   codec_setup_info *ci=vi->codec_setup;
   vorbis_info_psy_global *gi=&ci->psy_g_param;
-  envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
+  envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
   long i,j;
 
   int first=ve->current/ve->searchstep;
@@ -220,8 +225,8 @@ long _ve_envelope_search(vorbis_dsp_state *v){
   if(first<0)first=0;
 
   /* make sure we have enough storage to match the PCM */
-  if(last>ve->storage){
-    ve->storage=last+VE_WIN;
+  if(last+VE_WIN+VE_POST>ve->storage){
+    ve->storage=last+VE_WIN+VE_POST; /* be sure */
     ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
   }
 
@@ -231,10 +236,10 @@ long _ve_envelope_search(vorbis_dsp_state *v){
     ve->stretch++;
     if(ve->stretch>VE_MAXSTRETCH*2)
       ve->stretch=VE_MAXSTRETCH*2;
-    
+
     for(i=0;i<ve->ch;i++){
       float *pcm=v->pcm[i]+ve->searchstep*(j);
-      ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
+      ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS);
     }
 
     ve->mark[j+VE_POST]=0;
@@ -260,70 +265,70 @@ long _ve_envelope_search(vorbis_dsp_state *v){
       ci->blocksizes[v->W]/4+
       ci->blocksizes[1]/2+
       ci->blocksizes[0]/4;
-    
+
     j=ve->cursor;
-    
+
     while(j<ve->current-(ve->searchstep)){/* account for postecho
                                              working back one window */
       if(j>=testW)return(1);
+
       ve->cursor=j;
 
       if(ve->mark[j/ve->searchstep]){
-       if(j>centerW){
-
-         #if 0
-         if(j>ve->curmark){
-           float *marker=alloca(v->pcm_current*sizeof(*marker));
-           int l,m;
-           memset(marker,0,sizeof(*marker)*v->pcm_current);
-           fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
-                   seq,
-                   (totalshift+ve->cursor)/44100.,
-                   (totalshift+j)/44100.);
-           _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
-           _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
-
-           _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
-           _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
-           
-           for(m=0;m<VE_BANDS;m++){
-             char buf[80];
-             sprintf(buf,"delL%d",m);
-             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
-             _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
-           }
-
-           for(m=0;m<VE_BANDS;m++){
-             char buf[80];
-             sprintf(buf,"delR%d",m);
-             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
-             _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
-           }
-
-
-           for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->stretchm[l]*.1;
-           _analysis_output_always("stretch",seq,marker,v->pcm_current,0,0,totalshift);
-           
-           seq++;
-           
-         }
-         #endif
-
-         ve->curmark=j;
-         if(j>=testW)return(1);
-         return(0);
-       }
+        if(j>centerW){
+
+#if 0
+          if(j>ve->curmark){
+            float *marker=alloca(v->pcm_current*sizeof(*marker));
+            int l,m;
+            memset(marker,0,sizeof(*marker)*v->pcm_current);
+            fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
+                    seq,
+                    (totalshift+ve->cursor)/44100.,
+                    (totalshift+j)/44100.);
+            _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
+            _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
+
+            _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
+            _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
+
+            for(m=0;m<VE_BANDS;m++){
+              char buf[80];
+              sprintf(buf,"delL%d",m);
+              for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
+              _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
+            }
+
+            for(m=0;m<VE_BANDS;m++){
+              char buf[80];
+              sprintf(buf,"delR%d",m);
+              for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
+              _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
+            }
+
+            for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->mark[l]*.4;
+            _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift);
+
+
+            seq++;
+
+          }
+#endif
+
+          ve->curmark=j;
+          if(j>=testW)return(1);
+          return(0);
+        }
       }
       j+=ve->searchstep;
     }
   }
-  
+
   return(-1);
 }
 
 int _ve_envelope_mark(vorbis_dsp_state *v){
-  envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
+  envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
   vorbis_info *vi=v->vi;
   codec_setup_info *ci=vi->codec_setup;
   long centerW=v->centerW;
@@ -349,29 +354,22 @@ int _ve_envelope_mark(vorbis_dsp_state *v){
 }
 
 void _ve_envelope_shift(envelope_lookup *e,long shift){
-  int smallsize=e->current/e->searchstep; 
+  int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
+                                                     ahead of ve->current */
   int smallshift=shift/e->searchstep;
-  int i;
 
   memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
-  
+
 #if 0
   for(i=0;i<VE_BANDS*e->ch;i++)
     memmove(e->filter[i].markers,
-           e->filter[i].markers+smallshift,
-           (1024-smallshift)*sizeof(*(*e->filter).markers));
-  memmove(e->stretchm,e->stretchm+smallshift,(smallsize-smallshift)*sizeof(*e->stretchm));
+            e->filter[i].markers+smallshift,
+            (1024-smallshift)*sizeof(*(*e->filter).markers));
   totalshift+=shift;
-#endif 
+#endif
 
   e->current-=shift;
   if(e->curmark>=0)
     e->curmark-=shift;
   e->cursor-=shift;
 }
-
-
-
-
-
-