codebook.h 4.86 KB
Newer Older
Monty's avatar
 
Monty committed
1 2
/********************************************************************
 *                                                                  *
Monty's avatar
 
Monty committed
3
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Monty's avatar
 
Monty committed
4 5 6
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
Monty's avatar
 
Monty committed
7
 *                                                                  *
Ralph Giles's avatar
Ralph Giles committed
8
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015             *
9
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
10
 *                                                                  *
Monty's avatar
 
Monty committed
11 12
 ********************************************************************

Monty's avatar
 
Monty committed
13
 function: basic shared codebook operations
14
 last mod: $Id$
Monty's avatar
 
Monty committed
15 16 17 18 19 20

 ********************************************************************/

#ifndef _V_CODEBOOK_H_
#define _V_CODEBOOK_H_

21
#include <ogg/ogg.h>
Monty's avatar
 
Monty committed
22

Monty's avatar
 
Monty committed
23 24 25 26 27 28 29 30 31 32 33 34 35
/* This structure encapsulates huffman and VQ style encoding books; it
   doesn't do anything specific to either.

   valuelist/quantlist are nonNULL (and q_* significant) only if
   there's entry->value mapping to be done.

   If encode-side mapping must be done (and thus the entry needs to be
   hunted), the auxiliary encode pointer will point to a decision
   tree.  This is true of both VQ and huffman, but is mostly useful
   with VQ.

*/

Monty's avatar
 
Monty committed
36
typedef struct static_codebook{
37 38 39
  long   dim;           /* codebook dimensions (elements per vector) */
  long   entries;       /* codebook entries */
  char  *lengthlist;    /* codeword lengths in bits */
Monty's avatar
 
Monty committed
40 41

  /* mapping ***************************************************************/
42 43 44
  int    maptype;       /* 0=none
                           1=implicitly populated values from map column
                           2=listed arbitrary values */
Monty's avatar
 
Monty committed
45 46 47 48 49 50 51

  /* The below does a linear, single monotonic sequence mapping. */
  long     q_min;       /* packed 32 bit float; quant value 0 maps to minval */
  long     q_delta;     /* packed 32 bit float; val 1 - val 0 == delta */
  int      q_quant;     /* bits: 0 < quant <= 16 */
  int      q_sequencep; /* bitflag */

Monty's avatar
 
Monty committed
52
  long     *quantlist;  /* map == 1: (int)(entries^(1/dim)) element column map
53 54
                           map == 2: list of dim*entries quantized entry vals
                        */
Monty's avatar
 
Monty committed
55
  int allocedp;
Monty's avatar
 
Monty committed
56
} static_codebook;
Monty's avatar
 
Monty committed
57

Monty's avatar
 
Monty committed
58 59 60
typedef struct codebook{
  long dim;           /* codebook dimensions (elements per vector) */
  long entries;       /* codebook entries */
Monty's avatar
 
Monty committed
61
  long used_entries;  /* populated codebook entries */
Monty's avatar
 
Monty committed
62
  const static_codebook *c;
Monty's avatar
 
Monty committed
63

Monty's avatar
 
Monty committed
64 65 66
  /* for encode, the below are entry-ordered, fully populated */
  /* for decode, the below are ordered by bitreversed codeword and only
     used entries are populated */
Monty's avatar
Monty committed
67
  float        *valuelist;  /* list of dim*entries actual entry values */
Monty's avatar
 
Monty committed
68 69 70 71
  ogg_uint32_t *codelist;   /* list of bitstream codewords for each entry */

  int          *dec_index;  /* only used if sparseness collapsed */
  char         *dec_codelengths;
Monty's avatar
 
Monty committed
72
  ogg_uint32_t *dec_firsttable;
Monty's avatar
 
Monty committed
73 74
  int           dec_firsttablen;
  int           dec_maxlength;
Monty's avatar
 
Monty committed
75

76 77 78 79
  /* The current encoder uses only centered, integer-only lattice books. */
  int           quantvals;
  int           minval;
  int           delta;
Monty's avatar
 
Monty committed
80 81
} codebook;

Monty's avatar
 
Monty committed
82 83 84 85 86
extern void vorbis_staticbook_destroy(static_codebook *b);
extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source);
extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source);
extern void vorbis_book_clear(codebook *b);

Monty's avatar
 
Monty committed
87
extern float *_book_unquantize(const static_codebook *b,int n,int *map);
Monty's avatar
 
Monty committed
88 89 90 91 92 93 94 95 96
extern float *_book_logdist(const static_codebook *b,float *vals);
extern float _float32_unpack(long val);
extern long   _float32_pack(float val);
extern int  _best(codebook *book, float *a, int step);
extern long _book_maptype1_quantvals(const static_codebook *b);

extern int vorbis_book_besterror(codebook *book,float *a,int step,int addmul);
extern long vorbis_book_codeword(codebook *book,int entry);
extern long vorbis_book_codelen(codebook *book,int entry);
Monty's avatar
 
Monty committed
97 98 99



Monty's avatar
 
Monty committed
100
extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b);
101
extern static_codebook *vorbis_staticbook_unpack(oggpack_buffer *b);
Monty's avatar
 
Monty committed
102

Monty's avatar
 
Monty committed
103
extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b);
Monty's avatar
 
Monty committed
104

Monty's avatar
 
Monty committed
105
extern long vorbis_book_decode(codebook *book, oggpack_buffer *b);
Monty's avatar
Monty committed
106
extern long vorbis_book_decodevs_add(codebook *book, float *a,
107
                                     oggpack_buffer *b,int n);
Monty's avatar
Monty committed
108
extern long vorbis_book_decodev_set(codebook *book, float *a,
109
                                    oggpack_buffer *b,int n);
Monty's avatar
Monty committed
110
extern long vorbis_book_decodev_add(codebook *book, float *a,
111
                                    oggpack_buffer *b,int n);
Monty's avatar
 
Monty committed
112
extern long vorbis_book_decodevv_add(codebook *book, float **a,
Monty's avatar
Monty committed
113
                                     long off,int ch,
114
                                    oggpack_buffer *b,int n);
Monty's avatar
 
Monty committed
115 116 117 118



#endif