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){
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]=='#'){
74 int main(int argc,char *argv[]){
80 int entries=-1,dim=-1,aux=-1;
87 fprintf(stderr,"Need a trained data set on the command line.\n");
93 char *filename=strdup(argv[1]);
95 in=fopen(filename,"r");
97 fprintf(stderr,"Could not open input file %s\n",filename);
101 ptr=strrchr(filename,'-');
104 name=strdup(filename);
107 name=strdup(filename);
108 strcat(filename,".vqh");
111 out=fopen(filename,"w");
113 fprintf(stderr,"Unable to open %s for writing\n",filename);
118 /* suck in the trained book */
120 /* read book type, but it doesn't matter */
124 if(sscanf(line,"%d %d %d",&entries,&dim,&aux)!=3){
125 fprintf(stderr,"Syntax error reading book file\n");
129 /* just use it to allocate mem */
130 vqgen_init(&v,dim,0,entries,NULL,NULL);
134 if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
135 &q.quant,&q.sequencep)!=4){
136 fprintf(stderr,"Syntax error reading book file\n");
140 /* quantized entries */
141 /* save quant data; we don't want to requantize later as our method
142 is currently imperfect wrt repeated application */
144 quantlist=malloc(sizeof(long)*v.elements*v.entries);
145 for(j=0;j<entries;j++){
149 sscanf(line,"%lf",&a);
151 quantlist[i++]=rint(a);
156 for(j=0;j<entries;j++)line=rline(in,out);
160 /* training points */
162 double *b=alloca(sizeof(double)*(dim+aux));
164 v.entries=0; /* hack to avoid reseeding */
166 for(k=0;k<dim+aux;k++){
169 sscanf(line,"%lf",b+k);
172 vqgen_addpoint(&v,b,NULL);
178 vqgen_unquantize(&v,&q);
181 vqsp_book(&v,&b,quantlist);
183 /* save the book in C header form */
185 "/********************************************************************\n"
187 " * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *\n"
188 " * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *\n"
189 " * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *\n"
190 " * PLEASE READ THESE TERMS DISTRIBUTING. *\n"
192 " * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *\n"
193 " * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *\n"
194 " * http://www.xiph.org/ *\n"
196 " ********************************************************************\n"
198 " function: static codebook autogenerated by vq/vqbuild\n"
200 " ********************************************************************/\n\n");
202 fprintf(out,"#ifndef _V_%s_VQH_\n#define _V_%s_VQH_\n",name,name);
203 fprintf(out,"#include \"vqgen.h\"\n\n");
205 /* first, the static vectors, then the book structure to tie it together. */
207 fprintf(out,"static long _vq_quantlist_%s[] = {\n",name);
209 for(j=0;j<entries;j++){
212 fprintf(out,"%5ld, ",b.quantlist[i++]);
215 fprintf(out,"};\n\n");
218 fprintf(out,"static long _vq_codelist_%s[] = {\n",name);
221 for(k=0;k<8 && j<entries;k++,j++)
222 fprintf(out,"%ld,",b.codelist[j]);
225 fprintf(out,"};\n\n");
228 fprintf(out,"static long _vq_lengthlist_%s[] = {\n",name);
231 for(k=0;k<16 && j<entries;k++,j++)
232 fprintf(out,"%2ld,",b.lengthlist[j]);
235 fprintf(out,"};\n\n");
238 fprintf(out,"static long _vq_ptr0_%s[] = {\n",name);
241 for(k=0;k<8 && j<b.aux;k++,j++)
242 fprintf(out,"%5ld,",b.ptr0[j]);
245 fprintf(out,"};\n\n");
248 fprintf(out,"static long _vq_ptr1_%s[] = {\n",name);
251 for(k=0;k<8 && j<b.aux;k++,j++)
252 fprintf(out,"%6ld,",b.ptr1[j]);
255 fprintf(out,"};\n\n");
258 fprintf(out,"static long _vq_p_%s[] = {\n",name);
261 for(k=0;k<8 && j<b.aux;k++,j++)
262 fprintf(out,"%6ld,",b.p[j]);
265 fprintf(out,"};\n\n");
268 fprintf(out,"static long _vq_q_%s[] = {\n",name);
271 for(k=0;k<8 && j<b.aux;k++,j++)
272 fprintf(out,"%6ld,",b.q[j]);
275 fprintf(out,"};\n\n");
277 /* tie it all together */
278 fprintf(out,"static vqbook _vq_book_%s = {\n",name);
279 fprintf(out,"\t%ld, %ld, %ld, %ld, %d, %d,\n",
280 b.dim,b.entries,q.min,q.delta,q.quant,q.sequencep);
281 fprintf(out,"\t0,\n"); /* valuelist */
282 fprintf(out,"\t_vq_quantlist_%s,\n",name);
283 fprintf(out,"\t_vq_codelist_%s,\n",name);
284 fprintf(out,"\t_vq_lengthlist_%s,\n",name);
285 fprintf(out,"\t_vq_ptr0_%s,\n",name);
286 fprintf(out,"\t_vq_ptr1_%s,\n",name);
287 fprintf(out,"\t0,\n",name);
288 fprintf(out,"\t0,\n",name);
289 fprintf(out,"\t_vq_p_%s,\n",name);
290 fprintf(out,"\t_vq_q_%s,\n",name);
291 fprintf(out,"\t%ld, %ld };\n\n",b.aux,b.aux);
293 fprintf(out,"\n#endif\n");