1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Probability price calculation
6 // Copyright (C) 1999-2008 Igor Pavlov
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
24 #define RC_MOVE_REDUCING_BITS 4
25 #define RC_BIT_PRICE_SHIFT_BITS 4
26 #define RC_PRICE_TABLE_SIZE (RC_BIT_MODEL_TOTAL >> RC_MOVE_REDUCING_BITS)
28 #define RC_INFINITY_PRICE (UINT32_C(1) << 30)
31 #if !defined(LZMA_RANGE_ENCODER_H) || defined(HAVE_SMALL)
32 /// Probability prices used by *_get_price() macros. This is initialized
33 /// by lzma_rc_init() and is not modified later.
34 extern uint32_t lzma_rc_prices[RC_PRICE_TABLE_SIZE];
36 /// Initializes lzma_rc_prices[]. This needs to be called only once.
37 extern void lzma_rc_init(void);
40 // Not building a size optimized version, so we use a precomputed
42 extern const uint32_t lzma_rc_prices[RC_PRICE_TABLE_SIZE];
47 static inline uint32_t
48 rc_bit_price(const probability prob, const uint32_t bit)
50 return lzma_rc_prices[(prob ^ ((UINT32_C(0) - bit)
51 & (RC_BIT_MODEL_TOTAL - 1))) >> RC_MOVE_REDUCING_BITS];
55 static inline uint32_t
56 rc_bit_0_price(const probability prob)
58 return lzma_rc_prices[prob >> RC_MOVE_REDUCING_BITS];
62 static inline uint32_t
63 rc_bit_1_price(const probability prob)
65 return lzma_rc_prices[(prob ^ (RC_BIT_MODEL_TOTAL - 1))
66 >> RC_MOVE_REDUCING_BITS];
70 static inline uint32_t
71 rc_bittree_price(const probability *const probs,
72 const uint32_t bit_levels, uint32_t symbol)
75 symbol += UINT32_C(1) << bit_levels;
78 const uint32_t bit = symbol & 1;
80 price += rc_bit_price(probs[symbol], bit);
81 } while (symbol != 1);
87 static inline uint32_t
88 rc_bittree_reverse_price(const probability *const probs,
89 uint32_t bit_levels, uint32_t symbol)
92 uint32_t model_index = 1;
95 const uint32_t bit = symbol & 1;
97 price += rc_bit_price(probs[model_index], bit);
98 model_index = (model_index << 1) + bit;
99 } while (--bit_levels != 0);
105 static inline uint32_t
106 rc_direct_price(const uint32_t bits)
108 return bits << RC_BIT_PRICE_SHIFT_BITS;