Update the VQ tools to pared down codebook abstraction.
[platform/upstream/libvorbis.git] / vq / build.c
index 3269923..ac76bc8 100644 (file)
@@ -1,20 +1,17 @@
 /********************************************************************
  *                                                                  *
- * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
- * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
- * PLEASE READ THESE TERMS DISTRIBUTING.                            *
+ * 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 OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999             *
- * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company       *
- * http://www.xiph.org/                                             *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
+ * by the Xiph.Org Foundation http://www.xiph.org/                  *
  *                                                                  *
  ********************************************************************
 
  function: utility main for building codebooks from training sets
- author: Monty <xiphmont@mit.edu>
- modifications by: Monty
- last modification date: Dec 15 1999
+ last mod: $Id$
 
  ********************************************************************/
 
 #include <math.h>
 #include <string.h>
 #include <errno.h>
+#include "bookutil.h"
+
 #include "vqgen.h"
+#include "vqsplit.h"
 
 static char *linebuffer=NULL;
 static int  lbufsize=0;
-static char *rline(FILE *in,FILE *out,int pass){
+static char *rline(FILE *in,FILE *out){
   long sofar=0;
   if(feof(in))return NULL;
 
@@ -36,31 +36,30 @@ static char *rline(FILE *in,FILE *out,int pass){
 
     while(!gotline){
       if(sofar>=lbufsize){
-       if(!lbufsize){  
-         lbufsize=1024;
-         linebuffer=malloc(lbufsize);
-       }else{
-         lbufsize*=2;
-         linebuffer=realloc(linebuffer,lbufsize);
-       }
+        if(!lbufsize){        
+          lbufsize=1024;
+          linebuffer=_ogg_malloc(lbufsize);
+        }else{
+          lbufsize*=2;
+          linebuffer=_ogg_realloc(linebuffer,lbufsize);
+        }
       }
       {
-       long c=fgetc(in);
-       switch(c){
-       case '\n':
-       case EOF:
-         gotline=1;
-         break;
-       default:
-         linebuffer[sofar++]=c;
-         linebuffer[sofar]='\0';
-         break;
-       }
+        long c=fgetc(in);
+        switch(c){
+        case '\n':
+        case EOF:
+          gotline=1;
+          break;
+        default:
+          linebuffer[sofar++]=c;
+          linebuffer[sofar]='\0';
+          break;
+        }
       }
     }
     
     if(linebuffer[0]=='#'){
-      if(pass)fprintf(out,"%s\n",linebuffer);
       sofar=0;
     }else{
       return(linebuffer);
@@ -74,16 +73,19 @@ static char *rline(FILE *in,FILE *out,int pass){
 
 int main(int argc,char *argv[]){
   vqgen v;
-  vqbook b;
+  static_codebook c;
+  codebook b;
   quant_meta q;
-  int *quantlist=NULL;
 
-  int entries=-1,dim=-1,dummy;
+  long *quantlist=NULL;
+  int entries=-1,dim=-1,aux=-1;
   FILE *out=NULL;
   FILE *in=NULL;
   char *line,*name;
   long i,j,k;
 
+  b.c=&c;
+
   if(argv[1]==NULL){
     fprintf(stderr,"Need a trained data set on the command line.\n");
     exit(1);
@@ -99,14 +101,14 @@ int main(int argc,char *argv[]){
       exit(1);
     }
     
-    ptr=strrchr(filename,'.');
+    ptr=strrchr(filename,'-');
     if(ptr){
       *ptr='\0';
-      name=strdup(ptr);
-      sprintf(ptr,".h");
+      name=strdup(filename);
+      sprintf(ptr,".vqh");
     }else{
       name=strdup(filename);
-      strcat(filename,".h");
+      strcat(filename,".vqh");
     }
 
     out=fopen(filename,"w");
@@ -119,21 +121,21 @@ int main(int argc,char *argv[]){
   /* suck in the trained book */
 
   /* read book type, but it doesn't matter */
-  line=rline(in,out,1);
+  line=rline(in,out);
   
-  line=rline(in,out,1);
-  if(sscanf(line,"%d %d %d",&entries,&dim,&dummy)!=2){
+  line=rline(in,out);
+  if(sscanf(line,"%d %d %d",&entries,&dim,&aux)!=3){
     fprintf(stderr,"Syntax error reading book file\n");
     exit(1);
   }
   
   /* just use it to allocate mem */
-  vqgen_init(&v,dim,0,entries,NULL,NULL);
+  vqgen_init(&v,dim,0,entries,0.f,NULL,NULL,0);
   
   /* quant */
-  line=rline(in,out,1);
+  line=rline(in,out);
   if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
-           &q.quant,&q.sequencep)!=4){
+            &q.quant,&q.sequencep)!=4){
     fprintf(stderr,"Syntax error reading book file\n");
     exit(1);
   }
@@ -142,32 +144,32 @@ int main(int argc,char *argv[]){
   /* save quant data; we don't want to requantize later as our method
      is currently imperfect wrt repeated application */
   i=0;
-  quantlist=malloc(sizeof(int)*v.elements*v.entries);
+  quantlist=_ogg_malloc(sizeof(long)*v.elements*v.entries);
   for(j=0;j<entries;j++){
-    double a;
+    float a;
     for(k=0;k<dim;k++){
-      line=rline(in,out,0);
-      sscanf(line,"%lf",&a);
+      line=rline(in,out);
+      sscanf(line,"%f",&a);
       v.entrylist[i]=a;
       quantlist[i++]=rint(a);
     }
   }    
   
   /* ignore bias */
-  for(j=0;j<entries;j++)line=rline(in,out,0);
+  for(j=0;j<entries;j++)line=rline(in,out);
   free(v.bias);
   v.bias=NULL;
   
   /* training points */
   {
-    double b[80];
+    float *b=alloca(sizeof(float)*(dim+aux));
     i=0;
     v.entries=0; /* hack to avoid reseeding */
     while(1){
-      for(k=0;k<dim && k<80;k++){
-       line=rline(in,out,0);
-       if(!line)break;
-       sscanf(line,"%lf",b+k);
+      for(k=0;k<dim+aux;k++){
+        line=rline(in,out);
+        if(!line)break;
+        sscanf(line,"%f",b+k);
       }
       if(feof(in))break;
       vqgen_addpoint(&v,b,NULL);
@@ -179,12 +181,15 @@ int main(int argc,char *argv[]){
   vqgen_unquantize(&v,&q);
 
   /* build the book */
-  vqsp_book(&v,&b);
+  vqsp_book(&v,&b,quantlist);
+  c.q_min=q.min;
+  c.q_delta=q.delta;
+  c.q_quant=q.quant;
+  c.q_sequencep=q.sequencep;
 
   /* save the book in C header form */
+  write_codebook(out,name,b.c);
 
-
-
-
+  fclose(out);
   exit(0);
 }