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