I had forgotten that the res2 top level interprets limits differently
[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$
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   {
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     acc[entry]++;
359 #endif
360
361     bits+=vorbis_book_encode(book,entry,opb);
362   }
363
364   return(bits);
365 }
366
367 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
368                        float **in,int ch){
369   long i,j,k;
370   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
371   vorbis_info_residue0 *info=look->info;
372   vorbis_info           *vi=vb->vd->vi;
373   codec_setup_info      *ci=vi->codec_setup;
374
375   /* move all this setup out later */
376   int samples_per_partition=info->grouping;
377   int possible_partitions=info->partitions;
378   int end=(info->end<vb->pcmend/2?info->end:vb->pcmend/2);
379   int n=end-info->begin;
380   
381   if(n>0){
382     int partvals=n/samples_per_partition;
383     long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
384     float scale=100./samples_per_partition;
385     
386     /* we find the partition type for each partition of each
387        channel.  We'll go back and do the interleaved encoding in a
388        bit.  For now, clarity */
389     
390     for(i=0;i<ch;i++){
391       partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
392       memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
393     }
394     
395     for(i=0;i<partvals;i++){
396       int offset=i*samples_per_partition+info->begin;
397       for(j=0;j<ch;j++){
398         float max=0.;
399         float ent=0.;
400         for(k=0;k<samples_per_partition;k++){
401           if(fabs(in[j][offset+k])>max)max=fabs(in[j][offset+k]);
402           ent+=fabs(rint(in[j][offset+k]));
403         }
404         ent*=scale;
405         
406         for(k=0;k<possible_partitions-1;k++)
407           if(max<=info->classmetric1[k] &&
408              (info->classmetric2[k]<0 || (int)ent<info->classmetric2[k]))
409             break;
410         
411         partword[j][i]=k;  
412       }
413     }
414     
415 #ifdef TRAIN_RESAUX
416     {
417       FILE *of;
418       char buffer[80];
419       
420       for(i=0;i<ch;i++){
421         sprintf(buffer,"resaux_%d.vqd",look->train_seq);
422         of=fopen(buffer,"a");
423         for(j=0;j<partvals;j++)
424           fprintf(of,"%ld, ",partword[i][j]);
425         fprintf(of,"\n");
426         fclose(of);
427       }
428     }
429 #endif
430     look->frames++;
431     
432     return(partword);
433   }else
434     return(0);
435 }
436
437 /* designed for stereo or other modes where the partition size is an
438    integer multiple of the number of channels encoded in the current
439    submap */
440 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in,
441                       int ch){
442   long i,j,k,l;
443   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
444   vorbis_info_residue0 *info=look->info;
445
446   /* move all this setup out later */
447   int samples_per_partition=info->grouping;
448   int possible_partitions=info->partitions;
449   int end=(info->end<vb->pcmend/2?info->end:vb->pcmend/2);
450   int n=end-info->begin;
451
452   if(n>0){
453     int partvals=n/samples_per_partition;
454     long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
455     
456 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
457     FILE *of;
458     char buffer[80];
459 #endif
460     
461     partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
462     memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
463     
464     for(i=0,l=info->begin/ch;i<partvals;i++){
465       float magmax=0.f;
466       float angmax=0.f;
467       for(j=0;j<samples_per_partition;j+=ch){
468         if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]);
469         for(k=1;k<ch;k++)
470           if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]);
471         l++;
472       }
473       
474       for(j=0;j<possible_partitions-1;j++)
475         if(magmax<=info->classmetric1[j] &&
476            angmax<=info->classmetric2[j])
477           break;
478       
479       partword[0][i]=j;
480       
481     }  
482     
483 #ifdef TRAIN_RESAUX
484     sprintf(buffer,"resaux_%d.vqd",look->train_seq);
485     of=fopen(buffer,"a");
486     for(i=0;i<partvals;i++)
487       fprintf(of,"%ld, ",partword[0][i]);
488     fprintf(of,"\n");
489     fclose(of);
490 #endif
491     
492     look->frames++;
493     
494     return(partword);
495   }else
496     return(0);
497 }
498
499 static int _01forward(oggpack_buffer *opb,
500                       vorbis_block *vb,vorbis_look_residue *vl,
501                       float **in,int ch,
502                       long **partword,
503                       int (*encode)(oggpack_buffer *,float *,int,
504                                     codebook *,long *)){
505   long i,j,k,s;
506   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
507   vorbis_info_residue0 *info=look->info;
508
509   vorbis_dsp_state      *vd=vb->vd;
510
511   /* move all this setup out later */
512   int samples_per_partition=info->grouping;
513   int possible_partitions=info->partitions;
514   int partitions_per_word=look->phrasebook->dim;
515   int end=(info->end<vb->pcmend/2?info->end:vb->pcmend/2);
516   int n=end-info->begin;
517
518   if(n>0){
519     int partvals=n/samples_per_partition;
520     long resbits[128];
521     long resvals[128];
522     
523 #ifdef TRAIN_RES
524     for(i=0;i<ch;i++)
525       for(j=info->begin;j<end;j++){
526         if(in[i][j]>look->tmax)look->tmax=in[i][j];
527         if(in[i][j]<look->tmin)look->tmin=in[i][j];
528       }
529 #endif
530     
531     memset(resbits,0,sizeof(resbits));
532     memset(resvals,0,sizeof(resvals));
533     
534     /* we code the partition words for each channel, then the residual
535        words for a partition per channel until we've written all the
536        residual words for that partition word.  Then write the next
537        partition channel words... */
538     
539     for(s=0;s<look->stages;s++){
540       
541       for(i=0;i<partvals;){
542         
543         /* first we encode a partition codeword for each channel */
544         if(s==0){
545           for(j=0;j<ch;j++){
546             long val=partword[j][i];
547             for(k=1;k<partitions_per_word;k++){
548               val*=possible_partitions;
549               if(i+k<partvals)
550                 val+=partword[j][i+k];
551             }   
552             
553             /* training hack */
554             if(val<look->phrasebook->entries)
555               look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
556 #if 0 /*def TRAIN_RES*/
557             else
558               fprintf(stderr,"!");
559 #endif
560             
561           }
562         }
563         
564         /* now we encode interleaved residual values for the partitions */
565         for(k=0;k<partitions_per_word && i<partvals;k++,i++){
566           long offset=i*samples_per_partition+info->begin;
567           
568           for(j=0;j<ch;j++){
569             if(s==0)resvals[partword[j][i]]+=samples_per_partition;
570             if(info->secondstages[partword[j][i]]&(1<<s)){
571               codebook *statebook=look->partbooks[partword[j][i]][s];
572               if(statebook){
573                 int ret;
574                 long *accumulator=NULL;
575                 
576 #ifdef TRAIN_RES
577                 accumulator=look->training_data[s][partword[j][i]];
578                 {
579                   int l;
580                   float *samples=in[j]+offset;
581                   for(l=0;l<samples_per_partition;l++){
582                     if(samples[l]<look->training_min[s][partword[j][i]])
583                       look->training_min[s][partword[j][i]]=samples[l];
584                     if(samples[l]>look->training_max[s][partword[j][i]])
585                       look->training_max[s][partword[j][i]]=samples[l];
586                   }
587                 }
588 #endif
589                 
590                 ret=encode(opb,in[j]+offset,samples_per_partition,
591                            statebook,accumulator);
592                 
593                 look->postbits+=ret;
594                 resbits[partword[j][i]]+=ret;
595               }
596             }
597           }
598         }
599       }
600     }
601     
602   /*{
603     long total=0;
604     long totalbits=0;
605     fprintf(stderr,"%d :: ",vb->mode);
606     for(k=0;k<possible_partitions;k++){
607     fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
608     total+=resvals[k];
609     totalbits+=resbits[k];
610     }
611     
612     fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
613     }*/
614   }
615   return(0);
616 }
617
618 /* a truncated packet here just means 'stop working'; it's not an error */
619 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
620                       float **in,int ch,
621                       long (*decodepart)(codebook *, float *, 
622                                          oggpack_buffer *,int)){
623
624   long i,j,k,l,s;
625   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
626   vorbis_info_residue0 *info=look->info;
627
628   /* move all this setup out later */
629   int samples_per_partition=info->grouping;
630   int partitions_per_word=look->phrasebook->dim;
631   int end=(info->end<vb->pcmend/2?info->end:vb->pcmend/2);
632   int n=end-info->begin;
633   
634   if(n>0){
635     int partvals=n/samples_per_partition;
636     int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
637     int ***partword=alloca(ch*sizeof(*partword));
638     
639     for(j=0;j<ch;j++)
640       partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
641     
642     for(s=0;s<look->stages;s++){
643       
644       /* each loop decodes on partition codeword containing 
645          partitions_per_word partitions */
646       for(i=0,l=0;i<partvals;l++){
647         if(s==0){
648           /* fetch the partition word for each channel */
649           for(j=0;j<ch;j++){
650             int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
651             
652             if(temp==-1)goto eopbreak;
653             partword[j][l]=look->decodemap[temp];
654             if(partword[j][l]==NULL)goto errout;
655           }
656         }
657         
658         /* now we decode residual values for the partitions */
659         for(k=0;k<partitions_per_word && i<partvals;k++,i++)
660           for(j=0;j<ch;j++){
661             long offset=info->begin+i*samples_per_partition;
662             if(info->secondstages[partword[j][l][k]]&(1<<s)){
663               codebook *stagebook=look->partbooks[partword[j][l][k]][s];
664               if(stagebook){
665                 if(decodepart(stagebook,in[j]+offset,&vb->opb,
666                               samples_per_partition)==-1)goto eopbreak;
667               }
668             }
669           }
670       } 
671     }
672   }
673  errout:
674  eopbreak:
675   return(0);
676 }
677
678 #if 0
679 /* residue 0 and 1 are just slight variants of one another. 0 is
680    interleaved, 1 is not */
681 long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
682                   float **in,int *nonzero,int ch){
683   /* we encode only the nonzero parts of a bundle */
684   int i,used=0;
685   for(i=0;i<ch;i++)
686     if(nonzero[i])
687       in[used++]=in[i];
688   if(used)
689     /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
690     return(_01class(vb,vl,in,used));
691   else
692     return(0);
693 }
694
695 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
696                  float **in,float **out,int *nonzero,int ch,
697                  long **partword){
698   /* we encode only the nonzero parts of a bundle */
699   int i,j,used=0,n=vb->pcmend/2;
700   for(i=0;i<ch;i++)
701     if(nonzero[i]){
702       if(out)
703         for(j=0;j<n;j++)
704           out[i][j]+=in[i][j];
705       in[used++]=in[i];
706     }
707   if(used){
708     int ret=_01forward(vb,vl,in,used,partword,
709                       _interleaved_encodepart);
710     if(out){
711       used=0;
712       for(i=0;i<ch;i++)
713         if(nonzero[i]){
714           for(j=0;j<n;j++)
715             out[i][j]-=in[used][j];
716           used++;
717         }
718     }
719     return(ret);
720   }else{
721     return(0);
722   }
723 }
724 #endif
725
726 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
727                  float **in,int *nonzero,int ch){
728   int i,used=0;
729   for(i=0;i<ch;i++)
730     if(nonzero[i])
731       in[used++]=in[i];
732   if(used)
733     return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
734   else
735     return(0);
736 }
737
738 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
739                  float **in,float **out,int *nonzero,int ch,
740                  long **partword){
741   int i,j,used=0,n=vb->pcmend/2;
742   for(i=0;i<ch;i++)
743     if(nonzero[i]){
744       if(out)
745         for(j=0;j<n;j++)
746           out[i][j]+=in[i][j];
747       in[used++]=in[i];
748     }
749
750   if(used){
751     int ret=_01forward(opb,vb,vl,in,used,partword,_encodepart);
752     if(out){
753       used=0;
754       for(i=0;i<ch;i++)
755         if(nonzero[i]){
756           for(j=0;j<n;j++)
757             out[i][j]-=in[used][j];
758           used++;
759         }
760     }
761     return(ret);
762   }else{
763     return(0);
764   }
765 }
766
767 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
768                   float **in,int *nonzero,int ch){
769   int i,used=0;
770   for(i=0;i<ch;i++)
771     if(nonzero[i])
772       in[used++]=in[i];
773   if(used)
774     return(_01class(vb,vl,in,used));
775   else
776     return(0);
777 }
778
779 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
780                  float **in,int *nonzero,int ch){
781   int i,used=0;
782   for(i=0;i<ch;i++)
783     if(nonzero[i])
784       in[used++]=in[i];
785   if(used)
786     return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
787   else
788     return(0);
789 }
790
791 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
792                   float **in,int *nonzero,int ch){
793   int i,used=0;
794   for(i=0;i<ch;i++)
795     if(nonzero[i])used++;
796   if(used)
797     return(_2class(vb,vl,in,ch));
798   else
799     return(0);
800 }
801
802 /* res2 is slightly more different; all the channels are interleaved
803    into a single vector and encoded. */
804
805 int res2_forward(oggpack_buffer *opb,
806                  vorbis_block *vb,vorbis_look_residue *vl,
807                  float **in,float **out,int *nonzero,int ch,
808                  long **partword){
809   long i,j,k,n=vb->pcmend/2,used=0;
810
811   /* don't duplicate the code; use a working vector hack for now and
812      reshape ourselves into a single channel res1 */
813   /* ugly; reallocs for each coupling pass :-( */
814   float *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
815   for(i=0;i<ch;i++){
816     float *pcm=in[i];
817     if(nonzero[i])used++;
818     for(j=0,k=i;j<n;j++,k+=ch)
819       work[k]=pcm[j];
820   }
821   
822   if(used){
823     int ret=_01forward(opb,vb,vl,&work,1,partword,_encodepart);
824     /* update the sofar vector */
825     if(out){
826       for(i=0;i<ch;i++){
827         float *pcm=in[i];
828         float *sofar=out[i];
829         for(j=0,k=i;j<n;j++,k+=ch)
830           sofar[j]+=pcm[j]-work[k];
831         
832       }
833     }
834     return(ret);
835   }else{
836     return(0);
837   }
838 }
839
840 /* duplicate code here as speed is somewhat more important */
841 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
842                  float **in,int *nonzero,int ch){
843   long i,k,l,s;
844   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
845   vorbis_info_residue0 *info=look->info;
846
847   /* move all this setup out later */
848   int samples_per_partition=info->grouping;
849   int partitions_per_word=look->phrasebook->dim;
850   int max=(vb->pcmend*ch)>>1;
851   int end=(info->end<max?info->end:max);
852   int n=info->end-info->begin;
853
854   if(n>0){
855     int partvals=n/samples_per_partition;
856     int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
857     int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
858     
859     for(i=0;i<ch;i++)if(nonzero[i])break;
860     if(i==ch)return(0); /* no nonzero vectors */
861     
862     for(s=0;s<look->stages;s++){
863       for(i=0,l=0;i<partvals;l++){
864         
865         if(s==0){
866           /* fetch the partition word */
867           int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
868           if(temp==-1)goto eopbreak;
869           partword[l]=look->decodemap[temp];
870           if(partword[l]==NULL)goto errout;
871         }
872         
873         /* now we decode residual values for the partitions */
874         for(k=0;k<partitions_per_word && i<partvals;k++,i++)
875           if(info->secondstages[partword[l][k]]&(1<<s)){
876             codebook *stagebook=look->partbooks[partword[l][k]][s];
877             
878             if(stagebook){
879               if(vorbis_book_decodevv_add(stagebook,in,
880                                           i*samples_per_partition+info->begin,ch,
881                                           &vb->opb,samples_per_partition)==-1)
882                 goto eopbreak;
883             }
884           }
885       } 
886     }
887   }
888  errout:
889  eopbreak:
890   return(0);
891 }
892
893
894 vorbis_func_residue residue0_exportbundle={
895   NULL,
896   &res0_unpack,
897   &res0_look,
898   &res0_free_info,
899   &res0_free_look,
900   NULL,
901   NULL,
902   &res0_inverse
903 };
904
905 vorbis_func_residue residue1_exportbundle={
906   &res0_pack,
907   &res0_unpack,
908   &res0_look,
909   &res0_free_info,
910   &res0_free_look,
911   &res1_class,
912   &res1_forward,
913   &res1_inverse
914 };
915
916 vorbis_func_residue residue2_exportbundle={
917   &res0_pack,
918   &res0_unpack,
919   &res0_look,
920   &res0_free_info,
921   &res0_free_look,
922   &res2_class,
923   &res2_forward,
924   &res2_inverse
925 };
926