1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: residue backend 0 partitioner
15 last mod: $Id: residuesplit.c,v 1.1 2000/02/12 08:33:01 xiphmont Exp $
17 ********************************************************************/
23 #include "../lib/scales.h"
24 #include "../vq/bookutil.h"
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
31 /* modifies the pcm vector, returns book membership in aux */
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
37 static double _maxval(double *v,int n){
41 double val=fabs(v[i]);
47 /* mean dB actually */
48 static double _meanval(double *v,int n){
52 acc+=todB(fabs(v[i]));
53 return(fromdB(acc/n));
59 int quantaux(double *mask, double *floor,int n,
60 double *maskmbound,double *maskabound,double *floorbound,
65 double maxmask=_maxval(mask+i,subn);
66 double meanmask=_meanval(mask+i,subn);
67 double meanfloor=_meanval(floor+i,subn);
70 for(j=0;j<parts-1;j++)
71 if(maxmask<maskmbound[j] &&
72 meanmask<maskabound[j] &&
73 meanfloor<floorbound[j])
77 fprintf(of,"%d, ",aux);
79 for(j=0;j<subn;j++,i++)
80 fprintf(or[aux],"%g, ",floor[i]);
81 fprintf(or[aux],"\n");
89 static int getline(FILE *in,double *vec,int begin,int n){
93 if(get_next_value(in,vec))return(0);
96 get_line_value(in,vec);
103 if(get_line_value(in,vec+i)){
104 fprintf(stderr,"ran out of columns in input data\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");
124 int main(int argc, char *argv[]){
127 int i,parts,begin,n,subn;
130 double *maskmbound,*maskabound,*maskvec;
131 double *floorbound,*floorvec;
135 base=strdup(argv[4]);
136 buffer=alloca(strlen(base)+20);
138 char *pos=strchr(argv[3],',');
144 pos=strchr(pos+1,',');
149 if(n/subn*subn != n){
150 fprintf(stderr,"n must be divisible by group\n");
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);
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;
169 maskabound[i]=atof(pos+1);
170 if(pos[1]==',')maskabound[i]=1e50;
171 pos=strchr(pos+1,',');
175 floorbound[i]=atof(pos+1);
183 mask=fopen(argv[1],"r");
185 fprintf(stderr,"Could not open file %s\n",argv[1]);
188 floor=fopen(argv[2],"r");
190 fprintf(stderr,"Could not open file %s\n",argv[2]);
194 or=alloca(parts*sizeof(FILE*));
195 sprintf(buffer,"%saux.vqd",base);
196 of=fopen(buffer,"w");
198 fprintf(stderr,"Could not open file %s for writing\n",buffer);
201 for(i=0;i<parts;i++){
202 sprintf(buffer,"%s_%d.vqd",base,i);
203 or[i]=fopen(buffer,"w");
205 fprintf(stderr,"Could not open file %s for writing\n",buffer);
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);