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-2014 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: hufftree builder
15 ********************************************************************/
24 static int getval(FILE *in,int begin,int n,int group,int max){
29 if(nsofar>=n || get_line_value(in,&v)){
32 if(get_next_value(in,&v))
35 get_line_value(in,&v);
41 for(i=1;i<group;i++,nsofar++)
42 if(nsofar>=n || get_line_value(in,&v))
43 return(getval(in,begin,n,group,max));
52 "huffbuild <input>.vqd <begin,n,group>|<lorange-hirange> [noguard]\n"
53 " where begin,n,group is first scalar, \n"
54 " number of scalars of each in line,\n"
55 " number of scalars in a group\n"
56 "eg: huffbuild reslongaux.vqd 0,1024,4\n"
57 "produces reslongaux.vqh\n\n");
61 int main(int argc, char *argv[]){
64 int i,j,k,begin,n,subn,guard=1;
72 infile=strdup(argv[1]);
75 strrchr(base,'.')[0]='\0';
78 char *pos=strchr(argv[2],',');
79 char *dpos=strchr(argv[2],'-');
91 pos=strchr(pos+1,',');
97 fprintf(stderr,"n must be divisible by group\n");
103 /* scan the file for maximum value */
104 file=fopen(infile,"r");
106 fprintf(stderr,"Could not open file %s\n",infile);
110 fprintf(stderr," making untrained books.\n");
118 if(get_next_ivalue(file,&v))break;
119 if(v>maxval)maxval=v;
121 if(!(i++&0xff))spinnit("loading... ",i);
128 long vals=pow(maxval,subn);
129 long *hist=_ogg_calloc(vals,sizeof(long));
130 long *lengths=_ogg_calloc(vals,sizeof(long));
132 for(j=loval;j<vals;j++)hist[j]=guard;
138 long val=getval(file,begin,n,subn,maxval);
139 if(val==-1 || val>=vals)break;
141 if(!(i--&0xff))spinnit("loading... ",i*subn);
146 /* we have the probabilities, build the tree */
147 fprintf(stderr,"Building tree for %ld entries\n",vals);
148 build_tree_from_lengths0(vals,hist,lengths);
152 char *buffer=alloca(strlen(base)+5);
154 strcat(buffer,".vqh");
155 file=fopen(buffer,"w");
157 fprintf(stderr,"Could not open file %s\n",buffer);
162 /* first, the static vectors, then the book structure to tie it together. */
164 fprintf(file,"static const char _huff_lengthlist_%s[] = {\n",base);
167 for(k=0;k<16 && j<vals;k++,j++)
168 fprintf(file,"%2ld,",lengths[j]);
171 fprintf(file,"};\n\n");
173 /* the toplevel book */
174 fprintf(file,"static const static_codebook _huff_book_%s = {\n",base);
175 fprintf(file,"\t%d, %ld,\n",subn,vals);
176 fprintf(file,"\t(char *)_huff_lengthlist_%s,\n",base);
177 fprintf(file,"\t0, 0, 0, 0, 0,\n");
178 fprintf(file,"\tNULL,\n");
180 fprintf(file,"\t0\n};\n\n");
183 fprintf(stderr,"Done. \n\n");