1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file range_decoder.h
4 /// \brief Range Decoder
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_DECODER_H
22 #define LZMA_RANGE_DECODER_H
24 #include "range_common.h"
30 uint32_t init_bytes_left;
35 rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in,
36 size_t *restrict in_pos, size_t in_size)
38 while (rc->init_bytes_left > 0) {
39 if (*in_pos == in_size)
42 rc->code = (rc->code << 8) | in[*in_pos];
44 --rc->init_bytes_left;
51 /// Makes local copies of range decoder variables.
52 #define rc_to_local(range_decoder) \
53 lzma_range_decoder rc = range_decoder; \
56 /// Stores the local copes back to the range decoder structure.
57 #define rc_from_local(range_decoder) \
60 /// Resets the range decoder structure.
61 #define rc_reset(range_decoder) \
63 (range_decoder).range = UINT32_MAX; \
64 (range_decoder).code = 0; \
65 (range_decoder).init_bytes_left = 5; \
69 // All of the macros in this file expect the following variables being defined:
70 // - lzma_range_decoder range_decoder;
71 // - uint32_t rc_bound; // Temporary variable
73 // - size_t in_pos_local; // Local alias for *in_pos
80 // Read the next byte of compressed data from buffer_in, if needed.
81 #define rc_normalize() \
83 if (rc.range < TOP_VALUE) { \
84 rc.range <<= SHIFT_BITS; \
85 rc.code = (rc.code << SHIFT_BITS) | in[in_pos_local++]; \
94 // Range decoder's DecodeBit() is splitted into three macros:
103 #define if_bit_0(prob) \
105 rc_bound = (rc.range >> BIT_MODEL_TOTAL_BITS) * (prob); \
106 if (rc.code < rc_bound)
109 #define update_bit_0(prob) \
111 rc.range = rc_bound; \
112 prob += (BIT_MODEL_TOTAL - (prob)) >> MOVE_BITS; \
116 #define update_bit_1(prob) \
118 rc.range -= rc_bound; \
119 rc.code -= rc_bound; \
120 prob -= (prob) >> MOVE_BITS; \
124 #define rc_decode_direct(dest, count) \
128 rc_bound = (rc.code - rc.range) >> 31; \
129 rc.code -= rc.range & (rc_bound - 1); \
130 dest = ((dest) << 1) | (1 - rc_bound);\
131 } while (--count > 0)
134 // Dummy versions don't update prob or dest.
135 #define update_bit_0_dummy() \
139 #define update_bit_1_dummy() \
141 rc.range -= rc_bound; \
142 rc.code -= rc_bound; \
146 #define rc_decode_direct_dummy(count) \
150 rc_bound = (rc.code - rc.range) >> 31; \
151 rc.code -= rc.range & (rc_bound - 1); \
152 } while (--count > 0)
155 ///////////////////////
156 // Bit tree decoding //
157 ///////////////////////
159 #define bittree_decode(target, probs, bit_levels) \
161 uint32_t model_index = 1; \
162 for (uint32_t bit_index = (bit_levels); bit_index != 0; --bit_index) { \
163 if_bit_0((probs)[model_index]) { \
164 update_bit_0((probs)[model_index]); \
167 update_bit_1((probs)[model_index]); \
168 model_index = (model_index << 1) | 1; \
171 target += model_index - (1 << bit_levels); \
175 #define bittree_reverse_decode(target, probs, bit_levels) \
177 uint32_t model_index = 1; \
178 for (uint32_t bit_index = 0; bit_index < bit_levels; ++bit_index) { \
179 if_bit_0((probs)[model_index]) { \
180 update_bit_0((probs)[model_index]); \
183 update_bit_1((probs)[model_index]); \
184 model_index = (model_index << 1) | 1; \
185 target += 1 << bit_index; \
191 // Dummy versions don't update prob.
192 #define bittree_decode_dummy(target, probs, bit_levels) \
194 uint32_t model_index = 1; \
195 for (uint32_t bit_index = (bit_levels); bit_index != 0; --bit_index) { \
196 if_bit_0((probs)[model_index]) { \
197 update_bit_0_dummy(); \
200 update_bit_1_dummy(); \
201 model_index = (model_index << 1) | 1; \
204 target += model_index - (1 << bit_levels); \
208 #define bittree_reverse_decode_dummy(probs, bit_levels) \
210 uint32_t model_index = 1; \
211 for (uint32_t bit_index = 0; bit_index < bit_levels; ++bit_index) { \
212 if_bit_0((probs)[model_index]) { \
213 update_bit_0_dummy(); \
216 update_bit_1_dummy(); \
217 model_index = (model_index << 1) | 1; \