Coverage Report

Created: 2025-06-22 08:04

/src/libjxl/lib/jxl/enc_modular.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_modular.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <array>
11
#include <cstddef>
12
#include <cstdint>
13
#include <limits>
14
#include <utility>
15
#include <vector>
16
17
#include "lib/jxl/base/compiler_specific.h"
18
#include "lib/jxl/base/printf_macros.h"
19
#include "lib/jxl/base/rect.h"
20
#include "lib/jxl/base/status.h"
21
#include "lib/jxl/chroma_from_luma.h"
22
#include "lib/jxl/compressed_dc.h"
23
#include "lib/jxl/dec_ans.h"
24
#include "lib/jxl/dec_modular.h"
25
#include "lib/jxl/enc_aux_out.h"
26
#include "lib/jxl/enc_bit_writer.h"
27
#include "lib/jxl/enc_cluster.h"
28
#include "lib/jxl/enc_fields.h"
29
#include "lib/jxl/enc_gaborish.h"
30
#include "lib/jxl/enc_params.h"
31
#include "lib/jxl/enc_patch_dictionary.h"
32
#include "lib/jxl/enc_quant_weights.h"
33
#include "lib/jxl/frame_dimensions.h"
34
#include "lib/jxl/frame_header.h"
35
#include "lib/jxl/modular/encoding/context_predict.h"
36
#include "lib/jxl/modular/encoding/enc_encoding.h"
37
#include "lib/jxl/modular/encoding/encoding.h"
38
#include "lib/jxl/modular/encoding/ma_common.h"
39
#include "lib/jxl/modular/modular_image.h"
40
#include "lib/jxl/modular/options.h"
41
#include "lib/jxl/modular/transform/enc_transform.h"
42
#include "lib/jxl/pack_signed.h"
43
#include "lib/jxl/quant_weights.h"
44
#include "modular/options.h"
45
46
namespace jxl {
47
48
namespace {
49
// constexpr bool kPrintTree = false;
50
51
// Squeeze default quantization factors
52
// these quantization factors are for -Q 50  (other qualities simply scale the
53
// factors; things are rounded down and obviously cannot get below 1)
54
const float squeeze_quality_factor =
55
    0.35;  // for easy tweaking of the quality range (decrease this number for
56
           // higher quality)
57
const float squeeze_luma_factor =
58
    1.1;  // for easy tweaking of the balance between luma (or anything
59
          // non-chroma) and chroma (decrease this number for higher quality
60
          // luma)
61
const float squeeze_quality_factor_xyb = 4.8f;
62
const float squeeze_xyb_qtable[3][16] = {
63
    {163.84, 81.92, 40.96, 20.48, 10.24, 5.12, 2.56, 1.28, 0.64, 0.32, 0.16,
64
     0.08, 0.04, 0.02, 0.01, 0.005},  // Y
65
    {1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.5, 0.5, 0.5, 0.5,
66
     0.5},  // X
67
    {2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.5, 0.5, 0.5,
68
     0.5},  // B-Y
69
};
70
71
const float squeeze_luma_qtable[16] = {163.84, 81.92, 40.96, 20.48, 10.24, 5.12,
72
                                       2.56,   1.28,  0.64,  0.32,  0.16,  0.08,
73
                                       0.04,   0.02,  0.01,  0.005};
74
// for 8-bit input, the range of YCoCg chroma is -255..255 so basically this
75
// does 4:2:0 subsampling (two most fine grained layers get quantized away)
76
const float squeeze_chroma_qtable[16] = {
77
    1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.5, 0.5, 0.5, 0.5, 0.5};
78
79
// Merges the trees in `trees` using nodes that decide on stream_id, as defined
80
// by `tree_splits`.
81
Status MergeTrees(const std::vector<Tree>& trees,
82
                  const std::vector<size_t>& tree_splits, size_t begin,
83
0
                  size_t end, Tree* tree) {
84
0
  JXL_ENSURE(trees.size() + 1 == tree_splits.size());
85
0
  JXL_ENSURE(end > begin);
86
0
  JXL_ENSURE(end <= trees.size());
87
0
  if (end == begin + 1) {
88
    // Insert the tree, adding the opportune offset to all child nodes.
89
    // This will make the leaf IDs wrong, but subsequent roundtripping will fix
90
    // them.
91
0
    size_t sz = tree->size();
92
0
    tree->insert(tree->end(), trees[begin].begin(), trees[begin].end());
93
0
    for (size_t i = sz; i < tree->size(); i++) {
94
0
      (*tree)[i].lchild += sz;
95
0
      (*tree)[i].rchild += sz;
96
0
    }
97
0
    return true;
98
0
  }
99
0
  size_t mid = (begin + end) / 2;
100
0
  size_t splitval = tree_splits[mid] - 1;
101
0
  size_t cur = tree->size();
102
0
  tree->emplace_back(1 /*stream_id*/, splitval, 0, 0, Predictor::Zero, 0, 1);
103
0
  (*tree)[cur].lchild = tree->size();
104
0
  JXL_RETURN_IF_ERROR(MergeTrees(trees, tree_splits, mid, end, tree));
105
0
  (*tree)[cur].rchild = tree->size();
106
0
  JXL_RETURN_IF_ERROR(MergeTrees(trees, tree_splits, begin, mid, tree));
107
0
  return true;
108
0
}
109
110
0
void QuantizeChannel(Channel& ch, const int q) {
111
0
  if (q == 1) return;
112
0
  for (size_t y = 0; y < ch.plane.ysize(); y++) {
113
0
    pixel_type* row = ch.plane.Row(y);
114
0
    for (size_t x = 0; x < ch.plane.xsize(); x++) {
115
0
      if (row[x] < 0) {
116
0
        row[x] = -((-row[x] + q / 2) / q) * q;
117
0
      } else {
118
0
        row[x] = ((row[x] + q / 2) / q) * q;
119
0
      }
120
0
    }
121
0
  }
122
0
}
123
124
// convert binary32 float that corresponds to custom [bits]-bit float (with
125
// [exp_bits] exponent bits) to a [bits]-bit integer representation that should
126
// fit in pixel_type
127
Status float_to_int(const float* const row_in, pixel_type* const row_out,
128
                    size_t xsize, unsigned int bits, unsigned int exp_bits,
129
0
                    bool fp, double dfactor) {
130
0
  JXL_ENSURE(sizeof(pixel_type) * 8 >= bits);
131
0
  if (!fp) {
132
0
    if (bits > 22) {
133
0
      for (size_t x = 0; x < xsize; ++x) {
134
0
        row_out[x] = row_in[x] * dfactor + (row_in[x] < 0 ? -0.5 : 0.5);
135
0
      }
136
0
    } else {
137
0
      float factor = dfactor;
138
0
      for (size_t x = 0; x < xsize; ++x) {
139
0
        row_out[x] = row_in[x] * factor + (row_in[x] < 0 ? -0.5f : 0.5f);
140
0
      }
141
0
    }
142
0
    return true;
143
0
  }
144
0
  if (bits == 32 && fp) {
145
0
    JXL_ENSURE(exp_bits == 8);
146
0
    memcpy(static_cast<void*>(row_out), static_cast<const void*>(row_in),
147
0
           4 * xsize);
148
0
    return true;
149
0
  }
150
151
0
  JXL_ENSURE(bits > 0);
152
0
  int exp_bias = (1 << (exp_bits - 1)) - 1;
153
0
  int max_exp = (1 << exp_bits) - 1;
154
0
  uint32_t sign = (1u << (bits - 1));
155
0
  int mant_bits = bits - exp_bits - 1;
156
0
  int mant_shift = 23 - mant_bits;
157
0
  for (size_t x = 0; x < xsize; ++x) {
158
0
    uint32_t f;
159
0
    memcpy(&f, &row_in[x], 4);
160
0
    int signbit = (f >> 31);
161
0
    f &= 0x7fffffff;
162
0
    if (f == 0) {
163
0
      row_out[x] = (signbit ? sign : 0);
164
0
      continue;
165
0
    }
166
0
    int exp = (f >> 23) - 127;
167
0
    if (exp == 128) return JXL_FAILURE("Inf/NaN not allowed");
168
0
    int mantissa = (f & 0x007fffff);
169
    // broke up the binary32 into its parts, now reassemble into
170
    // arbitrary float
171
0
    exp += exp_bias;
172
0
    if (exp < 0) {  // will become a subnormal number
173
      // add implicit leading 1 to mantissa
174
0
      mantissa |= 0x00800000;
175
0
      if (exp < -mant_bits) {
176
0
        return JXL_FAILURE(
177
0
            "Invalid float number: %g cannot be represented with %i "
178
0
            "exp_bits and %i mant_bits (exp %i)",
179
0
            row_in[x], exp_bits, mant_bits, exp);
180
0
      }
181
0
      mantissa >>= 1 - exp;
182
0
      exp = 0;
183
0
    }
184
    // exp should be representable in exp_bits, otherwise input was
185
    // invalid
186
0
    if (exp > max_exp) return JXL_FAILURE("Invalid float exponent");
187
0
    if (mantissa & ((1 << mant_shift) - 1)) {
188
0
      return JXL_FAILURE("%g is losing precision (mant: %x)", row_in[x],
189
0
                         mantissa);
190
0
    }
191
0
    mantissa >>= mant_shift;
192
0
    f = (signbit ? sign : 0);
193
0
    f |= (exp << mant_bits);
194
0
    f |= mantissa;
195
0
    row_out[x] = static_cast<pixel_type>(f);
196
0
  }
197
0
  return true;
198
0
}
199
200
0
float EstimateWPCost(const Image& img, size_t i) {
201
0
  size_t extra_bits = 0;
202
0
  float histo_cost = 0;
203
0
  HybridUintConfig config;
204
0
  int32_t cutoffs[] = {-500, -392, -255, -191, -127, -95, -63, -47, -31,
205
0
                       -23,  -15,  -11,  -7,   -4,   -3,  -1,  0,   1,
206
0
                       3,    5,    7,    11,   15,   23,  31,  47,  63,
207
0
                       95,   127,  191,  255,  392,  500};
208
0
  constexpr size_t nc = sizeof(cutoffs) / sizeof(*cutoffs) + 1;
209
0
  Histogram histo[nc] = {};
210
0
  weighted::Header wp_header;
211
0
  PredictorMode(i, &wp_header);
212
0
  for (const Channel& ch : img.channel) {
213
0
    const intptr_t onerow = ch.plane.PixelsPerRow();
214
0
    weighted::State wp_state(wp_header, ch.w, ch.h);
215
0
    Properties properties(1);
216
0
    for (size_t y = 0; y < ch.h; y++) {
217
0
      const pixel_type* JXL_RESTRICT r = ch.Row(y);
218
0
      for (size_t x = 0; x < ch.w; x++) {
219
0
        size_t offset = 0;
220
0
        pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0);
221
0
        pixel_type_w top = (y ? *(r + x - onerow) : left);
222
0
        pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left);
223
0
        pixel_type_w topright =
224
0
            (x + 1 < ch.w && y ? *(r + x + 1 - onerow) : top);
225
0
        pixel_type_w toptop = (y > 1 ? *(r + x - onerow - onerow) : top);
226
0
        pixel_type guess = wp_state.Predict</*compute_properties=*/true>(
227
0
            x, y, ch.w, top, left, topright, topleft, toptop, &properties,
228
0
            offset);
229
0
        size_t ctx = 0;
230
0
        for (int c : cutoffs) {
231
0
          ctx += (c >= properties[0]) ? 1 : 0;
232
0
        }
233
0
        pixel_type res = r[x] - guess;
234
0
        uint32_t token;
235
0
        uint32_t nbits;
236
0
        uint32_t bits;
237
0
        config.Encode(PackSigned(res), &token, &nbits, &bits);
238
0
        histo[ctx].Add(token);
239
0
        extra_bits += nbits;
240
0
        wp_state.UpdateErrors(r[x], x, y, ch.w);
241
0
      }
242
0
    }
243
0
    for (auto& h : histo) {
244
0
      histo_cost += h.ShannonEntropy();
245
0
      h.Clear();
246
0
    }
247
0
  }
248
0
  return histo_cost + extra_bits;
249
0
}
250
251
0
float EstimateCost(const Image& img) {
252
  // TODO(veluca): consider SIMDfication of this code.
253
0
  size_t extra_bits = 0;
254
0
  float histo_cost = 0;
255
0
  HybridUintConfig config;
256
0
  uint32_t cutoffs[] = {0,  1,  3,  5,   7,   11,  15,  23, 31,
257
0
                        47, 63, 95, 127, 191, 255, 392, 500};
258
0
  constexpr size_t nc = sizeof(cutoffs) / sizeof(*cutoffs) + 1;
259
0
  Histogram histo[nc] = {};
260
0
  for (const Channel& ch : img.channel) {
261
0
    const intptr_t onerow = ch.plane.PixelsPerRow();
262
0
    for (size_t y = 0; y < ch.h; y++) {
263
0
      const pixel_type* JXL_RESTRICT r = ch.Row(y);
264
0
      for (size_t x = 0; x < ch.w; x++) {
265
0
        pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0);
266
0
        pixel_type_w top = (y ? *(r + x - onerow) : left);
267
0
        pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left);
268
0
        size_t maxdiff = std::max(std::max(left, top), topleft) -
269
0
                         std::min(std::min(left, top), topleft);
270
0
        size_t ctx = 0;
271
0
        for (uint32_t c : cutoffs) {
272
0
          ctx += (c > maxdiff) ? 1 : 0;
273
0
        }
274
0
        pixel_type res = r[x] - ClampedGradient(top, left, topleft);
275
0
        uint32_t token;
276
0
        uint32_t nbits;
277
0
        uint32_t bits;
278
0
        config.Encode(PackSigned(res), &token, &nbits, &bits);
279
0
        histo[ctx].Add(token);
280
0
        extra_bits += nbits;
281
0
      }
282
0
    }
283
0
    for (auto& h : histo) {
284
0
      histo_cost += h.ShannonEntropy();
285
0
      h.Clear();
286
0
    }
287
0
  }
288
0
  return histo_cost + extra_bits;
289
0
}
290
291
bool do_transform(Image& image, const Transform& tr,
292
                  const weighted::Header& wp_header,
293
0
                  jxl::ThreadPool* pool = nullptr, bool force_jxlart = false) {
294
0
  Transform t = tr;
295
0
  bool did_it = true;
296
0
  if (force_jxlart) {
297
0
    if (!t.MetaApply(image)) return false;
298
0
  } else {
299
0
    did_it = TransformForward(t, image, wp_header, pool);
300
0
  }
301
0
  if (did_it) image.transform.push_back(t);
302
0
  return did_it;
303
0
}
304
305
bool maybe_do_transform(Image& image, const Transform& tr,
306
                        const CompressParams& cparams,
307
                        const weighted::Header& wp_header, float cost_before,
308
                        jxl::ThreadPool* pool = nullptr,
309
0
                        bool force_jxlart = false) {
310
0
  if (force_jxlart || cparams.speed_tier >= SpeedTier::kSquirrel) {
311
0
    return do_transform(image, tr, wp_header, pool, force_jxlart);
312
0
  }
313
0
  bool did_it = do_transform(image, tr, wp_header, pool);
314
0
  if (did_it) {
315
0
    float cost_after = EstimateCost(image);
316
0
    JXL_DEBUG_V(7, "Cost before: %f  cost after: %f", cost_before, cost_after);
317
0
    if (cost_after > cost_before) {
318
0
      Transform t = image.transform.back();
319
0
      JXL_RETURN_IF_ERROR(t.Inverse(image, wp_header, pool));
320
0
      image.transform.pop_back();
321
0
      did_it = false;
322
0
    }
323
0
  }
324
0
  return did_it;
325
0
}
326
327
void try_palettes(Image& gi, int& max_bitdepth, int& maxval,
328
                  const CompressParams& cparams_, float channel_colors_percent,
329
0
                  jxl::ThreadPool* pool = nullptr) {
330
0
  float cost_before = 0.f;
331
0
  size_t did_palette = 0;
332
0
  float nb_pixels = gi.channel[0].w * gi.channel[0].h;
333
0
  int nb_chans = gi.channel.size() - gi.nb_meta_channels;
334
  // arbitrary estimate: 4.8 bpp for 8-bit RGB
335
0
  float arbitrary_bpp_estimate = 0.2f * gi.bitdepth * nb_chans;
336
337
0
  if (cparams_.palette_colors != 0 || cparams_.lossy_palette) {
338
    // when not estimating, assume some arbitrary bpp
339
0
    cost_before = cparams_.speed_tier <= SpeedTier::kSquirrel
340
0
                      ? EstimateCost(gi)
341
0
                      : nb_pixels * arbitrary_bpp_estimate;
342
    // all-channel palette (e.g. RGBA)
343
0
    if (nb_chans > 1) {
344
0
      Transform maybe_palette(TransformId::kPalette);
345
0
      maybe_palette.begin_c = gi.nb_meta_channels;
346
0
      maybe_palette.num_c = nb_chans;
347
      // Heuristic choice of max colors for a palette:
348
      // max_colors = nb_pixels * estimated_bpp_without_palette * 0.0005 +
349
      //              + nb_pixels / 128 + 128
350
      //       (estimated_bpp_without_palette = cost_before / nb_pixels)
351
      // Rationale: small image with large palette is not effective;
352
      // also if the entropy (estimated bpp) is low (e.g. mostly solid/gradient
353
      // areas), palette is less useful and may even be counterproductive.
354
0
      maybe_palette.nb_colors = std::min(
355
0
          static_cast<int>(cost_before * 0.0005f + nb_pixels / 128 + 128),
356
0
          std::abs(cparams_.palette_colors));
357
0
      maybe_palette.ordered_palette = cparams_.palette_colors >= 0;
358
0
      maybe_palette.lossy_palette =
359
0
          (cparams_.lossy_palette && maybe_palette.num_c == 3);
360
0
      if (maybe_palette.lossy_palette) {
361
0
        maybe_palette.predictor = Predictor::Average4;
362
0
      }
363
      // TODO(veluca): use a custom weighted header if using the weighted
364
      // predictor.
365
0
      if (maybe_do_transform(gi, maybe_palette, cparams_, weighted::Header(),
366
0
                             cost_before, pool, cparams_.options.zero_tokens)) {
367
0
        did_palette = 1;
368
0
      };
369
0
    }
370
    // all-minus-one-channel palette (RGB with separate alpha, or CMY with
371
    // separate K)
372
0
    if (!did_palette && nb_chans > 3) {
373
0
      Transform maybe_palette_3(TransformId::kPalette);
374
0
      maybe_palette_3.begin_c = gi.nb_meta_channels;
375
0
      maybe_palette_3.num_c = nb_chans - 1;
376
0
      maybe_palette_3.nb_colors = std::min(
377
0
          static_cast<int>(cost_before * 0.0005f + nb_pixels / 128 + 128),
378
0
          std::abs(cparams_.palette_colors));
379
0
      maybe_palette_3.ordered_palette = cparams_.palette_colors >= 0;
380
0
      maybe_palette_3.lossy_palette = cparams_.lossy_palette;
381
0
      if (maybe_palette_3.lossy_palette) {
382
0
        maybe_palette_3.predictor = Predictor::Average4;
383
0
      }
384
0
      if (maybe_do_transform(gi, maybe_palette_3, cparams_, weighted::Header(),
385
0
                             cost_before, pool, cparams_.options.zero_tokens)) {
386
0
        did_palette = 1;
387
0
      }
388
0
    }
389
0
  }
390
391
0
  if (channel_colors_percent > 0) {
392
    // single channel palette (like FLIF's ChannelCompact)
393
0
    size_t nb_channels = gi.channel.size() - gi.nb_meta_channels - did_palette;
394
0
    int orig_bitdepth = max_bitdepth;
395
0
    max_bitdepth = 0;
396
0
    if (nb_channels > 0 && (did_palette || cost_before == 0)) {
397
0
      cost_before =
398
0
          cparams_.speed_tier < SpeedTier::kSquirrel ? EstimateCost(gi) : 0;
399
0
    }
400
0
    for (size_t i = did_palette; i < nb_channels + did_palette; i++) {
401
0
      int32_t min;
402
0
      int32_t max;
403
0
      compute_minmax(gi.channel[gi.nb_meta_channels + i], &min, &max);
404
0
      int64_t colors = static_cast<int64_t>(max) - min + 1;
405
0
      JXL_DEBUG_V(10, "Channel %" PRIuS ": range=%i..%i", i, min, max);
406
0
      Transform maybe_palette_1(TransformId::kPalette);
407
0
      maybe_palette_1.begin_c = i + gi.nb_meta_channels;
408
0
      maybe_palette_1.num_c = 1;
409
      // simple heuristic: if less than X percent of the values in the range
410
      // actually occur, it is probably worth it to do a compaction
411
      // (but only if the channel palette is less than 6% the size of the
412
      // image itself)
413
0
      maybe_palette_1.nb_colors =
414
0
          std::min(static_cast<int>(nb_pixels / 16),
415
0
                   static_cast<int>(channel_colors_percent / 100. * colors));
416
0
      if (maybe_do_transform(gi, maybe_palette_1, cparams_, weighted::Header(),
417
0
                             cost_before, pool)) {
418
        // effective bit depth is lower, adjust quantization accordingly
419
0
        compute_minmax(gi.channel[gi.nb_meta_channels + i], &min, &max);
420
0
        if (max < maxval) maxval = max;
421
0
        int ch_bitdepth =
422
0
            (max > 0 ? CeilLog2Nonzero(static_cast<uint32_t>(max)) : 0);
423
0
        if (ch_bitdepth > max_bitdepth) max_bitdepth = ch_bitdepth;
424
0
      } else {
425
0
        max_bitdepth = orig_bitdepth;
426
0
      }
427
0
    }
428
0
  }
429
0
}
430
431
}  // namespace
432
433
StatusOr<ModularFrameEncoder> ModularFrameEncoder::Create(
434
    JxlMemoryManager* memory_manager, const FrameHeader& frame_header,
435
0
    const CompressParams& cparams_orig, bool streaming_mode) {
436
0
  ModularFrameEncoder self{memory_manager};
437
0
  JXL_RETURN_IF_ERROR(self.Init(frame_header, cparams_orig, streaming_mode));
438
0
  return self;
439
0
}
440
441
ModularFrameEncoder::ModularFrameEncoder(JxlMemoryManager* memory_manager)
442
0
    : memory_manager_(memory_manager) {}
443
444
Status ModularFrameEncoder::Init(const FrameHeader& frame_header,
445
                                 const CompressParams& cparams_orig,
446
0
                                 bool streaming_mode) {
447
0
  frame_dim_ = frame_header.ToFrameDimensions();
448
0
  cparams_ = cparams_orig;
449
450
0
  size_t num_streams =
451
0
      ModularStreamId::Num(frame_dim_, frame_header.passes.num_passes);
452
0
  if (cparams_.ModularPartIsLossless()) {
453
0
    switch (cparams_.decoding_speed_tier) {
454
0
      case 0:
455
0
        break;
456
0
      case 1:
457
0
        cparams_.options.wp_tree_mode = ModularOptions::TreeMode::kWPOnly;
458
0
        break;
459
0
      case 2: {
460
0
        cparams_.options.wp_tree_mode = ModularOptions::TreeMode::kGradientOnly;
461
0
        cparams_.options.predictor = Predictor::Gradient;
462
0
        break;
463
0
      }
464
0
      case 3: {  // LZ77, no Gradient.
465
0
        cparams_.options.nb_repeats = 0;
466
0
        cparams_.options.predictor = Predictor::Gradient;
467
0
        break;
468
0
      }
469
0
      default: {  // LZ77, no predictor.
470
0
        cparams_.options.nb_repeats = 0;
471
0
        cparams_.options.predictor = Predictor::Zero;
472
0
        break;
473
0
      }
474
0
    }
475
0
  }
476
0
  if (cparams_.decoding_speed_tier >= 1 && cparams_.responsive &&
477
0
      cparams_.ModularPartIsLossless()) {
478
0
    cparams_.options.tree_kind =
479
0
        ModularOptions::TreeKind::kTrivialTreeNoPredictor;
480
0
    cparams_.options.nb_repeats = 0;
481
0
  }
482
0
  for (size_t i = 0; i < num_streams; ++i) {
483
0
    stream_images_.emplace_back(memory_manager_);
484
0
  }
485
486
  // use a sensible default if nothing explicit is specified:
487
  // Squeeze for lossy, no squeeze for lossless
488
0
  if (cparams_.responsive < 0) {
489
0
    if (cparams_.ModularPartIsLossless()) {
490
0
      cparams_.responsive = 0;
491
0
    } else {
492
0
      cparams_.responsive = 1;
493
0
    }
494
0
  }
495
496
0
  cparams_.options.splitting_heuristics_node_threshold =
497
0
      82 + 14 * static_cast<int>(cparams_.speed_tier);
498
499
0
  {
500
    // Set properties.
501
0
    std::vector<uint32_t> prop_order;
502
0
    if (cparams_.responsive) {
503
      // Properties in order of their likelihood of being useful for Squeeze
504
      // residuals.
505
0
      prop_order = {0, 1, 4, 5, 6, 7, 8, 15, 9, 10, 11, 12, 13, 14, 2, 3};
506
0
    } else {
507
      // Same, but for the non-Squeeze case.
508
0
      prop_order = {0, 1, 15, 9, 10, 11, 12, 13, 14, 2, 3, 4, 5, 6, 7, 8};
509
      // if few groups, don't use group as a property
510
0
      if (num_streams < 30 && cparams_.speed_tier > SpeedTier::kTortoise &&
511
0
          cparams_orig.ModularPartIsLossless()) {
512
0
        prop_order.erase(prop_order.begin() + 1);
513
0
      }
514
0
    }
515
0
    int max_properties = std::min<int>(
516
0
        cparams_.options.max_properties,
517
0
        static_cast<int>(
518
0
            frame_header.nonserialized_metadata->m.num_extra_channels) +
519
0
            (frame_header.encoding == FrameEncoding::kModular ? 2 : -1));
520
0
    switch (cparams_.speed_tier) {
521
0
      case SpeedTier::kHare:
522
0
        cparams_.options.splitting_heuristics_properties.assign(
523
0
            prop_order.begin(), prop_order.begin() + 4);
524
0
        cparams_.options.max_property_values = 24;
525
0
        break;
526
0
      case SpeedTier::kWombat:
527
0
        cparams_.options.splitting_heuristics_properties.assign(
528
0
            prop_order.begin(), prop_order.begin() + 5);
529
0
        cparams_.options.max_property_values = 32;
530
0
        break;
531
0
      case SpeedTier::kSquirrel:
532
0
        cparams_.options.splitting_heuristics_properties.assign(
533
0
            prop_order.begin(), prop_order.begin() + 7);
534
0
        cparams_.options.max_property_values = 48;
535
0
        break;
536
0
      case SpeedTier::kKitten:
537
0
        cparams_.options.splitting_heuristics_properties.assign(
538
0
            prop_order.begin(), prop_order.begin() + 10);
539
0
        cparams_.options.max_property_values = 96;
540
0
        break;
541
0
      case SpeedTier::kGlacier:
542
0
      case SpeedTier::kTortoise:
543
0
        cparams_.options.splitting_heuristics_properties = prop_order;
544
0
        cparams_.options.max_property_values = 256;
545
0
        break;
546
0
      default:
547
0
        cparams_.options.splitting_heuristics_properties.assign(
548
0
            prop_order.begin(), prop_order.begin() + 3);
549
0
        cparams_.options.max_property_values = 16;
550
0
        break;
551
0
    }
552
0
    if (cparams_.speed_tier > SpeedTier::kTortoise) {
553
      // Gradient in previous channels.
554
0
      for (int i = 0; i < max_properties; i++) {
555
0
        cparams_.options.splitting_heuristics_properties.push_back(
556
0
            kNumNonrefProperties + i * 4 + 3);
557
0
      }
558
0
    } else {
559
      // All the extra properties in Tortoise mode.
560
0
      for (int i = 0; i < max_properties * 4; i++) {
561
0
        cparams_.options.splitting_heuristics_properties.push_back(
562
0
            kNumNonrefProperties + i);
563
0
      }
564
0
    }
565
0
  }
566
567
0
  if ((cparams_.options.predictor == Predictor::Average0 ||
568
0
       cparams_.options.predictor == Predictor::Average1 ||
569
0
       cparams_.options.predictor == Predictor::Average2 ||
570
0
       cparams_.options.predictor == Predictor::Average3 ||
571
0
       cparams_.options.predictor == Predictor::Average4 ||
572
0
       cparams_.options.predictor == Predictor::Weighted) &&
573
0
      !cparams_.ModularPartIsLossless()) {
574
    // Lossy + Average/Weighted predictors does not work, so switch to default
575
    // predictors.
576
0
    cparams_.options.predictor = kUndefinedPredictor;
577
0
  }
578
579
0
  if (cparams_.options.predictor == kUndefinedPredictor) {
580
    // no explicit predictor(s) given, set a good default
581
0
    if ((cparams_.speed_tier <= SpeedTier::kGlacier ||
582
0
         cparams_.modular_mode == false) &&
583
0
        cparams_.IsLossless() && cparams_.responsive == JXL_FALSE) {
584
      // TODO(veluca): allow all predictors that don't break residual
585
      // multipliers in lossy mode.
586
0
      cparams_.options.predictor = Predictor::Variable;
587
0
    } else if (cparams_.responsive || cparams_.lossy_palette) {
588
      // zero predictor for Squeeze residues and lossy palette
589
0
      cparams_.options.predictor = Predictor::Zero;
590
0
    } else if (!cparams_.IsLossless()) {
591
      // If not responsive and lossy. TODO(veluca): use near_lossless instead?
592
0
      cparams_.options.predictor = Predictor::Gradient;
593
0
    } else if (cparams_.speed_tier < SpeedTier::kFalcon) {
594
      // try median and weighted predictor for anything else
595
0
      cparams_.options.predictor = Predictor::Best;
596
0
    } else if (cparams_.speed_tier == SpeedTier::kFalcon) {
597
      // just weighted predictor in falcon mode
598
0
      cparams_.options.predictor = Predictor::Weighted;
599
0
    } else if (cparams_.speed_tier > SpeedTier::kFalcon) {
600
      // just gradient predictor in thunder mode
601
0
      cparams_.options.predictor = Predictor::Gradient;
602
0
    }
603
0
  } else {
604
0
    if (cparams_.lossy_palette) cparams_.options.predictor = Predictor::Zero;
605
0
  }
606
0
  if (!cparams_.ModularPartIsLossless()) {
607
0
    if (cparams_.options.predictor == Predictor::Weighted ||
608
0
        cparams_.options.predictor == Predictor::Variable ||
609
0
        cparams_.options.predictor == Predictor::Best)
610
0
      cparams_.options.predictor = Predictor::Zero;
611
0
  }
612
0
  tree_splits_.push_back(0);
613
0
  if (cparams_.modular_mode == false) {
614
0
    JXL_ASSIGN_OR_RETURN(ModularStreamId qt0, ModularStreamId::QuantTable(0));
615
0
    cparams_.options.fast_decode_multiplier = 1.0f;
616
0
    tree_splits_.push_back(ModularStreamId::VarDCTDC(0).ID(frame_dim_));
617
0
    tree_splits_.push_back(ModularStreamId::ModularDC(0).ID(frame_dim_));
618
0
    tree_splits_.push_back(ModularStreamId::ACMetadata(0).ID(frame_dim_));
619
0
    tree_splits_.push_back(qt0.ID(frame_dim_));
620
0
    tree_splits_.push_back(ModularStreamId::ModularAC(0, 0).ID(frame_dim_));
621
0
    ac_metadata_size.resize(frame_dim_.num_dc_groups);
622
0
    extra_dc_precision.resize(frame_dim_.num_dc_groups);
623
0
  }
624
0
  tree_splits_.push_back(num_streams);
625
0
  cparams_.options.max_chan_size = frame_dim_.group_dim;
626
0
  cparams_.options.group_dim = frame_dim_.group_dim;
627
628
  // TODO(veluca): figure out how to use different predictor sets per channel.
629
0
  stream_options_.resize(num_streams, cparams_.options);
630
631
0
  stream_options_[0] = cparams_.options;
632
0
  if (cparams_.speed_tier == SpeedTier::kFalcon) {
633
0
    stream_options_[0].tree_kind = ModularOptions::TreeKind::kWPFixedDC;
634
0
  } else if (cparams_.speed_tier == SpeedTier::kThunder) {
635
0
    stream_options_[0].tree_kind = ModularOptions::TreeKind::kGradientFixedDC;
636
0
  }
637
0
  stream_options_[0].histogram_params =
638
0
      HistogramParams::ForModular(cparams_, {}, streaming_mode);
639
0
  return true;
640
0
}
641
642
Status ModularFrameEncoder::ComputeEncodingData(
643
    const FrameHeader& frame_header, const ImageMetadata& metadata,
644
    Image3F* JXL_RESTRICT color, const std::vector<ImageF>& extra_channels,
645
    const Rect& group_rect, const FrameDimensions& patch_dim,
646
    const Rect& frame_area_rect, PassesEncoderState* JXL_RESTRICT enc_state,
647
    const JxlCmsInterface& cms, ThreadPool* pool, AuxOut* aux_out,
648
0
    bool do_color) {
649
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
650
0
  JXL_DEBUG_V(6, "Computing modular encoding data for frame %s",
651
0
              frame_header.DebugString().c_str());
652
653
0
  bool groupwise = enc_state->streaming_mode;
654
655
0
  if (do_color && frame_header.loop_filter.gab && !groupwise) {
656
0
    float w = 0.9908511000000001f;
657
0
    float weights[3] = {w, w, w};
658
0
    JXL_RETURN_IF_ERROR(GaborishInverse(color, Rect(*color), weights, pool));
659
0
  }
660
661
0
  if (do_color && metadata.bit_depth.bits_per_sample <= 16 &&
662
0
      cparams_.speed_tier < SpeedTier::kCheetah &&
663
0
      cparams_.decoding_speed_tier < 2 && !groupwise) {
664
0
    JXL_RETURN_IF_ERROR(FindBestPatchDictionary(
665
0
        *color, enc_state, cms, nullptr, aux_out,
666
0
        cparams_.color_transform == ColorTransform::kXYB));
667
0
    JXL_RETURN_IF_ERROR(PatchDictionaryEncoder::SubtractFrom(
668
0
        enc_state->shared.image_features.patches, color));
669
0
  }
670
671
0
  if (cparams_.custom_splines.HasAny()) {
672
0
    PassesSharedState& shared = enc_state->shared;
673
0
    ImageFeatures& image_features = shared.image_features;
674
0
    image_features.splines = cparams_.custom_splines;
675
0
  }
676
677
  // Convert ImageBundle to modular Image object
678
0
  const size_t xsize = patch_dim.xsize;
679
0
  const size_t ysize = patch_dim.ysize;
680
681
0
  int nb_chans = 3;
682
0
  if (metadata.color_encoding.IsGray() &&
683
0
      cparams_.color_transform == ColorTransform::kNone) {
684
0
    nb_chans = 1;
685
0
  }
686
0
  if (!do_color) nb_chans = 0;
687
688
0
  nb_chans += extra_channels.size();
689
690
0
  bool fp = metadata.bit_depth.floating_point_sample &&
691
0
            cparams_.color_transform != ColorTransform::kXYB;
692
693
  // bits_per_sample is just metadata for XYB images.
694
0
  if (metadata.bit_depth.bits_per_sample >= 32 && do_color &&
695
0
      cparams_.color_transform != ColorTransform::kXYB) {
696
0
    if (metadata.bit_depth.bits_per_sample == 32 && fp == false) {
697
0
      return JXL_FAILURE("uint32_t not supported in enc_modular");
698
0
    } else if (metadata.bit_depth.bits_per_sample > 32) {
699
0
      return JXL_FAILURE("bits_per_sample > 32 not supported");
700
0
    }
701
0
  }
702
703
  // in the non-float case, there is an implicit 0 sign bit
704
0
  int max_bitdepth =
705
0
      do_color ? metadata.bit_depth.bits_per_sample + (fp ? 0 : 1) : 0;
706
0
  Image& gi = stream_images_[0];
707
0
  JXL_ASSIGN_OR_RETURN(
708
0
      gi, Image::Create(memory_manager, xsize, ysize,
709
0
                        metadata.bit_depth.bits_per_sample, nb_chans));
710
0
  int c = 0;
711
0
  if (cparams_.color_transform == ColorTransform::kXYB &&
712
0
      cparams_.modular_mode == true) {
713
0
    float enc_factors[3] = {65536.0f, 4096.0f, 4096.0f};
714
0
    if (cparams_.butteraugli_distance > 0 && !cparams_.responsive) {
715
      // quantize XYB here and then treat it as a lossless image
716
0
      enc_factors[0] *= 1.f / (1.f + 23.f * cparams_.butteraugli_distance);
717
0
      enc_factors[1] *= 1.f / (1.f + 14.f * cparams_.butteraugli_distance);
718
0
      enc_factors[2] *= 1.f / (1.f + 14.f * cparams_.butteraugli_distance);
719
0
      cparams_.butteraugli_distance = 0;
720
0
    }
721
0
    if (cparams_.manual_xyb_factors.size() == 3) {
722
0
      JXL_RETURN_IF_ERROR(DequantMatricesSetCustomDC(
723
0
          memory_manager, &enc_state->shared.matrices,
724
0
          cparams_.manual_xyb_factors.data()));
725
      // TODO(jon): update max_bitdepth in this case
726
0
    } else {
727
0
      JXL_RETURN_IF_ERROR(DequantMatricesSetCustomDC(
728
0
          memory_manager, &enc_state->shared.matrices, enc_factors));
729
0
      max_bitdepth = 12;
730
0
    }
731
0
  }
732
0
  pixel_type maxval = gi.bitdepth < 32 ? (1u << gi.bitdepth) - 1 : 0;
733
0
  if (do_color) {
734
0
    for (; c < 3; c++) {
735
0
      if (metadata.color_encoding.IsGray() &&
736
0
          cparams_.color_transform == ColorTransform::kNone &&
737
0
          c != (cparams_.color_transform == ColorTransform::kXYB ? 1 : 0))
738
0
        continue;
739
0
      int c_out = c;
740
      // XYB is encoded as YX(B-Y)
741
0
      if (cparams_.color_transform == ColorTransform::kXYB && c < 2)
742
0
        c_out = 1 - c_out;
743
0
      double factor = maxval;
744
0
      if (cparams_.color_transform == ColorTransform::kXYB)
745
0
        factor = enc_state->shared.matrices.InvDCQuant(c);
746
0
      if (c == 2 && cparams_.color_transform == ColorTransform::kXYB) {
747
0
        JXL_ENSURE(!fp);
748
0
        for (size_t y = 0; y < ysize; ++y) {
749
0
          const float* const JXL_RESTRICT row_in = color->PlaneRow(c, y);
750
0
          pixel_type* const JXL_RESTRICT row_out = gi.channel[c_out].Row(y);
751
0
          pixel_type* const JXL_RESTRICT row_Y = gi.channel[0].Row(y);
752
0
          for (size_t x = 0; x < xsize; ++x) {
753
            // TODO(eustas): check if std::roundf is appropriate
754
0
            row_out[x] = row_in[x] * factor + 0.5f;
755
0
            row_out[x] -= row_Y[x];
756
0
          }
757
0
        }
758
0
      } else {
759
0
        int bits = metadata.bit_depth.bits_per_sample;
760
0
        int exp_bits = metadata.bit_depth.exponent_bits_per_sample;
761
0
        gi.channel[c_out].hshift = frame_header.chroma_subsampling.HShift(c);
762
0
        gi.channel[c_out].vshift = frame_header.chroma_subsampling.VShift(c);
763
0
        size_t xsize_shifted = DivCeil(xsize, 1 << gi.channel[c_out].hshift);
764
0
        size_t ysize_shifted = DivCeil(ysize, 1 << gi.channel[c_out].vshift);
765
0
        JXL_RETURN_IF_ERROR(
766
0
            gi.channel[c_out].shrink(xsize_shifted, ysize_shifted));
767
0
        const auto process_row = [&](const int task,
768
0
                                     const int thread) -> Status {
769
0
          const size_t y = task;
770
0
          const float* const JXL_RESTRICT row_in =
771
0
              color->PlaneRow(c, y + group_rect.y0()) + group_rect.x0();
772
0
          pixel_type* const JXL_RESTRICT row_out = gi.channel[c_out].Row(y);
773
0
          JXL_RETURN_IF_ERROR(float_to_int(row_in, row_out, xsize_shifted, bits,
774
0
                                           exp_bits, fp, factor));
775
0
          return true;
776
0
        };
777
0
        JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, ysize_shifted,
778
0
                                      ThreadPool::NoInit, process_row,
779
0
                                      "float2int"));
780
0
      }
781
0
    }
782
0
    if (metadata.color_encoding.IsGray() &&
783
0
        cparams_.color_transform == ColorTransform::kNone)
784
0
      c = 1;
785
0
  }
786
787
0
  for (size_t ec = 0; ec < extra_channels.size(); ec++, c++) {
788
0
    const ExtraChannelInfo& eci = metadata.extra_channel_info[ec];
789
0
    size_t ecups = frame_header.extra_channel_upsampling[ec];
790
0
    JXL_RETURN_IF_ERROR(
791
0
        gi.channel[c].shrink(DivCeil(patch_dim.xsize_upsampled, ecups),
792
0
                             DivCeil(patch_dim.ysize_upsampled, ecups)));
793
0
    gi.channel[c].hshift = gi.channel[c].vshift =
794
0
        CeilLog2Nonzero(ecups) - CeilLog2Nonzero(frame_header.upsampling);
795
796
0
    int bits = eci.bit_depth.bits_per_sample;
797
0
    int exp_bits = eci.bit_depth.exponent_bits_per_sample;
798
0
    bool fp = eci.bit_depth.floating_point_sample;
799
0
    double factor = (fp ? 1 : ((1u << eci.bit_depth.bits_per_sample) - 1));
800
0
    if (bits + (fp ? 0 : 1) > max_bitdepth) max_bitdepth = bits + (fp ? 0 : 1);
801
0
    const auto process_row = [&](const int task, const int thread) -> Status {
802
0
      const size_t y = task;
803
0
      const float* const JXL_RESTRICT row_in =
804
0
          extra_channels[ec].Row(y + group_rect.y0()) + group_rect.x0();
805
0
      pixel_type* const JXL_RESTRICT row_out = gi.channel[c].Row(y);
806
0
      JXL_RETURN_IF_ERROR(float_to_int(row_in, row_out,
807
0
                                       gi.channel[c].plane.xsize(), bits,
808
0
                                       exp_bits, fp, factor));
809
0
      return true;
810
0
    };
811
0
    JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, gi.channel[c].plane.ysize(),
812
0
                                  ThreadPool::NoInit, process_row,
813
0
                                  "float2int"));
814
0
  }
815
0
  JXL_ENSURE(c == nb_chans);
816
817
0
  int level_max_bitdepth = (cparams_.level == 5 ? 16 : 32);
818
0
  if (max_bitdepth > level_max_bitdepth) {
819
0
    return JXL_FAILURE(
820
0
        "Bitdepth too high for level %i (need %i bits, have only %i in this "
821
0
        "level)",
822
0
        cparams_.level, max_bitdepth, level_max_bitdepth);
823
0
  }
824
825
  // Set options and apply transformations
826
0
  if (!cparams_.ModularPartIsLossless()) {
827
0
    if (cparams_.palette_colors != 0) {
828
0
      JXL_DEBUG_V(3, "Lossy encode, not doing palette transforms");
829
0
    }
830
0
    if (cparams_.color_transform == ColorTransform::kXYB) {
831
0
      cparams_.channel_colors_pre_transform_percent = 0;
832
0
    }
833
0
    cparams_.channel_colors_percent = 0;
834
0
    cparams_.palette_colors = 0;
835
0
    cparams_.lossy_palette = false;
836
0
  }
837
838
  // Global palette transforms
839
0
  float channel_colors_percent = 0;
840
0
  if (!cparams_.lossy_palette &&
841
0
      (cparams_.speed_tier <= SpeedTier::kThunder ||
842
0
       (do_color && metadata.bit_depth.bits_per_sample > 8))) {
843
0
    channel_colors_percent = cparams_.channel_colors_pre_transform_percent;
844
0
  }
845
0
  if (!groupwise) {
846
0
    try_palettes(gi, max_bitdepth, maxval, cparams_, channel_colors_percent,
847
0
                 pool);
848
0
  }
849
850
  // don't do an RCT if we're short on bits
851
0
  if (cparams_.color_transform == ColorTransform::kNone && do_color &&
852
0
      gi.channel.size() - gi.nb_meta_channels >= 3 &&
853
0
      max_bitdepth + 1 < level_max_bitdepth) {
854
0
    if (cparams_.colorspace < 0 && (!cparams_.ModularPartIsLossless() ||
855
0
                                    cparams_.speed_tier > SpeedTier::kHare)) {
856
0
      Transform ycocg{TransformId::kRCT};
857
0
      ycocg.rct_type = 6;
858
0
      ycocg.begin_c = gi.nb_meta_channels;
859
0
      do_transform(gi, ycocg, weighted::Header(), pool);
860
0
      max_bitdepth++;
861
0
    } else if (cparams_.colorspace > 0) {
862
0
      Transform sg(TransformId::kRCT);
863
0
      sg.begin_c = gi.nb_meta_channels;
864
0
      sg.rct_type = cparams_.colorspace;
865
0
      do_transform(gi, sg, weighted::Header(), pool);
866
0
      max_bitdepth++;
867
0
    }
868
0
  }
869
870
0
  if (cparams_.move_to_front_from_channel > 0) {
871
0
    for (size_t tgt = 0;
872
0
         tgt + cparams_.move_to_front_from_channel < gi.channel.size(); tgt++) {
873
0
      size_t pos = cparams_.move_to_front_from_channel;
874
0
      while (pos > 0) {
875
0
        Transform move(TransformId::kRCT);
876
0
        if (pos == 1) {
877
0
          move.begin_c = tgt;
878
0
          move.rct_type = 28;  // RGB -> GRB
879
0
          pos -= 1;
880
0
        } else {
881
0
          move.begin_c = tgt + pos - 2;
882
0
          move.rct_type = 14;  // RGB -> BRG
883
0
          pos -= 2;
884
0
        }
885
0
        do_transform(gi, move, weighted::Header(), pool);
886
0
      }
887
0
    }
888
0
  }
889
890
  // don't do squeeze if we don't have some spare bits
891
0
  if (!groupwise && cparams_.responsive && !gi.channel.empty() &&
892
0
      max_bitdepth + 2 < level_max_bitdepth) {
893
0
    Transform t(TransformId::kSqueeze);
894
0
    do_transform(gi, t, weighted::Header(), pool);
895
0
    max_bitdepth += 2;
896
0
  }
897
898
0
  if (max_bitdepth + 1 > level_max_bitdepth) {
899
    // force no group RCTs if we don't have a spare bit
900
0
    cparams_.colorspace = 0;
901
0
  }
902
0
  JXL_ENSURE(max_bitdepth <= level_max_bitdepth);
903
904
0
  if (!cparams_.ModularPartIsLossless()) {
905
0
    quants_.resize(gi.channel.size(), 1);
906
0
    float quantizer = 0.25f;
907
0
    if (!cparams_.responsive) {
908
0
      JXL_DEBUG_V(1,
909
0
                  "Warning: lossy compression without Squeeze "
910
0
                  "transform is just color quantization.");
911
0
      quantizer *= 0.1f;
912
0
    }
913
0
    float bitdepth_correction = 1.f;
914
0
    if (cparams_.color_transform != ColorTransform::kXYB) {
915
0
      bitdepth_correction = maxval / 255.f;
916
0
    }
917
0
    std::vector<float> quantizers;
918
0
    for (size_t i = 0; i < 3; i++) {
919
0
      float dist = cparams_.butteraugli_distance;
920
0
      quantizers.push_back(quantizer * dist * bitdepth_correction);
921
0
    }
922
0
    for (size_t i = 0; i < extra_channels.size(); i++) {
923
0
      int ec_bitdepth =
924
0
          metadata.extra_channel_info[i].bit_depth.bits_per_sample;
925
0
      pixel_type ec_maxval = ec_bitdepth < 32 ? (1u << ec_bitdepth) - 1 : 0;
926
0
      bitdepth_correction = ec_maxval / 255.f;
927
0
      float dist = 0;
928
0
      if (i < cparams_.ec_distance.size()) dist = cparams_.ec_distance[i];
929
0
      if (dist < 0) dist = cparams_.butteraugli_distance;
930
0
      quantizers.push_back(quantizer * dist * bitdepth_correction);
931
0
    }
932
0
    if (cparams_.options.nb_repeats == 0) {
933
0
      return JXL_FAILURE("nb_repeats = 0 not supported with modular lossy!");
934
0
    }
935
0
    for (uint32_t i = gi.nb_meta_channels; i < gi.channel.size(); i++) {
936
0
      Channel& ch = gi.channel[i];
937
0
      int shift = ch.hshift + ch.vshift;  // number of pixel halvings
938
0
      if (shift > 16) shift = 16;
939
0
      if (shift > 0) shift--;
940
0
      int q;
941
      // assuming default Squeeze here
942
0
      int component =
943
0
          (do_color ? 0 : 3) + ((i - gi.nb_meta_channels) % nb_chans);
944
      // last 4 channels are final chroma residuals
945
0
      if (nb_chans > 2 && i >= gi.channel.size() - 4 && cparams_.responsive) {
946
0
        component = 1;
947
0
      }
948
0
      if (cparams_.color_transform == ColorTransform::kXYB && component < 3) {
949
0
        q = quantizers[component] * squeeze_quality_factor_xyb *
950
0
            squeeze_xyb_qtable[component][shift];
951
0
      } else {
952
0
        if (cparams_.colorspace != 0 && component > 0 && component < 3) {
953
0
          q = quantizers[component] * squeeze_quality_factor *
954
0
              squeeze_chroma_qtable[shift];
955
0
        } else {
956
0
          q = quantizers[component] * squeeze_quality_factor *
957
0
              squeeze_luma_factor * squeeze_luma_qtable[shift];
958
0
        }
959
0
      }
960
0
      if (q < 1) q = 1;
961
0
      QuantizeChannel(gi.channel[i], q);
962
0
      quants_[i] = q;
963
0
    }
964
0
  }
965
966
  // Fill other groups.
967
  // DC
968
0
  for (size_t group_id = 0; group_id < patch_dim.num_dc_groups; group_id++) {
969
0
    const size_t rgx = group_id % patch_dim.xsize_dc_groups;
970
0
    const size_t rgy = group_id / patch_dim.xsize_dc_groups;
971
0
    const Rect rect(rgx * patch_dim.dc_group_dim, rgy * patch_dim.dc_group_dim,
972
0
                    patch_dim.dc_group_dim, patch_dim.dc_group_dim);
973
0
    size_t gx = rgx + frame_area_rect.x0() / 2048;
974
0
    size_t gy = rgy + frame_area_rect.y0() / 2048;
975
0
    size_t real_group_id = gy * frame_dim_.xsize_dc_groups + gx;
976
    // minShift==3 because (frame_dim.dc_group_dim >> 3) == frame_dim.group_dim
977
    // maxShift==1000 is infinity
978
0
    stream_params_.push_back(
979
0
        GroupParams{rect, 3, 1000, ModularStreamId::ModularDC(real_group_id)});
980
0
  }
981
  // AC global -> nothing.
982
  // AC
983
0
  for (size_t group_id = 0; group_id < patch_dim.num_groups; group_id++) {
984
0
    const size_t rgx = group_id % patch_dim.xsize_groups;
985
0
    const size_t rgy = group_id / patch_dim.xsize_groups;
986
0
    const Rect mrect(rgx * patch_dim.group_dim, rgy * patch_dim.group_dim,
987
0
                     patch_dim.group_dim, patch_dim.group_dim);
988
0
    size_t gx = rgx + frame_area_rect.x0() / (frame_dim_.group_dim);
989
0
    size_t gy = rgy + frame_area_rect.y0() / (frame_dim_.group_dim);
990
0
    size_t real_group_id = gy * frame_dim_.xsize_groups + gx;
991
0
    for (size_t i = 0; i < enc_state->progressive_splitter.GetNumPasses();
992
0
         i++) {
993
0
      int maxShift;
994
0
      int minShift;
995
0
      frame_header.passes.GetDownsamplingBracket(i, minShift, maxShift);
996
0
      stream_params_.push_back(
997
0
          GroupParams{mrect, minShift, maxShift,
998
0
                      ModularStreamId::ModularAC(real_group_id, i)});
999
0
    }
1000
0
  }
1001
  // if there's only one group, everything ends up in GlobalModular
1002
  // in that case, also try RCTs/WP params for the one group
1003
0
  if (stream_params_.size() == 2) {
1004
0
    stream_params_.push_back(GroupParams{Rect(0, 0, xsize, ysize), 0, 1000,
1005
0
                                         ModularStreamId::Global()});
1006
0
  }
1007
0
  gi_channel_.resize(stream_images_.size());
1008
1009
0
  const auto process_row = [&](const uint32_t i,
1010
0
                               size_t /* thread */) -> Status {
1011
0
    size_t stream = stream_params_[i].id.ID(frame_dim_);
1012
0
    if (stream != 0) {
1013
0
      stream_options_[stream] = stream_options_[0];
1014
0
    }
1015
0
    JXL_RETURN_IF_ERROR(PrepareStreamParams(
1016
0
        stream_params_[i].rect, cparams_, stream_params_[i].minShift,
1017
0
        stream_params_[i].maxShift, stream_params_[i].id, do_color, groupwise));
1018
0
    return true;
1019
0
  };
1020
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, stream_params_.size(),
1021
0
                                ThreadPool::NoInit, process_row,
1022
0
                                "ChooseParams"));
1023
0
  {
1024
    // Clear out channels that have been copied to groups.
1025
0
    Image& full_image = stream_images_[0];
1026
0
    size_t c = full_image.nb_meta_channels;
1027
0
    for (; c < full_image.channel.size(); c++) {
1028
0
      Channel& fc = full_image.channel[c];
1029
0
      if (fc.w > frame_dim_.group_dim || fc.h > frame_dim_.group_dim) break;
1030
0
    }
1031
0
    for (; c < full_image.channel.size(); c++) {
1032
0
      full_image.channel[c].plane = ImageI();
1033
0
    }
1034
0
  }
1035
1036
0
  JXL_RETURN_IF_ERROR(ValidateChannelDimensions(gi, stream_options_[0]));
1037
0
  return true;
1038
0
}
1039
1040
0
Status ModularFrameEncoder::ComputeTree(ThreadPool* pool) {
1041
0
  std::vector<ModularMultiplierInfo> multiplier_info;
1042
0
  if (!quants_.empty()) {
1043
0
    for (uint32_t stream_id = 0; stream_id < stream_images_.size();
1044
0
         stream_id++) {
1045
      // skip non-modular stream_ids
1046
0
      if (stream_id > 0 && gi_channel_[stream_id].empty()) continue;
1047
0
      const Image& image = stream_images_[stream_id];
1048
0
      const ModularOptions& options = stream_options_[stream_id];
1049
0
      for (uint32_t i = image.nb_meta_channels; i < image.channel.size(); i++) {
1050
0
        if (i >= image.nb_meta_channels &&
1051
0
            (image.channel[i].w > options.max_chan_size ||
1052
0
             image.channel[i].h > options.max_chan_size)) {
1053
0
          continue;
1054
0
        }
1055
0
        if (stream_id > 0 && gi_channel_[stream_id].empty()) continue;
1056
0
        size_t ch_id = stream_id == 0
1057
0
                           ? i
1058
0
                           : gi_channel_[stream_id][i - image.nb_meta_channels];
1059
0
        uint32_t q = quants_[ch_id];
1060
        // Inform the tree splitting heuristics that each channel in each group
1061
        // used this quantization factor. This will produce a tree with the
1062
        // given multipliers.
1063
0
        if (multiplier_info.empty() ||
1064
0
            multiplier_info.back().range[1][0] != stream_id ||
1065
0
            multiplier_info.back().multiplier != q) {
1066
0
          StaticPropRange range;
1067
0
          range[0] = {{i, i + 1}};
1068
0
          range[1] = {{stream_id, stream_id + 1}};
1069
0
          multiplier_info.push_back({range, static_cast<uint32_t>(q)});
1070
0
        } else {
1071
          // Previous channel in the same group had the same quantization
1072
          // factor. Don't provide two different ranges, as that creates
1073
          // unnecessary nodes.
1074
0
          multiplier_info.back().range[0][1] = i + 1;
1075
0
        }
1076
0
      }
1077
0
    }
1078
    // Merge group+channel settings that have the same channels and quantization
1079
    // factors, to avoid unnecessary nodes.
1080
0
    std::sort(multiplier_info.begin(), multiplier_info.end(),
1081
0
              [](ModularMultiplierInfo a, ModularMultiplierInfo b) {
1082
0
                return std::make_tuple(a.range, a.multiplier) <
1083
0
                       std::make_tuple(b.range, b.multiplier);
1084
0
              });
1085
0
    size_t new_num = 1;
1086
0
    for (size_t i = 1; i < multiplier_info.size(); i++) {
1087
0
      ModularMultiplierInfo& prev = multiplier_info[new_num - 1];
1088
0
      ModularMultiplierInfo& cur = multiplier_info[i];
1089
0
      if (prev.range[0] == cur.range[0] && prev.multiplier == cur.multiplier &&
1090
0
          prev.range[1][1] == cur.range[1][0]) {
1091
0
        prev.range[1][1] = cur.range[1][1];
1092
0
      } else {
1093
0
        multiplier_info[new_num++] = multiplier_info[i];
1094
0
      }
1095
0
    }
1096
0
    multiplier_info.resize(new_num);
1097
0
  }
1098
1099
0
  if (!cparams_.custom_fixed_tree.empty()) {
1100
0
    tree_ = cparams_.custom_fixed_tree;
1101
0
  } else if (cparams_.speed_tier < SpeedTier::kFalcon ||
1102
0
             !cparams_.modular_mode) {
1103
    // Avoid creating a tree with leaves that don't correspond to any pixels.
1104
0
    std::vector<size_t> useful_splits;
1105
0
    useful_splits.reserve(tree_splits_.size());
1106
0
    for (size_t chunk = 0; chunk < tree_splits_.size() - 1; chunk++) {
1107
0
      bool has_pixels = false;
1108
0
      size_t start = tree_splits_[chunk];
1109
0
      size_t stop = tree_splits_[chunk + 1];
1110
0
      for (size_t i = start; i < stop; i++) {
1111
0
        if (!stream_images_[i].empty()) has_pixels = true;
1112
0
      }
1113
0
      if (has_pixels) {
1114
0
        useful_splits.push_back(tree_splits_[chunk]);
1115
0
      }
1116
0
    }
1117
    // Don't do anything if modular mode does not have any pixels in this image
1118
0
    if (useful_splits.empty()) return true;
1119
0
    useful_splits.push_back(tree_splits_.back());
1120
1121
0
    std::vector<Tree> trees(useful_splits.size() - 1);
1122
0
    const auto process_chunk = [&](const uint32_t chunk,
1123
0
                                   size_t /* thread */) -> Status {
1124
      // TODO(veluca): parallelize more.
1125
0
      size_t total_pixels = 0;
1126
0
      uint32_t start = useful_splits[chunk];
1127
0
      uint32_t stop = useful_splits[chunk + 1];
1128
0
      while (start < stop && stream_images_[start].empty()) ++start;
1129
0
      while (start < stop && stream_images_[stop - 1].empty()) --stop;
1130
0
      if (stream_options_[start].tree_kind !=
1131
0
          ModularOptions::TreeKind::kLearn) {
1132
0
        for (size_t i = start; i < stop; i++) {
1133
0
          for (const Channel& ch : stream_images_[i].channel) {
1134
0
            total_pixels += ch.w * ch.h;
1135
0
          }
1136
0
        }
1137
0
        trees[chunk] = PredefinedTree(stream_options_[start].tree_kind,
1138
0
                                      total_pixels, 8, 0);
1139
0
        return true;
1140
0
      }
1141
0
      TreeSamples tree_samples;
1142
0
      JXL_RETURN_IF_ERROR(
1143
0
          tree_samples.SetPredictor(stream_options_[start].predictor,
1144
0
                                    stream_options_[start].wp_tree_mode));
1145
0
      JXL_RETURN_IF_ERROR(tree_samples.SetProperties(
1146
0
          stream_options_[start].splitting_heuristics_properties,
1147
0
          stream_options_[start].wp_tree_mode));
1148
0
      uint32_t max_c = 0;
1149
0
      std::vector<pixel_type> pixel_samples;
1150
0
      std::vector<pixel_type> diff_samples;
1151
0
      std::vector<uint32_t> group_pixel_count;
1152
0
      std::vector<uint32_t> channel_pixel_count;
1153
0
      for (uint32_t i = start; i < stop; i++) {
1154
0
        max_c = std::max<uint32_t>(stream_images_[i].channel.size(), max_c);
1155
0
        CollectPixelSamples(stream_images_[i], stream_options_[i], i,
1156
0
                            group_pixel_count, channel_pixel_count,
1157
0
                            pixel_samples, diff_samples);
1158
0
      }
1159
0
      StaticPropRange range;
1160
0
      range[0] = {{0, max_c}};
1161
0
      range[1] = {{start, stop}};
1162
1163
0
      tree_samples.PreQuantizeProperties(
1164
0
          range, multiplier_info, group_pixel_count, channel_pixel_count,
1165
0
          pixel_samples, diff_samples,
1166
0
          stream_options_[start].max_property_values);
1167
0
      for (size_t i = start; i < stop; i++) {
1168
0
        JXL_RETURN_IF_ERROR(
1169
0
            ModularGenericCompress(stream_images_[i], stream_options_[i],
1170
0
                                   /*writer=*/nullptr,
1171
0
                                   /*aux_out=*/nullptr, LayerType::Header, i,
1172
0
                                   &tree_samples, &total_pixels));
1173
0
      }
1174
1175
      // TODO(veluca): parallelize more.
1176
0
      JXL_ASSIGN_OR_RETURN(
1177
0
          trees[chunk],
1178
0
          LearnTree(std::move(tree_samples), total_pixels,
1179
0
                    stream_options_[start], multiplier_info, range));
1180
0
      return true;
1181
0
    };
1182
0
    JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, useful_splits.size() - 1,
1183
0
                                  ThreadPool::NoInit, process_chunk,
1184
0
                                  "LearnTrees"));
1185
0
    tree_.clear();
1186
0
    JXL_RETURN_IF_ERROR(
1187
0
        MergeTrees(trees, useful_splits, 0, useful_splits.size() - 1, &tree_));
1188
0
  } else {
1189
    // Fixed tree.
1190
0
    size_t total_pixels = 0;
1191
0
    int max_bitdepth = 0;
1192
0
    for (const Image& img : stream_images_) {
1193
0
      max_bitdepth = std::max(max_bitdepth, img.bitdepth);
1194
0
      for (const Channel& ch : img.channel) {
1195
0
        total_pixels += ch.w * ch.h;
1196
0
      }
1197
0
    }
1198
0
    if (cparams_.speed_tier <= SpeedTier::kFalcon) {
1199
0
      tree_ = PredefinedTree(ModularOptions::TreeKind::kWPFixedDC, total_pixels,
1200
0
                             max_bitdepth, stream_options_[0].max_properties);
1201
0
    } else if (cparams_.speed_tier <= SpeedTier::kThunder) {
1202
0
      tree_ = PredefinedTree(ModularOptions::TreeKind::kGradientFixedDC,
1203
0
                             total_pixels, max_bitdepth,
1204
0
                             stream_options_[0].max_properties);
1205
0
    } else {
1206
0
      tree_ = {PropertyDecisionNode::Leaf(Predictor::Gradient)};
1207
0
    }
1208
0
  }
1209
0
  tree_tokens_.resize(1);
1210
0
  tree_tokens_[0].clear();
1211
0
  Tree decoded_tree;
1212
0
  JXL_RETURN_IF_ERROR(TokenizeTree(tree_, tree_tokens_.data(), &decoded_tree));
1213
0
  JXL_ENSURE(tree_.size() == decoded_tree.size());
1214
0
  tree_ = std::move(decoded_tree);
1215
1216
  /* TODO(szabadka) Add text output callback to cparams
1217
  if (kPrintTree && WantDebugOutput(aux_out)) {
1218
    if (frame_header.dc_level > 0) {
1219
      PrintTree(tree_, aux_out->debug_prefix + "/dc_frame_level" +
1220
                           std::to_string(frame_header.dc_level) + "_tree");
1221
    } else {
1222
      PrintTree(tree_, aux_out->debug_prefix + "/global_tree");
1223
    }
1224
  } */
1225
0
  return true;
1226
0
}
1227
1228
0
Status ModularFrameEncoder::ComputeTokens(ThreadPool* pool) {
1229
0
  size_t num_streams = stream_images_.size();
1230
0
  stream_headers_.resize(num_streams);
1231
0
  tokens_.resize(num_streams);
1232
0
  image_widths_.resize(num_streams);
1233
0
  const auto process_stream = [&](const uint32_t stream_id,
1234
0
                                  size_t /* thread */) -> Status {
1235
0
    AuxOut my_aux_out;
1236
0
    tokens_[stream_id].clear();
1237
0
    JXL_RETURN_IF_ERROR(ModularGenericCompress(
1238
0
        stream_images_[stream_id], stream_options_[stream_id],
1239
0
        /*writer=*/nullptr, &my_aux_out, LayerType::Header, stream_id,
1240
0
        /*tree_samples=*/nullptr,
1241
0
        /*total_pixels=*/nullptr,
1242
0
        /*tree=*/&tree_, /*header=*/&stream_headers_[stream_id],
1243
0
        /*tokens=*/&tokens_[stream_id],
1244
0
        /*widths=*/&image_widths_[stream_id]));
1245
0
    return true;
1246
0
  };
1247
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, num_streams, ThreadPool::NoInit,
1248
0
                                process_stream, "ComputeTokens"));
1249
0
  return true;
1250
0
}
1251
1252
Status ModularFrameEncoder::EncodeGlobalInfo(bool streaming_mode,
1253
                                             BitWriter* writer,
1254
0
                                             AuxOut* aux_out) {
1255
0
  JxlMemoryManager* memory_manager = writer->memory_manager();
1256
0
  bool skip_rest = false;
1257
0
  JXL_RETURN_IF_ERROR(
1258
0
      writer->WithMaxBits(1, LayerType::ModularTree, aux_out, [&] {
1259
        // If we are using brotli, or not using modular mode.
1260
0
        if (tree_tokens_.empty() || tree_tokens_[0].empty()) {
1261
0
          writer->Write(1, 0);
1262
0
          skip_rest = true;
1263
0
        } else {
1264
0
          writer->Write(1, 1);
1265
0
        }
1266
0
        return true;
1267
0
      }));
1268
0
  if (skip_rest) return true;
1269
1270
  // Write tree
1271
0
  HistogramParams params =
1272
0
      HistogramParams::ForModular(cparams_, extra_dc_precision, streaming_mode);
1273
0
  {
1274
0
    EntropyEncodingData tree_code;
1275
0
    std::vector<uint8_t> tree_context_map;
1276
0
    JXL_ASSIGN_OR_RETURN(
1277
0
        size_t cost,
1278
0
        BuildAndEncodeHistograms(memory_manager, params, kNumTreeContexts,
1279
0
                                 tree_tokens_, &tree_code, &tree_context_map,
1280
0
                                 writer, LayerType::ModularTree, aux_out));
1281
0
    (void)cost;
1282
0
    JXL_RETURN_IF_ERROR(WriteTokens(tree_tokens_[0], tree_code,
1283
0
                                    tree_context_map, 0, writer,
1284
0
                                    LayerType::ModularTree, aux_out));
1285
0
  }
1286
0
  params.streaming_mode = streaming_mode;
1287
0
  params.add_missing_symbols = streaming_mode;
1288
0
  params.image_widths = image_widths_;
1289
  // Write histograms.
1290
0
  JXL_ASSIGN_OR_RETURN(
1291
0
      size_t cost,
1292
0
      BuildAndEncodeHistograms(memory_manager, params, (tree_.size() + 1) / 2,
1293
0
                               tokens_, &code_, &context_map_, writer,
1294
0
                               LayerType::ModularGlobal, aux_out));
1295
0
  (void)cost;
1296
0
  return true;
1297
0
}
1298
1299
Status ModularFrameEncoder::EncodeStream(BitWriter* writer, AuxOut* aux_out,
1300
                                         LayerType layer,
1301
0
                                         const ModularStreamId& stream) {
1302
0
  size_t stream_id = stream.ID(frame_dim_);
1303
0
  if (stream_images_[stream_id].channel.empty()) {
1304
0
    JXL_DEBUG_V(10, "Modular stream %" PRIuS " is empty.", stream_id);
1305
0
    return true;  // Image with no channels, header never gets decoded.
1306
0
  }
1307
0
  if (tokens_.empty()) {
1308
0
    JXL_RETURN_IF_ERROR(ModularGenericCompress(
1309
0
        stream_images_[stream_id], stream_options_[stream_id], writer, aux_out,
1310
0
        layer, stream_id));
1311
0
  } else {
1312
0
    JXL_RETURN_IF_ERROR(
1313
0
        Bundle::Write(stream_headers_[stream_id], writer, layer, aux_out));
1314
0
    JXL_RETURN_IF_ERROR(WriteTokens(tokens_[stream_id], code_, context_map_, 0,
1315
0
                                    writer, layer, aux_out));
1316
0
  }
1317
0
  return true;
1318
0
}
1319
1320
0
void ModularFrameEncoder::ClearStreamData(const ModularStreamId& stream) {
1321
0
  size_t stream_id = stream.ID(frame_dim_);
1322
0
  Image empty_image(stream_images_[stream_id].memory_manager());
1323
0
  std::swap(stream_images_[stream_id], empty_image);
1324
0
}
1325
1326
0
void ModularFrameEncoder::ClearModularStreamData() {
1327
0
  for (const auto& group : stream_params_) {
1328
0
    ClearStreamData(group.id);
1329
0
  }
1330
0
  stream_params_.clear();
1331
0
}
1332
1333
size_t ModularFrameEncoder::ComputeStreamingAbsoluteAcGroupId(
1334
    size_t dc_group_id, size_t ac_group_id,
1335
0
    const FrameDimensions& patch_dim) const {
1336
0
  size_t dc_group_x = dc_group_id % frame_dim_.xsize_dc_groups;
1337
0
  size_t dc_group_y = dc_group_id / frame_dim_.xsize_dc_groups;
1338
0
  size_t ac_group_x = ac_group_id % patch_dim.xsize_groups;
1339
0
  size_t ac_group_y = ac_group_id / patch_dim.xsize_groups;
1340
0
  return (dc_group_x * 8 + ac_group_x) +
1341
0
         (dc_group_y * 8 + ac_group_y) * frame_dim_.xsize_groups;
1342
0
}
1343
1344
Status ModularFrameEncoder::PrepareStreamParams(const Rect& rect,
1345
                                                const CompressParams& cparams_,
1346
                                                int minShift, int maxShift,
1347
                                                const ModularStreamId& stream,
1348
0
                                                bool do_color, bool groupwise) {
1349
0
  size_t stream_id = stream.ID(frame_dim_);
1350
0
  if (stream_id == 0 && frame_dim_.num_groups != 1) {
1351
    // If we have multiple groups, then the stream with ID 0 holds the full
1352
    // image and we do not want to apply transforms or in general change the
1353
    // pixel values.
1354
0
    return true;
1355
0
  }
1356
0
  Image& full_image = stream_images_[0];
1357
0
  JxlMemoryManager* memory_manager = full_image.memory_manager();
1358
0
  const size_t xsize = rect.xsize();
1359
0
  const size_t ysize = rect.ysize();
1360
0
  Image& gi = stream_images_[stream_id];
1361
0
  if (stream_id > 0) {
1362
0
    JXL_ASSIGN_OR_RETURN(gi, Image::Create(memory_manager, xsize, ysize,
1363
0
                                           full_image.bitdepth, 0));
1364
    // start at the first bigger-than-frame_dim.group_dim non-metachannel
1365
0
    size_t c = full_image.nb_meta_channels;
1366
0
    if (!groupwise) {
1367
0
      for (; c < full_image.channel.size(); c++) {
1368
0
        Channel& fc = full_image.channel[c];
1369
0
        if (fc.w > frame_dim_.group_dim || fc.h > frame_dim_.group_dim) break;
1370
0
      }
1371
0
    }
1372
0
    for (; c < full_image.channel.size(); c++) {
1373
0
      Channel& fc = full_image.channel[c];
1374
0
      int shift = std::min(fc.hshift, fc.vshift);
1375
0
      if (shift > maxShift) continue;
1376
0
      if (shift < minShift) continue;
1377
0
      Rect r(rect.x0() >> fc.hshift, rect.y0() >> fc.vshift,
1378
0
             rect.xsize() >> fc.hshift, rect.ysize() >> fc.vshift, fc.w, fc.h);
1379
0
      if (r.xsize() == 0 || r.ysize() == 0) continue;
1380
0
      gi_channel_[stream_id].push_back(c);
1381
0
      JXL_ASSIGN_OR_RETURN(
1382
0
          Channel gc, Channel::Create(memory_manager, r.xsize(), r.ysize()));
1383
0
      gc.hshift = fc.hshift;
1384
0
      gc.vshift = fc.vshift;
1385
0
      for (size_t y = 0; y < r.ysize(); ++y) {
1386
0
        memcpy(gc.Row(y), r.ConstRow(fc.plane, y),
1387
0
               r.xsize() * sizeof(pixel_type));
1388
0
      }
1389
0
      gi.channel.emplace_back(std::move(gc));
1390
0
    }
1391
1392
0
    if (gi.channel.empty()) return true;
1393
    // Do some per-group transforms
1394
1395
    // Local palette transforms
1396
    // TODO(veluca): make this work with quantize-after-prediction in lossy
1397
    // mode.
1398
0
    if (cparams_.butteraugli_distance == 0.f && !cparams_.lossy_palette &&
1399
0
        cparams_.speed_tier < SpeedTier::kCheetah) {
1400
0
      int max_bitdepth = 0, maxval = 0;  // don't care about that here
1401
0
      float channel_color_percent = 0;
1402
0
      if (!(cparams_.responsive && cparams_.decoding_speed_tier >= 1)) {
1403
0
        channel_color_percent = cparams_.channel_colors_percent;
1404
0
      }
1405
0
      try_palettes(gi, max_bitdepth, maxval, cparams_, channel_color_percent);
1406
0
    }
1407
0
  }
1408
1409
  // lossless and no specific color transform specified: try Nothing, YCoCg,
1410
  // and 17 RCTs
1411
0
  if (cparams_.color_transform == ColorTransform::kNone &&
1412
0
      cparams_.IsLossless() && cparams_.colorspace < 0 &&
1413
0
      gi.channel.size() - gi.nb_meta_channels >= 3 &&
1414
0
      cparams_.responsive == JXL_FALSE && do_color &&
1415
0
      cparams_.speed_tier <= SpeedTier::kHare) {
1416
0
    Transform sg(TransformId::kRCT);
1417
0
    sg.begin_c = gi.nb_meta_channels;
1418
0
    size_t nb_rcts_to_try = 0;
1419
0
    switch (cparams_.speed_tier) {
1420
0
      case SpeedTier::kLightning:
1421
0
      case SpeedTier::kThunder:
1422
0
      case SpeedTier::kFalcon:
1423
0
      case SpeedTier::kCheetah:
1424
0
        nb_rcts_to_try = 0;  // Just do global YCoCg
1425
0
        break;
1426
0
      case SpeedTier::kHare:
1427
0
        nb_rcts_to_try = 4;
1428
0
        break;
1429
0
      case SpeedTier::kWombat:
1430
0
        nb_rcts_to_try = 5;
1431
0
        break;
1432
0
      case SpeedTier::kSquirrel:
1433
0
        nb_rcts_to_try = 7;
1434
0
        break;
1435
0
      case SpeedTier::kKitten:
1436
0
        nb_rcts_to_try = 9;
1437
0
        break;
1438
0
      case SpeedTier::kTectonicPlate:
1439
0
      case SpeedTier::kGlacier:
1440
0
      case SpeedTier::kTortoise:
1441
0
        nb_rcts_to_try = 19;
1442
0
        break;
1443
0
    }
1444
0
    float best_cost = std::numeric_limits<float>::max();
1445
0
    size_t best_rct = 0;
1446
    // These should be 19 actually different transforms; the remaining ones
1447
    // are equivalent to one of these (note that the first two are do-nothing
1448
    // and YCoCg) modulo channel reordering (which only matters in the case of
1449
    // MA-with-prev-channels-properties) and/or sign (e.g. RmG vs GmR)
1450
0
    for (int i : {0 * 7 + 0, 0 * 7 + 6, 0 * 7 + 5, 1 * 7 + 3, 3 * 7 + 5,
1451
0
                  5 * 7 + 5, 1 * 7 + 5, 2 * 7 + 5, 1 * 7 + 1, 0 * 7 + 4,
1452
0
                  1 * 7 + 2, 2 * 7 + 1, 2 * 7 + 2, 2 * 7 + 3, 4 * 7 + 4,
1453
0
                  4 * 7 + 5, 0 * 7 + 2, 0 * 7 + 1, 0 * 7 + 3}) {
1454
0
      if (nb_rcts_to_try == 0) break;
1455
0
      sg.rct_type = i;
1456
0
      nb_rcts_to_try--;
1457
0
      if (do_transform(gi, sg, weighted::Header())) {
1458
0
        float cost = EstimateCost(gi);
1459
0
        if (cost < best_cost) {
1460
0
          best_rct = i;
1461
0
          best_cost = cost;
1462
0
        }
1463
0
        Transform t = gi.transform.back();
1464
0
        JXL_RETURN_IF_ERROR(t.Inverse(gi, weighted::Header(), nullptr));
1465
0
        gi.transform.pop_back();
1466
0
      }
1467
0
    }
1468
    // Apply the best RCT to the image for future encoding.
1469
0
    sg.rct_type = best_rct;
1470
0
    do_transform(gi, sg, weighted::Header());
1471
0
  } else {
1472
    // No need to try anything, just use the default options.
1473
0
  }
1474
0
  size_t nb_wp_modes = 1;
1475
0
  if (cparams_.speed_tier <= SpeedTier::kTortoise) {
1476
0
    nb_wp_modes = 5;
1477
0
  } else if (cparams_.speed_tier <= SpeedTier::kKitten) {
1478
0
    nb_wp_modes = 2;
1479
0
  }
1480
0
  if (nb_wp_modes > 1 &&
1481
0
      (stream_options_[stream_id].predictor == Predictor::Weighted ||
1482
0
       stream_options_[stream_id].predictor == Predictor::Best ||
1483
0
       stream_options_[stream_id].predictor == Predictor::Variable)) {
1484
0
    float best_cost = std::numeric_limits<float>::max();
1485
0
    stream_options_[stream_id].wp_mode = 0;
1486
0
    for (size_t i = 0; i < nb_wp_modes; i++) {
1487
0
      float cost = EstimateWPCost(gi, i);
1488
0
      if (cost < best_cost) {
1489
0
        best_cost = cost;
1490
0
        stream_options_[stream_id].wp_mode = i;
1491
0
      }
1492
0
    }
1493
0
  }
1494
0
  return true;
1495
0
}
1496
1497
constexpr float q_deadzone = 0.62f;
1498
int QuantizeWP(const int32_t* qrow, size_t onerow, size_t c, size_t x, size_t y,
1499
               size_t w, weighted::State* wp_state, float value,
1500
0
               float inv_factor) {
1501
0
  float svalue = value * inv_factor;
1502
0
  PredictionResult pred =
1503
0
      PredictNoTreeWP(w, qrow + x, onerow, x, y, Predictor::Weighted, wp_state);
1504
0
  svalue -= pred.guess;
1505
0
  if (svalue > -q_deadzone && svalue < q_deadzone) svalue = 0;
1506
0
  int residual = roundf(svalue);
1507
0
  if (residual > 2 || residual < -2) residual = roundf(svalue * 0.5) * 2;
1508
0
  return residual + pred.guess;
1509
0
}
1510
1511
int QuantizeGradient(const int32_t* qrow, size_t onerow, size_t c, size_t x,
1512
0
                     size_t y, size_t w, float value, float inv_factor) {
1513
0
  float svalue = value * inv_factor;
1514
0
  PredictionResult pred =
1515
0
      PredictNoTreeNoWP(w, qrow + x, onerow, x, y, Predictor::Gradient);
1516
0
  svalue -= pred.guess;
1517
0
  if (svalue > -q_deadzone && svalue < q_deadzone) svalue = 0;
1518
0
  int residual = roundf(svalue);
1519
0
  if (residual > 2 || residual < -2) residual = roundf(svalue * 0.5) * 2;
1520
0
  return residual + pred.guess;
1521
0
}
1522
1523
Status ModularFrameEncoder::AddVarDCTDC(const FrameHeader& frame_header,
1524
                                        const Image3F& dc, const Rect& r,
1525
                                        size_t group_index, bool nl_dc,
1526
                                        PassesEncoderState* enc_state,
1527
0
                                        bool jpeg_transcode) {
1528
0
  JxlMemoryManager* memory_manager = dc.memory_manager();
1529
0
  extra_dc_precision[group_index] = nl_dc ? 1 : 0;
1530
0
  float mul = 1 << extra_dc_precision[group_index];
1531
1532
0
  size_t stream_id = ModularStreamId::VarDCTDC(group_index).ID(frame_dim_);
1533
0
  stream_options_[stream_id].max_chan_size = 0xFFFFFF;
1534
0
  stream_options_[stream_id].predictor = Predictor::Weighted;
1535
0
  stream_options_[stream_id].wp_tree_mode = ModularOptions::TreeMode::kWPOnly;
1536
0
  if (cparams_.speed_tier >= SpeedTier::kSquirrel) {
1537
0
    stream_options_[stream_id].tree_kind = ModularOptions::TreeKind::kWPFixedDC;
1538
0
  }
1539
0
  if (cparams_.speed_tier < SpeedTier::kSquirrel && !nl_dc) {
1540
0
    stream_options_[stream_id].predictor =
1541
0
        (cparams_.speed_tier < SpeedTier::kKitten ? Predictor::Variable
1542
0
                                                  : Predictor::Best);
1543
0
    stream_options_[stream_id].wp_tree_mode =
1544
0
        ModularOptions::TreeMode::kDefault;
1545
0
    stream_options_[stream_id].tree_kind = ModularOptions::TreeKind::kLearn;
1546
0
  }
1547
0
  if (cparams_.decoding_speed_tier >= 1) {
1548
0
    stream_options_[stream_id].tree_kind =
1549
0
        ModularOptions::TreeKind::kGradientFixedDC;
1550
0
  }
1551
0
  stream_options_[stream_id].histogram_params =
1552
0
      stream_options_[0].histogram_params;
1553
1554
0
  JXL_ASSIGN_OR_RETURN(
1555
0
      stream_images_[stream_id],
1556
0
      Image::Create(memory_manager, r.xsize(), r.ysize(), 8, 3));
1557
0
  const ColorCorrelation& color_correlation = enc_state->shared.cmap.base();
1558
0
  if (nl_dc && stream_options_[stream_id].tree_kind ==
1559
0
                   ModularOptions::TreeKind::kGradientFixedDC) {
1560
0
    JXL_ENSURE(frame_header.chroma_subsampling.Is444());
1561
0
    for (size_t c : {1, 0, 2}) {
1562
0
      float inv_factor = enc_state->shared.quantizer.GetInvDcStep(c) * mul;
1563
0
      float y_factor = enc_state->shared.quantizer.GetDcStep(1) / mul;
1564
0
      float cfl_factor = color_correlation.DCFactors()[c];
1565
0
      for (size_t y = 0; y < r.ysize(); y++) {
1566
0
        int32_t* quant_row =
1567
0
            stream_images_[stream_id].channel[c < 2 ? c ^ 1 : c].plane.Row(y);
1568
0
        size_t stride = stream_images_[stream_id]
1569
0
                            .channel[c < 2 ? c ^ 1 : c]
1570
0
                            .plane.PixelsPerRow();
1571
0
        const float* row = r.ConstPlaneRow(dc, c, y);
1572
0
        if (c == 1) {
1573
0
          for (size_t x = 0; x < r.xsize(); x++) {
1574
0
            quant_row[x] = QuantizeGradient(quant_row, stride, c, x, y,
1575
0
                                            r.xsize(), row[x], inv_factor);
1576
0
          }
1577
0
        } else {
1578
0
          int32_t* quant_row_y =
1579
0
              stream_images_[stream_id].channel[0].plane.Row(y);
1580
0
          for (size_t x = 0; x < r.xsize(); x++) {
1581
0
            quant_row[x] = QuantizeGradient(
1582
0
                quant_row, stride, c, x, y, r.xsize(),
1583
0
                row[x] - quant_row_y[x] * (y_factor * cfl_factor), inv_factor);
1584
0
          }
1585
0
        }
1586
0
      }
1587
0
    }
1588
0
  } else if (nl_dc) {
1589
0
    JXL_ENSURE(frame_header.chroma_subsampling.Is444());
1590
0
    for (size_t c : {1, 0, 2}) {
1591
0
      float inv_factor = enc_state->shared.quantizer.GetInvDcStep(c) * mul;
1592
0
      float y_factor = enc_state->shared.quantizer.GetDcStep(1) / mul;
1593
0
      float cfl_factor = color_correlation.DCFactors()[c];
1594
0
      weighted::Header header;
1595
0
      weighted::State wp_state(header, r.xsize(), r.ysize());
1596
0
      for (size_t y = 0; y < r.ysize(); y++) {
1597
0
        int32_t* quant_row =
1598
0
            stream_images_[stream_id].channel[c < 2 ? c ^ 1 : c].plane.Row(y);
1599
0
        size_t stride = stream_images_[stream_id]
1600
0
                            .channel[c < 2 ? c ^ 1 : c]
1601
0
                            .plane.PixelsPerRow();
1602
0
        const float* row = r.ConstPlaneRow(dc, c, y);
1603
0
        if (c == 1) {
1604
0
          for (size_t x = 0; x < r.xsize(); x++) {
1605
0
            quant_row[x] = QuantizeWP(quant_row, stride, c, x, y, r.xsize(),
1606
0
                                      &wp_state, row[x], inv_factor);
1607
0
            wp_state.UpdateErrors(quant_row[x], x, y, r.xsize());
1608
0
          }
1609
0
        } else {
1610
0
          int32_t* quant_row_y =
1611
0
              stream_images_[stream_id].channel[0].plane.Row(y);
1612
0
          for (size_t x = 0; x < r.xsize(); x++) {
1613
0
            quant_row[x] = QuantizeWP(
1614
0
                quant_row, stride, c, x, y, r.xsize(), &wp_state,
1615
0
                row[x] - quant_row_y[x] * (y_factor * cfl_factor), inv_factor);
1616
0
            wp_state.UpdateErrors(quant_row[x], x, y, r.xsize());
1617
0
          }
1618
0
        }
1619
0
      }
1620
0
    }
1621
0
  } else if (frame_header.chroma_subsampling.Is444()) {
1622
0
    for (size_t c : {1, 0, 2}) {
1623
0
      float inv_factor = enc_state->shared.quantizer.GetInvDcStep(c) * mul;
1624
0
      float y_factor = enc_state->shared.quantizer.GetDcStep(1) / mul;
1625
0
      float cfl_factor = color_correlation.DCFactors()[c];
1626
0
      for (size_t y = 0; y < r.ysize(); y++) {
1627
0
        int32_t* quant_row =
1628
0
            stream_images_[stream_id].channel[c < 2 ? c ^ 1 : c].plane.Row(y);
1629
0
        const float* row = r.ConstPlaneRow(dc, c, y);
1630
0
        if (c == 1) {
1631
0
          for (size_t x = 0; x < r.xsize(); x++) {
1632
0
            quant_row[x] = roundf(row[x] * inv_factor);
1633
0
          }
1634
0
        } else {
1635
0
          int32_t* quant_row_y =
1636
0
              stream_images_[stream_id].channel[0].plane.Row(y);
1637
0
          for (size_t x = 0; x < r.xsize(); x++) {
1638
0
            quant_row[x] =
1639
0
                roundf((row[x] - quant_row_y[x] * (y_factor * cfl_factor)) *
1640
0
                       inv_factor);
1641
0
          }
1642
0
        }
1643
0
      }
1644
0
    }
1645
0
  } else {
1646
0
    for (size_t c : {1, 0, 2}) {
1647
0
      Rect rect(r.x0() >> frame_header.chroma_subsampling.HShift(c),
1648
0
                r.y0() >> frame_header.chroma_subsampling.VShift(c),
1649
0
                r.xsize() >> frame_header.chroma_subsampling.HShift(c),
1650
0
                r.ysize() >> frame_header.chroma_subsampling.VShift(c));
1651
0
      float inv_factor = enc_state->shared.quantizer.GetInvDcStep(c) * mul;
1652
0
      size_t ys = rect.ysize();
1653
0
      size_t xs = rect.xsize();
1654
0
      Channel& ch = stream_images_[stream_id].channel[c < 2 ? c ^ 1 : c];
1655
0
      ch.w = xs;
1656
0
      ch.h = ys;
1657
0
      JXL_RETURN_IF_ERROR(ch.shrink());
1658
0
      for (size_t y = 0; y < ys; y++) {
1659
0
        int32_t* quant_row = ch.plane.Row(y);
1660
0
        const float* row = rect.ConstPlaneRow(dc, c, y);
1661
0
        for (size_t x = 0; x < xs; x++) {
1662
0
          quant_row[x] = roundf(row[x] * inv_factor);
1663
0
        }
1664
0
      }
1665
0
    }
1666
0
  }
1667
1668
0
  DequantDC(r, &enc_state->shared.dc_storage, &enc_state->shared.quant_dc,
1669
0
            stream_images_[stream_id], enc_state->shared.quantizer.MulDC(),
1670
0
            1.0 / mul, color_correlation.DCFactors(),
1671
0
            frame_header.chroma_subsampling, enc_state->shared.block_ctx_map);
1672
0
  return true;
1673
0
}
1674
1675
Status ModularFrameEncoder::AddACMetadata(const Rect& r, size_t group_index,
1676
                                          bool jpeg_transcode,
1677
0
                                          PassesEncoderState* enc_state) {
1678
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
1679
0
  size_t stream_id = ModularStreamId::ACMetadata(group_index).ID(frame_dim_);
1680
0
  stream_options_[stream_id].max_chan_size = 0xFFFFFF;
1681
0
  if (stream_options_[stream_id].predictor != Predictor::Weighted) {
1682
0
    stream_options_[stream_id].wp_tree_mode = ModularOptions::TreeMode::kNoWP;
1683
0
  }
1684
0
  if (jpeg_transcode) {
1685
0
    stream_options_[stream_id].tree_kind =
1686
0
        ModularOptions::TreeKind::kJpegTranscodeACMeta;
1687
0
  } else if (cparams_.speed_tier >= SpeedTier::kFalcon) {
1688
0
    stream_options_[stream_id].tree_kind =
1689
0
        ModularOptions::TreeKind::kFalconACMeta;
1690
0
  } else if (cparams_.speed_tier > SpeedTier::kKitten) {
1691
0
    stream_options_[stream_id].tree_kind = ModularOptions::TreeKind::kACMeta;
1692
0
  }
1693
  // If we are using a non-constant CfL field, and are in a slow enough mode,
1694
  // re-enable tree computation for it.
1695
0
  if (cparams_.speed_tier < SpeedTier::kSquirrel &&
1696
0
      cparams_.force_cfl_jpeg_recompression) {
1697
0
    stream_options_[stream_id].tree_kind = ModularOptions::TreeKind::kLearn;
1698
0
  }
1699
0
  stream_options_[stream_id].histogram_params =
1700
0
      stream_options_[0].histogram_params;
1701
  // YToX, YToB, ACS + QF, EPF
1702
0
  Image& image = stream_images_[stream_id];
1703
0
  JXL_ASSIGN_OR_RETURN(
1704
0
      image, Image::Create(memory_manager, r.xsize(), r.ysize(), 8, 4));
1705
0
  static_assert(kColorTileDimInBlocks == 8, "Color tile size changed");
1706
0
  Rect cr(r.x0() >> 3, r.y0() >> 3, (r.xsize() + 7) >> 3, (r.ysize() + 7) >> 3);
1707
0
  JXL_ASSIGN_OR_RETURN(
1708
0
      image.channel[0],
1709
0
      Channel::Create(memory_manager, cr.xsize(), cr.ysize(), 3, 3));
1710
0
  JXL_ASSIGN_OR_RETURN(
1711
0
      image.channel[1],
1712
0
      Channel::Create(memory_manager, cr.xsize(), cr.ysize(), 3, 3));
1713
0
  JXL_ASSIGN_OR_RETURN(
1714
0
      image.channel[2],
1715
0
      Channel::Create(memory_manager, r.xsize() * r.ysize(), 2, 0, 0));
1716
0
  JXL_RETURN_IF_ERROR(ConvertPlaneAndClamp(cr, enc_state->shared.cmap.ytox_map,
1717
0
                                           Rect(image.channel[0].plane),
1718
0
                                           &image.channel[0].plane));
1719
0
  JXL_RETURN_IF_ERROR(ConvertPlaneAndClamp(cr, enc_state->shared.cmap.ytob_map,
1720
0
                                           Rect(image.channel[1].plane),
1721
0
                                           &image.channel[1].plane));
1722
0
  size_t num = 0;
1723
0
  for (size_t y = 0; y < r.ysize(); y++) {
1724
0
    AcStrategyRow row_acs = enc_state->shared.ac_strategy.ConstRow(r, y);
1725
0
    const int32_t* row_qf = r.ConstRow(enc_state->shared.raw_quant_field, y);
1726
0
    const uint8_t* row_epf = r.ConstRow(enc_state->shared.epf_sharpness, y);
1727
0
    int32_t* out_acs = image.channel[2].plane.Row(0);
1728
0
    int32_t* out_qf = image.channel[2].plane.Row(1);
1729
0
    int32_t* row_out_epf = image.channel[3].plane.Row(y);
1730
0
    for (size_t x = 0; x < r.xsize(); x++) {
1731
0
      row_out_epf[x] = row_epf[x];
1732
0
      if (!row_acs[x].IsFirstBlock()) continue;
1733
0
      out_acs[num] = row_acs[x].RawStrategy();
1734
0
      out_qf[num] = row_qf[x] - 1;
1735
0
      num++;
1736
0
    }
1737
0
  }
1738
0
  image.channel[2].w = num;
1739
0
  ac_metadata_size[group_index] = num;
1740
0
  return true;
1741
0
}
1742
1743
Status ModularFrameEncoder::EncodeQuantTable(
1744
    JxlMemoryManager* memory_manager, size_t size_x, size_t size_y,
1745
    BitWriter* writer, const QuantEncoding& encoding, size_t idx,
1746
0
    ModularFrameEncoder* modular_frame_encoder) {
1747
0
  JXL_ENSURE(encoding.qraw.qtable);
1748
0
  JXL_ENSURE(size_x * size_y * 3 == encoding.qraw.qtable->size());
1749
0
  JXL_ENSURE(idx < kNumQuantTables);
1750
0
  int* qtable = encoding.qraw.qtable->data();
1751
0
  JXL_RETURN_IF_ERROR(F16Coder::Write(encoding.qraw.qtable_den, writer));
1752
0
  if (modular_frame_encoder) {
1753
0
    JXL_ASSIGN_OR_RETURN(ModularStreamId qt, ModularStreamId::QuantTable(idx));
1754
0
    JXL_RETURN_IF_ERROR(modular_frame_encoder->EncodeStream(
1755
0
        writer, nullptr, LayerType::Header, qt));
1756
0
    return true;
1757
0
  }
1758
0
  JXL_ASSIGN_OR_RETURN(Image image,
1759
0
                       Image::Create(memory_manager, size_x, size_y, 8, 3));
1760
0
  for (size_t c = 0; c < 3; c++) {
1761
0
    for (size_t y = 0; y < size_y; y++) {
1762
0
      int32_t* JXL_RESTRICT row = image.channel[c].Row(y);
1763
0
      for (size_t x = 0; x < size_x; x++) {
1764
0
        row[x] = qtable[c * size_x * size_y + y * size_x + x];
1765
0
      }
1766
0
    }
1767
0
  }
1768
0
  ModularOptions cfopts;
1769
0
  JXL_RETURN_IF_ERROR(ModularGenericCompress(image, cfopts, writer));
1770
0
  return true;
1771
0
}
1772
1773
Status ModularFrameEncoder::AddQuantTable(size_t size_x, size_t size_y,
1774
                                          const QuantEncoding& encoding,
1775
0
                                          size_t idx) {
1776
0
  JXL_ENSURE(idx < kNumQuantTables);
1777
0
  JXL_ASSIGN_OR_RETURN(ModularStreamId qt, ModularStreamId::QuantTable(idx));
1778
0
  size_t stream_id = qt.ID(frame_dim_);
1779
0
  JXL_ENSURE(encoding.qraw.qtable);
1780
0
  JXL_ENSURE(size_x * size_y * 3 == encoding.qraw.qtable->size());
1781
0
  int* qtable = encoding.qraw.qtable->data();
1782
0
  Image& image = stream_images_[stream_id];
1783
0
  JxlMemoryManager* memory_manager = image.memory_manager();
1784
0
  JXL_ASSIGN_OR_RETURN(image,
1785
0
                       Image::Create(memory_manager, size_x, size_y, 8, 3));
1786
0
  for (size_t c = 0; c < 3; c++) {
1787
0
    for (size_t y = 0; y < size_y; y++) {
1788
0
      int32_t* JXL_RESTRICT row = image.channel[c].Row(y);
1789
0
      for (size_t x = 0; x < size_x; x++) {
1790
0
        row[x] = qtable[c * size_x * size_y + y * size_x + x];
1791
0
      }
1792
0
    }
1793
0
  }
1794
0
  return true;
1795
0
}
1796
}  // namespace jxl