Coverage Report

Created: 2025-06-16 07:00

/src/libjxl/lib/jxl/enc_params.h
Line
Count
Source (jump to first uncovered line)
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
97
  // Use brotli compression for any boxes derived from a JPEG frame.
98
  bool jpeg_compress_boxes = true;
99
100
  // Preserve this metadata when doing JPEG recompression.
101
  bool jpeg_keep_exif = true;
102
  bool jpeg_keep_xmp = true;
103
  bool jpeg_keep_jumbf = true;
104
105
  // Set the noise to what it would approximately be if shooting at the nominal
106
  // exposure for a given ISO setting on a 35mm camera.
107
  float photon_noise_iso = 0;
108
109
  // modular mode options below
110
  ModularOptions options;
111
112
  // TODO(eustas): use Override?
113
  int responsive = -1;
114
  int colorspace = -1;
115
  int move_to_front_from_channel = -1;
116
117
  // Use Global channel palette if #colors < this percentage of range
118
  float channel_colors_pre_transform_percent = 95.f;
119
  // Use Local channel palette if #colors < this percentage of range
120
  float channel_colors_percent = 80.f;
121
  int palette_colors = 1 << 10;  // up to 10-bit palette is probably worthwhile
122
  bool lossy_palette = false;
123
124
  // Returns whether these params are lossless as defined by SetLossless();
125
766
  bool IsLossless() const { return modular_mode && ModularPartIsLossless(); }
126
127
1.73k
  bool ModularPartIsLossless() const {
128
1.73k
    if (modular_mode) {
129
      // YCbCr is also considered lossless here since it's intended for
130
      // source material that is already YCbCr (we don't do the fwd transform)
131
776
      if (butteraugli_distance != 0 ||
132
776
          color_transform == jxl::ColorTransform::kXYB)
133
776
        return false;
134
776
    }
135
957
    for (float f : ec_distance) {
136
98
      if (f > 0) return false;
137
98
      if (f < 0 && butteraugli_distance != 0) return false;
138
98
    }
139
    // all modular channels are encoded at distance 0
140
957
    return true;
141
957
  }
142
143
  // Sets the parameters required to make the codec lossless.
144
0
  void SetLossless() {
145
0
    modular_mode = true;
146
0
    butteraugli_distance = 0.0f;
147
0
    for (float& f : ec_distance) f = 0.0f;
148
0
    color_transform = jxl::ColorTransform::kNone;
149
0
  }
150
151
  // Down/upsample the image before encoding / after decoding by this factor.
152
  // The resampling value can also be set to <= 0 to automatically choose based
153
  // on distance, however EncodeFrame doesn't support this, so it is
154
  // required to call PostInit() to set a valid positive resampling
155
  // value and altered butteraugli score if this is used.
156
  int resampling = -1;
157
  int ec_resampling = -1;
158
  // Skip the downsampling before encoding if this is true.
159
  bool already_downsampled = false;
160
  // Butteraugli target distance on the original full size image, this can be
161
  // different from butteraugli_distance if resampling was used.
162
  float original_butteraugli_distance = -1.0f;
163
164
  float quant_ac_rescale = 1.0;
165
166
  // Codestream level to conform to.
167
  // -1: don't care
168
  int level = -1;
169
170
  // See JXL_ENC_FRAME_SETTING_BUFFERING option value.
171
  int buffering = -1;
172
  // See JXL_ENC_FRAME_SETTING_USE_FULL_IMAGE_HEURISTICS option value.
173
  bool use_full_image_heuristics = true;
174
175
  std::vector<float> manual_noise;
176
  std::vector<float> manual_xyb_factors;
177
178
  // If not empty, this tree will be used for dc global section.
179
  // Used in jxl_from_tree tool.
180
  Tree custom_fixed_tree;
181
  // If not empty, these custom splines will be used instead of the computed
182
  // ones. Used in jxl_from_tee tool.
183
  Splines custom_splines;
184
  // If not null, overrides progressive mode settings. Used in decode_test.
185
  const ProgressiveMode* custom_progressive_mode = nullptr;
186
187
  JxlDebugImageCallback debug_image = nullptr;
188
  void* debug_image_opaque;
189
};
190
191
static constexpr float kMinButteraugliForDynamicAR = 0.5f;
192
static constexpr float kMinButteraugliForDots = 3.0f;
193
static constexpr float kMinButteraugliToSubtractOriginalPatches = 3.0f;
194
195
// Always off
196
static constexpr float kMinButteraugliForNoise = 99.0f;
197
198
// Minimum butteraugli distance the encoder accepts.
199
// Below d0.05 is not useful and risks going outside Level 5 limits
200
// (in particular modular_16bit_buffers becomes an issue for DC)
201
static constexpr float kMinButteraugliDistance = 0.05f;
202
203
// Tile size for encoder-side processing. Must be equal to color tile dim in the
204
// current implementation.
205
static constexpr size_t kEncTileDim = 64;
206
static constexpr size_t kEncTileDimInBlocks = kEncTileDim / kBlockDim;
207
208
}  // namespace jxl
209
210
#endif  // LIB_JXL_ENC_PARAMS_H_