Update the VQ tools to pared down codebook abstraction.
[platform/upstream/libvorbis.git] / vq / build.c
index b15aa52..ac76bc8 100644 (file)
@@ -1,18 +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-2000             *
- * by 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
- last mod: $Id: build.c,v 1.9 2000/01/21 13:42:36 xiphmont Exp $
+ last mod: $Id$
 
  ********************************************************************/
 
@@ -21,7 +20,7 @@
 #include <math.h>
 #include <string.h>
 #include <errno.h>
-#include "vorbis/codebook.h"
+#include "bookutil.h"
 
 #include "vqgen.h"
 #include "vqsplit.h"
@@ -37,26 +36,26 @@ static char *rline(FILE *in,FILE *out){
 
     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;
+        }
       }
     }
     
@@ -131,12 +130,12 @@ int main(int argc,char *argv[]){
   }
   
   /* 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);
   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);
   }
@@ -145,12 +144,12 @@ 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(long)*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);
-      sscanf(line,"%lf",&a);
+      sscanf(line,"%f",&a);
       v.entrylist[i]=a;
       quantlist[i++]=rint(a);
     }
@@ -163,14 +162,14 @@ int main(int argc,char *argv[]){
   
   /* training points */
   {
-    double *b=alloca(sizeof(double)*(dim+aux));
+    float *b=alloca(sizeof(float)*(dim+aux));
     i=0;
     v.entries=0; /* hack to avoid reseeding */
     while(1){
       for(k=0;k<dim+aux;k++){
-       line=rline(in,out);
-       if(!line)break;
-       sscanf(line,"%lf",b+k);
+        line=rline(in,out);
+        if(!line)break;
+        sscanf(line,"%f",b+k);
       }
       if(feof(in))break;
       vqgen_addpoint(&v,b,NULL);
@@ -183,109 +182,14 @@ int main(int argc,char *argv[]){
 
   /* build the book */
   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 */
-  fprintf(out,
- "/********************************************************************\n"
- " *                                                                  *\n"
- " * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *\n"
- " * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *\n"
- " * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *\n"
- " * PLEASE READ THESE TERMS DISTRIBUTING.                            *\n"
- " *                                                                  *\n"
- " * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999             *\n"
- " * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company       *\n"
- " * http://www.xiph.org/                                             *\n"
- " *                                                                  *\n"
- " ********************************************************************\n"
- "\n"
- " function: static codebook autogenerated by vq/vqbuild\n"
- "\n"
- " ********************************************************************/\n\n");
+  write_codebook(out,name,b.c);
 
-  fprintf(out,"#ifndef _V_%s_VQH_\n#define _V_%s_VQH_\n",name,name);
-  fprintf(out,"#include \"vorbis/codebook.h\"\n\n");
-
-  /* first, the static vectors, then the book structure to tie it together. */
-  /* quantlist */
-  fprintf(out,"static long _vq_quantlist_%s[] = {\n",name);
-  i=0;
-  for(j=0;j<entries;j++){
-    fprintf(out,"\t");
-    for(k=0;k<dim;k++)
-      fprintf(out,"%5ld, ",c.quantlist[i++]);
-    fprintf(out,"\n");
-  }
-  fprintf(out,"};\n\n");
-
-  /* lengthlist */
-  fprintf(out,"static long _vq_lengthlist_%s[] = {\n",name);
-  for(j=0;j<entries;){
-    fprintf(out,"\t");
-    for(k=0;k<16 && j<entries;k++,j++)
-      fprintf(out,"%2ld,",c.lengthlist[j]);
-    fprintf(out,"\n");
-  }
-  fprintf(out,"};\n\n");
-
-  /* ptr0 */
-  fprintf(out,"static long _vq_ptr0_%s[] = {\n",name);
-  for(j=0;j<c.encode_tree->aux;){
-    fprintf(out,"\t");
-    for(k=0;k<8 && j<c.encode_tree->aux;k++,j++)
-      fprintf(out,"%6ld,",c.encode_tree->ptr0[j]);
-    fprintf(out,"\n");
-  }
-  fprintf(out,"};\n\n");
-
-  /* ptr1 */
-  fprintf(out,"static long _vq_ptr1_%s[] = {\n",name);
-  for(j=0;j<c.encode_tree->aux;){
-    fprintf(out,"\t");
-    for(k=0;k<8 && j<c.encode_tree->aux;k++,j++)
-      fprintf(out,"%6ld,",c.encode_tree->ptr1[j]);
-    fprintf(out,"\n");
-  }
-  fprintf(out,"};\n\n");
-
-  /* p */
-  fprintf(out,"static long _vq_p_%s[] = {\n",name);
-  for(j=0;j<c.encode_tree->aux;){
-    fprintf(out,"\t");
-    for(k=0;k<8 && j<c.encode_tree->aux;k++,j++)
-      fprintf(out,"%6ld,",c.encode_tree->p[j]);
-    fprintf(out,"\n");
-  }
-  fprintf(out,"};\n\n");
-
-  /* q */
-  fprintf(out,"static long _vq_q_%s[] = {\n",name);
-  for(j=0;j<c.encode_tree->aux;){
-    fprintf(out,"\t");
-    for(k=0;k<8 && j<c.encode_tree->aux;k++,j++)
-      fprintf(out,"%6ld,",c.encode_tree->q[j]);
-    fprintf(out,"\n");
-  }
-  fprintf(out,"};\n\n");
-
-  /* tie it all together */
-
-  fprintf(out,"static encode_aux _vq_aux_%s = {\n",name);
-  fprintf(out,"\t_vq_ptr0_%s,\n",name);
-  fprintf(out,"\t_vq_ptr1_%s,\n",name);
-  fprintf(out,"\t_vq_p_%s,\n",name);
-  fprintf(out,"\t_vq_q_%s,\n",name);
-  fprintf(out,"\t%ld, %ld\n};\n\n",c.encode_tree->aux,c.encode_tree->aux);
-
-  fprintf(out,"static static_codebook _vq_book_%s = {\n",name);
-  fprintf(out,"\t%ld, %ld, %ld, %ld, %d, %d,\n",
-         c.dim,c.entries,q.min,q.delta,q.quant,q.sequencep);
-  fprintf(out,"\t_vq_quantlist_%s,\n",name);
-  fprintf(out,"\t_vq_lengthlist_%s,\n",name);
-  fprintf(out,"\t&_vq_aux_%s,\n",name);
-  fprintf(out,"};\n\n");
-
-  fprintf(out,"\n#endif\n");
   fclose(out);
   exit(0);
 }