* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
- * by the XIPHOPHORUS Company http://www.xiph.org/ *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
+ * by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: floor backend 1 implementation
- last mod: $Id: floor1.c,v 1.23 2002/07/11 06:40:48 xiphmont Exp $
+ last mod: $Id$
********************************************************************/
#define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */
-typedef struct {
- int sorted_index[VIF_POSIT+2];
- int forward_index[VIF_POSIT+2];
- int reverse_index[VIF_POSIT+2];
-
- int hineighbor[VIF_POSIT];
- int loneighbor[VIF_POSIT];
- int posts;
-
- int n;
- int quant_q;
- vorbis_info_floor1 *vi;
-
- long phrasebits;
- long postbits;
- long frames;
-} vorbis_look_floor1;
-
typedef struct lsfit_acc{
long x0;
long x1;
long x2a;
long y2a;
long xya;
- long n;
long an;
} lsfit_acc;
static int ilog2(unsigned int v){
int ret=0;
- while(v>1){
+ if(v)--v;
+ while(v){
ret++;
v>>=1;
}
info->postlist[1]=1<<rangebits;
return(info);
-
+
err_out:
floor1_free_info(info);
return(NULL);
for(i=0;i<n;i++)look->reverse_index[look->forward_index[i]]=i;
/* we actually need the post values too */
for(i=0;i<n;i++)look->sorted_index[i]=info->postlist[look->forward_index[i]];
-
+
/* quantize values to multiplier spec */
switch(info->mult){
case 1: /* 1024 -> 256 */
static int render_point(int x0,int x1,int y0,int y1,int x){
y0&=0x7fff; /* mask off flag */
y1&=0x7fff;
-
+
{
int dy=y1-y0;
int adx=x1-x0;
int ady=abs(dy);
int err=ady*(x-x0);
-
+
int off=err/adx;
if(dy<0)return(y0-off);
return(y0+off);
0.82788260F, 0.88168307F, 0.9389798F, 1.F,
};
-static void render_line(int x0,int x1,int y0,int y1,float *d){
+static void render_line(int n, int x0,int x1,int y0,int y1,float *d){
int dy=y1-y0;
int adx=x1-x0;
int ady=abs(dy);
ady-=abs(base*adx);
- d[x]*=FLOOR1_fromdB_LOOKUP[y];
- while(++x<x1){
+ if(n>x1)n=x1;
+
+ if(x<n)
+ d[x]*=FLOOR1_fromdB_LOOKUP[y];
+
+ while(++x<n){
err=err+ady;
if(err>=adx){
err-=adx;
int x0, int x1,lsfit_acc *a,
int n,vorbis_info_floor1 *info){
long i;
- int quantized=vorbis_dBquant(flr+x0);
long xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0;
a->y2a=y2a*weight+y2b;
a->xya=xya*weight+xyb;
a->an=na*weight+nb;
- a->n=nb;
}
return(na);
}
static void fit_line(lsfit_acc *a,int fits,int *y0,int *y1){
- long x=0,y=0,x2=0,y2=0,xy=0,n=0,an=0,i;
+ long x=0,y=0,x2=0,y2=0,xy=0,an=0,i;
long x0=a[0].x0;
long x1=a[fits-1].x1;
x2+=a[i].x2a;
y2+=a[i].y2a;
xy+=a[i].xya;
- n+=a[i].n;
an+=a[i].an;
}
x2+= x0 * x0;
y2+= *y0 * *y0;
xy+= *y0 * x0;
- n++;
an++;
}
x2+= x1 * x1;
y2+= *y1 * *y1;
xy+= *y1 * x1;
- n++;
an++;
}
-
- {
+
+ if(an){
/* need 64 bit multiplies, which C doesn't give portably as int */
double fx=x;
double fy=y;
if(*y0<0)*y0=0;
if(*y1<0)*y1=0;
+ }else{
+ *y0=0;
+ *y1=0;
}
}
for(i=0;i<fits && y==0;i++)
y+=a[i].ya;
-
+
*y0=*y1=y;
}*/
int n=0;
ady-=abs(base*adx);
-
+
mse=(y-val);
mse*=mse;
n++;
}
}
}
-
+
if(info->maxover*info->maxover/n>info->maxerr)return(0);
if(info->maxunder*info->maxunder/n>info->maxerr)return(0);
if(mse/n>info->maxerr)return(1);
return (A[pos]+B[pos])>>1;
}
-static int seq=0;
-
int *floor1_fit(vorbis_block *vb,vorbis_look_floor1 *look,
const float *logmdct, /* in */
const float *logmask){
int sortpos=look->reverse_index[i];
int ln=loneighbor[sortpos];
int hn=hineighbor[sortpos];
-
+
/* eliminate repeat searches of a particular range with a memo */
if(memo[ln]!=hn){
/* haven't performed this error search yet */
int hx=info->postlist[hn];
int ly=post_Y(fit_valueA,fit_valueB,ln);
int hy=post_Y(fit_valueA,fit_valueB,hn);
-
+
+ if(ly==-1 || hy==-1){
+ exit(1);
+ }
+
if(inspect_error(lx,hx,ly,hy,logmask,logmdct,info)){
/* outside error bounds/begin search area. Split it. */
int ly0=-200;
int hy1=-200;
fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1);
fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1);
-
+
/* store new edge values */
fit_valueB[ln]=ly0;
if(ln==0)fit_valueA[ln]=ly0;
fit_valueB[i]=hy0;
fit_valueA[hn]=hy1;
if(hn==1)fit_valueB[hn]=hy1;
-
+
if(ly1>=0 || hy0>=0){
/* store new neighbor values */
for(j=sortpos-1;j>=0;j--)
loneighbor[j]=i;
else
break;
-
+
}
}else{
-
+
fit_valueA[i]=-200;
fit_valueB[i]=-200;
}
}
}
}
-
+
output=_vorbis_block_alloc(vb,sizeof(*output)*posts);
-
+
output[0]=post_Y(fit_valueA,fit_valueB,0);
output[1]=post_Y(fit_valueA,fit_valueB,1);
-
+
/* fill in posts marked as not using a fit; we will zero
back out to 'unused' when encoding them so long as curve
interpolation doesn't force them into use */
int x1=info->postlist[hn];
int y0=output[ln];
int y1=output[hn];
-
+
int predicted=render_point(x0,x1,y0,y1,info->postlist[i]);
int vx=post_Y(fit_valueA,fit_valueB,i);
-
+
if(vx>=0 && predicted!=vx){
output[i]=vx;
}else{
}
return(output);
-
+
}
int *floor1_interpolate_fit(vorbis_block *vb,vorbis_look_floor1 *look,
long i;
long posts=look->posts;
int *output=NULL;
-
+
if(A && B){
output=_vorbis_block_alloc(vb,sizeof(*output)*posts);
-
+
for(i=0;i<posts;i++){
output[i]=((65536-del)*(A[i]&0x7fff)+del*(B[i]&0x7fff)+32768)>>16;
if(A[i]&0x8000 && B[i]&0x8000)output[i]|=0x8000;
}
-int floor1_encode(vorbis_block *vb,vorbis_look_floor1 *look,
+int floor1_encode(oggpack_buffer *opb,vorbis_block *vb,
+ vorbis_look_floor1 *look,
int *post,int *ilogmask){
long i,j;
vorbis_info_floor1 *info=look->vi;
- long n=look->n;
long posts=look->posts;
codec_setup_info *ci=vb->vd->vi->codec_setup;
int out[VIF_POSIT+2];
static_codebook **sbooks=ci->book_param;
codebook *books=ci->fullbooks;
- static long seq=0;
/* quantize values to multiplier spec */
if(post){
int x1=info->postlist[hn];
int y0=post[ln];
int y1=post[hn];
-
+
int predicted=render_point(x0,x1,y0,y1,info->postlist[i]);
-
+
if((post[i]&0x8000) || (predicted==post[i])){
post[i]=predicted|0x8000; /* in case there was roundoff jitter
in interpolation */
post[hn]&=0x7fff;
}
}
-
+
/* we have everything we need. pack it out */
/* mark nontrivial floor */
- oggpack_write(&vb->opb,1,1);
-
+ oggpack_write(opb,1,1);
+
/* beginning/end post */
look->frames++;
look->postbits+=ilog(look->quant_q-1)*2;
- oggpack_write(&vb->opb,out[0],ilog(look->quant_q-1));
- oggpack_write(&vb->opb,out[1],ilog(look->quant_q-1));
-
-
+ oggpack_write(opb,out[0],ilog(look->quant_q-1));
+ oggpack_write(opb,out[1],ilog(look->quant_q-1));
+
+
/* partition by partition */
for(i=0,j=2;i<info->partitions;i++){
int class=info->partitionclass[i];
}
/* write it */
look->phrasebits+=
- vorbis_book_encode(books+info->class_book[class],cval,&vb->opb);
+ vorbis_book_encode(books+info->class_book[class],cval,opb);
#ifdef TRAIN_FLOOR1
{
/* hack to allow training with 'bad' books */
if(out[j+k]<(books+book)->entries)
look->postbits+=vorbis_book_encode(books+book,
- out[j+k],&vb->opb);
+ out[j+k],opb);
/*else
fprintf(stderr,"+!");*/
-
+
#ifdef TRAIN_FLOOR1
{
FILE *of;
}
j+=cdim;
}
-
+
{
/* generate quantized floor equivalent to what we'd unpack in decode */
/* render the lines */
int current=look->forward_index[j];
int hy=post[current]&0x7fff;
if(hy==post[current]){
-
+
hy*=info->mult;
hx=info->postlist[current];
ly=hy;
}
}
- for(j=hx;j<vb->pcmend/2;j++)ilogmask[j]=ly; /* be certain */
- seq++;
+ for(j=hx;j<vb->pcmend/2;j++)ilogmask[j]=ly; /* be certain */
return(1);
}
}else{
- oggpack_write(&vb->opb,0,1);
+ oggpack_write(opb,0,1);
memset(ilogmask,0,vb->pcmend/2*sizeof(*ilogmask));
- seq++;
return(0);
}
}
vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
vorbis_info_floor1 *info=look->vi;
codec_setup_info *ci=vb->vd->vi->codec_setup;
-
+
int i,j,k;
- codebook *books=ci->fullbooks;
+ codebook *books=ci->fullbooks;
/* unpack wrapped/predicted values from stream */
if(oggpack_read(&vb->opb,1)==1){
hy*=info->mult;
hx=info->postlist[current];
- render_line(lx,hx,ly,hy,out);
+ render_line(n,lx,hx,ly,hy,out);
lx=hx;
ly=hy;
}
}
- for(j=hx;j<n;j++)out[j]*=FLOOR1_fromdB_LOOKUP[ly]; /* be certain */
+ for(j=hx;j<n;j++)out[j]*=FLOOR1_fromdB_LOOKUP[ly]; /* be certain */
return(1);
}
memset(out,0,sizeof(*out)*n);
}
/* export hooks */
-vorbis_func_floor floor1_exportbundle={
+const vorbis_func_floor floor1_exportbundle={
&floor1_pack,&floor1_unpack,&floor1_look,&floor1_free_info,
&floor1_free_look,&floor1_inverse1,&floor1_inverse2
};
-
-