1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: utility functions for loading .vqh and .vqd files
14 last mod: $Id: bookutil.c 16959 2010-03-10 16:03:11Z xiphmont $
16 ********************************************************************/
25 int _best(codebook *book, float *a, int step){
29 int minval=book->minval;
31 int qv=book->quantvals;
34 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
37 for(i=0,o=step*(dim-1);i<dim;i++,o-=step){
38 int v = ((int)rint(a[o])-minval+(del>>1))/del;
39 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
40 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
43 for(i=0,o=step*(dim-1);i<dim;i++,o-=step){
44 int v = (int)rint(a[o])-minval;
45 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
46 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
50 if(book->c->lengthlist[index]<=0){
51 const static_codebook *c=book->c;
53 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
54 int e[8]={0,0,0,0,0,0,0,0};
55 int maxval = book->minval + book->delta*(book->quantvals-1);
56 for(i=0;i<book->entries;i++){
57 if(c->lengthlist[i]>0){
60 float val=(e[j]-a[j*step]);
63 if(best==-1 || this<best){
68 /* assumes the value patterning created by the tools in vq/ */
81 /* A few little utils for reading files */
82 /* read a line. Use global, persistent buffering */
83 static char *linebuffer=NULL;
84 static int lbufsize=0;
85 char *get_line(FILE *in){
87 if(feof(in))return NULL;
93 if(sofar+1>=lbufsize){
96 linebuffer=_ogg_malloc(lbufsize);
99 linebuffer=_ogg_realloc(linebuffer,lbufsize);
106 if(sofar==0)return(NULL);
107 /* fallthrough correct */
109 linebuffer[sofar]='\0';
113 linebuffer[sofar++]=c;
114 linebuffer[sofar]='\0';
120 if(linebuffer[0]=='#'){
128 /* read the next numerical value from the given file */
129 static char *value_line_buff=NULL;
131 int get_line_value(FILE *in,float *value){
134 if(!value_line_buff)return(-1);
136 *value=strtod(value_line_buff, &next);
137 if(next==value_line_buff){
138 value_line_buff=NULL;
141 value_line_buff=next;
142 while(*value_line_buff>44)value_line_buff++;
143 if(*value_line_buff==44)value_line_buff++;
148 int get_next_value(FILE *in,float *value){
150 if(get_line_value(in,value)){
151 value_line_buff=get_line(in);
152 if(!value_line_buff)return(-1);
159 int get_next_ivalue(FILE *in,long *ivalue){
161 int ret=get_next_value(in,&value);
166 static float sequence_base=0.f;
167 static int v_sofar=0;
168 void reset_next_value(void){
169 value_line_buff=NULL;
174 char *setup_line(FILE *in){
176 value_line_buff=get_line(in);
177 return(value_line_buff);
181 int get_vector(codebook *b,FILE *in,int start, int n,float *a){
183 const static_codebook *c=b->c;
187 if(v_sofar==n || get_line_value(in,a)){
189 if(get_next_value(in,a))
191 for(i=0;i<start;i++){
193 get_line_value(in,a);
197 for(i=1;i<c->dim;i++)
198 if(get_line_value(in,a+i))
202 float temp=a[c->dim-1];
203 for(i=0;i<c->dim;i++)a[i]-=sequence_base;
204 if(c->q_sequencep)sequence_base=temp;
214 /* read lines fromt he beginning until we find one containing the
216 char *find_seek_to(FILE *in,char *s){
219 char *line=get_line(in);
229 /* this reads the format as written by vqbuild/latticebuild; innocent
230 (legal) tweaking of the file that would not affect its valid
231 header-ness will break this routine */
233 codebook *codebook_load(char *filename){
234 codebook *b=_ogg_calloc(1,sizeof(codebook));
235 static_codebook *c=(static_codebook *)(b->c=_ogg_calloc(1,sizeof(static_codebook)));
237 FILE *in=fopen(filename,"r");
242 fprintf(stderr,"Couldn't open codebook %s\n",filename);
246 /* find the codebook struct */
247 find_seek_to(in,"static const static_codebook ");
249 /* get the major important values */
251 if(sscanf(line,"%ld, %ld,",
252 &(c->dim),&(c->entries))!=2){
253 fprintf(stderr,"1: syntax in %s in line:\t %s",filename,line);
258 if(sscanf(line,"%d, %ld, %ld, %d, %d,",
259 &(c->maptype),&(c->q_min),&(c->q_delta),&(c->q_quant),
260 &(c->q_sequencep))!=5){
261 fprintf(stderr,"1: syntax in %s in line:\t %s",filename,line);
270 quant_to_read=_book_maptype1_quantvals(c);
273 quant_to_read=c->entries*c->dim;
277 /* load the quantized entries */
278 find_seek_to(in,"static const long _vq_quantlist_");
280 c->quantlist=_ogg_malloc(sizeof(long)*quant_to_read);
281 for(i=0;i<quant_to_read;i++)
282 if(get_next_ivalue(in,c->quantlist+i)){
283 fprintf(stderr,"out of data while reading codebook %s\n",filename);
287 /* load the lengthlist */
288 find_seek_to(in,"_lengthlist");
290 c->lengthlist=_ogg_malloc(sizeof(long)*c->entries);
291 for(i=0;i<c->entries;i++)
292 if(get_next_ivalue(in,c->lengthlist+i)){
293 fprintf(stderr,"out of data while reading codebook %s\n",filename);
300 vorbis_book_init_encode(b,c);
301 b->valuelist=_book_unquantize(c,c->entries,NULL);
306 void spinnit(char *s,int n){
308 static long lasttime=0;
310 struct timeval thistime;
312 gettimeofday(&thistime,NULL);
313 test=thistime.tv_sec*10+thistime.tv_usec/100000;
317 fprintf(stderr,"%s%d ",s,n);
322 fprintf(stderr,"| \r");
325 fprintf(stderr,"/ \r");
328 fprintf(stderr,"- \r");
331 fprintf(stderr,"\\ \r");
338 void build_tree_from_lengths(int vals, long *hist, long *lengths){
340 long *membership=_ogg_malloc(vals*sizeof(long));
341 long *histsave=alloca(vals*sizeof(long));
342 memcpy(histsave,hist,vals*sizeof(long));
344 for(i=0;i<vals;i++)membership[i]=i;
346 /* find codeword lengths */
347 /* much more elegant means exist. Brute force n^2, minimum thought */
349 int first=-1,second=-1;
352 spinnit("building... ",i);
354 /* find the two nodes to join */
356 if(least==-1 || hist[j]<=least){
362 if((least==-1 || hist[j]<=least) && membership[j]!=first){
364 second=membership[j];
366 if(first==-1 || second==-1){
367 fprintf(stderr,"huffman fault; no free branch\n");
372 least=hist[first]+hist[second];
374 if(membership[j]==first || membership[j]==second){
380 for(i=0;i<vals-1;i++)
381 if(membership[i]!=membership[i+1]){
382 fprintf(stderr,"huffman fault; failed to build single tree\n");
386 /* for sanity check purposes: how many bits would it have taken to
387 encode the training set? */
392 bitsum+=(histsave[i]-1)*lengths[i];
393 samples+=histsave[i]-1;
397 fprintf(stderr,"\rTotal samples in training set: %ld \n",samples);
398 fprintf(stderr,"\rTotal bits used to represent training set: %ld\n",
406 /* wrap build_tree_from_lengths to allow zero entries in the histogram */
407 void build_tree_from_lengths0(int vals, long *hist, long *lengths){
409 /* pack the 'sparse' hit list into a dense list, then unpack
410 the lengths after the build */
413 long *lengthlist=_ogg_calloc(vals,sizeof(long));
414 long *newhist=alloca(vals*sizeof(long));
418 newhist[upper++]=hist[i];
421 fprintf(stderr,"\rEliminating %d unused entries; %d entries remain\n",
425 build_tree_from_lengths(upper,newhist,lengthlist);
430 lengths[i]=lengthlist[upper++];
437 void write_codebook(FILE *out,char *name,const static_codebook *c){
440 /* save the book in C header form */
442 /* first, the static vectors, then the book structure to tie it together. */
445 long vals=(c->maptype==1?_book_maptype1_quantvals(c):c->entries*c->dim);
446 fprintf(out,"static const long _vq_quantlist_%s[] = {\n",name);
448 fprintf(out,"\t%ld,\n",c->quantlist[j]);
450 fprintf(out,"};\n\n");
454 fprintf(out,"static const long _vq_lengthlist_%s[] = {\n",name);
455 for(j=0;j<c->entries;){
457 for(k=0;k<16 && j<c->entries;k++,j++)
458 fprintf(out,"%2ld,",c->lengthlist[j]);
461 fprintf(out,"};\n\n");
463 /* tie it all together */
465 fprintf(out,"static const static_codebook %s = {\n",name);
467 fprintf(out,"\t%ld, %ld,\n",c->dim,c->entries);
468 fprintf(out,"\t(long *)_vq_lengthlist_%s,\n",name);
469 fprintf(out,"\t%d, %ld, %ld, %d, %d,\n",
470 c->maptype,c->q_min,c->q_delta,c->q_quant,c->q_sequencep);
472 fprintf(out,"\t(long *)_vq_quantlist_%s,\n",name);
474 fprintf(out,"\tNULL,\n");
476 fprintf(out,"\t0\n};\n\n");