Remove svn $Id$ header.
[platform/upstream/libvorbis.git] / vq / latticebuild.c
index b0e9f7a..6171193 100644 (file)
@@ -1,18 +1,16 @@
 /********************************************************************
  *                                                                  *
- * 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 lattice descriptions
- last mod: $Id: latticebuild.c,v 1.4 2000/07/17 12:55:37 xiphmont Exp $
 
  ********************************************************************/
 
@@ -21,8 +19,6 @@
 #include <math.h>
 #include <string.h>
 #include <errno.h>
-#include "vorbis/codebook.h"
-#include "../lib/sharedbook.h"
 #include "bookutil.h"
 
 /* The purpose of this util is just to finish packaging the
@@ -77,7 +73,7 @@ int main(int argc,char *argv[]){
 
   {
     char *ptr;
-    char *filename=calloc(strlen(argv[1])+4,1);
+    char *filename=_ogg_calloc(strlen(argv[1])+4,1);
 
     strcpy(filename,argv[1]);
     in=fopen(filename,"r");
@@ -100,30 +96,35 @@ int main(int argc,char *argv[]){
   line=get_line(in);
   if(sscanf(line,"%d %d %d %d",&quantvals,&dim,&addmul,&sequencep)!=4){
     if(sscanf(line,"%d %d %d",&quantvals,&dim,&addmul)!=3){
-      fprintf(stderr,"Syntax error reading book file (line 1)\n");
+      fprintf(stderr,"Syntax error reading description file (line 1)\n");
       exit(1);
     }
   }
   entries=pow(quantvals,dim);
   c.dim=dim;
   c.entries=entries;
-  c.lengthlist=malloc(entries*sizeof(long));
+  c.lengthlist=_ogg_malloc(entries*sizeof(long));
   c.maptype=1;
   c.q_sequencep=sequencep;
-  c.quantlist=calloc(quantvals,sizeof(long));
+  c.quantlist=_ogg_calloc(quantvals,sizeof(long));
 
-  quantlist=malloc(sizeof(long)*c.dim*c.entries);
-  hits=malloc(c.entries*sizeof(long));
+  quantlist=_ogg_malloc(sizeof(double)*c.dim*c.entries);
+  hits=_ogg_malloc(c.entries*sizeof(long));
   for(j=0;j<entries;j++)hits[j]=1;
   for(j=0;j<entries;j++)c.lengthlist[j]=1;
 
   reset_next_value();
-  setup_line(in);
-  for(j=0;j<quantvals;j++){  
-    if(get_line_value(in,quantlist+j)==-1){
+  line=setup_line(in);
+  for(j=0;j<quantvals;j++){ 
+    char *temp;
+    if(!line || sscanf(line,"%lf",quantlist+j)!=1){
       fprintf(stderr,"Ran out of data on line 2 of description file\n");
       exit(1);
     }
+    temp=strchr(line,',');
+    if(!temp)temp=strchr(line,' ');
+    if(temp)temp++;
+    line=temp;
   }
 
   /* gen a real quant list from the more easily human-grokked input */
@@ -133,20 +134,22 @@ int main(int argc,char *argv[]){
     int fac=1;
     for(j=1;j<quantvals;j++)if(quantlist[j]<min)min=quantlist[j];
     for(j=0;j<quantvals;j++)
-      if(min!=quantlist[j] && (mindel==-1 || quantlist[j]-min<mindel))
-       mindel=quantlist[j]-min;
+      for(i=j+1;i<quantvals;i++)
+        if(mindel==-1 || fabs(quantlist[j]-quantlist[i])<mindel)
+          mindel=fabs(quantlist[j]-quantlist[i]);
 
-    fprintf(stderr,"min=%g mindel=%g\n",min,mindel);
     j=0;
     while(j<quantvals){
       for(j=0;j<quantvals;j++){
-       double test=(quantlist[j]-min)/(mindel/fac);
-       if( fabs(rint(test)-test)>.000001) break;
+        double test=fac*(quantlist[j]-min)/mindel;
+        if( fabs(rint(test)-test)>.00001f) break;
       }
+      if(fac>100)break;
       if(j<quantvals)fac++;
     }
 
     mindel/=fac;
+    fprintf(stderr,"min=%g mindel=%g\n",min,mindel);
 
     c.q_min=_float32_pack(min);
     c.q_delta=_float32_pack(mindel);
@@ -168,6 +171,6 @@ int main(int argc,char *argv[]){
   /* save the book in C header form */
   write_codebook(stdout,name,&c);
   fprintf(stderr,"\r                                                     "
-         "\nDone.\n");
+          "\nDone.\n");
   exit(0);
 }