lzham_symbol_codec.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // File: lzham_symbol_codec.h
  2. // See Copyright Notice and license at the end of include/lzham.h
  3. #pragma once
  4. #include "lzham_prefix_coding.h"
  5. namespace lzham
  6. {
  7. class symbol_codec;
  8. class adaptive_arith_data_model;
  9. const uint cSymbolCodecArithMinLen = 0x01000000U;
  10. const uint cSymbolCodecArithMaxLen = 0xFFFFFFFFU;
  11. const uint cSymbolCodecArithProbBits = 11;
  12. const uint cSymbolCodecArithProbScale = 1 << cSymbolCodecArithProbBits;
  13. const uint cSymbolCodecArithProbHalfScale = 1 << (cSymbolCodecArithProbBits - 1);
  14. const uint cSymbolCodecArithProbMoveBits = 5;
  15. typedef uint64 bit_cost_t;
  16. const uint32 cBitCostScaleShift = 24;
  17. const uint32 cBitCostScale = (1U << cBitCostScaleShift);
  18. const bit_cost_t cBitCostMax = UINT64_MAX;
  19. inline bit_cost_t convert_to_scaled_bitcost(uint bits) { LZHAM_ASSERT(bits <= 255); uint32 scaled_bits = bits << cBitCostScaleShift; return static_cast<bit_cost_t>(scaled_bits); }
  20. extern uint32 g_prob_cost[cSymbolCodecArithProbScale];
  21. class raw_quasi_adaptive_huffman_data_model
  22. {
  23. public:
  24. raw_quasi_adaptive_huffman_data_model(bool encoding = false, uint total_syms = 0, uint max_update_interval = 0, uint adapt_rate = 0);
  25. raw_quasi_adaptive_huffman_data_model(const raw_quasi_adaptive_huffman_data_model& other);
  26. ~raw_quasi_adaptive_huffman_data_model();
  27. bool assign(const raw_quasi_adaptive_huffman_data_model& rhs);
  28. raw_quasi_adaptive_huffman_data_model& operator= (const raw_quasi_adaptive_huffman_data_model& rhs);
  29. void clear();
  30. bool init2(bool encoding, uint total_syms, uint max_update_interval, uint adapt_rate, const uint16 *pInitial_sym_freq);
  31. bool reset();
  32. inline uint get_total_syms() const { return m_total_syms; }
  33. void rescale();
  34. void reset_update_rate();
  35. bool update_sym(uint sym);
  36. inline bit_cost_t get_cost(uint sym) const { return convert_to_scaled_bitcost(m_code_sizes[sym]); }
  37. public:
  38. lzham::vector<uint16> m_initial_sym_freq;
  39. lzham::vector<uint16> m_sym_freq;
  40. lzham::vector<uint16> m_codes;
  41. lzham::vector<uint8> m_code_sizes;
  42. prefix_coding::decoder_tables* m_pDecode_tables;
  43. uint m_total_syms;
  44. uint m_max_cycle;
  45. uint m_update_cycle;
  46. uint m_symbols_until_update;
  47. uint m_total_count;
  48. uint8 m_decoder_table_bits;
  49. uint16 m_max_update_interval; // def=16, typical range 12-128, controls the max interval between table updates, higher=longer max interval (faster decode/lower ratio)
  50. uint16 m_adapt_rate; // def=10, 8 or higher, scaled by 8, controls the slowing of the update update freq, higher=more rapid slowing (faster decode/lower ratio)
  51. bool m_encoding;
  52. bool update_tables(int force_update_cycle = -1, bool sym_freq_all_ones = false);
  53. friend class symbol_codec;
  54. };
  55. struct quasi_adaptive_huffman_data_model : public raw_quasi_adaptive_huffman_data_model
  56. {
  57. #if LZHAM_64BIT_POINTERS
  58. // Ensures sizeof(quasi_adaptive_huffman_data_model) is 128 bytes on x64 (it's 64 on x86).
  59. char m_unused_alignment[128 - sizeof(raw_quasi_adaptive_huffman_data_model)];
  60. #endif
  61. };
  62. class adaptive_bit_model
  63. {
  64. public:
  65. inline adaptive_bit_model() { clear(); }
  66. adaptive_bit_model(float prob0);
  67. adaptive_bit_model(const adaptive_bit_model& other);
  68. inline adaptive_bit_model& operator= (const adaptive_bit_model& rhs) { m_bit_0_prob = rhs.m_bit_0_prob; return *this; }
  69. inline void clear() { m_bit_0_prob = 1U << (cSymbolCodecArithProbBits - 1); }
  70. void set_probability_0(float prob0);
  71. inline void update(uint bit)
  72. {
  73. if (!bit)
  74. m_bit_0_prob += ((cSymbolCodecArithProbScale - m_bit_0_prob) >> cSymbolCodecArithProbMoveBits);
  75. else
  76. m_bit_0_prob -= (m_bit_0_prob >> cSymbolCodecArithProbMoveBits);
  77. LZHAM_ASSERT(m_bit_0_prob >= 1);
  78. LZHAM_ASSERT(m_bit_0_prob < cSymbolCodecArithProbScale);
  79. }
  80. inline bit_cost_t get_cost(uint bit) const { return g_prob_cost[bit ? (cSymbolCodecArithProbScale - m_bit_0_prob) : m_bit_0_prob]; }
  81. public:
  82. uint16 m_bit_0_prob;
  83. friend class symbol_codec;
  84. friend class adaptive_arith_data_model;
  85. };
  86. // This class is not actually used by LZHAM - it's only here for comparison/experimental purposes.
  87. class adaptive_arith_data_model
  88. {
  89. public:
  90. adaptive_arith_data_model(bool encoding = true, uint total_syms = 0);
  91. adaptive_arith_data_model(const adaptive_arith_data_model& other);
  92. ~adaptive_arith_data_model();
  93. adaptive_arith_data_model& operator= (const adaptive_arith_data_model& rhs);
  94. void clear();
  95. bool init(bool encoding, uint total_syms);
  96. bool init(bool encoding, uint total_syms, bool fast_encoding) { LZHAM_NOTE_UNUSED(fast_encoding); return init(encoding, total_syms); }
  97. void reset();
  98. void reset_update_rate();
  99. bool update(uint sym);
  100. uint get_total_syms() const { return m_total_syms; }
  101. bit_cost_t get_cost(uint sym) const;
  102. public:
  103. uint m_total_syms;
  104. typedef lzham::vector<adaptive_bit_model> adaptive_bit_model_vector;
  105. adaptive_bit_model_vector m_probs;
  106. friend class symbol_codec;
  107. };
  108. #if LZHAM_CPU_HAS_64BIT_REGISTERS
  109. #define LZHAM_SYMBOL_CODEC_USE_64_BIT_BUFFER 1
  110. #else
  111. #define LZHAM_SYMBOL_CODEC_USE_64_BIT_BUFFER 0
  112. #endif
  113. class symbol_codec
  114. {
  115. public:
  116. symbol_codec();
  117. void reset();
  118. // clear() is like reset(), except it also frees all memory.
  119. void clear();
  120. // Encoding
  121. bool start_encoding(uint expected_file_size);
  122. bool encode_bits(uint bits, uint num_bits);
  123. bool encode_arith_init();
  124. bool encode_align_to_byte();
  125. bool encode(uint sym, quasi_adaptive_huffman_data_model& model);
  126. bool encode(uint bit, adaptive_bit_model& model, bool update_model = true);
  127. bool encode(uint sym, adaptive_arith_data_model& model);
  128. inline uint encode_get_total_bits_written() const { return m_total_bits_written; }
  129. bool stop_encoding(bool support_arith);
  130. const lzham::vector<uint8>& get_encoding_buf() const { return m_output_buf; }
  131. lzham::vector<uint8>& get_encoding_buf() { return m_output_buf; }
  132. // Decoding
  133. typedef void (*need_bytes_func_ptr)(size_t num_bytes_consumed, void *pPrivate_data, const uint8* &pBuf, size_t &buf_size, bool &eof_flag);
  134. bool start_decoding(const uint8* pBuf, size_t buf_size, bool eof_flag = true, need_bytes_func_ptr pNeed_bytes_func = NULL, void *pPrivate_data = NULL);
  135. inline void decode_set_input_buffer(const uint8* pBuf, size_t buf_size, const uint8* pBuf_next, bool eof_flag)
  136. {
  137. m_pDecode_buf = pBuf;
  138. m_pDecode_buf_next = pBuf_next;
  139. m_decode_buf_size = buf_size;
  140. m_pDecode_buf_end = pBuf + buf_size;
  141. m_decode_buf_eof = eof_flag;
  142. }
  143. inline uint64 decode_get_bytes_consumed() const { return m_pDecode_buf_next - m_pDecode_buf; }
  144. inline uint64 decode_get_bits_remaining() const { return ((m_pDecode_buf_end - m_pDecode_buf_next) << 3) + m_bit_count; }
  145. void start_arith_decoding();
  146. uint decode_bits(uint num_bits);
  147. uint decode_peek_bits(uint num_bits);
  148. void decode_remove_bits(uint num_bits);
  149. void decode_align_to_byte();
  150. int decode_remove_byte_from_bit_buf();
  151. uint decode(quasi_adaptive_huffman_data_model& model);
  152. uint decode(adaptive_bit_model& model, bool update_model = true);
  153. uint decode(adaptive_arith_data_model& model);
  154. uint64 stop_decoding();
  155. uint get_total_model_updates() const { return m_total_model_updates; }
  156. public:
  157. const uint8* m_pDecode_buf;
  158. const uint8* m_pDecode_buf_next;
  159. const uint8* m_pDecode_buf_end;
  160. size_t m_decode_buf_size;
  161. bool m_decode_buf_eof;
  162. need_bytes_func_ptr m_pDecode_need_bytes_func;
  163. void* m_pDecode_private_data;
  164. #if LZHAM_SYMBOL_CODEC_USE_64_BIT_BUFFER
  165. typedef uint64 bit_buf_t;
  166. enum { cBitBufSize = 64 };
  167. #else
  168. typedef uint32 bit_buf_t;
  169. enum { cBitBufSize = 32 };
  170. #endif
  171. bit_buf_t m_bit_buf;
  172. int m_bit_count;
  173. uint m_total_model_updates;
  174. lzham::vector<uint8> m_output_buf;
  175. lzham::vector<uint8> m_arith_output_buf;
  176. struct output_symbol
  177. {
  178. uint m_bits;
  179. enum
  180. {
  181. cArithSym = -1,
  182. cAlignToByteSym = -2,
  183. cArithInit = -3
  184. };
  185. int16 m_num_bits;
  186. uint16 m_arith_prob0;
  187. };
  188. lzham::vector<output_symbol> m_output_syms;
  189. uint m_total_bits_written;
  190. uint m_arith_base;
  191. uint m_arith_value;
  192. uint m_arith_length;
  193. uint m_arith_total_bits;
  194. quasi_adaptive_huffman_data_model* m_pSaved_huff_model;
  195. void* m_pSaved_model;
  196. uint m_saved_node_index;
  197. bool put_bits_init(uint expected_size);
  198. bool record_put_bits(uint bits, uint num_bits);
  199. void arith_propagate_carry();
  200. bool arith_renorm_enc_interval();
  201. void arith_start_encoding();
  202. bool arith_stop_encoding();
  203. bool put_bits(uint bits, uint num_bits);
  204. bool put_bits_align_to_byte();
  205. bool flush_bits();
  206. bool assemble_output_buf();
  207. uint get_bits(uint num_bits);
  208. void remove_bits(uint num_bits);
  209. void decode_need_bytes();
  210. enum
  211. {
  212. cNull,
  213. cEncoding,
  214. cDecoding
  215. } m_mode;
  216. };
  217. // Optional macros for faster decompression. These macros implement the symbol_codec class's decode functionality.
  218. // This is hard to debug (and just plain ugly), but using these macros eliminate function calls, and they place the most important
  219. // member variables on the stack so they're hopefully put in registers (avoiding horrible load hit stores on some CPU's).
  220. // The user must define the LZHAM_DECODE_NEEDS_BYTES macro, which is invoked when the decode buffer is exhausted.
  221. #define LZHAM_SYMBOL_CODEC_DECODE_DECLARE(codec) \
  222. uint arith_value = 0; \
  223. uint arith_length = 0; \
  224. symbol_codec::bit_buf_t bit_buf = 0; \
  225. int bit_count = 0; \
  226. const uint8* pDecode_buf_next = NULL;
  227. #define LZHAM_SYMBOL_CODEC_DECODE_BEGIN(codec) \
  228. arith_value = codec.m_arith_value; \
  229. arith_length = codec.m_arith_length; \
  230. bit_buf = codec.m_bit_buf; \
  231. bit_count = codec.m_bit_count; \
  232. pDecode_buf_next = codec.m_pDecode_buf_next;
  233. #define LZHAM_SYMBOL_CODEC_DECODE_END(codec) \
  234. codec.m_arith_value = arith_value; \
  235. codec.m_arith_length = arith_length; \
  236. codec.m_bit_buf = bit_buf; \
  237. codec.m_bit_count = bit_count; \
  238. codec.m_pDecode_buf_next = pDecode_buf_next;
  239. // The user must declare the LZHAM_DECODE_NEEDS_BYTES macro.
  240. #define LZHAM_SYMBOL_CODEC_DECODE_GET_BITS(codec, result, num_bits) \
  241. { \
  242. while (LZHAM_BUILTIN_EXPECT(bit_count < (int)(num_bits), 0)) \
  243. { \
  244. uint r; \
  245. if (LZHAM_BUILTIN_EXPECT(pDecode_buf_next == codec.m_pDecode_buf_end, 0)) \
  246. { \
  247. if (LZHAM_BUILTIN_EXPECT(!codec.m_decode_buf_eof, 1)) \
  248. { \
  249. LZHAM_SYMBOL_CODEC_DECODE_END(codec) \
  250. LZHAM_DECODE_NEEDS_BYTES \
  251. LZHAM_SYMBOL_CODEC_DECODE_BEGIN(codec) \
  252. } \
  253. r = 0; \
  254. if (LZHAM_BUILTIN_EXPECT(pDecode_buf_next < codec.m_pDecode_buf_end, 1)) r = *pDecode_buf_next++; \
  255. } \
  256. else \
  257. r = *pDecode_buf_next++; \
  258. bit_count += 8; \
  259. bit_buf |= (static_cast<symbol_codec::bit_buf_t>(r) << (symbol_codec::cBitBufSize - bit_count)); \
  260. } \
  261. result = (num_bits) ? static_cast<uint>(bit_buf >> (symbol_codec::cBitBufSize - (num_bits))) : 0; \
  262. bit_buf <<= (num_bits); \
  263. bit_count -= (num_bits); \
  264. }
  265. #define LZHAM_SYMBOL_CODEC_DECODE_ARITH_BIT(codec, result, model) \
  266. { \
  267. adaptive_bit_model *pModel; \
  268. pModel = &model; \
  269. while (LZHAM_BUILTIN_EXPECT(arith_length < cSymbolCodecArithMinLen, 0)) \
  270. { \
  271. uint c; codec.m_pSaved_model = pModel; \
  272. LZHAM_SYMBOL_CODEC_DECODE_GET_BITS(codec, c, 8); \
  273. pModel = static_cast<adaptive_bit_model*>(codec.m_pSaved_model); \
  274. arith_value = (arith_value << 8) | c; \
  275. arith_length <<= 8; \
  276. } \
  277. uint x = pModel->m_bit_0_prob * (arith_length >> cSymbolCodecArithProbBits); \
  278. result = (arith_value >= x); \
  279. if (!result) \
  280. { \
  281. pModel->m_bit_0_prob += ((cSymbolCodecArithProbScale - pModel->m_bit_0_prob) >> cSymbolCodecArithProbMoveBits); \
  282. arith_length = x; \
  283. } \
  284. else \
  285. { \
  286. pModel->m_bit_0_prob -= (pModel->m_bit_0_prob >> cSymbolCodecArithProbMoveBits); \
  287. arith_value -= x; \
  288. arith_length -= x; \
  289. } \
  290. }
  291. #define LZHAM_SYMBOL_CODEC_DECODE_ADAPTIVE_ARITHMETIC(codec, result, model) \
  292. { \
  293. adaptive_arith_data_model *pArith_data_model; \
  294. pArith_data_model = &model; \
  295. uint node_index; \
  296. node_index = 1; \
  297. do \
  298. { \
  299. while (LZHAM_BUILTIN_EXPECT(arith_length < cSymbolCodecArithMinLen, 0)) \
  300. { \
  301. uint c; codec.m_saved_node_index = node_index; codec.m_pSaved_model = pArith_data_model; \
  302. LZHAM_SYMBOL_CODEC_DECODE_GET_BITS(codec, c, 8); \
  303. node_index = codec.m_saved_node_index; pArith_data_model = static_cast<adaptive_arith_data_model *>(codec.m_pSaved_model); \
  304. arith_value = (arith_value << 8) | c; \
  305. arith_length <<= 8; \
  306. } \
  307. adaptive_bit_model *pBit_model; pBit_model = &pArith_data_model->m_probs[node_index]; \
  308. uint x = pBit_model->m_bit_0_prob * (arith_length >> cSymbolCodecArithProbBits); \
  309. uint bit; bit = (arith_value >= x); \
  310. if (!bit) \
  311. { \
  312. pBit_model->m_bit_0_prob += ((cSymbolCodecArithProbScale - pBit_model->m_bit_0_prob) >> cSymbolCodecArithProbMoveBits); \
  313. arith_length = x; \
  314. } \
  315. else \
  316. { \
  317. pBit_model->m_bit_0_prob -= (pBit_model->m_bit_0_prob >> cSymbolCodecArithProbMoveBits); \
  318. arith_value -= x; \
  319. arith_length -= x; \
  320. } \
  321. node_index = (node_index << 1) + bit; \
  322. } while (node_index < pArith_data_model->m_total_syms); \
  323. result = node_index - pArith_data_model->m_total_syms; \
  324. }
  325. #if LZHAM_SYMBOL_CODEC_USE_64_BIT_BUFFER
  326. #define LZHAM_SYMBOL_CODEC_DECODE_ADAPTIVE_HUFFMAN(codec, result, model) \
  327. { \
  328. quasi_adaptive_huffman_data_model* pModel; const prefix_coding::decoder_tables* pTables; \
  329. pModel = &model; pTables = model.m_pDecode_tables; \
  330. if (LZHAM_BUILTIN_EXPECT(bit_count < 24, 0)) \
  331. { \
  332. uint c; \
  333. pDecode_buf_next += sizeof(uint32); \
  334. if (LZHAM_BUILTIN_EXPECT(pDecode_buf_next >= codec.m_pDecode_buf_end, 0)) \
  335. { \
  336. pDecode_buf_next -= sizeof(uint32); \
  337. while (bit_count < 24) \
  338. { \
  339. if (!codec.m_decode_buf_eof) \
  340. { \
  341. codec.m_pSaved_huff_model = pModel; \
  342. LZHAM_SYMBOL_CODEC_DECODE_END(codec) \
  343. LZHAM_DECODE_NEEDS_BYTES \
  344. LZHAM_SYMBOL_CODEC_DECODE_BEGIN(codec) \
  345. pModel = codec.m_pSaved_huff_model; pTables = pModel->m_pDecode_tables; \
  346. } \
  347. c = 0; if (pDecode_buf_next < codec.m_pDecode_buf_end) c = *pDecode_buf_next++; \
  348. bit_count += 8; \
  349. bit_buf |= (static_cast<symbol_codec::bit_buf_t>(c) << (symbol_codec::cBitBufSize - bit_count)); \
  350. } \
  351. } \
  352. else \
  353. { \
  354. c = LZHAM_READ_BIG_ENDIAN_UINT32(pDecode_buf_next - sizeof(uint32)); \
  355. bit_count += 32; \
  356. bit_buf |= (static_cast<symbol_codec::bit_buf_t>(c) << (symbol_codec::cBitBufSize - bit_count)); \
  357. } \
  358. } \
  359. uint k = static_cast<uint>((bit_buf >> (symbol_codec::cBitBufSize - 16)) + 1); \
  360. uint len; \
  361. if (LZHAM_BUILTIN_EXPECT(k <= pTables->m_table_max_code, 1)) \
  362. { \
  363. uint32 t = pTables->m_lookup[bit_buf >> (symbol_codec::cBitBufSize - pTables->m_table_bits)]; \
  364. result = t & UINT16_MAX; \
  365. len = t >> 16; \
  366. } \
  367. else \
  368. { \
  369. len = pTables->m_decode_start_code_size; \
  370. for ( ; ; ) \
  371. { \
  372. if (LZHAM_BUILTIN_EXPECT(k <= pTables->m_max_codes[len - 1], 0)) \
  373. break; \
  374. len++; \
  375. } \
  376. int val_ptr = pTables->m_val_ptrs[len - 1] + static_cast<int>(bit_buf >> (symbol_codec::cBitBufSize - len)); \
  377. if (((uint)val_ptr >= pModel->m_total_syms)) val_ptr = 0; \
  378. result = pTables->m_sorted_symbol_order[val_ptr]; \
  379. } \
  380. bit_buf <<= len; \
  381. bit_count -= len; \
  382. uint freq = pModel->m_sym_freq[result]; \
  383. freq++; \
  384. pModel->m_sym_freq[result] = static_cast<uint16>(freq); \
  385. LZHAM_ASSERT(freq <= UINT16_MAX); \
  386. if (LZHAM_BUILTIN_EXPECT(--pModel->m_symbols_until_update == 0, 0)) \
  387. { \
  388. pModel->update_tables(); \
  389. } \
  390. }
  391. #else
  392. #define LZHAM_SYMBOL_CODEC_DECODE_ADAPTIVE_HUFFMAN(codec, result, model) \
  393. { \
  394. quasi_adaptive_huffman_data_model* pModel; const prefix_coding::decoder_tables* pTables; \
  395. pModel = &model; pTables = model.m_pDecode_tables; \
  396. while (LZHAM_BUILTIN_EXPECT(bit_count < (symbol_codec::cBitBufSize - 8), 1)) \
  397. { \
  398. uint c; \
  399. if (LZHAM_BUILTIN_EXPECT(pDecode_buf_next == codec.m_pDecode_buf_end, 0)) \
  400. { \
  401. if (LZHAM_BUILTIN_EXPECT(!codec.m_decode_buf_eof, 1)) \
  402. { \
  403. codec.m_pSaved_huff_model = pModel; \
  404. LZHAM_SYMBOL_CODEC_DECODE_END(codec) \
  405. LZHAM_DECODE_NEEDS_BYTES \
  406. LZHAM_SYMBOL_CODEC_DECODE_BEGIN(codec) \
  407. pModel = codec.m_pSaved_huff_model; pTables = pModel->m_pDecode_tables; \
  408. } \
  409. c = 0; if (LZHAM_BUILTIN_EXPECT(pDecode_buf_next < codec.m_pDecode_buf_end, 1)) c = *pDecode_buf_next++; \
  410. } \
  411. else \
  412. c = *pDecode_buf_next++; \
  413. bit_count += 8; \
  414. bit_buf |= (static_cast<symbol_codec::bit_buf_t>(c) << (symbol_codec::cBitBufSize - bit_count)); \
  415. } \
  416. uint k = static_cast<uint>((bit_buf >> (symbol_codec::cBitBufSize - 16)) + 1); \
  417. uint len; \
  418. if (LZHAM_BUILTIN_EXPECT(k <= pTables->m_table_max_code, 1)) \
  419. { \
  420. uint32 t = pTables->m_lookup[bit_buf >> (symbol_codec::cBitBufSize - pTables->m_table_bits)]; \
  421. result = t & UINT16_MAX; \
  422. len = t >> 16; \
  423. } \
  424. else \
  425. { \
  426. len = pTables->m_decode_start_code_size; \
  427. for ( ; ; ) \
  428. { \
  429. if (LZHAM_BUILTIN_EXPECT(k <= pTables->m_max_codes[len - 1], 0)) \
  430. break; \
  431. len++; \
  432. } \
  433. int val_ptr = pTables->m_val_ptrs[len - 1] + static_cast<int>(bit_buf >> (symbol_codec::cBitBufSize - len)); \
  434. if (LZHAM_BUILTIN_EXPECT(((uint)val_ptr >= pModel->m_total_syms), 0)) val_ptr = 0; \
  435. result = pTables->m_sorted_symbol_order[val_ptr]; \
  436. } \
  437. bit_buf <<= len; \
  438. bit_count -= len; \
  439. uint freq = pModel->m_sym_freq[result]; \
  440. freq++; \
  441. pModel->m_sym_freq[result] = static_cast<uint16>(freq); \
  442. LZHAM_ASSERT(freq <= UINT16_MAX); \
  443. if (LZHAM_BUILTIN_EXPECT(--pModel->m_symbols_until_update == 0, 0)) \
  444. { \
  445. pModel->update_tables(); \
  446. } \
  447. }
  448. #endif
  449. #define LZHAM_SYMBOL_CODEC_DECODE_ALIGN_TO_BYTE(codec) if (bit_count & 7) { int dummy_result; LZHAM_NOTE_UNUSED(dummy_result); LZHAM_SYMBOL_CODEC_DECODE_GET_BITS(codec, dummy_result, bit_count & 7); }
  450. #define LZHAM_SYMBOL_CODEC_DECODE_REMOVE_BYTE_FROM_BIT_BUF(codec, result) \
  451. { \
  452. result = -1; \
  453. if (bit_count >= 8) \
  454. { \
  455. result = static_cast<int>(bit_buf >> (symbol_codec::cBitBufSize - 8)); \
  456. bit_buf <<= 8; \
  457. bit_count -= 8; \
  458. } \
  459. }
  460. #define LZHAM_SYMBOL_CODEC_DECODE_ARITH_START(codec) \
  461. { \
  462. for ( arith_value = 0, arith_length = 0; arith_length < 4; ++arith_length ) \
  463. { \
  464. uint val; LZHAM_SYMBOL_CODEC_DECODE_GET_BITS(codec, val, 8); \
  465. arith_value = (arith_value << 8) | val; \
  466. } \
  467. arith_length = cSymbolCodecArithMaxLen; \
  468. }
  469. } // namespace lzham