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.15 2000/10/12 03:13:01 xiphmont Exp $
17 ********************************************************************/
24 #include "vorbis/codebook.h"
25 #include "../lib/sharedbook.h"
31 static char *linebuffer=NULL;
32 static int lbufsize=0;
33 static char *rline(FILE *in,FILE *out){
35 if(feof(in))return NULL;
44 linebuffer=malloc(lbufsize);
47 linebuffer=realloc(linebuffer,lbufsize);
58 linebuffer[sofar++]=c;
59 linebuffer[sofar]='\0';
65 if(linebuffer[0]=='#'){
77 int main(int argc,char *argv[]){
84 int entries=-1,dim=-1,aux=-1;
93 fprintf(stderr,"Need a trained data set on the command line.\n");
99 char *filename=strdup(argv[1]);
101 in=fopen(filename,"r");
103 fprintf(stderr,"Could not open input file %s\n",filename);
107 ptr=strrchr(filename,'-');
110 name=strdup(filename);
113 name=strdup(filename);
114 strcat(filename,".vqh");
117 out=fopen(filename,"w");
119 fprintf(stderr,"Unable to open %s for writing\n",filename);
124 /* suck in the trained book */
126 /* read book type, but it doesn't matter */
130 if(sscanf(line,"%d %d %d",&entries,&dim,&aux)!=3){
131 fprintf(stderr,"Syntax error reading book file\n");
135 /* just use it to allocate mem */
136 vqgen_init(&v,dim,0,entries,0.,NULL,NULL,0);
140 if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
141 &q.quant,&q.sequencep)!=4){
142 fprintf(stderr,"Syntax error reading book file\n");
146 /* quantized entries */
147 /* save quant data; we don't want to requantize later as our method
148 is currently imperfect wrt repeated application */
150 quantlist=malloc(sizeof(long)*v.elements*v.entries);
151 for(j=0;j<entries;j++){
155 sscanf(line,"%lf",&a);
157 quantlist[i++]=rint(a);
162 for(j=0;j<entries;j++)line=rline(in,out);
166 /* training points */
168 float *b=alloca(sizeof(float)*(dim+aux));
170 v.entries=0; /* hack to avoid reseeding */
172 for(k=0;k<dim+aux;k++){
175 sscanf(line,"%lf",b+k);
178 vqgen_addpoint(&v,b,NULL);
184 vqgen_unquantize(&v,&q);
187 vqsp_book(&v,&b,quantlist);
191 c.q_sequencep=q.sequencep;
193 /* save the book in C header form */
194 write_codebook(out,name,b.c);