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