1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file range_encoder.h
4 /// \brief Range Encoder
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007 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 // Allow #including this file even if RC_TEMP_BUFFER_SIZE isn't defined.
34 uint8_t buffer[RC_BUFFER_SIZE];
40 /// Makes local copies of range encoder variables.
41 #define rc_to_local(rc) \
42 uint64_t rc_low = (rc).low; \
43 uint32_t rc_range = (rc).range; \
44 uint32_t rc_cache_size = (rc).cache_size; \
45 uint8_t rc_cache = (rc).cache; \
46 uint8_t *rc_buffer = (rc).buffer; \
47 size_t rc_buffer_size = (rc).buffer_size
49 /// Stores the local copes back to the range encoder structure.
50 #define rc_from_local(rc) \
53 (rc).range = rc_range; \
54 (rc).cache_size = rc_cache_size; \
55 (rc).cache = rc_cache; \
56 (rc).buffer_size = rc_buffer_size; \
59 /// Resets the range encoder structure.
60 #define rc_reset(rc) \
63 (rc).range = 0xFFFFFFFF; \
64 (rc).cache_size = 1; \
66 (rc).buffer_size = 0; \
74 // These macros expect that the following variables are defined:
76 // - uint32_t rc_range;
77 // - uint8_t rc_cache;
78 // - uint32_t rc_cache_size;
80 // - size_t out_pos_local; // Local copy of *out_pos
84 // Combined from NRangeCoder::CEncoder::Encode()
85 // and NRangeCoder::CEncoder::UpdateModel().
86 #define bit_encode(prob, symbol) \
88 probability rc_prob = prob; \
89 const uint32_t rc_bound \
90 = (rc_range >> BIT_MODEL_TOTAL_BITS) * rc_prob; \
91 if ((symbol) == 0) { \
92 rc_range = rc_bound; \
93 rc_prob += (BIT_MODEL_TOTAL - rc_prob) >> MOVE_BITS; \
96 rc_range -= rc_bound; \
97 rc_prob -= rc_prob >> MOVE_BITS; \
104 // Optimized version of bit_encode(prob, 0)
105 #define bit_encode_0(prob) \
107 probability rc_prob = prob; \
108 rc_range = (rc_range >> BIT_MODEL_TOTAL_BITS) * rc_prob; \
109 rc_prob += (BIT_MODEL_TOTAL - rc_prob) >> MOVE_BITS; \
115 // Optimized version of bit_encode(prob, 1)
116 #define bit_encode_1(prob) \
118 probability rc_prob = prob; \
119 const uint32_t rc_bound = (rc_range >> BIT_MODEL_TOTAL_BITS) \
121 rc_low += rc_bound; \
122 rc_range -= rc_bound; \
123 rc_prob -= rc_prob >> MOVE_BITS; \
129 ///////////////////////
130 // Bit tree encoding //
131 ///////////////////////
133 #define bittree_encode(probs, bit_levels, symbol) \
135 uint32_t model_index = 1; \
136 for (int32_t bit_index = bit_levels - 1; \
137 bit_index >= 0; --bit_index) { \
138 const uint32_t bit = ((symbol) >> bit_index) & 1; \
139 bit_encode((probs)[model_index], bit); \
140 model_index = (model_index << 1) | bit; \
145 #define bittree_reverse_encode(probs, bit_levels, symbol) \
147 uint32_t model_index = 1; \
148 for (uint32_t bit_index = 0; bit_index < bit_levels; ++bit_index) { \
149 const uint32_t bit = ((symbol) >> bit_index) & 1; \
150 bit_encode((probs)[model_index], bit); \
151 model_index = (model_index << 1) | bit; \
160 #define rc_encode_direct_bits(value, num_total_bits) \
162 for (int32_t rc_i = (num_total_bits) - 1; rc_i >= 0; --rc_i) { \
164 if ((((value) >> rc_i) & 1) == 1) \
165 rc_low += rc_range; \
175 // Calls rc_shift_low() to write out a byte if needed.
176 #define rc_normalize() \
178 if (rc_range < TOP_VALUE) { \
179 rc_range <<= SHIFT_BITS; \
185 // Flushes all the pending output.
187 for (int32_t rc_i = 0; rc_i < 5; ++rc_i) \
191 // Writes the compressed data to next_out.
192 // TODO: Notation change?
193 // (uint32_t)(0xFF000000) => ((uint32_t)(0xFF) << TOP_BITS)
194 // TODO: Another notation change?
195 // rc_low = (uint32_t)(rc_low) << SHIFT_BITS;
197 // rc_low &= TOP_VALUE - 1;
198 // rc_low <<= SHIFT_BITS;
199 #define rc_shift_low() \
201 if ((uint32_t)(rc_low) < (uint32_t)(0xFF000000) \
202 || (uint32_t)(rc_low >> 32) != 0) { \
203 uint8_t rc_temp = rc_cache; \
205 rc_write_byte(rc_temp + (uint8_t)(rc_low >> 32)); \
207 } while(--rc_cache_size != 0); \
208 rc_cache = (uint8_t)((uint32_t)(rc_low) >> 24); \
211 rc_low = (uint32_t)(rc_low) << SHIFT_BITS; \
215 // Write one byte of compressed data to *next_out. Updates out_pos_local.
216 // If out_pos_local == out_size, the byte is appended to rc_buffer.
217 #define rc_write_byte(b) \
219 if (out_pos_local == out_size) { \
220 rc_buffer[rc_buffer_size++] = (uint8_t)(b); \
221 assert(rc_buffer_size < RC_BUFFER_SIZE); \
223 assert(rc_buffer_size == 0); \
224 out[out_pos_local++] = (uint8_t)(b); \
233 // These macros expect that the following variables are defined:
234 // - uint32_t lzma_rc_prob_prices;
236 #define bit_get_price(prob, symbol) \
237 lzma_rc_prob_prices[((((prob) - (symbol)) ^ (-(symbol))) \
238 & (BIT_MODEL_TOTAL - 1)) >> MOVE_REDUCING_BITS]
241 #define bit_get_price_0(prob) \
242 lzma_rc_prob_prices[(prob) >> MOVE_REDUCING_BITS]
245 #define bit_get_price_1(prob) \
246 lzma_rc_prob_prices[(BIT_MODEL_TOTAL - (prob)) >> MOVE_REDUCING_BITS]
249 // Adds price to price_target. TODO Optimize/Cleanup?
250 #define bittree_get_price(price_target, probs, bit_levels, symbol) \
252 uint32_t bittree_symbol = (symbol) | (UINT32_C(1) << bit_levels); \
253 while (bittree_symbol != 1) { \
254 price_target += bit_get_price((probs)[bittree_symbol >> 1], \
255 bittree_symbol & 1); \
256 bittree_symbol >>= 1; \
261 // Adds price to price_target.
262 #define bittree_reverse_get_price(price_target, probs, bit_levels, symbol) \
264 uint32_t model_index = 1; \
265 for (uint32_t bit_index = 0; bit_index < bit_levels; ++bit_index) { \
266 const uint32_t bit = ((symbol) >> bit_index) & 1; \
267 price_target += bit_get_price((probs)[model_index], bit); \
268 model_index = (model_index << 1) | bit; \
273 //////////////////////
274 // Global variables //
275 //////////////////////
277 // Probability prices used by *_get_price() macros. This is initialized
278 // by lzma_rc_init() and is not modified later.
279 extern uint32_t lzma_rc_prob_prices[BIT_MODEL_TOTAL >> MOVE_REDUCING_BITS];
286 /// Initializes lzma_rc_prob_prices[]. This needs to be called only once.
287 extern void lzma_rc_init(void);
290 #ifdef RC_BUFFER_SIZE
291 /// Flushes data from rc->temp[] to out[] as much as possible. If everything
292 /// cannot be flushed, returns true; false otherwise.
294 rc_flush_buffer(lzma_range_encoder *rc,
295 uint8_t *out, size_t *out_pos, size_t out_size)
297 if (rc->buffer_size > 0) {
298 const size_t out_avail = out_size - *out_pos;
299 if (rc->buffer_size > out_avail) {
300 memcpy(out + *out_pos, rc->buffer, out_avail);
301 *out_pos += out_avail;
302 rc->buffer_size -= out_avail;
303 memmove(rc->buffer, rc->buffer + out_avail,
308 memcpy(out + *out_pos, rc->buffer, rc->buffer_size);
309 *out_pos += rc->buffer_size;