]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lzma/lzma_encoder_private.h
Revised the fastpos code. It now uses the slightly faster
[icculus/xz.git] / src / liblzma / lzma / lzma_encoder_private.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_encoder_private.h
4 /// \brief      Private definitions for 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 #ifndef LZMA_LZMA_ENCODER_PRIVATE_H
22 #define LZMA_LZMA_ENCODER_PRIVATE_H
23
24 #include "lzma_encoder.h"
25 #include "lzma_common.h"
26 #include "lz_encoder.h"
27 #include "range_encoder.h"
28
29 // We need space for about two encoding loops, because there is no check
30 // for available buffer space before end of payload marker gets written.
31 // 2*26 bytes should be enough for this... but Lasse isn't very sure about
32 // the exact value. 64 bytes certainly is enough. :-)
33 #if LZMA_LZ_TEMP_SIZE < 64
34 #       error LZMA_LZ_TEMP_SIZE is too small.
35 #endif
36
37
38 #define move_pos(num) \
39 do { \
40         assert((int32_t)(num) >= 0); \
41         if ((num) != 0) { \
42                 coder->additional_offset += num; \
43                 coder->lz.skip(&coder->lz, num); \
44         } \
45 } while (0)
46
47
48 typedef struct {
49         probability choice;
50         probability choice2;
51         probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
52         probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
53         probability high[LEN_HIGH_SYMBOLS];
54
55         uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS];
56         uint32_t table_size;
57         uint32_t counters[POS_STATES_MAX];
58
59 } lzma_length_encoder;
60
61
62 typedef struct {
63         uint32_t state;
64
65         bool prev_1_is_char;
66         bool prev_2;
67
68         uint32_t pos_prev_2;
69         uint32_t back_prev_2;
70
71         uint32_t price;
72         uint32_t pos_prev;  // pos_next;
73         uint32_t back_prev;
74
75         uint32_t backs[4];
76
77 } lzma_optimal;
78
79
80 struct lzma_coder_s {
81         // Next coder in the chain
82         lzma_next_coder next;
83
84         // In window and match finder
85         lzma_lz_encoder lz;
86
87         // Range encoder
88         lzma_range_encoder rc;
89
90         // State
91         uint32_t state;
92         uint8_t previous_byte;
93         uint32_t rep_distances[REP_DISTANCES];
94
95         // Misc
96         uint32_t match_distances[MATCH_MAX_LEN * 2 + 2 + 1];
97         uint32_t num_distance_pairs;
98         uint32_t additional_offset;
99         uint32_t now_pos; // Lowest 32 bits are enough here.
100         bool best_compression;           ///< True when LZMA_MODE_BEST is used
101         bool is_initialized;
102
103         // Literal encoder
104         lzma_literal_coder *literal_coder;
105
106         // Bit encoders
107         probability is_match[STATES][POS_STATES_MAX];
108         probability is_rep[STATES];
109         probability is_rep0[STATES];
110         probability is_rep1[STATES];
111         probability is_rep2[STATES];
112         probability is_rep0_long[STATES][POS_STATES_MAX];
113         probability pos_encoders[FULL_DISTANCES - END_POS_MODEL_INDEX];
114
115         // Bit Tree Encoders
116         probability pos_slot_encoder[LEN_TO_POS_STATES][1 << POS_SLOT_BITS];
117         probability pos_align_encoder[1 << ALIGN_BITS];
118
119         // Length encoders
120         lzma_length_encoder len_encoder;
121         lzma_length_encoder rep_match_len_encoder;
122
123         // Optimal
124         lzma_optimal optimum[OPTS];
125         uint32_t optimum_end_index;
126         uint32_t optimum_current_index;
127         uint32_t longest_match_length;
128         bool longest_match_was_found;
129
130         // Prices
131         uint32_t pos_slot_prices[LEN_TO_POS_STATES][DIST_TABLE_SIZE_MAX];
132         uint32_t distances_prices[LEN_TO_POS_STATES][FULL_DISTANCES];
133         uint32_t align_prices[ALIGN_TABLE_SIZE];
134         uint32_t align_price_count;
135         uint32_t dist_table_size;
136         uint32_t match_price_count;
137
138         // LZMA specific settings
139         uint32_t dictionary_size;        ///< Size in bytes
140         uint32_t fast_bytes;
141         uint32_t pos_state_bits;
142         uint32_t pos_mask;         ///< (1 << pos_state_bits) - 1
143 };
144
145
146 extern void lzma_length_encoder_update_table(lzma_length_encoder *lencoder,
147                 const uint32_t pos_state);
148
149 extern bool lzma_lzma_encode(lzma_coder *coder, uint8_t *restrict out,
150                 size_t *restrict out_pos, size_t out_size);
151
152 extern void lzma_get_optimum(lzma_coder *restrict coder,
153                 uint32_t *restrict back_res, uint32_t *restrict len_res);
154
155 extern void lzma_get_optimum_fast(lzma_coder *restrict coder,
156                 uint32_t *restrict back_res, uint32_t *restrict len_res);
157
158
159 // NOTE: Don't add 'restrict'.
160 static inline void
161 lzma_read_match_distances(lzma_coder *coder,
162                 uint32_t *len_res, uint32_t *num_distance_pairs)
163 {
164         *len_res = 0;
165
166         coder->lz.get_matches(&coder->lz, coder->match_distances);
167
168         *num_distance_pairs = coder->match_distances[0];
169
170         if (*num_distance_pairs > 0) {
171                 *len_res = coder->match_distances[*num_distance_pairs - 1];
172                 assert(*len_res <= MATCH_MAX_LEN);
173
174                 if (*len_res == coder->fast_bytes) {
175                         uint32_t offset = *len_res - 1;
176                         const uint32_t distance = coder->match_distances[
177                                         *num_distance_pairs] + 1;
178                         uint32_t limit = MATCH_MAX_LEN - *len_res;
179
180                         assert(offset + limit < coder->lz.keep_size_after);
181                         assert(coder->lz.read_pos <= coder->lz.write_pos);
182
183                         // If we are close to end of the stream, we may need
184                         // to limit the length of the match.
185                         if (coder->lz.write_pos - coder->lz.read_pos
186                                         < offset + limit)
187                                 limit = coder->lz.write_pos
188                                         - (coder->lz.read_pos + offset);
189
190                         offset += coder->lz.read_pos;
191                         uint32_t i = 0;
192                         while (i < limit && coder->lz.buffer[offset + i]
193                                         == coder->lz.buffer[
194                                                 offset + i - distance])
195                                 ++i;
196
197                         *len_res += i;
198                 }
199         }
200
201         ++coder->additional_offset;
202
203         return;
204 }
205
206 #endif