Continued preecho tuning/fixes. Gone to average dB with even/odd
[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.44 2002/03/24 21:04:00 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=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(3000.f/22050.f*n/4.f)*2.f;
57   e->band[0].end=rint(6000.f/22050.f*n/4.f)*2.f-e->band[0].begin;
58   e->band[1].begin=rint(5000.f/22050.f*n/4.f)*2.f;
59   e->band[1].end=rint(10000.f/22050.f*n/4.f)*2.f-e->band[1].begin;
60   e->band[2].begin=rint(8000.f/22050.f*n/4.f)*2.f;
61   e->band[2].end=rint(16000.f/22050.f*n/4.f)*2.f-e->band[2].begin;
62   e->band[3].begin=rint(12000.f/22050.f*n/4.f)*2.f;
63   e->band[3].end=rint(20000.f/22050.f*n/4.f)*2.f-e->band[3].begin;
64
65   e->band[0].window=_ogg_malloc((e->band[0].end)/2*sizeof(*e->band[0].window));
66   e->band[1].window=_ogg_malloc((e->band[1].end)/2*sizeof(*e->band[1].window));
67   e->band[2].window=_ogg_malloc((e->band[2].end)/2*sizeof(*e->band[2].window));
68   e->band[3].window=_ogg_malloc((e->band[3].end)/2*sizeof(*e->band[3].window));
69   
70   n=e->band[0].end/2;
71   for(i=0;i<n;i++){
72     e->band[0].window[i]=sin((i+.5)/n*M_PI);
73     e->band[0].total+=e->band[0].window[i];
74   }
75   n=e->band[1].end/2;
76   for(i=0;i<n;i++){
77     e->band[1].window[i]=sin((i+.5)/n*M_PI);
78     e->band[1].total+=e->band[1].window[i];
79   }
80   n=e->band[2].end/2;
81   for(i=0;i<n;i++){
82     e->band[2].window[i]=sin((i+.5)/n*M_PI);
83     e->band[2].total+=e->band[2].window[i];
84   }
85   n=e->band[3].end/2;
86   for(i=0;i<n;i++){
87     e->band[3].window[i]=sin((i+.5)/n*M_PI);
88     e->band[3].total+=e->band[3].window[i];
89   }
90
91   
92   e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
93   e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));
94
95
96 }
97
98 void _ve_envelope_clear(envelope_lookup *e){
99   int i;
100   mdct_clear(&e->mdct);
101   for(i=0;i<VE_BANDS;i++)
102     _ogg_free(e->band[i].window);
103   _ogg_free(e->mdct_win);
104   _ogg_free(e->filter);
105   _ogg_free(e->mark);
106   memset(e,0,sizeof(*e));
107 }
108
109 /* fairly straight threshhold-by-band based until we find something
110    that works better and isn't patented. */
111 static int _ve_amp(envelope_lookup *ve,
112                    vorbis_info_psy_global *gi,
113                    float *data,
114                    envelope_band *bands,
115                    envelope_filter_state *filters,
116                    long pos){
117   long n=ve->winlength;
118   int ret=0;
119   long i,j;
120
121   /* we want to have a 'minimum bar' for energy, else we're just
122      basing blocks on quantization noise that outweighs the signal
123      itself (for low power signals) */
124
125   float minV=ve->minenergy,acc[VE_BANDS];
126   float *vec=alloca(n*sizeof(*vec));
127   memset(acc,0,sizeof(acc));
128  
129  /* window and transform */
130   for(i=0;i<n;i++)
131     vec[i]=data[i]*ve->mdct_win[i];
132   mdct_forward(&ve->mdct,vec,vec);
133
134   /* accumulate amplitude by band */
135   for(j=0;j<VE_BANDS;j++){
136     for(i=0;i<bands[j].end;i+=2){
137       float val=FABS(vec+i+bands[j].begin)+FABS(vec+i+bands[j].begin+1);
138       acc[j]+=todB(&val)*bands[j].window[i>>1];
139     }
140     acc[j]/=bands[j].total;
141     if(acc[j]<minV)acc[j]=minV;
142     //fprintf(stderr,"%d %gdB :: ",j,acc[j]);
143   }
144   //fprintf(stderr,"\n");
145
146   /* convert amplitude to delta */
147   for(j=0;j<VE_BANDS;j++){
148     float val=acc[j]-filters[j].ampbuf[filters[j].ampptr];
149     filters[j].ampbuf[filters[j].ampptr]=acc[j];
150     acc[j]=val;
151     filters[j].ampptr++;
152     if(filters[j].ampptr>=VE_DIV)filters[j].ampptr=0;
153   }
154
155   /* convolve deltas to threshhold values */
156   for(j=0;j<VE_BANDS;j++){
157     float *buf=filters[j].delbuf;
158     float val=.14*buf[0]+.14*buf[1]+.72*acc[j];
159     buf[0]=buf[1];buf[1]=acc[j];
160     acc[j]=val;
161     /*filters[j].markers[pos+1]=val;*/
162   }
163
164   /* look at local min/max */
165   for(j=0;j<VE_BANDS;j++){
166     float *buf=filters[j].convbuf;
167     if(buf[1]>gi->preecho_thresh[j] && buf[0]<buf[1] && acc[j]<buf[1])ret|=1;
168     if(buf[1]<gi->postecho_thresh[j] && buf[0]>buf[1] && acc[j]>buf[1])ret|=2;
169     buf[0]=buf[1];buf[1]=acc[j];
170   }
171   return(ret);
172 }
173
174 long _ve_envelope_search(vorbis_dsp_state *v){
175   vorbis_info *vi=v->vi;
176   codec_setup_info *ci=vi->codec_setup;
177   vorbis_info_psy_global *gi=&ci->psy_g_param;
178   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
179   long i,j;
180
181   int first=ve->current/ve->searchstep;
182   int last=v->pcm_current/ve->searchstep-VE_DIV;
183   if(first<0)first=0;
184
185   /* make sure we have enough storage to match the PCM */
186   if(last>ve->storage){
187     ve->storage=last+VE_DIV;
188     ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
189   }
190
191   for(j=first;j<last;j++){
192     int ret=0;
193     for(i=0;i<ve->ch;i++){
194       /* the mark delay is one searchstep because of min/max finder */
195       float *pcm=v->pcm[i]+ve->searchstep*(j+1);
196       ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
197     }
198
199     /* we assume a 'transient' occupies half a short block; this way,
200        it's contained in two short blocks, else the first block is
201        short and the second long, causing smearing.
202
203       preecho triggers follow the impulse marker; postecho triger preceed it */
204
205     ve->mark[j+VE_DIV/2]=0;
206     if(ret&1)
207       for(i=0;i<=VE_DIV/2;i++)
208         ve->mark[j+i]=1;
209     if(ret&2)
210       for(i=-1;i>=-VE_DIV/2 && j+i>=0;i--)
211         ve->mark[j+i]=1;
212   }
213
214   ve->current=last*ve->searchstep;
215
216   {
217     long centerW=v->centerW;
218     long testW=
219       centerW+
220       ci->blocksizes[v->W]/4+
221       ci->blocksizes[1]/2+
222       ci->blocksizes[0]/4;
223     
224     j=ve->cursor;
225     
226     while(j<ve->current-(VE_DIV/2*ve->searchstep)){ /* modified to
227                                                        stay clear of
228                                                        possibly
229                                                        unfinished
230                                                        postecho
231                                                        detection */
232       if(j>=testW)return(1);
233       if(ve->mark[j/ve->searchstep]){
234         if(j>centerW){
235
236 #if 0
237           if(j>ve->curmark){
238             float *marker=alloca(v->pcm_current*sizeof(*marker));
239             int l;
240             memset(marker,0,sizeof(*marker)*v->pcm_current);
241             fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
242                     seq,
243                     (totalshift+ve->cursor)/44100.,
244                     (totalshift+j)/44100.);
245             _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
246             _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
247
248             _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
249             _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
250             
251
252             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[0].markers[l]*.01;
253             _analysis_output_always("delL0",seq,marker,v->pcm_current,0,0,totalshift);
254             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[1].markers[l]*.01;
255             _analysis_output_always("delL1",seq,marker,v->pcm_current,0,0,totalshift);
256             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[2].markers[l]*.01;
257             _analysis_output_always("delL2",seq,marker,v->pcm_current,0,0,totalshift);
258             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[3].markers[l]*.01;
259             _analysis_output_always("delL3",seq,marker,v->pcm_current,0,0,totalshift);
260             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[4].markers[l]*.01;
261             _analysis_output_always("delR0",seq,marker,v->pcm_current,0,0,totalshift);
262             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[5].markers[l]*.01;
263             _analysis_output_always("delR1",seq,marker,v->pcm_current,0,0,totalshift);
264             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[6].markers[l]*.01;
265             _analysis_output_always("delR2",seq,marker,v->pcm_current,0,0,totalshift);
266             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[7].markers[l]*.01;
267             _analysis_output_always("delR3",seq,marker,v->pcm_current,0,0,totalshift);
268             seq++;
269
270           }
271 #endif      
272
273           ve->curmark=j;
274           ve->cursor=j;
275           if(j>=testW)return(1);
276           return(0);
277         }
278       }
279       j+=ve->searchstep;
280     }
281   }
282   ve->cursor=j;
283  
284   return(-1);
285 }
286
287 int _ve_envelope_mark(vorbis_dsp_state *v){
288   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
289   vorbis_info *vi=v->vi;
290   codec_setup_info *ci=vi->codec_setup;
291   long centerW=v->centerW;
292   long beginW=centerW-ci->blocksizes[v->W]/4;
293   long endW=centerW+ci->blocksizes[v->W]/4;
294   if(v->W){
295     beginW-=ci->blocksizes[v->lW]/4;
296     endW+=ci->blocksizes[v->nW]/4;
297   }else{
298     beginW-=ci->blocksizes[0]/4;
299     endW+=ci->blocksizes[0]/4;
300   }
301
302   if(ve->curmark>=beginW && ve->curmark<endW)return(1);
303   {
304     long first=beginW/ve->searchstep;
305     long last=endW/ve->searchstep;
306     long i;
307     for(i=first;i<last;i++)
308       if(ve->mark[i])return(1);
309   }
310   return(0);
311 }
312
313 void _ve_envelope_shift(envelope_lookup *e,long shift){
314   int smallsize=e->current/e->searchstep+VE_DIV/2; /* VE_DIV/2 is to
315                                                       match setting a
316                                                       mark on a region
317                                                       in
318                                                       envelope_search */
319   int smallshift=shift/e->searchstep;
320   int i;
321
322   memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
323   
324 #if 0
325   for(i=0;i<VE_BANDS*e->ch;i++)
326     memmove(e->filter[i].markers,
327             e->filter[i].markers+smallshift,
328             (1024-smallshift)*sizeof(*(*e->filter).markers));
329   totalshift+=shift;
330 #endif
331
332   e->current-=shift;
333   if(e->curmark>=0)
334     e->curmark-=shift;
335   e->cursor-=shift;
336 }
337
338