1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: residue backend 0, 1 and 2 implementation
14 last mod: $Id: res0.c,v 1.49 2003/01/18 08:28:37 xiphmont Exp $
16 ********************************************************************/
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,
27 #include "vorbis/codec.h"
28 #include "codec_internal.h"
39 vorbis_info_residue0 *info;
45 codebook ***partbooks;
56 long *training_data[8][64];
57 float training_max[8][64];
58 float training_min[8][64];
63 } vorbis_look_residue0;
65 void res0_free_info(vorbis_info_residue *i){
66 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
68 memset(info,0,sizeof(*info));
73 void res0_free_look(vorbis_look_residue *i){
77 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
82 for(j=0;j<look->parts;j++){
83 /*fprintf(stderr,"partition %d: ",j);*/
85 if(look->training_data[k][j]){
88 codebook *statebook=look->partbooks[j][k];
90 /* long and short into the same bucket by current convention */
91 sprintf(buffer,"res_part%d_pass%d.vqd",j,k);
94 for(l=0;l<statebook->entries;l++)
95 fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
99 /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
100 look->training_min[k][j],look->training_max[k][j]);*/
102 _ogg_free(look->training_data[k][j]);
104 /*fprintf(stderr,"\n");*/
107 fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
109 /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
110 (float)look->phrasebits/look->frames,
111 (float)look->postbits/look->frames,
112 (float)(look->postbits+look->phrasebits)/look->frames);*/
116 /*vorbis_info_residue0 *info=look->info;
119 "%ld frames encoded in %ld phrasebits and %ld residue bits "
120 "(%g/frame) \n",look->frames,look->phrasebits,
122 (look->phrasebits+look->resbitsflat)/(float)look->frames);
124 for(j=0;j<look->parts;j++){
126 fprintf(stderr,"\t[%d] == ",j);
127 for(k=0;k<look->stages;k++)
128 if((info->secondstages[j]>>k)&1){
129 fprintf(stderr,"%ld,",look->resbits[j][k]);
130 acc+=look->resbits[j][k];
133 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
134 acc?(float)acc/(look->resvals[j]*info->grouping):0);
136 fprintf(stderr,"\n");*/
138 for(j=0;j<look->parts;j++)
139 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
140 _ogg_free(look->partbooks);
141 for(j=0;j<look->partvals;j++)
142 _ogg_free(look->decodemap[j]);
143 _ogg_free(look->decodemap);
145 memset(look,0,sizeof(*look));
150 static int ilog(unsigned int v){
159 static int icount(unsigned int v){
169 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
170 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
172 oggpack_write(opb,info->begin,24);
173 oggpack_write(opb,info->end,24);
175 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
176 code with a partitioned book */
177 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
178 oggpack_write(opb,info->groupbook,8); /* group huffman book */
180 /* secondstages is a bitmask; as encoding progresses pass by pass, a
181 bitmask of one indicates this partition class has bits to write
183 for(j=0;j<info->partitions;j++){
184 if(ilog(info->secondstages[j])>3){
185 /* yes, this is a minor hack due to not thinking ahead */
186 oggpack_write(opb,info->secondstages[j],3);
187 oggpack_write(opb,1,1);
188 oggpack_write(opb,info->secondstages[j]>>3,5);
190 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
191 acc+=icount(info->secondstages[j]);
194 oggpack_write(opb,info->booklist[j],8);
198 /* vorbis_info is for range checking */
199 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
201 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
202 codec_setup_info *ci=vi->codec_setup;
204 info->begin=oggpack_read(opb,24);
205 info->end=oggpack_read(opb,24);
206 info->grouping=oggpack_read(opb,24)+1;
207 info->partitions=oggpack_read(opb,6)+1;
208 info->groupbook=oggpack_read(opb,8);
210 for(j=0;j<info->partitions;j++){
211 int cascade=oggpack_read(opb,3);
212 if(oggpack_read(opb,1))
213 cascade|=(oggpack_read(opb,5)<<3);
214 info->secondstages[j]=cascade;
216 acc+=icount(cascade);
219 info->booklist[j]=oggpack_read(opb,8);
221 if(info->groupbook>=ci->books)goto errout;
223 if(info->booklist[j]>=ci->books)goto errout;
227 res0_free_info(info);
231 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
232 vorbis_info_residue *vr){
233 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
234 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
235 codec_setup_info *ci=vd->vi->codec_setup;
242 look->parts=info->partitions;
243 look->fullbooks=ci->fullbooks;
244 look->phrasebook=ci->fullbooks+info->groupbook;
245 dim=look->phrasebook->dim;
247 look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
249 for(j=0;j<look->parts;j++){
250 int stages=ilog(info->secondstages[j]);
252 if(stages>maxstage)maxstage=stages;
253 look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
254 for(k=0;k<stages;k++)
255 if(info->secondstages[j]&(1<<k)){
256 look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
258 look->training_data[k][j]=calloc(look->partbooks[j][k]->entries,
259 sizeof(***look->training_data));
265 look->partvals=rint(pow((float)look->parts,(float)dim));
266 look->stages=maxstage;
267 look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
268 for(j=0;j<look->partvals;j++){
270 long mult=look->partvals/look->parts;
271 look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
276 look->decodemap[j][k]=deco;
281 static int train_seq=0;
282 look->train_seq=train_seq++;
288 /* break an abstraction and copy some code for performance purposes */
289 static int local_book_besterror(codebook *book,float *a){
290 int dim=book->dim,i,k,o;
292 encode_aux_threshmatch *tt=book->c->thresh_tree;
294 /* find the quant val of each scalar */
295 for(k=0,o=dim;k<dim;++k){
299 if(val<tt->quantthresh[i]){
300 if(val<tt->quantthresh[i-1]){
302 if(val>=tt->quantthresh[i-1])
307 for(++i;i<tt->threshvals-1;++i)
308 if(val<tt->quantthresh[i])break;
312 best=(best*tt->quantvals)+tt->quantmap[i];
314 /* regular lattices are easy :-) */
316 if(book->c->lengthlist[best]<=0){
317 const static_codebook *c=book->c;
320 float *e=book->valuelist;
322 for(i=0;i<book->entries;i++){
323 if(c->lengthlist[i]>0){
326 float val=(e[j]-a[j]);
329 if(best==-1 || this<bestf){
339 float *ptr=book->valuelist+best*dim;
347 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
348 codebook *book,long *acc){
354 int entry=local_book_besterror(book,vec+i*dim);
360 bits+=vorbis_book_encode(book,entry,opb);
366 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
369 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
370 vorbis_info_residue0 *info=look->info;
371 vorbis_info *vi=vb->vd->vi;
372 codec_setup_info *ci=vi->codec_setup;
374 /* move all this setup out later */
375 int samples_per_partition=info->grouping;
376 int possible_partitions=info->partitions;
377 int n=info->end-info->begin;
379 int partvals=n/samples_per_partition;
380 long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
381 float scale=100./samples_per_partition;
383 /* we find the partition type for each partition of each
384 channel. We'll go back and do the interleaved encoding in a
385 bit. For now, clarity */
388 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
389 memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
392 for(i=0;i<partvals;i++){
393 int offset=i*samples_per_partition+info->begin;
397 for(k=0;k<samples_per_partition;k++){
398 if(fabs(in[j][offset+k])>max)max=fabs(in[j][offset+k]);
399 ent+=fabs(rint(in[j][offset+k]));
403 for(k=0;k<possible_partitions-1;k++)
404 if(max<=info->classmetric1[k] &&
405 (info->classmetric2[k]<0 || (int)ent<info->classmetric2[k]))
418 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
419 of=fopen(buffer,"a");
420 for(j=0;j<partvals;j++)
421 fprintf(of,"%ld, ",partword[i][j]);
432 /* designed for stereo or other modes where the partition size is an
433 integer multiple of the number of channels encoded in the current
435 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in,
438 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
439 vorbis_info_residue0 *info=look->info;
441 /* move all this setup out later */
442 int samples_per_partition=info->grouping;
443 int possible_partitions=info->partitions;
444 int n=info->end-info->begin;
446 int partvals=n/samples_per_partition;
447 long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
454 partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
455 memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
457 for(i=0,l=info->begin/ch;i<partvals;i++){
460 for(j=0;j<samples_per_partition;j+=ch){
461 if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]);
463 if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]);
467 for(j=0;j<possible_partitions-1;j++)
468 if(magmax<=info->classmetric1[j] &&
469 angmax<=info->classmetric2[j])
477 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
478 of=fopen(buffer,"a");
479 for(i=0;i<partvals;i++)
480 fprintf(of,"%ld, ",partword[0][i]);
490 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
493 int (*encode)(oggpack_buffer *,float *,int,
496 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
497 vorbis_info_residue0 *info=look->info;
499 vorbis_dsp_state *vd=vb->vd;
501 /* move all this setup out later */
502 int samples_per_partition=info->grouping;
503 int possible_partitions=info->partitions;
504 int partitions_per_word=look->phrasebook->dim;
505 int n=info->end-info->begin;
507 int partvals=n/samples_per_partition;
513 for(j=info->begin;j<info->end;j++){
514 if(in[i][j]>look->tmax)look->tmax=in[i][j];
515 if(in[i][j]<look->tmin)look->tmin=in[i][j];
519 memset(resbits,0,sizeof(resbits));
520 memset(resvals,0,sizeof(resvals));
522 /* we code the partition words for each channel, then the residual
523 words for a partition per channel until we've written all the
524 residual words for that partition word. Then write the next
525 partition channel words... */
527 for(s=0;s<look->stages;s++){
529 for(i=0;i<partvals;){
531 /* first we encode a partition codeword for each channel */
534 long val=partword[j][i];
535 for(k=1;k<partitions_per_word;k++){
536 val*=possible_partitions;
538 val+=partword[j][i+k];
542 if(val<look->phrasebook->entries)
543 look->phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
544 #if 0 /*def TRAIN_RES*/
552 /* now we encode interleaved residual values for the partitions */
553 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
554 long offset=i*samples_per_partition+info->begin;
557 if(s==0)resvals[partword[j][i]]+=samples_per_partition;
558 if(info->secondstages[partword[j][i]]&(1<<s)){
559 codebook *statebook=look->partbooks[partword[j][i]][s];
562 long *accumulator=NULL;
565 accumulator=look->training_data[s][partword[j][i]];
568 float *samples=in[j]+offset;
569 for(l=0;l<samples_per_partition;l++){
570 if(samples[l]<look->training_min[s][partword[j][i]])
571 look->training_min[s][partword[j][i]]=samples[l];
572 if(samples[l]>look->training_max[s][partword[j][i]])
573 look->training_max[s][partword[j][i]]=samples[l];
578 ret=encode(&vb->opb,in[j]+offset,samples_per_partition,
579 statebook,accumulator);
582 resbits[partword[j][i]]+=ret;
593 fprintf(stderr,"%d :: ",vb->mode);
594 for(k=0;k<possible_partitions;k++){
595 fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
597 totalbits+=resbits[k];
600 fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
605 /* a truncated packet here just means 'stop working'; it's not an error */
606 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
608 long (*decodepart)(codebook *, float *,
609 oggpack_buffer *,int)){
612 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
613 vorbis_info_residue0 *info=look->info;
615 /* move all this setup out later */
616 int samples_per_partition=info->grouping;
617 int partitions_per_word=look->phrasebook->dim;
618 int n=info->end-info->begin;
620 int partvals=n/samples_per_partition;
621 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
622 int ***partword=alloca(ch*sizeof(*partword));
625 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
627 for(s=0;s<look->stages;s++){
629 /* each loop decodes on partition codeword containing
630 partitions_pre_word partitions */
631 for(i=0,l=0;i<partvals;l++){
633 /* fetch the partition word for each channel */
635 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
636 if(temp==-1)goto eopbreak;
637 partword[j][l]=look->decodemap[temp];
638 if(partword[j][l]==NULL)goto errout;
642 /* now we decode residual values for the partitions */
643 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
645 long offset=info->begin+i*samples_per_partition;
646 if(info->secondstages[partword[j][l][k]]&(1<<s)){
647 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
649 if(decodepart(stagebook,in[j]+offset,&vb->opb,
650 samples_per_partition)==-1)goto eopbreak;
663 /* residue 0 and 1 are just slight variants of one another. 0 is
664 interleaved, 1 is not */
665 long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
666 float **in,int *nonzero,int ch){
667 /* we encode only the nonzero parts of a bundle */
673 /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
674 return(_01class(vb,vl,in,used));
679 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
680 float **in,float **out,int *nonzero,int ch,
682 /* we encode only the nonzero parts of a bundle */
683 int i,j,used=0,n=vb->pcmend/2;
692 int ret=_01forward(vb,vl,in,used,partword,
693 _interleaved_encodepart);
699 out[i][j]-=in[used][j];
710 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
711 float **in,int *nonzero,int ch){
717 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
722 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
723 float **in,float **out,int *nonzero,int ch,
725 int i,j,used=0,n=vb->pcmend/2;
735 int ret=_01forward(vb,vl,in,used,partword,_encodepart);
741 out[i][j]-=in[used][j];
751 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
752 float **in,int *nonzero,int ch){
758 return(_01class(vb,vl,in,used));
763 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
764 float **in,int *nonzero,int ch){
770 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
775 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
776 float **in,int *nonzero,int ch){
779 if(nonzero[i])used++;
781 return(_2class(vb,vl,in,ch));
786 /* res2 is slightly more different; all the channels are interleaved
787 into a single vector and encoded. */
789 int res2_forward(vorbis_block *vb,vorbis_look_residue *vl,
790 float **in,float **out,int *nonzero,int ch,
792 long i,j,k,n=vb->pcmend/2,used=0;
794 /* don't duplicate the code; use a working vector hack for now and
795 reshape ourselves into a single channel res1 */
796 /* ugly; reallocs for each coupling pass :-( */
797 float *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
800 if(nonzero[i])used++;
801 for(j=0,k=i;j<n;j++,k+=ch)
806 int ret=_01forward(vb,vl,&work,1,partword,_encodepart);
807 /* update the sofar vector */
812 for(j=0,k=i;j<n;j++,k+=ch)
813 sofar[j]+=pcm[j]-work[k];
823 /* duplicate code here as speed is somewhat more important */
824 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
825 float **in,int *nonzero,int ch){
827 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
828 vorbis_info_residue0 *info=look->info;
830 /* move all this setup out later */
831 int samples_per_partition=info->grouping;
832 int partitions_per_word=look->phrasebook->dim;
833 int n=info->end-info->begin;
835 int partvals=n/samples_per_partition;
836 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
837 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
839 for(i=0;i<ch;i++)if(nonzero[i])break;
840 if(i==ch)return(0); /* no nonzero vectors */
842 for(s=0;s<look->stages;s++){
843 for(i=0,l=0;i<partvals;l++){
846 /* fetch the partition word */
847 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
848 if(temp==-1)goto eopbreak;
849 partword[l]=look->decodemap[temp];
850 if(partword[l]==NULL)goto errout;
853 /* now we decode residual values for the partitions */
854 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
855 if(info->secondstages[partword[l][k]]&(1<<s)){
856 codebook *stagebook=look->partbooks[partword[l][k]][s];
859 if(vorbis_book_decodevv_add(stagebook,in,
860 i*samples_per_partition+info->begin,ch,
861 &vb->opb,samples_per_partition)==-1)
874 vorbis_func_residue residue0_exportbundle={
885 vorbis_func_residue residue1_exportbundle={
896 vorbis_func_residue residue2_exportbundle={