xref: /freebsd/contrib/xz/src/liblzma/lzma/lzma_encoder_private.h (revision e9b411d273336647e61704213964b995952a44fd)
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_encoder_private.h
4 /// \brief      Private definitions for LZMA encoder
5 ///
6 //  Authors:    Igor Pavlov
7 //              Lasse Collin
8 //
9 //  This file has been put into the public domain.
10 //  You can do whatever you want with this file.
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13 
14 #ifndef LZMA_LZMA_ENCODER_PRIVATE_H
15 #define LZMA_LZMA_ENCODER_PRIVATE_H
16 
17 #include "lz_encoder.h"
18 #include "range_encoder.h"
19 #include "lzma_common.h"
20 #include "lzma_encoder.h"
21 
22 
23 // Macro to compare if the first two bytes in two buffers differ. This is
24 // needed in lzma_lzma_optimum_*() to test if the match is at least
25 // MATCH_LEN_MIN bytes. Unaligned access gives tiny gain so there's no
26 // reason to not use it when it is supported.
27 #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
28 #	define not_equal_16(a, b) (read16ne(a) != read16ne(b))
29 #else
30 #	define not_equal_16(a, b) \
31 		((a)[0] != (b)[0] || (a)[1] != (b)[1])
32 #endif
33 
34 
35 // Optimal - Number of entries in the optimum array.
36 #define OPTS (1 << 12)
37 
38 
39 typedef struct {
40 	probability choice;
41 	probability choice2;
42 	probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
43 	probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
44 	probability high[LEN_HIGH_SYMBOLS];
45 
46 	uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS];
47 	uint32_t table_size;
48 	uint32_t counters[POS_STATES_MAX];
49 
50 } lzma_length_encoder;
51 
52 
53 typedef struct {
54 	lzma_lzma_state state;
55 
56 	bool prev_1_is_literal;
57 	bool prev_2;
58 
59 	uint32_t pos_prev_2;
60 	uint32_t back_prev_2;
61 
62 	uint32_t price;
63 	uint32_t pos_prev;  // pos_next;
64 	uint32_t back_prev;
65 
66 	uint32_t backs[REPS];
67 
68 } lzma_optimal;
69 
70 
71 struct lzma_lzma1_encoder_s {
72 	/// Range encoder
73 	lzma_range_encoder rc;
74 
75 	/// Uncompressed size (doesn't include possible preset dictionary)
76 	uint64_t uncomp_size;
77 
78 	/// If non-zero, produce at most this much output.
79 	/// Some input may then be missing from the output.
80 	uint64_t out_limit;
81 
82 	/// If the above out_limit is non-zero, *uncomp_size_ptr is set to
83 	/// the amount of uncompressed data that we were able to fit
84 	/// in the output buffer.
85 	uint64_t *uncomp_size_ptr;
86 
87 	/// State
88 	lzma_lzma_state state;
89 
90 	/// The four most recent match distances
91 	uint32_t reps[REPS];
92 
93 	/// Array of match candidates
94 	lzma_match matches[MATCH_LEN_MAX + 1];
95 
96 	/// Number of match candidates in matches[]
97 	uint32_t matches_count;
98 
99 	/// Variable to hold the length of the longest match between calls
100 	/// to lzma_lzma_optimum_*().
101 	uint32_t longest_match_length;
102 
103 	/// True if using getoptimumfast
104 	bool fast_mode;
105 
106 	/// True if the encoder has been initialized by encoding the first
107 	/// byte as a literal.
108 	bool is_initialized;
109 
110 	/// True if the range encoder has been flushed, but not all bytes
111 	/// have been written to the output buffer yet.
112 	bool is_flushed;
113 
114 	/// True if end of payload marker will be written.
115 	bool use_eopm;
116 
117 	uint32_t pos_mask;         ///< (1 << pos_bits) - 1
118 	uint32_t literal_context_bits;
119 	uint32_t literal_pos_mask;
120 
121 	// These are the same as in lzma_decoder.c. See comments there.
122 	probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
123 	probability is_match[STATES][POS_STATES_MAX];
124 	probability is_rep[STATES];
125 	probability is_rep0[STATES];
126 	probability is_rep1[STATES];
127 	probability is_rep2[STATES];
128 	probability is_rep0_long[STATES][POS_STATES_MAX];
129 	probability dist_slot[DIST_STATES][DIST_SLOTS];
130 	probability dist_special[FULL_DISTANCES - DIST_MODEL_END];
131 	probability dist_align[ALIGN_SIZE];
132 
133 	// These are the same as in lzma_decoder.c except that the encoders
134 	// include also price tables.
135 	lzma_length_encoder match_len_encoder;
136 	lzma_length_encoder rep_len_encoder;
137 
138 	// Price tables
139 	uint32_t dist_slot_prices[DIST_STATES][DIST_SLOTS];
140 	uint32_t dist_prices[DIST_STATES][FULL_DISTANCES];
141 	uint32_t dist_table_size;
142 	uint32_t match_price_count;
143 
144 	uint32_t align_prices[ALIGN_SIZE];
145 	uint32_t align_price_count;
146 
147 	// Optimal
148 	uint32_t opts_end_index;
149 	uint32_t opts_current_index;
150 	lzma_optimal opts[OPTS];
151 };
152 
153 
154 extern void lzma_lzma_optimum_fast(
155 		lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf,
156 		uint32_t *restrict back_res, uint32_t *restrict len_res);
157 
158 extern void lzma_lzma_optimum_normal(lzma_lzma1_encoder *restrict coder,
159 		lzma_mf *restrict mf, uint32_t *restrict back_res,
160 		uint32_t *restrict len_res, uint32_t position);
161 
162 #endif
163