]> wimlib.net Git - wimlib/blob - src/lzms-compress.c
da1904fc9f66cf8927db8a08cc31176a7e9c4c51
[wimlib] / src / lzms-compress.c
1 /*
2  * lzms-compress.c
3  *
4  * A compressor that produces output compatible with the LZMS compression format.
5  */
6
7 /*
8  * Copyright (C) 2013, 2014 Eric Biggers
9  *
10  * This file is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option) any
13  * later version.
14  *
15  * This file is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this file; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/compress_common.h"
29 #include "wimlib/compressor_ops.h"
30 #include "wimlib/endianness.h"
31 #include "wimlib/error.h"
32 #include "wimlib/lz_mf.h"
33 #include "wimlib/lz_repsearch.h"
34 #include "wimlib/lzms.h"
35 #include "wimlib/util.h"
36
37 #include <string.h>
38 #include <limits.h>
39 #include <pthread.h>
40
41 /* Stucture used for writing raw bits as a series of 16-bit little endian coding
42  * units.  This starts at the *end* of the compressed data buffer and proceeds
43  * backwards.  */
44 struct lzms_output_bitstream {
45
46         /* Bits that haven't yet been written to the output buffer.  */
47         u64 bitbuf;
48
49         /* Number of bits currently held in @bitbuf.  */
50         unsigned bitcount;
51
52         /* Pointer to one past the next position in the compressed data buffer
53          * at which to output a 16-bit coding unit.  */
54         le16 *next;
55
56         /* Pointer to the beginning of the output buffer.  (The "end" when
57          * writing backwards!)  */
58         le16 *begin;
59 };
60
61 /* Stucture used for range encoding (raw version).  This starts at the
62  * *beginning* of the compressed data buffer and proceeds forward.  */
63 struct lzms_range_encoder_raw {
64
65         /* A 33-bit variable that holds the low boundary of the current range.
66          * The 33rd bit is needed to catch carries.  */
67         u64 low;
68
69         /* Size of the current range.  */
70         u32 range;
71
72         /* Next 16-bit coding unit to output.  */
73         u16 cache;
74
75         /* Number of 16-bit coding units whose output has been delayed due to
76          * possible carrying.  The first such coding unit is @cache; all
77          * subsequent such coding units are 0xffff.  */
78         u32 cache_size;
79
80         /* Pointer to the beginning of the output buffer.  */
81         le16 *begin;
82
83         /* Pointer to the position in the output buffer at which the next coding
84          * unit must be written.  */
85         le16 *next;
86
87         /* Pointer just past the end of the output buffer.  */
88         le16 *end;
89 };
90
91 /* Structure used for range encoding.  This wraps around `struct
92  * lzms_range_encoder_raw' to use and maintain probability entries.  */
93 struct lzms_range_encoder {
94
95         /* Pointer to the raw range encoder, which has no persistent knowledge
96          * of probabilities.  Multiple lzms_range_encoder's share the same
97          * lzms_range_encoder_raw.  */
98         struct lzms_range_encoder_raw *rc;
99
100         /* Bits recently encoded by this range encoder.  This is used as an
101          * index into @prob_entries.  */
102         u32 state;
103
104         /* Bitmask for @state to prevent its value from exceeding the number of
105          * probability entries.  */
106         u32 mask;
107
108         /* Probability entries being used for this range encoder.  */
109         struct lzms_probability_entry prob_entries[LZMS_MAX_NUM_STATES];
110 };
111
112 /* Structure used for Huffman encoding.  */
113 struct lzms_huffman_encoder {
114
115         /* Bitstream to write Huffman-encoded symbols and verbatim bits to.
116          * Multiple lzms_huffman_encoder's share the same lzms_output_bitstream.
117          */
118         struct lzms_output_bitstream *os;
119
120         /* Number of symbols that have been written using this code far.  Reset
121          * to 0 whenever the code is rebuilt.  */
122         u32 num_syms_written;
123
124         /* When @num_syms_written reaches this number, the Huffman code must be
125          * rebuilt.  */
126         u32 rebuild_freq;
127
128         /* Number of symbols in the represented Huffman code.  */
129         unsigned num_syms;
130
131         /* Running totals of symbol frequencies.  These are diluted slightly
132          * whenever the code is rebuilt.  */
133         u32 sym_freqs[LZMS_MAX_NUM_SYMS];
134
135         /* The length, in bits, of each symbol in the Huffman code.  */
136         u8 lens[LZMS_MAX_NUM_SYMS];
137
138         /* The codeword of each symbol in the Huffman code.  */
139         u32 codewords[LZMS_MAX_NUM_SYMS];
140 };
141
142 /* Internal compression parameters  */
143 struct lzms_compressor_params {
144         u32 min_match_length;
145         u32 nice_match_length;
146         u32 max_search_depth;
147         u32 optim_array_length;
148 };
149
150 /* State of the LZMS compressor  */
151 struct lzms_compressor {
152
153         /* Internal compression parameters  */
154         struct lzms_compressor_params params;
155
156         /* Data currently being compressed  */
157         u8 *cur_window;
158         u32 cur_window_size;
159
160         /* Lempel-Ziv match-finder  */
161         struct lz_mf *mf;
162
163         /* Temporary space to store found matches  */
164         struct lz_match *matches;
165
166         /* Per-position data for near-optimal parsing  */
167         struct lzms_mc_pos_data *optimum;
168         struct lzms_mc_pos_data *optimum_end;
169
170         /* Raw range encoder which outputs to the beginning of the compressed
171          * data buffer, proceeding forwards  */
172         struct lzms_range_encoder_raw rc;
173
174         /* Bitstream which outputs to the end of the compressed data buffer,
175          * proceeding backwards  */
176         struct lzms_output_bitstream os;
177
178         /* Range encoders  */
179         struct lzms_range_encoder main_range_encoder;
180         struct lzms_range_encoder match_range_encoder;
181         struct lzms_range_encoder lz_match_range_encoder;
182         struct lzms_range_encoder lz_repeat_match_range_encoders[LZMS_NUM_RECENT_OFFSETS - 1];
183         struct lzms_range_encoder delta_match_range_encoder;
184         struct lzms_range_encoder delta_repeat_match_range_encoders[LZMS_NUM_RECENT_OFFSETS - 1];
185
186         /* Huffman encoders  */
187         struct lzms_huffman_encoder literal_encoder;
188         struct lzms_huffman_encoder lz_offset_encoder;
189         struct lzms_huffman_encoder length_encoder;
190         struct lzms_huffman_encoder delta_power_encoder;
191         struct lzms_huffman_encoder delta_offset_encoder;
192
193         /* Used for preprocessing  */
194         s32 last_target_usages[65536];
195
196 #define LZMS_NUM_FAST_LENGTHS 256
197         /* Table: length => length slot for small lengths  */
198         u8 length_slot_fast[LZMS_NUM_FAST_LENGTHS];
199
200         /* Table: length => current cost for small match lengths  */
201         u32 length_cost_fast[LZMS_NUM_FAST_LENGTHS];
202
203 #define LZMS_NUM_FAST_OFFSETS 32768
204         /* Table: offset => offset slot for small offsets  */
205         u8 offset_slot_fast[LZMS_NUM_FAST_OFFSETS];
206 };
207
208 /*
209  * Match chooser position data:
210  *
211  * An array of these structures is used during the near-optimal match-choosing
212  * algorithm.  They correspond to consecutive positions in the window and are
213  * used to keep track of the cost to reach each position, and the match/literal
214  * choices that need to be chosen to reach that position.
215  */
216 struct lzms_mc_pos_data {
217
218         /* The cost, in bits, of the lowest-cost path that has been found to
219          * reach this position.  This can change as progressively lower cost
220          * paths are found to reach this position.  */
221         u32 cost;
222 #define MC_INFINITE_COST UINT32_MAX
223
224         /* The match or literal that was taken to reach this position.  This can
225          * change as progressively lower cost paths are found to reach this
226          * position.
227          *
228          * This variable is divided into two bitfields.
229          *
230          * Literals:
231          *      Low bits are 1, high bits are the literal.
232          *
233          * Explicit offset matches:
234          *      Low bits are the match length, high bits are the offset plus 2.
235          *
236          * Repeat offset matches:
237          *      Low bits are the match length, high bits are the queue index.
238          */
239         u64 mc_item_data;
240 #define MC_OFFSET_SHIFT 32
241 #define MC_LEN_MASK (((u64)1 << MC_OFFSET_SHIFT) - 1)
242
243         /* The LZMS adaptive state that exists at this position.  This is filled
244          * in lazily, only after the minimum-cost path to this position is
245          * found.
246          *
247          * Note: the way we handle this adaptive state in the "minimum-cost"
248          * parse is actually only an approximation.  It's possible for the
249          * globally optimal, minimum cost path to contain a prefix, ending at a
250          * position, where that path prefix is *not* the minimum cost path to
251          * that position.  This can happen if such a path prefix results in a
252          * different adaptive state which results in lower costs later.  We do
253          * not solve this problem; we only consider the lowest cost to reach
254          * each position, which seems to be an acceptable approximation.
255          *
256          * Note: this adaptive state also does not include the probability
257          * entries or current Huffman codewords.  Those aren't maintained
258          * per-position and are only updated occassionally.  */
259         struct lzms_adaptive_state {
260                 struct lzms_lz_lru_queues lru;
261                 u8 main_state;
262                 u8 match_state;
263                 u8 lz_match_state;
264                 u8 lz_repeat_match_state[LZMS_NUM_RECENT_OFFSETS - 1];
265         } state;
266 };
267
268 static void
269 lzms_init_fast_slots(struct lzms_compressor *c)
270 {
271         /* Create table mapping small lengths to length slots.  */
272         for (unsigned slot = 0, i = 0; i < LZMS_NUM_FAST_LENGTHS; i++) {
273                 while (i >= lzms_length_slot_base[slot + 1])
274                         slot++;
275                 c->length_slot_fast[i] = slot;
276         }
277
278         /* Create table mapping small offsets to offset slots.  */
279         for (unsigned slot = 0, i = 0; i < LZMS_NUM_FAST_OFFSETS; i++) {
280                 while (i >= lzms_offset_slot_base[slot + 1])
281                         slot++;
282                 c->offset_slot_fast[i] = slot;
283         }
284 }
285
286 static inline unsigned
287 lzms_get_length_slot_fast(const struct lzms_compressor *c, u32 length)
288 {
289         if (likely(length < LZMS_NUM_FAST_LENGTHS))
290                 return c->length_slot_fast[length];
291         else
292                 return lzms_get_length_slot(length);
293 }
294
295 static inline unsigned
296 lzms_get_offset_slot_fast(const struct lzms_compressor *c, u32 offset)
297 {
298         if (offset < LZMS_NUM_FAST_OFFSETS)
299                 return c->offset_slot_fast[offset];
300         else
301                 return lzms_get_offset_slot(offset);
302 }
303
304 /* Initialize the output bitstream @os to write backwards to the specified
305  * compressed data buffer @out that is @out_limit 16-bit integers long.  */
306 static void
307 lzms_output_bitstream_init(struct lzms_output_bitstream *os,
308                            le16 *out, size_t out_limit)
309 {
310         os->bitbuf = 0;
311         os->bitcount = 0;
312         os->next = out + out_limit;
313         os->begin = out;
314 }
315
316 /*
317  * Write some bits, contained in the low @num_bits bits of @bits (ordered from
318  * high-order to low-order), to the output bitstream @os.
319  *
320  * @max_num_bits is a compile-time constant that specifies the maximum number of
321  * bits that can ever be written at this call site.
322  */
323 static inline void
324 lzms_output_bitstream_put_varbits(struct lzms_output_bitstream *os,
325                                   u32 bits, unsigned num_bits,
326                                   unsigned max_num_bits)
327 {
328         LZMS_ASSERT(num_bits <= 48);
329
330         /* Add the bits to the bit buffer variable.  */
331         os->bitcount += num_bits;
332         os->bitbuf = (os->bitbuf << num_bits) | bits;
333
334         /* Check whether any coding units need to be written.  */
335         while (os->bitcount >= 16) {
336
337                 os->bitcount -= 16;
338
339                 /* Write a coding unit, unless it would underflow the buffer. */
340                 if (os->next != os->begin)
341                         *--os->next = cpu_to_le16(os->bitbuf >> os->bitcount);
342
343                 /* Optimization for call sites that never write more than 16
344                  * bits at once.  */
345                 if (max_num_bits <= 16)
346                         break;
347         }
348 }
349
350 /* Flush the output bitstream, ensuring that all bits written to it have been
351  * written to memory.  Returns %true if all bits have been output successfully,
352  * or %false if an overrun occurred.  */
353 static bool
354 lzms_output_bitstream_flush(struct lzms_output_bitstream *os)
355 {
356         if (os->next == os->begin)
357                 return false;
358
359         if (os->bitcount != 0)
360                 *--os->next = cpu_to_le16(os->bitbuf << (16 - os->bitcount));
361
362         return true;
363 }
364
365 /* Initialize the range encoder @rc to write forwards to the specified
366  * compressed data buffer @out that is @out_limit 16-bit integers long.  */
367 static void
368 lzms_range_encoder_raw_init(struct lzms_range_encoder_raw *rc,
369                             le16 *out, size_t out_limit)
370 {
371         rc->low = 0;
372         rc->range = 0xffffffff;
373         rc->cache = 0;
374         rc->cache_size = 1;
375         rc->begin = out;
376         rc->next = out - 1;
377         rc->end = out + out_limit;
378 }
379
380 /*
381  * Attempt to flush bits from the range encoder.
382  *
383  * Note: this is based on the public domain code for LZMA written by Igor
384  * Pavlov.  The only differences in this function are that in LZMS the bits must
385  * be output in 16-bit coding units instead of 8-bit coding units, and that in
386  * LZMS the first coding unit is not ignored by the decompressor, so the encoder
387  * cannot output a dummy value to that position.
388  *
389  * The basic idea is that we're writing bits from @rc->low to the output.
390  * However, due to carrying, the writing of coding units with value 0xffff, as
391  * well as one prior coding unit, must be delayed until it is determined whether
392  * a carry is needed.
393  */
394 static void
395 lzms_range_encoder_raw_shift_low(struct lzms_range_encoder_raw *rc)
396 {
397         if ((u32)(rc->low) < 0xffff0000 ||
398             (u32)(rc->low >> 32) != 0)
399         {
400                 /* Carry not needed (rc->low < 0xffff0000), or carry occurred
401                  * ((rc->low >> 32) != 0, a.k.a. the carry bit is 1).  */
402                 do {
403                         if (likely(rc->next >= rc->begin)) {
404                                 if (rc->next != rc->end)
405                                         *rc->next++ = cpu_to_le16(rc->cache +
406                                                                   (u16)(rc->low >> 32));
407                         } else {
408                                 rc->next++;
409                         }
410                         rc->cache = 0xffff;
411                 } while (--rc->cache_size != 0);
412
413                 rc->cache = (rc->low >> 16) & 0xffff;
414         }
415         ++rc->cache_size;
416         rc->low = (rc->low & 0xffff) << 16;
417 }
418
419 static void
420 lzms_range_encoder_raw_normalize(struct lzms_range_encoder_raw *rc)
421 {
422         if (rc->range <= 0xffff) {
423                 rc->range <<= 16;
424                 lzms_range_encoder_raw_shift_low(rc);
425         }
426 }
427
428 static bool
429 lzms_range_encoder_raw_flush(struct lzms_range_encoder_raw *rc)
430 {
431         for (unsigned i = 0; i < 4; i++)
432                 lzms_range_encoder_raw_shift_low(rc);
433         return rc->next != rc->end;
434 }
435
436 /* Encode the next bit using the range encoder (raw version).
437  *
438  * @prob is the chance out of LZMS_PROBABILITY_MAX that the next bit is 0.  */
439 static inline void
440 lzms_range_encoder_raw_encode_bit(struct lzms_range_encoder_raw *rc,
441                                   int bit, u32 prob)
442 {
443         lzms_range_encoder_raw_normalize(rc);
444
445         u32 bound = (rc->range >> LZMS_PROBABILITY_BITS) * prob;
446         if (bit == 0) {
447                 rc->range = bound;
448         } else {
449                 rc->low += bound;
450                 rc->range -= bound;
451         }
452 }
453
454 /* Encode a bit using the specified range encoder. This wraps around
455  * lzms_range_encoder_raw_encode_bit() to handle using and updating the
456  * appropriate state and probability entry.  */
457 static void
458 lzms_range_encode_bit(struct lzms_range_encoder *enc, int bit)
459 {
460         struct lzms_probability_entry *prob_entry;
461         u32 prob;
462
463         /* Load the probability entry corresponding to the current state.  */
464         prob_entry = &enc->prob_entries[enc->state];
465
466         /* Update the state based on the next bit.  */
467         enc->state = ((enc->state << 1) | bit) & enc->mask;
468
469         /* Get the probability that the bit is 0.  */
470         prob = lzms_get_probability(prob_entry);
471
472         /* Update the probability entry.  */
473         lzms_update_probability_entry(prob_entry, bit);
474
475         /* Encode the bit.  */
476         lzms_range_encoder_raw_encode_bit(enc->rc, bit, prob);
477 }
478
479 /* Called when an adaptive Huffman code needs to be rebuilt.  */
480 static void
481 lzms_rebuild_huffman_code(struct lzms_huffman_encoder *enc)
482 {
483         make_canonical_huffman_code(enc->num_syms,
484                                     LZMS_MAX_CODEWORD_LEN,
485                                     enc->sym_freqs,
486                                     enc->lens,
487                                     enc->codewords);
488
489         /* Dilute the frequencies.  */
490         for (unsigned i = 0; i < enc->num_syms; i++) {
491                 enc->sym_freqs[i] >>= 1;
492                 enc->sym_freqs[i] += 1;
493         }
494         enc->num_syms_written = 0;
495 }
496
497 /* Encode a symbol using the specified Huffman encoder.  */
498 static inline void
499 lzms_huffman_encode_symbol(struct lzms_huffman_encoder *enc, unsigned sym)
500 {
501         lzms_output_bitstream_put_varbits(enc->os,
502                                           enc->codewords[sym],
503                                           enc->lens[sym],
504                                           LZMS_MAX_CODEWORD_LEN);
505         ++enc->sym_freqs[sym];
506         if (++enc->num_syms_written == enc->rebuild_freq)
507                 lzms_rebuild_huffman_code(enc);
508 }
509
510 static void
511 lzms_update_fast_length_costs(struct lzms_compressor *c);
512
513 /* Encode a match length.  */
514 static void
515 lzms_encode_length(struct lzms_compressor *c, u32 length)
516 {
517         unsigned slot;
518         unsigned num_extra_bits;
519         u32 extra_bits;
520
521         slot = lzms_get_length_slot_fast(c, length);
522
523         extra_bits = length - lzms_length_slot_base[slot];
524         num_extra_bits = lzms_extra_length_bits[slot];
525
526         lzms_huffman_encode_symbol(&c->length_encoder, slot);
527         if (c->length_encoder.num_syms_written == 0)
528                 lzms_update_fast_length_costs(c);
529
530         lzms_output_bitstream_put_varbits(c->length_encoder.os,
531                                           extra_bits, num_extra_bits, 30);
532 }
533
534 /* Encode an LZ match offset.  */
535 static void
536 lzms_encode_lz_offset(struct lzms_compressor *c, u32 offset)
537 {
538         unsigned slot;
539         unsigned num_extra_bits;
540         u32 extra_bits;
541
542         slot = lzms_get_offset_slot_fast(c, offset);
543
544         extra_bits = offset - lzms_offset_slot_base[slot];
545         num_extra_bits = lzms_extra_offset_bits[slot];
546
547         lzms_huffman_encode_symbol(&c->lz_offset_encoder, slot);
548         lzms_output_bitstream_put_varbits(c->lz_offset_encoder.os,
549                                           extra_bits, num_extra_bits, 30);
550 }
551
552 /* Encode a literal byte.  */
553 static void
554 lzms_encode_literal(struct lzms_compressor *c, unsigned literal)
555 {
556         /* Main bit: 0 = a literal, not a match.  */
557         lzms_range_encode_bit(&c->main_range_encoder, 0);
558
559         /* Encode the literal using the current literal Huffman code.  */
560         lzms_huffman_encode_symbol(&c->literal_encoder, literal);
561 }
562
563 /* Encode an LZ repeat offset match.  */
564 static void
565 lzms_encode_lz_repeat_offset_match(struct lzms_compressor *c,
566                                    u32 length, unsigned rep_index)
567 {
568         unsigned i;
569
570         /* Main bit: 1 = a match, not a literal.  */
571         lzms_range_encode_bit(&c->main_range_encoder, 1);
572
573         /* Match bit: 0 = an LZ match, not a delta match.  */
574         lzms_range_encode_bit(&c->match_range_encoder, 0);
575
576         /* LZ match bit: 1 = repeat offset, not an explicit offset.  */
577         lzms_range_encode_bit(&c->lz_match_range_encoder, 1);
578
579         /* Encode the repeat offset index.  A 1 bit is encoded for each index
580          * passed up.  This sequence of 1 bits is terminated by a 0 bit, or
581          * automatically when (LZMS_NUM_RECENT_OFFSETS - 1) 1 bits have been
582          * encoded.  */
583         for (i = 0; i < rep_index; i++)
584                 lzms_range_encode_bit(&c->lz_repeat_match_range_encoders[i], 1);
585
586         if (i < LZMS_NUM_RECENT_OFFSETS - 1)
587                 lzms_range_encode_bit(&c->lz_repeat_match_range_encoders[i], 0);
588
589         /* Encode the match length.  */
590         lzms_encode_length(c, length);
591 }
592
593 /* Encode an LZ explicit offset match.  */
594 static void
595 lzms_encode_lz_explicit_offset_match(struct lzms_compressor *c,
596                                      u32 length, u32 offset)
597 {
598         /* Main bit: 1 = a match, not a literal.  */
599         lzms_range_encode_bit(&c->main_range_encoder, 1);
600
601         /* Match bit: 0 = an LZ match, not a delta match.  */
602         lzms_range_encode_bit(&c->match_range_encoder, 0);
603
604         /* LZ match bit: 0 = explicit offset, not a repeat offset.  */
605         lzms_range_encode_bit(&c->lz_match_range_encoder, 0);
606
607         /* Encode the match offset.  */
608         lzms_encode_lz_offset(c, offset);
609
610         /* Encode the match length.  */
611         lzms_encode_length(c, length);
612 }
613
614 static void
615 lzms_encode_item(struct lzms_compressor *c, u64 mc_item_data)
616 {
617         u32 len = mc_item_data & MC_LEN_MASK;
618         u32 offset_data = mc_item_data >> MC_OFFSET_SHIFT;
619
620         if (len == 1)
621                 lzms_encode_literal(c, offset_data);
622         else if (offset_data < LZMS_NUM_RECENT_OFFSETS)
623                 lzms_encode_lz_repeat_offset_match(c, len, offset_data);
624         else
625                 lzms_encode_lz_explicit_offset_match(c, len, offset_data - LZMS_OFFSET_OFFSET);
626 }
627
628 /* Encode a list of matches and literals chosen by the parsing algorithm.  */
629 static void
630 lzms_encode_item_list(struct lzms_compressor *c,
631                       struct lzms_mc_pos_data *cur_optimum_ptr)
632 {
633         struct lzms_mc_pos_data *end_optimum_ptr;
634         u64 saved_item;
635         u64 item;
636
637         /* The list is currently in reverse order (last item to first item).
638          * Reverse it.  */
639         end_optimum_ptr = cur_optimum_ptr;
640         saved_item = cur_optimum_ptr->mc_item_data;
641         do {
642                 item = saved_item;
643                 cur_optimum_ptr -= item & MC_LEN_MASK;
644                 saved_item = cur_optimum_ptr->mc_item_data;
645                 cur_optimum_ptr->mc_item_data = item;
646         } while (cur_optimum_ptr != c->optimum);
647
648         /* Walk the list of items from beginning to end, encoding each item.  */
649         do {
650                 lzms_encode_item(c, cur_optimum_ptr->mc_item_data);
651                 cur_optimum_ptr += (cur_optimum_ptr->mc_item_data) & MC_LEN_MASK;
652         } while (cur_optimum_ptr != end_optimum_ptr);
653 }
654
655 /* Each bit costs 1 << LZMS_COST_SHIFT units.  */
656 #define LZMS_COST_SHIFT 6
657
658 /*#define LZMS_RC_COSTS_USE_FLOATING_POINT*/
659
660 static u32
661 lzms_rc_costs[LZMS_PROBABILITY_MAX + 1];
662
663 #ifdef LZMS_RC_COSTS_USE_FLOATING_POINT
664 #  include <math.h>
665 #endif
666
667 static void
668 lzms_do_init_rc_costs(void)
669 {
670         /* Fill in a table that maps range coding probabilities needed to code a
671          * bit X (0 or 1) to the number of bits (scaled by a constant factor, to
672          * handle fractional costs) needed to code that bit X.
673          *
674          * Consider the range of the range decoder.  To eliminate exactly half
675          * the range (logical probability of 0.5), we need exactly 1 bit.  For
676          * lower probabilities we need more bits and for higher probabilities we
677          * need fewer bits.  In general, a logical probability of N will
678          * eliminate the proportion 1 - N of the range; this information takes
679          * log2(1 / N) bits to encode.
680          *
681          * The below loop is simply calculating this number of bits for each
682          * possible probability allowed by the LZMS compression format, but
683          * without using real numbers.  To handle fractional probabilities, each
684          * cost is multiplied by (1 << LZMS_COST_SHIFT).  These techniques are
685          * based on those used by LZMA.
686          *
687          * Note that in LZMS, a probability x really means x / 64, and 0 / 64 is
688          * really interpreted as 1 / 64 and 64 / 64 is really interpreted as
689          * 63 / 64.
690          */
691         for (u32 i = 0; i <= LZMS_PROBABILITY_MAX; i++) {
692                 u32 prob = i;
693
694                 if (prob == 0)
695                         prob = 1;
696                 else if (prob == LZMS_PROBABILITY_MAX)
697                         prob = LZMS_PROBABILITY_MAX - 1;
698
699         #ifdef LZMS_RC_COSTS_USE_FLOATING_POINT
700                 lzms_rc_costs[i] = log2((double)LZMS_PROBABILITY_MAX / prob) *
701                                         (1 << LZMS_COST_SHIFT);
702         #else
703                 u32 w = prob;
704                 u32 bit_count = 0;
705                 for (u32 j = 0; j < LZMS_COST_SHIFT; j++) {
706                         w *= w;
707                         bit_count <<= 1;
708                         while (w >= ((u32)1 << 16)) {
709                                 w >>= 1;
710                                 ++bit_count;
711                         }
712                 }
713                 lzms_rc_costs[i] = (LZMS_PROBABILITY_BITS << LZMS_COST_SHIFT) -
714                                    (15 + bit_count);
715         #endif
716         }
717 }
718
719 static void
720 lzms_init_rc_costs(void)
721 {
722         static pthread_once_t once = PTHREAD_ONCE_INIT;
723
724         pthread_once(&once, lzms_do_init_rc_costs);
725 }
726
727 /* Return the cost to range-encode the specified bit from the specified state.*/
728 static inline u32
729 lzms_rc_bit_cost(const struct lzms_range_encoder *enc, u8 cur_state, int bit)
730 {
731         u32 prob_zero;
732         u32 prob_correct;
733
734         prob_zero = enc->prob_entries[cur_state].num_recent_zero_bits;
735
736         if (bit == 0)
737                 prob_correct = prob_zero;
738         else
739                 prob_correct = LZMS_PROBABILITY_MAX - prob_zero;
740
741         return lzms_rc_costs[prob_correct];
742 }
743
744 /* Return the cost to Huffman-encode the specified symbol.  */
745 static inline u32
746 lzms_huffman_symbol_cost(const struct lzms_huffman_encoder *enc, unsigned sym)
747 {
748         return (u32)enc->lens[sym] << LZMS_COST_SHIFT;
749 }
750
751 /* Return the cost to encode the specified literal byte.  */
752 static inline u32
753 lzms_literal_cost(const struct lzms_compressor *c, unsigned literal,
754                   const struct lzms_adaptive_state *state)
755 {
756         return lzms_rc_bit_cost(&c->main_range_encoder, state->main_state, 0) +
757                lzms_huffman_symbol_cost(&c->literal_encoder, literal);
758 }
759
760 /* Update the table that directly provides the costs for small lengths.  */
761 static void
762 lzms_update_fast_length_costs(struct lzms_compressor *c)
763 {
764         u32 len;
765         int slot = -1;
766         u32 cost = 0;
767
768         for (len = 1; len < LZMS_NUM_FAST_LENGTHS; len++) {
769
770                 while (len >= lzms_length_slot_base[slot + 1]) {
771                         slot++;
772                         cost = (u32)(c->length_encoder.lens[slot] +
773                                      lzms_extra_length_bits[slot]) << LZMS_COST_SHIFT;
774                 }
775
776                 c->length_cost_fast[len] = cost;
777         }
778 }
779
780 /* Return the cost to encode the specified match length, which must be less than
781  * LZMS_NUM_FAST_LENGTHS.  */
782 static inline u32
783 lzms_fast_length_cost(const struct lzms_compressor *c, u32 length)
784 {
785         LZMS_ASSERT(length < LZMS_NUM_FAST_LENGTHS);
786         return c->length_cost_fast[length];
787 }
788
789 /* Return the cost to encode the specified LZ match offset.  */
790 static inline u32
791 lzms_lz_offset_cost(const struct lzms_compressor *c, u32 offset)
792 {
793         unsigned slot = lzms_get_offset_slot_fast(c, offset);
794
795         return (u32)(c->lz_offset_encoder.lens[slot] +
796                      lzms_extra_offset_bits[slot]) << LZMS_COST_SHIFT;
797 }
798
799 /*
800  * Consider coding the match at repeat offset index @rep_idx.  Consider each
801  * length from the minimum (2) to the full match length (@rep_len).
802  */
803 static inline void
804 lzms_consider_lz_repeat_offset_match(const struct lzms_compressor *c,
805                                      struct lzms_mc_pos_data *cur_optimum_ptr,
806                                      u32 rep_len, unsigned rep_idx)
807 {
808         u32 len;
809         u32 base_cost;
810         u32 cost;
811         unsigned i;
812
813         base_cost = cur_optimum_ptr->cost;
814
815         base_cost += lzms_rc_bit_cost(&c->main_range_encoder,
816                                       cur_optimum_ptr->state.main_state, 1);
817
818         base_cost += lzms_rc_bit_cost(&c->match_range_encoder,
819                                       cur_optimum_ptr->state.match_state, 0);
820
821         base_cost += lzms_rc_bit_cost(&c->lz_match_range_encoder,
822                                       cur_optimum_ptr->state.lz_match_state, 1);
823
824         for (i = 0; i < rep_idx; i++)
825                 base_cost += lzms_rc_bit_cost(&c->lz_repeat_match_range_encoders[i],
826                                               cur_optimum_ptr->state.lz_repeat_match_state[i], 1);
827
828         if (i < LZMS_NUM_RECENT_OFFSETS - 1)
829                 base_cost += lzms_rc_bit_cost(&c->lz_repeat_match_range_encoders[i],
830                                               cur_optimum_ptr->state.lz_repeat_match_state[i], 0);
831
832         len = 2;
833         do {
834                 cost = base_cost + lzms_fast_length_cost(c, len);
835                 if (cost < (cur_optimum_ptr + len)->cost) {
836                         (cur_optimum_ptr + len)->mc_item_data =
837                                 ((u64)rep_idx << MC_OFFSET_SHIFT) | len;
838                         (cur_optimum_ptr + len)->cost = cost;
839                 }
840         } while (++len <= rep_len);
841 }
842
843 /*
844  * Consider coding each match in @matches as an explicit offset match.
845  *
846  * @matches must be sorted by strictly increasing length and strictly increasing
847  * offset.  This is guaranteed by the match-finder.
848  *
849  * We consider each length from the minimum (2) to the longest
850  * (matches[num_matches - 1].len).  For each length, we consider only the
851  * smallest offset for which that length is available.  Although this is not
852  * guaranteed to be optimal due to the possibility of a larger offset costing
853  * less than a smaller offset to code, this is a very useful heuristic.
854  */
855 static inline void
856 lzms_consider_lz_explicit_offset_matches(const struct lzms_compressor *c,
857                                          struct lzms_mc_pos_data *cur_optimum_ptr,
858                                          const struct lz_match matches[],
859                                          u32 num_matches)
860 {
861         u32 len;
862         u32 i;
863         u32 base_cost;
864         u32 position_cost;
865         u32 cost;
866
867         base_cost = cur_optimum_ptr->cost;
868
869         base_cost += lzms_rc_bit_cost(&c->main_range_encoder,
870                                       cur_optimum_ptr->state.main_state, 1);
871
872         base_cost += lzms_rc_bit_cost(&c->match_range_encoder,
873                                       cur_optimum_ptr->state.match_state, 0);
874
875         base_cost += lzms_rc_bit_cost(&c->lz_match_range_encoder,
876                                       cur_optimum_ptr->state.lz_match_state, 0);
877         len = 2;
878         i = 0;
879         do {
880                 position_cost = base_cost + lzms_lz_offset_cost(c, matches[i].offset);
881                 do {
882                         cost = position_cost + lzms_fast_length_cost(c, len);
883                         if (cost < (cur_optimum_ptr + len)->cost) {
884                                 (cur_optimum_ptr + len)->mc_item_data =
885                                         ((u64)(matches[i].offset + LZMS_OFFSET_OFFSET)
886                                                 << MC_OFFSET_SHIFT) | len;
887                                 (cur_optimum_ptr + len)->cost = cost;
888                         }
889                 } while (++len <= matches[i].len);
890         } while (++i != num_matches);
891 }
892
893 static void
894 lzms_init_adaptive_state(struct lzms_adaptive_state *state)
895 {
896         unsigned i;
897
898         lzms_init_lz_lru_queues(&state->lru);
899         state->main_state = 0;
900         state->match_state = 0;
901         state->lz_match_state = 0;
902         for (i = 0; i < LZMS_NUM_RECENT_OFFSETS - 1; i++)
903                 state->lz_repeat_match_state[i] = 0;
904 }
905
906 static inline void
907 lzms_update_main_state(struct lzms_adaptive_state *state, int is_match)
908 {
909         state->main_state = ((state->main_state << 1) | is_match) % LZMS_NUM_MAIN_STATES;
910 }
911
912 static inline void
913 lzms_update_match_state(struct lzms_adaptive_state *state, int is_delta)
914 {
915         state->match_state = ((state->match_state << 1) | is_delta) % LZMS_NUM_MATCH_STATES;
916 }
917
918 static inline void
919 lzms_update_lz_match_state(struct lzms_adaptive_state *state, int is_repeat_offset)
920 {
921         state->lz_match_state = ((state->lz_match_state << 1) | is_repeat_offset) % LZMS_NUM_LZ_MATCH_STATES;
922 }
923
924 static inline void
925 lzms_update_lz_repeat_match_state(struct lzms_adaptive_state *state, int rep_idx)
926 {
927         int i;
928
929         for (i = 0; i < rep_idx; i++)
930                 state->lz_repeat_match_state[i] =
931                         ((state->lz_repeat_match_state[i] << 1) | 1) %
932                                 LZMS_NUM_LZ_REPEAT_MATCH_STATES;
933
934         if (i < LZMS_NUM_RECENT_OFFSETS - 1)
935                 state->lz_repeat_match_state[i] =
936                         ((state->lz_repeat_match_state[i] << 1) | 0) %
937                                 LZMS_NUM_LZ_REPEAT_MATCH_STATES;
938 }
939
940 /*
941  * The main near-optimal parsing routine.
942  *
943  * Briefly, the algorithm does an approximate minimum-cost path search to find a
944  * "near-optimal" sequence of matches and literals to output, based on the
945  * current cost model.  The algorithm steps forward, position by position (byte
946  * by byte), and updates the minimum cost path to reach each later position that
947  * can be reached using a match or literal from the current position.  This is
948  * essentially Dijkstra's algorithm in disguise: the graph nodes are positions,
949  * the graph edges are possible matches/literals to code, and the cost of each
950  * edge is the estimated number of bits that will be required to output the
951  * corresponding match or literal.  But one difference is that we actually
952  * compute the lowest-cost path in pieces, where each piece is terminated when
953  * there are no choices to be made.
954  *
955  * Notes:
956  *
957  * - This does not output any delta matches.
958  *
959  * - The costs of literals and matches are estimated using the range encoder
960  *   states and the semi-adaptive Huffman codes.  Except for range encoding
961  *   states, costs are assumed to be constant throughout a single run of the
962  *   parsing algorithm, which can parse up to @optim_array_length bytes of data.
963  *   This introduces a source of inaccuracy because the probabilities and
964  *   Huffman codes can change over this part of the data.
965  */
966 static void
967 lzms_near_optimal_parse(struct lzms_compressor *c)
968 {
969         const u8 *window_ptr;
970         const u8 *window_end;
971         struct lzms_mc_pos_data *cur_optimum_ptr;
972         struct lzms_mc_pos_data *end_optimum_ptr;
973         u32 num_matches;
974         u32 longest_len;
975         u32 rep_max_len;
976         unsigned rep_max_idx;
977         unsigned literal;
978         unsigned i;
979         u32 cost;
980         u32 len;
981         u32 offset_data;
982
983         window_ptr = c->cur_window;
984         window_end = window_ptr + c->cur_window_size;
985
986         lzms_init_adaptive_state(&c->optimum[0].state);
987
988 begin:
989         /* Start building a new list of items, which will correspond to the next
990          * piece of the overall minimum-cost path.  */
991
992         cur_optimum_ptr = c->optimum;
993         cur_optimum_ptr->cost = 0;
994         end_optimum_ptr = cur_optimum_ptr;
995
996         /* States should currently be consistent with the encoders.  */
997         LZMS_ASSERT(cur_optimum_ptr->state.main_state == c->main_range_encoder.state);
998         LZMS_ASSERT(cur_optimum_ptr->state.match_state == c->match_range_encoder.state);
999         LZMS_ASSERT(cur_optimum_ptr->state.lz_match_state == c->lz_match_range_encoder.state);
1000         for (i = 0; i < LZMS_NUM_RECENT_OFFSETS - 1; i++)
1001                 LZMS_ASSERT(cur_optimum_ptr->state.lz_repeat_match_state[i] ==
1002                             c->lz_repeat_match_range_encoders[i].state);
1003
1004         if (window_ptr == window_end)
1005                 return;
1006
1007         /* The following loop runs once for each per byte in the window, except
1008          * in a couple shortcut cases.  */
1009         for (;;) {
1010
1011                 /* Find explicit offset matches with the current position.  */
1012                 num_matches = lz_mf_get_matches(c->mf, c->matches);
1013
1014                 if (num_matches) {
1015                         /*
1016                          * Find the longest repeat offset match with the current
1017                          * position.
1018                          *
1019                          * Heuristics:
1020                          *
1021                          * - Only search for repeat offset matches if the
1022                          *   match-finder already found at least one match.
1023                          *
1024                          * - Only consider the longest repeat offset match.  It
1025                          *   seems to be rare for the optimal parse to include a
1026                          *   repeat offset match that doesn't have the longest
1027                          *   length (allowing for the possibility that not all
1028                          *   of that length is actually used).
1029                          */
1030                         if (likely(window_ptr - c->cur_window >= LZMS_MAX_INIT_RECENT_OFFSET)) {
1031                                 BUILD_BUG_ON(LZMS_NUM_RECENT_OFFSETS != 3);
1032                                 rep_max_len = lz_repsearch3(window_ptr,
1033                                                             window_end - window_ptr,
1034                                                             cur_optimum_ptr->state.lru.recent_offsets,
1035                                                             &rep_max_idx);
1036                         } else {
1037                                 rep_max_len = 0;
1038                         }
1039
1040                         if (rep_max_len) {
1041                                 /* If there's a very long repeat offset match,
1042                                  * choose it immediately.  */
1043                                 if (rep_max_len >= c->params.nice_match_length) {
1044
1045                                         lz_mf_skip_positions(c->mf, rep_max_len - 1);
1046                                         window_ptr += rep_max_len;
1047
1048                                         if (cur_optimum_ptr != c->optimum)
1049                                                 lzms_encode_item_list(c, cur_optimum_ptr);
1050
1051                                         lzms_encode_lz_repeat_offset_match(c, rep_max_len,
1052                                                                            rep_max_idx);
1053
1054                                         c->optimum[0].state = cur_optimum_ptr->state;
1055
1056                                         lzms_update_main_state(&c->optimum[0].state, 1);
1057                                         lzms_update_match_state(&c->optimum[0].state, 0);
1058                                         lzms_update_lz_match_state(&c->optimum[0].state, 1);
1059                                         lzms_update_lz_repeat_match_state(&c->optimum[0].state,
1060                                                                           rep_max_idx);
1061
1062                                         c->optimum[0].state.lru.upcoming_offset =
1063                                                 c->optimum[0].state.lru.recent_offsets[rep_max_idx];
1064
1065                                         for (i = rep_max_idx; i < LZMS_NUM_RECENT_OFFSETS; i++)
1066                                                 c->optimum[0].state.lru.recent_offsets[i] =
1067                                                         c->optimum[0].state.lru.recent_offsets[i + 1];
1068
1069                                         lzms_update_lz_lru_queue(&c->optimum[0].state.lru);
1070                                         goto begin;
1071                                 }
1072
1073                                 /* If reaching any positions for the first time,
1074                                  * initialize their costs to "infinity".  */
1075                                 while (end_optimum_ptr < cur_optimum_ptr + rep_max_len)
1076                                         (++end_optimum_ptr)->cost = MC_INFINITE_COST;
1077
1078                                 /* Consider coding a repeat offset match.  */
1079                                 lzms_consider_lz_repeat_offset_match(c, cur_optimum_ptr,
1080                                                                      rep_max_len, rep_max_idx);
1081                         }
1082
1083                         longest_len = c->matches[num_matches - 1].len;
1084
1085                         /* If there's a very long explicit offset match, choose
1086                          * it immediately.  */
1087                         if (longest_len >= c->params.nice_match_length) {
1088
1089                                 lz_mf_skip_positions(c->mf, longest_len - 1);
1090                                 window_ptr += longest_len;
1091
1092                                 if (cur_optimum_ptr != c->optimum)
1093                                         lzms_encode_item_list(c, cur_optimum_ptr);
1094
1095                                 lzms_encode_lz_explicit_offset_match(c, longest_len,
1096                                                                      c->matches[num_matches - 1].offset);
1097
1098                                 c->optimum[0].state = cur_optimum_ptr->state;
1099
1100                                 lzms_update_main_state(&c->optimum[0].state, 1);
1101                                 lzms_update_match_state(&c->optimum[0].state, 0);
1102                                 lzms_update_lz_match_state(&c->optimum[0].state, 0);
1103
1104                                 c->optimum[0].state.lru.upcoming_offset =
1105                                         c->matches[num_matches - 1].offset;
1106
1107                                 lzms_update_lz_lru_queue(&c->optimum[0].state.lru);
1108                                 goto begin;
1109                         }
1110
1111                         /* If reaching any positions for the first time,
1112                          * initialize their costs to "infinity".  */
1113                         while (end_optimum_ptr < cur_optimum_ptr + longest_len)
1114                                 (++end_optimum_ptr)->cost = MC_INFINITE_COST;
1115
1116                         /* Consider coding an explicit offset match.  */
1117                         lzms_consider_lz_explicit_offset_matches(c, cur_optimum_ptr,
1118                                                                  c->matches, num_matches);
1119                 } else {
1120                         /* No matches found.  The only choice at this position
1121                          * is to code a literal.  */
1122
1123                         if (end_optimum_ptr == cur_optimum_ptr)
1124                                 (++end_optimum_ptr)->cost = MC_INFINITE_COST;
1125                 }
1126
1127                 /* Consider coding a literal.
1128
1129                  * To avoid an extra unpredictable brench, actually checking the
1130                  * preferability of coding a literal is integrated into the
1131                  * adaptive state update code below.  */
1132                 literal = *window_ptr++;
1133                 cost = cur_optimum_ptr->cost +
1134                        lzms_literal_cost(c, literal, &cur_optimum_ptr->state);
1135
1136                 /* Advance to the next position.  */
1137                 cur_optimum_ptr++;
1138
1139                 /* The lowest-cost path to the current position is now known.
1140                  * Finalize the adaptive state that results from taking this
1141                  * lowest-cost path.  */
1142
1143                 if (cost < cur_optimum_ptr->cost) {
1144                         /* Literal  */
1145                         cur_optimum_ptr->cost = cost;
1146                         cur_optimum_ptr->mc_item_data = ((u64)literal << MC_OFFSET_SHIFT) | 1;
1147
1148                         cur_optimum_ptr->state = (cur_optimum_ptr - 1)->state;
1149
1150                         lzms_update_main_state(&cur_optimum_ptr->state, 0);
1151
1152                         cur_optimum_ptr->state.lru.upcoming_offset = 0;
1153                 } else {
1154                         /* LZ match  */
1155                         len = cur_optimum_ptr->mc_item_data & MC_LEN_MASK;
1156                         offset_data = cur_optimum_ptr->mc_item_data >> MC_OFFSET_SHIFT;
1157
1158                         cur_optimum_ptr->state = (cur_optimum_ptr - len)->state;
1159
1160                         lzms_update_main_state(&cur_optimum_ptr->state, 1);
1161                         lzms_update_match_state(&cur_optimum_ptr->state, 0);
1162
1163                         if (offset_data >= LZMS_NUM_RECENT_OFFSETS) {
1164
1165                                 /* Explicit offset LZ match  */
1166
1167                                 lzms_update_lz_match_state(&cur_optimum_ptr->state, 0);
1168
1169                                 cur_optimum_ptr->state.lru.upcoming_offset =
1170                                         offset_data - LZMS_OFFSET_OFFSET;
1171                         } else {
1172                                 /* Repeat offset LZ match  */
1173
1174                                 lzms_update_lz_match_state(&cur_optimum_ptr->state, 1);
1175                                 lzms_update_lz_repeat_match_state(&cur_optimum_ptr->state,
1176                                                                   offset_data);
1177
1178                                 cur_optimum_ptr->state.lru.upcoming_offset =
1179                                         cur_optimum_ptr->state.lru.recent_offsets[offset_data];
1180
1181                                 for (i = offset_data; i < LZMS_NUM_RECENT_OFFSETS; i++)
1182                                         cur_optimum_ptr->state.lru.recent_offsets[i] =
1183                                                 cur_optimum_ptr->state.lru.recent_offsets[i + 1];
1184                         }
1185                 }
1186
1187                 lzms_update_lz_lru_queue(&cur_optimum_ptr->state.lru);
1188
1189                 /*
1190                  * This loop will terminate when either of the following
1191                  * conditions is true:
1192                  *
1193                  * (1) cur_optimum_ptr == end_optimum_ptr
1194                  *
1195                  *      There are no paths that extend beyond the current
1196                  *      position.  In this case, any path to a later position
1197                  *      must pass through the current position, so we can go
1198                  *      ahead and choose the list of items that led to this
1199                  *      position.
1200                  *
1201                  * (2) cur_optimum_ptr == c->optimum_end
1202                  *
1203                  *      This bounds the number of times the algorithm can step
1204                  *      forward before it is guaranteed to start choosing items.
1205                  *      This limits the memory usage.  It also guarantees that
1206                  *      the parser will not go too long without updating the
1207                  *      probability tables.
1208                  *
1209                  * Note: no check for end-of-window is needed because
1210                  * end-of-window will trigger condition (1).
1211                  */
1212                 if (cur_optimum_ptr == end_optimum_ptr ||
1213                     cur_optimum_ptr == c->optimum_end)
1214                 {
1215                         c->optimum[0].state = cur_optimum_ptr->state;
1216                         break;
1217                 }
1218         }
1219
1220         /* Output the current list of items that constitute the minimum-cost
1221          * path to the current position.  */
1222         lzms_encode_item_list(c, cur_optimum_ptr);
1223         goto begin;
1224 }
1225
1226 static void
1227 lzms_init_range_encoder(struct lzms_range_encoder *enc,
1228                         struct lzms_range_encoder_raw *rc, u32 num_states)
1229 {
1230         enc->rc = rc;
1231         enc->state = 0;
1232         LZMS_ASSERT(is_power_of_2(num_states));
1233         enc->mask = num_states - 1;
1234         for (u32 i = 0; i < num_states; i++) {
1235                 enc->prob_entries[i].num_recent_zero_bits = LZMS_INITIAL_PROBABILITY;
1236                 enc->prob_entries[i].recent_bits = LZMS_INITIAL_RECENT_BITS;
1237         }
1238 }
1239
1240 static void
1241 lzms_init_huffman_encoder(struct lzms_huffman_encoder *enc,
1242                           struct lzms_output_bitstream *os,
1243                           unsigned num_syms,
1244                           unsigned rebuild_freq)
1245 {
1246         enc->os = os;
1247         enc->num_syms_written = 0;
1248         enc->rebuild_freq = rebuild_freq;
1249         enc->num_syms = num_syms;
1250         for (unsigned i = 0; i < num_syms; i++)
1251                 enc->sym_freqs[i] = 1;
1252
1253         make_canonical_huffman_code(enc->num_syms,
1254                                     LZMS_MAX_CODEWORD_LEN,
1255                                     enc->sym_freqs,
1256                                     enc->lens,
1257                                     enc->codewords);
1258 }
1259
1260 /* Prepare the LZMS compressor for compressing a block of data.  */
1261 static void
1262 lzms_prepare_compressor(struct lzms_compressor *c, const u8 *udata, u32 ulen,
1263                         le16 *cdata, u32 clen16)
1264 {
1265         unsigned num_offset_slots;
1266
1267         /* Copy the uncompressed data into the @c->cur_window buffer.  */
1268         memcpy(c->cur_window, udata, ulen);
1269         c->cur_window_size = ulen;
1270
1271         /* Initialize the raw range encoder (writing forwards).  */
1272         lzms_range_encoder_raw_init(&c->rc, cdata, clen16);
1273
1274         /* Initialize the output bitstream for Huffman symbols and verbatim bits
1275          * (writing backwards).  */
1276         lzms_output_bitstream_init(&c->os, cdata, clen16);
1277
1278         /* Calculate the number of offset slots required.  */
1279         num_offset_slots = lzms_get_offset_slot(ulen - 1) + 1;
1280
1281         /* Initialize a Huffman encoder for each alphabet.  */
1282         lzms_init_huffman_encoder(&c->literal_encoder, &c->os,
1283                                   LZMS_NUM_LITERAL_SYMS,
1284                                   LZMS_LITERAL_CODE_REBUILD_FREQ);
1285
1286         lzms_init_huffman_encoder(&c->lz_offset_encoder, &c->os,
1287                                   num_offset_slots,
1288                                   LZMS_LZ_OFFSET_CODE_REBUILD_FREQ);
1289
1290         lzms_init_huffman_encoder(&c->length_encoder, &c->os,
1291                                   LZMS_NUM_LEN_SYMS,
1292                                   LZMS_LENGTH_CODE_REBUILD_FREQ);
1293
1294         lzms_init_huffman_encoder(&c->delta_offset_encoder, &c->os,
1295                                   num_offset_slots,
1296                                   LZMS_DELTA_OFFSET_CODE_REBUILD_FREQ);
1297
1298         lzms_init_huffman_encoder(&c->delta_power_encoder, &c->os,
1299                                   LZMS_NUM_DELTA_POWER_SYMS,
1300                                   LZMS_DELTA_POWER_CODE_REBUILD_FREQ);
1301
1302         /* Initialize range encoders, all of which wrap around the same
1303          * lzms_range_encoder_raw.  */
1304         lzms_init_range_encoder(&c->main_range_encoder,
1305                                 &c->rc, LZMS_NUM_MAIN_STATES);
1306
1307         lzms_init_range_encoder(&c->match_range_encoder,
1308                                 &c->rc, LZMS_NUM_MATCH_STATES);
1309
1310         lzms_init_range_encoder(&c->lz_match_range_encoder,
1311                                 &c->rc, LZMS_NUM_LZ_MATCH_STATES);
1312
1313         for (unsigned i = 0; i < ARRAY_LEN(c->lz_repeat_match_range_encoders); i++)
1314                 lzms_init_range_encoder(&c->lz_repeat_match_range_encoders[i],
1315                                         &c->rc, LZMS_NUM_LZ_REPEAT_MATCH_STATES);
1316
1317         lzms_init_range_encoder(&c->delta_match_range_encoder,
1318                                 &c->rc, LZMS_NUM_DELTA_MATCH_STATES);
1319
1320         for (unsigned i = 0; i < ARRAY_LEN(c->delta_repeat_match_range_encoders); i++)
1321                 lzms_init_range_encoder(&c->delta_repeat_match_range_encoders[i],
1322                                         &c->rc, LZMS_NUM_DELTA_REPEAT_MATCH_STATES);
1323
1324         /* Set initial length costs for lengths < LZMS_NUM_FAST_LENGTHS.  */
1325         lzms_update_fast_length_costs(c);
1326 }
1327
1328 /* Flush the output streams, prepare the final compressed data, and return its
1329  * size in bytes.
1330  *
1331  * A return value of 0 indicates that the data could not be compressed to fit in
1332  * the available space.  */
1333 static size_t
1334 lzms_finalize(struct lzms_compressor *c, u8 *cdata, size_t csize_avail)
1335 {
1336         size_t num_forwards_bytes;
1337         size_t num_backwards_bytes;
1338
1339         /* Flush both the forwards and backwards streams, and make sure they
1340          * didn't cross each other and start overwriting each other's data.  */
1341         if (!lzms_output_bitstream_flush(&c->os))
1342                 return 0;
1343
1344         if (!lzms_range_encoder_raw_flush(&c->rc))
1345                 return 0;
1346
1347         if (c->rc.next > c->os.next)
1348                 return 0;
1349
1350         /* Now the compressed buffer contains the data output by the forwards
1351          * bitstream, then empty space, then data output by the backwards
1352          * bitstream.  Move the data output by the backwards bitstream to be
1353          * adjacent to the data output by the forward bitstream, and calculate
1354          * the compressed size that this results in.  */
1355         num_forwards_bytes = (u8*)c->rc.next - (u8*)cdata;
1356         num_backwards_bytes = ((u8*)cdata + csize_avail) - (u8*)c->os.next;
1357
1358         memmove(cdata + num_forwards_bytes, c->os.next, num_backwards_bytes);
1359
1360         return num_forwards_bytes + num_backwards_bytes;
1361 }
1362
1363 /* Set internal compression parameters for the specified compression level and
1364  * maximum window size.  */
1365 static void
1366 lzms_build_params(unsigned int compression_level,
1367                   struct lzms_compressor_params *params)
1368 {
1369         /* Allow length 2 matches if the compression level is sufficiently high.
1370          */
1371         if (compression_level >= 45)
1372                 params->min_match_length = 2;
1373         else
1374                 params->min_match_length = 3;
1375
1376         /* Scale nice_match_length and max_search_depth with the compression
1377          * level.  But to allow an optimization on length cost calculations,
1378          * don't allow nice_match_length to exceed LZMS_NUM_FAST_LENGTH.  */
1379         params->nice_match_length = ((u64)compression_level * 32) / 50;
1380         if (params->nice_match_length < params->min_match_length)
1381                 params->nice_match_length = params->min_match_length;
1382         if (params->nice_match_length > LZMS_NUM_FAST_LENGTHS)
1383                 params->nice_match_length = LZMS_NUM_FAST_LENGTHS;
1384         params->max_search_depth = compression_level;
1385
1386         params->optim_array_length = 1024;
1387 }
1388
1389 /* Given the internal compression parameters and maximum window size, build the
1390  * Lempel-Ziv match-finder parameters.  */
1391 static void
1392 lzms_build_mf_params(const struct lzms_compressor_params *lzms_params,
1393                      u32 max_window_size, struct lz_mf_params *mf_params)
1394 {
1395         memset(mf_params, 0, sizeof(*mf_params));
1396
1397         /* Choose an appropriate match-finding algorithm.  */
1398         if (max_window_size <= 2097152)
1399                 mf_params->algorithm = LZ_MF_BINARY_TREES;
1400         else if (max_window_size <= 33554432)
1401                 mf_params->algorithm = LZ_MF_LCP_INTERVAL_TREE;
1402         else
1403                 mf_params->algorithm = LZ_MF_LINKED_SUFFIX_ARRAY;
1404
1405         mf_params->max_window_size = max_window_size;
1406         mf_params->min_match_len = lzms_params->min_match_length;
1407         mf_params->max_search_depth = lzms_params->max_search_depth;
1408         mf_params->nice_match_len = lzms_params->nice_match_length;
1409 }
1410
1411 static void
1412 lzms_free_compressor(void *_c);
1413
1414 static u64
1415 lzms_get_needed_memory(size_t max_block_size, unsigned int compression_level)
1416 {
1417         struct lzms_compressor_params params;
1418         struct lz_mf_params mf_params;
1419         u64 size = 0;
1420
1421         if (max_block_size >= INT32_MAX)
1422                 return 0;
1423
1424         lzms_build_params(compression_level, &params);
1425         lzms_build_mf_params(&params, max_block_size, &mf_params);
1426
1427         size += sizeof(struct lzms_compressor);
1428
1429         /* cur_window */
1430         size += max_block_size;
1431
1432         /* mf */
1433         size += lz_mf_get_needed_memory(mf_params.algorithm, max_block_size);
1434
1435         /* matches */
1436         size += min(params.max_search_depth, params.nice_match_length) *
1437                 sizeof(struct lz_match);
1438
1439         /* optimum */
1440         size += (params.optim_array_length + params.nice_match_length) *
1441                 sizeof(struct lzms_mc_pos_data);
1442
1443         return size;
1444 }
1445
1446 static int
1447 lzms_create_compressor(size_t max_block_size, unsigned int compression_level,
1448                        void **ctx_ret)
1449 {
1450         struct lzms_compressor *c;
1451         struct lzms_compressor_params params;
1452         struct lz_mf_params mf_params;
1453
1454         if (max_block_size >= INT32_MAX)
1455                 return WIMLIB_ERR_INVALID_PARAM;
1456
1457         lzms_build_params(compression_level, &params);
1458         lzms_build_mf_params(&params, max_block_size, &mf_params);
1459         if (!lz_mf_params_valid(&mf_params))
1460                 return WIMLIB_ERR_INVALID_PARAM;
1461
1462         c = CALLOC(1, sizeof(struct lzms_compressor));
1463         if (!c)
1464                 goto oom;
1465
1466         c->params = params;
1467
1468         c->cur_window = MALLOC(max_block_size);
1469         if (!c->cur_window)
1470                 goto oom;
1471
1472         c->mf = lz_mf_alloc(&mf_params);
1473         if (!c->mf)
1474                 goto oom;
1475
1476         c->matches = MALLOC(min(params.max_search_depth,
1477                                 params.nice_match_length) *
1478                             sizeof(struct lz_match));
1479         if (!c->matches)
1480                 goto oom;
1481
1482         c->optimum = MALLOC((params.optim_array_length +
1483                              params.nice_match_length) *
1484                             sizeof(struct lzms_mc_pos_data));
1485         if (!c->optimum)
1486                 goto oom;
1487         c->optimum_end = &c->optimum[params.optim_array_length];
1488
1489         lzms_init_slots();
1490
1491         lzms_init_rc_costs();
1492
1493         lzms_init_fast_slots(c);
1494
1495         *ctx_ret = c;
1496         return 0;
1497
1498 oom:
1499         lzms_free_compressor(c);
1500         return WIMLIB_ERR_NOMEM;
1501 }
1502
1503 static size_t
1504 lzms_compress(const void *uncompressed_data, size_t uncompressed_size,
1505               void *compressed_data, size_t compressed_size_avail, void *_c)
1506 {
1507         struct lzms_compressor *c = _c;
1508
1509         /* Don't bother compressing extremely small inputs.  */
1510         if (uncompressed_size < 4)
1511                 return 0;
1512
1513         /* Cap the available compressed size to a 32-bit integer and round it
1514          * down to the nearest multiple of 2.  */
1515         if (compressed_size_avail > UINT32_MAX)
1516                 compressed_size_avail = UINT32_MAX;
1517         if (compressed_size_avail & 1)
1518                 compressed_size_avail--;
1519
1520         /* Initialize the compressor structures.  */
1521         lzms_prepare_compressor(c, uncompressed_data, uncompressed_size,
1522                                 compressed_data, compressed_size_avail / 2);
1523
1524         /* Preprocess the uncompressed data.  */
1525         lzms_x86_filter(c->cur_window, c->cur_window_size,
1526                         c->last_target_usages, false);
1527
1528         /* Load the window into the match-finder.  */
1529         lz_mf_load_window(c->mf, c->cur_window, c->cur_window_size);
1530
1531         /* Compute and encode a literal/match sequence that decompresses to the
1532          * preprocessed data.  */
1533         lzms_near_optimal_parse(c);
1534
1535         /* Return the compressed data size or 0.  */
1536         return lzms_finalize(c, compressed_data, compressed_size_avail);
1537 }
1538
1539 static void
1540 lzms_free_compressor(void *_c)
1541 {
1542         struct lzms_compressor *c = _c;
1543
1544         if (c) {
1545                 FREE(c->cur_window);
1546                 lz_mf_free(c->mf);
1547                 FREE(c->matches);
1548                 FREE(c->optimum);
1549                 FREE(c);
1550         }
1551 }
1552
1553 const struct compressor_ops lzms_compressor_ops = {
1554         .get_needed_memory  = lzms_get_needed_memory,
1555         .create_compressor  = lzms_create_compressor,
1556         .compress           = lzms_compress,
1557         .free_compressor    = lzms_free_compressor,
1558 };