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 splitted 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 sucessfully.
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;
184 lzma_raw_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
185 const lzma_filter *options)
187 return lzma_raw_coder_init(next, allocator,
188 options, (lzma_filter_find)(&encoder_find), true);
192 extern LZMA_API(lzma_ret)
193 lzma_raw_encoder(lzma_stream *strm, const lzma_filter *options)
195 lzma_next_strm_init(lzma_raw_coder_init, strm, options,
196 (lzma_filter_find)(&encoder_find), true);
198 strm->internal->supported_actions[LZMA_RUN] = true;
199 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
200 strm->internal->supported_actions[LZMA_FINISH] = true;
206 extern LZMA_API(uint64_t)
207 lzma_raw_encoder_memusage(const lzma_filter *filters)
209 return lzma_raw_coder_memusage(
210 (lzma_filter_find)(&encoder_find), filters);
214 extern LZMA_API(lzma_vli)
215 lzma_chunk_size(const lzma_filter *filters)
219 for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
220 const lzma_filter_encoder *const fe
221 = encoder_find(filters[i].id);
222 if (fe->chunk_size != NULL) {
224 = fe->chunk_size(filters[i].options);
225 if (size == LZMA_VLI_UNKNOWN)
226 return LZMA_VLI_UNKNOWN;
237 extern LZMA_API(lzma_ret)
238 lzma_properties_size(uint32_t *size, const lzma_filter *filter)
240 const lzma_filter_encoder *const fe = encoder_find(filter->id);
242 // Unknown filter - if the Filter ID is a proper VLI,
243 // return LZMA_OPTIONS_ERROR instead of LZMA_PROG_ERROR,
244 // because it's possible that we just don't have support
245 // compiled in for the requested filter.
246 return filter->id <= LZMA_VLI_MAX
247 ? LZMA_OPTIONS_ERROR : LZMA_PROG_ERROR;
250 if (fe->props_size_get == NULL) {
251 // No props_size_get() function, use props_size_fixed.
252 *size = fe->props_size_fixed;
256 return fe->props_size_get(size, filter->options);
260 extern LZMA_API(lzma_ret)
261 lzma_properties_encode(const lzma_filter *filter, uint8_t *props)
263 const lzma_filter_encoder *const fe = encoder_find(filter->id);
265 return LZMA_PROG_ERROR;
267 if (fe->props_encode == NULL)
270 return fe->props_encode(filter->options, props);