encode.h 15 KB

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