nuclear@0: /******************************************************************** nuclear@0: * * nuclear@0: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@0: * * nuclear@0: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * nuclear@0: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@0: * * nuclear@0: ******************************************************************** nuclear@0: nuclear@0: function: PCM data envelope analysis nuclear@0: last mod: $Id: envelope.c 16227 2009-07-08 06:58:46Z xiphmont $ nuclear@0: nuclear@0: ********************************************************************/ nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "vorbis/codec.h" nuclear@0: #include "codec_internal.h" nuclear@0: nuclear@0: #include "os.h" nuclear@0: #include "scales.h" nuclear@0: #include "envelope.h" nuclear@0: #include "mdct.h" nuclear@0: #include "misc.h" nuclear@0: nuclear@0: void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){ nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: vorbis_info_psy_global *gi=&ci->psy_g_param; nuclear@0: int ch=vi->channels; nuclear@0: int i,j; nuclear@0: int n=e->winlength=128; nuclear@0: e->searchstep=64; /* not random */ nuclear@0: nuclear@0: e->minenergy=gi->preecho_minenergy; nuclear@0: e->ch=ch; nuclear@0: e->storage=128; nuclear@0: e->cursor=ci->blocksizes[1]/2; nuclear@0: e->mdct_win=_ogg_calloc(n,sizeof(*e->mdct_win)); nuclear@0: mdct_init(&e->mdct,n); nuclear@0: nuclear@0: for(i=0;imdct_win[i]=sin(i/(n-1.)*M_PI); nuclear@0: e->mdct_win[i]*=e->mdct_win[i]; nuclear@0: } nuclear@0: nuclear@0: /* magic follows */ nuclear@0: e->band[0].begin=2; e->band[0].end=4; nuclear@0: e->band[1].begin=4; e->band[1].end=5; nuclear@0: e->band[2].begin=6; e->band[2].end=6; nuclear@0: e->band[3].begin=9; e->band[3].end=8; nuclear@0: e->band[4].begin=13; e->band[4].end=8; nuclear@0: e->band[5].begin=17; e->band[5].end=8; nuclear@0: e->band[6].begin=22; e->band[6].end=8; nuclear@0: nuclear@0: for(j=0;jband[j].end; nuclear@0: e->band[j].window=_ogg_malloc(n*sizeof(*e->band[0].window)); nuclear@0: for(i=0;iband[j].window[i]=sin((i+.5)/n*M_PI); nuclear@0: e->band[j].total+=e->band[j].window[i]; nuclear@0: } nuclear@0: e->band[j].total=1./e->band[j].total; nuclear@0: } nuclear@0: nuclear@0: e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter)); nuclear@0: e->mark=_ogg_calloc(e->storage,sizeof(*e->mark)); nuclear@0: nuclear@0: } nuclear@0: nuclear@0: void _ve_envelope_clear(envelope_lookup *e){ nuclear@0: int i; nuclear@0: mdct_clear(&e->mdct); nuclear@0: for(i=0;iband[i].window); nuclear@0: _ogg_free(e->mdct_win); nuclear@0: _ogg_free(e->filter); nuclear@0: _ogg_free(e->mark); nuclear@0: memset(e,0,sizeof(*e)); nuclear@0: } nuclear@0: nuclear@0: /* fairly straight threshhold-by-band based until we find something nuclear@0: that works better and isn't patented. */ nuclear@0: nuclear@0: static int _ve_amp(envelope_lookup *ve, nuclear@0: vorbis_info_psy_global *gi, nuclear@0: float *data, nuclear@0: envelope_band *bands, nuclear@0: envelope_filter_state *filters){ nuclear@0: long n=ve->winlength; nuclear@0: int ret=0; nuclear@0: long i,j; nuclear@0: float decay; nuclear@0: nuclear@0: /* we want to have a 'minimum bar' for energy, else we're just nuclear@0: basing blocks on quantization noise that outweighs the signal nuclear@0: itself (for low power signals) */ nuclear@0: nuclear@0: float minV=ve->minenergy; nuclear@0: float *vec=alloca(n*sizeof(*vec)); nuclear@0: nuclear@0: /* stretch is used to gradually lengthen the number of windows nuclear@0: considered prevoius-to-potential-trigger */ nuclear@0: int stretch=max(VE_MINSTRETCH,ve->stretch/2); nuclear@0: float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH); nuclear@0: if(penalty<0.f)penalty=0.f; nuclear@0: if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty; nuclear@0: nuclear@0: /*_analysis_output_always("lpcm",seq2,data,n,0,0, nuclear@0: totalshift+pos*ve->searchstep);*/ nuclear@0: nuclear@0: /* window and transform */ nuclear@0: for(i=0;imdct_win[i]; nuclear@0: mdct_forward(&ve->mdct,vec,vec); nuclear@0: nuclear@0: /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */ nuclear@0: nuclear@0: /* near-DC spreading function; this has nothing to do with nuclear@0: psychoacoustics, just sidelobe leakage and window size */ nuclear@0: { nuclear@0: float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2]; nuclear@0: int ptr=filters->nearptr; nuclear@0: nuclear@0: /* the accumulation is regularly refreshed from scratch to avoid nuclear@0: floating point creep */ nuclear@0: if(ptr==0){ nuclear@0: decay=filters->nearDC_acc=filters->nearDC_partialacc+temp; nuclear@0: filters->nearDC_partialacc=temp; nuclear@0: }else{ nuclear@0: decay=filters->nearDC_acc+=temp; nuclear@0: filters->nearDC_partialacc+=temp; nuclear@0: } nuclear@0: filters->nearDC_acc-=filters->nearDC[ptr]; nuclear@0: filters->nearDC[ptr]=temp; nuclear@0: nuclear@0: decay*=(1./(VE_NEARDC+1)); nuclear@0: filters->nearptr++; nuclear@0: if(filters->nearptr>=VE_NEARDC)filters->nearptr=0; nuclear@0: decay=todB(&decay)*.5-15.f; nuclear@0: } nuclear@0: nuclear@0: /* perform spreading and limiting, also smooth the spectrum. yes, nuclear@0: the MDCT results in all real coefficients, but it still *behaves* nuclear@0: like real/imaginary pairs */ nuclear@0: for(i=0;i>1]=val; nuclear@0: decay-=8.; nuclear@0: } nuclear@0: nuclear@0: /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/ nuclear@0: nuclear@0: /* perform preecho/postecho triggering by band */ nuclear@0: for(j=0;j=VE_AMP)filters[j].ampptr=0; nuclear@0: } nuclear@0: nuclear@0: /* look at min/max, decide trigger */ nuclear@0: if(valmax>gi->preecho_thresh[j]+penalty){ nuclear@0: ret|=1; nuclear@0: ret|=4; nuclear@0: } nuclear@0: if(valminpostecho_thresh[j]-penalty)ret|=2; nuclear@0: } nuclear@0: nuclear@0: return(ret); nuclear@0: } nuclear@0: nuclear@0: #if 0 nuclear@0: static int seq=0; nuclear@0: static ogg_int64_t totalshift=-1024; nuclear@0: #endif nuclear@0: nuclear@0: long _ve_envelope_search(vorbis_dsp_state *v){ nuclear@0: vorbis_info *vi=v->vi; nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: vorbis_info_psy_global *gi=&ci->psy_g_param; nuclear@0: envelope_lookup *ve=((private_state *)(v->backend_state))->ve; nuclear@0: long i,j; nuclear@0: nuclear@0: int first=ve->current/ve->searchstep; nuclear@0: int last=v->pcm_current/ve->searchstep-VE_WIN; nuclear@0: if(first<0)first=0; nuclear@0: nuclear@0: /* make sure we have enough storage to match the PCM */ nuclear@0: if(last+VE_WIN+VE_POST>ve->storage){ nuclear@0: ve->storage=last+VE_WIN+VE_POST; /* be sure */ nuclear@0: ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark)); nuclear@0: } nuclear@0: nuclear@0: for(j=first;jstretch++; nuclear@0: if(ve->stretch>VE_MAXSTRETCH*2) nuclear@0: ve->stretch=VE_MAXSTRETCH*2; nuclear@0: nuclear@0: for(i=0;ich;i++){ nuclear@0: float *pcm=v->pcm[i]+ve->searchstep*(j); nuclear@0: ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS); nuclear@0: } nuclear@0: nuclear@0: ve->mark[j+VE_POST]=0; nuclear@0: if(ret&1){ nuclear@0: ve->mark[j]=1; nuclear@0: ve->mark[j+1]=1; nuclear@0: } nuclear@0: nuclear@0: if(ret&2){ nuclear@0: ve->mark[j]=1; nuclear@0: if(j>0)ve->mark[j-1]=1; nuclear@0: } nuclear@0: nuclear@0: if(ret&4)ve->stretch=-1; nuclear@0: } nuclear@0: nuclear@0: ve->current=last*ve->searchstep; nuclear@0: nuclear@0: { nuclear@0: long centerW=v->centerW; nuclear@0: long testW= nuclear@0: centerW+ nuclear@0: ci->blocksizes[v->W]/4+ nuclear@0: ci->blocksizes[1]/2+ nuclear@0: ci->blocksizes[0]/4; nuclear@0: nuclear@0: j=ve->cursor; nuclear@0: nuclear@0: while(jcurrent-(ve->searchstep)){/* account for postecho nuclear@0: working back one window */ nuclear@0: if(j>=testW)return(1); nuclear@0: nuclear@0: ve->cursor=j; nuclear@0: nuclear@0: if(ve->mark[j/ve->searchstep]){ nuclear@0: if(j>centerW){ nuclear@0: nuclear@0: #if 0 nuclear@0: if(j>ve->curmark){ nuclear@0: float *marker=alloca(v->pcm_current*sizeof(*marker)); nuclear@0: int l,m; nuclear@0: memset(marker,0,sizeof(*marker)*v->pcm_current); nuclear@0: fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n", nuclear@0: seq, nuclear@0: (totalshift+ve->cursor)/44100., nuclear@0: (totalshift+j)/44100.); nuclear@0: _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift); nuclear@0: _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift); nuclear@0: nuclear@0: _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift); nuclear@0: _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift); nuclear@0: nuclear@0: for(m=0;msearchstep]=ve->filter[m].markers[l]*.1; nuclear@0: _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift); nuclear@0: } nuclear@0: nuclear@0: for(m=0;msearchstep]=ve->filter[m+VE_BANDS].markers[l]*.1; nuclear@0: _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift); nuclear@0: } nuclear@0: nuclear@0: for(l=0;lsearchstep]=ve->mark[l]*.4; nuclear@0: _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift); nuclear@0: nuclear@0: nuclear@0: seq++; nuclear@0: nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: ve->curmark=j; nuclear@0: if(j>=testW)return(1); nuclear@0: return(0); nuclear@0: } nuclear@0: } nuclear@0: j+=ve->searchstep; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: return(-1); nuclear@0: } nuclear@0: nuclear@0: int _ve_envelope_mark(vorbis_dsp_state *v){ nuclear@0: envelope_lookup *ve=((private_state *)(v->backend_state))->ve; nuclear@0: vorbis_info *vi=v->vi; nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: long centerW=v->centerW; nuclear@0: long beginW=centerW-ci->blocksizes[v->W]/4; nuclear@0: long endW=centerW+ci->blocksizes[v->W]/4; nuclear@0: if(v->W){ nuclear@0: beginW-=ci->blocksizes[v->lW]/4; nuclear@0: endW+=ci->blocksizes[v->nW]/4; nuclear@0: }else{ nuclear@0: beginW-=ci->blocksizes[0]/4; nuclear@0: endW+=ci->blocksizes[0]/4; nuclear@0: } nuclear@0: nuclear@0: if(ve->curmark>=beginW && ve->curmarksearchstep; nuclear@0: long last=endW/ve->searchstep; nuclear@0: long i; nuclear@0: for(i=first;imark[i])return(1); nuclear@0: } nuclear@0: return(0); nuclear@0: } nuclear@0: nuclear@0: void _ve_envelope_shift(envelope_lookup *e,long shift){ nuclear@0: int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks nuclear@0: ahead of ve->current */ nuclear@0: int smallshift=shift/e->searchstep; nuclear@0: nuclear@0: memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark)); nuclear@0: nuclear@0: #if 0 nuclear@0: for(i=0;ich;i++) nuclear@0: memmove(e->filter[i].markers, nuclear@0: e->filter[i].markers+smallshift, nuclear@0: (1024-smallshift)*sizeof(*(*e->filter).markers)); nuclear@0: totalshift+=shift; nuclear@0: #endif nuclear@0: nuclear@0: e->current-=shift; nuclear@0: if(e->curmark>=0) nuclear@0: e->curmark-=shift; nuclear@0: e->cursor-=shift; nuclear@0: }