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