1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file range_encoder.h
4 /// \brief Range Encoder
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007-2008 Lasse Collin
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 ///////////////////////////////////////////////////////////////////////////////
21 #ifndef LZMA_RANGE_ENCODER_H
22 #define LZMA_RANGE_ENCODER_H
24 #include "range_common.h"
27 /// Maximum number of symbols that can be put pending into lzma_range_encoder
28 /// structure between calls to lzma_rc_encode(). For LZMA, 52+5 is enough
29 /// (match with big distance and length followed by range encoder flush).
30 #define RC_SYMBOLS_MAX 58
39 /// Number of symbols in the tables
42 /// rc_encode()'s position in the tables
52 } symbols[RC_SYMBOLS_MAX];
54 /// Probabilities associated with RC_BIT_0 or RC_BIT_1
55 probability *probs[RC_SYMBOLS_MAX];
61 rc_reset(lzma_range_encoder *rc)
65 rc->range = UINT32_MAX;
73 rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
75 rc->symbols[rc->count] = bit;
76 rc->probs[rc->count] = prob;
82 rc_bittree(lzma_range_encoder *rc, probability *probs,
83 uint32_t bit_count, uint32_t symbol)
85 uint32_t model_index = 1;
88 const uint32_t bit = (symbol >> --bit_count) & 1;
89 rc_bit(rc, &probs[model_index], bit);
90 model_index = (model_index << 1) | bit;
91 } while (bit_count != 0);
96 rc_bittree_reverse(lzma_range_encoder *rc, probability *probs,
97 uint32_t bit_count, uint32_t symbol)
99 uint32_t model_index = 1;
102 const uint32_t bit = symbol & 1;
104 rc_bit(rc, &probs[model_index], bit);
105 model_index = (model_index << 1) | bit;
106 } while (--bit_count != 0);
111 rc_direct(lzma_range_encoder *rc,
112 uint32_t value, uint32_t bit_count)
115 rc->symbols[rc->count++]
116 = RC_DIRECT_0 + ((value >> --bit_count) & 1);
117 } while (bit_count != 0);
122 rc_flush(lzma_range_encoder *rc)
124 for (size_t i = 0; i < 5; ++i)
125 rc->symbols[rc->count++] = RC_FLUSH;
130 rc_shift_low(lzma_range_encoder *rc,
131 uint8_t *out, size_t *out_pos, size_t out_size)
133 if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000)
134 || (uint32_t)(rc->low >> 32) != 0) {
136 if (*out_pos == out_size)
139 out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32);
143 } while (--rc->cache_size != 0);
145 rc->cache = (rc->low >> 24) & 0xFF;
149 rc->low = (rc->low & 0x00FFFFFF) << SHIFT_BITS;
156 rc_encode(lzma_range_encoder *rc,
157 uint8_t *out, size_t *out_pos, size_t out_size)
159 while (rc->pos < rc->count) {
161 if (rc->range < TOP_VALUE) {
162 if (rc_shift_low(rc, out, out_pos, out_size))
165 rc->range <<= SHIFT_BITS;
169 switch (rc->symbols[rc->pos]) {
171 probability prob = *rc->probs[rc->pos];
172 rc->range = (rc->range >> BIT_MODEL_TOTAL_BITS) * prob;
173 prob += (BIT_MODEL_TOTAL - prob) >> MOVE_BITS;
174 *rc->probs[rc->pos] = prob;
179 probability prob = *rc->probs[rc->pos];
180 const uint32_t bound = prob
181 * (rc->range >> BIT_MODEL_TOTAL_BITS);
184 prob -= prob >> MOVE_BITS;
185 *rc->probs[rc->pos] = prob;
195 rc->low += rc->range;
199 // Prevent further normalizations.
200 rc->range = UINT32_MAX;
202 // Flush the last five bytes (see rc_flush()).
204 if (rc_shift_low(rc, out, out_pos, out_size))
206 } while (++rc->pos < rc->count);
208 // Reset the range encoder so we are ready to continue
209 // encoding if we weren't finishing the stream.
228 static inline uint64_t
229 rc_pending(const lzma_range_encoder *rc)
231 return rc->cache_size + 5 - 1;
240 /// Probability prices used by *_get_price() macros. This is initialized
241 /// by lzma_rc_init() and is not modified later.
242 extern uint32_t lzma_rc_prob_prices[BIT_MODEL_TOTAL >> MOVE_REDUCING_BITS];
244 /// Initializes lzma_rc_prob_prices[]. This needs to be called only once.
245 extern void lzma_rc_init(void);
248 // Not building a size optimized version, so we use a precomputed
250 extern const uint32_t
251 lzma_rc_prob_prices[BIT_MODEL_TOTAL >> MOVE_REDUCING_BITS];
256 #define bit_get_price(prob, symbol) \
257 lzma_rc_prob_prices[((((prob) - (symbol)) ^ (-(symbol))) \
258 & (BIT_MODEL_TOTAL - 1)) >> MOVE_REDUCING_BITS]
261 #define bit_get_price_0(prob) \
262 lzma_rc_prob_prices[(prob) >> MOVE_REDUCING_BITS]
265 #define bit_get_price_1(prob) \
266 lzma_rc_prob_prices[(BIT_MODEL_TOTAL - (prob)) >> MOVE_REDUCING_BITS]
269 static inline uint32_t
270 bittree_get_price(const probability *probs,
271 uint32_t bit_levels, uint32_t symbol)
274 symbol |= UINT32_C(1) << bit_levels;
277 price += bit_get_price(probs[symbol >> 1], symbol & 1);
279 } while (symbol != 1);
285 static inline uint32_t
286 bittree_reverse_get_price(const probability *probs,
287 uint32_t bit_levels, uint32_t symbol)
290 uint32_t model_index = 1;
293 const uint32_t bit = symbol & 1;
295 price += bit_get_price(probs[model_index], bit);
296 model_index = (model_index << 1) | bit;
297 } while (--bit_levels != 0);