/********************************************************************
* *
- * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
- * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
- * PLEASE READ THESE TERMS DISTRIBUTING. *
+ * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
- * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
- * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
- * http://www.xiph.org/ *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
+ * by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: utility main for building codebooks from training sets
- last mod: $Id: build.c,v 1.8 2000/01/05 10:14:54 xiphmont Exp $
+ last mod: $Id$
********************************************************************/
#include <math.h>
#include <string.h>
#include <errno.h>
-#include "vorbis/codebook.h"
+#include "bookutil.h"
#include "vqgen.h"
#include "vqsplit.h"
while(!gotline){
if(sofar>=lbufsize){
- if(!lbufsize){
- lbufsize=1024;
- linebuffer=malloc(lbufsize);
- }else{
- lbufsize*=2;
- linebuffer=realloc(linebuffer,lbufsize);
- }
+ if(!lbufsize){
+ lbufsize=1024;
+ linebuffer=_ogg_malloc(lbufsize);
+ }else{
+ lbufsize*=2;
+ linebuffer=_ogg_realloc(linebuffer,lbufsize);
+ }
}
{
- long c=fgetc(in);
- switch(c){
- case '\n':
- case EOF:
- gotline=1;
- break;
- default:
- linebuffer[sofar++]=c;
- linebuffer[sofar]='\0';
- break;
- }
+ long c=fgetc(in);
+ switch(c){
+ case '\n':
+ case EOF:
+ gotline=1;
+ break;
+ default:
+ linebuffer[sofar++]=c;
+ linebuffer[sofar]='\0';
+ break;
+ }
}
}
int main(int argc,char *argv[]){
vqgen v;
+ static_codebook c;
codebook b;
quant_meta q;
char *line,*name;
long i,j,k;
+ b.c=&c;
+
if(argv[1]==NULL){
fprintf(stderr,"Need a trained data set on the command line.\n");
exit(1);
}
/* just use it to allocate mem */
- vqgen_init(&v,dim,0,entries,NULL,NULL);
+ vqgen_init(&v,dim,0,entries,0.f,NULL,NULL,0);
/* quant */
line=rline(in,out);
if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
- &q.quant,&q.sequencep)!=4){
+ &q.quant,&q.sequencep)!=4){
fprintf(stderr,"Syntax error reading book file\n");
exit(1);
}
/* save quant data; we don't want to requantize later as our method
is currently imperfect wrt repeated application */
i=0;
- quantlist=malloc(sizeof(long)*v.elements*v.entries);
+ quantlist=_ogg_malloc(sizeof(long)*v.elements*v.entries);
for(j=0;j<entries;j++){
- double a;
+ float a;
for(k=0;k<dim;k++){
line=rline(in,out);
- sscanf(line,"%lf",&a);
+ sscanf(line,"%f",&a);
v.entrylist[i]=a;
quantlist[i++]=rint(a);
}
/* training points */
{
- double *b=alloca(sizeof(double)*(dim+aux));
+ float *b=alloca(sizeof(float)*(dim+aux));
i=0;
v.entries=0; /* hack to avoid reseeding */
while(1){
for(k=0;k<dim+aux;k++){
- line=rline(in,out);
- if(!line)break;
- sscanf(line,"%lf",b+k);
+ line=rline(in,out);
+ if(!line)break;
+ sscanf(line,"%f",b+k);
}
if(feof(in))break;
vqgen_addpoint(&v,b,NULL);
/* build the book */
vqsp_book(&v,&b,quantlist);
+ c.q_min=q.min;
+ c.q_delta=q.delta;
+ c.q_quant=q.quant;
+ c.q_sequencep=q.sequencep;
/* save the book in C header form */
- fprintf(out,
- "/********************************************************************\n"
- " * *\n"
- " * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *\n"
- " * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *\n"
- " * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *\n"
- " * PLEASE READ THESE TERMS DISTRIBUTING. *\n"
- " * *\n"
- " * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *\n"
- " * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *\n"
- " * http://www.xiph.org/ *\n"
- " * *\n"
- " ********************************************************************\n"
- "\n"
- " function: static codebook autogenerated by vq/vqbuild\n"
- "\n"
- " ********************************************************************/\n\n");
-
- fprintf(out,"#ifndef _V_%s_VQH_\n#define _V_%s_VQH_\n",name,name);
- fprintf(out,"#include \"vorbis/codebook.h\"\n\n");
-
- /* first, the static vectors, then the book structure to tie it together. */
- /* quantlist */
- fprintf(out,"static long _vq_quantlist_%s[] = {\n",name);
- i=0;
- for(j=0;j<entries;j++){
- fprintf(out,"\t");
- for(k=0;k<dim;k++)
- fprintf(out,"%5ld, ",b.quantlist[i++]);
- fprintf(out,"\n");
- }
- fprintf(out,"};\n\n");
-
- /* codelist */
- fprintf(out,"static long _vq_codelist_%s[] = {\n",name);
- for(j=0;j<entries;){
- fprintf(out,"\t");
- for(k=0;k<8 && j<entries;k++,j++)
- fprintf(out,"%ld,",b.codelist[j]);
- fprintf(out,"\n");
- }
- fprintf(out,"};\n\n");
-
- /* lengthlist */
- fprintf(out,"static long _vq_lengthlist_%s[] = {\n",name);
- for(j=0;j<entries;){
- fprintf(out,"\t");
- for(k=0;k<16 && j<entries;k++,j++)
- fprintf(out,"%2ld,",b.lengthlist[j]);
- fprintf(out,"\n");
- }
- fprintf(out,"};\n\n");
-
- /* ptr0 */
- fprintf(out,"static long _vq_ptr0_%s[] = {\n",name);
- for(j=0;j<b.encode_tree->aux;){
- fprintf(out,"\t");
- for(k=0;k<8 && j<b.encode_tree->aux;k++,j++)
- fprintf(out,"%6ld,",b.encode_tree->ptr0[j]);
- fprintf(out,"\n");
- }
- fprintf(out,"};\n\n");
-
- /* ptr1 */
- fprintf(out,"static long _vq_ptr1_%s[] = {\n",name);
- for(j=0;j<b.encode_tree->aux;){
- fprintf(out,"\t");
- for(k=0;k<8 && j<b.encode_tree->aux;k++,j++)
- fprintf(out,"%6ld,",b.encode_tree->ptr1[j]);
- fprintf(out,"\n");
- }
- fprintf(out,"};\n\n");
-
- /* p */
- fprintf(out,"static long _vq_p_%s[] = {\n",name);
- for(j=0;j<b.encode_tree->aux;){
- fprintf(out,"\t");
- for(k=0;k<8 && j<b.encode_tree->aux;k++,j++)
- fprintf(out,"%6ld,",b.encode_tree->p[j]);
- fprintf(out,"\n");
- }
- fprintf(out,"};\n\n");
-
- /* q */
- fprintf(out,"static long _vq_q_%s[] = {\n",name);
- for(j=0;j<b.encode_tree->aux;){
- fprintf(out,"\t");
- for(k=0;k<8 && j<b.encode_tree->aux;k++,j++)
- fprintf(out,"%6ld,",b.encode_tree->q[j]);
- fprintf(out,"\n");
- }
- fprintf(out,"};\n\n");
-
- /* tie it all together */
-
- fprintf(out,"static encode_aux _vq_aux_%s = {\n",name);
- fprintf(out,"\t_vq_ptr0_%s,\n",name);
- fprintf(out,"\t_vq_ptr1_%s,\n",name);
- fprintf(out,"\t0,\n");
- fprintf(out,"\t0,\n");
- fprintf(out,"\t_vq_p_%s,\n",name);
- fprintf(out,"\t_vq_q_%s,\n",name);
- fprintf(out,"\t%ld, %ld\n};\n\n",b.encode_tree->aux,b.encode_tree->aux);
-
- fprintf(out,"static codebook _vq_book_%s = {\n",name);
- fprintf(out,"\t%ld, %ld, %ld, %ld, %d, %d,\n",
- b.dim,b.entries,q.min,q.delta,q.quant,q.sequencep);
- fprintf(out,"\t0,\n"); /* valuelist */
- fprintf(out,"\t_vq_quantlist_%s,\n",name);
- fprintf(out,"\t_vq_codelist_%s,\n",name);
- fprintf(out,"\t_vq_lengthlist_%s,\n",name);
- fprintf(out,"\t&_vq_aux_%s,\n",name);
- fprintf(out,"\t0\n");
- fprintf(out,"};\n\n");
+ write_codebook(out,name,b.c);
- fprintf(out,"\n#endif\n");
fclose(out);
exit(0);
}