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