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