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 XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: utility main for setting entropy encoding parameters
15 last mod: $Id: latticetune.c,v 1.9 2001/06/15 21:15:43 xiphmont Exp $
17 ********************************************************************/
26 static char *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 if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
115 if(!strrcmp_i(argv[0],"res1tune"))
121 if(!(lines&0xfff))spinnit("codewords so far...",lines);
125 while(*temp==' ')temp++;
126 for(cols=0;*temp;cols++){
127 while(*temp>32)temp++;
128 while(*temp==' ')temp++;
130 vec=alloca(sizeof(float)*cols);
143 if(get_line_value(in,vec+j)){
144 fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
148 for(j=0;j<max;j+=adv){
150 code=_best(b,vec+j,step);
160 /* build the codeword lengths */
161 build_tree_from_lengths0(entries,hits,lengths);
163 c->lengthlist=lengths;
164 write_codebook(stdout,name,c);
167 long bins=_book_maptype1_quantvals(c);
168 long i,k,base=c->lengthlist[0];
169 for(i=0;i<entries;i++)
170 if(c->lengthlist[i]>base)base=c->lengthlist[i];
172 for(j=0;j<entries;j++){
173 if(c->lengthlist[j]){
175 fprintf(stderr,"%4ld: ",j);
176 for(k=0;k<c->dim;k++){
177 int index= (j/indexdiv)%bins;
178 fprintf(stderr,"%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
179 _float32_unpack(c->q_min));
182 fprintf(stderr,"\t|",(1<<(base-c->lengthlist[j])));
183 for(k=0;k<base-c->lengthlist[j];k++)fprintf(stderr,"*");
184 fprintf(stderr,"\n");