diff options
Diffstat (limited to 'lib/res0.c')
-rw-r--r-- | lib/res0.c | 212 |
1 files changed, 111 insertions, 101 deletions
@@ -5,8 +5,8 @@ * 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/ * * * ******************************************************************** @@ -336,7 +336,7 @@ static int local_book_besterror(codebook *book,float *a){ } } - { + if(best>-1){ float *ptr=book->valuelist+best*dim; for(i=0;i<dim;i++) *a++ -= *ptr++; @@ -355,10 +355,12 @@ static int _encodepart(oggpack_buffer *opb,float *vec, int n, int entry=local_book_besterror(book,vec+i*dim); #ifdef TRAIN_RES - acc[entry]++; + if(entry>0) + acc[entry]++; #endif - + bits+=vorbis_book_encode(book,entry,opb); + } return(bits); @@ -380,11 +382,11 @@ static long **_01class(vorbis_block *vb,vorbis_look_residue *vl, int partvals=n/samples_per_partition; long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword)); float scale=100./samples_per_partition; - + /* 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 */ - + for(i=0;i<ch;i++){ partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i])); memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i])); @@ -409,12 +411,12 @@ static long **_01class(vorbis_block *vb,vorbis_look_residue *vl, partword[j][i]=k; } } - + #ifdef TRAIN_RESAUX { FILE *of; char buffer[80]; - + for(i=0;i<ch;i++){ sprintf(buffer,"resaux_%d.vqd",look->train_seq); of=fopen(buffer,"a"); @@ -426,7 +428,7 @@ static long **_01class(vorbis_block *vb,vorbis_look_residue *vl, } #endif look->frames++; - + return(partword); } @@ -446,7 +448,7 @@ static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in, int partvals=n/samples_per_partition; long **partword=_vorbis_block_alloc(vb,sizeof(*partword)); - + #if defined(TRAIN_RES) || defined (TRAIN_RESAUX) FILE *of; char buffer[80]; @@ -454,7 +456,7 @@ static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in, partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0])); memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0])); - + for(i=0,l=info->begin/ch;i<partvals;i++){ float magmax=0.f; float angmax=0.f; @@ -462,16 +464,16 @@ static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in, if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]); for(k=1;k<ch;k++) if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]); - l++; + l++; } - + for(j=0;j<possible_partitions-1;j++) if(magmax<=info->classmetric1[j] && angmax<=info->classmetric2[j]) break; - + partword[0][i]=j; - + } #ifdef TRAIN_RESAUX @@ -482,9 +484,9 @@ static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in, fprintf(of,"\n"); fclose(of); #endif - + look->frames++; - + return(partword); } @@ -509,15 +511,15 @@ static int _01forward(oggpack_buffer *opb, int partvals=n/samples_per_partition; long resbits[128]; long resvals[128]; - + #ifdef TRAIN_RES for(i=0;i<ch;i++) - for(j=info->begin;j<info->end;j++){ + for(j=info->begin;j<end;j++){ if(in[i][j]>look->tmax)look->tmax=in[i][j]; if(in[i][j]<look->tmin)look->tmin=in[i][j]; } #endif - + memset(resbits,0,sizeof(resbits)); memset(resvals,0,sizeof(resvals)); @@ -525,11 +527,11 @@ static int _01forward(oggpack_buffer *opb, 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=0;i<partvals;){ - + /* first we encode a partition codeword for each channel */ if(s==0){ for(j=0;j<ch;j++){ @@ -539,7 +541,7 @@ static int _01forward(oggpack_buffer *opb, if(i+k<partvals) val+=partword[j][i+k]; } - + /* training hack */ if(val<look->phrasebook->entries) look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb); @@ -547,14 +549,14 @@ static int _01forward(oggpack_buffer *opb, else fprintf(stderr,"!"); #endif - + } } /* now we encode interleaved residual values for the partitions */ 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(s==0)resvals[partword[j][i]]+=samples_per_partition; if(info->secondstages[partword[j][i]]&(1<<s)){ @@ -562,7 +564,7 @@ static int _01forward(oggpack_buffer *opb, if(statebook){ int ret; long *accumulator=NULL; - + #ifdef TRAIN_RES accumulator=look->training_data[s][partword[j][i]]; { @@ -579,7 +581,7 @@ static int _01forward(oggpack_buffer *opb, ret=encode(opb,in[j]+offset,samples_per_partition, statebook,accumulator); - + look->postbits+=ret; resbits[partword[j][i]]+=ret; } @@ -588,19 +590,20 @@ static int _01forward(oggpack_buffer *opb, } } } - + /*{ 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, ",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); } @@ -617,45 +620,49 @@ static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl, /* 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 max=vb->pcmend>>1; + int end=(info->end<max?info->end:max); + int n=end-info->begin; - int partvals=n/samples_per_partition; - int partwords=(partvals+partitions_per_word-1)/partitions_per_word; - int ***partword=alloca(ch*sizeof(*partword)); - - for(j=0;j<ch;j++) - partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j])); - - for(s=0;s<look->stages;s++){ - - /* 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++){ - int temp=vorbis_book_decode(look->phrasebook,&vb->opb); - if(temp==-1)goto eopbreak; - partword[j][l]=look->decodemap[temp]; - if(partword[j][l]==NULL)goto errout; - } - } + if(n>0){ + int partvals=n/samples_per_partition; + int partwords=(partvals+partitions_per_word-1)/partitions_per_word; + int ***partword=alloca(ch*sizeof(*partword)); + + for(j=0;j<ch;j++) + partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j])); + + for(s=0;s<look->stages;s++){ - /* now we decode residual values for the partitions */ - 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,in[j]+offset,&vb->opb, - samples_per_partition)==-1)goto eopbreak; - } + /* each loop decodes on partition codeword containing + partitions_per_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++){ + int temp=vorbis_book_decode(look->phrasebook,&vb->opb); + + if(temp==-1)goto eopbreak; + partword[j][l]=look->decodemap[temp]; + if(partword[j][l]==NULL)goto errout; } } - } + + /* now we decode residual values for the partitions */ + 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,in[j]+offset,&vb->opb, + samples_per_partition)==-1)goto eopbreak; + } + } + } + } + } } - errout: eopbreak: return(0); @@ -833,41 +840,44 @@ int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl, /* 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(*partword)); - - 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; - } + int max=(vb->pcmend*ch)>>1; + int end=(info->end<max?info->end:max); + int n=end-info->begin; + + if(n>0){ + int partvals=n/samples_per_partition; + int partwords=(partvals+partitions_per_word-1)/partitions_per_word; + int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword)); + + 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); |