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-2014 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: utility functions for loading .vqh and .vqd files
15 ********************************************************************/
24 int _best(codebook *book, float *a, int step){
28 int minval=book->minval;
30 int qv=book->quantvals;
33 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
36 for(i=0,o=step*(dim-1);i<dim;i++,o-=step){
37 int v = ((int)rint(a[o])-minval+(del>>1))/del;
38 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
39 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
42 for(i=0,o=step*(dim-1);i<dim;i++,o-=step){
43 int v = (int)rint(a[o])-minval;
44 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
45 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
49 if(book->c->lengthlist[index]<=0){
50 const static_codebook *c=book->c;
52 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
53 int e[8]={0,0,0,0,0,0,0,0};
54 int maxval = book->minval + book->delta*(book->quantvals-1);
55 for(i=0;i<book->entries;i++){
56 if(c->lengthlist[i]>0){
59 float val=(e[j]-a[j*step]);
62 if(best==-1 || this<best){
67 /* assumes the value patterning created by the tools in vq/ */
80 /* A few little utils for reading files */
81 /* read a line. Use global, persistent buffering */
82 static char *linebuffer=NULL;
83 static int lbufsize=0;
84 char *get_line(FILE *in){
86 if(feof(in))return NULL;
92 if(sofar+1>=lbufsize){
95 linebuffer=_ogg_malloc(lbufsize);
98 linebuffer=_ogg_realloc(linebuffer,lbufsize);
105 if(sofar==0)return(NULL);
106 /* fallthrough correct */
108 linebuffer[sofar]='\0';
112 linebuffer[sofar++]=c;
113 linebuffer[sofar]='\0';
119 if(linebuffer[0]=='#'){
127 /* read the next numerical value from the given file */
128 static char *value_line_buff=NULL;
130 int get_line_value(FILE *in,float *value){
133 if(!value_line_buff)return(-1);
135 *value=strtod(value_line_buff, &next);
136 if(next==value_line_buff){
137 value_line_buff=NULL;
140 value_line_buff=next;
141 while(*value_line_buff>44)value_line_buff++;
142 if(*value_line_buff==44)value_line_buff++;
147 int get_next_value(FILE *in,float *value){
149 if(get_line_value(in,value)){
150 value_line_buff=get_line(in);
151 if(!value_line_buff)return(-1);
158 int get_next_ivalue(FILE *in,long *ivalue){
160 int ret=get_next_value(in,&value);
165 static float sequence_base=0.f;
166 static int v_sofar=0;
167 void reset_next_value(void){
168 value_line_buff=NULL;
173 char *setup_line(FILE *in){
175 value_line_buff=get_line(in);
176 return(value_line_buff);
180 int get_vector(codebook *b,FILE *in,int start, int n,float *a){
182 const static_codebook *c=b->c;
186 if(v_sofar==n || get_line_value(in,a)){
188 if(get_next_value(in,a))
190 for(i=0;i<start;i++){
192 get_line_value(in,a);
196 for(i=1;i<c->dim;i++)
197 if(get_line_value(in,a+i))
201 float temp=a[c->dim-1];
202 for(i=0;i<c->dim;i++)a[i]-=sequence_base;
203 if(c->q_sequencep)sequence_base=temp;
213 /* read lines fromt he beginning until we find one containing the
215 char *find_seek_to(FILE *in,char *s){
218 char *line=get_line(in);
228 /* this reads the format as written by vqbuild/latticebuild; innocent
229 (legal) tweaking of the file that would not affect its valid
230 header-ness will break this routine */
232 codebook *codebook_load(char *filename){
233 codebook *b=_ogg_calloc(1,sizeof(codebook));
234 static_codebook *c=(static_codebook *)(b->c=_ogg_calloc(1,sizeof(static_codebook)));
236 FILE *in=fopen(filename,"r");
241 fprintf(stderr,"Couldn't open codebook %s\n",filename);
245 /* find the codebook struct */
246 find_seek_to(in,"static const static_codebook ");
248 /* get the major important values */
250 if(sscanf(line,"%ld, %ld,",
251 &(c->dim),&(c->entries))!=2){
252 fprintf(stderr,"1: syntax in %s in line:\t %s",filename,line);
257 if(sscanf(line,"%d, %ld, %ld, %d, %d,",
258 &(c->maptype),&(c->q_min),&(c->q_delta),&(c->q_quant),
259 &(c->q_sequencep))!=5){
260 fprintf(stderr,"1: syntax in %s in line:\t %s",filename,line);
269 quant_to_read=_book_maptype1_quantvals(c);
272 quant_to_read=c->entries*c->dim;
276 /* load the quantized entries */
277 find_seek_to(in,"static const long _vq_quantlist_");
279 c->quantlist=_ogg_malloc(sizeof(long)*quant_to_read);
280 for(i=0;i<quant_to_read;i++)
281 if(get_next_ivalue(in,c->quantlist+i)){
282 fprintf(stderr,"out of data while reading codebook %s\n",filename);
286 /* load the lengthlist */
287 find_seek_to(in,"_lengthlist");
289 c->lengthlist=_ogg_malloc(sizeof(long)*c->entries);
290 for(i=0;i<c->entries;i++)
291 if(get_next_ivalue(in,c->lengthlist+i)){
292 fprintf(stderr,"out of data while reading codebook %s\n",filename);
299 vorbis_book_init_encode(b,c);
300 b->valuelist=_book_unquantize(c,c->entries,NULL);
305 void spinnit(char *s,int n){
307 static long lasttime=0;
309 struct timeval thistime;
311 gettimeofday(&thistime,NULL);
312 test=thistime.tv_sec*10+thistime.tv_usec/100000;
316 fprintf(stderr,"%s%d ",s,n);
321 fprintf(stderr,"| \r");
324 fprintf(stderr,"/ \r");
327 fprintf(stderr,"- \r");
330 fprintf(stderr,"\\ \r");
337 void build_tree_from_lengths(int vals, long *hist, long *lengths){
339 long *membership=_ogg_malloc(vals*sizeof(long));
340 long *histsave=alloca(vals*sizeof(long));
341 memcpy(histsave,hist,vals*sizeof(long));
343 for(i=0;i<vals;i++)membership[i]=i;
345 /* find codeword lengths */
346 /* much more elegant means exist. Brute force n^2, minimum thought */
348 int first=-1,second=-1;
351 spinnit("building... ",i);
353 /* find the two nodes to join */
355 if(least==-1 || hist[j]<=least){
361 if((least==-1 || hist[j]<=least) && membership[j]!=first){
363 second=membership[j];
365 if(first==-1 || second==-1){
366 fprintf(stderr,"huffman fault; no free branch\n");
371 least=hist[first]+hist[second];
373 if(membership[j]==first || membership[j]==second){
379 for(i=0;i<vals-1;i++)
380 if(membership[i]!=membership[i+1]){
381 fprintf(stderr,"huffman fault; failed to build single tree\n");
385 /* for sanity check purposes: how many bits would it have taken to
386 encode the training set? */
391 bitsum+=(histsave[i]-1)*lengths[i];
392 samples+=histsave[i]-1;
396 fprintf(stderr,"\rTotal samples in training set: %ld \n",samples);
397 fprintf(stderr,"\rTotal bits used to represent training set: %ld\n",
405 /* wrap build_tree_from_lengths to allow zero entries in the histogram */
406 void build_tree_from_lengths0(int vals, long *hist, long *lengths){
408 /* pack the 'sparse' hit list into a dense list, then unpack
409 the lengths after the build */
412 long *lengthlist=_ogg_calloc(vals,sizeof(long));
413 long *newhist=alloca(vals*sizeof(long));
417 newhist[upper++]=hist[i];
420 fprintf(stderr,"\rEliminating %d unused entries; %d entries remain\n",
424 build_tree_from_lengths(upper,newhist,lengthlist);
429 lengths[i]=lengthlist[upper++];
436 void write_codebook(FILE *out,char *name,const static_codebook *c){
439 /* save the book in C header form */
441 /* first, the static vectors, then the book structure to tie it together. */
444 long vals=(c->maptype==1?_book_maptype1_quantvals(c):c->entries*c->dim);
445 fprintf(out,"static const long _vq_quantlist_%s[] = {\n",name);
447 fprintf(out,"\t%ld,\n",c->quantlist[j]);
449 fprintf(out,"};\n\n");
453 fprintf(out,"static const char _vq_lengthlist_%s[] = {\n",name);
454 for(j=0;j<c->entries;){
456 for(k=0;k<16 && j<c->entries;k++,j++)
457 fprintf(out,"%2ld,",c->lengthlist[j]);
460 fprintf(out,"};\n\n");
462 /* tie it all together */
464 fprintf(out,"static const static_codebook %s = {\n",name);
466 fprintf(out,"\t%ld, %ld,\n",c->dim,c->entries);
467 fprintf(out,"\t(char *)_vq_lengthlist_%s,\n",name);
468 fprintf(out,"\t%d, %ld, %ld, %d, %d,\n",
469 c->maptype,c->q_min,c->q_delta,c->q_quant,c->q_sequencep);
471 fprintf(out,"\t(long *)_vq_quantlist_%s,\n",name);
473 fprintf(out,"\tNULL,\n");
475 fprintf(out,"\t0\n};\n\n");