Cleanup: Removed tailing white-spaces in C code files
[platform/upstream/libvorbis.git] / vq / latticetune.c
1 /********************************************************************
2  *                                                                  *
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.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the Xiph.Org Foundation http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: utility main for setting entropy encoding parameters
14            for lattice codebooks
15
16  ********************************************************************/
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <math.h>
21 #include <string.h>
22 #include <errno.h>
23 #include "bookutil.h"
24
25 static int strrcmp_i(char *s,char *cmp){
26   return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
27 }
28
29 /* This util takes a training-collected file listing codewords used in
30    LSP fitting, then generates new codeword lengths for maximally
31    efficient integer-bits entropy encoding.
32
33    command line:
34    latticetune book.vqh input.vqd [unused_entriesp]
35
36    latticetune produces book.vqh on stdout */
37
38 int main(int argc,char *argv[]){
39   codebook *b;
40   static_codebook *c;
41   long *lengths;
42   long *hits;
43
44   int entries=-1,dim=-1,guard=1;
45   FILE *in=NULL;
46   char *line,*name;
47   long j;
48
49   if(argv[1]==NULL){
50     fprintf(stderr,"Need a lattice codebook on the command line.\n");
51     exit(1);
52   }
53   if(argv[2]==NULL){
54     fprintf(stderr,"Need a codeword data file on the command line.\n");
55     exit(1);
56   }
57   if(argv[3]!=NULL)guard=0;
58
59   {
60     char *ptr;
61     char *filename=strdup(argv[1]);
62
63     b=codebook_load(filename);
64     c=(static_codebook *)(b->c);
65
66     ptr=strrchr(filename,'.');
67     if(ptr){
68       *ptr='\0';
69       name=strdup(filename);
70     }else{
71       name=strdup(filename);
72     }
73   }
74
75   if(c->maptype!=1){
76     fprintf(stderr,"Provided book is not a latticebook.\n");
77     exit(1);
78   }
79
80   entries=b->entries;
81   dim=b->dim;
82
83   hits=_ogg_malloc(entries*sizeof(long));
84   lengths=_ogg_calloc(entries,sizeof(long));
85   for(j=0;j<entries;j++)hits[j]=guard;
86
87   in=fopen(argv[2],"r");
88   if(!in){
89     fprintf(stderr,"Could not open input file %s\n",argv[2]);
90     exit(1);
91   }
92
93   if(!strrcmp_i(argv[0],"latticetune")){
94     long lines=0;
95     line=setup_line(in);
96     while(line){
97       long code;
98       lines++;
99       if(!(lines&0xfff))spinnit("codewords so far...",lines);
100
101       if(sscanf(line,"%ld",&code)==1)
102         hits[code]++;
103
104       line=setup_line(in);
105     }
106   }
107
108   /* now we simply count already collated by-entry data */
109   if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
110
111     line=setup_line(in);
112     while(line){
113
114       /* code:hits\n */
115       /* likely to have multiple listing for each code entry; must
116          accumulate */
117
118       char *pos=strchr(line,':');
119       if(pos){
120         long code=atol(line);
121         long val=atol(pos+1);
122         hits[code]+=val;
123       }
124
125       line=setup_line(in);
126     }
127   }
128
129   fclose(in);
130
131   /* build the codeword lengths */
132   build_tree_from_lengths0(entries,hits,lengths);
133
134   c->lengthlist=lengths;
135   write_codebook(stdout,name,c);
136
137   {
138     long bins=_book_maptype1_quantvals(c);
139     long i,k,base=c->lengthlist[0];
140     for(i=0;i<entries;i++)
141       if(c->lengthlist[i]>base)base=c->lengthlist[i];
142
143     for(j=0;j<entries;j++){
144       if(c->lengthlist[j]){
145         int indexdiv=1;
146         fprintf(stderr,"%4ld: ",j);
147         for(k=0;k<c->dim;k++){
148           int index= (j/indexdiv)%bins;
149           fprintf(stderr,"%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
150                  _float32_unpack(c->q_min));
151           indexdiv*=bins;
152         }
153         fprintf(stderr,"\t|");
154         for(k=0;k<base-c->lengthlist[j];k++)fprintf(stderr,"*");
155         fprintf(stderr,"\n");
156       }
157     }
158   }
159
160   fprintf(stderr,"\r                                                     "
161           "\nDone.\n");
162   exit(0);
163 }