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 http://www.xiph.org/ *
11 ********************************************************************
13 function: utility main for setting entropy encoding parameters
15 last mod: $Id: latticetune.c 16037 2009-05-26 21:10:58Z xiphmont $
17 ********************************************************************/
26 static int strrcmp_i(char *s,char *cmp){
27 return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
30 /* This util takes a training-collected file listing codewords used in
31 LSP fitting, then generates new codeword lengths for maximally
32 efficient integer-bits entropy encoding.
35 latticetune book.vqh input.vqd [unused_entriesp]
37 latticetune produces book.vqh on stdout */
39 int main(int argc,char *argv[]){
45 int entries=-1,dim=-1,guard=1;
51 fprintf(stderr,"Need a lattice codebook on the command line.\n");
55 fprintf(stderr,"Need a codeword data file on the command line.\n");
58 if(argv[3]!=NULL)guard=0;
62 char *filename=strdup(argv[1]);
64 b=codebook_load(filename);
65 c=(static_codebook *)(b->c);
67 ptr=strrchr(filename,'.');
70 name=strdup(filename);
72 name=strdup(filename);
77 fprintf(stderr,"Provided book is not a latticebook.\n");
84 hits=_ogg_malloc(entries*sizeof(long));
85 lengths=_ogg_calloc(entries,sizeof(long));
86 for(j=0;j<entries;j++)hits[j]=guard;
88 in=fopen(argv[2],"r");
90 fprintf(stderr,"Could not open input file %s\n",argv[2]);
94 if(!strrcmp_i(argv[0],"latticetune")){
100 if(!(lines&0xfff))spinnit("codewords so far...",lines);
102 if(sscanf(line,"%ld",&code)==1)
109 /* now we simply count already collated by-entry data */
110 if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
116 /* likely to have multiple listing for each code entry; must
119 char *pos=strchr(line,':');
121 long code=atol(line);
122 long val=atol(pos+1);
132 /* build the codeword lengths */
133 build_tree_from_lengths0(entries,hits,lengths);
135 c->lengthlist=lengths;
136 write_codebook(stdout,name,c);
139 long bins=_book_maptype1_quantvals(c);
140 long i,k,base=c->lengthlist[0];
141 for(i=0;i<entries;i++)
142 if(c->lengthlist[i]>base)base=c->lengthlist[i];
144 for(j=0;j<entries;j++){
145 if(c->lengthlist[j]){
147 fprintf(stderr,"%4ld: ",j);
148 for(k=0;k<c->dim;k++){
149 int index= (j/indexdiv)%bins;
150 fprintf(stderr,"%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
151 _float32_unpack(c->q_min));
154 fprintf(stderr,"\t|");
155 for(k=0;k<base-c->lengthlist[j];k++)fprintf(stderr,"*");
156 fprintf(stderr,"\n");