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-2007 *
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 13293 2007-07-24 00:09:47Z 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"
34 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
39 vorbis_info_residue0
*info
;
45 codebook
***partbooks
;
54 #if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
56 long *training_data
[8][64];
57 float training_max
[8][64];
58 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_part%d_pass%d.vqd",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 ilog(unsigned int v
){
160 static int icount(unsigned int v
){
170 void res0_pack(vorbis_info_residue
*vr
,oggpack_buffer
*opb
){
171 vorbis_info_residue0
*info
=(vorbis_info_residue0
*)vr
;
173 oggpack_write(opb
,info
->begin
,24);
174 oggpack_write(opb
,info
->end
,24);
176 oggpack_write(opb
,info
->grouping
-1,24); /* residue vectors to group and
177 code with a partitioned book */
178 oggpack_write(opb
,info
->partitions
-1,6); /* possible partition choices */
179 oggpack_write(opb
,info
->groupbook
,8); /* group huffman book */
181 /* secondstages is a bitmask; as encoding progresses pass by pass, a
182 bitmask of one indicates this partition class has bits to write
184 for(j
=0;j
<info
->partitions
;j
++){
185 if(ilog(info
->secondstages
[j
])>3){
186 /* yes, this is a minor hack due to not thinking ahead */
187 oggpack_write(opb
,info
->secondstages
[j
],3);
188 oggpack_write(opb
,1,1);
189 oggpack_write(opb
,info
->secondstages
[j
]>>3,5);
191 oggpack_write(opb
,info
->secondstages
[j
],4); /* trailing zero */
192 acc
+=icount(info
->secondstages
[j
]);
195 oggpack_write(opb
,info
->booklist
[j
],8);
199 /* vorbis_info is for range checking */
200 vorbis_info_residue
*res0_unpack(vorbis_info
*vi
,oggpack_buffer
*opb
){
202 vorbis_info_residue0
*info
=_ogg_calloc(1,sizeof(*info
));
203 codec_setup_info
*ci
=vi
->codec_setup
;
205 info
->begin
=oggpack_read(opb
,24);
206 info
->end
=oggpack_read(opb
,24);
207 info
->grouping
=oggpack_read(opb
,24)+1;
208 info
->partitions
=oggpack_read(opb
,6)+1;
209 info
->groupbook
=oggpack_read(opb
,8);
211 for(j
=0;j
<info
->partitions
;j
++){
212 int cascade
=oggpack_read(opb
,3);
213 if(oggpack_read(opb
,1))
214 cascade
|=(oggpack_read(opb
,5)<<3);
215 info
->secondstages
[j
]=cascade
;
217 acc
+=icount(cascade
);
220 info
->booklist
[j
]=oggpack_read(opb
,8);
222 if(info
->groupbook
>=ci
->books
)goto errout
;
224 if(info
->booklist
[j
]>=ci
->books
)goto errout
;
228 res0_free_info(info
);
232 vorbis_look_residue
*res0_look(vorbis_dsp_state
*vd
,
233 vorbis_info_residue
*vr
){
234 vorbis_info_residue0
*info
=(vorbis_info_residue0
*)vr
;
235 vorbis_look_residue0
*look
=_ogg_calloc(1,sizeof(*look
));
236 codec_setup_info
*ci
=vd
->vi
->codec_setup
;
243 look
->parts
=info
->partitions
;
244 look
->fullbooks
=ci
->fullbooks
;
245 look
->phrasebook
=ci
->fullbooks
+info
->groupbook
;
246 dim
=look
->phrasebook
->dim
;
248 look
->partbooks
=_ogg_calloc(look
->parts
,sizeof(*look
->partbooks
));
250 for(j
=0;j
<look
->parts
;j
++){
251 int stages
=ilog(info
->secondstages
[j
]);
253 if(stages
>maxstage
)maxstage
=stages
;
254 look
->partbooks
[j
]=_ogg_calloc(stages
,sizeof(*look
->partbooks
[j
]));
255 for(k
=0;k
<stages
;k
++)
256 if(info
->secondstages
[j
]&(1<<k
)){
257 look
->partbooks
[j
][k
]=ci
->fullbooks
+info
->booklist
[acc
++];
259 look
->training_data
[k
][j
]=_ogg_calloc(look
->partbooks
[j
][k
]->entries
,
260 sizeof(***look
->training_data
));
266 look
->partvals
=rint(pow((float)look
->parts
,(float)dim
));
267 look
->stages
=maxstage
;
268 look
->decodemap
=_ogg_malloc(look
->partvals
*sizeof(*look
->decodemap
));
269 for(j
=0;j
<look
->partvals
;j
++){
271 long mult
=look
->partvals
/look
->parts
;
272 look
->decodemap
[j
]=_ogg_malloc(dim
*sizeof(*look
->decodemap
[j
]));
277 look
->decodemap
[j
][k
]=deco
;
280 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
282 static int train_seq
=0;
283 look
->train_seq
=train_seq
++;
289 /* break an abstraction and copy some code for performance purposes */
290 static int local_book_besterror(codebook
*book
,float *a
){
291 int dim
=book
->dim
,i
,k
,o
;
293 encode_aux_threshmatch
*tt
=book
->c
->thresh_tree
;
295 /* find the quant val of each scalar */
296 for(k
=0,o
=dim
;k
<dim
;++k
){
300 if(val
<tt
->quantthresh
[i
]){
301 if(val
<tt
->quantthresh
[i
-1]){
303 if(val
>=tt
->quantthresh
[i
-1])
308 for(++i
;i
<tt
->threshvals
-1;++i
)
309 if(val
<tt
->quantthresh
[i
])break;
313 best
=(best
*tt
->quantvals
)+tt
->quantmap
[i
];
315 /* regular lattices are easy :-) */
317 if(book
->c
->lengthlist
[best
]<=0){
318 const static_codebook
*c
=book
->c
;
321 float *e
=book
->valuelist
;
323 for(i
=0;i
<book
->entries
;i
++){
324 if(c
->lengthlist
[i
]>0){
327 float val
=(e
[j
]-a
[j
]);
330 if(best
==-1 || this<bestf
){
340 float *ptr
=book
->valuelist
+best
*dim
;
348 static int _encodepart(oggpack_buffer
*opb
,float *vec
, int n
,
349 codebook
*book
,long *acc
){
355 int entry
=local_book_besterror(book
,vec
+i
*dim
);
362 bits
+=vorbis_book_encode(book
,entry
,opb
);
369 static long **_01class(vorbis_block
*vb
,vorbis_look_residue
*vl
,
372 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
373 vorbis_info_residue0
*info
=look
->info
;
374 vorbis_info
*vi
=vb
->vd
->vi
;
375 codec_setup_info
*ci
=vi
->codec_setup
;
377 /* move all this setup out later */
378 int samples_per_partition
=info
->grouping
;
379 int possible_partitions
=info
->partitions
;
380 int n
=info
->end
-info
->begin
;
382 int partvals
=n
/samples_per_partition
;
383 long **partword
=_vorbis_block_alloc(vb
,ch
*sizeof(*partword
));
384 float scale
=100./samples_per_partition
;
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
++){
396 int offset
=i
*samples_per_partition
+info
->begin
;
400 for(k
=0;k
<samples_per_partition
;k
++){
401 if(fabs(in
[j
][offset
+k
])>max
)max
=fabs(in
[j
][offset
+k
]);
402 ent
+=fabs(rint(in
[j
][offset
+k
]));
406 for(k
=0;k
<possible_partitions
-1;k
++)
407 if(max
<=info
->classmetric1
[k
] &&
408 (info
->classmetric2
[k
]<0 || (int)ent
<info
->classmetric2
[k
]))
421 sprintf(buffer
,"resaux_%d.vqd",look
->train_seq
);
422 of
=fopen(buffer
,"a");
423 for(j
=0;j
<partvals
;j
++)
424 fprintf(of
,"%ld, ",partword
[i
][j
]);
435 /* designed for stereo or other modes where the partition size is an
436 integer multiple of the number of channels encoded in the current
438 static long **_2class(vorbis_block
*vb
,vorbis_look_residue
*vl
,float **in
,
441 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
442 vorbis_info_residue0
*info
=look
->info
;
444 /* move all this setup out later */
445 int samples_per_partition
=info
->grouping
;
446 int possible_partitions
=info
->partitions
;
447 int n
=info
->end
-info
->begin
;
449 int partvals
=n
/samples_per_partition
;
450 long **partword
=_vorbis_block_alloc(vb
,sizeof(*partword
));
452 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
457 partword
[0]=_vorbis_block_alloc(vb
,n
*ch
/samples_per_partition
*sizeof(*partword
[0]));
458 memset(partword
[0],0,n
*ch
/samples_per_partition
*sizeof(*partword
[0]));
460 for(i
=0,l
=info
->begin
/ch
;i
<partvals
;i
++){
463 for(j
=0;j
<samples_per_partition
;j
+=ch
){
464 if(fabs(in
[0][l
])>magmax
)magmax
=fabs(in
[0][l
]);
466 if(fabs(in
[k
][l
])>angmax
)angmax
=fabs(in
[k
][l
]);
470 for(j
=0;j
<possible_partitions
-1;j
++)
471 if(magmax
<=info
->classmetric1
[j
] &&
472 angmax
<=info
->classmetric2
[j
])
480 sprintf(buffer
,"resaux_%d.vqd",look
->train_seq
);
481 of
=fopen(buffer
,"a");
482 for(i
=0;i
<partvals
;i
++)
483 fprintf(of
,"%ld, ",partword
[0][i
]);
493 static int _01forward(oggpack_buffer
*opb
,
494 vorbis_block
*vb
,vorbis_look_residue
*vl
,
497 int (*encode
)(oggpack_buffer
*,float *,int,
500 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
501 vorbis_info_residue0
*info
=look
->info
;
503 vorbis_dsp_state
*vd
=vb
->vd
;
505 /* move all this setup out later */
506 int samples_per_partition
=info
->grouping
;
507 int possible_partitions
=info
->partitions
;
508 int partitions_per_word
=look
->phrasebook
->dim
;
509 int n
=info
->end
-info
->begin
;
511 int partvals
=n
/samples_per_partition
;
517 for(j
=info
->begin
;j
<end
;j
++){
518 if(in
[i
][j
]>look
->tmax
)look
->tmax
=in
[i
][j
];
519 if(in
[i
][j
]<look
->tmin
)look
->tmin
=in
[i
][j
];
523 memset(resbits
,0,sizeof(resbits
));
524 memset(resvals
,0,sizeof(resvals
));
526 /* we code the partition words for each channel, then the residual
527 words for a partition per channel until we've written all the
528 residual words for that partition word. Then write the next
529 partition channel words... */
531 for(s
=0;s
<look
->stages
;s
++){
533 for(i
=0;i
<partvals
;){
535 /* first we encode a partition codeword for each channel */
538 long val
=partword
[j
][i
];
539 for(k
=1;k
<partitions_per_word
;k
++){
540 val
*=possible_partitions
;
542 val
+=partword
[j
][i
+k
];
546 if(val
<look
->phrasebook
->entries
)
547 look
->phrasebits
+=vorbis_book_encode(look
->phrasebook
,val
,opb
);
548 #if 0 /*def TRAIN_RES*/
556 /* now we encode interleaved residual values for the partitions */
557 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++){
558 long offset
=i
*samples_per_partition
+info
->begin
;
561 if(s
==0)resvals
[partword
[j
][i
]]+=samples_per_partition
;
562 if(info
->secondstages
[partword
[j
][i
]]&(1<<s
)){
563 codebook
*statebook
=look
->partbooks
[partword
[j
][i
]][s
];
566 long *accumulator
=NULL
;
569 accumulator
=look
->training_data
[s
][partword
[j
][i
]];
572 float *samples
=in
[j
]+offset
;
573 for(l
=0;l
<samples_per_partition
;l
++){
574 if(samples
[l
]<look
->training_min
[s
][partword
[j
][i
]])
575 look
->training_min
[s
][partword
[j
][i
]]=samples
[l
];
576 if(samples
[l
]>look
->training_max
[s
][partword
[j
][i
]])
577 look
->training_max
[s
][partword
[j
][i
]]=samples
[l
];
582 ret
=encode(opb
,in
[j
]+offset
,samples_per_partition
,
583 statebook
,accumulator
);
586 resbits
[partword
[j
][i
]]+=ret
;
597 fprintf(stderr,"%d :: ",vb->mode);
598 for(k=0;k<possible_partitions;k++){
599 fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
601 totalbits+=resbits[k];
604 fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
610 /* a truncated packet here just means 'stop working'; it's not an error */
611 static int _01inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
613 long (*decodepart
)(codebook
*, float *,
614 oggpack_buffer
*,int)){
617 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
618 vorbis_info_residue0
*info
=look
->info
;
620 /* move all this setup out later */
621 int samples_per_partition
=info
->grouping
;
622 int partitions_per_word
=look
->phrasebook
->dim
;
623 int max
=vb
->pcmend
>>1;
624 int end
=(info
->end
<max
?info
->end
:max
);
625 int n
=end
-info
->begin
;
628 int partvals
=n
/samples_per_partition
;
629 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
630 int ***partword
=alloca(ch
*sizeof(*partword
));
633 partword
[j
]=_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
[j
]));
635 for(s
=0;s
<look
->stages
;s
++){
637 /* each loop decodes on partition codeword containing
638 partitions_per_word partitions */
639 for(i
=0,l
=0;i
<partvals
;l
++){
641 /* fetch the partition word for each channel */
643 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
645 if(temp
==-1)goto eopbreak
;
646 partword
[j
][l
]=look
->decodemap
[temp
];
647 if(partword
[j
][l
]==NULL
)goto errout
;
651 /* now we decode residual values for the partitions */
652 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
654 long offset
=info
->begin
+i
*samples_per_partition
;
655 if(info
->secondstages
[partword
[j
][l
][k
]]&(1<<s
)){
656 codebook
*stagebook
=look
->partbooks
[partword
[j
][l
][k
]][s
];
658 if(decodepart(stagebook
,in
[j
]+offset
,&vb
->opb
,
659 samples_per_partition
)==-1)goto eopbreak
;
672 /* residue 0 and 1 are just slight variants of one another. 0 is
673 interleaved, 1 is not */
674 long **res0_class(vorbis_block
*vb
,vorbis_look_residue
*vl
,
675 float **in
,int *nonzero
,int ch
){
676 /* we encode only the nonzero parts of a bundle */
682 /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
683 return(_01class(vb
,vl
,in
,used
));
688 int res0_forward(vorbis_block
*vb
,vorbis_look_residue
*vl
,
689 float **in
,float **out
,int *nonzero
,int ch
,
691 /* we encode only the nonzero parts of a bundle */
692 int i
,j
,used
=0,n
=vb
->pcmend
/2;
701 int ret
=_01forward(vb
,vl
,in
,used
,partword
,
702 _interleaved_encodepart
);
708 out
[i
][j
]-=in
[used
][j
];
719 int res0_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
720 float **in
,int *nonzero
,int ch
){
726 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodevs_add
));
731 int res1_forward(oggpack_buffer
*opb
,vorbis_block
*vb
,vorbis_look_residue
*vl
,
732 float **in
,float **out
,int *nonzero
,int ch
,
734 int i
,j
,used
=0,n
=vb
->pcmend
/2;
744 int ret
=_01forward(opb
,vb
,vl
,in
,used
,partword
,_encodepart
);
750 out
[i
][j
]-=in
[used
][j
];
760 long **res1_class(vorbis_block
*vb
,vorbis_look_residue
*vl
,
761 float **in
,int *nonzero
,int ch
){
767 return(_01class(vb
,vl
,in
,used
));
772 int res1_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
773 float **in
,int *nonzero
,int ch
){
779 return(_01inverse(vb
,vl
,in
,used
,vorbis_book_decodev_add
));
784 long **res2_class(vorbis_block
*vb
,vorbis_look_residue
*vl
,
785 float **in
,int *nonzero
,int ch
){
788 if(nonzero
[i
])used
++;
790 return(_2class(vb
,vl
,in
,ch
));
795 /* res2 is slightly more different; all the channels are interleaved
796 into a single vector and encoded. */
798 int res2_forward(oggpack_buffer
*opb
,
799 vorbis_block
*vb
,vorbis_look_residue
*vl
,
800 float **in
,float **out
,int *nonzero
,int ch
,
802 long i
,j
,k
,n
=vb
->pcmend
/2,used
=0;
804 /* don't duplicate the code; use a working vector hack for now and
805 reshape ourselves into a single channel res1 */
806 /* ugly; reallocs for each coupling pass :-( */
807 float *work
=_vorbis_block_alloc(vb
,ch
*n
*sizeof(*work
));
810 if(nonzero
[i
])used
++;
811 for(j
=0,k
=i
;j
<n
;j
++,k
+=ch
)
816 int ret
=_01forward(opb
,vb
,vl
,&work
,1,partword
,_encodepart
);
817 /* update the sofar vector */
822 for(j
=0,k
=i
;j
<n
;j
++,k
+=ch
)
823 sofar
[j
]+=pcm
[j
]-work
[k
];
833 /* duplicate code here as speed is somewhat more important */
834 int res2_inverse(vorbis_block
*vb
,vorbis_look_residue
*vl
,
835 float **in
,int *nonzero
,int ch
){
837 vorbis_look_residue0
*look
=(vorbis_look_residue0
*)vl
;
838 vorbis_info_residue0
*info
=look
->info
;
840 /* move all this setup out later */
841 int samples_per_partition
=info
->grouping
;
842 int partitions_per_word
=look
->phrasebook
->dim
;
843 int max
=(vb
->pcmend
*ch
)>>1;
844 int end
=(info
->end
<max
?info
->end
:max
);
845 int n
=end
-info
->begin
;
848 int partvals
=n
/samples_per_partition
;
849 int partwords
=(partvals
+partitions_per_word
-1)/partitions_per_word
;
850 int **partword
=_vorbis_block_alloc(vb
,partwords
*sizeof(*partword
));
852 for(i
=0;i
<ch
;i
++)if(nonzero
[i
])break;
853 if(i
==ch
)return(0); /* no nonzero vectors */
855 for(s
=0;s
<look
->stages
;s
++){
856 for(i
=0,l
=0;i
<partvals
;l
++){
859 /* fetch the partition word */
860 int temp
=vorbis_book_decode(look
->phrasebook
,&vb
->opb
);
861 if(temp
==-1)goto eopbreak
;
862 partword
[l
]=look
->decodemap
[temp
];
863 if(partword
[l
]==NULL
)goto errout
;
866 /* now we decode residual values for the partitions */
867 for(k
=0;k
<partitions_per_word
&& i
<partvals
;k
++,i
++)
868 if(info
->secondstages
[partword
[l
][k
]]&(1<<s
)){
869 codebook
*stagebook
=look
->partbooks
[partword
[l
][k
]][s
];
872 if(vorbis_book_decodevv_add(stagebook
,in
,
873 i
*samples_per_partition
+info
->begin
,ch
,
874 &vb
->opb
,samples_per_partition
)==-1)
887 vorbis_func_residue residue0_exportbundle
={
898 vorbis_func_residue residue1_exportbundle
={
909 vorbis_func_residue residue2_exportbundle
={