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