/******************************************************************** * * * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** function: utility main for training codebooks last mod: $Id: train.c 13293 2007-07-24 00:09:47Z xiphmont $ ********************************************************************/ #include #include #include #include #include #include #include "vqgen.h" #include "vqext.h" #include "bookutil.h" static char *rline(FILE *in,FILE *out,int pass){ while(1){ char *line=get_line(in); if(line && line[0]=='#'){ if(pass)fprintf(out,"%s\n",line); }else{ return(line); } } } /* command line: trainvq vqfile [options] trainfile [trainfile] options: -params entries,dim,quant -subvector start[,num] -error desired_error -iterations iterations */ static void usage(void){ fprintf(stderr, "\nOggVorbis %s VQ codebook trainer\n\n" "vqtrain vqfile [options] [datasetfile] [datasetfile]\n" "options: -p[arams] \n" " -s[ubvector] \n" " -e[rror] \n" " -i[terations] \n" " -d[istance] quantization mesh spacing for density limitation\n" " -b eliminate cell size biasing; use normal LBG\n\n" " -c Use centroid (not median) midpoints\n" "examples:\n" " train a new codebook to 1%% tolerance on datafile 'foo':\n" " xxxvqtrain book -p 256,6,8 -e .01 foo\n" " (produces a trained set in book-0.vqi)\n\n" " continue training 'book-0.vqi' (produces book-1.vqi):\n" " xxxvqtrain book-0.vqi\n\n" " add subvector from element 1 to from files\n" " data*.m to the training in progress, prodicing book-1.vqi:\n" " xxxvqtrain book-0.vqi -s 1,1 data*.m\n\n",vqext_booktype); } int exiting=0; void setexit(int dummy){ fprintf(stderr,"\nexiting... please wait to finish this iteration\n"); exiting=1; } int main(int argc,char *argv[]){ vqgen v; int entries=-1,dim=-1; int start=0,num=-1; float desired=.05f,mindist=0.f; int iter=1000; int biasp=1; int centroid=0; FILE *out=NULL; char *line; long i,j,k; int init=0; q.quant=-1; argv++; if(!*argv){ usage(); exit(0); } /* get the book name, a preexisting book to continue training */ { FILE *in=NULL; char *filename=alloca(strlen(*argv)+30),*ptr; strcpy(filename,*argv); in=fopen(filename,"r"); ptr=strrchr(filename,'-'); if(ptr){ int num; ptr++; num=atoi(ptr); sprintf(ptr,"%d.vqi",num+1); }else strcat(filename,"-0.vqi"); out=fopen(filename,"w"); if(out==NULL){ fprintf(stderr,"Unable to open %s for writing\n",filename); exit(1); } if(in){ /* we wish to suck in a preexisting book and continue to train it */ float a; line=rline(in,out,1); if(strcmp(line,vqext_booktype)){ fprintf(stderr,"wrong book type; %s!=%s\n",line,vqext_booktype); exit(1); } line=rline(in,out,1); if(sscanf(line,"%d %d %d",&entries,&dim,&vqext_aux)!=3){ fprintf(stderr,"Syntax error reading book file\n"); exit(1); } vqgen_init(&v,dim,vqext_aux,entries,mindist, vqext_metric,vqext_weight,centroid); init=1; /* quant setup */ line=rline(in,out,1); if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta, &q.quant,&q.sequencep)!=4){ fprintf(stderr,"Syntax error reading book file\n"); exit(1); } /* quantized entries */ i=0; for(j=0;j32)temp++; while(*temp==' ')temp++; } fprintf(stderr,"%d colums per line in file %s\n",cols,file); } { int i; float b[cols]; if(start+num*dim>cols){ fprintf(stderr,"ran out of columns reading %s\n",file); exit(1); } while(*line==' ')line++; for(i=0;i32)temp++; old=temp[0]; temp[0]='\0'; b[i]=atof(line); temp[0]=old; while(*line>32)line++; while(*line==' ')line++; } if(num<=0)num=(cols-start)/dim; for(i=0;i