Minor build fixes, integrate XMMS into the autoconfed stuff
[platform/upstream/libvorbis.git] / vq / lspdata.c
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
14  function: metrics and quantization code for LSP VQ codebooks
15  last mod: $Id: lspdata.c,v 1.7 1999/12/30 07:27:00 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <math.h>
21 #include <stdio.h>
22 #include "vqgen.h"
23 #include "vqext.h"
24
25 char *vqext_booktype="LSPdata";  
26 quant_meta q={0,0,0,1};          /* set sequence data */
27 int vqext_aux=1;
28
29 /* LSP training metric.  We weight error proportional to distance
30    *between* LSP vector values.  The idea of this metric is not to set
31    final cells, but get the midpoint spacing into a form conducive to
32    what we want, which is weighting toward preserving narrower
33    features. */
34
35 double global_maxdel=M_PI;
36 #define FUDGE (global_maxdel-weight[i])
37 double *weight=NULL;
38
39 double *vqext_weight(vqgen *v,double *p){
40   int i;
41   int el=v->elements;
42   double lastp=0.;
43   for(i=0;i<el;i++){
44     double predist=(p[i]-lastp);
45     double postdist=(p[i+1]-p[i]);
46     weight[i]=(predist<postdist?predist:postdist);
47     lastp=p[i];
48   }
49   return p;
50 }
51
52                             /* candidate,actual */
53 double vqext_metric(vqgen *v,double *e, double *p){
54   int i;
55   int el=v->elements;
56   double acc=0.;
57   for(i=0;i<el;i++){
58     double val=(p[i]-e[i])*FUDGE;
59     acc+=val*val;
60   }
61   return acc;
62 }
63
64 /* Data files are line-vectors, starting with zero.  If we want to
65    train on a subvector starting in the middle, we need to adjust the
66    data as if it was starting at zero.  we also need to add the 'aux'
67    value, which is an extra point at the end so we have leading and
68    trailing space */
69
70 /* assume vqext_aux==1 */
71 void vqext_addpoint_adj(vqgen *v,double *b,int start,int dim,int cols){
72   double *a=alloca(sizeof(double)*(dim+1)); /* +aux */
73   double base=0;
74   int i;
75
76   if(start>0)base=b[start-1];
77   for(i=0;i<dim;i++)a[i]=b[i+start]-base;
78   if(start+dim+1>cols) /* +aux */
79     a[i]=M_PI-base;
80   else
81     a[i]=b[i+start]-base;
82   
83   vqgen_addpoint(v,a,a+dim);
84 }
85
86 /* we just need to calc the global_maxdel from the training set */
87 void vqext_preprocess(vqgen *v){
88   long j,k;
89
90   global_maxdel=0.;
91   for(j=0;j<v->points;j++){
92     double last=0.;
93     for(k=0;k<v->elements+v->aux;k++){
94       double p=_point(v,j)[k];
95       if(p-last>global_maxdel)global_maxdel=p-last;
96       last=p;
97     }
98   }
99
100   global_maxdel*=1.1;
101   weight=malloc(sizeof(double)*v->elements);
102 }
103