Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/brotli/c/enc/quality.h @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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. */ | |
| 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 | |
| 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. */ | |
| 55 static BROTLI_INLINE size_t MaxZopfliCandidates( | |
| 56 const BrotliEncoderParams* params) { | |
| 57 return params->quality <= 10 ? 1 : 5; | |
| 58 } | |
| 59 | |
| 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. */ | |
| 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. */ | |
| 100 static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) { | |
| 101 return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock); | |
| 102 } | |
| 103 | |
| 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. */ | |
| 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: h58 (longest_match_simd ), b14, l4 | |
| 132 - q05: h68 (longest_match64_simd ), b15, l5 | for large files | |
| 133 - q05: h40 (forgetful_chain ), b15, l4 | for small window | |
| 134 | |
| 135 - q06: h58 (longest_match_simd ), b14, l4 | |
| 136 - q06: h68 (longest_match64_simd ), b15, l5 | for large files | |
| 137 - q06: h40 (forgetful_chain ), b15, l4 | for small window | |
| 138 | |
| 139 - q07: h58 (longest_match_simd ), b15, l4 | |
| 140 - q07: h68 (longest_match64_simd ), 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. */ | |
| 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 #if defined(BROTLI_MAX_SIMD_QUALITY) | |
| 169 hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 68 : 6; | |
| 170 #else | |
| 171 hparams->type = 6; | |
| 172 #endif | |
| 173 hparams->block_bits = params->quality - 1; | |
| 174 hparams->bucket_bits = 15; | |
| 175 hparams->num_last_distances_to_check = | |
| 176 params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; | |
| 177 } else { | |
| 178 /* TODO(eustas): often previous setting (H6) is faster and denser; consider | |
| 179 adding an option to use it. */ | |
| 180 #if defined(BROTLI_MAX_SIMD_QUALITY) | |
| 181 hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 58 : 5; | |
| 182 #else | |
| 183 hparams->type = 5; | |
| 184 #endif | |
| 185 hparams->block_bits = params->quality - 1; | |
| 186 hparams->bucket_bits = params->quality < 7 ? 14 : 15; | |
| 187 hparams->num_last_distances_to_check = | |
| 188 params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16; | |
| 189 } | |
| 190 | |
| 191 if (params->lgwin > 24) { | |
| 192 /* Different hashers for large window brotli: not for qualities <= 2, | |
| 193 these are too fast for large window. Not for qualities >= 10: their | |
| 194 hasher already works well with large window. So the changes are: | |
| 195 H3 --> H35: for quality 3. | |
| 196 H54 --> H55: for quality 4 with size hint > 1MB | |
| 197 H6/H68 --> H65: for qualities 5, 6, 7, 8, 9. */ | |
| 198 if (hparams->type == 3) { | |
| 199 hparams->type = 35; | |
| 200 } | |
| 201 if (hparams->type == 54) { | |
| 202 hparams->type = 55; | |
| 203 } | |
| 204 if (hparams->type == 6 || hparams->type == 68) { | |
| 205 hparams->type = 65; | |
| 206 } | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 #endif /* BROTLI_ENC_QUALITY_H_ */ |
