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_MODULAR_OPTIONS_H_
7 #define LIB_JXL_MODULAR_OPTIONS_H_
8 
9 #include <stdint.h>
10 
11 #include <array>
12 #include <vector>
13 
14 namespace jxl {
15 
16 using PropertyVal = int32_t;
17 using Properties = std::vector<PropertyVal>;
18 
19 enum class Predictor : uint32_t {
20   Zero = 0,
21   Left = 1,
22   Top = 2,
23   Average0 = 3,
24   Select = 4,
25   Gradient = 5,
26   Weighted = 6,
27   TopRight = 7,
28   TopLeft = 8,
29   LeftLeft = 9,
30   Average1 = 10,
31   Average2 = 11,
32   Average3 = 12,
33   Average4 = 13,
34   // The following predictors are encoder-only.
35   Best = 14,  // Best of Gradient and Weighted
36   Variable =
37       15,  // Find the best decision tree for predictors/predictor per row
38 };
39 
40 constexpr size_t kNumModularPredictors =
41     static_cast<size_t>(Predictor::Average4) + 1;
42 constexpr size_t kNumModularEncoderPredictors =
43     static_cast<size_t>(Predictor::Variable) + 1;
44 
45 static constexpr ssize_t kNumStaticProperties = 2;  // channel, group_id.
46 
47 using StaticPropRange =
48     std::array<std::array<uint32_t, 2>, kNumStaticProperties>;
49 
50 struct ModularMultiplierInfo {
51   StaticPropRange range;
52   uint32_t multiplier;
53 };
54 
55 struct ModularOptions {
56   /// Used in both encode and decode:
57 
58   // Stop encoding/decoding when reaching a (non-meta) channel that has a
59   // dimension bigger than max_chan_size.
60   size_t max_chan_size = 0xFFFFFF;
61 
62   // Used during decoding for validation of transforms (sqeeezing) scheme.
63   size_t group_dim = 0x1FFFFFFF;
64 
65   /// Encode options:
66   // Fraction of pixels to look at to learn a MA tree
67   // Number of iterations to do to learn a MA tree
68   // (if zero there is no MA context model)
69   float nb_repeats = .5f;
70 
71   // Maximum number of (previous channel) properties to use in the MA trees
72   int max_properties = 0;  // no previous channels
73 
74   // Alternative heuristic tweaks.
75   // Properties default to channel, group, weighted, gradient residual, W-NW,
76   // NW-N, N-NE, N-NN
77   std::vector<uint32_t> splitting_heuristics_properties = {0,  1,  15, 9,
78                                                            10, 11, 12, 13};
79   float splitting_heuristics_node_threshold = 96;
80   size_t max_property_values = 32;
81 
82   // Predictor to use for each channel.
83   Predictor predictor = static_cast<Predictor>(-1);
84 
85   int wp_mode = 0;
86 
87   float fast_decode_multiplier = 1.01f;
88 
89   // Forces the encoder to produce a tree that is compatible with the WP-only
90   // decode path (or with the no-wp path, or the gradient-only path).
91   enum class TreeMode { kGradientOnly, kWPOnly, kNoWP, kDefault };
92   TreeMode wp_tree_mode = TreeMode::kDefault;
93 
94   // Skip fast paths in the encoder.
95   bool skip_encoder_fast_path = false;
96 
97   // Kind of tree to use.
98   // TODO(veluca): add tree kinds for JPEG recompression with CfL enabled,
99   // general AC metadata, different DC qualities, and others.
100   enum class TreeKind {
101     kLearn,
102     kJpegTranscodeACMeta,
103     kFalconACMeta,
104     kACMeta,
105     kWPFixedDC,
106     kGradientFixedDC,
107   };
108   TreeKind tree_kind = TreeKind::kLearn;
109 
110   // Ignore the image and just pretend all tokens are zeroes
111   bool zero_tokens = false;
112 };
113 
114 }  // namespace jxl
115 
116 #endif  // LIB_JXL_MODULAR_OPTIONS_H_
117