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