• 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 } BrotliEncoderParameter;
206 
207 /**
208  * Opaque structure that holds encoder state.
209  *
210  * Allocated and initialized with ::BrotliEncoderCreateInstance.
211  * Cleaned up and deallocated with ::BrotliEncoderDestroyInstance.
212  */
213 typedef struct BrotliEncoderStateStruct BrotliEncoderState;
214 
215 /**
216  * Sets the specified parameter to the given encoder instance.
217  *
218  * @param state encoder instance
219  * @param param parameter to set
220  * @param value new parameter value
221  * @returns ::BROTLI_FALSE if parameter is unrecognized, or value is invalid
222  * @returns ::BROTLI_FALSE if value of parameter can not be changed at current
223  *          encoder state (e.g. when encoding is started, window size might be
224  *          already encoded and therefore it is impossible to change it)
225  * @returns ::BROTLI_TRUE if value is accepted
226  * @warning invalid values might be accepted in case they would not break
227  *          encoding process.
228  */
229 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderSetParameter(
230     BrotliEncoderState* state, BrotliEncoderParameter param, uint32_t value);
231 
232 /**
233  * Creates an instance of ::BrotliEncoderState and initializes it.
234  *
235  * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
236  * case they are both zero, default memory allocators are used. @p opaque is
237  * passed to @p alloc_func and @p free_func when they are called. @p free_func
238  * has to return without doing anything when asked to free a NULL pointer.
239  *
240  * @param alloc_func custom memory allocation function
241  * @param free_func custom memory free function
242  * @param opaque custom memory manager handle
243  * @returns @c 0 if instance can not be allocated or initialized
244  * @returns pointer to initialized ::BrotliEncoderState otherwise
245  */
246 BROTLI_ENC_API BrotliEncoderState* BrotliEncoderCreateInstance(
247     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
248 
249 /**
250  * Deinitializes and frees ::BrotliEncoderState instance.
251  *
252  * @param state decoder instance to be cleaned up and deallocated
253  */
254 BROTLI_ENC_API void BrotliEncoderDestroyInstance(BrotliEncoderState* state);
255 
256 /**
257  * Calculates the output size bound for the given @p input_size.
258  *
259  * @warning Result is only valid if quality is at least @c 2 and, in
260  *          case ::BrotliEncoderCompressStream was used, no flushes
261  *          (::BROTLI_OPERATION_FLUSH) were performed.
262  *
263  * @param input_size size of projected input
264  * @returns @c 0 if result does not fit @c size_t
265  */
266 BROTLI_ENC_API size_t BrotliEncoderMaxCompressedSize(size_t input_size);
267 
268 /**
269  * Performs one-shot memory-to-memory compression.
270  *
271  * Compresses the data in @p input_buffer into @p encoded_buffer, and sets
272  * @p *encoded_size to the compressed length.
273  *
274  * @note If ::BrotliEncoderMaxCompressedSize(@p input_size) returns non-zero
275  *       value, then output is guaranteed to be no longer than that.
276  *
277  * @param quality quality parameter value, e.g. ::BROTLI_DEFAULT_QUALITY
278  * @param lgwin lgwin parameter value, e.g. ::BROTLI_DEFAULT_WINDOW
279  * @param mode mode parameter value, e.g. ::BROTLI_DEFAULT_MODE
280  * @param input_size size of @p input_buffer
281  * @param input_buffer input data buffer with at least @p input_size
282  *        addressable bytes
283  * @param[in, out] encoded_size @b in: size of @p encoded_buffer; \n
284  *                 @b out: length of compressed data written to
285  *                 @p encoded_buffer, or @c 0 if compression fails
286  * @param encoded_buffer compressed data destination buffer
287  * @returns ::BROTLI_FALSE in case of compression error
288  * @returns ::BROTLI_FALSE if output buffer is too small
289  * @returns ::BROTLI_TRUE otherwise
290  */
291 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompress(
292     int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
293     const uint8_t input_buffer[BROTLI_ARRAY_PARAM(input_size)],
294     size_t* encoded_size,
295     uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(*encoded_size)]);
296 
297 /**
298  * Compresses input stream to output stream.
299  *
300  * The values @p *available_in and @p *available_out must specify the number of
301  * bytes addressable at @p *next_in and @p *next_out respectively.
302  * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL.
303  *
304  * After each call, @p *available_in will be decremented by the amount of input
305  * bytes consumed, and the @p *next_in pointer will be incremented by that
306  * amount. Similarly, @p *available_out will be decremented by the amount of
307  * output bytes written, and the @p *next_out pointer will be incremented by
308  * that amount.
309  *
310  * @p total_out, if it is not a null-pointer, will be set to the number
311  * of bytes compressed since the last @p state initialization.
312  *
313  *
314  *
315  * Internally workflow consists of 3 tasks:
316  *  -# (optionally) copy input data to internal buffer
317  *  -# actually compress data and (optionally) store it to internal buffer
318  *  -# (optionally) copy compressed bytes from internal buffer to output stream
319  *
320  * Whenever all 3 tasks can't move forward anymore, or error occurs, this
321  * method returns the control flow to caller.
322  *
323  * @p op is used to perform flush, finish the stream, or inject metadata block.
324  * See ::BrotliEncoderOperation for more information.
325  *
326  * Flushing the stream means forcing encoding of all input passed to encoder and
327  * completing the current output block, so it could be fully decoded by stream
328  * decoder. To perform flush set @p op to ::BROTLI_OPERATION_FLUSH.
329  * Under some circumstances (e.g. lack of output stream capacity) this operation
330  * would require several calls to ::BrotliEncoderCompressStream. The method must
331  * be called again until both input stream is depleted and encoder has no more
332  * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
333  *
334  * Finishing the stream means encoding of all input passed to encoder and
335  * adding specific "final" marks, so stream decoder could determine that stream
336  * is complete. To perform finish set @p op to ::BROTLI_OPERATION_FINISH.
337  * Under some circumstances (e.g. lack of output stream capacity) this operation
338  * would require several calls to ::BrotliEncoderCompressStream. The method must
339  * be called again until both input stream is depleted and encoder has no more
340  * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
341  *
342  * @warning When flushing and finishing, @p op should not change until operation
343  *          is complete; input stream should not be swapped, reduced or
344  *          extended as well.
345  *
346  * @param state encoder instance
347  * @param op requested operation
348  * @param[in, out] available_in @b in: amount of available input; \n
349  *                 @b out: amount of unused input
350  * @param[in, out] next_in pointer to the next input byte
351  * @param[in, out] available_out @b in: length of output buffer; \n
352  *                 @b out: remaining size of output buffer
353  * @param[in, out] next_out compressed output buffer cursor;
354  *                 can be @c NULL if @p available_out is @c 0
355  * @param[out] total_out number of bytes produced so far; can be @c NULL
356  * @returns ::BROTLI_FALSE if there was an error
357  * @returns ::BROTLI_TRUE otherwise
358  */
359 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompressStream(
360     BrotliEncoderState* state, BrotliEncoderOperation op, size_t* available_in,
361     const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
362     size_t* total_out);
363 
364 /**
365  * Checks if encoder instance reached the final state.
366  *
367  * @param state encoder instance
368  * @returns ::BROTLI_TRUE if encoder is in a state where it reached the end of
369  *          the input and produced all of the output
370  * @returns ::BROTLI_FALSE otherwise
371  */
372 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* state);
373 
374 /**
375  * Checks if encoder has more output.
376  *
377  * @param state encoder instance
378  * @returns ::BROTLI_TRUE, if encoder has some unconsumed output
379  * @returns ::BROTLI_FALSE otherwise
380  */
381 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderHasMoreOutput(
382     BrotliEncoderState* state);
383 
384 /**
385  * Acquires pointer to internal output buffer.
386  *
387  * This method is used to make language bindings easier and more efficient:
388  *  -# push data to ::BrotliEncoderCompressStream,
389  *     until ::BrotliEncoderHasMoreOutput returns BROTL_TRUE
390  *  -# use ::BrotliEncoderTakeOutput to peek bytes and copy to language-specific
391  *     entity
392  *
393  * Also this could be useful if there is an output stream that is able to
394  * consume all the provided data (e.g. when data is saved to file system).
395  *
396  * @attention After every call to ::BrotliEncoderTakeOutput @p *size bytes of
397  *            output are considered consumed for all consecutive calls to the
398  *            instance methods; returned pointer becomes invalidated as well.
399  *
400  * @note Encoder output is not guaranteed to be contiguous. This means that
401  *       after the size-unrestricted call to ::BrotliEncoderTakeOutput,
402  *       immediate next call to ::BrotliEncoderTakeOutput may return more data.
403  *
404  * @param state encoder instance
405  * @param[in, out] size @b in: number of bytes caller is ready to take, @c 0 if
406  *                 any amount could be handled; \n
407  *                 @b out: amount of data pointed by returned pointer and
408  *                 considered consumed; \n
409  *                 out value is never greater than in value, unless it is @c 0
410  * @returns pointer to output data
411  */
412 BROTLI_ENC_API const uint8_t* BrotliEncoderTakeOutput(
413     BrotliEncoderState* state, size_t* size);
414 
415 
416 /**
417  * Gets an encoder library version.
418  *
419  * Look at BROTLI_VERSION for more information.
420  */
421 BROTLI_ENC_API uint32_t BrotliEncoderVersion(void);
422 
423 #if defined(__cplusplus) || defined(c_plusplus)
424 }  /* extern "C" */
425 #endif
426 
427 #endif  /* BROTLI_ENC_ENCODE_H_ */
428