fse_decompress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * FSE : Finite State Entropy decoder
  3. * Copyright (C) 2013-2015, Yann Collet.
  4. *
  5. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following disclaimer
  15. * in the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * This program is free software; you can redistribute it and/or modify it under
  31. * the terms of the GNU General Public License version 2 as published by the
  32. * Free Software Foundation. This program is dual-licensed; you may select
  33. * either version 2 of the GNU General Public License ("GPL") or BSD license
  34. * ("BSD").
  35. *
  36. * You can contact the author at :
  37. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  38. */
  39. /* **************************************************************
  40. * Compiler specifics
  41. ****************************************************************/
  42. #define FORCE_INLINE static __always_inline
  43. /* **************************************************************
  44. * Includes
  45. ****************************************************************/
  46. #include "bitstream.h"
  47. #include "fse.h"
  48. #include <linux/compiler.h>
  49. #include <linux/kernel.h>
  50. #include <linux/string.h> /* memcpy, memset */
  51. /* **************************************************************
  52. * Error Management
  53. ****************************************************************/
  54. #define FSE_isError ERR_isError
  55. #define FSE_STATIC_ASSERT(c) \
  56. { \
  57. enum { FSE_static_assert = 1 / (int)(!!(c)) }; \
  58. } /* use only *after* variable declarations */
  59. /* check and forward error code */
  60. #define CHECK_F(f) \
  61. { \
  62. size_t const e = f; \
  63. if (FSE_isError(e)) \
  64. return e; \
  65. }
  66. /* **************************************************************
  67. * Templates
  68. ****************************************************************/
  69. /*
  70. designed to be included
  71. for type-specific functions (template emulation in C)
  72. Objective is to write these functions only once, for improved maintenance
  73. */
  74. /* safety checks */
  75. #ifndef FSE_FUNCTION_EXTENSION
  76. #error "FSE_FUNCTION_EXTENSION must be defined"
  77. #endif
  78. #ifndef FSE_FUNCTION_TYPE
  79. #error "FSE_FUNCTION_TYPE must be defined"
  80. #endif
  81. /* Function names */
  82. #define FSE_CAT(X, Y) X##Y
  83. #define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y)
  84. #define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y)
  85. /* Function templates */
  86. size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize)
  87. {
  88. void *const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
  89. FSE_DECODE_TYPE *const tableDecode = (FSE_DECODE_TYPE *)(tdPtr);
  90. U16 *symbolNext = (U16 *)workspace;
  91. U32 const maxSV1 = maxSymbolValue + 1;
  92. U32 const tableSize = 1 << tableLog;
  93. U32 highThreshold = tableSize - 1;
  94. /* Sanity Checks */
  95. if (workspaceSize < sizeof(U16) * (FSE_MAX_SYMBOL_VALUE + 1))
  96. return ERROR(tableLog_tooLarge);
  97. if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE)
  98. return ERROR(maxSymbolValue_tooLarge);
  99. if (tableLog > FSE_MAX_TABLELOG)
  100. return ERROR(tableLog_tooLarge);
  101. /* Init, lay down lowprob symbols */
  102. {
  103. FSE_DTableHeader DTableH;
  104. DTableH.tableLog = (U16)tableLog;
  105. DTableH.fastMode = 1;
  106. {
  107. S16 const largeLimit = (S16)(1 << (tableLog - 1));
  108. U32 s;
  109. for (s = 0; s < maxSV1; s++) {
  110. if (normalizedCounter[s] == -1) {
  111. tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
  112. symbolNext[s] = 1;
  113. } else {
  114. if (normalizedCounter[s] >= largeLimit)
  115. DTableH.fastMode = 0;
  116. symbolNext[s] = normalizedCounter[s];
  117. }
  118. }
  119. }
  120. memcpy(dt, &DTableH, sizeof(DTableH));
  121. }
  122. /* Spread symbols */
  123. {
  124. U32 const tableMask = tableSize - 1;
  125. U32 const step = FSE_TABLESTEP(tableSize);
  126. U32 s, position = 0;
  127. for (s = 0; s < maxSV1; s++) {
  128. int i;
  129. for (i = 0; i < normalizedCounter[s]; i++) {
  130. tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
  131. position = (position + step) & tableMask;
  132. while (position > highThreshold)
  133. position = (position + step) & tableMask; /* lowprob area */
  134. }
  135. }
  136. if (position != 0)
  137. return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
  138. }
  139. /* Build Decoding table */
  140. {
  141. U32 u;
  142. for (u = 0; u < tableSize; u++) {
  143. FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
  144. U16 nextState = symbolNext[symbol]++;
  145. tableDecode[u].nbBits = (BYTE)(tableLog - BIT_highbit32((U32)nextState));
  146. tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize);
  147. }
  148. }
  149. return 0;
  150. }
  151. /*-*******************************************************
  152. * Decompression (Byte symbols)
  153. *********************************************************/
  154. size_t FSE_buildDTable_rle(FSE_DTable *dt, BYTE symbolValue)
  155. {
  156. void *ptr = dt;
  157. FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
  158. void *dPtr = dt + 1;
  159. FSE_decode_t *const cell = (FSE_decode_t *)dPtr;
  160. DTableH->tableLog = 0;
  161. DTableH->fastMode = 0;
  162. cell->newState = 0;
  163. cell->symbol = symbolValue;
  164. cell->nbBits = 0;
  165. return 0;
  166. }
  167. size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits)
  168. {
  169. void *ptr = dt;
  170. FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
  171. void *dPtr = dt + 1;
  172. FSE_decode_t *const dinfo = (FSE_decode_t *)dPtr;
  173. const unsigned tableSize = 1 << nbBits;
  174. const unsigned tableMask = tableSize - 1;
  175. const unsigned maxSV1 = tableMask + 1;
  176. unsigned s;
  177. /* Sanity checks */
  178. if (nbBits < 1)
  179. return ERROR(GENERIC); /* min size */
  180. /* Build Decoding Table */
  181. DTableH->tableLog = (U16)nbBits;
  182. DTableH->fastMode = 1;
  183. for (s = 0; s < maxSV1; s++) {
  184. dinfo[s].newState = 0;
  185. dinfo[s].symbol = (BYTE)s;
  186. dinfo[s].nbBits = (BYTE)nbBits;
  187. }
  188. return 0;
  189. }
  190. FORCE_INLINE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt,
  191. const unsigned fast)
  192. {
  193. BYTE *const ostart = (BYTE *)dst;
  194. BYTE *op = ostart;
  195. BYTE *const omax = op + maxDstSize;
  196. BYTE *const olimit = omax - 3;
  197. BIT_DStream_t bitD;
  198. FSE_DState_t state1;
  199. FSE_DState_t state2;
  200. /* Init */
  201. CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
  202. FSE_initDState(&state1, &bitD, dt);
  203. FSE_initDState(&state2, &bitD, dt);
  204. #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
  205. /* 4 symbols per loop */
  206. for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) {
  207. op[0] = FSE_GETSYMBOL(&state1);
  208. if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
  209. BIT_reloadDStream(&bitD);
  210. op[1] = FSE_GETSYMBOL(&state2);
  211. if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
  212. {
  213. if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) {
  214. op += 2;
  215. break;
  216. }
  217. }
  218. op[2] = FSE_GETSYMBOL(&state1);
  219. if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
  220. BIT_reloadDStream(&bitD);
  221. op[3] = FSE_GETSYMBOL(&state2);
  222. }
  223. /* tail */
  224. /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
  225. while (1) {
  226. if (op > (omax - 2))
  227. return ERROR(dstSize_tooSmall);
  228. *op++ = FSE_GETSYMBOL(&state1);
  229. if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
  230. *op++ = FSE_GETSYMBOL(&state2);
  231. break;
  232. }
  233. if (op > (omax - 2))
  234. return ERROR(dstSize_tooSmall);
  235. *op++ = FSE_GETSYMBOL(&state2);
  236. if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
  237. *op++ = FSE_GETSYMBOL(&state1);
  238. break;
  239. }
  240. }
  241. return op - ostart;
  242. }
  243. size_t FSE_decompress_usingDTable(void *dst, size_t originalSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt)
  244. {
  245. const void *ptr = dt;
  246. const FSE_DTableHeader *DTableH = (const FSE_DTableHeader *)ptr;
  247. const U32 fastMode = DTableH->fastMode;
  248. /* select fast mode (static) */
  249. if (fastMode)
  250. return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
  251. return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
  252. }
  253. size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize)
  254. {
  255. const BYTE *const istart = (const BYTE *)cSrc;
  256. const BYTE *ip = istart;
  257. unsigned tableLog;
  258. unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
  259. size_t NCountLength;
  260. FSE_DTable *dt;
  261. short *counting;
  262. size_t spaceUsed32 = 0;
  263. FSE_STATIC_ASSERT(sizeof(FSE_DTable) == sizeof(U32));
  264. dt = (FSE_DTable *)((U32 *)workspace + spaceUsed32);
  265. spaceUsed32 += FSE_DTABLE_SIZE_U32(maxLog);
  266. counting = (short *)((U32 *)workspace + spaceUsed32);
  267. spaceUsed32 += ALIGN(sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1), sizeof(U32)) >> 2;
  268. if ((spaceUsed32 << 2) > workspaceSize)
  269. return ERROR(tableLog_tooLarge);
  270. workspace = (U32 *)workspace + spaceUsed32;
  271. workspaceSize -= (spaceUsed32 << 2);
  272. /* normal FSE decoding mode */
  273. NCountLength = FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
  274. if (FSE_isError(NCountLength))
  275. return NCountLength;
  276. // if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining
  277. // case : NCountLength==cSrcSize */
  278. if (tableLog > maxLog)
  279. return ERROR(tableLog_tooLarge);
  280. ip += NCountLength;
  281. cSrcSize -= NCountLength;
  282. CHECK_F(FSE_buildDTable_wksp(dt, counting, maxSymbolValue, tableLog, workspace, workspaceSize));
  283. return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, dt); /* always return, even if it is an error code */
  284. }