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 #define FAST_ONE_PASS_COMPRESSION_QUALITY 0
18 #define FAST_TWO_PASS_COMPRESSION_QUALITY 1
19 #define ZOPFLIFICATION_QUALITY 10
20 #define HQ_ZOPFLIFICATION_QUALITY 11
21
22 #define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2
23 #define MIN_QUALITY_FOR_BLOCK_SPLIT 4
24 #define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4
25 #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4
26 #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5
27 #define MIN_QUALITY_FOR_CONTEXT_MODELING 5
28 #define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7
29 #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. */
MaxHashTableSize(int quality)36 static BROTLI_INLINE size_t MaxHashTableSize(int quality) {
37 return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17;
38 }
39
40 /* The maximum length for which the zopflification uses distinct distances. */
41 #define MAX_ZOPFLI_LEN_QUALITY_10 150
42 #define MAX_ZOPFLI_LEN_QUALITY_11 325
43
44 /* Do not thoroughly search when a long copy is found. */
45 #define BROTLI_LONG_COPY_QUICK_STEP 16384
46
MaxZopfliLen(const BrotliEncoderParams * params)47 static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) {
48 return params->quality <= 10 ?
49 MAX_ZOPFLI_LEN_QUALITY_10 :
50 MAX_ZOPFLI_LEN_QUALITY_11;
51 }
52
53 /* Number of best candidates to evaluate to expand Zopfli chain. */
MaxZopfliCandidates(const BrotliEncoderParams * params)54 static BROTLI_INLINE size_t MaxZopfliCandidates(
55 const BrotliEncoderParams* params) {
56 return params->quality <= 10 ? 1 : 5;
57 }
58
SanitizeParams(BrotliEncoderParams * params)59 static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) {
60 params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY,
61 BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality));
62 if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) {
63 params->large_window = BROTLI_FALSE;
64 }
65 if (params->lgwin < BROTLI_MIN_WINDOW_BITS) {
66 params->lgwin = BROTLI_MIN_WINDOW_BITS;
67 } else {
68 int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS :
69 BROTLI_MAX_WINDOW_BITS;
70 if (params->lgwin > max_lgwin) params->lgwin = max_lgwin;
71 }
72 }
73
74 /* Returns optimized lg_block value. */
ComputeLgBlock(const BrotliEncoderParams * params)75 static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) {
76 int lgblock = params->lgblock;
77 if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
78 params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
79 lgblock = params->lgwin;
80 } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
81 lgblock = 14;
82 } else if (lgblock == 0) {
83 lgblock = 16;
84 if (params->quality >= 9 && params->lgwin > lgblock) {
85 lgblock = BROTLI_MIN(int, 18, params->lgwin);
86 }
87 } else {
88 lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS,
89 BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock));
90 }
91 return lgblock;
92 }
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. */
ComputeRbBits(const BrotliEncoderParams * params)99 static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) {
100 return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock);
101 }
102
MaxMetablockSize(const BrotliEncoderParams * params)103 static BROTLI_INLINE size_t MaxMetablockSize(
104 const BrotliEncoderParams* params) {
105 int bits =
106 BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS);
107 return (size_t)1 << bits;
108 }
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. */
LiteralSpreeLengthForSparseSearch(const BrotliEncoderParams * params)116 static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch(
117 const BrotliEncoderParams* params) {
118 return params->quality < 9 ? 64 : 512;
119 }
120
ChooseHasher(const BrotliEncoderParams * params,BrotliHasherParams * hparams)121 static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
122 BrotliHasherParams* hparams) {
123 if (params->quality > 9) {
124 hparams->type = 10;
125 } else if (params->quality == 4 && params->size_hint >= (1 << 20)) {
126 hparams->type = 54;
127 } else if (params->quality < 5) {
128 hparams->type = params->quality;
129 } else if (params->lgwin <= 16) {
130 hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42;
131 } else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) {
132 hparams->type = 6;
133 hparams->block_bits = params->quality - 1;
134 hparams->bucket_bits = 15;
135 hparams->hash_len = 5;
136 hparams->num_last_distances_to_check =
137 params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
138 } else {
139 hparams->type = 5;
140 hparams->block_bits = params->quality - 1;
141 hparams->bucket_bits = params->quality < 7 ? 14 : 15;
142 hparams->num_last_distances_to_check =
143 params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
144 }
145
146 if (params->lgwin > 24) {
147 /* Different hashers for large window brotli: not for qualities <= 2,
148 these are too fast for large window. Not for qualities >= 10: their
149 hasher already works well with large window. So the changes are:
150 H3 --> H35: for quality 3.
151 H54 --> H55: for quality 4 with size hint > 1MB
152 H6 --> H65: for qualities 5, 6, 7, 8, 9. */
153 if (hparams->type == 3) {
154 hparams->type = 35;
155 }
156 if (hparams->type == 54) {
157 hparams->type = 55;
158 }
159 if (hparams->type == 6) {
160 hparams->type = 65;
161 }
162 }
163 }
164
165 #endif /* BROTLI_ENC_QUALITY_H_ */
166