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
17 #include "range_common.h"
21 /// Maximum number of symbols that can be put pending into lzma_range_encoder
22 /// structure between calls to lzma_rc_encode(). For LZMA, 52+5 is enough
23 /// (match with big distance and length followed by range encoder flush).
24 #define RC_SYMBOLS_MAX 58
33 /// Number of symbols in the tables
36 /// rc_encode()'s position in the tables
46 } symbols[RC_SYMBOLS_MAX];
48 /// Probabilities associated with RC_BIT_0 or RC_BIT_1
49 probability *probs[RC_SYMBOLS_MAX];
55 rc_reset(lzma_range_encoder *rc)
59 rc->range = UINT32_MAX;
67 rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
69 rc->symbols[rc->count] = bit;
70 rc->probs[rc->count] = prob;
76 rc_bittree(lzma_range_encoder *rc, probability *probs,
77 uint32_t bit_count, uint32_t symbol)
79 uint32_t model_index = 1;
82 const uint32_t bit = (symbol >> --bit_count) & 1;
83 rc_bit(rc, &probs[model_index], bit);
84 model_index = (model_index << 1) + bit;
85 } while (bit_count != 0);
90 rc_bittree_reverse(lzma_range_encoder *rc, probability *probs,
91 uint32_t bit_count, uint32_t symbol)
93 uint32_t model_index = 1;
96 const uint32_t bit = symbol & 1;
98 rc_bit(rc, &probs[model_index], bit);
99 model_index = (model_index << 1) + bit;
100 } while (--bit_count != 0);
105 rc_direct(lzma_range_encoder *rc,
106 uint32_t value, uint32_t bit_count)
109 rc->symbols[rc->count++]
110 = RC_DIRECT_0 + ((value >> --bit_count) & 1);
111 } while (bit_count != 0);
116 rc_flush(lzma_range_encoder *rc)
119 for (i = 0; i < 5; ++i)
120 rc->symbols[rc->count++] = RC_FLUSH;
125 rc_shift_low(lzma_range_encoder *rc,
126 uint8_t *out, size_t *out_pos, size_t out_size)
128 if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000)
129 || (uint32_t)(rc->low >> 32) != 0) {
131 if (*out_pos == out_size)
134 out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32);
138 } while (--rc->cache_size != 0);
140 rc->cache = (rc->low >> 24) & 0xFF;
144 rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS;
151 rc_encode(lzma_range_encoder *rc,
152 uint8_t *out, size_t *out_pos, size_t out_size)
154 assert(rc->count <= RC_SYMBOLS_MAX);
156 while (rc->pos < rc->count) {
158 if (rc->range < RC_TOP_VALUE) {
159 if (rc_shift_low(rc, out, out_pos, out_size))
162 rc->range <<= RC_SHIFT_BITS;
166 switch (rc->symbols[rc->pos]) {
168 probability prob = *rc->probs[rc->pos];
169 rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS)
171 prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS;
172 *rc->probs[rc->pos] = prob;
177 probability prob = *rc->probs[rc->pos];
178 const uint32_t bound = prob * (rc->range
179 >> RC_BIT_MODEL_TOTAL_BITS);
182 prob -= prob >> RC_MOVE_BITS;
183 *rc->probs[rc->pos] = prob;
193 rc->low += rc->range;
197 // Prevent further normalizations.
198 rc->range = UINT32_MAX;
200 // Flush the last five bytes (see rc_flush()).
202 if (rc_shift_low(rc, out, out_pos, out_size))
204 } while (++rc->pos < rc->count);
206 // Reset the range encoder so we are ready to continue
207 // encoding if we weren't finishing the stream.
226 static inline uint64_t
227 rc_pending(const lzma_range_encoder *rc)
229 return rc->cache_size + 5 - 1;