annotate src/libvorbis-1.3.3/vq/latticetune.c @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents 98c1576536ae
children
rev   line source
cannam@86 1 /********************************************************************
cannam@86 2 * *
cannam@86 3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
cannam@86 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
cannam@86 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
cannam@86 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
cannam@86 7 * *
cannam@86 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
cannam@86 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
cannam@86 10 * *
cannam@86 11 ********************************************************************
cannam@86 12
cannam@86 13 function: utility main for setting entropy encoding parameters
cannam@86 14 for lattice codebooks
cannam@86 15 last mod: $Id: latticetune.c 16037 2009-05-26 21:10:58Z xiphmont $
cannam@86 16
cannam@86 17 ********************************************************************/
cannam@86 18
cannam@86 19 #include <stdlib.h>
cannam@86 20 #include <stdio.h>
cannam@86 21 #include <math.h>
cannam@86 22 #include <string.h>
cannam@86 23 #include <errno.h>
cannam@86 24 #include "bookutil.h"
cannam@86 25
cannam@86 26 static int strrcmp_i(char *s,char *cmp){
cannam@86 27 return(strncmp(s+strlen(s)-strlen(cmp),cmp,strlen(cmp)));
cannam@86 28 }
cannam@86 29
cannam@86 30 /* This util takes a training-collected file listing codewords used in
cannam@86 31 LSP fitting, then generates new codeword lengths for maximally
cannam@86 32 efficient integer-bits entropy encoding.
cannam@86 33
cannam@86 34 command line:
cannam@86 35 latticetune book.vqh input.vqd [unused_entriesp]
cannam@86 36
cannam@86 37 latticetune produces book.vqh on stdout */
cannam@86 38
cannam@86 39 int main(int argc,char *argv[]){
cannam@86 40 codebook *b;
cannam@86 41 static_codebook *c;
cannam@86 42 long *lengths;
cannam@86 43 long *hits;
cannam@86 44
cannam@86 45 int entries=-1,dim=-1,guard=1;
cannam@86 46 FILE *in=NULL;
cannam@86 47 char *line,*name;
cannam@86 48 long j;
cannam@86 49
cannam@86 50 if(argv[1]==NULL){
cannam@86 51 fprintf(stderr,"Need a lattice codebook on the command line.\n");
cannam@86 52 exit(1);
cannam@86 53 }
cannam@86 54 if(argv[2]==NULL){
cannam@86 55 fprintf(stderr,"Need a codeword data file on the command line.\n");
cannam@86 56 exit(1);
cannam@86 57 }
cannam@86 58 if(argv[3]!=NULL)guard=0;
cannam@86 59
cannam@86 60 {
cannam@86 61 char *ptr;
cannam@86 62 char *filename=strdup(argv[1]);
cannam@86 63
cannam@86 64 b=codebook_load(filename);
cannam@86 65 c=(static_codebook *)(b->c);
cannam@86 66
cannam@86 67 ptr=strrchr(filename,'.');
cannam@86 68 if(ptr){
cannam@86 69 *ptr='\0';
cannam@86 70 name=strdup(filename);
cannam@86 71 }else{
cannam@86 72 name=strdup(filename);
cannam@86 73 }
cannam@86 74 }
cannam@86 75
cannam@86 76 if(c->maptype!=1){
cannam@86 77 fprintf(stderr,"Provided book is not a latticebook.\n");
cannam@86 78 exit(1);
cannam@86 79 }
cannam@86 80
cannam@86 81 entries=b->entries;
cannam@86 82 dim=b->dim;
cannam@86 83
cannam@86 84 hits=_ogg_malloc(entries*sizeof(long));
cannam@86 85 lengths=_ogg_calloc(entries,sizeof(long));
cannam@86 86 for(j=0;j<entries;j++)hits[j]=guard;
cannam@86 87
cannam@86 88 in=fopen(argv[2],"r");
cannam@86 89 if(!in){
cannam@86 90 fprintf(stderr,"Could not open input file %s\n",argv[2]);
cannam@86 91 exit(1);
cannam@86 92 }
cannam@86 93
cannam@86 94 if(!strrcmp_i(argv[0],"latticetune")){
cannam@86 95 long lines=0;
cannam@86 96 line=setup_line(in);
cannam@86 97 while(line){
cannam@86 98 long code;
cannam@86 99 lines++;
cannam@86 100 if(!(lines&0xfff))spinnit("codewords so far...",lines);
cannam@86 101
cannam@86 102 if(sscanf(line,"%ld",&code)==1)
cannam@86 103 hits[code]++;
cannam@86 104
cannam@86 105 line=setup_line(in);
cannam@86 106 }
cannam@86 107 }
cannam@86 108
cannam@86 109 /* now we simply count already collated by-entry data */
cannam@86 110 if(!strrcmp_i(argv[0],"res0tune") || !strrcmp_i(argv[0],"res1tune")){
cannam@86 111
cannam@86 112 line=setup_line(in);
cannam@86 113 while(line){
cannam@86 114
cannam@86 115 /* code:hits\n */
cannam@86 116 /* likely to have multiple listing for each code entry; must
cannam@86 117 accumulate */
cannam@86 118
cannam@86 119 char *pos=strchr(line,':');
cannam@86 120 if(pos){
cannam@86 121 long code=atol(line);
cannam@86 122 long val=atol(pos+1);
cannam@86 123 hits[code]+=val;
cannam@86 124 }
cannam@86 125
cannam@86 126 line=setup_line(in);
cannam@86 127 }
cannam@86 128 }
cannam@86 129
cannam@86 130 fclose(in);
cannam@86 131
cannam@86 132 /* build the codeword lengths */
cannam@86 133 build_tree_from_lengths0(entries,hits,lengths);
cannam@86 134
cannam@86 135 c->lengthlist=lengths;
cannam@86 136 write_codebook(stdout,name,c);
cannam@86 137
cannam@86 138 {
cannam@86 139 long bins=_book_maptype1_quantvals(c);
cannam@86 140 long i,k,base=c->lengthlist[0];
cannam@86 141 for(i=0;i<entries;i++)
cannam@86 142 if(c->lengthlist[i]>base)base=c->lengthlist[i];
cannam@86 143
cannam@86 144 for(j=0;j<entries;j++){
cannam@86 145 if(c->lengthlist[j]){
cannam@86 146 int indexdiv=1;
cannam@86 147 fprintf(stderr,"%4ld: ",j);
cannam@86 148 for(k=0;k<c->dim;k++){
cannam@86 149 int index= (j/indexdiv)%bins;
cannam@86 150 fprintf(stderr,"%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
cannam@86 151 _float32_unpack(c->q_min));
cannam@86 152 indexdiv*=bins;
cannam@86 153 }
cannam@86 154 fprintf(stderr,"\t|");
cannam@86 155 for(k=0;k<base-c->lengthlist[j];k++)fprintf(stderr,"*");
cannam@86 156 fprintf(stderr,"\n");
cannam@86 157 }
cannam@86 158 }
cannam@86 159 }
cannam@86 160
cannam@86 161 fprintf(stderr,"\r "
cannam@86 162 "\nDone.\n");
cannam@86 163 exit(0);
cannam@86 164 }