range_encoder.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file range_encoder.h
  5. /// \brief Range Encoder
  6. ///
  7. // Authors: Igor Pavlov
  8. // Lasse Collin
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef LZMA_RANGE_ENCODER_H
  12. #define LZMA_RANGE_ENCODER_H
  13. #include "range_common.h"
  14. #include "price.h"
  15. /// Maximum number of symbols that can be put pending into lzma_range_encoder
  16. /// structure between calls to lzma_rc_encode(). For LZMA, 48+5 is enough
  17. /// (match with big distance and length followed by range encoder flush).
  18. #define RC_SYMBOLS_MAX 53
  19. typedef struct {
  20. uint64_t low;
  21. uint64_t cache_size;
  22. uint32_t range;
  23. uint8_t cache;
  24. /// Number of bytes written out by rc_encode() -> rc_shift_low()
  25. uint64_t out_total;
  26. /// Number of symbols in the tables
  27. size_t count;
  28. /// rc_encode()'s position in the tables
  29. size_t pos;
  30. /// Symbols to encode
  31. enum {
  32. RC_BIT_0,
  33. RC_BIT_1,
  34. RC_DIRECT_0,
  35. RC_DIRECT_1,
  36. RC_FLUSH,
  37. } symbols[RC_SYMBOLS_MAX];
  38. /// Probabilities associated with RC_BIT_0 or RC_BIT_1
  39. probability *probs[RC_SYMBOLS_MAX];
  40. } lzma_range_encoder;
  41. static inline void
  42. rc_reset(lzma_range_encoder *rc)
  43. {
  44. rc->low = 0;
  45. rc->cache_size = 1;
  46. rc->range = UINT32_MAX;
  47. rc->cache = 0;
  48. rc->out_total = 0;
  49. rc->count = 0;
  50. rc->pos = 0;
  51. }
  52. static inline void
  53. rc_forget(lzma_range_encoder *rc)
  54. {
  55. // This must not be called when rc_encode() is partially done.
  56. assert(rc->pos == 0);
  57. rc->count = 0;
  58. }
  59. static inline void
  60. rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
  61. {
  62. rc->symbols[rc->count] = bit;
  63. rc->probs[rc->count] = prob;
  64. ++rc->count;
  65. }
  66. static inline void
  67. rc_bittree(lzma_range_encoder *rc, probability *probs,
  68. uint32_t bit_count, uint32_t symbol)
  69. {
  70. uint32_t model_index = 1;
  71. do {
  72. const uint32_t bit = (symbol >> --bit_count) & 1;
  73. rc_bit(rc, &probs[model_index], bit);
  74. model_index = (model_index << 1) + bit;
  75. } while (bit_count != 0);
  76. }
  77. static inline void
  78. rc_bittree_reverse(lzma_range_encoder *rc, probability *probs,
  79. uint32_t bit_count, uint32_t symbol)
  80. {
  81. uint32_t model_index = 1;
  82. do {
  83. const uint32_t bit = symbol & 1;
  84. symbol >>= 1;
  85. rc_bit(rc, &probs[model_index], bit);
  86. model_index = (model_index << 1) + bit;
  87. } while (--bit_count != 0);
  88. }
  89. static inline void
  90. rc_direct(lzma_range_encoder *rc,
  91. uint32_t value, uint32_t bit_count)
  92. {
  93. do {
  94. rc->symbols[rc->count++]
  95. = RC_DIRECT_0 + ((value >> --bit_count) & 1);
  96. } while (bit_count != 0);
  97. }
  98. static inline void
  99. rc_flush(lzma_range_encoder *rc)
  100. {
  101. for (size_t i = 0; i < 5; ++i)
  102. rc->symbols[rc->count++] = RC_FLUSH;
  103. }
  104. static inline bool
  105. rc_shift_low(lzma_range_encoder *rc,
  106. uint8_t *out, size_t *out_pos, size_t out_size)
  107. {
  108. if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000)
  109. || (uint32_t)(rc->low >> 32) != 0) {
  110. do {
  111. if (*out_pos == out_size)
  112. return true;
  113. out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32);
  114. ++*out_pos;
  115. ++rc->out_total;
  116. rc->cache = 0xFF;
  117. } while (--rc->cache_size != 0);
  118. rc->cache = (rc->low >> 24) & 0xFF;
  119. }
  120. ++rc->cache_size;
  121. rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS;
  122. return false;
  123. }
  124. // NOTE: The last two arguments are uint64_t instead of size_t because in
  125. // the dummy version these refer to the size of the whole range-encoded
  126. // output stream, not just to the currently available output buffer space.
  127. static inline bool
  128. rc_shift_low_dummy(uint64_t *low, uint64_t *cache_size, uint8_t *cache,
  129. uint64_t *out_pos, uint64_t out_size)
  130. {
  131. if ((uint32_t)(*low) < (uint32_t)(0xFF000000)
  132. || (uint32_t)(*low >> 32) != 0) {
  133. do {
  134. if (*out_pos == out_size)
  135. return true;
  136. ++*out_pos;
  137. *cache = 0xFF;
  138. } while (--*cache_size != 0);
  139. *cache = (*low >> 24) & 0xFF;
  140. }
  141. ++*cache_size;
  142. *low = (*low & 0x00FFFFFF) << RC_SHIFT_BITS;
  143. return false;
  144. }
  145. static inline bool
  146. rc_encode(lzma_range_encoder *rc,
  147. uint8_t *out, size_t *out_pos, size_t out_size)
  148. {
  149. assert(rc->count <= RC_SYMBOLS_MAX);
  150. while (rc->pos < rc->count) {
  151. // Normalize
  152. if (rc->range < RC_TOP_VALUE) {
  153. if (rc_shift_low(rc, out, out_pos, out_size))
  154. return true;
  155. rc->range <<= RC_SHIFT_BITS;
  156. }
  157. // Encode a bit
  158. switch (rc->symbols[rc->pos]) {
  159. case RC_BIT_0: {
  160. probability prob = *rc->probs[rc->pos];
  161. rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS)
  162. * prob;
  163. prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS;
  164. *rc->probs[rc->pos] = prob;
  165. break;
  166. }
  167. case RC_BIT_1: {
  168. probability prob = *rc->probs[rc->pos];
  169. const uint32_t bound = prob * (rc->range
  170. >> RC_BIT_MODEL_TOTAL_BITS);
  171. rc->low += bound;
  172. rc->range -= bound;
  173. prob -= prob >> RC_MOVE_BITS;
  174. *rc->probs[rc->pos] = prob;
  175. break;
  176. }
  177. case RC_DIRECT_0:
  178. rc->range >>= 1;
  179. break;
  180. case RC_DIRECT_1:
  181. rc->range >>= 1;
  182. rc->low += rc->range;
  183. break;
  184. case RC_FLUSH:
  185. // Prevent further normalizations.
  186. rc->range = UINT32_MAX;
  187. // Flush the last five bytes (see rc_flush()).
  188. do {
  189. if (rc_shift_low(rc, out, out_pos, out_size))
  190. return true;
  191. } while (++rc->pos < rc->count);
  192. // Reset the range encoder so we are ready to continue
  193. // encoding if we weren't finishing the stream.
  194. rc_reset(rc);
  195. return false;
  196. default:
  197. assert(0);
  198. break;
  199. }
  200. ++rc->pos;
  201. }
  202. rc->count = 0;
  203. rc->pos = 0;
  204. return false;
  205. }
  206. static inline bool
  207. rc_encode_dummy(const lzma_range_encoder *rc, uint64_t out_limit)
  208. {
  209. assert(rc->count <= RC_SYMBOLS_MAX);
  210. uint64_t low = rc->low;
  211. uint64_t cache_size = rc->cache_size;
  212. uint32_t range = rc->range;
  213. uint8_t cache = rc->cache;
  214. uint64_t out_pos = rc->out_total;
  215. size_t pos = rc->pos;
  216. while (true) {
  217. // Normalize
  218. if (range < RC_TOP_VALUE) {
  219. if (rc_shift_low_dummy(&low, &cache_size, &cache,
  220. &out_pos, out_limit))
  221. return true;
  222. range <<= RC_SHIFT_BITS;
  223. }
  224. // This check is here because the normalization above must
  225. // be done before flushing the last bytes.
  226. if (pos == rc->count)
  227. break;
  228. // Encode a bit
  229. switch (rc->symbols[pos]) {
  230. case RC_BIT_0: {
  231. probability prob = *rc->probs[pos];
  232. range = (range >> RC_BIT_MODEL_TOTAL_BITS)
  233. * prob;
  234. break;
  235. }
  236. case RC_BIT_1: {
  237. probability prob = *rc->probs[pos];
  238. const uint32_t bound = prob * (range
  239. >> RC_BIT_MODEL_TOTAL_BITS);
  240. low += bound;
  241. range -= bound;
  242. break;
  243. }
  244. case RC_DIRECT_0:
  245. range >>= 1;
  246. break;
  247. case RC_DIRECT_1:
  248. range >>= 1;
  249. low += range;
  250. break;
  251. case RC_FLUSH:
  252. default:
  253. assert(0);
  254. break;
  255. }
  256. ++pos;
  257. }
  258. // Flush the last bytes. This isn't in rc->symbols[] so we do
  259. // it after the above loop to take into account the size of
  260. // the flushing that will be done at the end of the stream.
  261. for (pos = 0; pos < 5; ++pos) {
  262. if (rc_shift_low_dummy(&low, &cache_size,
  263. &cache, &out_pos, out_limit))
  264. return true;
  265. }
  266. return false;
  267. }
  268. static inline uint64_t
  269. rc_pending(const lzma_range_encoder *rc)
  270. {
  271. return rc->cache_size + 5 - 1;
  272. }
  273. #endif