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 16962 2010-03-11 07:30:34Z xiphmont $
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"
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;
254 partvals *= info->partitions;
255 if(partvals > entries) goto errout;
258 info->partvals = partvals;
263 res0_free_info(info);
267 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
268 vorbis_info_residue *vr){
269 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
270 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
271 codec_setup_info *ci=vd->vi->codec_setup;
278 look->parts=info->partitions;
279 look->fullbooks=ci->fullbooks;
280 look->phrasebook=ci->fullbooks+info->groupbook;
281 dim=look->phrasebook->dim;
283 look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
285 for(j=0;j<look->parts;j++){
286 int stages=ilog(info->secondstages[j]);
288 if(stages>maxstage)maxstage=stages;
289 look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
290 for(k=0;k<stages;k++)
291 if(info->secondstages[j]&(1<<k)){
292 look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
294 look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
295 sizeof(***look->training_data));
303 look->partvals*=look->parts;
305 look->stages=maxstage;
306 look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
307 for(j=0;j<look->partvals;j++){
309 long mult=look->partvals/look->parts;
310 look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
315 look->decodemap[j][k]=deco;
318 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
320 static int train_seq=0;
321 look->train_seq=train_seq++;
327 /* break an abstraction and copy some code for performance purposes */
328 static int local_book_besterror(codebook *book,int *a){
331 int minval=book->minval;
333 int qv=book->quantvals;
336 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
337 int p[8]={0,0,0,0,0,0,0,0};
340 for(i=0,o=dim;i<dim;i++){
341 int v = (a[--o]-minval+(del>>1))/del;
342 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
343 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
347 for(i=0,o=dim;i<dim;i++){
348 int v = a[--o]-minval;
349 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
350 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
355 if(book->c->lengthlist[index]<=0){
356 const static_codebook *c=book->c;
358 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
359 int e[8]={0,0,0,0,0,0,0,0};
360 int maxval = book->minval + book->delta*(book->quantvals-1);
361 for(i=0;i<book->entries;i++){
362 if(c->lengthlist[i]>0){
368 if(best==-1 || this<best){
369 memcpy(p,e,sizeof(p));
374 /* assumes the value patterning created by the tools in vq/ */
392 static int _encodepart(oggpack_buffer *opb,int *vec, int n,
393 codebook *book,long *acc){
399 int entry=local_book_besterror(book,vec+i*dim);
406 bits+=vorbis_book_encode(book,entry,opb);
413 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
416 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
417 vorbis_info_residue0 *info=look->info;
419 /* move all this setup out later */
420 int samples_per_partition=info->grouping;
421 int possible_partitions=info->partitions;
422 int n=info->end-info->begin;
424 int partvals=n/samples_per_partition;
425 long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
426 float scale=100./samples_per_partition;
428 /* we find the partition type for each partition of each
429 channel. We'll go back and do the interleaved encoding in a
430 bit. For now, clarity */
433 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
434 memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
437 for(i=0;i<partvals;i++){
438 int offset=i*samples_per_partition+info->begin;
442 for(k=0;k<samples_per_partition;k++){
443 if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
444 ent+=abs(in[j][offset+k]);
448 for(k=0;k<possible_partitions-1;k++)
449 if(max<=info->classmetric1[k] &&
450 (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
463 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
464 of=fopen(buffer,"a");
465 for(j=0;j<partvals;j++)
466 fprintf(of,"%ld, ",partword[i][j]);
477 /* designed for stereo or other modes where the partition size is an
478 integer multiple of the number of channels encoded in the current
480 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
483 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
484 vorbis_info_residue0 *info=look->info;
486 /* move all this setup out later */
487 int samples_per_partition=info->grouping;
488 int possible_partitions=info->partitions;
489 int n=info->end-info->begin;
491 int partvals=n/samples_per_partition;
492 long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
494 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
499 partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
500 memset(partword[0],0,partvals*sizeof(*partword[0]));
502 for(i=0,l=info->begin/ch;i<partvals;i++){
505 for(j=0;j<samples_per_partition;j+=ch){
506 if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
508 if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
512 for(j=0;j<possible_partitions-1;j++)
513 if(magmax<=info->classmetric1[j] &&
514 angmax<=info->classmetric2[j])
522 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
523 of=fopen(buffer,"a");
524 for(i=0;i<partvals;i++)
525 fprintf(of,"%ld, ",partword[0][i]);
535 static int _01forward(oggpack_buffer *opb,
536 vorbis_block *vb,vorbis_look_residue *vl,
539 int (*encode)(oggpack_buffer *,int *,int,
543 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
544 vorbis_info_residue0 *info=look->info;
550 /* move all this setup out later */
551 int samples_per_partition=info->grouping;
552 int possible_partitions=info->partitions;
553 int partitions_per_word=look->phrasebook->dim;
554 int n=info->end-info->begin;
556 int partvals=n/samples_per_partition;
562 for(j=info->begin;j<info->end;j++){
563 if(in[i][j]>look->tmax)look->tmax=in[i][j];
564 if(in[i][j]<look->tmin)look->tmin=in[i][j];
568 memset(resbits,0,sizeof(resbits));
569 memset(resvals,0,sizeof(resvals));
571 /* we code the partition words for each channel, then the residual
572 words for a partition per channel until we've written all the
573 residual words for that partition word. Then write the next
574 partition channel words... */
576 for(s=0;s<look->stages;s++){
578 for(i=0;i<partvals;){
580 /* first we encode a partition codeword for each channel */
583 long val=partword[j][i];
584 for(k=1;k<partitions_per_word;k++){
585 val*=possible_partitions;
587 val+=partword[j][i+k];
591 if(val<look->phrasebook->entries)
592 look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
593 #if 0 /*def TRAIN_RES*/
601 /* now we encode interleaved residual values for the partitions */
602 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
603 long offset=i*samples_per_partition+info->begin;
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 int *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(opb,in[j]+offset,samples_per_partition,
628 statebook,accumulator);
631 resbits[partword[j][i]]+=ret;
642 fprintf(stderr,"%d :: ",vb->mode);
643 for(k=0;k<possible_partitions;k++){
644 fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
646 totalbits+=resbits[k];
649 fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
655 /* a truncated packet here just means 'stop working'; it's not an error */
656 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
658 long (*decodepart)(codebook *, float *,
659 oggpack_buffer *,int)){
662 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
663 vorbis_info_residue0 *info=look->info;
665 /* move all this setup out later */
666 int samples_per_partition=info->grouping;
667 int partitions_per_word=look->phrasebook->dim;
668 int max=vb->pcmend>>1;
669 int end=(info->end<max?info->end:max);
670 int n=end-info->begin;
673 int partvals=n/samples_per_partition;
674 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
675 int ***partword=alloca(ch*sizeof(*partword));
678 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
680 for(s=0;s<look->stages;s++){
682 /* each loop decodes on partition codeword containing
683 partitions_per_word partitions */
684 for(i=0,l=0;i<partvals;l++){
686 /* fetch the partition word for each channel */
688 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
690 if(temp==-1 || temp>=info->partvals)goto eopbreak;
691 partword[j][l]=look->decodemap[temp];
692 if(partword[j][l]==NULL)goto errout;
696 /* now we decode residual values for the partitions */
697 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
699 long offset=info->begin+i*samples_per_partition;
700 if(info->secondstages[partword[j][l][k]]&(1<<s)){
701 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
703 if(decodepart(stagebook,in[j]+offset,&vb->opb,
704 samples_per_partition)==-1)goto eopbreak;
716 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
717 float **in,int *nonzero,int ch){
723 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
728 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
729 int **in,int *nonzero,int ch, long **partword, int submap){
736 return _01forward(opb,vb,vl,in,used,partword,_encodepart,submap);
742 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
743 int **in,int *nonzero,int ch){
749 return(_01class(vb,vl,in,used));
754 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
755 float **in,int *nonzero,int ch){
761 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
766 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
767 int **in,int *nonzero,int ch){
770 if(nonzero[i])used++;
772 return(_2class(vb,vl,in,ch));
777 /* res2 is slightly more different; all the channels are interleaved
778 into a single vector and encoded. */
780 int res2_forward(oggpack_buffer *opb,
781 vorbis_block *vb,vorbis_look_residue *vl,
782 int **in,int *nonzero,int ch, long **partword,int submap){
783 long i,j,k,n=vb->pcmend/2,used=0;
785 /* don't duplicate the code; use a working vector hack for now and
786 reshape ourselves into a single channel res1 */
787 /* ugly; reallocs for each coupling pass :-( */
788 int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
791 if(nonzero[i])used++;
792 for(j=0,k=i;j<n;j++,k+=ch)
797 return _01forward(opb,vb,vl,&work,1,partword,_encodepart,submap);
803 /* duplicate code here as speed is somewhat more important */
804 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
805 float **in,int *nonzero,int ch){
807 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
808 vorbis_info_residue0 *info=look->info;
810 /* move all this setup out later */
811 int samples_per_partition=info->grouping;
812 int partitions_per_word=look->phrasebook->dim;
813 int max=(vb->pcmend*ch)>>1;
814 int end=(info->end<max?info->end:max);
815 int n=end-info->begin;
818 int partvals=n/samples_per_partition;
819 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
820 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
822 for(i=0;i<ch;i++)if(nonzero[i])break;
823 if(i==ch)return(0); /* no nonzero vectors */
825 for(s=0;s<look->stages;s++){
826 for(i=0,l=0;i<partvals;l++){
829 /* fetch the partition word */
830 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
831 if(temp==-1 || temp>info->partvals)goto eopbreak;
832 partword[l]=look->decodemap[temp];
833 if(partword[l]==NULL)goto errout;
836 /* now we decode residual values for the partitions */
837 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
838 if(info->secondstages[partword[l][k]]&(1<<s)){
839 codebook *stagebook=look->partbooks[partword[l][k]][s];
842 if(vorbis_book_decodevv_add(stagebook,in,
843 i*samples_per_partition+info->begin,ch,
844 &vb->opb,samples_per_partition)==-1)
857 const vorbis_func_residue residue0_exportbundle={
868 const vorbis_func_residue residue1_exportbundle={
879 const vorbis_func_residue residue2_exportbundle={