00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include <string.h>
00023 #include <errno.h>
00024 #include "bookutil.h"
00025
00026 static int strrcmp_i(char *s,char *cmp){
00027 return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
00028 }
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 int main(int argc,char *argv[]){
00040 codebook *b;
00041 static_codebook *c;
00042 long *lengths;
00043 long *hits;
00044
00045 int entries=-1,dim=-1,guard=1;
00046 FILE *in=NULL;
00047 char *line,*name;
00048 long j;
00049
00050 if(argv[1]==NULL){
00051 fprintf(stderr,"Need a lattice codebook on the command line.\n");
00052 exit(1);
00053 }
00054 if(argv[2]==NULL){
00055 fprintf(stderr,"Need a codeword data file on the command line.\n");
00056 exit(1);
00057 }
00058 if(argv[3]!=NULL)guard=0;
00059
00060 {
00061 char *ptr;
00062 char *filename=strdup(argv[1]);
00063
00064 b=codebook_load(filename);
00065 c=(static_codebook *)(b->c);
00066
00067 ptr=strrchr(filename,'.');
00068 if(ptr){
00069 *ptr='\0';
00070 name=strdup(filename);
00071 }else{
00072 name=strdup(filename);
00073 }
00074 }
00075
00076 if(c->maptype!=1){
00077 fprintf(stderr,"Provided book is not a latticebook.\n");
00078 exit(1);
00079 }
00080
00081 entries=b->entries;
00082 dim=b->dim;
00083
00084 hits=_ogg_malloc(entries*sizeof(long));
00085 lengths=_ogg_calloc(entries,sizeof(long));
00086 for(j=0;j<entries;j++)hits[j]=guard;
00087
00088 in=fopen(argv[2],"r");
00089 if(!in){
00090 fprintf(stderr,"Could not open input file %s\n",argv[2]);
00091 exit(1);
00092 }
00093
00094 if(!strrcmp_i(argv[0],"latticetune")){
00095 long lines=0;
00096 line=setup_line(in);
00097 while(line){
00098 long code;
00099 lines++;
00100 if(!(lines&0xfff))spinnit("codewords so far...",lines);
00101
00102 if(sscanf(line,"%ld",&code)==1)
00103 hits[code]++;
00104
00105 line=setup_line(in);
00106 }
00107 }
00108
00109
00110 if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
00111
00112 line=setup_line(in);
00113 while(line){
00114
00115
00116
00117
00118
00119 char *pos=strchr(line,':');
00120 if(pos){
00121 long code=atol(line);
00122 long val=atol(pos+1);
00123 hits[code]+=val;
00124 }
00125
00126 line=setup_line(in);
00127 }
00128 }
00129
00130 fclose(in);
00131
00132
00133 build_tree_from_lengths0(entries,hits,lengths);
00134
00135 c->lengthlist=lengths;
00136 write_codebook(stdout,name,c);
00137
00138 {
00139 long bins=_book_maptype1_quantvals(c);
00140 long i,k,base=c->lengthlist[0];
00141 for(i=0;i<entries;i++)
00142 if(c->lengthlist[i]>base)base=c->lengthlist[i];
00143
00144 for(j=0;j<entries;j++){
00145 if(c->lengthlist[j]){
00146 int indexdiv=1;
00147 fprintf(stderr,"%4ld: ",j);
00148 for(k=0;k<c->dim;k++){
00149 int index= (j/indexdiv)%bins;
00150 fprintf(stderr,"%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
00151 _float32_unpack(c->q_min));
00152 indexdiv*=bins;
00153 }
00154 fprintf(stderr,"\t|");
00155 for(k=0;k<base-c->lengthlist[j];k++)fprintf(stderr,"*");
00156 fprintf(stderr,"\n");
00157 }
00158 }
00159 }
00160
00161 fprintf(stderr,"\r "
00162 "\nDone.\n");
00163 exit(0);
00164 }