1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: hufftree builder
14 last mod: $Id: huffbuild.c,v 1.13 2002/06/28 22:19:56 xiphmont Exp $
16 ********************************************************************/
25 static int getval(FILE *in,int begin,int n,int group,int max){
30 if(nsofar>=n || get_line_value(in,&v)){
33 if(get_next_value(in,&v))
36 get_line_value(in,&v);
42 for(i=1;i<group;i++,nsofar++)
43 if(nsofar>=n || get_line_value(in,&v))
44 return(getval(in,begin,n,group,max));
53 "huffbuild <input>.vqd <begin,n,group>|<lorange-hirange> [noguard]\n"
54 " where begin,n,group is first scalar, \n"
55 " number of scalars of each in line,\n"
56 " number of scalars in a group\n"
57 "eg: huffbuild reslongaux.vqd 0,1024,4\n"
58 "produces reslongaux.vqh\n\n");
62 int main(int argc, char *argv[]){
65 int i,j,k,begin,n,subn,guard=1;
73 infile=strdup(argv[1]);
76 strrchr(base,'.')[0]='\0';
79 char *pos=strchr(argv[2],',');
80 char *dpos=strchr(argv[2],'-');
92 pos=strchr(pos+1,',');
98 fprintf(stderr,"n must be divisible by group\n");
104 /* scan the file for maximum value */
105 file=fopen(infile,"r");
107 fprintf(stderr,"Could not open file %s\n",infile);
111 fprintf(stderr," making untrained books.\n");
119 if(get_next_ivalue(file,&v))break;
120 if(v>maxval)maxval=v;
122 if(!(i++&0xff))spinnit("loading... ",i);
129 long vals=pow(maxval,subn);
130 long *hist=_ogg_calloc(vals,sizeof(long));
131 long *lengths=_ogg_calloc(vals,sizeof(long));
133 for(j=loval;j<vals;j++)hist[j]=guard;
139 long val=getval(file,begin,n,subn,maxval);
140 if(val==-1 || val>=vals)break;
142 if(!(i--&0xff))spinnit("loading... ",i*subn);
147 /* we have the probabilities, build the tree */
148 fprintf(stderr,"Building tree for %ld entries\n",vals);
149 build_tree_from_lengths0(vals,hist,lengths);
153 char *buffer=alloca(strlen(base)+5);
155 strcat(buffer,".vqh");
156 file=fopen(buffer,"w");
158 fprintf(stderr,"Could not open file %s\n",buffer);
163 /* first, the static vectors, then the book structure to tie it together. */
165 fprintf(file,"static long _huff_lengthlist_%s[] = {\n",base);
168 for(k=0;k<16 && j<vals;k++,j++)
169 fprintf(file,"%2ld,",lengths[j]);
172 fprintf(file,"};\n\n");
174 /* the toplevel book */
175 fprintf(file,"static static_codebook _huff_book_%s = {\n",base);
176 fprintf(file,"\t%d, %ld,\n",subn,vals);
177 fprintf(file,"\t_huff_lengthlist_%s,\n",base);
178 fprintf(file,"\t0, 0, 0, 0, 0,\n");
179 fprintf(file,"\tNULL,\n");
181 fprintf(file,"\tNULL,\n");
182 fprintf(file,"\tNULL,\n");
183 fprintf(file,"\tNULL,\n");
184 fprintf(file,"\t0\n};\n\n");
187 fprintf(stderr,"Done. \n\n");