Coverage Report

Created: 2025-12-13 07:57

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
166k
  Header() { Bundle::Init(this); }
37
38
256k
  Status VisitFields(Visitor *JXL_RESTRICT visitor) override {
39
256k
    if (visitor->AllDefault(*this, &all_default)) {
40
      // Overwrite all serialized fields, but not any nonserialized_*.
41
43.6k
      visitor->SetDefault(this);
42
43.6k
      return true;
43
43.6k
    }
44
1.49M
    auto visit_p = [visitor](pixel_type val, pixel_type *p) -> Status {
45
1.49M
      uint32_t up = *p;
46
1.49M
      JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(5, val, &up));
47
1.49M
      *p = up;
48
1.49M
      return true;
49
1.49M
    };
50
213k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(16, &p1C));
51
213k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(10, &p2C));
52
213k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Ca));
53
213k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cb));
54
213k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cc));
55
213k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Cd));
56
213k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Ce));
57
213k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xd, &w[0]));
58
213k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[1]));
59
213k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[2]));
60
213k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[3]));
61
213k
    return true;
62
213k
  }
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.08G
  constexpr static pixel_type_w AddBits(pixel_type_w x) {
90
1.08G
    return static_cast<uint64_t>(x) << kPredExtraBits;
91
1.08G
  }
92
93
28.4k
  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
113k
    for (auto &pred_error : pred_errors) {
97
113k
      pred_error.resize((xsize + 2) * 2);
98
113k
    }
99
28.4k
    error.resize((xsize + 2) * 2);
100
28.4k
  }
101
102
  // Approximates 4+(maxweight<<24)/(x+1), avoiding division
103
708M
  JXL_INLINE uint32_t ErrorWeight(uint64_t x, uint32_t maxweight) const {
104
708M
    int shift = static_cast<int>(FloorLog2Nonzero(x + 1)) - 5;
105
708M
    if (shift < 0) shift = 0;
106
708M
    return 4 + ((maxweight * divlookup[x >> shift]) >> shift);
107
708M
  }
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
177M
                  std::array<uint32_t, kNumPredictors> w) const {
114
177M
    uint32_t weight_sum = 0;
115
885M
    for (size_t i = 0; i < kNumPredictors; i++) {
116
708M
      weight_sum += w[i];
117
708M
    }
118
177M
    JXL_DASSERT(weight_sum > 15);
119
177M
    uint32_t log_weight = FloorLog2Nonzero(weight_sum);  // at least 4.
120
177M
    weight_sum = 0;
121
885M
    for (size_t i = 0; i < kNumPredictors; i++) {
122
708M
      w[i] >>= log_weight - 4;
123
708M
      weight_sum += w[i];
124
708M
    }
125
    // for rounding.
126
177M
    pixel_type_w sum = (weight_sum >> 1) - 1;
127
885M
    for (size_t i = 0; i < kNumPredictors; i++) {
128
708M
      sum += p[i] * w[i];
129
708M
    }
130
177M
    return (sum * divlookup[weight_sum - 1]) >> 24;
131
177M
  }
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
177M
                                  size_t offset) {
139
177M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
177M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
177M
    size_t pos_N = prev_row + x;
142
177M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
177M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
177M
    std::array<uint32_t, kNumPredictors> weights;
145
885M
    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
708M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
708M
                   pred_errors[i][pos_NW];
150
708M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
708M
    }
152
153
177M
    N = AddBits(N);
154
177M
    W = AddBits(W);
155
177M
    NE = AddBits(NE);
156
177M
    NW = AddBits(NW);
157
177M
    NN = AddBits(NN);
158
159
177M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
177M
    pixel_type_w teN = error[pos_N];
161
177M
    pixel_type_w teNW = error[pos_NW];
162
177M
    pixel_type_w sumWN = teN + teW;
163
177M
    pixel_type_w teNE = error[pos_NE];
164
165
177M
    if (compute_properties) {
166
163M
      pixel_type_w p = teW;
167
163M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
163M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
163M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
163M
      (*properties)[offset++] = p;
171
163M
    }
172
173
177M
    prediction[0] = W + NE - N;
174
177M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
177M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
177M
    prediction[3] =
177
177M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
177M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
177M
             5);
180
181
177M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
177M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
45.4M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
45.4M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
131M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
131M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
131M
    pred = std::max(mn, std::min(mx, pred));
192
131M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
177M
  }
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
13.3M
                                  size_t offset) {
139
13.3M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
13.3M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
13.3M
    size_t pos_N = prev_row + x;
142
13.3M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
13.3M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
13.3M
    std::array<uint32_t, kNumPredictors> weights;
145
66.5M
    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
53.2M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
53.2M
                   pred_errors[i][pos_NW];
150
53.2M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
53.2M
    }
152
153
13.3M
    N = AddBits(N);
154
13.3M
    W = AddBits(W);
155
13.3M
    NE = AddBits(NE);
156
13.3M
    NW = AddBits(NW);
157
13.3M
    NN = AddBits(NN);
158
159
13.3M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
13.3M
    pixel_type_w teN = error[pos_N];
161
13.3M
    pixel_type_w teNW = error[pos_NW];
162
13.3M
    pixel_type_w sumWN = teN + teW;
163
13.3M
    pixel_type_w teNE = error[pos_NE];
164
165
13.3M
    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
13.3M
    prediction[0] = W + NE - N;
174
13.3M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
13.3M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
13.3M
    prediction[3] =
177
13.3M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
13.3M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
13.3M
             5);
180
181
13.3M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
13.3M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
1.87M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
1.87M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
11.4M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
11.4M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
11.4M
    pred = std::max(mn, std::min(mx, pred));
192
11.4M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
13.3M
  }
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
163M
                                  size_t offset) {
139
163M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
163M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
163M
    size_t pos_N = prev_row + x;
142
163M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
163M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
163M
    std::array<uint32_t, kNumPredictors> weights;
145
819M
    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
655M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
655M
                   pred_errors[i][pos_NW];
150
655M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
655M
    }
152
153
163M
    N = AddBits(N);
154
163M
    W = AddBits(W);
155
163M
    NE = AddBits(NE);
156
163M
    NW = AddBits(NW);
157
163M
    NN = AddBits(NN);
158
159
163M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
163M
    pixel_type_w teN = error[pos_N];
161
163M
    pixel_type_w teNW = error[pos_NW];
162
163M
    pixel_type_w sumWN = teN + teW;
163
163M
    pixel_type_w teNE = error[pos_NE];
164
165
163M
    if (compute_properties) {
166
163M
      pixel_type_w p = teW;
167
163M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
163M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
163M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
163M
      (*properties)[offset++] = p;
171
163M
    }
172
173
163M
    prediction[0] = W + NE - N;
174
163M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
163M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
163M
    prediction[3] =
177
163M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
163M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
163M
             5);
180
181
163M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
163M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
43.5M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
43.5M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
120M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
120M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
120M
    pred = std::max(mn, std::min(mx, pred));
192
120M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
163M
  }
194
195
  JXL_INLINE void UpdateErrors(pixel_type_w val, size_t x, size_t y,
196
195M
                               size_t xsize) {
197
195M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
198
195M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
199
195M
    val = AddBits(val);
200
195M
    error[cur_row + x] = pred - val;
201
978M
    for (size_t i = 0; i < kNumPredictors; i++) {
202
782M
      pixel_type_w err =
203
782M
          (std::abs(prediction[i] - val) + kPredictionRound) >> kPredExtraBits;
204
      // For predicting in the next row.
205
782M
      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
782M
      pred_errors[i][prev_row + x + 1] += err;
209
782M
    }
210
195M
  }
211
};
212
213
// Encoder helper function to set the parameters to some presets.
214
1.86k
inline void PredictorMode(int i, Header *header) {
215
1.86k
  switch (i) {
216
1.86k
    case 0:
217
      // ~ lossless16 predictor
218
1.86k
      header->w[0] = 0xd;
219
1.86k
      header->w[1] = 0xc;
220
1.86k
      header->w[2] = 0xc;
221
1.86k
      header->w[3] = 0xc;
222
1.86k
      header->p1C = 16;
223
1.86k
      header->p2C = 10;
224
1.86k
      header->p3Ca = 7;
225
1.86k
      header->p3Cb = 7;
226
1.86k
      header->p3Cc = 7;
227
1.86k
      header->p3Cd = 0;
228
1.86k
      header->p3Ce = 0;
229
1.86k
      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
1.86k
  }
288
1.86k
}
289
}  // namespace weighted
290
291
// Returns true if the (meta)predictor makes use of the weighted predictor.
292
10.2k
inline bool PredictorHasWeighted(Predictor predictor) {
293
  // Use a non-defaulted switch to generate a warning if a case is missing.
294
10.2k
  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
8.36k
    case Predictor::Gradient:
301
8.36k
      return false;
302
1.86k
    case Predictor::Weighted:
303
1.86k
      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
10.2k
  }
316
317
0
  return false;
318
10.2k
}
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
41.4k
  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
144M
  JXL_INLINE LookupResult Lookup(const Properties &properties) const {
352
144M
    uint32_t pos = 0;
353
250M
    while (true) {
354
250M
#define TRAVERSE_THE_TREE                                                \
355
412M
  {                                                                      \
356
412M
    const FlatDecisionNode &node = nodes_[pos];                          \
357
412M
    if (node.property0 < 0) {                                            \
358
144M
      return {node.childID, node.predictor, node.predictor_offset,       \
359
144M
              node.multiplier};                                          \
360
144M
    }                                                                    \
361
412M
    bool p0 = properties[node.property0] <= node.splitval0;              \
362
267M
    uint32_t off0 = properties[node.properties[0]] <= node.splitvals[0]; \
363
267M
    uint32_t off1 =                                                      \
364
267M
        2 | int{properties[node.properties[1]] <= node.splitvals[1]};    \
365
267M
    pos = node.childID + (p0 ? off1 : off0);                             \
366
267M
  }
367
368
250M
      TRAVERSE_THE_TREE;
369
162M
      TRAVERSE_THE_TREE;
370
105M
    }
371
144M
  }
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
169M
                                          const int32_t l) {
387
169M
  const int32_t m = std::min(n, w);
388
169M
  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
169M
  const int32_t grad =
395
169M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
169M
                           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
169M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
169M
  return (l > M) ? m : grad_clamp_M;
402
169M
}
Unexecuted instantiation: enc_frame.cc:jxl::ClampedGradient(int, int, int)
enc_modular.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
386
8.68M
                                          const int32_t l) {
387
8.68M
  const int32_t m = std::min(n, w);
388
8.68M
  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
8.68M
  const int32_t grad =
395
8.68M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
8.68M
                           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
8.68M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
8.68M
  return (l > M) ? m : grad_clamp_M;
402
8.68M
}
Unexecuted instantiation: enc_patch_dictionary.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_quant_weights.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: enc_heuristics.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)
enc_encoding.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
386
146M
                                          const int32_t l) {
387
146M
  const int32_t m = std::min(n, w);
388
146M
  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
146M
  const int32_t grad =
395
146M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
146M
                           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
146M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
146M
  return (l > M) ? m : grad_clamp_M;
402
146M
}
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: enc_palette.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: coeff_order.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: decode.cc:jxl::ClampedGradient(int, int, int)
encoding.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
386
14.3M
                                          const int32_t l) {
387
14.3M
  const int32_t m = std::min(n, w);
388
14.3M
  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
14.3M
  const int32_t grad =
395
14.3M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
396
14.3M
                           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
14.3M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
401
14.3M
  return (l > M) ? m : grad_clamp_M;
402
14.3M
}
Unexecuted instantiation: modular_image.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: transform.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: rct.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: palette.cc:jxl::ClampedGradient(int, int, int)
Unexecuted instantiation: quant_weights.cc:jxl::ClampedGradient(int, int, int)
403
404
3.09M
inline pixel_type_w Select(pixel_type_w a, pixel_type_w b, pixel_type_w c) {
405
3.09M
  pixel_type_w p = a + b - c;
406
3.09M
  pixel_type_w pa = std::abs(p - a);
407
3.09M
  pixel_type_w pb = std::abs(p - b);
408
3.09M
  return pa < pb ? a : b;
409
3.09M
}
410
411
inline void PrecomputeReferences(const Channel &ch, size_t y,
412
                                 const Image &image, uint32_t i,
413
1.73M
                                 Channel *references) {
414
1.73M
  ZeroFillImage(&references->plane);
415
1.73M
  uint32_t offset = 0;
416
1.73M
  size_t num_extra_props = references->w;
417
1.73M
  ptrdiff_t onerow = references->plane.PixelsPerRow();
418
1.73M
  for (int32_t j = static_cast<int32_t>(i) - 1;
419
1.94M
       j >= 0 && offset < num_extra_props; j--) {
420
203k
    if (image.channel[j].w != image.channel[i].w ||
421
171k
        image.channel[j].h != image.channel[i].h) {
422
171k
      continue;
423
171k
    }
424
31.3k
    if (image.channel[j].hshift != image.channel[i].hshift) continue;
425
26.7k
    if (image.channel[j].vshift != image.channel[i].vshift) continue;
426
26.0k
    pixel_type *JXL_RESTRICT rp = references->Row(0) + offset;
427
26.0k
    const pixel_type *JXL_RESTRICT rpp = image.channel[j].Row(y);
428
26.0k
    const pixel_type *JXL_RESTRICT rpprev = image.channel[j].Row(y ? y - 1 : 0);
429
211k
    for (size_t x = 0; x < ch.w; x++, rp += onerow) {
430
185k
      pixel_type_w v = rpp[x];
431
185k
      rp[0] = std::abs(v);
432
185k
      rp[1] = v;
433
185k
      pixel_type_w vleft = (x ? rpp[x - 1] : 0);
434
185k
      pixel_type_w vtop = (y ? rpprev[x] : vleft);
435
185k
      pixel_type_w vtopleft = (x && y ? rpprev[x - 1] : vleft);
436
185k
      pixel_type_w vpredicted = ClampedGradient(vleft, vtop, vtopleft);
437
185k
      rp[2] = std::abs(v - vpredicted);
438
185k
      rp[3] = v - vpredicted;
439
185k
    }
440
441
26.0k
    offset += kExtraPropsPerChannel;
442
26.0k
  }
443
1.73M
}
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
1.73M
    const int y) {
456
5.21M
  for (size_t i = 0; i < kNumStaticProperties; i++) {
457
3.47M
    (*p)[i] = static_props[i];
458
3.47M
  }
459
1.73M
  (*p)[2] = y;
460
1.73M
  (*p)[9] = 0;  // local gradient.
461
1.73M
}
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
284M
                                   pixel_type_w wp_pred) {
478
284M
  switch (p) {
479
37.7M
    case Predictor::Zero:
480
37.7M
      return pixel_type_w{0};
481
20.0M
    case Predictor::Left:
482
20.0M
      return left;
483
34.6M
    case Predictor::Top:
484
34.6M
      return top;
485
3.09M
    case Predictor::Select:
486
3.09M
      return Select(left, top, topleft);
487
15.7M
    case Predictor::Weighted:
488
15.7M
      return wp_pred;
489
159M
    case Predictor::Gradient:
490
159M
      return pixel_type_w{ClampedGradient(left, top, topleft)};
491
1.18M
    case Predictor::TopLeft:
492
1.18M
      return topleft;
493
25.0k
    case Predictor::TopRight:
494
25.0k
      return topright;
495
661k
    case Predictor::LeftLeft:
496
661k
      return leftleft;
497
1.21M
    case Predictor::Average0:
498
1.21M
      return (left + top) / 2;
499
49.6k
    case Predictor::Average1:
500
49.6k
      return (left + topleft) / 2;
501
70.5k
    case Predictor::Average2:
502
70.5k
      return (topleft + top) / 2;
503
3.03M
    case Predictor::Average3:
504
3.03M
      return (top + topright) / 2;
505
7.58M
    case Predictor::Average4:
506
7.58M
      return (6 * top - 2 * toptop + 7 * left + 1 * leftleft +
507
7.58M
              1 * toprightright + 3 * topright + 8) /
508
7.58M
             16;
509
0
    default:
510
0
      return pixel_type_w{0};
511
284M
  }
512
284M
}
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
284M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
284M
  size_t offset = 3;
522
284M
  constexpr bool compute_properties =
523
284M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
284M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
284M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
284M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
284M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
284M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
284M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
284M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
284M
  pixel_type_w toprightright =
532
284M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
284M
  if (compute_properties) {
535
    // location
536
217M
    (*p)[offset++] = x;
537
    // neighbors
538
217M
    (*p)[offset++] = top > 0 ? top : -top;
539
217M
    (*p)[offset++] = left > 0 ? left : -left;
540
217M
    (*p)[offset++] = top;
541
217M
    (*p)[offset++] = left;
542
543
    // local gradient
544
217M
    (*p)[offset] = left - (*p)[offset + 1];
545
217M
    offset++;
546
    // local gradient
547
217M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
217M
    (*p)[offset++] = left - topleft;
551
217M
    (*p)[offset++] = topleft - top;
552
217M
    (*p)[offset++] = top - topright;
553
217M
    (*p)[offset++] = top - toptop;
554
217M
    (*p)[offset++] = left - leftleft;
555
217M
  }
556
557
284M
  pixel_type_w wp_pred = 0;
558
284M
  if (mode & kUseWP) {
559
165M
    wp_pred = wp_state->Predict<compute_properties>(
560
165M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
165M
  }
562
284M
  if (!nec && compute_properties) {
563
82.9M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
82.9M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
86.8M
    for (size_t i = 0; i < references->w; i++) {
567
3.86M
      (*p)[offset++] = rp[i];
568
3.86M
    }
569
82.9M
  }
570
284M
  PredictionResult result;
571
284M
  if (mode & kUseTree) {
572
144M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
144M
    result.context = lr.context;
574
144M
    result.guess = lr.offset;
575
144M
    result.multiplier = lr.multiplier;
576
144M
    predictor = lr.predictor;
577
144M
  }
578
284M
  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
284M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
284M
                             leftleft, toprightright, wp_pred);
587
284M
  result.predictor = predictor;
588
589
284M
  return result;
590
284M
}
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
53.7M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
53.7M
  size_t offset = 3;
522
53.7M
  constexpr bool compute_properties =
523
53.7M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
53.7M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
53.7M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
53.7M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
53.7M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
53.7M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
53.7M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
53.7M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
53.7M
  pixel_type_w toprightright =
532
53.7M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
53.7M
  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
53.7M
  pixel_type_w wp_pred = 0;
558
53.7M
  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
53.7M
  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
53.7M
  PredictionResult result;
571
53.7M
  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
53.7M
  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
53.7M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
53.7M
                             leftleft, toprightright, wp_pred);
587
53.7M
  result.predictor = predictor;
588
589
53.7M
  return result;
590
53.7M
}
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
13.3M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
13.3M
  size_t offset = 3;
522
13.3M
  constexpr bool compute_properties =
523
13.3M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
13.3M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
13.3M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
13.3M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
13.3M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
13.3M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
13.3M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
13.3M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
13.3M
  pixel_type_w toprightright =
532
13.3M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
13.3M
  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
13.3M
  pixel_type_w wp_pred = 0;
558
13.3M
  if (mode & kUseWP) {
559
13.3M
    wp_pred = wp_state->Predict<compute_properties>(
560
13.3M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
13.3M
  }
562
13.3M
  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
13.3M
  PredictionResult result;
571
13.3M
  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
13.3M
  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
13.3M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
13.3M
                             leftleft, toprightright, wp_pred);
587
13.3M
  result.predictor = predictor;
588
589
13.3M
  return result;
590
13.3M
}
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
14.5M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
14.5M
  size_t offset = 3;
522
14.5M
  constexpr bool compute_properties =
523
14.5M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
14.5M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
14.5M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
14.5M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
14.5M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
14.5M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
14.5M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
14.5M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
14.5M
  pixel_type_w toprightright =
532
14.5M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
14.5M
  if (compute_properties) {
535
    // location
536
14.5M
    (*p)[offset++] = x;
537
    // neighbors
538
14.5M
    (*p)[offset++] = top > 0 ? top : -top;
539
14.5M
    (*p)[offset++] = left > 0 ? left : -left;
540
14.5M
    (*p)[offset++] = top;
541
14.5M
    (*p)[offset++] = left;
542
543
    // local gradient
544
14.5M
    (*p)[offset] = left - (*p)[offset + 1];
545
14.5M
    offset++;
546
    // local gradient
547
14.5M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
14.5M
    (*p)[offset++] = left - topleft;
551
14.5M
    (*p)[offset++] = topleft - top;
552
14.5M
    (*p)[offset++] = top - topright;
553
14.5M
    (*p)[offset++] = top - toptop;
554
14.5M
    (*p)[offset++] = left - leftleft;
555
14.5M
  }
556
557
14.5M
  pixel_type_w wp_pred = 0;
558
14.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
14.5M
  if (!nec && compute_properties) {
563
14.5M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
14.5M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
16.1M
    for (size_t i = 0; i < references->w; i++) {
567
1.65M
      (*p)[offset++] = rp[i];
568
1.65M
    }
569
14.5M
  }
570
14.5M
  PredictionResult result;
571
14.5M
  if (mode & kUseTree) {
572
14.5M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
14.5M
    result.context = lr.context;
574
14.5M
    result.guess = lr.offset;
575
14.5M
    result.multiplier = lr.multiplier;
576
14.5M
    predictor = lr.predictor;
577
14.5M
  }
578
14.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
14.5M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
14.5M
                             leftleft, toprightright, wp_pred);
587
14.5M
  result.predictor = predictor;
588
589
14.5M
  return result;
590
14.5M
}
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
51.2M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
51.2M
  size_t offset = 3;
522
51.2M
  constexpr bool compute_properties =
523
51.2M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
51.2M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
51.2M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
51.2M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
51.2M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
51.2M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
51.2M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
51.2M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
51.2M
  pixel_type_w toprightright =
532
51.2M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
51.2M
  if (compute_properties) {
535
    // location
536
51.2M
    (*p)[offset++] = x;
537
    // neighbors
538
51.2M
    (*p)[offset++] = top > 0 ? top : -top;
539
51.2M
    (*p)[offset++] = left > 0 ? left : -left;
540
51.2M
    (*p)[offset++] = top;
541
51.2M
    (*p)[offset++] = left;
542
543
    // local gradient
544
51.2M
    (*p)[offset] = left - (*p)[offset + 1];
545
51.2M
    offset++;
546
    // local gradient
547
51.2M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
51.2M
    (*p)[offset++] = left - topleft;
551
51.2M
    (*p)[offset++] = topleft - top;
552
51.2M
    (*p)[offset++] = top - topright;
553
51.2M
    (*p)[offset++] = top - toptop;
554
51.2M
    (*p)[offset++] = left - leftleft;
555
51.2M
  }
556
557
51.2M
  pixel_type_w wp_pred = 0;
558
51.2M
  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
51.2M
  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.2M
  PredictionResult result;
571
51.2M
  if (mode & kUseTree) {
572
51.2M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
51.2M
    result.context = lr.context;
574
51.2M
    result.guess = lr.offset;
575
51.2M
    result.multiplier = lr.multiplier;
576
51.2M
    predictor = lr.predictor;
577
51.2M
  }
578
51.2M
  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.2M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
51.2M
                             leftleft, toprightright, wp_pred);
587
51.2M
  result.predictor = predictor;
588
589
51.2M
  return result;
590
51.2M
}
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
65.2M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
65.2M
  size_t offset = 3;
522
65.2M
  constexpr bool compute_properties =
523
65.2M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
65.2M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
65.2M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
65.2M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
65.2M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
65.2M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
65.2M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
65.2M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
65.2M
  pixel_type_w toprightright =
532
65.2M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
65.2M
  if (compute_properties) {
535
    // location
536
65.2M
    (*p)[offset++] = x;
537
    // neighbors
538
65.2M
    (*p)[offset++] = top > 0 ? top : -top;
539
65.2M
    (*p)[offset++] = left > 0 ? left : -left;
540
65.2M
    (*p)[offset++] = top;
541
65.2M
    (*p)[offset++] = left;
542
543
    // local gradient
544
65.2M
    (*p)[offset] = left - (*p)[offset + 1];
545
65.2M
    offset++;
546
    // local gradient
547
65.2M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
65.2M
    (*p)[offset++] = left - topleft;
551
65.2M
    (*p)[offset++] = topleft - top;
552
65.2M
    (*p)[offset++] = top - topright;
553
65.2M
    (*p)[offset++] = top - toptop;
554
65.2M
    (*p)[offset++] = left - leftleft;
555
65.2M
  }
556
557
65.2M
  pixel_type_w wp_pred = 0;
558
65.2M
  if (mode & kUseWP) {
559
65.2M
    wp_pred = wp_state->Predict<compute_properties>(
560
65.2M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
65.2M
  }
562
65.2M
  if (!nec && compute_properties) {
563
65.2M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
65.2M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
67.4M
    for (size_t i = 0; i < references->w; i++) {
567
2.21M
      (*p)[offset++] = rp[i];
568
2.21M
    }
569
65.2M
  }
570
65.2M
  PredictionResult result;
571
65.2M
  if (mode & kUseTree) {
572
65.2M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
65.2M
    result.context = lr.context;
574
65.2M
    result.guess = lr.offset;
575
65.2M
    result.multiplier = lr.multiplier;
576
65.2M
    predictor = lr.predictor;
577
65.2M
  }
578
65.2M
  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
65.2M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
65.2M
                             leftleft, toprightright, wp_pred);
587
65.2M
  result.predictor = predictor;
588
589
65.2M
  return result;
590
65.2M
}
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
13.6M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
13.6M
  size_t offset = 3;
522
13.6M
  constexpr bool compute_properties =
523
13.6M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
13.6M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
13.6M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
13.6M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
13.6M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
13.6M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
13.6M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
13.6M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
13.6M
  pixel_type_w toprightright =
532
13.6M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
13.6M
  if (compute_properties) {
535
    // location
536
13.6M
    (*p)[offset++] = x;
537
    // neighbors
538
13.6M
    (*p)[offset++] = top > 0 ? top : -top;
539
13.6M
    (*p)[offset++] = left > 0 ? left : -left;
540
13.6M
    (*p)[offset++] = top;
541
13.6M
    (*p)[offset++] = left;
542
543
    // local gradient
544
13.6M
    (*p)[offset] = left - (*p)[offset + 1];
545
13.6M
    offset++;
546
    // local gradient
547
13.6M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
13.6M
    (*p)[offset++] = left - topleft;
551
13.6M
    (*p)[offset++] = topleft - top;
552
13.6M
    (*p)[offset++] = top - topright;
553
13.6M
    (*p)[offset++] = top - toptop;
554
13.6M
    (*p)[offset++] = left - leftleft;
555
13.6M
  }
556
557
13.6M
  pixel_type_w wp_pred = 0;
558
13.6M
  if (mode & kUseWP) {
559
13.6M
    wp_pred = wp_state->Predict<compute_properties>(
560
13.6M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
13.6M
  }
562
13.6M
  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
13.6M
  PredictionResult result;
571
13.6M
  if (mode & kUseTree) {
572
13.6M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
573
13.6M
    result.context = lr.context;
574
13.6M
    result.guess = lr.offset;
575
13.6M
    result.multiplier = lr.multiplier;
576
13.6M
    predictor = lr.predictor;
577
13.6M
  }
578
13.6M
  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
13.6M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
13.6M
                             leftleft, toprightright, wp_pred);
587
13.6M
  result.predictor = predictor;
588
589
13.6M
  return result;
590
13.6M
}
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
3.22M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
3.22M
  size_t offset = 3;
522
3.22M
  constexpr bool compute_properties =
523
3.22M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
3.22M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
3.22M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
3.22M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
3.22M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
3.22M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
3.22M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
3.22M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
3.22M
  pixel_type_w toprightright =
532
3.22M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
3.22M
  if (compute_properties) {
535
    // location
536
3.22M
    (*p)[offset++] = x;
537
    // neighbors
538
3.22M
    (*p)[offset++] = top > 0 ? top : -top;
539
3.22M
    (*p)[offset++] = left > 0 ? left : -left;
540
3.22M
    (*p)[offset++] = top;
541
3.22M
    (*p)[offset++] = left;
542
543
    // local gradient
544
3.22M
    (*p)[offset] = left - (*p)[offset + 1];
545
3.22M
    offset++;
546
    // local gradient
547
3.22M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
3.22M
    (*p)[offset++] = left - topleft;
551
3.22M
    (*p)[offset++] = topleft - top;
552
3.22M
    (*p)[offset++] = top - topright;
553
3.22M
    (*p)[offset++] = top - toptop;
554
3.22M
    (*p)[offset++] = left - leftleft;
555
3.22M
  }
556
557
3.22M
  pixel_type_w wp_pred = 0;
558
3.22M
  if (mode & kUseWP) {
559
3.22M
    wp_pred = wp_state->Predict<compute_properties>(
560
3.22M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
3.22M
  }
562
3.22M
  if (!nec && compute_properties) {
563
3.22M
    offset += weighted::kNumProperties;
564
    // Extra properties.
565
3.22M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
566
3.22M
    for (size_t i = 0; i < references->w; i++) {
567
0
      (*p)[offset++] = rp[i];
568
0
    }
569
3.22M
  }
570
3.22M
  PredictionResult result;
571
3.22M
  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
3.22M
  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
3.22M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
3.22M
                             leftleft, toprightright, wp_pred);
587
3.22M
  result.predictor = predictor;
588
589
3.22M
  return result;
590
3.22M
}
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
69.6M
    weighted::State *wp_state, pixel_type_w *predictions) {
520
  // We start in position 3 because of 2 static properties + y.
521
69.6M
  size_t offset = 3;
522
69.6M
  constexpr bool compute_properties =
523
69.6M
      !!(mode & kUseTree) || !!(mode & kForceComputeProperties);
524
69.6M
  constexpr bool nec = !!(mode & kNoEdgeCases);
525
69.6M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
526
69.6M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
527
69.6M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
528
69.6M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
529
69.6M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
530
69.6M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
531
69.6M
  pixel_type_w toprightright =
532
69.6M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
533
534
69.6M
  if (compute_properties) {
535
    // location
536
69.6M
    (*p)[offset++] = x;
537
    // neighbors
538
69.6M
    (*p)[offset++] = top > 0 ? top : -top;
539
69.6M
    (*p)[offset++] = left > 0 ? left : -left;
540
69.6M
    (*p)[offset++] = top;
541
69.6M
    (*p)[offset++] = left;
542
543
    // local gradient
544
69.6M
    (*p)[offset] = left - (*p)[offset + 1];
545
69.6M
    offset++;
546
    // local gradient
547
69.6M
    (*p)[offset++] = left + top - topleft;
548
549
    // FFV1 context properties
550
69.6M
    (*p)[offset++] = left - topleft;
551
69.6M
    (*p)[offset++] = topleft - top;
552
69.6M
    (*p)[offset++] = top - topright;
553
69.6M
    (*p)[offset++] = top - toptop;
554
69.6M
    (*p)[offset++] = left - leftleft;
555
69.6M
  }
556
557
69.6M
  pixel_type_w wp_pred = 0;
558
69.6M
  if (mode & kUseWP) {
559
69.6M
    wp_pred = wp_state->Predict<compute_properties>(
560
69.6M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
561
69.6M
  }
562
69.6M
  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
69.6M
  PredictionResult result;
571
69.6M
  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
69.6M
  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
69.6M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
586
69.6M
                             leftleft, toprightright, wp_pred);
587
69.6M
  result.predictor = predictor;
588
589
69.6M
  return result;
590
69.6M
}
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
53.7M
                                          const int y, Predictor predictor) {
597
53.7M
  return detail::Predict</*mode=*/0>(
598
53.7M
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
599
53.7M
      /*references=*/nullptr, /*wp_state=*/nullptr, /*predictions=*/nullptr);
600
53.7M
}
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
13.3M
                                        weighted::State *wp_state) {
607
13.3M
  return detail::Predict<detail::kUseWP>(
608
13.3M
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
609
13.3M
      /*references=*/nullptr, wp_state, /*predictions=*/nullptr);
610
13.3M
}
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
14.5M
                                        const Channel &references) {
618
14.5M
  return detail::Predict<detail::kUseTree>(
619
14.5M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
620
14.5M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
621
14.5M
}
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
51.2M
                   const MATreeLookup &tree_lookup, const Channel &references) {
627
51.2M
  return detail::Predict<detail::kUseTree | detail::kNoEdgeCases>(
628
51.2M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
629
51.2M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
630
51.2M
}
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
65.2M
                                      weighted::State *wp_state) {
639
65.2M
  return detail::Predict<detail::kUseTree | detail::kUseWP>(
640
65.2M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
641
65.2M
      wp_state, /*predictions=*/nullptr);
642
65.2M
}
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
13.6M
                                             weighted::State *wp_state) {
650
13.6M
  return detail::Predict<detail::kUseTree | detail::kUseWP |
651
13.6M
                         detail::kNoEdgeCases>(
652
13.6M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
653
13.6M
      wp_state, /*predictions=*/nullptr);
654
13.6M
}
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
3.22M
                                     weighted::State *wp_state) {
662
3.22M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP>(
663
3.22M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
664
3.22M
      wp_state, /*predictions=*/nullptr);
665
3.22M
}
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
69.6M
                                        weighted::State *wp_state) {
684
69.6M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
685
69.6M
                         detail::kNoEdgeCases>(
686
69.6M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
687
69.6M
      wp_state, /*predictions=*/nullptr);
688
69.6M
}
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_