1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *
9 * by 1999 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 author: Monty <xiphmont@mit.edu>
16 modifications by: Monty
17 last modification date: Dec 15 1999
19 ********************************************************************/
28 static char *linebuffer=NULL;
29 static int lbufsize=0;
30 static char *rline(FILE *in,FILE *out,int pass){
32 if(feof(in))return NULL;
41 linebuffer=malloc(lbufsize);
44 linebuffer=realloc(linebuffer,lbufsize);
55 linebuffer[sofar++]=c;
56 linebuffer[sofar]='\0';
62 if(linebuffer[0]=='#'){
63 if(pass)fprintf(out,"%s\n",linebuffer);
75 int main(int argc,char *argv[]){
81 int entries=-1,dim=-1,dummy;
88 fprintf(stderr,"Need a trained data set on the command line.\n");
94 char *filename=strdup(argv[1]);
96 in=fopen(filename,"r");
98 fprintf(stderr,"Could not open input file %s\n",filename);
102 ptr=strrchr(filename,'.');
108 name=strdup(filename);
109 strcat(filename,".h");
112 out=fopen(filename,"w");
114 fprintf(stderr,"Unable to open %s for writing\n",filename);
119 /* suck in the trained book */
121 /* read book type, but it doesn't matter */
122 line=rline(in,out,1);
124 line=rline(in,out,1);
125 if(sscanf(line,"%d %d %d",&entries,&dim,&dummy)!=2){
126 fprintf(stderr,"Syntax error reading book file\n");
130 /* just use it to allocate mem */
131 vqgen_init(&v,dim,0,entries,NULL,NULL);
134 line=rline(in,out,1);
135 if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
136 &q.quant,&q.sequencep)!=4){
137 fprintf(stderr,"Syntax error reading book file\n");
141 /* quantized entries */
142 /* save quant data; we don't want to requantize later as our method
143 is currently imperfect wrt repeated application */
145 quantlist=malloc(sizeof(int)*v.elements*v.entries);
146 for(j=0;j<entries;j++){
149 line=rline(in,out,0);
150 sscanf(line,"%lf",&a);
152 quantlist[i++]=rint(a);
157 for(j=0;j<entries;j++)line=rline(in,out,0);
161 /* training points */
165 v.entries=0; /* hack to avoid reseeding */
167 for(k=0;k<dim && k<80;k++){
168 line=rline(in,out,0);
170 sscanf(line,"%lf",b+k);
173 vqgen_addpoint(&v,b,NULL);
179 vqgen_unquantize(&v,&q);
184 /* save the book in C header form */