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 loading/testing/running finished codebooks
15 last mod: $Id: run.c,v 1.2 1999/12/30 07:27:01 xiphmont Exp $
17 ********************************************************************/
25 /* this is a bit silly; it's a C stub used by a Perl script to build a
26 quick executable against the chosen codebook and run tests */
28 static char *linebuffer=NULL;
29 static int lbufsize=0;
30 static char *rline(FILE *in){
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]=='#'){
70 void vqbook_unquantize(vqbook *b){
72 double mindel=float24_unpack(b->min);
73 double delta=float24_unpack(b->delta);
74 if(!b->valuelist)b->valuelist=malloc(sizeof(double)*b->entries*b->dim);
76 for(j=0;j<b->entries;j++){
78 for(k=0;k<b->dim;k++){
79 double val=b->quantlist[j*b->dim+k]*delta+last+mindel;
80 b->valuelist[j*b->dim+k]=val;
81 if(b->sequencep)last=val;
89 run outbase [-m] [-s <start>,<n>] datafile [-s <start>,<n>] [datafile...]
91 produces: outbase-residue.m (error between input data and chosen codewords;
92 can be used to cascade)
93 outbase-cells.m (2d gnuplot file of cells if -m)
95 currently assumes a 'sequenced' codebook wants its test data to be
96 normalized to begin at zero... */
98 int main(int argc,char *argv[]){
107 double mean=0.,meansquare=0.,mean_count=0.;
112 fprintf(stderr,"Need a basename.\n");
117 vqbook_unquantize(b);
121 snprintf(buf,80,"%s-residue.vqd",name);
122 residue=fopen(buf,"w");
124 fprintf(stderr,"Unable to open output file %s\n",buf);
129 /* parse the command line; handle things as the come */
138 snprintf(buf,80,"%s-cells.m",name);
139 cells=fopen(buf,"w");
141 fprintf(stderr,"Unable to open output file %s\n",buf);
149 fprintf(stderr,"Option %s missing argument.\n",argv[0]);
152 if(sscanf(argv[1],"%d,%d",&start,&num)!=2){
154 if(sscanf(argv[1],"%d",&start)!=1){
155 fprintf(stderr,"Option %s syntax error.\n",argv[0]);
163 /* it's an input file */
164 char *file=strdup(*argv++);
170 fprintf(stderr,"Could not open input file %s\n",file);
174 while((line=rline(in))){
177 while(*temp==' ')temp++;
178 for(cols=0;*temp;cols++){
179 while(*temp>32)temp++;
180 while(*temp==' ')temp++;
185 double *p=malloc(cols*sizeof(double));
186 if(start*num+b->dim>cols){
187 fprintf(stderr,"ran out of columns reading %s\n",file);
190 while(*line==' ')line++;
193 /* static length buffer bug workaround */
196 while(*temp>32)temp++;
203 while(*line>32)line++;
204 while(*line==' ')line++;
206 if(num<=0)num=(cols-start)/b->dim;
207 for(i=num-1;i>=0;i--){
209 double *base=p+start+i*b->dim;
211 /* if this is a sequenced book and i||start,
212 normalize the beginning to zero */
213 if(b->sequencep && (i>0 || start>0)){
214 for(k=0;k<b->dim;k++)
218 /* assign the point */
219 entry=vqenc_entry(b,base);
221 /* accumulate metrics */
222 for(k=0;k<b->dim;k++){
223 double err=base[k]-b->valuelist[k+entry*b->dim];
229 /* brute force it... did that work better? */
232 /* paint the cell if -m */
234 fprintf(cells,"%g %g\n%g %g\n\n",
236 b->valuelist[0+entry*b->dim],
237 b->valuelist[1+entry*b->dim]);
240 /* write cascading data */
254 if(cells)fclose(cells);
256 /* print accumulated error statistics */
257 fprintf(stderr,"results:\n\tmean squared error:%g\n\tmean error:%g\n\n",
258 sqrt(meansquare/mean_count),mean/mean_count);