First merge of new psychoacoustics. Have some unused codebooks to
[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/classifier
15  last mod: $Id: residuesplit.c,v 1.2 2000/05/08 20:49:42 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22 #include <stdio.h>
23 #include "../vq/bookutil.h"
24
25 static FILE *of;
26 static FILE **or;
27
28 /* currently classify only by maximum amplitude */
29
30 /* This is currently a bit specific to/hardwired for mapping 0; things
31    will need to change in the future when we get real multichannel
32    mappings */
33
34 int quantaux(double *res,int n,double *bound,int parts, int subn){
35   long i,j,aux;
36   
37   for(i=0;i<=n-subn;i+=subn){
38     double max=0.;
39     for(j=0;j<subn;j++)
40       if(fabs(res[i+j])>max)max=fabs(res[i+j]);
41     
42     for(j=0;j<parts-1;j++)
43       if(max>=bound[j])
44         break;
45     aux=j;
46     
47     fprintf(of,"%ld, ",aux);
48     
49     for(j=0;j<subn;j++)
50       fprintf(or[aux],"%g, ",res[j+i]);
51     
52     fprintf(or[aux],"\n");
53   }
54
55   fprintf(of,"\n");
56
57   return(0);
58 }
59
60 static int getline(FILE *in,double *vec,int begin,int n){
61   int i,next=0;
62
63   reset_next_value();
64   if(get_next_value(in,vec))return(0);
65   if(begin){
66     for(i=1;i<begin;i++)
67       get_line_value(in,vec);
68     next=0;
69   }else{
70     next=1;
71   }
72
73   for(i=next;i<n;i++)
74     if(get_line_value(in,vec+i)){
75       fprintf(stderr,"ran out of columns in input data\n");
76       exit(1);
77     }
78   
79   return(1);
80 }
81
82 static void usage(){
83   fprintf(stderr,
84           "usage:\n" 
85           "residuesplit <res> <begin,n,group> <baseout> <min> [<min>]...\n"
86           "   where begin,n,group is first scalar, \n"
87           "                          number of scalars of each in line,\n"
88           "                          number of scalars in a group\n"
89           "         min is the minimum peak value required for membership in a group\n"
90           "eg: residuesplit mask.vqd floor.vqd 0,1024,16 res 25.5 13.5 7.5 1.5 0\n"
91           "produces resaux.vqd and res_0...n.vqd\n\n");
92   exit(1);
93 }
94
95 int main(int argc, char *argv[]){
96   char *buffer;
97   char *base;
98   int i,parts,begin,n,subn;
99   FILE *res;
100   double *bound,*vec;
101   long c=0;
102   if(argc<5)usage();
103
104   base=strdup(argv[3]);
105   buffer=alloca(strlen(base)+20);
106   {
107     char *pos=strchr(argv[2],',');
108     begin=atoi(argv[2]);
109     if(!pos)
110       usage();
111     else
112       n=atoi(pos+1);
113     pos=strchr(pos+1,',');
114     if(!pos)
115       usage();
116     else
117       subn=atoi(pos+1);
118     if(n/subn*subn != n){
119       fprintf(stderr,"n must be divisible by group\n");
120       exit(1);
121     }
122   }
123
124   /* how many parts?... */
125   parts=argc-3;
126   bound=malloc(sizeof(double)*parts);
127
128   for(i=0;i<parts-1;i++){
129     bound[i]=atof(argv[4+i]);
130   }
131   bound[i]=0;
132
133   res=fopen(argv[1],"r");
134   if(!res){
135     fprintf(stderr,"Could not open file %s\n",argv[1]);
136     exit(1);
137   }
138
139   or=alloca(parts*sizeof(FILE*));
140   sprintf(buffer,"%saux.vqd",base);
141   of=fopen(buffer,"w");
142   if(!of){
143     fprintf(stderr,"Could not open file %s for writing\n",buffer);
144     exit(1);
145   }
146   for(i=0;i<parts;i++){
147     sprintf(buffer,"%s_%d.vqd",base,i);
148     or[i]=fopen(buffer,"w");
149     if(!or[i]){
150       fprintf(stderr,"Could not open file %s for writing\n",buffer);
151       exit(1);
152     }
153   }
154   
155   vec=malloc(sizeof(double)*n);
156   /* get the input line by line and process it */
157   while(!feof(res)){
158     if(getline(res,vec,begin,n))
159       quantaux(vec,n,bound,parts,subn);
160     c++;
161     if(!(c&0xf)){
162       spinnit("kB so far...",(int)(ftell(res)/1024));
163     }
164   }
165   fclose(res);
166   fclose(of);
167   for(i=0;i<parts;i++)
168     fclose(or[i]);
169   fprintf(stderr,"\rDone                         \n");
170   return(0);
171 }
172
173
174
175