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