latticebuild.c 5.0 KB

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