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-2009 *
9 * by the Xiph.Org Foundation https://xiph.org/ *
11 ********************************************************************
13 function: PCM data envelope analysis
15 ********************************************************************/
22 #include "vorbis/codec.h"
23 #include "codec_internal.h"
31 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
32 codec_setup_info *ci=vi->codec_setup;
33 vorbis_info_psy_global *gi=&ci->psy_g_param;
36 int n=e->winlength=128;
37 e->searchstep=64; /* not random */
39 e->minenergy=gi->preecho_minenergy;
42 e->cursor=ci->blocksizes[1]/2;
43 e->mdct_win=_ogg_calloc(n,sizeof(*e->mdct_win));
44 mdct_init(&e->mdct,n);
47 e->mdct_win[i]=sin(i/(n-1.)*M_PI);
48 e->mdct_win[i]*=e->mdct_win[i];
52 e->band[0].begin=2; e->band[0].end=4;
53 e->band[1].begin=4; e->band[1].end=5;
54 e->band[2].begin=6; e->band[2].end=6;
55 e->band[3].begin=9; e->band[3].end=8;
56 e->band[4].begin=13; e->band[4].end=8;
57 e->band[5].begin=17; e->band[5].end=8;
58 e->band[6].begin=22; e->band[6].end=8;
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 /* fairly straight threshhold-by-band based until we find something
87 that works better and isn't patented. */
89 static int _ve_amp(envelope_lookup *ve,
90 vorbis_info_psy_global *gi,
93 envelope_filter_state *filters){
99 /* we want to have a 'minimum bar' for energy, else we're just
100 basing blocks on quantization noise that outweighs the signal
101 itself (for low power signals) */
103 float minV=ve->minenergy;
104 float *vec=alloca(n*sizeof(*vec));
106 /* stretch is used to gradually lengthen the number of windows
107 considered prevoius-to-potential-trigger */
108 int stretch=max(VE_MINSTRETCH,ve->stretch/2);
109 float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH);
110 if(penalty<0.f)penalty=0.f;
111 if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty;
113 /*_analysis_output_always("lpcm",seq2,data,n,0,0,
114 totalshift+pos*ve->searchstep);*/
116 /* window and transform */
118 vec[i]=data[i]*ve->mdct_win[i];
119 mdct_forward(&ve->mdct,vec,vec);
121 /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */
123 /* near-DC spreading function; this has nothing to do with
124 psychoacoustics, just sidelobe leakage and window size */
126 float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
127 int ptr=filters->nearptr;
129 /* the accumulation is regularly refreshed from scratch to avoid
130 floating point creep */
132 decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
133 filters->nearDC_partialacc=temp;
135 decay=filters->nearDC_acc+=temp;
136 filters->nearDC_partialacc+=temp;
138 filters->nearDC_acc-=filters->nearDC[ptr];
139 filters->nearDC[ptr]=temp;
141 decay*=(1./(VE_NEARDC+1));
143 if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
144 decay=todB(&decay)*.5-15.f;
147 /* perform spreading and limiting, also smooth the spectrum. yes,
148 the MDCT results in all real coefficients, but it still *behaves*
149 like real/imaginary pairs */
151 float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
153 if(val<decay)val=decay;
154 if(val<minV)val=minV;
159 /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/
161 /* perform preecho/postecho triggering by band */
162 for(j=0;j<VE_BANDS;j++){
166 /* accumulate amplitude */
167 for(i=0;i<bands[j].end;i++)
168 acc+=vec[i+bands[j].begin]*bands[j].window[i];
172 /* convert amplitude to delta */
174 int p,this=filters[j].ampptr;
175 float postmax,postmin,premax=-99999.f,premin=99999.f;
180 postmax=max(acc,filters[j].ampbuf[p]);
181 postmin=min(acc,filters[j].ampbuf[p]);
183 for(i=0;i<stretch;i++){
186 premax=max(premax,filters[j].ampbuf[p]);
187 premin=min(premin,filters[j].ampbuf[p]);
190 valmin=postmin-premin;
191 valmax=postmax-premax;
193 /*filters[j].markers[pos]=valmax;*/
194 filters[j].ampbuf[this]=acc;
196 if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
199 /* look at min/max, decide trigger */
200 if(valmax>gi->preecho_thresh[j]+penalty){
204 if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
212 static ogg_int64_t totalshift=-1024;
215 long _ve_envelope_search(vorbis_dsp_state *v){
216 vorbis_info *vi=v->vi;
217 codec_setup_info *ci=vi->codec_setup;
218 vorbis_info_psy_global *gi=&ci->psy_g_param;
219 envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
222 int first=ve->current/ve->searchstep;
223 int last=v->pcm_current/ve->searchstep-VE_WIN;
226 /* make sure we have enough storage to match the PCM */
227 if(last+VE_WIN+VE_POST>ve->storage){
228 ve->storage=last+VE_WIN+VE_POST; /* be sure */
229 ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
232 for(j=first;j<last;j++){
236 if(ve->stretch>VE_MAXSTRETCH*2)
237 ve->stretch=VE_MAXSTRETCH*2;
239 for(i=0;i<ve->ch;i++){
240 float *pcm=v->pcm[i]+ve->searchstep*(j);
241 ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS);
244 ve->mark[j+VE_POST]=0;
252 if(j>0)ve->mark[j-1]=1;
255 if(ret&4)ve->stretch=-1;
258 ve->current=last*ve->searchstep;
261 long centerW=v->centerW;
264 ci->blocksizes[v->W]/4+
270 while(j<ve->current-(ve->searchstep)){/* account for postecho
271 working back one window */
272 if(j>=testW)return(1);
276 if(ve->mark[j/ve->searchstep]){
281 float *marker=alloca(v->pcm_current*sizeof(*marker));
283 memset(marker,0,sizeof(*marker)*v->pcm_current);
284 fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
286 (totalshift+ve->cursor)/44100.,
287 (totalshift+j)/44100.);
288 _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
289 _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
291 _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
292 _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
294 for(m=0;m<VE_BANDS;m++){
296 sprintf(buf,"delL%d",m);
297 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
298 _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
301 for(m=0;m<VE_BANDS;m++){
303 sprintf(buf,"delR%d",m);
304 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
305 _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
308 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->mark[l]*.4;
309 _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift);
318 if(j>=testW)return(1);
329 int _ve_envelope_mark(vorbis_dsp_state *v){
330 envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
331 vorbis_info *vi=v->vi;
332 codec_setup_info *ci=vi->codec_setup;
333 long centerW=v->centerW;
334 long beginW=centerW-ci->blocksizes[v->W]/4;
335 long endW=centerW+ci->blocksizes[v->W]/4;
337 beginW-=ci->blocksizes[v->lW]/4;
338 endW+=ci->blocksizes[v->nW]/4;
340 beginW-=ci->blocksizes[0]/4;
341 endW+=ci->blocksizes[0]/4;
344 if(ve->curmark>=beginW && ve->curmark<endW)return(1);
346 long first=beginW/ve->searchstep;
347 long last=endW/ve->searchstep;
349 for(i=first;i<last;i++)
350 if(ve->mark[i])return(1);
355 void _ve_envelope_shift(envelope_lookup *e,long shift){
356 int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
357 ahead of ve->current */
358 int smallshift=shift/e->searchstep;
360 memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
363 for(i=0;i<VE_BANDS*e->ch;i++)
364 memmove(e->filter[i].markers,
365 e->filter[i].markers+smallshift,
366 (1024-smallshift)*sizeof(*(*e->filter).markers));