Remove warnings about unused or uninitialized variables.
[platform/upstream/libvorbis.git] / lib / res0.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
9  * by the Xiph.Org Foundation http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: residue backend 0, 1 and 2 implementation
14  last mod: $Id$
15
16  ********************************************************************/
17
18 /* Slow, slow, slow, simpleminded and did I mention it was slow?  The
19    encode/decode loops are coded for clarity and performance is not
20    yet even a nagging little idea lurking in the shadows.  Oh and BTW,
21    it's slow. */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26 #include <ogg/ogg.h>
27 #include "vorbis/codec.h"
28 #include "codec_internal.h"
29 #include "registry.h"
30 #include "codebook.h"
31 #include "misc.h"
32 #include "os.h"
33
34 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
35 #include <stdio.h>
36 #endif 
37
38 typedef struct {
39   vorbis_info_residue0 *info;
40   
41   int         parts;
42   int         stages;
43   codebook   *fullbooks;
44   codebook   *phrasebook;
45   codebook ***partbooks;
46
47   int         partvals;
48   int       **decodemap;
49
50   long      postbits;
51   long      phrasebits;
52   long      frames;
53
54 #if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
55   int        train_seq;
56   long      *training_data[8][64];
57   float      training_max[8][64];
58   float      training_min[8][64];
59   float     tmin;
60   float     tmax;
61 #endif
62
63 } vorbis_look_residue0;
64
65 void res0_free_info(vorbis_info_residue *i){
66   vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
67   if(info){
68     memset(info,0,sizeof(*info));
69     _ogg_free(info);
70   }
71 }
72
73 void res0_free_look(vorbis_look_residue *i){
74   int j;
75   if(i){
76
77     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
78
79 #ifdef TRAIN_RES
80     {
81       int j,k,l;
82       for(j=0;j<look->parts;j++){
83         /*fprintf(stderr,"partition %d: ",j);*/
84         for(k=0;k<8;k++)
85           if(look->training_data[k][j]){
86             char buffer[80];
87             FILE *of;
88             codebook *statebook=look->partbooks[j][k];
89             
90             /* long and short into the same bucket by current convention */
91             sprintf(buffer,"res_part%d_pass%d.vqd",j,k);
92             of=fopen(buffer,"a");
93
94             for(l=0;l<statebook->entries;l++)
95               fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
96             
97             fclose(of);
98             
99             /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
100               look->training_min[k][j],look->training_max[k][j]);*/
101
102             _ogg_free(look->training_data[k][j]);
103             look->training_data[k][j]=NULL;
104           }
105         /*fprintf(stderr,"\n");*/
106       }
107     }
108     fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
109
110     /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
111             (float)look->phrasebits/look->frames,
112             (float)look->postbits/look->frames,
113             (float)(look->postbits+look->phrasebits)/look->frames);*/
114 #endif
115
116
117     /*vorbis_info_residue0 *info=look->info;
118
119     fprintf(stderr,
120             "%ld frames encoded in %ld phrasebits and %ld residue bits "
121             "(%g/frame) \n",look->frames,look->phrasebits,
122             look->resbitsflat,
123             (look->phrasebits+look->resbitsflat)/(float)look->frames);
124     
125     for(j=0;j<look->parts;j++){
126       long acc=0;
127       fprintf(stderr,"\t[%d] == ",j);
128       for(k=0;k<look->stages;k++)
129         if((info->secondstages[j]>>k)&1){
130           fprintf(stderr,"%ld,",look->resbits[j][k]);
131           acc+=look->resbits[j][k];
132         }
133
134       fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
135               acc?(float)acc/(look->resvals[j]*info->grouping):0);
136     }
137     fprintf(stderr,"\n");*/
138
139     for(j=0;j<look->parts;j++)
140       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
141     _ogg_free(look->partbooks);
142     for(j=0;j<look->partvals;j++)
143       _ogg_free(look->decodemap[j]);
144     _ogg_free(look->decodemap);
145
146     memset(look,0,sizeof(*look));
147     _ogg_free(look);
148   }
149 }
150
151 static int ilog(unsigned int v){
152   int ret=0;
153   while(v){
154     ret++;
155     v>>=1;
156   }
157   return(ret);
158 }
159
160 static int icount(unsigned int v){
161   int ret=0;
162   while(v){
163     ret+=v&1;
164     v>>=1;
165   }
166   return(ret);
167 }
168
169
170 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
171   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
172   int j,acc=0;
173   oggpack_write(opb,info->begin,24);
174   oggpack_write(opb,info->end,24);
175
176   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and 
177                                              code with a partitioned book */
178   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
179   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
180
181   /* secondstages is a bitmask; as encoding progresses pass by pass, a
182      bitmask of one indicates this partition class has bits to write
183      this pass */
184   for(j=0;j<info->partitions;j++){
185     if(ilog(info->secondstages[j])>3){
186       /* yes, this is a minor hack due to not thinking ahead */
187       oggpack_write(opb,info->secondstages[j],3); 
188       oggpack_write(opb,1,1);
189       oggpack_write(opb,info->secondstages[j]>>3,5); 
190     }else
191       oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
192     acc+=icount(info->secondstages[j]);
193   }
194   for(j=0;j<acc;j++)
195     oggpack_write(opb,info->booklist[j],8);
196
197 }
198
199 /* vorbis_info is for range checking */
200 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
201   int j,acc=0;
202   vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
203   codec_setup_info     *ci=vi->codec_setup;
204
205   info->begin=oggpack_read(opb,24);
206   info->end=oggpack_read(opb,24);
207   info->grouping=oggpack_read(opb,24)+1;
208   info->partitions=oggpack_read(opb,6)+1;
209   info->groupbook=oggpack_read(opb,8);
210
211   for(j=0;j<info->partitions;j++){
212     int cascade=oggpack_read(opb,3);
213     if(oggpack_read(opb,1))
214       cascade|=(oggpack_read(opb,5)<<3);
215     info->secondstages[j]=cascade;
216
217     acc+=icount(cascade);
218   }
219   for(j=0;j<acc;j++)
220     info->booklist[j]=oggpack_read(opb,8);
221
222   if(info->groupbook>=ci->books)goto errout;
223   for(j=0;j<acc;j++)
224     if(info->booklist[j]>=ci->books)goto errout;
225
226   return(info);
227  errout:
228   res0_free_info(info);
229   return(NULL);
230 }
231
232 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
233                                vorbis_info_residue *vr){
234   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
235   vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
236   codec_setup_info     *ci=vd->vi->codec_setup;
237
238   int j,k,acc=0;
239   int dim;
240   int maxstage=0;
241   look->info=info;
242
243   look->parts=info->partitions;
244   look->fullbooks=ci->fullbooks;
245   look->phrasebook=ci->fullbooks+info->groupbook;
246   dim=look->phrasebook->dim;
247
248   look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
249
250   for(j=0;j<look->parts;j++){
251     int stages=ilog(info->secondstages[j]);
252     if(stages){
253       if(stages>maxstage)maxstage=stages;
254       look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
255       for(k=0;k<stages;k++)
256         if(info->secondstages[j]&(1<<k)){
257           look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
258 #ifdef TRAIN_RES
259           look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
260                                            sizeof(***look->training_data));
261 #endif
262         }
263     }
264   }
265
266   look->partvals=rint(pow((float)look->parts,(float)dim));
267   look->stages=maxstage;
268   look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
269   for(j=0;j<look->partvals;j++){
270     long val=j;
271     long mult=look->partvals/look->parts;
272     look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
273     for(k=0;k<dim;k++){
274       long deco=val/mult;
275       val-=deco*mult;
276       mult/=look->parts;
277       look->decodemap[j][k]=deco;
278     }
279   }
280 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
281   {
282     static int train_seq=0;
283     look->train_seq=train_seq++;
284   }
285 #endif
286   return(look);
287 }
288
289 /* break an abstraction and copy some code for performance purposes */
290 static int local_book_besterror(codebook *book,float *a){
291   int dim=book->dim,i,k,o;
292   int best=0;
293   encode_aux_threshmatch *tt=book->c->thresh_tree;
294
295   /* find the quant val of each scalar */
296   for(k=0,o=dim;k<dim;++k){
297     float val=a[--o];
298     i=tt->threshvals>>1;
299
300     if(val<tt->quantthresh[i]){      
301       if(val<tt->quantthresh[i-1]){
302         for(--i;i>0;--i)
303           if(val>=tt->quantthresh[i-1])
304             break;
305       }
306     }else{
307       
308       for(++i;i<tt->threshvals-1;++i)
309         if(val<tt->quantthresh[i])break;
310       
311     }
312
313     best=(best*tt->quantvals)+tt->quantmap[i];
314   }
315   /* regular lattices are easy :-) */
316   
317   if(book->c->lengthlist[best]<=0){
318     const static_codebook *c=book->c;
319     int i,j;
320     float bestf=0.f;
321     float *e=book->valuelist;
322     best=-1;
323     for(i=0;i<book->entries;i++){
324       if(c->lengthlist[i]>0){
325         float this=0.f;
326         for(j=0;j<dim;j++){
327           float val=(e[j]-a[j]);
328           this+=val*val;
329         }
330         if(best==-1 || this<bestf){
331           bestf=this;
332           best=i;
333         }
334       }
335       e+=dim;
336     }
337   }
338
339   if(best>-1){
340     float *ptr=book->valuelist+best*dim;
341     for(i=0;i<dim;i++)
342       *a++ -= *ptr++;
343   }
344
345   return(best);
346 }
347
348 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
349                        codebook *book,long *acc){
350   int i,bits=0;
351   int dim=book->dim;
352   int step=n/dim;
353
354   for(i=0;i<step;i++){
355     int entry=local_book_besterror(book,vec+i*dim);
356
357 #ifdef TRAIN_RES
358     if(entry>0)
359       acc[entry]++;
360 #endif
361       
362     bits+=vorbis_book_encode(book,entry,opb);
363   
364   }
365
366   return(bits);
367 }
368
369 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
370                        float **in,int ch){
371   long i,j,k;
372   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
373   vorbis_info_residue0 *info=look->info;
374   vorbis_info           *vi=vb->vd->vi;
375
376   /* move all this setup out later */
377   int samples_per_partition=info->grouping;
378   int possible_partitions=info->partitions;
379   int n=info->end-info->begin;
380   
381   int partvals=n/samples_per_partition;
382   long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
383   float scale=100./samples_per_partition;
384   
385   /* we find the partition type for each partition of each
386      channel.  We'll go back and do the interleaved encoding in a
387      bit.  For now, clarity */
388   
389   for(i=0;i<ch;i++){
390     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
391     memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
392   }
393   
394   for(i=0;i<partvals;i++){
395     int offset=i*samples_per_partition+info->begin;
396     for(j=0;j<ch;j++){
397       float max=0.;
398       float ent=0.;
399       for(k=0;k<samples_per_partition;k++){
400         if(fabs(in[j][offset+k])>max)max=fabs(in[j][offset+k]);
401         ent+=fabs(rint(in[j][offset+k]));
402       }
403       ent*=scale;
404       
405       for(k=0;k<possible_partitions-1;k++)
406         if(max<=info->classmetric1[k] &&
407            (info->classmetric2[k]<0 || (int)ent<info->classmetric2[k]))
408           break;
409       
410       partword[j][i]=k;  
411     }
412   }
413   
414 #ifdef TRAIN_RESAUX
415   {
416     FILE *of;
417     char buffer[80];
418     
419     for(i=0;i<ch;i++){
420       sprintf(buffer,"resaux_%d.vqd",look->train_seq);
421       of=fopen(buffer,"a");
422       for(j=0;j<partvals;j++)
423         fprintf(of,"%ld, ",partword[i][j]);
424       fprintf(of,"\n");
425       fclose(of);
426     }
427   }
428 #endif
429   look->frames++;
430   
431   return(partword);
432 }
433
434 /* designed for stereo or other modes where the partition size is an
435    integer multiple of the number of channels encoded in the current
436    submap */
437 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in,
438                       int ch){
439   long i,j,k,l;
440   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
441   vorbis_info_residue0 *info=look->info;
442
443   /* move all this setup out later */
444   int samples_per_partition=info->grouping;
445   int possible_partitions=info->partitions;
446   int n=info->end-info->begin;
447
448   int partvals=n/samples_per_partition;
449   long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
450   
451 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
452   FILE *of;
453   char buffer[80];
454 #endif
455   
456   partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
457   memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
458   
459   for(i=0,l=info->begin/ch;i<partvals;i++){
460     float magmax=0.f;
461     float angmax=0.f;
462     for(j=0;j<samples_per_partition;j+=ch){
463       if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]);
464       for(k=1;k<ch;k++)
465         if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]);
466         l++;
467     }
468     
469     for(j=0;j<possible_partitions-1;j++)
470       if(magmax<=info->classmetric1[j] &&
471          angmax<=info->classmetric2[j])
472         break;
473     
474     partword[0][i]=j;
475     
476   }  
477   
478 #ifdef TRAIN_RESAUX
479   sprintf(buffer,"resaux_%d.vqd",look->train_seq);
480   of=fopen(buffer,"a");
481   for(i=0;i<partvals;i++)
482     fprintf(of,"%ld, ",partword[0][i]);
483   fprintf(of,"\n");
484   fclose(of);
485 #endif
486   
487   look->frames++;
488   
489   return(partword);
490 }
491
492 static int _01forward(oggpack_buffer *opb,
493                       vorbis_block *vb,vorbis_look_residue *vl,
494                       float **in,int ch,
495                       long **partword,
496                       int (*encode)(oggpack_buffer *,float *,int,
497                                     codebook *,long *)){
498   long i,j,k,s;
499   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
500   vorbis_info_residue0 *info=look->info;
501
502   vorbis_dsp_state      *vd=vb->vd;
503
504   /* move all this setup out later */
505   int samples_per_partition=info->grouping;
506   int possible_partitions=info->partitions;
507   int partitions_per_word=look->phrasebook->dim;
508   int n=info->end-info->begin;
509
510   int partvals=n/samples_per_partition;
511   long resbits[128];
512   long resvals[128];
513   
514 #ifdef TRAIN_RES
515   for(i=0;i<ch;i++)
516     for(j=info->begin;j<end;j++){
517       if(in[i][j]>look->tmax)look->tmax=in[i][j];
518       if(in[i][j]<look->tmin)look->tmin=in[i][j];
519     }
520 #endif
521   
522   memset(resbits,0,sizeof(resbits));
523   memset(resvals,0,sizeof(resvals));
524   
525   /* we code the partition words for each channel, then the residual
526      words for a partition per channel until we've written all the
527      residual words for that partition word.  Then write the next
528      partition channel words... */
529   
530   for(s=0;s<look->stages;s++){
531     
532     for(i=0;i<partvals;){
533       
534       /* first we encode a partition codeword for each channel */
535       if(s==0){
536         for(j=0;j<ch;j++){
537           long val=partword[j][i];
538           for(k=1;k<partitions_per_word;k++){
539             val*=possible_partitions;
540             if(i+k<partvals)
541               val+=partword[j][i+k];
542           }     
543           
544           /* training hack */
545           if(val<look->phrasebook->entries)
546             look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
547 #if 0 /*def TRAIN_RES*/
548           else
549             fprintf(stderr,"!");
550 #endif
551           
552         }
553       }
554       
555       /* now we encode interleaved residual values for the partitions */
556       for(k=0;k<partitions_per_word && i<partvals;k++,i++){
557         long offset=i*samples_per_partition+info->begin;
558           
559         for(j=0;j<ch;j++){
560           if(s==0)resvals[partword[j][i]]+=samples_per_partition;
561           if(info->secondstages[partword[j][i]]&(1<<s)){
562             codebook *statebook=look->partbooks[partword[j][i]][s];
563             if(statebook){
564               int ret;
565               long *accumulator=NULL;
566               
567 #ifdef TRAIN_RES
568               accumulator=look->training_data[s][partword[j][i]];
569               {
570                 int l;
571                 float *samples=in[j]+offset;
572                 for(l=0;l<samples_per_partition;l++){
573                   if(samples[l]<look->training_min[s][partword[j][i]])
574                     look->training_min[s][partword[j][i]]=samples[l];
575                   if(samples[l]>look->training_max[s][partword[j][i]])
576                     look->training_max[s][partword[j][i]]=samples[l];
577                 }
578               }
579 #endif
580               
581               ret=encode(opb,in[j]+offset,samples_per_partition,
582                          statebook,accumulator);
583               
584               look->postbits+=ret;
585               resbits[partword[j][i]]+=ret;
586             }
587           }
588         }
589       }
590     }
591   }
592   
593   /*{
594     long total=0;
595     long totalbits=0;
596     fprintf(stderr,"%d :: ",vb->mode);
597     for(k=0;k<possible_partitions;k++){
598     fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
599     total+=resvals[k];
600     totalbits+=resbits[k];
601     }
602     
603     fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
604     }*/
605
606   return(0);
607 }
608
609 /* a truncated packet here just means 'stop working'; it's not an error */
610 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
611                       float **in,int ch,
612                       long (*decodepart)(codebook *, float *, 
613                                          oggpack_buffer *,int)){
614
615   long i,j,k,l,s;
616   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
617   vorbis_info_residue0 *info=look->info;
618
619   /* move all this setup out later */
620   int samples_per_partition=info->grouping;
621   int partitions_per_word=look->phrasebook->dim;
622   int max=vb->pcmend>>1;
623   int end=(info->end<max?info->end:max);
624   int n=end-info->begin;
625   
626   if(n>0){
627     int partvals=n/samples_per_partition;
628     int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
629     int ***partword=alloca(ch*sizeof(*partword));
630     
631     for(j=0;j<ch;j++)
632       partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
633     
634     for(s=0;s<look->stages;s++){
635       
636       /* each loop decodes on partition codeword containing 
637          partitions_per_word partitions */
638       for(i=0,l=0;i<partvals;l++){
639         if(s==0){
640           /* fetch the partition word for each channel */
641           for(j=0;j<ch;j++){
642             int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
643             
644             if(temp==-1)goto eopbreak;
645             partword[j][l]=look->decodemap[temp];
646             if(partword[j][l]==NULL)goto errout;
647           }
648         }
649         
650         /* now we decode residual values for the partitions */
651         for(k=0;k<partitions_per_word && i<partvals;k++,i++)
652           for(j=0;j<ch;j++){
653             long offset=info->begin+i*samples_per_partition;
654             if(info->secondstages[partword[j][l][k]]&(1<<s)){
655               codebook *stagebook=look->partbooks[partword[j][l][k]][s];
656               if(stagebook){
657                 if(decodepart(stagebook,in[j]+offset,&vb->opb,
658                               samples_per_partition)==-1)goto eopbreak;
659               }
660             }
661           }
662       } 
663     }
664   }
665  errout:
666  eopbreak:
667   return(0);
668 }
669
670 #if 0
671 /* residue 0 and 1 are just slight variants of one another. 0 is
672    interleaved, 1 is not */
673 long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
674                   float **in,int *nonzero,int ch){
675   /* we encode only the nonzero parts of a bundle */
676   int i,used=0;
677   for(i=0;i<ch;i++)
678     if(nonzero[i])
679       in[used++]=in[i];
680   if(used)
681     /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
682     return(_01class(vb,vl,in,used));
683   else
684     return(0);
685 }
686
687 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
688                  float **in,float **out,int *nonzero,int ch,
689                  long **partword){
690   /* we encode only the nonzero parts of a bundle */
691   int i,j,used=0,n=vb->pcmend/2;
692   for(i=0;i<ch;i++)
693     if(nonzero[i]){
694       if(out)
695         for(j=0;j<n;j++)
696           out[i][j]+=in[i][j];
697       in[used++]=in[i];
698     }
699   if(used){
700     int ret=_01forward(vb,vl,in,used,partword,
701                       _interleaved_encodepart);
702     if(out){
703       used=0;
704       for(i=0;i<ch;i++)
705         if(nonzero[i]){
706           for(j=0;j<n;j++)
707             out[i][j]-=in[used][j];
708           used++;
709         }
710     }
711     return(ret);
712   }else{
713     return(0);
714   }
715 }
716 #endif
717
718 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
719                  float **in,int *nonzero,int ch){
720   int i,used=0;
721   for(i=0;i<ch;i++)
722     if(nonzero[i])
723       in[used++]=in[i];
724   if(used)
725     return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
726   else
727     return(0);
728 }
729
730 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
731                  float **in,float **out,int *nonzero,int ch,
732                  long **partword){
733   int i,j,used=0,n=vb->pcmend/2;
734   for(i=0;i<ch;i++)
735     if(nonzero[i]){
736       if(out)
737         for(j=0;j<n;j++)
738           out[i][j]+=in[i][j];
739       in[used++]=in[i];
740     }
741
742   if(used){
743     int ret=_01forward(opb,vb,vl,in,used,partword,_encodepart);
744     if(out){
745       used=0;
746       for(i=0;i<ch;i++)
747         if(nonzero[i]){
748           for(j=0;j<n;j++)
749             out[i][j]-=in[used][j];
750           used++;
751         }
752     }
753     return(ret);
754   }else{
755     return(0);
756   }
757 }
758
759 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
760                   float **in,int *nonzero,int ch){
761   int i,used=0;
762   for(i=0;i<ch;i++)
763     if(nonzero[i])
764       in[used++]=in[i];
765   if(used)
766     return(_01class(vb,vl,in,used));
767   else
768     return(0);
769 }
770
771 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
772                  float **in,int *nonzero,int ch){
773   int i,used=0;
774   for(i=0;i<ch;i++)
775     if(nonzero[i])
776       in[used++]=in[i];
777   if(used)
778     return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
779   else
780     return(0);
781 }
782
783 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
784                   float **in,int *nonzero,int ch){
785   int i,used=0;
786   for(i=0;i<ch;i++)
787     if(nonzero[i])used++;
788   if(used)
789     return(_2class(vb,vl,in,ch));
790   else
791     return(0);
792 }
793
794 /* res2 is slightly more different; all the channels are interleaved
795    into a single vector and encoded. */
796
797 int res2_forward(oggpack_buffer *opb,
798                  vorbis_block *vb,vorbis_look_residue *vl,
799                  float **in,float **out,int *nonzero,int ch,
800                  long **partword){
801   long i,j,k,n=vb->pcmend/2,used=0;
802
803   /* don't duplicate the code; use a working vector hack for now and
804      reshape ourselves into a single channel res1 */
805   /* ugly; reallocs for each coupling pass :-( */
806   float *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
807   for(i=0;i<ch;i++){
808     float *pcm=in[i];
809     if(nonzero[i])used++;
810     for(j=0,k=i;j<n;j++,k+=ch)
811       work[k]=pcm[j];
812   }
813   
814   if(used){
815     int ret=_01forward(opb,vb,vl,&work,1,partword,_encodepart);
816     /* update the sofar vector */
817     if(out){
818       for(i=0;i<ch;i++){
819         float *pcm=in[i];
820         float *sofar=out[i];
821         for(j=0,k=i;j<n;j++,k+=ch)
822           sofar[j]+=pcm[j]-work[k];
823         
824       }
825     }
826     return(ret);
827   }else{
828     return(0);
829   }
830 }
831
832 /* duplicate code here as speed is somewhat more important */
833 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
834                  float **in,int *nonzero,int ch){
835   long i,k,l,s;
836   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
837   vorbis_info_residue0 *info=look->info;
838
839   /* move all this setup out later */
840   int samples_per_partition=info->grouping;
841   int partitions_per_word=look->phrasebook->dim;
842   int max=(vb->pcmend*ch)>>1;
843   int end=(info->end<max?info->end:max);
844   int n=end-info->begin;
845
846   if(n>0){
847     int partvals=n/samples_per_partition;
848     int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
849     int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
850     
851     for(i=0;i<ch;i++)if(nonzero[i])break;
852     if(i==ch)return(0); /* no nonzero vectors */
853     
854     for(s=0;s<look->stages;s++){
855       for(i=0,l=0;i<partvals;l++){
856         
857         if(s==0){
858           /* fetch the partition word */
859           int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
860           if(temp==-1)goto eopbreak;
861           partword[l]=look->decodemap[temp];
862           if(partword[l]==NULL)goto errout;
863         }
864         
865         /* now we decode residual values for the partitions */
866         for(k=0;k<partitions_per_word && i<partvals;k++,i++)
867           if(info->secondstages[partword[l][k]]&(1<<s)){
868             codebook *stagebook=look->partbooks[partword[l][k]][s];
869             
870             if(stagebook){
871               if(vorbis_book_decodevv_add(stagebook,in,
872                                           i*samples_per_partition+info->begin,ch,
873                                           &vb->opb,samples_per_partition)==-1)
874                 goto eopbreak;
875             }
876           }
877       } 
878     }
879   }
880  errout:
881  eopbreak:
882   return(0);
883 }
884
885
886 vorbis_func_residue residue0_exportbundle={
887   NULL,
888   &res0_unpack,
889   &res0_look,
890   &res0_free_info,
891   &res0_free_look,
892   NULL,
893   NULL,
894   &res0_inverse
895 };
896
897 vorbis_func_residue residue1_exportbundle={
898   &res0_pack,
899   &res0_unpack,
900   &res0_look,
901   &res0_free_info,
902   &res0_free_look,
903   &res1_class,
904   &res1_forward,
905   &res1_inverse
906 };
907
908 vorbis_func_residue residue2_exportbundle={
909   &res0_pack,
910   &res0_unpack,
911   &res0_look,
912   &res0_free_info,
913   &res0_free_look,
914   &res2_class,
915   &res2_forward,
916   &res2_inverse
917 };
918