/src/libjxl/third_party/brotli/c/enc/quality.h
Line | Count | Source (jump to first uncovered line) |
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 <brotli/encode.h> |
14 | | |
15 | | #include "../common/platform.h" |
16 | | #include "params.h" |
17 | | |
18 | 0 | #define FAST_ONE_PASS_COMPRESSION_QUALITY 0 |
19 | 0 | #define FAST_TWO_PASS_COMPRESSION_QUALITY 1 |
20 | 0 | #define ZOPFLIFICATION_QUALITY 10 |
21 | 0 | #define HQ_ZOPFLIFICATION_QUALITY 11 |
22 | | |
23 | 0 | #define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2 |
24 | 0 | #define MIN_QUALITY_FOR_BLOCK_SPLIT 4 |
25 | 0 | #define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4 |
26 | 0 | #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4 |
27 | 0 | #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5 |
28 | 0 | #define MIN_QUALITY_FOR_CONTEXT_MODELING 5 |
29 | 0 | #define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7 |
30 | 0 | #define MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING 10 |
31 | | |
32 | | /* For quality below MIN_QUALITY_FOR_BLOCK_SPLIT there is no block splitting, |
33 | | so we buffer at most this much literals and commands. */ |
34 | | #define MAX_NUM_DELAYED_SYMBOLS 0x2FFF |
35 | | |
36 | | /* Returns hash-table size for quality levels 0 and 1. */ |
37 | 0 | static BROTLI_INLINE size_t MaxHashTableSize(int quality) { |
38 | 0 | return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17; |
39 | 0 | } Unexecuted instantiation: encode.c:MaxHashTableSize Unexecuted instantiation: encoder_dict.c:MaxHashTableSize Unexecuted instantiation: compound_dictionary.c:MaxHashTableSize Unexecuted instantiation: metablock.c:MaxHashTableSize Unexecuted instantiation: block_splitter.c:MaxHashTableSize Unexecuted instantiation: bit_cost.c:MaxHashTableSize Unexecuted instantiation: cluster.c:MaxHashTableSize Unexecuted instantiation: histogram.c:MaxHashTableSize Unexecuted instantiation: compress_fragment_two_pass.c:MaxHashTableSize Unexecuted instantiation: brotli_bit_stream.c:MaxHashTableSize Unexecuted instantiation: compress_fragment.c:MaxHashTableSize Unexecuted instantiation: backward_references_hq.c:MaxHashTableSize Unexecuted instantiation: backward_references.c:MaxHashTableSize |
40 | | |
41 | | /* The maximum length for which the zopflification uses distinct distances. */ |
42 | 0 | #define MAX_ZOPFLI_LEN_QUALITY_10 150 |
43 | 0 | #define MAX_ZOPFLI_LEN_QUALITY_11 325 |
44 | | |
45 | | /* Do not thoroughly search when a long copy is found. */ |
46 | 0 | #define BROTLI_LONG_COPY_QUICK_STEP 16384 |
47 | | |
48 | 0 | static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) { |
49 | 0 | return params->quality <= 10 ? |
50 | 0 | MAX_ZOPFLI_LEN_QUALITY_10 : |
51 | 0 | MAX_ZOPFLI_LEN_QUALITY_11; |
52 | 0 | } Unexecuted instantiation: encode.c:MaxZopfliLen Unexecuted instantiation: encoder_dict.c:MaxZopfliLen Unexecuted instantiation: compound_dictionary.c:MaxZopfliLen Unexecuted instantiation: metablock.c:MaxZopfliLen Unexecuted instantiation: block_splitter.c:MaxZopfliLen Unexecuted instantiation: bit_cost.c:MaxZopfliLen Unexecuted instantiation: cluster.c:MaxZopfliLen Unexecuted instantiation: histogram.c:MaxZopfliLen Unexecuted instantiation: compress_fragment_two_pass.c:MaxZopfliLen Unexecuted instantiation: brotli_bit_stream.c:MaxZopfliLen Unexecuted instantiation: compress_fragment.c:MaxZopfliLen Unexecuted instantiation: backward_references_hq.c:MaxZopfliLen Unexecuted instantiation: backward_references.c:MaxZopfliLen |
53 | | |
54 | | /* Number of best candidates to evaluate to expand Zopfli chain. */ |
55 | | static BROTLI_INLINE size_t MaxZopfliCandidates( |
56 | 0 | const BrotliEncoderParams* params) { |
57 | 0 | return params->quality <= 10 ? 1 : 5; |
58 | 0 | } Unexecuted instantiation: encode.c:MaxZopfliCandidates Unexecuted instantiation: encoder_dict.c:MaxZopfliCandidates Unexecuted instantiation: compound_dictionary.c:MaxZopfliCandidates Unexecuted instantiation: metablock.c:MaxZopfliCandidates Unexecuted instantiation: block_splitter.c:MaxZopfliCandidates Unexecuted instantiation: bit_cost.c:MaxZopfliCandidates Unexecuted instantiation: cluster.c:MaxZopfliCandidates Unexecuted instantiation: histogram.c:MaxZopfliCandidates Unexecuted instantiation: compress_fragment_two_pass.c:MaxZopfliCandidates Unexecuted instantiation: brotli_bit_stream.c:MaxZopfliCandidates Unexecuted instantiation: compress_fragment.c:MaxZopfliCandidates Unexecuted instantiation: backward_references_hq.c:MaxZopfliCandidates Unexecuted instantiation: backward_references.c:MaxZopfliCandidates |
59 | | |
60 | 0 | static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) { |
61 | 0 | params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY, |
62 | 0 | BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality)); |
63 | 0 | if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) { |
64 | 0 | params->large_window = BROTLI_FALSE; |
65 | 0 | } |
66 | 0 | if (params->lgwin < BROTLI_MIN_WINDOW_BITS) { |
67 | 0 | params->lgwin = BROTLI_MIN_WINDOW_BITS; |
68 | 0 | } else { |
69 | 0 | int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS : |
70 | 0 | BROTLI_MAX_WINDOW_BITS; |
71 | 0 | if (params->lgwin > max_lgwin) params->lgwin = max_lgwin; |
72 | 0 | } |
73 | 0 | } Unexecuted instantiation: encode.c:SanitizeParams Unexecuted instantiation: encoder_dict.c:SanitizeParams Unexecuted instantiation: compound_dictionary.c:SanitizeParams Unexecuted instantiation: metablock.c:SanitizeParams Unexecuted instantiation: block_splitter.c:SanitizeParams Unexecuted instantiation: bit_cost.c:SanitizeParams Unexecuted instantiation: cluster.c:SanitizeParams Unexecuted instantiation: histogram.c:SanitizeParams Unexecuted instantiation: compress_fragment_two_pass.c:SanitizeParams Unexecuted instantiation: brotli_bit_stream.c:SanitizeParams Unexecuted instantiation: compress_fragment.c:SanitizeParams Unexecuted instantiation: backward_references_hq.c:SanitizeParams Unexecuted instantiation: backward_references.c:SanitizeParams |
74 | | |
75 | | /* Returns optimized lg_block value. */ |
76 | 0 | static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) { |
77 | 0 | int lgblock = params->lgblock; |
78 | 0 | if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY || |
79 | 0 | params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) { |
80 | 0 | lgblock = params->lgwin; |
81 | 0 | } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) { |
82 | 0 | lgblock = 14; |
83 | 0 | } else if (lgblock == 0) { |
84 | 0 | lgblock = 16; |
85 | 0 | if (params->quality >= 9 && params->lgwin > lgblock) { |
86 | 0 | lgblock = BROTLI_MIN(int, 18, params->lgwin); |
87 | 0 | } |
88 | 0 | } else { |
89 | 0 | lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS, |
90 | 0 | BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock)); |
91 | 0 | } |
92 | 0 | return lgblock; |
93 | 0 | } Unexecuted instantiation: encode.c:ComputeLgBlock Unexecuted instantiation: encoder_dict.c:ComputeLgBlock Unexecuted instantiation: compound_dictionary.c:ComputeLgBlock Unexecuted instantiation: metablock.c:ComputeLgBlock Unexecuted instantiation: block_splitter.c:ComputeLgBlock Unexecuted instantiation: bit_cost.c:ComputeLgBlock Unexecuted instantiation: cluster.c:ComputeLgBlock Unexecuted instantiation: histogram.c:ComputeLgBlock Unexecuted instantiation: compress_fragment_two_pass.c:ComputeLgBlock Unexecuted instantiation: brotli_bit_stream.c:ComputeLgBlock Unexecuted instantiation: compress_fragment.c:ComputeLgBlock Unexecuted instantiation: backward_references_hq.c:ComputeLgBlock Unexecuted instantiation: backward_references.c:ComputeLgBlock |
94 | | |
95 | | /* Returns log2 of the size of main ring buffer area. |
96 | | Allocate at least lgwin + 1 bits for the ring buffer so that the newly |
97 | | added block fits there completely and we still get lgwin bits and at least |
98 | | read_block_size_bits + 1 bits because the copy tail length needs to be |
99 | | smaller than ring-buffer size. */ |
100 | 0 | static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) { |
101 | 0 | return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock); |
102 | 0 | } Unexecuted instantiation: encode.c:ComputeRbBits Unexecuted instantiation: encoder_dict.c:ComputeRbBits Unexecuted instantiation: compound_dictionary.c:ComputeRbBits Unexecuted instantiation: metablock.c:ComputeRbBits Unexecuted instantiation: block_splitter.c:ComputeRbBits Unexecuted instantiation: bit_cost.c:ComputeRbBits Unexecuted instantiation: cluster.c:ComputeRbBits Unexecuted instantiation: histogram.c:ComputeRbBits Unexecuted instantiation: compress_fragment_two_pass.c:ComputeRbBits Unexecuted instantiation: brotli_bit_stream.c:ComputeRbBits Unexecuted instantiation: compress_fragment.c:ComputeRbBits Unexecuted instantiation: backward_references_hq.c:ComputeRbBits Unexecuted instantiation: backward_references.c:ComputeRbBits |
103 | | |
104 | | static BROTLI_INLINE size_t MaxMetablockSize( |
105 | 0 | const BrotliEncoderParams* params) { |
106 | 0 | int bits = |
107 | 0 | BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS); |
108 | 0 | return (size_t)1 << bits; |
109 | 0 | } Unexecuted instantiation: encode.c:MaxMetablockSize Unexecuted instantiation: encoder_dict.c:MaxMetablockSize Unexecuted instantiation: compound_dictionary.c:MaxMetablockSize Unexecuted instantiation: metablock.c:MaxMetablockSize Unexecuted instantiation: block_splitter.c:MaxMetablockSize Unexecuted instantiation: bit_cost.c:MaxMetablockSize Unexecuted instantiation: cluster.c:MaxMetablockSize Unexecuted instantiation: histogram.c:MaxMetablockSize Unexecuted instantiation: compress_fragment_two_pass.c:MaxMetablockSize Unexecuted instantiation: brotli_bit_stream.c:MaxMetablockSize Unexecuted instantiation: compress_fragment.c:MaxMetablockSize Unexecuted instantiation: backward_references_hq.c:MaxMetablockSize Unexecuted instantiation: backward_references.c:MaxMetablockSize |
110 | | |
111 | | /* When searching for backward references and have not seen matches for a long |
112 | | time, we can skip some match lookups. Unsuccessful match lookups are very |
113 | | expensive and this kind of a heuristic speeds up compression quite a lot. |
114 | | At first 8 byte strides are taken and every second byte is put to hasher. |
115 | | After 4x more literals stride by 16 bytes, every put 4-th byte to hasher. |
116 | | Applied only to qualities 2 to 9. */ |
117 | | static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch( |
118 | 0 | const BrotliEncoderParams* params) { |
119 | 0 | return params->quality < 9 ? 64 : 512; |
120 | 0 | } Unexecuted instantiation: encode.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: encoder_dict.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: compound_dictionary.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: metablock.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: block_splitter.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: bit_cost.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: cluster.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: histogram.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: compress_fragment_two_pass.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: brotli_bit_stream.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: compress_fragment.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: backward_references_hq.c:LiteralSpreeLengthForSparseSearch Unexecuted instantiation: backward_references.c:LiteralSpreeLengthForSparseSearch |
121 | | |
122 | | static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params, |
123 | 0 | BrotliHasherParams* hparams) { |
124 | 0 | if (params->quality > 9) { |
125 | 0 | hparams->type = 10; |
126 | 0 | } else if (params->quality == 4 && params->size_hint >= (1 << 20)) { |
127 | 0 | hparams->type = 54; |
128 | 0 | } else if (params->quality < 5) { |
129 | 0 | hparams->type = params->quality; |
130 | 0 | } else if (params->lgwin <= 16) { |
131 | 0 | hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42; |
132 | 0 | } else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) { |
133 | 0 | hparams->type = 6; |
134 | 0 | hparams->block_bits = params->quality - 1; |
135 | 0 | hparams->bucket_bits = 15; |
136 | 0 | hparams->hash_len = 5; |
137 | 0 | hparams->num_last_distances_to_check = |
138 | 0 | params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; |
139 | 0 | } else { |
140 | 0 | hparams->type = 5; |
141 | 0 | hparams->block_bits = params->quality - 1; |
142 | 0 | hparams->bucket_bits = params->quality < 7 ? 14 : 15; |
143 | 0 | hparams->num_last_distances_to_check = |
144 | 0 | params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; |
145 | 0 | } |
146 | |
|
147 | 0 | if (params->lgwin > 24) { |
148 | | /* Different hashers for large window brotli: not for qualities <= 2, |
149 | | these are too fast for large window. Not for qualities >= 10: their |
150 | | hasher already works well with large window. So the changes are: |
151 | | H3 --> H35: for quality 3. |
152 | | H54 --> H55: for quality 4 with size hint > 1MB |
153 | | H6 --> H65: for qualities 5, 6, 7, 8, 9. */ |
154 | 0 | if (hparams->type == 3) { |
155 | 0 | hparams->type = 35; |
156 | 0 | } |
157 | 0 | if (hparams->type == 54) { |
158 | 0 | hparams->type = 55; |
159 | 0 | } |
160 | 0 | if (hparams->type == 6) { |
161 | 0 | hparams->type = 65; |
162 | 0 | } |
163 | 0 | } |
164 | 0 | } Unexecuted instantiation: encode.c:ChooseHasher Unexecuted instantiation: encoder_dict.c:ChooseHasher Unexecuted instantiation: compound_dictionary.c:ChooseHasher Unexecuted instantiation: metablock.c:ChooseHasher Unexecuted instantiation: block_splitter.c:ChooseHasher Unexecuted instantiation: bit_cost.c:ChooseHasher Unexecuted instantiation: cluster.c:ChooseHasher Unexecuted instantiation: histogram.c:ChooseHasher Unexecuted instantiation: compress_fragment_two_pass.c:ChooseHasher Unexecuted instantiation: brotli_bit_stream.c:ChooseHasher Unexecuted instantiation: compress_fragment.c:ChooseHasher Unexecuted instantiation: backward_references_hq.c:ChooseHasher Unexecuted instantiation: backward_references.c:ChooseHasher |
165 | | |
166 | | #endif /* BROTLI_ENC_QUALITY_H_ */ |