Update header copyright dates, update copyright assignemnt
[platform/upstream/libvorbis.git] / vq / huffbuild.c
1 /********************************************************************
2  *                                                                  *
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.        *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: hufftree builder
14  last mod: $Id: huffbuild.c,v 1.7 2001/02/02 03:52:27 xiphmont Exp $
15
16  ********************************************************************/
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <math.h>
21 #include <stdio.h>
22 #include "bookutil.h"
23
24 static int nsofar=0;
25 static int getval(FILE *in,int begin,int n,int group,int max){
26   float v;
27   int i;
28   long val=0;
29
30   if(nsofar>=n || get_line_value(in,&v)){
31     reset_next_value();
32     nsofar=0;
33     if(get_next_value(in,&v))
34       return(-1);
35     for(i=1;i<=begin;i++)
36       get_line_value(in,&v);
37   }
38
39   val=(int)v;
40   nsofar++;
41
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));
45     else
46       val = val*max+(int)v;
47   return(val);
48 }
49
50 static void usage(){
51   fprintf(stderr,
52           "usage:\n" 
53           "huffbuild <input>.vqd <begin,n,group> [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");
59   exit(1);
60 }
61
62 int main(int argc, char *argv[]){
63   char *base;
64   char *infile;
65   int i,j,k,begin,n,subn,guard=1;
66   FILE *file;
67   int maxval=0;
68
69   if(argc<3)usage();
70   if(argc==4)guard=0;
71
72   infile=strdup(argv[1]);
73   base=strdup(infile);
74   if(strrchr(base,'.'))
75     strrchr(base,'.')[0]='\0';
76
77   {
78     char *pos=strchr(argv[2],',');
79     begin=atoi(argv[2]);
80     if(!pos)
81       usage();
82     else
83       n=atoi(pos+1);
84     pos=strchr(pos+1,',');
85     if(!pos)
86       usage();
87     else
88       subn=atoi(pos+1);
89     if(n/subn*subn != n){
90       fprintf(stderr,"n must be divisible by group\n");
91       exit(1);
92     }
93   }
94
95   /* scan the file for maximum value */
96   file=fopen(infile,"r");
97   if(!file){
98     fprintf(stderr,"Could not open file %s\n",infile);
99     exit(1);
100   }
101   i=0;
102   while(1){
103     long v;
104     if(get_next_ivalue(file,&v))break;
105     if(v>maxval)maxval=v;
106
107     if(!(i++&0xff))spinnit("loading... ",i);
108   }
109   rewind(file);
110   maxval++;
111
112   {
113     long vals=pow(maxval,subn);
114     long *hist=_ogg_malloc(vals*sizeof(long));
115     long *lengths=_ogg_malloc(vals*sizeof(long));
116     
117     for(j=0;j<vals;j++)hist[j]=guard;
118     
119     reset_next_value();
120     i/=subn;
121     while(!feof(file)){
122       long val=getval(file,begin,n,subn,maxval);
123       if(val==-1)break;
124       hist[val]++;
125       if(!(i--&0xff))spinnit("loading... ",i*subn);
126     }
127     fclose(file);
128  
129     /* we have the probabilities, build the tree */
130     fprintf(stderr,"Building tree for %ld entries\n",vals);
131     build_tree_from_lengths0(vals,hist,lengths);
132
133     /* save the book */
134     {
135       char *buffer=alloca(strlen(base)+5);
136       strcpy(buffer,base);
137       strcat(buffer,".vqh");
138       file=fopen(buffer,"w");
139       if(!file){
140         fprintf(stderr,"Could not open file %s\n",buffer);
141         exit(1);
142       }
143     }
144     
145     fprintf(file,
146  "/********************************************************************\n"
147  " *                                                                  *\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"
152  " *                                                                  *\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"
156  " *                                                                  *\n"
157  " ********************************************************************\n"
158  "\n"
159  " function: static codebook autogenerated by huff/huffbuld\n"
160  "\n"
161  " ********************************************************************/\n\n");
162
163     fprintf(file,"#ifndef _V_%s_VQH_\n#define _V_%s_VQH_\n",base,base);
164     fprintf(file,"#include \"codebook.h\"\n\n");
165     
166     /* first, the static vectors, then the book structure to tie it together. */
167     /* lengthlist */
168     fprintf(file,"static long _huff_lengthlist_%s[] = {\n",base);
169     for(j=0;j<vals;){
170       fprintf(file,"\t");
171       for(k=0;k<16 && j<vals;k++,j++)
172         fprintf(file,"%2ld,",lengths[j]);
173       fprintf(file,"\n");
174     }
175     fprintf(file,"};\n\n");
176     
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,"\t0\n};\n\n");
186     
187     fprintf(file,"\n#endif\n");
188     fclose(file);
189     fprintf(stderr,"Done.                                \n\n");
190   }
191   exit(0);
192 }
193
194