6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
24 /* This a compressor for the LZMS compression format. More details about this
25 * format can be found in lzms-decompress.c.
27 * Also see lzx-compress.c for general information about match-finding and
28 * match-choosing that also applies to this LZMS compressor.
30 * NOTE: this compressor currently does not code any delta matches.
38 #include "wimlib/assert.h"
39 #include "wimlib/compiler.h"
40 #include "wimlib/compressor_ops.h"
41 #include "wimlib/compress_common.h"
42 #include "wimlib/endianness.h"
43 #include "wimlib/error.h"
44 #include "wimlib/lz.h"
45 #include "wimlib/lz_bt.h"
46 #include "wimlib/lzms.h"
47 #include "wimlib/util.h"
53 /* Stucture used for writing raw bits to the end of the LZMS-compressed data as
54 * a series of 16-bit little endian coding units. */
55 struct lzms_output_bitstream {
56 /* Buffer variable containing zero or more bits that have been logically
57 * written to the bitstream but not yet written to memory. This must be
58 * at least as large as the coding unit size. */
61 /* Number of bits in @bitbuf that are valid. */
62 unsigned num_free_bits;
64 /* Pointer to one past the next position in the compressed data buffer
65 * at which to output a 16-bit coding unit. */
68 /* Maximum number of 16-bit coding units that can still be output to
69 * the compressed data buffer. */
70 size_t num_le16_remaining;
72 /* Set to %true if not all coding units could be output due to
73 * insufficient space. */
77 /* Stucture used for range encoding (raw version). */
78 struct lzms_range_encoder_raw {
80 /* A 33-bit variable that holds the low boundary of the current range.
81 * The 33rd bit is needed to catch carries. */
84 /* Size of the current range. */
87 /* Next 16-bit coding unit to output. */
90 /* Number of 16-bit coding units whose output has been delayed due to
91 * possible carrying. The first such coding unit is @cache; all
92 * subsequent such coding units are 0xffff. */
95 /* Pointer to the next position in the compressed data buffer at which
96 * to output a 16-bit coding unit. */
99 /* Maximum number of 16-bit coding units that can still be output to
100 * the compressed data buffer. */
101 size_t num_le16_remaining;
103 /* %true when the very first coding unit has not yet been output. */
106 /* Set to %true if not all coding units could be output due to
107 * insufficient space. */
111 /* Structure used for range encoding. This wraps around `struct
112 * lzms_range_encoder_raw' to use and maintain probability entries. */
113 struct lzms_range_encoder {
114 /* Pointer to the raw range encoder, which has no persistent knowledge
115 * of probabilities. Multiple lzms_range_encoder's share the same
116 * lzms_range_encoder_raw. */
117 struct lzms_range_encoder_raw *rc;
119 /* Bits recently encoded by this range encoder. This is used as an
120 * index into @prob_entries. */
123 /* Bitmask for @state to prevent its value from exceeding the number of
124 * probability entries. */
127 /* Probability entries being used for this range encoder. */
128 struct lzms_probability_entry prob_entries[LZMS_MAX_NUM_STATES];
131 /* Structure used for Huffman encoding. */
132 struct lzms_huffman_encoder {
134 /* Bitstream to write Huffman-encoded symbols and verbatim bits to.
135 * Multiple lzms_huffman_encoder's share the same lzms_output_bitstream.
137 struct lzms_output_bitstream *os;
139 /* Number of symbols that have been written using this code far. Reset
140 * to 0 whenever the code is rebuilt. */
141 u32 num_syms_written;
143 /* When @num_syms_written reaches this number, the Huffman code must be
147 /* Number of symbols in the represented Huffman code. */
150 /* Running totals of symbol frequencies. These are diluted slightly
151 * whenever the code is rebuilt. */
152 u32 sym_freqs[LZMS_MAX_NUM_SYMS];
154 /* The length, in bits, of each symbol in the Huffman code. */
155 u8 lens[LZMS_MAX_NUM_SYMS];
157 /* The codeword of each symbol in the Huffman code. */
158 u32 codewords[LZMS_MAX_NUM_SYMS];
161 /* State of the LZMS compressor. */
162 struct lzms_compressor {
163 struct wimlib_lzms_compressor_params params;
165 /* Pointer to a buffer holding the preprocessed data to compress. */
168 /* Current position in @buffer. */
171 /* Size of the data in @buffer. */
174 /* Binary tree match-finder. */
177 /* Temporary space to store found matches. */
178 struct raw_match *matches;
180 /* Match-chooser data. */
181 struct lzms_mc_pos_data *optimum;
182 unsigned optimum_cur_idx;
183 unsigned optimum_end_idx;
185 /* Maximum block size this compressor instantiation allows. This is the
186 * allocated size of @window. */
189 /* Raw range encoder which outputs to the beginning of the compressed
190 * data buffer, proceeding forwards. */
191 struct lzms_range_encoder_raw rc;
193 /* Bitstream which outputs to the end of the compressed data buffer,
194 * proceeding backwards. */
195 struct lzms_output_bitstream os;
197 /* Range encoders. */
198 struct lzms_range_encoder main_range_encoder;
199 struct lzms_range_encoder match_range_encoder;
200 struct lzms_range_encoder lz_match_range_encoder;
201 struct lzms_range_encoder lz_repeat_match_range_encoders[LZMS_NUM_RECENT_OFFSETS - 1];
202 struct lzms_range_encoder delta_match_range_encoder;
203 struct lzms_range_encoder delta_repeat_match_range_encoders[LZMS_NUM_RECENT_OFFSETS - 1];
205 /* Huffman encoders. */
206 struct lzms_huffman_encoder literal_encoder;
207 struct lzms_huffman_encoder lz_offset_encoder;
208 struct lzms_huffman_encoder length_encoder;
209 struct lzms_huffman_encoder delta_power_encoder;
210 struct lzms_huffman_encoder delta_offset_encoder;
212 /* LRU (least-recently-used) queues for match information. */
213 struct lzms_lru_queues lru;
215 /* Used for preprocessing. */
216 s32 last_target_usages[65536];
219 struct lzms_mc_pos_data {
221 #define MC_INFINITE_COST ((u32)~0UL)
232 struct lzms_adaptive_state {
233 struct lzms_lz_lru_queues lru;
237 u8 lz_repeat_match_state[LZMS_NUM_RECENT_OFFSETS - 1];
241 /* Initialize the output bitstream @os to write forwards to the specified
242 * compressed data buffer @out that is @out_limit 16-bit integers long. */
244 lzms_output_bitstream_init(struct lzms_output_bitstream *os,
245 le16 *out, size_t out_limit)
248 os->num_free_bits = 16;
249 os->out = out + out_limit;
250 os->num_le16_remaining = out_limit;
254 /* Write @num_bits bits, contained in the low @num_bits bits of @bits (ordered
255 * from high-order to low-order), to the output bitstream @os. */
257 lzms_output_bitstream_put_bits(struct lzms_output_bitstream *os,
258 u32 bits, unsigned num_bits)
260 bits &= (1U << num_bits) - 1;
262 while (num_bits > os->num_free_bits) {
264 if (unlikely(os->num_le16_remaining == 0)) {
269 unsigned num_fill_bits = os->num_free_bits;
271 os->bitbuf <<= num_fill_bits;
272 os->bitbuf |= bits >> (num_bits - num_fill_bits);
274 *--os->out = cpu_to_le16(os->bitbuf);
275 --os->num_le16_remaining;
277 os->num_free_bits = 16;
278 num_bits -= num_fill_bits;
279 bits &= (1U << num_bits) - 1;
281 os->bitbuf <<= num_bits;
283 os->num_free_bits -= num_bits;
286 /* Flush the output bitstream, ensuring that all bits written to it have been
287 * written to memory. Returns %true if all bits were output successfully, or
288 * %false if an overrun occurred. */
290 lzms_output_bitstream_flush(struct lzms_output_bitstream *os)
292 if (os->num_free_bits != 16)
293 lzms_output_bitstream_put_bits(os, 0, os->num_free_bits + 1);
297 /* Initialize the range encoder @rc to write forwards to the specified
298 * compressed data buffer @out that is @out_limit 16-bit integers long. */
300 lzms_range_encoder_raw_init(struct lzms_range_encoder_raw *rc,
301 le16 *out, size_t out_limit)
304 rc->range = 0xffffffff;
308 rc->num_le16_remaining = out_limit;
314 * Attempt to flush bits from the range encoder.
316 * Note: this is based on the public domain code for LZMA written by Igor
317 * Pavlov. The only differences in this function are that in LZMS the bits must
318 * be output in 16-bit coding units instead of 8-bit coding units, and that in
319 * LZMS the first coding unit is not ignored by the decompressor, so the encoder
320 * cannot output a dummy value to that position.
322 * The basic idea is that we're writing bits from @rc->low to the output.
323 * However, due to carrying, the writing of coding units with value 0xffff, as
324 * well as one prior coding unit, must be delayed until it is determined whether
328 lzms_range_encoder_raw_shift_low(struct lzms_range_encoder_raw *rc)
330 LZMS_DEBUG("low=%"PRIx64", cache=%"PRIx64", cache_size=%u",
331 rc->low, rc->cache, rc->cache_size);
332 if ((u32)(rc->low) < 0xffff0000 ||
333 (u32)(rc->low >> 32) != 0)
335 /* Carry not needed (rc->low < 0xffff0000), or carry occurred
336 * ((rc->low >> 32) != 0, a.k.a. the carry bit is 1). */
339 if (rc->num_le16_remaining == 0) {
343 *rc->out++ = cpu_to_le16(rc->cache +
344 (u16)(rc->low >> 32));
345 --rc->num_le16_remaining;
351 } while (--rc->cache_size != 0);
353 rc->cache = (rc->low >> 16) & 0xffff;
356 rc->low = (rc->low & 0xffff) << 16;
360 lzms_range_encoder_raw_normalize(struct lzms_range_encoder_raw *rc)
362 if (rc->range <= 0xffff) {
364 lzms_range_encoder_raw_shift_low(rc);
369 lzms_range_encoder_raw_flush(struct lzms_range_encoder_raw *rc)
371 for (unsigned i = 0; i < 4; i++)
372 lzms_range_encoder_raw_shift_low(rc);
376 /* Encode the next bit using the range encoder (raw version).
378 * @prob is the chance out of LZMS_PROBABILITY_MAX that the next bit is 0. */
380 lzms_range_encoder_raw_encode_bit(struct lzms_range_encoder_raw *rc, int bit,
383 lzms_range_encoder_raw_normalize(rc);
385 u32 bound = (rc->range >> LZMS_PROBABILITY_BITS) * prob;
394 /* Encode a bit using the specified range encoder. This wraps around
395 * lzms_range_encoder_raw_encode_bit() to handle using and updating the
396 * appropriate probability table. */
398 lzms_range_encode_bit(struct lzms_range_encoder *enc, int bit)
400 struct lzms_probability_entry *prob_entry;
403 /* Load the probability entry corresponding to the current state. */
404 prob_entry = &enc->prob_entries[enc->state];
406 /* Treat the number of zero bits in the most recently encoded
407 * LZMS_PROBABILITY_MAX bits with this probability entry as the chance,
408 * out of LZMS_PROBABILITY_MAX, that the next bit will be a 0. However,
409 * don't allow 0% or 100% probabilities. */
410 prob = prob_entry->num_recent_zero_bits;
413 else if (prob == LZMS_PROBABILITY_MAX)
414 prob = LZMS_PROBABILITY_MAX - 1;
416 /* Encode the next bit. */
417 lzms_range_encoder_raw_encode_bit(enc->rc, bit, prob);
419 /* Update the state based on the newly encoded bit. */
420 enc->state = ((enc->state << 1) | bit) & enc->mask;
422 /* Update the recent bits, including the cached count of 0's. */
423 BUILD_BUG_ON(LZMS_PROBABILITY_MAX > sizeof(prob_entry->recent_bits) * 8);
425 if (prob_entry->recent_bits & (1ULL << (LZMS_PROBABILITY_MAX - 1))) {
426 /* Replacing 1 bit with 0 bit; increment the zero count.
428 prob_entry->num_recent_zero_bits++;
431 if (!(prob_entry->recent_bits & (1ULL << (LZMS_PROBABILITY_MAX - 1)))) {
432 /* Replacing 0 bit with 1 bit; decrement the zero count.
434 prob_entry->num_recent_zero_bits--;
437 prob_entry->recent_bits = (prob_entry->recent_bits << 1) | bit;
440 /* Encode a symbol using the specified Huffman encoder. */
442 lzms_huffman_encode_symbol(struct lzms_huffman_encoder *enc, u32 sym)
444 LZMS_ASSERT(sym < enc->num_syms);
445 lzms_output_bitstream_put_bits(enc->os,
448 ++enc->sym_freqs[sym];
449 if (++enc->num_syms_written == enc->rebuild_freq) {
450 /* Adaptive code needs to be rebuilt. */
451 LZMS_DEBUG("Rebuilding code (num_syms=%u)", enc->num_syms);
452 make_canonical_huffman_code(enc->num_syms,
453 LZMS_MAX_CODEWORD_LEN,
458 /* Dilute the frequencies. */
459 for (unsigned i = 0; i < enc->num_syms; i++) {
460 enc->sym_freqs[i] >>= 1;
461 enc->sym_freqs[i] += 1;
463 enc->num_syms_written = 0;
468 lzms_encode_length(struct lzms_huffman_encoder *enc, u32 length)
471 unsigned num_extra_bits;
474 slot = lzms_get_length_slot(length);
476 num_extra_bits = lzms_extra_length_bits[slot];
478 extra_bits = length - lzms_length_slot_base[slot];
480 lzms_huffman_encode_symbol(enc, slot);
481 lzms_output_bitstream_put_bits(enc->os, extra_bits, num_extra_bits);
485 lzms_encode_offset(struct lzms_huffman_encoder *enc, u32 offset)
488 unsigned num_extra_bits;
491 slot = lzms_get_position_slot(offset);
493 num_extra_bits = lzms_extra_position_bits[slot];
495 extra_bits = offset - lzms_position_slot_base[slot];
497 lzms_huffman_encode_symbol(enc, slot);
498 lzms_output_bitstream_put_bits(enc->os, extra_bits, num_extra_bits);
502 lzms_begin_encode_item(struct lzms_compressor *ctx)
504 ctx->lru.lz.upcoming_offset = 0;
505 ctx->lru.delta.upcoming_offset = 0;
506 ctx->lru.delta.upcoming_power = 0;
510 lzms_end_encode_item(struct lzms_compressor *ctx, u32 length)
512 LZMS_ASSERT(ctx->window_size - ctx->cur_window_pos >= length);
513 ctx->cur_window_pos += length;
514 lzms_update_lru_queues(&ctx->lru);
517 /* Encode a literal byte. */
519 lzms_encode_literal(struct lzms_compressor *ctx, u8 literal)
521 LZMS_DEBUG("Position %u: Encoding literal 0x%02x ('%c')",
522 ctx->cur_window_pos, literal, literal);
524 lzms_begin_encode_item(ctx);
526 /* Main bit: 0 = a literal, not a match. */
527 lzms_range_encode_bit(&ctx->main_range_encoder, 0);
529 /* Encode the literal using the current literal Huffman code. */
530 lzms_huffman_encode_symbol(&ctx->literal_encoder, literal);
532 lzms_end_encode_item(ctx, 1);
535 /* Encode a (length, offset) pair (LZ match). */
537 lzms_encode_lz_match(struct lzms_compressor *ctx, u32 length, u32 offset)
539 int recent_offset_idx;
541 LZMS_DEBUG("Position %u: Encoding LZ match {length=%u, offset=%u}",
542 ctx->cur_window_pos, length, offset);
544 LZMS_ASSERT(length <= ctx->window_size - ctx->cur_window_pos);
545 LZMS_ASSERT(offset <= ctx->cur_window_pos);
546 LZMS_ASSERT(!memcmp(&ctx->window[ctx->cur_window_pos],
547 &ctx->window[ctx->cur_window_pos - offset],
550 lzms_begin_encode_item(ctx);
552 /* Main bit: 1 = a match, not a literal. */
553 lzms_range_encode_bit(&ctx->main_range_encoder, 1);
555 /* Match bit: 0 = an LZ match, not a delta match. */
556 lzms_range_encode_bit(&ctx->match_range_encoder, 0);
558 /* Determine if the offset can be represented as a recent offset. */
559 for (recent_offset_idx = 0;
560 recent_offset_idx < LZMS_NUM_RECENT_OFFSETS;
562 if (offset == ctx->lru.lz.recent_offsets[recent_offset_idx])
565 if (recent_offset_idx == LZMS_NUM_RECENT_OFFSETS) {
566 /* Explicit offset. */
568 /* LZ match bit: 0 = explicit offset, not a recent offset. */
569 lzms_range_encode_bit(&ctx->lz_match_range_encoder, 0);
571 /* Encode the match offset. */
572 lzms_encode_offset(&ctx->lz_offset_encoder, offset);
578 /* LZ match bit: 1 = recent offset, not an explicit offset. */
579 lzms_range_encode_bit(&ctx->lz_match_range_encoder, 1);
581 /* Encode the recent offset index. A 1 bit is encoded for each
582 * index passed up. This sequence of 1 bits is terminated by a
583 * 0 bit, or automatically when (LZMS_NUM_RECENT_OFFSETS - 1) 1
584 * bits have been encoded. */
585 for (i = 0; i < recent_offset_idx; i++)
586 lzms_range_encode_bit(&ctx->lz_repeat_match_range_encoders[i], 1);
588 if (i < LZMS_NUM_RECENT_OFFSETS - 1)
589 lzms_range_encode_bit(&ctx->lz_repeat_match_range_encoders[i], 0);
591 /* Initial update of the LZ match offset LRU queue. */
592 for (; i < LZMS_NUM_RECENT_OFFSETS; i++)
593 ctx->lru.lz.recent_offsets[i] = ctx->lru.lz.recent_offsets[i + 1];
596 /* Encode the match length. */
597 lzms_encode_length(&ctx->length_encoder, length);
599 /* Save the match offset for later insertion at the front of the LZ
600 * match offset LRU queue. */
601 ctx->lru.lz.upcoming_offset = offset;
603 lzms_end_encode_item(ctx, length);
606 #define LZMS_COST_SHIFT 5
608 /*#define LZMS_RC_COSTS_USE_FLOATING_POINT*/
611 lzms_rc_costs[LZMS_PROBABILITY_MAX + 1];
613 #ifdef LZMS_RC_COSTS_USE_FLOATING_POINT
618 lzms_do_init_rc_costs(void)
620 /* Fill in a table that maps range coding probabilities needed to code a
621 * bit X (0 or 1) to the number of bits (scaled by a constant factor, to
622 * handle fractional costs) needed to code that bit X.
624 * Consider the range of the range decoder. To eliminate exactly half
625 * the range (logical probability of 0.5), we need exactly 1 bit. For
626 * lower probabilities we need more bits and for higher probabilities we
627 * need fewer bits. In general, a logical probability of N will
628 * eliminate the proportion 1 - N of the range; this information takes
629 * log2(1 / N) bits to encode.
631 * The below loop is simply calculating this number of bits for each
632 * possible probability allowed by the LZMS compression format, but
633 * without using real numbers. To handle fractional probabilities, each
634 * cost is multiplied by (1 << LZMS_COST_SHIFT). These techniques are
635 * based on those used by LZMA.
637 * Note that in LZMS, a probability x really means x / 64, and 0 / 64 is
638 * really interpreted as 1 / 64 and 64 / 64 is really interpreted as
641 for (u32 i = 0; i <= LZMS_PROBABILITY_MAX; i++) {
646 else if (prob == LZMS_PROBABILITY_MAX)
647 prob = LZMS_PROBABILITY_MAX - 1;
649 #ifdef LZMS_RC_COSTS_USE_FLOATING_POINT
650 lzms_rc_costs[i] = log2((double)LZMS_PROBABILITY_MAX / prob) *
651 (1 << LZMS_COST_SHIFT);
655 for (u32 j = 0; j < LZMS_COST_SHIFT; j++) {
658 while (w >= (1U << 16)) {
663 lzms_rc_costs[i] = (LZMS_PROBABILITY_BITS << LZMS_COST_SHIFT) -
670 lzms_init_rc_costs(void)
672 static pthread_once_t once = PTHREAD_ONCE_INIT;
674 pthread_once(&once, lzms_do_init_rc_costs);
678 * Return the cost to range-encode the specified bit when in the specified
681 * @enc The range encoder to use.
682 * @cur_state Current state, which indicates the probability entry to choose.
683 * Updated by this function.
684 * @bit The bit to encode (0 or 1).
687 lzms_rc_bit_cost(const struct lzms_range_encoder *enc, u8 *cur_state, int bit)
692 prob_zero = enc->prob_entries[*cur_state & enc->mask].num_recent_zero_bits;
694 *cur_state = (*cur_state << 1) | bit;
697 prob_correct = prob_zero;
699 prob_correct = LZMS_PROBABILITY_MAX - prob_zero;
701 return lzms_rc_costs[prob_correct];
705 lzms_huffman_symbol_cost(const struct lzms_huffman_encoder *enc, u32 sym)
707 return enc->lens[sym] << LZMS_COST_SHIFT;
711 lzms_offset_cost(const struct lzms_huffman_encoder *enc, u32 offset)
717 slot = lzms_get_position_slot(offset);
719 cost += lzms_huffman_symbol_cost(enc, slot);
721 num_extra_bits = lzms_extra_position_bits[slot];
723 cost += num_extra_bits << LZMS_COST_SHIFT;
729 lzms_get_length_cost(const struct lzms_huffman_encoder *enc, u32 length)
735 slot = lzms_get_length_slot(length);
737 cost += lzms_huffman_symbol_cost(enc, slot);
739 num_extra_bits = lzms_extra_length_bits[slot];
741 cost += num_extra_bits << LZMS_COST_SHIFT;
747 lzms_get_matches(struct lzms_compressor *ctx, struct raw_match **matches_ret)
749 *matches_ret = ctx->matches;
750 return lz_bt_get_matches(&ctx->mf, ctx->matches);
754 lzms_skip_bytes(struct lzms_compressor *ctx, u32 n)
756 lz_bt_skip_positions(&ctx->mf, n);
760 lzms_get_literal_cost(struct lzms_compressor *ctx,
761 struct lzms_adaptive_state *state, u8 literal)
765 state->lru.upcoming_offset = 0;
766 lzms_update_lz_lru_queues(&state->lru);
768 cost += lzms_rc_bit_cost(&ctx->main_range_encoder,
769 &state->main_state, 0);
771 cost += lzms_huffman_symbol_cost(&ctx->literal_encoder, literal);
777 lzms_get_lz_match_cost_nolen(struct lzms_compressor *ctx,
778 struct lzms_adaptive_state *state, u32 offset)
781 int recent_offset_idx;
783 cost += lzms_rc_bit_cost(&ctx->main_range_encoder,
784 &state->main_state, 1);
785 cost += lzms_rc_bit_cost(&ctx->match_range_encoder,
786 &state->match_state, 0);
788 for (recent_offset_idx = 0;
789 recent_offset_idx < LZMS_NUM_RECENT_OFFSETS;
791 if (offset == state->lru.recent_offsets[recent_offset_idx])
794 if (recent_offset_idx == LZMS_NUM_RECENT_OFFSETS) {
795 /* Explicit offset. */
796 cost += lzms_rc_bit_cost(&ctx->lz_match_range_encoder,
797 &state->lz_match_state, 0);
799 cost += lzms_offset_cost(&ctx->lz_offset_encoder, offset);
804 cost += lzms_rc_bit_cost(&ctx->lz_match_range_encoder,
805 &state->lz_match_state, 1);
807 for (i = 0; i < recent_offset_idx; i++)
808 cost += lzms_rc_bit_cost(&ctx->lz_repeat_match_range_encoders[i],
809 &state->lz_repeat_match_state[i], 0);
811 if (i < LZMS_NUM_RECENT_OFFSETS - 1)
812 cost += lzms_rc_bit_cost(&ctx->lz_repeat_match_range_encoders[i],
813 &state->lz_repeat_match_state[i], 1);
816 /* Initial update of the LZ match offset LRU queue. */
817 for (; i < LZMS_NUM_RECENT_OFFSETS; i++)
818 state->lru.recent_offsets[i] = state->lru.recent_offsets[i + 1];
822 state->lru.upcoming_offset = offset;
823 lzms_update_lz_lru_queues(&state->lru);
829 lzms_get_lz_match_cost(struct lzms_compressor *ctx,
830 struct lzms_adaptive_state *state,
831 u32 length, u32 offset)
833 return lzms_get_lz_match_cost_nolen(ctx, state, offset) +
834 lzms_get_length_cost(&ctx->length_encoder, length);
837 static struct raw_match
838 lzms_match_chooser_reverse_list(struct lzms_compressor *ctx, unsigned cur_pos)
840 unsigned prev_link, saved_prev_link;
841 unsigned prev_match_offset, saved_prev_match_offset;
843 ctx->optimum_end_idx = cur_pos;
845 saved_prev_link = ctx->optimum[cur_pos].prev.link;
846 saved_prev_match_offset = ctx->optimum[cur_pos].prev.match_offset;
849 prev_link = saved_prev_link;
850 prev_match_offset = saved_prev_match_offset;
852 saved_prev_link = ctx->optimum[prev_link].prev.link;
853 saved_prev_match_offset = ctx->optimum[prev_link].prev.match_offset;
855 ctx->optimum[prev_link].next.link = cur_pos;
856 ctx->optimum[prev_link].next.match_offset = prev_match_offset;
859 } while (cur_pos != 0);
861 ctx->optimum_cur_idx = ctx->optimum[0].next.link;
863 return (struct raw_match)
864 { .len = ctx->optimum_cur_idx,
865 .offset = ctx->optimum[0].next.match_offset,
869 /* This is similar to lzx_get_near_optimal_match() in lzx-compress.c.
870 * Read that one if you want to understand it. */
871 static struct raw_match
872 lzms_get_near_optimal_match(struct lzms_compressor *ctx)
875 struct raw_match *matches;
876 struct raw_match match;
879 u32 longest_rep_offset;
882 struct lzms_adaptive_state initial_state;
884 if (ctx->optimum_cur_idx != ctx->optimum_end_idx) {
885 match.len = ctx->optimum[ctx->optimum_cur_idx].next.link -
886 ctx->optimum_cur_idx;
887 match.offset = ctx->optimum[ctx->optimum_cur_idx].next.match_offset;
889 ctx->optimum_cur_idx = ctx->optimum[ctx->optimum_cur_idx].next.link;
893 ctx->optimum_cur_idx = 0;
894 ctx->optimum_end_idx = 0;
896 longest_rep_len = ctx->params.min_match_length - 1;
897 if (lz_bt_get_position(&ctx->mf) >= LZMS_MAX_INIT_RECENT_OFFSET) {
898 u32 limit = min(ctx->params.max_match_length,
899 lz_bt_get_remaining_size(&ctx->mf));
900 for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS; i++) {
901 u32 offset = ctx->lru.lz.recent_offsets[i];
902 const u8 *strptr = lz_bt_get_window_ptr(&ctx->mf);
903 const u8 *matchptr = strptr - offset;
905 while (len < limit && strptr[len] == matchptr[len])
907 if (len > longest_rep_len) {
908 longest_rep_len = len;
909 longest_rep_offset = offset;
914 if (longest_rep_len >= ctx->params.nice_match_length) {
915 lzms_skip_bytes(ctx, longest_rep_len);
916 return (struct raw_match) {
917 .len = longest_rep_len,
918 .offset = longest_rep_offset,
922 num_matches = lzms_get_matches(ctx, &matches);
925 longest_len = matches[num_matches - 1].len;
926 if (longest_len >= ctx->params.nice_match_length) {
927 lzms_skip_bytes(ctx, longest_len - 1);
928 return matches[num_matches - 1];
934 initial_state.lru = ctx->lru.lz;
935 initial_state.main_state = ctx->main_range_encoder.state;
936 initial_state.match_state = ctx->match_range_encoder.state;
937 initial_state.lz_match_state = ctx->lz_match_range_encoder.state;
938 for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS - 1; i++)
939 initial_state.lz_repeat_match_state[i] = ctx->lz_repeat_match_range_encoders[i].state;
941 ctx->optimum[1].state = initial_state;
942 ctx->optimum[1].cost = lzms_get_literal_cost(ctx,
943 &ctx->optimum[1].state,
944 *(lz_bt_get_window_ptr(&ctx->mf) - 1));
945 ctx->optimum[1].prev.link = 0;
947 for (u32 i = 0, len = 2; i < num_matches; i++) {
948 u32 offset = matches[i].offset;
949 struct lzms_adaptive_state state;
952 state = initial_state;
954 position_cost += lzms_get_lz_match_cost_nolen(ctx, &state, offset);
959 cost = position_cost;
960 cost += lzms_get_length_cost(&ctx->length_encoder, len);
962 ctx->optimum[len].state = state;
963 ctx->optimum[len].prev.link = 0;
964 ctx->optimum[len].prev.match_offset = offset;
965 ctx->optimum[len].cost = cost;
966 } while (++len <= matches[i].len);
968 end_pos = longest_len;
970 if (longest_rep_len >= ctx->params.min_match_length) {
971 struct lzms_adaptive_state state;
974 while (end_pos < longest_rep_len)
975 ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
977 state = initial_state;
978 cost = lzms_get_lz_match_cost(ctx,
982 if (cost <= ctx->optimum[longest_rep_len].cost) {
983 ctx->optimum[longest_rep_len].state = state;
984 ctx->optimum[longest_rep_len].prev.link = 0;
985 ctx->optimum[longest_rep_len].prev.match_offset = longest_rep_offset;
986 ctx->optimum[longest_rep_len].cost = cost;
993 struct lzms_adaptive_state state;
997 if (cur_pos == end_pos || cur_pos == ctx->params.optim_array_length)
998 return lzms_match_chooser_reverse_list(ctx, cur_pos);
1000 longest_rep_len = ctx->params.min_match_length - 1;
1001 if (lz_bt_get_position(&ctx->mf) >= LZMS_MAX_INIT_RECENT_OFFSET) {
1002 u32 limit = min(ctx->params.max_match_length,
1003 lz_bt_get_remaining_size(&ctx->mf));
1004 for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS; i++) {
1005 u32 offset = ctx->optimum[cur_pos].state.lru.recent_offsets[i];
1006 const u8 *strptr = lz_bt_get_window_ptr(&ctx->mf);
1007 const u8 *matchptr = strptr - offset;
1009 while (len < limit && strptr[len] == matchptr[len])
1011 if (len > longest_rep_len) {
1012 longest_rep_len = len;
1013 longest_rep_offset = offset;
1018 if (longest_rep_len >= ctx->params.nice_match_length) {
1019 match = lzms_match_chooser_reverse_list(ctx, cur_pos);
1021 ctx->optimum[cur_pos].next.match_offset = longest_rep_offset;
1022 ctx->optimum[cur_pos].next.link = cur_pos + longest_rep_len;
1023 ctx->optimum_end_idx = cur_pos + longest_rep_len;
1025 lzms_skip_bytes(ctx, longest_rep_len);
1030 num_matches = lzms_get_matches(ctx, &matches);
1033 longest_len = matches[num_matches - 1].len;
1034 if (longest_len >= ctx->params.nice_match_length) {
1035 match = lzms_match_chooser_reverse_list(ctx, cur_pos);
1037 ctx->optimum[cur_pos].next.match_offset =
1038 matches[num_matches - 1].offset;
1039 ctx->optimum[cur_pos].next.link = cur_pos + longest_len;
1040 ctx->optimum_end_idx = cur_pos + longest_len;
1042 lzms_skip_bytes(ctx, longest_len - 1);
1050 while (end_pos < cur_pos + longest_len)
1051 ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
1053 state = ctx->optimum[cur_pos].state;
1054 cost = ctx->optimum[cur_pos].cost +
1055 lzms_get_literal_cost(ctx,
1057 *(lz_bt_get_window_ptr(&ctx->mf) - 1));
1058 if (cost < ctx->optimum[cur_pos + 1].cost) {
1059 ctx->optimum[cur_pos + 1].state = state;
1060 ctx->optimum[cur_pos + 1].cost = cost;
1061 ctx->optimum[cur_pos + 1].prev.link = cur_pos;
1064 for (u32 i = 0, len = 2; i < num_matches; i++) {
1065 u32 offset = matches[i].offset;
1066 struct lzms_adaptive_state state;
1069 state = ctx->optimum[cur_pos].state;
1070 position_cost = ctx->optimum[cur_pos].cost;
1071 position_cost += lzms_get_lz_match_cost_nolen(ctx, &state, offset);
1076 cost = position_cost;
1077 cost += lzms_get_length_cost(&ctx->length_encoder, len);
1079 if (cost < ctx->optimum[cur_pos + len].cost) {
1080 ctx->optimum[cur_pos + len].state = state;
1081 ctx->optimum[cur_pos + len].prev.link = cur_pos;
1082 ctx->optimum[cur_pos + len].prev.match_offset = offset;
1083 ctx->optimum[cur_pos + len].cost = cost;
1085 } while (++len <= matches[i].len);
1088 if (longest_rep_len >= ctx->params.min_match_length) {
1090 while (end_pos < cur_pos + longest_rep_len)
1091 ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
1093 state = ctx->optimum[cur_pos].state;
1095 cost = ctx->optimum[cur_pos].cost +
1096 lzms_get_lz_match_cost(ctx,
1099 longest_rep_offset);
1100 if (cost <= ctx->optimum[cur_pos + longest_rep_len].cost) {
1101 ctx->optimum[cur_pos + longest_rep_len].state =
1103 ctx->optimum[cur_pos + longest_rep_len].prev.link =
1105 ctx->optimum[cur_pos + longest_rep_len].prev.match_offset =
1107 ctx->optimum[cur_pos + longest_rep_len].cost =
1115 * The main loop for the LZMS compressor.
1119 * - This uses near-optimal LZ parsing backed by a binary tree match-finder.
1121 * - This does not output any delta matches.
1123 * - The costs of literals and matches are estimated using the range encoder
1124 * states and the semi-adaptive Huffman codes. Except for range encoding
1125 * states, costs are assumed to be constant throughout a single run of the
1126 * parsing algorithm, which can parse up to @optim_array_length (from the
1127 * `struct wimlib_lzms_compressor_params') bytes of data. This introduces a
1128 * source of inaccuracy because the probabilities and Huffman codes can change
1129 * over this part of the data.
1132 lzms_encode(struct lzms_compressor *ctx)
1134 struct raw_match match;
1136 /* Load window into the binary tree match-finder. */
1137 lz_bt_load_window(&ctx->mf, ctx->window, ctx->window_size);
1139 /* Reset the match-chooser. */
1140 ctx->optimum_cur_idx = 0;
1141 ctx->optimum_end_idx = 0;
1143 while (ctx->cur_window_pos != ctx->window_size) {
1144 match = lzms_get_near_optimal_match(ctx);
1146 lzms_encode_literal(ctx, ctx->window[ctx->cur_window_pos]);
1148 lzms_encode_lz_match(ctx, match.len, match.offset);
1153 lzms_init_range_encoder(struct lzms_range_encoder *enc,
1154 struct lzms_range_encoder_raw *rc, u32 num_states)
1158 enc->mask = num_states - 1;
1159 for (u32 i = 0; i < num_states; i++) {
1160 enc->prob_entries[i].num_recent_zero_bits = LZMS_INITIAL_PROBABILITY;
1161 enc->prob_entries[i].recent_bits = LZMS_INITIAL_RECENT_BITS;
1166 lzms_init_huffman_encoder(struct lzms_huffman_encoder *enc,
1167 struct lzms_output_bitstream *os,
1169 unsigned rebuild_freq)
1172 enc->num_syms_written = 0;
1173 enc->rebuild_freq = rebuild_freq;
1174 enc->num_syms = num_syms;
1175 for (unsigned i = 0; i < num_syms; i++)
1176 enc->sym_freqs[i] = 1;
1178 make_canonical_huffman_code(enc->num_syms,
1179 LZMS_MAX_CODEWORD_LEN,
1185 /* Initialize the LZMS compressor. */
1187 lzms_init_compressor(struct lzms_compressor *ctx, const u8 *udata, u32 ulen,
1188 le16 *cdata, u32 clen16)
1190 unsigned num_position_slots;
1192 /* Copy the uncompressed data into the @ctx->window buffer. */
1193 memcpy(ctx->window, udata, ulen);
1194 ctx->cur_window_pos = 0;
1195 ctx->window_size = ulen;
1197 /* Initialize the raw range encoder (writing forwards). */
1198 lzms_range_encoder_raw_init(&ctx->rc, cdata, clen16);
1200 /* Initialize the output bitstream for Huffman symbols and verbatim bits
1201 * (writing backwards). */
1202 lzms_output_bitstream_init(&ctx->os, cdata, clen16);
1204 /* Calculate the number of position slots needed for this compressed
1206 num_position_slots = lzms_get_position_slot(ulen - 1) + 1;
1208 LZMS_DEBUG("Using %u position slots", num_position_slots);
1210 /* Initialize Huffman encoders for each alphabet used in the compressed
1211 * representation. */
1212 lzms_init_huffman_encoder(&ctx->literal_encoder, &ctx->os,
1213 LZMS_NUM_LITERAL_SYMS,
1214 LZMS_LITERAL_CODE_REBUILD_FREQ);
1216 lzms_init_huffman_encoder(&ctx->lz_offset_encoder, &ctx->os,
1218 LZMS_LZ_OFFSET_CODE_REBUILD_FREQ);
1220 lzms_init_huffman_encoder(&ctx->length_encoder, &ctx->os,
1222 LZMS_LENGTH_CODE_REBUILD_FREQ);
1224 lzms_init_huffman_encoder(&ctx->delta_offset_encoder, &ctx->os,
1226 LZMS_DELTA_OFFSET_CODE_REBUILD_FREQ);
1228 lzms_init_huffman_encoder(&ctx->delta_power_encoder, &ctx->os,
1229 LZMS_NUM_DELTA_POWER_SYMS,
1230 LZMS_DELTA_POWER_CODE_REBUILD_FREQ);
1232 /* Initialize range encoders, all of which wrap around the same
1233 * lzms_range_encoder_raw. */
1234 lzms_init_range_encoder(&ctx->main_range_encoder,
1235 &ctx->rc, LZMS_NUM_MAIN_STATES);
1237 lzms_init_range_encoder(&ctx->match_range_encoder,
1238 &ctx->rc, LZMS_NUM_MATCH_STATES);
1240 lzms_init_range_encoder(&ctx->lz_match_range_encoder,
1241 &ctx->rc, LZMS_NUM_LZ_MATCH_STATES);
1243 for (size_t i = 0; i < ARRAY_LEN(ctx->lz_repeat_match_range_encoders); i++)
1244 lzms_init_range_encoder(&ctx->lz_repeat_match_range_encoders[i],
1245 &ctx->rc, LZMS_NUM_LZ_REPEAT_MATCH_STATES);
1247 lzms_init_range_encoder(&ctx->delta_match_range_encoder,
1248 &ctx->rc, LZMS_NUM_DELTA_MATCH_STATES);
1250 for (size_t i = 0; i < ARRAY_LEN(ctx->delta_repeat_match_range_encoders); i++)
1251 lzms_init_range_encoder(&ctx->delta_repeat_match_range_encoders[i],
1252 &ctx->rc, LZMS_NUM_DELTA_REPEAT_MATCH_STATES);
1254 /* Initialize LRU match information. */
1255 lzms_init_lru_queues(&ctx->lru);
1258 /* Flush the output streams, prepare the final compressed data, and return its
1261 * A return value of 0 indicates that the data could not be compressed to fit in
1262 * the available space. */
1264 lzms_finalize(struct lzms_compressor *ctx, u8 *cdata, size_t csize_avail)
1266 size_t num_forwards_bytes;
1267 size_t num_backwards_bytes;
1268 size_t compressed_size;
1270 /* Flush both the forwards and backwards streams, and make sure they
1271 * didn't cross each other and start overwriting each other's data. */
1272 if (!lzms_output_bitstream_flush(&ctx->os)) {
1273 LZMS_DEBUG("Backwards bitstream overrun.");
1277 if (!lzms_range_encoder_raw_flush(&ctx->rc)) {
1278 LZMS_DEBUG("Forwards bitstream overrun.");
1282 if (ctx->rc.out > ctx->os.out) {
1283 LZMS_DEBUG("Two bitstreams crossed.");
1287 /* Now the compressed buffer contains the data output by the forwards
1288 * bitstream, then empty space, then data output by the backwards
1289 * bitstream. Move the data output by the backwards bitstream to be
1290 * adjacent to the data output by the forward bitstream, and calculate
1291 * the compressed size that this results in. */
1292 num_forwards_bytes = (u8*)ctx->rc.out - (u8*)cdata;
1293 num_backwards_bytes = ((u8*)cdata + csize_avail) - (u8*)ctx->os.out;
1295 memmove(cdata + num_forwards_bytes, ctx->os.out, num_backwards_bytes);
1297 compressed_size = num_forwards_bytes + num_backwards_bytes;
1298 LZMS_DEBUG("num_forwards_bytes=%zu, num_backwards_bytes=%zu, "
1299 "compressed_size=%zu",
1300 num_forwards_bytes, num_backwards_bytes, compressed_size);
1301 LZMS_ASSERT(compressed_size % 2 == 0);
1302 return compressed_size;
1306 lzms_compress(const void *uncompressed_data, size_t uncompressed_size,
1307 void *compressed_data, size_t compressed_size_avail, void *_ctx)
1309 struct lzms_compressor *ctx = _ctx;
1310 size_t compressed_size;
1312 LZMS_DEBUG("uncompressed_size=%zu, compressed_size_avail=%zu",
1313 uncompressed_size, compressed_size_avail);
1315 /* Make sure the uncompressed size is compatible with this compressor.
1317 if (uncompressed_size > ctx->max_block_size) {
1318 LZMS_DEBUG("Can't compress %zu bytes: LZMS context "
1319 "only supports %u bytes",
1320 uncompressed_size, ctx->max_block_size);
1324 /* Don't bother compressing extremely small inputs. */
1325 if (uncompressed_size < 4) {
1326 LZMS_DEBUG("Input too small to bother compressing.");
1330 /* Cap the available compressed size to a 32-bit integer and round it
1331 * down to the nearest multiple of 2. */
1332 if (compressed_size_avail > UINT32_MAX)
1333 compressed_size_avail = UINT32_MAX;
1334 if (compressed_size_avail & 1)
1335 compressed_size_avail--;
1337 /* Initialize the compressor structures. */
1338 lzms_init_compressor(ctx, uncompressed_data, uncompressed_size,
1339 compressed_data, compressed_size_avail / 2);
1341 /* Preprocess the uncompressed data. */
1342 lzms_x86_filter(ctx->window, ctx->window_size,
1343 ctx->last_target_usages, false);
1345 /* Compute and encode a literal/match sequence that decompresses to the
1346 * preprocessed data. */
1349 /* Get and return the compressed data size. */
1350 compressed_size = lzms_finalize(ctx, compressed_data,
1351 compressed_size_avail);
1353 if (compressed_size == 0) {
1354 LZMS_DEBUG("Data did not compress to requested size or less.");
1358 LZMS_DEBUG("Compressed %zu => %zu bytes",
1359 uncompressed_size, compressed_size);
1361 #if defined(ENABLE_VERIFY_COMPRESSION) || defined(ENABLE_LZMS_DEBUG)
1362 /* Verify that we really get the same thing back when decompressing. */
1364 struct wimlib_decompressor *decompressor;
1366 LZMS_DEBUG("Verifying LZMS compression.");
1368 if (0 == wimlib_create_decompressor(WIMLIB_COMPRESSION_TYPE_LZMS,
1369 ctx->max_block_size,
1374 ret = wimlib_decompress(compressed_data,
1379 wimlib_free_decompressor(decompressor);
1382 ERROR("Failed to decompress data we "
1383 "compressed using LZMS algorithm");
1387 if (memcmp(uncompressed_data, ctx->window,
1390 ERROR("Data we compressed using LZMS algorithm "
1391 "didn't decompress to original");
1396 WARNING("Failed to create decompressor for "
1397 "data verification!");
1400 #endif /* ENABLE_LZMS_DEBUG || ENABLE_VERIFY_COMPRESSION */
1402 return compressed_size;
1406 lzms_free_compressor(void *_ctx)
1408 struct lzms_compressor *ctx = _ctx;
1413 lz_bt_destroy(&ctx->mf);
1419 static const struct wimlib_lzms_compressor_params lzms_default = {
1421 .size = sizeof(struct wimlib_lzms_compressor_params),
1423 .min_match_length = 2,
1424 .max_match_length = UINT32_MAX,
1425 .nice_match_length = 32,
1426 .max_search_depth = 50,
1427 .optim_array_length = 1024,
1431 lzms_params_valid(const struct wimlib_compressor_params_header *);
1433 static const struct wimlib_lzms_compressor_params *
1434 lzms_get_params(const struct wimlib_compressor_params_header *_params)
1436 const struct wimlib_lzms_compressor_params *params =
1437 (const struct wimlib_lzms_compressor_params*)_params;
1440 params = &lzms_default;
1442 LZMS_ASSERT(lzms_params_valid(¶ms->hdr));
1448 lzms_create_compressor(size_t max_block_size,
1449 const struct wimlib_compressor_params_header *_params,
1452 struct lzms_compressor *ctx;
1453 const struct wimlib_lzms_compressor_params *params = lzms_get_params(_params);
1455 if (max_block_size == 0 || max_block_size >= INT32_MAX) {
1456 LZMS_DEBUG("Invalid max_block_size (%u)", max_block_size);
1457 return WIMLIB_ERR_INVALID_PARAM;
1460 ctx = CALLOC(1, sizeof(struct lzms_compressor));
1464 ctx->window = MALLOC(max_block_size);
1465 if (ctx->window == NULL)
1468 ctx->matches = MALLOC(min(params->max_match_length -
1469 params->min_match_length + 1,
1470 params->max_search_depth + 2) *
1471 sizeof(ctx->matches[0]));
1472 if (ctx->matches == NULL)
1475 if (!lz_bt_init(&ctx->mf,
1477 params->min_match_length,
1478 params->max_match_length,
1479 params->nice_match_length,
1480 params->max_search_depth))
1483 ctx->optimum = MALLOC((params->optim_array_length +
1484 min(params->nice_match_length,
1485 params->max_match_length)) *
1486 sizeof(ctx->optimum[0]));
1490 /* Initialize position and length slot data if not done already. */
1493 /* Initialize range encoding cost table if not done already. */
1494 lzms_init_rc_costs();
1496 ctx->max_block_size = max_block_size;
1497 memcpy(&ctx->params, params, sizeof(*params));
1503 lzms_free_compressor(ctx);
1504 return WIMLIB_ERR_NOMEM;
1508 lzms_get_needed_memory(size_t max_block_size,
1509 const struct wimlib_compressor_params_header *_params)
1511 const struct wimlib_lzms_compressor_params *params = lzms_get_params(_params);
1515 size += max_block_size;
1516 size += sizeof(struct lzms_compressor);
1517 size += lz_bt_get_needed_memory(max_block_size);
1518 size += (params->optim_array_length +
1519 min(params->nice_match_length,
1520 params->max_match_length)) *
1521 sizeof(((struct lzms_compressor *)0)->optimum[0]);
1522 size += min(params->max_match_length - params->min_match_length + 1,
1523 params->max_search_depth + 2) *
1524 sizeof(((struct lzms_compressor*)0)->matches[0]);
1529 lzms_params_valid(const struct wimlib_compressor_params_header *_params)
1531 const struct wimlib_lzms_compressor_params *params =
1532 (const struct wimlib_lzms_compressor_params*)_params;
1534 if (params->hdr.size != sizeof(*params) ||
1535 params->max_match_length < params->min_match_length ||
1536 params->min_match_length < 2 ||
1537 params->optim_array_length == 0 ||
1538 min(params->max_match_length, params->nice_match_length) > 65536)
1544 const struct compressor_ops lzms_compressor_ops = {
1545 .params_valid = lzms_params_valid,
1546 .get_needed_memory = lzms_get_needed_memory,
1547 .create_compressor = lzms_create_compressor,
1548 .compress = lzms_compress,
1549 .free_compressor = lzms_free_compressor,