/src/ghostpdl/brotli/c/enc/block_splitter.c
Line | Count | Source |
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 | | /* Block split point selection utilities. */ |
8 | | |
9 | | #include "block_splitter.h" |
10 | | |
11 | | #include "../common/platform.h" |
12 | | #include "bit_cost.h" |
13 | | #include "cluster.h" |
14 | | #include "command.h" |
15 | | #include "fast_log.h" |
16 | | #include "histogram.h" |
17 | | #include "memory.h" |
18 | | #include "quality.h" |
19 | | |
20 | | #if defined(__cplusplus) || defined(c_plusplus) |
21 | | extern "C" { |
22 | | #endif |
23 | | |
24 | | static const size_t kMaxLiteralHistograms = 100; |
25 | | static const size_t kMaxCommandHistograms = 50; |
26 | | static const double kLiteralBlockSwitchCost = 28.1; |
27 | | static const double kCommandBlockSwitchCost = 13.5; |
28 | | static const double kDistanceBlockSwitchCost = 14.6; |
29 | | static const size_t kLiteralStrideLength = 70; |
30 | | static const size_t kCommandStrideLength = 40; |
31 | | static const size_t kDistanceStrideLength = 40; |
32 | | static const size_t kSymbolsPerLiteralHistogram = 544; |
33 | | static const size_t kSymbolsPerCommandHistogram = 530; |
34 | | static const size_t kSymbolsPerDistanceHistogram = 544; |
35 | | static const size_t kMinLengthForBlockSplitting = 128; |
36 | | static const size_t kIterMulForRefining = 2; |
37 | | static const size_t kMinItersForRefining = 100; |
38 | | |
39 | 0 | static size_t CountLiterals(const Command* cmds, const size_t num_commands) { |
40 | | /* Count how many we have. */ |
41 | 0 | size_t total_length = 0; |
42 | 0 | size_t i; |
43 | 0 | for (i = 0; i < num_commands; ++i) { |
44 | 0 | total_length += cmds[i].insert_len_; |
45 | 0 | } |
46 | 0 | return total_length; |
47 | 0 | } |
48 | | |
49 | | static void CopyLiteralsToByteArray(const Command* cmds, |
50 | | const size_t num_commands, |
51 | | const uint8_t* data, |
52 | | const size_t offset, |
53 | | const size_t mask, |
54 | 0 | uint8_t* literals) { |
55 | 0 | size_t pos = 0; |
56 | 0 | size_t from_pos = offset & mask; |
57 | 0 | size_t i; |
58 | 0 | for (i = 0; i < num_commands; ++i) { |
59 | 0 | size_t insert_len = cmds[i].insert_len_; |
60 | 0 | if (from_pos + insert_len > mask) { |
61 | 0 | size_t head_size = mask + 1 - from_pos; |
62 | 0 | memcpy(literals + pos, data + from_pos, head_size); |
63 | 0 | from_pos = 0; |
64 | 0 | pos += head_size; |
65 | 0 | insert_len -= head_size; |
66 | 0 | } |
67 | 0 | if (insert_len > 0) { |
68 | 0 | memcpy(literals + pos, data + from_pos, insert_len); |
69 | 0 | pos += insert_len; |
70 | 0 | } |
71 | 0 | from_pos = (from_pos + insert_len + CommandCopyLen(&cmds[i])) & mask; |
72 | 0 | } |
73 | 0 | } |
74 | | |
75 | 0 | static BROTLI_INLINE uint32_t MyRand(uint32_t* seed) { |
76 | | /* Initial seed should be 7. In this case, loop length is (1 << 29). */ |
77 | 0 | *seed *= 16807U; |
78 | 0 | return *seed; |
79 | 0 | } |
80 | | |
81 | 0 | static BROTLI_INLINE double BitCost(size_t count) { |
82 | 0 | return count == 0 ? -2.0 : FastLog2(count); |
83 | 0 | } |
84 | | |
85 | 0 | #define HISTOGRAMS_PER_BATCH 64 |
86 | 0 | #define CLUSTERS_PER_BATCH 16 |
87 | | |
88 | 0 | #define FN(X) X ## Literal |
89 | | #define DataType uint8_t |
90 | | /* NOLINTNEXTLINE(build/include) */ |
91 | | #include "block_splitter_inc.h" |
92 | | #undef DataType |
93 | | #undef FN |
94 | | |
95 | 0 | #define FN(X) X ## Command |
96 | | #define DataType uint16_t |
97 | | /* NOLINTNEXTLINE(build/include) */ |
98 | | #include "block_splitter_inc.h" |
99 | | #undef FN |
100 | | |
101 | 0 | #define FN(X) X ## Distance |
102 | | /* NOLINTNEXTLINE(build/include) */ |
103 | | #include "block_splitter_inc.h" |
104 | | #undef DataType |
105 | | #undef FN |
106 | | |
107 | 0 | void BrotliInitBlockSplit(BlockSplit* self) { |
108 | 0 | self->num_types = 0; |
109 | 0 | self->num_blocks = 0; |
110 | 0 | self->types = 0; |
111 | 0 | self->lengths = 0; |
112 | 0 | self->types_alloc_size = 0; |
113 | 0 | self->lengths_alloc_size = 0; |
114 | 0 | } |
115 | | |
116 | 0 | void BrotliDestroyBlockSplit(MemoryManager* m, BlockSplit* self) { |
117 | 0 | BROTLI_FREE(m, self->types); |
118 | 0 | BROTLI_FREE(m, self->lengths); |
119 | 0 | } |
120 | | |
121 | | /* Extracts literals, command distance and prefix codes, then applies |
122 | | * SplitByteVector to create partitioning. */ |
123 | | void BrotliSplitBlock(MemoryManager* m, |
124 | | const Command* cmds, |
125 | | const size_t num_commands, |
126 | | const uint8_t* data, |
127 | | const size_t pos, |
128 | | const size_t mask, |
129 | | const BrotliEncoderParams* params, |
130 | | BlockSplit* literal_split, |
131 | | BlockSplit* insert_and_copy_split, |
132 | 0 | BlockSplit* dist_split) { |
133 | 0 | { |
134 | 0 | size_t literals_count = CountLiterals(cmds, num_commands); |
135 | 0 | uint8_t* literals = BROTLI_ALLOC(m, uint8_t, literals_count); |
136 | 0 | if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(literals)) return; |
137 | | /* Create a continuous array of literals. */ |
138 | 0 | CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals); |
139 | | /* Create the block split on the array of literals. |
140 | | * Literal histograms can have alphabet size up to 256. |
141 | | * Though, to accommodate context modeling, less than half of maximum size |
142 | | * is allowed. */ |
143 | 0 | SplitByteVectorLiteral( |
144 | 0 | m, literals, literals_count, |
145 | 0 | kSymbolsPerLiteralHistogram, kMaxLiteralHistograms, |
146 | 0 | kLiteralStrideLength, kLiteralBlockSwitchCost, params, |
147 | 0 | literal_split); |
148 | 0 | if (BROTLI_IS_OOM(m)) return; |
149 | 0 | BROTLI_FREE(m, literals); |
150 | | /* NB: this might be a good place for injecting extra splitting without |
151 | | * increasing encoder complexity; however, output partition would be less |
152 | | * optimal than one produced with forced splitting inside |
153 | | * SplitByteVector (FindBlocks / ClusterBlocks). */ |
154 | 0 | } |
155 | | |
156 | 0 | { |
157 | | /* Compute prefix codes for commands. */ |
158 | 0 | uint16_t* insert_and_copy_codes = BROTLI_ALLOC(m, uint16_t, num_commands); |
159 | 0 | size_t i; |
160 | 0 | if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(insert_and_copy_codes)) return; |
161 | 0 | for (i = 0; i < num_commands; ++i) { |
162 | 0 | insert_and_copy_codes[i] = cmds[i].cmd_prefix_; |
163 | 0 | } |
164 | | /* Create the block split on the array of command prefixes. */ |
165 | 0 | SplitByteVectorCommand( |
166 | 0 | m, insert_and_copy_codes, num_commands, |
167 | 0 | kSymbolsPerCommandHistogram, kMaxCommandHistograms, |
168 | 0 | kCommandStrideLength, kCommandBlockSwitchCost, params, |
169 | 0 | insert_and_copy_split); |
170 | 0 | if (BROTLI_IS_OOM(m)) return; |
171 | | /* TODO(eustas): reuse for distances? */ |
172 | 0 | BROTLI_FREE(m, insert_and_copy_codes); |
173 | 0 | } |
174 | | |
175 | 0 | { |
176 | | /* Create a continuous array of distance prefixes. */ |
177 | 0 | uint16_t* distance_prefixes = BROTLI_ALLOC(m, uint16_t, num_commands); |
178 | 0 | size_t j = 0; |
179 | 0 | size_t i; |
180 | 0 | if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(distance_prefixes)) return; |
181 | 0 | for (i = 0; i < num_commands; ++i) { |
182 | 0 | const Command* cmd = &cmds[i]; |
183 | 0 | if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) { |
184 | 0 | distance_prefixes[j++] = cmd->dist_prefix_ & 0x3FF; |
185 | 0 | } |
186 | 0 | } |
187 | | /* Create the block split on the array of distance prefixes. */ |
188 | 0 | SplitByteVectorDistance( |
189 | 0 | m, distance_prefixes, j, |
190 | 0 | kSymbolsPerDistanceHistogram, kMaxCommandHistograms, |
191 | 0 | kDistanceStrideLength, kDistanceBlockSwitchCost, params, |
192 | 0 | dist_split); |
193 | 0 | if (BROTLI_IS_OOM(m)) return; |
194 | 0 | BROTLI_FREE(m, distance_prefixes); |
195 | 0 | } |
196 | 0 | } |
197 | | |
198 | | #if defined(BROTLI_TEST) |
199 | | size_t BrotliCountLiteralsForTest(const Command*, size_t); |
200 | | size_t BrotliCountLiteralsForTest(const Command* cmds, size_t num_commands) { |
201 | | return CountLiterals(cmds, num_commands); |
202 | | } |
203 | | void BrotliCopyLiteralsToByteArrayForTest( |
204 | | const Command*, size_t, const uint8_t*, size_t, size_t, uint8_t*); |
205 | | void BrotliCopyLiteralsToByteArrayForTest(const Command* cmds, |
206 | | size_t num_commands, const uint8_t* data, size_t offset, size_t mask, |
207 | | uint8_t* literals) { |
208 | | CopyLiteralsToByteArray(cmds, num_commands, data, offset, mask, literals); |
209 | | } |
210 | | #endif |
211 | | |
212 | | #if defined(__cplusplus) || defined(c_plusplus) |
213 | | } /* extern "C" */ |
214 | | #endif |