examples/sfexamples/oggvorbiscodec/src/tremor/res012.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
00004  *                                                                  *
00005  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00006  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00007  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00008  *                                                                  *
00009  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
00010  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
00011  *                                                                  *
00012  ********************************************************************
00013 
00014  function: residue backend 0, 1 and 2 implementation
00015 
00016  ********************************************************************/
00017 
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #include <math.h>
00021 #include "ogg.h"
00022 #include "ivorbiscodec.h"
00023 #include "codec_internal.h"
00024 #include "registry.h"
00025 #include "codebook.h"
00026 #include "misc.h"
00027 #include "os.h"
00028 
00029 typedef struct {
00030   vorbis_info_residue0 *info;
00031   int         map;
00032   
00033   int         parts;
00034   int         stages;
00035   codebook   *fullbooks;
00036   codebook   *phrasebook;
00037   codebook ***partbooks;
00038 
00039   int         partvals;
00040   int       **decodemap;
00041 
00042 } vorbis_look_residue0;
00043 
00044 void res0_free_info(vorbis_info_residue *i){
00045   vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
00046   if(info){
00047     memset(info,0,sizeof(*info));
00048     _ogg_free(info);
00049   }
00050 }
00051 
00052 void res0_free_look(vorbis_look_residue *i){
00053   int j;
00054   if(i){
00055 
00056     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
00057 
00058     for(j=0;j<look->parts;j++)
00059       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
00060     _ogg_free(look->partbooks);
00061     for(j=0;j<look->partvals;j++)
00062       _ogg_free(look->decodemap[j]);
00063     _ogg_free(look->decodemap);
00064 
00065     memset(look,0,sizeof(*look));
00066     _ogg_free(look);
00067   }
00068 }
00069 
00070 static int ilog(unsigned int v){
00071   int ret=0;
00072   while(v){
00073     ret++;
00074     v>>=1;
00075   }
00076   return(ret);
00077 }
00078 
00079 static int icount(unsigned int v){
00080   int ret=0;
00081   while(v){
00082     ret+=v&1;
00083     v>>=1;
00084   }
00085   return(ret);
00086 }
00087 
00088 /* vorbis_info is for range checking */
00089 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
00090   int j,acc=0;
00091   vorbis_info_residue0 *info=(vorbis_info_residue0 *)_ogg_calloc(1,sizeof(*info));
00092   codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
00093 
00094   info->begin=oggpack_read(opb,24);
00095   info->end=oggpack_read(opb,24);
00096   info->grouping=oggpack_read(opb,24)+1;
00097   info->partitions=oggpack_read(opb,6)+1;
00098   info->groupbook=oggpack_read(opb,8);
00099 
00100   for(j=0;j<info->partitions;j++){
00101     int cascade=oggpack_read(opb,3);
00102     if(oggpack_read(opb,1))
00103       cascade|=(oggpack_read(opb,5)<<3);
00104     info->secondstages[j]=cascade;
00105 
00106     acc+=icount(cascade);
00107   }
00108   for(j=0;j<acc;j++)
00109     info->booklist[j]=oggpack_read(opb,8);
00110 
00111   if(info->groupbook>=ci->books)goto errout;
00112   for(j=0;j<acc;j++)
00113     if(info->booklist[j]>=ci->books)goto errout;
00114 
00115   return(info);
00116  errout:
00117   res0_free_info(info);
00118   return(NULL);
00119 }
00120 
00121 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
00122                           vorbis_info_residue *vr){
00123   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
00124   vorbis_look_residue0 *look=(vorbis_look_residue0 *)_ogg_calloc(1,sizeof(*look));
00125   codec_setup_info     *ci=(codec_setup_info *)vd->vi->codec_setup;
00126 
00127   int j,k,acc=0;
00128   int dim;
00129   int maxstage=0;
00130   look->info=info;
00131   look->map=vm->mapping;
00132 
00133   look->parts=info->partitions;
00134   look->fullbooks=ci->fullbooks;
00135   look->phrasebook=ci->fullbooks+info->groupbook;
00136   dim=look->phrasebook->dim;
00137 
00138   look->partbooks=(codebook ***)_ogg_calloc(look->parts,sizeof(*look->partbooks));
00139 
00140   for(j=0;j<look->parts;j++){
00141     int stages=ilog(info->secondstages[j]);
00142     if(stages){
00143       if(stages>maxstage)maxstage=stages;
00144       look->partbooks[j]=(codebook **)_ogg_calloc(stages,sizeof(*look->partbooks[j]));
00145       for(k=0;k<stages;k++)
00146         if(info->secondstages[j]&(1<<k)){
00147           look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
00148 #ifdef TRAIN_RES
00149           look->training_data[k][j]=calloc(look->partbooks[j][k]->entries,
00150                                            sizeof(***look->training_data));
00151 #endif
00152         }
00153     }
00154   }
00155 
00156   look->partvals=look->parts;
00157   for(j=1;j<dim;j++)look->partvals*=look->parts;
00158   look->stages=maxstage;
00159   look->decodemap=(int **)_ogg_malloc(look->partvals*sizeof(*look->decodemap));
00160   for(j=0;j<look->partvals;j++){
00161     long val=j;
00162     long mult=look->partvals/look->parts;
00163     look->decodemap[j]=(int *)_ogg_malloc(dim*sizeof(*look->decodemap[j]));
00164     for(k=0;k<dim;k++){
00165       long deco=val/mult;
00166       val-=deco*mult;
00167       mult/=look->parts;
00168       look->decodemap[j][k]=deco;
00169     }
00170   }
00171 
00172   return(look);
00173 }
00174 
00175 void *_vorbis_block_alloc(vorbis_block *vb,long bytes);
00176 /* a truncated packet here just means 'stop working'; it's not an error */
00177 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
00178                       ogg_int32_t **in,int ch,
00179                       long (*decodepart)(codebook *, ogg_int32_t *, 
00180                                          oggpack_buffer *,int,int)){
00181 
00182   long i,j,k,l,s;
00183   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
00184   vorbis_info_residue0 *info=look->info;
00185 
00186   /* move all this setup out later */
00187   int samples_per_partition=info->grouping;
00188   int partitions_per_word=look->phrasebook->dim;
00189   int n=info->end-info->begin;
00190   
00191   int partvals=n/samples_per_partition;
00192   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
00193   int ***partword=(int ***)_ogg_malloc(ch*sizeof(*partword));
00194   for(j=0;j<ch;j++)
00195     partword[j]=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
00196 
00197   for(s=0;s<look->stages;s++){
00198 
00199     /* each loop decodes on partition codeword containing 
00200        partitions_pre_word partitions */
00201     for(i=0,l=0;i<partvals;l++){
00202       if(s==0){
00203         /* fetch the partition word for each channel */
00204         for(j=0;j<ch;j++){
00205           int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
00206           if(temp==-1)goto eopbreak;
00207           partword[j][l]=look->decodemap[temp];
00208           if(partword[j][l]==NULL)goto errout;
00209         }
00210       }
00211       
00212       /* now we decode residual values for the partitions */
00213       for(k=0;k<partitions_per_word && i<partvals;k++,i++)
00214         for(j=0;j<ch;j++){
00215           long offset=info->begin+i*samples_per_partition;
00216           if(info->secondstages[partword[j][l][k]]&(1<<s)){
00217             codebook *stagebook=look->partbooks[partword[j][l][k]][s];
00218             if(stagebook){
00219               if(decodepart(stagebook,in[j]+offset,&vb->opb,
00220                             samples_per_partition,-8)==-1)goto eopbreak;
00221             }
00222           }
00223         }
00224     } 
00225   }
00226   
00227  errout:
00228  eopbreak:
00229  _ogg_free(partword);
00230   return(0);
00231 }
00232 
00233 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
00234                  ogg_int32_t **in,int *nonzero,int ch){
00235   int i,used=0;
00236   for(i=0;i<ch;i++)
00237     if(nonzero[i])
00238       in[used++]=in[i];
00239   if(used)
00240     return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
00241   else
00242     return(0);
00243 }
00244 
00245 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
00246                  ogg_int32_t **in,int *nonzero,int ch){
00247   int i,used=0;
00248   for(i=0;i<ch;i++)
00249     if(nonzero[i])
00250       in[used++]=in[i];
00251   if(used)
00252     return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
00253   else
00254     return(0);
00255 }
00256 
00257 /* duplicate code here as speed is somewhat more important */
00258 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
00259                  ogg_int32_t **in,int *nonzero,int ch){
00260   long i,k,l,s;
00261   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
00262   vorbis_info_residue0 *info=look->info;
00263 
00264   /* move all this setup out later */
00265   int samples_per_partition=info->grouping;
00266   int partitions_per_word=look->phrasebook->dim;
00267   int n=info->end-info->begin;
00268 
00269   int partvals=n/samples_per_partition;
00270   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
00271   int **partword=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword));
00272   int beginoff=info->begin/ch;
00273 
00274   for(i=0;i<ch;i++)if(nonzero[i])break;
00275   if(i==ch)return(0); /* no nonzero vectors */
00276 
00277   samples_per_partition/=ch;
00278 
00279   for(s=0;s<look->stages;s++){
00280     for(i=0,l=0;i<partvals;l++){
00281 
00282       if(s==0){
00283         /* fetch the partition word */
00284         int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
00285         if(temp==-1)goto eopbreak;
00286         partword[l]=look->decodemap[temp];
00287         if(partword[l]==NULL)goto errout;
00288       }
00289 
00290       /* now we decode residual values for the partitions */
00291       for(k=0;k<partitions_per_word && i<partvals;k++,i++)
00292         if(info->secondstages[partword[l][k]]&(1<<s)){
00293           codebook *stagebook=look->partbooks[partword[l][k]][s];
00294           
00295           if(stagebook){
00296             if(vorbis_book_decodevv_add(stagebook,in,
00297                                         i*samples_per_partition+beginoff,ch,
00298                                         &vb->opb,
00299                                         samples_per_partition,-8)==-1)
00300               goto eopbreak;
00301           }
00302         }
00303     } 
00304   }
00305   
00306  errout:
00307  eopbreak:
00308   return(0);
00309 }
00310 
00311 
00312 const vorbis_func_residue residue0_exportbundle={
00313   &res0_unpack,
00314   &res0_look,
00315   &res0_free_info,
00316   &res0_free_look,
00317   &res0_inverse
00318 };
00319 
00320 const vorbis_func_residue residue1_exportbundle={
00321   &res0_unpack,
00322   &res0_look,
00323   &res0_free_info,
00324   &res0_free_look,
00325   &res1_inverse
00326 };
00327 
00328 const vorbis_func_residue residue2_exportbundle={
00329   &res0_unpack,
00330   &res0_look,
00331   &res0_free_info,
00332   &res0_free_look,
00333   &res2_inverse
00334 };

Generated by  doxygen 1.6.2