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