1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
6 * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
9 * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: utility main for setting entropy encoding parameters
16 last mod: $Id: latticetune.c,v 1.5 2000/11/08 03:23:23 xiphmont Exp $
18 ********************************************************************/
27 static char *strrcmp_i(char *s,char *cmp){
28 return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
31 /* This util takes a training-collected file listing codewords used in
32 LSP fitting, then generates new codeword lengths for maximally
33 efficient integer-bits entropy encoding.
36 latticetune book.vqh input.vqd [unused_entriesp]
38 latticetune produces book.vqh on stdout */
40 int main(int argc,char *argv[]){
46 int entries=-1,dim=-1,guard=1;
52 fprintf(stderr,"Need a lattice codebook on the command line.\n");
56 fprintf(stderr,"Need a codeword data file on the command line.\n");
59 if(argv[3]!=NULL)guard=0;
63 char *filename=strdup(argv[1]);
65 b=codebook_load(filename);
66 c=(static_codebook *)(b->c);
68 ptr=strrchr(filename,'.');
71 name=strdup(filename);
73 name=strdup(filename);
78 fprintf(stderr,"Provided book is not a latticebook.\n");
85 hits=_ogg_malloc(entries*sizeof(long));
86 lengths=_ogg_calloc(entries,sizeof(long));
87 for(j=0;j<entries;j++)hits[j]=guard;
89 in=fopen(argv[2],"r");
91 fprintf(stderr,"Could not open input file %s\n",argv[2]);
95 if(!strrcmp_i(argv[0],"latticetune")){
101 if(!(lines&0xfff))spinnit("codewords so far...",lines);
103 if(sscanf(line,"%ld",&code)==1)
110 if(!strrcmp_i(argv[0],"restune")){
118 if(!(lines&0xfff))spinnit("codewords so far...",lines);
122 while(*temp==' ')temp++;
123 for(cols=0;*temp;cols++){
124 while(*temp>32)temp++;
125 while(*temp==' ')temp++;
127 vec=alloca(sizeof(float)*cols);
132 if(get_line_value(in,vec+j)){
133 fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
139 code=_best(b,vec+j,step);
149 /* build the codeword lengths */
150 build_tree_from_lengths0(entries,hits,lengths);
152 c->lengthlist=lengths;
153 write_codebook(stdout,name,c);