Fixes for codebook geenration (keeping up to date)
[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 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-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.5 2000/11/08 03:23:23 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 "bookutil.h"
26
27 static char *strrcmp_i(char *s,char *cmp){
28   return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
29 }
30
31 /* This util takes a training-collected file listing codewords used in
32    LSP fitting, then generates new codeword lengths for maximally
33    efficient integer-bits entropy encoding.
34
35    command line:
36    latticetune book.vqh input.vqd [unused_entriesp]
37
38    latticetune produces book.vqh on stdout */
39
40 int main(int argc,char *argv[]){
41   codebook *b;
42   static_codebook *c;
43   long *lengths;
44   long *hits;
45
46   int entries=-1,dim=-1,guard=1;
47   FILE *in=NULL;
48   char *line,*name;
49   long j;
50
51   if(argv[1]==NULL){
52     fprintf(stderr,"Need a lattice codebook on the command line.\n");
53     exit(1);
54   }
55   if(argv[2]==NULL){
56     fprintf(stderr,"Need a codeword data file on the command line.\n");
57     exit(1);
58   }
59   if(argv[3]!=NULL)guard=0;
60
61   {
62     char *ptr;
63     char *filename=strdup(argv[1]);
64
65     b=codebook_load(filename);
66     c=(static_codebook *)(b->c);
67     
68     ptr=strrchr(filename,'.');
69     if(ptr){
70       *ptr='\0';
71       name=strdup(filename);
72     }else{
73       name=strdup(filename);
74     }
75   }
76
77   if(c->maptype!=1){
78     fprintf(stderr,"Provided book is not a latticebook.\n");
79     exit(1);
80   }
81
82   entries=b->entries;
83   dim=b->dim;
84
85   hits=_ogg_malloc(entries*sizeof(long));
86   lengths=_ogg_calloc(entries,sizeof(long));
87   for(j=0;j<entries;j++)hits[j]=guard;
88
89   in=fopen(argv[2],"r");
90   if(!in){
91     fprintf(stderr,"Could not open input file %s\n",argv[2]);
92     exit(1);
93   }
94
95   if(!strrcmp_i(argv[0],"latticetune")){
96     long lines=0;
97     line=setup_line(in);
98     while(line){      
99       long code;
100       lines++;
101       if(!(lines&0xfff))spinnit("codewords so far...",lines);
102       
103       if(sscanf(line,"%ld",&code)==1)
104         hits[code]++;
105
106       line=setup_line(in);
107     }
108   }
109
110   if(!strrcmp_i(argv[0],"restune")){
111     long step;
112     long lines=0;
113     long cols=-1;
114     float *vec;
115     line=setup_line(in);
116     while(line){
117       int code;
118       if(!(lines&0xfff))spinnit("codewords so far...",lines);
119
120       if(cols==-1){
121         char *temp=line;
122         while(*temp==' ')temp++;
123         for(cols=0;*temp;cols++){
124           while(*temp>32)temp++;
125           while(*temp==' ')temp++;
126         }
127         vec=alloca(sizeof(float)*cols);
128         step=cols/dim;
129       }
130       
131       for(j=0;j<cols;j++)
132         if(get_line_value(in,vec+j)){
133           fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
134           exit(1);
135         }
136       
137       for(j=0;j<step;j++){
138         lines++;
139         code=_best(b,vec+j,step);
140         hits[code]++;
141       }
142
143       line=setup_line(in);
144     }
145   }
146
147   fclose(in);
148
149   /* build the codeword lengths */
150   build_tree_from_lengths0(entries,hits,lengths);
151
152   c->lengthlist=lengths;
153   write_codebook(stdout,name,c); 
154   
155   fprintf(stderr,"\r                                                     "
156           "\nDone.\n");
157   exit(0);
158 }