1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: hufftree builder
15 last mod: $Id: huffbuild.c,v 1.3 2000/05/08 20:49:42 xiphmont Exp $
17 ********************************************************************/
23 #include "../vq/bookutil.h"
26 static int getval(FILE *in,int begin,int n,int group,int max){
31 if(nsofar>=n || get_line_value(in,&v)){
34 if(get_next_value(in,&v))
37 get_line_value(in,&v);
43 for(i=1;i<group;i++,nsofar++)
44 if(nsofar>=n || get_line_value(in,&v))
45 return(getval(in,begin,n,group,max));
54 "huffbuild <input>.vqd <begin,n,group>\n"
55 " where begin,n,group is first scalar, \n"
56 " number of scalars of each in line,\n"
57 " number of scalars in a group\n"
58 "eg: huffbuild reslongaux.vqd 0,1024,4\n"
59 "produces reslongaux.vqh\n\n");
63 int main(int argc, char *argv[]){
66 int i,j,k,begin,n,subn;
72 infile=strdup(argv[1]);
75 strrchr(base,'.')[0]='\0';
78 char *pos=strchr(argv[2],',');
84 pos=strchr(pos+1,',');
90 fprintf(stderr,"n must be divisible by group\n");
95 /* scan the file for maximum value */
96 file=fopen(infile,"r");
98 fprintf(stderr,"Could not open file %s\n",infile);
104 if(get_next_ivalue(file,&v))break;
105 if(v>maxval)maxval=v;
107 if(!(i++&0xff))spinnit("loading... ",i);
113 long vals=pow(maxval,subn);
114 long *hist=malloc(vals*sizeof(long));
115 long *lengths=malloc(vals*sizeof(long));
117 for(j=0;j<vals;j++)hist[j]=1;
122 long val=getval(file,begin,n,subn,maxval);
125 if(!(i--&0xff))spinnit("loading... ",i*subn);
129 /* we have the probabilities, build the tree */
130 fprintf(stderr,"Building tree for %ld entries\n",vals);
131 build_tree_from_lengths(vals,hist,lengths);
135 char *buffer=alloca(strlen(base)+5);
137 strcat(buffer,".vqh");
138 file=fopen(buffer,"w");
140 fprintf(stderr,"Could not open file %s\n",buffer);
146 "/********************************************************************\n"
148 " * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *\n"
149 " * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *\n"
150 " * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *\n"
151 " * PLEASE READ THESE TERMS DISTRIBUTING. *\n"
153 " * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *\n"
154 " * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *\n"
155 " * http://www.xiph.org/ *\n"
157 " ********************************************************************\n"
159 " function: static codebook autogenerated by huff/huffbuld\n"
161 " ********************************************************************/\n\n");
163 fprintf(file,"#ifndef _V_%s_VQH_\n#define _V_%s_VQH_\n",base,base);
164 fprintf(file,"#include \"vorbis/codebook.h\"\n\n");
166 /* first, the static vectors, then the book structure to tie it together. */
168 fprintf(file,"static long _huff_lengthlist_%s[] = {\n",base);
171 for(k=0;k<16 && j<vals;k++,j++)
172 fprintf(file,"%2ld,",lengths[j]);
175 fprintf(file,"};\n\n");
177 /* the toplevel book */
178 fprintf(file,"static static_codebook _huff_book_%s = {\n",base);
179 fprintf(file,"\t%d, %ld,\n",subn,vals);
180 fprintf(file,"\t_huff_lengthlist_%s,\n",base);
181 fprintf(file,"\t0, 0, 0, 0, 0,\n");
182 fprintf(file,"\tNULL,\n");
183 fprintf(file,"\tNULL,\n");
184 fprintf(file,"\tNULL,\n");
185 fprintf(file,"};\n\n");
187 fprintf(file,"\n#endif\n");
189 fprintf(stderr,"Done. \n\n");