Merging the postbeta2 branch onto the mainline.
[platform/upstream/libvorbis.git] / vq / latticetune.c
1 /********************************************************************
2  *                                                                  *
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.                            *
7  *                                                                  *
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/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: utility main for setting entropy encoding parameters
15            for lattice codebooks
16  last mod: $Id: latticetune.c,v 1.3 2000/10/12 03:13:02 xiphmont Exp $
17
18  ********************************************************************/
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <string.h>
24 #include <errno.h>
25 #include "vorbis/codebook.h"
26 #include "../lib/sharedbook.h"
27 #include "bookutil.h"
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=malloc(entries*sizeof(long));
84   lengths=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(!strcmp(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   if(!strcmp(argv[0],"restune")){
109     long step;
110     long lines=0;
111     long cols=-1;
112     float *vec;
113     line=setup_line(in);
114     while(line){
115       int code;
116       if(!(lines&0xfff))spinnit("codewords so far...",lines);
117
118       if(cols==-1){
119         char *temp=line;
120         while(*temp==' ')temp++;
121         for(cols=0;*temp;cols++){
122           while(*temp>32)temp++;
123           while(*temp==' ')temp++;
124         }
125         vec=alloca(sizeof(float)*cols);
126         step=cols/dim;
127       }
128       
129       for(j=0;j<cols;j++)
130         if(get_line_value(in,vec+j)){
131           fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
132           exit(1);
133         }
134       
135       for(j=0;j<step;j++){
136         lines++;
137         code=_best(b,vec+j,step);
138         hits[code]++;
139       }
140
141       line=setup_line(in);
142     }
143   }
144
145   fclose(in);
146
147   /* build the codeword lengths */
148   build_tree_from_lengths0(entries,hits,lengths);
149
150   c->lengthlist=lengths;
151   write_codebook(stdout,name,c); 
152   
153   fprintf(stderr,"\r                                                     "
154           "\nDone.\n");
155   exit(0);
156 }