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