Coverage Report

Created: 2025-07-23 08:18

/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
209k
  Header() { Bundle::Init(this); }
37
38
312k
  Status VisitFields(Visitor *JXL_RESTRICT visitor) override {
39
312k
    if (visitor->AllDefault(*this, &all_default)) {
40
      // Overwrite all serialized fields, but not any nonserialized_*.
41
47.1k
      visitor->SetDefault(this);
42
47.1k
      return true;
43
47.1k
    }
44
1.85M
    auto visit_p = [visitor](pixel_type val, pixel_type *p) -> Status {
45
1.85M
      uint32_t up = *p;
46
1.85M
      JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(5, val, &up));
47
1.85M
      *p = up;
48
1.85M
      return true;
49
1.85M
    };
50
265k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(16, &p1C));
51
264k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(10, &p2C));
52
264k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Ca));
53
264k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cb));
54
264k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(7, &p3Cc));
55
264k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Cd));
56
264k
    JXL_QUIET_RETURN_IF_ERROR(visit_p(0, &p3Ce));
57
264k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xd, &w[0]));
58
264k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[1]));
59
263k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[2]));
60
263k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(4, 0xc, &w[3]));
61
263k
    return true;
62
263k
  }
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
536M
  constexpr static pixel_type_w AddBits(pixel_type_w x) {
90
536M
    return static_cast<uint64_t>(x) << kPredExtraBits;
91
536M
  }
92
93
25.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
101k
    for (auto &pred_error : pred_errors) {
97
101k
      pred_error.resize((xsize + 2) * 2);
98
101k
    }
99
25.4k
    error.resize((xsize + 2) * 2);
100
25.4k
  }
101
102
  // Approximates 4+(maxweight<<24)/(x+1), avoiding division
103
357M
  JXL_INLINE uint32_t ErrorWeight(uint64_t x, uint32_t maxweight) const {
104
357M
    int shift = static_cast<int>(FloorLog2Nonzero(x + 1)) - 5;
105
357M
    if (shift < 0) shift = 0;
106
357M
    return 4 + ((maxweight * divlookup[x >> shift]) >> shift);
107
357M
  }
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
89.4M
                  std::array<uint32_t, kNumPredictors> w) const {
114
89.4M
    uint32_t weight_sum = 0;
115
447M
    for (size_t i = 0; i < kNumPredictors; i++) {
116
357M
      weight_sum += w[i];
117
357M
    }
118
89.4M
    JXL_DASSERT(weight_sum > 15);
119
89.4M
    uint32_t log_weight = FloorLog2Nonzero(weight_sum);  // at least 4.
120
89.4M
    weight_sum = 0;
121
447M
    for (size_t i = 0; i < kNumPredictors; i++) {
122
357M
      w[i] >>= log_weight - 4;
123
357M
      weight_sum += w[i];
124
357M
    }
125
    // for rounding.
126
89.4M
    pixel_type_w sum = (weight_sum >> 1) - 1;
127
447M
    for (size_t i = 0; i < kNumPredictors; i++) {
128
357M
      sum += p[i] * w[i];
129
357M
    }
130
89.4M
    return (sum * divlookup[weight_sum - 1]) >> 24;
131
89.4M
  }
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
89.4M
                                  size_t offset) {
139
89.4M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
89.4M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
89.4M
    size_t pos_N = prev_row + x;
142
89.4M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
89.4M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
89.4M
    std::array<uint32_t, kNumPredictors> weights;
145
447M
    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
357M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
357M
                   pred_errors[i][pos_NW];
150
357M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
357M
    }
152
153
89.4M
    N = AddBits(N);
154
89.4M
    W = AddBits(W);
155
89.4M
    NE = AddBits(NE);
156
89.4M
    NW = AddBits(NW);
157
89.4M
    NN = AddBits(NN);
158
159
89.4M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
89.4M
    pixel_type_w teN = error[pos_N];
161
89.4M
    pixel_type_w teNW = error[pos_NW];
162
89.4M
    pixel_type_w sumWN = teN + teW;
163
89.4M
    pixel_type_w teNE = error[pos_NE];
164
165
89.4M
    if (compute_properties) {
166
80.7M
      pixel_type_w p = teW;
167
80.7M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
80.7M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
80.7M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
80.7M
      (*properties)[offset++] = p;
171
80.7M
    }
172
173
89.4M
    prediction[0] = W + NE - N;
174
89.4M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
89.4M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
89.4M
    prediction[3] =
177
89.4M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
89.4M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
89.4M
             5);
180
181
89.4M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
89.4M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
16.4M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
16.4M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
72.9M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
72.9M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
72.9M
    pred = std::max(mn, std::min(mx, pred));
192
72.9M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
89.4M
  }
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
8.75M
                                  size_t offset) {
139
8.75M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
8.75M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
8.75M
    size_t pos_N = prev_row + x;
142
8.75M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
8.75M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
8.75M
    std::array<uint32_t, kNumPredictors> weights;
145
43.7M
    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
35.0M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
35.0M
                   pred_errors[i][pos_NW];
150
35.0M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
35.0M
    }
152
153
8.75M
    N = AddBits(N);
154
8.75M
    W = AddBits(W);
155
8.75M
    NE = AddBits(NE);
156
8.75M
    NW = AddBits(NW);
157
8.75M
    NN = AddBits(NN);
158
159
8.75M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
8.75M
    pixel_type_w teN = error[pos_N];
161
8.75M
    pixel_type_w teNW = error[pos_NW];
162
8.75M
    pixel_type_w sumWN = teN + teW;
163
8.75M
    pixel_type_w teNE = error[pos_NE];
164
165
8.75M
    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
8.75M
    prediction[0] = W + NE - N;
174
8.75M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
8.75M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
8.75M
    prediction[3] =
177
8.75M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
8.75M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
8.75M
             5);
180
181
8.75M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
8.75M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
1.07M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
1.07M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
7.68M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
7.68M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
7.68M
    pred = std::max(mn, std::min(mx, pred));
192
7.68M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
8.75M
  }
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
80.7M
                                  size_t offset) {
139
80.7M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
140
80.7M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
141
80.7M
    size_t pos_N = prev_row + x;
142
80.7M
    size_t pos_NE = x < xsize - 1 ? pos_N + 1 : pos_N;
143
80.7M
    size_t pos_NW = x > 0 ? pos_N - 1 : pos_N;
144
80.7M
    std::array<uint32_t, kNumPredictors> weights;
145
403M
    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
322M
      weights[i] = pred_errors[i][pos_N] + pred_errors[i][pos_NE] +
149
322M
                   pred_errors[i][pos_NW];
150
322M
      weights[i] = ErrorWeight(weights[i], header.w[i]);
151
322M
    }
152
153
80.7M
    N = AddBits(N);
154
80.7M
    W = AddBits(W);
155
80.7M
    NE = AddBits(NE);
156
80.7M
    NW = AddBits(NW);
157
80.7M
    NN = AddBits(NN);
158
159
80.7M
    pixel_type_w teW = x == 0 ? 0 : error[cur_row + x - 1];
160
80.7M
    pixel_type_w teN = error[pos_N];
161
80.7M
    pixel_type_w teNW = error[pos_NW];
162
80.7M
    pixel_type_w sumWN = teN + teW;
163
80.7M
    pixel_type_w teNE = error[pos_NE];
164
165
80.7M
    if (compute_properties) {
166
80.7M
      pixel_type_w p = teW;
167
80.7M
      if (std::abs(teN) > std::abs(p)) p = teN;
168
80.7M
      if (std::abs(teNW) > std::abs(p)) p = teNW;
169
80.7M
      if (std::abs(teNE) > std::abs(p)) p = teNE;
170
80.7M
      (*properties)[offset++] = p;
171
80.7M
    }
172
173
80.7M
    prediction[0] = W + NE - N;
174
80.7M
    prediction[1] = N - (((sumWN + teNE) * header.p1C) >> 5);
175
80.7M
    prediction[2] = W - (((sumWN + teNW) * header.p2C) >> 5);
176
80.7M
    prediction[3] =
177
80.7M
        N - ((teNW * header.p3Ca + teN * header.p3Cb + teNE * header.p3Cc +
178
80.7M
              (NN - N) * header.p3Cd + (NW - W) * header.p3Ce) >>
179
80.7M
             5);
180
181
80.7M
    pred = WeightedAverage(prediction, weights);
182
183
    // If all three have the same sign, skip clamping.
184
80.7M
    if (((teN ^ teW) | (teN ^ teNW)) > 0) {
185
15.4M
      return (pred + kPredictionRound) >> kPredExtraBits;
186
15.4M
    }
187
188
    // Otherwise, clamp to min/max of neighbouring pixels (just W, NE, N).
189
65.2M
    pixel_type_w mx = std::max(W, std::max(NE, N));
190
65.2M
    pixel_type_w mn = std::min(W, std::min(NE, N));
191
65.2M
    pred = std::max(mn, std::min(mx, pred));
192
65.2M
    return (pred + kPredictionRound) >> kPredExtraBits;
193
80.7M
  }
194
195
  JXL_INLINE void UpdateErrors(pixel_type_w val, size_t x, size_t y,
196
89.5M
                               size_t xsize) {
197
89.5M
    size_t cur_row = y & 1 ? 0 : (xsize + 2);
198
89.5M
    size_t prev_row = y & 1 ? (xsize + 2) : 0;
199
89.5M
    val = AddBits(val);
200
89.5M
    error[cur_row + x] = pred - val;
201
447M
    for (size_t i = 0; i < kNumPredictors; i++) {
202
358M
      pixel_type_w err =
203
358M
          (std::abs(prediction[i] - val) + kPredictionRound) >> kPredExtraBits;
204
      // For predicting in the next row.
205
358M
      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
358M
      pred_errors[i][prev_row + x + 1] += err;
209
358M
    }
210
89.5M
  }
211
};
212
213
// Encoder helper function to set the parameters to some presets.
214
2.13k
inline void PredictorMode(int i, Header *header) {
215
2.13k
  switch (i) {
216
2.13k
    case 0:
217
      // ~ lossless16 predictor
218
2.13k
      header->w[0] = 0xd;
219
2.13k
      header->w[1] = 0xc;
220
2.13k
      header->w[2] = 0xc;
221
2.13k
      header->w[3] = 0xc;
222
2.13k
      header->p1C = 16;
223
2.13k
      header->p2C = 10;
224
2.13k
      header->p3Ca = 7;
225
2.13k
      header->p3Cb = 7;
226
2.13k
      header->p3Cc = 7;
227
2.13k
      header->p3Cd = 0;
228
2.13k
      header->p3Ce = 0;
229
2.13k
      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.13k
  }
288
2.13k
}
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
53.7k
  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
90.9M
  JXL_INLINE LookupResult Lookup(const Properties &properties) const {
323
90.9M
    uint32_t pos = 0;
324
162M
    while (true) {
325
162M
#define TRAVERSE_THE_TREE                                                \
326
276M
  {                                                                      \
327
276M
    const FlatDecisionNode &node = nodes_[pos];                          \
328
276M
    if (node.property0 < 0) {                                            \
329
90.9M
      return {node.childID, node.predictor, node.predictor_offset,       \
330
90.9M
              node.multiplier};                                          \
331
90.9M
    }                                                                    \
332
276M
    bool p0 = properties[node.property0] <= node.splitval0;              \
333
185M
    uint32_t off0 = properties[node.properties[0]] <= node.splitvals[0]; \
334
185M
    uint32_t off1 =                                                      \
335
185M
        2 | int{properties[node.properties[1]] <= node.splitvals[1]};    \
336
185M
    pos = node.childID + (p0 ? off1 : off0);                             \
337
185M
  }
338
339
162M
      TRAVERSE_THE_TREE;
340
114M
      TRAVERSE_THE_TREE;
341
71.7M
    }
342
90.9M
  }
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
73.8M
                                          const int32_t l) {
358
73.8M
  const int32_t m = std::min(n, w);
359
73.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
73.8M
  const int32_t grad =
366
73.8M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
73.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
73.8M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
73.8M
  return (l > M) ? m : grad_clamp_M;
373
73.8M
}
Unexecuted instantiation: decode.cc:jxl::ClampedGradient(int, int, int)
encoding.cc:jxl::ClampedGradient(int, int, int)
Line
Count
Source
357
49.6M
                                          const int32_t l) {
358
49.6M
  const int32_t m = std::min(n, w);
359
49.6M
  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
49.6M
  const int32_t grad =
366
49.6M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
49.6M
                           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
49.6M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
49.6M
  return (l > M) ? m : grad_clamp_M;
373
49.6M
}
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
27.3k
                                          const int32_t l) {
358
27.3k
  const int32_t m = std::min(n, w);
359
27.3k
  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.3k
  const int32_t grad =
366
27.3k
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
27.3k
                           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.3k
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
27.3k
  return (l > M) ? m : grad_clamp_M;
373
27.3k
}
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
24.1M
                                          const int32_t l) {
358
24.1M
  const int32_t m = std::min(n, w);
359
24.1M
  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
24.1M
  const int32_t grad =
366
24.1M
      static_cast<int32_t>(static_cast<uint32_t>(n) + static_cast<uint32_t>(w) -
367
24.1M
                           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
24.1M
  const int32_t grad_clamp_M = (l < m) ? M : grad;
372
24.1M
  return (l > M) ? m : grad_clamp_M;
373
24.1M
}
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
1.10M
inline pixel_type_w Select(pixel_type_w a, pixel_type_w b, pixel_type_w c) {
376
1.10M
  pixel_type_w p = a + b - c;
377
1.10M
  pixel_type_w pa = std::abs(p - a);
378
1.10M
  pixel_type_w pb = std::abs(p - b);
379
1.10M
  return pa < pb ? a : b;
380
1.10M
}
381
382
inline void PrecomputeReferences(const Channel &ch, size_t y,
383
                                 const Image &image, uint32_t i,
384
1.29M
                                 Channel *references) {
385
1.29M
  ZeroFillImage(&references->plane);
386
1.29M
  uint32_t offset = 0;
387
1.29M
  size_t num_extra_props = references->w;
388
1.29M
  intptr_t onerow = references->plane.PixelsPerRow();
389
1.29M
  for (int32_t j = static_cast<int32_t>(i) - 1;
390
1.70M
       j >= 0 && offset < num_extra_props; j--) {
391
412k
    if (image.channel[j].w != image.channel[i].w ||
392
412k
        image.channel[j].h != image.channel[i].h) {
393
391k
      continue;
394
391k
    }
395
20.3k
    if (image.channel[j].hshift != image.channel[i].hshift) continue;
396
20.0k
    if (image.channel[j].vshift != image.channel[i].vshift) continue;
397
19.9k
    pixel_type *JXL_RESTRICT rp = references->Row(0) + offset;
398
19.9k
    const pixel_type *JXL_RESTRICT rpp = image.channel[j].Row(y);
399
19.9k
    const pixel_type *JXL_RESTRICT rpprev = image.channel[j].Row(y ? y - 1 : 0);
400
689k
    for (size_t x = 0; x < ch.w; x++, rp += onerow) {
401
669k
      pixel_type_w v = rpp[x];
402
669k
      rp[0] = std::abs(v);
403
669k
      rp[1] = v;
404
669k
      pixel_type_w vleft = (x ? rpp[x - 1] : 0);
405
669k
      pixel_type_w vtop = (y ? rpprev[x] : vleft);
406
669k
      pixel_type_w vtopleft = (x && y ? rpprev[x - 1] : vleft);
407
669k
      pixel_type_w vpredicted = ClampedGradient(vleft, vtop, vtopleft);
408
669k
      rp[2] = std::abs(v - vpredicted);
409
669k
      rp[3] = v - vpredicted;
410
669k
    }
411
412
19.9k
    offset += kExtraPropsPerChannel;
413
19.9k
  }
414
1.29M
}
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.29M
    const int y) {
427
3.88M
  for (size_t i = 0; i < kNumStaticProperties; i++) {
428
2.59M
    (*p)[i] = static_props[i];
429
2.59M
  }
430
1.29M
  (*p)[2] = y;
431
1.29M
  (*p)[9] = 0;  // local gradient.
432
1.29M
}
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
124M
                                   pixel_type_w wp_pred) {
449
124M
  switch (p) {
450
10.6M
    case Predictor::Zero:
451
10.6M
      return pixel_type_w{0};
452
9.17M
    case Predictor::Left:
453
9.17M
      return left;
454
13.4M
    case Predictor::Top:
455
13.4M
      return top;
456
1.10M
    case Predictor::Select:
457
1.10M
      return Select(left, top, topleft);
458
12.6M
    case Predictor::Weighted:
459
12.6M
      return wp_pred;
460
71.6M
    case Predictor::Gradient:
461
71.6M
      return pixel_type_w{ClampedGradient(left, top, topleft)};
462
388k
    case Predictor::TopLeft:
463
388k
      return topleft;
464
298k
    case Predictor::TopRight:
465
298k
      return topright;
466
56.8k
    case Predictor::LeftLeft:
467
56.8k
      return leftleft;
468
3.50M
    case Predictor::Average0:
469
3.50M
      return (left + top) / 2;
470
211k
    case Predictor::Average1:
471
211k
      return (left + topleft) / 2;
472
356k
    case Predictor::Average2:
473
356k
      return (topleft + top) / 2;
474
83.8k
    case Predictor::Average3:
475
83.8k
      return (top + topright) / 2;
476
444k
    case Predictor::Average4:
477
444k
      return (6 * top - 2 * toptop + 7 * left + 1 * leftleft +
478
444k
              1 * toprightright + 3 * topright + 8) /
479
444k
             16;
480
0
    default:
481
0
      return pixel_type_w{0};
482
124M
  }
483
124M
}
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
124M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
124M
  size_t offset = 3;
493
124M
  constexpr bool compute_properties =
494
124M
      mode & kUseTree || mode & kForceComputeProperties;
495
124M
  constexpr bool nec = mode & kNoEdgeCases;
496
124M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
124M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
124M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
124M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
124M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
124M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
124M
  pixel_type_w toprightright =
503
124M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
124M
  if (compute_properties) {
506
    // location
507
114M
    (*p)[offset++] = x;
508
    // neighbors
509
114M
    (*p)[offset++] = top > 0 ? top : -top;
510
114M
    (*p)[offset++] = left > 0 ? left : -left;
511
114M
    (*p)[offset++] = top;
512
114M
    (*p)[offset++] = left;
513
514
    // local gradient
515
114M
    (*p)[offset] = left - (*p)[offset + 1];
516
114M
    offset++;
517
    // local gradient
518
114M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
114M
    (*p)[offset++] = left - topleft;
522
114M
    (*p)[offset++] = topleft - top;
523
114M
    (*p)[offset++] = top - topright;
524
114M
    (*p)[offset++] = top - toptop;
525
114M
    (*p)[offset++] = left - leftleft;
526
114M
  }
527
528
124M
  pixel_type_w wp_pred = 0;
529
124M
  if (mode & kUseWP) {
530
80.2M
    wp_pred = wp_state->Predict<compute_properties>(
531
80.2M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
80.2M
  }
533
124M
  if (!nec && compute_properties) {
534
37.7M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
37.7M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
50.5M
    for (size_t i = 0; i < references->w; i++) {
538
12.7M
      (*p)[offset++] = rp[i];
539
12.7M
    }
540
37.7M
  }
541
124M
  PredictionResult result;
542
124M
  if (mode & kUseTree) {
543
90.9M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
90.9M
    result.context = lr.context;
545
90.9M
    result.guess = lr.offset;
546
90.9M
    result.multiplier = lr.multiplier;
547
90.9M
    predictor = lr.predictor;
548
90.9M
  }
549
124M
  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
124M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
124M
                             leftleft, toprightright, wp_pred);
558
124M
  result.predictor = predictor;
559
560
124M
  return result;
561
124M
}
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
782k
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
782k
  size_t offset = 3;
493
782k
  constexpr bool compute_properties =
494
782k
      mode & kUseTree || mode & kForceComputeProperties;
495
782k
  constexpr bool nec = mode & kNoEdgeCases;
496
782k
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
782k
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
782k
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
782k
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
782k
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
782k
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
782k
  pixel_type_w toprightright =
503
782k
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
782k
  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
782k
  pixel_type_w wp_pred = 0;
529
782k
  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
782k
  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
782k
  PredictionResult result;
542
782k
  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
782k
  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
782k
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
782k
                             leftleft, toprightright, wp_pred);
558
782k
  result.predictor = predictor;
559
560
782k
  return result;
561
782k
}
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
8.75M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
8.75M
  size_t offset = 3;
493
8.75M
  constexpr bool compute_properties =
494
8.75M
      mode & kUseTree || mode & kForceComputeProperties;
495
8.75M
  constexpr bool nec = mode & kNoEdgeCases;
496
8.75M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
8.75M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
8.75M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
8.75M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
8.75M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
8.75M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
8.75M
  pixel_type_w toprightright =
503
8.75M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
8.75M
  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
8.75M
  pixel_type_w wp_pred = 0;
529
8.75M
  if (mode & kUseWP) {
530
8.75M
    wp_pred = wp_state->Predict<compute_properties>(
531
8.75M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
8.75M
  }
533
8.75M
  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
8.75M
  PredictionResult result;
542
8.75M
  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
8.75M
  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
8.75M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
8.75M
                             leftleft, toprightright, wp_pred);
558
8.75M
  result.predictor = predictor;
559
560
8.75M
  return result;
561
8.75M
}
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
14.6M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
14.6M
  size_t offset = 3;
493
14.6M
  constexpr bool compute_properties =
494
14.6M
      mode & kUseTree || mode & kForceComputeProperties;
495
14.6M
  constexpr bool nec = mode & kNoEdgeCases;
496
14.6M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
14.6M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
14.6M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
14.6M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
14.6M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
14.6M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
14.6M
  pixel_type_w toprightright =
503
14.6M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
14.6M
  if (compute_properties) {
506
    // location
507
14.6M
    (*p)[offset++] = x;
508
    // neighbors
509
14.6M
    (*p)[offset++] = top > 0 ? top : -top;
510
14.6M
    (*p)[offset++] = left > 0 ? left : -left;
511
14.6M
    (*p)[offset++] = top;
512
14.6M
    (*p)[offset++] = left;
513
514
    // local gradient
515
14.6M
    (*p)[offset] = left - (*p)[offset + 1];
516
14.6M
    offset++;
517
    // local gradient
518
14.6M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
14.6M
    (*p)[offset++] = left - topleft;
522
14.6M
    (*p)[offset++] = topleft - top;
523
14.6M
    (*p)[offset++] = top - topright;
524
14.6M
    (*p)[offset++] = top - toptop;
525
14.6M
    (*p)[offset++] = left - leftleft;
526
14.6M
  }
527
528
14.6M
  pixel_type_w wp_pred = 0;
529
14.6M
  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
14.6M
  if (!nec && compute_properties) {
534
14.6M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
14.6M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
25.7M
    for (size_t i = 0; i < references->w; i++) {
538
11.1M
      (*p)[offset++] = rp[i];
539
11.1M
    }
540
14.6M
  }
541
14.6M
  PredictionResult result;
542
14.6M
  if (mode & kUseTree) {
543
14.6M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
14.6M
    result.context = lr.context;
545
14.6M
    result.guess = lr.offset;
546
14.6M
    result.multiplier = lr.multiplier;
547
14.6M
    predictor = lr.predictor;
548
14.6M
  }
549
14.6M
  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
14.6M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
14.6M
                             leftleft, toprightright, wp_pred);
558
14.6M
  result.predictor = predictor;
559
560
14.6M
  return result;
561
14.6M
}
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
28.4M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
28.4M
  size_t offset = 3;
493
28.4M
  constexpr bool compute_properties =
494
28.4M
      mode & kUseTree || mode & kForceComputeProperties;
495
28.4M
  constexpr bool nec = mode & kNoEdgeCases;
496
28.4M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
28.4M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
28.4M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
28.4M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
28.4M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
28.4M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
28.4M
  pixel_type_w toprightright =
503
28.4M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
28.4M
  if (compute_properties) {
506
    // location
507
28.4M
    (*p)[offset++] = x;
508
    // neighbors
509
28.4M
    (*p)[offset++] = top > 0 ? top : -top;
510
28.4M
    (*p)[offset++] = left > 0 ? left : -left;
511
28.4M
    (*p)[offset++] = top;
512
28.4M
    (*p)[offset++] = left;
513
514
    // local gradient
515
28.4M
    (*p)[offset] = left - (*p)[offset + 1];
516
28.4M
    offset++;
517
    // local gradient
518
28.4M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
28.4M
    (*p)[offset++] = left - topleft;
522
28.4M
    (*p)[offset++] = topleft - top;
523
28.4M
    (*p)[offset++] = top - topright;
524
28.4M
    (*p)[offset++] = top - toptop;
525
28.4M
    (*p)[offset++] = left - leftleft;
526
28.4M
  }
527
528
28.4M
  pixel_type_w wp_pred = 0;
529
28.4M
  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
28.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
28.4M
  PredictionResult result;
542
28.4M
  if (mode & kUseTree) {
543
28.4M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
28.4M
    result.context = lr.context;
545
28.4M
    result.guess = lr.offset;
546
28.4M
    result.multiplier = lr.multiplier;
547
28.4M
    predictor = lr.predictor;
548
28.4M
  }
549
28.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
28.4M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
28.4M
                             leftleft, toprightright, wp_pred);
558
28.4M
  result.predictor = predictor;
559
560
28.4M
  return result;
561
28.4M
}
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
21.9M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
21.9M
  size_t offset = 3;
493
21.9M
  constexpr bool compute_properties =
494
21.9M
      mode & kUseTree || mode & kForceComputeProperties;
495
21.9M
  constexpr bool nec = mode & kNoEdgeCases;
496
21.9M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
21.9M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
21.9M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
21.9M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
21.9M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
21.9M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
21.9M
  pixel_type_w toprightright =
503
21.9M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
21.9M
  if (compute_properties) {
506
    // location
507
21.9M
    (*p)[offset++] = x;
508
    // neighbors
509
21.9M
    (*p)[offset++] = top > 0 ? top : -top;
510
21.9M
    (*p)[offset++] = left > 0 ? left : -left;
511
21.9M
    (*p)[offset++] = top;
512
21.9M
    (*p)[offset++] = left;
513
514
    // local gradient
515
21.9M
    (*p)[offset] = left - (*p)[offset + 1];
516
21.9M
    offset++;
517
    // local gradient
518
21.9M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
21.9M
    (*p)[offset++] = left - topleft;
522
21.9M
    (*p)[offset++] = topleft - top;
523
21.9M
    (*p)[offset++] = top - topright;
524
21.9M
    (*p)[offset++] = top - toptop;
525
21.9M
    (*p)[offset++] = left - leftleft;
526
21.9M
  }
527
528
21.9M
  pixel_type_w wp_pred = 0;
529
21.9M
  if (mode & kUseWP) {
530
21.9M
    wp_pred = wp_state->Predict<compute_properties>(
531
21.9M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
21.9M
  }
533
21.9M
  if (!nec && compute_properties) {
534
21.9M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
21.9M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
23.6M
    for (size_t i = 0; i < references->w; i++) {
538
1.62M
      (*p)[offset++] = rp[i];
539
1.62M
    }
540
21.9M
  }
541
21.9M
  PredictionResult result;
542
21.9M
  if (mode & kUseTree) {
543
21.9M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
21.9M
    result.context = lr.context;
545
21.9M
    result.guess = lr.offset;
546
21.9M
    result.multiplier = lr.multiplier;
547
21.9M
    predictor = lr.predictor;
548
21.9M
  }
549
21.9M
  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
21.9M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
21.9M
                             leftleft, toprightright, wp_pred);
558
21.9M
  result.predictor = predictor;
559
560
21.9M
  return result;
561
21.9M
}
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
25.9M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
25.9M
  size_t offset = 3;
493
25.9M
  constexpr bool compute_properties =
494
25.9M
      mode & kUseTree || mode & kForceComputeProperties;
495
25.9M
  constexpr bool nec = mode & kNoEdgeCases;
496
25.9M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
25.9M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
25.9M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
25.9M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
25.9M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
25.9M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
25.9M
  pixel_type_w toprightright =
503
25.9M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
25.9M
  if (compute_properties) {
506
    // location
507
25.9M
    (*p)[offset++] = x;
508
    // neighbors
509
25.9M
    (*p)[offset++] = top > 0 ? top : -top;
510
25.9M
    (*p)[offset++] = left > 0 ? left : -left;
511
25.9M
    (*p)[offset++] = top;
512
25.9M
    (*p)[offset++] = left;
513
514
    // local gradient
515
25.9M
    (*p)[offset] = left - (*p)[offset + 1];
516
25.9M
    offset++;
517
    // local gradient
518
25.9M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
25.9M
    (*p)[offset++] = left - topleft;
522
25.9M
    (*p)[offset++] = topleft - top;
523
25.9M
    (*p)[offset++] = top - topright;
524
25.9M
    (*p)[offset++] = top - toptop;
525
25.9M
    (*p)[offset++] = left - leftleft;
526
25.9M
  }
527
528
25.9M
  pixel_type_w wp_pred = 0;
529
25.9M
  if (mode & kUseWP) {
530
25.9M
    wp_pred = wp_state->Predict<compute_properties>(
531
25.9M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
25.9M
  }
533
25.9M
  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.9M
  PredictionResult result;
542
25.9M
  if (mode & kUseTree) {
543
25.9M
    MATreeLookup::LookupResult lr = lookup->Lookup(*p);
544
25.9M
    result.context = lr.context;
545
25.9M
    result.guess = lr.offset;
546
25.9M
    result.multiplier = lr.multiplier;
547
25.9M
    predictor = lr.predictor;
548
25.9M
  }
549
25.9M
  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.9M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
25.9M
                             leftleft, toprightright, wp_pred);
558
25.9M
  result.predictor = predictor;
559
560
25.9M
  return result;
561
25.9M
}
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.18M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
1.18M
  size_t offset = 3;
493
1.18M
  constexpr bool compute_properties =
494
1.18M
      mode & kUseTree || mode & kForceComputeProperties;
495
1.18M
  constexpr bool nec = mode & kNoEdgeCases;
496
1.18M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
1.18M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
1.18M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
1.18M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
1.18M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
1.18M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
1.18M
  pixel_type_w toprightright =
503
1.18M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
1.18M
  if (compute_properties) {
506
    // location
507
1.18M
    (*p)[offset++] = x;
508
    // neighbors
509
1.18M
    (*p)[offset++] = top > 0 ? top : -top;
510
1.18M
    (*p)[offset++] = left > 0 ? left : -left;
511
1.18M
    (*p)[offset++] = top;
512
1.18M
    (*p)[offset++] = left;
513
514
    // local gradient
515
1.18M
    (*p)[offset] = left - (*p)[offset + 1];
516
1.18M
    offset++;
517
    // local gradient
518
1.18M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
1.18M
    (*p)[offset++] = left - topleft;
522
1.18M
    (*p)[offset++] = topleft - top;
523
1.18M
    (*p)[offset++] = top - topright;
524
1.18M
    (*p)[offset++] = top - toptop;
525
1.18M
    (*p)[offset++] = left - leftleft;
526
1.18M
  }
527
528
1.18M
  pixel_type_w wp_pred = 0;
529
1.18M
  if (mode & kUseWP) {
530
1.18M
    wp_pred = wp_state->Predict<compute_properties>(
531
1.18M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
1.18M
  }
533
1.18M
  if (!nec && compute_properties) {
534
1.18M
    offset += weighted::kNumProperties;
535
    // Extra properties.
536
1.18M
    const pixel_type *JXL_RESTRICT rp = references->Row(x);
537
1.18M
    for (size_t i = 0; i < references->w; i++) {
538
0
      (*p)[offset++] = rp[i];
539
0
    }
540
1.18M
  }
541
1.18M
  PredictionResult result;
542
1.18M
  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.18M
  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.18M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
1.18M
                             leftleft, toprightright, wp_pred);
558
1.18M
  result.predictor = predictor;
559
560
1.18M
  return result;
561
1.18M
}
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
22.3M
    weighted::State *wp_state, pixel_type_w *predictions) {
491
  // We start in position 3 because of 2 static properties + y.
492
22.3M
  size_t offset = 3;
493
22.3M
  constexpr bool compute_properties =
494
22.3M
      mode & kUseTree || mode & kForceComputeProperties;
495
22.3M
  constexpr bool nec = mode & kNoEdgeCases;
496
22.3M
  pixel_type_w left = (nec || x ? pp[-1] : (y ? pp[-onerow] : 0));
497
22.3M
  pixel_type_w top = (nec || y ? pp[-onerow] : left);
498
22.3M
  pixel_type_w topleft = (nec || (x && y) ? pp[-1 - onerow] : left);
499
22.3M
  pixel_type_w topright = (nec || (x + 1 < w && y) ? pp[1 - onerow] : top);
500
22.3M
  pixel_type_w leftleft = (nec || x > 1 ? pp[-2] : left);
501
22.3M
  pixel_type_w toptop = (nec || y > 1 ? pp[-onerow - onerow] : top);
502
22.3M
  pixel_type_w toprightright =
503
22.3M
      (nec || (x + 2 < w && y) ? pp[2 - onerow] : topright);
504
505
22.3M
  if (compute_properties) {
506
    // location
507
22.3M
    (*p)[offset++] = x;
508
    // neighbors
509
22.3M
    (*p)[offset++] = top > 0 ? top : -top;
510
22.3M
    (*p)[offset++] = left > 0 ? left : -left;
511
22.3M
    (*p)[offset++] = top;
512
22.3M
    (*p)[offset++] = left;
513
514
    // local gradient
515
22.3M
    (*p)[offset] = left - (*p)[offset + 1];
516
22.3M
    offset++;
517
    // local gradient
518
22.3M
    (*p)[offset++] = left + top - topleft;
519
520
    // FFV1 context properties
521
22.3M
    (*p)[offset++] = left - topleft;
522
22.3M
    (*p)[offset++] = topleft - top;
523
22.3M
    (*p)[offset++] = top - topright;
524
22.3M
    (*p)[offset++] = top - toptop;
525
22.3M
    (*p)[offset++] = left - leftleft;
526
22.3M
  }
527
528
22.3M
  pixel_type_w wp_pred = 0;
529
22.3M
  if (mode & kUseWP) {
530
22.3M
    wp_pred = wp_state->Predict<compute_properties>(
531
22.3M
        x, y, w, top, left, topright, topleft, toptop, p, offset);
532
22.3M
  }
533
22.3M
  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
22.3M
  PredictionResult result;
542
22.3M
  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
22.3M
  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
22.3M
  result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
557
22.3M
                             leftleft, toprightright, wp_pred);
558
22.3M
  result.predictor = predictor;
559
560
22.3M
  return result;
561
22.3M
}
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
782k
                                          const int y, Predictor predictor) {
568
782k
  return detail::Predict</*mode=*/0>(
569
782k
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
570
782k
      /*references=*/nullptr, /*wp_state=*/nullptr, /*predictions=*/nullptr);
571
782k
}
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
8.75M
                                        weighted::State *wp_state) {
578
8.75M
  return detail::Predict<detail::kUseWP>(
579
8.75M
      /*p=*/nullptr, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr,
580
8.75M
      /*references=*/nullptr, wp_state, /*predictions=*/nullptr);
581
8.75M
}
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
14.6M
                                        const Channel &references) {
589
14.6M
  return detail::Predict<detail::kUseTree>(
590
14.6M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
591
14.6M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
592
14.6M
}
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
28.4M
                   const MATreeLookup &tree_lookup, const Channel &references) {
598
28.4M
  return detail::Predict<detail::kUseTree | detail::kNoEdgeCases>(
599
28.4M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
600
28.4M
      /*wp_state=*/nullptr, /*predictions=*/nullptr);
601
28.4M
}
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
21.9M
                                      weighted::State *wp_state) {
610
21.9M
  return detail::Predict<detail::kUseTree | detail::kUseWP>(
611
21.9M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
612
21.9M
      wp_state, /*predictions=*/nullptr);
613
21.9M
}
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
25.9M
                                             weighted::State *wp_state) {
621
25.9M
  return detail::Predict<detail::kUseTree | detail::kUseWP |
622
25.9M
                         detail::kNoEdgeCases>(
623
25.9M
      p, w, pp, onerow, x, y, Predictor::Zero, &tree_lookup, &references,
624
25.9M
      wp_state, /*predictions=*/nullptr);
625
25.9M
}
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.18M
                                     weighted::State *wp_state) {
633
1.18M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP>(
634
1.18M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
635
1.18M
      wp_state, /*predictions=*/nullptr);
636
1.18M
}
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
22.3M
                                        weighted::State *wp_state) {
655
22.3M
  return detail::Predict<detail::kForceComputeProperties | detail::kUseWP |
656
22.3M
                         detail::kNoEdgeCases>(
657
22.3M
      p, w, pp, onerow, x, y, predictor, /*lookup=*/nullptr, &references,
658
22.3M
      wp_state, /*predictions=*/nullptr);
659
22.3M
}
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_