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: floor backend 1 implementation nuclear@1: last mod: $Id: floor1.c 18151 2012-01-20 07:35:26Z xiphmont $ nuclear@1: nuclear@1: ********************************************************************/ nuclear@1: 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: #include "registry.h" nuclear@1: #include "codebook.h" nuclear@1: #include "misc.h" nuclear@1: #include "scales.h" nuclear@1: nuclear@1: #include nuclear@1: nuclear@1: #define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */ nuclear@1: nuclear@1: typedef struct lsfit_acc{ nuclear@1: int x0; nuclear@1: int x1; nuclear@1: nuclear@1: int xa; nuclear@1: int ya; nuclear@1: int x2a; nuclear@1: int y2a; nuclear@1: int xya; nuclear@1: int an; nuclear@1: nuclear@1: int xb; nuclear@1: int yb; nuclear@1: int x2b; nuclear@1: int y2b; nuclear@1: int xyb; nuclear@1: int bn; nuclear@1: } lsfit_acc; nuclear@1: nuclear@1: /***********************************************/ nuclear@1: nuclear@1: static void floor1_free_info(vorbis_info_floor *i){ nuclear@1: vorbis_info_floor1 *info=(vorbis_info_floor1 *)i; nuclear@1: if(info){ nuclear@1: memset(info,0,sizeof(*info)); nuclear@1: _ogg_free(info); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void floor1_free_look(vorbis_look_floor *i){ nuclear@1: vorbis_look_floor1 *look=(vorbis_look_floor1 *)i; nuclear@1: if(look){ nuclear@1: /*fprintf(stderr,"floor 1 bit usage %f:%f (%f total)\n", nuclear@1: (float)look->phrasebits/look->frames, nuclear@1: (float)look->postbits/look->frames, nuclear@1: (float)(look->postbits+look->phrasebits)/look->frames);*/ nuclear@1: nuclear@1: memset(look,0,sizeof(*look)); nuclear@1: _ogg_free(look); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static int ilog(unsigned int v){ nuclear@1: int ret=0; nuclear@1: while(v){ nuclear@1: ret++; nuclear@1: v>>=1; nuclear@1: } nuclear@1: return(ret); nuclear@1: } nuclear@1: nuclear@1: static int ilog2(unsigned int v){ nuclear@1: int ret=0; nuclear@1: if(v)--v; nuclear@1: while(v){ nuclear@1: ret++; nuclear@1: v>>=1; nuclear@1: } nuclear@1: return(ret); nuclear@1: } nuclear@1: nuclear@1: static void floor1_pack (vorbis_info_floor *i,oggpack_buffer *opb){ nuclear@1: vorbis_info_floor1 *info=(vorbis_info_floor1 *)i; nuclear@1: int j,k; nuclear@1: int count=0; nuclear@1: int rangebits; nuclear@1: int maxposit=info->postlist[1]; nuclear@1: int maxclass=-1; nuclear@1: nuclear@1: /* save out partitions */ nuclear@1: oggpack_write(opb,info->partitions,5); /* only 0 to 31 legal */ nuclear@1: for(j=0;jpartitions;j++){ nuclear@1: oggpack_write(opb,info->partitionclass[j],4); /* only 0 to 15 legal */ nuclear@1: if(maxclasspartitionclass[j])maxclass=info->partitionclass[j]; nuclear@1: } nuclear@1: nuclear@1: /* save out partition classes */ nuclear@1: for(j=0;jclass_dim[j]-1,3); /* 1 to 8 */ nuclear@1: oggpack_write(opb,info->class_subs[j],2); /* 0 to 3 */ nuclear@1: if(info->class_subs[j])oggpack_write(opb,info->class_book[j],8); nuclear@1: for(k=0;k<(1<class_subs[j]);k++) nuclear@1: oggpack_write(opb,info->class_subbook[j][k]+1,8); nuclear@1: } nuclear@1: nuclear@1: /* save out the post list */ nuclear@1: oggpack_write(opb,info->mult-1,2); /* only 1,2,3,4 legal now */ nuclear@1: oggpack_write(opb,ilog2(maxposit),4); nuclear@1: rangebits=ilog2(maxposit); nuclear@1: nuclear@1: for(j=0,k=0;jpartitions;j++){ nuclear@1: count+=info->class_dim[info->partitionclass[j]]; nuclear@1: for(;kpostlist[k+2],rangebits); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static int icomp(const void *a,const void *b){ nuclear@1: return(**(int **)a-**(int **)b); nuclear@1: } nuclear@1: nuclear@1: static vorbis_info_floor *floor1_unpack (vorbis_info *vi,oggpack_buffer *opb){ nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: int j,k,count=0,maxclass=-1,rangebits; nuclear@1: nuclear@1: vorbis_info_floor1 *info=_ogg_calloc(1,sizeof(*info)); nuclear@1: /* read partitions */ nuclear@1: info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */ nuclear@1: for(j=0;jpartitions;j++){ nuclear@1: info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */ nuclear@1: if(info->partitionclass[j]<0)goto err_out; nuclear@1: if(maxclasspartitionclass[j])maxclass=info->partitionclass[j]; nuclear@1: } nuclear@1: nuclear@1: /* read partition classes */ nuclear@1: for(j=0;jclass_dim[j]=oggpack_read(opb,3)+1; /* 1 to 8 */ nuclear@1: info->class_subs[j]=oggpack_read(opb,2); /* 0,1,2,3 bits */ nuclear@1: if(info->class_subs[j]<0) nuclear@1: goto err_out; nuclear@1: if(info->class_subs[j])info->class_book[j]=oggpack_read(opb,8); nuclear@1: if(info->class_book[j]<0 || info->class_book[j]>=ci->books) nuclear@1: goto err_out; nuclear@1: for(k=0;k<(1<class_subs[j]);k++){ nuclear@1: info->class_subbook[j][k]=oggpack_read(opb,8)-1; nuclear@1: if(info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books) nuclear@1: goto err_out; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* read the post list */ nuclear@1: info->mult=oggpack_read(opb,2)+1; /* only 1,2,3,4 legal now */ nuclear@1: rangebits=oggpack_read(opb,4); nuclear@1: if(rangebits<0)goto err_out; nuclear@1: nuclear@1: for(j=0,k=0;jpartitions;j++){ nuclear@1: count+=info->class_dim[info->partitionclass[j]]; nuclear@1: if(count>VIF_POSIT) goto err_out; nuclear@1: for(;kpostlist[k+2]=oggpack_read(opb,rangebits); nuclear@1: if(t<0 || t>=(1<postlist[0]=0; nuclear@1: info->postlist[1]=1<postlist+j; nuclear@1: qsort(sortpointer,count+2,sizeof(*sortpointer),icomp); nuclear@1: nuclear@1: for(j=1;jvi=info; nuclear@1: look->n=info->postlist[1]; nuclear@1: nuclear@1: /* we drop each position value in-between already decoded values, nuclear@1: and use linear interpolation to predict each new value past the nuclear@1: edges. The positions are read in the order of the position nuclear@1: list... we precompute the bounding positions in the lookup. Of nuclear@1: course, the neighbors can change (if a position is declined), but nuclear@1: this is an initial mapping */ nuclear@1: nuclear@1: for(i=0;ipartitions;i++)n+=info->class_dim[info->partitionclass[i]]; nuclear@1: n+=2; nuclear@1: look->posts=n; nuclear@1: nuclear@1: /* also store a sorted position index */ nuclear@1: for(i=0;ipostlist+i; nuclear@1: qsort(sortpointer,n,sizeof(*sortpointer),icomp); nuclear@1: nuclear@1: /* points from sort order back to range number */ nuclear@1: for(i=0;iforward_index[i]=sortpointer[i]-info->postlist; nuclear@1: /* points from range order to sorted position */ nuclear@1: for(i=0;ireverse_index[look->forward_index[i]]=i; nuclear@1: /* we actually need the post values too */ nuclear@1: for(i=0;isorted_index[i]=info->postlist[look->forward_index[i]]; nuclear@1: nuclear@1: /* quantize values to multiplier spec */ nuclear@1: switch(info->mult){ nuclear@1: case 1: /* 1024 -> 256 */ nuclear@1: look->quant_q=256; nuclear@1: break; nuclear@1: case 2: /* 1024 -> 128 */ nuclear@1: look->quant_q=128; nuclear@1: break; nuclear@1: case 3: /* 1024 -> 86 */ nuclear@1: look->quant_q=86; nuclear@1: break; nuclear@1: case 4: /* 1024 -> 64 */ nuclear@1: look->quant_q=64; nuclear@1: break; nuclear@1: } nuclear@1: nuclear@1: /* discover our neighbors for decode where we don't use fit flags nuclear@1: (that would push the neighbors outward) */ nuclear@1: for(i=0;in; nuclear@1: int currentx=info->postlist[i+2]; nuclear@1: for(j=0;jpostlist[j]; nuclear@1: if(x>lx && xcurrentx){ nuclear@1: hi=j; nuclear@1: hx=x; nuclear@1: } nuclear@1: } nuclear@1: look->loneighbor[i]=lo; nuclear@1: look->hineighbor[i]=hi; nuclear@1: } nuclear@1: nuclear@1: return(look); nuclear@1: } nuclear@1: nuclear@1: static int render_point(int x0,int x1,int y0,int y1,int x){ nuclear@1: y0&=0x7fff; /* mask off flag */ nuclear@1: y1&=0x7fff; nuclear@1: nuclear@1: { nuclear@1: int dy=y1-y0; nuclear@1: int adx=x1-x0; nuclear@1: int ady=abs(dy); nuclear@1: int err=ady*(x-x0); nuclear@1: nuclear@1: int off=err/adx; nuclear@1: if(dy<0)return(y0-off); nuclear@1: return(y0+off); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static int vorbis_dBquant(const float *x){ nuclear@1: int i= *x*7.3142857f+1023.5f; nuclear@1: if(i>1023)return(1023); nuclear@1: if(i<0)return(0); nuclear@1: return i; nuclear@1: } nuclear@1: nuclear@1: static const 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: static void render_line(int n, int x0,int x1,int y0,int y1,float *d){ nuclear@1: int dy=y1-y0; nuclear@1: int adx=x1-x0; nuclear@1: int ady=abs(dy); nuclear@1: int base=dy/adx; nuclear@1: int sy=(dy<0?base-1:base+1); nuclear@1: int x=x0; nuclear@1: int y=y0; nuclear@1: int err=0; nuclear@1: nuclear@1: ady-=abs(base*adx); nuclear@1: nuclear@1: if(n>x1)n=x1; nuclear@1: nuclear@1: if(x=adx){ nuclear@1: err-=adx; nuclear@1: y+=sy; nuclear@1: }else{ nuclear@1: y+=base; nuclear@1: } nuclear@1: d[x]*=FLOOR1_fromdB_LOOKUP[y]; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void render_line0(int n, int x0,int x1,int y0,int y1,int *d){ nuclear@1: int dy=y1-y0; nuclear@1: int adx=x1-x0; nuclear@1: int ady=abs(dy); nuclear@1: int base=dy/adx; nuclear@1: int sy=(dy<0?base-1:base+1); nuclear@1: int x=x0; nuclear@1: int y=y0; nuclear@1: int err=0; nuclear@1: nuclear@1: ady-=abs(base*adx); nuclear@1: nuclear@1: if(n>x1)n=x1; nuclear@1: nuclear@1: if(x=adx){ nuclear@1: err-=adx; nuclear@1: y+=sy; nuclear@1: }else{ nuclear@1: y+=base; nuclear@1: } nuclear@1: d[x]=y; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* the floor has already been filtered to only include relevant sections */ nuclear@1: static int accumulate_fit(const float *flr,const float *mdct, nuclear@1: int x0, int x1,lsfit_acc *a, nuclear@1: int n,vorbis_info_floor1 *info){ nuclear@1: long i; nuclear@1: nuclear@1: int xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0; nuclear@1: nuclear@1: memset(a,0,sizeof(*a)); nuclear@1: a->x0=x0; nuclear@1: a->x1=x1; nuclear@1: if(x1>=n)x1=n-1; nuclear@1: nuclear@1: for(i=x0;i<=x1;i++){ nuclear@1: int quantized=vorbis_dBquant(flr+i); nuclear@1: if(quantized){ nuclear@1: if(mdct[i]+info->twofitatten>=flr[i]){ nuclear@1: xa += i; nuclear@1: ya += quantized; nuclear@1: x2a += i*i; nuclear@1: y2a += quantized*quantized; nuclear@1: xya += i*quantized; nuclear@1: na++; nuclear@1: }else{ nuclear@1: xb += i; nuclear@1: yb += quantized; nuclear@1: x2b += i*i; nuclear@1: y2b += quantized*quantized; nuclear@1: xyb += i*quantized; nuclear@1: nb++; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: a->xa=xa; nuclear@1: a->ya=ya; nuclear@1: a->x2a=x2a; nuclear@1: a->y2a=y2a; nuclear@1: a->xya=xya; nuclear@1: a->an=na; nuclear@1: nuclear@1: a->xb=xb; nuclear@1: a->yb=yb; nuclear@1: a->x2b=x2b; nuclear@1: a->y2b=y2b; nuclear@1: a->xyb=xyb; nuclear@1: a->bn=nb; nuclear@1: nuclear@1: return(na); nuclear@1: } nuclear@1: nuclear@1: static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1, nuclear@1: vorbis_info_floor1 *info){ nuclear@1: double xb=0,yb=0,x2b=0,y2b=0,xyb=0,bn=0; nuclear@1: int i; nuclear@1: int x0=a[0].x0; nuclear@1: int x1=a[fits-1].x1; nuclear@1: nuclear@1: for(i=0;itwofitweight/(a[i].an+1)+1.; nuclear@1: nuclear@1: xb+=a[i].xb + a[i].xa * weight; nuclear@1: yb+=a[i].yb + a[i].ya * weight; nuclear@1: x2b+=a[i].x2b + a[i].x2a * weight; nuclear@1: y2b+=a[i].y2b + a[i].y2a * weight; nuclear@1: xyb+=a[i].xyb + a[i].xya * weight; nuclear@1: bn+=a[i].bn + a[i].an * weight; nuclear@1: } nuclear@1: nuclear@1: if(*y0>=0){ nuclear@1: xb+= x0; nuclear@1: yb+= *y0; nuclear@1: x2b+= x0 * x0; nuclear@1: y2b+= *y0 * *y0; nuclear@1: xyb+= *y0 * x0; nuclear@1: bn++; nuclear@1: } nuclear@1: nuclear@1: if(*y1>=0){ nuclear@1: xb+= x1; nuclear@1: yb+= *y1; nuclear@1: x2b+= x1 * x1; nuclear@1: y2b+= *y1 * *y1; nuclear@1: xyb+= *y1 * x1; nuclear@1: bn++; nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: double denom=(bn*x2b-xb*xb); nuclear@1: nuclear@1: if(denom>0.){ nuclear@1: double a=(yb*x2b-xyb*xb)/denom; nuclear@1: double b=(bn*xyb-xb*yb)/denom; nuclear@1: *y0=rint(a+b*x0); nuclear@1: *y1=rint(a+b*x1); nuclear@1: nuclear@1: /* limit to our range! */ nuclear@1: if(*y0>1023)*y0=1023; nuclear@1: if(*y1>1023)*y1=1023; nuclear@1: if(*y0<0)*y0=0; nuclear@1: if(*y1<0)*y1=0; nuclear@1: nuclear@1: return 0; nuclear@1: }else{ nuclear@1: *y0=0; nuclear@1: *y1=0; nuclear@1: return 1; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static int inspect_error(int x0,int x1,int y0,int y1,const float *mask, nuclear@1: const float *mdct, nuclear@1: vorbis_info_floor1 *info){ nuclear@1: int dy=y1-y0; nuclear@1: int adx=x1-x0; nuclear@1: int ady=abs(dy); nuclear@1: int base=dy/adx; nuclear@1: int sy=(dy<0?base-1:base+1); nuclear@1: int x=x0; nuclear@1: int y=y0; nuclear@1: int err=0; nuclear@1: int val=vorbis_dBquant(mask+x); nuclear@1: int mse=0; nuclear@1: int n=0; nuclear@1: nuclear@1: ady-=abs(base*adx); nuclear@1: nuclear@1: mse=(y-val); nuclear@1: mse*=mse; nuclear@1: n++; nuclear@1: if(mdct[x]+info->twofitatten>=mask[x]){ nuclear@1: if(y+info->maxovermaxunder>val)return(1); nuclear@1: } nuclear@1: nuclear@1: while(++x=adx){ nuclear@1: err-=adx; nuclear@1: y+=sy; nuclear@1: }else{ nuclear@1: y+=base; nuclear@1: } nuclear@1: nuclear@1: val=vorbis_dBquant(mask+x); nuclear@1: mse+=((y-val)*(y-val)); nuclear@1: n++; nuclear@1: if(mdct[x]+info->twofitatten>=mask[x]){ nuclear@1: if(val){ nuclear@1: if(y+info->maxovermaxunder>val)return(1); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: if(info->maxover*info->maxover/n>info->maxerr)return(0); nuclear@1: if(info->maxunder*info->maxunder/n>info->maxerr)return(0); nuclear@1: if(mse/n>info->maxerr)return(1); nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: static int post_Y(int *A,int *B,int pos){ nuclear@1: if(A[pos]<0) nuclear@1: return B[pos]; nuclear@1: if(B[pos]<0) nuclear@1: return A[pos]; nuclear@1: nuclear@1: return (A[pos]+B[pos])>>1; nuclear@1: } nuclear@1: nuclear@1: int *floor1_fit(vorbis_block *vb,vorbis_look_floor1 *look, nuclear@1: const float *logmdct, /* in */ nuclear@1: const float *logmask){ nuclear@1: long i,j; nuclear@1: vorbis_info_floor1 *info=look->vi; nuclear@1: long n=look->n; nuclear@1: long posts=look->posts; nuclear@1: long nonzero=0; nuclear@1: lsfit_acc fits[VIF_POSIT+1]; nuclear@1: int fit_valueA[VIF_POSIT+2]; /* index by range list position */ nuclear@1: int fit_valueB[VIF_POSIT+2]; /* index by range list position */ nuclear@1: nuclear@1: int loneighbor[VIF_POSIT+2]; /* sorted index of range list position (+2) */ nuclear@1: int hineighbor[VIF_POSIT+2]; nuclear@1: int *output=NULL; nuclear@1: int memo[VIF_POSIT+2]; nuclear@1: nuclear@1: for(i=0;isorted_index[i], nuclear@1: look->sorted_index[i+1],fits+i, nuclear@1: n,info); nuclear@1: } nuclear@1: nuclear@1: if(nonzero){ nuclear@1: /* start by fitting the implicit base case.... */ nuclear@1: int y0=-200; nuclear@1: int y1=-200; nuclear@1: fit_line(fits,posts-1,&y0,&y1,info); nuclear@1: nuclear@1: fit_valueA[0]=y0; nuclear@1: fit_valueB[0]=y0; nuclear@1: fit_valueB[1]=y1; nuclear@1: fit_valueA[1]=y1; nuclear@1: nuclear@1: /* Non degenerate case */ nuclear@1: /* start progressive splitting. This is a greedy, non-optimal nuclear@1: algorithm, but simple and close enough to the best nuclear@1: answer. */ nuclear@1: for(i=2;ireverse_index[i]; nuclear@1: int ln=loneighbor[sortpos]; nuclear@1: int hn=hineighbor[sortpos]; nuclear@1: nuclear@1: /* eliminate repeat searches of a particular range with a memo */ nuclear@1: if(memo[ln]!=hn){ nuclear@1: /* haven't performed this error search yet */ nuclear@1: int lsortpos=look->reverse_index[ln]; nuclear@1: int hsortpos=look->reverse_index[hn]; nuclear@1: memo[ln]=hn; nuclear@1: nuclear@1: { nuclear@1: /* A note: we want to bound/minimize *local*, not global, error */ nuclear@1: int lx=info->postlist[ln]; nuclear@1: int hx=info->postlist[hn]; nuclear@1: int ly=post_Y(fit_valueA,fit_valueB,ln); nuclear@1: int hy=post_Y(fit_valueA,fit_valueB,hn); nuclear@1: nuclear@1: if(ly==-1 || hy==-1){ nuclear@1: exit(1); nuclear@1: } nuclear@1: nuclear@1: if(inspect_error(lx,hx,ly,hy,logmask,logmdct,info)){ nuclear@1: /* outside error bounds/begin search area. Split it. */ nuclear@1: int ly0=-200; nuclear@1: int ly1=-200; nuclear@1: int hy0=-200; nuclear@1: int hy1=-200; nuclear@1: int ret0=fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1,info); nuclear@1: int ret1=fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1,info); nuclear@1: nuclear@1: if(ret0){ nuclear@1: ly0=ly; nuclear@1: ly1=hy0; nuclear@1: } nuclear@1: if(ret1){ nuclear@1: hy0=ly1; nuclear@1: hy1=hy; nuclear@1: } nuclear@1: nuclear@1: if(ret0 && ret1){ nuclear@1: fit_valueA[i]=-200; nuclear@1: fit_valueB[i]=-200; nuclear@1: }else{ nuclear@1: /* store new edge values */ nuclear@1: fit_valueB[ln]=ly0; nuclear@1: if(ln==0)fit_valueA[ln]=ly0; nuclear@1: fit_valueA[i]=ly1; nuclear@1: fit_valueB[i]=hy0; nuclear@1: fit_valueA[hn]=hy1; nuclear@1: if(hn==1)fit_valueB[hn]=hy1; nuclear@1: nuclear@1: if(ly1>=0 || hy0>=0){ nuclear@1: /* store new neighbor values */ nuclear@1: for(j=sortpos-1;j>=0;j--) nuclear@1: if(hineighbor[j]==hn) nuclear@1: hineighbor[j]=i; nuclear@1: else nuclear@1: break; nuclear@1: for(j=sortpos+1;jloneighbor[i-2]; nuclear@1: int hn=look->hineighbor[i-2]; nuclear@1: int x0=info->postlist[ln]; nuclear@1: int x1=info->postlist[hn]; nuclear@1: int y0=output[ln]; nuclear@1: int y1=output[hn]; nuclear@1: nuclear@1: int predicted=render_point(x0,x1,y0,y1,info->postlist[i]); nuclear@1: int vx=post_Y(fit_valueA,fit_valueB,i); nuclear@1: nuclear@1: if(vx>=0 && predicted!=vx){ nuclear@1: output[i]=vx; nuclear@1: }else{ nuclear@1: output[i]= predicted|0x8000; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: return(output); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: int *floor1_interpolate_fit(vorbis_block *vb,vorbis_look_floor1 *look, nuclear@1: int *A,int *B, nuclear@1: int del){ nuclear@1: nuclear@1: long i; nuclear@1: long posts=look->posts; nuclear@1: int *output=NULL; nuclear@1: nuclear@1: if(A && B){ nuclear@1: output=_vorbis_block_alloc(vb,sizeof(*output)*posts); nuclear@1: nuclear@1: /* overly simpleminded--- look again post 1.2 */ nuclear@1: for(i=0;i>16; nuclear@1: if(A[i]&0x8000 && B[i]&0x8000)output[i]|=0x8000; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: return(output); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: int floor1_encode(oggpack_buffer *opb,vorbis_block *vb, nuclear@1: vorbis_look_floor1 *look, nuclear@1: int *post,int *ilogmask){ nuclear@1: nuclear@1: long i,j; nuclear@1: vorbis_info_floor1 *info=look->vi; nuclear@1: long posts=look->posts; nuclear@1: codec_setup_info *ci=vb->vd->vi->codec_setup; nuclear@1: int out[VIF_POSIT+2]; nuclear@1: static_codebook **sbooks=ci->book_param; nuclear@1: codebook *books=ci->fullbooks; nuclear@1: nuclear@1: /* quantize values to multiplier spec */ nuclear@1: if(post){ nuclear@1: for(i=0;imult){ nuclear@1: case 1: /* 1024 -> 256 */ nuclear@1: val>>=2; nuclear@1: break; nuclear@1: case 2: /* 1024 -> 128 */ nuclear@1: val>>=3; nuclear@1: break; nuclear@1: case 3: /* 1024 -> 86 */ nuclear@1: val/=12; nuclear@1: break; nuclear@1: case 4: /* 1024 -> 64 */ nuclear@1: val>>=4; nuclear@1: break; nuclear@1: } nuclear@1: post[i]=val | (post[i]&0x8000); nuclear@1: } nuclear@1: nuclear@1: out[0]=post[0]; nuclear@1: out[1]=post[1]; nuclear@1: nuclear@1: /* find prediction values for each post and subtract them */ nuclear@1: for(i=2;iloneighbor[i-2]; nuclear@1: int hn=look->hineighbor[i-2]; nuclear@1: int x0=info->postlist[ln]; nuclear@1: int x1=info->postlist[hn]; nuclear@1: int y0=post[ln]; nuclear@1: int y1=post[hn]; nuclear@1: nuclear@1: int predicted=render_point(x0,x1,y0,y1,info->postlist[i]); nuclear@1: nuclear@1: if((post[i]&0x8000) || (predicted==post[i])){ nuclear@1: post[i]=predicted|0x8000; /* in case there was roundoff jitter nuclear@1: in interpolation */ nuclear@1: out[i]=0; nuclear@1: }else{ nuclear@1: int headroom=(look->quant_q-predictedquant_q-predicted:predicted); nuclear@1: nuclear@1: int val=post[i]-predicted; nuclear@1: nuclear@1: /* at this point the 'deviation' value is in the range +/- max nuclear@1: range, but the real, unique range can always be mapped to nuclear@1: only [0-maxrange). So we want to wrap the deviation into nuclear@1: this limited range, but do it in the way that least screws nuclear@1: an essentially gaussian probability distribution. */ nuclear@1: nuclear@1: if(val<0) nuclear@1: if(val<-headroom) nuclear@1: val=headroom-val-1; nuclear@1: else nuclear@1: val=-1-(val<<1); nuclear@1: else nuclear@1: if(val>=headroom) nuclear@1: val= val+headroom; nuclear@1: else nuclear@1: val<<=1; nuclear@1: nuclear@1: out[i]=val; nuclear@1: post[ln]&=0x7fff; nuclear@1: post[hn]&=0x7fff; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* we have everything we need. pack it out */ nuclear@1: /* mark nontrivial floor */ nuclear@1: oggpack_write(opb,1,1); nuclear@1: nuclear@1: /* beginning/end post */ nuclear@1: look->frames++; nuclear@1: look->postbits+=ilog(look->quant_q-1)*2; nuclear@1: oggpack_write(opb,out[0],ilog(look->quant_q-1)); nuclear@1: oggpack_write(opb,out[1],ilog(look->quant_q-1)); nuclear@1: nuclear@1: nuclear@1: /* partition by partition */ nuclear@1: for(i=0,j=2;ipartitions;i++){ nuclear@1: int class=info->partitionclass[i]; nuclear@1: int cdim=info->class_dim[class]; nuclear@1: int csubbits=info->class_subs[class]; nuclear@1: int csub=1<class_subbook[class][k]; nuclear@1: if(booknum<0){ nuclear@1: maxval[k]=1; nuclear@1: }else{ nuclear@1: maxval[k]=sbooks[info->class_subbook[class][k]]->entries; nuclear@1: } nuclear@1: } nuclear@1: for(k=0;kphrasebits+= nuclear@1: vorbis_book_encode(books+info->class_book[class],cval,opb); nuclear@1: nuclear@1: #ifdef TRAIN_FLOOR1 nuclear@1: { nuclear@1: FILE *of; nuclear@1: char buffer[80]; nuclear@1: sprintf(buffer,"line_%dx%ld_class%d.vqd", nuclear@1: vb->pcmend/2,posts-2,class); nuclear@1: of=fopen(buffer,"a"); nuclear@1: fprintf(of,"%d\n",cval); nuclear@1: fclose(of); nuclear@1: } nuclear@1: #endif nuclear@1: } nuclear@1: nuclear@1: /* write post values */ nuclear@1: for(k=0;kclass_subbook[class][bookas[k]]; nuclear@1: if(book>=0){ nuclear@1: /* hack to allow training with 'bad' books */ nuclear@1: if(out[j+k]<(books+book)->entries) nuclear@1: look->postbits+=vorbis_book_encode(books+book, nuclear@1: out[j+k],opb); nuclear@1: /*else nuclear@1: fprintf(stderr,"+!");*/ nuclear@1: nuclear@1: #ifdef TRAIN_FLOOR1 nuclear@1: { nuclear@1: FILE *of; nuclear@1: char buffer[80]; nuclear@1: sprintf(buffer,"line_%dx%ld_%dsub%d.vqd", nuclear@1: vb->pcmend/2,posts-2,class,bookas[k]); nuclear@1: of=fopen(buffer,"a"); nuclear@1: fprintf(of,"%d\n",out[j+k]); nuclear@1: fclose(of); nuclear@1: } nuclear@1: #endif nuclear@1: } nuclear@1: } nuclear@1: j+=cdim; nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* generate quantized floor equivalent to what we'd unpack in decode */ nuclear@1: /* render the lines */ nuclear@1: int hx=0; nuclear@1: int lx=0; nuclear@1: int ly=post[0]*info->mult; nuclear@1: int n=ci->blocksizes[vb->W]/2; nuclear@1: nuclear@1: for(j=1;jposts;j++){ nuclear@1: int current=look->forward_index[j]; nuclear@1: int hy=post[current]&0x7fff; nuclear@1: if(hy==post[current]){ nuclear@1: nuclear@1: hy*=info->mult; nuclear@1: hx=info->postlist[current]; nuclear@1: nuclear@1: render_line0(n,lx,hx,ly,hy,ilogmask); nuclear@1: nuclear@1: lx=hx; nuclear@1: ly=hy; nuclear@1: } nuclear@1: } nuclear@1: for(j=hx;jpcmend/2;j++)ilogmask[j]=ly; /* be certain */ nuclear@1: return(1); nuclear@1: } nuclear@1: }else{ nuclear@1: oggpack_write(opb,0,1); nuclear@1: memset(ilogmask,0,vb->pcmend/2*sizeof(*ilogmask)); nuclear@1: return(0); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void *floor1_inverse1(vorbis_block *vb,vorbis_look_floor *in){ nuclear@1: vorbis_look_floor1 *look=(vorbis_look_floor1 *)in; nuclear@1: vorbis_info_floor1 *info=look->vi; nuclear@1: codec_setup_info *ci=vb->vd->vi->codec_setup; nuclear@1: nuclear@1: int i,j,k; nuclear@1: codebook *books=ci->fullbooks; nuclear@1: nuclear@1: /* unpack wrapped/predicted values from stream */ nuclear@1: if(oggpack_read(&vb->opb,1)==1){ nuclear@1: int *fit_value=_vorbis_block_alloc(vb,(look->posts)*sizeof(*fit_value)); nuclear@1: nuclear@1: fit_value[0]=oggpack_read(&vb->opb,ilog(look->quant_q-1)); nuclear@1: fit_value[1]=oggpack_read(&vb->opb,ilog(look->quant_q-1)); nuclear@1: nuclear@1: /* partition by partition */ nuclear@1: for(i=0,j=2;ipartitions;i++){ nuclear@1: int class=info->partitionclass[i]; nuclear@1: int cdim=info->class_dim[class]; nuclear@1: int csubbits=info->class_subs[class]; nuclear@1: int csub=1<class_book[class],&vb->opb); nuclear@1: nuclear@1: if(cval==-1)goto eop; nuclear@1: } nuclear@1: nuclear@1: for(k=0;kclass_subbook[class][cval&(csub-1)]; nuclear@1: cval>>=csubbits; nuclear@1: if(book>=0){ nuclear@1: if((fit_value[j+k]=vorbis_book_decode(books+book,&vb->opb))==-1) nuclear@1: goto eop; nuclear@1: }else{ nuclear@1: fit_value[j+k]=0; nuclear@1: } nuclear@1: } nuclear@1: j+=cdim; nuclear@1: } nuclear@1: nuclear@1: /* unwrap positive values and reconsitute via linear interpolation */ nuclear@1: for(i=2;iposts;i++){ nuclear@1: int predicted=render_point(info->postlist[look->loneighbor[i-2]], nuclear@1: info->postlist[look->hineighbor[i-2]], nuclear@1: fit_value[look->loneighbor[i-2]], nuclear@1: fit_value[look->hineighbor[i-2]], nuclear@1: info->postlist[i]); nuclear@1: int hiroom=look->quant_q-predicted; nuclear@1: int loroom=predicted; nuclear@1: int room=(hiroom=room){ nuclear@1: if(hiroom>loroom){ nuclear@1: val = val-loroom; nuclear@1: }else{ nuclear@1: val = -1-(val-hiroom); nuclear@1: } nuclear@1: }else{ nuclear@1: if(val&1){ nuclear@1: val= -((val+1)>>1); nuclear@1: }else{ nuclear@1: val>>=1; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: fit_value[i]=(val+predicted)&0x7fff; nuclear@1: fit_value[look->loneighbor[i-2]]&=0x7fff; nuclear@1: fit_value[look->hineighbor[i-2]]&=0x7fff; nuclear@1: nuclear@1: }else{ nuclear@1: fit_value[i]=predicted|0x8000; nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: return(fit_value); nuclear@1: } nuclear@1: eop: nuclear@1: return(NULL); nuclear@1: } nuclear@1: nuclear@1: static int floor1_inverse2(vorbis_block *vb,vorbis_look_floor *in,void *memo, nuclear@1: float *out){ nuclear@1: vorbis_look_floor1 *look=(vorbis_look_floor1 *)in; nuclear@1: vorbis_info_floor1 *info=look->vi; nuclear@1: nuclear@1: codec_setup_info *ci=vb->vd->vi->codec_setup; nuclear@1: int n=ci->blocksizes[vb->W]/2; nuclear@1: int j; nuclear@1: nuclear@1: if(memo){ nuclear@1: /* render the lines */ nuclear@1: int *fit_value=(int *)memo; nuclear@1: int hx=0; nuclear@1: int lx=0; nuclear@1: int ly=fit_value[0]*info->mult; nuclear@1: /* guard lookup against out-of-range values */ nuclear@1: ly=(ly<0?0:ly>255?255:ly); nuclear@1: nuclear@1: for(j=1;jposts;j++){ nuclear@1: int current=look->forward_index[j]; nuclear@1: int hy=fit_value[current]&0x7fff; nuclear@1: if(hy==fit_value[current]){ nuclear@1: nuclear@1: hx=info->postlist[current]; nuclear@1: hy*=info->mult; nuclear@1: /* guard lookup against out-of-range values */ nuclear@1: hy=(hy<0?0:hy>255?255:hy); nuclear@1: nuclear@1: render_line(n,lx,hx,ly,hy,out); nuclear@1: nuclear@1: lx=hx; nuclear@1: ly=hy; nuclear@1: } nuclear@1: } nuclear@1: for(j=hx;j