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-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.7 1999/12/30 07:26:59 xiphmont Exp $
17 ********************************************************************/
26 static char *linebuffer=NULL;
27 static int lbufsize=0;
28 static char *rline(FILE *in,FILE *out){
30 if(feof(in))return NULL;
39 linebuffer=malloc(lbufsize);
42 linebuffer=realloc(linebuffer,lbufsize);
53 linebuffer[sofar++]=c;
54 linebuffer[sofar]='\0';
60 if(linebuffer[0]=='#'){
72 int main(int argc,char *argv[]){
78 int entries=-1,dim=-1,aux=-1;
85 fprintf(stderr,"Need a trained data set on the command line.\n");
91 char *filename=strdup(argv[1]);
93 in=fopen(filename,"r");
95 fprintf(stderr,"Could not open input file %s\n",filename);
99 ptr=strrchr(filename,'-');
102 name=strdup(filename);
105 name=strdup(filename);
106 strcat(filename,".vqh");
109 out=fopen(filename,"w");
111 fprintf(stderr,"Unable to open %s for writing\n",filename);
116 /* suck in the trained book */
118 /* read book type, but it doesn't matter */
122 if(sscanf(line,"%d %d %d",&entries,&dim,&aux)!=3){
123 fprintf(stderr,"Syntax error reading book file\n");
127 /* just use it to allocate mem */
128 vqgen_init(&v,dim,0,entries,NULL,NULL);
132 if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
133 &q.quant,&q.sequencep)!=4){
134 fprintf(stderr,"Syntax error reading book file\n");
138 /* quantized entries */
139 /* save quant data; we don't want to requantize later as our method
140 is currently imperfect wrt repeated application */
142 quantlist=malloc(sizeof(long)*v.elements*v.entries);
143 for(j=0;j<entries;j++){
147 sscanf(line,"%lf",&a);
149 quantlist[i++]=rint(a);
154 for(j=0;j<entries;j++)line=rline(in,out);
158 /* training points */
160 double *b=alloca(sizeof(double)*(dim+aux));
162 v.entries=0; /* hack to avoid reseeding */
164 for(k=0;k<dim+aux;k++){
167 sscanf(line,"%lf",b+k);
170 vqgen_addpoint(&v,b,NULL);
176 vqgen_unquantize(&v,&q);
179 vqsp_book(&v,&b,quantlist);
181 /* save the book in C header form */
183 "/********************************************************************\n"
185 " * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *\n"
186 " * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *\n"
187 " * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *\n"
188 " * PLEASE READ THESE TERMS DISTRIBUTING. *\n"
190 " * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *\n"
191 " * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *\n"
192 " * http://www.xiph.org/ *\n"
194 " ********************************************************************\n"
196 " function: static codebook autogenerated by vq/vqbuild\n"
198 " ********************************************************************/\n\n");
200 fprintf(out,"#ifndef _V_%s_VQH_\n#define _V_%s_VQH_\n",name,name);
201 fprintf(out,"#include \"vqgen.h\"\n\n");
203 /* first, the static vectors, then the book structure to tie it together. */
205 fprintf(out,"static long _vq_quantlist_%s[] = {\n",name);
207 for(j=0;j<entries;j++){
210 fprintf(out,"%5ld, ",b.quantlist[i++]);
213 fprintf(out,"};\n\n");
216 fprintf(out,"static long _vq_codelist_%s[] = {\n",name);
219 for(k=0;k<8 && j<entries;k++,j++)
220 fprintf(out,"%ld,",b.codelist[j]);
223 fprintf(out,"};\n\n");
226 fprintf(out,"static long _vq_lengthlist_%s[] = {\n",name);
229 for(k=0;k<16 && j<entries;k++,j++)
230 fprintf(out,"%2ld,",b.lengthlist[j]);
233 fprintf(out,"};\n\n");
236 fprintf(out,"static long _vq_ptr0_%s[] = {\n",name);
239 for(k=0;k<8 && j<b.aux;k++,j++)
240 fprintf(out,"%5ld,",b.ptr0[j]);
243 fprintf(out,"};\n\n");
246 fprintf(out,"static long _vq_ptr1_%s[] = {\n",name);
249 for(k=0;k<8 && j<b.aux;k++,j++)
250 fprintf(out,"%6ld,",b.ptr1[j]);
253 fprintf(out,"};\n\n");
256 fprintf(out,"static long _vq_p_%s[] = {\n",name);
259 for(k=0;k<8 && j<b.aux;k++,j++)
260 fprintf(out,"%6ld,",b.p[j]);
263 fprintf(out,"};\n\n");
266 fprintf(out,"static long _vq_q_%s[] = {\n",name);
269 for(k=0;k<8 && j<b.aux;k++,j++)
270 fprintf(out,"%6ld,",b.q[j]);
273 fprintf(out,"};\n\n");
275 /* tie it all together */
276 fprintf(out,"static vqbook _vq_book_%s = {\n",name);
277 fprintf(out,"\t%ld, %ld, %ld, %ld, %d, %d,\n",
278 b.dim,b.entries,q.min,q.delta,q.quant,q.sequencep);
279 fprintf(out,"\t0,\n"); /* valuelist */
280 fprintf(out,"\t_vq_quantlist_%s,\n",name);
281 fprintf(out,"\t_vq_codelist_%s,\n",name);
282 fprintf(out,"\t_vq_lengthlist_%s,\n",name);
283 fprintf(out,"\t_vq_ptr0_%s,\n",name);
284 fprintf(out,"\t_vq_ptr1_%s,\n",name);
285 fprintf(out,"\t0,\n",name);
286 fprintf(out,"\t0,\n",name);
287 fprintf(out,"\t_vq_p_%s,\n",name);
288 fprintf(out,"\t_vq_q_%s,\n",name);
289 fprintf(out,"\t%ld, %ld };\n\n",b.aux,b.aux);
291 fprintf(out,"\n#endif\n");