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