| OLD | NEW |
| (Empty) | |
| 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
| 2 |
| 3 Distributed under MIT license. |
| 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 */ |
| 6 |
| 7 // Function to find backward reference copies. |
| 8 |
| 9 #ifndef BROTLI_ENC_BACKWARD_REFERENCES_H_ |
| 10 #define BROTLI_ENC_BACKWARD_REFERENCES_H_ |
| 11 |
| 12 #include <vector> |
| 13 |
| 14 #include "./hash.h" |
| 15 #include "./command.h" |
| 16 #include "./types.h" |
| 17 |
| 18 namespace brotli { |
| 19 |
| 20 // "commands" points to the next output command to write to, "*num_commands" is |
| 21 // initially the total amount of commands output by previous |
| 22 // CreateBackwardReferences calls, and must be incremented by the amount written |
| 23 // by this call. |
| 24 void CreateBackwardReferences(size_t num_bytes, |
| 25 size_t position, |
| 26 bool is_last, |
| 27 const uint8_t* ringbuffer, |
| 28 size_t ringbuffer_mask, |
| 29 const int quality, |
| 30 const int lgwin, |
| 31 Hashers* hashers, |
| 32 int hash_type, |
| 33 int* dist_cache, |
| 34 size_t* last_insert_len, |
| 35 Command* commands, |
| 36 size_t* num_commands, |
| 37 size_t* num_literals); |
| 38 |
| 39 static const float kInfinity = std::numeric_limits<float>::infinity(); |
| 40 |
| 41 struct ZopfliNode { |
| 42 ZopfliNode(void) : length(1), |
| 43 distance(0), |
| 44 insert_length(0), |
| 45 cost(kInfinity) {} |
| 46 |
| 47 inline uint32_t copy_length() const { |
| 48 return length & 0xffffff; |
| 49 } |
| 50 |
| 51 inline uint32_t length_code() const { |
| 52 const uint32_t modifier = length >> 24; |
| 53 return copy_length() + 9u - modifier; |
| 54 } |
| 55 |
| 56 inline uint32_t copy_distance() const { |
| 57 return distance & 0x1ffffff; |
| 58 } |
| 59 |
| 60 inline uint32_t distance_code() const { |
| 61 const uint32_t short_code = distance >> 25; |
| 62 return short_code == 0 ? copy_distance() + 15 : short_code - 1; |
| 63 } |
| 64 |
| 65 inline uint32_t command_length() const { |
| 66 return copy_length() + insert_length; |
| 67 } |
| 68 |
| 69 // best length to get up to this byte (not including this byte itself) |
| 70 // highest 8 bit is used to reconstruct the length code |
| 71 uint32_t length; |
| 72 // distance associated with the length |
| 73 // highest 7 bit contains distance short code + 1 (or zero if no short code) |
| 74 uint32_t distance; |
| 75 // number of literal inserts before this copy |
| 76 uint32_t insert_length; |
| 77 // smallest cost to get to this byte from the beginning, as found so far |
| 78 float cost; |
| 79 }; |
| 80 |
| 81 // Computes the shortest path of commands from position to at most |
| 82 // position + num_bytes. |
| 83 // |
| 84 // On return, path->size() is the number of commands found and path[i] is the |
| 85 // length of the ith command (copy length plus insert length). |
| 86 // Note that the sum of the lengths of all commands can be less than num_bytes. |
| 87 // |
| 88 // On return, the nodes[0..num_bytes] array will have the following |
| 89 // "ZopfliNode array invariant": |
| 90 // For each i in [1..num_bytes], if nodes[i].cost < kInfinity, then |
| 91 // (1) nodes[i].copy_length() >= 2 |
| 92 // (2) nodes[i].command_length() <= i and |
| 93 // (3) nodes[i - nodes[i].command_length()].cost < kInfinity |
| 94 void ZopfliComputeShortestPath(size_t num_bytes, |
| 95 size_t position, |
| 96 const uint8_t* ringbuffer, |
| 97 size_t ringbuffer_mask, |
| 98 const size_t max_backward_limit, |
| 99 const int* dist_cache, |
| 100 Hashers::H10* hasher, |
| 101 ZopfliNode* nodes, |
| 102 std::vector<uint32_t>* path); |
| 103 |
| 104 void ZopfliCreateCommands(const size_t num_bytes, |
| 105 const size_t block_start, |
| 106 const size_t max_backward_limit, |
| 107 const std::vector<uint32_t>& path, |
| 108 const ZopfliNode* nodes, |
| 109 int* dist_cache, |
| 110 size_t* last_insert_len, |
| 111 Command* commands, |
| 112 size_t* num_literals); |
| 113 |
| 114 } // namespace brotli |
| 115 |
| 116 #endif // BROTLI_ENC_BACKWARD_REFERENCES_H_ |
| OLD | NEW |