Coverage Report

Created: 2025-07-11 06:26

/src/h2o/deps/brotli/c/enc/quality.h
Line
Count
Source (jump to first uncovered line)
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
#include "./port.h"
15
16
0
#define FAST_ONE_PASS_COMPRESSION_QUALITY 0
17
0
#define FAST_TWO_PASS_COMPRESSION_QUALITY 1
18
0
#define ZOPFLIFICATION_QUALITY 10
19
0
#define HQ_ZOPFLIFICATION_QUALITY 11
20
21
0
#define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2
22
0
#define MIN_QUALITY_FOR_BLOCK_SPLIT 4
23
0
#define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4
24
0
#define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5
25
0
#define MIN_QUALITY_FOR_CONTEXT_MODELING 5
26
0
#define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7
27
0
#define MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING 10
28
/* Only for "font" mode. */
29
0
#define MIN_QUALITY_FOR_RECOMPUTE_DISTANCE_PREFIXES 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
typedef struct BrotliHasherParams {
36
  int type;
37
  int bucket_bits;
38
  int block_bits;
39
  int hash_len;
40
  int num_last_distances_to_check;
41
} BrotliHasherParams;
42
43
/* Encoding parameters */
44
typedef struct BrotliEncoderParams {
45
  BrotliEncoderMode mode;
46
  int quality;
47
  int lgwin;
48
  int lgblock;
49
  size_t size_hint;
50
  BROTLI_BOOL disable_literal_context_modeling;
51
  BrotliHasherParams hasher;
52
} BrotliEncoderParams;
53
54
/* Returns hash-table size for quality levels 0 and 1. */
55
0
static BROTLI_INLINE size_t MaxHashTableSize(int quality) {
56
0
  return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17;
57
0
}
Unexecuted instantiation: backward_references.c:MaxHashTableSize
Unexecuted instantiation: backward_references_hq.c:MaxHashTableSize
Unexecuted instantiation: bit_cost.c:MaxHashTableSize
Unexecuted instantiation: block_splitter.c:MaxHashTableSize
Unexecuted instantiation: brotli_bit_stream.c:MaxHashTableSize
Unexecuted instantiation: cluster.c:MaxHashTableSize
Unexecuted instantiation: compress_fragment.c:MaxHashTableSize
Unexecuted instantiation: compress_fragment_two_pass.c:MaxHashTableSize
Unexecuted instantiation: encode.c:MaxHashTableSize
Unexecuted instantiation: histogram.c:MaxHashTableSize
Unexecuted instantiation: metablock.c:MaxHashTableSize
58
59
/* The maximum length for which the zopflification uses distinct distances. */
60
0
#define MAX_ZOPFLI_LEN_QUALITY_10 150
61
0
#define MAX_ZOPFLI_LEN_QUALITY_11 325
62
63
/* Do not thoroughly search when a long copy is found. */
64
0
#define BROTLI_LONG_COPY_QUICK_STEP 16384
65
66
0
static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) {
67
0
  return params->quality <= 10 ?
68
0
      MAX_ZOPFLI_LEN_QUALITY_10 :
69
0
      MAX_ZOPFLI_LEN_QUALITY_11;
70
0
}
Unexecuted instantiation: backward_references.c:MaxZopfliLen
Unexecuted instantiation: backward_references_hq.c:MaxZopfliLen
Unexecuted instantiation: bit_cost.c:MaxZopfliLen
Unexecuted instantiation: block_splitter.c:MaxZopfliLen
Unexecuted instantiation: brotli_bit_stream.c:MaxZopfliLen
Unexecuted instantiation: cluster.c:MaxZopfliLen
Unexecuted instantiation: compress_fragment.c:MaxZopfliLen
Unexecuted instantiation: compress_fragment_two_pass.c:MaxZopfliLen
Unexecuted instantiation: encode.c:MaxZopfliLen
Unexecuted instantiation: histogram.c:MaxZopfliLen
Unexecuted instantiation: metablock.c:MaxZopfliLen
71
72
/* Number of best candidates to evaluate to expand Zopfli chain. */
73
static BROTLI_INLINE size_t MaxZopfliCandidates(
74
0
  const BrotliEncoderParams* params) {
75
0
  return params->quality <= 10 ? 1 : 5;
76
0
}
Unexecuted instantiation: backward_references.c:MaxZopfliCandidates
Unexecuted instantiation: backward_references_hq.c:MaxZopfliCandidates
Unexecuted instantiation: bit_cost.c:MaxZopfliCandidates
Unexecuted instantiation: block_splitter.c:MaxZopfliCandidates
Unexecuted instantiation: brotli_bit_stream.c:MaxZopfliCandidates
Unexecuted instantiation: cluster.c:MaxZopfliCandidates
Unexecuted instantiation: compress_fragment.c:MaxZopfliCandidates
Unexecuted instantiation: compress_fragment_two_pass.c:MaxZopfliCandidates
Unexecuted instantiation: encode.c:MaxZopfliCandidates
Unexecuted instantiation: histogram.c:MaxZopfliCandidates
Unexecuted instantiation: metablock.c:MaxZopfliCandidates
77
78
0
static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) {
79
0
  params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY,
80
0
      BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality));
81
0
  if (params->lgwin < BROTLI_MIN_WINDOW_BITS) {
82
0
    params->lgwin = BROTLI_MIN_WINDOW_BITS;
83
0
  } else if (params->lgwin > BROTLI_MAX_WINDOW_BITS) {
84
0
    params->lgwin = BROTLI_MAX_WINDOW_BITS;
85
0
  }
86
0
}
Unexecuted instantiation: backward_references.c:SanitizeParams
Unexecuted instantiation: backward_references_hq.c:SanitizeParams
Unexecuted instantiation: bit_cost.c:SanitizeParams
Unexecuted instantiation: block_splitter.c:SanitizeParams
Unexecuted instantiation: brotli_bit_stream.c:SanitizeParams
Unexecuted instantiation: cluster.c:SanitizeParams
Unexecuted instantiation: compress_fragment.c:SanitizeParams
Unexecuted instantiation: compress_fragment_two_pass.c:SanitizeParams
Unexecuted instantiation: encode.c:SanitizeParams
Unexecuted instantiation: histogram.c:SanitizeParams
Unexecuted instantiation: metablock.c:SanitizeParams
87
88
/* Returns optimized lg_block value. */
89
0
static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) {
90
0
  int lgblock = params->lgblock;
91
0
  if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
92
0
      params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
93
0
    lgblock = params->lgwin;
94
0
  } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
95
0
    lgblock = 14;
96
0
  } else if (lgblock == 0) {
97
0
    lgblock = 16;
98
0
    if (params->quality >= 9 && params->lgwin > lgblock) {
99
0
      lgblock = BROTLI_MIN(int, 18, params->lgwin);
100
0
    }
101
0
  } else {
102
0
    lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS,
103
0
        BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock));
104
0
  }
105
0
  return lgblock;
106
0
}
Unexecuted instantiation: backward_references.c:ComputeLgBlock
Unexecuted instantiation: backward_references_hq.c:ComputeLgBlock
Unexecuted instantiation: bit_cost.c:ComputeLgBlock
Unexecuted instantiation: block_splitter.c:ComputeLgBlock
Unexecuted instantiation: brotli_bit_stream.c:ComputeLgBlock
Unexecuted instantiation: cluster.c:ComputeLgBlock
Unexecuted instantiation: compress_fragment.c:ComputeLgBlock
Unexecuted instantiation: compress_fragment_two_pass.c:ComputeLgBlock
Unexecuted instantiation: encode.c:ComputeLgBlock
Unexecuted instantiation: histogram.c:ComputeLgBlock
Unexecuted instantiation: metablock.c:ComputeLgBlock
107
108
/* Returns log2 of the size of main ring buffer area.
109
   Allocate at least lgwin + 1 bits for the ring buffer so that the newly
110
   added block fits there completely and we still get lgwin bits and at least
111
   read_block_size_bits + 1 bits because the copy tail length needs to be
112
   smaller than ring-buffer size. */
113
0
static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) {
114
0
  return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock);
115
0
}
Unexecuted instantiation: backward_references.c:ComputeRbBits
Unexecuted instantiation: backward_references_hq.c:ComputeRbBits
Unexecuted instantiation: bit_cost.c:ComputeRbBits
Unexecuted instantiation: block_splitter.c:ComputeRbBits
Unexecuted instantiation: brotli_bit_stream.c:ComputeRbBits
Unexecuted instantiation: cluster.c:ComputeRbBits
Unexecuted instantiation: compress_fragment.c:ComputeRbBits
Unexecuted instantiation: compress_fragment_two_pass.c:ComputeRbBits
Unexecuted instantiation: encode.c:ComputeRbBits
Unexecuted instantiation: histogram.c:ComputeRbBits
Unexecuted instantiation: metablock.c:ComputeRbBits
116
117
static BROTLI_INLINE size_t MaxMetablockSize(
118
0
    const BrotliEncoderParams* params) {
119
0
  int bits =
120
0
      BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS);
121
0
  return (size_t)1 << bits;
122
0
}
Unexecuted instantiation: backward_references.c:MaxMetablockSize
Unexecuted instantiation: backward_references_hq.c:MaxMetablockSize
Unexecuted instantiation: bit_cost.c:MaxMetablockSize
Unexecuted instantiation: block_splitter.c:MaxMetablockSize
Unexecuted instantiation: brotli_bit_stream.c:MaxMetablockSize
Unexecuted instantiation: cluster.c:MaxMetablockSize
Unexecuted instantiation: compress_fragment.c:MaxMetablockSize
Unexecuted instantiation: compress_fragment_two_pass.c:MaxMetablockSize
Unexecuted instantiation: encode.c:MaxMetablockSize
Unexecuted instantiation: histogram.c:MaxMetablockSize
Unexecuted instantiation: metablock.c:MaxMetablockSize
123
124
/* When searching for backward references and have not seen matches for a long
125
   time, we can skip some match lookups. Unsuccessful match lookups are very
126
   expensive and this kind of a heuristic speeds up compression quite a lot.
127
   At first 8 byte strides are taken and every second byte is put to hasher.
128
   After 4x more literals stride by 16 bytes, every put 4-th byte to hasher.
129
   Applied only to qualities 2 to 9. */
130
static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch(
131
0
    const BrotliEncoderParams* params) {
132
0
  return params->quality < 9 ? 64 : 512;
133
0
}
Unexecuted instantiation: backward_references.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: backward_references_hq.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: bit_cost.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: block_splitter.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: brotli_bit_stream.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: cluster.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: compress_fragment.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: compress_fragment_two_pass.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: encode.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: histogram.c:LiteralSpreeLengthForSparseSearch
Unexecuted instantiation: metablock.c:LiteralSpreeLengthForSparseSearch
134
135
static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
136
0
                                       BrotliHasherParams* hparams) {
137
0
  if (params->quality > 9) {
138
0
    hparams->type = 10;
139
0
  } else if (params->quality == 4 && params->size_hint >= (1 << 20)) {
140
0
    hparams->type = 54;
141
0
  } else if (params->quality < 5) {
142
0
    hparams->type = params->quality;
143
0
  } else if (params->lgwin <= 16) {
144
0
    hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42;
145
0
  } else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) {
146
0
    hparams->type = 6;
147
0
    hparams->block_bits = params->quality - 1;
148
0
    hparams->bucket_bits = 15;
149
0
    hparams->hash_len = 5;
150
0
    hparams->num_last_distances_to_check =
151
0
        params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
152
0
  } else {
153
0
    hparams->type = 5;
154
0
    hparams->block_bits = params->quality - 1;
155
0
    hparams->bucket_bits = params->quality < 7 ? 14 : 15;
156
0
    hparams->num_last_distances_to_check =
157
0
        params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
158
0
  }
159
0
}
Unexecuted instantiation: backward_references.c:ChooseHasher
Unexecuted instantiation: backward_references_hq.c:ChooseHasher
Unexecuted instantiation: bit_cost.c:ChooseHasher
Unexecuted instantiation: block_splitter.c:ChooseHasher
Unexecuted instantiation: brotli_bit_stream.c:ChooseHasher
Unexecuted instantiation: cluster.c:ChooseHasher
Unexecuted instantiation: compress_fragment.c:ChooseHasher
Unexecuted instantiation: compress_fragment_two_pass.c:ChooseHasher
Unexecuted instantiation: encode.c:ChooseHasher
Unexecuted instantiation: histogram.c:ChooseHasher
Unexecuted instantiation: metablock.c:ChooseHasher
160
161
#endif  /* BROTLI_ENC_QUALITY_H_ */