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