| 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_cxx . . $ `c/enc/quality.h:11:9`! |
| 12 | |
| 13 | #include "../common/platform.h"cxx . . $ `<file>/c/common/platform.h`/ |
| 14 | #include <brotli/encode.h>cxx . . $ `<file>/c/include/brotli/encode.h`/ |
| 15 | #include "params.h"cxx . . $ `<file>/c/enc/params.h`/ |
| 16 | |
| 17 | #define FAST_ONE_PASS_COMPRESSION_QUALITYcxx . . $ `c/enc/quality.h:17:9`! 0 |
| 18 | #define FAST_TWO_PASS_COMPRESSION_QUALITYcxx . . $ `c/enc/quality.h:18:9`! 1 |
| 19 | #define ZOPFLIFICATION_QUALITYcxx . . $ `c/enc/quality.h:19:9`! 10 |
| 20 | #define HQ_ZOPFLIFICATION_QUALITYcxx . . $ `c/enc/quality.h:20:9`! 11 |
| 21 | |
| 22 | #define MAX_QUALITY_FOR_STATIC_ENTROPY_CODEScxx . . $ `c/enc/quality.h:22:9`! 2 |
| 23 | #define MIN_QUALITY_FOR_BLOCK_SPLITcxx . . $ `c/enc/quality.h:23:9`! 4 |
| 24 | #define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMScxx . . $ `c/enc/quality.h:24:9`! 4 |
| 25 | #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMScxx . . $ `c/enc/quality.h:25:9`! 4 |
| 26 | #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCHcxx . . $ `c/enc/quality.h:26:9`! 5 |
| 27 | #define MIN_QUALITY_FOR_CONTEXT_MODELINGcxx . . $ `c/enc/quality.h:27:9`! 5 |
| 28 | #define MIN_QUALITY_FOR_HQ_CONTEXT_MODELINGcxx . . $ `c/enc/quality.h:28:9`! 7 |
| 29 | #define MIN_QUALITY_FOR_HQ_BLOCK_SPLITTINGcxx . . $ `c/enc/quality.h:29:9`! 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_SYMBOLScxx . . $ `c/enc/quality.h:33:9`! 0x2FFF |
| 34 | |
| 35 | /* Returns hash-table size for quality levels 0 and 1. */ |
| 36 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t MaxHashTableSizecxx . . $ MaxHashTableSize(c104ebd7d68b2193).(int quality) { |
| 37 | return quality == FAST_ONE_PASS_COMPRESSION_QUALITYcxx . . $ `c/enc/quality.h:17:9`! ? 1 << 15 : 1 << 17; |
| 38 | } |
| 39 | |
| 40 | /* The maximum length for which the zopflification uses distinct distances. */ |
| 41 | #define MAX_ZOPFLI_LEN_QUALITY_10cxx . . $ `c/enc/quality.h:41:9`! 150 |
| 42 | #define MAX_ZOPFLI_LEN_QUALITY_11cxx . . $ `c/enc/quality.h:42:9`! 325 |
| 43 | |
| 44 | /* Do not thoroughly search when a long copy is found. */ |
| 45 | #define BROTLI_LONG_COPY_QUICK_STEPcxx . . $ `c/enc/quality.h:45:9`! 16384 |
| 46 | |
| 47 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t MaxZopfliLencxx . . $ MaxZopfliLen(76b5aa81b68a1b65).(const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) { |
| 48 | return params->qualitycxx . . $ BrotliEncoderParams#quality. <= 10 ? |
| 49 | MAX_ZOPFLI_LEN_QUALITY_10cxx . . $ `c/enc/quality.h:41:9`! : |
| 50 | MAX_ZOPFLI_LEN_QUALITY_11cxx . . $ `c/enc/quality.h:42:9`!; |
| 51 | } |
| 52 | |
| 53 | /* Number of best candidates to evaluate to expand Zopfli chain. */ |
| 54 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t MaxZopfliCandidatescxx . . $ MaxZopfliCandidates(76b5aa81b68a1b65).( |
| 55 | const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) { |
| 56 | return params->qualitycxx . . $ BrotliEncoderParams#quality. <= 10 ? 1 : 5; |
| 57 | } |
| 58 | |
| 59 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void SanitizeParamscxx . . $ SanitizeParams(4797c20457e2faee).(BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) { |
| 60 | params->qualitycxx . . $ BrotliEncoderParams#quality. = BROTLI_MINcxx . . $ `c/common/platform.h:522:9`!(int, BROTLI_MAX_QUALITYcxx . . $ `c/include/brotli/encode.h:43:9`!, |
| 61 | BROTLI_MAXcxx . . $ `c/common/platform.h:523:9`!(int, BROTLI_MIN_QUALITYcxx . . $ `c/include/brotli/encode.h:41:9`!, params->quality)); |
| 62 | if (params->qualitycxx . . $ BrotliEncoderParams#quality. <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODEScxx . . $ `c/enc/quality.h:22:9`!) { |
| 63 | params->large_windowcxx . . $ BrotliEncoderParams#large_window. = BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!; |
| 64 | } |
| 65 | if (params->lgwincxx . . $ BrotliEncoderParams#lgwin. < BROTLI_MIN_WINDOW_BITScxx . . $ `c/include/brotli/encode.h:24:9`!) { |
| 66 | params->lgwincxx . . $ BrotliEncoderParams#lgwin. = BROTLI_MIN_WINDOW_BITScxx . . $ `c/include/brotli/encode.h:24:9`!; |
| 67 | } else { |
| 68 | int max_lgwin = params->large_windowcxx . . $ BrotliEncoderParams#large_window. ? BROTLI_LARGE_MAX_WINDOW_BITScxx . . $ `c/include/brotli/encode.h:35:9`! : |
| 69 | BROTLI_MAX_WINDOW_BITScxx . . $ `c/include/brotli/encode.h:30:9`!; |
| 70 | if (params->lgwincxx . . $ BrotliEncoderParams#lgwin. > max_lgwin) params->lgwincxx . . $ BrotliEncoderParams#lgwin. = max_lgwin; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Returns optimized lg_block value. */ |
| 75 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! int ComputeLgBlockcxx . . $ ComputeLgBlock(6f0a176252fad017).(const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) { |
| 76 | int lgblock = params->lgblockcxx . . $ BrotliEncoderParams#lgblock.; |
| 77 | if (params->qualitycxx . . $ BrotliEncoderParams#quality. == FAST_ONE_PASS_COMPRESSION_QUALITYcxx . . $ `c/enc/quality.h:17:9`! || |
| 78 | params->qualitycxx . . $ BrotliEncoderParams#quality. == FAST_TWO_PASS_COMPRESSION_QUALITYcxx . . $ `c/enc/quality.h:18:9`!) { |
| 79 | lgblock = params->lgwincxx . . $ BrotliEncoderParams#lgwin.; |
| 80 | } else if (params->qualitycxx . . $ BrotliEncoderParams#quality. < MIN_QUALITY_FOR_BLOCK_SPLITcxx . . $ `c/enc/quality.h:23:9`!) { |
| 81 | lgblock = 14; |
| 82 | } else if (lgblock == 0) { |
| 83 | lgblock = 16; |
| 84 | if (params->qualitycxx . . $ BrotliEncoderParams#quality. >= 9 && params->lgwincxx . . $ BrotliEncoderParams#lgwin. > lgblock) { |
| 85 | lgblock = BROTLI_MINcxx . . $ `c/common/platform.h:522:9`!(int, 18, params->lgwin); |
| 86 | } |
| 87 | } else { |
| 88 | lgblock = BROTLI_MINcxx . . $ `c/common/platform.h:522:9`!(int, BROTLI_MAX_INPUT_BLOCK_BITScxx . . $ `c/include/brotli/encode.h:39:9`!, |
| 89 | BROTLI_MAXcxx . . $ `c/common/platform.h:523:9`!(int, BROTLI_MIN_INPUT_BLOCK_BITScxx . . $ `c/include/brotli/encode.h:37:9`!, 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_INLINEcxx . . $ `c/common/platform.h:119:9`! int ComputeRbBitscxx . . $ ComputeRbBits(6f0a176252fad017).(const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) { |
| 100 | return 1 + BROTLI_MAXcxx . . $ `c/common/platform.h:523:9`!(int, params->lgwin, params->lgblock); |
| 101 | } |
| 102 | |
| 103 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t MaxMetablockSizecxx . . $ MaxMetablockSize(76b5aa81b68a1b65).( |
| 104 | const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) { |
| 105 | int bits = |
| 106 | BROTLI_MINcxx . . $ `c/common/platform.h:522:9`!(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITScxx . . $ `c/include/brotli/encode.h:39:9`!); |
| 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_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t LiteralSpreeLengthForSparseSearchcxx . . $ LiteralSpreeLengthForSparseSearch(76b5aa81b68a1b65).( |
| 117 | const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) { |
| 118 | return params->qualitycxx . . $ BrotliEncoderParams#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_INLINEcxx . . $ `c/common/platform.h:119:9`! void ChooseHashercxx . . $ ChooseHasher(59aa9b7ae637a74c).(const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params, |
| 157 | BrotliHasherParamscxx . . $ BrotliHasherParams#* hparams) { |
| 158 | if (params->qualitycxx . . $ BrotliEncoderParams#quality. > 9) { |
| 159 | hparams->typecxx . . $ BrotliHasherParams#type. = 10; |
| 160 | } else if (params->qualitycxx . . $ BrotliEncoderParams#quality. == 4 && params->size_hintcxx . . $ BrotliEncoderParams#size_hint. >= (1 << 20)) { |
| 161 | hparams->typecxx . . $ BrotliHasherParams#type. = 54; |
| 162 | } else if (params->qualitycxx . . $ BrotliEncoderParams#quality. < 5) { |
| 163 | hparams->typecxx . . $ BrotliHasherParams#type. = params->qualitycxx . . $ BrotliEncoderParams#quality.; |
| 164 | } else if (params->lgwincxx . . $ BrotliEncoderParams#lgwin. <= 16) { |
| 165 | hparams->typecxx . . $ BrotliHasherParams#type. = params->qualitycxx . . $ BrotliEncoderParams#quality. < 7 ? 40 : params->qualitycxx . . $ BrotliEncoderParams#quality. < 9 ? 41 : 42; |
| 166 | } else if (params->size_hintcxx . . $ BrotliEncoderParams#size_hint. >= (1 << 20) && params->lgwincxx . . $ BrotliEncoderParams#lgwin. >= 19) { |
| 167 | #if defined(BROTLI_MAX_SIMD_QUALITYcxx . . $ `c/common/platform.h:655:9`!) |
| 168 | hparams->typecxx . . $ BrotliHasherParams#type. = params->qualitycxx . . $ BrotliEncoderParams#quality. <= BROTLI_MAX_SIMD_QUALITYcxx . . $ `c/common/platform.h:655:9`! ? 68 : 6; |
| 169 | #else |
| 170 | hparams->type = 6; |
| 171 | #endif |
| 172 | hparams->block_bitscxx . . $ BrotliHasherParams#block_bits. = params->qualitycxx . . $ BrotliEncoderParams#quality. - 1; |
| 173 | hparams->bucket_bitscxx . . $ BrotliHasherParams#bucket_bits. = 15; |
| 174 | hparams->num_last_distances_to_checkcxx . . $ BrotliHasherParams#num_last_distances_to_check. = |
| 175 | params->qualitycxx . . $ BrotliEncoderParams#quality. < 7 ? 4 : params->qualitycxx . . $ BrotliEncoderParams#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_QUALITYcxx . . $ `c/common/platform.h:655:9`!) |
| 180 | hparams->typecxx . . $ BrotliHasherParams#type. = params->qualitycxx . . $ BrotliEncoderParams#quality. <= BROTLI_MAX_SIMD_QUALITYcxx . . $ `c/common/platform.h:655:9`! ? 58 : 5; |
| 181 | #else |
| 182 | hparams->type = 5; |
| 183 | #endif |
| 184 | hparams->block_bitscxx . . $ BrotliHasherParams#block_bits. = params->qualitycxx . . $ BrotliEncoderParams#quality. - 1; |
| 185 | hparams->bucket_bitscxx . . $ BrotliHasherParams#bucket_bits. = params->qualitycxx . . $ BrotliEncoderParams#quality. < 7 ? 14 : 15; |
| 186 | hparams->num_last_distances_to_checkcxx . . $ BrotliHasherParams#num_last_distances_to_check. = |
| 187 | params->qualitycxx . . $ BrotliEncoderParams#quality. < 7 ? 4 : params->qualitycxx . . $ BrotliEncoderParams#quality. < 9 ? 10 : 16; |
| 188 | } |
| 189 | |
| 190 | if (params->lgwincxx . . $ BrotliEncoderParams#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->typecxx . . $ BrotliHasherParams#type. == 3) { |
| 198 | hparams->typecxx . . $ BrotliHasherParams#type. = 35; |
| 199 | } |
| 200 | if (hparams->typecxx . . $ BrotliHasherParams#type. == 54) { |
| 201 | hparams->typecxx . . $ BrotliHasherParams#type. = 55; |
| 202 | } |
| 203 | if (hparams->typecxx . . $ BrotliHasherParams#type. == 6 || hparams->typecxx . . $ BrotliHasherParams#type. == 68) { |
| 204 | hparams->typecxx . . $ BrotliHasherParams#type. = 65; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | #endif /* BROTLI_ENC_QUALITY_H_ */ |