1 | /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | |
3 | Distributed under MIT license. |
4 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 | */ |
6 | |
7 | /** |
8 | * @file |
9 | * API for Brotli compression. |
10 | */ |
11 | |
12 | #ifndef BROTLI_ENC_ENCODE_H_ |
13 | #define BROTLI_ENC_ENCODE_H_ |
14 | |
15 | #include <brotli/port.h> |
16 | #include <brotli/types.h> |
17 | |
18 | #if defined(__cplusplus) || defined(c_plusplus) |
19 | extern "C" { |
20 | #endif |
21 | |
22 | /** Minimal value for ::BROTLI_PARAM_LGWIN parameter. */ |
23 | #define BROTLI_MIN_WINDOW_BITS 10 |
24 | /** |
25 | * Maximal value for ::BROTLI_PARAM_LGWIN parameter. |
26 | * |
27 | * @note equal to @c BROTLI_MAX_DISTANCE_BITS constant. |
28 | */ |
29 | #define BROTLI_MAX_WINDOW_BITS 24 |
30 | /** Minimal value for ::BROTLI_PARAM_LGBLOCK parameter. */ |
31 | #define BROTLI_MIN_INPUT_BLOCK_BITS 16 |
32 | /** Maximal value for ::BROTLI_PARAM_LGBLOCK parameter. */ |
33 | #define BROTLI_MAX_INPUT_BLOCK_BITS 24 |
34 | /** Minimal value for ::BROTLI_PARAM_QUALITY parameter. */ |
35 | #define BROTLI_MIN_QUALITY 0 |
36 | /** Maximal value for ::BROTLI_PARAM_QUALITY parameter. */ |
37 | #define BROTLI_MAX_QUALITY 11 |
38 | |
39 | BROTLI_DEPRECATED static const int kBrotliMinWindowBits = |
40 | BROTLI_MIN_WINDOW_BITS; |
41 | BROTLI_DEPRECATED static const int kBrotliMaxWindowBits = |
42 | BROTLI_MAX_WINDOW_BITS; |
43 | |
44 | /** Options for ::BROTLI_PARAM_MODE parameter. */ |
45 | typedef enum BrotliEncoderMode { |
46 | /** |
47 | * Default compression mode. |
48 | * |
49 | * In this mode compressor does not know anything in advance about the |
50 | * properties of the input. |
51 | */ |
52 | BROTLI_MODE_GENERIC = 0, |
53 | /** Compression mode for UTF-8 formatted text input. */ |
54 | BROTLI_MODE_TEXT = 1, |
55 | /** Compression mode used in WOFF 2.0. */ |
56 | BROTLI_MODE_FONT = 2 |
57 | } BrotliEncoderMode; |
58 | |
59 | /** Default value for ::BROTLI_PARAM_QUALITY parameter. */ |
60 | #define BROTLI_DEFAULT_QUALITY 11 |
61 | /** Default value for ::BROTLI_PARAM_LGWIN parameter. */ |
62 | #define BROTLI_DEFAULT_WINDOW 22 |
63 | /** Default value for ::BROTLI_PARAM_MODE parameter. */ |
64 | #define BROTLI_DEFAULT_MODE BROTLI_MODE_GENERIC |
65 | |
66 | /** Operations that can be performed by streaming encoder. */ |
67 | typedef enum BrotliEncoderOperation { |
68 | /** |
69 | * Process input. |
70 | * |
71 | * Encoder may postpone producing output, until it has processed enough input. |
72 | */ |
73 | BROTLI_OPERATION_PROCESS = 0, |
74 | /** |
75 | * Produce output for all processed input. |
76 | * |
77 | * Actual flush is performed when input stream is depleted and there is enough |
78 | * space in output stream. This means that client should repeat |
79 | * ::BROTLI_OPERATION_FLUSH operation until @p available_in becomes @c 0, and |
80 | * ::BrotliEncoderHasMoreOutput returns ::BROTLI_FALSE. |
81 | * |
82 | * @warning Until flush is complete, client @b SHOULD @b NOT swap, |
83 | * reduce or extend input stream. |
84 | * |
85 | * When flush is complete, output data will be sufficient for decoder to |
86 | * reproduce all the given input. |
87 | */ |
88 | BROTLI_OPERATION_FLUSH = 1, |
89 | /** |
90 | * Finalize the stream. |
91 | * |
92 | * Actual finalization is performed when input stream is depleted and there is |
93 | * enough space in output stream. This means that client should repeat |
94 | * ::BROTLI_OPERATION_FLUSH operation until @p available_in becomes @c 0, and |
95 | * ::BrotliEncoderHasMoreOutput returns ::BROTLI_FALSE. |
96 | * |
97 | * @warning Until finalization is complete, client @b SHOULD @b NOT swap, |
98 | * reduce or extend input stream. |
99 | * |
100 | * Helper function ::BrotliEncoderIsFinished checks if stream is finalized and |
101 | * output fully dumped. |
102 | * |
103 | * Adding more input data to finalized stream is impossible. |
104 | */ |
105 | BROTLI_OPERATION_FINISH = 2, |
106 | /** |
107 | * Emit metadata block to stream. |
108 | * |
109 | * Metadata is opaque to Brotli: neither encoder, nor decoder processes this |
110 | * data or relies on it. It may be used to pass some extra information from |
111 | * encoder client to decoder client without interfering with main data stream. |
112 | * |
113 | * @note Encoder may emit empty metadata blocks internally, to pad encoded |
114 | * stream to byte boundary. |
115 | * |
116 | * @warning Until emitting metadata is complete client @b SHOULD @b NOT swap, |
117 | * reduce or extend input stream. |
118 | * |
119 | * @warning The whole content of input buffer is considered to be the content |
120 | * of metadata block. Do @b NOT @e append metadata to input stream, |
121 | * before it is depleted with other operations. |
122 | * |
123 | * Stream is soft-flushed before metadata block is emitted. Metadata block |
124 | * @b MUST be no longer than than 16MiB. |
125 | */ |
126 | BROTLI_OPERATION_EMIT_METADATA = 3 |
127 | } BrotliEncoderOperation; |
128 | |
129 | /** Options to be used with ::BrotliEncoderSetParameter. */ |
130 | typedef enum BrotliEncoderParameter { |
131 | /** |
132 | * Tune encoder for specific input. |
133 | * |
134 | * ::BrotliEncoderMode enumerates all available values. |
135 | */ |
136 | BROTLI_PARAM_MODE = 0, |
137 | /** |
138 | * The main compression speed-density lever. |
139 | * |
140 | * The higher the quality, the slower the compression. Range is |
141 | * from ::BROTLI_MIN_QUALITY to ::BROTLI_MAX_QUALITY. |
142 | */ |
143 | BROTLI_PARAM_QUALITY = 1, |
144 | /** |
145 | * Recommended sliding LZ77 window size. |
146 | * |
147 | * Encoder may reduce this value, e.g. if input is much smaller than |
148 | * window size. |
149 | * |
150 | * Window size is `(1 << value) - 16`. |
151 | * |
152 | * Range is from ::BROTLI_MIN_WINDOW_BITS to ::BROTLI_MAX_WINDOW_BITS. |
153 | */ |
154 | BROTLI_PARAM_LGWIN = 2, |
155 | /** |
156 | * Recommended input block size. |
157 | * |
158 | * Encoder may reduce this value, e.g. if input is much smaller than input |
159 | * block size. |
160 | * |
161 | * Range is from ::BROTLI_MIN_INPUT_BLOCK_BITS to |
162 | * ::BROTLI_MAX_INPUT_BLOCK_BITS. |
163 | * |
164 | * @note Bigger input block size allows better compression, but consumes more |
165 | * memory. \n The rough formula of memory used for temporary input |
166 | * storage is `3 << lgBlock`. |
167 | */ |
168 | BROTLI_PARAM_LGBLOCK = 3, |
169 | /** |
170 | * Flag that affects usage of "literal context modeling" format feature. |
171 | * |
172 | * This flag is a "decoding-speed vs compression ratio" trade-off. |
173 | */ |
174 | BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING = 4, |
175 | /** |
176 | * Estimated total input size for all ::BrotliEncoderCompressStream calls. |
177 | * |
178 | * The default value is 0, which means that the total input size is unknown. |
179 | */ |
180 | BROTLI_PARAM_SIZE_HINT = 5 |
181 | } BrotliEncoderParameter; |
182 | |
183 | /** |
184 | * Opaque structure that holds encoder state. |
185 | * |
186 | * Allocated and initialized with ::BrotliEncoderCreateInstance. |
187 | * Cleaned up and deallocated with ::BrotliEncoderDestroyInstance. |
188 | */ |
189 | typedef struct BrotliEncoderStateStruct BrotliEncoderState; |
190 | |
191 | /** |
192 | * Sets the specified parameter to the given encoder instance. |
193 | * |
194 | * @param state encoder instance |
195 | * @param param parameter to set |
196 | * @param value new parameter value |
197 | * @returns ::BROTLI_FALSE if parameter is unrecognized, or value is invalid |
198 | * @returns ::BROTLI_FALSE if value of parameter can not be changed at current |
199 | * encoder state (e.g. when encoding is started, window size might be |
200 | * already encoded and therefore it is impossible to change it) |
201 | * @returns ::BROTLI_TRUE if value is accepted |
202 | * @warning invalid values might be accepted in case they would not break |
203 | * encoding process. |
204 | */ |
205 | BROTLI_ENC_API BROTLI_BOOL BrotliEncoderSetParameter( |
206 | BrotliEncoderState* state, BrotliEncoderParameter param, uint32_t value); |
207 | |
208 | /** |
209 | * Creates an instance of ::BrotliEncoderState and initializes it. |
210 | * |
211 | * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the |
212 | * case they are both zero, default memory allocators are used. @p opaque is |
213 | * passed to @p alloc_func and @p free_func when they are called. |
214 | * |
215 | * @param alloc_func custom memory allocation function |
216 | * @param free_func custom memory fee function |
217 | * @param opaque custom memory manager handle |
218 | * @returns @c 0 if instance can not be allocated or initialized |
219 | * @returns pointer to initialized ::BrotliEncoderState otherwise |
220 | */ |
221 | BROTLI_ENC_API BrotliEncoderState* BrotliEncoderCreateInstance( |
222 | brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque); |
223 | |
224 | /** |
225 | * Deinitializes and frees ::BrotliEncoderState instance. |
226 | * |
227 | * @param state decoder instance to be cleaned up and deallocated |
228 | */ |
229 | BROTLI_ENC_API void BrotliEncoderDestroyInstance(BrotliEncoderState* state); |
230 | |
231 | /* Calculates maximum input size that can be processed at once. */ |
232 | BROTLI_DEPRECATED BROTLI_ENC_API size_t BrotliEncoderInputBlockSize( |
233 | BrotliEncoderState* state); |
234 | |
235 | /* Copies the given input data to the internal ring buffer. */ |
236 | BROTLI_DEPRECATED BROTLI_ENC_API void BrotliEncoderCopyInputToRingBuffer( |
237 | BrotliEncoderState* state, const size_t input_size, |
238 | const uint8_t* input_buffer); |
239 | |
240 | /* Processes the accumulated input. */ |
241 | BROTLI_DEPRECATED BROTLI_ENC_API BROTLI_BOOL BrotliEncoderWriteData( |
242 | BrotliEncoderState* state, const BROTLI_BOOL is_last, |
243 | const BROTLI_BOOL force_flush, size_t* out_size, uint8_t** output); |
244 | |
245 | /** |
246 | * Prepends imaginary LZ77 dictionary. |
247 | * |
248 | * Fills the fresh ::BrotliEncoderState with additional data corpus for LZ77 |
249 | * backward references. |
250 | * |
251 | * @note Not to be confused with the static dictionary (see RFC7932 section 8). |
252 | * |
253 | * Workflow: |
254 | * -# Allocate and initialize state with ::BrotliEncoderCreateInstance |
255 | * -# Set ::BROTLI_PARAM_LGWIN parameter |
256 | * -# Invoke ::BrotliEncoderSetCustomDictionary |
257 | * -# Use ::BrotliEncoderCompressStream |
258 | * -# Clean up and free state with ::BrotliEncoderDestroyInstance |
259 | * |
260 | * @param state encoder instance |
261 | * @param size length of @p dict; at most "window size" bytes are used |
262 | * @param dict "dictionary"; @b MUST use same dictionary during decompression |
263 | */ |
264 | BROTLI_ENC_API void BrotliEncoderSetCustomDictionary( |
265 | BrotliEncoderState* state, size_t size, |
266 | const uint8_t dict[BROTLI_ARRAY_PARAM(size)]); |
267 | |
268 | /** |
269 | * Calculates the output size bound for the given @p input_size. |
270 | * |
271 | * @warning Result is not applicable to ::BrotliEncoderCompressStream output, |
272 | * because every "flush" adds extra overhead bytes, and some encoder |
273 | * settings (e.g. quality @c 0 and @c 1) might imply a "soft flush" |
274 | * after every chunk of input. |
275 | * |
276 | * @param input_size size of projected input |
277 | * @returns @c 0 if result does not fit @c size_t |
278 | */ |
279 | BROTLI_ENC_API size_t BrotliEncoderMaxCompressedSize(size_t input_size); |
280 | |
281 | /** |
282 | * Performs one-shot memory-to-memory compression. |
283 | * |
284 | * Compresses the data in @p input_buffer into @p encoded_buffer, and sets |
285 | * @p *encoded_size to the compressed length. |
286 | * |
287 | * @note If ::BrotliEncoderMaxCompressedSize(@p input_size) returns non-zero |
288 | * value, then output is guaranteed to be no longer than that. |
289 | * |
290 | * @param quality quality parameter value, e.g. ::BROTLI_DEFAULT_QUALITY |
291 | * @param lgwin lgwin parameter value, e.g. ::BROTLI_DEFAULT_WINDOW |
292 | * @param mode mode parameter value, e.g. ::BROTLI_DEFAULT_MODE |
293 | * @param input_size size of @p input_buffer |
294 | * @param input_buffer input data buffer with at least @p input_size |
295 | * addressable bytes |
296 | * @param[in, out] encoded_size @b in: size of @p encoded_buffer; \n |
297 | * @b out: length of compressed data written to |
298 | * @p encoded_buffer, or @c 0 if compression fails |
299 | * @param encoded_buffer compressed data destination buffer |
300 | * @returns ::BROTLI_FALSE in case of compression error |
301 | * @returns ::BROTLI_FALSE if output buffer is too small |
302 | * @returns ::BROTLI_TRUE otherwise |
303 | */ |
304 | BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompress( |
305 | int quality, int lgwin, BrotliEncoderMode mode, size_t input_size, |
306 | const uint8_t input_buffer[BROTLI_ARRAY_PARAM(input_size)], |
307 | size_t* encoded_size, |
308 | uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(*encoded_size)]); |
309 | |
310 | /** |
311 | * Compresses input stream to output stream. |
312 | * |
313 | * The values @p *available_in and @p *available_out must specify the number of |
314 | * bytes addressable at @p *next_in and @p *next_out respectively. |
315 | * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL. |
316 | * |
317 | * After each call, @p *available_in will be decremented by the amount of input |
318 | * bytes consumed, and the @p *next_in pointer will be incremented by that |
319 | * amount. Similarly, @p *available_out will be decremented by the amount of |
320 | * output bytes written, and the @p *next_out pointer will be incremented by |
321 | * that amount. |
322 | * |
323 | * @p total_out, if it is not a null-pointer, will be set to the number |
324 | * of bytes decompressed since the last @p state initialization. |
325 | * |
326 | * |
327 | * |
328 | * Internally workflow consists of 3 tasks: |
329 | * -# (optionally) copy input data to internal buffer |
330 | * -# actually compress data and (optionally) store it to internal buffer |
331 | * -# (optionally) copy compressed bytes from internal buffer to output stream |
332 | * |
333 | * Whenever all 3 tasks can't move forward anymore, or error occurs, this |
334 | * method returns the control flow to caller. |
335 | * |
336 | * @p op is used to perform flush, finish the stream, or inject metadata block. |
337 | * See ::BrotliEncoderOperation for more information. |
338 | * |
339 | * Flushing the stream means forcing encoding of all input passed to encoder and |
340 | * completing the current output block, so it could be fully decoded by stream |
341 | * decoder. To perform flush set @p op to ::BROTLI_OPERATION_FLUSH. |
342 | * Under some circumstances (e.g. lack of output stream capacity) this operation |
343 | * would require several calls to ::BrotliEncoderCompressStream. The method must |
344 | * be called again until both input stream is depleted and encoder has no more |
345 | * output (see ::BrotliEncoderHasMoreOutput) after the method is called. |
346 | * |
347 | * Finishing the stream means encoding of all input passed to encoder and |
348 | * adding specific "final" marks, so stream decoder could determine that stream |
349 | * is complete. To perform finish set @p op to ::BROTLI_OPERATION_FINISH. |
350 | * Under some circumstances (e.g. lack of output stream capacity) this operation |
351 | * would require several calls to ::BrotliEncoderCompressStream. The method must |
352 | * be called again until both input stream is depleted and encoder has no more |
353 | * output (see ::BrotliEncoderHasMoreOutput) after the method is called. |
354 | * |
355 | * @warning When flushing and finishing, @p op should not change until operation |
356 | * is complete; input stream should not be swapped, reduced or |
357 | * extended as well. |
358 | * |
359 | * @param state encoder instance |
360 | * @param op requested operation |
361 | * @param[in, out] available_in @b in: amount of available input; \n |
362 | * @b out: amount of unused input |
363 | * @param[in, out] next_in pointer to the next input byte |
364 | * @param[in, out] available_out @b in: length of output buffer; \n |
365 | * @b out: remaining size of output buffer |
366 | * @param[in, out] next_out compressed output buffer cursor; |
367 | * can be @c NULL if @p available_out is @c 0 |
368 | * @param[out] total_out number of bytes produced so far; can be @c NULL |
369 | * @returns ::BROTLI_FALSE if there was an error |
370 | * @returns ::BROTLI_TRUE otherwise |
371 | */ |
372 | BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompressStream( |
373 | BrotliEncoderState* state, BrotliEncoderOperation op, size_t* available_in, |
374 | const uint8_t** next_in, size_t* available_out, uint8_t** next_out, |
375 | size_t* total_out); |
376 | |
377 | /** |
378 | * Checks if encoder instance reached the final state. |
379 | * |
380 | * @param state encoder instance |
381 | * @returns ::BROTLI_TRUE if encoder is in a state where it reached the end of |
382 | * the input and produced all of the output |
383 | * @returns ::BROTLI_FALSE otherwise |
384 | */ |
385 | BROTLI_ENC_API BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* state); |
386 | |
387 | /** |
388 | * Checks if encoder has more output. |
389 | * |
390 | * @param state encoder instance |
391 | * @returns ::BROTLI_TRUE, if encoder has some unconsumed output |
392 | * @returns ::BROTLI_FALSE otherwise |
393 | */ |
394 | BROTLI_ENC_API BROTLI_BOOL BrotliEncoderHasMoreOutput( |
395 | BrotliEncoderState* state); |
396 | |
397 | /** |
398 | * Acquires pointer to internal output buffer. |
399 | * |
400 | * This method is used to make language bindings easier and more efficient: |
401 | * -# push data to ::BrotliEncoderCompressStream, |
402 | * until ::BrotliEncoderHasMoreOutput returns BROTL_TRUE |
403 | * -# use ::BrotliEncoderTakeOutput to peek bytes and copy to language-specific |
404 | * entity |
405 | * |
406 | * Also this could be useful if there is an output stream that is able to |
407 | * consume all the provided data (e.g. when data is saved to file system). |
408 | * |
409 | * @attention After every call to ::BrotliEncoderTakeOutput @p *size bytes of |
410 | * output are considered consumed for all consecutive calls to the |
411 | * instance methods; returned pointer becomes invalidated as well. |
412 | * |
413 | * @note Encoder output is not guaranteed to be contiguous. This means that |
414 | * after the size-unrestricted call to ::BrotliEncoderTakeOutput, |
415 | * immediate next call to ::BrotliEncoderTakeOutput may return more data. |
416 | * |
417 | * @param state encoder instance |
418 | * @param[in, out] size @b in: number of bytes caller is ready to take, @c 0 if |
419 | * any amount could be handled; \n |
420 | * @b out: amount of data pointed by returned pointer and |
421 | * considered consumed; \n |
422 | * out value is never greater than in value, unless it is @c 0 |
423 | * @returns pointer to output data |
424 | */ |
425 | BROTLI_ENC_API const uint8_t* BrotliEncoderTakeOutput( |
426 | BrotliEncoderState* state, size_t* size); |
427 | |
428 | |
429 | /** |
430 | * Gets an encoder library version. |
431 | * |
432 | * Look at BROTLI_VERSION for more information. |
433 | */ |
434 | BROTLI_ENC_API uint32_t BrotliEncoderVersion(void); |
435 | |
436 | #if defined(__cplusplus) || defined(c_plusplus) |
437 | } /* extern "C" */ |
438 | #endif |
439 | |
440 | #endif /* BROTLI_ENC_ENCODE_H_ */ |
441 | |