range_encoder.h 6.9 KB

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