backward_references_hq.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* Function to find backward reference copies. */
  6. #ifndef BROTLI_ENC_BACKWARD_REFERENCES_HQ_H_
  7. #define BROTLI_ENC_BACKWARD_REFERENCES_HQ_H_
  8. #include <brotli/types.h>
  9. #include "../common/constants.h"
  10. #include "../common/context.h"
  11. #include "../common/dictionary.h"
  12. #include "../common/platform.h"
  13. #include "command.h"
  14. #include "hash.h"
  15. #include "memory.h"
  16. #include "quality.h"
  17. #if defined(__cplusplus) || defined(c_plusplus)
  18. extern "C" {
  19. #endif
  20. BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(MemoryManager* m,
  21. size_t num_bytes,
  22. size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
  23. ContextLut literal_context_lut, const BrotliEncoderParams* params,
  24. Hasher* hasher, int* dist_cache, size_t* last_insert_len,
  25. Command* commands, size_t* num_commands, size_t* num_literals);
  26. BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m,
  27. size_t num_bytes,
  28. size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
  29. ContextLut literal_context_lut, const BrotliEncoderParams* params,
  30. Hasher* hasher, int* dist_cache, size_t* last_insert_len,
  31. Command* commands, size_t* num_commands, size_t* num_literals);
  32. typedef struct ZopfliNode {
  33. /* Best length to get up to this byte (not including this byte itself)
  34. highest 7 bit is used to reconstruct the length code. */
  35. uint32_t length;
  36. /* Distance associated with the length. */
  37. uint32_t distance;
  38. /* Number of literal inserts before this copy; highest 5 bits contain
  39. distance short code + 1 (or zero if no short code). */
  40. uint32_t dcode_insert_length;
  41. /* This union holds information used by dynamic-programming. During forward
  42. pass |cost| it used to store the goal function. When node is processed its
  43. |cost| is invalidated in favor of |shortcut|. On path back-tracing pass
  44. |next| is assigned the offset to next node on the path. */
  45. union {
  46. /* Smallest cost to get to this byte from the beginning, as found so far. */
  47. float cost;
  48. /* Offset to the next node on the path. Equals to command_length() of the
  49. next node on the path. For last node equals to BROTLI_UINT32_MAX */
  50. uint32_t next;
  51. /* Node position that provides next distance for distance cache. */
  52. uint32_t shortcut;
  53. } u;
  54. } ZopfliNode;
  55. BROTLI_INTERNAL void BrotliInitZopfliNodes(ZopfliNode* array, size_t length);
  56. /* Computes the shortest path of commands from position to at most
  57. position + num_bytes.
  58. On return, path->size() is the number of commands found and path[i] is the
  59. length of the i-th command (copy length plus insert length).
  60. Note that the sum of the lengths of all commands can be less than num_bytes.
  61. On return, the nodes[0..num_bytes] array will have the following
  62. "ZopfliNode array invariant":
  63. For each i in [1..num_bytes], if nodes[i].cost < kInfinity, then
  64. (1) nodes[i].copy_length() >= 2
  65. (2) nodes[i].command_length() <= i and
  66. (3) nodes[i - nodes[i].command_length()].cost < kInfinity */
  67. BROTLI_INTERNAL size_t BrotliZopfliComputeShortestPath(
  68. MemoryManager* m, size_t num_bytes,
  69. size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
  70. ContextLut literal_context_lut, const BrotliEncoderParams* params,
  71. const int* dist_cache, Hasher* hasher, ZopfliNode* nodes);
  72. BROTLI_INTERNAL void BrotliZopfliCreateCommands(
  73. const size_t num_bytes, const size_t block_start, const ZopfliNode* nodes,
  74. int* dist_cache, size_t* last_insert_len, const BrotliEncoderParams* params,
  75. Command* commands, size_t* num_literals);
  76. #if defined(__cplusplus) || defined(c_plusplus)
  77. } /* extern "C" */
  78. #endif
  79. #endif /* BROTLI_ENC_BACKWARD_REFERENCES_HQ_H_ */