1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: residue backend 0 partitioner/classifier
16 ********************************************************************/
24 /* does not guard against invalid settings; eg, a subn of 16 and a
25 subgroup request of 32. Max subn of 128 */
26 static float _testhack(float *vec,int n){
33 for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
35 /* handle case subgrp==1 outside */
37 if(temp[i]>max)max=temp[i];
39 for(i=0;i<n;i++)temp[i]=rint(temp[i]);
56 if(temp[i]>max)max=temp[i];
63 /* we evaluate the the entropy measure for each interleaved subgroup */
64 /* This is currently a bit specific to/hardwired for mapping 0; things
65 will need to change in the future when we get real multichannel
67 int quantaux(float *res,int n,float *ebound,float *mbound,int *subgrp,int parts, int subn,
72 for(i=0;i<=n-subn;i+=subn,part++){
76 lentropy=_testhack(res+i,subn);
79 if(fabs(res[i+j])>max)max=fabs(res[i+j]);
81 for(j=0;j<parts-1;j++)
82 if(lentropy<=ebound[j] &&
88 fprintf(of,"%d, ",aux);
95 int quantwrite(float *res,int n,int subn, int *class,int offset){
99 for(i=0;i<=n-subn;i+=subn,part++){
103 fprintf(or[aux+offset],"%g, ",res[j+i]);
105 fprintf(or[aux+offset],"\n");
111 static int getline(FILE *in,float *vec,int begin,int n){
115 if(get_next_value(in,vec))return(0);
118 get_line_value(in,vec);
125 if(get_line_value(in,vec+i)){
126 fprintf(stderr,"ran out of columns in input data\n");
136 "residuesplit <res> [<res>] <begin,n,group> <baseout> <ent,peak,sub> [<ent,peak,sub>]...\n"
137 " where begin,n,group is first scalar, \n"
138 " number of scalars of each in line,\n"
139 " number of scalars in a group\n"
140 " ent is the maximum entropy value allowed for membership in a group\n"
141 " peak is the maximum amplitude value allowed for membership in a group\n"
142 " subn is the maximum subpartiton number allowed in the group\n\n");
146 int main(int argc, char *argv[]){
149 int i,j,parts,begin,n,subn,*subgrp,*class;
152 float *ebound,*mbound,*vec;
156 /* count the res file names, open the files */
157 while(!strcmp(argv[resfiles+1]+strlen(argv[resfiles+1])-4,".vqd"))
159 if(resfiles<1)usage();
161 res=alloca(sizeof(*res)*resfiles);
162 for(i=0;i<resfiles;i++){
163 res[i]=fopen(argv[i+1],"r");
165 fprintf(stderr,"Could not open file %s\n",argv[1+i]);
170 base=strdup(argv[2+resfiles]);
171 buffer=alloca(strlen(base)+20);
173 char *pos=strchr(argv[1+resfiles],',');
174 begin=atoi(argv[1+resfiles]);
179 pos=strchr(pos+1,',');
184 if(n/subn*subn != n){
185 fprintf(stderr,"n must be divisible by group\n");
190 /* how many parts?... */
191 parts=argc-resfiles-2;
193 ebound=_ogg_malloc(sizeof(float)*parts);
194 mbound=_ogg_malloc(sizeof(float)*parts);
195 subgrp=_ogg_malloc(sizeof(int)*parts);
197 for(i=0;i<parts-1;i++){
198 char *pos=strchr(argv[3+i+resfiles],',');
200 if(*argv[3+i+resfiles]==',')
203 ebound[i]=atof(argv[3+i+resfiles]);
211 mbound[i]=atof(pos+1);
212 pos=strchr(pos+1,',');
215 subgrp[i]=atoi(pos+1);
218 if(subgrp[i]<=0)subgrp[i]=99999;
225 or=alloca(parts*resfiles*sizeof(FILE*));
226 sprintf(buffer,"%saux.vqd",base);
227 of=fopen(buffer,"w");
229 fprintf(stderr,"Could not open file %s for writing\n",buffer);
233 for(j=0;j<resfiles;j++){
234 for(i=0;i<parts;i++){
235 sprintf(buffer,"%s_%d%c.vqd",base,i,j+65);
236 or[i+j*parts]=fopen(buffer,"w");
238 fprintf(stderr,"Could not open file %s for writing\n",buffer);
244 vec=_ogg_malloc(sizeof(float)*n);
245 class=_ogg_malloc(sizeof(float)*n);
246 /* get the input line by line and process it */
248 if(getline(res[0],vec,begin,n)){
249 quantaux(vec,n,ebound,mbound,subgrp,parts,subn,class);
250 quantwrite(vec,n,subn,class,0);
252 for(i=1;i<resfiles;i++){
253 if(getline(res[i],vec,begin,n)){
254 quantwrite(vec,n,subn,class,parts*i);
256 fprintf(stderr,"Getline loss of sync (%d).\n\n",i);
261 if(feof(res[0]))break;
262 fprintf(stderr,"Getline loss of sync (0).\n\n");
268 spinnit("kB so far...",(int)(ftell(res[0])/1024));
271 for(i=0;i<resfiles;i++)
274 for(i=0;i<parts*resfiles;i++)
276 fprintf(stderr,"\rDone \n");