/src/xz/src/liblzma/rangecoder/range_encoder.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: 0BSD |
2 | | |
3 | | /////////////////////////////////////////////////////////////////////////////// |
4 | | // |
5 | | /// \file range_encoder.h |
6 | | /// \brief Range Encoder |
7 | | /// |
8 | | // Authors: Igor Pavlov |
9 | | // Lasse Collin |
10 | | // |
11 | | /////////////////////////////////////////////////////////////////////////////// |
12 | | |
13 | | #ifndef LZMA_RANGE_ENCODER_H |
14 | | #define LZMA_RANGE_ENCODER_H |
15 | | |
16 | | #include "range_common.h" |
17 | | #include "price.h" |
18 | | |
19 | | |
20 | | /// Maximum number of symbols that can be put pending into lzma_range_encoder |
21 | | /// structure between calls to lzma_rc_encode(). For LZMA, 48+5 is enough |
22 | | /// (match with big distance and length followed by range encoder flush). |
23 | | #define RC_SYMBOLS_MAX 53 |
24 | | |
25 | | |
26 | | typedef struct { |
27 | | uint64_t low; |
28 | | uint64_t cache_size; |
29 | | uint32_t range; |
30 | | uint8_t cache; |
31 | | |
32 | | /// Number of bytes written out by rc_encode() -> rc_shift_low() |
33 | | uint64_t out_total; |
34 | | |
35 | | /// Number of symbols in the tables |
36 | | size_t count; |
37 | | |
38 | | /// rc_encode()'s position in the tables |
39 | | size_t pos; |
40 | | |
41 | | /// Symbols to encode |
42 | | enum { |
43 | | RC_BIT_0, |
44 | | RC_BIT_1, |
45 | | RC_DIRECT_0, |
46 | | RC_DIRECT_1, |
47 | | RC_FLUSH, |
48 | | } symbols[RC_SYMBOLS_MAX]; |
49 | | |
50 | | /// Probabilities associated with RC_BIT_0 or RC_BIT_1 |
51 | | probability *probs[RC_SYMBOLS_MAX]; |
52 | | |
53 | | } lzma_range_encoder; |
54 | | |
55 | | |
56 | | static inline void |
57 | | rc_reset(lzma_range_encoder *rc) |
58 | 8.15k | { |
59 | 8.15k | rc->low = 0; |
60 | 8.15k | rc->cache_size = 1; |
61 | 8.15k | rc->range = UINT32_MAX; |
62 | 8.15k | rc->cache = 0; |
63 | 8.15k | rc->out_total = 0; |
64 | 8.15k | rc->count = 0; |
65 | 8.15k | rc->pos = 0; |
66 | 8.15k | } Line | Count | Source | 58 | 8.15k | { | 59 | 8.15k | rc->low = 0; | 60 | 8.15k | rc->cache_size = 1; | 61 | | rc->range = UINT32_MAX; | 62 | 8.15k | rc->cache = 0; | 63 | 8.15k | rc->out_total = 0; | 64 | 8.15k | rc->count = 0; | 65 | 8.15k | rc->pos = 0; | 66 | 8.15k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_reset Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_reset Unexecuted instantiation: price_table.c:rc_reset |
67 | | |
68 | | |
69 | | static inline void |
70 | | rc_forget(lzma_range_encoder *rc) |
71 | 0 | { |
72 | | // This must not be called when rc_encode() is partially done. |
73 | 0 | assert(rc->pos == 0); |
74 | 0 | rc->count = 0; |
75 | 0 | } Unexecuted instantiation: lzma_encoder.c:rc_forget Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_forget Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_forget Unexecuted instantiation: price_table.c:rc_forget |
76 | | |
77 | | |
78 | | static inline void |
79 | | rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit) |
80 | 4.49M | { |
81 | 4.49M | rc->symbols[rc->count] = bit; |
82 | 4.49M | rc->probs[rc->count] = prob; |
83 | 4.49M | ++rc->count; |
84 | 4.49M | } Line | Count | Source | 80 | 4.49M | { | 81 | 4.49M | rc->symbols[rc->count] = bit; | 82 | 4.49M | rc->probs[rc->count] = prob; | 83 | 4.49M | ++rc->count; | 84 | 4.49M | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_bit Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_bit Unexecuted instantiation: price_table.c:rc_bit |
85 | | |
86 | | |
87 | | static inline void |
88 | | rc_bittree(lzma_range_encoder *rc, probability *probs, |
89 | | uint32_t bit_count, uint32_t symbol) |
90 | 456k | { |
91 | 456k | uint32_t model_index = 1; |
92 | | |
93 | 3.14M | do { |
94 | 3.14M | const uint32_t bit = (symbol >> --bit_count) & 1; |
95 | 3.14M | rc_bit(rc, &probs[model_index], bit); |
96 | 3.14M | model_index = (model_index << 1) + bit; |
97 | 3.14M | } while (bit_count != 0); |
98 | 456k | } lzma_encoder.c:rc_bittree Line | Count | Source | 90 | 456k | { | 91 | 456k | uint32_t model_index = 1; | 92 | | | 93 | 3.14M | do { | 94 | 3.14M | const uint32_t bit = (symbol >> --bit_count) & 1; | 95 | 3.14M | rc_bit(rc, &probs[model_index], bit); | 96 | 3.14M | model_index = (model_index << 1) + bit; | 97 | 3.14M | } while (bit_count != 0); | 98 | 456k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_bittree Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_bittree Unexecuted instantiation: price_table.c:rc_bittree |
99 | | |
100 | | |
101 | | static inline void |
102 | | rc_bittree_reverse(lzma_range_encoder *rc, probability *probs, |
103 | | uint32_t bit_count, uint32_t symbol) |
104 | 46.1k | { |
105 | 46.1k | uint32_t model_index = 1; |
106 | | |
107 | 158k | do { |
108 | 158k | const uint32_t bit = symbol & 1; |
109 | 158k | symbol >>= 1; |
110 | 158k | rc_bit(rc, &probs[model_index], bit); |
111 | 158k | model_index = (model_index << 1) + bit; |
112 | 158k | } while (--bit_count != 0); |
113 | 46.1k | } lzma_encoder.c:rc_bittree_reverse Line | Count | Source | 104 | 46.1k | { | 105 | 46.1k | uint32_t model_index = 1; | 106 | | | 107 | 158k | do { | 108 | 158k | const uint32_t bit = symbol & 1; | 109 | 158k | symbol >>= 1; | 110 | 158k | rc_bit(rc, &probs[model_index], bit); | 111 | 158k | model_index = (model_index << 1) + bit; | 112 | 158k | } while (--bit_count != 0); | 113 | 46.1k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_bittree_reverse Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_bittree_reverse Unexecuted instantiation: price_table.c:rc_bittree_reverse |
114 | | |
115 | | |
116 | | static inline void |
117 | | rc_direct(lzma_range_encoder *rc, |
118 | | uint32_t value, uint32_t bit_count) |
119 | 14.2k | { |
120 | 43.1k | do { |
121 | 43.1k | rc->symbols[rc->count++] |
122 | 43.1k | = RC_DIRECT_0 + ((value >> --bit_count) & 1); |
123 | 43.1k | } while (bit_count != 0); |
124 | 14.2k | } Line | Count | Source | 119 | 14.2k | { | 120 | 43.1k | do { | 121 | 43.1k | rc->symbols[rc->count++] | 122 | 43.1k | = RC_DIRECT_0 + ((value >> --bit_count) & 1); | 123 | 43.1k | } while (bit_count != 0); | 124 | 14.2k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_direct Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_direct Unexecuted instantiation: price_table.c:rc_direct |
125 | | |
126 | | |
127 | | static inline void |
128 | | rc_flush(lzma_range_encoder *rc) |
129 | 4.07k | { |
130 | 24.4k | for (size_t i = 0; i < 5; ++i) |
131 | 20.3k | rc->symbols[rc->count++] = RC_FLUSH; |
132 | 4.07k | } Line | Count | Source | 129 | 4.07k | { | 130 | 24.4k | for (size_t i = 0; i < 5; ++i) | 131 | 20.3k | rc->symbols[rc->count++] = RC_FLUSH; | 132 | 4.07k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_flush Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_flush Unexecuted instantiation: price_table.c:rc_flush |
133 | | |
134 | | |
135 | | static inline bool |
136 | | rc_shift_low(lzma_range_encoder *rc, |
137 | | uint8_t *out, size_t *out_pos, size_t out_size) |
138 | 530k | { |
139 | 530k | if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000) |
140 | 527k | || (uint32_t)(rc->low >> 32) != 0) { |
141 | 530k | do { |
142 | 530k | if (*out_pos == out_size) |
143 | 0 | return true; |
144 | | |
145 | 530k | out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32); |
146 | 530k | ++*out_pos; |
147 | 530k | ++rc->out_total; |
148 | 530k | rc->cache = 0xFF; |
149 | | |
150 | 530k | } while (--rc->cache_size != 0); |
151 | | |
152 | 527k | rc->cache = (rc->low >> 24) & 0xFF; |
153 | 527k | } |
154 | | |
155 | 530k | ++rc->cache_size; |
156 | 530k | rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS; |
157 | | |
158 | 530k | return false; |
159 | 530k | } lzma_encoder.c:rc_shift_low Line | Count | Source | 138 | 530k | { | 139 | 530k | if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000) | 140 | 527k | || (uint32_t)(rc->low >> 32) != 0) { | 141 | 530k | do { | 142 | 530k | if (*out_pos == out_size) | 143 | 0 | return true; | 144 | | | 145 | 530k | out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32); | 146 | 530k | ++*out_pos; | 147 | 530k | ++rc->out_total; | 148 | 530k | rc->cache = 0xFF; | 149 | | | 150 | 530k | } while (--rc->cache_size != 0); | 151 | | | 152 | 527k | rc->cache = (rc->low >> 24) & 0xFF; | 153 | 527k | } | 154 | | | 155 | 530k | ++rc->cache_size; | 156 | 530k | rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS; | 157 | | | 158 | | return false; | 159 | 530k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_shift_low Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_shift_low Unexecuted instantiation: price_table.c:rc_shift_low |
160 | | |
161 | | |
162 | | // NOTE: The last two arguments are uint64_t instead of size_t because in |
163 | | // the dummy version these refer to the size of the whole range-encoded |
164 | | // output stream, not just to the currently available output buffer space. |
165 | | static inline bool |
166 | | rc_shift_low_dummy(uint64_t *low, uint64_t *cache_size, uint8_t *cache, |
167 | | uint64_t *out_pos, uint64_t out_size) |
168 | 0 | { |
169 | 0 | if ((uint32_t)(*low) < (uint32_t)(0xFF000000) |
170 | 0 | || (uint32_t)(*low >> 32) != 0) { |
171 | 0 | do { |
172 | 0 | if (*out_pos == out_size) |
173 | 0 | return true; |
174 | | |
175 | 0 | ++*out_pos; |
176 | 0 | *cache = 0xFF; |
177 | |
|
178 | 0 | } while (--*cache_size != 0); |
179 | | |
180 | 0 | *cache = (*low >> 24) & 0xFF; |
181 | 0 | } |
182 | | |
183 | 0 | ++*cache_size; |
184 | 0 | *low = (*low & 0x00FFFFFF) << RC_SHIFT_BITS; |
185 | |
|
186 | 0 | return false; |
187 | 0 | } Unexecuted instantiation: lzma_encoder.c:rc_shift_low_dummy Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_shift_low_dummy Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_shift_low_dummy Unexecuted instantiation: price_table.c:rc_shift_low_dummy |
188 | | |
189 | | |
190 | | static inline bool |
191 | | rc_encode(lzma_range_encoder *rc, |
192 | | uint8_t *out, size_t *out_pos, size_t out_size) |
193 | 465k | { |
194 | 465k | assert(rc->count <= RC_SYMBOLS_MAX); |
195 | | |
196 | 5.00M | while (rc->pos < rc->count) { |
197 | | // Normalize |
198 | 4.54M | if (rc->range < RC_TOP_VALUE) { |
199 | 509k | if (rc_shift_low(rc, out, out_pos, out_size)) |
200 | 0 | return true; |
201 | | |
202 | 509k | rc->range <<= RC_SHIFT_BITS; |
203 | 509k | } |
204 | | |
205 | | // Encode a bit |
206 | 4.54M | switch (rc->symbols[rc->pos]) { |
207 | 2.59M | case RC_BIT_0: { |
208 | 2.59M | probability prob = *rc->probs[rc->pos]; |
209 | 2.59M | rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) |
210 | 2.59M | * prob; |
211 | 2.59M | prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS; |
212 | 2.59M | *rc->probs[rc->pos] = prob; |
213 | 2.59M | break; |
214 | 0 | } |
215 | | |
216 | 1.90M | case RC_BIT_1: { |
217 | 1.90M | probability prob = *rc->probs[rc->pos]; |
218 | 1.90M | const uint32_t bound = prob * (rc->range |
219 | 1.90M | >> RC_BIT_MODEL_TOTAL_BITS); |
220 | 1.90M | rc->low += bound; |
221 | 1.90M | rc->range -= bound; |
222 | 1.90M | prob -= prob >> RC_MOVE_BITS; |
223 | 1.90M | *rc->probs[rc->pos] = prob; |
224 | 1.90M | break; |
225 | 0 | } |
226 | | |
227 | 23.0k | case RC_DIRECT_0: |
228 | 23.0k | rc->range >>= 1; |
229 | 23.0k | break; |
230 | | |
231 | 20.1k | case RC_DIRECT_1: |
232 | 20.1k | rc->range >>= 1; |
233 | 20.1k | rc->low += rc->range; |
234 | 20.1k | break; |
235 | | |
236 | 4.07k | case RC_FLUSH: |
237 | | // Prevent further normalizations. |
238 | 4.07k | rc->range = UINT32_MAX; |
239 | | |
240 | | // Flush the last five bytes (see rc_flush()). |
241 | 20.3k | do { |
242 | 20.3k | if (rc_shift_low(rc, out, out_pos, out_size)) |
243 | 0 | return true; |
244 | 20.3k | } while (++rc->pos < rc->count); |
245 | | |
246 | | // Reset the range encoder so we are ready to continue |
247 | | // encoding if we weren't finishing the stream. |
248 | 4.07k | rc_reset(rc); |
249 | 4.07k | return false; |
250 | | |
251 | 0 | default: |
252 | 0 | assert(0); |
253 | 0 | break; |
254 | 4.54M | } |
255 | | |
256 | 4.53M | ++rc->pos; |
257 | 4.53M | } |
258 | | |
259 | 461k | rc->count = 0; |
260 | 461k | rc->pos = 0; |
261 | | |
262 | 461k | return false; |
263 | 465k | } Line | Count | Source | 193 | 465k | { | 194 | 465k | assert(rc->count <= RC_SYMBOLS_MAX); | 195 | | | 196 | 5.00M | while (rc->pos < rc->count) { | 197 | | // Normalize | 198 | 4.54M | if (rc->range < RC_TOP_VALUE) { | 199 | 509k | if (rc_shift_low(rc, out, out_pos, out_size)) | 200 | 0 | return true; | 201 | | | 202 | 509k | rc->range <<= RC_SHIFT_BITS; | 203 | 509k | } | 204 | | | 205 | | // Encode a bit | 206 | 4.54M | switch (rc->symbols[rc->pos]) { | 207 | 2.59M | case RC_BIT_0: { | 208 | 2.59M | probability prob = *rc->probs[rc->pos]; | 209 | 2.59M | rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) | 210 | 2.59M | * prob; | 211 | 2.59M | prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS; | 212 | 2.59M | *rc->probs[rc->pos] = prob; | 213 | 2.59M | break; | 214 | 0 | } | 215 | | | 216 | 1.90M | case RC_BIT_1: { | 217 | 1.90M | probability prob = *rc->probs[rc->pos]; | 218 | 1.90M | const uint32_t bound = prob * (rc->range | 219 | 1.90M | >> RC_BIT_MODEL_TOTAL_BITS); | 220 | 1.90M | rc->low += bound; | 221 | 1.90M | rc->range -= bound; | 222 | 1.90M | prob -= prob >> RC_MOVE_BITS; | 223 | 1.90M | *rc->probs[rc->pos] = prob; | 224 | 1.90M | break; | 225 | 0 | } | 226 | | | 227 | 23.0k | case RC_DIRECT_0: | 228 | 23.0k | rc->range >>= 1; | 229 | 23.0k | break; | 230 | | | 231 | 20.1k | case RC_DIRECT_1: | 232 | 20.1k | rc->range >>= 1; | 233 | 20.1k | rc->low += rc->range; | 234 | 20.1k | break; | 235 | | | 236 | 4.07k | case RC_FLUSH: | 237 | | // Prevent further normalizations. | 238 | 4.07k | rc->range = UINT32_MAX; | 239 | | | 240 | | // Flush the last five bytes (see rc_flush()). | 241 | 20.3k | do { | 242 | 20.3k | if (rc_shift_low(rc, out, out_pos, out_size)) | 243 | 0 | return true; | 244 | 20.3k | } while (++rc->pos < rc->count); | 245 | | | 246 | | // Reset the range encoder so we are ready to continue | 247 | | // encoding if we weren't finishing the stream. | 248 | 4.07k | rc_reset(rc); | 249 | 4.07k | return false; | 250 | | | 251 | 0 | default: | 252 | 0 | assert(0); | 253 | 0 | break; | 254 | 4.54M | } | 255 | | | 256 | 4.53M | ++rc->pos; | 257 | 4.53M | } | 258 | | | 259 | 461k | rc->count = 0; | 260 | 461k | rc->pos = 0; | 261 | | | 262 | | return false; | 263 | 465k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_encode Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_encode Unexecuted instantiation: price_table.c:rc_encode |
264 | | |
265 | | |
266 | | static inline bool |
267 | | rc_encode_dummy(const lzma_range_encoder *rc, uint64_t out_limit) |
268 | 0 | { |
269 | 0 | assert(rc->count <= RC_SYMBOLS_MAX); |
270 | |
|
271 | 0 | uint64_t low = rc->low; |
272 | 0 | uint64_t cache_size = rc->cache_size; |
273 | 0 | uint32_t range = rc->range; |
274 | 0 | uint8_t cache = rc->cache; |
275 | 0 | uint64_t out_pos = rc->out_total; |
276 | |
|
277 | 0 | size_t pos = rc->pos; |
278 | |
|
279 | 0 | while (true) { |
280 | | // Normalize |
281 | 0 | if (range < RC_TOP_VALUE) { |
282 | 0 | if (rc_shift_low_dummy(&low, &cache_size, &cache, |
283 | 0 | &out_pos, out_limit)) |
284 | 0 | return true; |
285 | | |
286 | 0 | range <<= RC_SHIFT_BITS; |
287 | 0 | } |
288 | | |
289 | | // This check is here because the normalization above must |
290 | | // be done before flushing the last bytes. |
291 | 0 | if (pos == rc->count) |
292 | 0 | break; |
293 | | |
294 | | // Encode a bit |
295 | 0 | switch (rc->symbols[pos]) { |
296 | 0 | case RC_BIT_0: { |
297 | 0 | probability prob = *rc->probs[pos]; |
298 | 0 | range = (range >> RC_BIT_MODEL_TOTAL_BITS) |
299 | 0 | * prob; |
300 | 0 | break; |
301 | 0 | } |
302 | | |
303 | 0 | case RC_BIT_1: { |
304 | 0 | probability prob = *rc->probs[pos]; |
305 | 0 | const uint32_t bound = prob * (range |
306 | 0 | >> RC_BIT_MODEL_TOTAL_BITS); |
307 | 0 | low += bound; |
308 | 0 | range -= bound; |
309 | 0 | break; |
310 | 0 | } |
311 | | |
312 | 0 | case RC_DIRECT_0: |
313 | 0 | range >>= 1; |
314 | 0 | break; |
315 | | |
316 | 0 | case RC_DIRECT_1: |
317 | 0 | range >>= 1; |
318 | 0 | low += range; |
319 | 0 | break; |
320 | | |
321 | 0 | case RC_FLUSH: |
322 | 0 | default: |
323 | 0 | assert(0); |
324 | 0 | break; |
325 | 0 | } |
326 | | |
327 | 0 | ++pos; |
328 | 0 | } |
329 | | |
330 | | // Flush the last bytes. This isn't in rc->symbols[] so we do |
331 | | // it after the above loop to take into account the size of |
332 | | // the flushing that will be done at the end of the stream. |
333 | 0 | for (pos = 0; pos < 5; ++pos) { |
334 | 0 | if (rc_shift_low_dummy(&low, &cache_size, |
335 | 0 | &cache, &out_pos, out_limit)) |
336 | 0 | return true; |
337 | 0 | } |
338 | | |
339 | 0 | return false; |
340 | 0 | } Unexecuted instantiation: lzma_encoder.c:rc_encode_dummy Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_encode_dummy Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_encode_dummy Unexecuted instantiation: price_table.c:rc_encode_dummy |
341 | | |
342 | | |
343 | | static inline uint64_t |
344 | | rc_pending(const lzma_range_encoder *rc) |
345 | 461k | { |
346 | 461k | return rc->cache_size + 5 - 1; |
347 | 461k | } lzma_encoder.c:rc_pending Line | Count | Source | 345 | 461k | { | 346 | 461k | return rc->cache_size + 5 - 1; | 347 | 461k | } |
Unexecuted instantiation: lzma_encoder_optimum_fast.c:rc_pending Unexecuted instantiation: lzma_encoder_optimum_normal.c:rc_pending Unexecuted instantiation: price_table.c:rc_pending |
348 | | |
349 | | #endif |