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 Xiph.Org Foundation https://xiph.org/ *
11 ********************************************************************
13 function: utility main for setting entropy encoding parameters
16 ********************************************************************/
25 static int strrcmp_i(char *s,char *cmp){
26 return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
29 /* This util takes a training-collected file listing codewords used in
30 LSP fitting, then generates new codeword lengths for maximally
31 efficient integer-bits entropy encoding.
34 latticetune book.vqh input.vqd [unused_entriesp]
36 latticetune produces book.vqh on stdout */
38 int main(int argc,char *argv[]){
44 int entries=-1,dim=-1,guard=1;
50 fprintf(stderr,"Need a lattice codebook on the command line.\n");
54 fprintf(stderr,"Need a codeword data file on the command line.\n");
57 if(argv[3]!=NULL)guard=0;
61 char *filename=strdup(argv[1]);
63 b=codebook_load(filename);
64 c=(static_codebook *)(b->c);
66 ptr=strrchr(filename,'.');
69 name=strdup(filename);
71 name=strdup(filename);
76 fprintf(stderr,"Provided book is not a latticebook.\n");
83 hits=_ogg_malloc(entries*sizeof(long));
84 lengths=_ogg_calloc(entries,sizeof(long));
85 for(j=0;j<entries;j++)hits[j]=guard;
87 in=fopen(argv[2],"r");
89 fprintf(stderr,"Could not open input file %s\n",argv[2]);
93 if(!strrcmp_i(argv[0],"latticetune")){
99 if(!(lines&0xfff))spinnit("codewords so far...",lines);
101 if(sscanf(line,"%ld",&code)==1)
108 /* now we simply count already collated by-entry data */
109 if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
115 /* likely to have multiple listing for each code entry; must
118 char *pos=strchr(line,':');
120 long code=atol(line);
121 long val=atol(pos+1);
131 /* build the codeword lengths */
132 build_tree_from_lengths0(entries,hits,lengths);
134 c->lengthlist=lengths;
135 write_codebook(stdout,name,c);
138 long bins=_book_maptype1_quantvals(c);
139 long i,k,base=c->lengthlist[0];
140 for(i=0;i<entries;i++)
141 if(c->lengthlist[i]>base)base=c->lengthlist[i];
143 for(j=0;j<entries;j++){
144 if(c->lengthlist[j]){
146 fprintf(stderr,"%4ld: ",j);
147 for(k=0;k<c->dim;k++){
148 int index= (j/indexdiv)%bins;
149 fprintf(stderr,"%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
150 _float32_unpack(c->q_min));
153 fprintf(stderr,"\t|");
154 for(k=0;k<base-c->lengthlist[j];k++)fprintf(stderr,"*");
155 fprintf(stderr,"\n");