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-2010 * nuclear@1: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@1: * * nuclear@1: ******************************************************************** nuclear@1: nuclear@1: function: psychoacoustics not including preecho nuclear@1: last mod: $Id: psy.c 18077 2011-09-02 02:49:00Z giles $ nuclear@1: nuclear@1: ********************************************************************/ nuclear@1: 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 "masking.h" nuclear@1: #include "psy.h" nuclear@1: #include "os.h" nuclear@1: #include "lpc.h" nuclear@1: #include "smallft.h" nuclear@1: #include "scales.h" nuclear@1: #include "misc.h" nuclear@1: nuclear@1: #define NEGINF -9999.f nuclear@1: static const double stereo_threshholds[]={0.0, .5, 1.0, 1.5, 2.5, 4.5, 8.5, 16.5, 9e10}; nuclear@1: static const double stereo_threshholds_limited[]={0.0, .5, 1.0, 1.5, 2.0, 2.5, 4.5, 8.5, 9e10}; nuclear@1: nuclear@1: vorbis_look_psy_global *_vp_global_look(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: vorbis_look_psy_global *look=_ogg_calloc(1,sizeof(*look)); nuclear@1: nuclear@1: look->channels=vi->channels; nuclear@1: nuclear@1: look->ampmax=-9999.; nuclear@1: look->gi=gi; nuclear@1: return(look); nuclear@1: } nuclear@1: nuclear@1: void _vp_global_free(vorbis_look_psy_global *look){ nuclear@1: if(look){ nuclear@1: memset(look,0,sizeof(*look)); nuclear@1: _ogg_free(look); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: void _vi_gpsy_free(vorbis_info_psy_global *i){ nuclear@1: if(i){ nuclear@1: memset(i,0,sizeof(*i)); nuclear@1: _ogg_free(i); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: void _vi_psy_free(vorbis_info_psy *i){ nuclear@1: if(i){ nuclear@1: memset(i,0,sizeof(*i)); nuclear@1: _ogg_free(i); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void min_curve(float *c, nuclear@1: float *c2){ nuclear@1: int i; nuclear@1: for(i=0;ic[i])c[i]=c2[i]; nuclear@1: } nuclear@1: nuclear@1: static void attenuate_curve(float *c,float att){ nuclear@1: int i; nuclear@1: for(i=0;iATH[j+k+ath_offset])min=ATH[j+k+ath_offset]; nuclear@1: }else{ nuclear@1: if(min>ATH[MAX_ATH-1])min=ATH[MAX_ATH-1]; nuclear@1: } nuclear@1: ath[j]=min; nuclear@1: } nuclear@1: nuclear@1: /* copy curves into working space, replicate the 50dB curve to 30 nuclear@1: and 40, replicate the 100dB curve to 110 */ nuclear@1: for(j=0;j<6;j++) nuclear@1: memcpy(workc[i][j+2],tonemasks[i][j],EHMER_MAX*sizeof(*tonemasks[i][j])); nuclear@1: memcpy(workc[i][0],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0])); nuclear@1: memcpy(workc[i][1],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0])); nuclear@1: nuclear@1: /* apply centered curve boost/decay */ nuclear@1: for(j=0;j0)adj=0.; nuclear@1: if(adj>0. && center_boost<0)adj=0.; nuclear@1: workc[i][j][k]+=adj; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* normalize curves so the driving amplitude is 0dB */ nuclear@1: /* make temp curves with the ATH overlayed */ nuclear@1: for(j=0;j an eighth of an octave and that the eighth nuclear@1: octave values may also be composited. */ nuclear@1: nuclear@1: /* which octave curves will we be compositing? */ nuclear@1: bin=floor(fromOC(i*.5)/binHz); nuclear@1: lo_curve= ceil(toOC(bin*binHz+1)*2); nuclear@1: hi_curve= floor(toOC((bin+1)*binHz)*2); nuclear@1: if(lo_curve>i)lo_curve=i; nuclear@1: if(lo_curve<0)lo_curve=0; nuclear@1: if(hi_curve>=P_BANDS)hi_curve=P_BANDS-1; nuclear@1: nuclear@1: for(m=0;mn)lo_bin=n; nuclear@1: if(lo_binn)hi_bin=n; nuclear@1: nuclear@1: for(;lworkc[k][m][j]) nuclear@1: brute_buffer[l]=workc[k][m][j]; nuclear@1: } nuclear@1: nuclear@1: for(;lworkc[k][m][EHMER_MAX-1]) nuclear@1: brute_buffer[l]=workc[k][m][EHMER_MAX-1]; nuclear@1: nuclear@1: } nuclear@1: nuclear@1: /* be equally paranoid about being valid up to next half ocatve */ nuclear@1: if(i+1n)lo_bin=n; nuclear@1: if(lo_binn)hi_bin=n; nuclear@1: nuclear@1: for(;lworkc[k][m][j]) nuclear@1: brute_buffer[l]=workc[k][m][j]; nuclear@1: } nuclear@1: nuclear@1: for(;lworkc[k][m][EHMER_MAX-1]) nuclear@1: brute_buffer[l]=workc[k][m][EHMER_MAX-1]; nuclear@1: nuclear@1: } nuclear@1: nuclear@1: nuclear@1: for(j=0;j=n){ nuclear@1: ret[i][m][j+2]=-999.; nuclear@1: }else{ nuclear@1: ret[i][m][j+2]=brute_buffer[bin]; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* add fenceposts */ nuclear@1: for(j=0;j-200.f)break; nuclear@1: ret[i][m][0]=j; nuclear@1: nuclear@1: for(j=EHMER_MAX-1;j>EHMER_OFFSET+1;j--) nuclear@1: if(ret[i][m][j+2]>-200.f) nuclear@1: break; nuclear@1: ret[i][m][1]=j; nuclear@1: nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: return(ret); nuclear@1: } nuclear@1: nuclear@1: void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi, nuclear@1: vorbis_info_psy_global *gi,int n,long rate){ nuclear@1: long i,j,lo=-99,hi=1; nuclear@1: long maxoc; nuclear@1: memset(p,0,sizeof(*p)); nuclear@1: nuclear@1: p->eighth_octave_lines=gi->eighth_octave_lines; nuclear@1: p->shiftoc=rint(log(gi->eighth_octave_lines*8.f)/log(2.f))-1; nuclear@1: nuclear@1: p->firstoc=toOC(.25f*rate*.5/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines; nuclear@1: maxoc=toOC((n+.25f)*rate*.5/n)*(1<<(p->shiftoc+1))+.5f; nuclear@1: p->total_octave_lines=maxoc-p->firstoc+1; nuclear@1: p->ath=_ogg_malloc(n*sizeof(*p->ath)); nuclear@1: nuclear@1: p->octave=_ogg_malloc(n*sizeof(*p->octave)); nuclear@1: p->bark=_ogg_malloc(n*sizeof(*p->bark)); nuclear@1: p->vi=vi; nuclear@1: p->n=n; nuclear@1: p->rate=rate; nuclear@1: nuclear@1: /* AoTuV HF weighting */ nuclear@1: p->m_val = 1.; nuclear@1: if(rate < 26000) p->m_val = 0; nuclear@1: else if(rate < 38000) p->m_val = .94; /* 32kHz */ nuclear@1: else if(rate > 46000) p->m_val = 1.275; /* 48kHz */ nuclear@1: nuclear@1: /* set up the lookups for a given blocksize and sample rate */ nuclear@1: nuclear@1: for(i=0,j=0;iath[j]=base+100.; nuclear@1: base+=delta; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: for(;jath[j]=p->ath[j-1]; nuclear@1: } nuclear@1: nuclear@1: for(i=0;inoisewindowlominnoisewindowlo);lo++); nuclear@1: nuclear@1: for(;hi<=n && (hinoisewindowhimin || nuclear@1: toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++); nuclear@1: nuclear@1: p->bark[i]=((lo-1)<<16)+(hi-1); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: for(i=0;ioctave[i]=toOC((i+.25f)*.5*rate/n)*(1<<(p->shiftoc+1))+.5f; nuclear@1: nuclear@1: p->tonecurves=setup_tone_curves(vi->toneatt,rate*.5/n,n, nuclear@1: vi->tone_centerboost,vi->tone_decay); nuclear@1: nuclear@1: /* set up rolling noise median */ nuclear@1: p->noiseoffset=_ogg_malloc(P_NOISECURVES*sizeof(*p->noiseoffset)); nuclear@1: for(i=0;inoiseoffset[i]=_ogg_malloc(n*sizeof(**p->noiseoffset)); nuclear@1: nuclear@1: for(i=0;i=P_BANDS-1)halfoc=P_BANDS-1; nuclear@1: inthalfoc=(int)halfoc; nuclear@1: del=halfoc-inthalfoc; nuclear@1: nuclear@1: for(j=0;jnoiseoffset[j][i]= nuclear@1: p->vi->noiseoff[j][inthalfoc]*(1.-del) + nuclear@1: p->vi->noiseoff[j][inthalfoc+1]*del; nuclear@1: nuclear@1: } nuclear@1: #if 0 nuclear@1: { nuclear@1: static int ls=0; nuclear@1: _analysis_output_always("noiseoff0",ls,p->noiseoffset[0],n,1,0,0); nuclear@1: _analysis_output_always("noiseoff1",ls,p->noiseoffset[1],n,1,0,0); nuclear@1: _analysis_output_always("noiseoff2",ls++,p->noiseoffset[2],n,1,0,0); nuclear@1: } nuclear@1: #endif nuclear@1: } nuclear@1: nuclear@1: void _vp_psy_clear(vorbis_look_psy *p){ nuclear@1: int i,j; nuclear@1: if(p){ nuclear@1: if(p->ath)_ogg_free(p->ath); nuclear@1: if(p->octave)_ogg_free(p->octave); nuclear@1: if(p->bark)_ogg_free(p->bark); nuclear@1: if(p->tonecurves){ nuclear@1: for(i=0;itonecurves[i][j]); nuclear@1: } nuclear@1: _ogg_free(p->tonecurves[i]); nuclear@1: } nuclear@1: _ogg_free(p->tonecurves); nuclear@1: } nuclear@1: if(p->noiseoffset){ nuclear@1: for(i=0;inoiseoffset[i]); nuclear@1: } nuclear@1: _ogg_free(p->noiseoffset); nuclear@1: } nuclear@1: memset(p,0,sizeof(*p)); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* octave/(8*eighth_octave_lines) x scale and dB y scale */ nuclear@1: static void seed_curve(float *seed, nuclear@1: const float **curves, nuclear@1: float amp, nuclear@1: int oc, int n, nuclear@1: int linesper,float dBoffset){ nuclear@1: int i,post1; nuclear@1: int seedptr; nuclear@1: const float *posts,*curve; nuclear@1: nuclear@1: int choice=(int)((amp+dBoffset-P_LEVEL_0)*.1f); nuclear@1: choice=max(choice,0); nuclear@1: choice=min(choice,P_LEVELS-1); nuclear@1: posts=curves[choice]; nuclear@1: curve=posts+2; nuclear@1: post1=(int)posts[1]; nuclear@1: seedptr=oc+(posts[0]-EHMER_OFFSET)*linesper-(linesper>>1); nuclear@1: nuclear@1: for(i=posts[0];i0){ nuclear@1: float lin=amp+curve[i]; nuclear@1: if(seed[seedptr]=n)break; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void seed_loop(vorbis_look_psy *p, nuclear@1: const float ***curves, nuclear@1: const float *f, nuclear@1: const float *flr, nuclear@1: float *seed, nuclear@1: float specmax){ nuclear@1: vorbis_info_psy *vi=p->vi; nuclear@1: long n=p->n,i; nuclear@1: float dBoffset=vi->max_curve_dB-specmax; nuclear@1: nuclear@1: /* prime the working vector with peak values */ nuclear@1: nuclear@1: for(i=0;ioctave[i]; nuclear@1: while(i+1octave[i+1]==oc){ nuclear@1: i++; nuclear@1: if(f[i]>max)max=f[i]; nuclear@1: } nuclear@1: nuclear@1: if(max+6.f>flr[i]){ nuclear@1: oc=oc>>p->shiftoc; nuclear@1: nuclear@1: if(oc>=P_BANDS)oc=P_BANDS-1; nuclear@1: if(oc<0)oc=0; nuclear@1: nuclear@1: seed_curve(seed, nuclear@1: curves[oc], nuclear@1: max, nuclear@1: p->octave[i]-p->firstoc, nuclear@1: p->total_octave_lines, nuclear@1: p->eighth_octave_lines, nuclear@1: dBoffset); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void seed_chase(float *seeds, int linesper, long n){ nuclear@1: long *posstack=alloca(n*sizeof(*posstack)); nuclear@1: float *ampstack=alloca(n*sizeof(*ampstack)); nuclear@1: long stack=0; nuclear@1: long pos=0; nuclear@1: long i; nuclear@1: nuclear@1: for(i=0;i1 && ampstack[stack-1]<=ampstack[stack-2] && nuclear@1: iampstack[i]){ nuclear@1: endpos=posstack[i+1]; nuclear@1: }else{ nuclear@1: endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is nuclear@1: discarded in short frames */ nuclear@1: } nuclear@1: if(endpos>n)endpos=n; nuclear@1: for(;pos nuclear@1: static void max_seeds(vorbis_look_psy *p, nuclear@1: float *seed, nuclear@1: float *flr){ nuclear@1: long n=p->total_octave_lines; nuclear@1: int linesper=p->eighth_octave_lines; nuclear@1: long linpos=0; nuclear@1: long pos; nuclear@1: nuclear@1: seed_chase(seed,linesper,n); /* for masking */ nuclear@1: nuclear@1: pos=p->octave[0]-p->firstoc-(linesper>>1); nuclear@1: nuclear@1: while(linpos+1n){ nuclear@1: float minV=seed[pos]; nuclear@1: long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc; nuclear@1: if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit; nuclear@1: while(pos+1<=end){ nuclear@1: pos++; nuclear@1: if((seed[pos]>NEGINF && seed[pos]firstoc; nuclear@1: for(;linposn && p->octave[linpos]<=end;linpos++) nuclear@1: if(flr[linpos]total_octave_lines-1]; nuclear@1: for(;linposn;linpos++) nuclear@1: if(flr[linpos]> 16; nuclear@1: if( lo>=0 ) break; nuclear@1: hi = b[i] & 0xffff; nuclear@1: nuclear@1: tN = N[hi] + N[-lo]; nuclear@1: tX = X[hi] - X[-lo]; nuclear@1: tXX = XX[hi] + XX[-lo]; nuclear@1: tY = Y[hi] + Y[-lo]; nuclear@1: tXY = XY[hi] - XY[-lo]; nuclear@1: nuclear@1: A = tY * tXX - tX * tXY; nuclear@1: B = tN * tXY - tX * tY; nuclear@1: D = tN * tXX - tX * tX; nuclear@1: R = (A + x * B) / D; nuclear@1: if (R < 0.f) nuclear@1: R = 0.f; nuclear@1: nuclear@1: noise[i] = R - offset; nuclear@1: } nuclear@1: nuclear@1: for ( ;; i++, x += 1.f) { nuclear@1: nuclear@1: lo = b[i] >> 16; nuclear@1: hi = b[i] & 0xffff; nuclear@1: if(hi>=n)break; nuclear@1: nuclear@1: tN = N[hi] - N[lo]; nuclear@1: tX = X[hi] - X[lo]; nuclear@1: tXX = XX[hi] - XX[lo]; nuclear@1: tY = Y[hi] - Y[lo]; nuclear@1: tXY = XY[hi] - XY[lo]; nuclear@1: nuclear@1: A = tY * tXX - tX * tXY; nuclear@1: B = tN * tXY - tX * tY; nuclear@1: D = tN * tXX - tX * tX; nuclear@1: R = (A + x * B) / D; nuclear@1: if (R < 0.f) R = 0.f; nuclear@1: nuclear@1: noise[i] = R - offset; nuclear@1: } nuclear@1: for ( ; i < n; i++, x += 1.f) { nuclear@1: nuclear@1: R = (A + x * B) / D; nuclear@1: if (R < 0.f) R = 0.f; nuclear@1: nuclear@1: noise[i] = R - offset; nuclear@1: } nuclear@1: nuclear@1: if (fixed <= 0) return; nuclear@1: nuclear@1: for (i = 0, x = 0.f;; i++, x += 1.f) { nuclear@1: hi = i + fixed / 2; nuclear@1: lo = hi - fixed; nuclear@1: if(lo>=0)break; nuclear@1: nuclear@1: tN = N[hi] + N[-lo]; nuclear@1: tX = X[hi] - X[-lo]; nuclear@1: tXX = XX[hi] + XX[-lo]; nuclear@1: tY = Y[hi] + Y[-lo]; nuclear@1: tXY = XY[hi] - XY[-lo]; nuclear@1: nuclear@1: nuclear@1: A = tY * tXX - tX * tXY; nuclear@1: B = tN * tXY - tX * tY; nuclear@1: D = tN * tXX - tX * tX; nuclear@1: R = (A + x * B) / D; nuclear@1: nuclear@1: if (R - offset < noise[i]) noise[i] = R - offset; nuclear@1: } nuclear@1: for ( ;; i++, x += 1.f) { nuclear@1: nuclear@1: hi = i + fixed / 2; nuclear@1: lo = hi - fixed; nuclear@1: if(hi>=n)break; nuclear@1: nuclear@1: tN = N[hi] - N[lo]; nuclear@1: tX = X[hi] - X[lo]; nuclear@1: tXX = XX[hi] - XX[lo]; nuclear@1: tY = Y[hi] - Y[lo]; nuclear@1: tXY = XY[hi] - XY[lo]; nuclear@1: nuclear@1: A = tY * tXX - tX * tXY; nuclear@1: B = tN * tXY - tX * tY; nuclear@1: D = tN * tXX - tX * tX; nuclear@1: R = (A + x * B) / D; nuclear@1: nuclear@1: if (R - offset < noise[i]) noise[i] = R - offset; nuclear@1: } nuclear@1: for ( ; i < n; i++, x += 1.f) { nuclear@1: R = (A + x * B) / D; nuclear@1: if (R - offset < noise[i]) noise[i] = R - offset; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: void _vp_noisemask(vorbis_look_psy *p, nuclear@1: float *logmdct, nuclear@1: float *logmask){ nuclear@1: nuclear@1: int i,n=p->n; nuclear@1: float *work=alloca(n*sizeof(*work)); nuclear@1: nuclear@1: bark_noise_hybridmp(n,p->bark,logmdct,logmask, nuclear@1: 140.,-1); nuclear@1: nuclear@1: for(i=0;ibark,work,logmask,0., nuclear@1: p->vi->noisewindowfixed); nuclear@1: nuclear@1: for(i=0;i=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1; nuclear@1: if(dB<0)dB=0; nuclear@1: logmask[i]= work[i]+p->vi->noisecompand[dB]; nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: void _vp_tonemask(vorbis_look_psy *p, nuclear@1: float *logfft, nuclear@1: float *logmask, nuclear@1: float global_specmax, nuclear@1: float local_specmax){ nuclear@1: nuclear@1: int i,n=p->n; nuclear@1: nuclear@1: float *seed=alloca(sizeof(*seed)*p->total_octave_lines); nuclear@1: float att=local_specmax+p->vi->ath_adjatt; nuclear@1: for(i=0;itotal_octave_lines;i++)seed[i]=NEGINF; nuclear@1: nuclear@1: /* set the ATH (floating below localmax, not global max by a nuclear@1: specified att) */ nuclear@1: if(attvi->ath_maxatt)att=p->vi->ath_maxatt; nuclear@1: nuclear@1: for(i=0;iath[i]+att; nuclear@1: nuclear@1: /* tone masking */ nuclear@1: seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax); nuclear@1: max_seeds(p,seed,logmask); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: void _vp_offset_and_mix(vorbis_look_psy *p, nuclear@1: float *noise, nuclear@1: float *tone, nuclear@1: int offset_select, nuclear@1: float *logmask, nuclear@1: float *mdct, nuclear@1: float *logmdct){ nuclear@1: int i,n=p->n; nuclear@1: float de, coeffi, cx;/* AoTuV */ nuclear@1: float toneatt=p->vi->tone_masteratt[offset_select]; nuclear@1: nuclear@1: cx = p->m_val; nuclear@1: nuclear@1: for(i=0;inoiseoffset[offset_select][i]; nuclear@1: if(val>p->vi->noisemaxsupp)val=p->vi->noisemaxsupp; nuclear@1: logmask[i]=max(val,tone[i]+toneatt); nuclear@1: nuclear@1: nuclear@1: /* AoTuV */ nuclear@1: /** @ M1 ** nuclear@1: The following codes improve a noise problem. nuclear@1: A fundamental idea uses the value of masking and carries out nuclear@1: the relative compensation of the MDCT. nuclear@1: However, this code is not perfect and all noise problems cannot be solved. nuclear@1: by Aoyumi @ 2004/04/18 nuclear@1: */ nuclear@1: nuclear@1: if(offset_select == 1) { nuclear@1: coeffi = -17.2; /* coeffi is a -17.2dB threshold */ nuclear@1: val = val - logmdct[i]; /* val == mdct line value relative to floor in dB */ nuclear@1: nuclear@1: if(val > coeffi){ nuclear@1: /* mdct value is > -17.2 dB below floor */ nuclear@1: nuclear@1: de = 1.0-((val-coeffi)*0.005*cx); nuclear@1: /* pro-rated attenuation: nuclear@1: -0.00 dB boost if mdct value is -17.2dB (relative to floor) nuclear@1: -0.77 dB boost if mdct value is 0dB (relative to floor) nuclear@1: -1.64 dB boost if mdct value is +17.2dB (relative to floor) nuclear@1: etc... */ nuclear@1: nuclear@1: if(de < 0) de = 0.0001; nuclear@1: }else nuclear@1: /* mdct value is <= -17.2 dB below floor */ nuclear@1: nuclear@1: de = 1.0-((val-coeffi)*0.0003*cx); nuclear@1: /* pro-rated attenuation: nuclear@1: +0.00 dB atten if mdct value is -17.2dB (relative to floor) nuclear@1: +0.45 dB atten if mdct value is -34.4dB (relative to floor) nuclear@1: etc... */ nuclear@1: nuclear@1: mdct[i] *= de; nuclear@1: nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){ nuclear@1: vorbis_info *vi=vd->vi; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: vorbis_info_psy_global *gi=&ci->psy_g_param; nuclear@1: nuclear@1: int n=ci->blocksizes[vd->W]/2; nuclear@1: float secs=(float)n/vi->rate; nuclear@1: nuclear@1: amp+=secs*gi->ampmax_att_per_sec; nuclear@1: if(amp<-9999)amp=-9999; nuclear@1: return(amp); nuclear@1: } nuclear@1: nuclear@1: static float FLOOR1_fromdB_LOOKUP[256]={ nuclear@1: 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F, nuclear@1: 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F, nuclear@1: 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F, nuclear@1: 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F, nuclear@1: 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F, nuclear@1: 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F, nuclear@1: 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F, nuclear@1: 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F, nuclear@1: 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F, nuclear@1: 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F, nuclear@1: 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F, nuclear@1: 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F, nuclear@1: 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F, nuclear@1: 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F, nuclear@1: 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F, nuclear@1: 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F, nuclear@1: 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F, nuclear@1: 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F, nuclear@1: 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F, nuclear@1: 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F, nuclear@1: 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F, nuclear@1: 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F, nuclear@1: 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F, nuclear@1: 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F, nuclear@1: 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F, nuclear@1: 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F, nuclear@1: 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F, nuclear@1: 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F, nuclear@1: 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F, nuclear@1: 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F, nuclear@1: 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F, nuclear@1: 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F, nuclear@1: 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F, nuclear@1: 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F, nuclear@1: 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F, nuclear@1: 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F, nuclear@1: 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F, nuclear@1: 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F, nuclear@1: 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F, nuclear@1: 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F, nuclear@1: 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F, nuclear@1: 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F, nuclear@1: 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F, nuclear@1: 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F, nuclear@1: 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F, nuclear@1: 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F, nuclear@1: 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F, nuclear@1: 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F, nuclear@1: 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F, nuclear@1: 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F, nuclear@1: 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F, nuclear@1: 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F, nuclear@1: 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F, nuclear@1: 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F, nuclear@1: 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F, nuclear@1: 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F, nuclear@1: 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F, nuclear@1: 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F, nuclear@1: 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F, nuclear@1: 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F, nuclear@1: 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F, nuclear@1: 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F, nuclear@1: 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F, nuclear@1: 0.82788260F, 0.88168307F, 0.9389798F, 1.F, nuclear@1: }; nuclear@1: nuclear@1: /* this is for per-channel noise normalization */ nuclear@1: static int apsort(const void *a, const void *b){ nuclear@1: float f1=**(float**)a; nuclear@1: float f2=**(float**)b; nuclear@1: return (f1f2); nuclear@1: } nuclear@1: nuclear@1: static void flag_lossless(int limit, float prepoint, float postpoint, float *mdct, nuclear@1: float *floor, int *flag, int i, int jn){ nuclear@1: int j; nuclear@1: for(j=0;j=limit-i ? postpoint : prepoint; nuclear@1: float r = fabs(mdct[j])/floor[j]; nuclear@1: if(rvi; nuclear@1: float **sort = alloca(n*sizeof(*sort)); nuclear@1: int j,count=0; nuclear@1: int start = (vi->normal_p ? vi->normal_start-i : n); nuclear@1: if(start>n)start=n; nuclear@1: nuclear@1: /* force classic behavior where only energy in the current band is considered */ nuclear@1: acc=0.f; nuclear@1: nuclear@1: /* still responsible for populating *out where noise norm not in nuclear@1: effect. There's no need to [re]populate *q in these areas */ nuclear@1: for(j=0;j pointlimit */ nuclear@1: if(ve<.25f && (!flags || j>=limit-i)){ nuclear@1: acc += ve; nuclear@1: sort[count++]=q+j; /* q is fabs(r) for unflagged element */ nuclear@1: }else{ nuclear@1: /* For now: no acc adjustment for nonzero quantization. populate *out and q as this value is final. */ nuclear@1: if(r[j]<0) nuclear@1: out[j] = -rint(sqrt(ve)); nuclear@1: else nuclear@1: out[j] = rint(sqrt(ve)); nuclear@1: q[j] = out[j]*out[j]*f[j]; nuclear@1: } nuclear@1: }/* else{ nuclear@1: again, no energy adjustment for error in nonzero quant-- for now nuclear@1: }*/ nuclear@1: } nuclear@1: nuclear@1: if(count){ nuclear@1: /* noise norm to do */ nuclear@1: qsort(sort,count,sizeof(*sort),apsort); nuclear@1: for(j=0;j=vi->normal_thresh){ nuclear@1: out[k]=unitnorm(r[k]); nuclear@1: acc-=1.f; nuclear@1: q[k]=f[k]; nuclear@1: }else{ nuclear@1: out[k]=0; nuclear@1: q[k]=0.f; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: return acc; nuclear@1: } nuclear@1: nuclear@1: /* Noise normalization, quantization and coupling are not wholly nuclear@1: seperable processes in depth>1 coupling. */ nuclear@1: void _vp_couple_quantize_normalize(int blobno, nuclear@1: vorbis_info_psy_global *g, nuclear@1: vorbis_look_psy *p, nuclear@1: vorbis_info_mapping0 *vi, nuclear@1: float **mdct, nuclear@1: int **iwork, nuclear@1: int *nonzero, nuclear@1: int sliding_lowpass, nuclear@1: int ch){ nuclear@1: nuclear@1: int i; nuclear@1: int n = p->n; nuclear@1: int partition=(p->vi->normal_p ? p->vi->normal_partition : 16); nuclear@1: int limit = g->coupling_pointlimit[p->vi->blockflag][blobno]; nuclear@1: float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]]; nuclear@1: float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]]; nuclear@1: #if 0 nuclear@1: float de=0.1*p->m_val; /* a blend of the AoTuV M2 and M3 code here and below */ nuclear@1: #endif nuclear@1: nuclear@1: /* mdct is our raw mdct output, floor not removed. */ nuclear@1: /* inout passes in the ifloor, passes back quantized result */ nuclear@1: nuclear@1: /* unquantized energy (negative indicates amplitude has negative sign) */ nuclear@1: float **raw = alloca(ch*sizeof(*raw)); nuclear@1: nuclear@1: /* dual pupose; quantized energy (if flag set), othersize fabs(raw) */ nuclear@1: float **quant = alloca(ch*sizeof(*quant)); nuclear@1: nuclear@1: /* floor energy */ nuclear@1: float **floor = alloca(ch*sizeof(*floor)); nuclear@1: nuclear@1: /* flags indicating raw/quantized status of elements in raw vector */ nuclear@1: int **flag = alloca(ch*sizeof(*flag)); nuclear@1: nuclear@1: /* non-zero flag working vector */ nuclear@1: int *nz = alloca(ch*sizeof(*nz)); nuclear@1: nuclear@1: /* energy surplus/defecit tracking */ nuclear@1: float *acc = alloca((ch+vi->coupling_steps)*sizeof(*acc)); nuclear@1: nuclear@1: /* The threshold of a stereo is changed with the size of n */ nuclear@1: if(n > 1000) nuclear@1: postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]]; nuclear@1: nuclear@1: raw[0] = alloca(ch*partition*sizeof(**raw)); nuclear@1: quant[0] = alloca(ch*partition*sizeof(**quant)); nuclear@1: floor[0] = alloca(ch*partition*sizeof(**floor)); nuclear@1: flag[0] = alloca(ch*partition*sizeof(**flag)); nuclear@1: nuclear@1: for(i=1;icoupling_steps;i++) nuclear@1: acc[i]=0.f; nuclear@1: nuclear@1: for(i=0;i n-i ? n-i : partition; nuclear@1: int step,track = 0; nuclear@1: nuclear@1: memcpy(nz,nonzero,sizeof(*nz)*ch); nuclear@1: nuclear@1: /* prefill */ nuclear@1: memset(flag[0],0,ch*partition*sizeof(**flag)); nuclear@1: for(k=0;kcoupling_steps;step++){ nuclear@1: int Mi = vi->coupling_mag[step]; nuclear@1: int Ai = vi->coupling_ang[step]; nuclear@1: int *iM = &iwork[Mi][i]; nuclear@1: int *iA = &iwork[Ai][i]; nuclear@1: float *reM = raw[Mi]; nuclear@1: float *reA = raw[Ai]; nuclear@1: float *qeM = quant[Mi]; nuclear@1: float *qeA = quant[Ai]; nuclear@1: float *floorM = floor[Mi]; nuclear@1: float *floorA = floor[Ai]; nuclear@1: int *fM = flag[Mi]; nuclear@1: int *fA = flag[Ai]; nuclear@1: nuclear@1: if(nz[Mi] || nz[Ai]){ nuclear@1: nz[Mi] = nz[Ai] = 1; nuclear@1: nuclear@1: for(j=0;jabs(B)){ nuclear@1: iA[j]=(A>0?A-B:B-A); nuclear@1: }else{ nuclear@1: iA[j]=(B>0?A-B:B-A); nuclear@1: iM[j]=B; nuclear@1: } nuclear@1: nuclear@1: /* collapse two equivalent tuples to one */ nuclear@1: if(iA[j]>=abs(iM[j])*2){ nuclear@1: iA[j]= -iA[j]; nuclear@1: iM[j]= -iM[j]; nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: }else{ nuclear@1: /* lossy (point) coupling */ nuclear@1: if(jcoupling_steps;i++){ nuclear@1: /* make sure coupling a zero and a nonzero channel results in two nuclear@1: nonzero channels. */ nuclear@1: if(nonzero[vi->coupling_mag[i]] || nuclear@1: nonzero[vi->coupling_ang[i]]){ nuclear@1: nonzero[vi->coupling_mag[i]]=1; nuclear@1: nonzero[vi->coupling_ang[i]]=1; nuclear@1: } nuclear@1: } nuclear@1: }