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-2002 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: PCM data envelope analysis
14 last mod: $Id: envelope.c,v 1.54 2003/09/05 23:17:49 giles 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=2; e->band[0].end=4;
54 e->band[1].begin=4; e->band[1].end=5;
55 e->band[2].begin=6; e->band[2].end=6;
56 e->band[3].begin=9; e->band[3].end=8;
57 e->band[4].begin=13; e->band[4].end=8;
58 e->band[5].begin=17; e->band[5].end=8;
59 e->band[6].begin=22; e->band[6].end=8;
61 for(j=0;j<VE_BANDS;j++){
63 e->band[j].window=_ogg_malloc(n*sizeof(*e->band[0].window));
65 e->band[j].window[i]=sin((i+.5)/n*M_PI);
66 e->band[j].total+=e->band[j].window[i];
68 e->band[j].total=1./e->band[j].total;
71 e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
72 e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));
76 void _ve_envelope_clear(envelope_lookup *e){
79 for(i=0;i<VE_BANDS;i++)
80 _ogg_free(e->band[i].window);
81 _ogg_free(e->mdct_win);
84 memset(e,0,sizeof(*e));
87 /* fairly straight threshhold-by-band based until we find something
88 that works better and isn't patented. */
90 static int _ve_amp(envelope_lookup *ve,
91 vorbis_info_psy_global *gi,
94 envelope_filter_state *filters,
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) */
105 float minV=ve->minenergy;
106 float *vec=alloca(n*sizeof(*vec));
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;
115 /*_analysis_output_always("lpcm",seq2,data,n,0,0,
116 totalshift+pos*ve->searchstep);*/
118 /* window and transform */
120 vec[i]=data[i]*ve->mdct_win[i];
121 mdct_forward(&ve->mdct,vec,vec);
123 /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */
125 /* near-DC spreading function; this has nothing to do with
126 psychoacoustics, just sidelobe leakage and window size */
128 float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
129 int ptr=filters->nearptr;
131 /* the accumulation is regularly refreshed from scratch to avoid
132 floating point creep */
134 decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
135 filters->nearDC_partialacc=temp;
137 decay=filters->nearDC_acc+=temp;
138 filters->nearDC_partialacc+=temp;
140 filters->nearDC_acc-=filters->nearDC[ptr];
141 filters->nearDC[ptr]=temp;
143 decay*=(1./(VE_NEARDC+1));
145 if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
146 decay=todB(&decay)*.5-15.f;
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 */
153 float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
155 if(val<decay)val=decay;
156 if(val<minV)val=minV;
161 /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/
163 /* perform preecho/postecho triggering by band */
164 for(j=0;j<VE_BANDS;j++){
168 /* accumulate amplitude */
169 for(i=0;i<bands[j].end;i++)
170 acc+=vec[i+bands[j].begin]*bands[j].window[i];
174 /* convert amplitude to delta */
176 int p,this=filters[j].ampptr;
177 float postmax,postmin,premax=-99999.f,premin=99999.f;
182 postmax=max(acc,filters[j].ampbuf[p]);
183 postmin=min(acc,filters[j].ampbuf[p]);
185 for(i=0;i<stretch;i++){
188 premax=max(premax,filters[j].ampbuf[p]);
189 premin=min(premin,filters[j].ampbuf[p]);
192 valmin=postmin-premin;
193 valmax=postmax-premax;
195 /*filters[j].markers[pos]=valmax;*/
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){
206 if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
214 static ogg_int64_t totalshift=-1024;
217 long _ve_envelope_search(vorbis_dsp_state *v){
218 vorbis_info *vi=v->vi;
219 codec_setup_info *ci=vi->codec_setup;
220 vorbis_info_psy_global *gi=&ci->psy_g_param;
221 envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
224 int first=ve->current/ve->searchstep;
225 int last=v->pcm_current/ve->searchstep-VE_WIN;
228 /* make sure we have enough storage to match the PCM */
229 if(last+VE_WIN+VE_POST>ve->storage){
230 ve->storage=last+VE_WIN+VE_POST; /* be sure */
231 ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
234 for(j=first;j<last;j++){
238 if(ve->stretch>VE_MAXSTRETCH*2)
239 ve->stretch=VE_MAXSTRETCH*2;
241 for(i=0;i<ve->ch;i++){
242 float *pcm=v->pcm[i]+ve->searchstep*(j);
243 ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
246 ve->mark[j+VE_POST]=0;
254 if(j>0)ve->mark[j-1]=1;
257 if(ret&4)ve->stretch=-1;
260 ve->current=last*ve->searchstep;
263 long centerW=v->centerW;
266 ci->blocksizes[v->W]/4+
272 while(j<ve->current-(ve->searchstep)){/* account for postecho
273 working back one window */
274 if(j>=testW)return(1);
278 if(ve->mark[j/ve->searchstep]){
283 float *marker=alloca(v->pcm_current*sizeof(*marker));
285 memset(marker,0,sizeof(*marker)*v->pcm_current);
286 fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
288 (totalshift+ve->cursor)/44100.,
289 (totalshift+j)/44100.);
290 _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
291 _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
293 _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
294 _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
296 for(m=0;m<VE_BANDS;m++){
298 sprintf(buf,"delL%d",m);
299 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
300 _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
303 for(m=0;m<VE_BANDS;m++){
305 sprintf(buf,"delR%d",m);
306 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
307 _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
310 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->mark[l]*.4;
311 _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift);
320 if(j>=testW)return(1);
331 int _ve_envelope_mark(vorbis_dsp_state *v){
332 envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
333 vorbis_info *vi=v->vi;
334 codec_setup_info *ci=vi->codec_setup;
335 long centerW=v->centerW;
336 long beginW=centerW-ci->blocksizes[v->W]/4;
337 long endW=centerW+ci->blocksizes[v->W]/4;
339 beginW-=ci->blocksizes[v->lW]/4;
340 endW+=ci->blocksizes[v->nW]/4;
342 beginW-=ci->blocksizes[0]/4;
343 endW+=ci->blocksizes[0]/4;
346 if(ve->curmark>=beginW && ve->curmark<endW)return(1);
348 long first=beginW/ve->searchstep;
349 long last=endW/ve->searchstep;
351 for(i=first;i<last;i++)
352 if(ve->mark[i])return(1);
357 void _ve_envelope_shift(envelope_lookup *e,long shift){
358 int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
359 ahead of ve->current */
360 int smallshift=shift/e->searchstep;
362 memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
365 for(i=0;i<VE_BANDS*e->ch;i++)
366 memmove(e->filter[i].markers,
367 e->filter[i].markers+smallshift,
368 (1024-smallshift)*sizeof(*(*e->filter).markers));