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