residuesplit.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /********************************************************************
  2. * *
  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. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************
  11. function: residue backend 0 partitioner/classifier
  12. last mod: $Id: residuesplit.c,v 1.10.6.1 2001/07/08 08:48:10 xiphmont Exp $
  13. ********************************************************************/
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include "bookutil.h"
  19. /* does not guard against invalid settings; eg, a subn of 16 and a
  20. subgroup request of 32. Max subn of 128 */
  21. static float _testhack(float *vec,int n){
  22. int i,j=0;
  23. float max=0.f;
  24. float temp[128];
  25. float entropy=0.;
  26. /* setup */
  27. for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
  28. /* handle case subgrp==1 outside */
  29. for(i=0;i<n;i++)
  30. if(temp[i]>max)max=temp[i];
  31. for(i=0;i<n;i++)temp[i]=rint(temp[i]);
  32. for(i=0;i<n;i++)
  33. entropy+=temp[i];
  34. return entropy;
  35. /*while(1){
  36. entropy[j]=max;
  37. n>>=1;
  38. j++;
  39. if(n<=0)break;
  40. for(i=0;i<n;i++){
  41. temp[i]+=temp[i+n];
  42. }
  43. max=0.f;
  44. for(i=0;i<n;i++)
  45. if(temp[i]>max)max=temp[i];
  46. }*/
  47. }
  48. static FILE *of;
  49. static FILE **or;
  50. /* we evaluate the the entropy measure for each interleaved subgroup */
  51. /* This is currently a bit specific to/hardwired for mapping 0; things
  52. will need to change in the future when we get real multichannel
  53. mappings */
  54. int quantaux(float *res,int n,float *ebound,float *mbound,int *subgrp,int parts, int subn){
  55. long i,j,part=0;
  56. int aux;
  57. for(i=0;i<=n-subn;i+=subn,part++){
  58. float max=0.f;
  59. float lentropy=0.f;
  60. lentropy=_testhack(res+i,subn);
  61. for(j=0;j<subn;j++)
  62. if(fabs(res[i+j])>max)max=fabs(res[i+j]);
  63. for(j=0;j<parts-1;j++)
  64. if(lentropy<=ebound[j] &&
  65. max<=mbound[j] &&
  66. part<subgrp[j])
  67. break;
  68. aux=j;
  69. fprintf(of,"%d, ",aux);
  70. for(j=0;j<subn;j++)
  71. fprintf(or[aux],"%g, ",res[j+i]);
  72. fprintf(or[aux],"\n");
  73. }
  74. fprintf(of,"\n");
  75. return(0);
  76. }
  77. static int getline(FILE *in,float *vec,int begin,int n){
  78. int i,next=0;
  79. reset_next_value();
  80. if(get_next_value(in,vec))return(0);
  81. if(begin){
  82. for(i=1;i<begin;i++)
  83. get_line_value(in,vec);
  84. next=0;
  85. }else{
  86. next=1;
  87. }
  88. for(i=next;i<n;i++)
  89. if(get_line_value(in,vec+i)){
  90. fprintf(stderr,"ran out of columns in input data\n");
  91. exit(1);
  92. }
  93. return(1);
  94. }
  95. static void usage(){
  96. fprintf(stderr,
  97. "usage:\n"
  98. "residuesplit <res> <begin,n,group> <baseout> <ent,peak,sub> [<ent,peak,sub>]...\n"
  99. " where begin,n,group is first scalar, \n"
  100. " number of scalars of each in line,\n"
  101. " number of scalars in a group\n"
  102. " ent is the maximum entropy value allowed for membership in a group\n"
  103. " peak is the maximum amplitude value allowed for membership in a group\n"
  104. " subn is the maximum subpartiton number allowed in the group\n"
  105. "eg: residuesplit mask.vqd floor.vqd 0,1024,16 res 0,.5,2 3,1.5,4 \n"
  106. "produces resaux.vqd and res_0...n.vqd\n\n");
  107. exit(1);
  108. }
  109. int main(int argc, char *argv[]){
  110. char *buffer;
  111. char *base;
  112. int i,parts,begin,n,subn,*subgrp;
  113. FILE *res;
  114. float *ebound,*mbound,*vec;
  115. long c=0;
  116. if(argc<5)usage();
  117. base=strdup(argv[3]);
  118. buffer=alloca(strlen(base)+20);
  119. {
  120. char *pos=strchr(argv[2],',');
  121. begin=atoi(argv[2]);
  122. if(!pos)
  123. usage();
  124. else
  125. n=atoi(pos+1);
  126. pos=strchr(pos+1,',');
  127. if(!pos)
  128. usage();
  129. else
  130. subn=atoi(pos+1);
  131. if(n/subn*subn != n){
  132. fprintf(stderr,"n must be divisible by group\n");
  133. exit(1);
  134. }
  135. }
  136. /* how many parts?... */
  137. parts=argc-3;
  138. ebound=_ogg_malloc(sizeof(float)*parts);
  139. mbound=_ogg_malloc(sizeof(float)*parts);
  140. subgrp=_ogg_malloc(sizeof(int)*parts);
  141. for(i=0;i<parts-1;i++){
  142. char *pos=strchr(argv[4+i],',');
  143. subgrp[i]=0;
  144. if(*argv[4+i]==',')
  145. ebound[i]=1e50f;
  146. else
  147. ebound[i]=atof(argv[4+i]);
  148. if(!pos){
  149. mbound[i]=1e50f;
  150. }else{
  151. if(*(pos+1)==',')
  152. mbound[i]=1e50f;
  153. else
  154. mbound[i]=atof(pos+1);
  155. pos=strchr(pos+1,',');
  156. if(pos)
  157. subgrp[i]=atoi(pos+1);
  158. }
  159. if(subgrp[i]<=0)subgrp[i]=99999;
  160. }
  161. ebound[i]=1e50f;
  162. mbound[i]=1e50f;
  163. subgrp[i]=9999999;
  164. res=fopen(argv[1],"r");
  165. if(!res){
  166. fprintf(stderr,"Could not open file %s\n",argv[1]);
  167. exit(1);
  168. }
  169. or=alloca(parts*sizeof(FILE*));
  170. sprintf(buffer,"%saux.vqd",base);
  171. of=fopen(buffer,"w");
  172. if(!of){
  173. fprintf(stderr,"Could not open file %s for writing\n",buffer);
  174. exit(1);
  175. }
  176. for(i=0;i<parts;i++){
  177. sprintf(buffer,"%s_%d.vqd",base,i);
  178. or[i]=fopen(buffer,"w");
  179. if(!or[i]){
  180. fprintf(stderr,"Could not open file %s for writing\n",buffer);
  181. exit(1);
  182. }
  183. }
  184. vec=_ogg_malloc(sizeof(float)*n);
  185. /* get the input line by line and process it */
  186. while(!feof(res)){
  187. if(getline(res,vec,begin,n))
  188. quantaux(vec,n,ebound,mbound,subgrp,parts,subn);
  189. c++;
  190. if(!(c&0xf)){
  191. spinnit("kB so far...",(int)(ftell(res)/1024));
  192. }
  193. }
  194. fclose(res);
  195. fclose(of);
  196. for(i=0;i<parts;i++)
  197. fclose(or[i]);
  198. fprintf(stderr,"\rDone \n");
  199. return(0);
  200. }