minor preecho tweak
[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.48 2002/04/01 00:49:41 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,j;
37   int n=e->winlength=128;
38   e->searchstep=64; /* 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/(n-1.)*M_PI);
49     e->mdct_win[i]*=e->mdct_win[i];
50   }
51
52   /* magic follows */
53   e->band[0].begin=2;  e->band[0].end=4;
54   e->band[1].begin=4;  e->band[1].end=8;
55   e->band[2].begin=6;  e->band[2].end=10;
56   e->band[3].begin=12; e->band[3].end=12;
57   e->band[4].begin=18; e->band[4].end=16;
58   e->band[5].begin=26; e->band[5].end=20;
59   e->band[6].begin=36; e->band[6].end=24;
60
61   for(j=0;j<VE_BANDS;j++){
62     n=e->band[j].end;
63     e->band[j].window=_ogg_malloc(n*sizeof(*e->band[0].window));
64     for(i=0;i<n;i++){
65       e->band[j].window[i]=sin((i+.5)/n*M_PI);
66       e->band[j].total+=e->band[j].window[i];
67     }
68     e->band[j].total=1./e->band[j].total;
69   }
70   
71   e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
72   e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));
73
74 }
75
76 void _ve_envelope_clear(envelope_lookup *e){
77   int i;
78   mdct_clear(&e->mdct);
79   for(i=0;i<VE_BANDS;i++)
80     _ogg_free(e->band[i].window);
81   _ogg_free(e->mdct_win);
82   _ogg_free(e->filter);
83   _ogg_free(e->mark);
84   memset(e,0,sizeof(*e));
85 }
86
87 /* fairly straight threshhold-by-band based until we find something
88    that works better and isn't patented. */
89
90 static int _ve_amp(envelope_lookup *ve,
91                    vorbis_info_psy_global *gi,
92                    float *data,
93                    envelope_band *bands,
94                    envelope_filter_state *filters,
95                    long pos){
96   long n=ve->winlength;
97   int ret=0;
98   long i,j;
99   float decay;
100
101   /* we want to have a 'minimum bar' for energy, else we're just
102      basing blocks on quantization noise that outweighs the signal
103      itself (for low power signals) */
104
105   float minV=ve->minenergy;
106   float *vec=alloca(n*sizeof(*vec));
107
108   /* stretch is used to gradually lengthen the number of windows
109      considered prevoius-to-potential-trigger */
110   int stretch=max(VE_MINSTRETCH,ve->stretch/2);
111   float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH);
112   if(penalty<0.f)penalty=0.f;
113   if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty;
114   
115   /*_analysis_output_always("lpcm",seq2,data,n,0,0,
116     totalshift+pos*ve->searchstep);*/
117   
118  /* window and transform */
119   for(i=0;i<n;i++)
120     vec[i]=data[i]*ve->mdct_win[i];
121   mdct_forward(&ve->mdct,vec,vec);
122   
123   /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */
124
125   /* near-DC spreading function; this has nothing to do with
126      psychoacoustics, just sidelobe leakage and window size */
127   {
128     float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
129     int ptr=filters->nearptr;
130
131     /* the accumulation is regularly refreshed from scratch to avoid
132        floating point creep */
133     if(ptr==0){
134       decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
135       filters->nearDC_partialacc=temp;
136     }else{
137       decay=filters->nearDC_acc+=temp;
138       filters->nearDC_partialacc+=temp;
139     }
140     filters->nearDC_acc-=filters->nearDC[ptr];
141     filters->nearDC[ptr]=temp;
142
143     decay*=(1./(VE_NEARDC+1));
144     filters->nearptr++;
145     if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
146     decay=todB(&decay)*.5-15.f;
147   }
148   
149   /* perform spreading and limiting, also smooth the spectrum.  yes,
150      the MDCT results in all real coefficients, but it still *behaves*
151      like real/imaginary pairs */
152   for(i=0;i<n/2;i+=2){
153     float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
154     val=todB(&val)*.5f;
155     if(val<decay)val=decay;
156     if(val<minV)val=minV;
157     vec[i>>1]=val;
158     decay-=8.;
159   }
160
161   /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/
162   
163   /* perform preecho/postecho triggering by band */
164   for(j=0;j<VE_BANDS;j++){
165     float acc=0.;
166     float valmax,valmin;
167
168     /* accumulate amplitude */
169     for(i=0;i<bands[j].end;i++)
170       acc+=vec[i+bands[j].begin]*bands[j].window[i];
171    
172     acc*=bands[j].total;
173
174     /* convert amplitude to delta */
175     {
176       int p,this=filters[j].ampptr;
177       float postmax,postmin,premax=-99999.f,premin=99999.f;
178       
179       p=this;
180       p--;
181       if(p<0)p+=VE_AMP;
182       postmax=max(acc,filters[j].ampbuf[p]);
183       postmin=min(acc,filters[j].ampbuf[p]);
184       
185       for(i=0;i<stretch;i++){
186         p--;
187         if(p<0)p+=VE_AMP;
188         premax=max(premax,filters[j].ampbuf[p]);
189         premin=min(premin,filters[j].ampbuf[p]);
190       }
191       
192       valmin=postmin-premin;
193       valmax=postmax-premax;
194
195       filters[j].ampbuf[this]=acc;
196       filters[j].ampptr++;
197       if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
198     }
199
200     /* look at min/max, decide trigger */
201     if(valmax>gi->preecho_thresh[j]+penalty){
202       ret|=1;
203       ret|=4;
204     }
205     if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
206   }
207  
208   return(ret);
209 }
210
211 long _ve_envelope_search(vorbis_dsp_state *v){
212   vorbis_info *vi=v->vi;
213   codec_setup_info *ci=vi->codec_setup;
214   vorbis_info_psy_global *gi=&ci->psy_g_param;
215   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
216   long i,j;
217
218   int first=ve->current/ve->searchstep;
219   int last=v->pcm_current/ve->searchstep-VE_WIN;
220   if(first<0)first=0;
221
222   /* make sure we have enough storage to match the PCM */
223   if(last>ve->storage){
224     ve->storage=last+VE_WIN;
225     ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
226   }
227
228   for(j=first;j<last;j++){
229     int ret=0;
230
231     ve->stretch++;
232     if(ve->stretch>VE_MAXSTRETCH*2)
233       ve->stretch=VE_MAXSTRETCH*2;
234     
235     for(i=0;i<ve->ch;i++){
236       float *pcm=v->pcm[i]+ve->searchstep*(j);
237       ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
238     }
239
240     ve->mark[j+VE_POST]=0;
241     if(ret&1){
242       ve->mark[j]=1;
243       ve->mark[j+1]=1;
244     }
245
246     if(ret&2){
247       ve->mark[j]=1;
248       if(j>0)ve->mark[j-1]=1;
249     }
250
251     if(ret&4)ve->stretch=-1;
252   }
253
254   ve->current=last*ve->searchstep;
255
256   {
257     long centerW=v->centerW;
258     long testW=
259       centerW+
260       ci->blocksizes[v->W]/4+
261       ci->blocksizes[1]/2+
262       ci->blocksizes[0]/4;
263     
264     j=ve->cursor;
265     
266     while(j<ve->current-(ve->searchstep)){/* account for postecho
267                                              working back one window */
268       if(j>=testW)return(1);
269  
270       ve->cursor=j;
271
272       if(ve->mark[j/ve->searchstep]){
273         if(j>centerW){
274
275           #if 0
276           if(j>ve->curmark){
277             float *marker=alloca(v->pcm_current*sizeof(*marker));
278             int l,m;
279             memset(marker,0,sizeof(*marker)*v->pcm_current);
280             fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
281                     seq,
282                     (totalshift+ve->cursor)/44100.,
283                     (totalshift+j)/44100.);
284             _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
285             _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
286
287             _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
288             _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
289             
290             for(m=0;m<VE_BANDS;m++){
291               char buf[80];
292               sprintf(buf,"delL%d",m);
293               for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
294               _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
295             }
296
297             for(m=0;m<VE_BANDS;m++){
298               char buf[80];
299               sprintf(buf,"delR%d",m);
300               for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
301               _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
302             }
303
304
305             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->stretchm[l]*.1;
306             _analysis_output_always("stretch",seq,marker,v->pcm_current,0,0,totalshift);
307             
308             seq++;
309             
310           }
311           #endif
312
313           ve->curmark=j;
314           if(j>=testW)return(1);
315           return(0);
316         }
317       }
318       j+=ve->searchstep;
319     }
320   }
321   
322   return(-1);
323 }
324
325 int _ve_envelope_mark(vorbis_dsp_state *v){
326   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
327   vorbis_info *vi=v->vi;
328   codec_setup_info *ci=vi->codec_setup;
329   long centerW=v->centerW;
330   long beginW=centerW-ci->blocksizes[v->W]/4;
331   long endW=centerW+ci->blocksizes[v->W]/4;
332   if(v->W){
333     beginW-=ci->blocksizes[v->lW]/4;
334     endW+=ci->blocksizes[v->nW]/4;
335   }else{
336     beginW-=ci->blocksizes[0]/4;
337     endW+=ci->blocksizes[0]/4;
338   }
339
340   if(ve->curmark>=beginW && ve->curmark<endW)return(1);
341   {
342     long first=beginW/ve->searchstep;
343     long last=endW/ve->searchstep;
344     long i;
345     for(i=first;i<last;i++)
346       if(ve->mark[i])return(1);
347   }
348   return(0);
349 }
350
351 void _ve_envelope_shift(envelope_lookup *e,long shift){
352   int smallsize=e->current/e->searchstep; 
353   int smallshift=shift/e->searchstep;
354   int i;
355
356   memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
357   
358 #if 0
359   for(i=0;i<VE_BANDS*e->ch;i++)
360     memmove(e->filter[i].markers,
361             e->filter[i].markers+smallshift,
362             (1024-smallshift)*sizeof(*(*e->filter).markers));
363   memmove(e->stretchm,e->stretchm+smallshift,(smallsize-smallshift)*sizeof(*e->stretchm));
364   totalshift+=shift;
365 #endif 
366
367   e->current-=shift;
368   if(e->curmark>=0)
369     e->curmark-=shift;
370   e->cursor-=shift;
371 }
372
373
374
375
376
377