]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lzma/lzma_encoder_init.c
Revised the fastpos code. It now uses the slightly faster
[icculus/xz.git] / src / liblzma / lzma / lzma_encoder_init.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_encoder_init.c
4 /// \brief      Creating, resetting and destroying the 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
23
24 /// \brief      Initializes the length encoder
25 static void
26 length_encoder_reset(lzma_length_encoder *lencoder,
27                 const uint32_t num_pos_states, const uint32_t table_size)
28 {
29         // NLength::CPriceTableEncoder::SetTableSize()
30         lencoder->table_size = table_size;
31
32         // NLength::CEncoder::Init()
33         bit_reset(lencoder->choice);
34         bit_reset(lencoder->choice2);
35
36         for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
37                 bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS);
38                 bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS);
39         }
40
41         bittree_reset(lencoder->high, LEN_HIGH_BITS);
42
43         // NLength::CPriceTableEncoder::UpdateTables()
44         for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state)
45                 lzma_length_encoder_update_table(lencoder, pos_state);
46
47         return;
48 }
49
50
51 static void
52 lzma_lzma_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
53 {
54         lzma_lz_encoder_end(&coder->lz, allocator);
55         lzma_literal_end(&coder->literal_coder, allocator);
56         lzma_free(coder, allocator);
57         return;
58 }
59
60
61 extern lzma_ret
62 lzma_lzma_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
63                 const lzma_filter_info *filters)
64 {
65         if (next->coder == NULL) {
66                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
67                 if (next->coder == NULL)
68                         return LZMA_MEM_ERROR;
69
70                 next->coder->next = LZMA_NEXT_CODER_INIT;
71                 next->coder->lz = LZMA_LZ_ENCODER_INIT;
72                 next->coder->literal_coder = NULL;
73         }
74
75         // Validate options that aren't validated elsewhere.
76         const lzma_options_lzma *options = filters[0].options;
77         if (options->pos_bits > LZMA_POS_BITS_MAX
78                         || options->fast_bytes < LZMA_FAST_BYTES_MIN
79                         || options->fast_bytes > LZMA_FAST_BYTES_MAX) {
80                 lzma_lzma_encoder_end(next->coder, allocator);
81                 return LZMA_HEADER_ERROR;
82         }
83
84         // Set compression mode.
85         switch (options->mode) {
86                 case LZMA_MODE_FAST:
87                         next->coder->best_compression = false;
88                         break;
89
90                 case LZMA_MODE_BEST:
91                         next->coder->best_compression = true;
92                         break;
93
94                 default:
95                         lzma_lzma_encoder_end(next->coder, allocator);
96                         return LZMA_HEADER_ERROR;
97         }
98
99         // Initialize literal coder.
100         {
101                 const lzma_ret ret = lzma_literal_init(
102                                 &next->coder->literal_coder, allocator,
103                                 options->literal_context_bits,
104                                 options->literal_pos_bits);
105                 if (ret != LZMA_OK) {
106                         lzma_lzma_encoder_end(next->coder, allocator);
107                         return ret;
108                 }
109         }
110
111         // Initialize LZ encoder.
112         {
113                 const lzma_ret ret = lzma_lz_encoder_reset(
114                                 &next->coder->lz, allocator, &lzma_lzma_encode,
115                                 filters[0].uncompressed_size,
116                                 options->dictionary_size, OPTS,
117                                 options->fast_bytes, MATCH_MAX_LEN + 1 + OPTS,
118                                 options->match_finder,
119                                 options->match_finder_cycles,
120                                 options->preset_dictionary,
121                                 options->preset_dictionary_size);
122                 if (ret != LZMA_OK) {
123                         lzma_lzma_encoder_end(next->coder, allocator);
124                         return ret;
125                 }
126         }
127
128         // Set dist_table_size.
129         {
130                 // Round the dictionary size up to next 2^n.
131                 uint32_t log_size;
132                 for (log_size = 0; (UINT32_C(1) << log_size)
133                                 < options->dictionary_size; ++log_size) ;
134
135                 next->coder->dist_table_size = log_size * 2;
136         }
137
138         // Misc FIXME desc
139         next->coder->dictionary_size = options->dictionary_size;
140         next->coder->pos_mask = (1U << options->pos_bits) - 1;
141         next->coder->fast_bytes = options->fast_bytes;
142
143         // Range coder
144         rc_reset(next->coder->rc);
145
146         // State
147         next->coder->state = 0;
148         next->coder->previous_byte = 0;
149         for (size_t i = 0; i < REP_DISTANCES; ++i)
150                 next->coder->rep_distances[i] = 0;
151
152         // Bit encoders
153         for (size_t i = 0; i < STATES; ++i) {
154                 for (size_t j = 0; j <= next->coder->pos_mask; ++j) {
155                         bit_reset(next->coder->is_match[i][j]);
156                         bit_reset(next->coder->is_rep0_long[i][j]);
157                 }
158
159                 bit_reset(next->coder->is_rep[i]);
160                 bit_reset(next->coder->is_rep0[i]);
161                 bit_reset(next->coder->is_rep1[i]);
162                 bit_reset(next->coder->is_rep2[i]);
163         }
164
165         for (size_t i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
166                 bit_reset(next->coder->pos_encoders[i]);
167
168         // Bit tree encoders
169         for (size_t i = 0; i < LEN_TO_POS_STATES; ++i)
170                 bittree_reset(next->coder->pos_slot_encoder[i], POS_SLOT_BITS);
171
172         bittree_reset(next->coder->pos_align_encoder, ALIGN_BITS);
173
174         // Length encoders
175         length_encoder_reset(&next->coder->len_encoder, 1U << options->pos_bits,
176                         options->fast_bytes + 1 - MATCH_MIN_LEN);
177
178         length_encoder_reset(&next->coder->rep_match_len_encoder,
179                         1U << options->pos_bits,
180                         next->coder->fast_bytes + 1 - MATCH_MIN_LEN);
181
182         // Misc
183         next->coder->longest_match_was_found = false;
184         next->coder->optimum_end_index = 0;
185         next->coder->optimum_current_index = 0;
186         next->coder->additional_offset = 0;
187
188         next->coder->now_pos = 0;
189         next->coder->is_initialized = false;
190
191         // Initialize the next decoder in the chain, if any.
192         {
193                 const lzma_ret ret = lzma_next_filter_init(&next->coder->next,
194                                 allocator, filters + 1);
195                 if (ret != LZMA_OK) {
196                         lzma_lzma_encoder_end(next->coder, allocator);
197                         return ret;
198                 }
199         }
200
201         // Initialization successful. Set the function pointers.
202         next->code = &lzma_lz_encode;
203         next->end = &lzma_lzma_encoder_end;
204
205         return LZMA_OK;
206 }
207
208
209 extern bool
210 lzma_lzma_encode_properties(const lzma_options_lzma *options, uint8_t *byte)
211 {
212         if (options->literal_context_bits > LZMA_LITERAL_CONTEXT_BITS_MAX
213                         || options->literal_pos_bits
214                                 > LZMA_LITERAL_POS_BITS_MAX
215                         || options->pos_bits > LZMA_POS_BITS_MAX)
216                 return true;
217
218         *byte = (options->pos_bits * 5 + options->literal_pos_bits) * 9
219                         + options->literal_context_bits;
220         assert(*byte <= (4 * 5 + 4) * 9 + 8);
221
222         return false;
223 }