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