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-2010 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: residue backend 0, 1 and 2 implementation
14 last mod: $Id: res0.c 17556 2010-10-21 18:25:19Z tterribe $
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,
26 #include <ext/libogg/ogg.h>
28 #include "codec_internal.h"
35 //#define TRAIN_RESAUX 1
37 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
42 vorbis_info_residue0 *info;
48 codebook ***partbooks;
57 #if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
59 long *training_data[8][64];
60 float training_max[8][64];
61 float training_min[8][64];
67 } vorbis_look_residue0;
69 void res0_free_info(vorbis_info_residue *i){
70 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
72 memset(info,0,sizeof(*info));
77 void res0_free_look(vorbis_look_residue *i){
81 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
86 for(j=0;j<look->parts;j++){
87 /*fprintf(stderr,"partition %d: ",j);*/
89 if(look->training_data[k][j]){
92 codebook *statebook=look->partbooks[j][k];
94 /* long and short into the same bucket by current convention */
95 sprintf(buffer,"res_sub%d_part%d_pass%d.vqd",look->submap,j,k);
98 for(l=0;l<statebook->entries;l++)
99 fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
103 /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
104 look->training_min[k][j],look->training_max[k][j]);*/
106 _ogg_free(look->training_data[k][j]);
107 look->training_data[k][j]=NULL;
109 /*fprintf(stderr,"\n");*/
112 fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
114 /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
115 (float)look->phrasebits/look->frames,
116 (float)look->postbits/look->frames,
117 (float)(look->postbits+look->phrasebits)/look->frames);*/
121 /*vorbis_info_residue0 *info=look->info;
124 "%ld frames encoded in %ld phrasebits and %ld residue bits "
125 "(%g/frame) \n",look->frames,look->phrasebits,
127 (look->phrasebits+look->resbitsflat)/(float)look->frames);
129 for(j=0;j<look->parts;j++){
131 fprintf(stderr,"\t[%d] == ",j);
132 for(k=0;k<look->stages;k++)
133 if((info->secondstages[j]>>k)&1){
134 fprintf(stderr,"%ld,",look->resbits[j][k]);
135 acc+=look->resbits[j][k];
138 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
139 acc?(float)acc/(look->resvals[j]*info->grouping):0);
141 fprintf(stderr,"\n");*/
143 for(j=0;j<look->parts;j++)
144 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
145 _ogg_free(look->partbooks);
146 for(j=0;j<look->partvals;j++)
147 _ogg_free(look->decodemap[j]);
148 _ogg_free(look->decodemap);
150 memset(look,0,sizeof(*look));
155 static int ilog(unsigned int v){
164 static int icount(unsigned int v){
174 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
175 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
177 oggpack_write(opb,info->begin,24);
178 oggpack_write(opb,info->end,24);
180 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
181 code with a partitioned book */
182 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
183 oggpack_write(opb,info->groupbook,8); /* group huffman book */
185 /* secondstages is a bitmask; as encoding progresses pass by pass, a
186 bitmask of one indicates this partition class has bits to write
188 for(j=0;j<info->partitions;j++){
189 if(ilog(info->secondstages[j])>3){
190 /* yes, this is a minor hack due to not thinking ahead */
191 oggpack_write(opb,info->secondstages[j],3);
192 oggpack_write(opb,1,1);
193 oggpack_write(opb,info->secondstages[j]>>3,5);
195 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
196 acc+=icount(info->secondstages[j]);
199 oggpack_write(opb,info->booklist[j],8);
203 /* vorbis_info is for range checking */
204 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
206 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
207 codec_setup_info *ci=vi->codec_setup;
209 info->begin=oggpack_read(opb,24);
210 info->end=oggpack_read(opb,24);
211 info->grouping=oggpack_read(opb,24)+1;
212 info->partitions=oggpack_read(opb,6)+1;
213 info->groupbook=oggpack_read(opb,8);
215 /* check for premature EOP */
216 if(info->groupbook<0)goto errout;
218 for(j=0;j<info->partitions;j++){
219 int cascade=oggpack_read(opb,3);
220 int cflag=oggpack_read(opb,1);
221 if(cflag<0) goto errout;
223 int c=oggpack_read(opb,5);
227 info->secondstages[j]=cascade;
229 acc+=icount(cascade);
232 int book=oggpack_read(opb,8);
233 if(book<0) goto errout;
234 info->booklist[j]=book;
237 if(info->groupbook>=ci->books)goto errout;
239 if(info->booklist[j]>=ci->books)goto errout;
240 if(ci->book_param[info->booklist[j]]->maptype==0)goto errout;
243 /* verify the phrasebook is not specifying an impossible or
244 inconsistent partitioning scheme. */
245 /* modify the phrasebook ranging check from r16327; an early beta
246 encoder had a bug where it used an oversized phrasebook by
247 accident. These files should continue to be playable, but don't
250 int entries = ci->book_param[info->groupbook]->entries;
251 int dim = ci->book_param[info->groupbook]->dim;
253 if (dim<1) goto errout;
255 partvals *= info->partitions;
256 if(partvals > entries) goto errout;
259 info->partvals = partvals;
264 res0_free_info(info);
268 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
269 vorbis_info_residue *vr){
270 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
271 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
272 codec_setup_info *ci=vd->vi->codec_setup;
279 look->parts=info->partitions;
280 look->fullbooks=ci->fullbooks;
281 look->phrasebook=ci->fullbooks+info->groupbook;
282 dim=look->phrasebook->dim;
284 look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
286 for(j=0;j<look->parts;j++){
287 int stages=ilog(info->secondstages[j]);
289 if(stages>maxstage)maxstage=stages;
290 look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
291 for(k=0;k<stages;k++)
292 if(info->secondstages[j]&(1<<k)){
293 look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
295 look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
296 sizeof(***look->training_data));
304 look->partvals*=look->parts;
306 look->stages=maxstage;
307 look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
308 for(j=0;j<look->partvals;j++){
310 long mult=look->partvals/look->parts;
311 look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
316 look->decodemap[j][k]=deco;
319 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
321 static int train_seq=0;
322 look->train_seq=train_seq++;
328 /* break an abstraction and copy some code for performance purposes */
329 static int local_book_besterror(codebook *book,int *a){
332 int minval=book->minval;
334 int qv=book->quantvals;
337 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
338 int p[8]={0,0,0,0,0,0,0,0};
341 for(i=0,o=dim;i<dim;i++){
342 int v = (a[--o]-minval+(del>>1))/del;
343 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
344 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
348 for(i=0,o=dim;i<dim;i++){
349 int v = a[--o]-minval;
350 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
351 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
356 if(book->c->lengthlist[index]<=0){
357 const static_codebook *c=book->c;
359 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
360 int e[8]={0,0,0,0,0,0,0,0};
361 int maxval = book->minval + book->delta*(book->quantvals-1);
362 for(i=0;i<book->entries;i++){
363 if(c->lengthlist[i]>0){
369 if(best==-1 || this<best){
370 memcpy(p,e,sizeof(p));
375 /* assumes the value patterning created by the tools in vq/ */
393 static int _encodepart(oggpack_buffer *opb,int *vec, int n,
394 codebook *book,long *acc){
400 int entry=local_book_besterror(book,vec+i*dim);
407 bits+=vorbis_book_encode(book,entry,opb);
414 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
417 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
418 vorbis_info_residue0 *info=look->info;
420 /* move all this setup out later */
421 int samples_per_partition=info->grouping;
422 int possible_partitions=info->partitions;
423 int n=info->end-info->begin;
425 int partvals=n/samples_per_partition;
426 long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
427 float scale=100./samples_per_partition;
429 /* we find the partition type for each partition of each
430 channel. We'll go back and do the interleaved encoding in a
431 bit. For now, clarity */
434 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
435 memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
438 for(i=0;i<partvals;i++){
439 int offset=i*samples_per_partition+info->begin;
443 for(k=0;k<samples_per_partition;k++){
444 if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
445 ent+=abs(in[j][offset+k]);
449 for(k=0;k<possible_partitions-1;k++)
450 if(max<=info->classmetric1[k] &&
451 (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
464 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
465 of=fopen(buffer,"a");
466 for(j=0;j<partvals;j++)
467 fprintf(of,"%ld, ",partword[i][j]);
478 /* designed for stereo or other modes where the partition size is an
479 integer multiple of the number of channels encoded in the current
481 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
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 possible_partitions=info->partitions;
490 int n=info->end-info->begin;
492 int partvals=n/samples_per_partition;
493 long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
495 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
500 partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
501 memset(partword[0],0,partvals*sizeof(*partword[0]));
503 for(i=0,l=info->begin/ch;i<partvals;i++){
506 for(j=0;j<samples_per_partition;j+=ch){
507 if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
509 if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
513 for(j=0;j<possible_partitions-1;j++)
514 if(magmax<=info->classmetric1[j] &&
515 angmax<=info->classmetric2[j])
523 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
524 of=fopen(buffer,"a");
525 for(i=0;i<partvals;i++)
526 fprintf(of,"%ld, ",partword[0][i]);
536 static int _01forward(oggpack_buffer *opb,
537 vorbis_block *vb,vorbis_look_residue *vl,
540 int (*encode)(oggpack_buffer *,int *,int,
544 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
545 vorbis_info_residue0 *info=look->info;
551 /* move all this setup out later */
552 int samples_per_partition=info->grouping;
553 int possible_partitions=info->partitions;
554 int partitions_per_word=look->phrasebook->dim;
555 int n=info->end-info->begin;
557 int partvals=n/samples_per_partition;
563 for(j=info->begin;j<info->end;j++){
564 if(in[i][j]>look->tmax)look->tmax=in[i][j];
565 if(in[i][j]<look->tmin)look->tmin=in[i][j];
569 memset(resbits,0,sizeof(resbits));
570 memset(resvals,0,sizeof(resvals));
572 /* we code the partition words for each channel, then the residual
573 words for a partition per channel until we've written all the
574 residual words for that partition word. Then write the next
575 partition channel words... */
577 for(s=0;s<look->stages;s++){
579 for(i=0;i<partvals;){
581 /* first we encode a partition codeword for each channel */
584 long val=partword[j][i];
585 for(k=1;k<partitions_per_word;k++){
586 val*=possible_partitions;
588 val+=partword[j][i+k];
592 if(val<look->phrasebook->entries)
593 look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
594 #if 0 /*def TRAIN_RES*/
602 /* now we encode interleaved residual values for the partitions */
603 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
604 long offset=i*samples_per_partition+info->begin;
607 if(s==0)resvals[partword[j][i]]+=samples_per_partition;
608 if(info->secondstages[partword[j][i]]&(1<<s)){
609 codebook *statebook=look->partbooks[partword[j][i]][s];
612 long *accumulator=NULL;
615 accumulator=look->training_data[s][partword[j][i]];
618 int *samples=in[j]+offset;
619 for(l=0;l<samples_per_partition;l++){
620 if(samples[l]<look->training_min[s][partword[j][i]])
621 look->training_min[s][partword[j][i]]=samples[l];
622 if(samples[l]>look->training_max[s][partword[j][i]])
623 look->training_max[s][partword[j][i]]=samples[l];
628 ret=encode(opb,in[j]+offset,samples_per_partition,
629 statebook,accumulator);
632 resbits[partword[j][i]]+=ret;
643 fprintf(stderr,"%d :: ",vb->mode);
644 for(k=0;k<possible_partitions;k++){
645 fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
647 totalbits+=resbits[k];
650 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 max=vb->pcmend>>1;
670 int end=(info->end<max?info->end:max);
671 int n=end-info->begin;
674 int partvals=n/samples_per_partition;
675 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
676 int ***partword=alloca(ch*sizeof(*partword));
679 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
681 for(s=0;s<look->stages;s++){
683 /* each loop decodes on partition codeword containing
684 partitions_per_word partitions */
685 for(i=0,l=0;i<partvals;l++){
687 /* fetch the partition word for each channel */
689 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
691 if(temp==-1 || temp>=info->partvals)goto eopbreak;
692 partword[j][l]=look->decodemap[temp];
693 if(partword[j][l]==NULL)goto errout;
697 /* now we decode residual values for the partitions */
698 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
700 long offset=info->begin+i*samples_per_partition;
701 if(info->secondstages[partword[j][l][k]]&(1<<s)){
702 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
704 if(decodepart(stagebook,in[j]+offset,&vb->opb,
705 samples_per_partition)==-1)goto eopbreak;
717 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
718 float **in,int *nonzero,int ch){
724 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
729 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
730 int **in,int *nonzero,int ch, long **partword, int submap){
737 return _01forward(opb,vb,vl,in,used,partword,_encodepart,submap);
743 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
744 int **in,int *nonzero,int ch){
750 return(_01class(vb,vl,in,used));
755 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
756 float **in,int *nonzero,int ch){
762 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
767 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
768 int **in,int *nonzero,int ch){
771 if(nonzero[i])used++;
773 return(_2class(vb,vl,in,ch));
778 /* res2 is slightly more different; all the channels are interleaved
779 into a single vector and encoded. */
781 int res2_forward(oggpack_buffer *opb,
782 vorbis_block *vb,vorbis_look_residue *vl,
783 int **in,int *nonzero,int ch, long **partword,int submap){
784 long i,j,k,n=vb->pcmend/2,used=0;
786 /* don't duplicate the code; use a working vector hack for now and
787 reshape ourselves into a single channel res1 */
788 /* ugly; reallocs for each coupling pass :-( */
789 int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
792 if(nonzero[i])used++;
793 for(j=0,k=i;j<n;j++,k+=ch)
798 return _01forward(opb,vb,vl,&work,1,partword,_encodepart,submap);
804 /* duplicate code here as speed is somewhat more important */
805 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
806 float **in,int *nonzero,int ch){
808 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
809 vorbis_info_residue0 *info=look->info;
811 /* move all this setup out later */
812 int samples_per_partition=info->grouping;
813 int partitions_per_word=look->phrasebook->dim;
814 int max=(vb->pcmend*ch)>>1;
815 int end=(info->end<max?info->end:max);
816 int n=end-info->begin;
819 int partvals=n/samples_per_partition;
820 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
821 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
823 for(i=0;i<ch;i++)if(nonzero[i])break;
824 if(i==ch)return(0); /* no nonzero vectors */
826 for(s=0;s<look->stages;s++){
827 for(i=0,l=0;i<partvals;l++){
830 /* fetch the partition word */
831 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
832 if(temp==-1 || temp>=info->partvals)goto eopbreak;
833 partword[l]=look->decodemap[temp];
834 if(partword[l]==NULL)goto errout;
837 /* now we decode residual values for the partitions */
838 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
839 if(info->secondstages[partword[l][k]]&(1<<s)){
840 codebook *stagebook=look->partbooks[partword[l][k]][s];
843 if(vorbis_book_decodevv_add(stagebook,in,
844 i*samples_per_partition+info->begin,ch,
845 &vb->opb,samples_per_partition)==-1)
858 const vorbis_func_residue residue0_exportbundle={
869 const vorbis_func_residue residue1_exportbundle={
880 const vorbis_func_residue residue2_exportbundle={