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.7 2001/02/26 03:51:12 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],"restune")){
117 if(!(lines&0xfff))spinnit("codewords so far...",lines);
121 while(*temp==' ')temp++;
122 for(cols=0;*temp;cols++){
123 while(*temp>32)temp++;
124 while(*temp==' ')temp++;
126 vec=alloca(sizeof(float)*cols);
131 if(get_line_value(in,vec+j)){
132 fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
138 code=_best(b,vec+j,step);
148 /* build the codeword lengths */
149 build_tree_from_lengths0(entries,hits,lengths);
151 c->lengthlist=lengths;
152 write_codebook(stdout,name,c);