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