123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /********************************************************************
- * *
- * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
- * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
- * PLEASE READ THESE TERMS DISTRIBUTING. *
- * *
- * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
- * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
- * http://www.xiph.org/ *
- * *
- ********************************************************************
- function: utility main for loading and operating on codebooks
- last mod: $Id: run.c,v 1.10.8.1 2000/08/31 09:00:03 xiphmont Exp $
- ********************************************************************/
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include "bookutil.h"
- /* command line:
- utilname [-i] +|* input_book.vqh [+|* input_book.vqh]
- input_data.vqd [input_data.vqd]
- produces output data on stdout
- (may also take input data from stdin)
- */
- extern void process_preprocess(codebook **b,char *basename);
- extern void process_postprocess(codebook **b,char *basename);
- extern void process_vector(codebook **b,int *addmul, int inter,float *a,int n);
- extern void process_usage(void);
- int main(int argc,char *argv[]){
- char *basename;
- codebook **b=calloc(1,sizeof(codebook *));
- int *addmul=calloc(1,sizeof(int));
- int books=0;
- int input=0;
- int interleave=0;
- int j;
- int start=0;
- int num=-1;
- argv++;
- if(*argv==NULL){
- process_usage();
- exit(1);
- }
- /* yes, this is evil. However, it's very convenient to parse file
- extentions */
- while(*argv){
- if(*argv[0]=='-'){
- /* option */
- if(argv[0][1]=='s'){
- /* subvector */
- if(sscanf(argv[1],"%d,%d",&start,&num)!=2){
- num= -1;
- if(sscanf(argv[1],"%d",&start)!=1){
- fprintf(stderr,"Syntax error using -s\n");
- exit(1);
- }
- }
- argv+=2;
- }
- if(argv[0][1]=='i'){
- /* interleave */
- interleave=1;
- argv+=1;
- }
- }else{
- /* input file. What kind? */
- char *dot;
- char *ext=NULL;
- char *name=strdup(*argv++);
- dot=strrchr(name,'.');
- if(dot)
- ext=dot+1;
- else
- ext="";
- /* codebook */
- if(!strcmp(ext,"vqh")){
- int multp=0;
- if(input){
- fprintf(stderr,"specify all input data (.vqd) files following\n"
- "codebook header (.vqh) files\n");
- exit(1);
- }
- /* is it additive or multiplicative? */
- if(name[0]=='*'){
- multp=1;
- name++;
- }
- if(name[0]=='+')name++;
- basename=strrchr(name,'/');
- if(basename)
- basename=strdup(basename)+1;
- else
- basename=strdup(name);
- dot=strrchr(basename,'.');
- if(dot)*dot='\0';
- b=realloc(b,sizeof(codebook *)*(books+2));
- b[books]=codebook_load(name);
- addmul=realloc(addmul,sizeof(int)*(books+1));
- addmul[books++]=multp;
- b[books]=NULL;
- }
- /* data file */
- if(!strcmp(ext,"vqd")){
- int cols;
- long lines=0;
- char *line;
- float *vec;
- FILE *in=fopen(name,"r");
- if(!in){
- fprintf(stderr,"Could not open input file %s\n",name);
- exit(1);
- }
- if(!input){
- process_preprocess(b,basename);
- input++;
- }
- reset_next_value();
- line=setup_line(in);
- /* count cols before we start reading */
- {
- char *temp=line;
- while(*temp==' ')temp++;
- for(cols=0;*temp;cols++){
- while(*temp>32)temp++;
- while(*temp==' ')temp++;
- }
- }
- vec=alloca(cols*sizeof(float));
- while(line){
- lines++;
- for(j=0;j<cols;j++)
- if(get_line_value(in,vec+j)){
- fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
- exit(1);
- }
- /* ignores -s for now */
- process_vector(b,addmul,interleave,vec,cols);
- line=setup_line(in);
- }
- fclose(in);
- }
- }
- }
- /* take any data from stdin */
- {
- struct stat st;
- if(fstat(STDIN_FILENO,&st)==-1){
- fprintf(stderr,"Could not stat STDIN\n");
- exit(1);
- }
- if((S_IFIFO|S_IFREG|S_IFSOCK)&st.st_mode){
- int cols;
- char *line;
- long lines=0;
- float *vec;
- if(!input){
- process_preprocess(b,basename);
- input++;
- }
-
- line=setup_line(stdin);
- /* count cols before we start reading */
- {
- char *temp=line;
- while(*temp==' ')temp++;
- for(cols=0;*temp;cols++){
- while(*temp>32)temp++;
- while(*temp==' ')temp++;
- }
- }
- vec=alloca(cols*sizeof(float));
- while(line){
- lines++;
- for(j=0;j<cols;j++)
- if(get_line_value(stdin,vec+j)){
- fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
- exit(1);
- }
- /* ignores -s for now */
- process_vector(b,addmul,interleave,vec,cols);
-
- line=setup_line(stdin);
- }
- }
- }
- process_postprocess(b,basename);
- return 0;
- }
|