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