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,"%ld, ",b.quantlist[i++]);
215 fprintf(out,"};\n\n");
218 fprintf(out,"static long _vq_codelist_%s[] = {\n",name);
219 for(j=0;j<entries;j++){
220 fprintf(out,"\t%ld,\n",b.codelist[j]);
222 fprintf(out,"};\n\n");
225 fprintf(out,"static long _vq_lengthlist_%s[] = {\n",name);
226 for(j=0;j<entries;j++){
227 fprintf(out,"\t%ld,\n",b.lengthlist[j]);
229 fprintf(out,"};\n\n");
232 fprintf(out,"static long _vq_ptr0_%s[] = {\n",name);
233 for(j=0;j<b.aux;j++){
234 fprintf(out,"\t%ld,\n",b.ptr0[j]);
236 fprintf(out,"};\n\n");
239 fprintf(out,"static long _vq_ptr1_%s[] = {\n",name);
240 for(j=0;j<b.aux;j++){
241 fprintf(out,"\t%ld,\n",b.ptr1[j]);
243 fprintf(out,"};\n\n");
246 fprintf(out,"static double _vq_n_%s[] = {\n",name);
248 for(j=0;j<b.aux;j++){
251 fprintf(out,"%g,",b.n[i++]);
254 fprintf(out,"};\n\n");
257 fprintf(out,"static double _vq_c_%s[] = {\n",name);
258 for(j=0;j<b.aux;j++){
259 fprintf(out,"\t%g,\n",b.c[j]);
261 fprintf(out,"};\n\n");
263 /* tie it all together */
264 fprintf(out,"static vqbook _vq_book_%s = {\n",name);
265 fprintf(out,"\t%ld, %ld, %ld, %ld, %d, %d,\n",
266 b.dim,b.entries,q.min,q.delta,q.quant,q.sequencep);
267 fprintf(out,"\t0,\n"); /* valuelist */
268 fprintf(out,"\t_vq_quantlist_%s,\n",name);
269 fprintf(out,"\t_vq_codelist_%s,\n",name);
270 fprintf(out,"\t_vq_lengthlist_%s,\n",name);
271 fprintf(out,"\t_vq_ptr0_%s,\n",name);
272 fprintf(out,"\t_vq_ptr1_%s,\n",name);
273 fprintf(out,"\t_vq_n_%s,\n",name);
274 fprintf(out,"\t_vq_c_%s,\n",name);
275 fprintf(out,"\t%ld, %ld };\n\n",b.aux,b.aux);
277 fprintf(out,"\n#endif\n");