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 building codebooks from training sets
14 last mod: $Id: build.c,v 1.21 2001/12/20 01:00:39 segher Exp $
16 ********************************************************************/
28 static char *linebuffer=NULL;
29 static int lbufsize=0;
30 static char *rline(FILE *in,FILE *out){
32 if(feof(in))return NULL;
41 linebuffer=_ogg_malloc(lbufsize);
44 linebuffer=_ogg_realloc(linebuffer,lbufsize);
55 linebuffer[sofar++]=c;
56 linebuffer[sofar]='\0';
62 if(linebuffer[0]=='#'){
74 int main(int argc,char *argv[]){
81 int entries=-1,dim=-1,aux=-1;
90 fprintf(stderr,"Need a trained data set on the command line.\n");
96 char *filename=strdup(argv[1]);
98 in=fopen(filename,"r");
100 fprintf(stderr,"Could not open input file %s\n",filename);
104 ptr=strrchr(filename,'-');
107 name=strdup(filename);
110 name=strdup(filename);
111 strcat(filename,".vqh");
114 out=fopen(filename,"w");
116 fprintf(stderr,"Unable to open %s for writing\n",filename);
121 /* suck in the trained book */
123 /* read book type, but it doesn't matter */
127 if(sscanf(line,"%d %d %d",&entries,&dim,&aux)!=3){
128 fprintf(stderr,"Syntax error reading book file\n");
132 /* just use it to allocate mem */
133 vqgen_init(&v,dim,0,entries,0.f,NULL,NULL,0);
137 if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
138 &q.quant,&q.sequencep)!=4){
139 fprintf(stderr,"Syntax error reading book file\n");
143 /* quantized entries */
144 /* save quant data; we don't want to requantize later as our method
145 is currently imperfect wrt repeated application */
147 quantlist=_ogg_malloc(sizeof(long)*v.elements*v.entries);
148 for(j=0;j<entries;j++){
152 sscanf(line,"%f",&a);
154 quantlist[i++]=rint(a);
159 for(j=0;j<entries;j++)line=rline(in,out);
163 /* training points */
165 float *b=alloca(sizeof(float)*(dim+aux));
167 v.entries=0; /* hack to avoid reseeding */
169 for(k=0;k<dim+aux;k++){
172 sscanf(line,"%f",b+k);
175 vqgen_addpoint(&v,b,NULL);
181 vqgen_unquantize(&v,&q);
184 vqsp_book(&v,&b,quantlist);
188 c.q_sequencep=q.sequencep;
190 /* save the book in C header form */
191 write_codebook(out,name,b.c);