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 building codebooks from training sets
15 last mod: $Id: build.c,v 1.17 2000/11/08 03:23:23 xiphmont Exp $
17 ********************************************************************/
29 static char *linebuffer=NULL;
30 static int lbufsize=0;
31 static char *rline(FILE *in,FILE *out){
33 if(feof(in))return NULL;
42 linebuffer=_ogg_malloc(lbufsize);
45 linebuffer=_ogg_realloc(linebuffer,lbufsize);
56 linebuffer[sofar++]=c;
57 linebuffer[sofar]='\0';
63 if(linebuffer[0]=='#'){
75 int main(int argc,char *argv[]){
82 int entries=-1,dim=-1,aux=-1;
91 fprintf(stderr,"Need a trained data set on the command line.\n");
97 char *filename=strdup(argv[1]);
99 in=fopen(filename,"r");
101 fprintf(stderr,"Could not open input file %s\n",filename);
105 ptr=strrchr(filename,'-');
108 name=strdup(filename);
111 name=strdup(filename);
112 strcat(filename,".vqh");
115 out=fopen(filename,"w");
117 fprintf(stderr,"Unable to open %s for writing\n",filename);
122 /* suck in the trained book */
124 /* read book type, but it doesn't matter */
128 if(sscanf(line,"%d %d %d",&entries,&dim,&aux)!=3){
129 fprintf(stderr,"Syntax error reading book file\n");
133 /* just use it to allocate mem */
134 vqgen_init(&v,dim,0,entries,0.,NULL,NULL,0);
138 if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
139 &q.quant,&q.sequencep)!=4){
140 fprintf(stderr,"Syntax error reading book file\n");
144 /* quantized entries */
145 /* save quant data; we don't want to requantize later as our method
146 is currently imperfect wrt repeated application */
148 quantlist=_ogg_malloc(sizeof(long)*v.elements*v.entries);
149 for(j=0;j<entries;j++){
153 sscanf(line,"%f",&a);
155 quantlist[i++]=rint(a);
160 for(j=0;j<entries;j++)line=rline(in,out);
164 /* training points */
166 float *b=alloca(sizeof(float)*(dim+aux));
168 v.entries=0; /* hack to avoid reseeding */
170 for(k=0;k<dim+aux;k++){
173 sscanf(line,"%f",b+k);
176 vqgen_addpoint(&v,b,NULL);
182 vqgen_unquantize(&v,&q);
185 vqsp_book(&v,&b,quantlist);
189 c.q_sequencep=q.sequencep;
191 /* save the book in C header form */
192 write_codebook(out,name,b.c);