• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define FAST_ONE_PASS_COMPRESSION_QUALITY 0
19 #define FAST_TWO_PASS_COMPRESSION_QUALITY 1
20 #define ZOPFLIFICATION_QUALITY 10
21 #define HQ_ZOPFLIFICATION_QUALITY 11
22 
23 #define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2
24 #define MIN_QUALITY_FOR_BLOCK_SPLIT 4
25 #define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4
26 #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4
27 #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5
28 #define MIN_QUALITY_FOR_CONTEXT_MODELING 5
29 #define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7
30 #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. */
MaxHashTableSize(int quality)37 static BROTLI_INLINE size_t MaxHashTableSize(int quality) {
38   return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17;
39 }
40 
41 /* The maximum length for which the zopflification uses distinct distances. */
42 #define MAX_ZOPFLI_LEN_QUALITY_10 150
43 #define MAX_ZOPFLI_LEN_QUALITY_11 325
44 
45 /* Do not thoroughly search when a long copy is found. */
46 #define BROTLI_LONG_COPY_QUICK_STEP 16384
47 
MaxZopfliLen(const BrotliEncoderParams * params)48 static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) {
49   return params->quality <= 10 ?
50       MAX_ZOPFLI_LEN_QUALITY_10 :
51       MAX_ZOPFLI_LEN_QUALITY_11;
52 }
53 
54 /* Number of best candidates to evaluate to expand Zopfli chain. */
MaxZopfliCandidates(const BrotliEncoderParams * params)55 static BROTLI_INLINE size_t MaxZopfliCandidates(
56   const BrotliEncoderParams* params) {
57   return params->quality <= 10 ? 1 : 5;
58 }
59 
SanitizeParams(BrotliEncoderParams * params)60 static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) {
61   params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY,
62       BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality));
63   if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) {
64     params->large_window = BROTLI_FALSE;
65   }
66   if (params->lgwin < BROTLI_MIN_WINDOW_BITS) {
67     params->lgwin = BROTLI_MIN_WINDOW_BITS;
68   } else {
69     int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS :
70                                            BROTLI_MAX_WINDOW_BITS;
71     if (params->lgwin > max_lgwin) params->lgwin = max_lgwin;
72   }
73 }
74 
75 /* Returns optimized lg_block value. */
ComputeLgBlock(const BrotliEncoderParams * params)76 static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) {
77   int lgblock = params->lgblock;
78   if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
79       params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
80     lgblock = params->lgwin;
81   } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
82     lgblock = 14;
83   } else if (lgblock == 0) {
84     lgblock = 16;
85     if (params->quality >= 9 && params->lgwin > lgblock) {
86       lgblock = BROTLI_MIN(int, 18, params->lgwin);
87     }
88   } else {
89     lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS,
90         BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock));
91   }
92   return lgblock;
93 }
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. */
ComputeRbBits(const BrotliEncoderParams * params)100 static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) {
101   return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock);
102 }
103 
MaxMetablockSize(const BrotliEncoderParams * params)104 static BROTLI_INLINE size_t MaxMetablockSize(
105     const BrotliEncoderParams* params) {
106   int bits =
107       BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS);
108   return (size_t)1 << bits;
109 }
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. */
LiteralSpreeLengthForSparseSearch(const BrotliEncoderParams * params)117 static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch(
118     const BrotliEncoderParams* params) {
119   return params->quality < 9 ? 64 : 512;
120 }
121 
122 /* Quality to hasher mapping:
123 
124    - q02: h02 (longest_match_quickly), b16, l5
125 
126    - q03: h03 (longest_match_quickly), b17, l5
127 
128    - q04: h04 (longest_match_quickly), b17, l5
129    - q04: h54 (longest_match_quickly), b20, l7 | for large files
130 
131    - q05: h05 (longest_match        ), b14, l4
132    - q05: h06 (longest_match64      ), b15, l5 | for large files
133    - q05: h40 (forgetful_chain      ), b15, l4 | for small window
134 
135    - q06: h05 (longest_match        ), b14, l4
136    - q06: h06 (longest_match64      ), b15, l5 | for large files
137    - q06: h40 (forgetful_chain      ), b15, l4 | for small window
138 
139    - q07: h05 (longest_match        ), b15, l4
140    - q07: h06 (longest_match64      ), b15, l5 | for large files
141    - q07: h41 (forgetful_chain      ), b15, l4 | for small window
142 
143    - q08: h05 (longest_match        ), b15, l4
144    - q08: h06 (longest_match64      ), b15, l5 | for large files
145    - q08: h41 (forgetful_chain      ), b15, l4 | for small window
146 
147    - q09: h05 (longest_match        ), b15, l4
148    - q09: h06 (longest_match64      ), b15, l5 | for large files
149    - q09: h42 (forgetful_chain      ), b15, l4 | for small window
150 
151    - q10: t10 (to_binary_tree       ), b17, l128
152 
153    - q11: t10 (to_binary_tree       ), b17, l128
154 
155   Where "q" is quality, "h" is hasher type, "b" is bucket bits,
156   "l" is source len. */
ChooseHasher(const BrotliEncoderParams * params,BrotliHasherParams * hparams)157 static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
158                                        BrotliHasherParams* hparams) {
159   if (params->quality > 9) {
160     hparams->type = 10;
161   } else if (params->quality == 4 && params->size_hint >= (1 << 20)) {
162     hparams->type = 54;
163   } else if (params->quality < 5) {
164     hparams->type = params->quality;
165   } else if (params->lgwin <= 16) {
166     hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42;
167   } else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) {
168     hparams->type = 6;
169     hparams->block_bits = params->quality - 1;
170     hparams->bucket_bits = 15;
171     hparams->num_last_distances_to_check =
172         params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
173   } else {
174     /* TODO(eustas): often previous setting (H6) is faster and denser; consider
175                      adding an option to use it. */
176     hparams->type = 5;
177     hparams->block_bits = params->quality - 1;
178     hparams->bucket_bits = params->quality < 7 ? 14 : 15;
179     hparams->num_last_distances_to_check =
180         params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
181   }
182 
183   if (params->lgwin > 24) {
184     /* Different hashers for large window brotli: not for qualities <= 2,
185        these are too fast for large window. Not for qualities >= 10: their
186        hasher already works well with large window. So the changes are:
187        H3 --> H35: for quality 3.
188        H54 --> H55: for quality 4 with size hint > 1MB
189        H6 --> H65: for qualities 5, 6, 7, 8, 9. */
190     if (hparams->type == 3) {
191       hparams->type = 35;
192     }
193     if (hparams->type == 54) {
194       hparams->type = 55;
195     }
196     if (hparams->type == 6) {
197       hparams->type = 65;
198     }
199   }
200 }
201 
202 #endif  /* BROTLI_ENC_QUALITY_H_ */
203