more automation work
[platform/upstream/libvorbis.git] / lib / codebook.h
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis 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: basic shared codebook operations
15  last mod: $Id: codebook.h,v 1.2 2000/11/06 00:07:00 xiphmont Exp $
16
17  ********************************************************************/
18
19 #ifndef _V_CODEBOOK_H_
20 #define _V_CODEBOOK_H_
21
22 /* This structure encapsulates huffman and VQ style encoding books; it
23    doesn't do anything specific to either.
24
25    valuelist/quantlist are nonNULL (and q_* significant) only if
26    there's entry->value mapping to be done.
27
28    If encode-side mapping must be done (and thus the entry needs to be
29    hunted), the auxiliary encode pointer will point to a decision
30    tree.  This is true of both VQ and huffman, but is mostly useful
31    with VQ.
32
33 */
34
35 typedef struct static_codebook{
36   long   dim;            /* codebook dimensions (elements per vector) */
37   long   entries;        /* codebook entries */
38   long  *lengthlist;     /* codeword lengths in bits */
39
40   /* mapping ***************************************************************/
41   int    maptype;        /* 0=none
42                             1=implicitly populated values from map column 
43                             2=listed arbitrary values */
44
45   /* The below does a linear, single monotonic sequence mapping. */
46   long     q_min;       /* packed 32 bit float; quant value 0 maps to minval */
47   long     q_delta;     /* packed 32 bit float; val 1 - val 0 == delta */
48   int      q_quant;     /* bits: 0 < quant <= 16 */
49   int      q_sequencep; /* bitflag */
50
51   long     *quantlist;  /* map == 1: (int)(entries^(1/dim)) element column map
52                            map == 2: list of dim*entries quantized entry vals
53                         */
54
55   /* encode helpers ********************************************************/
56   struct encode_aux_nearestmatch *nearest_tree;
57   struct encode_aux_threshmatch  *thresh_tree;
58   struct encode_aux_pigeonhole  *pigeon_tree;
59
60   int allocedp;
61 } static_codebook;
62
63 /* this structures an arbitrary trained book to quickly find the
64    nearest cell match */
65 typedef struct encode_aux_nearestmatch{
66   /* pre-calculated partitioning tree */
67   long   *ptr0;
68   long   *ptr1;
69
70   long   *p;         /* decision points (each is an entry) */
71   long   *q;         /* decision points (each is an entry) */
72   long   aux;        /* number of tree entries */
73   long   alloc;       
74 } encode_aux_nearestmatch;
75
76 /* assumes a maptype of 1; encode side only, so that's OK */
77 typedef struct encode_aux_threshmatch{
78   float *quantthresh;
79   long   *quantmap;
80   int     quantvals; 
81   int     threshvals; 
82 } encode_aux_threshmatch;
83
84 typedef struct encode_aux_pigeonhole{
85   float min;
86   float del;
87
88   int  mapentries;
89   int  quantvals;
90   long *pigeonmap;
91
92   long fittotal;
93   long *fitlist;
94   long *fitmap;
95   long *fitlength;
96 } encode_aux_pigeonhole;
97
98 typedef struct decode_aux{
99   long   *tab;
100   int    *tabl;
101   int    tabn;
102
103   long   *ptr0;
104   long   *ptr1;
105   long   aux;        /* number of tree entries */
106 } decode_aux;
107
108 typedef struct codebook{
109   long dim;           /* codebook dimensions (elements per vector) */
110   long entries;       /* codebook entries */
111   const static_codebook *c;
112
113   float  *valuelist;  /* list of dim*entries actual entry values */
114   long   *codelist;   /* list of bitstream codewords for each entry */
115   struct decode_aux *decode_tree;
116
117 } codebook;
118
119 extern void vorbis_staticbook_clear(static_codebook *b);
120 extern void vorbis_staticbook_destroy(static_codebook *b);
121 extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source);
122 extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source);
123 extern void vorbis_book_clear(codebook *b);
124
125 extern float *_book_unquantize(const static_codebook *b);
126 extern float *_book_logdist(const static_codebook *b,float *vals);
127 extern float _float32_unpack(long val);
128 extern long   _float32_pack(float val);
129 extern int  _best(codebook *book, float *a, int step);
130 extern int _ilog(unsigned int v);
131 extern long _book_maptype1_quantvals(const static_codebook *b);
132
133 extern int vorbis_book_besterror(codebook *book,float *a,int step,int addmul);
134 extern long vorbis_book_codeword(codebook *book,int entry);
135 extern long vorbis_book_codelen(codebook *book,int entry);
136
137
138
139 extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b);
140 extern int vorbis_staticbook_unpack(oggpack_buffer *b,static_codebook *c);
141
142 extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b);
143 extern int vorbis_book_errorv(codebook *book, float *a);
144 extern int vorbis_book_encodev(codebook *book, int best,float *a, 
145                                oggpack_buffer *b);
146 extern int vorbis_book_encodevs(codebook *book, float *a, oggpack_buffer *b,
147                                 int step,int stagetype);
148
149 extern long vorbis_book_decode(codebook *book, oggpack_buffer *b);
150 extern long vorbis_book_decodevs(codebook *book, float *a, oggpack_buffer *b,
151                                  int step,int stagetype);
152 extern long s_vorbis_book_decodevs(codebook *book, float *a, oggpack_buffer *b,
153                                    int step,int stagetype);
154
155
156
157 #endif