1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: utility main for building codebooks from lattice descriptions
15 last mod: $Id: latticebuild.c,v 1.7 2000/11/06 00:07:25 xiphmont Exp $
17 ********************************************************************/
24 #include "vorbis/codebook.h"
25 #include "../lib/sharedbook.h"
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)
34 latticebuild description.vql
36 the lattice description file contains two lines:
38 <n> <dim> <multiplicitavep> <sequentialp>
39 <value_0> <value_1> <value_2> ... <value_n-1>
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...
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.
48 latticebuild produces a codebook on stdout */
50 static int ilog(unsigned int v){
59 int main(int argc,char *argv[]){
65 int entries=-1,dim=-1,quantvals=-1,addmul=-1,sequencep=0;
70 memset(&b,0,sizeof(b));
71 memset(&c,0,sizeof(c));
74 fprintf(stderr,"Need a lattice description file on the command line.\n");
80 char *filename=_ogg_calloc(strlen(argv[1])+4,1);
82 strcpy(filename,argv[1]);
83 in=fopen(filename,"r");
85 fprintf(stderr,"Could not open input file %s\n",filename);
89 ptr=strrchr(filename,'.');
92 name=strdup(filename);
94 name=strdup(filename);
99 /* read the description */
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");
107 entries=pow(quantvals,dim);
110 c.lengthlist=_ogg_malloc(entries*sizeof(long));
112 c.q_sequencep=sequencep;
113 c.quantlist=_ogg_calloc(quantvals,sizeof(long));
115 quantlist=_ogg_malloc(sizeof(long)*c.dim*c.entries);
116 hits=_ogg_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;
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");
129 /* gen a real quant list from the more easily human-grokked input */
131 float min=quantlist[0];
134 for(j=1;j<quantvals;j++)if(quantlist[j]<min)min=quantlist[j];
135 for(j=0;j<quantvals;j++)
136 for(i=j+1;i<quantvals;i++)
137 if(mindel==-1 || fabs(quantlist[j]-quantlist[i])<mindel)
138 mindel=fabs(quantlist[j]-quantlist[i]);
142 for(j=0;j<quantvals;j++){
143 float test=(quantlist[j]-min)/(mindel/fac);
144 if( fabs(rint(test)-test)>.000001) break;
146 if(j<quantvals)fac++;
150 fprintf(stderr,"min=%g mindel=%g\n",min,mindel);
152 c.q_min=_float32_pack(min);
153 c.q_delta=_float32_pack(mindel);
156 min=_float32_unpack(c.q_min);
157 mindel=_float32_unpack(c.q_delta);
158 for(j=0;j<quantvals;j++){
159 c.quantlist[j]=rint((quantlist[j]-min)/mindel);
160 if(ilog(c.quantlist[j])>c.q_quant)c.q_quant=ilog(c.quantlist[j]);
164 /* build the [default] codeword lengths */
165 memset(c.lengthlist,0,sizeof(long)*entries);
166 for(i=0;i<entries;i++)hits[i]=1;
167 build_tree_from_lengths(entries,hits,c.lengthlist);
169 /* save the book in C header form */
170 write_codebook(stdout,name,&c);