backward_references.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_H_
  7. #define BROTLI_ENC_BACKWARD_REFERENCES_H_
  8. #include "../common/types.h"
  9. #include "./command.h"
  10. #include "./hash.h"
  11. #include "./memory.h"
  12. #include "./port.h"
  13. #if defined(__cplusplus) || defined(c_plusplus)
  14. extern "C" {
  15. #endif
  16. /* "commands" points to the next output command to write to, "*num_commands" is
  17. initially the total amount of commands output by previous
  18. CreateBackwardReferences calls, and must be incremented by the amount written
  19. by this call. */
  20. BROTLI_INTERNAL void BrotliCreateBackwardReferences(MemoryManager* m,
  21. size_t num_bytes,
  22. size_t position,
  23. int is_last,
  24. const uint8_t* ringbuffer,
  25. size_t ringbuffer_mask,
  26. const int quality,
  27. const int lgwin,
  28. Hashers* hashers,
  29. int hash_type,
  30. int* dist_cache,
  31. size_t* last_insert_len,
  32. Command* commands,
  33. size_t* num_commands,
  34. size_t* num_literals);
  35. typedef struct ZopfliNode {
  36. /* best length to get up to this byte (not including this byte itself)
  37. highest 8 bit is used to reconstruct the length code */
  38. uint32_t length;
  39. /* distance associated with the length
  40. highest 7 bit contains distance short code + 1 (or zero if no short code)
  41. */
  42. uint32_t distance;
  43. /* number of literal inserts before this copy */
  44. uint32_t insert_length;
  45. /* This union holds information used by dynamic-programming. During forward
  46. pass |cost| it used to store the goal function. When node is processed its
  47. |cost| is invalidated in favor of |shortcut|. On path backtracing pass
  48. |next| is assigned the offset to next node on the path. */
  49. union {
  50. /* Smallest cost to get to this byte from the beginning, as found so far. */
  51. float cost;
  52. /* Offset to the next node on the path. Equals to command_length() of the
  53. next node on the path. For last node equals to BROTLI_UINT32_MAX */
  54. uint32_t next;
  55. /* Node position that provides next distance for distance cache. */
  56. uint32_t shortcut;
  57. } u;
  58. } ZopfliNode;
  59. BROTLI_INTERNAL void BrotliInitZopfliNodes(ZopfliNode* array, size_t length);
  60. /* Computes the shortest path of commands from position to at most
  61. position + num_bytes.
  62. On return, path->size() is the number of commands found and path[i] is the
  63. length of the ith command (copy length plus insert length).
  64. Note that the sum of the lengths of all commands can be less than num_bytes.
  65. On return, the nodes[0..num_bytes] array will have the following
  66. "ZopfliNode array invariant":
  67. For each i in [1..num_bytes], if nodes[i].cost < kInfinity, then
  68. (1) nodes[i].copy_length() >= 2
  69. (2) nodes[i].command_length() <= i and
  70. (3) nodes[i - nodes[i].command_length()].cost < kInfinity */
  71. BROTLI_INTERNAL size_t BrotliZopfliComputeShortestPath(
  72. MemoryManager* m, size_t num_bytes, size_t position,
  73. const uint8_t* ringbuffer, size_t ringbuffer_mask, const int quality,
  74. const size_t max_backward_limit, const int* dist_cache, H10* hasher,
  75. ZopfliNode* nodes);
  76. BROTLI_INTERNAL void BrotliZopfliCreateCommands(const size_t num_bytes,
  77. const size_t block_start,
  78. const size_t max_backward_limit,
  79. const ZopfliNode* nodes,
  80. int* dist_cache,
  81. size_t* last_insert_len,
  82. Command* commands,
  83. size_t* num_literals);
  84. /* Maximum distance, see section 9.1. of the spec. */
  85. static BROTLI_INLINE size_t MaxBackwardLimit(int lgwin) {
  86. return (1u << lgwin) - 16;
  87. }
  88. #if defined(__cplusplus) || defined(c_plusplus)
  89. } /* extern "C" */
  90. #endif
  91. #endif /* BROTLI_ENC_BACKWARD_REFERENCES_H_ */