1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file filter_decoder.c
4 /// \brief Filter ID mapping to filter-specific functions
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_encoder.h"
14 #include "filter_common.h"
15 #include "lzma_encoder.h"
16 #include "lzma2_encoder.h"
17 #include "subblock_encoder.h"
18 #include "simple_encoder.h"
19 #include "delta_encoder.h"
26 /// Initializes the filter encoder and calls lzma_next_filter_init()
28 lzma_init_function init;
30 /// Calculates memory usage of the encoder. If the options are
31 /// invalid, UINT64_MAX is returned.
32 uint64_t (*memusage)(const void *options);
34 /// Calculates the minimum sane size for Blocks (or other types of
35 /// chunks) to which the input data can be split to make
36 /// multithreaded encoding possible. If this is NULL, it is assumed
37 /// that the encoder is fast enough with single thread.
38 lzma_vli (*chunk_size)(const void *options);
40 /// Tells the size of the Filter Properties field. If options are
41 /// invalid, UINT32_MAX is returned. If this is NULL, props_size_fixed
43 lzma_ret (*props_size_get)(uint32_t *size, const void *options);
44 uint32_t props_size_fixed;
46 /// Encodes Filter Properties.
48 /// \return - LZMA_OK: Properties encoded successfully.
49 /// - LZMA_OPTIONS_ERROR: Unsupported options
50 /// - LZMA_PROG_ERROR: Invalid options or not enough
52 lzma_ret (*props_encode)(const void *options, uint8_t *out);
54 } lzma_filter_encoder;
57 static const lzma_filter_encoder encoders[] = {
58 #ifdef HAVE_ENCODER_LZMA1
60 .id = LZMA_FILTER_LZMA1,
61 .init = &lzma_lzma_encoder_init,
62 .memusage = &lzma_lzma_encoder_memusage,
63 .chunk_size = NULL, // FIXME
64 .props_size_get = NULL,
65 .props_size_fixed = 5,
66 .props_encode = &lzma_lzma_props_encode,
69 #ifdef HAVE_ENCODER_LZMA2
71 .id = LZMA_FILTER_LZMA2,
72 .init = &lzma_lzma2_encoder_init,
73 .memusage = &lzma_lzma2_encoder_memusage,
74 .chunk_size = NULL, // FIXME
75 .props_size_get = NULL,
76 .props_size_fixed = 1,
77 .props_encode = &lzma_lzma2_props_encode,
80 #ifdef HAVE_ENCODER_SUBBLOCK
82 .id = LZMA_FILTER_SUBBLOCK,
83 .init = &lzma_subblock_encoder_init,
84 // .memusage = &lzma_subblock_encoder_memusage,
86 .props_size_get = NULL,
87 .props_size_fixed = 0,
91 #ifdef HAVE_ENCODER_X86
93 .id = LZMA_FILTER_X86,
94 .init = &lzma_simple_x86_encoder_init,
97 .props_size_get = &lzma_simple_props_size,
98 .props_encode = &lzma_simple_props_encode,
101 #ifdef HAVE_ENCODER_POWERPC
103 .id = LZMA_FILTER_POWERPC,
104 .init = &lzma_simple_powerpc_encoder_init,
107 .props_size_get = &lzma_simple_props_size,
108 .props_encode = &lzma_simple_props_encode,
111 #ifdef HAVE_ENCODER_IA64
113 .id = LZMA_FILTER_IA64,
114 .init = &lzma_simple_ia64_encoder_init,
117 .props_size_get = &lzma_simple_props_size,
118 .props_encode = &lzma_simple_props_encode,
121 #ifdef HAVE_ENCODER_ARM
123 .id = LZMA_FILTER_ARM,
124 .init = &lzma_simple_arm_encoder_init,
127 .props_size_get = &lzma_simple_props_size,
128 .props_encode = &lzma_simple_props_encode,
131 #ifdef HAVE_ENCODER_ARMTHUMB
133 .id = LZMA_FILTER_ARMTHUMB,
134 .init = &lzma_simple_armthumb_encoder_init,
137 .props_size_get = &lzma_simple_props_size,
138 .props_encode = &lzma_simple_props_encode,
141 #ifdef HAVE_ENCODER_SPARC
143 .id = LZMA_FILTER_SPARC,
144 .init = &lzma_simple_sparc_encoder_init,
147 .props_size_get = &lzma_simple_props_size,
148 .props_encode = &lzma_simple_props_encode,
151 #ifdef HAVE_ENCODER_DELTA
153 .id = LZMA_FILTER_DELTA,
154 .init = &lzma_delta_encoder_init,
155 .memusage = &lzma_delta_coder_memusage,
157 .props_size_get = NULL,
158 .props_size_fixed = 1,
159 .props_encode = &lzma_delta_props_encode,
165 static const lzma_filter_encoder *
166 encoder_find(lzma_vli id)
168 for (size_t i = 0; i < ARRAY_SIZE(encoders); ++i)
169 if (encoders[i].id == id)
176 extern LZMA_API(lzma_bool)
177 lzma_filter_encoder_is_supported(lzma_vli id)
179 return encoder_find(id) != NULL;
183 extern LZMA_API(lzma_ret)
184 lzma_filters_update(lzma_stream *strm, const lzma_filter *filters)
186 if (strm->internal->next.update == NULL)
187 return LZMA_PROG_ERROR;
189 // Validate the filter chain.
190 if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
191 return LZMA_OPTIONS_ERROR;
193 // The actual filter chain in the encoder is reversed. Some things
194 // still want the normal order chain, so we provide both.
196 while (filters[count].id != LZMA_VLI_UNKNOWN)
199 lzma_filter reversed_filters[LZMA_FILTERS_MAX + 1];
200 for (size_t i = 0; i < count; ++i)
201 reversed_filters[count - i - 1] = filters[i];
203 reversed_filters[count].id = LZMA_VLI_UNKNOWN;
205 return strm->internal->next.update(strm->internal->next.coder,
206 strm->allocator, filters, reversed_filters);
211 lzma_raw_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
212 const lzma_filter *options)
214 return lzma_raw_coder_init(next, allocator,
215 options, (lzma_filter_find)(&encoder_find), true);
219 extern LZMA_API(lzma_ret)
220 lzma_raw_encoder(lzma_stream *strm, const lzma_filter *options)
222 lzma_next_strm_init(lzma_raw_coder_init, strm, options,
223 (lzma_filter_find)(&encoder_find), true);
225 strm->internal->supported_actions[LZMA_RUN] = true;
226 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
227 strm->internal->supported_actions[LZMA_FINISH] = true;
233 extern LZMA_API(uint64_t)
234 lzma_raw_encoder_memusage(const lzma_filter *filters)
236 return lzma_raw_coder_memusage(
237 (lzma_filter_find)(&encoder_find), filters);
241 extern LZMA_API(lzma_vli)
242 lzma_chunk_size(const lzma_filter *filters)
246 for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
247 const lzma_filter_encoder *const fe
248 = encoder_find(filters[i].id);
249 if (fe->chunk_size != NULL) {
251 = fe->chunk_size(filters[i].options);
252 if (size == LZMA_VLI_UNKNOWN)
253 return LZMA_VLI_UNKNOWN;
264 extern LZMA_API(lzma_ret)
265 lzma_properties_size(uint32_t *size, const lzma_filter *filter)
267 const lzma_filter_encoder *const fe = encoder_find(filter->id);
269 // Unknown filter - if the Filter ID is a proper VLI,
270 // return LZMA_OPTIONS_ERROR instead of LZMA_PROG_ERROR,
271 // because it's possible that we just don't have support
272 // compiled in for the requested filter.
273 return filter->id <= LZMA_VLI_MAX
274 ? LZMA_OPTIONS_ERROR : LZMA_PROG_ERROR;
277 if (fe->props_size_get == NULL) {
278 // No props_size_get() function, use props_size_fixed.
279 *size = fe->props_size_fixed;
283 return fe->props_size_get(size, filter->options);
287 extern LZMA_API(lzma_ret)
288 lzma_properties_encode(const lzma_filter *filter, uint8_t *props)
290 const lzma_filter_encoder *const fe = encoder_find(filter->id);
292 return LZMA_PROG_ERROR;
294 if (fe->props_encode == NULL)
297 return fe->props_encode(filter->options, props);