Coverage Report

Created: 2025-06-22 08:04

/src/libjxl/lib/jxl/enc_heuristics.cc
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
#include "lib/jxl/enc_heuristics.h"
7
8
#include <jxl/cms_interface.h>
9
#include <jxl/memory_manager.h>
10
11
#include <algorithm>
12
#include <cstddef>
13
#include <cstdint>
14
#include <cstdlib>
15
#include <limits>
16
#include <memory>
17
#include <numeric>
18
#include <string>
19
#include <utility>
20
#include <vector>
21
22
#include "lib/jxl/ac_context.h"
23
#include "lib/jxl/ac_strategy.h"
24
#include "lib/jxl/base/common.h"
25
#include "lib/jxl/base/compiler_specific.h"
26
#include "lib/jxl/base/data_parallel.h"
27
#include "lib/jxl/base/override.h"
28
#include "lib/jxl/base/rect.h"
29
#include "lib/jxl/base/status.h"
30
#include "lib/jxl/butteraugli/butteraugli.h"
31
#include "lib/jxl/chroma_from_luma.h"
32
#include "lib/jxl/coeff_order.h"
33
#include "lib/jxl/coeff_order_fwd.h"
34
#include "lib/jxl/common.h"
35
#include "lib/jxl/dec_cache.h"
36
#include "lib/jxl/dec_group.h"
37
#include "lib/jxl/dec_noise.h"
38
#include "lib/jxl/dec_xyb.h"
39
#include "lib/jxl/enc_ac_strategy.h"
40
#include "lib/jxl/enc_adaptive_quantization.h"
41
#include "lib/jxl/enc_cache.h"
42
#include "lib/jxl/enc_chroma_from_luma.h"
43
#include "lib/jxl/enc_gaborish.h"
44
#include "lib/jxl/enc_modular.h"
45
#include "lib/jxl/enc_noise.h"
46
#include "lib/jxl/enc_params.h"
47
#include "lib/jxl/enc_patch_dictionary.h"
48
#include "lib/jxl/enc_quant_weights.h"
49
#include "lib/jxl/enc_splines.h"
50
#include "lib/jxl/epf.h"
51
#include "lib/jxl/frame_dimensions.h"
52
#include "lib/jxl/frame_header.h"
53
#include "lib/jxl/image.h"
54
#include "lib/jxl/image_metadata.h"
55
#include "lib/jxl/image_ops.h"
56
#include "lib/jxl/memory_manager_internal.h"
57
#include "lib/jxl/passes_state.h"
58
#include "lib/jxl/quant_weights.h"
59
60
namespace jxl {
61
62
struct AuxOut;
63
64
void FindBestBlockEntropyModel(const CompressParams& cparams, const ImageI& rqf,
65
                               const AcStrategyImage& ac_strategy,
66
0
                               BlockCtxMap* block_ctx_map) {
67
0
  if (cparams.decoding_speed_tier >= 1) {
68
0
    static constexpr uint8_t kSimpleCtxMap[] = {
69
        // Cluster all blocks together
70
0
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  //
71
0
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  //
72
0
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  //
73
0
    };
74
0
    static_assert(
75
0
        3 * kNumOrders == sizeof(kSimpleCtxMap) / sizeof *kSimpleCtxMap,
76
0
        "Update simple context map");
77
78
0
    auto bcm = *block_ctx_map;
79
0
    bcm.ctx_map.assign(std::begin(kSimpleCtxMap), std::end(kSimpleCtxMap));
80
0
    bcm.num_ctxs = 2;
81
0
    bcm.num_dc_ctxs = 1;
82
0
    return;
83
0
  }
84
0
  if (cparams.speed_tier >= SpeedTier::kFalcon) {
85
0
    return;
86
0
  }
87
  // No need to change context modeling for small images.
88
0
  size_t tot = rqf.xsize() * rqf.ysize();
89
0
  size_t size_for_ctx_model = (1 << 10) * cparams.butteraugli_distance;
90
0
  if (tot < size_for_ctx_model) return;
91
92
0
  struct OccCounters {
93
    // count the occurrences of each qf value and each strategy type.
94
0
    OccCounters(const ImageI& rqf, const AcStrategyImage& ac_strategy) {
95
0
      for (size_t y = 0; y < rqf.ysize(); y++) {
96
0
        const int32_t* qf_row = rqf.Row(y);
97
0
        AcStrategyRow acs_row = ac_strategy.ConstRow(y);
98
0
        for (size_t x = 0; x < rqf.xsize(); x++) {
99
0
          int ord = kStrategyOrder[acs_row[x].RawStrategy()];
100
0
          int qf = qf_row[x] - 1;
101
0
          qf_counts[qf]++;
102
0
          qf_ord_counts[ord][qf]++;
103
0
          ord_counts[ord]++;
104
0
        }
105
0
      }
106
0
    }
107
108
0
    size_t qf_counts[256] = {};
109
0
    size_t qf_ord_counts[kNumOrders][256] = {};
110
0
    size_t ord_counts[kNumOrders] = {};
111
0
  };
112
  // The OccCounters struct is too big to allocate on the stack.
113
0
  std::unique_ptr<OccCounters> counters(new OccCounters(rqf, ac_strategy));
114
115
  // Splitting the context model according to the quantization field seems to
116
  // mostly benefit only large images.
117
0
  size_t size_for_qf_split = (1 << 13) * cparams.butteraugli_distance;
118
0
  size_t num_qf_segments = tot < size_for_qf_split ? 1 : 2;
119
0
  std::vector<uint32_t>& qft = block_ctx_map->qf_thresholds;
120
0
  qft.clear();
121
  // Divide the quant field in up to num_qf_segments segments.
122
0
  size_t cumsum = 0;
123
0
  size_t next = 1;
124
0
  size_t last_cut = 256;
125
0
  size_t cut = tot * next / num_qf_segments;
126
0
  for (uint32_t j = 0; j < 256; j++) {
127
0
    cumsum += counters->qf_counts[j];
128
0
    if (cumsum > cut) {
129
0
      if (j != 0) {
130
0
        qft.push_back(j);
131
0
      }
132
0
      last_cut = j;
133
0
      while (cumsum > cut) {
134
0
        next++;
135
0
        cut = tot * next / num_qf_segments;
136
0
      }
137
0
    } else if (next > qft.size() + 1) {
138
0
      if (j - 1 == last_cut && j != 0) {
139
0
        qft.push_back(j);
140
0
      }
141
0
    }
142
0
  }
143
144
  // Count the occurrences of each segment.
145
0
  std::vector<size_t> counts(kNumOrders * (qft.size() + 1));
146
0
  size_t qft_pos = 0;
147
0
  for (size_t j = 0; j < 256; j++) {
148
0
    if (qft_pos < qft.size() && j == qft[qft_pos]) {
149
0
      qft_pos++;
150
0
    }
151
0
    for (size_t i = 0; i < kNumOrders; i++) {
152
0
      counts[qft_pos + i * (qft.size() + 1)] += counters->qf_ord_counts[i][j];
153
0
    }
154
0
  }
155
156
  // Repeatedly merge the lowest-count pair.
157
0
  std::vector<uint8_t> remap((qft.size() + 1) * kNumOrders);
158
0
  std::iota(remap.begin(), remap.end(), 0);
159
0
  std::vector<uint8_t> clusters(remap);
160
0
  size_t nb_clusters =
161
0
      Clamp1(static_cast<int>(tot / size_for_ctx_model / 2), 2, 9);
162
0
  size_t nb_clusters_chroma =
163
0
      Clamp1(static_cast<int>(tot / size_for_ctx_model / 3), 1, 5);
164
  // This is O(n^2 log n), but n is small.
165
0
  while (clusters.size() > nb_clusters) {
166
0
    std::sort(clusters.begin(), clusters.end(),
167
0
              [&](int a, int b) { return counts[a] > counts[b]; });
168
0
    counts[clusters[clusters.size() - 2]] += counts[clusters.back()];
169
0
    counts[clusters.back()] = 0;
170
0
    remap[clusters.back()] = clusters[clusters.size() - 2];
171
0
    clusters.pop_back();
172
0
  }
173
0
  for (size_t i = 0; i < remap.size(); i++) {
174
0
    while (remap[remap[i]] != remap[i]) {
175
0
      remap[i] = remap[remap[i]];
176
0
    }
177
0
  }
178
  // Relabel starting from 0.
179
0
  std::vector<uint8_t> remap_remap(remap.size(), remap.size());
180
0
  size_t num = 0;
181
0
  for (size_t i = 0; i < remap.size(); i++) {
182
0
    if (remap_remap[remap[i]] == remap.size()) {
183
0
      remap_remap[remap[i]] = num++;
184
0
    }
185
0
    remap[i] = remap_remap[remap[i]];
186
0
  }
187
  // Write the block context map.
188
0
  auto& ctx_map = block_ctx_map->ctx_map;
189
0
  ctx_map = remap;
190
0
  ctx_map.resize(remap.size() * 3);
191
  // for chroma, only use up to nb_clusters_chroma separate block contexts
192
  // (those for the biggest clusters)
193
0
  for (size_t i = remap.size(); i < remap.size() * 3; i++) {
194
0
    ctx_map[i] = num + Clamp1(static_cast<int>(remap[i % remap.size()]), 0,
195
0
                              static_cast<int>(nb_clusters_chroma) - 1);
196
0
  }
197
0
  block_ctx_map->num_ctxs =
198
0
      *std::max_element(ctx_map.begin(), ctx_map.end()) + 1;
199
0
}
200
201
namespace {
202
203
Status FindBestDequantMatrices(JxlMemoryManager* memory_manager,
204
                               const CompressParams& cparams,
205
                               ModularFrameEncoder* modular_frame_encoder,
206
0
                               DequantMatrices* dequant_matrices) {
207
  // TODO(veluca): quant matrices for no-gaborish.
208
  // TODO(veluca): heuristics for in-bitstream quant tables.
209
0
  *dequant_matrices = DequantMatrices();
210
0
  if (cparams.max_error_mode || cparams.disable_perceptual_optimizations) {
211
0
    constexpr float kMSEWeights[3] = {0.001, 0.001, 0.001};
212
0
    const float* wp = cparams.disable_perceptual_optimizations
213
0
                          ? kMSEWeights
214
0
                          : cparams.max_error;
215
    // Set numerators of all quantization matrices to constant values.
216
0
    float weights[3][1] = {{1.0f / wp[0]}, {1.0f / wp[1]}, {1.0f / wp[2]}};
217
0
    DctQuantWeightParams dct_params(weights);
218
0
    std::vector<QuantEncoding> encodings(kNumQuantTables,
219
0
                                         QuantEncoding::DCT(dct_params));
220
0
    JXL_RETURN_IF_ERROR(DequantMatricesSetCustom(dequant_matrices, encodings,
221
0
                                                 modular_frame_encoder));
222
0
    float dc_weights[3] = {1.0f / wp[0], 1.0f / wp[1], 1.0f / wp[2]};
223
0
    JXL_RETURN_IF_ERROR(DequantMatricesSetCustomDC(
224
0
        memory_manager, dequant_matrices, dc_weights));
225
0
  }
226
0
  return true;
227
0
}
228
229
0
void StoreMin2(const float v, float& min1, float& min2) {
230
0
  if (v < min2) {
231
0
    if (v < min1) {
232
0
      min2 = min1;
233
0
      min1 = v;
234
0
    } else {
235
0
      min2 = v;
236
0
    }
237
0
  }
238
0
}
239
240
0
void CreateMask(const ImageF& image, ImageF& mask) {
241
0
  for (size_t y = 0; y < image.ysize(); y++) {
242
0
    const auto* row_n = y > 0 ? image.Row(y - 1) : image.Row(y);
243
0
    const auto* row_in = image.Row(y);
244
0
    const auto* row_s = y + 1 < image.ysize() ? image.Row(y + 1) : image.Row(y);
245
0
    auto* row_out = mask.Row(y);
246
0
    for (size_t x = 0; x < image.xsize(); x++) {
247
      // Center, west, east, north, south values and their absolute difference
248
0
      float c = row_in[x];
249
0
      float w = x > 0 ? row_in[x - 1] : row_in[x];
250
0
      float e = x + 1 < image.xsize() ? row_in[x + 1] : row_in[x];
251
0
      float n = row_n[x];
252
0
      float s = row_s[x];
253
0
      float dw = std::abs(c - w);
254
0
      float de = std::abs(c - e);
255
0
      float dn = std::abs(c - n);
256
0
      float ds = std::abs(c - s);
257
0
      float min = std::numeric_limits<float>::max();
258
0
      float min2 = std::numeric_limits<float>::max();
259
0
      StoreMin2(dw, min, min2);
260
0
      StoreMin2(de, min, min2);
261
0
      StoreMin2(dn, min, min2);
262
0
      StoreMin2(ds, min, min2);
263
0
      row_out[x] = min2;
264
0
    }
265
0
  }
266
0
}
267
268
// Downsamples the image by a factor of 2 with a kernel that's sharper than
269
// the standard 2x2 box kernel used by DownsampleImage.
270
// The kernel is optimized against the result of the 2x2 upsampling kernel used
271
// by the decoder. Ringing is slightly reduced by clamping the values of the
272
// resulting pixels within certain bounds of a small region in the original
273
// image.
274
0
Status DownsampleImage2_Sharper(const ImageF& input, ImageF* output) {
275
0
  const int64_t kernelx = 12;
276
0
  const int64_t kernely = 12;
277
0
  JxlMemoryManager* memory_manager = input.memory_manager();
278
279
0
  static const float kernel[144] = {
280
0
      -0.000314256996835, -0.000314256996835, -0.000897597057705,
281
0
      -0.000562751488849, -0.000176807273646, 0.001864627368902,
282
0
      0.001864627368902,  -0.000176807273646, -0.000562751488849,
283
0
      -0.000897597057705, -0.000314256996835, -0.000314256996835,
284
0
      -0.000314256996835, -0.001527942804748, -0.000121760530512,
285
0
      0.000191123989093,  0.010193185932466,  0.058637519197110,
286
0
      0.058637519197110,  0.010193185932466,  0.000191123989093,
287
0
      -0.000121760530512, -0.001527942804748, -0.000314256996835,
288
0
      -0.000897597057705, -0.000121760530512, 0.000946363683751,
289
0
      0.007113577630288,  0.000437956841058,  -0.000372823835211,
290
0
      -0.000372823835211, 0.000437956841058,  0.007113577630288,
291
0
      0.000946363683751,  -0.000121760530512, -0.000897597057705,
292
0
      -0.000562751488849, 0.000191123989093,  0.007113577630288,
293
0
      0.044592622228814,  0.000222278879007,  -0.162864473015945,
294
0
      -0.162864473015945, 0.000222278879007,  0.044592622228814,
295
0
      0.007113577630288,  0.000191123989093,  -0.000562751488849,
296
0
      -0.000176807273646, 0.010193185932466,  0.000437956841058,
297
0
      0.000222278879007,  -0.000913092543974, -0.017071696107902,
298
0
      -0.017071696107902, -0.000913092543974, 0.000222278879007,
299
0
      0.000437956841058,  0.010193185932466,  -0.000176807273646,
300
0
      0.001864627368902,  0.058637519197110,  -0.000372823835211,
301
0
      -0.162864473015945, -0.017071696107902, 0.414660099370354,
302
0
      0.414660099370354,  -0.017071696107902, -0.162864473015945,
303
0
      -0.000372823835211, 0.058637519197110,  0.001864627368902,
304
0
      0.001864627368902,  0.058637519197110,  -0.000372823835211,
305
0
      -0.162864473015945, -0.017071696107902, 0.414660099370354,
306
0
      0.414660099370354,  -0.017071696107902, -0.162864473015945,
307
0
      -0.000372823835211, 0.058637519197110,  0.001864627368902,
308
0
      -0.000176807273646, 0.010193185932466,  0.000437956841058,
309
0
      0.000222278879007,  -0.000913092543974, -0.017071696107902,
310
0
      -0.017071696107902, -0.000913092543974, 0.000222278879007,
311
0
      0.000437956841058,  0.010193185932466,  -0.000176807273646,
312
0
      -0.000562751488849, 0.000191123989093,  0.007113577630288,
313
0
      0.044592622228814,  0.000222278879007,  -0.162864473015945,
314
0
      -0.162864473015945, 0.000222278879007,  0.044592622228814,
315
0
      0.007113577630288,  0.000191123989093,  -0.000562751488849,
316
0
      -0.000897597057705, -0.000121760530512, 0.000946363683751,
317
0
      0.007113577630288,  0.000437956841058,  -0.000372823835211,
318
0
      -0.000372823835211, 0.000437956841058,  0.007113577630288,
319
0
      0.000946363683751,  -0.000121760530512, -0.000897597057705,
320
0
      -0.000314256996835, -0.001527942804748, -0.000121760530512,
321
0
      0.000191123989093,  0.010193185932466,  0.058637519197110,
322
0
      0.058637519197110,  0.010193185932466,  0.000191123989093,
323
0
      -0.000121760530512, -0.001527942804748, -0.000314256996835,
324
0
      -0.000314256996835, -0.000314256996835, -0.000897597057705,
325
0
      -0.000562751488849, -0.000176807273646, 0.001864627368902,
326
0
      0.001864627368902,  -0.000176807273646, -0.000562751488849,
327
0
      -0.000897597057705, -0.000314256996835, -0.000314256996835};
328
329
0
  int64_t xsize = input.xsize();
330
0
  int64_t ysize = input.ysize();
331
332
0
  JXL_ASSIGN_OR_RETURN(ImageF box_downsample,
333
0
                       ImageF::Create(memory_manager, xsize, ysize));
334
0
  JXL_RETURN_IF_ERROR(CopyImageTo(input, &box_downsample));
335
0
  JXL_ASSIGN_OR_RETURN(box_downsample, DownsampleImage(box_downsample, 2));
336
337
0
  JXL_ASSIGN_OR_RETURN(ImageF mask,
338
0
                       ImageF::Create(memory_manager, box_downsample.xsize(),
339
0
                                      box_downsample.ysize()));
340
0
  CreateMask(box_downsample, mask);
341
342
0
  for (size_t y = 0; y < output->ysize(); y++) {
343
0
    float* row_out = output->Row(y);
344
0
    const float* row_in[kernely];
345
0
    const float* row_mask = mask.Row(y);
346
    // get the rows in the support
347
0
    for (size_t ky = 0; ky < kernely; ky++) {
348
0
      int64_t iy = y * 2 + ky - (kernely - 1) / 2;
349
0
      if (iy < 0) iy = 0;
350
0
      if (iy >= ysize) iy = ysize - 1;
351
0
      row_in[ky] = input.Row(iy);
352
0
    }
353
354
0
    for (size_t x = 0; x < output->xsize(); x++) {
355
      // get min and max values of the original image in the support
356
0
      float min = std::numeric_limits<float>::max();
357
0
      float max = std::numeric_limits<float>::min();
358
      // kernelx - R and kernely - R are the radius of a rectangular region in
359
      // which the values of a pixel are bounded to reduce ringing.
360
0
      static constexpr int64_t R = 5;
361
0
      for (int64_t ky = R; ky + R < kernely; ky++) {
362
0
        for (int64_t kx = R; kx + R < kernelx; kx++) {
363
0
          int64_t ix = x * 2 + kx - (kernelx - 1) / 2;
364
0
          if (ix < 0) ix = 0;
365
0
          if (ix >= xsize) ix = xsize - 1;
366
0
          min = std::min<float>(min, row_in[ky][ix]);
367
0
          max = std::max<float>(max, row_in[ky][ix]);
368
0
        }
369
0
      }
370
371
0
      float sum = 0;
372
0
      for (int64_t ky = 0; ky < kernely; ky++) {
373
0
        for (int64_t kx = 0; kx < kernelx; kx++) {
374
0
          int64_t ix = x * 2 + kx - (kernelx - 1) / 2;
375
0
          if (ix < 0) ix = 0;
376
0
          if (ix >= xsize) ix = xsize - 1;
377
0
          sum += row_in[ky][ix] * kernel[ky * kernelx + kx];
378
0
        }
379
0
      }
380
381
0
      row_out[x] = sum;
382
383
      // Clamp the pixel within the value  of a small area to prevent ringning.
384
      // The mask determines how much to clamp, clamp more to reduce more
385
      // ringing in smooth areas, clamp less in noisy areas to get more
386
      // sharpness. Higher mask_multiplier gives less clamping, so less
387
      // ringing reduction.
388
0
      const constexpr float mask_multiplier = 1;
389
0
      float a = row_mask[x] * mask_multiplier;
390
0
      float clip_min = min - a;
391
0
      float clip_max = max + a;
392
0
      if (row_out[x] < clip_min) {
393
0
        row_out[x] = clip_min;
394
0
      } else if (row_out[x] > clip_max) {
395
0
        row_out[x] = clip_max;
396
0
      }
397
0
    }
398
0
  }
399
0
  return true;
400
0
}
401
402
}  // namespace
403
404
0
Status DownsampleImage2_Sharper(Image3F* opsin) {
405
  // Allocate extra space to avoid a reallocation when padding.
406
0
  JxlMemoryManager* memory_manager = opsin->memory_manager();
407
0
  JXL_ASSIGN_OR_RETURN(
408
0
      Image3F downsampled,
409
0
      Image3F::Create(memory_manager, DivCeil(opsin->xsize(), 2) + kBlockDim,
410
0
                      DivCeil(opsin->ysize(), 2) + kBlockDim));
411
0
  JXL_RETURN_IF_ERROR(downsampled.ShrinkTo(downsampled.xsize() - kBlockDim,
412
0
                                           downsampled.ysize() - kBlockDim));
413
414
0
  for (size_t c = 0; c < 3; c++) {
415
0
    JXL_RETURN_IF_ERROR(
416
0
        DownsampleImage2_Sharper(opsin->Plane(c), &downsampled.Plane(c)));
417
0
  }
418
0
  *opsin = std::move(downsampled);
419
0
  return true;
420
0
}
421
422
namespace {
423
424
// The default upsampling kernels used by Upsampler in the decoder.
425
const constexpr int64_t kSize = 5;
426
427
const float kernel00[25] = {
428
    -0.01716200f, -0.03452303f, -0.04022174f, -0.02921014f, -0.00624645f,
429
    -0.03452303f, 0.14111091f,  0.28896755f,  0.00278718f,  -0.01610267f,
430
    -0.04022174f, 0.28896755f,  0.56661550f,  0.03777607f,  -0.01986694f,
431
    -0.02921014f, 0.00278718f,  0.03777607f,  -0.03144731f, -0.01185068f,
432
    -0.00624645f, -0.01610267f, -0.01986694f, -0.01185068f, -0.00213539f,
433
};
434
const float kernel01[25] = {
435
    -0.00624645f, -0.01610267f, -0.01986694f, -0.01185068f, -0.00213539f,
436
    -0.02921014f, 0.00278718f,  0.03777607f,  -0.03144731f, -0.01185068f,
437
    -0.04022174f, 0.28896755f,  0.56661550f,  0.03777607f,  -0.01986694f,
438
    -0.03452303f, 0.14111091f,  0.28896755f,  0.00278718f,  -0.01610267f,
439
    -0.01716200f, -0.03452303f, -0.04022174f, -0.02921014f, -0.00624645f,
440
};
441
const float kernel10[25] = {
442
    -0.00624645f, -0.02921014f, -0.04022174f, -0.03452303f, -0.01716200f,
443
    -0.01610267f, 0.00278718f,  0.28896755f,  0.14111091f,  -0.03452303f,
444
    -0.01986694f, 0.03777607f,  0.56661550f,  0.28896755f,  -0.04022174f,
445
    -0.01185068f, -0.03144731f, 0.03777607f,  0.00278718f,  -0.02921014f,
446
    -0.00213539f, -0.01185068f, -0.01986694f, -0.01610267f, -0.00624645f,
447
};
448
const float kernel11[25] = {
449
    -0.00213539f, -0.01185068f, -0.01986694f, -0.01610267f, -0.00624645f,
450
    -0.01185068f, -0.03144731f, 0.03777607f,  0.00278718f,  -0.02921014f,
451
    -0.01986694f, 0.03777607f,  0.56661550f,  0.28896755f,  -0.04022174f,
452
    -0.01610267f, 0.00278718f,  0.28896755f,  0.14111091f,  -0.03452303f,
453
    -0.00624645f, -0.02921014f, -0.04022174f, -0.03452303f, -0.01716200f,
454
};
455
456
// Does exactly the same as the Upsampler in dec_upsampler for 2x2 pixels, with
457
// default CustomTransformData.
458
// TODO(lode): use Upsampler instead. However, it requires pre-initialization
459
// and padding on the left side of the image which requires refactoring the
460
// other code using this.
461
0
void UpsampleImage(const ImageF& input, ImageF* output) {
462
0
  int64_t xsize = input.xsize();
463
0
  int64_t ysize = input.ysize();
464
0
  int64_t xsize2 = output->xsize();
465
0
  int64_t ysize2 = output->ysize();
466
0
  for (int64_t y = 0; y < ysize2; y++) {
467
0
    for (int64_t x = 0; x < xsize2; x++) {
468
0
      const auto* kernel = kernel00;
469
0
      if ((x & 1) && (y & 1)) {
470
0
        kernel = kernel11;
471
0
      } else if (x & 1) {
472
0
        kernel = kernel10;
473
0
      } else if (y & 1) {
474
0
        kernel = kernel01;
475
0
      }
476
0
      float sum = 0;
477
0
      int64_t x2 = x / 2;
478
0
      int64_t y2 = y / 2;
479
480
      // get min and max values of the original image in the support
481
0
      float min = std::numeric_limits<float>::max();
482
0
      float max = std::numeric_limits<float>::min();
483
484
0
      for (int64_t ky = 0; ky < kSize; ky++) {
485
0
        for (int64_t kx = 0; kx < kSize; kx++) {
486
0
          int64_t xi = x2 - kSize / 2 + kx;
487
0
          int64_t yi = y2 - kSize / 2 + ky;
488
0
          if (xi < 0) xi = 0;
489
0
          if (xi >= xsize) xi = input.xsize() - 1;
490
0
          if (yi < 0) yi = 0;
491
0
          if (yi >= ysize) yi = input.ysize() - 1;
492
0
          min = std::min<float>(min, input.Row(yi)[xi]);
493
0
          max = std::max<float>(max, input.Row(yi)[xi]);
494
0
        }
495
0
      }
496
497
0
      for (int64_t ky = 0; ky < kSize; ky++) {
498
0
        for (int64_t kx = 0; kx < kSize; kx++) {
499
0
          int64_t xi = x2 - kSize / 2 + kx;
500
0
          int64_t yi = y2 - kSize / 2 + ky;
501
0
          if (xi < 0) xi = 0;
502
0
          if (xi >= xsize) xi = input.xsize() - 1;
503
0
          if (yi < 0) yi = 0;
504
0
          if (yi >= ysize) yi = input.ysize() - 1;
505
0
          sum += input.Row(yi)[xi] * kernel[ky * kSize + kx];
506
0
        }
507
0
      }
508
0
      output->Row(y)[x] = sum;
509
0
      if (output->Row(y)[x] < min) output->Row(y)[x] = min;
510
0
      if (output->Row(y)[x] > max) output->Row(y)[x] = max;
511
0
    }
512
0
  }
513
0
}
514
515
// Returns the derivative of Upsampler, with respect to input pixel x2, y2, to
516
// output pixel x, y (ignoring the clamping).
517
0
float UpsamplerDeriv(int64_t x2, int64_t y2, int64_t x, int64_t y) {
518
0
  const auto* kernel = kernel00;
519
0
  if ((x & 1) && (y & 1)) {
520
0
    kernel = kernel11;
521
0
  } else if (x & 1) {
522
0
    kernel = kernel10;
523
0
  } else if (y & 1) {
524
0
    kernel = kernel01;
525
0
  }
526
527
0
  int64_t ix = x / 2;
528
0
  int64_t iy = y / 2;
529
0
  int64_t kx = x2 - ix + kSize / 2;
530
0
  int64_t ky = y2 - iy + kSize / 2;
531
532
  // This should not happen.
533
0
  if (kx < 0 || kx >= kSize || ky < 0 || ky >= kSize) return 0;
534
535
0
  return kernel[ky * kSize + kx];
536
0
}
537
538
// Apply the derivative of the Upsampler to the input, reversing the effect of
539
// its coefficients. The output image is 2x2 times smaller than the input.
540
0
void AntiUpsample(const ImageF& input, ImageF* d) {
541
0
  int64_t xsize = input.xsize();
542
0
  int64_t ysize = input.ysize();
543
0
  int64_t xsize2 = d->xsize();
544
0
  int64_t ysize2 = d->ysize();
545
0
  int64_t k0 = kSize - 1;
546
0
  int64_t k1 = kSize;
547
0
  for (int64_t y2 = 0; y2 < ysize2; ++y2) {
548
0
    auto* row = d->Row(y2);
549
0
    for (int64_t x2 = 0; x2 < xsize2; ++x2) {
550
0
      int64_t x0 = x2 * 2 - k0;
551
0
      if (x0 < 0) x0 = 0;
552
0
      int64_t x1 = x2 * 2 + k1 + 1;
553
0
      if (x1 > xsize) x1 = xsize;
554
0
      int64_t y0 = y2 * 2 - k0;
555
0
      if (y0 < 0) y0 = 0;
556
0
      int64_t y1 = y2 * 2 + k1 + 1;
557
0
      if (y1 > ysize) y1 = ysize;
558
559
0
      float sum = 0;
560
0
      for (int64_t y = y0; y < y1; ++y) {
561
0
        const auto* row_in = input.Row(y);
562
0
        for (int64_t x = x0; x < x1; ++x) {
563
0
          double deriv = UpsamplerDeriv(x2, y2, x, y);
564
0
          sum += deriv * row_in[x];
565
0
        }
566
0
      }
567
0
      row[x2] = sum;
568
0
    }
569
0
  }
570
0
}
571
572
// Element-wise multiplies two images.
573
template <typename T>
574
Status ElwiseMul(const Plane<T>& image1, const Plane<T>& image2,
575
0
                 Plane<T>* out) {
576
0
  const size_t xsize = image1.xsize();
577
0
  const size_t ysize = image1.ysize();
578
0
  JXL_ENSURE(xsize == image2.xsize());
579
0
  JXL_ENSURE(ysize == image2.ysize());
580
0
  JXL_ENSURE(xsize == out->xsize());
581
0
  JXL_ENSURE(ysize == out->ysize());
582
0
  for (size_t y = 0; y < ysize; ++y) {
583
0
    const T* const JXL_RESTRICT row1 = image1.Row(y);
584
0
    const T* const JXL_RESTRICT row2 = image2.Row(y);
585
0
    T* const JXL_RESTRICT row_out = out->Row(y);
586
0
    for (size_t x = 0; x < xsize; ++x) {
587
0
      row_out[x] = row1[x] * row2[x];
588
0
    }
589
0
  }
590
0
  return true;
591
0
}
592
593
// Element-wise divides two images.
594
template <typename T>
595
Status ElwiseDiv(const Plane<T>& image1, const Plane<T>& image2,
596
0
                 Plane<T>* out) {
597
0
  const size_t xsize = image1.xsize();
598
0
  const size_t ysize = image1.ysize();
599
0
  JXL_ENSURE(xsize == image2.xsize());
600
0
  JXL_ENSURE(ysize == image2.ysize());
601
0
  JXL_ENSURE(xsize == out->xsize());
602
0
  JXL_ENSURE(ysize == out->ysize());
603
0
  for (size_t y = 0; y < ysize; ++y) {
604
0
    const T* const JXL_RESTRICT row1 = image1.Row(y);
605
0
    const T* const JXL_RESTRICT row2 = image2.Row(y);
606
0
    T* const JXL_RESTRICT row_out = out->Row(y);
607
0
    for (size_t x = 0; x < xsize; ++x) {
608
0
      row_out[x] = row1[x] / row2[x];
609
0
    }
610
0
  }
611
0
  return true;
612
0
}
613
614
0
void ReduceRinging(const ImageF& initial, const ImageF& mask, ImageF& down) {
615
0
  int64_t xsize2 = down.xsize();
616
0
  int64_t ysize2 = down.ysize();
617
618
0
  for (size_t y = 0; y < down.ysize(); y++) {
619
0
    const float* row_mask = mask.Row(y);
620
0
    float* row_out = down.Row(y);
621
0
    for (size_t x = 0; x < down.xsize(); x++) {
622
0
      float v = down.Row(y)[x];
623
0
      float min = initial.Row(y)[x];
624
0
      float max = initial.Row(y)[x];
625
0
      for (int64_t yi = -1; yi < 2; yi++) {
626
0
        for (int64_t xi = -1; xi < 2; xi++) {
627
0
          int64_t x2 = static_cast<int64_t>(x) + xi;
628
0
          int64_t y2 = static_cast<int64_t>(y) + yi;
629
0
          if (x2 < 0 || y2 < 0 || x2 >= xsize2 || y2 >= ysize2) continue;
630
0
          min = std::min<float>(min, initial.Row(y2)[x2]);
631
0
          max = std::max<float>(max, initial.Row(y2)[x2]);
632
0
        }
633
0
      }
634
635
0
      row_out[x] = v;
636
637
      // Clamp the pixel within the value  of a small area to prevent ringning.
638
      // The mask determines how much to clamp, clamp more to reduce more
639
      // ringing in smooth areas, clamp less in noisy areas to get more
640
      // sharpness. Higher mask_multiplier gives less clamping, so less
641
      // ringing reduction.
642
0
      const constexpr float mask_multiplier = 2;
643
0
      float a = row_mask[x] * mask_multiplier;
644
0
      float clip_min = min - a;
645
0
      float clip_max = max + a;
646
0
      if (row_out[x] < clip_min) row_out[x] = clip_min;
647
0
      if (row_out[x] > clip_max) row_out[x] = clip_max;
648
0
    }
649
0
  }
650
0
}
651
652
// TODO(lode): move this to a separate file enc_downsample.cc
653
0
Status DownsampleImage2_Iterative(const ImageF& orig, ImageF* output) {
654
0
  int64_t xsize = orig.xsize();
655
0
  int64_t ysize = orig.ysize();
656
0
  int64_t xsize2 = DivCeil(orig.xsize(), 2);
657
0
  int64_t ysize2 = DivCeil(orig.ysize(), 2);
658
0
  JxlMemoryManager* memory_manager = orig.memory_manager();
659
660
0
  JXL_ASSIGN_OR_RETURN(ImageF box_downsample,
661
0
                       ImageF::Create(memory_manager, xsize, ysize));
662
0
  JXL_RETURN_IF_ERROR(CopyImageTo(orig, &box_downsample));
663
0
  JXL_ASSIGN_OR_RETURN(box_downsample, DownsampleImage(box_downsample, 2));
664
0
  JXL_ASSIGN_OR_RETURN(ImageF mask,
665
0
                       ImageF::Create(memory_manager, box_downsample.xsize(),
666
0
                                      box_downsample.ysize()));
667
0
  CreateMask(box_downsample, mask);
668
669
0
  JXL_RETURN_IF_ERROR(output->ShrinkTo(xsize2, ysize2));
670
671
  // Initial result image using the sharper downsampling.
672
  // Allocate extra space to avoid a reallocation when padding.
673
0
  JXL_ASSIGN_OR_RETURN(
674
0
      ImageF initial,
675
0
      ImageF::Create(memory_manager, DivCeil(orig.xsize(), 2) + kBlockDim,
676
0
                     DivCeil(orig.ysize(), 2) + kBlockDim));
677
0
  JXL_RETURN_IF_ERROR(initial.ShrinkTo(initial.xsize() - kBlockDim,
678
0
                                       initial.ysize() - kBlockDim));
679
0
  JXL_RETURN_IF_ERROR(DownsampleImage2_Sharper(orig, &initial));
680
681
0
  JXL_ASSIGN_OR_RETURN(
682
0
      ImageF down,
683
0
      ImageF::Create(memory_manager, initial.xsize(), initial.ysize()));
684
0
  JXL_RETURN_IF_ERROR(CopyImageTo(initial, &down));
685
0
  JXL_ASSIGN_OR_RETURN(ImageF up, ImageF::Create(memory_manager, xsize, ysize));
686
0
  JXL_ASSIGN_OR_RETURN(ImageF corr,
687
0
                       ImageF::Create(memory_manager, xsize, ysize));
688
0
  JXL_ASSIGN_OR_RETURN(ImageF corr2,
689
0
                       ImageF::Create(memory_manager, xsize2, ysize2));
690
691
  // In the weights map, relatively higher values will allow less ringing but
692
  // also less sharpness. With all constant values, it optimizes equally
693
  // everywhere. Even in this case, the weights2 computed from
694
  // this is still used and differs at the borders of the image.
695
  // TODO(lode): Make use of the weights field for anti-ringing and clamping,
696
  // the values are all set to 1 for now, but it is intended to be used for
697
  // reducing ringing based on the mask, and taking clamping into account.
698
0
  JXL_ASSIGN_OR_RETURN(ImageF weights,
699
0
                       ImageF::Create(memory_manager, xsize, ysize));
700
0
  for (size_t y = 0; y < weights.ysize(); y++) {
701
0
    auto* row = weights.Row(y);
702
0
    for (size_t x = 0; x < weights.xsize(); x++) {
703
0
      row[x] = 1;
704
0
    }
705
0
  }
706
0
  JXL_ASSIGN_OR_RETURN(ImageF weights2,
707
0
                       ImageF::Create(memory_manager, xsize2, ysize2));
708
0
  AntiUpsample(weights, &weights2);
709
710
0
  const size_t num_it = 3;
711
0
  for (size_t it = 0; it < num_it; ++it) {
712
0
    UpsampleImage(down, &up);
713
0
    JXL_ASSIGN_OR_RETURN(corr, LinComb<float>(1, orig, -1, up));
714
0
    JXL_RETURN_IF_ERROR(ElwiseMul(corr, weights, &corr));
715
0
    AntiUpsample(corr, &corr2);
716
0
    JXL_RETURN_IF_ERROR(ElwiseDiv(corr2, weights2, &corr2));
717
718
0
    JXL_ASSIGN_OR_RETURN(down, LinComb<float>(1, down, 1, corr2));
719
0
  }
720
721
0
  ReduceRinging(initial, mask, down);
722
723
  // can't just use CopyImage, because the output image was prepared with
724
  // padding.
725
0
  for (size_t y = 0; y < down.ysize(); y++) {
726
0
    for (size_t x = 0; x < down.xsize(); x++) {
727
0
      float v = down.Row(y)[x];
728
0
      output->Row(y)[x] = v;
729
0
    }
730
0
  }
731
0
  return true;
732
0
}
733
734
}  // namespace
735
736
0
Status DownsampleImage2_Iterative(Image3F* opsin) {
737
0
  JxlMemoryManager* memory_manager = opsin->memory_manager();
738
  // Allocate extra space to avoid a reallocation when padding.
739
0
  JXL_ASSIGN_OR_RETURN(
740
0
      Image3F downsampled,
741
0
      Image3F::Create(memory_manager, DivCeil(opsin->xsize(), 2) + kBlockDim,
742
0
                      DivCeil(opsin->ysize(), 2) + kBlockDim));
743
0
  JXL_RETURN_IF_ERROR(downsampled.ShrinkTo(downsampled.xsize() - kBlockDim,
744
0
                                           downsampled.ysize() - kBlockDim));
745
746
0
  JXL_ASSIGN_OR_RETURN(
747
0
      Image3F rgb,
748
0
      Image3F::Create(memory_manager, opsin->xsize(), opsin->ysize()));
749
0
  OpsinParams opsin_params;  // TODO(user): use the ones that are actually used
750
0
  opsin_params.Init(kDefaultIntensityTarget);
751
0
  JXL_RETURN_IF_ERROR(
752
0
      OpsinToLinear(*opsin, Rect(rgb), nullptr, &rgb, opsin_params));
753
754
0
  JXL_ASSIGN_OR_RETURN(
755
0
      ImageF mask,
756
0
      ImageF::Create(memory_manager, opsin->xsize(), opsin->ysize()));
757
0
  ButteraugliParams butter_params;
758
0
  JXL_ASSIGN_OR_RETURN(std::unique_ptr<ButteraugliComparator> butter,
759
0
                       ButteraugliComparator::Make(rgb, butter_params));
760
0
  JXL_RETURN_IF_ERROR(butter->Mask(&mask));
761
0
  JXL_ASSIGN_OR_RETURN(
762
0
      ImageF mask_fuzzy,
763
0
      ImageF::Create(memory_manager, opsin->xsize(), opsin->ysize()));
764
765
0
  for (size_t c = 0; c < 3; c++) {
766
0
    JXL_RETURN_IF_ERROR(
767
0
        DownsampleImage2_Iterative(opsin->Plane(c), &downsampled.Plane(c)));
768
0
  }
769
0
  *opsin = std::move(downsampled);
770
0
  return true;
771
0
}
772
773
StatusOr<Image3F> ReconstructImage(
774
    const FrameHeader& orig_frame_header, const PassesSharedState& shared,
775
0
    const std::vector<std::unique_ptr<ACImage>>& coeffs, ThreadPool* pool) {
776
0
  const FrameDimensions& frame_dim = shared.frame_dim;
777
0
  JxlMemoryManager* memory_manager = shared.memory_manager;
778
779
0
  FrameHeader frame_header = orig_frame_header;
780
0
  frame_header.UpdateFlag(shared.image_features.patches.HasAny(),
781
0
                          FrameHeader::kPatches);
782
0
  frame_header.UpdateFlag(shared.image_features.splines.HasAny(),
783
0
                          FrameHeader::kSplines);
784
0
  frame_header.color_transform = ColorTransform::kNone;
785
786
0
  CodecMetadata metadata = *frame_header.nonserialized_metadata;
787
0
  metadata.m.extra_channel_info.clear();
788
0
  metadata.m.num_extra_channels = metadata.m.extra_channel_info.size();
789
0
  frame_header.nonserialized_metadata = &metadata;
790
0
  frame_header.extra_channel_upsampling.clear();
791
792
0
  const bool is_gray = shared.metadata->m.color_encoding.IsGray();
793
0
  PassesDecoderState dec_state(memory_manager);
794
0
  JXL_RETURN_IF_ERROR(
795
0
      dec_state.output_encoding_info.SetFromMetadata(*shared.metadata));
796
0
  JXL_RETURN_IF_ERROR(dec_state.output_encoding_info.MaybeSetColorEncoding(
797
0
      ColorEncoding::LinearSRGB(is_gray)));
798
0
  dec_state.shared = &shared;
799
0
  JXL_RETURN_IF_ERROR(dec_state.Init(frame_header));
800
801
0
  ImageBundle decoded(memory_manager, &shared.metadata->m);
802
0
  decoded.origin = frame_header.frame_origin;
803
0
  JXL_ASSIGN_OR_RETURN(
804
0
      Image3F tmp,
805
0
      Image3F::Create(memory_manager, frame_dim.xsize, frame_dim.ysize));
806
0
  JXL_RETURN_IF_ERROR(decoded.SetFromImage(
807
0
      std::move(tmp), dec_state.output_encoding_info.color_encoding));
808
809
0
  PassesDecoderState::PipelineOptions options;
810
0
  options.use_slow_render_pipeline = false;
811
0
  options.coalescing = false;
812
0
  options.render_spotcolors = false;
813
0
  options.render_noise = true;
814
815
0
  JXL_RETURN_IF_ERROR(dec_state.PreparePipeline(
816
0
      frame_header, &shared.metadata->m, &decoded, options));
817
818
0
  AlignedArray<GroupDecCache> group_dec_caches;
819
0
  const auto allocate_storage = [&](const size_t num_threads) -> Status {
820
0
    JXL_RETURN_IF_ERROR(
821
0
        dec_state.render_pipeline->PrepareForThreads(num_threads,
822
0
                                                     /*use_group_ids=*/false));
823
0
    JXL_ASSIGN_OR_RETURN(group_dec_caches, AlignedArray<GroupDecCache>::Create(
824
0
                                               memory_manager, num_threads));
825
0
    return true;
826
0
  };
827
0
  const auto process_group = [&](const uint32_t group_index,
828
0
                                 const size_t thread) -> Status {
829
0
    if (frame_header.loop_filter.epf_iters > 0) {
830
0
      JXL_RETURN_IF_ERROR(ComputeSigma(frame_header.loop_filter,
831
0
                                       frame_dim.BlockGroupRect(group_index),
832
0
                                       &dec_state));
833
0
    }
834
0
    RenderPipelineInput input =
835
0
        dec_state.render_pipeline->GetInputBuffers(group_index, thread);
836
0
    JXL_RETURN_IF_ERROR(DecodeGroupForRoundtrip(
837
0
        frame_header, coeffs, group_index, &dec_state,
838
0
        &group_dec_caches[thread], thread, input, nullptr, nullptr));
839
0
    if ((frame_header.flags & FrameHeader::kNoise) != 0) {
840
0
      PrepareNoiseInput(dec_state, shared.frame_dim, frame_header, group_index,
841
0
                        thread);
842
0
    }
843
0
    JXL_RETURN_IF_ERROR(input.Done());
844
0
    return true;
845
0
  };
846
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, frame_dim.num_groups, allocate_storage,
847
0
                                process_group, "ReconstructImage"));
848
0
  return std::move(*decoded.color());
849
0
}
850
851
float ComputeBlockL2Distance(const Image3F& a, const Image3F& b,
852
0
                             const ImageF& mask1x1, size_t by, size_t bx) {
853
0
  Rect rect(bx * kBlockDim, by * kBlockDim, kBlockDim, kBlockDim, a.xsize(),
854
0
            a.ysize());
855
0
  float err2[3] = {0.0f};
856
0
  for (size_t y = 0; y < rect.ysize(); ++y) {
857
0
    const float* row_a[3] = {
858
0
        rect.ConstPlaneRow(a, 0, y),
859
0
        rect.ConstPlaneRow(a, 1, y),
860
0
        rect.ConstPlaneRow(a, 2, y),
861
0
    };
862
0
    const float* row_b[3] = {
863
0
        rect.ConstPlaneRow(b, 0, y),
864
0
        rect.ConstPlaneRow(b, 1, y),
865
0
        rect.ConstPlaneRow(b, 2, y),
866
0
    };
867
0
    const float* row_mask = rect.ConstRow(mask1x1, y);
868
0
    for (size_t x = 0; x < rect.xsize(); ++x) {
869
0
      float mask = row_mask[x];
870
0
      float mask2 = mask * mask;
871
0
      for (int i = 0; i < 3; ++i) {
872
0
        float diff = row_a[i][x] - row_b[i][x];
873
0
        err2[i] += mask2 * diff * diff;
874
0
      }
875
0
    }
876
0
  }
877
0
  static const double kW[] = {
878
0
      12.339445295782363,
879
0
      1.0,
880
0
      0.2,
881
0
  };
882
0
  float retval = kW[0] * err2[0] + kW[1] * err2[1] + kW[2] * err2[2];
883
0
  return retval;
884
0
}
885
886
Status ComputeARHeuristics(const FrameHeader& frame_header,
887
                           PassesEncoderState* enc_state,
888
                           const Image3F& orig_opsin, const Rect& rect,
889
0
                           ThreadPool* pool) {
890
0
  const CompressParams& cparams = enc_state->cparams;
891
0
  PassesSharedState& shared = enc_state->shared;
892
0
  const FrameDimensions& frame_dim = shared.frame_dim;
893
0
  const ImageF& initial_quant_masking1x1 = enc_state->initial_quant_masking1x1;
894
0
  ImageB& epf_sharpness = shared.epf_sharpness;
895
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
896
897
0
  float clamped_butteraugli = std::min(5.0f, cparams.butteraugli_distance);
898
0
  if (cparams.butteraugli_distance < kMinButteraugliForDynamicAR ||
899
0
      cparams.speed_tier > SpeedTier::kWombat ||
900
0
      frame_header.loop_filter.epf_iters == 0) {
901
0
    FillPlane(static_cast<uint8_t>(4), &epf_sharpness, Rect(epf_sharpness));
902
0
    return true;
903
0
  }
904
905
0
  std::vector<uint8_t> epf_steps;
906
0
  if (cparams.butteraugli_distance > 4.5f) {
907
0
    epf_steps.push_back(0);
908
0
    epf_steps.push_back(4);
909
0
  } else {
910
0
    epf_steps.push_back(0);
911
0
    epf_steps.push_back(2);
912
0
    epf_steps.push_back(7);
913
0
  }
914
0
  static const int kNumEPFVals = 8;
915
0
  size_t epf_steps_lut[kNumEPFVals] = {0};
916
0
  {
917
0
    for (size_t i = 0; i < epf_steps.size(); ++i) {
918
0
      epf_steps_lut[epf_steps[i]] = i;
919
0
    }
920
0
  }
921
0
  std::array<ImageF, kNumEPFVals> error_images;
922
0
  for (uint8_t val : epf_steps) {
923
0
    FillPlane(val, &epf_sharpness, Rect(epf_sharpness));
924
0
    JXL_ASSIGN_OR_RETURN(
925
0
        Image3F decoded,
926
0
        ReconstructImage(frame_header, shared, enc_state->coeffs, pool));
927
0
    JXL_ASSIGN_OR_RETURN(error_images[val],
928
0
                         ImageF::Create(memory_manager, frame_dim.xsize_blocks,
929
0
                                        frame_dim.ysize_blocks));
930
0
    for (size_t by = 0; by < frame_dim.ysize_blocks; by++) {
931
0
      float* error_row = error_images[val].Row(by);
932
0
      for (size_t bx = 0; bx < frame_dim.xsize_blocks; bx++) {
933
0
        error_row[bx] = ComputeBlockL2Distance(
934
0
            orig_opsin, decoded, initial_quant_masking1x1, by, bx);
935
0
      }
936
0
    }
937
0
  }
938
0
  std::vector<std::vector<size_t>> histo(9, std::vector<size_t>(kNumEPFVals));
939
0
  std::vector<size_t> totals(9, 1);
940
0
  const float c5 = 0.007620386618483585f;
941
0
  const float c6 = 0.0083224805679680686f;
942
0
  const float c7 = 0.99663939685686753;
943
0
  for (size_t by = 0; by < frame_dim.ysize_blocks; by++) {
944
0
    uint8_t* JXL_RESTRICT out_row = epf_sharpness.Row(by);
945
0
    uint8_t* JXL_RESTRICT prev_row = epf_sharpness.Row(by > 0 ? by - 1 : 0);
946
0
    for (size_t bx = 0; bx < frame_dim.xsize_blocks; bx++) {
947
0
      uint8_t best_val = 0;
948
0
      float best_error = std::numeric_limits<float>::max();
949
0
      uint8_t top_val = by > 0 ? prev_row[bx] : 0;
950
0
      uint8_t left_val = bx > 0 ? out_row[bx - 1] : 0;
951
0
      float top_error = error_images[top_val].Row(by)[bx];
952
0
      float left_error = error_images[left_val].Row(by)[bx];
953
0
      for (uint8_t val : epf_steps) {
954
0
        float error = error_images[val].Row(by)[bx];
955
0
        if (val == 0) {
956
0
          error *= c7 - c5 * clamped_butteraugli;
957
0
        }
958
0
        if (error < best_error) {
959
0
          best_val = val;
960
0
          best_error = error;
961
0
        }
962
0
      }
963
0
      if (best_error <
964
0
          (1.0 - c6 * clamped_butteraugli) * std::min(top_error, left_error)) {
965
0
        out_row[bx] = best_val;
966
0
      } else if (top_error < left_error) {
967
0
        out_row[bx] = top_val;
968
0
      } else {
969
0
        out_row[bx] = left_val;
970
0
      }
971
0
      int context = epf_steps_lut[top_val] * 3 + epf_steps_lut[left_val];
972
0
      ++histo[context][out_row[bx]];
973
0
      ++totals[context];
974
0
    }
975
0
  }
976
0
  const float c1 = 0.059588212153340203f;
977
0
  const float c2 = 0.10599497107315753f;
978
0
  const float c3base = 0.97;
979
0
  const float c3 = pow(c3base, clamped_butteraugli);
980
0
  const float c4 = 1.247544678665836f;
981
0
  const float context_weight = c1 + c2 * clamped_butteraugli;
982
0
  for (size_t by = 0; by < frame_dim.ysize_blocks; by++) {
983
0
    uint8_t* JXL_RESTRICT out_row = epf_sharpness.Row(by);
984
0
    uint8_t* JXL_RESTRICT prev_row = epf_sharpness.Row(by > 0 ? by - 1 : 0);
985
0
    for (size_t bx = 0; bx < frame_dim.xsize_blocks; bx++) {
986
0
      uint8_t best_val = 0;
987
0
      float best_error = std::numeric_limits<float>::max();
988
0
      uint8_t top_val = by > 0 ? prev_row[bx] : 0;
989
0
      uint8_t left_val = bx > 0 ? out_row[bx - 1] : 0;
990
0
      int context = epf_steps_lut[top_val] * 3 + epf_steps_lut[left_val];
991
0
      const auto& ctx_histo = histo[context];
992
0
      for (uint8_t val : epf_steps) {
993
0
        float error = error_images[val].Row(by)[bx] /
994
0
                      (c4 + std::log1p(ctx_histo[val] * context_weight /
995
0
                                       totals[context]));
996
0
        if (val == 0) {
997
0
          error *= c3;
998
0
        }
999
0
        if (error < best_error) {
1000
0
          best_val = val;
1001
0
          best_error = error;
1002
0
        }
1003
0
      }
1004
0
      out_row[bx] = best_val;
1005
0
    }
1006
0
  }
1007
1008
0
  return true;
1009
0
}
1010
1011
Status LossyFrameHeuristics(const FrameHeader& frame_header,
1012
                            PassesEncoderState* enc_state,
1013
                            ModularFrameEncoder* modular_frame_encoder,
1014
                            const Image3F* linear, Image3F* opsin,
1015
                            const Rect& rect, const JxlCmsInterface& cms,
1016
0
                            ThreadPool* pool, AuxOut* aux_out) {
1017
0
  const CompressParams& cparams = enc_state->cparams;
1018
0
  const bool streaming_mode = enc_state->streaming_mode;
1019
0
  const bool initialize_global_state = enc_state->initialize_global_state;
1020
0
  PassesSharedState& shared = enc_state->shared;
1021
0
  const FrameDimensions& frame_dim = shared.frame_dim;
1022
0
  ImageFeatures& image_features = shared.image_features;
1023
0
  DequantMatrices& matrices = shared.matrices;
1024
0
  Quantizer& quantizer = shared.quantizer;
1025
0
  ImageF& initial_quant_masking1x1 = enc_state->initial_quant_masking1x1;
1026
0
  ImageI& raw_quant_field = shared.raw_quant_field;
1027
0
  ColorCorrelationMap& cmap = shared.cmap;
1028
0
  AcStrategyImage& ac_strategy = shared.ac_strategy;
1029
0
  BlockCtxMap& block_ctx_map = shared.block_ctx_map;
1030
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
1031
1032
  // Find and subtract splines.
1033
0
  if (cparams.custom_splines.HasAny()) {
1034
0
    image_features.splines = cparams.custom_splines;
1035
0
  }
1036
0
  if (!streaming_mode && cparams.speed_tier <= SpeedTier::kSquirrel) {
1037
0
    if (!cparams.custom_splines.HasAny()) {
1038
0
      image_features.splines = FindSplines(*opsin);
1039
0
    }
1040
0
    JXL_RETURN_IF_ERROR(image_features.splines.InitializeDrawCache(
1041
0
        opsin->xsize(), opsin->ysize(), cmap.base()));
1042
0
    image_features.splines.SubtractFrom(opsin);
1043
0
  }
1044
1045
  // Find and subtract patches/dots.
1046
0
  if (!streaming_mode &&
1047
0
      ApplyOverride(cparams.patches,
1048
0
                    cparams.speed_tier <= SpeedTier::kSquirrel)) {
1049
0
    JXL_RETURN_IF_ERROR(
1050
0
        FindBestPatchDictionary(*opsin, enc_state, cms, pool, aux_out));
1051
0
    JXL_RETURN_IF_ERROR(
1052
0
        PatchDictionaryEncoder::SubtractFrom(image_features.patches, opsin));
1053
0
  }
1054
1055
0
  const float quant_dc = InitialQuantDC(cparams.butteraugli_distance);
1056
1057
  // TODO(veluca): we can now run all the code from here to FindBestQuantizer
1058
  // (excluded) one rect at a time. Do that.
1059
1060
  // Dependency graph:
1061
  //
1062
  // input: either XYB or input image
1063
  //
1064
  // input image -> XYB [optional]
1065
  // XYB -> initial quant field
1066
  // XYB -> Gaborished XYB
1067
  // Gaborished XYB -> CfL1
1068
  // initial quant field, Gaborished XYB, CfL1 -> ACS
1069
  // initial quant field, ACS, Gaborished XYB -> EPF control field
1070
  // initial quant field -> adjusted initial quant field
1071
  // adjusted initial quant field, ACS -> raw quant field
1072
  // raw quant field, ACS, Gaborished XYB -> CfL2
1073
  //
1074
  // output: Gaborished XYB, CfL, ACS, raw quant field, EPF control field.
1075
1076
0
  AcStrategyHeuristics acs_heuristics(memory_manager, cparams);
1077
0
  CfLHeuristics cfl_heuristics(memory_manager);
1078
0
  ImageF initial_quant_field;
1079
0
  ImageF initial_quant_masking;
1080
1081
  // Compute an initial estimate of the quantization field.
1082
  // Call InitialQuantField only in Hare mode or slower. Otherwise, rely
1083
  // on simple heuristics in FindBestAcStrategy, or set a constant for Falcon
1084
  // mode.
1085
0
  if (cparams.speed_tier > SpeedTier::kHare ||
1086
0
      cparams.disable_perceptual_optimizations) {
1087
0
    JXL_ASSIGN_OR_RETURN(initial_quant_field,
1088
0
                         ImageF::Create(memory_manager, frame_dim.xsize_blocks,
1089
0
                                        frame_dim.ysize_blocks));
1090
0
    JXL_ASSIGN_OR_RETURN(initial_quant_masking,
1091
0
                         ImageF::Create(memory_manager, frame_dim.xsize_blocks,
1092
0
                                        frame_dim.ysize_blocks));
1093
0
    float q = 0.79 / cparams.butteraugli_distance;
1094
0
    FillImage(q, &initial_quant_field);
1095
0
    float masking = 1.0f / (q + 0.001f);
1096
0
    FillImage(masking, &initial_quant_masking);
1097
0
    if (cparams.disable_perceptual_optimizations) {
1098
0
      JXL_ASSIGN_OR_RETURN(
1099
0
          initial_quant_masking1x1,
1100
0
          ImageF::Create(memory_manager, frame_dim.xsize, frame_dim.ysize));
1101
0
      FillImage(masking, &initial_quant_masking1x1);
1102
0
    }
1103
0
    quantizer.ComputeGlobalScaleAndQuant(quant_dc, q, 0);
1104
0
  } else {
1105
    // Call this here, as it relies on pre-gaborish values.
1106
0
    float butteraugli_distance_for_iqf = cparams.butteraugli_distance;
1107
0
    if (!frame_header.loop_filter.gab) {
1108
0
      butteraugli_distance_for_iqf *= 0.62f;
1109
0
    }
1110
0
    JXL_ASSIGN_OR_RETURN(
1111
0
        initial_quant_field,
1112
0
        InitialQuantField(butteraugli_distance_for_iqf, *opsin, rect, pool,
1113
0
                          1.0f, &initial_quant_masking,
1114
0
                          &initial_quant_masking1x1));
1115
0
    float q = 0.39 / cparams.butteraugli_distance;
1116
0
    quantizer.ComputeGlobalScaleAndQuant(quant_dc, q, 0);
1117
0
  }
1118
1119
  // TODO(veluca): do something about animations.
1120
1121
  // Apply inverse-gaborish.
1122
0
  if (frame_header.loop_filter.gab) {
1123
    // Changing the weight here to 0.99f would help to reduce ringing in
1124
    // generation loss.
1125
0
    float weight[3] = {
1126
0
        1.0f,
1127
0
        1.0f,
1128
0
        1.0f,
1129
0
    };
1130
0
    JXL_RETURN_IF_ERROR(GaborishInverse(opsin, rect, weight, pool));
1131
0
  }
1132
1133
0
  if (initialize_global_state) {
1134
0
    JXL_RETURN_IF_ERROR(FindBestDequantMatrices(
1135
0
        memory_manager, cparams, modular_frame_encoder, &matrices));
1136
0
  }
1137
1138
0
  JXL_RETURN_IF_ERROR(cfl_heuristics.Init(rect));
1139
0
  JXL_RETURN_IF_ERROR(acs_heuristics.Init(*opsin, rect, initial_quant_field,
1140
0
                                          initial_quant_masking,
1141
0
                                          initial_quant_masking1x1, &matrices));
1142
1143
0
  auto process_tile = [&](const uint32_t tid, const size_t thread) -> Status {
1144
0
    size_t n_enc_tiles = DivCeil(frame_dim.xsize_blocks, kEncTileDimInBlocks);
1145
0
    size_t tx = tid % n_enc_tiles;
1146
0
    size_t ty = tid / n_enc_tiles;
1147
0
    size_t by0 = ty * kEncTileDimInBlocks;
1148
0
    size_t by1 =
1149
0
        std::min((ty + 1) * kEncTileDimInBlocks, frame_dim.ysize_blocks);
1150
0
    size_t bx0 = tx * kEncTileDimInBlocks;
1151
0
    size_t bx1 =
1152
0
        std::min((tx + 1) * kEncTileDimInBlocks, frame_dim.xsize_blocks);
1153
0
    Rect r(bx0, by0, bx1 - bx0, by1 - by0);
1154
1155
    // For speeds up to Wombat, we only compute the color correlation map
1156
    // once we know the transform type and the quantization map.
1157
0
    if (cparams.speed_tier <= SpeedTier::kSquirrel) {
1158
0
      JXL_RETURN_IF_ERROR(cfl_heuristics.ComputeTile(
1159
0
          r, *opsin, rect, matrices,
1160
0
          /*ac_strategy=*/nullptr,
1161
0
          /*raw_quant_field=*/nullptr,
1162
0
          /*quantizer=*/nullptr, /*fast=*/false, thread, &cmap));
1163
0
    }
1164
1165
    // Choose block sizes.
1166
0
    JXL_RETURN_IF_ERROR(
1167
0
        acs_heuristics.ProcessRect(r, cmap, &ac_strategy, thread));
1168
1169
    // Always set the initial quant field, so we can compute the CfL map with
1170
    // more accuracy. The initial quant field might change in slower modes, but
1171
    // adjusting the quant field with butteraugli when all the other encoding
1172
    // parameters are fixed is likely a more reliable choice anyway.
1173
0
    JXL_RETURN_IF_ERROR(AdjustQuantField(
1174
0
        ac_strategy, r, cparams.butteraugli_distance, &initial_quant_field));
1175
0
    quantizer.SetQuantFieldRect(initial_quant_field, r, &raw_quant_field);
1176
1177
    // Compute a non-default CfL map if we are at Hare speed, or slower.
1178
0
    if (cparams.speed_tier <= SpeedTier::kHare) {
1179
0
      JXL_RETURN_IF_ERROR(cfl_heuristics.ComputeTile(
1180
0
          r, *opsin, rect, matrices, &ac_strategy, &raw_quant_field, &quantizer,
1181
0
          /*fast=*/cparams.speed_tier >= SpeedTier::kWombat, thread, &cmap));
1182
0
    }
1183
0
    return true;
1184
0
  };
1185
0
  size_t num_tiles = DivCeil(frame_dim.xsize_blocks, kEncTileDimInBlocks) *
1186
0
                     DivCeil(frame_dim.ysize_blocks, kEncTileDimInBlocks);
1187
0
  const auto prepare = [&](const size_t num_threads) -> Status {
1188
0
    JXL_RETURN_IF_ERROR(acs_heuristics.PrepareForThreads(num_threads));
1189
0
    JXL_RETURN_IF_ERROR(cfl_heuristics.PrepareForThreads(num_threads));
1190
0
    return true;
1191
0
  };
1192
0
  JXL_RETURN_IF_ERROR(
1193
0
      RunOnPool(pool, 0, num_tiles, prepare, process_tile, "Enc Heuristics"));
1194
1195
0
  JXL_RETURN_IF_ERROR(acs_heuristics.Finalize(frame_dim, ac_strategy, aux_out));
1196
1197
  // Refine quantization levels.
1198
0
  if (!streaming_mode && !cparams.disable_perceptual_optimizations) {
1199
0
    ImageB& epf_sharpness = shared.epf_sharpness;
1200
0
    FillPlane(static_cast<uint8_t>(4), &epf_sharpness, Rect(epf_sharpness));
1201
0
    JXL_RETURN_IF_ERROR(FindBestQuantizer(frame_header, linear, *opsin,
1202
0
                                          initial_quant_field, enc_state, cms,
1203
0
                                          pool, aux_out));
1204
0
  }
1205
1206
  // Choose a context model that depends on the amount of quantization for AC.
1207
0
  if (cparams.speed_tier < SpeedTier::kFalcon && initialize_global_state) {
1208
0
    FindBestBlockEntropyModel(cparams, raw_quant_field, ac_strategy,
1209
0
                              &block_ctx_map);
1210
0
  }
1211
0
  return true;
1212
0
}
1213
1214
}  // namespace jxl