range_encoder.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, 52+5 is enough
  19. /// (match with big distance and length followed by range encoder flush).
  20. #define RC_SYMBOLS_MAX 58
  21. typedef struct {
  22. uint64_t low;
  23. uint64_t cache_size;
  24. uint32_t range;
  25. uint8_t cache;
  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->count = 0;
  49. rc->pos = 0;
  50. }
  51. static inline void
  52. rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
  53. {
  54. rc->symbols[rc->count] = bit;
  55. rc->probs[rc->count] = prob;
  56. ++rc->count;
  57. }
  58. static inline void
  59. rc_bittree(lzma_range_encoder *rc, probability *probs,
  60. uint32_t bit_count, uint32_t symbol)
  61. {
  62. uint32_t model_index = 1;
  63. do {
  64. const uint32_t bit = (symbol >> --bit_count) & 1;
  65. rc_bit(rc, &probs[model_index], bit);
  66. model_index = (model_index << 1) + bit;
  67. } while (bit_count != 0);
  68. }
  69. static inline void
  70. rc_bittree_reverse(lzma_range_encoder *rc, probability *probs,
  71. uint32_t bit_count, uint32_t symbol)
  72. {
  73. uint32_t model_index = 1;
  74. do {
  75. const uint32_t bit = symbol & 1;
  76. symbol >>= 1;
  77. rc_bit(rc, &probs[model_index], bit);
  78. model_index = (model_index << 1) + bit;
  79. } while (--bit_count != 0);
  80. }
  81. static inline void
  82. rc_direct(lzma_range_encoder *rc,
  83. uint32_t value, uint32_t bit_count)
  84. {
  85. do {
  86. rc->symbols[rc->count++]
  87. = RC_DIRECT_0 + ((value >> --bit_count) & 1);
  88. } while (bit_count != 0);
  89. }
  90. static inline void
  91. rc_flush(lzma_range_encoder *rc)
  92. {
  93. for (size_t i = 0; i < 5; ++i)
  94. rc->symbols[rc->count++] = RC_FLUSH;
  95. }
  96. static inline bool
  97. rc_shift_low(lzma_range_encoder *rc,
  98. uint8_t *out, size_t *out_pos, size_t out_size)
  99. {
  100. if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000)
  101. || (uint32_t)(rc->low >> 32) != 0) {
  102. do {
  103. if (*out_pos == out_size)
  104. return true;
  105. out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32);
  106. ++*out_pos;
  107. rc->cache = 0xFF;
  108. } while (--rc->cache_size != 0);
  109. rc->cache = (rc->low >> 24) & 0xFF;
  110. }
  111. ++rc->cache_size;
  112. rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS;
  113. return false;
  114. }
  115. static inline bool
  116. rc_encode(lzma_range_encoder *rc,
  117. uint8_t *out, size_t *out_pos, size_t out_size)
  118. {
  119. assert(rc->count <= RC_SYMBOLS_MAX);
  120. while (rc->pos < rc->count) {
  121. // Normalize
  122. if (rc->range < RC_TOP_VALUE) {
  123. if (rc_shift_low(rc, out, out_pos, out_size))
  124. return true;
  125. rc->range <<= RC_SHIFT_BITS;
  126. }
  127. // Encode a bit
  128. switch (rc->symbols[rc->pos]) {
  129. case RC_BIT_0: {
  130. probability prob = *rc->probs[rc->pos];
  131. rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS)
  132. * prob;
  133. prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS;
  134. *rc->probs[rc->pos] = prob;
  135. break;
  136. }
  137. case RC_BIT_1: {
  138. probability prob = *rc->probs[rc->pos];
  139. const uint32_t bound = prob * (rc->range
  140. >> RC_BIT_MODEL_TOTAL_BITS);
  141. rc->low += bound;
  142. rc->range -= bound;
  143. prob -= prob >> RC_MOVE_BITS;
  144. *rc->probs[rc->pos] = prob;
  145. break;
  146. }
  147. case RC_DIRECT_0:
  148. rc->range >>= 1;
  149. break;
  150. case RC_DIRECT_1:
  151. rc->range >>= 1;
  152. rc->low += rc->range;
  153. break;
  154. case RC_FLUSH:
  155. // Prevent further normalizations.
  156. rc->range = UINT32_MAX;
  157. // Flush the last five bytes (see rc_flush()).
  158. do {
  159. if (rc_shift_low(rc, out, out_pos, out_size))
  160. return true;
  161. } while (++rc->pos < rc->count);
  162. // Reset the range encoder so we are ready to continue
  163. // encoding if we weren't finishing the stream.
  164. rc_reset(rc);
  165. return false;
  166. default:
  167. assert(0);
  168. break;
  169. }
  170. ++rc->pos;
  171. }
  172. rc->count = 0;
  173. rc->pos = 0;
  174. return false;
  175. }
  176. static inline uint64_t
  177. rc_pending(const lzma_range_encoder *rc)
  178. {
  179. return rc->cache_size + 5 - 1;
  180. }
  181. #endif