latticetune.c 4.1 KB

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