residuesplit.c 4.3 KB

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