1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma_encoder_init.c
4 /// \brief Creating, resetting and destroying the LZMA encoder
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007 Lasse Collin
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.
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.
19 ///////////////////////////////////////////////////////////////////////////////
21 #include "lzma_encoder_private.h"
24 /// \brief Initializes the length encoder
26 length_encoder_reset(lzma_length_encoder *lencoder,
27 const uint32_t num_pos_states, const uint32_t table_size)
29 // NLength::CPriceTableEncoder::SetTableSize()
30 lencoder->table_size = table_size;
32 // NLength::CEncoder::Init()
33 bit_reset(lencoder->choice);
34 bit_reset(lencoder->choice2);
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);
41 bittree_reset(lencoder->high, LEN_HIGH_BITS);
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);
52 lzma_lzma_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
54 lzma_lz_encoder_end(&coder->lz, allocator);
55 lzma_literal_end(&coder->literal_coder, allocator);
56 lzma_free(coder, allocator);
62 lzma_lzma_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
63 const lzma_filter_info *filters)
65 if (next->coder == NULL) {
66 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
67 if (next->coder == NULL)
68 return LZMA_MEM_ERROR;
70 next->coder->next = LZMA_NEXT_CODER_INIT;
71 next->coder->lz = LZMA_LZ_ENCODER_INIT;
72 next->coder->literal_coder = NULL;
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;
84 // Set compression mode.
85 switch (options->mode) {
87 next->coder->best_compression = false;
91 next->coder->best_compression = true;
95 lzma_lzma_encoder_end(next->coder, allocator);
96 return LZMA_HEADER_ERROR;
99 // Initialize literal coder.
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);
111 // Initialize LZ encoder.
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);
128 // Set dist_table_size.
130 // Round the dictionary size up to next 2^n.
132 for (log_size = 0; (UINT32_C(1) << log_size)
133 < options->dictionary_size; ++log_size) ;
135 next->coder->dist_table_size = log_size * 2;
139 next->coder->align_price_count = UINT32_MAX;
140 next->coder->match_price_count = UINT32_MAX;
141 next->coder->dictionary_size = options->dictionary_size;
142 next->coder->pos_mask = (1U << options->pos_bits) - 1;
143 next->coder->fast_bytes = options->fast_bytes;
146 rc_reset(next->coder->rc);
149 next->coder->state = 0;
150 next->coder->previous_byte = 0;
151 for (size_t i = 0; i < REP_DISTANCES; ++i)
152 next->coder->rep_distances[i] = 0;
155 for (size_t i = 0; i < STATES; ++i) {
156 for (size_t j = 0; j <= next->coder->pos_mask; ++j) {
157 bit_reset(next->coder->is_match[i][j]);
158 bit_reset(next->coder->is_rep0_long[i][j]);
161 bit_reset(next->coder->is_rep[i]);
162 bit_reset(next->coder->is_rep0[i]);
163 bit_reset(next->coder->is_rep1[i]);
164 bit_reset(next->coder->is_rep2[i]);
167 for (size_t i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
168 bit_reset(next->coder->pos_encoders[i]);
171 for (size_t i = 0; i < LEN_TO_POS_STATES; ++i)
172 bittree_reset(next->coder->pos_slot_encoder[i], POS_SLOT_BITS);
174 bittree_reset(next->coder->pos_align_encoder, ALIGN_BITS);
177 length_encoder_reset(&next->coder->len_encoder, 1U << options->pos_bits,
178 options->fast_bytes + 1 - MATCH_MIN_LEN);
180 length_encoder_reset(&next->coder->rep_match_len_encoder,
181 1U << options->pos_bits,
182 next->coder->fast_bytes + 1 - MATCH_MIN_LEN);
185 next->coder->longest_match_was_found = false;
186 next->coder->optimum_end_index = 0;
187 next->coder->optimum_current_index = 0;
188 next->coder->additional_offset = 0;
190 next->coder->now_pos = 0;
191 next->coder->is_initialized = false;
193 // Initialize the next decoder in the chain, if any.
195 const lzma_ret ret = lzma_next_filter_init(&next->coder->next,
196 allocator, filters + 1);
197 if (ret != LZMA_OK) {
198 lzma_lzma_encoder_end(next->coder, allocator);
203 // Initialization successful. Set the function pointers.
204 next->code = &lzma_lz_encode;
205 next->end = &lzma_lzma_encoder_end;
212 lzma_lzma_encode_properties(const lzma_options_lzma *options, uint8_t *byte)
214 if (options->literal_context_bits > LZMA_LITERAL_CONTEXT_BITS_MAX
215 || options->literal_pos_bits
216 > LZMA_LITERAL_POS_BITS_MAX
217 || options->pos_bits > LZMA_POS_BITS_MAX)
220 *byte = (options->pos_bits * 5 + options->literal_pos_bits) * 9
221 + options->literal_context_bits;
222 assert(*byte <= (4 * 5 + 4) * 9 + 8);