* *
********************************************************************
- function: residue backend 0 and 1 implementation
- last mod: $Id: res0.c,v 1.29 2001/06/04 05:56:42 xiphmont Exp $
+ function: residue backend 0, 1 and 2 implementation
+ last mod: $Id: res0.c,v 1.35 2001/08/13 11:33:39 xiphmont Exp $
********************************************************************/
int partvals;
int **decodemap;
+ long postbits;
+ long phrasebits;
+ long frames;
+
} vorbis_look_residue0;
vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
void res0_free_look(vorbis_look_residue *i){
int j;
if(i){
+
vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
- long resbitsT=0;
- long resvalsT=0;
+
+ /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
+ (float)look->phrasebits/look->frames,
+ (float)look->postbits/look->frames,
+ (float)(look->postbits+look->phrasebits)/look->frames);*/
+
+ /*vorbis_info_residue0 *info=look->info;
+
+ fprintf(stderr,
+ "%ld frames encoded in %ld phrasebits and %ld residue bits "
+ "(%g/frame) \n",look->frames,look->phrasebits,
+ look->resbitsflat,
+ (look->phrasebits+look->resbitsflat)/(float)look->frames);
+
+ for(j=0;j<look->parts;j++){
+ long acc=0;
+ fprintf(stderr,"\t[%d] == ",j);
+ for(k=0;k<look->stages;k++)
+ if((info->secondstages[j]>>k)&1){
+ fprintf(stderr,"%ld,",look->resbits[j][k]);
+ acc+=look->resbits[j][k];
+ }
+
+ fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
+ acc?(float)acc/(look->resvals[j]*info->grouping):0);
+ }
+ fprintf(stderr,"\n");*/
for(j=0;j<look->parts;j++)
if(look->partbooks[j])_ogg_free(look->partbooks[j]);
info->groupbook=oggpack_read(opb,8);
for(j=0;j<info->partitions;j++){
- int cascade=info->secondstages[j]=oggpack_read(opb,3);
+ int cascade=oggpack_read(opb,3);
if(oggpack_read(opb,1))
cascade|=(oggpack_read(opb,5)<<3);
+ info->secondstages[j]=cascade;
+
acc+=icount(cascade);
}
for(j=0;j<acc;j++)
static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
int auxparts,int auxpartnum){
vorbis_info_residue0 *info=look->info;
- int i,j=0;
- float max,localmax=0.f;
+ int i;
+ float max=0.f;
float temp[128];
- float entropy[8];
+ float entropy=0.f;
/* setup */
for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
- /* handle case subgrp==1 outside */
for(i=0;i<n;i++)
- if(temp[i]>localmax)localmax=temp[i];
- max=localmax;
+ if(temp[i]>max)max=temp[i];
for(i=0;i<n;i++)temp[i]=rint(temp[i]);
-
- while(n){
- entropy[j]=localmax;
- n>>=1;
- j++;
- if(!n)break;
- for(i=0;i<n;i++){
- temp[i]=temp[i*2]+temp[i*2+1];
- }
- localmax=0.f;
- for(i=0;i<n;i++)
- if(temp[i]>localmax)localmax=temp[i];
- }
+
+ for(i=0;i<n;i++)
+ entropy+=temp[i];
for(i=0;i<auxparts-1;i++)
if(auxpartnum<info->blimit[i] &&
- entropy[info->subgrp[i]]<=info->entmax[i] &&
+ entropy<=info->entmax[i] &&
max<=info->ampmax[i])
break;
return(bits);
}
-static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
- float **in,int ch,
- int (*classify)(float *,int,vorbis_look_residue0 *,
- int,int),
- int (*encode)(oggpack_buffer *,float *,int,
- codebook *,vorbis_look_residue0 *)){
- long i,j,k,l,s;
+static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int ch,
+ int (*classify)(float *,int,vorbis_look_residue0 *,
+ int,int)){
+ long i,j;
vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
vorbis_info_residue0 *info=look->info;
/* move all this setup out later */
int samples_per_partition=info->grouping;
int possible_partitions=info->partitions;
- int partitions_per_word=look->phrasebook->dim;
int n=info->end-info->begin;
int partvals=n/samples_per_partition;
- int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
- partvals=partwords*partitions_per_word;
-
- /* we find the patition type for each partition of each
+ /* we find the partition type for each partition of each
channel. We'll go back and do the interleaved encoding in a
bit. For now, clarity */
memset(partword[i],0,n/samples_per_partition*sizeof(long));
}
- for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
+ for(i=0;i<partvals;i++){
for(j=0;j<ch;j++)
/* do the partition decision based on the 'entropy'
int the block */
- partword[j][l]=
- classify(in[j]+i,samples_per_partition,look,possible_partitions,l);
+ partword[j][i]=
+ classify(in[j]+i*samples_per_partition+info->begin,
+ samples_per_partition,look,possible_partitions,i);
}
+#ifdef TRAIN_RES
+ {
+ FILE *of;
+ char buffer[80];
+
+ for(i=0;i<ch;i++){
+ sprintf(buffer,"resaux_%d.vqd",vb->mode);
+ of=fopen(buffer,"a");
+ for(j=0;j<partvals;j++)
+ fprintf(of,"%ld, ",partword[i][j]);
+ fprintf(of,"\n");
+ fclose(of);
+ }
+ }
+#endif
+ look->frames++;
+
+ return(partword);
+}
+
+static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int ch,
+ int (*classify)(float *,int,vorbis_look_residue0 *,
+ int,int)){
+ long i,j,k,l;
+ vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
+ vorbis_info_residue0 *info=look->info;
+
+ /* move all this setup out later */
+ int samples_per_partition=info->grouping;
+ int possible_partitions=info->partitions;
+ int n=info->end-info->begin;
+
+ int partvals=n/samples_per_partition;
+ long **partword=_vorbis_block_alloc(vb,sizeof(long *));
+ float *work=alloca(sizeof(float)*samples_per_partition);
+
+ partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(long));
+ memset(partword[0],0,n*ch/samples_per_partition*sizeof(long));
+
+ for(i=0,j=0,k=0,l=info->begin;i<partvals;i++){
+ for(k=0;k<samples_per_partition;k++){
+ work[k]=in[j][l];
+ j++;
+ if(j>=ch){
+ j=0;
+ l++;
+ }
+ }
+ partword[0][i]=
+ classify(work,samples_per_partition,look,possible_partitions,i);
+ }
+
+#ifdef TRAIN_RES
+ {
+ FILE *of;
+ char buffer[80];
+
+ sprintf(buffer,"resaux_%d.vqd",vb->mode);
+ of=fopen(buffer,"a");
+ for(i=0;i<partvals;i++)
+ fprintf(of,"%ld, ",partword[0][i]);
+ fprintf(of,"\n");
+ fclose(of);
+ }
+#endif
+ look->frames++;
+
+ return(partword);
+}
+
+static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int ch,
+ int pass,long **partword,
+ int (*encode)(oggpack_buffer *,float *,int,
+ codebook *,vorbis_look_residue0 *)){
+ long i,j,k,s;
+ vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
+ vorbis_info_residue0 *info=look->info;
+
+ /* move all this setup out later */
+ int samples_per_partition=info->grouping;
+ int possible_partitions=info->partitions;
+ int partitions_per_word=look->phrasebook->dim;
+ int n=info->end-info->begin;
+
+ int partvals=n/samples_per_partition;
+ long resbits[128];
+ long resvals[128];
+
+#ifdef TRAIN_RES
+ FILE *of;
+ char buffer[80];
+ int m;
+
+ for(i=0;i<ch;i++){
+ sprintf(buffer,"residue_%d#%d.vqd",vb->mode,pass);
+ of=fopen(buffer,"a");
+ for(m=0;m<info->end;m++)
+ fprintf(of,"%.2f, ",in[i][m]);
+ fprintf(of,"\n");
+ fclose(of);
+ }
+#endif
+
+ memset(resbits,0,sizeof(resbits));
+ memset(resvals,0,sizeof(resvals));
+
/* we code the partition words for each channel, then the residual
words for a partition per channel until we've written all the
residual words for that partition word. Then write the next
partition channel words... */
- for(s=0;s<look->stages;s++){
- for(i=info->begin,l=0;i<info->end;){
-
+ for(s=(pass==0?0:info->passlimit[pass-1]);s<info->passlimit[pass];s++){
+ for(i=0;i<partvals;){
+
/* first we encode a partition codeword for each channel */
if(s==0){
for(j=0;j<ch;j++){
- long val=partword[j][l];
- for(k=1;k<partitions_per_word;k++)
- val= val*possible_partitions+partword[j][l+k];
- look->phrase+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
+ long val=partword[j][i];
+ long ret;
+ for(k=1;k<partitions_per_word;k++){
+ val*=possible_partitions;
+ if(i+k<partvals)
+ val+=partword[j][i+k];
+ }
+
+ /* training hack */
+ if(val<look->phrasebook->entries)
+ ret=vorbis_book_encode(look->phrasebook,val,&vb->opb);
+ /*else
+ fprintf(stderr,"!");*/
+
+ look->phrasebits+=ret;
+
}
}
-
+
/* now we encode interleaved residual values for the partitions */
- for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition){
+ for(k=0;k<partitions_per_word && i<partvals;k++,i++){
+ long offset=i*samples_per_partition+info->begin;
for(j=0;j<ch;j++){
- if(info->secondstages[partword[j][l]]&(1<<s)){
- codebook *statebook=look->partbooks[partword[j][l]][s];
+ if(s==0)resvals[partword[j][i]]+=samples_per_partition;
+ if(info->secondstages[partword[j][i]]&(1<<s)){
+ codebook *statebook=look->partbooks[partword[j][i]][s];
if(statebook){
- int ret=encode(&vb->opb,in[j]+i,samples_per_partition,
- statebook,look);
- look->bits[partword[j][l]]+=ret;
- if(s==0)look->vals[partword[j][l]]+=samples_per_partition;
+ int ret=encode(&vb->opb,in[j]+offset,samples_per_partition,
+ statebook,look);
+ look->postbits+=ret;
+ resbits[partword[j][i]]+=ret;
}
}
}
}
}
+ /*{
+ long total=0;
+ long totalbits=0;
+ fprintf(stderr,"%d :: ",vb->mode);
+ for(k=0;k<possible_partitions;k++){
+ fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
+ total+=resvals[k];
+ totalbits+=resbits[k];
+ }
+
+ fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
+ }*/
return(0);
}
static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
float **in,int ch,
long (*decodepart)(codebook *, float *,
- oggpack_buffer *,int,int)){
+ oggpack_buffer *,int)){
- long i,j,k,l,s,transend=vb->pcmend/2;
+ long i,j,k,l,s;
vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
vorbis_info_residue0 *info=look->info;
int samples_per_partition=info->grouping;
int partitions_per_word=look->phrasebook->dim;
int n=info->end-info->begin;
-
+
int partvals=n/samples_per_partition;
int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
int ***partword=alloca(ch*sizeof(int **));
- float **work=alloca(ch*sizeof(float *));
- partvals=partwords*partitions_per_word;
- /* make sure we're zeroed up to the start */
- for(j=0;j<ch;j++){
- work[j]=_vorbis_block_alloc(vb,n*sizeof(float));
+ for(j=0;j<ch;j++)
partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(int *));
- memset(work[j],0,sizeof(float)*n);
- }
for(s=0;s<look->stages;s++){
- for(i=info->begin,l=0;i<info->end;l++){
+ /* each loop decodes on partition codeword containing
+ partitions_pre_word partitions */
+ for(i=0,l=0;i<partvals;l++){
if(s==0){
/* fetch the partition word for each channel */
for(j=0;j<ch;j++){
}
/* now we decode residual values for the partitions */
- for(k=0;k<partitions_per_word;k++,i+=samples_per_partition)
+ for(k=0;k<partitions_per_word && i<partvals;k++,i++)
for(j=0;j<ch;j++){
+ long offset=info->begin+i*samples_per_partition;
if(info->secondstages[partword[j][l][k]]&(1<<s)){
codebook *stagebook=look->partbooks[partword[j][l][k]][s];
if(stagebook){
- if(decodepart(stagebook,work[j]+i,&vb->opb,
- samples_per_partition,0)==-1)goto eopbreak;
+ if(decodepart(stagebook,in[j]+offset,&vb->opb,
+ samples_per_partition)==-1)goto eopbreak;
}
}
}
}
}
-
- eopbreak:
- for(j=0;j<ch;j++){
- for(i=0;i<n;i++)
- in[j][i]*=work[j][i];
- for(;i<transend;i++)
- in[j][i]=0;
- }
-
- return(0);
-
errout:
- for(j=0;j<ch;j++)
- memset(in[j],0,sizeof(float)*transend);
+ eopbreak:
return(0);
}
/* residue 0 and 1 are just slight variants of one another. 0 is
interleaved, 1 is not */
+long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int *nonzero,int ch){
+ /* we encode only the nonzero parts of a bundle */
+ int i,used=0;
+ for(i=0;i<ch;i++)
+ if(nonzero[i])
+ in[used++]=in[i];
+ if(used)
+ /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
+ return(_01class(vb,vl,in,used,_testhack));
+ else
+ return(0);
+}
+
int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
- float **in,int ch){
- return(_01forward(vb,vl,in,ch,_interleaved_testhack,_interleaved_encodepart));
+ float **in,float **out,int *nonzero,int ch,
+ int pass, long **partword){
+ /* we encode only the nonzero parts of a bundle */
+ int i,j,used=0,n=vb->pcmend/2;
+ for(i=0;i<ch;i++)
+ if(nonzero[i]){
+ for(j=0;j<n;j++)
+ out[i][j]+=in[i][j];
+ in[used++]=in[i];
+ }
+ if(used){
+ int ret=_01forward(vb,vl,in,used,pass,partword,
+ _interleaved_encodepart);
+ used=0;
+ for(i=0;i<ch;i++)
+ if(nonzero[i]){
+ for(j=0;j<n;j++)
+ out[i][j]-=in[used][j];
+ used++;
+ }
+ return(ret);
+ }else
+ return(0);
}
-int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
- return(_01inverse(vb,vl,in,ch,vorbis_book_decodevs));
+int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int *nonzero,int ch){
+ int i,used=0;
+ for(i=0;i<ch;i++)
+ if(nonzero[i])
+ in[used++]=in[i];
+ if(used)
+ return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
+ else
+ return(0);
}
int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
- float **in,int ch){
- return(_01forward(vb,vl,in,ch,_testhack,_encodepart));
+ float **in,float **out,int *nonzero,int ch,
+ int pass, long **partword){
+ int i,j,used=0,n=vb->pcmend/2;
+ for(i=0;i<ch;i++)
+ if(nonzero[i]){
+ for(j=0;j<n;j++)
+ out[i][j]+=in[i][j];
+ in[used++]=in[i];
+ }
+
+ if(used){
+ int ret=_01forward(vb,vl,in,used,pass,partword,_encodepart);
+ used=0;
+ for(i=0;i<ch;i++)
+ if(nonzero[i]){
+ for(j=0;j<n;j++)
+ out[i][j]-=in[used][j];
+ used++;
+ }
+ return(ret);
+ }else
+ return(0);
+}
+
+long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int *nonzero,int ch){
+ int i,used=0;
+ for(i=0;i<ch;i++)
+ if(nonzero[i])
+ in[used++]=in[i];
+ if(used)
+ return(_01class(vb,vl,in,used,_testhack));
+ else
+ return(0);
+}
+
+int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int *nonzero,int ch){
+ int i,used=0;
+ for(i=0;i<ch;i++)
+ if(nonzero[i])
+ in[used++]=in[i];
+ if(used)
+ return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
+ else
+ return(0);
}
-int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
- return(_01inverse(vb,vl,in,ch,vorbis_book_decodev));
+long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int *nonzero,int ch){
+ int i,used=0;
+ for(i=0;i<ch;i++)
+ if(nonzero[i])
+ in[used++]=in[i];
+ if(used)
+ return(_2class(vb,vl,in,used,_testhack));
+ else
+ return(0);
}
+/* res2 is slightly more different; all the channels are interleaved
+ into a single vector and encoded. */
+
+int res2_forward(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,float **out,int *nonzero,int ch,
+ int pass,long **partword){
+ long i,j,k,n=vb->pcmend/2,used=0;
+
+ /* don't duplicate the code; use a working vector hack for now and
+ reshape ourselves into a single channel res1 */
+ /* ugly; reallocs for each coupling pass :-( */
+ float *work=_vorbis_block_alloc(vb,ch*n*sizeof(float));
+ for(i=0;i<ch;i++){
+ float *pcm=in[i];
+ if(nonzero[i])used++;
+ for(j=0,k=i;j<n;j++,k+=ch)
+ work[k]=pcm[j];
+ }
+
+ if(used){
+ int ret=_01forward(vb,vl,&work,1,pass,partword,_encodepart);
+ /* update the sofar vector */
+ for(i=0;i<ch;i++){
+ float *pcm=in[i];
+ float *sofar=out[i];
+ for(j=0,k=i;j<n;j++,k+=ch)
+ sofar[j]+=pcm[j]-work[k];
+ }
+
+ return(ret);
+ }else
+ return(0);
+}
+
+/* duplicate code here as speed is somewhat more important */
+int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
+ float **in,int *nonzero,int ch){
+ long i,k,l,s;
+ vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
+ vorbis_info_residue0 *info=look->info;
+
+ /* move all this setup out later */
+ int samples_per_partition=info->grouping;
+ int partitions_per_word=look->phrasebook->dim;
+ int n=info->end-info->begin;
+
+ int partvals=n/samples_per_partition;
+ int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
+ int **partword=_vorbis_block_alloc(vb,partwords*sizeof(int *));
+
+ for(i=0;i<ch;i++)if(nonzero[i])break;
+ if(i==ch)return(0); /* no nonzero vectors */
+
+ for(s=0;s<look->stages;s++){
+ for(i=0,l=0;i<partvals;l++){
+
+ if(s==0){
+ /* fetch the partition word */
+ int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
+ if(temp==-1)goto eopbreak;
+ partword[l]=look->decodemap[temp];
+ if(partword[l]==NULL)goto errout;
+ }
+
+ /* now we decode residual values for the partitions */
+ for(k=0;k<partitions_per_word && i<partvals;k++,i++)
+ if(info->secondstages[partword[l][k]]&(1<<s)){
+ codebook *stagebook=look->partbooks[partword[l][k]][s];
+
+ if(stagebook){
+ if(vorbis_book_decodevv_add(stagebook,in,
+ i*samples_per_partition+info->begin,ch,
+ &vb->opb,samples_per_partition)==-1)
+ goto eopbreak;
+ }
+ }
+ }
+ }
+
+ errout:
+ eopbreak:
+ return(0);
+}
+
+
vorbis_func_residue residue0_exportbundle={
&res0_pack,
&res0_unpack,
&res0_copy_info,
&res0_free_info,
&res0_free_look,
+ &res0_class,
&res0_forward,
&res0_inverse
};
&res0_copy_info,
&res0_free_info,
&res0_free_look,
+ &res1_class,
&res1_forward,
&res1_inverse
};
+
+vorbis_func_residue residue2_exportbundle={
+ &res0_pack,
+ &res0_unpack,
+ &res0_look,
+ &res0_copy_info,
+ &res0_free_info,
+ &res0_free_look,
+ &res2_class,
+ &res2_forward,
+ &res2_inverse
+};