cd5735a5380efc46cf8824df765f54521d267ef4
[platform/upstream/libvorbis.git] / vq / latticebuild.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and the XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: utility main for building codebooks from lattice descriptions
15  last mod: $Id: latticebuild.c,v 1.9 2001/01/22 01:38:51 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <math.h>
22 #include <string.h>
23 #include <errno.h>
24 #include "bookutil.h"
25
26 /* The purpose of this util is just to finish packaging the
27    description into a static codebook.  It used to count hits for a
28    histogram, but I've divorced that out to add some flexibility (it
29    currently generates an equal probability codebook) 
30
31    command line:
32    latticebuild description.vql
33
34    the lattice description file contains two lines:
35
36    <n> <dim> <multiplicitavep> <sequentialp>
37    <value_0> <value_1> <value_2> ... <value_n-1>
38    
39    a threshmap (or pigeonmap) struct is generated by latticehint;
40    there are fun tricks one can do with the threshmap and cascades,
41    but the utils don't know them...
42
43    entropy encoding is done by feeding an entry list collected from a
44    training set and feeding it to latticetune along with the book.
45
46    latticebuild produces a codebook on stdout */
47
48 static int ilog(unsigned int v){
49   int ret=0;
50   while(v){
51     ret++;
52     v>>=1;
53   }
54   return(ret);
55 }
56
57 int main(int argc,char *argv[]){
58   codebook b;
59   static_codebook c;
60   double *quantlist;
61   long *hits;
62
63   int entries=-1,dim=-1,quantvals=-1,addmul=-1,sequencep=0;
64   FILE *in=NULL;
65   char *line,*name;
66   long i,j;
67
68   memset(&b,0,sizeof(b));
69   memset(&c,0,sizeof(c));
70
71   if(argv[1]==NULL){
72     fprintf(stderr,"Need a lattice description file on the command line.\n");
73     exit(1);
74   }
75
76   {
77     char *ptr;
78     char *filename=_ogg_calloc(strlen(argv[1])+4,1);
79
80     strcpy(filename,argv[1]);
81     in=fopen(filename,"r");
82     if(!in){
83       fprintf(stderr,"Could not open input file %s\n",filename);
84       exit(1);
85     }
86     
87     ptr=strrchr(filename,'.');
88     if(ptr){
89       *ptr='\0';
90       name=strdup(filename);
91     }else{
92       name=strdup(filename);
93     }
94
95   }
96   
97   /* read the description */
98   line=get_line(in);
99   if(sscanf(line,"%d %d %d %d",&quantvals,&dim,&addmul,&sequencep)!=4){
100     if(sscanf(line,"%d %d %d",&quantvals,&dim,&addmul)!=3){
101       fprintf(stderr,"Syntax error reading description file (line 1)\n");
102       exit(1);
103     }
104   }
105   entries=pow(quantvals,dim);
106   c.dim=dim;
107   c.entries=entries;
108   c.lengthlist=_ogg_malloc(entries*sizeof(long));
109   c.maptype=1;
110   c.q_sequencep=sequencep;
111   c.quantlist=_ogg_calloc(quantvals,sizeof(long));
112
113   quantlist=_ogg_malloc(sizeof(double)*c.dim*c.entries);
114   hits=_ogg_malloc(c.entries*sizeof(long));
115   for(j=0;j<entries;j++)hits[j]=1;
116   for(j=0;j<entries;j++)c.lengthlist[j]=1;
117
118   reset_next_value();
119   line=setup_line(in);
120   for(j=0;j<quantvals;j++){ 
121     char *temp;
122     if(!line || sscanf(line,"%lf",quantlist+j)!=1){
123       fprintf(stderr,"Ran out of data on line 2 of description file\n");
124       exit(1);
125     }
126     temp=strchr(line,',');
127     if(!temp)temp=strchr(line,' ');
128     if(temp)temp++;
129     line=temp;
130   }
131
132   /* gen a real quant list from the more easily human-grokked input */
133   {
134     double min=quantlist[0];
135     double mindel=-1;
136     int fac=1;
137     for(j=1;j<quantvals;j++)if(quantlist[j]<min)min=quantlist[j];
138     for(j=0;j<quantvals;j++)
139       for(i=j+1;i<quantvals;i++)
140         if(mindel==-1 || fabs(quantlist[j]-quantlist[i])<mindel)
141           mindel=fabs(quantlist[j]-quantlist[i]);
142
143     j=0;
144     while(j<quantvals){
145       for(j=0;j<quantvals;j++){
146         double test=fac*(quantlist[j]-min)/mindel;
147         if( fabs(rint(test)-test)>.00001f) break;
148       }
149       if(fac>100)break;
150       if(j<quantvals)fac++;
151     }
152
153     mindel/=fac;
154     fprintf(stderr,"min=%g mindel=%g\n",min,mindel);
155
156     c.q_min=_float32_pack(min);
157     c.q_delta=_float32_pack(mindel);
158     c.q_quant=0;
159
160     min=_float32_unpack(c.q_min);
161     mindel=_float32_unpack(c.q_delta);
162     for(j=0;j<quantvals;j++){
163       c.quantlist[j]=rint((quantlist[j]-min)/mindel);
164       if(ilog(c.quantlist[j])>c.q_quant)c.q_quant=ilog(c.quantlist[j]);
165     }
166   }
167
168   /* build the [default] codeword lengths */
169   memset(c.lengthlist,0,sizeof(long)*entries);
170   for(i=0;i<entries;i++)hits[i]=1;
171   build_tree_from_lengths(entries,hits,c.lengthlist);
172
173   /* save the book in C header form */
174   write_codebook(stdout,name,&c);
175   fprintf(stderr,"\r                                                     "
176           "\nDone.\n");
177   exit(0);
178 }