localcodebook.h 6.0 KB

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