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 // LZ encoder initialization does the validation, also when just
429 // calculating memory usage, so we don't need to validate here.
430 lz_options->before_size = OPTS;
431 lz_options->dict_size = options->dict_size;
432 lz_options->after_size = LOOP_INPUT_MAX;
433 lz_options->match_len_max = MATCH_LEN_MAX;
434 lz_options->nice_len = options->nice_len;
435 lz_options->match_finder = options->mf;
436 lz_options->depth = options->depth;
437 lz_options->preset_dict = options->preset_dict;
438 lz_options->preset_dict_size = options->preset_dict_size;
443 length_encoder_reset(lzma_length_encoder *lencoder,
444 const uint32_t num_pos_states, const bool fast_mode)
446 bit_reset(lencoder->choice);
447 bit_reset(lencoder->choice2);
449 for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
450 bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS);
451 bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS);
454 bittree_reset(lencoder->high, LEN_HIGH_BITS);
457 for (size_t pos_state = 0; pos_state < num_pos_states;
459 length_update_prices(lencoder, pos_state);
466 lzma_lzma_encoder_reset(lzma_coder *coder, const lzma_options_lzma *options)
468 assert(!coder->is_flushed);
470 coder->pos_mask = (1U << options->pb) - 1;
471 coder->literal_context_bits = options->lc;
472 coder->literal_pos_mask = (1U << options->lp) - 1;
475 rc_reset(&coder->rc);
479 for (size_t i = 0; i < REP_DISTANCES; ++i)
482 literal_init(coder->literal, options->lc, options->lp);
485 for (size_t i = 0; i < STATES; ++i) {
486 for (size_t j = 0; j <= coder->pos_mask; ++j) {
487 bit_reset(coder->is_match[i][j]);
488 bit_reset(coder->is_rep0_long[i][j]);
491 bit_reset(coder->is_rep[i]);
492 bit_reset(coder->is_rep0[i]);
493 bit_reset(coder->is_rep1[i]);
494 bit_reset(coder->is_rep2[i]);
497 for (size_t i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
498 bit_reset(coder->pos_special[i]);
501 for (size_t i = 0; i < LEN_TO_POS_STATES; ++i)
502 bittree_reset(coder->pos_slot[i], POS_SLOT_BITS);
504 bittree_reset(coder->pos_align, ALIGN_BITS);
507 length_encoder_reset(&coder->match_len_encoder,
508 1U << options->pb, coder->fast_mode);
510 length_encoder_reset(&coder->rep_len_encoder,
511 1U << options->pb, coder->fast_mode);
513 // Price counts are incremented every time appropriate probabilities
514 // are changed. price counts are set to zero when the price tables
515 // are updated, which is done when the appropriate price counts have
516 // big enough value, and lzma_mf.read_ahead == 0 which happens at
517 // least every OPTS (a few thousand) possible price count increments.
519 // By resetting price counts to UINT32_MAX / 2, we make sure that the
520 // price tables will be initialized before they will be used (since
521 // the value is definitely big enough), and that it is OK to increment
522 // price counts without risk of integer overflow (since UINT32_MAX / 2
523 // is small enough). The current code doesn't increment price counts
524 // before initializing price tables, but it maybe done in future if
525 // we add support for saving the state between LZMA2 chunks.
526 coder->match_price_count = UINT32_MAX / 2;
527 coder->align_price_count = UINT32_MAX / 2;
529 coder->opts_end_index = 0;
530 coder->opts_current_index = 0;
535 lzma_lzma_encoder_create(lzma_coder **coder_ptr, lzma_allocator *allocator,
536 const lzma_options_lzma *options, lzma_lz_options *lz_options)
538 if (*coder_ptr == NULL) {
539 *coder_ptr = lzma_alloc(sizeof(lzma_coder), allocator);
540 if (*coder_ptr == NULL)
541 return LZMA_MEM_ERROR;
544 lzma_coder *coder = *coder_ptr;
546 // Validate some of the options. LZ encoder validates fast_bytes too
547 // but we need a valid value here earlier.
548 if (!is_lclppb_valid(options) || options->nice_len < MATCH_LEN_MIN
549 || options->nice_len > MATCH_LEN_MAX)
550 return LZMA_OPTIONS_ERROR;
552 // Set compression mode.
553 switch (options->mode) {
555 coder->fast_mode = true;
558 case LZMA_MODE_NORMAL: {
559 coder->fast_mode = false;
561 // Set dist_table_size.
562 // Round the dictionary size up to next 2^n.
563 uint32_t log_size = 0;
564 while ((UINT32_C(1) << log_size) < options->dict_size)
567 coder->dist_table_size = log_size * 2;
569 // Length encoders' price table size
570 coder->match_len_encoder.table_size
571 = options->nice_len + 1 - MATCH_LEN_MIN;
572 coder->rep_len_encoder.table_size
573 = options->nice_len + 1 - MATCH_LEN_MIN;
578 return LZMA_OPTIONS_ERROR;
581 coder->is_initialized = false;
582 coder->is_flushed = false;
584 lzma_lzma_encoder_reset(coder, options);
586 set_lz_options(lz_options, options);
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 return lzma_lz_encoder_init(
607 next, allocator, filters, &lzma_encoder_init);
612 lzma_lzma_encoder_memusage(const void *options)
614 lzma_lz_options lz_options;
615 set_lz_options(&lz_options, options);
617 const uint64_t lz_memusage = lzma_lz_encoder_memusage(&lz_options);
618 if (lz_memusage == UINT64_MAX)
621 return (uint64_t)(sizeof(lzma_coder)) + lz_memusage;
626 lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte)
628 if (!is_lclppb_valid(options))
631 *byte = (options->pb * 5 + options->lp) * 9 + options->lc;
632 assert(*byte <= (4 * 5 + 4) * 9 + 8);
638 #ifdef HAVE_ENCODER_LZMA1
640 lzma_lzma_props_encode(const void *options, uint8_t *out)
642 const lzma_options_lzma *const opt = options;
644 if (lzma_lzma_lclppb_encode(opt, out))
645 return LZMA_PROG_ERROR;
647 integer_write_32(out + 1, opt->dict_size);
654 extern LZMA_API lzma_bool
655 lzma_mode_is_available(lzma_mode mode)
657 return mode == LZMA_MODE_FAST || mode == LZMA_MODE_NORMAL;