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 https://xiph.org/ *
11 ********************************************************************
13 function: residue backend 0, 1 and 2 implementation
15 ********************************************************************/
17 /* Slow, slow, slow, simpleminded and did I mention it was slow? The
18 encode/decode loops are coded for clarity and performance is not
19 yet even a nagging little idea lurking in the shadows. Oh and BTW,
26 #include "vorbis/codec.h"
27 #include "codec_internal.h"
33 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
38 vorbis_info_residue0 *info;
44 codebook ***partbooks;
53 #if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
55 long *training_data[8][64];
56 float training_max[8][64];
57 float training_min[8][64];
63 } vorbis_look_residue0;
65 void res0_free_info(vorbis_info_residue *i){
66 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
68 memset(info,0,sizeof(*info));
73 void res0_free_look(vorbis_look_residue *i){
77 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
82 for(j=0;j<look->parts;j++){
83 /*fprintf(stderr,"partition %d: ",j);*/
85 if(look->training_data[k][j]){
88 codebook *statebook=look->partbooks[j][k];
90 /* long and short into the same bucket by current convention */
91 sprintf(buffer,"res_sub%d_part%d_pass%d.vqd",look->submap,j,k);
94 for(l=0;l<statebook->entries;l++)
95 fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
99 /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
100 look->training_min[k][j],look->training_max[k][j]);*/
102 _ogg_free(look->training_data[k][j]);
103 look->training_data[k][j]=NULL;
105 /*fprintf(stderr,"\n");*/
108 fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
110 /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
111 (float)look->phrasebits/look->frames,
112 (float)look->postbits/look->frames,
113 (float)(look->postbits+look->phrasebits)/look->frames);*/
117 /*vorbis_info_residue0 *info=look->info;
120 "%ld frames encoded in %ld phrasebits and %ld residue bits "
121 "(%g/frame) \n",look->frames,look->phrasebits,
123 (look->phrasebits+look->resbitsflat)/(float)look->frames);
125 for(j=0;j<look->parts;j++){
127 fprintf(stderr,"\t[%d] == ",j);
128 for(k=0;k<look->stages;k++)
129 if((info->secondstages[j]>>k)&1){
130 fprintf(stderr,"%ld,",look->resbits[j][k]);
131 acc+=look->resbits[j][k];
134 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
135 acc?(float)acc/(look->resvals[j]*info->grouping):0);
137 fprintf(stderr,"\n");*/
139 for(j=0;j<look->parts;j++)
140 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
141 _ogg_free(look->partbooks);
142 for(j=0;j<look->partvals;j++)
143 _ogg_free(look->decodemap[j]);
144 _ogg_free(look->decodemap);
146 memset(look,0,sizeof(*look));
151 static int icount(unsigned int v){
161 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
162 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
164 oggpack_write(opb,info->begin,24);
165 oggpack_write(opb,info->end,24);
167 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
168 code with a partitioned book */
169 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
170 oggpack_write(opb,info->groupbook,8); /* group huffman book */
172 /* secondstages is a bitmask; as encoding progresses pass by pass, a
173 bitmask of one indicates this partition class has bits to write
175 for(j=0;j<info->partitions;j++){
176 if(ov_ilog(info->secondstages[j])>3){
177 /* yes, this is a minor hack due to not thinking ahead */
178 oggpack_write(opb,info->secondstages[j],3);
179 oggpack_write(opb,1,1);
180 oggpack_write(opb,info->secondstages[j]>>3,5);
182 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
183 acc+=icount(info->secondstages[j]);
186 oggpack_write(opb,info->booklist[j],8);
190 /* vorbis_info is for range checking */
191 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
193 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
194 codec_setup_info *ci=vi->codec_setup;
196 info->begin=oggpack_read(opb,24);
197 info->end=oggpack_read(opb,24);
198 info->grouping=oggpack_read(opb,24)+1;
199 info->partitions=oggpack_read(opb,6)+1;
200 info->groupbook=oggpack_read(opb,8);
202 /* check for premature EOP */
203 if(info->groupbook<0)goto errout;
205 for(j=0;j<info->partitions;j++){
206 int cascade=oggpack_read(opb,3);
207 int cflag=oggpack_read(opb,1);
208 if(cflag<0) goto errout;
210 int c=oggpack_read(opb,5);
214 info->secondstages[j]=cascade;
216 acc+=icount(cascade);
219 int book=oggpack_read(opb,8);
220 if(book<0) goto errout;
221 info->booklist[j]=book;
224 if(info->groupbook>=ci->books)goto errout;
226 if(info->booklist[j]>=ci->books)goto errout;
227 if(ci->book_param[info->booklist[j]]->maptype==0)goto errout;
230 /* verify the phrasebook is not specifying an impossible or
231 inconsistent partitioning scheme. */
232 /* modify the phrasebook ranging check from r16327; an early beta
233 encoder had a bug where it used an oversized phrasebook by
234 accident. These files should continue to be playable, but don't
237 int entries = ci->book_param[info->groupbook]->entries;
238 int dim = ci->book_param[info->groupbook]->dim;
240 if (dim<1) goto errout;
242 partvals *= info->partitions;
243 if(partvals > entries) goto errout;
246 info->partvals = partvals;
251 res0_free_info(info);
255 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
256 vorbis_info_residue *vr){
257 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
258 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
259 codec_setup_info *ci=vd->vi->codec_setup;
266 look->parts=info->partitions;
267 look->fullbooks=ci->fullbooks;
268 look->phrasebook=ci->fullbooks+info->groupbook;
269 dim=look->phrasebook->dim;
271 look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
273 for(j=0;j<look->parts;j++){
274 int stages=ov_ilog(info->secondstages[j]);
276 if(stages>maxstage)maxstage=stages;
277 look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
278 for(k=0;k<stages;k++)
279 if(info->secondstages[j]&(1<<k)){
280 look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
282 look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
283 sizeof(***look->training_data));
291 look->partvals*=look->parts;
293 look->stages=maxstage;
294 look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
295 for(j=0;j<look->partvals;j++){
297 long mult=look->partvals/look->parts;
298 look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
303 look->decodemap[j][k]=deco;
306 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
308 static int train_seq=0;
309 look->train_seq=train_seq++;
315 /* break an abstraction and copy some code for performance purposes */
316 static int local_book_besterror(codebook *book,int *a){
319 int minval=book->minval;
321 int qv=book->quantvals;
324 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
325 int p[8]={0,0,0,0,0,0,0,0};
328 for(i=0,o=dim;i<dim;i++){
329 int v = (a[--o]-minval+(del>>1))/del;
330 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
331 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
335 for(i=0,o=dim;i<dim;i++){
336 int v = a[--o]-minval;
337 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
338 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
343 if(book->c->lengthlist[index]<=0){
344 const static_codebook *c=book->c;
346 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
347 int e[8]={0,0,0,0,0,0,0,0};
348 int maxval = book->minval + book->delta*(book->quantvals-1);
349 for(i=0;i<book->entries;i++){
350 if(c->lengthlist[i]>0){
356 if(best==-1 || this<best){
357 memcpy(p,e,sizeof(p));
362 /* assumes the value patterning created by the tools in vq/ */
381 static int _encodepart(oggpack_buffer *opb,int *vec, int n,
382 codebook *book,long *acc){
384 static int _encodepart(oggpack_buffer *opb,int *vec, int n,
392 int entry=local_book_besterror(book,vec+i*dim);
399 bits+=vorbis_book_encode(book,entry,opb);
406 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
409 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
410 vorbis_info_residue0 *info=look->info;
412 /* move all this setup out later */
413 int samples_per_partition=info->grouping;
414 int possible_partitions=info->partitions;
415 int n=info->end-info->begin;
417 int partvals=n/samples_per_partition;
418 long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
419 float scale=100./samples_per_partition;
421 /* we find the partition type for each partition of each
422 channel. We'll go back and do the interleaved encoding in a
423 bit. For now, clarity */
426 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
427 memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
430 for(i=0;i<partvals;i++){
431 int offset=i*samples_per_partition+info->begin;
435 for(k=0;k<samples_per_partition;k++){
436 if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
437 ent+=abs(in[j][offset+k]);
441 for(k=0;k<possible_partitions-1;k++)
442 if(max<=info->classmetric1[k] &&
443 (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
456 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
457 of=fopen(buffer,"a");
458 for(j=0;j<partvals;j++)
459 fprintf(of,"%ld, ",partword[i][j]);
470 /* designed for stereo or other modes where the partition size is an
471 integer multiple of the number of channels encoded in the current
473 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
476 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
477 vorbis_info_residue0 *info=look->info;
479 /* move all this setup out later */
480 int samples_per_partition=info->grouping;
481 int possible_partitions=info->partitions;
482 int n=info->end-info->begin;
484 int partvals=n/samples_per_partition;
485 long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
487 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
492 partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
493 memset(partword[0],0,partvals*sizeof(*partword[0]));
495 for(i=0,l=info->begin/ch;i<partvals;i++){
498 for(j=0;j<samples_per_partition;j+=ch){
499 if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
501 if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
505 for(j=0;j<possible_partitions-1;j++)
506 if(magmax<=info->classmetric1[j] &&
507 angmax<=info->classmetric2[j])
515 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
516 of=fopen(buffer,"a");
517 for(i=0;i<partvals;i++)
518 fprintf(of,"%ld, ",partword[0][i]);
528 static int _01forward(oggpack_buffer *opb,
529 vorbis_look_residue *vl,
533 int (*encode)(oggpack_buffer *,int *,int,
537 int (*encode)(oggpack_buffer *,int *,int,
542 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
543 vorbis_info_residue0 *info=look->info;
549 /* move all this setup out later */
550 int samples_per_partition=info->grouping;
551 int possible_partitions=info->partitions;
552 int partitions_per_word=look->phrasebook->dim;
553 int n=info->end-info->begin;
555 int partvals=n/samples_per_partition;
561 for(j=info->begin;j<info->end;j++){
562 if(in[i][j]>look->tmax)look->tmax=in[i][j];
563 if(in[i][j]<look->tmin)look->tmin=in[i][j];
567 memset(resbits,0,sizeof(resbits));
568 memset(resvals,0,sizeof(resvals));
570 /* we code the partition words for each channel, then the residual
571 words for a partition per channel until we've written all the
572 residual words for that partition word. Then write the next
573 partition channel words... */
575 for(s=0;s<look->stages;s++){
577 for(i=0;i<partvals;){
579 /* first we encode a partition codeword for each channel */
582 long val=partword[j][i];
583 for(k=1;k<partitions_per_word;k++){
584 val*=possible_partitions;
586 val+=partword[j][i+k];
590 if(val<look->phrasebook->entries)
591 look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
592 #if 0 /*def TRAIN_RES*/
600 /* now we encode interleaved residual values for the partitions */
601 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
602 long offset=i*samples_per_partition+info->begin;
605 if(s==0)resvals[partword[j][i]]+=samples_per_partition;
606 if(info->secondstages[partword[j][i]]&(1<<s)){
607 codebook *statebook=look->partbooks[partword[j][i]][s];
611 long *accumulator=NULL;
612 accumulator=look->training_data[s][partword[j][i]];
615 int *samples=in[j]+offset;
616 for(l=0;l<samples_per_partition;l++){
617 if(samples[l]<look->training_min[s][partword[j][i]])
618 look->training_min[s][partword[j][i]]=samples[l];
619 if(samples[l]>look->training_max[s][partword[j][i]])
620 look->training_max[s][partword[j][i]]=samples[l];
623 ret=encode(opb,in[j]+offset,samples_per_partition,
624 statebook,accumulator);
626 ret=encode(opb,in[j]+offset,samples_per_partition,
631 resbits[partword[j][i]]+=ret;
642 /* a truncated packet here just means 'stop working'; it's not an error */
643 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
645 long (*decodepart)(codebook *, float *,
646 oggpack_buffer *,int)){
649 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
650 vorbis_info_residue0 *info=look->info;
652 /* move all this setup out later */
653 int samples_per_partition=info->grouping;
654 int partitions_per_word=look->phrasebook->dim;
655 int max=vb->pcmend>>1;
656 int end=(info->end<max?info->end:max);
657 int n=end-info->begin;
660 int partvals=n/samples_per_partition;
661 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
662 int ***partword=alloca(ch*sizeof(*partword));
665 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
667 for(s=0;s<look->stages;s++){
669 /* each loop decodes on partition codeword containing
670 partitions_per_word partitions */
671 for(i=0,l=0;i<partvals;l++){
673 /* fetch the partition word for each channel */
675 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
677 if(temp==-1 || temp>=info->partvals)goto eopbreak;
678 partword[j][l]=look->decodemap[temp];
679 if(partword[j][l]==NULL)goto errout;
683 /* now we decode residual values for the partitions */
684 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
686 long offset=info->begin+i*samples_per_partition;
687 if(info->secondstages[partword[j][l][k]]&(1<<s)){
688 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
690 if(decodepart(stagebook,in[j]+offset,&vb->opb,
691 samples_per_partition)==-1)goto eopbreak;
703 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
704 float **in,int *nonzero,int ch){
710 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
715 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
716 int **in,int *nonzero,int ch, long **partword, int submap){
725 return _01forward(opb,vl,in,used,partword,_encodepart,submap);
728 return _01forward(opb,vl,in,used,partword,_encodepart);
735 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
736 int **in,int *nonzero,int ch){
742 return(_01class(vb,vl,in,used));
747 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
748 float **in,int *nonzero,int ch){
754 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
759 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
760 int **in,int *nonzero,int ch){
763 if(nonzero[i])used++;
765 return(_2class(vb,vl,in,ch));
770 /* res2 is slightly more different; all the channels are interleaved
771 into a single vector and encoded. */
773 int res2_forward(oggpack_buffer *opb,
774 vorbis_block *vb,vorbis_look_residue *vl,
775 int **in,int *nonzero,int ch, long **partword,int submap){
776 long i,j,k,n=vb->pcmend/2,used=0;
778 /* don't duplicate the code; use a working vector hack for now and
779 reshape ourselves into a single channel res1 */
780 /* ugly; reallocs for each coupling pass :-( */
781 int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
784 if(nonzero[i])used++;
785 for(j=0,k=i;j<n;j++,k+=ch)
791 return _01forward(opb,vl,&work,1,partword,_encodepart,submap);
794 return _01forward(opb,vl,&work,1,partword,_encodepart);
801 /* duplicate code here as speed is somewhat more important */
802 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
803 float **in,int *nonzero,int ch){
805 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
806 vorbis_info_residue0 *info=look->info;
808 /* move all this setup out later */
809 int samples_per_partition=info->grouping;
810 int partitions_per_word=look->phrasebook->dim;
811 int max=(vb->pcmend*ch)>>1;
812 int end=(info->end<max?info->end:max);
813 int n=end-info->begin;
816 int partvals=n/samples_per_partition;
817 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
818 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
820 for(i=0;i<ch;i++)if(nonzero[i])break;
821 if(i==ch)return(0); /* no nonzero vectors */
823 for(s=0;s<look->stages;s++){
824 for(i=0,l=0;i<partvals;l++){
827 /* fetch the partition word */
828 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
829 if(temp==-1 || temp>=info->partvals)goto eopbreak;
830 partword[l]=look->decodemap[temp];
831 if(partword[l]==NULL)goto errout;
834 /* now we decode residual values for the partitions */
835 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
836 if(info->secondstages[partword[l][k]]&(1<<s)){
837 codebook *stagebook=look->partbooks[partword[l][k]][s];
840 if(vorbis_book_decodevv_add(stagebook,in,
841 i*samples_per_partition+info->begin,ch,
842 &vb->opb,samples_per_partition)==-1)
855 const vorbis_func_residue residue0_exportbundle={
866 const vorbis_func_residue residue1_exportbundle={
877 const vorbis_func_residue residue2_exportbundle={