Collect training data from the lib when compiled with -DTRAIN
[platform/upstream/libvorbis.git] / vq / residuesplit.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: residue backend 0 partitioner
15  last mod: $Id: residuesplit.c,v 1.1 2000/02/12 08:33:01 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22 #include <stdio.h>
23 #include "../lib/scales.h"
24 #include "../vq/bookutil.h"
25
26 /* take a masking curve and raw residue; eliminate the inaduble and
27    quantize to the final form handed to the VQ.  All and any tricks to
28    squeeze out bits given knowledge of the encoding mode should go
29    here too */
30
31 /* modifies the pcm vector, returns book membership in aux */
32
33 /* This is currently a bit specific to/hardwired for mapping 0; things
34    will need to change in the future when we het real multichannel
35    mappings */
36
37 static double _maxval(double *v,int n){
38   int i;
39   double acc=0.;
40   for(i=0;i<n;i++){
41     double val=fabs(v[i]);
42     if(acc<val)acc=val;
43   }
44   return(acc);
45 }
46
47 /* mean dB actually */
48 static double _meanval(double *v,int n){
49   int i;
50   double acc=0.;
51   for(i=0;i<n;i++)
52     acc+=todB(fabs(v[i]));
53   return(fromdB(acc/n));
54 }
55
56 static FILE *of;
57 static FILE **or;
58
59 int quantaux(double *mask, double *floor,int n,
60              double *maskmbound,double *maskabound,double *floorbound,
61              int parts, int subn){
62   long i,j;
63
64   for(i=0;i<=n-subn;){
65     double maxmask=_maxval(mask+i,subn);
66     double meanmask=_meanval(mask+i,subn);
67     double meanfloor=_meanval(floor+i,subn);
68     int aux;
69
70     for(j=0;j<parts-1;j++)
71       if(maxmask<maskmbound[j] && 
72          meanmask<maskabound[j] &&
73          meanfloor<floorbound[j])
74         break;
75     aux=j;
76
77     fprintf(of,"%d, ",aux);      
78
79     for(j=0;j<subn;j++,i++)
80       fprintf(or[aux],"%g, ",floor[i]);
81     fprintf(or[aux],"\n");
82   }
83
84   fprintf(of,"\n");
85
86   return(0);
87 }
88
89 static int getline(FILE *in,double *vec,int begin,int n){
90   int i,next=0;
91
92   reset_next_value();
93   if(get_next_value(in,vec))return(0);
94   if(begin){
95     for(i=1;i<begin;i++)
96       get_line_value(in,vec);
97     next=0;
98   }else{
99     next=1;
100   }
101
102   for(i=next;i<n;i++)
103     if(get_line_value(in,vec+i)){
104       fprintf(stderr,"ran out of columns in input data\n");
105       exit(1);
106     }
107   
108   return(1);
109 }
110
111 static void usage(){
112   fprintf(stderr,
113           "usage:\n" 
114           "residuesplit <mask> <floor> <begin,n,group> <baseout> <m,a,f> [<m,a,f>]...\n"
115           "   where begin,n,group is first scalar, \n"
116           "                          number of scalars of each in line,\n"
117           "                          number of scalars in a group\n"
118           "         m,a,f are the boundary conditions for each group\n"
119           "eg: residuesplit mask.vqd floor.vqd 0,1024,32 res .5 2.5,1.5 ,,.25\n"
120           "produces resaux.vqd and res_0...n.vqd\n\n");
121   exit(1);
122 }
123
124 int main(int argc, char *argv[]){
125   char *buffer;
126   char *base;
127   int i,parts,begin,n,subn;
128   FILE *mask;
129   FILE *floor;
130   double *maskmbound,*maskabound,*maskvec;
131   double *floorbound,*floorvec;
132
133   if(argc<6)usage();
134
135   base=strdup(argv[4]);
136   buffer=alloca(strlen(base)+20);
137   {
138     char *pos=strchr(argv[3],',');
139     begin=atoi(argv[3]);
140     if(!pos)
141       usage();
142     else
143       n=atoi(pos+1);
144     pos=strchr(pos+1,',');
145     if(!pos)
146       usage();
147     else
148       subn=atoi(pos+1);
149     if(n/subn*subn != n){
150       fprintf(stderr,"n must be divisible by group\n");
151       exit(1);
152     }
153   }
154
155   /* how many parts?  Need to scan m,f... */
156   parts=argc-4; /* yes, one past */
157   maskmbound=malloc(sizeof(double)*parts);
158   maskabound=malloc(sizeof(double)*parts);
159   floorbound=malloc(sizeof(double)*parts);
160
161   for(i=0;i<parts-1;i++){
162     char *pos=strchr(argv[5+i],',');
163     maskmbound[i]=atof(argv[5+i]);
164     if(*argv[5+i]==',')maskmbound[i]=1e50;
165     if(!pos){
166       maskabound[i]=1e50;
167       floorbound[i]=1e50;
168     }else{
169       maskabound[i]=atof(pos+1);
170       if(pos[1]==',')maskabound[i]=1e50;
171       pos=strchr(pos+1,',');
172       if(!pos)
173         floorbound[i]=1e50;
174       else{
175         floorbound[i]=atof(pos+1);
176       }
177     }
178   }
179   maskmbound[i]=1e50;
180   maskabound[i]=1e50;
181   floorbound[i]=1e50;
182
183   mask=fopen(argv[1],"r");
184   if(!mask){
185     fprintf(stderr,"Could not open file %s\n",argv[1]);
186     exit(1);
187   }
188   floor=fopen(argv[2],"r");
189   if(!mask){
190     fprintf(stderr,"Could not open file %s\n",argv[2]);
191     exit(1);
192   }
193
194   or=alloca(parts*sizeof(FILE*));
195   sprintf(buffer,"%saux.vqd",base);
196   of=fopen(buffer,"w");
197   if(!of){
198     fprintf(stderr,"Could not open file %s for writing\n",buffer);
199     exit(1);
200   }
201   for(i=0;i<parts;i++){
202     sprintf(buffer,"%s_%d.vqd",base,i);
203     or[i]=fopen(buffer,"w");
204     if(!or[i]){
205       fprintf(stderr,"Could not open file %s for writing\n",buffer);
206       exit(1);
207     }
208   }
209   
210   maskvec=malloc(sizeof(double)*n);
211   floorvec=malloc(sizeof(double)*n);
212   /* get the input line by line and process it */
213   while(!feof(mask) && !feof(floor)){
214     if(getline(mask,maskvec,begin,n) &&
215        getline(floor,floorvec,begin,n)) 
216       quantaux(maskvec,floorvec,n,
217                maskmbound,maskabound,floorbound,parts,subn);
218     
219   }
220   fclose(mask);
221   fclose(floor);
222   fclose(of);
223   for(i=0;i<parts;i++)
224     fclose(or[i]);
225   return(0);
226 }
227
228
229
230