/src/libjxl/third_party/brotli/c/enc/quality.h
Line | Count | Source |
1 | | /* Copyright 2016 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 | | /* Constants and formulas that affect speed-ratio trade-offs and thus define |
8 | | quality levels. */ |
9 | | |
10 | | #ifndef BROTLI_ENC_QUALITY_H_ |
11 | | #define BROTLI_ENC_QUALITY_H_ |
12 | | |
13 | | #include "../common/platform.h" |
14 | | #include <brotli/encode.h> |
15 | | #include "params.h" |
16 | | |
17 | 0 | #define FAST_ONE_PASS_COMPRESSION_QUALITY 0 |
18 | 0 | #define FAST_TWO_PASS_COMPRESSION_QUALITY 1 |
19 | 0 | #define ZOPFLIFICATION_QUALITY 10 |
20 | 0 | #define HQ_ZOPFLIFICATION_QUALITY 11 |
21 | | |
22 | 0 | #define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2 |
23 | 0 | #define MIN_QUALITY_FOR_BLOCK_SPLIT 4 |
24 | 0 | #define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4 |
25 | 0 | #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4 |
26 | 0 | #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5 |
27 | 0 | #define MIN_QUALITY_FOR_CONTEXT_MODELING 5 |
28 | 0 | #define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7 |
29 | 0 | #define MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING 10 |
30 | | |
31 | | /* For quality below MIN_QUALITY_FOR_BLOCK_SPLIT there is no block splitting, |
32 | | so we buffer at most this much literals and commands. */ |
33 | | #define MAX_NUM_DELAYED_SYMBOLS 0x2FFF |
34 | | |
35 | | /* Returns hash-table size for quality levels 0 and 1. */ |
36 | 0 | static BROTLI_INLINE size_t MaxHashTableSize(int quality) { |
37 | 0 | return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17; |
38 | 0 | } Unexecuted instantiation: encode.c:MaxHashTableSize Unexecuted instantiation: encoder_dict.c:MaxHashTableSize Unexecuted instantiation: backward_references.c:MaxHashTableSize Unexecuted instantiation: backward_references_hq.c:MaxHashTableSize Unexecuted instantiation: block_splitter.c:MaxHashTableSize |
39 | | |
40 | | /* The maximum length for which the zopflification uses distinct distances. */ |
41 | 0 | #define MAX_ZOPFLI_LEN_QUALITY_10 150 |
42 | 0 | #define MAX_ZOPFLI_LEN_QUALITY_11 325 |
43 | | |
44 | | /* Do not thoroughly search when a long copy is found. */ |
45 | 0 | #define BROTLI_LONG_COPY_QUICK_STEP 16384 |
46 | | |
47 | 0 | static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) { |
48 | 0 | return params->quality <= 10 ? |
49 | 0 | MAX_ZOPFLI_LEN_QUALITY_10 : |
50 | 0 | MAX_ZOPFLI_LEN_QUALITY_11; |
51 | 0 | } Unexecuted instantiation: encode.c:MaxZopfliLen Unexecuted instantiation: encoder_dict.c:MaxZopfliLen Unexecuted instantiation: backward_references.c:MaxZopfliLen Unexecuted instantiation: backward_references_hq.c:MaxZopfliLen Unexecuted instantiation: block_splitter.c:MaxZopfliLen |
52 | | |
53 | | /* Number of best candidates to evaluate to expand Zopfli chain. */ |
54 | | static BROTLI_INLINE size_t MaxZopfliCandidates( |
55 | 0 | const BrotliEncoderParams* params) { |
56 | 0 | return params->quality <= 10 ? 1 : 5; |
57 | 0 | } Unexecuted instantiation: encode.c:MaxZopfliCandidates Unexecuted instantiation: encoder_dict.c:MaxZopfliCandidates Unexecuted instantiation: backward_references.c:MaxZopfliCandidates Unexecuted instantiation: backward_references_hq.c:MaxZopfliCandidates Unexecuted instantiation: block_splitter.c:MaxZopfliCandidates |
58 | | |
59 | 0 | static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) { |
60 | 0 | params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY, |
61 | 0 | BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality)); |
62 | 0 | if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) { |
63 | 0 | params->large_window = BROTLI_FALSE; |
64 | 0 | } |
65 | 0 | if (params->lgwin < BROTLI_MIN_WINDOW_BITS) { |
66 | 0 | params->lgwin = BROTLI_MIN_WINDOW_BITS; |
67 | 0 | } else { |
68 | 0 | int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS : |
69 | 0 | BROTLI_MAX_WINDOW_BITS; |
70 | 0 | if (params->lgwin > max_lgwin) params->lgwin = max_lgwin; |
71 | 0 | } |
72 | 0 | } Unexecuted instantiation: encode.c:SanitizeParams Unexecuted instantiation: encoder_dict.c:SanitizeParams Unexecuted instantiation: backward_references.c:SanitizeParams Unexecuted instantiation: backward_references_hq.c:SanitizeParams Unexecuted instantiation: block_splitter.c:SanitizeParams |
73 | | |
74 | | /* Returns optimized lg_block value. */ |
75 | 0 | static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) { |
76 | 0 | int lgblock = params->lgblock; |
77 | 0 | if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY || |
78 | 0 | params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) { |
79 | 0 | lgblock = params->lgwin; |
80 | 0 | } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) { |
81 | 0 | lgblock = 14; |
82 | 0 | } else if (lgblock == 0) { |
83 | 0 | lgblock = 16; |
84 | 0 | if (params->quality >= 9 && params->lgwin > lgblock) { |
85 | 0 | lgblock = BROTLI_MIN(int, 18, params->lgwin); |
86 | 0 | } |
87 | 0 | } else { |
88 | 0 | lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS, |
89 | 0 | BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock)); |
90 | 0 | } |
91 | 0 | return lgblock; |
92 | 0 | } Unexecuted instantiation: encode.c:ComputeLgBlock Unexecuted instantiation: encoder_dict.c:ComputeLgBlock Unexecuted instantiation: backward_references.c:ComputeLgBlock Unexecuted instantiation: backward_references_hq.c:ComputeLgBlock Unexecuted instantiation: block_splitter.c:ComputeLgBlock |
93 | | |
94 | | /* Returns log2 of the size of main ring buffer area. |
95 | | Allocate at least lgwin + 1 bits for the ring buffer so that the newly |
96 | | added block fits there completely and we still get lgwin bits and at least |
97 | | read_block_size_bits + 1 bits because the copy tail length needs to be |
98 | | smaller than ring-buffer size. */ |
99 | 0 | static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) { |
100 | 0 | return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock); |
101 | 0 | } Unexecuted instantiation: encode.c:ComputeRbBits Unexecuted instantiation: encoder_dict.c:ComputeRbBits Unexecuted instantiation: backward_references.c:ComputeRbBits Unexecuted instantiation: backward_references_hq.c:ComputeRbBits Unexecuted instantiation: block_splitter.c:ComputeRbBits |
102 | | |
103 | | static BROTLI_INLINE size_t MaxMetablockSize( |
104 | 0 | const BrotliEncoderParams* params) { |
105 | 0 | int bits = |
106 | 0 | BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS); |
107 | 0 | return (size_t)1 << bits; |
108 | 0 | } Unexecuted instantiation: encode.c:MaxMetablockSize Unexecuted instantiation: encoder_dict.c:MaxMetablockSize Unexecuted instantiation: backward_references.c:MaxMetablockSize Unexecuted instantiation: backward_references_hq.c:MaxMetablockSize Unexecuted instantiation: block_splitter.c:MaxMetablockSize |
109 | | |
110 | | /* When searching for backward references and have not seen matches for a long |
111 | | time, we can skip some match lookups. Unsuccessful match lookups are very |
112 | | expensive and this kind of a heuristic speeds up compression quite a lot. |
113 | | At first 8 byte strides are taken and every second byte is put to hasher. |
114 | | After 4x more literals stride by 16 bytes, every put 4-th byte to hasher. |
115 | | Applied only to qualities 2 to 9. */ |
116 | | static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch( |
117 | 0 | const BrotliEncoderParams* params) { |
118 | 0 | return params->quality < 9 ? 64 : 512; |
119 | 0 | } Unexecuted instantiation: encode.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: encoder_dict.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: backward_references.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: backward_references_hq.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: block_splitter.c:LiteralSpreeLengthForSparseSearch |
120 | | |
121 | | /* Quality to hasher mapping: |
122 | | |
123 | | - q02: h02 (longest_match_quickly), b16, l5 |
124 | | |
125 | | - q03: h03 (longest_match_quickly), b17, l5 |
126 | | |
127 | | - q04: h04 (longest_match_quickly), b17, l5 |
128 | | - q04: h54 (longest_match_quickly), b20, l7 | for large files |
129 | | |
130 | | - q05: h58 (longest_match_simd ), b14, l4 |
131 | | - q05: h68 (longest_match64_simd ), b15, l5 | for large files |
132 | | - q05: h40 (forgetful_chain ), b15, l4 | for small window |
133 | | |
134 | | - q06: h58 (longest_match_simd ), b14, l4 |
135 | | - q06: h68 (longest_match64_simd ), b15, l5 | for large files |
136 | | - q06: h40 (forgetful_chain ), b15, l4 | for small window |
137 | | |
138 | | - q07: h58 (longest_match_simd ), b15, l4 |
139 | | - q07: h68 (longest_match64_simd ), b15, l5 | for large files |
140 | | - q07: h41 (forgetful_chain ), b15, l4 | for small window |
141 | | |
142 | | - q08: h05 (longest_match ), b15, l4 |
143 | | - q08: h06 (longest_match64 ), b15, l5 | for large files |
144 | | - q08: h41 (forgetful_chain ), b15, l4 | for small window |
145 | | |
146 | | - q09: h05 (longest_match ), b15, l4 |
147 | | - q09: h06 (longest_match64 ), b15, l5 | for large files |
148 | | - q09: h42 (forgetful_chain ), b15, l4 | for small window |
149 | | |
150 | | - q10: t10 (to_binary_tree ), b17, l128 |
151 | | |
152 | | - q11: t10 (to_binary_tree ), b17, l128 |
153 | | |
154 | | Where "q" is quality, "h" is hasher type, "b" is bucket bits, |
155 | | "l" is source len. */ |
156 | | static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params, |
157 | 0 | BrotliHasherParams* hparams) { |
158 | 0 | if (params->quality > 9) { |
159 | 0 | hparams->type = 10; |
160 | 0 | } else if (params->quality == 4 && params->size_hint >= (1 << 20)) { |
161 | 0 | hparams->type = 54; |
162 | 0 | } else if (params->quality < 5) { |
163 | 0 | hparams->type = params->quality; |
164 | 0 | } else if (params->lgwin <= 16) { |
165 | 0 | hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42; |
166 | 0 | } else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) { |
167 | 0 | #if defined(BROTLI_MAX_SIMD_QUALITY) |
168 | 0 | hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 68 : 6; |
169 | | #else |
170 | | hparams->type = 6; |
171 | | #endif |
172 | 0 | hparams->block_bits = params->quality - 1; |
173 | 0 | hparams->bucket_bits = 15; |
174 | 0 | hparams->num_last_distances_to_check = |
175 | 0 | params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; |
176 | 0 | } else { |
177 | | /* TODO(eustas): often previous setting (H6) is faster and denser; consider |
178 | | adding an option to use it. */ |
179 | 0 | #if defined(BROTLI_MAX_SIMD_QUALITY) |
180 | 0 | hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 58 : 5; |
181 | | #else |
182 | | hparams->type = 5; |
183 | | #endif |
184 | 0 | hparams->block_bits = params->quality - 1; |
185 | 0 | hparams->bucket_bits = params->quality < 7 ? 14 : 15; |
186 | 0 | hparams->num_last_distances_to_check = |
187 | 0 | params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; |
188 | 0 | } |
189 | |
|
190 | 0 | if (params->lgwin > 24) { |
191 | | /* Different hashers for large window brotli: not for qualities <= 2, |
192 | | these are too fast for large window. Not for qualities >= 10: their |
193 | | hasher already works well with large window. So the changes are: |
194 | | H3 --> H35: for quality 3. |
195 | | H54 --> H55: for quality 4 with size hint > 1MB |
196 | | H6/H68 --> H65: for qualities 5, 6, 7, 8, 9. */ |
197 | 0 | if (hparams->type == 3) { |
198 | 0 | hparams->type = 35; |
199 | 0 | } |
200 | 0 | if (hparams->type == 54) { |
201 | 0 | hparams->type = 55; |
202 | 0 | } |
203 | 0 | if (hparams->type == 6 || hparams->type == 68) { |
204 | 0 | hparams->type = 65; |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } Unexecuted instantiation: encode.c:ChooseHasher Unexecuted instantiation: encoder_dict.c:ChooseHasher Unexecuted instantiation: backward_references.c:ChooseHasher Unexecuted instantiation: backward_references_hq.c:ChooseHasher Unexecuted instantiation: block_splitter.c:ChooseHasher |
208 | | |
209 | | #endif /* BROTLI_ENC_QUALITY_H_ */ |