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