Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/modular/encoding/context_predict.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_MODULAR_ENCODING_CONTEXT_PREDICT_H_
7
#define LIB_JXL_MODULAR_ENCODING_CONTEXT_PREDICT_H_
8
9
#include <algorithm>
10
#include <array>
11
#include <cmath>
12
#include <cstddef>
13
#include <cstdint>
14
#include <vector>
15
16
#include "lib/jxl/base/bits.h"
17
#include "lib/jxl/base/compiler_specific.h"
18
#include "lib/jxl/base/status.h"
19
#include "lib/jxl/field_encodings.h"
20
#include "lib/jxl/fields.h"
21
#include "lib/jxl/image_ops.h"
22
#include "lib/jxl/modular/modular_image.h"
23
#include "lib/jxl/modular/options.h"
24
25
namespace jxl {
26
27
namespace weighted {
28
constexpr static size_t kNumPredictors = 4;
29
constexpr static int64_t kPredExtraBits = 3;
30
constexpr static int64_t kPredictionRound = ((1 << kPredExtraBits) >> 1) - 1;
31
constexpr static size_t kNumProperties = 1;
32
33
struct Header : public Fields {
34
  JXL_FIELDS_NAME(WeightedPredictorHeader)
35
  // TODO(janwas): move to cc file, avoid including fields.h.
36
983k
  Header() { Bundle::Init(this); }
37
38
1.42M
  Status VisitFields(Visitor *JXL_RESTRICT visitor) override {
39
1.42M
    if (visitor->AllDefault(*this, &all_default)) {
40
      // Overwrite all serialized fields, but not any nonserialized_*.
41
141k
      visitor->SetDefault(this);
42
141k
      return true;
43
141k
    }
44
8.98M
    auto visit_p = [visitor](pixel_type val, pixel_type *p) -> Status {
45
8.98M
      uint32_t up = *p;
46
8.98M
      JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(5, val, &up));
47
8.98M
      *p = up;
48
8.98M
      return true;
49
8.98M
    };
50
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visit_p(16, &p1C));
51
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visit_p(10, &p2C));
52
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Ca));
53
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cb));
54
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cc));
55
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Cd));
56
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Ce));
57
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xd, &w[0]));
58
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[1]));
59
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[2]));
60
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[3]));
61
1.28M
    return true;
62
1.28M
  }
63
64
  bool all_default;
65
  pixel_type p1C = 0, p2C = 0, p3Ca = 0, p3Cb = 0, p3Cc = 0, p3Cd = 0, p3Ce = 0;
66
  uint32_t w[kNumPredictors] = {};
67
};
68
69
struct State {
70
  pixel_type_w prediction[kNumPredictors] = {};
71
  pixel_type_w pred = 0;  // *before* removing the added bits.
72
  std::vector<uint32_t> pred_errors[kNumPredictors];
73
  std::vector<int32_t> error;
74
  const Header &header;
75
76
  // Allows to approximate division by a number from 1 to 64.
77
  //  for (int i = 0; i < 64; i++) divlookup[i] = (1 << 24) / (i + 1);
78
79
  const uint32_t divlookup[64] = {
80
      16777216, 8388608, 5592405, 4194304, 3355443, 2796202, 2396745, 2097152,
81
      1864135,  1677721, 1525201, 1398101, 1290555, 1198372, 1118481, 1048576,
82
      986895,   932067,  883011,  838860,  798915,  762600,  729444,  699050,
83
      671088,   645277,  621378,  599186,  578524,  559240,  541200,  524288,
84
      508400,   493447,  479349,  466033,  453438,  441505,  430185,  419430,
85
      409200,   399457,  390167,  381300,  372827,  364722,  356962,  349525,
86
      342392,   335544,  328965,  322638,  316551,  310689,  305040,  299593,
87
      294337,   289262,  284359,  279620,  275036,  270600,  266305,  262144};
88
89
1.56G
  constexpr static pixel_type_w AddBits(pixel_type_w x) {
90
1.56G
    return static_cast<uint64_t>(x) << kPredExtraBits;
91
1.56G
  }
92
93
117k
  State(const Header &header, size_t xsize, size_t ysize) : header(header) {
94
    // Extra margin to avoid out-of-bounds writes.
95
    // All have space for two rows of data.
96
470k
    for (auto &pred_error : pred_errors) {
97
470k
      pred_error.resize((xsize + 2) * 2);
98
470k
    }
99
117k
    error.resize((xsize + 2) * 2);
100
117k
  }
101
102
  // Approximates 4+(maxweight<<24)/(x+1), avoiding division
103
1.04G
  JXL_INLINE uint32_t ErrorWeight(uint64_t x, uint32_t maxweight) const {
104
1.04G
    int shift = static_cast<int>(FloorLog2Nonzero(x + 1)) - 5;
105
1.04G
    if (shift < 0) shift = 0;
106
1.04G
    return 4 + ((maxweight * divlookup[x >> shift]) >> shift);
107
1.04G
  }
108
109
  // Approximates the weighted average of the input values with the given
110
  // weights, avoiding division. Weights must sum to at least 16.
111
  JXL_INLINE pixel_type_w
112
  WeightedAverage(const pixel_type_w *JXL_RESTRICT p,
113
261M
                  std::array<uint32_t, kNumPredictors> w) const {
114
261M
    uint32_t weight_sum = 0;
115
1.30G
    for (size_t i = 0; i < kNumPredictors; i++) {
116
1.04G
      weight_sum += w[i];
117
1.04G
    }
118
261M
    JXL_DASSERT(weight_sum > 15);
119
261M
    uint32_t log_weight = FloorLog2Nonzero(weight_sum);  // at least 4.
120
261M
    weight_sum = 0;
121
1.30G
    for (size_t i = 0; i < kNumPredictors; i++) {
122
1.04G
      w[i] >>= log_weight - 4;
123
1.04G
      weight_sum += w[i];
124
1.04G
    }
125
    // for rounding.
126
261M
    pixel_type_w sum = (weight_sum >> 1) - 1;
127
1.30G
    for (size_t i = 0; i < kNumPredictors; i++) {
128
1.04G
      sum += p[i] * w[i];
129
1.04G
    }
130
261M
    return (sum * divlookup[weight_sum - 1]) >> 24;
131
261M
  }
132
133
  template <bool compute_properties>
134
  JXL_INLINE pixel_type_w Predict(size_t x, size_t y, size_t xsize,
135
                                  pixel_type_w N, pixel_type_w W,
136
                                  pixel_type_w NE, pixel_type_w NW,
137
                                  pixel_type_w NN, Properties *properties,
138
261M
                                  size_t offset) {
139
261M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
261M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
261M
    size_t pos_N = prev_row + x;
142
261M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
261M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
261M
    std::array<uint32_t, kNumPredictors> weights;
145
1.30G
    for (size_t i = 0; i < kNumPredictors; i++) {
146
      // pred_errors[pos_N] also contains the error of pixel W.
147
      // pred_errors[pos_NW] also contains the error of pixel WW.
148
1.04G
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
1.04G
                   pred_errors[i][pos_NW];
150
1.04G
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
1.04G
    }
152
153
261M
    N = AddBits(N);
154
261M
    W = AddBits(W);
155
261M
    NE = AddBits(NE);
156
261M
    NW = AddBits(NW);
157
261M
    NN = AddBits(NN);
158
159
261M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
261M
    pixel_type_w teN = error[pos_N];
161
261M
    pixel_type_w teNW = error[pos_NW];
162
261M
    pixel_type_w sumWN = teN + teW;
163
261M
    pixel_type_w teNE = error[pos_NE];
164
165
261M
    if (compute_properties) {
166
209M
      pixel_type_w p = teW;
167
209M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
209M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
209M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
209M
      (*properties)[offset++] = p;
171
209M
    }
172
173
261M
    prediction[0] = W + NE - N;
174
261M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
261M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
261M
    prediction[3] =
177
261M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
261M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
261M
             5);
180
181
261M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
261M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
47.2M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
47.2M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
214M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
214M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
214M
    pred = std::max(mn, std::min(mx, pred));
192
214M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
261M
  }
long jxl::weighted::State::Predict<false>(unsigned long, unsigned long, unsigned long, long, long, long, long, long, std::__1::vector<int, std::__1::allocator<int> >*, unsigned long)
Line
Count
Source
138
51.5M
                                  size_t offset) {
139
51.5M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
51.5M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
51.5M
    size_t pos_N = prev_row + x;
142
51.5M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
51.5M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
51.5M
    std::array<uint32_t, kNumPredictors> weights;
145
257M
    for (size_t i = 0; i < kNumPredictors; i++) {
146
      // pred_errors[pos_N] also contains the error of pixel W.
147
      // pred_errors[pos_NW] also contains the error of pixel WW.
148
206M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
206M
                   pred_errors[i][pos_NW];
150
206M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
206M
    }
152
153
51.5M
    N = AddBits(N);
154
51.5M
    W = AddBits(W);
155
51.5M
    NE = AddBits(NE);
156
51.5M
    NW = AddBits(NW);
157
51.5M
    NN = AddBits(NN);
158
159
51.5M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
51.5M
    pixel_type_w teN = error[pos_N];
161
51.5M
    pixel_type_w teNW = error[pos_NW];
162
51.5M
    pixel_type_w sumWN = teN + teW;
163
51.5M
    pixel_type_w teNE = error[pos_NE];
164
165
51.5M
    if (compute_properties) {
166
0
      pixel_type_w p = teW;
167
0
      if (std::abs(teN) > std::abs(p)) p = teN;
168
0
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
0
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
0
      (*properties)[offset++] = p;
171
0
    }
172
173
51.5M
    prediction[0] = W + NE - N;
174
51.5M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
51.5M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
51.5M
    prediction[3] =
177
51.5M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
51.5M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
51.5M
             5);
180
181
51.5M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
51.5M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
3.46M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
3.46M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
48.1M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
48.1M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
48.1M
    pred = std::max(mn, std::min(mx, pred));
192
48.1M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
51.5M
  }
long jxl::weighted::State::Predict<true>(unsigned long, unsigned long, unsigned long, long, long, long, long, long, std::__1::vector<int, std::__1::allocator<int> >*, unsigned long)
Line
Count
Source
138
209M
                                  size_t offset) {
139
209M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
209M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
209M
    size_t pos_N = prev_row + x;
142
209M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
209M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
209M
    std::array<uint32_t, kNumPredictors> weights;
145
1.04G
    for (size_t i = 0; i < kNumPredictors; i++) {
146
      // pred_errors[pos_N] also contains the error of pixel W.
147
      // pred_errors[pos_NW] also contains the error of pixel WW.
148
838M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
838M
                   pred_errors[i][pos_NW];
150
838M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
838M
    }
152
153
209M
    N = AddBits(N);
154
209M
    W = AddBits(W);
155
209M
    NE = AddBits(NE);
156
209M
    NW = AddBits(NW);
157
209M
    NN = AddBits(NN);
158
159
209M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
209M
    pixel_type_w teN = error[pos_N];
161
209M
    pixel_type_w teNW = error[pos_NW];
162
209M
    pixel_type_w sumWN = teN + teW;
163
209M
    pixel_type_w teNE = error[pos_NE];
164
165
209M
    if (compute_properties) {
166
209M
      pixel_type_w p = teW;
167
209M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
209M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
209M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
209M
      (*properties)[offset++] = p;
171
209M
    }
172
173
209M
    prediction[0] = W + NE - N;
174
209M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
209M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
209M
    prediction[3] =
177
209M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
209M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
209M
             5);
180
181
209M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
209M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
43.7M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
43.7M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
165M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
165M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
165M
    pred = std::max(mn, std::min(mx, pred));
192
165M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
209M
  }
194
195
  JXL_INLINE void UpdateErrors(pixel_type_w val, size_t x, size_t y,
196
261M
                               size_t xsize) {
197
261M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
198
261M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
199
261M
    val = AddBits(val);
200
261M
    error[cur_row + x] = pred - val;
201
1.30G
    for (size_t i = 0; i < kNumPredictors; i++) {
202
1.04G
      pixel_type_w err =
203
1.04G
          (std::abs(prediction[i] - val) + kPredictionRound) >> kPredExtraBits;
204
      // For predicting in the next row.
205
1.04G
      pred_errors[i][cur_row + x] = err;
206
      // Add the error on this pixel to the error on the NE pixel. This has the
207
      // effect of adding the error on this pixel to the E and EE pixels.
208
1.04G
      pred_errors[i][prev_row + x + 1] += err;
209
1.04G
    }
210
261M
  }
211
};
212
213
// Encoder helper function to set the parameters to some presets.
214
5.22k
inline void PredictorMode(int i, Header *header) {
215
5.22k
  switch (i) {
216
5.22k
    case 0:
217
      // ~ lossless16 predictor
218
5.22k
      header->w[0] = 0xd;
219
5.22k
      header->w[1] = 0xc;
220
5.22k
      header->w[2] = 0xc;
221
5.22k
      header->w[3] = 0xc;
222
5.22k
      header->p1C = 16;
223
5.22k
      header->p2C = 10;
224
5.22k
      header->p3Ca = 7;
225
5.22k
      header->p3Cb = 7;
226
5.22k
      header->p3Cc = 7;
227
5.22k
      header->p3Cd = 0;
228
5.22k
      header->p3Ce = 0;
229
5.22k
      break;
230
0
    case 1:
231
      // ~ default lossless8 predictor
232
0
      header->w[0] = 0xd;
233
0
      header->w[1] = 0xc;
234
0
      header->w[2] = 0xc;
235
0
      header->w[3] = 0xb;
236
0
      header->p1C = 8;
237
0
      header->p2C = 8;
238
0
      header->p3Ca = 4;
239
0
      header->p3Cb = 0;
240
0
      header->p3Cc = 3;
241
0
      header->p3Cd = 23;
242
0
      header->p3Ce = 2;
243
0
      break;
244
0
    case 2:
245
      // ~ west lossless8 predictor
246
0
      header->w[0] = 0xd;
247
0
      header->w[1] = 0xc;
248
0
      header->w[2] = 0xd;
249
0
      header->w[3] = 0xc;
250
0
      header->p1C = 10;
251
0
      header->p2C = 9;
252
0
      header->p3Ca = 7;
253
0
      header->p3Cb = 0;
254
0
      header->p3Cc = 0;
255
0
      header->p3Cd = 16;
256
0
      header->p3Ce = 9;
257
0
      break;
258
0
    case 3:
259
      // ~ north lossless8 predictor
260
0
      header->w[0] = 0xd;
261
0
      header->w[1] = 0xd;
262
0
      header->w[2] = 0xc;
263
0
      header->w[3] = 0xc;
264
0
      header->p1C = 16;
265
0
      header->p2C = 8;
266
0
      header->p3Ca = 0;
267
0
      header->p3Cb = 16;
268
0
      header->p3Cc = 0;
269
0
      header->p3Cd = 23;
270
0
      header->p3Ce = 0;
271
0
      break;
272
0
    case 4:
273
0
    default:
274
      // something else, because why not
275
0
      header->w[0] = 0xd;
276
0
      header->w[1] = 0xc;
277
0
      header->w[2] = 0xc;
278
0
      header->w[3] = 0xc;
279
0
      header->p1C = 10;
280
0
      header->p2C = 10;
281
0
      header->p3Ca = 5;
282
0
      header->p3Cb = 5;
283
0
      header->p3Cc = 5;
284
0
      header->p3Cd = 12;
285
0
      header->p3Ce = 4;
286
0
      break;
287
5.22k
  }
288
5.22k
}
289
}  // namespace weighted
290
291
// Returns true if the (meta)predictor makes use of the weighted predictor.
292
15.8k
inline bool PredictorHasWeighted(Predictor predictor) {
293
  // Use a non-defaulted switch to generate a warning if a case is missing.
294
15.8k
  switch (predictor) {
295
0
    case Predictor::Zero:
296
0
    case Predictor::Left:
297
0
    case Predictor::Top:
298
0
    case Predictor::Average0:
299
0
    case Predictor::Select:
300
10.6k
    case Predictor::Gradient:
301
10.6k
      return false;
302
5.22k
    case Predictor::Weighted:
303
5.22k
      return true;
304
0
    case Predictor::TopRight:
305
0
    case Predictor::TopLeft:
306
0
    case Predictor::LeftLeft:
307
0
    case Predictor::Average1:
308
0
    case Predictor::Average2:
309
0
    case Predictor::Average3:
310
0
    case Predictor::Average4:
311
0
      return false;
312
0
    case Predictor::Best:
313
0
    case Predictor::Variable:
314
0
      return true;
315
15.8k
  }
316
317
0
  return false;
318
15.8k
}
319
320
// Stores a node and its two children at the same time. This significantly
321
// reduces the number of branches needed during decoding.
322
struct FlatDecisionNode {
323
  // Property + splitval of the top node.
324
  int32_t property0;  // -1 if leaf.
325
  union {
326
    PropertyVal splitval0;
327
    Predictor predictor;
328
  };
329
  // Property+splitval of the two child nodes.
330
  union {
331
    PropertyVal splitvals[2];
332
    int32_t multiplier;
333
  };
334
  uint32_t childID;  // childID is ctx id if leaf.
335
  union {
336
    int16_t properties[2];
337
    int32_t predictor_offset;
338
  };
339
};
340
using FlatTree = std::vector<FlatDecisionNode>;
341
342
class MATreeLookup {
343
 public:
344
311k
  explicit MATreeLookup(const FlatTree &tree) : nodes_(tree) {}
345
  struct LookupResult {
346
    uint32_t context;
347
    Predictor predictor;
348
    int32_t offset;
349
    int32_t multiplier;
350
  };
351
684M
  JXL_INLINE LookupResult Lookup(const Properties &properties) const {
352
684M
    uint32_t pos = 0;
353
858M
    while (true) {
354
858M
#define TRAVERSE_THE_TREE                                                \
355
1.11G
  {                                                                      \
356
1.11G
    const FlatDecisionNode &node = nodes_[pos];                          \
357
1.11G
    if (node.property0 < 0) {                                            \
358
684M
      return {node.childID, node.predictor, node.predictor_offset,       \
359
684M
              node.multiplier};                                          \
360
684M
    }                                                                    \
361
1.11G
    bool p0 = properties[node.property0] <= node.splitval0;              \
362
433M
    uint32_t off0 = properties[node.properties[0]] <= node.splitvals[0]; \
363
433M
    uint32_t off1 =                                                      \
364
433M
        2 | int{properties[node.properties[1]] <= node.splitvals[1]};    \
365
433M
    pos = node.childID + (p0 ? off1 : off0);                             \
366
433M
  }
367
368
858M
      TRAVERSE_THE_TREE;
369
259M
      TRAVERSE_THE_TREE;
370
174M
    }
371
684M
  }
372
373
 private:
374
  const FlatTree &nodes_;
375
};
376
377
static constexpr size_t kExtraPropsPerChannel = 4;
378
static constexpr size_t kNumNonrefProperties =
379
    kNumStaticProperties + 13 + weighted::kNumProperties;
380
381
constexpr size_t kWPProp = kNumNonrefProperties - weighted::kNumProperties;
382
constexpr size_t kGradientProp = 9;
383
384
// Clamps gradient to the min/max of n, w (and l, implicitly).
385
static JXL_INLINE int32_t ClampedGradient(const int32_t n, const int32_t w,
386
116M
                                          const int32_t l) {
387
116M
  const int32_t m = std::min(n, w);
388
116M
  const int32_t M = std::max(n, w);
389
  // The end result of this operation doesn't overflow or underflow if the
390
  // result is between m and M, but the intermediate value may overflow, so we
391
  // do the intermediate operations in uint32_t and check later if we had an
392
  // overflow or underflow condition comparing m, M and l directly.
393
  // grad = M + m - l = n + w - l
394
116M
  const int32_t grad =
395
116M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
116M
                           static_cast<uint32_t>(l));
397
  // We use two sets of ternary operators to force the evaluation of them in
398
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
399
  // x86.
400
116M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
116M
  return (l > M) ? m : grad_clamp_M;
402
116M
}
Unexecuted instantiation: decode.cc:jxl::ClampedGradient(int, int, int)
encoding.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
386
84.8M
                                          const int32_t l) {
387
84.8M
  const int32_t m = std::min(n, w);
388
84.8M
  const int32_t M = std::max(n, w);
389
  // The end result of this operation doesn't overflow or underflow if the
390
  // result is between m and M, but the intermediate value may overflow, so we
391
  // do the intermediate operations in uint32_t and check later if we had an
392
  // overflow or underflow condition comparing m, M and l directly.
393
  // grad = M + m - l = n + w - l
394
84.8M
  const int32_t grad =
395
84.8M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
84.8M
                           static_cast<uint32_t>(l));
397
  // We use two sets of ternary operators to force the evaluation of them in
398
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
399
  // x86.
400
84.8M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
84.8M
  return (l > M) ? m : grad_clamp_M;
402
84.8M
}
Unexecuted instantiation: modular_image.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: transform.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: quant_weights.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_frame.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_heuristics.cc:jxl::ClampedGradient(int, int, int)
enc_modular.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
386
1.93M
                                          const int32_t l) {
387
1.93M
  const int32_t m = std::min(n, w);
388
1.93M
  const int32_t M = std::max(n, w);
389
  // The end result of this operation doesn't overflow or underflow if the
390
  // result is between m and M, but the intermediate value may overflow, so we
391
  // do the intermediate operations in uint32_t and check later if we had an
392
  // overflow or underflow condition comparing m, M and l directly.
393
  // grad = M + m - l = n + w - l
394
1.93M
  const int32_t grad =
395
1.93M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
1.93M
                           static_cast<uint32_t>(l));
397
  // We use two sets of ternary operators to force the evaluation of them in
398
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
399
  // x86.
400
1.93M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
1.93M
  return (l > M) ? m : grad_clamp_M;
402
1.93M
}
Unexecuted instantiation: enc_patch_dictionary.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_quant_weights.cc:jxl::ClampedGradient(int, int, int)
enc_encoding.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
386
30.2M
                                          const int32_t l) {
387
30.2M
  const int32_t m = std::min(n, w);
388
30.2M
  const int32_t M = std::max(n, w);
389
  // The end result of this operation doesn't overflow or underflow if the
390
  // result is between m and M, but the intermediate value may overflow, so we
391
  // do the intermediate operations in uint32_t and check later if we had an
392
  // overflow or underflow condition comparing m, M and l directly.
393
  // grad = M + m - l = n + w - l
394
30.2M
  const int32_t grad =
395
30.2M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
30.2M
                           static_cast<uint32_t>(l));
397
  // We use two sets of ternary operators to force the evaluation of them in
398
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
399
  // x86.
400
30.2M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
30.2M
  return (l > M) ? m : grad_clamp_M;
402
30.2M
}
Unexecuted instantiation: enc_ma.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_rct.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_transform.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: dec_frame.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: dec_modular.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: palette.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: rct.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_adaptive_quantization.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_cache.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_palette.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: coeff_order.cc:jxl::ClampedGradient(int, int, int)
403
404
2.33M
inline pixel_type_w Select(pixel_type_w a, pixel_type_w b, pixel_type_w c) {
405
2.33M
  pixel_type_w p = a + b - c;
406
2.33M
  pixel_type_w pa = std::abs(p - a);
407
2.33M
  pixel_type_w pb = std::abs(p - b);
408
2.33M
  return pa < pb ? a : b;
409
2.33M
}
410
411
inline void PrecomputeReferences(const Channel &ch, size_t y,
412
                                 const Image &image, uint32_t i,
413
14.8M
                                 Channel *references) {
414
14.8M
  ZeroFillImage(&references->plane);
415
14.8M
  uint32_t offset = 0;
416
14.8M
  size_t num_extra_props = references->w;
417
14.8M
  ptrdiff_t onerow = references->plane.PixelsPerRow();
418
14.8M
  for (int32_t j = static_cast<int32_t>(i) - 1;
419
15.8M
       j >= 0 && offset < num_extra_props; j--) {
420
1.00M
    if (image.channel[j].w != image.channel[i].w ||
421
926k
        image.channel[j].h != image.channel[i].h) {
422
926k
      continue;
423
926k
    }
424
79.0k
    if (image.channel[j].hshift != image.channel[i].hshift) continue;
425
56.5k
    if (image.channel[j].vshift != image.channel[i].vshift) continue;
426
56.1k
    pixel_type *JXL_RESTRICT rp = references->Row(0) + offset;
427
56.1k
    const pixel_type *JXL_RESTRICT rpp = image.channel[j].Row(y);
428
56.1k
    const pixel_type *JXL_RESTRICT rpprev = image.channel[j].Row(y ? y - 1 : 0);
429
1.17M
    for (size_t x = 0; x < ch.w; x++, rp += onerow) {
430
1.11M
      pixel_type_w v = rpp[x];
431
1.11M
      rp[0] = std::abs(v);
432
1.11M
      rp[1] = v;
433
1.11M
      pixel_type_w vleft = (x ? rpp[x - 1] : 0);
434
1.11M
      pixel_type_w vtop = (y ? rpprev[x] : vleft);
435
1.11M
      pixel_type_w vtopleft = (x && y ? rpprev[x - 1] : vleft);
436
1.11M
      pixel_type_w vpredicted = ClampedGradient(vleft, vtop, vtopleft);
437
1.11M
      rp[2] = std::abs(v - vpredicted);
438
1.11M
      rp[3] = v - vpredicted;
439
1.11M
    }
440
441
56.1k
    offset += kExtraPropsPerChannel;
442
56.1k
  }
443
14.8M
}
444
445
struct PredictionResult {
446
  int context = 0;
447
  pixel_type_w guess = 0;
448
  Predictor predictor;
449
  int32_t multiplier;
450
};
451
452
inline void InitPropsRow(
453
    Properties *p,
454
    const std::array<pixel_type, kNumStaticProperties> &static_props,
455
14.8M
    const int y) {
456
44.6M
  for (size_t i = 0; i < kNumStaticProperties; i++) {
457
29.7M
    (*p)[i] = static_props[i];
458
29.7M
  }
459
14.8M
  (*p)[2] = y;
460
14.8M
  (*p)[9] = 0;  // local gradient.
461
14.8M
}
462
463
namespace detail {
464
enum PredictorMode {
465
  kUseTree = 1,
466
  kUseWP = 2,
467
  kForceComputeProperties = 4,
468
  kAllPredictions = 8,
469
  kNoEdgeCases = 16
470
};
471
472
JXL_INLINE pixel_type_w PredictOne(Predictor p, pixel_type_w left,
473
                                   pixel_type_w top, pixel_type_w toptop,
474
                                   pixel_type_w topleft, pixel_type_w topright,
475
                                   pixel_type_w leftleft,
476
                                   pixel_type_w toprightright,
477
783M
                                   pixel_type_w wp_pred) {
478
783M
  switch (p) {
479
38.6M
    case Predictor::Zero:
480
38.6M
      return pixel_type_w{0};
481
31.3M
    case Predictor::Left:
482
31.3M
      return left;
483
281M
    case Predictor::Top:
484
281M
      return top;
485
2.33M
    case Predictor::Select:
486
2.33M
      return Select(left, top, topleft);
487
78.7M
    case Predictor::Weighted:
488
78.7M
      return wp_pred;
489
104M
    case Predictor::Gradient:
490
104M
      return pixel_type_w{ClampedGradient(left, top, topleft)};
491
5.11M
    case Predictor::TopLeft:
492
5.11M
      return topleft;
493
2.90M
    case Predictor::TopRight:
494
2.90M
      return topright;
495
519k
    case Predictor::LeftLeft:
496
519k
      return leftleft;
497
202M
    case Predictor::Average0:
498
202M
      return (left + top) / 2;
499
1.34M
    case Predictor::Average1:
500
1.34M
      return (left + topleft) / 2;
501
16.7M
    case Predictor::Average2:
502
16.7M
      return (topleft + top) / 2;
503
15.5M
    case Predictor::Average3:
504
15.5M
      return (top + topright) / 2;
505
1.11M
    case Predictor::Average4:
506
1.11M
      return (6 * top - 2 * toptop + 7 * left + 1 * leftleft +
507
1.11M
              1 * toprightright + 3 * topright + 8) /
508
1.11M
             16;
509
0
    default:
510
0
      return pixel_type_w{0};
511
783M
  }
512
783M
}
513
514
template <int mode>
515
JXL_INLINE PredictionResult Predict(
516
    Properties *p, size_t w, const pixel_type *JXL_RESTRICT pp,
517
    const ptrdiff_t onerow, const size_t x, const size_t y, Predictor predictor,
518
    const MATreeLookup *lookup, const Channel *references,
519
783M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
783M
  size_t offset = 3;
522
783M
  constexpr bool compute_properties =
523
783M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
783M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
783M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
783M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
783M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
783M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
783M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
783M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
783M
  pixel_type_w toprightright =
532
783M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
783M
  if (compute_properties) {
535
    // location
536
711M
    (*p)[offset++] = x;
537
    // neighbors
538
711M
    (*p)[offset++] = top > 0 ? top : -top;
539
711M
    (*p)[offset++] = left > 0 ? left : -left;
540
711M
    (*p)[offset++] = top;
541
711M
    (*p)[offset++] = left;
542
543
    // local gradient
544
711M
    (*p)[offset] = left - (*p)[offset + 1];
545
711M
    offset++;
546
    // local gradient
547
711M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
711M
    (*p)[offset++] = left - topleft;
551
711M
    (*p)[offset++] = topleft - top;
552
711M
    (*p)[offset++] = top - topright;
553
711M
    (*p)[offset++] = top - toptop;
554
711M
    (*p)[offset++] = left - leftleft;
555
711M
  }
556
557
783M
  pixel_type_w wp_pred = 0;
558
783M
  if (mode & kUseWP) {
559
222M
    wp_pred = wp_state->Predict<compute_properties>(
560
222M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
222M
  }
562
783M
  if (!nec && compute_properties) {
563
125M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
125M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
202M
    for (size_t i = 0; i < references->w; i++) {
567
77.8M
      (*p)[offset++] = rp[i];
568
77.8M
    }
569
125M
  }
570
783M
  PredictionResult result;
571
783M
  if (mode & kUseTree) {
572
684M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
684M
    result.context = lr.context;
574
684M
    result.guess = lr.offset;
575
684M
    result.multiplier = lr.multiplier;
576
684M
    predictor = lr.predictor;
577
684M
  }
578
783M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
783M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
783M
                             leftleft, toprightright, wp_pred);
587
783M
  result.predictor = predictor;
588
589
783M
  return result;
590
783M
}
jxl::PredictionResult jxl::detail::Predict<0>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
19.5M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
19.5M
  size_t offset = 3;
522
19.5M
  constexpr bool compute_properties =
523
19.5M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
19.5M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
19.5M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
19.5M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
19.5M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
19.5M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
19.5M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
19.5M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
19.5M
  pixel_type_w toprightright =
532
19.5M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
19.5M
  if (compute_properties) {
535
    // location
536
0
    (*p)[offset++] = x;
537
    // neighbors
538
0
    (*p)[offset++] = top > 0 ? top : -top;
539
0
    (*p)[offset++] = left > 0 ? left : -left;
540
0
    (*p)[offset++] = top;
541
0
    (*p)[offset++] = left;
542
543
    // local gradient
544
0
    (*p)[offset] = left - (*p)[offset + 1];
545
0
    offset++;
546
    // local gradient
547
0
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
0
    (*p)[offset++] = left - topleft;
551
0
    (*p)[offset++] = topleft - top;
552
0
    (*p)[offset++] = top - topright;
553
0
    (*p)[offset++] = top - toptop;
554
0
    (*p)[offset++] = left - leftleft;
555
0
  }
556
557
19.5M
  pixel_type_w wp_pred = 0;
558
19.5M
  if (mode & kUseWP) {
559
0
    wp_pred = wp_state->Predict<compute_properties>(
560
0
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
0
  }
562
19.5M
  if (!nec && compute_properties) {
563
0
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
0
    for (size_t i = 0; i < references->w; i++) {
567
0
      (*p)[offset++] = rp[i];
568
0
    }
569
0
  }
570
19.5M
  PredictionResult result;
571
19.5M
  if (mode & kUseTree) {
572
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
0
    result.context = lr.context;
574
0
    result.guess = lr.offset;
575
0
    result.multiplier = lr.multiplier;
576
0
    predictor = lr.predictor;
577
0
  }
578
19.5M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
19.5M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
19.5M
                             leftleft, toprightright, wp_pred);
587
19.5M
  result.predictor = predictor;
588
589
19.5M
  return result;
590
19.5M
}
jxl::PredictionResult jxl::detail::Predict<2>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
51.5M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
51.5M
  size_t offset = 3;
522
51.5M
  constexpr bool compute_properties =
523
51.5M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
51.5M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
51.5M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
51.5M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
51.5M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
51.5M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
51.5M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
51.5M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
51.5M
  pixel_type_w toprightright =
532
51.5M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
51.5M
  if (compute_properties) {
535
    // location
536
0
    (*p)[offset++] = x;
537
    // neighbors
538
0
    (*p)[offset++] = top > 0 ? top : -top;
539
0
    (*p)[offset++] = left > 0 ? left : -left;
540
0
    (*p)[offset++] = top;
541
0
    (*p)[offset++] = left;
542
543
    // local gradient
544
0
    (*p)[offset] = left - (*p)[offset + 1];
545
0
    offset++;
546
    // local gradient
547
0
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
0
    (*p)[offset++] = left - topleft;
551
0
    (*p)[offset++] = topleft - top;
552
0
    (*p)[offset++] = top - topright;
553
0
    (*p)[offset++] = top - toptop;
554
0
    (*p)[offset++] = left - leftleft;
555
0
  }
556
557
51.5M
  pixel_type_w wp_pred = 0;
558
51.5M
  if (mode & kUseWP) {
559
51.5M
    wp_pred = wp_state->Predict<compute_properties>(
560
51.5M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
51.5M
  }
562
51.5M
  if (!nec && compute_properties) {
563
0
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
0
    for (size_t i = 0; i < references->w; i++) {
567
0
      (*p)[offset++] = rp[i];
568
0
    }
569
0
  }
570
51.5M
  PredictionResult result;
571
51.5M
  if (mode & kUseTree) {
572
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
0
    result.context = lr.context;
574
0
    result.guess = lr.offset;
575
0
    result.multiplier = lr.multiplier;
576
0
    predictor = lr.predictor;
577
0
  }
578
51.5M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
51.5M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
51.5M
                             leftleft, toprightright, wp_pred);
587
51.5M
  result.predictor = predictor;
588
589
51.5M
  return result;
590
51.5M
}
jxl::PredictionResult jxl::detail::Predict<1>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
90.4M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
90.4M
  size_t offset = 3;
522
90.4M
  constexpr bool compute_properties =
523
90.4M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
90.4M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
90.4M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
90.4M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
90.4M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
90.4M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
90.4M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
90.4M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
90.4M
  pixel_type_w toprightright =
532
90.4M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
90.4M
  if (compute_properties) {
535
    // location
536
90.4M
    (*p)[offset++] = x;
537
    // neighbors
538
90.4M
    (*p)[offset++] = top > 0 ? top : -top;
539
90.4M
    (*p)[offset++] = left > 0 ? left : -left;
540
90.4M
    (*p)[offset++] = top;
541
90.4M
    (*p)[offset++] = left;
542
543
    // local gradient
544
90.4M
    (*p)[offset] = left - (*p)[offset + 1];
545
90.4M
    offset++;
546
    // local gradient
547
90.4M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
90.4M
    (*p)[offset++] = left - topleft;
551
90.4M
    (*p)[offset++] = topleft - top;
552
90.4M
    (*p)[offset++] = top - topright;
553
90.4M
    (*p)[offset++] = top - toptop;
554
90.4M
    (*p)[offset++] = left - leftleft;
555
90.4M
  }
556
557
90.4M
  pixel_type_w wp_pred = 0;
558
90.4M
  if (mode & kUseWP) {
559
0
    wp_pred = wp_state->Predict<compute_properties>(
560
0
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
0
  }
562
90.4M
  if (!nec && compute_properties) {
563
90.4M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
90.4M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
129M
    for (size_t i = 0; i < references->w; i++) {
567
39.0M
      (*p)[offset++] = rp[i];
568
39.0M
    }
569
90.4M
  }
570
90.4M
  PredictionResult result;
571
90.4M
  if (mode & kUseTree) {
572
90.4M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
90.4M
    result.context = lr.context;
574
90.4M
    result.guess = lr.offset;
575
90.4M
    result.multiplier = lr.multiplier;
576
90.4M
    predictor = lr.predictor;
577
90.4M
  }
578
90.4M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
90.4M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
90.4M
                             leftleft, toprightright, wp_pred);
587
90.4M
  result.predictor = predictor;
588
589
90.4M
  return result;
590
90.4M
}
jxl::PredictionResult jxl::detail::Predict<17>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
451M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
451M
  size_t offset = 3;
522
451M
  constexpr bool compute_properties =
523
451M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
451M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
451M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
451M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
451M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
451M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
451M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
451M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
451M
  pixel_type_w toprightright =
532
451M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
451M
  if (compute_properties) {
535
    // location
536
451M
    (*p)[offset++] = x;
537
    // neighbors
538
451M
    (*p)[offset++] = top > 0 ? top : -top;
539
451M
    (*p)[offset++] = left > 0 ? left : -left;
540
451M
    (*p)[offset++] = top;
541
451M
    (*p)[offset++] = left;
542
543
    // local gradient
544
451M
    (*p)[offset] = left - (*p)[offset + 1];
545
451M
    offset++;
546
    // local gradient
547
451M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
451M
    (*p)[offset++] = left - topleft;
551
451M
    (*p)[offset++] = topleft - top;
552
451M
    (*p)[offset++] = top - topright;
553
451M
    (*p)[offset++] = top - toptop;
554
451M
    (*p)[offset++] = left - leftleft;
555
451M
  }
556
557
451M
  pixel_type_w wp_pred = 0;
558
451M
  if (mode & kUseWP) {
559
0
    wp_pred = wp_state->Predict<compute_properties>(
560
0
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
0
  }
562
451M
  if (!nec && compute_properties) {
563
0
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
0
    for (size_t i = 0; i < references->w; i++) {
567
0
      (*p)[offset++] = rp[i];
568
0
    }
569
0
  }
570
451M
  PredictionResult result;
571
451M
  if (mode & kUseTree) {
572
451M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
451M
    result.context = lr.context;
574
451M
    result.guess = lr.offset;
575
451M
    result.multiplier = lr.multiplier;
576
451M
    predictor = lr.predictor;
577
451M
  }
578
451M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
451M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
451M
                             leftleft, toprightright, wp_pred);
587
451M
  result.predictor = predictor;
588
589
451M
  return result;
590
451M
}
jxl::PredictionResult jxl::detail::Predict<3>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
32.8M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
32.8M
  size_t offset = 3;
522
32.8M
  constexpr bool compute_properties =
523
32.8M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
32.8M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
32.8M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
32.8M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
32.8M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
32.8M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
32.8M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
32.8M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
32.8M
  pixel_type_w toprightright =
532
32.8M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
32.8M
  if (compute_properties) {
535
    // location
536
32.8M
    (*p)[offset++] = x;
537
    // neighbors
538
32.8M
    (*p)[offset++] = top > 0 ? top : -top;
539
32.8M
    (*p)[offset++] = left > 0 ? left : -left;
540
32.8M
    (*p)[offset++] = top;
541
32.8M
    (*p)[offset++] = left;
542
543
    // local gradient
544
32.8M
    (*p)[offset] = left - (*p)[offset + 1];
545
32.8M
    offset++;
546
    // local gradient
547
32.8M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
32.8M
    (*p)[offset++] = left - topleft;
551
32.8M
    (*p)[offset++] = topleft - top;
552
32.8M
    (*p)[offset++] = top - topright;
553
32.8M
    (*p)[offset++] = top - toptop;
554
32.8M
    (*p)[offset++] = left - leftleft;
555
32.8M
  }
556
557
32.8M
  pixel_type_w wp_pred = 0;
558
32.8M
  if (mode & kUseWP) {
559
32.8M
    wp_pred = wp_state->Predict<compute_properties>(
560
32.8M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
32.8M
  }
562
32.8M
  if (!nec && compute_properties) {
563
32.8M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
32.8M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
71.6M
    for (size_t i = 0; i < references->w; i++) {
567
38.8M
      (*p)[offset++] = rp[i];
568
38.8M
    }
569
32.8M
  }
570
32.8M
  PredictionResult result;
571
32.8M
  if (mode & kUseTree) {
572
32.8M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
32.8M
    result.context = lr.context;
574
32.8M
    result.guess = lr.offset;
575
32.8M
    result.multiplier = lr.multiplier;
576
32.8M
    predictor = lr.predictor;
577
32.8M
  }
578
32.8M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
32.8M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
32.8M
                             leftleft, toprightright, wp_pred);
587
32.8M
  result.predictor = predictor;
588
589
32.8M
  return result;
590
32.8M
}
jxl::PredictionResult jxl::detail::Predict<19>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
109M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
109M
  size_t offset = 3;
522
109M
  constexpr bool compute_properties =
523
109M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
109M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
109M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
109M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
109M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
109M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
109M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
109M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
109M
  pixel_type_w toprightright =
532
109M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
109M
  if (compute_properties) {
535
    // location
536
109M
    (*p)[offset++] = x;
537
    // neighbors
538
109M
    (*p)[offset++] = top > 0 ? top : -top;
539
109M
    (*p)[offset++] = left > 0 ? left : -left;
540
109M
    (*p)[offset++] = top;
541
109M
    (*p)[offset++] = left;
542
543
    // local gradient
544
109M
    (*p)[offset] = left - (*p)[offset + 1];
545
109M
    offset++;
546
    // local gradient
547
109M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
109M
    (*p)[offset++] = left - topleft;
551
109M
    (*p)[offset++] = topleft - top;
552
109M
    (*p)[offset++] = top - topright;
553
109M
    (*p)[offset++] = top - toptop;
554
109M
    (*p)[offset++] = left - leftleft;
555
109M
  }
556
557
109M
  pixel_type_w wp_pred = 0;
558
109M
  if (mode & kUseWP) {
559
109M
    wp_pred = wp_state->Predict<compute_properties>(
560
109M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
109M
  }
562
109M
  if (!nec && compute_properties) {
563
0
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
0
    for (size_t i = 0; i < references->w; i++) {
567
0
      (*p)[offset++] = rp[i];
568
0
    }
569
0
  }
570
109M
  PredictionResult result;
571
109M
  if (mode & kUseTree) {
572
109M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
109M
    result.context = lr.context;
574
109M
    result.guess = lr.offset;
575
109M
    result.multiplier = lr.multiplier;
576
109M
    predictor = lr.predictor;
577
109M
  }
578
109M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
109M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
109M
                             leftleft, toprightright, wp_pred);
587
109M
  result.predictor = predictor;
588
589
109M
  return result;
590
109M
}
jxl::PredictionResult jxl::detail::Predict<6>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
1.83M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
1.83M
  size_t offset = 3;
522
1.83M
  constexpr bool compute_properties =
523
1.83M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
1.83M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
1.83M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
1.83M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
1.83M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
1.83M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
1.83M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
1.83M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
1.83M
  pixel_type_w toprightright =
532
1.83M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
1.83M
  if (compute_properties) {
535
    // location
536
1.83M
    (*p)[offset++] = x;
537
    // neighbors
538
1.83M
    (*p)[offset++] = top > 0 ? top : -top;
539
1.83M
    (*p)[offset++] = left > 0 ? left : -left;
540
1.83M
    (*p)[offset++] = top;
541
1.83M
    (*p)[offset++] = left;
542
543
    // local gradient
544
1.83M
    (*p)[offset] = left - (*p)[offset + 1];
545
1.83M
    offset++;
546
    // local gradient
547
1.83M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
1.83M
    (*p)[offset++] = left - topleft;
551
1.83M
    (*p)[offset++] = topleft - top;
552
1.83M
    (*p)[offset++] = top - topright;
553
1.83M
    (*p)[offset++] = top - toptop;
554
1.83M
    (*p)[offset++] = left - leftleft;
555
1.83M
  }
556
557
1.83M
  pixel_type_w wp_pred = 0;
558
1.83M
  if (mode & kUseWP) {
559
1.83M
    wp_pred = wp_state->Predict<compute_properties>(
560
1.83M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
1.83M
  }
562
1.83M
  if (!nec && compute_properties) {
563
1.83M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
1.83M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
1.83M
    for (size_t i = 0; i < references->w; i++) {
567
0
      (*p)[offset++] = rp[i];
568
0
    }
569
1.83M
  }
570
1.83M
  PredictionResult result;
571
1.83M
  if (mode & kUseTree) {
572
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
0
    result.context = lr.context;
574
0
    result.guess = lr.offset;
575
0
    result.multiplier = lr.multiplier;
576
0
    predictor = lr.predictor;
577
0
  }
578
1.83M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
1.83M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
1.83M
                             leftleft, toprightright, wp_pred);
587
1.83M
  result.predictor = predictor;
588
589
1.83M
  return result;
590
1.83M
}
Unexecuted instantiation: jxl::PredictionResult jxl::detail::Predict<14>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
jxl::PredictionResult jxl::detail::Predict<22>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Line
Count
Source
519
25.8M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
25.8M
  size_t offset = 3;
522
25.8M
  constexpr bool compute_properties =
523
25.8M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
25.8M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
25.8M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
25.8M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
25.8M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
25.8M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
25.8M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
25.8M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
25.8M
  pixel_type_w toprightright =
532
25.8M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
25.8M
  if (compute_properties) {
535
    // location
536
25.8M
    (*p)[offset++] = x;
537
    // neighbors
538
25.8M
    (*p)[offset++] = top > 0 ? top : -top;
539
25.8M
    (*p)[offset++] = left > 0 ? left : -left;
540
25.8M
    (*p)[offset++] = top;
541
25.8M
    (*p)[offset++] = left;
542
543
    // local gradient
544
25.8M
    (*p)[offset] = left - (*p)[offset + 1];
545
25.8M
    offset++;
546
    // local gradient
547
25.8M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
25.8M
    (*p)[offset++] = left - topleft;
551
25.8M
    (*p)[offset++] = topleft - top;
552
25.8M
    (*p)[offset++] = top - topright;
553
25.8M
    (*p)[offset++] = top - toptop;
554
25.8M
    (*p)[offset++] = left - leftleft;
555
25.8M
  }
556
557
25.8M
  pixel_type_w wp_pred = 0;
558
25.8M
  if (mode & kUseWP) {
559
25.8M
    wp_pred = wp_state->Predict<compute_properties>(
560
25.8M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
25.8M
  }
562
25.8M
  if (!nec && compute_properties) {
563
0
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
0
    for (size_t i = 0; i < references->w; i++) {
567
0
      (*p)[offset++] = rp[i];
568
0
    }
569
0
  }
570
25.8M
  PredictionResult result;
571
25.8M
  if (mode & kUseTree) {
572
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
0
    result.context = lr.context;
574
0
    result.guess = lr.offset;
575
0
    result.multiplier = lr.multiplier;
576
0
    predictor = lr.predictor;
577
0
  }
578
25.8M
  if (mode & kAllPredictions) {
579
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
580
0
      predictions[i] =
581
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
582
0
                     topright, leftleft, toprightright, wp_pred);
583
0
    }
584
0
  }
585
25.8M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
25.8M
                             leftleft, toprightright, wp_pred);
587
25.8M
  result.predictor = predictor;
588
589
25.8M
  return result;
590
25.8M
}
Unexecuted instantiation: jxl::PredictionResult jxl::detail::Predict<30>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
Unexecuted instantiation: jxl::PredictionResult jxl::detail::Predict<8>(std::__1::vector<int, std::__1::allocator<int> >*, unsigned long, int const*, long, unsigned long, unsigned long, jxl::Predictor, jxl::MATreeLookup const*, jxl::Channel const*, jxl::weighted::State*, long*)
591
}  // namespace detail
592
593
inline PredictionResult PredictNoTreeNoWP(size_t w,
594
                                          const pixel_type *JXL_RESTRICT pp,
595
                                          const ptrdiff_t onerow, const int x,
596
19.5M
                                          const int y, Predictor predictor) {
597
19.5M
  return detail::Predict</*mode=*/0>(
598
19.5M
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
599
19.5M
      /*references=*/nullptr, /*wp_state=*/nullptr, /*predictions=*/nullptr);
600
19.5M
}
601
602
inline PredictionResult PredictNoTreeWP(size_t w,
603
                                        const pixel_type *JXL_RESTRICT pp,
604
                                        const ptrdiff_t onerow, const int x,
605
                                        const int y, Predictor predictor,
606
51.5M
                                        weighted::State *wp_state) {
607
51.5M
  return detail::Predict<detail::kUseWP>(
608
51.5M
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
609
51.5M
      /*references=*/nullptr, wp_state, /*predictions=*/nullptr);
610
51.5M
}
611
612
inline PredictionResult PredictTreeNoWP(Properties *p, size_t w,
613
                                        const pixel_type *JXL_RESTRICT pp,
614
                                        const ptrdiff_t onerow, const int x,
615
                                        const int y,
616
                                        const MATreeLookup &tree_lookup,
617
90.4M
                                        const Channel &references) {
618
90.4M
  return detail::Predict<detail::kUseTree>(
619
90.4M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
620
90.4M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
621
90.4M
}
622
// Only use for y > 1, x > 1, x < w-2, and empty references
623
JXL_INLINE PredictionResult
624
PredictTreeNoWPNEC(Properties *p, size_t w, const pixel_type *JXL_RESTRICT pp,
625
                   const ptrdiff_t onerow, const int x, const int y,
626
451M
                   const MATreeLookup &tree_lookup, const Channel &references) {
627
451M
  return detail::Predict<detail::kUseTree | detail::kNoEdgeCases>(
628
451M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
629
451M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
630
451M
}
631
632
inline PredictionResult PredictTreeWP(Properties *p, size_t w,
633
                                      const pixel_type *JXL_RESTRICT pp,
634
                                      const ptrdiff_t onerow, const int x,
635
                                      const int y,
636
                                      const MATreeLookup &tree_lookup,
637
                                      const Channel &references,
638
32.8M
                                      weighted::State *wp_state) {
639
32.8M
  return detail::Predict<detail::kUseTree | detail::kUseWP>(
640
32.8M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
641
32.8M
      wp_state, /*predictions=*/nullptr);
642
32.8M
}
643
JXL_INLINE PredictionResult PredictTreeWPNEC(Properties *p, size_t w,
644
                                             const pixel_type *JXL_RESTRICT pp,
645
                                             const ptrdiff_t onerow, const int x,
646
                                             const int y,
647
                                             const MATreeLookup &tree_lookup,
648
                                             const Channel &references,
649
109M
                                             weighted::State *wp_state) {
650
109M
  return detail::Predict<detail::kUseTree | detail::kUseWP |
651
109M
                         detail::kNoEdgeCases>(
652
109M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
653
109M
      wp_state, /*predictions=*/nullptr);
654
109M
}
655
656
inline PredictionResult PredictLearn(Properties *p, size_t w,
657
                                     const pixel_type *JXL_RESTRICT pp,
658
                                     const ptrdiff_t onerow, const int x,
659
                                     const int y, Predictor predictor,
660
                                     const Channel &references,
661
1.83M
                                     weighted::State *wp_state) {
662
1.83M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP>(
663
1.83M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
664
1.83M
      wp_state, /*predictions=*/nullptr);
665
1.83M
}
666
667
inline void PredictLearnAll(Properties *p, size_t w,
668
                            const pixel_type *JXL_RESTRICT pp,
669
                            const ptrdiff_t onerow, const int x, const int y,
670
                            const Channel &references,
671
                            weighted::State *wp_state,
672
0
                            pixel_type_w *predictions) {
673
0
  detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
674
0
                  detail::kAllPredictions>(
675
0
      p, w, pp, onerow, x, y, Predictor::Zero,
676
0
      /*lookup=*/nullptr, &references, wp_state, predictions);
677
0
}
678
inline PredictionResult PredictLearnNEC(Properties *p, size_t w,
679
                                        const pixel_type *JXL_RESTRICT pp,
680
                                        const ptrdiff_t onerow, const int x,
681
                                        const int y, Predictor predictor,
682
                                        const Channel &references,
683
25.8M
                                        weighted::State *wp_state) {
684
25.8M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
685
25.8M
                         detail::kNoEdgeCases>(
686
25.8M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
687
25.8M
      wp_state, /*predictions=*/nullptr);
688
25.8M
}
689
690
inline void PredictLearnAllNEC(Properties *p, size_t w,
691
                               const pixel_type *JXL_RESTRICT pp,
692
                               const ptrdiff_t onerow, const int x, const int y,
693
                               const Channel &references,
694
                               weighted::State *wp_state,
695
0
                               pixel_type_w *predictions) {
696
0
  detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
697
0
                  detail::kAllPredictions | detail::kNoEdgeCases>(
698
0
      p, w, pp, onerow, x, y, Predictor::Zero,
699
0
      /*lookup=*/nullptr, &references, wp_state, predictions);
700
0
}
701
702
inline void PredictAllNoWP(size_t w, const pixel_type *JXL_RESTRICT pp,
703
                           const ptrdiff_t onerow, const int x, const int y,
704
0
                           pixel_type_w *predictions) {
705
0
  detail::Predict<detail::kAllPredictions>(
706
0
      /*p=*/nullptr, w, pp, onerow, x, y, Predictor::Zero,
707
0
      /*lookup=*/nullptr,
708
0
      /*references=*/nullptr, /*wp_state=*/nullptr, predictions);
709
0
}
710
}  // namespace jxl
711
712
#endif  // LIB_JXL_MODULAR_ENCODING_CONTEXT_PREDICT_H_