OLD | NEW |
(Empty) | |
| 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 #define FAST_ONE_PASS_COMPRESSION_QUALITY 0 |
| 16 #define FAST_TWO_PASS_COMPRESSION_QUALITY 1 |
| 17 #define ZOPFLIFICATION_QUALITY 10 |
| 18 #define HQ_ZOPFLIFICATION_QUALITY 11 |
| 19 |
| 20 #define MAX_QUALITY_FOR_STATIC_ENRTOPY_CODES 2 |
| 21 #define MIN_QUALITY_FOR_BLOCK_SPLIT 4 |
| 22 #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4 |
| 23 #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5 |
| 24 #define MIN_QUALITY_FOR_CONTEXT_MODELING 5 |
| 25 #define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7 |
| 26 #define MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING 10 |
| 27 /* Only for "font" mode. */ |
| 28 #define MIN_QUALITY_FOR_RECOMPUTE_DISTANCE_PREFIXES 10 |
| 29 |
| 30 /* For quality below MIN_QUALITY_FOR_BLOCK_SPLIT there is no block splitting, |
| 31 so we buffer at most this much literals and commands. */ |
| 32 #define MAX_NUM_DELAYED_SYMBOLS 0x2fff |
| 33 |
| 34 /* Encoding parameters */ |
| 35 typedef struct BrotliEncoderParams { |
| 36 BrotliEncoderMode mode; |
| 37 int quality; |
| 38 int lgwin; |
| 39 int lgblock; |
| 40 } BrotliEncoderParams; |
| 41 |
| 42 /* Returns hash-table size for quality levels 0 and 1. */ |
| 43 static BROTLI_INLINE size_t MaxHashTableSize(int quality) { |
| 44 return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17; |
| 45 } |
| 46 |
| 47 /* The maximum length for which the zopflification uses distinct distances. */ |
| 48 #define MAX_ZOPFLI_LEN_QUALITY_10 150 |
| 49 #define MAX_ZOPFLI_LEN_QUALITY_11 325 |
| 50 |
| 51 /* Do not thoroughly search when a long copy is found. */ |
| 52 #define BROTLI_LONG_COPY_QUICK_STEP 16384 |
| 53 |
| 54 static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) { |
| 55 return params->quality <= 10 ? |
| 56 MAX_ZOPFLI_LEN_QUALITY_10 : |
| 57 MAX_ZOPFLI_LEN_QUALITY_11; |
| 58 } |
| 59 |
| 60 /* Number of best candidates to evaluate to expand Zopfli chain. */ |
| 61 static BROTLI_INLINE size_t MaxZopfliCandidates( |
| 62 const BrotliEncoderParams* params) { |
| 63 return params->quality <= 10 ? 1 : 5; |
| 64 } |
| 65 |
| 66 static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) { |
| 67 params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY, |
| 68 BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality)); |
| 69 if (params->lgwin < BROTLI_MIN_WINDOW_BITS) { |
| 70 params->lgwin = BROTLI_MIN_WINDOW_BITS; |
| 71 } else if (params->lgwin > BROTLI_MAX_WINDOW_BITS) { |
| 72 params->lgwin = BROTLI_MAX_WINDOW_BITS; |
| 73 } |
| 74 } |
| 75 |
| 76 /* Returns optimized lg_block value. */ |
| 77 static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) { |
| 78 int lgblock = params->lgblock; |
| 79 if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY || |
| 80 params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) { |
| 81 lgblock = params->lgwin; |
| 82 } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) { |
| 83 lgblock = 14; |
| 84 } else if (lgblock == 0) { |
| 85 lgblock = 16; |
| 86 if (params->quality >= 9 && params->lgwin > lgblock) { |
| 87 lgblock = BROTLI_MIN(int, 18, params->lgwin); |
| 88 } |
| 89 } else { |
| 90 lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS, |
| 91 BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock)); |
| 92 } |
| 93 return lgblock; |
| 94 } |
| 95 |
| 96 /* Returns log2 of the size of main ring buffer area. |
| 97 Allocate at least lgwin + 1 bits for the ring buffer so that the newly |
| 98 added block fits there completely and we still get lgwin bits and at least |
| 99 read_block_size_bits + 1 bits because the copy tail length needs to be |
| 100 smaller than ring-buffer size. */ |
| 101 static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) { |
| 102 return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock); |
| 103 } |
| 104 |
| 105 static BROTLI_INLINE size_t MaxMetablockSize( |
| 106 const BrotliEncoderParams* params) { |
| 107 int bits = |
| 108 BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS); |
| 109 return (size_t)1 << bits; |
| 110 } |
| 111 |
| 112 /* When searching for backward references and have not seen matches for a long |
| 113 time, we can skip some match lookups. Unsuccessful match lookups are very |
| 114 expensive and this kind of a heuristic speeds up compression quite a lot. |
| 115 At first 8 byte strides are taken and every second byte is put to hasher. |
| 116 After 4x more literals stride by 16 bytes, every put 4-th byte to hasher. |
| 117 Applied only to qualities 2 to 9. */ |
| 118 static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch( |
| 119 const BrotliEncoderParams* params) { |
| 120 return params->quality < 9 ? 64 : 512; |
| 121 } |
| 122 |
| 123 static BROTLI_INLINE int ChooseHasher(const BrotliEncoderParams* params) { |
| 124 if (params->quality > 9) { |
| 125 return 10; |
| 126 } else if (params->quality < 5) { |
| 127 return params->quality; |
| 128 } else if (params->lgwin <= 16) { |
| 129 return params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42; |
| 130 } |
| 131 return params->quality; |
| 132 } |
| 133 |
| 134 #endif /* BROTLI_ENC_QUALITY_H_ */ |
OLD | NEW |