liblzma: Add RISC-V BCJ filter.
[xz.git] / src / liblzma / common / filter_encoder.c
blob45a215aaa8706b869b5c64289521a4d6d57831a6
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file filter_decoder.c
4 /// \brief Filter ID mapping to filter-specific functions
5 //
6 // Author: Lasse Collin
7 //
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 "simple_encoder.h"
18 #include "delta_encoder.h"
21 typedef struct {
22 /// Filter ID
23 lzma_vli id;
25 /// Initializes the filter encoder and calls lzma_next_filter_init()
26 /// for filters + 1.
27 lzma_init_function init;
29 /// Calculates memory usage of the encoder. If the options are
30 /// invalid, UINT64_MAX is returned.
31 uint64_t (*memusage)(const void *options);
33 /// Calculates the recommended Uncompressed Size for .xz Blocks to
34 /// which the input data can be split to make multithreaded
35 /// encoding possible. If this is NULL, it is assumed that
36 /// the encoder is fast enough with single thread. If the options
37 /// are invalid, UINT64_MAX is returned.
38 uint64_t (*block_size)(const void *options);
40 /// Tells the size of the Filter Properties field. If options are
41 /// invalid, LZMA_OPTIONS_ERROR is returned and size is set to
42 /// UINT32_MAX.
43 lzma_ret (*props_size_get)(uint32_t *size, const void *options);
45 /// Some filters will always have the same size Filter Properties
46 /// field. If props_size_get is NULL, this value is used.
47 uint32_t props_size_fixed;
49 /// Encodes Filter Properties.
50 ///
51 /// \return - LZMA_OK: Properties encoded successfully.
52 /// - LZMA_OPTIONS_ERROR: Unsupported options
53 /// - LZMA_PROG_ERROR: Invalid options or not enough
54 /// output space
55 lzma_ret (*props_encode)(const void *options, uint8_t *out);
57 } lzma_filter_encoder;
60 static const lzma_filter_encoder encoders[] = {
61 #ifdef HAVE_ENCODER_LZMA1
63 .id = LZMA_FILTER_LZMA1,
64 .init = &lzma_lzma_encoder_init,
65 .memusage = &lzma_lzma_encoder_memusage,
66 .block_size = NULL, // Not needed for LZMA1
67 .props_size_get = NULL,
68 .props_size_fixed = 5,
69 .props_encode = &lzma_lzma_props_encode,
72 .id = LZMA_FILTER_LZMA1EXT,
73 .init = &lzma_lzma_encoder_init,
74 .memusage = &lzma_lzma_encoder_memusage,
75 .block_size = NULL, // Not needed for LZMA1
76 .props_size_get = NULL,
77 .props_size_fixed = 5,
78 .props_encode = &lzma_lzma_props_encode,
80 #endif
81 #ifdef HAVE_ENCODER_LZMA2
83 .id = LZMA_FILTER_LZMA2,
84 .init = &lzma_lzma2_encoder_init,
85 .memusage = &lzma_lzma2_encoder_memusage,
86 .block_size = &lzma_lzma2_block_size,
87 .props_size_get = NULL,
88 .props_size_fixed = 1,
89 .props_encode = &lzma_lzma2_props_encode,
91 #endif
92 #ifdef HAVE_ENCODER_X86
94 .id = LZMA_FILTER_X86,
95 .init = &lzma_simple_x86_encoder_init,
96 .memusage = NULL,
97 .block_size = NULL,
98 .props_size_get = &lzma_simple_props_size,
99 .props_encode = &lzma_simple_props_encode,
101 #endif
102 #ifdef HAVE_ENCODER_POWERPC
104 .id = LZMA_FILTER_POWERPC,
105 .init = &lzma_simple_powerpc_encoder_init,
106 .memusage = NULL,
107 .block_size = NULL,
108 .props_size_get = &lzma_simple_props_size,
109 .props_encode = &lzma_simple_props_encode,
111 #endif
112 #ifdef HAVE_ENCODER_IA64
114 .id = LZMA_FILTER_IA64,
115 .init = &lzma_simple_ia64_encoder_init,
116 .memusage = NULL,
117 .block_size = NULL,
118 .props_size_get = &lzma_simple_props_size,
119 .props_encode = &lzma_simple_props_encode,
121 #endif
122 #ifdef HAVE_ENCODER_ARM
124 .id = LZMA_FILTER_ARM,
125 .init = &lzma_simple_arm_encoder_init,
126 .memusage = NULL,
127 .block_size = NULL,
128 .props_size_get = &lzma_simple_props_size,
129 .props_encode = &lzma_simple_props_encode,
131 #endif
132 #ifdef HAVE_ENCODER_ARMTHUMB
134 .id = LZMA_FILTER_ARMTHUMB,
135 .init = &lzma_simple_armthumb_encoder_init,
136 .memusage = NULL,
137 .block_size = NULL,
138 .props_size_get = &lzma_simple_props_size,
139 .props_encode = &lzma_simple_props_encode,
141 #endif
142 #ifdef HAVE_ENCODER_ARM64
144 .id = LZMA_FILTER_ARM64,
145 .init = &lzma_simple_arm64_encoder_init,
146 .memusage = NULL,
147 .block_size = NULL,
148 .props_size_get = &lzma_simple_props_size,
149 .props_encode = &lzma_simple_props_encode,
151 #endif
152 #ifdef HAVE_ENCODER_SPARC
154 .id = LZMA_FILTER_SPARC,
155 .init = &lzma_simple_sparc_encoder_init,
156 .memusage = NULL,
157 .block_size = NULL,
158 .props_size_get = &lzma_simple_props_size,
159 .props_encode = &lzma_simple_props_encode,
161 #endif
162 #ifdef HAVE_ENCODER_RISCV
164 .id = LZMA_FILTER_RISCV,
165 .init = &lzma_simple_riscv_encoder_init,
166 .memusage = NULL,
167 .block_size = NULL,
168 .props_size_get = &lzma_simple_props_size,
169 .props_encode = &lzma_simple_props_encode,
171 #endif
172 #ifdef HAVE_ENCODER_DELTA
174 .id = LZMA_FILTER_DELTA,
175 .init = &lzma_delta_encoder_init,
176 .memusage = &lzma_delta_coder_memusage,
177 .block_size = NULL,
178 .props_size_get = NULL,
179 .props_size_fixed = 1,
180 .props_encode = &lzma_delta_props_encode,
182 #endif
186 static const lzma_filter_encoder *
187 encoder_find(lzma_vli id)
189 for (size_t i = 0; i < ARRAY_SIZE(encoders); ++i)
190 if (encoders[i].id == id)
191 return encoders + i;
193 return NULL;
197 extern LZMA_API(lzma_bool)
198 lzma_filter_encoder_is_supported(lzma_vli id)
200 return encoder_find(id) != NULL;
204 extern LZMA_API(lzma_ret)
205 lzma_filters_update(lzma_stream *strm, const lzma_filter *filters)
207 if (strm->internal->next.update == NULL)
208 return LZMA_PROG_ERROR;
210 // Validate the filter chain.
211 if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
212 return LZMA_OPTIONS_ERROR;
214 // The actual filter chain in the encoder is reversed. Some things
215 // still want the normal order chain, so we provide both.
216 size_t count = 1;
217 while (filters[count].id != LZMA_VLI_UNKNOWN)
218 ++count;
220 lzma_filter reversed_filters[LZMA_FILTERS_MAX + 1];
221 for (size_t i = 0; i < count; ++i)
222 reversed_filters[count - i - 1] = filters[i];
224 reversed_filters[count].id = LZMA_VLI_UNKNOWN;
226 return strm->internal->next.update(strm->internal->next.coder,
227 strm->allocator, filters, reversed_filters);
231 extern lzma_ret
232 lzma_raw_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
233 const lzma_filter *filters)
235 return lzma_raw_coder_init(next, allocator,
236 filters, (lzma_filter_find)(&encoder_find), true);
240 extern LZMA_API(lzma_ret)
241 lzma_raw_encoder(lzma_stream *strm, const lzma_filter *filters)
243 lzma_next_strm_init(lzma_raw_coder_init, strm, filters,
244 (lzma_filter_find)(&encoder_find), true);
246 strm->internal->supported_actions[LZMA_RUN] = true;
247 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
248 strm->internal->supported_actions[LZMA_FINISH] = true;
250 return LZMA_OK;
254 extern LZMA_API(uint64_t)
255 lzma_raw_encoder_memusage(const lzma_filter *filters)
257 return lzma_raw_coder_memusage(
258 (lzma_filter_find)(&encoder_find), filters);
262 extern LZMA_API(uint64_t)
263 lzma_mt_block_size(const lzma_filter *filters)
265 if (filters == NULL)
266 return UINT64_MAX;
268 uint64_t max = 0;
270 for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
271 const lzma_filter_encoder *const fe
272 = encoder_find(filters[i].id);
273 if (fe == NULL)
274 return UINT64_MAX;
276 if (fe->block_size != NULL) {
277 const uint64_t size
278 = fe->block_size(filters[i].options);
279 if (size > max)
280 max = size;
284 return max == 0 ? UINT64_MAX : max;
288 extern LZMA_API(lzma_ret)
289 lzma_properties_size(uint32_t *size, const lzma_filter *filter)
291 const lzma_filter_encoder *const fe = encoder_find(filter->id);
292 if (fe == NULL) {
293 // Unknown filter - if the Filter ID is a proper VLI,
294 // return LZMA_OPTIONS_ERROR instead of LZMA_PROG_ERROR,
295 // because it's possible that we just don't have support
296 // compiled in for the requested filter.
297 return filter->id <= LZMA_VLI_MAX
298 ? LZMA_OPTIONS_ERROR : LZMA_PROG_ERROR;
301 if (fe->props_size_get == NULL) {
302 // No props_size_get() function, use props_size_fixed.
303 *size = fe->props_size_fixed;
304 return LZMA_OK;
307 return fe->props_size_get(size, filter->options);
311 extern LZMA_API(lzma_ret)
312 lzma_properties_encode(const lzma_filter *filter, uint8_t *props)
314 const lzma_filter_encoder *const fe = encoder_find(filter->id);
315 if (fe == NULL)
316 return LZMA_PROG_ERROR;
318 if (fe->props_encode == NULL)
319 return LZMA_OK;
321 return fe->props_encode(filter->options, props);