1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file range_encoder.h
4 /// \brief Range Encoder
6 // Authors: Igor Pavlov
9 // This file has been put into the public domain.
10 // You can do whatever you want with this file.
12 ///////////////////////////////////////////////////////////////////////////////
14 #ifndef LZMA_RANGE_ENCODER_H
15 #define LZMA_RANGE_ENCODER_H
22 #include "range_common.h"
26 /// Maximum number of symbols that can be put pending into lzma_range_encoder
27 /// structure between calls to lzma_rc_encode(). For LZMA, 52+5 is enough
28 /// (match with big distance and length followed by range encoder flush).
29 #define RC_SYMBOLS_MAX 58
38 /// Number of symbols in the tables
41 /// rc_encode()'s position in the tables
51 } symbols[RC_SYMBOLS_MAX];
53 /// Probabilities associated with RC_BIT_0 or RC_BIT_1
54 probability *probs[RC_SYMBOLS_MAX];
60 rc_reset(lzma_range_encoder *rc)
64 rc->range = UINT32_MAX;
72 rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
74 rc->symbols[rc->count] = bit;
75 rc->probs[rc->count] = prob;
81 rc_bittree(lzma_range_encoder *rc, probability *probs,
82 uint32_t bit_count, uint32_t symbol)
84 uint32_t model_index = 1;
87 const uint32_t bit = (symbol >> --bit_count) & 1;
88 rc_bit(rc, &probs[model_index], bit);
89 model_index = (model_index << 1) + bit;
90 } while (bit_count != 0);
95 rc_bittree_reverse(lzma_range_encoder *rc, probability *probs,
96 uint32_t bit_count, uint32_t symbol)
98 uint32_t model_index = 1;
101 const uint32_t bit = symbol & 1;
103 rc_bit(rc, &probs[model_index], bit);
104 model_index = (model_index << 1) + bit;
105 } while (--bit_count != 0);
110 rc_direct(lzma_range_encoder *rc,
111 uint32_t value, uint32_t bit_count)
114 rc->symbols[rc->count++]
115 = RC_DIRECT_0 + ((value >> --bit_count) & 1);
116 } while (bit_count != 0);
121 rc_flush(lzma_range_encoder *rc)
123 for (size_t i = 0; i < 5; ++i)
124 rc->symbols[rc->count++] = RC_FLUSH;
129 rc_shift_low(lzma_range_encoder *rc,
130 uint8_t *out, size_t *out_pos, size_t out_size)
132 if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000)
133 || (uint32_t)(rc->low >> 32) != 0) {
135 if (*out_pos == out_size)
138 out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32);
142 } while (--rc->cache_size != 0);
144 rc->cache = (rc->low >> 24) & 0xFF;
148 rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS;
155 rc_encode(lzma_range_encoder *rc,
156 uint8_t *out, size_t *out_pos, size_t out_size)
158 assert(rc->count <= RC_SYMBOLS_MAX);
160 while (rc->pos < rc->count) {
162 if (rc->range < RC_TOP_VALUE) {
163 if (rc_shift_low(rc, out, out_pos, out_size))
166 rc->range <<= RC_SHIFT_BITS;
170 switch (rc->symbols[rc->pos]) {
172 probability prob = *rc->probs[rc->pos];
173 rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS)
175 prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS;
176 *rc->probs[rc->pos] = prob;
181 probability prob = *rc->probs[rc->pos];
182 const uint32_t bound = prob * (rc->range
183 >> RC_BIT_MODEL_TOTAL_BITS);
186 prob -= prob >> RC_MOVE_BITS;
187 *rc->probs[rc->pos] = prob;
197 rc->low += rc->range;
201 // Prevent further normalizations.
202 rc->range = UINT32_MAX;
204 // Flush the last five bytes (see rc_flush()).
206 if (rc_shift_low(rc, out, out_pos, out_size))
208 } while (++rc->pos < rc->count);
210 // Reset the range encoder so we are ready to continue
211 // encoding if we weren't finishing the stream.
230 static inline uint64_t
231 rc_pending(const lzma_range_encoder *rc)
233 return rc->cache_size + 5 - 1;