1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file filter_common.c
4 /// \brief Filter-specific stuff common for both encoder and decoder
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "filter_common.h"
20 /// Size of the filter-specific options structure
23 /// True if it is OK to use this filter as non-last filter in
27 /// True if it is OK to use this filter as the last filter in
31 /// True if the filter may change the size of the data (that is, the
32 /// amount of encoded output can be different than the amount of
33 /// uncompressed input).
37 #if defined (HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
39 .id = LZMA_FILTER_LZMA1,
40 .options_size = sizeof(lzma_options_lzma),
46 #if defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
48 .id = LZMA_FILTER_LZMA2,
49 .options_size = sizeof(lzma_options_lzma),
55 #if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
57 .id = LZMA_FILTER_X86,
58 .options_size = sizeof(lzma_options_bcj),
61 .changes_size = false,
64 #if defined(HAVE_ENCODER_POWERPC) || defined(HAVE_DECODER_POWERPC)
66 .id = LZMA_FILTER_POWERPC,
67 .options_size = sizeof(lzma_options_bcj),
70 .changes_size = false,
73 #if defined(HAVE_ENCODER_IA64) || defined(HAVE_DECODER_IA64)
75 .id = LZMA_FILTER_IA64,
76 .options_size = sizeof(lzma_options_bcj),
79 .changes_size = false,
82 #if defined(HAVE_ENCODER_ARM) || defined(HAVE_DECODER_ARM)
84 .id = LZMA_FILTER_ARM,
85 .options_size = sizeof(lzma_options_bcj),
88 .changes_size = false,
91 #if defined(HAVE_ENCODER_ARMTHUMB) || defined(HAVE_DECODER_ARMTHUMB)
93 .id = LZMA_FILTER_ARMTHUMB,
94 .options_size = sizeof(lzma_options_bcj),
97 .changes_size = false,
100 #if defined(HAVE_ENCODER_SPARC) || defined(HAVE_DECODER_SPARC)
102 .id = LZMA_FILTER_SPARC,
103 .options_size = sizeof(lzma_options_bcj),
106 .changes_size = false,
109 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
111 .id = LZMA_FILTER_DELTA,
112 .options_size = sizeof(lzma_options_delta),
115 .changes_size = false,
119 .id = LZMA_VLI_UNKNOWN
124 extern LZMA_API(lzma_ret)
125 lzma_filters_copy(const lzma_filter *src, lzma_filter *dest,
126 const lzma_allocator *allocator)
128 if (src == NULL || dest == NULL)
129 return LZMA_PROG_ERROR;
133 for (i = 0; src[i].id != LZMA_VLI_UNKNOWN; ++i) {
134 // There must be a maximum of four filters plus
135 // the array terminator.
136 if (i == LZMA_FILTERS_MAX) {
137 ret = LZMA_OPTIONS_ERROR;
141 dest[i].id = src[i].id;
143 if (src[i].options == NULL) {
144 dest[i].options = NULL;
146 // See if the filter is supported only when the
147 // options is not NULL. This might be convenient
148 // sometimes if the app is actually copying only
149 // a partial filter chain with a place holder ID.
151 // When options is not NULL, the Filter ID must be
152 // supported by us, because otherwise we don't know
153 // how big the options are.
155 for (j = 0; src[i].id != features[j].id; ++j) {
156 if (features[j].id == LZMA_VLI_UNKNOWN) {
157 ret = LZMA_OPTIONS_ERROR;
162 // Allocate and copy the options.
163 dest[i].options = lzma_alloc(features[j].options_size,
165 if (dest[i].options == NULL) {
166 ret = LZMA_MEM_ERROR;
170 memcpy(dest[i].options, src[i].options,
171 features[j].options_size);
175 // Terminate the filter array.
176 assert(i <= LZMA_FILTERS_MAX + 1);
177 dest[i].id = LZMA_VLI_UNKNOWN;
178 dest[i].options = NULL;
183 // Free the options which we have already allocated.
185 lzma_free(dest[i].options, allocator);
186 dest[i].options = NULL;
194 validate_chain(const lzma_filter *filters, size_t *count)
196 // There must be at least one filter.
197 if (filters == NULL || filters[0].id == LZMA_VLI_UNKNOWN)
198 return LZMA_PROG_ERROR;
200 // Number of non-last filters that may change the size of the data
201 // significantly (that is, more than 1-2 % or so).
202 size_t changes_size_count = 0;
204 // True if it is OK to add a new filter after the current filter.
205 bool non_last_ok = true;
207 // True if the last filter in the given chain is actually usable as
208 // the last filter. Only filters that support embedding End of Payload
209 // Marker can be used as the last filter in the chain.
210 bool last_ok = false;
215 for (j = 0; filters[i].id != features[j].id; ++j)
216 if (features[j].id == LZMA_VLI_UNKNOWN)
217 return LZMA_OPTIONS_ERROR;
219 // If the previous filter in the chain cannot be a non-last
220 // filter, the chain is invalid.
222 return LZMA_OPTIONS_ERROR;
224 non_last_ok = features[j].non_last_ok;
225 last_ok = features[j].last_ok;
226 changes_size_count += features[j].changes_size;
228 } while (filters[++i].id != LZMA_VLI_UNKNOWN);
230 // There must be 1-4 filters. The last filter must be usable as
231 // the last filter in the chain. A maximum of three filters are
232 // allowed to change the size of the data.
233 if (i > LZMA_FILTERS_MAX || !last_ok || changes_size_count > 3)
234 return LZMA_OPTIONS_ERROR;
242 lzma_raw_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
243 const lzma_filter *options,
244 lzma_filter_find coder_find, bool is_encoder)
246 // Do some basic validation and get the number of filters.
248 return_if_error(validate_chain(options, &count));
250 // Set the filter functions and copy the options pointer.
251 lzma_filter_info filters[LZMA_FILTERS_MAX + 1];
253 for (size_t i = 0; i < count; ++i) {
254 // The order of the filters is reversed in the
255 // encoder. It allows more efficient handling
256 // of the uncompressed data.
257 const size_t j = count - i - 1;
259 const lzma_filter_coder *const fc
260 = coder_find(options[i].id);
261 if (fc == NULL || fc->init == NULL)
262 return LZMA_OPTIONS_ERROR;
264 filters[j].id = options[i].id;
265 filters[j].init = fc->init;
266 filters[j].options = options[i].options;
269 for (size_t i = 0; i < count; ++i) {
270 const lzma_filter_coder *const fc
271 = coder_find(options[i].id);
272 if (fc == NULL || fc->init == NULL)
273 return LZMA_OPTIONS_ERROR;
275 filters[i].id = options[i].id;
276 filters[i].init = fc->init;
277 filters[i].options = options[i].options;
281 // Terminate the array.
282 filters[count].id = LZMA_VLI_UNKNOWN;
283 filters[count].init = NULL;
285 // Initialize the filters.
286 const lzma_ret ret = lzma_next_filter_init(next, allocator, filters);
288 lzma_next_end(next, allocator);
295 lzma_raw_coder_memusage(lzma_filter_find coder_find,
296 const lzma_filter *filters)
298 // The chain has to have at least one filter.
301 if (validate_chain(filters, &tmp) != LZMA_OK)
309 const lzma_filter_coder *const fc
310 = coder_find(filters[i].id);
312 return UINT64_MAX; // Unsupported Filter ID
314 if (fc->memusage == NULL) {
315 // This filter doesn't have a function to calculate
316 // the memory usage and validate the options. Such
317 // filters need only little memory, so we use 1 KiB
318 // as a good estimate. They also accept all possible
319 // options, so there's no need to worry about lack
323 // Call the filter-specific memory usage calculation
326 = fc->memusage(filters[i].options);
327 if (usage == UINT64_MAX)
328 return UINT64_MAX; // Invalid options
332 } while (filters[++i].id != LZMA_VLI_UNKNOWN);
334 // Add some fixed amount of extra. It's to compensate memory usage
335 // of Stream, Block etc. coders, malloc() overhead, stack etc.
336 return total + LZMA_MEMUSAGE_BASE;