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-2009             *
9 * by the Xiph.Org Foundation http://www.xiph.org/                  *
10 *                                                                  *
11 ********************************************************************
12
13 function: basic shared codebook operations
14 last mod: $Id: codebook.h 17030 2010-03-25 06:52:55Z xiphmont $
15
16 ********************************************************************/
17
18#ifndef _V_CODEBOOK_H_
19#define _V_CODEBOOK_H_
20
21#include <ogg/ogg.h>
22
23/* This structure encapsulates huffman and VQ style encoding books; it
24   doesn't do anything specific to either.
25
26   valuelist/quantlist are nonNULL (and q_* significant) only if
27   there's entry->value mapping to be done.
28
29   If encode-side mapping must be done (and thus the entry needs to be
30   hunted), the auxiliary encode pointer will point to a decision
31   tree.  This is true of both VQ and huffman, but is mostly useful
32   with VQ.
33
34*/
35
36typedef struct static_codebook{
37  long   dim;            /* codebook dimensions (elements per vector) */
38  long   entries;        /* codebook entries */
39  long  *lengthlist;     /* codeword lengths in bits */
40
41  /* mapping ***************************************************************/
42  int    maptype;        /* 0=none
43                            1=implicitly populated values from map column
44                            2=listed arbitrary values */
45
46  /* The below does a linear, single monotonic sequence mapping. */
47  long     q_min;       /* packed 32 bit float; quant value 0 maps to minval */
48  long     q_delta;     /* packed 32 bit float; val 1 - val 0 == delta */
49  int      q_quant;     /* bits: 0 < quant <= 16 */
50  int      q_sequencep; /* bitflag */
51
52  long     *quantlist;  /* map == 1: (int)(entries^(1/dim)) element column map
53                           map == 2: list of dim*entries quantized entry vals
54                        */
55  int allocedp;
56} static_codebook;
57
58typedef struct codebook{
59  long dim;           /* codebook dimensions (elements per vector) */
60  long entries;       /* codebook entries */
61  long used_entries;  /* populated codebook entries */
62  const static_codebook *c;
63
64  /* for encode, the below are entry-ordered, fully populated */
65  /* for decode, the below are ordered by bitreversed codeword and only
66     used entries are populated */
67  float        *valuelist;  /* list of dim*entries actual entry values */
68  ogg_uint32_t *codelist;   /* list of bitstream codewords for each entry */
69
70  int          *dec_index;  /* only used if sparseness collapsed */
71  char         *dec_codelengths;
72  ogg_uint32_t *dec_firsttable;
73  int           dec_firsttablen;
74  int           dec_maxlength;
75
76  /* The current encoder uses only centered, integer-only lattice books. */
77  int           quantvals;
78  int           minval;
79  int           delta;
80} codebook;
81
82extern void vorbis_staticbook_destroy(static_codebook *b);
83extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source);
84extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source);
85extern void vorbis_book_clear(codebook *b);
86
87extern float *_book_unquantize(const static_codebook *b,int n,int *map);
88extern float *_book_logdist(const static_codebook *b,float *vals);
89extern float _float32_unpack(long val);
90extern long   _float32_pack(float val);
91extern int  _best(codebook *book, float *a, int step);
92extern int _ilog(unsigned int v);
93extern long _book_maptype1_quantvals(const static_codebook *b);
94
95extern int vorbis_book_besterror(codebook *book,float *a,int step,int addmul);
96extern long vorbis_book_codeword(codebook *book,int entry);
97extern long vorbis_book_codelen(codebook *book,int entry);
98
99
100
101extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b);
102extern static_codebook *vorbis_staticbook_unpack(oggpack_buffer *b);
103
104extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b);
105
106extern long vorbis_book_decode(codebook *book, oggpack_buffer *b);
107extern long vorbis_book_decodevs_add(codebook *book, float *a,
108                                     oggpack_buffer *b,int n);
109extern long vorbis_book_decodev_set(codebook *book, float *a,
110                                    oggpack_buffer *b,int n);
111extern long vorbis_book_decodev_add(codebook *book, float *a,
112                                    oggpack_buffer *b,int n);
113extern long vorbis_book_decodevv_add(codebook *book, float **a,
114                                     long off,int ch,
115                                    oggpack_buffer *b,int n);
116
117
118
119#endif
120