huf.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Huffman coder, part of New Generation Entropy library
  3. * header file
  4. * Copyright (C) 2013-2016, Yann Collet.
  5. *
  6. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * This program is free software; you can redistribute it and/or modify it under
  32. * the terms of the GNU General Public License version 2 as published by the
  33. * Free Software Foundation. This program is dual-licensed; you may select
  34. * either version 2 of the GNU General Public License ("GPL") or BSD license
  35. * ("BSD").
  36. *
  37. * You can contact the author at :
  38. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  39. */
  40. #ifndef HUF_H_298734234
  41. #define HUF_H_298734234
  42. /* *** Dependencies *** */
  43. #include <linux/types.h> /* size_t */
  44. /* *** Tool functions *** */
  45. #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
  46. size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
  47. /* Error Management */
  48. unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
  49. /* *** Advanced function *** */
  50. /** HUF_compress4X_wksp() :
  51. * Same as HUF_compress2(), but uses externally allocated `workSpace`, which must be a table of >= 1024 unsigned */
  52. size_t HUF_compress4X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
  53. size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
  54. /* *** Dependencies *** */
  55. #include "mem.h" /* U32 */
  56. /* *** Constants *** */
  57. #define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
  58. #define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */
  59. #define HUF_SYMBOLVALUE_MAX 255
  60. #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
  61. #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
  62. #error "HUF_TABLELOG_MAX is too large !"
  63. #endif
  64. /* ****************************************
  65. * Static allocation
  66. ******************************************/
  67. /* HUF buffer bounds */
  68. #define HUF_CTABLEBOUND 129
  69. #define HUF_BLOCKBOUND(size) (size + (size >> 8) + 8) /* only true if incompressible pre-filtered with fast heuristic */
  70. #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
  71. /* static allocation of HUF's Compression Table */
  72. #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
  73. U32 name##hb[maxSymbolValue + 1]; \
  74. void *name##hv = &(name##hb); \
  75. HUF_CElt *name = (HUF_CElt *)(name##hv) /* no final ; */
  76. /* static allocation of HUF's DTable */
  77. typedef U32 HUF_DTable;
  78. #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1 << (maxTableLog)))
  79. #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = {((U32)((maxTableLog)-1) * 0x01000001)}
  80. #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = {((U32)(maxTableLog)*0x01000001)}
  81. /* The workspace must have alignment at least 4 and be at least this large */
  82. #define HUF_COMPRESS_WORKSPACE_SIZE (6 << 10)
  83. #define HUF_COMPRESS_WORKSPACE_SIZE_U32 (HUF_COMPRESS_WORKSPACE_SIZE / sizeof(U32))
  84. /* The workspace must have alignment at least 4 and be at least this large */
  85. #define HUF_DECOMPRESS_WORKSPACE_SIZE (3 << 10)
  86. #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
  87. /* ****************************************
  88. * Advanced decompression functions
  89. ******************************************/
  90. size_t HUF_decompress4X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize); /**< decodes RLE and uncompressed */
  91. size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
  92. size_t workspaceSize); /**< considers RLE and uncompressed as errors */
  93. size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
  94. size_t workspaceSize); /**< single-symbol decoder */
  95. size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
  96. size_t workspaceSize); /**< double-symbols decoder */
  97. /* ****************************************
  98. * HUF detailed API
  99. ******************************************/
  100. /*!
  101. HUF_compress() does the following:
  102. 1. count symbol occurrence from source[] into table count[] using FSE_count()
  103. 2. (optional) refine tableLog using HUF_optimalTableLog()
  104. 3. build Huffman table from count using HUF_buildCTable()
  105. 4. save Huffman table to memory buffer using HUF_writeCTable_wksp()
  106. 5. encode the data stream using HUF_compress4X_usingCTable()
  107. The following API allows targeting specific sub-functions for advanced tasks.
  108. For example, it's possible to compress several blocks using the same 'CTable',
  109. or to save and regenerate 'CTable' using external methods.
  110. */
  111. /* FSE_count() : find it within "fse.h" */
  112. unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
  113. typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
  114. size_t HUF_writeCTable_wksp(void *dst, size_t maxDstSize, const HUF_CElt *CTable, unsigned maxSymbolValue, unsigned huffLog, void *workspace, size_t workspaceSize);
  115. size_t HUF_compress4X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable);
  116. typedef enum {
  117. HUF_repeat_none, /**< Cannot use the previous table */
  118. HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1,
  119. 4}X_repeat */
  120. HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */
  121. } HUF_repeat;
  122. /** HUF_compress4X_repeat() :
  123. * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
  124. * If it uses hufTable it does not modify hufTable or repeat.
  125. * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
  126. * If preferRepeat then the old table will always be used if valid. */
  127. size_t HUF_compress4X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
  128. size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat,
  129. int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
  130. /** HUF_buildCTable_wksp() :
  131. * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
  132. * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned.
  133. */
  134. size_t HUF_buildCTable_wksp(HUF_CElt *tree, const U32 *count, U32 maxSymbolValue, U32 maxNbBits, void *workSpace, size_t wkspSize);
  135. /*! HUF_readStats() :
  136. Read compact Huffman tree, saved by HUF_writeCTable().
  137. `huffWeight` is destination buffer.
  138. @return : size read from `src` , or an error Code .
  139. Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
  140. size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize,
  141. void *workspace, size_t workspaceSize);
  142. /** HUF_readCTable() :
  143. * Loading a CTable saved with HUF_writeCTable() */
  144. size_t HUF_readCTable_wksp(HUF_CElt *CTable, unsigned maxSymbolValue, const void *src, size_t srcSize, void *workspace, size_t workspaceSize);
  145. /*
  146. HUF_decompress() does the following:
  147. 1. select the decompression algorithm (X2, X4) based on pre-computed heuristics
  148. 2. build Huffman table from save, using HUF_readDTableXn()
  149. 3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable
  150. */
  151. /** HUF_selectDecoder() :
  152. * Tells which decoder is likely to decode faster,
  153. * based on a set of pre-determined metrics.
  154. * @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
  155. * Assumption : 0 < cSrcSize < dstSize <= 128 KB */
  156. U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize);
  157. size_t HUF_readDTableX2_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize);
  158. size_t HUF_readDTableX4_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize);
  159. size_t HUF_decompress4X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
  160. size_t HUF_decompress4X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
  161. size_t HUF_decompress4X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
  162. /* single stream variants */
  163. size_t HUF_compress1X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
  164. size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
  165. size_t HUF_compress1X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable);
  166. /** HUF_compress1X_repeat() :
  167. * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
  168. * If it uses hufTable it does not modify hufTable or repeat.
  169. * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
  170. * If preferRepeat then the old table will always be used if valid. */
  171. size_t HUF_compress1X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
  172. size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat,
  173. int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
  174. size_t HUF_decompress1X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize);
  175. size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
  176. size_t workspaceSize); /**< single-symbol decoder */
  177. size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
  178. size_t workspaceSize); /**< double-symbols decoder */
  179. size_t HUF_decompress1X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize,
  180. const HUF_DTable *DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */
  181. size_t HUF_decompress1X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
  182. size_t HUF_decompress1X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
  183. #endif /* HUF_H_298734234 */