1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file lzma_encoder_private.h
6 /// \brief Private definitions for LZMA encoder
8 // Authors: Igor Pavlov
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef LZMA_LZMA_ENCODER_PRIVATE_H
14 #define LZMA_LZMA_ENCODER_PRIVATE_H
16 #include "lz_encoder.h"
17 #include "range_encoder.h"
18 #include "lzma_common.h"
19 #include "lzma_encoder.h"
22 // Macro to compare if the first two bytes in two buffers differ. This is
23 // needed in lzma_lzma_optimum_*() to test if the match is at least
24 // MATCH_LEN_MIN bytes. Unaligned access gives tiny gain so there's no
25 // reason to not use it when it is supported.
26 #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
27 # define not_equal_16(a, b) (read16ne(a) != read16ne(b))
29 # define not_equal_16(a, b) \
30 ((a)[0] != (b)[0] || (a)[1] != (b)[1])
34 // Optimal - Number of entries in the optimum array.
35 #define OPTS (1 << 12)
41 probability low
[POS_STATES_MAX
][LEN_LOW_SYMBOLS
];
42 probability mid
[POS_STATES_MAX
][LEN_MID_SYMBOLS
];
43 probability high
[LEN_HIGH_SYMBOLS
];
45 uint32_t prices
[POS_STATES_MAX
][LEN_SYMBOLS
];
47 uint32_t counters
[POS_STATES_MAX
];
49 } lzma_length_encoder
;
53 lzma_lzma_state state
;
55 bool prev_1_is_literal
;
62 uint32_t pos_prev
; // pos_next;
70 struct lzma_lzma1_encoder_s
{
72 lzma_range_encoder rc
;
74 /// Uncompressed size (doesn't include possible preset dictionary)
77 /// If non-zero, produce at most this much output.
78 /// Some input may then be missing from the output.
81 /// If the above out_limit is non-zero, *uncomp_size_ptr is set to
82 /// the amount of uncompressed data that we were able to fit
83 /// in the output buffer.
84 uint64_t *uncomp_size_ptr
;
87 lzma_lzma_state state
;
89 /// The four most recent match distances
92 /// Array of match candidates
93 lzma_match matches
[MATCH_LEN_MAX
+ 1];
95 /// Number of match candidates in matches[]
96 uint32_t matches_count
;
98 /// Variable to hold the length of the longest match between calls
99 /// to lzma_lzma_optimum_*().
100 uint32_t longest_match_length
;
102 /// True if using getoptimumfast
105 /// True if the encoder has been initialized by encoding the first
106 /// byte as a literal.
109 /// True if the range encoder has been flushed, but not all bytes
110 /// have been written to the output buffer yet.
113 /// True if end of payload marker will be written.
116 uint32_t pos_mask
; ///< (1 << pos_bits) - 1
117 uint32_t literal_context_bits
;
118 uint32_t literal_mask
;
120 // These are the same as in lzma_decoder.c. See comments there.
121 probability literal
[LITERAL_CODERS_MAX
* LITERAL_CODER_SIZE
];
122 probability is_match
[STATES
][POS_STATES_MAX
];
123 probability is_rep
[STATES
];
124 probability is_rep0
[STATES
];
125 probability is_rep1
[STATES
];
126 probability is_rep2
[STATES
];
127 probability is_rep0_long
[STATES
][POS_STATES_MAX
];
128 probability dist_slot
[DIST_STATES
][DIST_SLOTS
];
129 probability dist_special
[FULL_DISTANCES
- DIST_MODEL_END
];
130 probability dist_align
[ALIGN_SIZE
];
132 // These are the same as in lzma_decoder.c except that the encoders
133 // include also price tables.
134 lzma_length_encoder match_len_encoder
;
135 lzma_length_encoder rep_len_encoder
;
138 uint32_t dist_slot_prices
[DIST_STATES
][DIST_SLOTS
];
139 uint32_t dist_prices
[DIST_STATES
][FULL_DISTANCES
];
140 uint32_t dist_table_size
;
141 uint32_t match_price_count
;
143 uint32_t align_prices
[ALIGN_SIZE
];
144 uint32_t align_price_count
;
147 uint32_t opts_end_index
;
148 uint32_t opts_current_index
;
149 lzma_optimal opts
[OPTS
];
153 extern void lzma_lzma_optimum_fast(
154 lzma_lzma1_encoder
*restrict coder
, lzma_mf
*restrict mf
,
155 uint32_t *restrict back_res
, uint32_t *restrict len_res
);
157 extern void lzma_lzma_optimum_normal(lzma_lzma1_encoder
*restrict coder
,
158 lzma_mf
*restrict mf
, uint32_t *restrict back_res
,
159 uint32_t *restrict len_res
, uint32_t position
);