latticebuild.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 building codebooks from lattice descriptions
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include "bookutil.h"
  21. /* The purpose of this util is just to finish packaging the
  22. description into a static codebook. It used to count hits for a
  23. histogram, but I've divorced that out to add some flexibility (it
  24. currently generates an equal probability codebook)
  25. command line:
  26. latticebuild description.vql
  27. the lattice description file contains two lines:
  28. <n> <dim> <multiplicitavep> <sequentialp>
  29. <value_0> <value_1> <value_2> ... <value_n-1>
  30. a threshmap (or pigeonmap) struct is generated by latticehint;
  31. there are fun tricks one can do with the threshmap and cascades,
  32. but the utils don't know them...
  33. entropy encoding is done by feeding an entry list collected from a
  34. training set and feeding it to latticetune along with the book.
  35. latticebuild produces a codebook on stdout */
  36. static int ilog(unsigned int v){
  37. int ret=0;
  38. while(v){
  39. ret++;
  40. v>>=1;
  41. }
  42. return(ret);
  43. }
  44. int main(int argc,char *argv[]){
  45. codebook b;
  46. static_codebook c;
  47. double *quantlist;
  48. long *hits;
  49. int entries=-1,dim=-1,quantvals=-1,addmul=-1,sequencep=0;
  50. FILE *in=NULL;
  51. char *line,*name;
  52. long i,j;
  53. memset(&b,0,sizeof(b));
  54. memset(&c,0,sizeof(c));
  55. if(argv[1]==NULL){
  56. fprintf(stderr,"Need a lattice description file on the command line.\n");
  57. exit(1);
  58. }
  59. {
  60. char *ptr;
  61. char *filename=_ogg_calloc(strlen(argv[1])+4,1);
  62. strcpy(filename,argv[1]);
  63. in=fopen(filename,"r");
  64. if(!in){
  65. fprintf(stderr,"Could not open input file %s\n",filename);
  66. exit(1);
  67. }
  68. ptr=strrchr(filename,'.');
  69. if(ptr){
  70. *ptr='\0';
  71. name=strdup(filename);
  72. }else{
  73. name=strdup(filename);
  74. }
  75. }
  76. /* read the description */
  77. line=get_line(in);
  78. if(sscanf(line,"%d %d %d %d",&quantvals,&dim,&addmul,&sequencep)!=4){
  79. if(sscanf(line,"%d %d %d",&quantvals,&dim,&addmul)!=3){
  80. fprintf(stderr,"Syntax error reading description file (line 1)\n");
  81. exit(1);
  82. }
  83. }
  84. entries=pow(quantvals,dim);
  85. c.dim=dim;
  86. c.entries=entries;
  87. c.lengthlist=_ogg_malloc(entries*sizeof(long));
  88. c.maptype=1;
  89. c.q_sequencep=sequencep;
  90. c.quantlist=_ogg_calloc(quantvals,sizeof(long));
  91. quantlist=_ogg_malloc(sizeof(double)*c.dim*c.entries);
  92. hits=_ogg_malloc(c.entries*sizeof(long));
  93. for(j=0;j<entries;j++)hits[j]=1;
  94. for(j=0;j<entries;j++)c.lengthlist[j]=1;
  95. reset_next_value();
  96. line=setup_line(in);
  97. for(j=0;j<quantvals;j++){
  98. char *temp;
  99. if(!line || sscanf(line,"%lf",quantlist+j)!=1){
  100. fprintf(stderr,"Ran out of data on line 2 of description file\n");
  101. exit(1);
  102. }
  103. temp=strchr(line,',');
  104. if(!temp)temp=strchr(line,' ');
  105. if(temp)temp++;
  106. line=temp;
  107. }
  108. /* gen a real quant list from the more easily human-grokked input */
  109. {
  110. double min=quantlist[0];
  111. double mindel=-1;
  112. int fac=1;
  113. for(j=1;j<quantvals;j++)if(quantlist[j]<min)min=quantlist[j];
  114. for(j=0;j<quantvals;j++)
  115. for(i=j+1;i<quantvals;i++)
  116. if(mindel==-1 || fabs(quantlist[j]-quantlist[i])<mindel)
  117. mindel=fabs(quantlist[j]-quantlist[i]);
  118. j=0;
  119. while(j<quantvals){
  120. for(j=0;j<quantvals;j++){
  121. double test=fac*(quantlist[j]-min)/mindel;
  122. if( fabs(rint(test)-test)>.00001f) break;
  123. }
  124. if(fac>100)break;
  125. if(j<quantvals)fac++;
  126. }
  127. mindel/=fac;
  128. fprintf(stderr,"min=%g mindel=%g\n",min,mindel);
  129. c.q_min=_float32_pack(min);
  130. c.q_delta=_float32_pack(mindel);
  131. c.q_quant=0;
  132. min=_float32_unpack(c.q_min);
  133. mindel=_float32_unpack(c.q_delta);
  134. for(j=0;j<quantvals;j++){
  135. c.quantlist[j]=rint((quantlist[j]-min)/mindel);
  136. if(ilog(c.quantlist[j])>c.q_quant)c.q_quant=ilog(c.quantlist[j]);
  137. }
  138. }
  139. /* build the [default] codeword lengths */
  140. memset(c.lengthlist,0,sizeof(long)*entries);
  141. for(i=0;i<entries;i++)hits[i]=1;
  142. build_tree_from_lengths(entries,hits,c.lengthlist);
  143. /* save the book in C header form */
  144. write_codebook(stdout,name,&c);
  145. fprintf(stderr,"\r "
  146. "\nDone.\n");
  147. exit(0);
  148. }