aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/rangecoder/range_encoder.h
blob: d513cfd17abf8d1114f1d6574e2a1c354271e069 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
///////////////////////////////////////////////////////////////////////////////
//
/// \file       range_encoder.h
/// \brief      Range Encoder
//
//  Copyright (C) 1999-2006 Igor Pavlov
//  Copyright (C) 2007 Lasse Collin
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef LZMA_RANGE_ENCODER_H
#define LZMA_RANGE_ENCODER_H

#include "range_common.h"


// Allow #including this file even if RC_TEMP_BUFFER_SIZE isn't defined.
#ifdef RC_BUFFER_SIZE
typedef struct {
	uint64_t low;
	uint32_t range;
	uint32_t cache_size;
	uint8_t cache;
	uint8_t buffer[RC_BUFFER_SIZE];
	size_t buffer_size;
} lzma_range_encoder;
#endif


/// Makes local copies of range encoder variables.
#define rc_to_local(rc) \
	uint64_t rc_low = (rc).low; \
	uint32_t rc_range = (rc).range; \
	uint32_t rc_cache_size = (rc).cache_size; \
	uint8_t rc_cache = (rc).cache; \
	uint8_t *rc_buffer = (rc).buffer; \
	size_t rc_buffer_size = (rc).buffer_size

/// Stores the local copes back to the range encoder structure.
#define rc_from_local(rc) \
do { \
	(rc).low = rc_low; \
	(rc).range = rc_range; \
	(rc).cache_size = rc_cache_size; \
	(rc).cache = rc_cache; \
	(rc).buffer_size = rc_buffer_size; \
} while (0)

/// Resets the range encoder structure.
#define rc_reset(rc) \
do { \
	(rc).low = 0; \
	(rc).range = 0xFFFFFFFF; \
	(rc).cache_size = 1; \
	(rc).cache = 0; \
	(rc).buffer_size = 0; \
} while (0)


//////////////////
// Bit encoding //
//////////////////

// These macros expect that the following variables are defined:
//  - uint64_t  rc_low;
//  - uint32_t  rc_range;
//  - uint8_t   rc_cache;
//  - uint32_t  rc_cache_size;
//  - uint8_t   *out;
//  - size_t    out_pos_local;  // Local copy of *out_pos
//  - size_t    size_out;


// Combined from NRangeCoder::CEncoder::Encode()
// and NRangeCoder::CEncoder::UpdateModel().
#define bit_encode(prob, symbol) \
do { \
	probability rc_prob = prob; \
	const uint32_t rc_bound \
			= (rc_range >> BIT_MODEL_TOTAL_BITS) * rc_prob; \
	if ((symbol) == 0) { \
		rc_range = rc_bound; \
		rc_prob += (BIT_MODEL_TOTAL - rc_prob) >> MOVE_BITS; \
	} else { \
		rc_low += rc_bound; \
		rc_range -= rc_bound; \
		rc_prob -= rc_prob >> MOVE_BITS; \
	} \
	prob = rc_prob; \
	rc_normalize(); \
} while (0)


// Optimized version of bit_encode(prob, 0)
#define bit_encode_0(prob) \
do { \
	probability rc_prob = prob; \
	rc_range = (rc_range >> BIT_MODEL_TOTAL_BITS) * rc_prob; \
	rc_prob += (BIT_MODEL_TOTAL - rc_prob) >> MOVE_BITS; \
	prob = rc_prob; \
	rc_normalize(); \
} while (0)


// Optimized version of bit_encode(prob, 1)
#define bit_encode_1(prob) \
do { \
	probability rc_prob = prob; \
	const uint32_t rc_bound = (rc_range >> BIT_MODEL_TOTAL_BITS) \
			* rc_prob; \
	rc_low += rc_bound; \
	rc_range -= rc_bound; \
	rc_prob -= rc_prob >> MOVE_BITS; \
	prob = rc_prob; \
	rc_normalize(); \
} while (0)


///////////////////////
// Bit tree encoding //
///////////////////////

#define bittree_encode(probs, bit_levels, symbol) \
do { \
	uint32_t model_index = 1; \
	for (int32_t bit_index = bit_levels - 1; \
			bit_index >= 0; --bit_index) { \
		const uint32_t bit = ((symbol) >> bit_index) & 1; \
		bit_encode((probs)[model_index], bit); \
		model_index = (model_index << 1) | bit; \
	} \
} while (0)


#define bittree_reverse_encode(probs, bit_levels, symbol) \
do { \
	uint32_t model_index = 1; \
	for (uint32_t bit_index = 0; bit_index < bit_levels; ++bit_index) { \
		const uint32_t bit = ((symbol) >> bit_index) & 1; \
		bit_encode((probs)[model_index], bit); \
		model_index = (model_index << 1) | bit; \
	} \
} while (0)


/////////////////
// Direct bits //
/////////////////

#define rc_encode_direct_bits(value, num_total_bits) \
do { \
	for (int32_t rc_i = (num_total_bits) - 1; rc_i >= 0; --rc_i) { \
		rc_range >>= 1; \
		if ((((value) >> rc_i) & 1) == 1) \
			rc_low += rc_range; \
		rc_normalize(); \
	} \
} while (0)


//////////////////
// Buffer "I/O" //
//////////////////

// Calls rc_shift_low() to write out a byte if needed.
#define rc_normalize() \
do { \
	if (rc_range < TOP_VALUE) { \
		rc_range <<= SHIFT_BITS; \
		rc_shift_low(); \
	} \
} while (0)


// Flushes all the pending output.
#define rc_flush() \
	for (int32_t rc_i = 0; rc_i < 5; ++rc_i) \
		rc_shift_low()


// Writes the compressed data to next_out.
// TODO: Notation change?
//       (uint32_t)(0xFF000000)  =>  ((uint32_t)(0xFF) << TOP_BITS)
// TODO: Another notation change?
//       rc_low = (uint32_t)(rc_low) << SHIFT_BITS;
//       =>
//       rc_low &= TOP_VALUE - 1;
//       rc_low <<= SHIFT_BITS;
#define rc_shift_low() \
do { \
	if ((uint32_t)(rc_low) < (uint32_t)(0xFF000000) \
			|| (uint32_t)(rc_low >> 32) != 0) { \
		uint8_t rc_temp = rc_cache; \
		do { \
			rc_write_byte(rc_temp + (uint8_t)(rc_low >> 32)); \
			rc_temp = 0xFF; \
		} while(--rc_cache_size != 0); \
		rc_cache = (uint8_t)((uint32_t)(rc_low) >> 24); \
	} \
	++rc_cache_size; \
	rc_low = (uint32_t)(rc_low) << SHIFT_BITS; \
} while (0)


// Write one byte of compressed data to *next_out. Updates out_pos_local.
// If out_pos_local == out_size, the byte is appended to rc_buffer.
#define rc_write_byte(b) \
do { \
	if (out_pos_local == out_size) { \
		rc_buffer[rc_buffer_size++] = (uint8_t)(b); \
		assert(rc_buffer_size < RC_BUFFER_SIZE); \
	} else { \
		assert(rc_buffer_size == 0); \
		out[out_pos_local++] = (uint8_t)(b); \
	} \
} while (0)


//////////////////
// Price macros //
//////////////////

// These macros expect that the following variables are defined:
//  - uint32_t lzma_rc_prob_prices;

#define bit_get_price(prob, symbol) \
	lzma_rc_prob_prices[((((prob) - (symbol)) ^ (-(symbol))) \
			& (BIT_MODEL_TOTAL - 1)) >> MOVE_REDUCING_BITS]


#define bit_get_price_0(prob) \
	lzma_rc_prob_prices[(prob) >> MOVE_REDUCING_BITS]


#define bit_get_price_1(prob) \
	lzma_rc_prob_prices[(BIT_MODEL_TOTAL - (prob)) >> MOVE_REDUCING_BITS]


// Adds price to price_target. TODO Optimize/Cleanup?
#define bittree_get_price(price_target, probs, bit_levels, symbol) \
do { \
	uint32_t bittree_symbol = (symbol) | (UINT32_C(1) << bit_levels); \
	while (bittree_symbol != 1) { \
		price_target += bit_get_price((probs)[bittree_symbol >> 1], \
				bittree_symbol & 1); \
		bittree_symbol >>= 1; \
	} \
} while (0)


// Adds price to price_target.
#define bittree_reverse_get_price(price_target, probs, bit_levels, symbol) \
do { \
	uint32_t model_index = 1; \
	for (uint32_t bit_index = 0; bit_index < bit_levels; ++bit_index) { \
		const uint32_t bit = ((symbol) >> bit_index) & 1; \
		price_target += bit_get_price((probs)[model_index], bit); \
		model_index = (model_index << 1) | bit; \
	} \
} while (0)


//////////////////////
// Global variables //
//////////////////////

// Probability prices used by *_get_price() macros. This is initialized
// by lzma_rc_init() and is not modified later.
extern uint32_t lzma_rc_prob_prices[BIT_MODEL_TOTAL >> MOVE_REDUCING_BITS];


///////////////
// Functions //
///////////////

/// Initializes lzma_rc_prob_prices[]. This needs to be called only once.
extern void lzma_rc_init(void);


#ifdef RC_BUFFER_SIZE
/// Flushes data from rc->temp[] to out[] as much as possible. If everything
/// cannot be flushed, returns true; false otherwise.
static inline bool
rc_flush_buffer(lzma_range_encoder *rc,
		uint8_t *out, size_t *out_pos, size_t out_size)
{
	if (rc->buffer_size > 0) {
		const size_t out_avail = out_size - *out_pos;
		if (rc->buffer_size > out_avail) {
			memcpy(out + *out_pos, rc->buffer, out_avail);
			*out_pos += out_avail;
			rc->buffer_size -= out_avail;
			memmove(rc->buffer, rc->buffer + out_avail,
					rc->buffer_size);
			return true;
		}

		memcpy(out + *out_pos, rc->buffer, rc->buffer_size);
		*out_pos += rc->buffer_size;
		rc->buffer_size = 0;
	}

	return false;
}
#endif

#endif