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