New preecho detection/short block trigger code, replacing an IIR
[platform/upstream/libvorbis.git] / lib / envelope.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: PCM data envelope analysis 
14  last mod: $Id: envelope.c,v 1.42 2002/03/17 19:50:47 xiphmont Exp $
15
16  ********************************************************************/
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <math.h>
22 #include <ogg/ogg.h>
23 #include "vorbis/codec.h"
24 #include "codec_internal.h"
25
26 #include "os.h"
27 #include "scales.h"
28 #include "envelope.h"
29 #include "mdct.h"
30 #include "misc.h"
31
32 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
33   codec_setup_info *ci=vi->codec_setup;
34   vorbis_info_psy_global *gi=&ci->psy_g_param;
35   int ch=vi->channels;
36   int i;
37   int n=e->winlength=ci->blocksizes[0];
38   e->searchstep=ci->blocksizes[0]/VE_DIV; /* not random */
39
40   e->minenergy=fromdB(gi->preecho_minenergy);
41   e->ch=ch;
42   e->storage=128;
43   e->cursor=ci->blocksizes[1]/2;
44   e->mdct_win=_ogg_calloc(n,sizeof(*e->mdct_win));
45   mdct_init(&e->mdct,n);
46
47   for(i=0;i<n;i++){
48     e->mdct_win[i]=sin((i+.5)/n*M_PI);
49     e->mdct_win[i]*=e->mdct_win[i];
50   }
51
52   /* overlapping bands, assuming 22050 (which is not always true, but
53      to Hell with that) */
54   /* 2(1.3-3) 4(2.6-6) 8(5.3-12) 16(10.6-18) */
55
56   e->band[0].begin=rint(1300.f/22050.f*n/4.f)*2.f;
57   e->band[0].end=rint(3000.f/22050.f*n/4.f)*2.f-e->band[0].begin;
58   e->band[1].begin=rint(2600.f/22050.f*n/4.f)*2.f;
59   e->band[1].end=rint(6000.f/22050.f*n/4.f)*2.f-e->band[1].begin;
60   e->band[2].begin=rint(5300.f/22050.f*n/4.f)*2.f;
61   e->band[2].end=rint(12000.f/22050.f*n/4.f)*2.f-e->band[2].begin;
62   e->band[3].begin=rint(10600.f/22050.f*n/4.f)*2.f;
63   e->band[3].end=rint(18000.f/22050.f*n/4.f)*2.f-e->band[3].begin;
64
65   e->band[0].window=_ogg_malloc((e->band[0].end)*sizeof(*e->band[0].window));
66   e->band[1].window=_ogg_malloc((e->band[1].end)*sizeof(*e->band[1].window));
67   e->band[2].window=_ogg_malloc((e->band[2].end)*sizeof(*e->band[2].window));
68   e->band[3].window=_ogg_malloc((e->band[3].end)*sizeof(*e->band[3].window));
69   
70   n=e->band[0].end;
71   for(i=0;i<n;i++)
72     e->band[0].window[i]=sin((i+.5)/n*M_PI);
73   n=e->band[1].end;
74   for(i=0;i<n;i++)
75     e->band[1].window[i]=sin((i+.5)/n*M_PI);
76   n=e->band[2].end;
77   for(i=0;i<n;i++)
78     e->band[2].window[i]=sin((i+.5)/n*M_PI);
79   n=e->band[3].end;
80   for(i=0;i<n;i++)
81     e->band[3].window[i]=sin((i+.5)/n*M_PI);
82   
83   e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
84   e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));
85
86
87 }
88
89 void _ve_envelope_clear(envelope_lookup *e){
90   int i;
91   mdct_clear(&e->mdct);
92   for(i=0;i<VE_BANDS;i++)
93     _ogg_free(e->band[i].window);
94   _ogg_free(e->mdct_win);
95   _ogg_free(e->filter);
96   _ogg_free(e->mark);
97   memset(e,0,sizeof(*e));
98 }
99
100 /* fairly straight threshhold-by-band based until we find something
101    that works better and isn't patented. */
102 static int seq2=0;
103 static int _ve_amp(envelope_lookup *ve,
104                    vorbis_info_psy_global *gi,
105                    float *data,
106                    envelope_band *bands,
107                    envelope_filter_state *filters,
108                    long pos){
109   long n=ve->winlength;
110   int ret=0;
111   long i,j;
112
113   /* we want to have a 'minimum bar' for energy, else we're just
114      basing blocks on quantization noise that outweighs the signal
115      itself (for low power signals) */
116
117   float minV=ve->minenergy,acc[VE_BANDS];
118   float *vec=alloca(n*sizeof(*vec));
119   memset(acc,0,sizeof(acc));
120  
121  /* window and transform */
122   for(i=0;i<n;i++)
123     vec[i]=data[i]*ve->mdct_win[i];
124   mdct_forward(&ve->mdct,vec,vec);
125
126   /* accumulate amplitude by band */
127   for(j=0;j<VE_BANDS;j++){
128     for(i=0;i<bands[j].end;i++){
129       float val=vec[i+bands[j].begin];
130       acc[j]+=val*val*bands[j].window[i];
131     }
132     acc[j]/=i*.707f;
133     if(acc[j]<minV*minV)acc[j]=minV*minV;
134     acc[j]=todB(acc+j);
135   }
136
137   /* convert amplitude to delta */
138   for(j=0;j<VE_BANDS;j++){
139     float val=acc[j]-filters[j].ampbuf[filters[j].ampptr];
140     filters[j].ampbuf[filters[j].ampptr]=acc[j];
141     acc[j]=val;
142     filters[j].ampptr++;
143     if(filters[j].ampptr>=VE_DIV)filters[j].ampptr=0;
144   }
145
146   /* convolve deltas to threshhold values */
147   for(j=0;j<VE_BANDS;j++){
148     float *buf=filters[j].delbuf;
149     float val=.14*buf[0]+.14*buf[1]+.72*acc[j];
150     buf[0]=buf[1];buf[1]=acc[j];
151     acc[j]=val;
152   }
153
154   /* look at local min/max */
155   for(j=0;j<VE_BANDS;j++){
156     float *buf=filters[j].convbuf;
157     if(buf[1]>gi->preecho_thresh[j] && buf[0]<buf[1] && acc[j]<buf[1])ret=1;
158     if(buf[1]<gi->postecho_thresh[j] && buf[0]>buf[1] && acc[j]>buf[1])ret=1;
159     buf[0]=buf[1];buf[1]=acc[j];
160   }
161   return(ret);
162 }
163
164 static int seq=0;
165 long _ve_envelope_search(vorbis_dsp_state *v){
166   vorbis_info *vi=v->vi;
167   codec_setup_info *ci=vi->codec_setup;
168   vorbis_info_psy_global *gi=&ci->psy_g_param;
169   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
170   long i,j;
171
172   int first=ve->current/ve->searchstep;
173   int last=v->pcm_current/ve->searchstep-VE_DIV;
174   if(first<0)first=0;
175
176   /* make sure we have enough storage to match the PCM */
177   if(last>ve->storage){
178     ve->storage=last;
179     ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
180   }
181
182   for(j=first;j<last;j++){
183     int ret=0;
184     for(i=0;i<ve->ch;i++){
185       float *pcm=v->pcm[i]+ve->searchstep*(j+1);
186       ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
187     }
188     /* the mark delay is one searchstep because of min/max finder */
189     ve->mark[j]=ret;
190   }
191
192   ve->current=last*ve->searchstep;
193
194   {
195     long centerW=v->centerW;
196     long testW=
197       centerW+
198       ci->blocksizes[v->W]/4+
199       ci->blocksizes[1]/2+
200       ci->blocksizes[0]/4;
201     
202     j=ve->cursor;
203     
204     while(j<ve->current){
205       if(j>=testW)return(1);
206       if(ve->mark[j/ve->searchstep]){
207         if(j>centerW){
208
209           ve->curmark=j;
210
211           if(j>=testW)return(1);
212           return(0);
213         }
214       }
215       j+=ve->searchstep;
216       ve->cursor=j;
217     }
218   }
219  
220   return(-1);
221 }
222
223 int _ve_envelope_mark(vorbis_dsp_state *v){
224   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
225   vorbis_info *vi=v->vi;
226   codec_setup_info *ci=vi->codec_setup;
227   long centerW=v->centerW;
228   long beginW=centerW-ci->blocksizes[v->W]/4;
229   long endW=centerW+ci->blocksizes[v->W]/4;
230   if(v->W){
231     beginW-=ci->blocksizes[v->lW]/4;
232     endW+=ci->blocksizes[v->nW]/4;
233   }else{
234     beginW-=ci->blocksizes[0]/4;
235     endW+=ci->blocksizes[0]/4;
236   }
237
238   if(ve->curmark>=beginW && ve->curmark<endW)return(1);
239   {
240     long first=beginW/ve->searchstep;
241     long last=endW/ve->searchstep;
242     long i;
243     for(i=first;i<last;i++)
244       if(ve->mark[i])return(1);
245   }
246   return(0);
247 }
248
249 void _ve_envelope_shift(envelope_lookup *e,long shift){
250   int smallsize=e->current/e->searchstep;
251   int smallshift=shift/e->searchstep;
252   int i;
253
254   memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
255
256   e->current-=shift;
257   if(e->curmark>=0)
258     e->curmark-=shift;
259   e->cursor-=shift;
260 }
261
262