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.31 2001/06/15 21:15:40 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;
48 /*long resbits[32][32];
54 } vorbis_look_residue0;
56 vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
57 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
58 vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
59 memcpy(ret,info,sizeof(vorbis_info_residue0));
63 void res0_free_info(vorbis_info_residue *i){
65 memset(i,0,sizeof(vorbis_info_residue0));
70 void res0_free_look(vorbis_look_residue *i){
74 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
75 vorbis_info_residue0 *info=look->info;
78 "%ld frames encoded in %ld phrasebits and %ld residue bits "
79 "(%g/frame) \n",look->frames,look->phrasebits,
81 (look->phrasebits+look->resbitsflat)/(float)look->frames);
83 for(j=0;j<look->parts;j++){
85 fprintf(stderr,"\t[%d] == ",j);
86 for(k=0;k<look->stages;k++)
87 if((info->secondstages[j]>>k)&1){
88 fprintf(stderr,"%ld,",look->resbits[j][k]);
89 acc+=look->resbits[j][k];
92 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
93 acc?(float)acc/(look->resvals[j]*info->grouping):0);
95 fprintf(stderr,"\n");*/
97 for(j=0;j<look->parts;j++)
98 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
99 _ogg_free(look->partbooks);
100 for(j=0;j<look->partvals;j++)
101 _ogg_free(look->decodemap[j]);
102 _ogg_free(look->decodemap);
103 memset(i,0,sizeof(vorbis_look_residue0));
108 static int ilog(unsigned int v){
117 static int icount(unsigned int v){
127 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
128 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
130 oggpack_write(opb,info->begin,24);
131 oggpack_write(opb,info->end,24);
133 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
134 code with a partitioned book */
135 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
136 oggpack_write(opb,info->groupbook,8); /* group huffman book */
138 /* secondstages is a bitmask; as encoding progresses pass by pass, a
139 bitmask of one indicates this partition class has bits to write
141 for(j=0;j<info->partitions;j++){
142 if(ilog(info->secondstages[j])>3){
143 /* yes, this is a minor hack due to not thinking ahead */
144 oggpack_write(opb,info->secondstages[j],3);
145 oggpack_write(opb,1,1);
146 oggpack_write(opb,info->secondstages[j]>>3,5);
148 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
149 acc+=icount(info->secondstages[j]);
152 oggpack_write(opb,info->booklist[j],8);
156 /* vorbis_info is for range checking */
157 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
159 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
160 codec_setup_info *ci=vi->codec_setup;
162 info->begin=oggpack_read(opb,24);
163 info->end=oggpack_read(opb,24);
164 info->grouping=oggpack_read(opb,24)+1;
165 info->partitions=oggpack_read(opb,6)+1;
166 info->groupbook=oggpack_read(opb,8);
168 for(j=0;j<info->partitions;j++){
169 int cascade=oggpack_read(opb,3);
170 if(oggpack_read(opb,1))
171 cascade|=(oggpack_read(opb,5)<<3);
172 info->secondstages[j]=cascade;
174 acc+=icount(cascade);
177 info->booklist[j]=oggpack_read(opb,8);
179 if(info->groupbook>=ci->books)goto errout;
181 if(info->booklist[j]>=ci->books)goto errout;
185 res0_free_info(info);
189 vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
190 vorbis_info_residue *vr){
191 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
192 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(vorbis_look_residue0));
193 backend_lookup_state *be=vd->backend_state;
199 look->map=vm->mapping;
201 look->parts=info->partitions;
202 look->fullbooks=be->fullbooks;
203 look->phrasebook=be->fullbooks+info->groupbook;
204 dim=look->phrasebook->dim;
206 look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
208 for(j=0;j<look->parts;j++){
209 int stages=ilog(info->secondstages[j]);
211 if(stages>maxstage)maxstage=stages;
212 look->partbooks[j]=_ogg_calloc(stages,sizeof(codebook *));
213 for(k=0;k<stages;k++)
214 if(info->secondstages[j]&(1<<k))
215 look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
219 look->partvals=rint(pow(look->parts,dim));
220 look->stages=maxstage;
221 look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
222 for(j=0;j<look->partvals;j++){
224 long mult=look->partvals/look->parts;
225 look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
230 look->decodemap[j][k]=deco;
238 /* does not guard against invalid settings; eg, a subn of 16 and a
239 subgroup request of 32. Max subn of 128 */
240 static int _interleaved_testhack(float *vec,int n,vorbis_look_residue0 *look,
241 int auxparts,int auxpartnum){
242 vorbis_info_residue0 *info=look->info;
244 float max,localmax=0.f;
249 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
251 /* handle case subgrp==1 outside */
253 if(temp[i]>localmax)localmax=temp[i];
256 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
269 if(temp[i]>localmax)localmax=temp[i];
272 for(i=0;i<auxparts-1;i++)
273 if(auxpartnum<info->blimit[i] &&
274 entropy[info->subgrp[i]]<=info->entmax[i] &&
275 max<=info->ampmax[i])
281 static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
282 int auxparts,int auxpartnum){
283 vorbis_info_residue0 *info=look->info;
285 float max,localmax=0.f;
290 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
292 /* handle case subgrp==1 outside */
294 if(temp[i]>localmax)localmax=temp[i];
297 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
305 temp[i]=temp[i*2]+temp[i*2+1];
309 if(temp[i]>localmax)localmax=temp[i];
312 for(i=0;i<auxparts-1;i++)
313 if(auxpartnum<info->blimit[i] &&
314 entropy[info->subgrp[i]]<=info->entmax[i] &&
315 max<=info->ampmax[i])
321 static int _interleaved_encodepart(oggpack_buffer *opb,float *vec, int n,
322 codebook *book,vorbis_look_residue0 *look){
329 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
334 int entry=vorbis_book_besterror(book,vec+i,step,0);
337 fprintf(f,"%d\n",entry);
340 bits+=vorbis_book_encode(book,entry,opb);
349 static int _encodepart(oggpack_buffer *opb,float *vec, int n,
350 codebook *book,vorbis_look_residue0 *look){
357 sprintf(buf,"res0_b%d.vqd",book-look->fullbooks);
362 int entry=vorbis_book_besterror(book,vec+i*dim,1,0);
365 fprintf(f,"%d\n",entry);
368 bits+=vorbis_book_encode(book,entry,opb);
377 static int _01forward(vorbis_block *vb,vorbis_look_residue *vl,
379 int (*classify)(float *,int,vorbis_look_residue0 *,
381 int (*encode)(oggpack_buffer *,float *,int,
382 codebook *,vorbis_look_residue0 *)){
384 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
385 vorbis_info_residue0 *info=look->info;
387 /* move all this setup out later */
388 int samples_per_partition=info->grouping;
389 int possible_partitions=info->partitions;
390 int partitions_per_word=look->phrasebook->dim;
391 int n=info->end-info->begin;
393 int partvals=n/samples_per_partition;
394 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
395 long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
403 sprintf(buffer,"residue_%d.vqd",vb->mode);
404 of=fopen(buffer,"a");
405 for(m=0;m<info->end;m++)
406 fprintf(of,"%.2f, ",in[i][m]);
413 partvals=partwords*partitions_per_word;
415 /* we find the patition type for each partition of each
416 channel. We'll go back and do the interleaved encoding in a
417 bit. For now, clarity */
420 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
421 memset(partword[i],0,n/samples_per_partition*sizeof(long));
424 for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
426 /* do the partition decision based on the 'entropy'
429 classify(in[j]+i,samples_per_partition,look,possible_partitions,l);
433 /* we code the partition words for each channel, then the residual
434 words for a partition per channel until we've written all the
435 residual words for that partition word. Then write the next
436 partition channel words... */
439 for(s=0;s<look->stages;s++){
440 for(i=info->begin,l=0;i<info->end;){
442 /* first we encode a partition codeword for each channel */
445 long val=partword[j][l];
447 for(k=1;k<partitions_per_word;k++)
448 val= val*possible_partitions+partword[j][l+k];
449 ret=vorbis_book_encode(look->phrasebook,val,&vb->opb);
450 /*look->phrasebits+=ret;*/
454 /* now we encode interleaved residual values for the partitions */
455 for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition){
458 /*if(s==0)look->resvals[partword[j][l]]++;*/
459 if(info->secondstages[partword[j][l]]&(1<<s)){
460 codebook *statebook=look->partbooks[partword[j][l]][s];
462 int ret=encode(&vb->opb,in[j]+i,samples_per_partition,
464 /*look->resbits[partword[j][l]][s]+=ret;
465 look->resbitsflat+=ret;*/
477 /* a truncated packet here just means 'stop working'; it's not an error */
478 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
480 long (*decodepart)(codebook *, float *,
481 oggpack_buffer *,int)){
484 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
485 vorbis_info_residue0 *info=look->info;
487 /* move all this setup out later */
488 int samples_per_partition=info->grouping;
489 int partitions_per_word=look->phrasebook->dim;
490 int n=info->end-info->begin;
492 int partvals=n/samples_per_partition;
493 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
494 int ***partword=alloca(ch*sizeof(int **));
495 partvals=partwords*partitions_per_word;
498 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(int *));
500 for(s=0;s<look->stages;s++){
501 for(i=info->begin,l=0;i<info->end;l++){
504 /* fetch the partition word for each channel */
506 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
507 if(temp==-1)goto eopbreak;
508 partword[j][l]=look->decodemap[temp];
509 if(partword[j][l]==NULL)goto errout;
513 /* now we decode residual values for the partitions */
514 for(k=0;k<partitions_per_word;k++,i+=samples_per_partition)
516 if(info->secondstages[partword[j][l][k]]&(1<<s)){
517 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
519 if(decodepart(stagebook,in[j]+i,&vb->opb,
520 samples_per_partition)==-1)goto eopbreak;
532 /* residue 0 and 1 are just slight variants of one another. 0 is
533 interleaved, 1 is not */
534 int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
536 return(_01forward(vb,vl,in,ch,_interleaved_testhack,_interleaved_encodepart));
539 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
540 return(_01inverse(vb,vl,in,ch,vorbis_book_decodevs_add));
543 int res1_forward(vorbis_block *vb,vorbis_look_residue *vl,
545 return(_01forward(vb,vl,in,ch,_testhack,_encodepart));
548 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
549 return(_01inverse(vb,vl,in,ch,vorbis_book_decodev_add));
552 /* res2 is slightly more different; all the channels are interleaved
553 into a single vector and encoded. */
554 int res2_forward(vorbis_block *vb,vorbis_look_residue *vl,
556 long i,j,k,n=vb->pcmend/2;
558 /* don't duplicate the code; use a working vector hack for now and
559 reshape ourselves into a single channel res1 */
560 float *work=_vorbis_block_alloc(vb,ch*n*sizeof(float));
562 float *pcm=vb->pcm[i];
563 for(j=0,k=i;j<n;j++,k+=ch)
567 return(_01forward(vb,vl,&work,1,_testhack,_encodepart));
570 /* duplicate code here as speed is somewhat more important */
571 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
573 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
574 vorbis_info_residue0 *info=look->info;
576 /* move all this setup out later */
577 int samples_per_partition=info->grouping;
578 int partitions_per_word=look->phrasebook->dim;
579 int n=info->end-info->begin;
581 int partvals=n/samples_per_partition;
582 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
583 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(int *));
584 partvals=partwords*partitions_per_word;
586 for(s=0;s<look->stages;s++){
587 for(i=info->begin,l=0;i<info->end;l++){
590 /* fetch the partition word */
591 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
592 if(temp==-1)goto eopbreak;
593 partword[l]=look->decodemap[temp];
594 if(partword[l]==NULL)goto errout;
597 /* now we decode residual values for the partitions */
598 for(k=0;k<partitions_per_word;k++,i+=samples_per_partition)
599 if(info->secondstages[partword[l][k]]&(1<<s)){
600 codebook *stagebook=look->partbooks[partword[l][k]][s];
603 if(vorbis_book_decodevv_add(stagebook,in,i,ch,
604 &vb->opb,samples_per_partition)==-1)
617 vorbis_func_residue residue0_exportbundle={
628 vorbis_func_residue residue1_exportbundle={
639 vorbis_func_residue residue2_exportbundle={