latticetune.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 setting entropy encoding parameters
  13. for lattice codebooks
  14. last mod: $Id$
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include "bookutil.h"
  22. static int strrcmp_i(char *s,char *cmp){
  23. return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
  24. }
  25. /* This util takes a training-collected file listing codewords used in
  26. LSP fitting, then generates new codeword lengths for maximally
  27. efficient integer-bits entropy encoding.
  28. command line:
  29. latticetune book.vqh input.vqd [unused_entriesp]
  30. latticetune produces book.vqh on stdout */
  31. int main(int argc,char *argv[]){
  32. codebook *b;
  33. static_codebook *c;
  34. long *lengths;
  35. long *hits;
  36. int entries=-1,dim=-1,guard=1;
  37. FILE *in=NULL;
  38. char *line,*name;
  39. long j;
  40. if(argv[1]==NULL){
  41. fprintf(stderr,"Need a lattice codebook on the command line.\n");
  42. exit(1);
  43. }
  44. if(argv[2]==NULL){
  45. fprintf(stderr,"Need a codeword data file on the command line.\n");
  46. exit(1);
  47. }
  48. if(argv[3]!=NULL)guard=0;
  49. {
  50. char *ptr;
  51. char *filename=strdup(argv[1]);
  52. b=codebook_load(filename);
  53. c=(static_codebook *)(b->c);
  54. ptr=strrchr(filename,'.');
  55. if(ptr){
  56. *ptr='\0';
  57. name=strdup(filename);
  58. }else{
  59. name=strdup(filename);
  60. }
  61. }
  62. if(c->maptype!=1){
  63. fprintf(stderr,"Provided book is not a latticebook.\n");
  64. exit(1);
  65. }
  66. entries=b->entries;
  67. dim=b->dim;
  68. hits=_ogg_malloc(entries*sizeof(long));
  69. lengths=_ogg_calloc(entries,sizeof(long));
  70. for(j=0;j<entries;j++)hits[j]=guard;
  71. in=fopen(argv[2],"r");
  72. if(!in){
  73. fprintf(stderr,"Could not open input file %s\n",argv[2]);
  74. exit(1);
  75. }
  76. if(!strrcmp_i(argv[0],"latticetune")){
  77. long lines=0;
  78. line=setup_line(in);
  79. while(line){
  80. long code;
  81. lines++;
  82. if(!(lines&0xfff))spinnit("codewords so far...",lines);
  83. if(sscanf(line,"%ld",&code)==1)
  84. hits[code]++;
  85. line=setup_line(in);
  86. }
  87. }
  88. /* now we simply count already collated by-entry data */
  89. if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
  90. line=setup_line(in);
  91. while(line){
  92. /* code:hits\n */
  93. /* likely to have multiple listing for each code entry; must
  94. accumulate */
  95. char *pos=strchr(line,':');
  96. if(pos){
  97. long code=atol(line);
  98. long val=atol(pos+1);
  99. hits[code]+=val;
  100. }
  101. line=setup_line(in);
  102. }
  103. }
  104. fclose(in);
  105. /* build the codeword lengths */
  106. build_tree_from_lengths0(entries,hits,lengths);
  107. c->lengthlist=lengths;
  108. write_codebook(stdout,name,c);
  109. {
  110. long bins=_book_maptype1_quantvals(c);
  111. long i,k,base=c->lengthlist[0];
  112. for(i=0;i<entries;i++)
  113. if(c->lengthlist[i]>base)base=c->lengthlist[i];
  114. for(j=0;j<entries;j++){
  115. if(c->lengthlist[j]){
  116. int indexdiv=1;
  117. fprintf(stderr,"%4ld: ",j);
  118. for(k=0;k<c->dim;k++){
  119. int index= (j/indexdiv)%bins;
  120. fprintf(stderr,"%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
  121. _float32_unpack(c->q_min));
  122. indexdiv*=bins;
  123. }
  124. fprintf(stderr,"\t|");
  125. for(k=0;k<base-c->lengthlist[j];k++)fprintf(stderr,"*");
  126. fprintf(stderr,"\n");
  127. }
  128. }
  129. }
  130. fprintf(stderr,"\r "
  131. "\nDone.\n");
  132. exit(0);
  133. }