run.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: utility main for loading and operating on codebooks
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <fcntl.h>
  24. #include "bookutil.h"
  25. /* command line:
  26. utilname [-i] +|* input_book.vqh [+|* input_book.vqh]
  27. input_data.vqd [input_data.vqd]
  28. produces output data on stdout
  29. (may also take input data from stdin)
  30. */
  31. extern void process_preprocess(codebook **b,char *basename);
  32. extern void process_postprocess(codebook **b,char *basename);
  33. extern void process_vector(codebook **b,int *addmul, int inter,float *a,int n);
  34. extern void process_usage(void);
  35. int main(int argc,char *argv[]){
  36. char *basename;
  37. codebook **b=_ogg_calloc(1,sizeof(codebook *));
  38. int *addmul=_ogg_calloc(1,sizeof(int));
  39. int books=0;
  40. int input=0;
  41. int interleave=0;
  42. int j;
  43. int start=0;
  44. int num=-1;
  45. argv++;
  46. if(*argv==NULL){
  47. process_usage();
  48. exit(1);
  49. }
  50. /* yes, this is evil. However, it's very convenient to parse file
  51. extentions */
  52. while(*argv){
  53. if(*argv[0]=='-'){
  54. /* option */
  55. if(argv[0][1]=='s'){
  56. /* subvector */
  57. if(sscanf(argv[1],"%d,%d",&start,&num)!=2){
  58. num= -1;
  59. if(sscanf(argv[1],"%d",&start)!=1){
  60. fprintf(stderr,"Syntax error using -s\n");
  61. exit(1);
  62. }
  63. }
  64. argv+=2;
  65. }
  66. if(argv[0][1]=='i'){
  67. /* interleave */
  68. interleave=1;
  69. argv+=1;
  70. }
  71. }else{
  72. /* input file. What kind? */
  73. char *dot;
  74. char *ext=NULL;
  75. char *name=strdup(*argv++);
  76. dot=strrchr(name,'.');
  77. if(dot)
  78. ext=dot+1;
  79. else
  80. ext="";
  81. /* codebook */
  82. if(!strcmp(ext,"vqh")){
  83. int multp=0;
  84. if(input){
  85. fprintf(stderr,"specify all input data (.vqd) files following\n"
  86. "codebook header (.vqh) files\n");
  87. exit(1);
  88. }
  89. /* is it additive or multiplicative? */
  90. if(name[0]=='*'){
  91. multp=1;
  92. name++;
  93. }
  94. if(name[0]=='+')name++;
  95. basename=strrchr(name,'/');
  96. if(basename)
  97. basename=strdup(basename)+1;
  98. else
  99. basename=strdup(name);
  100. dot=strrchr(basename,'.');
  101. if(dot)*dot='\0';
  102. b=_ogg_realloc(b,sizeof(codebook *)*(books+2));
  103. b[books]=codebook_load(name);
  104. addmul=_ogg_realloc(addmul,sizeof(int)*(books+1));
  105. addmul[books++]=multp;
  106. b[books]=NULL;
  107. }
  108. /* data file */
  109. if(!strcmp(ext,"vqd")){
  110. int cols;
  111. long lines=0;
  112. char *line;
  113. float *vec;
  114. FILE *in=fopen(name,"r");
  115. if(!in){
  116. fprintf(stderr,"Could not open input file %s\n",name);
  117. exit(1);
  118. }
  119. if(!input){
  120. process_preprocess(b,basename);
  121. input++;
  122. }
  123. reset_next_value();
  124. line=setup_line(in);
  125. /* count cols before we start reading */
  126. {
  127. char *temp=line;
  128. while(*temp==' ')temp++;
  129. for(cols=0;*temp;cols++){
  130. while(*temp>32)temp++;
  131. while(*temp==' ')temp++;
  132. }
  133. }
  134. vec=alloca(cols*sizeof(float));
  135. while(line){
  136. lines++;
  137. for(j=0;j<cols;j++)
  138. if(get_line_value(in,vec+j)){
  139. fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
  140. exit(1);
  141. }
  142. /* ignores -s for now */
  143. process_vector(b,addmul,interleave,vec,cols);
  144. line=setup_line(in);
  145. }
  146. fclose(in);
  147. }
  148. }
  149. }
  150. /* take any data from stdin */
  151. {
  152. struct stat st;
  153. if(fstat(STDIN_FILENO,&st)==-1){
  154. fprintf(stderr,"Could not stat STDIN\n");
  155. exit(1);
  156. }
  157. if((S_IFIFO|S_IFREG|S_IFSOCK)&st.st_mode){
  158. int cols;
  159. char *line;
  160. long lines=0;
  161. float *vec;
  162. if(!input){
  163. process_preprocess(b,basename);
  164. input++;
  165. }
  166. line=setup_line(stdin);
  167. /* count cols before we start reading */
  168. {
  169. char *temp=line;
  170. while(*temp==' ')temp++;
  171. for(cols=0;*temp;cols++){
  172. while(*temp>32)temp++;
  173. while(*temp==' ')temp++;
  174. }
  175. }
  176. vec=alloca(cols*sizeof(float));
  177. while(line){
  178. lines++;
  179. for(j=0;j<cols;j++)
  180. if(get_line_value(stdin,vec+j)){
  181. fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
  182. exit(1);
  183. }
  184. /* ignores -s for now */
  185. process_vector(b,addmul,interleave,vec,cols);
  186. line=setup_line(stdin);
  187. }
  188. }
  189. }
  190. process_postprocess(b,basename);
  191. return 0;
  192. }