Update website link in copyright headers.
[platform/upstream/libvorbis.git] / vq / latticebuild.c
index ef1e577..0f87e6a 100644 (file)
@@ -1,18 +1,16 @@
 /********************************************************************
  *                                                                  *
  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
- * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
- * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
+ * 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-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 https://xiph.org/                     *
  *                                                                  *
  ********************************************************************
 
  function: utility main for building codebooks from lattice descriptions
- last mod: $Id: latticebuild.c,v 1.7 2000/11/06 00:07:25 xiphmont Exp $
 
  ********************************************************************/
 
 #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
    description into a static codebook.  It used to count hits for a
    histogram, but I've divorced that out to add some flexibility (it
-   currently generates an equal probability codebook) 
+   currently generates an equal probability codebook)
 
    command line:
    latticebuild description.vql
@@ -37,7 +33,7 @@
 
    <n> <dim> <multiplicitavep> <sequentialp>
    <value_0> <value_1> <value_2> ... <value_n-1>
-   
+
    a threshmap (or pigeonmap) struct is generated by latticehint;
    there are fun tricks one can do with the threshmap and cascades,
    but the utils don't know them...
@@ -59,7 +55,7 @@ static int ilog(unsigned int v){
 int main(int argc,char *argv[]){
   codebook b;
   static_codebook c;
-  float *quantlist;
+  double *quantlist;
   long *hits;
 
   int entries=-1,dim=-1,quantvals=-1,addmul=-1,sequencep=0;
@@ -85,7 +81,7 @@ int main(int argc,char *argv[]){
       fprintf(stderr,"Could not open input file %s\n",filename);
       exit(1);
     }
-    
+
     ptr=strrchr(filename,'.');
     if(ptr){
       *ptr='\0';
@@ -95,12 +91,12 @@ int main(int argc,char *argv[]){
     }
 
   }
-  
+
   /* read the description */
   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);
     }
   }
@@ -112,37 +108,43 @@ int main(int argc,char *argv[]){
   c.q_sequencep=sequencep;
   c.quantlist=_ogg_calloc(quantvals,sizeof(long));
 
-  quantlist=_ogg_malloc(sizeof(long)*c.dim*c.entries);
+  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 */
   {
-    float min=quantlist[0];
-    float mindel=-1;
+    double min=quantlist[0];
+    double mindel=-1;
     int fac=1;
     for(j=1;j<quantvals;j++)if(quantlist[j]<min)min=quantlist[j];
     for(j=0;j<quantvals;j++)
       for(i=j+1;i<quantvals;i++)
-       if(mindel==-1 || fabs(quantlist[j]-quantlist[i])<mindel)
-         mindel=fabs(quantlist[j]-quantlist[i]);
+        if(mindel==-1 || fabs(quantlist[j]-quantlist[i])<mindel)
+          mindel=fabs(quantlist[j]-quantlist[i]);
 
     j=0;
     while(j<quantvals){
       for(j=0;j<quantvals;j++){
-       float 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++;
     }
 
@@ -169,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);
 }