Additional optimizations, rearrangement.
[platform/upstream/libvorbis.git] / vq / make_residue_books.pl
1 #!/usr/bin/perl
2
3 # quick, very dirty little script so that we can put all the
4 # information for building a residue book set (except the original
5 # partitioning) in one spec file.
6
7 #eg:
8
9 # >res0_128_128 interleaved
10 # haux res0_96_128aux.vqd 0,4,2
11 # :1 res0_128_128_1.vqd, 4, nonseq cull, 0 +- 1
12 # +1a, 4, nonseq, 0 +- .25 .5
13 # :2 res0_128_128_2.vqd, 4, nonseq, 0 +- 1 2
14 # :3 res0_128_128_3.vqd, 4, nonseq, 0 +- 1 3 5
15 # :4 res0_128_128_4.vqd, 2, nonseq, 0 +- 1 3 5 8 11
16 # :5 res0_128_128_5.vqd, 1, nonseq, 0 +- 1 3 5 8 11 14 17 20 24 28 31 35 39 
17 # +1a, 4, nonseq, 0 +- .5 1
18
19
20 die "Could not open $ARGV[0]: $!" unless open (F,$ARGV[0]);
21
22 $goflag=0;
23 while($line=<F>){
24
25     print "\n#### $line\n\n";
26     if($line=~m/^GO/){
27         $goflag=1;
28         next;
29     }
30
31     if($goflag==0){
32         if($line=~m/\S+/ && !($line=~m/^\#/) ){
33             my $command=$line;
34             print ">>> $command\n";
35             die "Couldn't shell command.\n\tcommand:$command\n" 
36                 if syst($command);
37         }
38         next;
39     }
40
41     # >res0_128_128
42     if($line=~m/^>(\S+)\s+(\S*)/){
43         # set the output name
44         $globalname=$1;
45         $interleave=$2;
46         next;
47     }
48
49     # haux res0_96_128aux.vqd 0,4,2
50     if($line=~m/^h(.*)/){
51         # build a huffman book (no mapping) 
52         my($name,$datafile,$arg)=split(' ',$1);
53         my $command="huffbuild $datafile $arg > $globalname$name.vqh";
54         print ">>> $command\n";
55         die "Couldn't build huffbook.\n\tcommand:$command\n" 
56             if syst($command);
57         next;
58     }
59
60     # :1 res0_128_128_1.vqd, 4, nonseq, 0 +- 1
61     if($line=~m/^:(.*)/){
62         my($namedata,$dim,$seqp,$vals)=split(',',$1);
63         my($name,$datafile)=split(' ',$namedata);
64         # build value list
65         my$plusminus="+";
66         my$list;
67         my$count=0;
68         foreach my$val (split(' ',$vals)){
69             if($val=~/\-?\+?\d+/){
70                 if($plusminus=~/-/){
71                     $list.="-$val ";
72                     $count++;
73                 }
74                 if($plusminus=~/\+/){
75                     $list.="$val ";
76                     $count++;
77                 }
78             }else{
79                 $plusminus=$val;
80             }
81         }
82         die "Couldn't open temp file temp$$.vql: $!" unless
83             open(G,">temp$$.vql");
84         print G "$count $dim 0 ";
85         if($seqp=~/non/){
86             print G "0\n$list\n";
87         }else{  
88             print G "1\n$list\n";
89         }
90         close(G);
91
92         my $command="latticebuild temp$$.vql > $globalname$name.vqh";
93         print ">>> $command\n";
94         die "Couldn't build latticebook.\n\tcommand:$command\n" 
95             if syst($command);
96
97         my $command="latticehint $globalname$name.vqh > temp$$.vqh";
98         print ">>> $command\n";
99         die "Couldn't pre-hint latticebook.\n\tcommand:$command\n" 
100             if syst($command);
101         
102         if($interleave=~/non/){
103             $restune="res1tune";
104         }else{
105             $restune="res0tune";
106         }
107
108         if($seqp=~/cull/){
109             my $command="$restune temp$$.vqh $datafile 1 > $globalname$name.vqh";
110             print ">>> $command\n";
111             die "Couldn't tune latticebook.\n\tcommand:$command\n" 
112                 if syst($command);
113         }else{
114             my $command="$restune temp$$.vqh $datafile > $globalname$name.vqh";
115             print ">>> $command\n";
116             die "Couldn't tune latticebook.\n\tcommand:$command\n" 
117                 if syst($command);
118         }
119
120         my $command="latticehint $globalname$name.vqh > temp$$.vqh";
121         print ">>> $command\n";
122         die "Couldn't post-hint latticebook.\n\tcommand:$command\n" 
123             if syst($command);
124
125         my $command="mv temp$$.vqh $globalname$name.vqh";
126         print ">>> $command\n";
127         die "Couldn't rename latticebook.\n\tcommand:$command\n" 
128             if syst($command);
129
130         # run the training data through book to cascade
131         if($interleave=~/non/){
132             $vqcascade="vqcascade";
133         }else{
134             $vqcascade="vqcascade -i";
135         }
136
137         my $command="$vqcascade +$globalname$name.vqh $datafile > temp$$.vqd";
138         print ">>> $command\n";
139         die "Couldn't cascade latticebook.\n\tcommand:$command\n" 
140             if syst($command);
141
142
143         my $command="rm temp$$.vql";
144         print ">>> $command\n";
145         die "Couldn't remove temp files.\n\tcommand:$command\n" 
146             if syst($command);
147
148         next;
149     }
150     # +a 4, nonseq, 0 +- 1
151     if($line=~m/^\+(.*)/){
152         my($name,$dim,$seqp,$vals)=split(',',$1);
153
154         # build value list
155         my$plusminus="+";
156         my$list;
157         my$count=0;
158         foreach my$val (split(' ',$vals)){
159             if($val=~/\-?\+?\d+/){
160                 if($plusminus=~/-/){
161                     $list.="-$val ";
162                     $count++;
163                 }
164                 if($plusminus=~/\+/){
165                     $list.="$val ";
166                     $count++;
167                 }
168             }else{
169                 $plusminus=$val;
170             }
171         }
172         die "Couldn't open temp file temp$$.vql: $!" unless
173             open(G,">temp$$.vql");
174         print G "$count $dim 0 ";
175         if($seqp=~/non/){
176             print G "0\n$list\n";
177         }else{  
178             print G "1\n$list\n";
179         }
180         close(G);
181
182         my $command="latticebuild temp$$.vql > $globalname$name.vqh";
183         print ">>> $command\n";
184         die "Couldn't build latticebook.\n\tcommand:$command\n" 
185             if syst($command);
186
187         my $command="latticehint $globalname$name.vqh > temp$$.vqh";
188         print ">>> $command\n";
189         die "Couldn't pre-hint latticebook.\n\tcommand:$command\n" 
190             if syst($command);
191         
192         if($interleave=~/non/){
193             $restune="res1tune";
194         }else{
195             $restune="res0tune";
196         }
197
198         if($seqp=~/cull/){
199             my $command="$restune temp$$.vqh temp$$.vqd 1 > $globalname$name.vqh";
200             print ">>> $command\n";
201             die "Couldn't tune latticebook.\n\tcommand:$command\n" 
202                 if syst($command);
203         }else{
204             my $command="$restune temp$$.vqh temp$$.vqd > $globalname$name.vqh";
205             print ">>> $command\n";
206             die "Couldn't tune latticebook.\n\tcommand:$command\n" 
207                 if syst($command);
208         }
209
210         my $command="latticehint $globalname$name.vqh > temp$$.vqh";
211         print ">>> $command\n";
212         die "Couldn't post-hint latticebook.\n\tcommand:$command\n" 
213             if syst($command);
214
215         my $command="mv temp$$.vqh $globalname$name.vqh";
216         print ">>> $command\n";
217         die "Couldn't rename latticebook.\n\tcommand:$command\n" 
218             if syst($command);
219
220         # run the training data through book to cascade
221         if($interleave=~/non/){
222             $vqcascade="vqcascade";
223         }else{
224             $vqcascade="vqcascade -i";
225         }
226
227         my $command="$vqcascade +$globalname$name.vqh temp$$.vqd > tempa$$.vqd";
228         print ">>> $command\n";
229         die "Couldn't cascade latticebook.\n\tcommand:$command\n" 
230             if syst($command);
231
232
233         my $command="rm temp$$.vql";
234         print ">>> $command\n";
235         die "Couldn't remove temp files.\n\tcommand:$command\n" 
236             if syst($command);
237
238         my $command="mv tempa$$.vqd temp$$.vqd";
239         print ">>> $command\n";
240         die "Couldn't rename temp file.\n\tcommand:$command\n" 
241             if syst($command);
242
243         next;
244     }
245 }
246
247 $command="rm temp$$.vqd";
248 print ">>> $command\n";
249 die "Couldn't remove temp files.\n\tcommand:$command\n" 
250     if syst($command);
251
252 sub syst{
253     system(@_)/256;
254 }