]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lzma/lzma_encoder.c
Make the memusage functions of LZMA1 and LZMA2 decoders
[icculus/xz.git] / src / liblzma / lzma / lzma_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_encoder.c
4 /// \brief      LZMA encoder
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2007 Lasse Collin
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2.1 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #include "lzma_encoder_private.h"
22 #include "fastpos.h"
23
24
25 /////////////
26 // Literal //
27 /////////////
28
29 static inline void
30 literal_matched(lzma_range_encoder *rc, probability *subcoder,
31                 uint32_t match_byte, uint32_t symbol)
32 {
33         uint32_t offset = 0x100;
34         symbol += UINT32_C(1) << 8;
35
36         do {
37                 match_byte <<= 1;
38                 const uint32_t match_bit = match_byte & offset;
39                 const uint32_t subcoder_index
40                                 = offset + match_bit + (symbol >> 8);
41                 const uint32_t bit = (symbol >> 7) & 1;
42                 rc_bit(rc, &subcoder[subcoder_index], bit);
43
44                 symbol <<= 1;
45                 offset &= ~(match_byte ^ symbol);
46
47         } while (symbol < (UINT32_C(1) << 16));
48 }
49
50
51 static inline void
52 literal(lzma_coder *coder, lzma_mf *mf, uint32_t position)
53 {
54         // Locate the literal byte to be encoded and the subcoder.
55         const uint8_t cur_byte = mf->buffer[
56                         mf->read_pos - mf->read_ahead];
57         probability *subcoder = literal_subcoder(coder->literal,
58                         coder->literal_context_bits, coder->literal_pos_mask,
59                         position, mf->buffer[mf->read_pos - mf->read_ahead - 1]);
60
61         if (is_literal_state(coder->state)) {
62                 // Previous LZMA-symbol was a literal. Encode a normal
63                 // literal without a match byte.
64                 rc_bittree(&coder->rc, subcoder, 8, cur_byte);
65         } else {
66                 // Previous LZMA-symbol was a match. Use the last byte of
67                 // the match as a "match byte". That is, compare the bits
68                 // of the current literal and the match byte.
69                 const uint8_t match_byte = mf->buffer[
70                                 mf->read_pos - coder->reps[0] - 1
71                                 - mf->read_ahead];
72                 literal_matched(&coder->rc, subcoder, match_byte, cur_byte);
73         }
74
75         update_literal(coder->state);
76 }
77
78
79 //////////////////
80 // Match length //
81 //////////////////
82
83 static void
84 length_update_prices(lzma_length_encoder *lc, const uint32_t pos_state)
85 {
86         const uint32_t table_size = lc->table_size;
87         lc->counters[pos_state] = table_size;
88
89         const uint32_t a0 = rc_bit_0_price(lc->choice);
90         const uint32_t a1 = rc_bit_1_price(lc->choice);
91         const uint32_t b0 = a1 + rc_bit_0_price(lc->choice2);
92         const uint32_t b1 = a1 + rc_bit_1_price(lc->choice2);
93         uint32_t *const prices = lc->prices[pos_state];
94
95         uint32_t i;
96         for (i = 0; i < table_size && i < LEN_LOW_SYMBOLS; ++i)
97                 prices[i] = a0 + rc_bittree_price(lc->low[pos_state],
98                                 LEN_LOW_BITS, i);
99
100         for (; i < table_size && i < LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; ++i)
101                 prices[i] = b0 + rc_bittree_price(lc->mid[pos_state],
102                                 LEN_MID_BITS, i - LEN_LOW_SYMBOLS);
103
104         for (; i < table_size; ++i)
105                 prices[i] = b1 + rc_bittree_price(lc->high, LEN_HIGH_BITS,
106                                 i - LEN_LOW_SYMBOLS - LEN_MID_SYMBOLS);
107
108         return;
109 }
110
111
112 static inline void
113 length(lzma_range_encoder *rc, lzma_length_encoder *lc,
114                 const uint32_t pos_state, uint32_t len, const bool fast_mode)
115 {
116         assert(len <= MATCH_LEN_MAX);
117         len -= MATCH_LEN_MIN;
118
119         if (len < LEN_LOW_SYMBOLS) {
120                 rc_bit(rc, &lc->choice, 0);
121                 rc_bittree(rc, lc->low[pos_state], LEN_LOW_BITS, len);
122         } else {
123                 rc_bit(rc, &lc->choice, 1);
124                 len -= LEN_LOW_SYMBOLS;
125
126                 if (len < LEN_MID_SYMBOLS) {
127                         rc_bit(rc, &lc->choice2, 0);
128                         rc_bittree(rc, lc->mid[pos_state], LEN_MID_BITS, len);
129                 } else {
130                         rc_bit(rc, &lc->choice2, 1);
131                         len -= LEN_MID_SYMBOLS;
132                         rc_bittree(rc, lc->high, LEN_HIGH_BITS, len);
133                 }
134         }
135
136         // Only getoptimum uses the prices so don't update the table when
137         // in fast mode.
138         if (!fast_mode)
139                 if (--lc->counters[pos_state] == 0)
140                         length_update_prices(lc, pos_state);
141 }
142
143
144 ///////////
145 // Match //
146 ///////////
147
148 static inline void
149 match(lzma_coder *coder, const uint32_t pos_state,
150                 const uint32_t distance, const uint32_t len)
151 {
152         update_match(coder->state);
153
154         length(&coder->rc, &coder->match_len_encoder, pos_state, len,
155                         coder->fast_mode);
156
157         const uint32_t pos_slot = get_pos_slot(distance);
158         const uint32_t len_to_pos_state = get_len_to_pos_state(len);
159         rc_bittree(&coder->rc, coder->pos_slot[len_to_pos_state],
160                         POS_SLOT_BITS, pos_slot);
161
162         if (pos_slot >= START_POS_MODEL_INDEX) {
163                 const uint32_t footer_bits = (pos_slot >> 1) - 1;
164                 const uint32_t base = (2 | (pos_slot & 1)) << footer_bits;
165                 const uint32_t pos_reduced = distance - base;
166
167                 if (pos_slot < END_POS_MODEL_INDEX) {
168                         // Careful here: base - pos_slot - 1 can be -1, but
169                         // rc_bittree_reverse starts at probs[1], not probs[0].
170                         rc_bittree_reverse(&coder->rc,
171                                 coder->pos_special + base - pos_slot - 1,
172                                 footer_bits, pos_reduced);
173                 } else {
174                         rc_direct(&coder->rc, pos_reduced >> ALIGN_BITS,
175                                         footer_bits - ALIGN_BITS);
176                         rc_bittree_reverse(
177                                         &coder->rc, coder->pos_align,
178                                         ALIGN_BITS, pos_reduced & ALIGN_MASK);
179                         ++coder->align_price_count;
180                 }
181         }
182
183         coder->reps[3] = coder->reps[2];
184         coder->reps[2] = coder->reps[1];
185         coder->reps[1] = coder->reps[0];
186         coder->reps[0] = distance;
187         ++coder->match_price_count;
188 }
189
190
191 ////////////////////
192 // Repeated match //
193 ////////////////////
194
195 static inline void
196 rep_match(lzma_coder *coder, const uint32_t pos_state,
197                 const uint32_t rep, const uint32_t len)
198 {
199         if (rep == 0) {
200                 rc_bit(&coder->rc, &coder->is_rep0[coder->state], 0);
201                 rc_bit(&coder->rc,
202                                 &coder->is_rep0_long[coder->state][pos_state],
203                                 len != 1);
204         } else {
205                 const uint32_t distance = coder->reps[rep];
206                 rc_bit(&coder->rc, &coder->is_rep0[coder->state], 1);
207
208                 if (rep == 1) {
209                         rc_bit(&coder->rc, &coder->is_rep1[coder->state], 0);
210                 } else {
211                         rc_bit(&coder->rc, &coder->is_rep1[coder->state], 1);
212                         rc_bit(&coder->rc, &coder->is_rep2[coder->state],
213                                         rep - 2);
214
215                         if (rep == 3)
216                                 coder->reps[3] = coder->reps[2];
217
218                         coder->reps[2] = coder->reps[1];
219                 }
220
221                 coder->reps[1] = coder->reps[0];
222                 coder->reps[0] = distance;
223         }
224
225         if (len == 1) {
226                 update_short_rep(coder->state);
227         } else {
228                 length(&coder->rc, &coder->rep_len_encoder, pos_state, len,
229                                 coder->fast_mode);
230                 update_long_rep(coder->state);
231         }
232 }
233
234
235 //////////
236 // Main //
237 //////////
238
239 static void
240 encode_symbol(lzma_coder *coder, lzma_mf *mf,
241                 uint32_t back, uint32_t len, uint32_t position)
242 {
243         const uint32_t pos_state = position & coder->pos_mask;
244
245         if (back == UINT32_MAX) {
246                 // Literal i.e. eight-bit byte
247                 assert(len == 1);
248                 rc_bit(&coder->rc,
249                                 &coder->is_match[coder->state][pos_state], 0);
250                 literal(coder, mf, position);
251         } else {
252                 // Some type of match
253                 rc_bit(&coder->rc,
254                         &coder->is_match[coder->state][pos_state], 1);
255
256                 if (back < REP_DISTANCES) {
257                         // It's a repeated match i.e. the same distance
258                         // has been used earlier.
259                         rc_bit(&coder->rc, &coder->is_rep[coder->state], 1);
260                         rep_match(coder, pos_state, back, len);
261                 } else {
262                         // Normal match
263                         rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
264                         match(coder, pos_state, back - REP_DISTANCES, len);
265                 }
266         }
267
268         assert(mf->read_ahead >= len);
269         mf->read_ahead -= len;
270 }
271
272
273 static bool
274 encode_init(lzma_coder *coder, lzma_mf *mf)
275 {
276         if (mf->read_pos == mf->read_limit) {
277                 if (mf->action == LZMA_RUN)
278                         return false; // We cannot do anything.
279
280                 // We are finishing (we cannot get here when flushing).
281                 assert(mf->write_pos == mf->read_pos);
282                 assert(mf->action == LZMA_FINISH);
283         } else {
284                 // Do the actual initialization. The first LZMA symbol must
285                 // always be a literal.
286                 mf_skip(mf, 1);
287                 mf->read_ahead = 0;
288                 rc_bit(&coder->rc, &coder->is_match[0][0], 0);
289                 rc_bittree(&coder->rc, coder->literal[0], 8, mf->buffer[0]);
290         }
291
292         // Initialization is done (except if empty file).
293         coder->is_initialized = true;
294
295         return true;
296 }
297
298
299 static void
300 encode_eopm(lzma_coder *coder, uint32_t position)
301 {
302         const uint32_t pos_state = position & coder->pos_mask;
303         rc_bit(&coder->rc, &coder->is_match[coder->state][pos_state], 1);
304         rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
305         match(coder, pos_state, UINT32_MAX, MATCH_LEN_MIN);
306 }
307
308
309 /// Number of bytes that a single encoding loop in lzma_lzma_encode() can
310 /// consume from the dictionary. This limit comes from lzma_lzma_optimum()
311 /// and may need to be updated if that function is significantly modified.
312 #define LOOP_INPUT_MAX (OPTS + 1)
313
314
315 extern lzma_ret
316 lzma_lzma_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
317                 uint8_t *restrict out, size_t *restrict out_pos,
318                 size_t out_size, uint32_t limit)
319 {
320         // Initialize the stream if no data has been encoded yet.
321         if (!coder->is_initialized && !encode_init(coder, mf))
322                 return LZMA_OK;
323
324         // Get the lowest bits of the uncompressed offset from the LZ layer.
325         uint32_t position = mf_position(mf);
326
327         while (true) {
328                 // Encode pending bits, if any. Calling this before encoding
329                 // the next symbol is needed only with plain LZMA, since
330                 // LZMA2 always provides big enough buffer to flush
331                 // everything out from the range encoder. For the same reason,
332                 // rc_encode() never returns true when this function is used
333                 // as part of LZMA2 encoder.
334                 if (rc_encode(&coder->rc, out, out_pos, out_size)) {
335                         assert(limit == UINT32_MAX);
336                         return LZMA_OK;
337                 }
338
339                 // With LZMA2 we need to take care that compressed size of
340                 // a chunk doesn't get too big.
341                 // TODO
342                 if (limit != UINT32_MAX
343                                 && (mf->read_pos - mf->read_ahead >= limit
344                                         || *out_pos + rc_pending(&coder->rc)
345                                                 >= (UINT32_C(1) << 16)
346                                                         - LOOP_INPUT_MAX))
347                         break;
348
349                 // Check that there is some input to process.
350                 if (mf->read_pos >= mf->read_limit) {
351                         if (mf->action == LZMA_RUN)
352                                 return LZMA_OK;
353
354                         if (mf->read_ahead == 0)
355                                 break;
356                 }
357
358                 // Get optimal match (repeat position and length).
359                 // Value ranges for pos:
360                 //   - [0, REP_DISTANCES): repeated match
361                 //   - [REP_DISTANCES, UINT32_MAX):
362                 //     match at (pos - REP_DISTANCES)
363                 //   - UINT32_MAX: not a match but a literal
364                 // Value ranges for len:
365                 //   - [MATCH_LEN_MIN, MATCH_LEN_MAX]
366                 uint32_t len;
367                 uint32_t back;
368
369                 if (coder->fast_mode)
370                         lzma_lzma_optimum_fast(coder, mf, &back, &len);
371                 else
372                         lzma_lzma_optimum_normal(
373                                         coder, mf, &back, &len, position);
374
375                 encode_symbol(coder, mf, back, len, position);
376
377                 position += len;
378         }
379
380         if (!coder->is_flushed) {
381                 coder->is_flushed = true;
382
383                 // We don't support encoding plain LZMA streams without EOPM,
384                 // and LZMA2 doesn't use EOPM at LZMA level.
385                 if (limit == UINT32_MAX)
386                         encode_eopm(coder, position);
387
388                 // Flush the remaining bytes from the range encoder.
389                 rc_flush(&coder->rc);
390
391                 // Copy the remaining bytes to the output buffer. If there
392                 // isn't enough output space, we will copy out the remaining
393                 // bytes on the next call to this function by using
394                 // the rc_encode() call in the encoding loop above.
395                 if (rc_encode(&coder->rc, out, out_pos, out_size)) {
396                         assert(limit == UINT32_MAX);
397                         return LZMA_OK;
398                 }
399         }
400
401         // Make it ready for the next LZMA2 chunk.
402         coder->is_flushed = false;
403
404         return LZMA_STREAM_END;
405 }
406
407
408 static lzma_ret
409 lzma_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
410                 uint8_t *restrict out, size_t *restrict out_pos,
411                 size_t out_size)
412 {
413         // Plain LZMA has no support for sync-flushing.
414         if (unlikely(mf->action == LZMA_SYNC_FLUSH))
415                 return LZMA_OPTIONS_ERROR;
416
417         return lzma_lzma_encode(coder, mf, out, out_pos, out_size, UINT32_MAX);
418 }
419
420
421 ////////////////////
422 // Initialization //
423 ////////////////////
424
425 static void
426 set_lz_options(lzma_lz_options *lz_options, const lzma_options_lzma *options)
427 {
428         // LZ encoder initialization does the validation, also when just
429         // calculating memory usage, so we don't need to validate here.
430         lz_options->before_size = OPTS;
431         lz_options->dict_size = options->dict_size;
432         lz_options->after_size = LOOP_INPUT_MAX;
433         lz_options->match_len_max = MATCH_LEN_MAX;
434         lz_options->nice_len = options->nice_len;
435         lz_options->match_finder = options->mf;
436         lz_options->depth = options->depth;
437         lz_options->preset_dict = options->preset_dict;
438         lz_options->preset_dict_size = options->preset_dict_size;
439 }
440
441
442 static void
443 length_encoder_reset(lzma_length_encoder *lencoder,
444                 const uint32_t num_pos_states, const bool fast_mode)
445 {
446         bit_reset(lencoder->choice);
447         bit_reset(lencoder->choice2);
448
449         for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
450                 bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS);
451                 bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS);
452         }
453
454         bittree_reset(lencoder->high, LEN_HIGH_BITS);
455
456         if (!fast_mode)
457                 for (size_t pos_state = 0; pos_state < num_pos_states;
458                                 ++pos_state)
459                         length_update_prices(lencoder, pos_state);
460
461         return;
462 }
463
464
465 extern void
466 lzma_lzma_encoder_reset(lzma_coder *coder, const lzma_options_lzma *options)
467 {
468         assert(!coder->is_flushed);
469
470         coder->pos_mask = (1U << options->pb) - 1;
471         coder->literal_context_bits = options->lc;
472         coder->literal_pos_mask = (1U << options->lp) - 1;
473
474         // Range coder
475         rc_reset(&coder->rc);
476
477         // State
478         coder->state = 0;
479         for (size_t i = 0; i < REP_DISTANCES; ++i)
480                 coder->reps[i] = 0;
481
482         literal_init(coder->literal, options->lc, options->lp);
483
484         // Bit encoders
485         for (size_t i = 0; i < STATES; ++i) {
486                 for (size_t j = 0; j <= coder->pos_mask; ++j) {
487                         bit_reset(coder->is_match[i][j]);
488                         bit_reset(coder->is_rep0_long[i][j]);
489                 }
490
491                 bit_reset(coder->is_rep[i]);
492                 bit_reset(coder->is_rep0[i]);
493                 bit_reset(coder->is_rep1[i]);
494                 bit_reset(coder->is_rep2[i]);
495         }
496
497         for (size_t i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
498                 bit_reset(coder->pos_special[i]);
499
500         // Bit tree encoders
501         for (size_t i = 0; i < LEN_TO_POS_STATES; ++i)
502                 bittree_reset(coder->pos_slot[i], POS_SLOT_BITS);
503
504         bittree_reset(coder->pos_align, ALIGN_BITS);
505
506         // Length encoders
507         length_encoder_reset(&coder->match_len_encoder,
508                         1U << options->pb, coder->fast_mode);
509
510         length_encoder_reset(&coder->rep_len_encoder,
511                         1U << options->pb, coder->fast_mode);
512
513         // Price counts are incremented every time appropriate probabilities
514         // are changed. price counts are set to zero when the price tables
515         // are updated, which is done when the appropriate price counts have
516         // big enough value, and lzma_mf.read_ahead == 0 which happens at
517         // least every OPTS (a few thousand) possible price count increments.
518         //
519         // By resetting price counts to UINT32_MAX / 2, we make sure that the
520         // price tables will be initialized before they will be used (since
521         // the value is definitely big enough), and that it is OK to increment
522         // price counts without risk of integer overflow (since UINT32_MAX / 2
523         // is small enough). The current code doesn't increment price counts
524         // before initializing price tables, but it maybe done in future if
525         // we add support for saving the state between LZMA2 chunks.
526         coder->match_price_count = UINT32_MAX / 2;
527         coder->align_price_count = UINT32_MAX / 2;
528
529         coder->opts_end_index = 0;
530         coder->opts_current_index = 0;
531 }
532
533
534 extern lzma_ret
535 lzma_lzma_encoder_create(lzma_coder **coder_ptr, lzma_allocator *allocator,
536                 const lzma_options_lzma *options, lzma_lz_options *lz_options)
537 {
538         if (*coder_ptr == NULL) {
539                 *coder_ptr = lzma_alloc(sizeof(lzma_coder), allocator);
540                 if (*coder_ptr == NULL)
541                         return LZMA_MEM_ERROR;
542         }
543
544         lzma_coder *coder = *coder_ptr;
545
546         // Validate some of the options. LZ encoder validates fast_bytes too
547         // but we need a valid value here earlier.
548         if (!is_lclppb_valid(options) || options->nice_len < MATCH_LEN_MIN
549                         || options->nice_len > MATCH_LEN_MAX)
550                 return LZMA_OPTIONS_ERROR;
551
552         // Set compression mode.
553         switch (options->mode) {
554                 case LZMA_MODE_FAST:
555                         coder->fast_mode = true;
556                         break;
557
558                 case LZMA_MODE_NORMAL: {
559                         coder->fast_mode = false;
560
561                         // Set dist_table_size.
562                         // Round the dictionary size up to next 2^n.
563                         uint32_t log_size = 0;
564                         while ((UINT32_C(1) << log_size) < options->dict_size)
565                                 ++log_size;
566
567                         coder->dist_table_size = log_size * 2;
568
569                         // Length encoders' price table size
570                         coder->match_len_encoder.table_size
571                                 = options->nice_len + 1 - MATCH_LEN_MIN;
572                         coder->rep_len_encoder.table_size
573                                 = options->nice_len + 1 - MATCH_LEN_MIN;
574                         break;
575                 }
576
577                 default:
578                         return LZMA_OPTIONS_ERROR;
579         }
580
581         coder->is_initialized = false;
582         coder->is_flushed = false;
583
584         lzma_lzma_encoder_reset(coder, options);
585
586         set_lz_options(lz_options, options);
587
588         return LZMA_OK;
589 }
590
591
592 static lzma_ret
593 lzma_encoder_init(lzma_lz_encoder *lz, lzma_allocator *allocator,
594                 const void *options, lzma_lz_options *lz_options)
595 {
596         lz->code = &lzma_encode;
597         return lzma_lzma_encoder_create(
598                         &lz->coder, allocator, options, lz_options);
599 }
600
601
602 extern lzma_ret
603 lzma_lzma_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
604                 const lzma_filter_info *filters)
605 {
606         return lzma_lz_encoder_init(
607                         next, allocator, filters, &lzma_encoder_init);
608 }
609
610
611 extern uint64_t
612 lzma_lzma_encoder_memusage(const void *options)
613 {
614         lzma_lz_options lz_options;
615         set_lz_options(&lz_options, options);
616
617         const uint64_t lz_memusage = lzma_lz_encoder_memusage(&lz_options);
618         if (lz_memusage == UINT64_MAX)
619                 return UINT64_MAX;
620
621         return (uint64_t)(sizeof(lzma_coder)) + lz_memusage;
622 }
623
624
625 extern bool
626 lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte)
627 {
628         if (!is_lclppb_valid(options))
629                 return true;
630
631         *byte = (options->pb * 5 + options->lp) * 9 + options->lc;
632         assert(*byte <= (4 * 5 + 4) * 9 + 8);
633
634         return false;
635 }
636
637
638 #ifdef HAVE_ENCODER_LZMA1
639 extern lzma_ret
640 lzma_lzma_props_encode(const void *options, uint8_t *out)
641 {
642         const lzma_options_lzma *const opt = options;
643
644         if (lzma_lzma_lclppb_encode(opt, out))
645                 return LZMA_PROG_ERROR;
646
647         integer_write_32(out + 1, opt->dict_size);
648
649         return LZMA_OK;
650 }
651 #endif
652
653
654 extern LZMA_API lzma_bool
655 lzma_mode_is_available(lzma_mode mode)
656 {
657         return mode == LZMA_MODE_FAST || mode == LZMA_MODE_NORMAL;
658 }