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