new preecho was switching back to long blocks too soon
[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.43 2002/03/23 03:17:33 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     filters[j].markers[pos+1]=val;
153   }
154
155   /* look at local min/max */
156   for(j=0;j<VE_BANDS;j++){
157     float *buf=filters[j].convbuf;
158     if(buf[1]>gi->preecho_thresh[j] && buf[0]<buf[1] && acc[j]<buf[1])ret=1;
159     if(buf[1]<gi->postecho_thresh[j] && buf[0]>buf[1] && acc[j]>buf[1])ret=1;
160     buf[0]=buf[1];buf[1]=acc[j];
161   }
162   return(ret);
163 }
164
165 static int seq=0;
166 static ogg_int64_t totalshift=-1024;
167
168 long _ve_envelope_search(vorbis_dsp_state *v){
169   vorbis_info *vi=v->vi;
170   codec_setup_info *ci=vi->codec_setup;
171   vorbis_info_psy_global *gi=&ci->psy_g_param;
172   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
173   long i,j;
174
175   int first=ve->current/ve->searchstep;
176   int last=v->pcm_current/ve->searchstep-VE_DIV;
177   if(first<0)first=0;
178
179   /* make sure we have enough storage to match the PCM */
180   if(last>ve->storage){
181     ve->storage=last+VE_DIV;
182     ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
183   }
184
185   for(j=first;j<last;j++){
186     int ret=0;
187     for(i=0;i<ve->ch;i++){
188       /* the mark delay is one searchstep because of min/max finder */
189       float *pcm=v->pcm[i]+ve->searchstep*(j+1);
190       ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
191     }
192
193     /* we assume a 'transient' occupies half a short block; this way,
194        it's contained in two short blocks, else the first block is
195        short and the second long, causing smearing */
196     ve->mark[j+VE_DIV/2]=0;
197     if(ret)
198       for(i=0;i<=VE_DIV/2;i++)
199         ve->mark[j+i]=ret;
200   }
201
202   ve->current=last*ve->searchstep;
203
204   {
205     long centerW=v->centerW;
206     long testW=
207       centerW+
208       ci->blocksizes[v->W]/4+
209       ci->blocksizes[1]/2+
210       ci->blocksizes[0]/4;
211     
212     j=ve->cursor;
213     
214     while(j<ve->current){
215       if(j>=testW)return(1);
216       if(ve->mark[j/ve->searchstep]){
217         if(j>centerW){
218
219 #if 0
220           if(j>ve->curmark){
221             float *marker=alloca(v->pcm_current*sizeof(*marker));
222             int l;
223             memset(marker,0,sizeof(*marker)*v->pcm_current);
224
225             fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
226                     seq,
227                     (totalshift+ve->cursor)/44100.,
228                     (totalshift+j)/44100.);
229
230             _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
231             _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
232
233             _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
234             _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
235             
236
237             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[0].markers[l]*.01;
238             _analysis_output_always("delL0",seq,marker,v->pcm_current,0,0,totalshift);
239             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[1].markers[l]*.01;
240             _analysis_output_always("delL1",seq,marker,v->pcm_current,0,0,totalshift);
241             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[2].markers[l]*.01;
242             _analysis_output_always("delL2",seq,marker,v->pcm_current,0,0,totalshift);
243             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[3].markers[l]*.01;
244             _analysis_output_always("delL3",seq,marker,v->pcm_current,0,0,totalshift);
245             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[4].markers[l]*.01;
246             _analysis_output_always("delR0",seq,marker,v->pcm_current,0,0,totalshift);
247             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[5].markers[l]*.01;
248             _analysis_output_always("delR1",seq,marker,v->pcm_current,0,0,totalshift);
249             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[6].markers[l]*.01;
250             _analysis_output_always("delR2",seq,marker,v->pcm_current,0,0,totalshift);
251             for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[7].markers[l]*.01;
252             _analysis_output_always("delR3",seq,marker,v->pcm_current,0,0,totalshift);
253               
254             seq++;
255
256           }
257 #endif
258
259           ve->curmark=j;
260           ve->cursor=j;
261           if(j>=testW)return(1);
262           return(0);
263         }
264       }
265       j+=ve->searchstep;
266     }
267   }
268   ve->cursor=j;
269  
270   return(-1);
271 }
272
273 int _ve_envelope_mark(vorbis_dsp_state *v){
274   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
275   vorbis_info *vi=v->vi;
276   codec_setup_info *ci=vi->codec_setup;
277   long centerW=v->centerW;
278   long beginW=centerW-ci->blocksizes[v->W]/4;
279   long endW=centerW+ci->blocksizes[v->W]/4;
280   if(v->W){
281     beginW-=ci->blocksizes[v->lW]/4;
282     endW+=ci->blocksizes[v->nW]/4;
283   }else{
284     beginW-=ci->blocksizes[0]/4;
285     endW+=ci->blocksizes[0]/4;
286   }
287
288   if(ve->curmark>=beginW && ve->curmark<endW)return(1);
289   {
290     long first=beginW/ve->searchstep;
291     long last=endW/ve->searchstep;
292     long i;
293     for(i=first;i<last;i++)
294       if(ve->mark[i])return(1);
295   }
296   return(0);
297 }
298
299 void _ve_envelope_shift(envelope_lookup *e,long shift){
300   int smallsize=e->current/e->searchstep+VE_DIV/2; /* VE_DIV/2 is to
301                                                       match setting a
302                                                       mark on a region
303                                                       in
304                                                       envelope_search */
305   int smallshift=shift/e->searchstep;
306   int i;
307
308   memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
309   
310   for(i=0;i<VE_BANDS*e->ch;i++)
311     memmove(e->filter[i].markers,
312             e->filter[i].markers+smallshift,
313             (1024-smallshift)*sizeof(*(*e->filter).markers));
314
315   e->current-=shift;
316   if(e->curmark>=0)
317     e->curmark-=shift;
318   e->cursor-=shift;
319   totalshift+=shift;
320 }
321
322