compress_fragment_two_pass.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright 2015 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. /* Function for fast encoding of an input fragment, independently from the input
  6. history. This function uses two-pass processing: in the first pass we save
  7. the found backward matches and literal bytes into a buffer, and in the
  8. second pass we emit them into the bit stream using prefix codes built based
  9. on the actual command and literal byte histograms. */
  10. #ifndef BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
  11. #define BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
  12. #include <brotli/types.h>
  13. #include "../common/constants.h"
  14. #include "../common/platform.h"
  15. #include "entropy_encode.h"
  16. #if defined(__cplusplus) || defined(c_plusplus)
  17. extern "C" {
  18. #endif
  19. /* TODO(eustas): turn to macro. */
  20. static const size_t kCompressFragmentTwoPassBlockSize = 1 << 17;
  21. typedef struct BrotliTwoPassArena {
  22. uint32_t lit_histo[256];
  23. uint8_t lit_depth[256];
  24. uint16_t lit_bits[256];
  25. uint32_t cmd_histo[128];
  26. uint8_t cmd_depth[128];
  27. uint16_t cmd_bits[128];
  28. /* BuildAndStoreCommandPrefixCode */
  29. HuffmanTree tmp_tree[2 * BROTLI_NUM_LITERAL_SYMBOLS + 1];
  30. uint8_t tmp_depth[BROTLI_NUM_COMMAND_SYMBOLS];
  31. uint16_t tmp_bits[64];
  32. } BrotliTwoPassArena;
  33. /* Compresses "input" string to the "*storage" buffer as one or more complete
  34. meta-blocks, and updates the "*storage_ix" bit position.
  35. If "is_last" is 1, emits an additional empty last meta-block.
  36. REQUIRES: "input_size" is greater than zero, or "is_last" is 1.
  37. REQUIRES: "input_size" is less or equal to maximal metablock size (1 << 24).
  38. REQUIRES: "command_buf" and "literal_buf" point to at least
  39. kCompressFragmentTwoPassBlockSize long arrays.
  40. REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
  41. REQUIRES: "table_size" is a power of two
  42. OUTPUT: maximal copy distance <= |input_size|
  43. OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */
  44. BROTLI_INTERNAL void BrotliCompressFragmentTwoPass(BrotliTwoPassArena* s,
  45. const uint8_t* input,
  46. size_t input_size,
  47. BROTLI_BOOL is_last,
  48. uint32_t* command_buf,
  49. uint8_t* literal_buf,
  50. int* table,
  51. size_t table_size,
  52. size_t* storage_ix,
  53. uint8_t* storage);
  54. #if defined(__cplusplus) || defined(c_plusplus)
  55. } /* extern "C" */
  56. #endif
  57. #endif /* BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_ */