| 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. */ |
| 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 |
|
| 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. */ |
| 54 |
static BROTLI_INLINE size_t MaxZopfliCandidates( |
| 55 |
const BrotliEncoderParams* params) { |
| 56 |
return params->quality <= 10 ? 1 : 5; |
| 57 |
} |
| 58 |
|
| 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. */ |
| 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. */ |
| 99 |
static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) { |
| 100 |
return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock); |
| 101 |
} |
| 102 |
|
| 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. */ |
| 116 |
static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch( |
| 117 |
const BrotliEncoderParams* params) { |
| 118 |
return params->quality < 9 ? 64 : 512; |
| 119 |
} |
| 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 |
BrotliHasherParams* hparams) { |
| 158 |
if (params->quality > 9) { |
| 159 |
hparams->type = 10; |
| 160 |
} else if (params->quality == 4 && params->size_hint >= (1 << 20)) { |
| 161 |
hparams->type = 54; |
| 162 |
} else if (params->quality < 5) { |
| 163 |
hparams->type = params->quality; |
| 164 |
} else if (params->lgwin <= 16) { |
| 165 |
hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42; |
| 166 |
} else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) { |
| 167 |
#if defined(BROTLI_MAX_SIMD_QUALITY) |
| 168 |
hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 68 : 6; |
| 169 |
#else |
| 170 |
hparams->type = 6; |
| 171 |
#endif |
| 172 |
hparams->block_bits = params->quality - 1; |
| 173 |
hparams->bucket_bits = 15; |
| 174 |
hparams->num_last_distances_to_check = |
| 175 |
params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; |
| 176 |
} else { |
| 177 |
/* TODO(eustas): often previous setting (H6) is faster and denser; consider |
| 178 |
adding an option to use it. */ |
| 179 |
#if defined(BROTLI_MAX_SIMD_QUALITY) |
| 180 |
hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 58 : 5; |
| 181 |
#else |
| 182 |
hparams->type = 5; |
| 183 |
#endif |
| 184 |
hparams->block_bits = params->quality - 1; |
| 185 |
hparams->bucket_bits = params->quality < 7 ? 14 : 15; |
| 186 |
hparams->num_last_distances_to_check = |
| 187 |
params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; |
| 188 |
} |
| 189 |
|
| 190 |
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 |
if (hparams->type == 3) { |
| 198 |
hparams->type = 35; |
| 199 |
} |
| 200 |
if (hparams->type == 54) { |
| 201 |
hparams->type = 55; |
| 202 |
} |
| 203 |
if (hparams->type == 6 || hparams->type == 68) { |
| 204 |
hparams->type = 65; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
#endif /* BROTLI_ENC_QUALITY_H_ */ |