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