encode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* API for Brotli compression. */
  6. #ifndef BROTLI_ENC_ENCODE_H_
  7. #define BROTLI_ENC_ENCODE_H_
  8. #include "../common/types.h"
  9. #if defined(__cplusplus) || defined(c_plusplus)
  10. extern "C" {
  11. #endif
  12. static const int kBrotliMaxWindowBits = 24;
  13. static const int kBrotliMinWindowBits = 10;
  14. static const int kBrotliMinInputBlockBits = 16;
  15. static const int kBrotliMaxInputBlockBits = 24;
  16. typedef enum BrotliEncoderMode {
  17. /* Default compression mode. The compressor does not know anything in
  18. advance about the properties of the input. */
  19. BROTLI_MODE_GENERIC = 0,
  20. /* Compression mode for UTF-8 format text input. */
  21. BROTLI_MODE_TEXT = 1,
  22. /* Compression mode used in WOFF 2.0. */
  23. BROTLI_MODE_FONT = 2
  24. } BrotliEncoderMode;
  25. #define BROTLI_DEFAULT_QUALITY 11
  26. #define BROTLI_DEFAULT_WINDOW 22
  27. #define BROTLI_DEFAULT_MODE BROTLI_MODE_GENERIC
  28. typedef enum BrotliEncoderOperation {
  29. BROTLI_OPERATION_PROCESS = 0,
  30. /* Request output stream to flush. Performed when input stream is depleted
  31. and there is enough space in output stream. */
  32. BROTLI_OPERATION_FLUSH = 1,
  33. /* Request output stream to finish. Performed when input stream is depleted
  34. and there is enough space in output stream. */
  35. BROTLI_OPERATION_FINISH = 2
  36. } BrotliEncoderOperation;
  37. typedef enum BrotliEncoderParameter {
  38. BROTLI_PARAM_MODE = 0,
  39. /* Controls the compression-speed vs compression-density tradeoffs. The higher
  40. the quality, the slower the compression. Range is 0 to 11. */
  41. BROTLI_PARAM_QUALITY = 1,
  42. /* Base 2 logarithm of the sliding window size. Range is 10 to 24. */
  43. BROTLI_PARAM_LGWIN = 2,
  44. /* Base 2 logarithm of the maximum input block size. Range is 16 to 24.
  45. If set to 0, the value will be set based on the quality. */
  46. BROTLI_PARAM_LGBLOCK = 3
  47. } BrotliEncoderParameter;
  48. /* A state can not be reused for multiple brotli streams. */
  49. typedef struct BrotliEncoderStateStruct BrotliEncoderState;
  50. int BrotliEncoderSetParameter(
  51. BrotliEncoderState* state, BrotliEncoderParameter p, uint32_t value);
  52. /* Creates the instance of BrotliEncoderState and initializes it.
  53. |alloc_func| and |free_func| MUST be both zero or both non-zero. In the case
  54. they are both zero, default memory allocators are used. |opaque| is passed to
  55. |alloc_func| and |free_func| when they are called. */
  56. BrotliEncoderState* BrotliEncoderCreateInstance(brotli_alloc_func alloc_func,
  57. brotli_free_func free_func,
  58. void* opaque);
  59. /* Deinitializes and frees BrotliEncoderState instance. */
  60. void BrotliEncoderDestroyInstance(BrotliEncoderState* state);
  61. /* The maximum input size that can be processed at once. */
  62. size_t BrotliEncoderInputBlockSize(BrotliEncoderState* state);
  63. /* Encodes the data in |input_buffer| as a meta-block and writes it to
  64. |encoded_buffer| (|*encoded_size should| be set to the size of
  65. |encoded_buffer|) and sets |*encoded_size| to the number of bytes that
  66. was written. The |input_size| must not be greater than input_block_size().
  67. Returns 0 if there was an error and 1 otherwise. */
  68. int BrotliEncoderWriteMetaBlock(BrotliEncoderState* state,
  69. const size_t input_size,
  70. const uint8_t* input_buffer, const int is_last,
  71. size_t* encoded_size, uint8_t* encoded_buffer);
  72. /* Writes a metadata meta-block containing the given input to encoded_buffer.
  73. |*encoded_size| should be set to the size of the encoded_buffer.
  74. Sets |*encoded_size| to the number of bytes that was written.
  75. Note that the given input data will not be part of the sliding window and
  76. thus no backward references can be made to this data from subsequent
  77. metablocks. |input_size| must not be greater than 2^24 and provided
  78. |*encoded_size| must not be less than |input_size| + 6.
  79. Returns 0 if there was an error and 1 otherwise. */
  80. int BrotliEncoderWriteMetadata(BrotliEncoderState* state,
  81. const size_t input_size,
  82. const uint8_t* input_buffer, const int is_last,
  83. size_t* encoded_size, uint8_t* encoded_buffer);
  84. /* Writes a zero-length meta-block with end-of-input bit set to the
  85. internal output buffer and copies the output buffer to |encoded_buffer|
  86. (|*encoded_size| should be set to the size of |encoded_buffer|) and sets
  87. |*encoded_size| to the number of bytes written.
  88. Returns 0 if there was an error and 1 otherwise. */
  89. int BrotliEncoderFinishStream(BrotliEncoderState* state, size_t* encoded_size,
  90. uint8_t* encoded_buffer);
  91. /* Copies the given input data to the internal ring buffer of the compressor.
  92. No processing of the data occurs at this time and this function can be
  93. called multiple times before calling WriteBrotliData() to process the
  94. accumulated input. At most input_block_size() bytes of input data can be
  95. copied to the ring buffer, otherwise the next WriteBrotliData() will fail.
  96. */
  97. void BrotliEncoderCopyInputToRingBuffer(BrotliEncoderState* state,
  98. const size_t input_size,
  99. const uint8_t* input_buffer);
  100. /* Processes the accumulated input data and sets |*out_size| to the length of
  101. the new output meta-block, or to zero if no new output meta-block has been
  102. created (in this case the processed input data is buffered internally).
  103. If |*out_size| is positive, |*output| points to the start of the output
  104. data. If |is_last| or |force_flush| is 1, an output meta-block is always
  105. created. However, until |is_last| is 1 encoder may retain up to 7 bits
  106. of the last byte of output. To force encoder to dump the remaining bits
  107. use WriteMetadata() to append an empty meta-data block.
  108. Returns 0 if the size of the input data is larger than
  109. input_block_size(). */
  110. int BrotliEncoderWriteData(BrotliEncoderState* state, const int is_last,
  111. const int force_flush, size_t* out_size,
  112. uint8_t** output);
  113. /* Fills the new state with a dictionary for LZ77, warming up the ringbuffer,
  114. e.g. for custom static dictionaries for data formats.
  115. Not to be confused with the built-in transformable dictionary of Brotli.
  116. To decode, use BrotliSetCustomDictionary() of the decoder with the same
  117. dictionary. */
  118. void BrotliEncoderSetCustomDictionary(BrotliEncoderState* state, size_t size,
  119. const uint8_t* dict);
  120. /* Returns buffer size that is large enough to contain BrotliEncoderCompress
  121. output for any input.
  122. Returns 0 if result does not fit size_t. */
  123. size_t BrotliEncoderMaxCompressedSize(size_t input_size);
  124. /* Compresses the data in |input_buffer| into |encoded_buffer|, and sets
  125. |*encoded_size| to the compressed length.
  126. BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW and BROTLI_DEFAULT_MODE should
  127. be used as |quality|, |lgwin| and |mode| if there are no specific
  128. requirements to encoder speed and compression ratio.
  129. If compression fails, |*encoded_size| is set to 0.
  130. If BrotliEncoderMaxCompressedSize(|input_size|) is not zero, then
  131. |*encoded_size| is never set to the bigger value.
  132. Returns 0 if there was an error and 1 otherwise. */
  133. int BrotliEncoderCompress(int quality, int lgwin, BrotliEncoderMode mode,
  134. size_t input_size, const uint8_t* input_buffer,
  135. size_t* encoded_size, uint8_t* encoded_buffer);
  136. /* Progressively compress input stream and push produced bytes to output stream.
  137. Internally workflow consists of 3 tasks:
  138. * (optional) copy input data to internal buffer
  139. * actually compress data and (optionally) store it to internal buffer
  140. * (optional) copy compressed bytes from internal buffer to output stream
  141. Whenever all 3 tasks can't move forward anymore, or error occurs, this
  142. method returns.
  143. |available_in| and |next_in| represent input stream; when X bytes of input
  144. are consumed, X is subtracted from |available_in| and added to |next_in|.
  145. |available_out| and |next_out| represent output stream; when Y bytes are
  146. pushed to output, Y is subtracted from |available_out| and added to
  147. |next_out|. |total_out|, if it is not a null-pointer, is assigned to the
  148. total amount of bytes pushed by the instance of encoder to output.
  149. |op| is used to perform flush or finish the stream.
  150. Flushing the stream means forcing encoding of all input passed to encoder and
  151. completing the current output block, so it could be fully decoded by stream
  152. decoder. To perform flush |op| must be set to BROTLI_OPERATION_FLUSH. Under
  153. some circumstances (e.g. lack of output stream capacity) this operation would
  154. require several calls to BrotliEncoderCompressStream. The method must be
  155. called again until both input stream is depleted and encoder has no more
  156. output (see BrotliEncoderHasMoreOutput) after the method is called.
  157. Finishing the stream means encoding of all input passed to encoder and
  158. adding specific "final" marks, so stream decoder could determine that stream
  159. is complete. To perform finish |op| must be set to BROTLI_OPERATION_FINISH.
  160. Under some circumstances (e.g. lack of output stream capacity) this operation
  161. would require several calls to BrotliEncoderCompressStream. The method must
  162. be called again until both input stream is depleted and encoder has no more
  163. output (see BrotliEncoderHasMoreOutput) after the method is called.
  164. WARNING: when flushing and finishing, |op| should not change until operation
  165. is complete; input stream should not be refilled as well.
  166. Returns 0 if there was an error and 1 otherwise.
  167. */
  168. int BrotliEncoderCompressStream(BrotliEncoderState* s,
  169. BrotliEncoderOperation op, size_t* available_in,
  170. const uint8_t** next_in, size_t* available_out,
  171. uint8_t** next_out, size_t* total_out);
  172. /* Check if encoder is in "finished" state, i.e. no more input is acceptable and
  173. no more output will be produced.
  174. Works only with BrotliEncoderCompressStream workflow.
  175. Returns 1 if stream is finished and 0 otherwise. */
  176. int BrotliEncoderIsFinished(BrotliEncoderState* s);
  177. /* Check if encoder has more output bytes in internal buffer.
  178. Works only with BrotliEncoderCompressStream workflow.
  179. Returns 1 if has more output (in internal buffer) and 0 otherwise. */
  180. int BrotliEncoderHasMoreOutput(BrotliEncoderState* s);
  181. #if defined(__cplusplus) || defined(c_plusplus)
  182. } /* extern "C" */
  183. #endif
  184. #endif /* BROTLI_ENC_ENCODE_H_ */