latticetune.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 setting entropy encoding parameters
  14. for lattice codebooks
  15. last mod: $Id: latticetune.c,v 1.2.2.1 2000/08/31 09:00:03 xiphmont Exp $
  16. ********************************************************************/
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include "vorbis/codebook.h"
  23. #include "../lib/sharedbook.h"
  24. #include "bookutil.h"
  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=malloc(entries*sizeof(long));
  69. lengths=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(!strcmp(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. if(!strcmp(argv[0],"restune")){
  89. long step;
  90. long lines=0;
  91. long cols=-1;
  92. float *vec;
  93. line=setup_line(in);
  94. while(line){
  95. int code;
  96. if(!(lines&0xfff))spinnit("codewords so far...",lines);
  97. if(cols==-1){
  98. char *temp=line;
  99. while(*temp==' ')temp++;
  100. for(cols=0;*temp;cols++){
  101. while(*temp>32)temp++;
  102. while(*temp==' ')temp++;
  103. }
  104. vec=alloca(sizeof(float)*cols);
  105. step=cols/dim;
  106. }
  107. for(j=0;j<cols;j++)
  108. if(get_line_value(in,vec+j)){
  109. fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
  110. exit(1);
  111. }
  112. for(j=0;j<step;j++){
  113. lines++;
  114. code=_best(b,vec+j,step);
  115. hits[code]++;
  116. }
  117. line=setup_line(in);
  118. }
  119. }
  120. fclose(in);
  121. /* build the codeword lengths */
  122. build_tree_from_lengths0(entries,hits,lengths);
  123. c->lengthlist=lengths;
  124. write_codebook(stdout,name,c);
  125. fprintf(stderr,"\r "
  126. "\nDone.\n");
  127. exit(0);
  128. }