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
19 #include "range_common.h"
23 /// Maximum number of symbols that can be put pending into lzma_range_encoder
24 /// structure between calls to lzma_rc_encode(). For LZMA, 52+5 is enough
25 /// (match with big distance and length followed by range encoder flush).
26 #define RC_SYMBOLS_MAX 58
35 /// Number of symbols in the tables
38 /// rc_encode()'s position in the tables
48 } symbols[RC_SYMBOLS_MAX];
50 /// Probabilities associated with RC_BIT_0 or RC_BIT_1
51 probability *probs[RC_SYMBOLS_MAX];
57 rc_reset(lzma_range_encoder *rc)
61 rc->range = UINT32_MAX;
69 rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
71 rc->symbols[rc->count] = bit;
72 rc->probs[rc->count] = prob;
78 rc_bittree(lzma_range_encoder *rc, probability *probs,
79 uint32_t bit_count, uint32_t symbol)
81 uint32_t model_index = 1;
84 const uint32_t bit = (symbol >> --bit_count) & 1;
85 rc_bit(rc, &probs[model_index], bit);
86 model_index = (model_index << 1) + bit;
87 } while (bit_count != 0);
92 rc_bittree_reverse(lzma_range_encoder *rc, probability *probs,
93 uint32_t bit_count, uint32_t symbol)
95 uint32_t model_index = 1;
98 const uint32_t bit = symbol & 1;
100 rc_bit(rc, &probs[model_index], bit);
101 model_index = (model_index << 1) + bit;
102 } while (--bit_count != 0);
107 rc_direct(lzma_range_encoder *rc,
108 uint32_t value, uint32_t bit_count)
111 rc->symbols[rc->count++]
112 = RC_DIRECT_0 + ((value >> --bit_count) & 1);
113 } while (bit_count != 0);
118 rc_flush(lzma_range_encoder *rc)
120 for (size_t i = 0; i < 5; ++i)
121 rc->symbols[rc->count++] = RC_FLUSH;
126 rc_shift_low(lzma_range_encoder *rc,
127 uint8_t *out, size_t *out_pos, size_t out_size)
129 if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000)
130 || (uint32_t)(rc->low >> 32) != 0) {
132 if (*out_pos == out_size)
135 out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32);
139 } while (--rc->cache_size != 0);
141 rc->cache = (rc->low >> 24) & 0xFF;
145 rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS;
152 rc_encode(lzma_range_encoder *rc,
153 uint8_t *out, size_t *out_pos, size_t out_size)
155 assert(rc->count <= RC_SYMBOLS_MAX);
157 while (rc->pos < rc->count) {
159 if (rc->range < RC_TOP_VALUE) {
160 if (rc_shift_low(rc, out, out_pos, out_size))
163 rc->range <<= RC_SHIFT_BITS;
167 switch (rc->symbols[rc->pos]) {
169 probability prob = *rc->probs[rc->pos];
170 rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS)
172 prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS;
173 *rc->probs[rc->pos] = prob;
178 probability prob = *rc->probs[rc->pos];
179 const uint32_t bound = prob * (rc->range
180 >> RC_BIT_MODEL_TOTAL_BITS);
183 prob -= prob >> RC_MOVE_BITS;
184 *rc->probs[rc->pos] = prob;
194 rc->low += rc->range;
198 // Prevent further normalizations.
199 rc->range = UINT32_MAX;
201 // Flush the last five bytes (see rc_flush()).
203 if (rc_shift_low(rc, out, out_pos, out_size))
205 } while (++rc->pos < rc->count);
207 // Reset the range encoder so we are ready to continue
208 // encoding if we weren't finishing the stream.
227 static inline uint64_t
228 rc_pending(const lzma_range_encoder *rc)
230 return rc->cache_size + 5 - 1;