Coverage Report

Created: 2025-08-11 08:01

/src/libjxl/lib/jxl/modular/encoding/context_predict.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#ifndef LIB_JXL_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
150k
  Header() { Bundle::Init(this); }
37
38
220k
  Status VisitFields(Visitor *JXL_RESTRICT visitor) override {
39
220k
    if (visitor->AllDefault(*this, &all_default)) {
40
      // Overwrite all serialized fields, but not any nonserialized_*.
41
33.8k
      visitor->SetDefault(this);
42
33.8k
      return true;
43
33.8k
    }
44
1.30M
    auto visit_p = [visitor](pixel_type val, pixel_type *p) -> Status {
45
1.30M
      uint32_t up = *p;
46
1.30M
      JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(5, val, &up));
47
1.30M
      *p = up;
48
1.30M
      return true;
49
1.30M
    };
50
186k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(16, &p1C));
51
186k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(10, &p2C));
52
186k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Ca));
53
186k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cb));
54
186k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cc));
55
186k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Cd));
56
186k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Ce));
57
186k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xd, &w[0]));
58
186k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[1]));
59
186k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[2]));
60
186k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[3]));
61
186k
    return true;
62
186k
  }
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
605M
  constexpr static pixel_type_w AddBits(pixel_type_w x) {
90
605M
    return static_cast<uint64_t>(x) << kPredExtraBits;
91
605M
  }
92
93
27.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
109k
    for (auto &pred_error : pred_errors) {
97
109k
      pred_error.resize((xsize + 2) * 2);
98
109k
    }
99
27.4k
    error.resize((xsize + 2) * 2);
100
27.4k
  }
101
102
  // Approximates 4+(maxweight<<24)/(x+1), avoiding division
103
403M
  JXL_INLINE uint32_t ErrorWeight(uint64_t x, uint32_t maxweight) const {
104
403M
    int shift = static_cast<int>(FloorLog2Nonzero(x + 1)) - 5;
105
403M
    if (shift < 0) shift = 0;
106
403M
    return 4 + ((maxweight * divlookup[x >> shift]) >> shift);
107
403M
  }
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
100M
                  std::array<uint32_t, kNumPredictors> w) const {
114
100M
    uint32_t weight_sum = 0;
115
504M
    for (size_t i = 0; i < kNumPredictors; i++) {
116
403M
      weight_sum += w[i];
117
403M
    }
118
100M
    JXL_DASSERT(weight_sum > 15);
119
100M
    uint32_t log_weight = FloorLog2Nonzero(weight_sum);  // at least 4.
120
100M
    weight_sum = 0;
121
504M
    for (size_t i = 0; i < kNumPredictors; i++) {
122
403M
      w[i] >>= log_weight - 4;
123
403M
      weight_sum += w[i];
124
403M
    }
125
    // for rounding.
126
100M
    pixel_type_w sum = (weight_sum >> 1) - 1;
127
504M
    for (size_t i = 0; i < kNumPredictors; i++) {
128
403M
      sum += p[i] * w[i];
129
403M
    }
130
100M
    return (sum * divlookup[weight_sum - 1]) >> 24;
131
100M
  }
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
100M
                                  size_t offset) {
139
100M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
100M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
100M
    size_t pos_N = prev_row + x;
142
100M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
100M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
100M
    std::array<uint32_t, kNumPredictors> weights;
145
504M
    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
403M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
403M
                   pred_errors[i][pos_NW];
150
403M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
403M
    }
152
153
100M
    N = AddBits(N);
154
100M
    W = AddBits(W);
155
100M
    NE = AddBits(NE);
156
100M
    NW = AddBits(NW);
157
100M
    NN = AddBits(NN);
158
159
100M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
100M
    pixel_type_w teN = error[pos_N];
161
100M
    pixel_type_w teNW = error[pos_NW];
162
100M
    pixel_type_w sumWN = teN + teW;
163
100M
    pixel_type_w teNE = error[pos_NE];
164
165
100M
    if (compute_properties) {
166
91.0M
      pixel_type_w p = teW;
167
91.0M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
91.0M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
91.0M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
91.0M
      (*properties)[offset++] = p;
171
91.0M
    }
172
173
100M
    prediction[0] = W + NE - N;
174
100M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
100M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
100M
    prediction[3] =
177
100M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
100M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
100M
             5);
180
181
100M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
100M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
18.7M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
18.7M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
82.0M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
82.0M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
82.0M
    pred = std::max(mn, std::min(mx, pred));
192
82.0M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
100M
  }
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
9.76M
                                  size_t offset) {
139
9.76M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
9.76M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
9.76M
    size_t pos_N = prev_row + x;
142
9.76M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
9.76M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
9.76M
    std::array<uint32_t, kNumPredictors> weights;
145
48.8M
    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
39.0M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
39.0M
                   pred_errors[i][pos_NW];
150
39.0M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
39.0M
    }
152
153
9.76M
    N = AddBits(N);
154
9.76M
    W = AddBits(W);
155
9.76M
    NE = AddBits(NE);
156
9.76M
    NW = AddBits(NW);
157
9.76M
    NN = AddBits(NN);
158
159
9.76M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
9.76M
    pixel_type_w teN = error[pos_N];
161
9.76M
    pixel_type_w teNW = error[pos_NW];
162
9.76M
    pixel_type_w sumWN = teN + teW;
163
9.76M
    pixel_type_w teNE = error[pos_NE];
164
165
9.76M
    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
9.76M
    prediction[0] = W + NE - N;
174
9.76M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
9.76M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
9.76M
    prediction[3] =
177
9.76M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
9.76M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
9.76M
             5);
180
181
9.76M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
9.76M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
1.25M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
1.25M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
8.50M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
8.50M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
8.50M
    pred = std::max(mn, std::min(mx, pred));
192
8.50M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
9.76M
  }
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
91.0M
                                  size_t offset) {
139
91.0M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
91.0M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
91.0M
    size_t pos_N = prev_row + x;
142
91.0M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
91.0M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
91.0M
    std::array<uint32_t, kNumPredictors> weights;
145
455M
    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
364M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
364M
                   pred_errors[i][pos_NW];
150
364M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
364M
    }
152
153
91.0M
    N = AddBits(N);
154
91.0M
    W = AddBits(W);
155
91.0M
    NE = AddBits(NE);
156
91.0M
    NW = AddBits(NW);
157
91.0M
    NN = AddBits(NN);
158
159
91.0M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
91.0M
    pixel_type_w teN = error[pos_N];
161
91.0M
    pixel_type_w teNW = error[pos_NW];
162
91.0M
    pixel_type_w sumWN = teN + teW;
163
91.0M
    pixel_type_w teNE = error[pos_NE];
164
165
91.0M
    if (compute_properties) {
166
91.0M
      pixel_type_w p = teW;
167
91.0M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
91.0M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
91.0M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
91.0M
      (*properties)[offset++] = p;
171
91.0M
    }
172
173
91.0M
    prediction[0] = W + NE - N;
174
91.0M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
91.0M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
91.0M
    prediction[3] =
177
91.0M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
91.0M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
91.0M
             5);
180
181
91.0M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
91.0M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
17.5M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
17.5M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
73.5M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
73.5M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
73.5M
    pred = std::max(mn, std::min(mx, pred));
192
73.5M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
91.0M
  }
194
195
  JXL_INLINE void UpdateErrors(pixel_type_w val, size_t x, size_t y,
196
100M
                               size_t xsize) {
197
100M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
198
100M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
199
100M
    val = AddBits(val);
200
100M
    error[cur_row + x] = pred - val;
201
504M
    for (size_t i = 0; i < kNumPredictors; i++) {
202
403M
      pixel_type_w err =
203
403M
          (std::abs(prediction[i] - val) + kPredictionRound) >> kPredExtraBits;
204
      // For predicting in the next row.
205
403M
      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
403M
      pred_errors[i][prev_row + x + 1] += err;
209
403M
    }
210
100M
  }
211
};
212
213
// Encoder helper function to set the parameters to some presets.
214
2.29k
inline void PredictorMode(int i, Header *header) {
215
2.29k
  switch (i) {
216
2.29k
    case 0:
217
      // ~ lossless16 predictor
218
2.29k
      header->w[0] = 0xd;
219
2.29k
      header->w[1] = 0xc;
220
2.29k
      header->w[2] = 0xc;
221
2.29k
      header->w[3] = 0xc;
222
2.29k
      header->p1C = 16;
223
2.29k
      header->p2C = 10;
224
2.29k
      header->p3Ca = 7;
225
2.29k
      header->p3Cb = 7;
226
2.29k
      header->p3Cc = 7;
227
2.29k
      header->p3Cd = 0;
228
2.29k
      header->p3Ce = 0;
229
2.29k
      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
2.29k
  }
288
2.29k
}
289
}  // namespace weighted
290
291
// Stores a node and its two children at the same time. This significantly
292
// reduces the number of branches needed during decoding.
293
struct FlatDecisionNode {
294
  // Property + splitval of the top node.
295
  int32_t property0;  // -1 if leaf.
296
  union {
297
    PropertyVal splitval0;
298
    Predictor predictor;
299
  };
300
  // Property+splitval of the two child nodes.
301
  union {
302
    PropertyVal splitvals[2];
303
    int32_t multiplier;
304
  };
305
  uint32_t childID;  // childID is ctx id if leaf.
306
  union {
307
    int16_t properties[2];
308
    int32_t predictor_offset;
309
  };
310
};
311
using FlatTree = std::vector<FlatDecisionNode>;
312
313
class MATreeLookup {
314
 public:
315
57.3k
  explicit MATreeLookup(const FlatTree &tree) : nodes_(tree) {}
316
  struct LookupResult {
317
    uint32_t context;
318
    Predictor predictor;
319
    int32_t offset;
320
    int32_t multiplier;
321
  };
322
102M
  JXL_INLINE LookupResult Lookup(const Properties &properties) const {
323
102M
    uint32_t pos = 0;
324
185M
    while (true) {
325
185M
#define TRAVERSE_THE_TREE                                                \
326
315M
  {                                                                      \
327
315M
    const FlatDecisionNode &node = nodes_[pos];                          \
328
315M
    if (node.property0 < 0) {                                            \
329
102M
      return {node.childID, node.predictor, node.predictor_offset,       \
330
102M
              node.multiplier};                                          \
331
102M
    }                                                                    \
332
315M
    bool p0 = properties[node.property0] <= node.splitval0;              \
333
212M
    uint32_t off0 = properties[node.properties[0]] <= node.splitvals[0]; \
334
212M
    uint32_t off1 =                                                      \
335
212M
        2 | int{properties[node.properties[1]] <= node.splitvals[1]};    \
336
212M
    pos = node.childID + (p0 ? off1 : off0);                             \
337
212M
  }
338
339
185M
      TRAVERSE_THE_TREE;
340
129M
      TRAVERSE_THE_TREE;
341
82.7M
    }
342
102M
  }
343
344
 private:
345
  const FlatTree &nodes_;
346
};
347
348
static constexpr size_t kExtraPropsPerChannel = 4;
349
static constexpr size_t kNumNonrefProperties =
350
    kNumStaticProperties + 13 + weighted::kNumProperties;
351
352
constexpr size_t kWPProp = kNumNonrefProperties - weighted::kNumProperties;
353
constexpr size_t kGradientProp = 9;
354
355
// Clamps gradient to the min/max of n, w (and l, implicitly).
356
static JXL_INLINE int32_t ClampedGradient(const int32_t n, const int32_t w,
357
83.8M
                                          const int32_t l) {
358
83.8M
  const int32_t m = std::min(n, w);
359
83.8M
  const int32_t M = std::max(n, w);
360
  // The end result of this operation doesn't overflow or underflow if the
361
  // result is between m and M, but the intermediate value may overflow, so we
362
  // do the intermediate operations in uint32_t and check later if we had an
363
  // overflow or underflow condition comparing m, M and l directly.
364
  // grad = M + m - l = n + w - l
365
83.8M
  const int32_t grad =
366
83.8M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
83.8M
                           static_cast<uint32_t>(l));
368
  // We use two sets of ternary operators to force the evaluation of them in
369
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
370
  // x86.
371
83.8M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
83.8M
  return (l > M) ? m : grad_clamp_M;
373
83.8M
}
Unexecuted instantiation: decode.cc:jxl::ClampedGradient(int, int, int)
encoding.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
357
56.3M
                                          const int32_t l) {
358
56.3M
  const int32_t m = std::min(n, w);
359
56.3M
  const int32_t M = std::max(n, w);
360
  // The end result of this operation doesn't overflow or underflow if the
361
  // result is between m and M, but the intermediate value may overflow, so we
362
  // do the intermediate operations in uint32_t and check later if we had an
363
  // overflow or underflow condition comparing m, M and l directly.
364
  // grad = M + m - l = n + w - l
365
56.3M
  const int32_t grad =
366
56.3M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
56.3M
                           static_cast<uint32_t>(l));
368
  // We use two sets of ternary operators to force the evaluation of them in
369
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
370
  // x86.
371
56.3M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
56.3M
  return (l > M) ? m : grad_clamp_M;
373
56.3M
}
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
357
35.8k
                                          const int32_t l) {
358
35.8k
  const int32_t m = std::min(n, w);
359
35.8k
  const int32_t M = std::max(n, w);
360
  // The end result of this operation doesn't overflow or underflow if the
361
  // result is between m and M, but the intermediate value may overflow, so we
362
  // do the intermediate operations in uint32_t and check later if we had an
363
  // overflow or underflow condition comparing m, M and l directly.
364
  // grad = M + m - l = n + w - l
365
35.8k
  const int32_t grad =
366
35.8k
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
35.8k
                           static_cast<uint32_t>(l));
368
  // We use two sets of ternary operators to force the evaluation of them in
369
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
370
  // x86.
371
35.8k
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
35.8k
  return (l > M) ? m : grad_clamp_M;
373
35.8k
}
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
357
27.4M
                                          const int32_t l) {
358
27.4M
  const int32_t m = std::min(n, w);
359
27.4M
  const int32_t M = std::max(n, w);
360
  // The end result of this operation doesn't overflow or underflow if the
361
  // result is between m and M, but the intermediate value may overflow, so we
362
  // do the intermediate operations in uint32_t and check later if we had an
363
  // overflow or underflow condition comparing m, M and l directly.
364
  // grad = M + m - l = n + w - l
365
27.4M
  const int32_t grad =
366
27.4M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
27.4M
                           static_cast<uint32_t>(l));
368
  // We use two sets of ternary operators to force the evaluation of them in
369
  // any case, allowing the compiler to avoid branches and use cmovl/cmovg in
370
  // x86.
371
27.4M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
27.4M
  return (l > M) ? m : grad_clamp_M;
373
27.4M
}
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)
374
375
951k
inline pixel_type_w Select(pixel_type_w a, pixel_type_w b, pixel_type_w c) {
376
951k
  pixel_type_w p = a + b - c;
377
951k
  pixel_type_w pa = std::abs(p - a);
378
951k
  pixel_type_w pb = std::abs(p - b);
379
951k
  return pa < pb ? a : b;
380
951k
}
381
382
inline void PrecomputeReferences(const Channel &ch, size_t y,
383
                                 const Image &image, uint32_t i,
384
1.41M
                                 Channel *references) {
385
1.41M
  ZeroFillImage(&references->plane);
386
1.41M
  uint32_t offset = 0;
387
1.41M
  size_t num_extra_props = references->w;
388
1.41M
  intptr_t onerow = references->plane.PixelsPerRow();
389
1.41M
  for (int32_t j = static_cast<int32_t>(i) - 1;
390
1.78M
       j >= 0 && offset < num_extra_props; j--) {
391
371k
    if (image.channel[j].w != image.channel[i].w ||
392
371k
        image.channel[j].h != image.channel[i].h) {
393
353k
      continue;
394
353k
    }
395
18.8k
    if (image.channel[j].hshift != image.channel[i].hshift) continue;
396
18.5k
    if (image.channel[j].vshift != image.channel[i].vshift) continue;
397
18.4k
    pixel_type *JXL_RESTRICT rp = references->Row(0) + offset;
398
18.4k
    const pixel_type *JXL_RESTRICT rpp = image.channel[j].Row(y);
399
18.4k
    const pixel_type *JXL_RESTRICT rpprev = image.channel[j].Row(y ? y - 1 : 0);
400
621k
    for (size_t x = 0; x < ch.w; x++, rp += onerow) {
401
603k
      pixel_type_w v = rpp[x];
402
603k
      rp[0] = std::abs(v);
403
603k
      rp[1] = v;
404
603k
      pixel_type_w vleft = (x ? rpp[x - 1] : 0);
405
603k
      pixel_type_w vtop = (y ? rpprev[x] : vleft);
406
603k
      pixel_type_w vtopleft = (x && y ? rpprev[x - 1] : vleft);
407
603k
      pixel_type_w vpredicted = ClampedGradient(vleft, vtop, vtopleft);
408
603k
      rp[2] = std::abs(v - vpredicted);
409
603k
      rp[3] = v - vpredicted;
410
603k
    }
411
412
18.4k
    offset += kExtraPropsPerChannel;
413
18.4k
  }
414
1.41M
}
415
416
struct PredictionResult {
417
  int context = 0;
418
  pixel_type_w guess = 0;
419
  Predictor predictor;
420
  int32_t multiplier;
421
};
422
423
inline void InitPropsRow(
424
    Properties *p,
425
    const std::array<pixel_type, kNumStaticProperties> &static_props,
426
1.41M
    const int y) {
427
4.24M
  for (size_t i = 0; i < kNumStaticProperties; i++) {
428
2.83M
    (*p)[i] = static_props[i];
429
2.83M
  }
430
1.41M
  (*p)[2] = y;
431
1.41M
  (*p)[9] = 0;  // local gradient.
432
1.41M
}
433
434
namespace detail {
435
enum PredictorMode {
436
  kUseTree = 1,
437
  kUseWP = 2,
438
  kForceComputeProperties = 4,
439
  kAllPredictions = 8,
440
  kNoEdgeCases = 16
441
};
442
443
JXL_INLINE pixel_type_w PredictOne(Predictor p, pixel_type_w left,
444
                                   pixel_type_w top, pixel_type_w toptop,
445
                                   pixel_type_w topleft, pixel_type_w topright,
446
                                   pixel_type_w leftleft,
447
                                   pixel_type_w toprightright,
448
139M
                                   pixel_type_w wp_pred) {
449
139M
  switch (p) {
450
11.4M
    case Predictor::Zero:
451
11.4M
      return pixel_type_w{0};
452
9.48M
    case Predictor::Left:
453
9.48M
      return left;
454
16.3M
    case Predictor::Top:
455
16.3M
      return top;
456
951k
    case Predictor::Select:
457
951k
      return Select(left, top, topleft);
458
13.7M
    case Predictor::Weighted:
459
13.7M
      return wp_pred;
460
81.5M
    case Predictor::Gradient:
461
81.5M
      return pixel_type_w{ClampedGradient(left, top, topleft)};
462
392k
    case Predictor::TopLeft:
463
392k
      return topleft;
464
78.3k
    case Predictor::TopRight:
465
78.3k
      return topright;
466
65.2k
    case Predictor::LeftLeft:
467
65.2k
      return leftleft;
468
4.61M
    case Predictor::Average0:
469
4.61M
      return (left + top) / 2;
470
228k
    case Predictor::Average1:
471
228k
      return (left + topleft) / 2;
472
404k
    case Predictor::Average2:
473
404k
      return (topleft + top) / 2;
474
83.8k
    case Predictor::Average3:
475
83.8k
      return (top + topright) / 2;
476
442k
    case Predictor::Average4:
477
442k
      return (6 * top - 2 * toptop + 7 * left + 1 * leftleft +
478
442k
              1 * toprightright + 3 * topright + 8) /
479
442k
             16;
480
0
    default:
481
0
      return pixel_type_w{0};
482
139M
  }
483
139M
}
484
485
template <int mode>
486
JXL_INLINE PredictionResult Predict(
487
    Properties *p, size_t w, const pixel_type *JXL_RESTRICT pp,
488
    const intptr_t onerow, const size_t x, const size_t y, Predictor predictor,
489
    const MATreeLookup *lookup, const Channel *references,
490
139M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
139M
  size_t offset = 3;
493
139M
  constexpr bool compute_properties =
494
139M
      mode & kUseTree || mode & kForceComputeProperties;
495
139M
  constexpr bool nec = mode & kNoEdgeCases;
496
139M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
139M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
139M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
139M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
139M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
139M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
139M
  pixel_type_w toprightright =
503
139M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
139M
  if (compute_properties) {
506
    // location
507
129M
    (*p)[offset++] = x;
508
    // neighbors
509
129M
    (*p)[offset++] = top > 0 ? top : -top;
510
129M
    (*p)[offset++] = left > 0 ? left : -left;
511
129M
    (*p)[offset++] = top;
512
129M
    (*p)[offset++] = left;
513
514
    // local gradient
515
129M
    (*p)[offset] = left - (*p)[offset + 1];
516
129M
    offset++;
517
    // local gradient
518
129M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
129M
    (*p)[offset++] = left - topleft;
522
129M
    (*p)[offset++] = topleft - top;
523
129M
    (*p)[offset++] = top - topright;
524
129M
    (*p)[offset++] = top - toptop;
525
129M
    (*p)[offset++] = left - leftleft;
526
129M
  }
527
528
139M
  pixel_type_w wp_pred = 0;
529
139M
  if (mode & kUseWP) {
530
90.4M
    wp_pred = wp_state->Predict<compute_properties>(
531
90.4M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
90.4M
  }
533
139M
  if (!nec && compute_properties) {
534
42.2M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
42.2M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
54.0M
    for (size_t i = 0; i < references->w; i++) {
538
11.7M
      (*p)[offset++] = rp[i];
539
11.7M
    }
540
42.2M
  }
541
139M
  PredictionResult result;
542
139M
  if (mode & kUseTree) {
543
102M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
102M
    result.context = lr.context;
545
102M
    result.guess = lr.offset;
546
102M
    result.multiplier = lr.multiplier;
547
102M
    predictor = lr.predictor;
548
102M
  }
549
139M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
139M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
139M
                             leftleft, toprightright, wp_pred);
558
139M
  result.predictor = predictor;
559
560
139M
  return result;
561
139M
}
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
490
584k
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
584k
  size_t offset = 3;
493
584k
  constexpr bool compute_properties =
494
584k
      mode & kUseTree || mode & kForceComputeProperties;
495
584k
  constexpr bool nec = mode & kNoEdgeCases;
496
584k
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
584k
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
584k
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
584k
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
584k
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
584k
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
584k
  pixel_type_w toprightright =
503
584k
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
584k
  if (compute_properties) {
506
    // location
507
0
    (*p)[offset++] = x;
508
    // neighbors
509
0
    (*p)[offset++] = top > 0 ? top : -top;
510
0
    (*p)[offset++] = left > 0 ? left : -left;
511
0
    (*p)[offset++] = top;
512
0
    (*p)[offset++] = left;
513
514
    // local gradient
515
0
    (*p)[offset] = left - (*p)[offset + 1];
516
0
    offset++;
517
    // local gradient
518
0
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
0
    (*p)[offset++] = left - topleft;
522
0
    (*p)[offset++] = topleft - top;
523
0
    (*p)[offset++] = top - topright;
524
0
    (*p)[offset++] = top - toptop;
525
0
    (*p)[offset++] = left - leftleft;
526
0
  }
527
528
584k
  pixel_type_w wp_pred = 0;
529
584k
  if (mode & kUseWP) {
530
0
    wp_pred = wp_state->Predict<compute_properties>(
531
0
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
0
  }
533
584k
  if (!nec && compute_properties) {
534
0
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
0
    for (size_t i = 0; i < references->w; i++) {
538
0
      (*p)[offset++] = rp[i];
539
0
    }
540
0
  }
541
584k
  PredictionResult result;
542
584k
  if (mode & kUseTree) {
543
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
0
    result.context = lr.context;
545
0
    result.guess = lr.offset;
546
0
    result.multiplier = lr.multiplier;
547
0
    predictor = lr.predictor;
548
0
  }
549
584k
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
584k
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
584k
                             leftleft, toprightright, wp_pred);
558
584k
  result.predictor = predictor;
559
560
584k
  return result;
561
584k
}
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
490
9.76M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
9.76M
  size_t offset = 3;
493
9.76M
  constexpr bool compute_properties =
494
9.76M
      mode & kUseTree || mode & kForceComputeProperties;
495
9.76M
  constexpr bool nec = mode & kNoEdgeCases;
496
9.76M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
9.76M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
9.76M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
9.76M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
9.76M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
9.76M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
9.76M
  pixel_type_w toprightright =
503
9.76M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
9.76M
  if (compute_properties) {
506
    // location
507
0
    (*p)[offset++] = x;
508
    // neighbors
509
0
    (*p)[offset++] = top > 0 ? top : -top;
510
0
    (*p)[offset++] = left > 0 ? left : -left;
511
0
    (*p)[offset++] = top;
512
0
    (*p)[offset++] = left;
513
514
    // local gradient
515
0
    (*p)[offset] = left - (*p)[offset + 1];
516
0
    offset++;
517
    // local gradient
518
0
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
0
    (*p)[offset++] = left - topleft;
522
0
    (*p)[offset++] = topleft - top;
523
0
    (*p)[offset++] = top - topright;
524
0
    (*p)[offset++] = top - toptop;
525
0
    (*p)[offset++] = left - leftleft;
526
0
  }
527
528
9.76M
  pixel_type_w wp_pred = 0;
529
9.76M
  if (mode & kUseWP) {
530
9.76M
    wp_pred = wp_state->Predict<compute_properties>(
531
9.76M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
9.76M
  }
533
9.76M
  if (!nec && compute_properties) {
534
0
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
0
    for (size_t i = 0; i < references->w; i++) {
538
0
      (*p)[offset++] = rp[i];
539
0
    }
540
0
  }
541
9.76M
  PredictionResult result;
542
9.76M
  if (mode & kUseTree) {
543
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
0
    result.context = lr.context;
545
0
    result.guess = lr.offset;
546
0
    result.multiplier = lr.multiplier;
547
0
    predictor = lr.predictor;
548
0
  }
549
9.76M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
9.76M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
9.76M
                             leftleft, toprightright, wp_pred);
558
9.76M
  result.predictor = predictor;
559
560
9.76M
  return result;
561
9.76M
}
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
490
16.1M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
16.1M
  size_t offset = 3;
493
16.1M
  constexpr bool compute_properties =
494
16.1M
      mode & kUseTree || mode & kForceComputeProperties;
495
16.1M
  constexpr bool nec = mode & kNoEdgeCases;
496
16.1M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
16.1M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
16.1M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
16.1M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
16.1M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
16.1M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
16.1M
  pixel_type_w toprightright =
503
16.1M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
16.1M
  if (compute_properties) {
506
    // location
507
16.1M
    (*p)[offset++] = x;
508
    // neighbors
509
16.1M
    (*p)[offset++] = top > 0 ? top : -top;
510
16.1M
    (*p)[offset++] = left > 0 ? left : -left;
511
16.1M
    (*p)[offset++] = top;
512
16.1M
    (*p)[offset++] = left;
513
514
    // local gradient
515
16.1M
    (*p)[offset] = left - (*p)[offset + 1];
516
16.1M
    offset++;
517
    // local gradient
518
16.1M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
16.1M
    (*p)[offset++] = left - topleft;
522
16.1M
    (*p)[offset++] = topleft - top;
523
16.1M
    (*p)[offset++] = top - topright;
524
16.1M
    (*p)[offset++] = top - toptop;
525
16.1M
    (*p)[offset++] = left - leftleft;
526
16.1M
  }
527
528
16.1M
  pixel_type_w wp_pred = 0;
529
16.1M
  if (mode & kUseWP) {
530
0
    wp_pred = wp_state->Predict<compute_properties>(
531
0
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
0
  }
533
16.1M
  if (!nec && compute_properties) {
534
16.1M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
16.1M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
26.3M
    for (size_t i = 0; i < references->w; i++) {
538
10.1M
      (*p)[offset++] = rp[i];
539
10.1M
    }
540
16.1M
  }
541
16.1M
  PredictionResult result;
542
16.1M
  if (mode & kUseTree) {
543
16.1M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
16.1M
    result.context = lr.context;
545
16.1M
    result.guess = lr.offset;
546
16.1M
    result.multiplier = lr.multiplier;
547
16.1M
    predictor = lr.predictor;
548
16.1M
  }
549
16.1M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
16.1M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
16.1M
                             leftleft, toprightright, wp_pred);
558
16.1M
  result.predictor = predictor;
559
560
16.1M
  return result;
561
16.1M
}
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
490
32.5M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
32.5M
  size_t offset = 3;
493
32.5M
  constexpr bool compute_properties =
494
32.5M
      mode & kUseTree || mode & kForceComputeProperties;
495
32.5M
  constexpr bool nec = mode & kNoEdgeCases;
496
32.5M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
32.5M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
32.5M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
32.5M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
32.5M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
32.5M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
32.5M
  pixel_type_w toprightright =
503
32.5M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
32.5M
  if (compute_properties) {
506
    // location
507
32.5M
    (*p)[offset++] = x;
508
    // neighbors
509
32.5M
    (*p)[offset++] = top > 0 ? top : -top;
510
32.5M
    (*p)[offset++] = left > 0 ? left : -left;
511
32.5M
    (*p)[offset++] = top;
512
32.5M
    (*p)[offset++] = left;
513
514
    // local gradient
515
32.5M
    (*p)[offset] = left - (*p)[offset + 1];
516
32.5M
    offset++;
517
    // local gradient
518
32.5M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
32.5M
    (*p)[offset++] = left - topleft;
522
32.5M
    (*p)[offset++] = topleft - top;
523
32.5M
    (*p)[offset++] = top - topright;
524
32.5M
    (*p)[offset++] = top - toptop;
525
32.5M
    (*p)[offset++] = left - leftleft;
526
32.5M
  }
527
528
32.5M
  pixel_type_w wp_pred = 0;
529
32.5M
  if (mode & kUseWP) {
530
0
    wp_pred = wp_state->Predict<compute_properties>(
531
0
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
0
  }
533
32.5M
  if (!nec && compute_properties) {
534
0
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
0
    for (size_t i = 0; i < references->w; i++) {
538
0
      (*p)[offset++] = rp[i];
539
0
    }
540
0
  }
541
32.5M
  PredictionResult result;
542
32.5M
  if (mode & kUseTree) {
543
32.5M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
32.5M
    result.context = lr.context;
545
32.5M
    result.guess = lr.offset;
546
32.5M
    result.multiplier = lr.multiplier;
547
32.5M
    predictor = lr.predictor;
548
32.5M
  }
549
32.5M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
32.5M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
32.5M
                             leftleft, toprightright, wp_pred);
558
32.5M
  result.predictor = predictor;
559
560
32.5M
  return result;
561
32.5M
}
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
490
24.7M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
24.7M
  size_t offset = 3;
493
24.7M
  constexpr bool compute_properties =
494
24.7M
      mode & kUseTree || mode & kForceComputeProperties;
495
24.7M
  constexpr bool nec = mode & kNoEdgeCases;
496
24.7M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
24.7M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
24.7M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
24.7M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
24.7M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
24.7M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
24.7M
  pixel_type_w toprightright =
503
24.7M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
24.7M
  if (compute_properties) {
506
    // location
507
24.7M
    (*p)[offset++] = x;
508
    // neighbors
509
24.7M
    (*p)[offset++] = top > 0 ? top : -top;
510
24.7M
    (*p)[offset++] = left > 0 ? left : -left;
511
24.7M
    (*p)[offset++] = top;
512
24.7M
    (*p)[offset++] = left;
513
514
    // local gradient
515
24.7M
    (*p)[offset] = left - (*p)[offset + 1];
516
24.7M
    offset++;
517
    // local gradient
518
24.7M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
24.7M
    (*p)[offset++] = left - topleft;
522
24.7M
    (*p)[offset++] = topleft - top;
523
24.7M
    (*p)[offset++] = top - topright;
524
24.7M
    (*p)[offset++] = top - toptop;
525
24.7M
    (*p)[offset++] = left - leftleft;
526
24.7M
  }
527
528
24.7M
  pixel_type_w wp_pred = 0;
529
24.7M
  if (mode & kUseWP) {
530
24.7M
    wp_pred = wp_state->Predict<compute_properties>(
531
24.7M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
24.7M
  }
533
24.7M
  if (!nec && compute_properties) {
534
24.7M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
24.7M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
26.3M
    for (size_t i = 0; i < references->w; i++) {
538
1.59M
      (*p)[offset++] = rp[i];
539
1.59M
    }
540
24.7M
  }
541
24.7M
  PredictionResult result;
542
24.7M
  if (mode & kUseTree) {
543
24.7M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
24.7M
    result.context = lr.context;
545
24.7M
    result.guess = lr.offset;
546
24.7M
    result.multiplier = lr.multiplier;
547
24.7M
    predictor = lr.predictor;
548
24.7M
  }
549
24.7M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
24.7M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
24.7M
                             leftleft, toprightright, wp_pred);
558
24.7M
  result.predictor = predictor;
559
560
24.7M
  return result;
561
24.7M
}
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
490
29.1M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
29.1M
  size_t offset = 3;
493
29.1M
  constexpr bool compute_properties =
494
29.1M
      mode & kUseTree || mode & kForceComputeProperties;
495
29.1M
  constexpr bool nec = mode & kNoEdgeCases;
496
29.1M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
29.1M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
29.1M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
29.1M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
29.1M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
29.1M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
29.1M
  pixel_type_w toprightright =
503
29.1M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
29.1M
  if (compute_properties) {
506
    // location
507
29.1M
    (*p)[offset++] = x;
508
    // neighbors
509
29.1M
    (*p)[offset++] = top > 0 ? top : -top;
510
29.1M
    (*p)[offset++] = left > 0 ? left : -left;
511
29.1M
    (*p)[offset++] = top;
512
29.1M
    (*p)[offset++] = left;
513
514
    // local gradient
515
29.1M
    (*p)[offset] = left - (*p)[offset + 1];
516
29.1M
    offset++;
517
    // local gradient
518
29.1M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
29.1M
    (*p)[offset++] = left - topleft;
522
29.1M
    (*p)[offset++] = topleft - top;
523
29.1M
    (*p)[offset++] = top - topright;
524
29.1M
    (*p)[offset++] = top - toptop;
525
29.1M
    (*p)[offset++] = left - leftleft;
526
29.1M
  }
527
528
29.1M
  pixel_type_w wp_pred = 0;
529
29.1M
  if (mode & kUseWP) {
530
29.1M
    wp_pred = wp_state->Predict<compute_properties>(
531
29.1M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
29.1M
  }
533
29.1M
  if (!nec && compute_properties) {
534
0
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
0
    for (size_t i = 0; i < references->w; i++) {
538
0
      (*p)[offset++] = rp[i];
539
0
    }
540
0
  }
541
29.1M
  PredictionResult result;
542
29.1M
  if (mode & kUseTree) {
543
29.1M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
29.1M
    result.context = lr.context;
545
29.1M
    result.guess = lr.offset;
546
29.1M
    result.multiplier = lr.multiplier;
547
29.1M
    predictor = lr.predictor;
548
29.1M
  }
549
29.1M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
29.1M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
29.1M
                             leftleft, toprightright, wp_pred);
558
29.1M
  result.predictor = predictor;
559
560
29.1M
  return result;
561
29.1M
}
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
490
1.31M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
1.31M
  size_t offset = 3;
493
1.31M
  constexpr bool compute_properties =
494
1.31M
      mode & kUseTree || mode & kForceComputeProperties;
495
1.31M
  constexpr bool nec = mode & kNoEdgeCases;
496
1.31M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
1.31M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
1.31M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
1.31M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
1.31M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
1.31M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
1.31M
  pixel_type_w toprightright =
503
1.31M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
1.31M
  if (compute_properties) {
506
    // location
507
1.31M
    (*p)[offset++] = x;
508
    // neighbors
509
1.31M
    (*p)[offset++] = top > 0 ? top : -top;
510
1.31M
    (*p)[offset++] = left > 0 ? left : -left;
511
1.31M
    (*p)[offset++] = top;
512
1.31M
    (*p)[offset++] = left;
513
514
    // local gradient
515
1.31M
    (*p)[offset] = left - (*p)[offset + 1];
516
1.31M
    offset++;
517
    // local gradient
518
1.31M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
1.31M
    (*p)[offset++] = left - topleft;
522
1.31M
    (*p)[offset++] = topleft - top;
523
1.31M
    (*p)[offset++] = top - topright;
524
1.31M
    (*p)[offset++] = top - toptop;
525
1.31M
    (*p)[offset++] = left - leftleft;
526
1.31M
  }
527
528
1.31M
  pixel_type_w wp_pred = 0;
529
1.31M
  if (mode & kUseWP) {
530
1.31M
    wp_pred = wp_state->Predict<compute_properties>(
531
1.31M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
1.31M
  }
533
1.31M
  if (!nec && compute_properties) {
534
1.31M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
1.31M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
1.31M
    for (size_t i = 0; i < references->w; i++) {
538
0
      (*p)[offset++] = rp[i];
539
0
    }
540
1.31M
  }
541
1.31M
  PredictionResult result;
542
1.31M
  if (mode & kUseTree) {
543
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
0
    result.context = lr.context;
545
0
    result.guess = lr.offset;
546
0
    result.multiplier = lr.multiplier;
547
0
    predictor = lr.predictor;
548
0
  }
549
1.31M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
1.31M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
1.31M
                             leftleft, toprightright, wp_pred);
558
1.31M
  result.predictor = predictor;
559
560
1.31M
  return result;
561
1.31M
}
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
490
25.4M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
25.4M
  size_t offset = 3;
493
25.4M
  constexpr bool compute_properties =
494
25.4M
      mode & kUseTree || mode & kForceComputeProperties;
495
25.4M
  constexpr bool nec = mode & kNoEdgeCases;
496
25.4M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
25.4M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
25.4M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
25.4M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
25.4M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
25.4M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
25.4M
  pixel_type_w toprightright =
503
25.4M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
25.4M
  if (compute_properties) {
506
    // location
507
25.4M
    (*p)[offset++] = x;
508
    // neighbors
509
25.4M
    (*p)[offset++] = top > 0 ? top : -top;
510
25.4M
    (*p)[offset++] = left > 0 ? left : -left;
511
25.4M
    (*p)[offset++] = top;
512
25.4M
    (*p)[offset++] = left;
513
514
    // local gradient
515
25.4M
    (*p)[offset] = left - (*p)[offset + 1];
516
25.4M
    offset++;
517
    // local gradient
518
25.4M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
25.4M
    (*p)[offset++] = left - topleft;
522
25.4M
    (*p)[offset++] = topleft - top;
523
25.4M
    (*p)[offset++] = top - topright;
524
25.4M
    (*p)[offset++] = top - toptop;
525
25.4M
    (*p)[offset++] = left - leftleft;
526
25.4M
  }
527
528
25.4M
  pixel_type_w wp_pred = 0;
529
25.4M
  if (mode & kUseWP) {
530
25.4M
    wp_pred = wp_state->Predict<compute_properties>(
531
25.4M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
25.4M
  }
533
25.4M
  if (!nec && compute_properties) {
534
0
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
0
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
0
    for (size_t i = 0; i < references->w; i++) {
538
0
      (*p)[offset++] = rp[i];
539
0
    }
540
0
  }
541
25.4M
  PredictionResult result;
542
25.4M
  if (mode & kUseTree) {
543
0
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
0
    result.context = lr.context;
545
0
    result.guess = lr.offset;
546
0
    result.multiplier = lr.multiplier;
547
0
    predictor = lr.predictor;
548
0
  }
549
25.4M
  if (mode & kAllPredictions) {
550
0
    for (size_t i = 0; i < kNumModularPredictors; i++) {
551
0
      predictions[i] =
552
0
          PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
553
0
                     topright, leftleft, toprightright, wp_pred);
554
0
    }
555
0
  }
556
25.4M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
25.4M
                             leftleft, toprightright, wp_pred);
558
25.4M
  result.predictor = predictor;
559
560
25.4M
  return result;
561
25.4M
}
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*)
562
}  // namespace detail
563
564
inline PredictionResult PredictNoTreeNoWP(size_t w,
565
                                          const pixel_type *JXL_RESTRICT pp,
566
                                          const intptr_t onerow, const int x,
567
584k
                                          const int y, Predictor predictor) {
568
584k
  return detail::Predict</*mode=*/0>(
569
584k
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
570
584k
      /*references=*/nullptr, /*wp_state=*/nullptr, /*predictions=*/nullptr);
571
584k
}
572
573
inline PredictionResult PredictNoTreeWP(size_t w,
574
                                        const pixel_type *JXL_RESTRICT pp,
575
                                        const intptr_t onerow, const int x,
576
                                        const int y, Predictor predictor,
577
9.76M
                                        weighted::State *wp_state) {
578
9.76M
  return detail::Predict<detail::kUseWP>(
579
9.76M
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
580
9.76M
      /*references=*/nullptr, wp_state, /*predictions=*/nullptr);
581
9.76M
}
582
583
inline PredictionResult PredictTreeNoWP(Properties *p, size_t w,
584
                                        const pixel_type *JXL_RESTRICT pp,
585
                                        const intptr_t onerow, const int x,
586
                                        const int y,
587
                                        const MATreeLookup &tree_lookup,
588
16.1M
                                        const Channel &references) {
589
16.1M
  return detail::Predict<detail::kUseTree>(
590
16.1M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
591
16.1M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
592
16.1M
}
593
// Only use for y > 1, x > 1, x < w-2, and empty references
594
JXL_INLINE PredictionResult
595
PredictTreeNoWPNEC(Properties *p, size_t w, const pixel_type *JXL_RESTRICT pp,
596
                   const intptr_t onerow, const int x, const int y,
597
32.5M
                   const MATreeLookup &tree_lookup, const Channel &references) {
598
32.5M
  return detail::Predict<detail::kUseTree | detail::kNoEdgeCases>(
599
32.5M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
600
32.5M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
601
32.5M
}
602
603
inline PredictionResult PredictTreeWP(Properties *p, size_t w,
604
                                      const pixel_type *JXL_RESTRICT pp,
605
                                      const intptr_t onerow, const int x,
606
                                      const int y,
607
                                      const MATreeLookup &tree_lookup,
608
                                      const Channel &references,
609
24.7M
                                      weighted::State *wp_state) {
610
24.7M
  return detail::Predict<detail::kUseTree | detail::kUseWP>(
611
24.7M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
612
24.7M
      wp_state, /*predictions=*/nullptr);
613
24.7M
}
614
JXL_INLINE PredictionResult PredictTreeWPNEC(Properties *p, size_t w,
615
                                             const pixel_type *JXL_RESTRICT pp,
616
                                             const intptr_t onerow, const int x,
617
                                             const int y,
618
                                             const MATreeLookup &tree_lookup,
619
                                             const Channel &references,
620
29.1M
                                             weighted::State *wp_state) {
621
29.1M
  return detail::Predict<detail::kUseTree | detail::kUseWP |
622
29.1M
                         detail::kNoEdgeCases>(
623
29.1M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
624
29.1M
      wp_state, /*predictions=*/nullptr);
625
29.1M
}
626
627
inline PredictionResult PredictLearn(Properties *p, size_t w,
628
                                     const pixel_type *JXL_RESTRICT pp,
629
                                     const intptr_t onerow, const int x,
630
                                     const int y, Predictor predictor,
631
                                     const Channel &references,
632
1.31M
                                     weighted::State *wp_state) {
633
1.31M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP>(
634
1.31M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
635
1.31M
      wp_state, /*predictions=*/nullptr);
636
1.31M
}
637
638
inline void PredictLearnAll(Properties *p, size_t w,
639
                            const pixel_type *JXL_RESTRICT pp,
640
                            const intptr_t onerow, const int x, const int y,
641
                            const Channel &references,
642
                            weighted::State *wp_state,
643
0
                            pixel_type_w *predictions) {
644
0
  detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
645
0
                  detail::kAllPredictions>(
646
0
      p, w, pp, onerow, x, y, Predictor::Zero,
647
0
      /*lookup=*/nullptr, &references, wp_state, predictions);
648
0
}
649
inline PredictionResult PredictLearnNEC(Properties *p, size_t w,
650
                                        const pixel_type *JXL_RESTRICT pp,
651
                                        const intptr_t onerow, const int x,
652
                                        const int y, Predictor predictor,
653
                                        const Channel &references,
654
25.4M
                                        weighted::State *wp_state) {
655
25.4M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
656
25.4M
                         detail::kNoEdgeCases>(
657
25.4M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
658
25.4M
      wp_state, /*predictions=*/nullptr);
659
25.4M
}
660
661
inline void PredictLearnAllNEC(Properties *p, size_t w,
662
                               const pixel_type *JXL_RESTRICT pp,
663
                               const intptr_t onerow, const int x, const int y,
664
                               const Channel &references,
665
                               weighted::State *wp_state,
666
0
                               pixel_type_w *predictions) {
667
0
  detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
668
0
                  detail::kAllPredictions | detail::kNoEdgeCases>(
669
0
      p, w, pp, onerow, x, y, Predictor::Zero,
670
0
      /*lookup=*/nullptr, &references, wp_state, predictions);
671
0
}
672
673
inline void PredictAllNoWP(size_t w, const pixel_type *JXL_RESTRICT pp,
674
                           const intptr_t onerow, const int x, const int y,
675
0
                           pixel_type_w *predictions) {
676
0
  detail::Predict<detail::kAllPredictions>(
677
0
      /*p=*/nullptr, w, pp, onerow, x, y, Predictor::Zero,
678
0
      /*lookup=*/nullptr,
679
0
      /*references=*/nullptr, /*wp_state=*/nullptr, predictions);
680
0
}
681
}  // namespace jxl
682
683
#endif  // LIB_JXL_MODULAR_ENCODING_CONTEXT_PREDICT_H_