libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

enc_params.h (6823B)


      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   SpeedTier speed_tier = SpeedTier::kSquirrel;
     41   int brotli_effort = -1;
     42 
     43   // 0 = default.
     44   // 1 = slightly worse quality.
     45   // 4 = fastest speed, lowest quality
     46   size_t decoding_speed_tier = 0;
     47 
     48   ColorTransform color_transform = ColorTransform::kXYB;
     49 
     50   // If true, the "modular mode options" members below are used.
     51   bool modular_mode = false;
     52 
     53   // Change group size in modular mode (0=128, 1=256, 2=512, 3=1024, -1=encoder
     54   // chooses).
     55   int modular_group_size_shift = -1;
     56 
     57   Override preview = Override::kDefault;
     58   Override noise = Override::kDefault;
     59   Override dots = Override::kDefault;
     60   Override patches = Override::kDefault;
     61   Override gaborish = Override::kDefault;
     62   int epf = -1;
     63 
     64   // Progressive mode.
     65   Override progressive_mode = Override::kDefault;
     66 
     67   // Quantized-progressive mode.
     68   Override qprogressive_mode = Override::kDefault;
     69 
     70   // Put center groups first in the bitstream.
     71   bool centerfirst = false;
     72 
     73   // Pixel coordinates of the center. First group will contain that center.
     74   size_t center_x = static_cast<size_t>(-1);
     75   size_t center_y = static_cast<size_t>(-1);
     76 
     77   int progressive_dc = -1;
     78 
     79   // If on: preserve color of invisible pixels (if off: don't care)
     80   // Default: on
     81   Override keep_invisible = Override::kDefault;
     82 
     83   JxlCmsInterface cms;
     84   bool cms_set = false;
     85   void SetCms(const JxlCmsInterface& cms) {
     86     this->cms = cms;
     87     cms_set = true;
     88   }
     89 
     90   // Force usage of CfL when doing JPEG recompression. This can have unexpected
     91   // effects on the decoded pixels, while still being JPEG-compliant and
     92   // allowing reconstruction of the original JPEG.
     93   bool force_cfl_jpeg_recompression = true;
     94 
     95   // Use brotli compression for any boxes derived from a JPEG frame.
     96   bool jpeg_compress_boxes = true;
     97 
     98   // Preserve this metadata when doing JPEG recompression.
     99   bool jpeg_keep_exif = true;
    100   bool jpeg_keep_xmp = true;
    101   bool jpeg_keep_jumbf = true;
    102 
    103   // Set the noise to what it would approximately be if shooting at the nominal
    104   // exposure for a given ISO setting on a 35mm camera.
    105   float photon_noise_iso = 0;
    106 
    107   // modular mode options below
    108   ModularOptions options;
    109   int responsive = -1;
    110   int colorspace = -1;
    111   int move_to_front_from_channel = -1;
    112   // Use Global channel palette if #colors < this percentage of range
    113   float channel_colors_pre_transform_percent = 95.f;
    114   // Use Local channel palette if #colors < this percentage of range
    115   float channel_colors_percent = 80.f;
    116   int palette_colors = 1 << 10;  // up to 10-bit palette is probably worthwhile
    117   bool lossy_palette = false;
    118 
    119   // Returns whether these params are lossless as defined by SetLossless();
    120   bool IsLossless() const { return modular_mode && ModularPartIsLossless(); }
    121 
    122   bool ModularPartIsLossless() const {
    123     if (modular_mode) {
    124       // YCbCr is also considered lossless here since it's intended for
    125       // source material that is already YCbCr (we don't do the fwd transform)
    126       if (butteraugli_distance != 0 ||
    127           color_transform == jxl::ColorTransform::kXYB)
    128         return false;
    129     }
    130     for (float f : ec_distance) {
    131       if (f > 0) return false;
    132       if (f < 0 && butteraugli_distance != 0) return false;
    133     }
    134     // all modular channels are encoded at distance 0
    135     return true;
    136   }
    137 
    138   // Sets the parameters required to make the codec lossless.
    139   void SetLossless() {
    140     modular_mode = true;
    141     butteraugli_distance = 0.0f;
    142     for (float& f : ec_distance) f = 0.0f;
    143     color_transform = jxl::ColorTransform::kNone;
    144   }
    145 
    146   // Down/upsample the image before encoding / after decoding by this factor.
    147   // The resampling value can also be set to <= 0 to automatically choose based
    148   // on distance, however EncodeFrame doesn't support this, so it is
    149   // required to call PostInit() to set a valid positive resampling
    150   // value and altered butteraugli score if this is used.
    151   int resampling = -1;
    152   int ec_resampling = -1;
    153   // Skip the downsampling before encoding if this is true.
    154   bool already_downsampled = false;
    155   // Butteraugli target distance on the original full size image, this can be
    156   // different from butteraugli_distance if resampling was used.
    157   float original_butteraugli_distance = -1.0f;
    158 
    159   float quant_ac_rescale = 1.0;
    160 
    161   // Codestream level to conform to.
    162   // -1: don't care
    163   int level = -1;
    164 
    165   // See JXL_ENC_FRAME_SETTING_BUFFERING option value.
    166   int buffering = -1;
    167   // See JXL_ENC_FRAME_SETTING_USE_FULL_IMAGE_HEURISTICS option value.
    168   bool use_full_image_heuristics = true;
    169 
    170   std::vector<float> manual_noise;
    171   std::vector<float> manual_xyb_factors;
    172 
    173   // If not empty, this tree will be used for dc global section.
    174   // Used in jxl_from_tree tool.
    175   Tree custom_fixed_tree;
    176   // If not empty, these custom splines will be used instead of the computed
    177   // ones. Used in jxl_from_tee tool.
    178   Splines custom_splines;
    179   // If not null, overrides progressive mode settings. Used in decode_test.
    180   const ProgressiveMode* custom_progressive_mode = nullptr;
    181 
    182   JxlDebugImageCallback debug_image = nullptr;
    183   void* debug_image_opaque;
    184 };
    185 
    186 static constexpr float kMinButteraugliForDynamicAR = 0.5f;
    187 static constexpr float kMinButteraugliForDots = 3.0f;
    188 static constexpr float kMinButteraugliToSubtractOriginalPatches = 3.0f;
    189 
    190 // Always off
    191 static constexpr float kMinButteraugliForNoise = 99.0f;
    192 
    193 // Minimum butteraugli distance the encoder accepts.
    194 static constexpr float kMinButteraugliDistance = 0.001f;
    195 
    196 // Tile size for encoder-side processing. Must be equal to color tile dim in the
    197 // current implementation.
    198 static constexpr size_t kEncTileDim = 64;
    199 static constexpr size_t kEncTileDimInBlocks = kEncTileDim / kBlockDim;
    200 
    201 }  // namespace jxl
    202 
    203 #endif  // LIB_JXL_ENC_PARAMS_H_