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-2001 *
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.43 2001/12/21 14:52:36 segher 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;
46 codebook ***partbooks;
55 int qoffsets[BITTRACK_DIVISOR+1];
58 long *training_data[8][64];
59 float training_max[8][64];
60 float training_min[8][64];
66 } vorbis_look_residue0;
68 vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
69 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
70 vorbis_info_residue0 *ret=_ogg_malloc(sizeof(*ret));
71 memcpy(ret,info,sizeof(*ret));
75 void res0_free_info(vorbis_info_residue *i){
76 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
78 memset(info,0,sizeof(*info));
83 void res0_free_look(vorbis_look_residue *i){
87 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
92 for(j=0;j<look->parts;j++){
93 fprintf(stderr,"partition %d: ",j);
95 if(look->training_data[k][j]){
98 codebook *statebook=look->partbooks[j][k];
100 /* long and short into the same bucket by current convention */
101 sprintf(buffer,"res_part%d_pass%d.vqd",j,k);
102 of=fopen(buffer,"a");
104 for(l=0;l<statebook->entries;l++)
105 fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
109 fprintf(stderr,"%d(%.2f|%.2f) ",k,look->training_min[k][j],look->training_max[k][j]);
111 _ogg_free(look->training_data[k][j]);
113 fprintf(stderr,"\n");
116 fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
118 fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
119 (float)look->phrasebits/look->frames,
120 (float)look->postbits/look->frames,
121 (float)(look->postbits+look->phrasebits)/look->frames);
125 /*vorbis_info_residue0 *info=look->info;
128 "%ld frames encoded in %ld phrasebits and %ld residue bits "
129 "(%g/frame) \n",look->frames,look->phrasebits,
131 (look->phrasebits+look->resbitsflat)/(float)look->frames);
133 for(j=0;j<look->parts;j++){
135 fprintf(stderr,"\t[%d] == ",j);
136 for(k=0;k<look->stages;k++)
137 if((info->secondstages[j]>>k)&1){
138 fprintf(stderr,"%ld,",look->resbits[j][k]);
139 acc+=look->resbits[j][k];
142 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
143 acc?(float)acc/(look->resvals[j]*info->grouping):0);
145 fprintf(stderr,"\n");*/
147 for(j=0;j<look->parts;j++)
148 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
149 _ogg_free(look->partbooks);
150 for(j=0;j<look->partvals;j++)
151 _ogg_free(look->decodemap[j]);
152 _ogg_free(look->decodemap);
154 memset(look,0,sizeof(*look));
159 static int ilog(unsigned int v){
168 static int icount(unsigned int v){
178 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
179 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
181 oggpack_write(opb,info->begin,24);
182 oggpack_write(opb,info->end,24);
184 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
185 code with a partitioned book */
186 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
187 oggpack_write(opb,info->groupbook,8); /* group huffman book */
189 /* secondstages is a bitmask; as encoding progresses pass by pass, a
190 bitmask of one indicates this partition class has bits to write
192 for(j=0;j<info->partitions;j++){
193 if(ilog(info->secondstages[j])>3){
194 /* yes, this is a minor hack due to not thinking ahead */
195 oggpack_write(opb,info->secondstages[j],3);
196 oggpack_write(opb,1,1);
197 oggpack_write(opb,info->secondstages[j]>>3,5);
199 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
200 acc+=icount(info->secondstages[j]);
203 oggpack_write(opb,info->booklist[j],8);
207 /* vorbis_info is for range checking */
208 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
210 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
211 codec_setup_info *ci=vi->codec_setup;
213 info->begin=oggpack_read(opb,24);
214 info->end=oggpack_read(opb,24);
215 info->grouping=oggpack_read(opb,24)+1;
216 info->partitions=oggpack_read(opb,6)+1;
217 info->groupbook=oggpack_read(opb,8);
219 for(j=0;j<info->partitions;j++){
220 int cascade=oggpack_read(opb,3);
221 if(oggpack_read(opb,1))
222 cascade|=(oggpack_read(opb,5)<<3);
223 info->secondstages[j]=cascade;
225 acc+=icount(cascade);
228 info->booklist[j]=oggpack_read(opb,8);
230 if(info->groupbook>=ci->books)goto errout;
232 if(info->booklist[j]>=ci->books)goto errout;
236 res0_free_info(info);
240 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
241 vorbis_info_residue *vr){
242 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
243 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
244 backend_lookup_state *be=vd->backend_state;
250 look->map=vm->mapping;
252 look->parts=info->partitions;
253 look->fullbooks=be->fullbooks;
254 look->phrasebook=be->fullbooks+info->groupbook;
255 dim=look->phrasebook->dim;
257 look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
259 for(j=0;j<look->parts;j++){
260 int stages=ilog(info->secondstages[j]);
262 if(stages>maxstage)maxstage=stages;
263 look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
264 for(k=0;k<stages;k++)
265 if(info->secondstages[j]&(1<<k)){
266 look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
268 look->training_data[k][j]=calloc(look->partbooks[j][k]->entries,
269 sizeof(***look->training_data));
275 look->partvals=rint(pow((float)look->parts,(pow)dim));
276 look->stages=maxstage;
277 look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
278 for(j=0;j<look->partvals;j++){
280 long mult=look->partvals/look->parts;
281 look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
286 look->decodemap[j][k]=deco;
291 int samples_per_partition=info->grouping;
292 int n=info->end-info->begin,i;
293 int partvals=n/samples_per_partition;
295 for(i=0;i<BITTRACK_DIVISOR;i++)
296 look->qoffsets[i]=partvals*(i+1)/BITTRACK_DIVISOR;
298 look->qoffsets[i]=9999999;
306 /* does not guard against invalid settings; eg, a subn of 16 and a
307 subgroup request of 32. Max subn of 128 */
308 static int _interleaved_testhack(float *vec,int n,vorbis_look_residue0 *look,
309 int auxparts,int auxpartnum){
310 vorbis_info_residue0 *info=look->info;
312 float max,localmax=0.f;
317 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
319 /* handle case subgrp==1 outside */
321 if(temp[i]>localmax)localmax=temp[i];
324 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
337 if(temp[i]>localmax)localmax=temp[i];
340 for(i=0;i<auxparts-1;i++)
341 if(auxpartnum<info->blimit[i] &&
342 entropy[info->subgrp[i]]<=info->entmax[i] &&
343 max<=info->ampmax[i])
351 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
352 int auxparts,int auxpartnum){
353 vorbis_info_residue0 *info=look->info;
360 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
363 if(temp[i]>max)max=temp[i];
365 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
370 for(i=0;i<auxparts-1;i++)
371 if(auxpartnum<info->blimit[i] &&
372 entropy<=info->entmax[i] &&
373 max<=info->ampmax[i])
379 static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
380 codebook *book,long *acc){
386 int entry=vorbis_book_besterror(book,vec+i,step,0);
392 bits+=vorbis_book_encode(book,entry,opb);
398 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
399 codebook *book,long *acc){
405 int entry=vorbis_book_besterror(book,vec+i*dim,1,0);
411 bits+=vorbis_book_encode(book,entry,opb);
417 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
419 int (*classify)(float *,int,vorbis_look_residue0 *,
422 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
423 vorbis_info_residue0 *info=look->info;
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;
430 int partvals=n/samples_per_partition;
431 long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
433 /* we find the partition type for each partition of each
434 channel. We'll go back and do the interleaved encoding in a
435 bit. For now, clarity */
438 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
439 memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
442 for(i=0;i<partvals;i++){
444 /* do the partition decision based on the 'entropy'
447 classify(in[j]+i*samples_per_partition+info->begin,
448 samples_per_partition,look,possible_partitions,i);
459 sprintf(buffer,"resaux_%s.vqd",(vb->mode?"long":"short"));
460 of=fopen(buffer,"a");
461 for(j=0;j<partvals;j++)
462 fprintf(of,"%ld, ",partword[i][j]);
473 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,
475 int (*classify)(float *,int,vorbis_look_residue0 *,
478 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
479 vorbis_info_residue0 *info=look->info;
481 /* move all this setup out later */
482 int samples_per_partition=info->grouping;
483 int possible_partitions=info->partitions;
484 int n=info->end-info->begin;
486 int partvals=n/samples_per_partition;
487 long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
488 float *work=alloca(sizeof(*work)*samples_per_partition);
495 partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
496 memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
498 for(i=0,j=0,k=0,l=info->begin;i<partvals;i++){
499 for(k=0;k<samples_per_partition;k++){
509 classify(work,samples_per_partition,look,possible_partitions,i);
516 sprintf(buffer,"resaux_%s.vqd",(vb->mode?"long":"short"));
517 of=fopen(buffer,"a");
518 for(i=0;i<partvals;i++)
519 fprintf(of,"%ld, ",partword[0][i]);
529 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
531 int pass,long **partword,
532 int (*encode)(oggpack_buffer *,float *,int,
534 ogg_uint32_t *stats){
536 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
537 vorbis_info_residue0 *info=look->info;
539 vorbis_dsp_state *vd=vb->vd;
540 vorbis_info *vi=vd->vi;
541 codec_setup_info *ci=vi->codec_setup;
544 /* move all this setup out later */
545 int samples_per_partition=info->grouping;
546 int possible_partitions=info->partitions;
547 int partitions_per_word=look->phrasebook->dim;
548 int n=info->end-info->begin;
550 int partvals=n/samples_per_partition;
556 for(j=info->begin;j<info->end;j++){
557 if(in[i][j]>look->tmax)look->tmax=in[i][j];
558 if(in[i][j]<look->tmin)look->tmin=in[i][j];
562 memset(resbits,0,sizeof(resbits));
563 memset(resvals,0,sizeof(resvals));
565 /* we code the partition words for each channel, then the residual
566 words for a partition per channel until we've written all the
567 residual words for that partition word. Then write the next
568 partition channel words... */
570 for(s=(pass==0?0:ci->passlimit[pass-1]);s<ci->passlimit[pass];s++){
572 ogg_uint32_t *qptr=NULL;
573 if(stats)qptr=stats+s*BITTRACK_DIVISOR;
575 for(i=0;i<partvals;){
577 /* first we encode a partition codeword for each channel */
580 long val=partword[j][i];
581 for(k=1;k<partitions_per_word;k++){
582 val*=possible_partitions;
584 val+=partword[j][i+k];
588 if(val<look->phrasebook->entries)
589 look->phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
598 /* now we encode interleaved residual values for the partitions */
599 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
600 long offset=i*samples_per_partition+info->begin;
602 if(qptr)while(i>=look->qoffsets[bin])
603 qptr[bin++]=oggpack_bits(&vb->opb);
606 if(s==0)resvals[partword[j][i]]+=samples_per_partition;
607 if(info->secondstages[partword[j][i]]&(1<<s)){
608 codebook *statebook=look->partbooks[partword[j][i]][s];
611 long *accumulator=NULL;
614 accumulator=look->training_data[s][partword[j][i]];
617 float *samples=in[j]+offset;
618 for(l=0;l<samples_per_partition;l++){
619 if(samples[l]<look->training_min[s][partword[j][i]])
620 look->training_min[s][partword[j][i]]=samples[l];
621 if(samples[l]>look->training_max[s][partword[j][i]])
622 look->training_max[s][partword[j][i]]=samples[l];
627 ret=encode(&vb->opb,in[j]+offset,samples_per_partition,
628 statebook,accumulator);
631 resbits[partword[j][i]]+=ret;
636 if(qptr)while(i>=look->qoffsets[bin])
637 qptr[bin++]=oggpack_bits(&vb->opb);
644 fprintf(stderr,"%d :: ",vb->mode);
645 for(k=0;k<possible_partitions;k++){
646 fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
648 totalbits+=resbits[k];
651 fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
656 /* a truncated packet here just means 'stop working'; it's not an error */
657 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
659 long (*decodepart)(codebook *, float *,
660 oggpack_buffer *,int)){
663 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
664 vorbis_info_residue0 *info=look->info;
666 /* move all this setup out later */
667 int samples_per_partition=info->grouping;
668 int partitions_per_word=look->phrasebook->dim;
669 int n=info->end-info->begin;
671 int partvals=n/samples_per_partition;
672 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
673 int ***partword=alloca(ch*sizeof(*partword));
676 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
678 for(s=0;s<look->stages;s++){
680 /* each loop decodes on partition codeword containing
681 partitions_pre_word partitions */
682 for(i=0,l=0;i<partvals;l++){
684 /* fetch the partition word for each channel */
686 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
687 if(temp==-1)goto eopbreak;
688 partword[j][l]=look->decodemap[temp];
689 if(partword[j][l]==NULL)goto errout;
693 /* now we decode residual values for the partitions */
694 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
696 long offset=info->begin+i*samples_per_partition;
697 if(info->secondstages[partword[j][l][k]]&(1<<s)){
698 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
700 if(decodepart(stagebook,in[j]+offset,&vb->opb,
701 samples_per_partition)==-1)goto eopbreak;
713 /* residue 0 and 1 are just slight variants of one another. 0 is
714 interleaved, 1 is not */
715 long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
716 float **in,int *nonzero,int ch){
717 /* we encode only the nonzero parts of a bundle */
723 /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
724 return(_01class(vb,vl,in,used,_testhack));
729 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
730 float **in,float **out,int *nonzero,int ch,
731 int pass, long **partword,ogg_uint32_t *stats){
732 /* we encode only the nonzero parts of a bundle */
733 int i,j,used=0,n=vb->pcmend/2;
741 int ret=_01forward(vb,vl,in,used,pass,partword,
742 _interleaved_encodepart,stats);
747 out[i][j]-=in[used][j];
752 for(i=0;i<vorbis_bitrate_maxmarkers();i++)
753 stats[i]=oggpack_bits(&vb->opb);
759 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
760 float **in,int *nonzero,int ch){
766 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
771 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
772 float **in,float **out,int *nonzero,int ch,
773 int pass, long **partword, ogg_uint32_t *stats){
774 int i,j,used=0,n=vb->pcmend/2;
783 int ret=_01forward(vb,vl,in,used,pass,partword,_encodepart,stats);
788 out[i][j]-=in[used][j];
793 for(i=0;i<vorbis_bitrate_maxmarkers();i++)
794 stats[i]=oggpack_bits(&vb->opb);
800 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
801 float **in,int *nonzero,int ch){
807 return(_01class(vb,vl,in,used,_testhack));
812 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
813 float **in,int *nonzero,int ch){
819 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
824 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
825 float **in,int *nonzero,int ch){
831 return(_2class(vb,vl,in,used,_testhack));
836 /* res2 is slightly more different; all the channels are interleaved
837 into a single vector and encoded. */
839 int res2_forward(vorbis_block *vb,vorbis_look_residue *vl,
840 float **in,float **out,int *nonzero,int ch,
841 int pass,long **partword,ogg_uint32_t *stats){
842 long i,j,k,n=vb->pcmend/2,used=0;
844 /* don't duplicate the code; use a working vector hack for now and
845 reshape ourselves into a single channel res1 */
846 /* ugly; reallocs for each coupling pass :-( */
847 float *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
850 if(nonzero[i])used++;
851 for(j=0,k=i;j<n;j++,k+=ch)
856 int ret=_01forward(vb,vl,&work,1,pass,partword,_encodepart,stats);
857 /* update the sofar vector */
861 for(j=0,k=i;j<n;j++,k+=ch)
862 sofar[j]+=pcm[j]-work[k];
867 for(i=0;i<vorbis_bitrate_maxmarkers();i++)
868 stats[i]=oggpack_bits(&vb->opb);
874 /* duplicate code here as speed is somewhat more important */
875 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
876 float **in,int *nonzero,int ch){
878 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
879 vorbis_info_residue0 *info=look->info;
881 /* move all this setup out later */
882 int samples_per_partition=info->grouping;
883 int partitions_per_word=look->phrasebook->dim;
884 int n=info->end-info->begin;
886 int partvals=n/samples_per_partition;
887 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
888 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
890 for(i=0;i<ch;i++)if(nonzero[i])break;
891 if(i==ch)return(0); /* no nonzero vectors */
893 for(s=0;s<look->stages;s++){
894 for(i=0,l=0;i<partvals;l++){
897 /* fetch the partition word */
898 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
899 if(temp==-1)goto eopbreak;
900 partword[l]=look->decodemap[temp];
901 if(partword[l]==NULL)goto errout;
904 /* now we decode residual values for the partitions */
905 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
906 if(info->secondstages[partword[l][k]]&(1<<s)){
907 codebook *stagebook=look->partbooks[partword[l][k]][s];
910 if(vorbis_book_decodevv_add(stagebook,in,
911 i*samples_per_partition+info->begin,ch,
912 &vb->opb,samples_per_partition)==-1)
925 vorbis_func_residue residue0_exportbundle={
937 vorbis_func_residue residue1_exportbundle={
949 vorbis_func_residue residue2_exportbundle={