1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: PCM data envelope analysis
14 last mod: $Id: envelope.c,v 1.46 2002/03/29 08:18:23 xiphmont Exp $
16 ********************************************************************/
23 #include "vorbis/codec.h"
24 #include "codec_internal.h"
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;
37 int n=e->winlength=128;
38 e->searchstep=64; /* not random */
40 e->minenergy=gi->preecho_minenergy;
43 e->cursor=ci->blocksizes[1]/2;
44 e->mdct_win=_ogg_calloc(n,sizeof(*e->mdct_win));
45 mdct_init(&e->mdct,n);
48 e->mdct_win[i]=sin(i/(n-1.)*M_PI);
49 e->mdct_win[i]*=e->mdct_win[i];
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;
60 for(j=0;j<VE_BANDS;j++){
62 e->band[j].window=_ogg_malloc(n*sizeof(*e->band[0].window));
64 e->band[j].window[i]=sin((i+.5)/n*M_PI);
65 e->band[j].total+=e->band[j].window[i];
67 e->band[j].total=1./e->band[j].total;
70 e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
71 e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));
75 void _ve_envelope_clear(envelope_lookup *e){
78 for(i=0;i<VE_BANDS;i++)
79 _ogg_free(e->band[i].window);
80 _ogg_free(e->mdct_win);
83 memset(e,0,sizeof(*e));
86 extern void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB,ogg_int64_t off);
88 /* fairly straight threshhold-by-band based until we find something
89 that works better and isn't patented. */
91 static int _ve_amp(envelope_lookup *ve,
92 vorbis_info_psy_global *gi,
95 envelope_filter_state *filters,
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) */
106 float minV=ve->minenergy;
107 float *vec=alloca(n*sizeof(*vec));
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;
116 /*_analysis_output_always("lpcm",seq2,data,n,0,0,
117 totalshift+pos*ve->searchstep);*/
119 /* window and transform */
121 vec[i]=data[i]*ve->mdct_win[i];
122 mdct_forward(&ve->mdct,vec,vec);
124 /* _analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */
126 /* near-DC spreading function; this has nothing to do with
127 psychoacoustics, just sidelobe leakage and window size */
129 float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
130 int ptr=filters->nearptr;
132 /* the accumulation is regularly refreshed from scratch to avoid
133 floating point creep */
135 decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
136 filters->nearDC_partialacc=temp;
138 decay=filters->nearDC_acc+=temp;
139 filters->nearDC_partialacc+=temp;
141 filters->nearDC_acc-=filters->nearDC[ptr];
142 filters->nearDC[ptr]=temp;
144 decay*=(1./(VE_NEARDC+1));
146 if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
147 decay=todB(&decay)*.5-15.f;
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 */
154 float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
156 if(val<decay)val=decay;
157 if(val<minV)val=minV;
162 /* _analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/
164 /* perform preecho/postecho triggering by band */
165 for(j=0;j<VE_BANDS;j++){
169 /* accumulate amplitude */
170 for(i=0;i<bands[j].end;i++)
171 acc+=vec[i+bands[j].begin]*bands[j].window[i];
175 /* convert amplitude to delta */
177 int p,this=filters[j].ampptr;
178 float postmax,postmin,premax=-99999.f,premin=99999.f;
183 postmax=max(acc,filters[j].ampbuf[p]);
184 postmin=min(acc,filters[j].ampbuf[p]);
186 for(i=0;i<stretch;i++){
189 premax=max(premax,filters[j].ampbuf[p]);
190 premin=min(premin,filters[j].ampbuf[p]);
193 valmin=postmin-premin;
194 valmax=postmax-premax;
196 filters[j].ampbuf[this]=acc;
198 if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
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;
206 if(ret&1)ve->stretch=-1;
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;
218 int first=ve->current/ve->searchstep;
219 int last=v->pcm_current/ve->searchstep-VE_WIN;
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));
228 for(j=first;j<last;j++){
232 if(ve->stretch>VE_MAXSTRETCH*2)
233 ve->stretch=VE_MAXSTRETCH*2;
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);
240 ve->mark[j+VE_POST]=0;
248 if(j>0)ve->mark[j-1]=1;
252 ve->current=last*ve->searchstep;
255 long centerW=v->centerW;
258 ci->blocksizes[v->W]/4+
264 while(j<ve->current-(ve->searchstep)){/* account for postecho
265 working back one window */
266 if(j>=testW)return(1);
270 if(ve->mark[j/ve->searchstep]){
275 float *marker=alloca(v->pcm_current*sizeof(*marker));
277 memset(marker,0,sizeof(*marker)*v->pcm_current);
278 fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
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);
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);
288 for(m=0;m<VE_BANDS;m++){
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);
295 for(m=0;m<VE_BANDS;m++){
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);
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);
311 if(j>=testW)return(1);
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;
330 beginW-=ci->blocksizes[v->lW]/4;
331 endW+=ci->blocksizes[v->nW]/4;
333 beginW-=ci->blocksizes[0]/4;
334 endW+=ci->blocksizes[0]/4;
337 if(ve->curmark>=beginW && ve->curmark<endW)return(1);
339 long first=beginW/ve->searchstep;
340 long last=endW/ve->searchstep;
342 for(i=first;i<last;i++)
343 if(ve->mark[i])return(1);
348 void _ve_envelope_shift(envelope_lookup *e,long shift){
349 int smallsize=e->current/e->searchstep;
350 int smallshift=shift/e->searchstep;
353 memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
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));
361 e->stretchm+smallshift,
362 (1024-smallshift)*sizeof(*e->stretchm));