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