Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/brotli/c/enc/quality.h
Line
Count
Source
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
0
#define FAST_ONE_PASS_COMPRESSION_QUALITY 0
18
0
#define FAST_TWO_PASS_COMPRESSION_QUALITY 1
19
0
#define ZOPFLIFICATION_QUALITY 10
20
0
#define HQ_ZOPFLIFICATION_QUALITY 11
21
22
0
#define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2
23
0
#define MIN_QUALITY_FOR_BLOCK_SPLIT 4
24
0
#define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4
25
0
#define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4
26
0
#define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5
27
0
#define MIN_QUALITY_FOR_CONTEXT_MODELING 5
28
0
#define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7
29
0
#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
0
static BROTLI_INLINE size_t MaxHashTableSize(int quality) {
37
0
  return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17;
38
0
}
Unexecuted instantiation: encode.c:MaxHashTableSize
Unexecuted instantiation: encoder_dict.c:MaxHashTableSize
Unexecuted instantiation: metablock.c:MaxHashTableSize
Unexecuted instantiation: backward_references.c:MaxHashTableSize
Unexecuted instantiation: backward_references_hq.c:MaxHashTableSize
Unexecuted instantiation: block_splitter.c:MaxHashTableSize
Unexecuted instantiation: brotli_bit_stream.c:MaxHashTableSize
Unexecuted instantiation: compress_fragment.c:MaxHashTableSize
Unexecuted instantiation: compress_fragment_two_pass.c:MaxHashTableSize
39
40
/* The maximum length for which the zopflification uses distinct distances. */
41
0
#define MAX_ZOPFLI_LEN_QUALITY_10 150
42
0
#define MAX_ZOPFLI_LEN_QUALITY_11 325
43
44
/* Do not thoroughly search when a long copy is found. */
45
0
#define BROTLI_LONG_COPY_QUICK_STEP 16384
46
47
0
static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) {
48
0
  return params->quality <= 10 ?
49
0
      MAX_ZOPFLI_LEN_QUALITY_10 :
50
0
      MAX_ZOPFLI_LEN_QUALITY_11;
51
0
}
Unexecuted instantiation: encode.c:MaxZopfliLen
Unexecuted instantiation: encoder_dict.c:MaxZopfliLen
Unexecuted instantiation: metablock.c:MaxZopfliLen
Unexecuted instantiation: backward_references.c:MaxZopfliLen
Unexecuted instantiation: backward_references_hq.c:MaxZopfliLen
Unexecuted instantiation: block_splitter.c:MaxZopfliLen
Unexecuted instantiation: brotli_bit_stream.c:MaxZopfliLen
Unexecuted instantiation: compress_fragment.c:MaxZopfliLen
Unexecuted instantiation: compress_fragment_two_pass.c:MaxZopfliLen
52
53
/* Number of best candidates to evaluate to expand Zopfli chain. */
54
static BROTLI_INLINE size_t MaxZopfliCandidates(
55
0
  const BrotliEncoderParams* params) {
56
0
  return params->quality <= 10 ? 1 : 5;
57
0
}
Unexecuted instantiation: encode.c:MaxZopfliCandidates
Unexecuted instantiation: encoder_dict.c:MaxZopfliCandidates
Unexecuted instantiation: metablock.c:MaxZopfliCandidates
Unexecuted instantiation: backward_references.c:MaxZopfliCandidates
Unexecuted instantiation: backward_references_hq.c:MaxZopfliCandidates
Unexecuted instantiation: block_splitter.c:MaxZopfliCandidates
Unexecuted instantiation: brotli_bit_stream.c:MaxZopfliCandidates
Unexecuted instantiation: compress_fragment.c:MaxZopfliCandidates
Unexecuted instantiation: compress_fragment_two_pass.c:MaxZopfliCandidates
58
59
0
static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) {
60
0
  params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY,
61
0
      BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality));
62
0
  if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) {
63
0
    params->large_window = BROTLI_FALSE;
64
0
  }
65
0
  if (params->lgwin < BROTLI_MIN_WINDOW_BITS) {
66
0
    params->lgwin = BROTLI_MIN_WINDOW_BITS;
67
0
  } else {
68
0
    int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS :
69
0
                                           BROTLI_MAX_WINDOW_BITS;
70
0
    if (params->lgwin > max_lgwin) params->lgwin = max_lgwin;
71
0
  }
72
0
}
Unexecuted instantiation: encode.c:SanitizeParams
Unexecuted instantiation: encoder_dict.c:SanitizeParams
Unexecuted instantiation: metablock.c:SanitizeParams
Unexecuted instantiation: backward_references.c:SanitizeParams
Unexecuted instantiation: backward_references_hq.c:SanitizeParams
Unexecuted instantiation: block_splitter.c:SanitizeParams
Unexecuted instantiation: brotli_bit_stream.c:SanitizeParams
Unexecuted instantiation: compress_fragment.c:SanitizeParams
Unexecuted instantiation: compress_fragment_two_pass.c:SanitizeParams
73
74
/* Returns optimized lg_block value. */
75
0
static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) {
76
0
  int lgblock = params->lgblock;
77
0
  if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
78
0
      params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
79
0
    lgblock = params->lgwin;
80
0
  } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
81
0
    lgblock = 14;
82
0
  } else if (lgblock == 0) {
83
0
    lgblock = 16;
84
0
    if (params->quality >= 9 && params->lgwin > lgblock) {
85
0
      lgblock = BROTLI_MIN(int, 18, params->lgwin);
86
0
    }
87
0
  } else {
88
0
    lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS,
89
0
        BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock));
90
0
  }
91
0
  return lgblock;
92
0
}
Unexecuted instantiation: encode.c:ComputeLgBlock
Unexecuted instantiation: encoder_dict.c:ComputeLgBlock
Unexecuted instantiation: metablock.c:ComputeLgBlock
Unexecuted instantiation: backward_references.c:ComputeLgBlock
Unexecuted instantiation: backward_references_hq.c:ComputeLgBlock
Unexecuted instantiation: block_splitter.c:ComputeLgBlock
Unexecuted instantiation: brotli_bit_stream.c:ComputeLgBlock
Unexecuted instantiation: compress_fragment.c:ComputeLgBlock
Unexecuted instantiation: compress_fragment_two_pass.c:ComputeLgBlock
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
0
static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) {
100
0
  return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock);
101
0
}
Unexecuted instantiation: encode.c:ComputeRbBits
Unexecuted instantiation: encoder_dict.c:ComputeRbBits
Unexecuted instantiation: metablock.c:ComputeRbBits
Unexecuted instantiation: backward_references.c:ComputeRbBits
Unexecuted instantiation: backward_references_hq.c:ComputeRbBits
Unexecuted instantiation: block_splitter.c:ComputeRbBits
Unexecuted instantiation: brotli_bit_stream.c:ComputeRbBits
Unexecuted instantiation: compress_fragment.c:ComputeRbBits
Unexecuted instantiation: compress_fragment_two_pass.c:ComputeRbBits
102
103
static BROTLI_INLINE size_t MaxMetablockSize(
104
0
    const BrotliEncoderParams* params) {
105
0
  int bits =
106
0
      BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS);
107
0
  return (size_t)1 << bits;
108
0
}
Unexecuted instantiation: encode.c:MaxMetablockSize
Unexecuted instantiation: encoder_dict.c:MaxMetablockSize
Unexecuted instantiation: metablock.c:MaxMetablockSize
Unexecuted instantiation: backward_references.c:MaxMetablockSize
Unexecuted instantiation: backward_references_hq.c:MaxMetablockSize
Unexecuted instantiation: block_splitter.c:MaxMetablockSize
Unexecuted instantiation: brotli_bit_stream.c:MaxMetablockSize
Unexecuted instantiation: compress_fragment.c:MaxMetablockSize
Unexecuted instantiation: compress_fragment_two_pass.c:MaxMetablockSize
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
0
    const BrotliEncoderParams* params) {
118
0
  return params->quality < 9 ? 64 : 512;
119
0
}
Unexecuted instantiation: encode.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: encoder_dict.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: metablock.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: backward_references.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: backward_references_hq.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: block_splitter.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: brotli_bit_stream.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: compress_fragment.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: compress_fragment_two_pass.c:LiteralSpreeLengthForSparseSearch
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: h59 (longest_match_simd_opt), 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: h59 (longest_match_simd_opt), b14, l4
137
   - q06: h68 (longest_match64_simd ), b15, l5 | for large files
138
   - q06: h40 (forgetful_chain      ), b15, l4 | for small window
139
140
   - q07: h58 (longest_match_simd   ), b15, l4
141
   - q07: h59 (longest_match_simd_opt), b15, l4
142
   - q07: h68 (longest_match64_simd ), b15, l5 | for large files
143
   - q07: h41 (forgetful_chain      ), b15, l4 | for small window
144
145
   - q08: h05 (longest_match        ), b15, l4
146
   - q08: h06 (longest_match64      ), b15, l5 | for large files
147
   - q08: h41 (forgetful_chain      ), b15, l4 | for small window
148
149
   - q09: h05 (longest_match        ), b15, l4
150
   - q09: h06 (longest_match64      ), b15, l5 | for large files
151
   - q09: h42 (forgetful_chain      ), b15, l4 | for small window
152
153
   - q10: t10 (to_binary_tree       ), b17, l128
154
155
   - q11: t10 (to_binary_tree       ), b17, l128
156
157
  Where "q" is quality, "h" is hasher type, "b" is bucket bits,
158
  "l" is source len. */
159
160
#if defined(BROTLI_MAX_SIMD_QUALITY)
161
static BROTLI_INLINE BROTLI_BOOL
162
0
ShouldUseSimdHasher(const BrotliEncoderParams* params) {
163
0
  int max_quality;
164
0
  if (params->simd_hasher == BROTLI_SIMD_HASHER_DISABLE) {
165
0
    return BROTLI_FALSE;
166
0
  }
167
0
  max_quality = BROTLI_MAX_RECOMMENDED_SIMD_QUALITY;
168
169
0
  if (params->simd_hasher == BROTLI_SIMD_HASHER_ENABLE) {
170
0
    max_quality = BROTLI_MAX_SIMD_QUALITY;
171
0
  }
172
0
  return TO_BROTLI_BOOL(params->quality <= max_quality);
173
0
}
Unexecuted instantiation: encode.c:ShouldUseSimdHasher
Unexecuted instantiation: encoder_dict.c:ShouldUseSimdHasher
Unexecuted instantiation: metablock.c:ShouldUseSimdHasher
Unexecuted instantiation: backward_references.c:ShouldUseSimdHasher
Unexecuted instantiation: backward_references_hq.c:ShouldUseSimdHasher
Unexecuted instantiation: block_splitter.c:ShouldUseSimdHasher
Unexecuted instantiation: brotli_bit_stream.c:ShouldUseSimdHasher
Unexecuted instantiation: compress_fragment.c:ShouldUseSimdHasher
Unexecuted instantiation: compress_fragment_two_pass.c:ShouldUseSimdHasher
174
#endif
175
176
static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
177
0
                                       BrotliHasherParams* hparams) {
178
0
  if (params->quality > 9) {
179
0
    hparams->type = 10;
180
0
  } else if (params->quality == 4 && params->size_hint >= (1 << 20)) {
181
0
    hparams->type = 54;
182
0
  } else if (params->quality < 5) {
183
0
    hparams->type = params->quality;
184
0
  } else if (params->lgwin <= 16) {
185
0
    hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42;
186
0
  } else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) {
187
0
#if defined(BROTLI_MAX_SIMD_QUALITY)
188
0
    hparams->type = ShouldUseSimdHasher(params) ? 68 : 6;
189
#else
190
    hparams->type = 6;
191
#endif
192
0
    hparams->block_bits = params->quality - 1;
193
0
    hparams->bucket_bits = 15;
194
0
    hparams->num_last_distances_to_check =
195
0
        params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
196
0
  } else {
197
    /* TODO(eustas): often previous setting (H6) is faster and denser; consider
198
                     adding an option to use it. */
199
0
#if defined(BROTLI_MAX_SIMD_QUALITY)
200
0
    hparams->type =
201
0
        ShouldUseSimdHasher(params) ? (params->hasher_opt ? 59 : 58) : 5;
202
#else
203
    hparams->type = 5;
204
#endif
205
0
    hparams->block_bits = params->quality - 1;
206
0
    hparams->bucket_bits = params->quality < 7 ? 14 : 15;
207
0
    hparams->num_last_distances_to_check =
208
0
        params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
209
0
  }
210
211
0
  if (params->lgwin > 24) {
212
    /* Different hashers for large window brotli: not for qualities <= 2,
213
       these are too fast for large window. Not for qualities >= 10: their
214
       hasher already works well with large window. So the changes are:
215
       H3 --> H35: for quality 3.
216
       H54 --> H55: for quality 4 with size hint > 1MB
217
       H6/H68 --> H65: for qualities 5, 6, 7, 8, 9. */
218
0
    if (hparams->type == 3) {
219
0
      hparams->type = 35;
220
0
    }
221
0
    if (hparams->type == 54) {
222
0
      hparams->type = 55;
223
0
    }
224
0
    if (hparams->type == 6 || hparams->type == 68) {
225
0
      hparams->type = 65;
226
0
    }
227
0
  }
228
0
}
Unexecuted instantiation: encode.c:ChooseHasher
Unexecuted instantiation: encoder_dict.c:ChooseHasher
Unexecuted instantiation: metablock.c:ChooseHasher
Unexecuted instantiation: backward_references.c:ChooseHasher
Unexecuted instantiation: backward_references_hq.c:ChooseHasher
Unexecuted instantiation: block_splitter.c:ChooseHasher
Unexecuted instantiation: brotli_bit_stream.c:ChooseHasher
Unexecuted instantiation: compress_fragment.c:ChooseHasher
Unexecuted instantiation: compress_fragment_two_pass.c:ChooseHasher
229
230
#endif  /* BROTLI_ENC_QUALITY_H_ */