Build CMake install target for libogg dependency on AppVeyor
[platform/upstream/libvorbis.git] / lib / envelope.c
index 1a93304..944a0a0 100644 (file)
@@ -1,24 +1,17 @@
 /********************************************************************
  *                                                                  *
- * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
- * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
- * PLEASE READ THESE TERMS DISTRIBUTING.                            *
+ * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  *                                                                  *
- * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999             *
- * by 1999 Monty <monty@xiph.org> and 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 and manipulation
- author: Monty <xiphmont@mit.edu>
- modifications by: Monty
- last modification date: Oct 06 1999
-
- Vorbis manipulates the dynamic range of the incoming PCM data
- envelope to minimise time-domain energy leakage from percussive and
- plosive waveforms being quantized in the MDCT domain.
+ function: PCM data envelope analysis
+ last mod: $Id$
 
  ********************************************************************/
 
 #include <string.h>
 #include <stdio.h>
 #include <math.h>
+#include <ogg/ogg.h>
+#include "vorbis/codec.h"
+#include "codec_internal.h"
 
-#include "codec.h"
+#include "os.h"
+#include "scales.h"
 #include "envelope.h"
-#include "bitwise.h"
-
-void _ve_envelope_init(envelope_lookup *e,int samples_per){
-  int i;
+#include "mdct.h"
+#include "misc.h"
 
-  e->winlen=samples_per*2;
-  e->window=malloc(e->winlen*sizeof(double));
+void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
+  codec_setup_info *ci=vi->codec_setup;
+  vorbis_info_psy_global *gi=&ci->psy_g_param;
+  int ch=vi->channels;
+  int i,j;
+  int n=e->winlength=128;
+  e->searchstep=64; /* not random */
+
+  e->minenergy=gi->preecho_minenergy;
+  e->ch=ch;
+  e->storage=128;
+  e->cursor=ci->blocksizes[1]/2;
+  e->mdct_win=_ogg_calloc(n,sizeof(*e->mdct_win));
+  mdct_init(&e->mdct,n);
+
+  for(i=0;i<n;i++){
+    e->mdct_win[i]=sin(i/(n-1.)*M_PI);
+    e->mdct_win[i]*=e->mdct_win[i];
+  }
 
-  /* We just use a straight sin^2(x) window for this */
-  for(i=0;i<e->winlen;i++){
-    double temp=sin((i+.5)/e->winlen*M_PI);
-    e->window[i]=temp*temp;
+  /* magic follows */
+  e->band[0].begin=2;  e->band[0].end=4;
+  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;
+    e->band[j].window=_ogg_malloc(n*sizeof(*e->band[0].window));
+    for(i=0;i<n;i++){
+      e->band[j].window[i]=sin((i+.5)/n*M_PI);
+      e->band[j].total+=e->band[j].window[i];
+    }
+    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));
+
 }
 
 void _ve_envelope_clear(envelope_lookup *e){
-  if(e->window)free(e->window);
-  memset(e,0,sizeof(envelope_lookup));
+  int i;
+  mdct_clear(&e->mdct);
+  for(i=0;i<VE_BANDS;i++)
+    _ogg_free(e->band[i].window);
+  _ogg_free(e->mdct_win);
+  _ogg_free(e->filter);
+  _ogg_free(e->mark);
+  memset(e,0,sizeof(*e));
 }
 
-/* initial and final blocks are special cases. Eg:
-   ______
-         `--_            
-   |_______|_`-.___|_______|_______|
-
-              ___                     
-          _--'   `--_     
-   |___.-'_|_______|_`-.___|_______|
-
-                      ___                     
-                  _--'   `--_     
-   |_______|___.-'_|_______|_`-.___|
-
-                               _____
-                           _--'
-   |_______|_______|____.-'|_______|
-   as we go block by block, we watch the collective metrics span. If we 
-   span the threshhold (assuming the threshhold is active), we use an 
-   abbreviated vector */
-
-static int _ve_envelope_generate(double *mult,double *env,double *look,
-                                int n,int step){
-  int i,j,p;
-  double m,mo;
-
-  for(j=0;j<n;j++)if(mult[j]!=1)break;
-  if(j==n)return(0);
-
-  n*=step;
-  /* first multiplier special case */
-  m=ldexp(1,mult[0]-1);
-  for(p=0;p<step/2;p++)env[p]=m;
-  
-  /* mid multipliers normal case */
-  for(j=1;p<n-step/2;j++){
-    mo=m;
-    m=ldexp(1,mult[j]-1);
-    if(mo==m)
-      for(i=0;i<step;i++,p++)env[p]=m;
-    else
-      for(i=0;i<step;i++,p++)env[p]=m*look[i]+mo*look[i+step];
-  }
+/* fairly straight threshhold-by-band based until we find something
+   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 n=ve->winlength;
+  int ret=0;
+  long i,j;
+  float decay;
+
+  /* we want to have a 'minimum bar' for energy, else we're just
+     basing blocks on quantization noise that outweighs the signal
+     itself (for low power signals) */
+
+  float minV=ve->minenergy;
+  float *vec=alloca(n*sizeof(*vec));
+
+  /* stretch is used to gradually lengthen the number of windows
+     considered prevoius-to-potential-trigger */
+  int stretch=max(VE_MINSTRETCH,ve->stretch/2);
+  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
+     psychoacoustics, just sidelobe leakage and window size */
+  {
+    float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
+    int ptr=filters->nearptr;
+
+    /* the accumulation is regularly refreshed from scratch to avoid
+       floating point creep */
+    if(ptr==0){
+      decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
+      filters->nearDC_partialacc=temp;
+    }else{
+      decay=filters->nearDC_acc+=temp;
+      filters->nearDC_partialacc+=temp;
+    }
+    filters->nearDC_acc-=filters->nearDC[ptr];
+    filters->nearDC[ptr]=temp;
 
-  /* last multiplier special case */
-  for(;p<n;p++)env[p]=m;
-  return(1);
-}
+    decay*=(1./(VE_NEARDC+1));
+    filters->nearptr++;
+    if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
+    decay=todB(&decay)*.5-15.f;
+  }
 
-/* right now, we do things simple and dirty (read: our current preecho
-   is a joke).  Should this prove inadequate, then we'll think of
-   something different.  The details of the encoding format do not
-   depend on the exact behavior, only the format of the bits that come
-   out.
-
-   Mark Taylor probably has much witter ways of doing this...  Let's
-   see if simple delta analysis gives us acceptible results for now.  */
-
-static void _ve_deltas(double *deltas,double *pcm,int n,double *win,
-                      int winsize){
-  int i,j,p=winsize/2;
-  for(j=0;j<n;j++){
-    p-=winsize/2;
-    for(i=0;i<winsize-1;i++,p++){
-      double temp=fabs(win[i]*pcm[p]-win[i+1]*pcm[p+1]);
-      if(deltas[j]<temp)deltas[j]=temp;
-    }
-    p++;
+  /* perform spreading and limiting, also smooth the spectrum.  yes,
+     the MDCT results in all real coefficients, but it still *behaves*
+     like real/imaginary pairs */
+  for(i=0;i<n/2;i+=2){
+    float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
+    val=todB(&val)*.5f;
+    if(val<decay)val=decay;
+    if(val<minV)val=minV;
+    vec[i>>1]=val;
+    decay-=8.;
   }
-}
 
-void _ve_envelope_multipliers(vorbis_dsp_state *v){
-  vorbis_info *vi=v->vi;
-  int step=vi->envelopesa;
-  static int frame=0;
-
-  /* we need a 1-1/4 envelope window overlap begin and 1/4 end */
-  int dtotal=(v->pcm_current-step/2)/vi->envelopesa;
-  int dcurr=v->envelope_current;
-  double *window=v->ve.window;
-  int winlen=v->ve.winlen;
-  int pch,ech;
-  
-  if(dtotal>dcurr){
-    for(ech=0;ech<vi->envelopech;ech++){
-      double *mult=v->multipliers[ech]+dcurr;
-      memset(mult,0,sizeof(double)*(dtotal-dcurr));
-      
-      for(pch=0;pch<vi->channels;pch++){
-       
-       /* does this channel contribute to the envelope analysis */
-       /*if(vi->envelopemap[pch]==ech){ not mapping yet */
-       if(pch==ech){
-
-         /* we need a 1/4 envelope window overlap front and back */
-         double *pcm=v->pcm[pch]+dcurr*step-step/2;
-         _ve_deltas(mult,pcm,dtotal-dcurr,window,winlen);
-
-       }
+  /*_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.;
+    float valmax,valmin;
+
+    /* 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]);
       }
+
+      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;
     }
-    v->envelope_current=dtotal;
-    frame++;
-  }
-}
 
-/* This readies the multiplier vector for use/coding.  Clamp/adjust
-   the multipliers to the allowed range and eliminate unneeded
-   coefficients */
-
-void _ve_envelope_sparsify(vorbis_block *vb){
-  vorbis_info *vi=vb->vd->vi;
-  int ch;
-  for(ch=0;ch<vi->envelopech;ch++){
-    int flag=0;
-    double *mult=vb->mult[ch];
-    int n=vb->multend;
-    double first=mult[0];
-    double last=first;
-    double clamp;
-    int i;
-
-    /* are we going to multiply anything? */
-    
-    for(i=1;i<n;i++){
-      if(mult[i]>=last*vi->preecho_thresh){
-       flag=1;
-       break;
-      }
-      if(i<n-1 && mult[i+1]>=last*vi->preecho_thresh){
-       flag=1;
-       break;
-      }
-      last=mult[i];
+    /* look at min/max, decide trigger */
+    if(valmax>gi->preecho_thresh[j]+penalty){
+      ret|=1;
+      ret|=4;
     }
-    
-    if(flag){
-      /* we need to adjust, so we might as well go nuts */
-      
-      int begin=i;
-      clamp=last?last:1;
-      
-      for(i=0;i<begin;i++)mult[i]=0;
-      
-      last=1;
-      for(;i<n;i++){
-       if(mult[i]/last>clamp*vi->preecho_thresh){
-         last=mult[i]/vi->preecho_clamp;
-         
-         mult[i]=floor(log(mult[i]/clamp/vi->preecho_clamp)/log(2))-1;
-         if(mult[i]>15)mult[i]=15;
-         if(mult[i]<1)mult[i]=1;
-
-       }else{
-         mult[i]=0;
-       }
-      }  
-    }else
-      memset(mult,0,sizeof(double)*n);
+    if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
   }
+
+  return(ret);
 }
 
-void _ve_envelope_apply(vorbis_block *vb,int multp){
-  vorbis_info *vi=vb->vd->vi;
-  double env[vb->multend*vi->envelopesa];
-  envelope_lookup *look=&vb->vd->ve;
-  int i,j;
-  
-  for(i=0;i<vi->envelopech;i++){
-    double *mult=vb->mult[i];
-    double last=1.;
-    
-    /* fill in the multiplier placeholders */
-
-    for(j=0;j<vb->multend;j++){
-      if(mult[j]){
-       last=mult[j];
-      }else{
-       mult[j]=last;
-      }
-    }
+#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=((private_state *)(v->backend_state))->ve;
+  long i,j;
+
+  int first=ve->current/ve->searchstep;
+  int last=v->pcm_current/ve->searchstep-VE_WIN;
+  if(first<0)first=0;
+
+  /* make sure we have enough storage to match the PCM */
+  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));
+  }
+
+  for(j=first;j<last;j++){
+    int ret=0;
 
-    /* compute the envelope curve */
-    if(_ve_envelope_generate(mult,env,look->window,vb->multend,
-                            vi->envelopesa)){
-      if(multp)
-       for(j=0;j<vb->multend*vi->envelopesa;j++)
-         vb->pcm[i][j]*=env[j];
-      else
-       for(j=0;j<vb->multend*vi->envelopesa;j++)
-         vb->pcm[i][j]/=env[j];
+    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);
     }
-  }
-}
 
-int _ve_envelope_encode(vorbis_block *vb){
-  /* Not huffcoded yet. */
+    ve->mark[j+VE_POST]=0;
+    if(ret&1){
+      ve->mark[j]=1;
+      ve->mark[j+1]=1;
+    }
 
-  vorbis_info *vi=vb->vd->vi;
-  int scale=vb->W;
-  int n=vb->multend;
-  int i,j;
+    if(ret&2){
+      ve->mark[j]=1;
+      if(j>0)ve->mark[j-1]=1;
+    }
 
-  /* range is 0-15 */
+    if(ret&4)ve->stretch=-1;
+  }
 
-  for(i=0;i<vi->envelopech;i++){
-    double *mult=vb->mult[i];
-    for(j=0;j<n;j++){
-      _oggpack_write(&vb->opb,(int)(mult[j]),4);
+  ve->current=last*ve->searchstep;
+
+  {
+    long centerW=v->centerW;
+    long testW=
+      centerW+
+      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->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(0);
-}
 
-/* synthesis expands the buffers in vb if needed.  We can assume we
-   have enough storage handed to us. */
-int _ve_envelope_decode(vorbis_block *vb){
-  vorbis_info *vi=vb->vd->vi;
-  int scale=vb->W;
-  int n=vb->multend;
-  int i,j;
+  return(-1);
+}
 
-  /* range is 0-15 */
+int _ve_envelope_mark(vorbis_dsp_state *v){
+  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;
+  long beginW=centerW-ci->blocksizes[v->W]/4;
+  long endW=centerW+ci->blocksizes[v->W]/4;
+  if(v->W){
+    beginW-=ci->blocksizes[v->lW]/4;
+    endW+=ci->blocksizes[v->nW]/4;
+  }else{
+    beginW-=ci->blocksizes[0]/4;
+    endW+=ci->blocksizes[0]/4;
+  }
 
-  for(i=0;i<vi->envelopech;i++){
-    double *mult=vb->mult[i];
-    for(j=0;j<n;j++)
-      mult[j]=_oggpack_read(&vb->opb,4);
+  if(ve->curmark>=beginW && ve->curmark<endW)return(1);
+  {
+    long first=beginW/ve->searchstep;
+    long last=endW/ve->searchstep;
+    long i;
+    for(i=first;i<last;i++)
+      if(ve->mark[i])return(1);
   }
   return(0);
 }
 
-
-
-
-
+void _ve_envelope_shift(envelope_lookup *e,long shift){
+  int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
+                                                     ahead of ve->current */
+  int smallshift=shift/e->searchstep;
+
+  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));
+  totalshift+=shift;
+#endif
+
+  e->current-=shift;
+  if(e->curmark>=0)
+    e->curmark-=shift;
+  e->cursor-=shift;
+}