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