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