1 /********************************************************************
2  *                                                                  *
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.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
9  * by the Xiph.Org Foundation https://xiph.org/                     *
10  *                                                                  *
11  ********************************************************************
12 
13  function: basic shared codebook operations
14 
15  ********************************************************************/
16 
17 #ifndef _V_CODEBOOK_H_
18 #define _V_CODEBOOK_H_
19 
20 #include <ogg/ogg.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   int allocedp;
55 } static_codebook;
56 
57 typedef struct codebook{
58   long dim;           /* codebook dimensions (elements per vector) */
59   long entries;       /* codebook entries */
60   long used_entries;  /* populated codebook entries */
61   static_codebook *c;
62 
63   /* for encode, the below are entry-ordered, fully populated */
64   /* for decode, the below are ordered by bitreversed codeword and only
65      used entries are populated */
66   float        *valuelist;  /* list of dim*entries actual entry values */
67   ogg_uint32_t *codelist;   /* list of bitstream codewords for each entry */
68 
69   int          *dec_index;  /* only used if sparseness collapsed */
70   char         *dec_codelengths;
71   ogg_uint32_t *dec_firsttable;
72   int           dec_firsttablen;
73   int           dec_maxlength;
74 
75   /* The current encoder uses only centered, integer-only lattice books. */
76   int           quantvals;
77   int           minval;
78   int           delta;
79 
80 } codebook;
81 
82 extern void vorbis_staticbook_clear(static_codebook *b);
83 extern void vorbis_staticbook_destroy(static_codebook *b);
84 extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source);
85 extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source);
86 extern void vorbis_book_clear(codebook *b);
87 
88 extern float *_book_unquantize(const static_codebook *b,int n,int *map);
89 extern float *_book_logdist(const static_codebook *b,float *vals);
90 extern float _float32_unpack(long val);
91 extern long   _float32_pack(float val);
92 extern int  _best(codebook *book, float *a, int step);
93 extern int _ilog(unsigned int v);
94 extern long _book_maptype1_quantvals(const static_codebook *b);
95 
96 extern int vorbis_book_besterror(codebook *book,float *a,int step,int addmul);
97 extern long vorbis_book_codeword(codebook *book,int entry);
98 extern long vorbis_book_codelen(codebook *book,int entry);
99 
100 
101 
102 extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b);
103 extern int vorbis_staticbook_unpack(oggpack_buffer *b,static_codebook *c);
104 
105 extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b);
106 
107 extern long vorbis_book_decode(codebook *book, oggpack_buffer *b);
108 extern long vorbis_book_decodevs_add(codebook *book, float *a,
109                                      oggpack_buffer *b,int n);
110 extern long vorbis_book_decodev_set(codebook *book, float *a,
111                                     oggpack_buffer *b,int n);
112 extern long vorbis_book_decodev_add(codebook *book, float *a,
113                                     oggpack_buffer *b,int n);
114 extern long vorbis_book_decodevv_add(codebook *book, float **a,
115                                      long off,int ch,
116                                     oggpack_buffer *b,int n);
117 
118 
119 
120 #endif
121