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
17 #include "range_common.h"
21 /// Maximum number of symbols that can be put pending into lzma_range_encoder
22 /// structure between calls to lzma_rc_encode(). For LZMA, 52+5 is enough
23 /// (match with big distance and length followed by range encoder flush).
24 #define RC_SYMBOLS_MAX 58
33 /// Number of symbols in the tables
36 /// rc_encode()'s position in the tables
46 } symbols
[RC_SYMBOLS_MAX
];
48 /// Probabilities associated with RC_BIT_0 or RC_BIT_1
49 probability
*probs
[RC_SYMBOLS_MAX
];
55 rc_reset(lzma_range_encoder
*rc
)
59 rc
->range
= UINT32_MAX
;
67 rc_bit(lzma_range_encoder
*rc
, probability
*prob
, uint32_t bit
)
69 rc
->symbols
[rc
->count
] = bit
;
70 rc
->probs
[rc
->count
] = prob
;
76 rc_bittree(lzma_range_encoder
*rc
, probability
*probs
,
77 uint32_t bit_count
, uint32_t symbol
)
79 uint32_t model_index
= 1;
82 const uint32_t bit
= (symbol
>> --bit_count
) & 1;
83 rc_bit(rc
, &probs
[model_index
], bit
);
84 model_index
= (model_index
<< 1) + bit
;
85 } while (bit_count
!= 0);
90 rc_bittree_reverse(lzma_range_encoder
*rc
, probability
*probs
,
91 uint32_t bit_count
, uint32_t symbol
)
93 uint32_t model_index
= 1;
96 const uint32_t bit
= symbol
& 1;
98 rc_bit(rc
, &probs
[model_index
], bit
);
99 model_index
= (model_index
<< 1) + bit
;
100 } while (--bit_count
!= 0);
105 rc_direct(lzma_range_encoder
*rc
,
106 uint32_t value
, uint32_t bit_count
)
109 rc
->symbols
[rc
->count
++]
110 = RC_DIRECT_0
+ ((value
>> --bit_count
) & 1);
111 } while (bit_count
!= 0);
116 rc_flush(lzma_range_encoder
*rc
)
118 for (size_t i
= 0; i
< 5; ++i
)
119 rc
->symbols
[rc
->count
++] = RC_FLUSH
;
124 rc_shift_low(lzma_range_encoder
*rc
,
125 uint8_t *out
, size_t *out_pos
, size_t out_size
)
127 if ((uint32_t)(rc
->low
) < (uint32_t)(0xFF000000)
128 || (uint32_t)(rc
->low
>> 32) != 0) {
130 if (*out_pos
== out_size
)
133 out
[*out_pos
] = rc
->cache
+ (uint8_t)(rc
->low
>> 32);
137 } while (--rc
->cache_size
!= 0);
139 rc
->cache
= (rc
->low
>> 24) & 0xFF;
143 rc
->low
= (rc
->low
& 0x00FFFFFF) << RC_SHIFT_BITS
;
150 rc_encode(lzma_range_encoder
*rc
,
151 uint8_t *out
, size_t *out_pos
, size_t out_size
)
153 assert(rc
->count
<= RC_SYMBOLS_MAX
);
155 while (rc
->pos
< rc
->count
) {
157 if (rc
->range
< RC_TOP_VALUE
) {
158 if (rc_shift_low(rc
, out
, out_pos
, out_size
))
161 rc
->range
<<= RC_SHIFT_BITS
;
165 switch (rc
->symbols
[rc
->pos
]) {
167 probability prob
= *rc
->probs
[rc
->pos
];
168 rc
->range
= (rc
->range
>> RC_BIT_MODEL_TOTAL_BITS
)
170 prob
+= (RC_BIT_MODEL_TOTAL
- prob
) >> RC_MOVE_BITS
;
171 *rc
->probs
[rc
->pos
] = prob
;
176 probability prob
= *rc
->probs
[rc
->pos
];
177 const uint32_t bound
= prob
* (rc
->range
178 >> RC_BIT_MODEL_TOTAL_BITS
);
181 prob
-= prob
>> RC_MOVE_BITS
;
182 *rc
->probs
[rc
->pos
] = prob
;
192 rc
->low
+= rc
->range
;
196 // Prevent further normalizations.
197 rc
->range
= UINT32_MAX
;
199 // Flush the last five bytes (see rc_flush()).
201 if (rc_shift_low(rc
, out
, out_pos
, out_size
))
203 } while (++rc
->pos
< rc
->count
);
205 // Reset the range encoder so we are ready to continue
206 // encoding if we weren't finishing the stream.
225 static inline uint64_t
226 rc_pending(const lzma_range_encoder
*rc
)
228 return rc
->cache_size
+ 5 - 1;