1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma_encoder.c
4 /// \brief LZMA encoder
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 #include "lzma_encoder_private.h"
30 literal_matched(lzma_range_encoder *rc, probability *subcoder,
31 uint32_t match_byte, uint32_t symbol)
33 uint32_t offset = 0x100;
34 symbol += UINT32_C(1) << 8;
38 const uint32_t match_bit = match_byte & offset;
39 const uint32_t subcoder_index
40 = offset + match_bit + (symbol >> 8);
41 const uint32_t bit = (symbol >> 7) & 1;
42 rc_bit(rc, &subcoder[subcoder_index], bit);
45 offset &= ~(match_byte ^ symbol);
47 } while (symbol < (UINT32_C(1) << 16));
52 literal(lzma_coder *coder, lzma_mf *mf, uint32_t position)
54 // Locate the literal byte to be encoded and the subcoder.
55 const uint8_t cur_byte = mf->buffer[
56 mf->read_pos - mf->read_ahead];
57 probability *subcoder = literal_subcoder(coder->literal,
58 coder->literal_context_bits, coder->literal_pos_mask,
59 position, mf->buffer[mf->read_pos - mf->read_ahead - 1]);
61 if (is_literal_state(coder->state)) {
62 // Previous LZMA-symbol was a literal. Encode a normal
63 // literal without a match byte.
64 rc_bittree(&coder->rc, subcoder, 8, cur_byte);
66 // Previous LZMA-symbol was a match. Use the last byte of
67 // the match as a "match byte". That is, compare the bits
68 // of the current literal and the match byte.
69 const uint8_t match_byte = mf->buffer[
70 mf->read_pos - coder->reps[0] - 1
72 literal_matched(&coder->rc, subcoder, match_byte, cur_byte);
75 update_literal(coder->state);
84 length_update_prices(lzma_length_encoder *lc, const uint32_t pos_state)
86 const uint32_t table_size = lc->table_size;
87 lc->counters[pos_state] = table_size;
89 const uint32_t a0 = rc_bit_0_price(lc->choice);
90 const uint32_t a1 = rc_bit_1_price(lc->choice);
91 const uint32_t b0 = a1 + rc_bit_0_price(lc->choice2);
92 const uint32_t b1 = a1 + rc_bit_1_price(lc->choice2);
93 uint32_t *const prices = lc->prices[pos_state];
96 for (i = 0; i < table_size && i < LEN_LOW_SYMBOLS; ++i)
97 prices[i] = a0 + rc_bittree_price(lc->low[pos_state],
100 for (; i < table_size && i < LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; ++i)
101 prices[i] = b0 + rc_bittree_price(lc->mid[pos_state],
102 LEN_MID_BITS, i - LEN_LOW_SYMBOLS);
104 for (; i < table_size; ++i)
105 prices[i] = b1 + rc_bittree_price(lc->high, LEN_HIGH_BITS,
106 i - LEN_LOW_SYMBOLS - LEN_MID_SYMBOLS);
113 length(lzma_range_encoder *rc, lzma_length_encoder *lc,
114 const uint32_t pos_state, uint32_t len, const bool fast_mode)
116 assert(len <= MATCH_LEN_MAX);
117 len -= MATCH_LEN_MIN;
119 if (len < LEN_LOW_SYMBOLS) {
120 rc_bit(rc, &lc->choice, 0);
121 rc_bittree(rc, lc->low[pos_state], LEN_LOW_BITS, len);
123 rc_bit(rc, &lc->choice, 1);
124 len -= LEN_LOW_SYMBOLS;
126 if (len < LEN_MID_SYMBOLS) {
127 rc_bit(rc, &lc->choice2, 0);
128 rc_bittree(rc, lc->mid[pos_state], LEN_MID_BITS, len);
130 rc_bit(rc, &lc->choice2, 1);
131 len -= LEN_MID_SYMBOLS;
132 rc_bittree(rc, lc->high, LEN_HIGH_BITS, len);
136 // Only getoptimum uses the prices so don't update the table when
139 if (--lc->counters[pos_state] == 0)
140 length_update_prices(lc, pos_state);
149 match(lzma_coder *coder, const uint32_t pos_state,
150 const uint32_t distance, const uint32_t len)
152 update_match(coder->state);
154 length(&coder->rc, &coder->match_len_encoder, pos_state, len,
157 const uint32_t pos_slot = get_pos_slot(distance);
158 const uint32_t len_to_pos_state = get_len_to_pos_state(len);
159 rc_bittree(&coder->rc, coder->pos_slot[len_to_pos_state],
160 POS_SLOT_BITS, pos_slot);
162 if (pos_slot >= START_POS_MODEL_INDEX) {
163 const uint32_t footer_bits = (pos_slot >> 1) - 1;
164 const uint32_t base = (2 | (pos_slot & 1)) << footer_bits;
165 const uint32_t pos_reduced = distance - base;
167 if (pos_slot < END_POS_MODEL_INDEX) {
168 // Careful here: base - pos_slot - 1 can be -1, but
169 // rc_bittree_reverse starts at probs[1], not probs[0].
170 rc_bittree_reverse(&coder->rc,
171 coder->pos_special + base - pos_slot - 1,
172 footer_bits, pos_reduced);
174 rc_direct(&coder->rc, pos_reduced >> ALIGN_BITS,
175 footer_bits - ALIGN_BITS);
177 &coder->rc, coder->pos_align,
178 ALIGN_BITS, pos_reduced & ALIGN_MASK);
179 ++coder->align_price_count;
183 coder->reps[3] = coder->reps[2];
184 coder->reps[2] = coder->reps[1];
185 coder->reps[1] = coder->reps[0];
186 coder->reps[0] = distance;
187 ++coder->match_price_count;
196 rep_match(lzma_coder *coder, const uint32_t pos_state,
197 const uint32_t rep, const uint32_t len)
200 rc_bit(&coder->rc, &coder->is_rep0[coder->state], 0);
202 &coder->is_rep0_long[coder->state][pos_state],
205 const uint32_t distance = coder->reps[rep];
206 rc_bit(&coder->rc, &coder->is_rep0[coder->state], 1);
209 rc_bit(&coder->rc, &coder->is_rep1[coder->state], 0);
211 rc_bit(&coder->rc, &coder->is_rep1[coder->state], 1);
212 rc_bit(&coder->rc, &coder->is_rep2[coder->state],
216 coder->reps[3] = coder->reps[2];
218 coder->reps[2] = coder->reps[1];
221 coder->reps[1] = coder->reps[0];
222 coder->reps[0] = distance;
226 update_short_rep(coder->state);
228 length(&coder->rc, &coder->rep_len_encoder, pos_state, len,
230 update_long_rep(coder->state);
240 encode_symbol(lzma_coder *coder, lzma_mf *mf,
241 uint32_t back, uint32_t len, uint32_t position)
243 const uint32_t pos_state = position & coder->pos_mask;
245 if (back == UINT32_MAX) {
246 // Literal i.e. eight-bit byte
249 &coder->is_match[coder->state][pos_state], 0);
250 literal(coder, mf, position);
252 // Some type of match
254 &coder->is_match[coder->state][pos_state], 1);
256 if (back < REP_DISTANCES) {
257 // It's a repeated match i.e. the same distance
258 // has been used earlier.
259 rc_bit(&coder->rc, &coder->is_rep[coder->state], 1);
260 rep_match(coder, pos_state, back, len);
263 rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
264 match(coder, pos_state, back - REP_DISTANCES, len);
268 assert(mf->read_ahead >= len);
269 mf->read_ahead -= len;
274 encode_init(lzma_coder *coder, lzma_mf *mf)
276 if (mf->read_pos == mf->read_limit) {
277 if (mf->action == LZMA_RUN)
278 return false; // We cannot do anything.
280 // We are finishing (we cannot get here when flushing).
281 assert(mf->write_pos == mf->read_pos);
282 assert(mf->action == LZMA_FINISH);
284 // Do the actual initialization. The first LZMA symbol must
285 // always be a literal.
288 rc_bit(&coder->rc, &coder->is_match[0][0], 0);
289 rc_bittree(&coder->rc, coder->literal[0], 8, mf->buffer[0]);
292 // Initialization is done (except if empty file).
293 coder->is_initialized = true;
300 encode_eopm(lzma_coder *coder, uint32_t position)
302 const uint32_t pos_state = position & coder->pos_mask;
303 rc_bit(&coder->rc, &coder->is_match[coder->state][pos_state], 1);
304 rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
305 match(coder, pos_state, UINT32_MAX, MATCH_LEN_MIN);
309 /// Number of bytes that a single encoding loop in lzma_lzma_encode() can
310 /// consume from the dictionary. This limit comes from lzma_lzma_optimum()
311 /// and may need to be updated if that function is significantly modified.
312 #define LOOP_INPUT_MAX (OPTS + 1)
316 lzma_lzma_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
317 uint8_t *restrict out, size_t *restrict out_pos,
318 size_t out_size, uint32_t limit)
320 // Initialize the stream if no data has been encoded yet.
321 if (!coder->is_initialized && !encode_init(coder, mf))
324 // Get the lowest bits of the uncompressed offset from the LZ layer.
325 uint32_t position = mf_position(mf);
328 // Encode pending bits, if any. Calling this before encoding
329 // the next symbol is needed only with plain LZMA, since
330 // LZMA2 always provides big enough buffer to flush
331 // everything out from the range encoder. For the same reason,
332 // rc_encode() never returns true when this function is used
333 // as part of LZMA2 encoder.
334 if (rc_encode(&coder->rc, out, out_pos, out_size)) {
335 assert(limit == UINT32_MAX);
339 // With LZMA2 we need to take care that compressed size of
340 // a chunk doesn't get too big.
342 if (limit != UINT32_MAX
343 && (mf->read_pos - mf->read_ahead >= limit
344 || *out_pos + rc_pending(&coder->rc)
345 >= (UINT32_C(1) << 16)
349 // Check that there is some input to process.
350 if (mf->read_pos >= mf->read_limit) {
351 if (mf->action == LZMA_RUN)
354 if (mf->read_ahead == 0)
358 // Get optimal match (repeat position and length).
359 // Value ranges for pos:
360 // - [0, REP_DISTANCES): repeated match
361 // - [REP_DISTANCES, UINT32_MAX):
362 // match at (pos - REP_DISTANCES)
363 // - UINT32_MAX: not a match but a literal
364 // Value ranges for len:
365 // - [MATCH_LEN_MIN, MATCH_LEN_MAX]
369 if (coder->fast_mode)
370 lzma_lzma_optimum_fast(coder, mf, &back, &len);
372 lzma_lzma_optimum_normal(
373 coder, mf, &back, &len, position);
375 encode_symbol(coder, mf, back, len, position);
380 if (!coder->is_flushed) {
381 coder->is_flushed = true;
383 // We don't support encoding plain LZMA streams without EOPM,
384 // and LZMA2 doesn't use EOPM at LZMA level.
385 if (limit == UINT32_MAX)
386 encode_eopm(coder, position);
388 // Flush the remaining bytes from the range encoder.
389 rc_flush(&coder->rc);
391 // Copy the remaining bytes to the output buffer. If there
392 // isn't enough output space, we will copy out the remaining
393 // bytes on the next call to this function by using
394 // the rc_encode() call in the encoding loop above.
395 if (rc_encode(&coder->rc, out, out_pos, out_size)) {
396 assert(limit == UINT32_MAX);
401 // Make it ready for the next LZMA2 chunk.
402 coder->is_flushed = false;
404 return LZMA_STREAM_END;
409 lzma_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
410 uint8_t *restrict out, size_t *restrict out_pos,
413 // Plain LZMA has no support for sync-flushing.
414 if (unlikely(mf->action == LZMA_SYNC_FLUSH))
415 return LZMA_OPTIONS_ERROR;
417 return lzma_lzma_encode(coder, mf, out, out_pos, out_size, UINT32_MAX);
426 set_lz_options(lzma_lz_options *lz_options, const lzma_options_lzma *options)
428 if (!is_lclppb_valid(options)
429 || options->fast_bytes < LZMA_FAST_BYTES_MIN
430 || options->fast_bytes > LZMA_FAST_BYTES_MAX)
435 lz_options->before_size = OPTS;
436 lz_options->dictionary_size = options->dictionary_size;
437 lz_options->after_size = LOOP_INPUT_MAX;
438 lz_options->match_len_max = MATCH_LEN_MAX;
439 lz_options->find_len_max = options->fast_bytes;
440 lz_options->match_finder = options->match_finder;
441 lz_options->match_finder_cycles = options->match_finder_cycles;
442 lz_options->preset_dictionary = options->preset_dictionary;
443 lz_options->preset_dictionary_size = options->preset_dictionary_size;
450 length_encoder_reset(lzma_length_encoder *lencoder,
451 const uint32_t num_pos_states, const bool fast_mode)
453 bit_reset(lencoder->choice);
454 bit_reset(lencoder->choice2);
456 for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
457 bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS);
458 bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS);
461 bittree_reset(lencoder->high, LEN_HIGH_BITS);
464 for (size_t pos_state = 0; pos_state < num_pos_states;
466 length_update_prices(lencoder, pos_state);
473 lzma_lzma_encoder_reset(lzma_coder *coder, const lzma_options_lzma *options)
475 assert(!coder->is_flushed);
477 coder->pos_mask = (1U << options->pos_bits) - 1;
478 coder->literal_context_bits = options->literal_context_bits;
479 coder->literal_pos_mask = (1 << options->literal_pos_bits) - 1;
483 rc_reset(&coder->rc);
487 for (size_t i = 0; i < REP_DISTANCES; ++i)
490 literal_init(coder->literal, options->literal_context_bits,
491 options->literal_pos_bits);
494 for (size_t i = 0; i < STATES; ++i) {
495 for (size_t j = 0; j <= coder->pos_mask; ++j) {
496 bit_reset(coder->is_match[i][j]);
497 bit_reset(coder->is_rep0_long[i][j]);
500 bit_reset(coder->is_rep[i]);
501 bit_reset(coder->is_rep0[i]);
502 bit_reset(coder->is_rep1[i]);
503 bit_reset(coder->is_rep2[i]);
506 for (size_t i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
507 bit_reset(coder->pos_special[i]);
510 for (size_t i = 0; i < LEN_TO_POS_STATES; ++i)
511 bittree_reset(coder->pos_slot[i], POS_SLOT_BITS);
513 bittree_reset(coder->pos_align, ALIGN_BITS);
516 length_encoder_reset(&coder->match_len_encoder,
517 1U << options->pos_bits, coder->fast_mode);
519 length_encoder_reset(&coder->rep_len_encoder,
520 1U << options->pos_bits, coder->fast_mode);
522 // FIXME: Too big or too small won't work when resetting in the middle of LZMA2.
523 coder->match_price_count = UINT32_MAX / 2;
524 coder->align_price_count = UINT32_MAX / 2;
526 coder->opts_end_index = 0;
527 coder->opts_current_index = 0;
532 lzma_lzma_encoder_create(lzma_coder **coder_ptr, lzma_allocator *allocator,
533 const lzma_options_lzma *options, lzma_lz_options *lz_options)
535 if (*coder_ptr == NULL) {
536 *coder_ptr = lzma_alloc(sizeof(lzma_coder), allocator);
537 if (*coder_ptr == NULL)
538 return LZMA_MEM_ERROR;
541 lzma_coder *coder = *coder_ptr;
543 // Validate options that aren't validated elsewhere.
544 if (!is_lclppb_valid(options)
545 || options->fast_bytes < LZMA_FAST_BYTES_MIN
546 || options->fast_bytes > LZMA_FAST_BYTES_MAX)
547 return LZMA_OPTIONS_ERROR;
549 // Set compression mode.
550 switch (options->mode) {
552 coder->fast_mode = true;
555 case LZMA_MODE_NORMAL: {
556 coder->fast_mode = false;
558 // Set dist_table_size.
559 // Round the dictionary size up to next 2^n.
560 uint32_t log_size = 0;
561 while ((UINT32_C(1) << log_size)
562 < options->dictionary_size)
565 coder->dist_table_size = log_size * 2;
567 // Length encoders' price table size
568 coder->match_len_encoder.table_size
569 = options->fast_bytes + 1 - MATCH_LEN_MIN;
570 coder->rep_len_encoder.table_size
571 = options->fast_bytes + 1 - MATCH_LEN_MIN;
576 return LZMA_OPTIONS_ERROR;
579 coder->is_initialized = false;
580 coder->is_flushed = false;
582 lzma_lzma_encoder_reset(coder, options);
584 // LZ encoder options FIXME validation
585 if (set_lz_options(lz_options, options))
586 return LZMA_OPTIONS_ERROR;
593 lzma_encoder_init(lzma_lz_encoder *lz, lzma_allocator *allocator,
594 const void *options, lzma_lz_options *lz_options)
596 lz->code = &lzma_encode;
597 return lzma_lzma_encoder_create(
598 &lz->coder, allocator, options, lz_options);
603 lzma_lzma_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
604 const lzma_filter_info *filters)
606 // Initialization call chain:
608 // lzma_lzma_encoder_init()
609 // `-- lzma_lz_encoder_init()
610 // `-- lzma_encoder_init()
611 // `-- lzma_encoder_init2()
613 // The above complexity is to let LZ encoder store the pointer to
614 // the LZMA encoder structure. Encoding call tree:
618 // | `-- Next coder in the chain, if any
620 // |-- lzma_dict_find()
621 // `-- lzma_dict_skip()
625 return lzma_lz_encoder_init(
626 next, allocator, filters, &lzma_encoder_init);
631 lzma_lzma_encoder_memusage(const void *options)
633 lzma_lz_options lz_options;
634 if (set_lz_options(&lz_options, options))
637 const uint64_t lz_memusage = lzma_lz_encoder_memusage(&lz_options);
638 if (lz_memusage == UINT64_MAX)
641 return (uint64_t)(sizeof(lzma_coder)) + lz_memusage;
646 lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte)
648 if (options->literal_context_bits > LZMA_LITERAL_CONTEXT_BITS_MAX
649 || options->literal_pos_bits
650 > LZMA_LITERAL_POS_BITS_MAX
651 || options->pos_bits > LZMA_POS_BITS_MAX
652 || options->literal_context_bits
653 + options->literal_pos_bits
654 > LZMA_LITERAL_BITS_MAX)
657 *byte = (options->pos_bits * 5 + options->literal_pos_bits) * 9
658 + options->literal_context_bits;
659 assert(*byte <= (4 * 5 + 4) * 9 + 8);
665 #ifdef HAVE_ENCODER_LZMA
667 lzma_lzma_props_encode(const void *options, uint8_t *out)
669 const lzma_options_lzma *const opt = options;
671 if (lzma_lzma_lclppb_encode(opt, out))
672 return LZMA_PROG_ERROR;
674 integer_write_32(out + 1, opt->dictionary_size);
681 extern LZMA_API lzma_bool
682 lzma_mode_is_available(lzma_mode mode)
684 return mode == LZMA_MODE_FAST || mode == LZMA_MODE_NORMAL;