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) \
29                     (*(const uint16_t *)(a) != *(const uint16_t *)(b))
30 #else
31 #         define not_equal_16(a, b) \
32                     ((a)[0] != (b)[0] || (a)[1] != (b)[1])
33 #endif
34 
35 
36 // Optimal - Number of entries in the optimum array.
37 #define OPTS (1 << 12)
38 
39 
40 typedef struct {
41           probability choice;
42           probability choice2;
43           probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
44           probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
45           probability high[LEN_HIGH_SYMBOLS];
46 
47           uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS];
48           uint32_t table_size;
49           uint32_t counters[POS_STATES_MAX];
50 
51 } lzma_length_encoder;
52 
53 
54 typedef struct {
55           lzma_lzma_state state;
56 
57           bool prev_1_is_literal;
58           bool prev_2;
59 
60           uint32_t pos_prev_2;
61           uint32_t back_prev_2;
62 
63           uint32_t price;
64           uint32_t pos_prev;  // pos_next;
65           uint32_t back_prev;
66 
67           uint32_t backs[REPS];
68 
69 } lzma_optimal;
70 
71 
72 struct lzma_lzma1_encoder_s {
73           /// Range encoder
74           lzma_range_encoder rc;
75 
76           /// State
77           lzma_lzma_state state;
78 
79           /// The four most recent match distances
80           uint32_t reps[REPS];
81 
82           /// Array of match candidates
83           lzma_match matches[MATCH_LEN_MAX + 1];
84 
85           /// Number of match candidates in matches[]
86           uint32_t matches_count;
87 
88           /// Variable to hold the length of the longest match between calls
89           /// to lzma_lzma_optimum_*().
90           uint32_t longest_match_length;
91 
92           /// True if using getoptimumfast
93           bool fast_mode;
94 
95           /// True if the encoder has been initialized by encoding the first
96           /// byte as a literal.
97           bool is_initialized;
98 
99           /// True if the range encoder has been flushed, but not all bytes
100           /// have been written to the output buffer yet.
101           bool is_flushed;
102 
103           uint32_t pos_mask;         ///< (1 << pos_bits) - 1
104           uint32_t literal_context_bits;
105           uint32_t literal_pos_mask;
106 
107           // These are the same as in lzma_decoder.c. See comments there.
108           probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
109           probability is_match[STATES][POS_STATES_MAX];
110           probability is_rep[STATES];
111           probability is_rep0[STATES];
112           probability is_rep1[STATES];
113           probability is_rep2[STATES];
114           probability is_rep0_long[STATES][POS_STATES_MAX];
115           probability dist_slot[DIST_STATES][DIST_SLOTS];
116           probability dist_special[FULL_DISTANCES - DIST_MODEL_END];
117           probability dist_align[ALIGN_SIZE];
118 
119           // These are the same as in lzma_decoder.c except that the encoders
120           // include also price tables.
121           lzma_length_encoder match_len_encoder;
122           lzma_length_encoder rep_len_encoder;
123 
124           // Price tables
125           uint32_t dist_slot_prices[DIST_STATES][DIST_SLOTS];
126           uint32_t dist_prices[DIST_STATES][FULL_DISTANCES];
127           uint32_t dist_table_size;
128           uint32_t match_price_count;
129 
130           uint32_t align_prices[ALIGN_SIZE];
131           uint32_t align_price_count;
132 
133           // Optimal
134           uint32_t opts_end_index;
135           uint32_t opts_current_index;
136           lzma_optimal opts[OPTS];
137 };
138 
139 
140 extern void lzma_lzma_optimum_fast(
141                     lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf,
142                     uint32_t *restrict back_res, uint32_t *restrict len_res);
143 
144 extern void lzma_lzma_optimum_normal(lzma_lzma1_encoder *restrict coder,
145                     lzma_mf *restrict mf, uint32_t *restrict back_res,
146                     uint32_t *restrict len_res, uint32_t position);
147 
148 #endif
149