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.34 2001/08/13 10:01:04 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,
28 #include "vorbis/codec.h"
29 #include "codec_internal.h"
36 vorbis_info_residue0 *info;
43 codebook ***partbooks;
52 } vorbis_look_residue0;
54 vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
55 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
56 vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
57 memcpy(ret,info,sizeof(vorbis_info_residue0));
61 void res0_free_info(vorbis_info_residue *i){
63 memset(i,0,sizeof(vorbis_info_residue0));
68 void res0_free_look(vorbis_look_residue *i){
72 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
74 fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
75 (float)look->phrasebits/look->frames,
76 (float)look->postbits/look->frames,
77 (float)(look->postbits+look->phrasebits)/look->frames);
79 /*vorbis_info_residue0 *info=look->info;
82 "%ld frames encoded in %ld phrasebits and %ld residue bits "
83 "(%g/frame) \n",look->frames,look->phrasebits,
85 (look->phrasebits+look->resbitsflat)/(float)look->frames);
87 for(j=0;j<look->parts;j++){
89 fprintf(stderr,"\t[%d] == ",j);
90 for(k=0;k<look->stages;k++)
91 if((info->secondstages[j]>>k)&1){
92 fprintf(stderr,"%ld,",look->resbits[j][k]);
93 acc+=look->resbits[j][k];
96 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
97 acc?(float)acc/(look->resvals[j]*info->grouping):0);
99 fprintf(stderr,"\n");*/
101 for(j=0;j<look->parts;j++)
102 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
103 _ogg_free(look->partbooks);
104 for(j=0;j<look->partvals;j++)
105 _ogg_free(look->decodemap[j]);
106 _ogg_free(look->decodemap);
107 memset(i,0,sizeof(vorbis_look_residue0));
112 static int ilog(unsigned int v){
121 static int icount(unsigned int v){
131 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
132 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
134 oggpack_write(opb,info->begin,24);
135 oggpack_write(opb,info->end,24);
137 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
138 code with a partitioned book */
139 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
140 oggpack_write(opb,info->groupbook,8); /* group huffman book */
142 /* secondstages is a bitmask; as encoding progresses pass by pass, a
143 bitmask of one indicates this partition class has bits to write
145 for(j=0;j<info->partitions;j++){
146 if(ilog(info->secondstages[j])>3){
147 /* yes, this is a minor hack due to not thinking ahead */
148 oggpack_write(opb,info->secondstages[j],3);
149 oggpack_write(opb,1,1);
150 oggpack_write(opb,info->secondstages[j]>>3,5);
152 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
153 acc+=icount(info->secondstages[j]);
156 oggpack_write(opb,info->booklist[j],8);
160 /* vorbis_info is for range checking */
161 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
163 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
164 codec_setup_info *ci=vi->codec_setup;
166 info->begin=oggpack_read(opb,24);
167 info->end=oggpack_read(opb,24);
168 info->grouping=oggpack_read(opb,24)+1;
169 info->partitions=oggpack_read(opb,6)+1;
170 info->groupbook=oggpack_read(opb,8);
172 for(j=0;j<info->partitions;j++){
173 int cascade=oggpack_read(opb,3);
174 if(oggpack_read(opb,1))
175 cascade|=(oggpack_read(opb,5)<<3);
176 info->secondstages[j]=cascade;
178 acc+=icount(cascade);
181 info->booklist[j]=oggpack_read(opb,8);
183 if(info->groupbook>=ci->books)goto errout;
185 if(info->booklist[j]>=ci->books)goto errout;
189 res0_free_info(info);
193 vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
194 vorbis_info_residue *vr){
195 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
196 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(vorbis_look_residue0));
197 backend_lookup_state *be=vd->backend_state;
203 look->map=vm->mapping;
205 look->parts=info->partitions;
206 look->fullbooks=be->fullbooks;
207 look->phrasebook=be->fullbooks+info->groupbook;
208 dim=look->phrasebook->dim;
210 look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
212 for(j=0;j<look->parts;j++){
213 int stages=ilog(info->secondstages[j]);
215 if(stages>maxstage)maxstage=stages;
216 look->partbooks[j]=_ogg_calloc(stages,sizeof(codebook *));
217 for(k=0;k<stages;k++)
218 if(info->secondstages[j]&(1<<k))
219 look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
223 look->partvals=rint(pow(look->parts,dim));
224 look->stages=maxstage;
225 look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
226 for(j=0;j<look->partvals;j++){
228 long mult=look->partvals/look->parts;
229 look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
234 look->decodemap[j][k]=deco;
242 /* does not guard against invalid settings; eg, a subn of 16 and a
243 subgroup request of 32. Max subn of 128 */
244 static int _interleaved_testhack(float *vec,int n,vorbis_look_residue0 *look,
245 int auxparts,int auxpartnum){
246 vorbis_info_residue0 *info=look->info;
248 float max,localmax=0.f;
253 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
255 /* handle case subgrp==1 outside */
257 if(temp[i]>localmax)localmax=temp[i];
260 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
273 if(temp[i]>localmax)localmax=temp[i];
276 for(i=0;i<auxparts-1;i++)
277 if(auxpartnum<info->blimit[i] &&
278 entropy[info->subgrp[i]]<=info->entmax[i] &&
279 max<=info->ampmax[i])
285 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
286 int auxparts,int auxpartnum){
287 vorbis_info_residue0 *info=look->info;
294 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
297 if(temp[i]>max)max=temp[i];
299 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
304 for(i=0;i<auxparts-1;i++)
305 if(auxpartnum<info->blimit[i] &&
306 entropy<=info->entmax[i] &&
307 max<=info->ampmax[i])
313 static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
314 codebook *book,vorbis_look_residue0 *look){
321 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
326 int entry=vorbis_book_besterror(book,vec+i,step,0);
329 fprintf(f,"%d\n",entry);
332 bits+=vorbis_book_encode(book,entry,opb);
341 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
342 codebook *book,vorbis_look_residue0 *look){
349 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
354 int entry=vorbis_book_besterror(book,vec+i*dim,1,0);
357 fprintf(f,"%d\n",entry);
360 bits+=vorbis_book_encode(book,entry,opb);
369 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
371 int (*classify)(float *,int,vorbis_look_residue0 *,
374 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
375 vorbis_info_residue0 *info=look->info;
377 /* move all this setup out later */
378 int samples_per_partition=info->grouping;
379 int possible_partitions=info->partitions;
380 int n=info->end-info->begin;
382 int partvals=n/samples_per_partition;
383 long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
385 /* we find the partition type for each partition of each
386 channel. We'll go back and do the interleaved encoding in a
387 bit. For now, clarity */
390 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
391 memset(partword[i],0,n/samples_per_partition*sizeof(long));
394 for(i=0;i<partvals;i++){
396 /* do the partition decision based on the 'entropy'
399 classify(in[j]+i*samples_per_partition+info->begin,
400 samples_per_partition,look,possible_partitions,i);
410 sprintf(buffer,"resaux_%d.vqd",vb->mode);
411 of=fopen(buffer,"a");
412 for(j=0;j<partvals;j++)
413 fprintf(of,"%ld, ",partword[i][j]);
424 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,
426 int (*classify)(float *,int,vorbis_look_residue0 *,
429 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
430 vorbis_info_residue0 *info=look->info;
432 /* move all this setup out later */
433 int samples_per_partition=info->grouping;
434 int possible_partitions=info->partitions;
435 int n=info->end-info->begin;
437 int partvals=n/samples_per_partition;
438 long **partword=_vorbis_block_alloc(vb,sizeof(long *));
439 float *work=alloca(sizeof(float)*samples_per_partition);
441 partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(long));
442 memset(partword[0],0,n*ch/samples_per_partition*sizeof(long));
444 for(i=0,j=0,k=0,l=info->begin;i<partvals;i++){
445 for(k=0;k<samples_per_partition;k++){
454 classify(work,samples_per_partition,look,possible_partitions,i);
462 sprintf(buffer,"resaux_%d.vqd",vb->mode);
463 of=fopen(buffer,"a");
464 for(i=0;i<partvals;i++)
465 fprintf(of,"%ld, ",partword[0][i]);
475 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
477 int pass,long **partword,
478 int (*encode)(oggpack_buffer *,float *,int,
479 codebook *,vorbis_look_residue0 *)){
481 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
482 vorbis_info_residue0 *info=look->info;
484 /* move all this setup out later */
485 int samples_per_partition=info->grouping;
486 int possible_partitions=info->partitions;
487 int partitions_per_word=look->phrasebook->dim;
488 int n=info->end-info->begin;
490 int partvals=n/samples_per_partition;
500 sprintf(buffer,"residue_%d#%d.vqd",vb->mode,pass);
501 of=fopen(buffer,"a");
502 for(m=0;m<info->end;m++)
503 fprintf(of,"%.2f, ",in[i][m]);
509 memset(resbits,0,sizeof(resbits));
510 memset(resvals,0,sizeof(resvals));
512 /* we code the partition words for each channel, then the residual
513 words for a partition per channel until we've written all the
514 residual words for that partition word. Then write the next
515 partition channel words... */
517 for(s=(pass==0?0:info->passlimit[pass-1]);s<info->passlimit[pass];s++){
518 for(i=0;i<partvals;){
520 /* first we encode a partition codeword for each channel */
523 long val=partword[j][i];
525 for(k=1;k<partitions_per_word;k++){
526 val*=possible_partitions;
528 val+=partword[j][i+k];
532 if(val<look->phrasebook->entries)
533 ret=vorbis_book_encode(look->phrasebook,val,&vb->opb);
537 look->phrasebits+=ret;
542 /* now we encode interleaved residual values for the partitions */
543 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
544 long offset=i*samples_per_partition+info->begin;
547 if(s==0)resvals[partword[j][i]]+=samples_per_partition;
548 if(info->secondstages[partword[j][i]]&(1<<s)){
549 codebook *statebook=look->partbooks[partword[j][i]][s];
551 int ret=encode(&vb->opb,in[j]+offset,samples_per_partition,
554 resbits[partword[j][i]]+=ret;
565 fprintf(stderr,"%d :: ",vb->mode);
566 for(k=0;k<possible_partitions;k++){
567 fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
569 totalbits+=resbits[k];
572 fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
577 /* a truncated packet here just means 'stop working'; it's not an error */
578 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
580 long (*decodepart)(codebook *, float *,
581 oggpack_buffer *,int)){
584 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
585 vorbis_info_residue0 *info=look->info;
587 /* move all this setup out later */
588 int samples_per_partition=info->grouping;
589 int partitions_per_word=look->phrasebook->dim;
590 int n=info->end-info->begin;
592 int partvals=n/samples_per_partition;
593 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
594 int ***partword=alloca(ch*sizeof(int **));
597 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(int *));
599 for(s=0;s<look->stages;s++){
601 /* each loop decodes on partition codeword containing
602 partitions_pre_word partitions */
603 for(i=0,l=0;i<partvals;l++){
605 /* fetch the partition word for each channel */
607 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
608 if(temp==-1)goto eopbreak;
609 partword[j][l]=look->decodemap[temp];
610 if(partword[j][l]==NULL)goto errout;
614 /* now we decode residual values for the partitions */
615 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
617 long offset=info->begin+i*samples_per_partition;
618 if(info->secondstages[partword[j][l][k]]&(1<<s)){
619 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
621 if(decodepart(stagebook,in[j]+offset,&vb->opb,
622 samples_per_partition)==-1)goto eopbreak;
634 /* residue 0 and 1 are just slight variants of one another. 0 is
635 interleaved, 1 is not */
636 long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
637 float **in,int *nonzero,int ch){
638 /* we encode only the nonzero parts of a bundle */
644 return(_01class(vb,vl,in,used,_interleaved_testhack));
649 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
650 float **in,float **out,int *nonzero,int ch,
651 int pass, long **partword){
652 /* we encode only the nonzero parts of a bundle */
653 int i,j,used=0,n=vb->pcmend/2;
661 int ret=_01forward(vb,vl,in,used,pass,partword,
662 _interleaved_encodepart);
667 out[i][j]-=in[used][j];
675 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
676 float **in,int *nonzero,int ch){
682 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
687 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
688 float **in,float **out,int *nonzero,int ch,
689 int pass, long **partword){
690 int i,j,used=0,n=vb->pcmend/2;
699 int ret=_01forward(vb,vl,in,used,pass,partword,_encodepart);
704 out[i][j]-=in[used][j];
712 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
713 float **in,int *nonzero,int ch){
719 return(_01class(vb,vl,in,used,_testhack));
724 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
725 float **in,int *nonzero,int ch){
731 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
736 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
737 float **in,int *nonzero,int ch){
743 return(_2class(vb,vl,in,used,_testhack));
748 /* res2 is slightly more different; all the channels are interleaved
749 into a single vector and encoded. */
751 int res2_forward(vorbis_block *vb,vorbis_look_residue *vl,
752 float **in,float **out,int *nonzero,int ch,
753 int pass,long **partword){
754 long i,j,k,n=vb->pcmend/2,used=0;
756 /* don't duplicate the code; use a working vector hack for now and
757 reshape ourselves into a single channel res1 */
758 /* ugly; reallocs for each coupling pass :-( */
759 float *work=_vorbis_block_alloc(vb,ch*n*sizeof(float));
762 if(nonzero[i])used++;
763 for(j=0,k=i;j<n;j++,k+=ch)
768 int ret=_01forward(vb,vl,&work,1,pass,partword,_encodepart);
769 /* update the sofar vector */
773 for(j=0,k=i;j<n;j++,k+=ch)
774 sofar[j]+=pcm[j]-work[k];
782 /* duplicate code here as speed is somewhat more important */
783 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
784 float **in,int *nonzero,int ch){
786 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
787 vorbis_info_residue0 *info=look->info;
789 /* move all this setup out later */
790 int samples_per_partition=info->grouping;
791 int partitions_per_word=look->phrasebook->dim;
792 int n=info->end-info->begin;
794 int partvals=n/samples_per_partition;
795 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
796 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(int *));
798 for(i=0;i<ch;i++)if(nonzero[i])break;
799 if(i==ch)return(0); /* no nonzero vectors */
801 for(s=0;s<look->stages;s++){
802 for(i=0,l=0;i<partvals;l++){
805 /* fetch the partition word */
806 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
807 if(temp==-1)goto eopbreak;
808 partword[l]=look->decodemap[temp];
809 if(partword[l]==NULL)goto errout;
812 /* now we decode residual values for the partitions */
813 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
814 if(info->secondstages[partword[l][k]]&(1<<s)){
815 codebook *stagebook=look->partbooks[partword[l][k]][s];
818 if(vorbis_book_decodevv_add(stagebook,in,
819 i*samples_per_partition+info->begin,ch,
820 &vb->opb,samples_per_partition)==-1)
833 vorbis_func_residue residue0_exportbundle={
845 vorbis_func_residue residue1_exportbundle={
857 vorbis_func_residue residue2_exportbundle={