Coverage Report

Created: 2026-10-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/enc_params.h
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#ifndef LIB_JXL_ENC_PARAMS_H_
7
#define LIB_JXL_ENC_PARAMS_H_
8
9
// Parameters and flags that govern JXL compression.
10
11
#include <jxl/cms_interface.h>
12
#include <jxl/encode.h>
13
#include <stddef.h>
14
15
#include <vector>
16
17
#include "lib/jxl/base/override.h"
18
#include "lib/jxl/common.h"
19
#include "lib/jxl/enc_progressive_split.h"
20
#include "lib/jxl/frame_dimensions.h"
21
#include "lib/jxl/frame_header.h"
22
#include "lib/jxl/modular/encoding/dec_ma.h"
23
#include "lib/jxl/modular/options.h"
24
#include "lib/jxl/splines.h"
25
26
namespace jxl {
27
28
// NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding)
29
struct CompressParams {
30
  float butteraugli_distance = 1.0f;
31
32
  // explicit distances for extra channels (defaults to butteraugli_distance
33
  // when not set; value of -1 can be used to represent 'default')
34
  std::vector<float> ec_distance;
35
36
  // Try to achieve a maximum pixel-by-pixel error on each channel.
37
  bool max_error_mode = false;
38
  float max_error[3] = {0.0, 0.0, 0.0};
39
40
  bool disable_perceptual_optimizations = false;
41
42
  SpeedTier speed_tier = SpeedTier::kSquirrel;
43
  int brotli_effort = -1;
44
45
  // 0 = default.
46
  // 1 = slightly worse quality.
47
  // 4 = fastest speed, lowest quality
48
  size_t decoding_speed_tier = 0;
49
50
  ColorTransform color_transform = ColorTransform::kXYB;
51
52
  // If true, the "modular mode options" members below are used.
53
  bool modular_mode = false;
54
55
  // Change group size in modular mode (0=128, 1=256, 2=512, 3=1024, -1=encoder
56
  // chooses).
57
  int modular_group_size_shift = -1;
58
59
  Override preview = Override::kDefault;
60
  Override noise = Override::kDefault;
61
  Override dots = Override::kDefault;
62
  Override patches = Override::kDefault;
63
  Override gaborish = Override::kDefault;
64
  int epf = -1;
65
66
  // Progressive mode.
67
  Override progressive_mode = Override::kDefault;
68
69
  // Quantized-progressive mode.
70
  Override qprogressive_mode = Override::kDefault;
71
72
  // Put center groups first in the bitstream.
73
  bool centerfirst = false;
74
75
  // Pixel coordinates of the center. First group will contain that center.
76
  size_t center_x = static_cast<size_t>(-1);
77
  size_t center_y = static_cast<size_t>(-1);
78
79
  int progressive_dc = -1;
80
81
  // If on: preserve color of invisible pixels (if off: don't care)
82
  // Default: on
83
  Override keep_invisible = Override::kDefault;
84
85
  JxlCmsInterface cms;
86
  bool cms_set = false;
87
0
  void SetCms(const JxlCmsInterface& new_cms) {
88
0
    cms = new_cms;
89
0
    cms_set = true;
90
0
  }
91
92
  // Force usage of CfL when doing JPEG recompression. This can have unexpected
93
  // effects on the decoded pixels, while still being JPEG-compliant and
94
  // allowing reconstruction of the original JPEG.
95
  bool force_cfl_jpeg_recompression = true;
96
  // Apply LF Smoothing when doing JPEG recompression. Same applies as above.
97
  // Default to non-subsampled smoothing only until decoders update.
98
  int force_lfs_jpeg_recompression = -1;
99
100
  // Use brotli compression for any boxes derived from a JPEG frame.
101
  bool jpeg_compress_boxes = true;
102
103
  // Preserve this metadata when doing JPEG recompression.
104
  bool jpeg_keep_exif = true;
105
  bool jpeg_keep_xmp = true;
106
  bool jpeg_keep_jumbf = true;
107
108
  // Set the noise to what it would approximately be if shooting at the nominal
109
  // exposure for a given ISO setting on a 35mm camera.
110
  float photon_noise_iso = 0;
111
112
  // modular mode options below
113
  ModularOptions options;
114
115
  // TODO(eustas): use Override?
116
  int responsive = -1;
117
  int colorspace = -1;
118
  int move_to_front_from_channel = -1;
119
120
  // Use Global channel palette if #colors < this percentage of range
121
  float channel_colors_pre_transform_percent = 95.f;
122
  // Use Local channel palette if #colors < this percentage of range
123
  float channel_colors_percent = 80.f;
124
  int palette_colors = 1 << 10;  // up to 10-bit palette is probably worthwhile
125
  bool lossy_palette = false;
126
127
  // Returns whether these params are lossless as defined by SetLossless();
128
0
  bool IsLossless() const { return modular_mode && ModularPartIsLossless(); }
129
130
0
  bool ModularPartIsLossless() const {
131
0
    if (modular_mode) {
132
      // YCbCr is also considered lossless here since it's intended for
133
      // source material that is already YCbCr (we don't do the fwd transform)
134
0
      if (butteraugli_distance != 0 ||
135
0
          color_transform == jxl::ColorTransform::kXYB)
136
0
        return false;
137
0
    }
138
0
    for (float f : ec_distance) {
139
0
      if (f > 0) return false;
140
0
      if (f < 0 && butteraugli_distance != 0) return false;
141
0
    }
142
    // all modular channels are encoded at distance 0
143
0
    return true;
144
0
  }
145
146
  // Sets the parameters required to make the codec lossless.
147
0
  void SetLossless() {
148
0
    modular_mode = true;
149
0
    butteraugli_distance = 0.0f;
150
0
    for (float& f : ec_distance) f = 0.0f;
151
0
    color_transform = jxl::ColorTransform::kNone;
152
0
  }
153
154
  // Down/upsample the image before encoding / after decoding by this factor.
155
  // The resampling value can also be set to <= 0 to automatically choose based
156
  // on distance, however EncodeFrame doesn't support this, so it is
157
  // required to call PostInit() to set a valid positive resampling
158
  // value and altered butteraugli score if this is used.
159
  int resampling = -1;
160
  int ec_resampling = -1;
161
  // Skip the downsampling before encoding if this is true.
162
  bool already_downsampled = false;
163
  // Butteraugli target distance on the original full size image, this can be
164
  // different from butteraugli_distance if resampling was used.
165
  float original_butteraugli_distance = -1.0f;
166
167
  float quant_ac_rescale = 1.0;
168
169
  // Codestream level to conform to.
170
  // -1: don't care
171
  int level = -1;
172
173
  // See JXL_ENC_FRAME_SETTING_BUFFERING option value.
174
  int buffering = -1;
175
  // Output streaming mode: 0=buffered, 1=seek-based streaming, 2=OOO jxlp.
176
  int output_mode = 0;
177
  // See JXL_ENC_FRAME_SETTING_USE_FULL_IMAGE_HEURISTICS option value.
178
  bool use_full_image_heuristics = true;
179
180
  std::vector<float> manual_noise;
181
  std::vector<float> manual_xyb_factors;
182
183
  // If not empty, this tree will be used for dc global section.
184
  // Used in jxl_from_tree tool.
185
  Tree custom_fixed_tree;
186
  // If not empty, these custom splines will be used instead of the computed
187
  // ones. Used in jxl_from_tee tool.
188
  SplineDataView custom_splines{};
189
  // If not null, overrides progressive mode settings. Used in decode_test.
190
  const ProgressiveMode* custom_progressive_mode = nullptr;
191
192
  JxlDebugImageCallback debug_image = nullptr;
193
  void* debug_image_opaque;
194
};
195
196
static constexpr float kMinButteraugliForDynamicAR = 0.5f;
197
static constexpr float kMinButteraugliForDots = 3.0f;
198
static constexpr float kMinButteraugliToSubtractOriginalPatches = 3.0f;
199
200
// Always off
201
static constexpr float kMinButteraugliForNoise = 99.0f;
202
203
// Minimum butteraugli distance the encoder accepts.
204
// Below d0.05 is not useful and risks going outside Level 5 limits
205
// (in particular modular_16bit_buffers becomes an issue for DC)
206
static constexpr float kMinButteraugliDistance = 0.05f;
207
208
// Tile size for encoder-side processing. Must be equal to color tile dim in the
209
// current implementation.
210
static constexpr size_t kEncTileDim = 64;
211
static constexpr size_t kEncTileDimInBlocks = kEncTileDim / kBlockDim;
212
213
}  // namespace jxl
214
215
#endif  // LIB_JXL_ENC_PARAMS_H_