examples/sfexamples/oggvorbiscodec/src/libvorbis/lib/res0.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
00004  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00005  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00006  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00007  *                                                                  *
00008  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002             *
00009  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
00010  *                                                                  *
00011  ********************************************************************
00012 
00013  function: residue backend 0, 1 and 2 implementation
00014  last mod: $Id: res0.c 7187 2004-07-20 07:24:27Z xiphmont $
00015 
00016  ********************************************************************/
00017 
00018 /* Slow, slow, slow, simpleminded and did I mention it was slow?  The
00019    encode/decode loops are coded for clarity and performance is not
00020    yet even a nagging little idea lurking in the shadows.  Oh and BTW,
00021    it's slow. */
00022 
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include "ogg/ogg.h"
00027 #include "vorbis/codec.h"
00028 #include "codec_internal.h"
00029 #include "registry.h"
00030 #include "codebook.h"
00031 #include "misc.h"
00032 #include "os.h"
00033 
00034 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
00035 #include <stdio.h>
00036 #endif 
00037 
00038 typedef struct {
00039   vorbis_info_residue0 *info;
00040   
00041   int         parts;
00042   int         stages;
00043   codebook   *fullbooks;
00044   codebook   *phrasebook;
00045   codebook ***partbooks;
00046 
00047   int         partvals;
00048   int       **decodemap;
00049 
00050   long      postbits;
00051   long      phrasebits;
00052   long      frames;
00053 
00054 #if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
00055   int        train_seq;
00056   long      *training_data[8][64];
00057   float      training_max[8][64];
00058   float      training_min[8][64];
00059   float     tmin;
00060   float     tmax;
00061 #endif
00062 
00063 } vorbis_look_residue0;
00064 
00065 void res0_free_info(vorbis_info_residue *i){
00066   vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
00067   if(info){
00068     memset(info,0,sizeof(*info));
00069     _ogg_free(info);
00070   }
00071 }
00072 
00073 void res0_free_look(vorbis_look_residue *i){
00074   int j;
00075   if(i){
00076 
00077     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
00078 
00079 #ifdef TRAIN_RES
00080     {
00081       int j,k,l;
00082       for(j=0;j<look->parts;j++){
00083         /*fprintf(stderr,"partition %d: ",j);*/
00084         for(k=0;k<8;k++)
00085           if(look->training_data[k][j]){
00086             char buffer[80];
00087             FILE *of;
00088             codebook *statebook=look->partbooks[j][k];
00089             
00090             /* long and short into the same bucket by current convention */
00091             sprintf(buffer,"res_part%d_pass%d.vqd",j,k);
00092             of=fopen(buffer,"a");
00093 
00094             for(l=0;l<statebook->entries;l++)
00095               fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
00096             
00097             fclose(of);
00098             
00099             /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
00100               look->training_min[k][j],look->training_max[k][j]);*/
00101 
00102             _ogg_free(look->training_data[k][j]);
00103             look->training_data[k][j]=NULL;
00104           }
00105         /*fprintf(stderr,"\n");*/
00106       }
00107     }
00108     fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
00109 
00110     /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
00111             (float)look->phrasebits/look->frames,
00112             (float)look->postbits/look->frames,
00113             (float)(look->postbits+look->phrasebits)/look->frames);*/
00114 #endif
00115 
00116 
00117     /*vorbis_info_residue0 *info=look->info;
00118 
00119     fprintf(stderr,
00120             "%ld frames encoded in %ld phrasebits and %ld residue bits "
00121             "(%g/frame) \n",look->frames,look->phrasebits,
00122             look->resbitsflat,
00123             (look->phrasebits+look->resbitsflat)/(float)look->frames);
00124     
00125     for(j=0;j<look->parts;j++){
00126       long acc=0;
00127       fprintf(stderr,"\t[%d] == ",j);
00128       for(k=0;k<look->stages;k++)
00129         if((info->secondstages[j]>>k)&1){
00130           fprintf(stderr,"%ld,",look->resbits[j][k]);
00131           acc+=look->resbits[j][k];
00132         }
00133 
00134       fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
00135               acc?(float)acc/(look->resvals[j]*info->grouping):0);
00136     }
00137     fprintf(stderr,"\n");*/
00138 
00139     for(j=0;j<look->parts;j++)
00140       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
00141     _ogg_free(look->partbooks);
00142     for(j=0;j<look->partvals;j++)
00143       _ogg_free(look->decodemap[j]);
00144     _ogg_free(look->decodemap);
00145 
00146     memset(look,0,sizeof(*look));
00147     _ogg_free(look);
00148   }
00149 }
00150 
00151 static int ilog(unsigned int v){
00152   int ret=0;
00153   while(v){
00154     ret++;
00155     v>>=1;
00156   }
00157   return(ret);
00158 }
00159 
00160 static int icount(unsigned int v){
00161   int ret=0;
00162   while(v){
00163     ret+=v&1;
00164     v>>=1;
00165   }
00166   return(ret);
00167 }
00168 
00169 
00170 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
00171   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
00172   int j,acc=0;
00173   oggpack_write(opb,info->begin,24);
00174   oggpack_write(opb,info->end,24);
00175 
00176   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
00177                                              code with a partitioned book */
00178   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
00179   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
00180 
00181   /* secondstages is a bitmask; as encoding progresses pass by pass, a
00182      bitmask of one indicates this partition class has bits to write
00183      this pass */
00184   for(j=0;j<info->partitions;j++){
00185     if(ilog(info->secondstages[j])>3){
00186       /* yes, this is a minor hack due to not thinking ahead */
00187       oggpack_write(opb,info->secondstages[j],3); 
00188       oggpack_write(opb,1,1);
00189       oggpack_write(opb,info->secondstages[j]>>3,5); 
00190     }else
00191       oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
00192     acc+=icount(info->secondstages[j]);
00193   }
00194   for(j=0;j<acc;j++)
00195     oggpack_write(opb,info->booklist[j],8);
00196 
00197 }
00198 
00199 /* vorbis_info is for range checking */
00200 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
00201   int j,acc=0;
00202   vorbis_info_residue0 *info=(vorbis_info_residue0*)_ogg_calloc(1,sizeof(*info));
00203   codec_setup_info     *ci=(codec_setup_info*)vi->codec_setup;
00204 
00205   info->begin=oggpack_read(opb,24);
00206   info->end=oggpack_read(opb,24);
00207   info->grouping=oggpack_read(opb,24)+1;
00208   info->partitions=oggpack_read(opb,6)+1;
00209   info->groupbook=oggpack_read(opb,8);
00210 
00211   for(j=0;j<info->partitions;j++){
00212     int cascade=oggpack_read(opb,3);
00213     if(oggpack_read(opb,1))
00214       cascade|=(oggpack_read(opb,5)<<3);
00215     info->secondstages[j]=cascade;
00216 
00217     acc+=icount(cascade);
00218   }
00219   for(j=0;j<acc;j++)
00220     info->booklist[j]=oggpack_read(opb,8);
00221 
00222   if(info->groupbook>=ci->books)goto errout;
00223   for(j=0;j<acc;j++)
00224     if(info->booklist[j]>=ci->books)goto errout;
00225 
00226   return(info);
00227  errout:
00228   res0_free_info(info);
00229   return(NULL);
00230 }
00231 
00232 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
00233                                vorbis_info_residue *vr){
00234   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
00235   vorbis_look_residue0 *look=(vorbis_look_residue0*)_ogg_calloc(1,sizeof(*look));
00236   codec_setup_info     *ci=(codec_setup_info*)vd->vi->codec_setup;
00237 
00238   int j,k,acc=0;
00239   int dim;
00240   int maxstage=0;
00241   look->info=info;
00242 
00243   look->parts=info->partitions;
00244   look->fullbooks=ci->fullbooks;
00245   look->phrasebook=ci->fullbooks+info->groupbook;
00246   dim=look->phrasebook->dim;
00247 
00248   look->partbooks=(codebook***)_ogg_calloc(look->parts,sizeof(*look->partbooks));
00249 
00250   for(j=0;j<look->parts;j++){
00251     int stages=ilog(info->secondstages[j]);
00252     if(stages){
00253       if(stages>maxstage)maxstage=stages;
00254       look->partbooks[j]=(codebook**)_ogg_calloc(stages,sizeof(*look->partbooks[j]));
00255       for(k=0;k<stages;k++)
00256         if(info->secondstages[j]&(1<<k)){
00257           look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
00258 #ifdef TRAIN_RES
00259           look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
00260                                            sizeof(***look->training_data));
00261 #endif
00262         }
00263     }
00264   }
00265 
00266   look->partvals=rint(pow((float)look->parts,(float)dim));
00267   look->stages=maxstage;
00268   look->decodemap=(int**)_ogg_malloc(look->partvals*sizeof(*look->decodemap));
00269   for(j=0;j<look->partvals;j++){
00270     long val=j;
00271     long mult=look->partvals/look->parts;
00272     look->decodemap[j]=(int*)_ogg_malloc(dim*sizeof(*look->decodemap[j]));
00273     for(k=0;k<dim;k++){
00274       long deco=val/mult;
00275       val-=deco*mult;
00276       mult/=look->parts;
00277       look->decodemap[j][k]=deco;
00278     }
00279   }
00280 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
00281   {
00282     static int train_seq=0;
00283     look->train_seq=train_seq++;
00284   }
00285 #endif
00286   return(look);
00287 }
00288 
00289 /* break an abstraction and copy some code for performance purposes */
00290 static int local_book_besterror(codebook *book,float *a){
00291   int dim=book->dim,i,k,o;
00292   int best=0;
00293   encode_aux_threshmatch *tt=book->c->thresh_tree;
00294 
00295   /* find the quant val of each scalar */
00296   for(k=0,o=dim;k<dim;++k){
00297     float val=a[--o];
00298     i=tt->threshvals>>1;
00299 
00300     if(val<tt->quantthresh[i]){      
00301       if(val<tt->quantthresh[i-1]){
00302         for(--i;i>0;--i)
00303           if(val>=tt->quantthresh[i-1])
00304             break;
00305       }
00306     }else{
00307       
00308       for(++i;i<tt->threshvals-1;++i)
00309         if(val<tt->quantthresh[i])break;
00310       
00311     }
00312 
00313     best=(best*tt->quantvals)+tt->quantmap[i];
00314   }
00315   /* regular lattices are easy :-) */
00316   
00317   if(book->c->lengthlist[best]<=0){
00318     const static_codebook *c=book->c;
00319     int i,j;
00320     float bestf=0.f;
00321     float *e=book->valuelist;
00322     best=-1;
00323     for(i=0;i<book->entries;i++){
00324       if(c->lengthlist[i]>0){
00325         float temp=0.f;
00326         for(j=0;j<dim;j++){
00327           float val=(e[j]-a[j]);
00328           temp+=val*val;
00329         }
00330         if(best==-1 || temp<bestf){
00331           bestf=temp;
00332           best=i;
00333         }
00334       }
00335       e+=dim;
00336     }
00337   }
00338 
00339   {
00340     float *ptr=book->valuelist+best*dim;
00341     for(i=0;i<dim;i++)
00342       *a++ -= *ptr++;
00343   }
00344 
00345   return(best);
00346 }
00347 
00348 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
00349                        codebook *book,long *acc){
00350   int i,bits=0;
00351   int dim=book->dim;
00352   int step=n/dim;
00353   acc = acc; //warning  
00354   for(i=0;i<step;i++){
00355     int entry=local_book_besterror(book,vec+i*dim);
00356 
00357 #ifdef TRAIN_RES
00358     acc[entry]++;
00359 #endif
00360 
00361     bits+=vorbis_book_encode(book,entry,opb);
00362   }
00363 
00364   return(bits);
00365 }
00366 
00367 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
00368                        float **in,int ch){
00369   long i,j,k;
00370   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
00371   vorbis_info_residue0 *info=look->info;
00372   //vorbis_info           *vi=vb->vd->vi;//warning
00373   //codec_setup_info      *ci=vi->codec_setup;//warning
00374 
00375   /* move all this setup out later */
00376   int samples_per_partition=info->grouping;
00377   int possible_partitions=info->partitions;
00378   int n=info->end-info->begin;
00379   
00380   int partvals=n/samples_per_partition;
00381   long **partword=(long int**)_vorbis_block_alloc(vb,ch*sizeof(*partword));
00382   float scale=100./samples_per_partition;
00383 
00384   /* we find the partition type for each partition of each
00385      channel.  We'll go back and do the interleaved encoding in a
00386      bit.  For now, clarity */
00387  
00388   for(i=0;i<ch;i++){
00389     partword[i]=(long int*)_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
00390     memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
00391   }
00392   
00393   for(i=0;i<partvals;i++){
00394     int offset=i*samples_per_partition+info->begin;
00395     for(j=0;j<ch;j++){
00396       float max=0.;
00397       float ent=0.;
00398       for(k=0;k<samples_per_partition;k++){
00399         if(fabs(in[j][offset+k])>max)max=fabs(in[j][offset+k]);
00400         ent+=fabs(rint(in[j][offset+k]));
00401       }
00402       ent*=scale;
00403       
00404       for(k=0;k<possible_partitions-1;k++)
00405         if(max<=info->classmetric1[k] &&
00406            (info->classmetric2[k]<0 || (int)ent<info->classmetric2[k]))
00407           break;
00408       
00409       partword[j][i]=k;  
00410     }
00411   }
00412 
00413 #ifdef TRAIN_RESAUX
00414   {
00415     FILE *of;
00416     char buffer[80];
00417   
00418     for(i=0;i<ch;i++){
00419       sprintf(buffer,"resaux_%d.vqd",look->train_seq);
00420       of=fopen(buffer,"a");
00421       for(j=0;j<partvals;j++)
00422         fprintf(of,"%ld, ",partword[i][j]);
00423       fprintf(of,"\n");
00424       fclose(of);
00425     }
00426   }
00427 #endif
00428   look->frames++;
00429 
00430   return(partword);
00431 }
00432 
00433 /* designed for stereo or other modes where the partition size is an
00434    integer multiple of the number of channels encoded in the current
00435    submap */
00436 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in,
00437                       int ch){
00438   long i,j,k,l;
00439   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
00440   vorbis_info_residue0 *info=look->info;
00441 
00442   /* move all this setup out later */
00443   int samples_per_partition=info->grouping;
00444   int possible_partitions=info->partitions;
00445   int n=info->end-info->begin;
00446 
00447   int partvals=n/samples_per_partition;
00448   long **partword=(long int**)_vorbis_block_alloc(vb,sizeof(*partword));
00449 
00450 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
00451   FILE *of;
00452   char buffer[80];
00453 #endif
00454   
00455   partword[0]=(long int*)_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
00456   memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
00457 
00458   for(i=0,l=info->begin/ch;i<partvals;i++){
00459     float magmax=0.f;
00460     float angmax=0.f;
00461     for(j=0;j<samples_per_partition;j+=ch){
00462       if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]);
00463       for(k=1;k<ch;k++)
00464         if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]);
00465       l++;
00466     }
00467 
00468     for(j=0;j<possible_partitions-1;j++)
00469       if(magmax<=info->classmetric1[j] &&
00470          angmax<=info->classmetric2[j])
00471         break;
00472 
00473     partword[0][i]=j;
00474 
00475   }  
00476   
00477 #ifdef TRAIN_RESAUX
00478   sprintf(buffer,"resaux_%d.vqd",look->train_seq);
00479   of=fopen(buffer,"a");
00480   for(i=0;i<partvals;i++)
00481     fprintf(of,"%ld, ",partword[0][i]);
00482   fprintf(of,"\n");
00483   fclose(of);
00484 #endif
00485 
00486   look->frames++;
00487 
00488   return(partword);
00489 }
00490 
00491 static int _01forward(oggpack_buffer *opb,
00492                       vorbis_block *vb,vorbis_look_residue *vl,
00493                       float **in,int ch,
00494                       long **partword,
00495                       int (*encode)(oggpack_buffer *,float *,int,
00496                                     codebook *,long *)){
00497   long i,j,k,s;
00498   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
00499   vorbis_info_residue0 *info=look->info;
00500   //vorbis_dsp_state      *vd=vb->vd;//warning
00501 
00502   /* move all this setup out later */
00503   int samples_per_partition=info->grouping;
00504   int possible_partitions=info->partitions;
00505   int partitions_per_word=look->phrasebook->dim;
00506   int n=info->end-info->begin;
00507 
00508   int partvals=n/samples_per_partition;
00509   long resbits[128];
00510   long resvals[128];
00511 
00512 #ifdef TRAIN_RES
00513   for(i=0;i<ch;i++)
00514     for(j=info->begin;j<info->end;j++){
00515       if(in[i][j]>look->tmax)look->tmax=in[i][j];
00516       if(in[i][j]<look->tmin)look->tmin=in[i][j];
00517     }
00518 #endif
00519   vb = vb; //warning            
00520   memset(resbits,0,sizeof(resbits));
00521   memset(resvals,0,sizeof(resvals));
00522   
00523   /* we code the partition words for each channel, then the residual
00524      words for a partition per channel until we've written all the
00525      residual words for that partition word.  Then write the next
00526      partition channel words... */
00527 
00528   for(s=0;s<look->stages;s++){
00529 
00530     for(i=0;i<partvals;){
00531 
00532       /* first we encode a partition codeword for each channel */
00533       if(s==0){
00534         for(j=0;j<ch;j++){
00535           long val=partword[j][i];
00536           for(k=1;k<partitions_per_word;k++){
00537             val*=possible_partitions;
00538             if(i+k<partvals)
00539               val+=partword[j][i+k];
00540           }     
00541 
00542           /* training hack */
00543           if(val<look->phrasebook->entries)
00544             look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
00545 #if 0 /*def TRAIN_RES*/
00546           else
00547             fprintf(stderr,"!");
00548 #endif
00549         
00550         }
00551       }
00552       
00553       /* now we encode interleaved residual values for the partitions */
00554       for(k=0;k<partitions_per_word && i<partvals;k++,i++){
00555         long offset=i*samples_per_partition+info->begin;
00556         
00557         for(j=0;j<ch;j++){
00558           if(s==0)resvals[partword[j][i]]+=samples_per_partition;
00559           if(info->secondstages[partword[j][i]]&(1<<s)){
00560             codebook *statebook=look->partbooks[partword[j][i]][s];
00561             if(statebook){
00562               int ret;
00563               long *accumulator=NULL;
00564 
00565 #ifdef TRAIN_RES
00566               accumulator=look->training_data[s][partword[j][i]];
00567               {
00568                 int l;
00569                 float *samples=in[j]+offset;
00570                 for(l=0;l<samples_per_partition;l++){
00571                   if(samples[l]<look->training_min[s][partword[j][i]])
00572                     look->training_min[s][partword[j][i]]=samples[l];
00573                   if(samples[l]>look->training_max[s][partword[j][i]])
00574                     look->training_max[s][partword[j][i]]=samples[l];
00575                 }
00576               }
00577 #endif
00578               
00579               ret=encode(opb,in[j]+offset,samples_per_partition,
00580                          statebook,accumulator);
00581 
00582               look->postbits+=ret;
00583               resbits[partword[j][i]]+=ret;
00584             }
00585           }
00586         }
00587       }
00588     }
00589   }
00590 
00591   /*{
00592     long total=0;
00593     long totalbits=0;
00594     fprintf(stderr,"%d :: ",vb->mode);
00595     for(k=0;k<possible_partitions;k++){
00596       fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
00597       total+=resvals[k];
00598       totalbits+=resbits[k];
00599       }
00600     
00601     fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
00602     }*/
00603   return(0);
00604 }
00605 
00606 /* a truncated packet here just means 'stop working'; it's not an error */
00607 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
00608                       float **in,int ch,
00609                       long (*decodepart)(codebook *, float *, 
00610                                          oggpack_buffer *,int)){
00611 
00612   long i,j,k,l,s;
00613   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
00614   vorbis_info_residue0 *info=look->info;
00615 
00616   /* move all this setup out later */
00617   int samples_per_partition=info->grouping;
00618   int partitions_per_word=look->phrasebook->dim;
00619   int n=info->end-info->begin;
00620   
00621   int partvals=n/samples_per_partition;
00622   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
00623   int ***partword=(int***)_ogg_malloc(ch*sizeof(*partword));//alloca(ch*sizeof(*partword)); //patch
00624   int ***partword_cpy = partword;
00625   for(j=0;j<ch;j++)
00626     partword[j]=(int**)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
00627 
00628   for(s=0;s<look->stages;s++){
00629 
00630     /* each loop decodes on partition codeword containing 
00631        partitions_pre_word partitions */
00632     for(i=0,l=0;i<partvals;l++){
00633       if(s==0){
00634         /* fetch the partition word for each channel */
00635         for(j=0;j<ch;j++){
00636           int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
00637           if(temp==-1)goto eopbreak;
00638           partword[j][l]=look->decodemap[temp];
00639           if(partword[j][l]==NULL)goto errout;
00640         }
00641       }
00642       
00643       /* now we decode residual values for the partitions */
00644       for(k=0;k<partitions_per_word && i<partvals;k++,i++)
00645         for(j=0;j<ch;j++){
00646           long offset=info->begin+i*samples_per_partition;
00647           if(info->secondstages[partword[j][l][k]]&(1<<s)){
00648             codebook *stagebook=look->partbooks[partword[j][l][k]][s];
00649             if(stagebook){
00650               if(decodepart(stagebook,in[j]+offset,&vb->opb,
00651                             samples_per_partition)==-1)goto eopbreak;
00652             }
00653           }
00654         }
00655     } 
00656   }
00657   
00658  errout:
00659  eopbreak:
00660  free(partword_cpy);
00661   return(0);
00662 }
00663 
00664 #if 0
00665 /* residue 0 and 1 are just slight variants of one another. 0 is
00666    interleaved, 1 is not */
00667 long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
00668                   float **in,int *nonzero,int ch){
00669   /* we encode only the nonzero parts of a bundle */
00670   int i,used=0;
00671   for(i=0;i<ch;i++)
00672     if(nonzero[i])
00673       in[used++]=in[i];
00674   if(used)
00675     /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
00676     return(_01class(vb,vl,in,used));
00677   else
00678     return(0);
00679 }
00680 
00681 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
00682                  float **in,float **out,int *nonzero,int ch,
00683                  long **partword){
00684   /* we encode only the nonzero parts of a bundle */
00685   int i,j,used=0,n=vb->pcmend/2;
00686   for(i=0;i<ch;i++)
00687     if(nonzero[i]){
00688       if(out)
00689         for(j=0;j<n;j++)
00690           out[i][j]+=in[i][j];
00691       in[used++]=in[i];
00692     }
00693   if(used){
00694     int ret=_01forward(vb,vl,in,used,partword,
00695                       _interleaved_encodepart);
00696     if(out){
00697       used=0;
00698       for(i=0;i<ch;i++)
00699         if(nonzero[i]){
00700           for(j=0;j<n;j++)
00701             out[i][j]-=in[used][j];
00702           used++;
00703         }
00704     }
00705     return(ret);
00706   }else{
00707     return(0);
00708   }
00709 }
00710 #endif
00711 
00712 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
00713                  float **in,int *nonzero,int ch){
00714   int i,used=0;
00715   for(i=0;i<ch;i++)
00716     if(nonzero[i])
00717       in[used++]=in[i];
00718   if(used)
00719     return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
00720   else
00721     return(0);
00722 }
00723 
00724 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
00725                  float **in,float **out,int *nonzero,int ch,
00726                  long **partword){
00727   int i,j,used=0,n=vb->pcmend/2;
00728   for(i=0;i<ch;i++)
00729     if(nonzero[i]){
00730       if(out)
00731         for(j=0;j<n;j++)
00732           out[i][j]+=in[i][j];
00733       in[used++]=in[i];
00734     }
00735 
00736   if(used){
00737     int ret=_01forward(opb,vb,vl,in,used,partword,_encodepart);
00738     if(out){
00739       used=0;
00740       for(i=0;i<ch;i++)
00741         if(nonzero[i]){
00742           for(j=0;j<n;j++)
00743             out[i][j]-=in[used][j];
00744           used++;
00745         }
00746     }
00747     return(ret);
00748   }else{
00749     return(0);
00750   }
00751 }
00752 
00753 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
00754                   float **in,int *nonzero,int ch){
00755   int i,used=0;
00756   for(i=0;i<ch;i++)
00757     if(nonzero[i])
00758       in[used++]=in[i];
00759   if(used)
00760     return(_01class(vb,vl,in,used));
00761   else
00762     return(0);
00763 }
00764 
00765 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
00766                  float **in,int *nonzero,int ch){
00767   int i,used=0;
00768   for(i=0;i<ch;i++)
00769     if(nonzero[i])
00770       in[used++]=in[i];
00771   if(used)
00772     return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
00773   else
00774     return(0);
00775 }
00776 
00777 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
00778                   float **in,int *nonzero,int ch){
00779   int i,used=0;
00780   for(i=0;i<ch;i++)
00781     if(nonzero[i])used++;
00782   if(used)
00783     return(_2class(vb,vl,in,ch));
00784   else
00785     return(0);
00786 }
00787 
00788 /* res2 is slightly more different; all the channels are interleaved
00789    into a single vector and encoded. */
00790 
00791 int res2_forward(oggpack_buffer *opb,
00792                  vorbis_block *vb,vorbis_look_residue *vl,
00793                  float **in,float **out,int *nonzero,int ch,
00794                  long **partword){
00795   long i,j,k,n=vb->pcmend/2,used=0;
00796 
00797   /* don't duplicate the code; use a working vector hack for now and
00798      reshape ourselves into a single channel res1 */
00799   /* ugly; reallocs for each coupling pass :-( */
00800   float *work=(float*)_vorbis_block_alloc(vb,ch*n*sizeof(*work));
00801   for(i=0;i<ch;i++){
00802     float *pcm=in[i];
00803     if(nonzero[i])used++;
00804     for(j=0,k=i;j<n;j++,k+=ch)
00805       work[k]=pcm[j];
00806   }
00807   
00808   if(used){
00809     int ret=_01forward(opb,vb,vl,&work,1,partword,_encodepart);
00810     /* update the sofar vector */
00811     if(out){
00812       for(i=0;i<ch;i++){
00813         float *pcm=in[i];
00814         float *sofar=out[i];
00815         for(j=0,k=i;j<n;j++,k+=ch)
00816           sofar[j]+=pcm[j]-work[k];
00817         
00818       }
00819     }
00820     return(ret);
00821   }else{
00822     return(0);
00823   }
00824 }
00825 
00826 /* duplicate code here as speed is somewhat more important */
00827 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
00828                  float **in,int *nonzero,int ch){
00829   long i,k,l,s;
00830   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
00831   vorbis_info_residue0 *info=look->info;
00832 
00833   /* move all this setup out later */
00834   int samples_per_partition=info->grouping;
00835   int partitions_per_word=look->phrasebook->dim;
00836   int n=info->end-info->begin;
00837 
00838   int partvals=n/samples_per_partition;
00839   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
00840   int **partword=(int**)_vorbis_block_alloc(vb,partwords*sizeof(*partword));
00841 
00842   for(i=0;i<ch;i++)if(nonzero[i])break;
00843   if(i==ch)return(0); /* no nonzero vectors */
00844 
00845   for(s=0;s<look->stages;s++){
00846     for(i=0,l=0;i<partvals;l++){
00847 
00848       if(s==0){
00849         /* fetch the partition word */
00850         int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
00851         if(temp==-1)goto eopbreak;
00852         partword[l]=look->decodemap[temp];
00853         if(partword[l]==NULL)goto errout;
00854       }
00855 
00856       /* now we decode residual values for the partitions */
00857       for(k=0;k<partitions_per_word && i<partvals;k++,i++)
00858         if(info->secondstages[partword[l][k]]&(1<<s)){
00859           codebook *stagebook=look->partbooks[partword[l][k]][s];
00860           
00861           if(stagebook){
00862             if(vorbis_book_decodevv_add(stagebook,in,
00863                                         i*samples_per_partition+info->begin,ch,
00864                                         &vb->opb,samples_per_partition)==-1)
00865               goto eopbreak;
00866           }
00867         }
00868     } 
00869   }
00870   
00871  errout:
00872  eopbreak:
00873   return(0);
00874 }
00875 
00876 
00877 vorbis_func_residue residue0_exportbundle={
00878   NULL,
00879   &res0_unpack,
00880   &res0_look,
00881   &res0_free_info,
00882   &res0_free_look,
00883   NULL,
00884   NULL,
00885   &res0_inverse
00886 };
00887 
00888 vorbis_func_residue residue1_exportbundle={
00889   &res0_pack,
00890   &res0_unpack,
00891   &res0_look,
00892   &res0_free_info,
00893   &res0_free_look,
00894   &res1_class,
00895   &res1_forward,
00896   &res1_inverse
00897 };
00898 
00899 vorbis_func_residue residue2_exportbundle={
00900   &res0_pack,
00901   &res0_unpack,
00902   &res0_look,
00903   &res0_free_info,
00904   &res0_free_look,
00905   &res2_class,
00906   &res2_forward,
00907   &res2_inverse
00908 };
00909 

Generated by  doxygen 1.6.2