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.37 2001/10/02 00:14:32 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,
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(*ret));
57 memcpy(ret,info,sizeof(*ret));
61 void res0_free_info(vorbis_info_residue *i){
62 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
64 memset(info,0,sizeof(*info));
69 void res0_free_look(vorbis_look_residue *i){
73 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
75 fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
76 (float)look->phrasebits/look->frames,
77 (float)look->postbits/look->frames,
78 (float)(look->postbits+look->phrasebits)/look->frames);
80 /*vorbis_info_residue0 *info=look->info;
83 "%ld frames encoded in %ld phrasebits and %ld residue bits "
84 "(%g/frame) \n",look->frames,look->phrasebits,
86 (look->phrasebits+look->resbitsflat)/(float)look->frames);
88 for(j=0;j<look->parts;j++){
90 fprintf(stderr,"\t[%d] == ",j);
91 for(k=0;k<look->stages;k++)
92 if((info->secondstages[j]>>k)&1){
93 fprintf(stderr,"%ld,",look->resbits[j][k]);
94 acc+=look->resbits[j][k];
97 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
98 acc?(float)acc/(look->resvals[j]*info->grouping):0);
100 fprintf(stderr,"\n");*/
102 for(j=0;j<look->parts;j++)
103 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
104 _ogg_free(look->partbooks);
105 for(j=0;j<look->partvals;j++)
106 _ogg_free(look->decodemap[j]);
107 _ogg_free(look->decodemap);
108 memset(look,0,sizeof(*look));
113 static int ilog(unsigned int v){
122 static int icount(unsigned int v){
132 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
133 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
135 oggpack_write(opb,info->begin,24);
136 oggpack_write(opb,info->end,24);
138 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
139 code with a partitioned book */
140 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
141 oggpack_write(opb,info->groupbook,8); /* group huffman book */
143 /* secondstages is a bitmask; as encoding progresses pass by pass, a
144 bitmask of one indicates this partition class has bits to write
146 for(j=0;j<info->partitions;j++){
147 if(ilog(info->secondstages[j])>3){
148 /* yes, this is a minor hack due to not thinking ahead */
149 oggpack_write(opb,info->secondstages[j],3);
150 oggpack_write(opb,1,1);
151 oggpack_write(opb,info->secondstages[j]>>3,5);
153 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
154 acc+=icount(info->secondstages[j]);
157 oggpack_write(opb,info->booklist[j],8);
161 /* vorbis_info is for range checking */
162 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
164 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
165 codec_setup_info *ci=vi->codec_setup;
167 info->begin=oggpack_read(opb,24);
168 info->end=oggpack_read(opb,24);
169 info->grouping=oggpack_read(opb,24)+1;
170 info->partitions=oggpack_read(opb,6)+1;
171 info->groupbook=oggpack_read(opb,8);
173 for(j=0;j<info->partitions;j++){
174 int cascade=oggpack_read(opb,3);
175 if(oggpack_read(opb,1))
176 cascade|=(oggpack_read(opb,5)<<3);
177 info->secondstages[j]=cascade;
179 acc+=icount(cascade);
182 info->booklist[j]=oggpack_read(opb,8);
184 if(info->groupbook>=ci->books)goto errout;
186 if(info->booklist[j]>=ci->books)goto errout;
190 res0_free_info(info);
194 vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
195 vorbis_info_residue *vr){
196 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
197 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
198 backend_lookup_state *be=vd->backend_state;
204 look->map=vm->mapping;
206 look->parts=info->partitions;
207 look->fullbooks=be->fullbooks;
208 look->phrasebook=be->fullbooks+info->groupbook;
209 dim=look->phrasebook->dim;
211 look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
213 for(j=0;j<look->parts;j++){
214 int stages=ilog(info->secondstages[j]);
216 if(stages>maxstage)maxstage=stages;
217 look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
218 for(k=0;k<stages;k++)
219 if(info->secondstages[j]&(1<<k))
220 look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
224 look->partvals=rint(pow(look->parts,dim));
225 look->stages=maxstage;
226 look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
227 for(j=0;j<look->partvals;j++){
229 long mult=look->partvals/look->parts;
230 look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
235 look->decodemap[j][k]=deco;
243 /* does not guard against invalid settings; eg, a subn of 16 and a
244 subgroup request of 32. Max subn of 128 */
245 static int _interleaved_testhack(float *vec,int n,vorbis_look_residue0 *look,
246 int auxparts,int auxpartnum){
247 vorbis_info_residue0 *info=look->info;
249 float max,localmax=0.f;
254 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
256 /* handle case subgrp==1 outside */
258 if(temp[i]>localmax)localmax=temp[i];
261 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
274 if(temp[i]>localmax)localmax=temp[i];
277 for(i=0;i<auxparts-1;i++)
278 if(auxpartnum<info->blimit[i] &&
279 entropy[info->subgrp[i]]<=info->entmax[i] &&
280 max<=info->ampmax[i])
286 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
287 int auxparts,int auxpartnum){
288 vorbis_info_residue0 *info=look->info;
295 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
298 if(temp[i]>max)max=temp[i];
300 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
305 for(i=0;i<auxparts-1;i++)
306 if(auxpartnum<info->blimit[i] &&
307 entropy<=info->entmax[i] &&
308 max<=info->ampmax[i])
314 static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
315 codebook *book,vorbis_look_residue0 *look){
322 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
327 int entry=vorbis_book_besterror(book,vec+i,step,0);
330 fprintf(f,"%d\n",entry);
333 bits+=vorbis_book_encode(book,entry,opb);
342 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
343 codebook *book,vorbis_look_residue0 *look){
350 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
355 int entry=vorbis_book_besterror(book,vec+i*dim,1,0);
358 fprintf(f,"%d\n",entry);
361 bits+=vorbis_book_encode(book,entry,opb);
370 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
372 int (*classify)(float *,int,vorbis_look_residue0 *,
375 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
376 vorbis_info_residue0 *info=look->info;
378 /* move all this setup out later */
379 int samples_per_partition=info->grouping;
380 int possible_partitions=info->partitions;
381 int n=info->end-info->begin;
383 int partvals=n/samples_per_partition;
384 long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
386 /* we find the partition type for each partition of each
387 channel. We'll go back and do the interleaved encoding in a
388 bit. For now, clarity */
391 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
392 memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
395 for(i=0;i<partvals;i++){
397 /* do the partition decision based on the 'entropy'
400 classify(in[j]+i*samples_per_partition+info->begin,
401 samples_per_partition,look,possible_partitions,i);
411 sprintf(buffer,"resaux_%d.vqd",vb->mode);
412 of=fopen(buffer,"a");
413 for(j=0;j<partvals;j++)
414 fprintf(of,"%ld, ",partword[i][j]);
425 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,
427 int (*classify)(float *,int,vorbis_look_residue0 *,
430 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
431 vorbis_info_residue0 *info=look->info;
433 /* move all this setup out later */
434 int samples_per_partition=info->grouping;
435 int possible_partitions=info->partitions;
436 int n=info->end-info->begin;
438 int partvals=n/samples_per_partition;
439 long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
440 float *work=alloca(sizeof(*work)*samples_per_partition);
442 partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
443 memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
445 for(i=0,j=0,k=0,l=info->begin;i<partvals;i++){
446 for(k=0;k<samples_per_partition;k++){
455 classify(work,samples_per_partition,look,possible_partitions,i);
463 sprintf(buffer,"resaux_%d.vqd",vb->mode);
464 of=fopen(buffer,"a");
465 for(i=0;i<partvals;i++)
466 fprintf(of,"%ld, ",partword[0][i]);
476 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
478 int pass,long **partword,
479 int (*encode)(oggpack_buffer *,float *,int,
480 codebook *,vorbis_look_residue0 *)){
482 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
483 vorbis_info_residue0 *info=look->info;
485 /* move all this setup out later */
486 int samples_per_partition=info->grouping;
487 int possible_partitions=info->partitions;
488 int partitions_per_word=look->phrasebook->dim;
489 int n=info->end-info->begin;
491 int partvals=n/samples_per_partition;
501 sprintf(buffer,"residue_%d#%d.vqd",vb->mode,pass);
502 of=fopen(buffer,"a");
503 for(m=0;m<info->end;m++)
504 fprintf(of,"%.2f, ",in[i][m]);
510 memset(resbits,0,sizeof(resbits));
511 memset(resvals,0,sizeof(resvals));
513 /* we code the partition words for each channel, then the residual
514 words for a partition per channel until we've written all the
515 residual words for that partition word. Then write the next
516 partition channel words... */
518 for(s=(pass==0?0:info->passlimit[pass-1]);s<info->passlimit[pass];s++){
519 for(i=0;i<partvals;){
521 /* first we encode a partition codeword for each channel */
524 long val=partword[j][i];
526 for(k=1;k<partitions_per_word;k++){
527 val*=possible_partitions;
529 val+=partword[j][i+k];
533 if(val<look->phrasebook->entries)
534 ret=vorbis_book_encode(look->phrasebook,val,&vb->opb);
538 look->phrasebits+=ret;
543 /* now we encode interleaved residual values for the partitions */
544 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
545 long offset=i*samples_per_partition+info->begin;
548 if(s==0)resvals[partword[j][i]]+=samples_per_partition;
549 if(info->secondstages[partword[j][i]]&(1<<s)){
550 codebook *statebook=look->partbooks[partword[j][i]][s];
552 int ret=encode(&vb->opb,in[j]+offset,samples_per_partition,
555 resbits[partword[j][i]]+=ret;
566 fprintf(stderr,"%d :: ",vb->mode);
567 for(k=0;k<possible_partitions;k++){
568 fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
570 totalbits+=resbits[k];
573 fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
578 /* a truncated packet here just means 'stop working'; it's not an error */
579 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
581 long (*decodepart)(codebook *, float *,
582 oggpack_buffer *,int)){
585 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
586 vorbis_info_residue0 *info=look->info;
588 /* move all this setup out later */
589 int samples_per_partition=info->grouping;
590 int partitions_per_word=look->phrasebook->dim;
591 int n=info->end-info->begin;
593 int partvals=n/samples_per_partition;
594 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
595 int ***partword=alloca(ch*sizeof(*partword));
598 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
600 for(s=0;s<look->stages;s++){
602 /* each loop decodes on partition codeword containing
603 partitions_pre_word partitions */
604 for(i=0,l=0;i<partvals;l++){
606 /* fetch the partition word for each channel */
608 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
609 if(temp==-1)goto eopbreak;
610 partword[j][l]=look->decodemap[temp];
611 if(partword[j][l]==NULL)goto errout;
615 /* now we decode residual values for the partitions */
616 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
618 long offset=info->begin+i*samples_per_partition;
619 if(info->secondstages[partword[j][l][k]]&(1<<s)){
620 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
622 if(decodepart(stagebook,in[j]+offset,&vb->opb,
623 samples_per_partition)==-1)goto eopbreak;
635 /* residue 0 and 1 are just slight variants of one another. 0 is
636 interleaved, 1 is not */
637 long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
638 float **in,int *nonzero,int ch){
639 /* we encode only the nonzero parts of a bundle */
645 /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
646 return(_01class(vb,vl,in,used,_testhack));
651 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
652 float **in,float **out,int *nonzero,int ch,
653 int pass, long **partword){
654 /* we encode only the nonzero parts of a bundle */
655 int i,j,used=0,n=vb->pcmend/2;
663 int ret=_01forward(vb,vl,in,used,pass,partword,
664 _interleaved_encodepart);
669 out[i][j]-=in[used][j];
677 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
678 float **in,int *nonzero,int ch){
684 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
689 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
690 float **in,float **out,int *nonzero,int ch,
691 int pass, long **partword){
692 int i,j,used=0,n=vb->pcmend/2;
701 int ret=_01forward(vb,vl,in,used,pass,partword,_encodepart);
706 out[i][j]-=in[used][j];
714 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
715 float **in,int *nonzero,int ch){
721 return(_01class(vb,vl,in,used,_testhack));
726 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
727 float **in,int *nonzero,int ch){
733 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
738 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
739 float **in,int *nonzero,int ch){
745 return(_2class(vb,vl,in,used,_testhack));
750 /* res2 is slightly more different; all the channels are interleaved
751 into a single vector and encoded. */
753 int res2_forward(vorbis_block *vb,vorbis_look_residue *vl,
754 float **in,float **out,int *nonzero,int ch,
755 int pass,long **partword){
756 long i,j,k,n=vb->pcmend/2,used=0;
758 /* don't duplicate the code; use a working vector hack for now and
759 reshape ourselves into a single channel res1 */
760 /* ugly; reallocs for each coupling pass :-( */
761 float *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
764 if(nonzero[i])used++;
765 for(j=0,k=i;j<n;j++,k+=ch)
770 int ret=_01forward(vb,vl,&work,1,pass,partword,_encodepart);
771 /* update the sofar vector */
775 for(j=0,k=i;j<n;j++,k+=ch)
776 sofar[j]+=pcm[j]-work[k];
784 /* duplicate code here as speed is somewhat more important */
785 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
786 float **in,int *nonzero,int ch){
788 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
789 vorbis_info_residue0 *info=look->info;
791 /* move all this setup out later */
792 int samples_per_partition=info->grouping;
793 int partitions_per_word=look->phrasebook->dim;
794 int n=info->end-info->begin;
796 int partvals=n/samples_per_partition;
797 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
798 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
800 for(i=0;i<ch;i++)if(nonzero[i])break;
801 if(i==ch)return(0); /* no nonzero vectors */
803 for(s=0;s<look->stages;s++){
804 for(i=0,l=0;i<partvals;l++){
807 /* fetch the partition word */
808 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
809 if(temp==-1)goto eopbreak;
810 partword[l]=look->decodemap[temp];
811 if(partword[l]==NULL)goto errout;
814 /* now we decode residual values for the partitions */
815 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
816 if(info->secondstages[partword[l][k]]&(1<<s)){
817 codebook *stagebook=look->partbooks[partword[l][k]][s];
820 if(vorbis_book_decodevv_add(stagebook,in,
821 i*samples_per_partition+info->begin,ch,
822 &vb->opb,samples_per_partition)==-1)
835 vorbis_func_residue residue0_exportbundle={
847 vorbis_func_residue residue1_exportbundle={
859 vorbis_func_residue residue2_exportbundle={