residuesplit.c 5.8 KB

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