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