examples/sfexamples/oggvorbiscodec/src/libvorbis/vq/latticetune.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
00004  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00005  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00006  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00007  *                                                                  *
00008  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
00009  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
00010  *                                                                  *
00011  ********************************************************************
00012 
00013  function: utility main for setting entropy encoding parameters
00014            for lattice codebooks
00015  last mod: $Id: latticetune.c 7187 2004-07-20 07:24:27Z xiphmont $
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 /* This util takes a training-collected file listing codewords used in
00031    LSP fitting, then generates new codeword lengths for maximally
00032    efficient integer-bits entropy encoding.
00033 
00034    command line:
00035    latticetune book.vqh input.vqd [unused_entriesp]
00036 
00037    latticetune produces book.vqh on stdout */
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   /* now we simply count already collated by-entry data */
00110   if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
00111 
00112     line=setup_line(in);
00113     while(line){
00114 
00115       /* code:hits\n */
00116       /* likely to have multiple listing for each code entry; must
00117          accumulate */
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   /* build the codeword lengths */
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 }

Generated by  doxygen 1.6.2