Coverage Report

Created: 2025-06-22 08:04

/src/libjxl/lib/jxl/enc_frame.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_frame.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <array>
12
#include <cmath>
13
#include <cstddef>
14
#include <cstdint>
15
#include <memory>
16
#include <numeric>
17
#include <utility>
18
#include <vector>
19
20
#include "lib/jxl/ac_context.h"
21
#include "lib/jxl/ac_strategy.h"
22
#include "lib/jxl/base/bits.h"
23
#include "lib/jxl/base/common.h"
24
#include "lib/jxl/base/compiler_specific.h"
25
#include "lib/jxl/base/data_parallel.h"
26
#include "lib/jxl/base/override.h"
27
#include "lib/jxl/base/printf_macros.h"
28
#include "lib/jxl/base/rect.h"
29
#include "lib/jxl/base/status.h"
30
#include "lib/jxl/chroma_from_luma.h"
31
#include "lib/jxl/coeff_order.h"
32
#include "lib/jxl/coeff_order_fwd.h"
33
#include "lib/jxl/color_encoding_internal.h"
34
#include "lib/jxl/common.h"  // kMaxNumPasses
35
#include "lib/jxl/dct_util.h"
36
#include "lib/jxl/dec_external_image.h"
37
#include "lib/jxl/enc_ac_strategy.h"
38
#include "lib/jxl/enc_adaptive_quantization.h"
39
#include "lib/jxl/enc_ans.h"
40
#include "lib/jxl/enc_aux_out.h"
41
#include "lib/jxl/enc_bit_writer.h"
42
#include "lib/jxl/enc_cache.h"
43
#include "lib/jxl/enc_chroma_from_luma.h"
44
#include "lib/jxl/enc_coeff_order.h"
45
#include "lib/jxl/enc_context_map.h"
46
#include "lib/jxl/enc_entropy_coder.h"
47
#include "lib/jxl/enc_external_image.h"
48
#include "lib/jxl/enc_fields.h"
49
#include "lib/jxl/enc_group.h"
50
#include "lib/jxl/enc_heuristics.h"
51
#include "lib/jxl/enc_modular.h"
52
#include "lib/jxl/enc_noise.h"
53
#include "lib/jxl/enc_params.h"
54
#include "lib/jxl/enc_patch_dictionary.h"
55
#include "lib/jxl/enc_photon_noise.h"
56
#include "lib/jxl/enc_quant_weights.h"
57
#include "lib/jxl/enc_splines.h"
58
#include "lib/jxl/enc_toc.h"
59
#include "lib/jxl/enc_xyb.h"
60
#include "lib/jxl/fields.h"
61
#include "lib/jxl/frame_dimensions.h"
62
#include "lib/jxl/frame_header.h"
63
#include "lib/jxl/image.h"
64
#include "lib/jxl/image_bundle.h"
65
#include "lib/jxl/image_ops.h"
66
#include "lib/jxl/jpeg/enc_jpeg_data.h"
67
#include "lib/jxl/loop_filter.h"
68
#include "lib/jxl/modular/options.h"
69
#include "lib/jxl/quant_weights.h"
70
#include "lib/jxl/quantizer.h"
71
#include "lib/jxl/splines.h"
72
#include "lib/jxl/toc.h"
73
74
namespace jxl {
75
76
0
Status ParamsPostInit(CompressParams* p) {
77
0
  if (!p->manual_noise.empty() &&
78
0
      p->manual_noise.size() != NoiseParams::kNumNoisePoints) {
79
0
    return JXL_FAILURE("Invalid number of noise lut entries");
80
0
  }
81
0
  if (!p->manual_xyb_factors.empty() && p->manual_xyb_factors.size() != 3) {
82
0
    return JXL_FAILURE("Invalid number of XYB quantization factors");
83
0
  }
84
0
  if (!p->modular_mode && p->butteraugli_distance == 0.0) {
85
0
    p->butteraugli_distance = kMinButteraugliDistance;
86
0
  }
87
0
  if (p->original_butteraugli_distance == -1.0) {
88
0
    p->original_butteraugli_distance = p->butteraugli_distance;
89
0
  }
90
0
  if (p->resampling <= 0) {
91
0
    p->resampling = 1;
92
    // For very low bit rates, using 2x2 resampling gives better results on
93
    // most photographic images, with an adjusted butteraugli score chosen to
94
    // give roughly the same amount of bits per pixel.
95
0
    if (!p->already_downsampled && p->butteraugli_distance >= 20) {
96
0
      p->resampling = 2;
97
0
      p->butteraugli_distance = 6 + ((p->butteraugli_distance - 20) * 0.25);
98
0
    }
99
0
  }
100
0
  if (p->ec_resampling <= 0) {
101
0
    p->ec_resampling = p->resampling;
102
0
  }
103
0
  return true;
104
0
}
105
106
namespace {
107
108
template <typename T>
109
uint32_t GetBitDepth(JxlBitDepth bit_depth, const T& metadata,
110
0
                     JxlPixelFormat format) {
111
0
  if (bit_depth.type == JXL_BIT_DEPTH_FROM_PIXEL_FORMAT) {
112
0
    return BitsPerChannel(format.data_type);
113
0
  } else if (bit_depth.type == JXL_BIT_DEPTH_FROM_CODESTREAM) {
114
0
    return metadata.bit_depth.bits_per_sample;
115
0
  } else if (bit_depth.type == JXL_BIT_DEPTH_CUSTOM) {
116
0
    return bit_depth.bits_per_sample;
117
0
  } else {
118
0
    return 0;
119
0
  }
120
0
}
Unexecuted instantiation: enc_frame.cc:unsigned int jxl::(anonymous namespace)::GetBitDepth<jxl::ImageMetadata>(JxlBitDepth, jxl::ImageMetadata const&, JxlPixelFormat)
Unexecuted instantiation: enc_frame.cc:unsigned int jxl::(anonymous namespace)::GetBitDepth<jxl::ExtraChannelInfo>(JxlBitDepth, jxl::ExtraChannelInfo const&, JxlPixelFormat)
121
122
Status CopyColorChannels(JxlChunkedFrameInputSource input, Rect rect,
123
                         const FrameInfo& frame_info,
124
                         const ImageMetadata& metadata, ThreadPool* pool,
125
                         Image3F* color, ImageF* alpha,
126
0
                         bool* has_interleaved_alpha) {
127
0
  JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
128
0
  input.get_color_channels_pixel_format(input.opaque, &format);
129
0
  *has_interleaved_alpha = format.num_channels == 2 || format.num_channels == 4;
130
0
  size_t bits_per_sample =
131
0
      GetBitDepth(frame_info.image_bit_depth, metadata, format);
132
0
  size_t row_offset;
133
0
  auto buffer = GetColorBuffer(input, rect.x0(), rect.y0(), rect.xsize(),
134
0
                               rect.ysize(), &row_offset);
135
0
  if (!buffer) {
136
0
    return JXL_FAILURE("no buffer for color channels given");
137
0
  }
138
0
  size_t color_channels = frame_info.ib_needs_color_transform
139
0
                              ? metadata.color_encoding.Channels()
140
0
                              : 3;
141
0
  if (format.num_channels < color_channels) {
142
0
    return JXL_FAILURE("Expected %" PRIuS
143
0
                       " color channels, received only %u channels",
144
0
                       color_channels, format.num_channels);
145
0
  }
146
0
  const uint8_t* data = reinterpret_cast<const uint8_t*>(buffer.get());
147
0
  for (size_t c = 0; c < color_channels; ++c) {
148
0
    JXL_RETURN_IF_ERROR(ConvertFromExternalNoSizeCheck(
149
0
        data, rect.xsize(), rect.ysize(), row_offset, bits_per_sample, format,
150
0
        c, pool, &color->Plane(c)));
151
0
  }
152
0
  if (color_channels == 1) {
153
0
    JXL_RETURN_IF_ERROR(CopyImageTo(color->Plane(0), &color->Plane(1)));
154
0
    JXL_RETURN_IF_ERROR(CopyImageTo(color->Plane(0), &color->Plane(2)));
155
0
  }
156
0
  if (alpha) {
157
0
    if (*has_interleaved_alpha) {
158
0
      JXL_RETURN_IF_ERROR(ConvertFromExternalNoSizeCheck(
159
0
          data, rect.xsize(), rect.ysize(), row_offset, bits_per_sample, format,
160
0
          format.num_channels - 1, pool, alpha));
161
0
    } else {
162
      // if alpha is not passed, but it is expected, then assume
163
      // it is all-opaque
164
0
      FillImage(1.0f, alpha);
165
0
    }
166
0
  }
167
0
  return true;
168
0
}
169
170
Status CopyExtraChannels(JxlChunkedFrameInputSource input, Rect rect,
171
                         const FrameInfo& frame_info,
172
                         const ImageMetadata& metadata,
173
                         bool has_interleaved_alpha, ThreadPool* pool,
174
0
                         std::vector<ImageF>* extra_channels) {
175
0
  for (size_t ec = 0; ec < metadata.num_extra_channels; ec++) {
176
0
    if (has_interleaved_alpha &&
177
0
        metadata.extra_channel_info[ec].type == ExtraChannel::kAlpha) {
178
      // Skip this alpha channel, but still request additional alpha channels
179
      // if they exist.
180
0
      has_interleaved_alpha = false;
181
0
      continue;
182
0
    }
183
0
    JxlPixelFormat ec_format = {1, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
184
0
    input.get_extra_channel_pixel_format(input.opaque, ec, &ec_format);
185
0
    ec_format.num_channels = 1;
186
0
    size_t row_offset;
187
0
    auto buffer =
188
0
        GetExtraChannelBuffer(input, ec, rect.x0(), rect.y0(), rect.xsize(),
189
0
                              rect.ysize(), &row_offset);
190
0
    if (!buffer) {
191
0
      return JXL_FAILURE("no buffer for extra channel given");
192
0
    }
193
0
    size_t bits_per_sample = GetBitDepth(
194
0
        frame_info.image_bit_depth, metadata.extra_channel_info[ec], ec_format);
195
0
    if (!ConvertFromExternalNoSizeCheck(
196
0
            reinterpret_cast<const uint8_t*>(buffer.get()), rect.xsize(),
197
0
            rect.ysize(), row_offset, bits_per_sample, ec_format, 0, pool,
198
0
            &(*extra_channels)[ec])) {
199
0
      return JXL_FAILURE("Failed to set buffer for extra channel");
200
0
    }
201
0
  }
202
0
  return true;
203
0
}
204
205
void SetProgressiveMode(const CompressParams& cparams,
206
0
                        ProgressiveSplitter* progressive_splitter) {
207
0
  constexpr PassDefinition progressive_passes_dc_vlf_lf_full_ac[] = {
208
0
      {/*num_coefficients=*/2, /*shift=*/0,
209
0
       /*suitable_for_downsampling_of_at_least=*/4},
210
0
      {/*num_coefficients=*/3, /*shift=*/0,
211
0
       /*suitable_for_downsampling_of_at_least=*/2},
212
0
      {/*num_coefficients=*/8, /*shift=*/0,
213
0
       /*suitable_for_downsampling_of_at_least=*/0},
214
0
  };
215
0
  constexpr PassDefinition progressive_passes_dc_quant_ac_full_ac[] = {
216
0
      {/*num_coefficients=*/8, /*shift=*/1,
217
0
       /*suitable_for_downsampling_of_at_least=*/2},
218
0
      {/*num_coefficients=*/8, /*shift=*/0,
219
0
       /*suitable_for_downsampling_of_at_least=*/0},
220
0
  };
221
0
  bool progressive_mode = ApplyOverride(cparams.progressive_mode, false);
222
0
  bool qprogressive_mode = ApplyOverride(cparams.qprogressive_mode, false);
223
0
  if (cparams.custom_progressive_mode) {
224
0
    progressive_splitter->SetProgressiveMode(*cparams.custom_progressive_mode);
225
0
  } else if (qprogressive_mode) {
226
0
    progressive_splitter->SetProgressiveMode(
227
0
        ProgressiveMode{progressive_passes_dc_quant_ac_full_ac});
228
0
  } else if (progressive_mode) {
229
0
    progressive_splitter->SetProgressiveMode(
230
0
        ProgressiveMode{progressive_passes_dc_vlf_lf_full_ac});
231
0
  }
232
0
}
233
234
0
uint64_t FrameFlagsFromParams(const CompressParams& cparams) {
235
0
  uint64_t flags = 0;
236
237
0
  const float dist = cparams.butteraugli_distance;
238
239
  // We don't add noise at low butteraugli distances because the original
240
  // noise is stored within the compressed image and adding noise makes things
241
  // worse.
242
0
  if (ApplyOverride(cparams.noise, dist >= kMinButteraugliForNoise) ||
243
0
      cparams.photon_noise_iso > 0 ||
244
0
      cparams.manual_noise.size() == NoiseParams::kNumNoisePoints) {
245
0
    flags |= FrameHeader::kNoise;
246
0
  }
247
248
0
  if (cparams.progressive_dc > 0 && cparams.modular_mode == false) {
249
0
    flags |= FrameHeader::kUseDcFrame;
250
0
  }
251
252
0
  return flags;
253
0
}
254
255
Status LoopFilterFromParams(const CompressParams& cparams, bool streaming_mode,
256
0
                            FrameHeader* JXL_RESTRICT frame_header) {
257
0
  LoopFilter* loop_filter = &frame_header->loop_filter;
258
259
  // Gaborish defaults to enabled in Hare or slower.
260
0
  loop_filter->gab = ApplyOverride(
261
0
      cparams.gaborish, cparams.speed_tier <= SpeedTier::kHare &&
262
0
                            frame_header->encoding == FrameEncoding::kVarDCT &&
263
0
                            cparams.decoding_speed_tier < 4 &&
264
0
                            cparams.butteraugli_distance > 0.5f &&
265
0
                            !cparams.disable_perceptual_optimizations);
266
267
0
  if (cparams.epf != -1) {
268
0
    loop_filter->epf_iters = cparams.epf;
269
0
  } else if (cparams.disable_perceptual_optimizations) {
270
0
    loop_filter->epf_iters = 0;
271
0
    return true;
272
0
  } else {
273
0
    if (frame_header->encoding == FrameEncoding::kModular) {
274
0
      loop_filter->epf_iters = 0;
275
0
    } else {
276
0
      constexpr float kThresholds[3] = {0.7, 1.5, 4.0};
277
0
      loop_filter->epf_iters = 0;
278
0
      if (cparams.decoding_speed_tier < 3) {
279
0
        for (size_t i = cparams.decoding_speed_tier == 2 ? 1 : 0; i < 3; i++) {
280
0
          if (cparams.butteraugli_distance >= kThresholds[i]) {
281
0
            loop_filter->epf_iters++;
282
0
          }
283
0
        }
284
0
      }
285
0
    }
286
0
  }
287
  // Strength of EPF in modular mode.
288
0
  if (frame_header->encoding == FrameEncoding::kModular &&
289
0
      !cparams.IsLossless()) {
290
    // TODO(veluca): this formula is nonsense.
291
0
    loop_filter->epf_sigma_for_modular =
292
0
        std::max(cparams.butteraugli_distance, 1.0f);
293
0
  }
294
0
  if (frame_header->encoding == FrameEncoding::kModular &&
295
0
      cparams.lossy_palette) {
296
0
    loop_filter->epf_sigma_for_modular = 1.0f;
297
0
  }
298
299
0
  return true;
300
0
}
301
302
Status MakeFrameHeader(size_t xsize, size_t ysize,
303
                       const CompressParams& cparams,
304
                       const ProgressiveSplitter& progressive_splitter,
305
                       const FrameInfo& frame_info,
306
                       const jpeg::JPEGData* jpeg_data, bool streaming_mode,
307
0
                       FrameHeader* JXL_RESTRICT frame_header) {
308
0
  frame_header->nonserialized_is_preview = frame_info.is_preview;
309
0
  frame_header->is_last = frame_info.is_last;
310
0
  frame_header->save_before_color_transform =
311
0
      frame_info.save_before_color_transform;
312
0
  frame_header->frame_type = frame_info.frame_type;
313
0
  frame_header->name = frame_info.name;
314
315
0
  JXL_RETURN_IF_ERROR(progressive_splitter.InitPasses(&frame_header->passes));
316
317
0
  if (cparams.modular_mode) {
318
0
    frame_header->encoding = FrameEncoding::kModular;
319
0
    if (cparams.modular_group_size_shift == -1) {
320
0
      frame_header->group_size_shift = 1;
321
      // no point using groups when only one group is full and the others are
322
      // less than half full: multithreading will not really help much, while
323
      // compression does suffer
324
0
      if (xsize <= 400 && ysize <= 400) {
325
0
        frame_header->group_size_shift = 2;
326
0
      }
327
0
    } else {
328
0
      frame_header->group_size_shift = cparams.modular_group_size_shift;
329
0
    }
330
0
  }
331
332
0
  if (jpeg_data) {
333
    // we are transcoding a JPEG, so we don't get to choose
334
0
    frame_header->encoding = FrameEncoding::kVarDCT;
335
0
    frame_header->x_qm_scale = 2;
336
0
    frame_header->b_qm_scale = 2;
337
0
    JXL_RETURN_IF_ERROR(SetChromaSubsamplingFromJpegData(
338
0
        *jpeg_data, &frame_header->chroma_subsampling));
339
0
    JXL_RETURN_IF_ERROR(SetColorTransformFromJpegData(
340
0
        *jpeg_data, &frame_header->color_transform));
341
0
  } else {
342
0
    frame_header->color_transform = cparams.color_transform;
343
0
    if (!cparams.modular_mode &&
344
0
        (frame_header->chroma_subsampling.MaxHShift() != 0 ||
345
0
         frame_header->chroma_subsampling.MaxVShift() != 0)) {
346
0
      return JXL_FAILURE(
347
0
          "Chroma subsampling is not supported in VarDCT mode when not "
348
0
          "recompressing JPEGs");
349
0
    }
350
0
  }
351
0
  if (frame_header->color_transform != ColorTransform::kYCbCr &&
352
0
      (frame_header->chroma_subsampling.MaxHShift() != 0 ||
353
0
       frame_header->chroma_subsampling.MaxVShift() != 0)) {
354
0
    return JXL_FAILURE(
355
0
        "Chroma subsampling is not supported when color transform is not "
356
0
        "YCbCr");
357
0
  }
358
359
0
  frame_header->flags = FrameFlagsFromParams(cparams);
360
  // Non-photon noise is not supported in the Modular encoder for now.
361
0
  if (frame_header->encoding != FrameEncoding::kVarDCT &&
362
0
      cparams.photon_noise_iso == 0 && cparams.manual_noise.empty()) {
363
0
    frame_header->UpdateFlag(false, FrameHeader::Flags::kNoise);
364
0
  }
365
366
0
  JXL_RETURN_IF_ERROR(
367
0
      LoopFilterFromParams(cparams, streaming_mode, frame_header));
368
369
0
  frame_header->dc_level = frame_info.dc_level;
370
0
  if (frame_header->dc_level > 2) {
371
    // With 3 or more progressive_dc frames, the implementation does not yet
372
    // work, see enc_cache.cc.
373
0
    return JXL_FAILURE("progressive_dc > 2 is not yet supported");
374
0
  }
375
0
  if (cparams.progressive_dc > 0 &&
376
0
      (cparams.ec_resampling != 1 || cparams.resampling != 1)) {
377
0
    return JXL_FAILURE("Resampling not supported with DC frames");
378
0
  }
379
0
  if (cparams.resampling != 1 && cparams.resampling != 2 &&
380
0
      cparams.resampling != 4 && cparams.resampling != 8) {
381
0
    return JXL_FAILURE("Invalid resampling factor");
382
0
  }
383
0
  if (cparams.ec_resampling != 1 && cparams.ec_resampling != 2 &&
384
0
      cparams.ec_resampling != 4 && cparams.ec_resampling != 8) {
385
0
    return JXL_FAILURE("Invalid ec_resampling factor");
386
0
  }
387
  // Resized frames.
388
0
  if (frame_info.frame_type != FrameType::kDCFrame) {
389
0
    frame_header->frame_origin = frame_info.origin;
390
0
    size_t ups = 1;
391
0
    if (cparams.already_downsampled) ups = cparams.resampling;
392
393
    // TODO(lode): this is not correct in case of odd original image sizes in
394
    // combination with cparams.already_downsampled. Likely these values should
395
    // be set to respectively frame_header->default_xsize() and
396
    // frame_header->default_ysize() instead, the original (non downsampled)
397
    // intended decoded image dimensions. But it may be more subtle than that
398
    // if combined with crop. This issue causes custom_size_or_origin to be
399
    // incorrectly set to true in case of already_downsampled with odd output
400
    // image size when no cropping is used.
401
0
    frame_header->frame_size.xsize = xsize * ups;
402
0
    frame_header->frame_size.ysize = ysize * ups;
403
0
    if (frame_info.origin.x0 != 0 || frame_info.origin.y0 != 0 ||
404
0
        frame_header->frame_size.xsize != frame_header->default_xsize() ||
405
0
        frame_header->frame_size.ysize != frame_header->default_ysize()) {
406
0
      frame_header->custom_size_or_origin = true;
407
0
    }
408
0
  }
409
  // Upsampling.
410
0
  frame_header->upsampling = cparams.resampling;
411
0
  const std::vector<ExtraChannelInfo>& extra_channels =
412
0
      frame_header->nonserialized_metadata->m.extra_channel_info;
413
0
  frame_header->extra_channel_upsampling.clear();
414
0
  frame_header->extra_channel_upsampling.resize(extra_channels.size(),
415
0
                                                cparams.ec_resampling);
416
0
  frame_header->save_as_reference = frame_info.save_as_reference;
417
418
  // Set blending-related information.
419
0
  if (frame_info.blend || frame_header->custom_size_or_origin) {
420
    // Set blend_channel to the first alpha channel. These values are only
421
    // encoded in case a blend mode involving alpha is used and there are more
422
    // than one extra channels.
423
0
    size_t index = 0;
424
0
    if (frame_info.alpha_channel == -1) {
425
0
      if (extra_channels.size() > 1) {
426
0
        for (size_t i = 0; i < extra_channels.size(); i++) {
427
0
          if (extra_channels[i].type == ExtraChannel::kAlpha) {
428
0
            index = i;
429
0
            break;
430
0
          }
431
0
        }
432
0
      }
433
0
    } else {
434
0
      index = static_cast<size_t>(frame_info.alpha_channel);
435
0
      JXL_ENSURE(index == 0 || index < extra_channels.size());
436
0
    }
437
0
    frame_header->blending_info.alpha_channel = index;
438
0
    frame_header->blending_info.mode =
439
0
        frame_info.blend ? frame_info.blendmode : BlendMode::kReplace;
440
0
    frame_header->blending_info.source = frame_info.source;
441
0
    frame_header->blending_info.clamp = frame_info.clamp;
442
0
    const auto& extra_channel_info = frame_info.extra_channel_blending_info;
443
0
    for (size_t i = 0; i < extra_channels.size(); i++) {
444
0
      if (i < extra_channel_info.size()) {
445
0
        frame_header->extra_channel_blending_info[i] = extra_channel_info[i];
446
0
      } else {
447
0
        frame_header->extra_channel_blending_info[i].alpha_channel = index;
448
0
        BlendMode default_blend = frame_info.blendmode;
449
0
        if (extra_channels[i].type != ExtraChannel::kBlack && i != index) {
450
          // K needs to be blended, spot colors and other stuff gets added
451
0
          default_blend = BlendMode::kAdd;
452
0
        }
453
0
        frame_header->extra_channel_blending_info[i].mode =
454
0
            frame_info.blend ? default_blend : BlendMode::kReplace;
455
0
        frame_header->extra_channel_blending_info[i].source = 1;
456
0
      }
457
0
    }
458
0
  }
459
460
0
  frame_header->animation_frame.duration = frame_info.duration;
461
0
  frame_header->animation_frame.timecode = frame_info.timecode;
462
463
0
  if (jpeg_data) {
464
0
    frame_header->UpdateFlag(false, FrameHeader::kUseDcFrame);
465
0
    frame_header->UpdateFlag(true, FrameHeader::kSkipAdaptiveDCSmoothing);
466
0
  }
467
468
0
  return true;
469
0
}
470
471
// Invisible (alpha = 0) pixels tend to be a mess in optimized PNGs.
472
// Since they have no visual impact whatsoever, we can replace them with
473
// something that compresses better and reduces artifacts near the edges. This
474
// does some kind of smooth stuff that seems to work.
475
// Replace invisible pixels with a weighted average of the pixel to the left,
476
// the pixel to the topright, and non-invisible neighbours.
477
// Produces downward-blurry smears, with in the upwards direction only a 1px
478
// edge duplication but not more. It would probably be better to smear in all
479
// directions. That requires an alpha-weighed convolution with a large enough
480
// kernel though, which might be overkill...
481
0
void SimplifyInvisible(Image3F* image, const ImageF& alpha, bool lossless) {
482
0
  for (size_t c = 0; c < 3; ++c) {
483
0
    for (size_t y = 0; y < image->ysize(); ++y) {
484
0
      float* JXL_RESTRICT row = image->PlaneRow(c, y);
485
0
      const float* JXL_RESTRICT prow =
486
0
          (y > 0 ? image->PlaneRow(c, y - 1) : nullptr);
487
0
      const float* JXL_RESTRICT nrow =
488
0
          (y + 1 < image->ysize() ? image->PlaneRow(c, y + 1) : nullptr);
489
0
      const float* JXL_RESTRICT a = alpha.Row(y);
490
0
      const float* JXL_RESTRICT pa = (y > 0 ? alpha.Row(y - 1) : nullptr);
491
0
      const float* JXL_RESTRICT na =
492
0
          (y + 1 < image->ysize() ? alpha.Row(y + 1) : nullptr);
493
0
      for (size_t x = 0; x < image->xsize(); ++x) {
494
0
        if (a[x] == 0) {
495
0
          if (lossless) {
496
0
            row[x] = 0;
497
0
            continue;
498
0
          }
499
0
          float d = 0.f;
500
0
          row[x] = 0;
501
0
          if (x > 0) {
502
0
            row[x] += row[x - 1];
503
0
            d++;
504
0
            if (a[x - 1] > 0.f) {
505
0
              row[x] += row[x - 1];
506
0
              d++;
507
0
            }
508
0
          }
509
0
          if (x + 1 < image->xsize()) {
510
0
            if (y > 0) {
511
0
              row[x] += prow[x + 1];
512
0
              d++;
513
0
            }
514
0
            if (a[x + 1] > 0.f) {
515
0
              row[x] += 2.f * row[x + 1];
516
0
              d += 2.f;
517
0
            }
518
0
            if (y > 0 && pa[x + 1] > 0.f) {
519
0
              row[x] += 2.f * prow[x + 1];
520
0
              d += 2.f;
521
0
            }
522
0
            if (y + 1 < image->ysize() && na[x + 1] > 0.f) {
523
0
              row[x] += 2.f * nrow[x + 1];
524
0
              d += 2.f;
525
0
            }
526
0
          }
527
0
          if (y > 0 && pa[x] > 0.f) {
528
0
            row[x] += 2.f * prow[x];
529
0
            d += 2.f;
530
0
          }
531
0
          if (y + 1 < image->ysize() && na[x] > 0.f) {
532
0
            row[x] += 2.f * nrow[x];
533
0
            d += 2.f;
534
0
          }
535
0
          if (d > 1.f) row[x] /= d;
536
0
        }
537
0
      }
538
0
    }
539
0
  }
540
0
}
541
542
struct PixelStatsForChromacityAdjustment {
543
  float dx = 0;
544
  float db = 0;
545
  float exposed_blue = 0;
546
0
  static float CalcPlane(const ImageF* JXL_RESTRICT plane, const Rect& rect) {
547
0
    float xmax = 0;
548
0
    float ymax = 0;
549
0
    for (size_t ty = 1; ty < rect.ysize(); ++ty) {
550
0
      for (size_t tx = 1; tx < rect.xsize(); ++tx) {
551
0
        float cur = rect.Row(plane, ty)[tx];
552
0
        float prev_row = rect.Row(plane, ty - 1)[tx];
553
0
        float prev = rect.Row(plane, ty)[tx - 1];
554
0
        xmax = std::max(xmax, std::abs(cur - prev));
555
0
        ymax = std::max(ymax, std::abs(cur - prev_row));
556
0
      }
557
0
    }
558
0
    return std::max(xmax, ymax);
559
0
  }
560
  void CalcExposedBlue(const ImageF* JXL_RESTRICT plane_y,
561
0
                       const ImageF* JXL_RESTRICT plane_b, const Rect& rect) {
562
0
    float eb = 0;
563
0
    float xmax = 0;
564
0
    float ymax = 0;
565
0
    for (size_t ty = 1; ty < rect.ysize(); ++ty) {
566
0
      for (size_t tx = 1; tx < rect.xsize(); ++tx) {
567
0
        float cur_y = rect.Row(plane_y, ty)[tx];
568
0
        float cur_b = rect.Row(plane_b, ty)[tx];
569
0
        float exposed_b = cur_b - cur_y * 1.2;
570
0
        float diff_b = cur_b - cur_y;
571
0
        float prev_row = rect.Row(plane_b, ty - 1)[tx];
572
0
        float prev = rect.Row(plane_b, ty)[tx - 1];
573
0
        float diff_prev_row = prev_row - rect.Row(plane_y, ty - 1)[tx];
574
0
        float diff_prev = prev - rect.Row(plane_y, ty)[tx - 1];
575
0
        xmax = std::max(xmax, std::abs(diff_b - diff_prev));
576
0
        ymax = std::max(ymax, std::abs(diff_b - diff_prev_row));
577
0
        if (exposed_b >= 0) {
578
0
          exposed_b *= fabs(cur_b - prev) + fabs(cur_b - prev_row);
579
0
          eb = std::max(eb, exposed_b);
580
0
        }
581
0
      }
582
0
    }
583
0
    exposed_blue = eb;
584
0
    db = std::max(xmax, ymax);
585
0
  }
586
0
  void Calc(const Image3F* JXL_RESTRICT opsin, const Rect& rect) {
587
0
    dx = CalcPlane(&opsin->Plane(0), rect);
588
0
    CalcExposedBlue(&opsin->Plane(1), &opsin->Plane(2), rect);
589
0
  }
590
0
  int HowMuchIsXChannelPixelized() const {
591
0
    if (dx >= 0.026) {
592
0
      return 3;
593
0
    }
594
0
    if (dx >= 0.022) {
595
0
      return 2;
596
0
    }
597
0
    if (dx >= 0.015) {
598
0
      return 1;
599
0
    }
600
0
    return 0;
601
0
  }
602
0
  int HowMuchIsBChannelPixelized() const {
603
0
    int add = exposed_blue >= 0.13 ? 1 : 0;
604
0
    if (db > 0.38) {
605
0
      return 2 + add;
606
0
    }
607
0
    if (db > 0.33) {
608
0
      return 1 + add;
609
0
    }
610
0
    if (db > 0.28) {
611
0
      return add;
612
0
    }
613
0
    return 0;
614
0
  }
615
};
616
617
void ComputeChromacityAdjustments(const CompressParams& cparams,
618
                                  const Image3F& opsin, const Rect& rect,
619
0
                                  FrameHeader* frame_header) {
620
0
  if (frame_header->encoding != FrameEncoding::kVarDCT ||
621
0
      cparams.max_error_mode) {
622
0
    return;
623
0
  }
624
  // 1) Distance based approach for chromacity adjustment:
625
0
  float x_qm_scale_steps[3] = {2.5f, 5.5f, 9.5f};
626
0
  frame_header->x_qm_scale = 3;
627
0
  for (float x_qm_scale_step : x_qm_scale_steps) {
628
0
    if (cparams.original_butteraugli_distance > x_qm_scale_step) {
629
0
      frame_header->x_qm_scale++;
630
0
    }
631
0
  }
632
  // 2) Pixel-based approach for chromacity adjustment:
633
  // look at the individual pixels and make a guess how difficult
634
  // the image would be based on the worst case pixel.
635
0
  PixelStatsForChromacityAdjustment pixel_stats;
636
0
  if (cparams.speed_tier <= SpeedTier::kSquirrel) {
637
0
    pixel_stats.Calc(&opsin, rect);
638
0
  }
639
  // For X take the most severe adjustment.
640
0
  frame_header->x_qm_scale = std::max<int>(
641
0
      frame_header->x_qm_scale, 2 + pixel_stats.HowMuchIsXChannelPixelized());
642
  // B only adjusted by pixel-based approach.
643
0
  frame_header->b_qm_scale = 2 + pixel_stats.HowMuchIsBChannelPixelized();
644
0
}
645
646
void ComputeNoiseParams(const CompressParams& cparams, bool streaming_mode,
647
                        bool color_is_jpeg, const Image3F& opsin,
648
                        const FrameDimensions& frame_dim,
649
0
                        FrameHeader* frame_header, NoiseParams* noise_params) {
650
0
  if (cparams.photon_noise_iso > 0) {
651
0
    *noise_params = SimulatePhotonNoise(frame_dim.xsize, frame_dim.ysize,
652
0
                                        cparams.photon_noise_iso);
653
0
  } else if (cparams.manual_noise.size() == NoiseParams::kNumNoisePoints) {
654
0
    for (size_t i = 0; i < NoiseParams::kNumNoisePoints; i++) {
655
0
      noise_params->lut[i] = cparams.manual_noise[i];
656
0
    }
657
0
  } else if (frame_header->encoding == FrameEncoding::kVarDCT &&
658
0
             frame_header->flags & FrameHeader::kNoise && !color_is_jpeg &&
659
0
             !streaming_mode) {
660
    // Don't start at zero amplitude since adding noise is expensive -- it
661
    // significantly slows down decoding, and this is unlikely to
662
    // completely go away even with advanced optimizations. After the
663
    // kNoiseModelingRampUpDistanceRange we have reached the full level,
664
    // i.e. noise is no longer represented by the compressed image, so we
665
    // can add full noise by the noise modeling itself.
666
0
    static const float kNoiseModelingRampUpDistanceRange = 0.6;
667
0
    static const float kNoiseLevelAtStartOfRampUp = 0.25;
668
0
    static const float kNoiseRampupStart = 1.0;
669
    // TODO(user) test and properly select quality_coef with smooth
670
    // filter
671
0
    float quality_coef = 1.0f;
672
0
    const float rampup = (cparams.butteraugli_distance - kNoiseRampupStart) /
673
0
                         kNoiseModelingRampUpDistanceRange;
674
0
    if (rampup < 1.0f) {
675
0
      quality_coef = kNoiseLevelAtStartOfRampUp +
676
0
                     (1.0f - kNoiseLevelAtStartOfRampUp) * rampup;
677
0
    }
678
0
    if (rampup < 0.0f) {
679
0
      quality_coef = kNoiseRampupStart;
680
0
    }
681
0
    if (!GetNoiseParameter(opsin, noise_params, quality_coef)) {
682
0
      frame_header->flags &= ~FrameHeader::kNoise;
683
0
    }
684
0
  }
685
0
}
686
687
Status DownsampleColorChannels(const CompressParams& cparams,
688
                               const FrameHeader& frame_header,
689
0
                               bool color_is_jpeg, Image3F* opsin) {
690
0
  if (color_is_jpeg || frame_header.upsampling == 1 ||
691
0
      cparams.already_downsampled) {
692
0
    return true;
693
0
  }
694
0
  if (frame_header.encoding == FrameEncoding::kVarDCT &&
695
0
      frame_header.upsampling == 2) {
696
    // TODO(lode): use the regular DownsampleImage, or adapt to the custom
697
    // coefficients, if there is are custom upscaling coefficients in
698
    // CustomTransformData
699
0
    if (cparams.speed_tier <= SpeedTier::kSquirrel) {
700
      // TODO(lode): DownsampleImage2_Iterative is currently too slow to
701
      // be used for squirrel, make it faster, and / or enable it only for
702
      // kitten.
703
0
      JXL_RETURN_IF_ERROR(DownsampleImage2_Iterative(opsin));
704
0
    } else {
705
0
      JXL_RETURN_IF_ERROR(DownsampleImage2_Sharper(opsin));
706
0
    }
707
0
  } else {
708
0
    JXL_ASSIGN_OR_RETURN(*opsin,
709
0
                         DownsampleImage(*opsin, frame_header.upsampling));
710
0
  }
711
0
  if (frame_header.encoding == FrameEncoding::kVarDCT) {
712
0
    JXL_RETURN_IF_ERROR(PadImageToBlockMultipleInPlace(opsin));
713
0
  }
714
0
  return true;
715
0
}
716
717
template <size_t L, typename V, typename R>
718
0
void FindIndexOfSumMaximum(const V* array, R* idx, V* sum) {
719
0
  static_assert(L > 0);
720
0
  V maxval = 0;
721
0
  V val = 0;
722
0
  R maxidx = 0;
723
0
  for (size_t i = 0; i < L; ++i) {
724
0
    val += array[i];
725
0
    if (val > maxval) {
726
0
      maxval = val;
727
0
      maxidx = i;
728
0
    }
729
0
  }
730
0
  *idx = maxidx;
731
0
  *sum = maxval;
732
0
}
733
734
Status ComputeJPEGTranscodingData(const jpeg::JPEGData& jpeg_data,
735
                                  const FrameHeader& frame_header,
736
                                  ThreadPool* pool,
737
                                  ModularFrameEncoder* enc_modular,
738
0
                                  PassesEncoderState* enc_state) {
739
0
  PassesSharedState& shared = enc_state->shared;
740
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
741
0
  const FrameDimensions& frame_dim = shared.frame_dim;
742
743
0
  const size_t xsize = frame_dim.xsize_padded;
744
0
  const size_t ysize = frame_dim.ysize_padded;
745
0
  const size_t xsize_blocks = frame_dim.xsize_blocks;
746
0
  const size_t ysize_blocks = frame_dim.ysize_blocks;
747
748
  // no-op chroma from luma
749
0
  JXL_ASSIGN_OR_RETURN(shared.cmap, ColorCorrelationMap::Create(
750
0
                                        memory_manager, xsize, ysize, false));
751
0
  shared.ac_strategy.FillDCT8();
752
0
  FillImage(static_cast<uint8_t>(0), &shared.epf_sharpness);
753
754
0
  enc_state->coeffs.clear();
755
0
  while (enc_state->coeffs.size() < enc_state->passes.size()) {
756
0
    JXL_ASSIGN_OR_RETURN(
757
0
        std::unique_ptr<ACImageT<int32_t>> coeffs,
758
0
        ACImageT<int32_t>::Make(memory_manager, kGroupDim * kGroupDim,
759
0
                                frame_dim.num_groups));
760
0
    enc_state->coeffs.emplace_back(std::move(coeffs));
761
0
  }
762
763
  // convert JPEG quantization table to a Quantizer object
764
0
  float dcquantization[3];
765
0
  std::vector<QuantEncoding> qe(kNumQuantTables, QuantEncoding::Library<0>());
766
767
0
  auto jpeg_c_map =
768
0
      JpegOrder(frame_header.color_transform, jpeg_data.components.size() == 1);
769
770
0
  std::vector<int> qt(192);
771
0
  for (size_t c = 0; c < 3; c++) {
772
0
    size_t jpeg_c = jpeg_c_map[c];
773
0
    const int32_t* quant =
774
0
        jpeg_data.quant[jpeg_data.components[jpeg_c].quant_idx].values.data();
775
776
0
    dcquantization[c] = 255 * 8.0f / quant[0];
777
0
    for (size_t y = 0; y < 8; y++) {
778
0
      for (size_t x = 0; x < 8; x++) {
779
        // JPEG XL transposes the DCT, JPEG doesn't.
780
0
        qt[c * 64 + 8 * x + y] = quant[8 * y + x];
781
0
      }
782
0
    }
783
0
  }
784
0
  JXL_RETURN_IF_ERROR(DequantMatricesSetCustomDC(
785
0
      memory_manager, &shared.matrices, dcquantization));
786
0
  float dcquantization_r[3] = {1.0f / dcquantization[0],
787
0
                               1.0f / dcquantization[1],
788
0
                               1.0f / dcquantization[2]};
789
790
0
  std::vector<int32_t> scaled_qtable(192);
791
0
  for (size_t c = 0; c < 3; c++) {
792
0
    for (size_t i = 0; i < 64; i++) {
793
0
      scaled_qtable[64 * c + i] =
794
0
          (1 << kCFLFixedPointPrecision) * qt[64 + i] / qt[64 * c + i];
795
0
    }
796
0
  }
797
798
0
  qe[static_cast<size_t>(AcStrategyType::DCT)] =
799
0
      QuantEncoding::RAW(std::move(qt));
800
0
  JXL_RETURN_IF_ERROR(
801
0
      DequantMatricesSetCustom(&shared.matrices, qe, enc_modular));
802
803
  // Ensure that InvGlobalScale() is 1.
804
0
  shared.quantizer = Quantizer(shared.matrices, 1, kGlobalScaleDenom);
805
  // Recompute MulDC() and InvMulDC().
806
0
  shared.quantizer.RecomputeFromGlobalScale();
807
808
  // Per-block dequant scaling should be 1.
809
0
  FillImage(static_cast<int32_t>(shared.quantizer.InvGlobalScale()),
810
0
            &shared.raw_quant_field);
811
812
0
  auto jpeg_row = [&](size_t c, size_t y) {
813
0
    return jpeg_data.components[jpeg_c_map[c]].coeffs.data() +
814
0
           jpeg_data.components[jpeg_c_map[c]].width_in_blocks * kDCTBlockSize *
815
0
               y;
816
0
  };
817
818
0
  bool DCzero = (frame_header.color_transform == ColorTransform::kYCbCr);
819
  // Compute chroma-from-luma for AC (doesn't seem to be useful for DC)
820
0
  if (frame_header.chroma_subsampling.Is444() &&
821
0
      enc_state->cparams.force_cfl_jpeg_recompression &&
822
0
      jpeg_data.components.size() == 3) {
823
0
    for (size_t c : {0, 2}) {
824
0
      ImageSB* map = (c == 0 ? &shared.cmap.ytox_map : &shared.cmap.ytob_map);
825
0
      const float kScale = kDefaultColorFactor;
826
0
      const int kOffset = 127;
827
0
      const float kBase = c == 0 ? shared.cmap.base().YtoXRatio(0)
828
0
                                 : shared.cmap.base().YtoBRatio(0);
829
0
      const float kZeroThresh =
830
0
          kScale * kZeroBiasDefault[c] *
831
0
          0.9999f;  // just epsilon less for better rounding
832
833
0
      auto process_row = [&](const uint32_t task,
834
0
                             const size_t thread) -> Status {
835
0
        size_t ty = task;
836
0
        int8_t* JXL_RESTRICT row_out = map->Row(ty);
837
0
        for (size_t tx = 0; tx < map->xsize(); ++tx) {
838
0
          const size_t y0 = ty * kColorTileDimInBlocks;
839
0
          const size_t x0 = tx * kColorTileDimInBlocks;
840
0
          const size_t y1 = std::min(frame_dim.ysize_blocks,
841
0
                                     (ty + 1) * kColorTileDimInBlocks);
842
0
          const size_t x1 = std::min(frame_dim.xsize_blocks,
843
0
                                     (tx + 1) * kColorTileDimInBlocks);
844
0
          int32_t d_num_zeros[257] = {0};
845
          // TODO(veluca): this needs SIMD + fixed point adaptation, and/or
846
          // conversion to the new CfL algorithm.
847
0
          for (size_t y = y0; y < y1; ++y) {
848
0
            const int16_t* JXL_RESTRICT row_m = jpeg_row(1, y);
849
0
            const int16_t* JXL_RESTRICT row_s = jpeg_row(c, y);
850
0
            for (size_t x = x0; x < x1; ++x) {
851
0
              for (size_t coeffpos = 1; coeffpos < kDCTBlockSize; coeffpos++) {
852
0
                const float scaled_m = row_m[x * kDCTBlockSize + coeffpos] *
853
0
                                       scaled_qtable[64 * c + coeffpos] *
854
0
                                       (1.0f / (1 << kCFLFixedPointPrecision));
855
0
                const float scaled_s =
856
0
                    kScale * row_s[x * kDCTBlockSize + coeffpos] +
857
0
                    (kOffset - kBase * kScale) * scaled_m;
858
0
                if (std::abs(scaled_m) > 1e-8f) {
859
0
                  float from;
860
0
                  float to;
861
0
                  if (scaled_m > 0) {
862
0
                    from = (scaled_s - kZeroThresh) / scaled_m;
863
0
                    to = (scaled_s + kZeroThresh) / scaled_m;
864
0
                  } else {
865
0
                    from = (scaled_s + kZeroThresh) / scaled_m;
866
0
                    to = (scaled_s - kZeroThresh) / scaled_m;
867
0
                  }
868
0
                  if (from < 0.0f) {
869
0
                    from = 0.0f;
870
0
                  }
871
0
                  if (to > 255.0f) {
872
0
                    to = 255.0f;
873
0
                  }
874
                  // Instead of clamping the both values
875
                  // we just check that range is sane.
876
0
                  if (from <= to) {
877
0
                    d_num_zeros[static_cast<int>(std::ceil(from))]++;
878
0
                    d_num_zeros[static_cast<int>(std::floor(to + 1))]--;
879
0
                  }
880
0
                }
881
0
              }
882
0
            }
883
0
          }
884
0
          int best = 0;
885
0
          int32_t best_sum = 0;
886
0
          FindIndexOfSumMaximum<256>(d_num_zeros, &best, &best_sum);
887
0
          int32_t offset_sum = 0;
888
0
          for (int i = 0; i < 256; ++i) {
889
0
            if (i <= kOffset) {
890
0
              offset_sum += d_num_zeros[i];
891
0
            }
892
0
          }
893
0
          row_out[tx] = 0;
894
0
          if (best_sum > offset_sum + 1) {
895
0
            row_out[tx] = best - kOffset;
896
0
          }
897
0
        }
898
0
        return true;
899
0
      };
900
901
0
      JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, map->ysize(), ThreadPool::NoInit,
902
0
                                    process_row, "FindCorrelation"));
903
0
    }
904
0
  }
905
906
0
  JXL_ASSIGN_OR_RETURN(
907
0
      Image3F dc, Image3F::Create(memory_manager, xsize_blocks, ysize_blocks));
908
0
  if (!frame_header.chroma_subsampling.Is444()) {
909
0
    ZeroFillImage(&dc);
910
0
    for (auto& coeff : enc_state->coeffs) {
911
0
      coeff->ZeroFill();
912
0
    }
913
0
  }
914
  // JPEG DC is from -1024 to 1023.
915
0
  std::vector<size_t> dc_counts[3] = {};
916
0
  dc_counts[0].resize(2048);
917
0
  dc_counts[1].resize(2048);
918
0
  dc_counts[2].resize(2048);
919
0
  size_t total_dc[3] = {};
920
0
  for (size_t c : {1, 0, 2}) {
921
0
    if (jpeg_data.components.size() == 1 && c != 1) {
922
0
      for (auto& coeff : enc_state->coeffs) {
923
0
        coeff->ZeroFillPlane(c);
924
0
      }
925
0
      ZeroFillImage(&dc.Plane(c));
926
      // Ensure no division by 0.
927
0
      dc_counts[c][1024] = 1;
928
0
      total_dc[c] = 1;
929
0
      continue;
930
0
    }
931
0
    size_t hshift = frame_header.chroma_subsampling.HShift(c);
932
0
    size_t vshift = frame_header.chroma_subsampling.VShift(c);
933
0
    ImageSB& map = (c == 0 ? shared.cmap.ytox_map : shared.cmap.ytob_map);
934
0
    for (size_t group_index = 0; group_index < frame_dim.num_groups;
935
0
         group_index++) {
936
0
      const size_t gx = group_index % frame_dim.xsize_groups;
937
0
      const size_t gy = group_index / frame_dim.xsize_groups;
938
0
      int32_t* coeffs[kMaxNumPasses];
939
0
      for (size_t i = 0; i < enc_state->coeffs.size(); i++) {
940
0
        coeffs[i] = enc_state->coeffs[i]->PlaneRow(c, group_index, 0).ptr32;
941
0
      }
942
0
      int32_t block[64];
943
0
      for (size_t by = gy * kGroupDimInBlocks;
944
0
           by < ysize_blocks && by < (gy + 1) * kGroupDimInBlocks; ++by) {
945
0
        if ((by >> vshift) << vshift != by) continue;
946
0
        const int16_t* JXL_RESTRICT inputjpeg = jpeg_row(c, by >> vshift);
947
0
        const int16_t* JXL_RESTRICT inputjpegY = jpeg_row(1, by);
948
0
        float* JXL_RESTRICT fdc = dc.PlaneRow(c, by >> vshift);
949
0
        const int8_t* JXL_RESTRICT cm =
950
0
            map.ConstRow(by / kColorTileDimInBlocks);
951
0
        for (size_t bx = gx * kGroupDimInBlocks;
952
0
             bx < xsize_blocks && bx < (gx + 1) * kGroupDimInBlocks; ++bx) {
953
0
          if ((bx >> hshift) << hshift != bx) continue;
954
0
          size_t base = (bx >> hshift) * kDCTBlockSize;
955
0
          int idc;
956
0
          if (DCzero) {
957
0
            idc = inputjpeg[base];
958
0
          } else {
959
0
            idc = inputjpeg[base] + 1024 / qt[c * 64];
960
0
          }
961
0
          dc_counts[c][std::min(static_cast<uint32_t>(idc + 1024),
962
0
                                static_cast<uint32_t>(2047))]++;
963
0
          total_dc[c]++;
964
0
          fdc[bx >> hshift] = idc * dcquantization_r[c];
965
0
          if (c == 1 || !enc_state->cparams.force_cfl_jpeg_recompression ||
966
0
              !frame_header.chroma_subsampling.Is444()) {
967
0
            for (size_t y = 0; y < 8; y++) {
968
0
              for (size_t x = 0; x < 8; x++) {
969
0
                block[y * 8 + x] = inputjpeg[base + x * 8 + y];
970
0
              }
971
0
            }
972
0
          } else {
973
0
            const int32_t scale =
974
0
                ColorCorrelation::RatioJPEG(cm[bx / kColorTileDimInBlocks]);
975
976
0
            for (size_t y = 0; y < 8; y++) {
977
0
              for (size_t x = 0; x < 8; x++) {
978
0
                int Y = inputjpegY[kDCTBlockSize * bx + x * 8 + y];
979
0
                int QChroma = inputjpeg[kDCTBlockSize * bx + x * 8 + y];
980
                // Fixed-point multiply of CfL scale with quant table ratio
981
                // first, and Y value second.
982
0
                int coeff_scale = (scale * scaled_qtable[64 * c + y * 8 + x] +
983
0
                                   (1 << (kCFLFixedPointPrecision - 1))) >>
984
0
                                  kCFLFixedPointPrecision;
985
0
                int cfl_factor =
986
0
                    (Y * coeff_scale + (1 << (kCFLFixedPointPrecision - 1))) >>
987
0
                    kCFLFixedPointPrecision;
988
0
                int QCR = QChroma - cfl_factor;
989
0
                block[y * 8 + x] = QCR;
990
0
              }
991
0
            }
992
0
          }
993
0
          enc_state->progressive_splitter.SplitACCoefficients(
994
0
              block, AcStrategy::FromRawStrategy(AcStrategyType::DCT), bx, by,
995
0
              coeffs);
996
0
          for (size_t i = 0; i < enc_state->coeffs.size(); i++) {
997
0
            coeffs[i] += kDCTBlockSize;
998
0
          }
999
0
        }
1000
0
      }
1001
0
    }
1002
0
  }
1003
1004
0
  auto& dct = enc_state->shared.block_ctx_map.dc_thresholds;
1005
0
  auto& num_dc_ctxs = enc_state->shared.block_ctx_map.num_dc_ctxs;
1006
0
  num_dc_ctxs = 1;
1007
0
  for (size_t i = 0; i < 3; i++) {
1008
0
    dct[i].clear();
1009
0
    int num_thresholds = (CeilLog2Nonzero(total_dc[i]) - 12) / 2;
1010
    // up to 3 buckets per channel:
1011
    // dark/medium/bright, yellow/unsat/blue, green/unsat/red
1012
0
    num_thresholds = std::min(std::max(num_thresholds, 0), 2);
1013
0
    size_t cumsum = 0;
1014
0
    size_t cut = total_dc[i] / (num_thresholds + 1);
1015
0
    for (int j = 0; j < 2048; j++) {
1016
0
      cumsum += dc_counts[i][j];
1017
0
      if (cumsum > cut) {
1018
0
        dct[i].push_back(j - 1025);
1019
0
        cut = total_dc[i] * (dct[i].size() + 1) / (num_thresholds + 1);
1020
0
      }
1021
0
    }
1022
0
    num_dc_ctxs *= dct[i].size() + 1;
1023
0
  }
1024
1025
0
  auto& ctx_map = enc_state->shared.block_ctx_map.ctx_map;
1026
0
  ctx_map.clear();
1027
0
  ctx_map.resize(3 * kNumOrders * num_dc_ctxs, 0);
1028
1029
0
  int lbuckets = (dct[1].size() + 1);
1030
0
  for (size_t i = 0; i < num_dc_ctxs; i++) {
1031
    // up to 9 contexts for luma
1032
0
    ctx_map[i] = i / lbuckets;
1033
    // up to 3 contexts for chroma
1034
0
    ctx_map[kNumOrders * num_dc_ctxs + i] =
1035
0
        ctx_map[2 * kNumOrders * num_dc_ctxs + i] =
1036
0
            num_dc_ctxs / lbuckets + (i % lbuckets);
1037
0
  }
1038
0
  enc_state->shared.block_ctx_map.num_ctxs =
1039
0
      *std::max_element(ctx_map.begin(), ctx_map.end()) + 1;
1040
1041
  // disable DC frame for now
1042
0
  auto compute_dc_coeffs = [&](const uint32_t group_index,
1043
0
                               size_t /* thread */) -> Status {
1044
0
    const Rect r = enc_state->shared.frame_dim.DCGroupRect(group_index);
1045
0
    JXL_RETURN_IF_ERROR(enc_modular->AddVarDCTDC(frame_header, dc, r,
1046
0
                                                 group_index,
1047
0
                                                 /*nl_dc=*/false, enc_state,
1048
0
                                                 /*jpeg_transcode=*/true));
1049
0
    JXL_RETURN_IF_ERROR(enc_modular->AddACMetadata(
1050
0
        r, group_index, /*jpeg_transcode=*/true, enc_state));
1051
0
    return true;
1052
0
  };
1053
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, shared.frame_dim.num_dc_groups,
1054
0
                                ThreadPool::NoInit, compute_dc_coeffs,
1055
0
                                "Compute DC coeffs"));
1056
1057
0
  return true;
1058
0
}
1059
1060
Status ComputeVarDCTEncodingData(const FrameHeader& frame_header,
1061
                                 const Image3F* linear,
1062
                                 Image3F* JXL_RESTRICT opsin, const Rect& rect,
1063
                                 const JxlCmsInterface& cms, ThreadPool* pool,
1064
                                 ModularFrameEncoder* enc_modular,
1065
                                 PassesEncoderState* enc_state,
1066
0
                                 AuxOut* aux_out) {
1067
0
  JXL_ENSURE((rect.xsize() % kBlockDim) == 0 &&
1068
0
             (rect.ysize() % kBlockDim) == 0);
1069
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
1070
  // Save pre-Gaborish opsin for AR control field heuristics computation.
1071
0
  Image3F orig_opsin;
1072
0
  JXL_ASSIGN_OR_RETURN(
1073
0
      orig_opsin, Image3F::Create(memory_manager, rect.xsize(), rect.ysize()));
1074
0
  JXL_RETURN_IF_ERROR(CopyImageTo(rect, *opsin, Rect(orig_opsin), &orig_opsin));
1075
0
  JXL_RETURN_IF_ERROR(orig_opsin.ShrinkTo(enc_state->shared.frame_dim.xsize,
1076
0
                                          enc_state->shared.frame_dim.ysize));
1077
1078
0
  JXL_RETURN_IF_ERROR(LossyFrameHeuristics(frame_header, enc_state, enc_modular,
1079
0
                                           linear, opsin, rect, cms, pool,
1080
0
                                           aux_out));
1081
1082
0
  JXL_RETURN_IF_ERROR(InitializePassesEncoder(
1083
0
      frame_header, *opsin, rect, cms, pool, enc_state, enc_modular, aux_out));
1084
1085
0
  JXL_RETURN_IF_ERROR(
1086
0
      ComputeARHeuristics(frame_header, enc_state, orig_opsin, rect, pool));
1087
1088
0
  JXL_RETURN_IF_ERROR(ComputeACMetadata(pool, enc_state, enc_modular));
1089
1090
0
  return true;
1091
0
}
1092
1093
Status ComputeAllCoeffOrders(PassesEncoderState& enc_state,
1094
0
                             const FrameDimensions& frame_dim) {
1095
0
  auto used_orders_info = ComputeUsedOrders(
1096
0
      enc_state.cparams.speed_tier, enc_state.shared.ac_strategy,
1097
0
      Rect(enc_state.shared.raw_quant_field));
1098
0
  enc_state.used_orders.resize(enc_state.progressive_splitter.GetNumPasses());
1099
0
  for (size_t i = 0; i < enc_state.progressive_splitter.GetNumPasses(); i++) {
1100
0
    JXL_RETURN_IF_ERROR(ComputeCoeffOrder(
1101
0
        enc_state.cparams.speed_tier, *enc_state.coeffs[i],
1102
0
        enc_state.shared.ac_strategy, frame_dim, enc_state.used_orders[i],
1103
0
        enc_state.used_acs, used_orders_info.first, used_orders_info.second,
1104
0
        &enc_state.shared.coeff_orders[i * enc_state.shared.coeff_order_size]));
1105
0
  }
1106
0
  enc_state.used_acs |= used_orders_info.first;
1107
0
  return true;
1108
0
}
1109
1110
// Working area for TokenizeCoefficients (per-group!)
1111
struct EncCache {
1112
  // Allocates memory when first called.
1113
0
  Status InitOnce(JxlMemoryManager* memory_manager) {
1114
0
    if (num_nzeroes.xsize() == 0) {
1115
0
      JXL_ASSIGN_OR_RETURN(num_nzeroes,
1116
0
                           Image3I::Create(memory_manager, kGroupDimInBlocks,
1117
0
                                           kGroupDimInBlocks));
1118
0
    }
1119
0
    return true;
1120
0
  }
1121
  // TokenizeCoefficients
1122
  Image3I num_nzeroes;
1123
};
1124
1125
Status TokenizeAllCoefficients(const FrameHeader& frame_header,
1126
                               ThreadPool* pool,
1127
0
                               PassesEncoderState* enc_state) {
1128
0
  PassesSharedState& shared = enc_state->shared;
1129
0
  std::vector<EncCache> group_caches;
1130
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
1131
0
  const auto tokenize_group_init = [&](const size_t num_threads) -> Status {
1132
0
    group_caches.resize(num_threads);
1133
0
    return true;
1134
0
  };
1135
0
  const auto tokenize_group = [&](const uint32_t group_index,
1136
0
                                  const size_t thread) -> Status {
1137
    // Tokenize coefficients.
1138
0
    const Rect rect = shared.frame_dim.BlockGroupRect(group_index);
1139
0
    for (size_t idx_pass = 0; idx_pass < enc_state->passes.size(); idx_pass++) {
1140
0
      JXL_ENSURE(enc_state->coeffs[idx_pass]->Type() == ACType::k32);
1141
0
      const int32_t* JXL_RESTRICT ac_rows[3] = {
1142
0
          enc_state->coeffs[idx_pass]->PlaneRow(0, group_index, 0).ptr32,
1143
0
          enc_state->coeffs[idx_pass]->PlaneRow(1, group_index, 0).ptr32,
1144
0
          enc_state->coeffs[idx_pass]->PlaneRow(2, group_index, 0).ptr32,
1145
0
      };
1146
      // Ensure group cache is initialized.
1147
0
      JXL_RETURN_IF_ERROR(group_caches[thread].InitOnce(memory_manager));
1148
0
      JXL_RETURN_IF_ERROR(TokenizeCoefficients(
1149
0
          &shared.coeff_orders[idx_pass * shared.coeff_order_size], rect,
1150
0
          ac_rows, shared.ac_strategy, frame_header.chroma_subsampling,
1151
0
          &group_caches[thread].num_nzeroes,
1152
0
          &enc_state->passes[idx_pass].ac_tokens[group_index], shared.quant_dc,
1153
0
          shared.raw_quant_field, shared.block_ctx_map));
1154
0
    }
1155
0
    return true;
1156
0
  };
1157
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, shared.frame_dim.num_groups,
1158
0
                                tokenize_group_init, tokenize_group,
1159
0
                                "TokenizeGroup"));
1160
0
  return true;
1161
0
}
1162
1163
Status EncodeGlobalDCInfo(const PassesSharedState& shared, BitWriter* writer,
1164
0
                          AuxOut* aux_out) {
1165
  // Encode quantizer DC and global scale.
1166
0
  QuantizerParams params = shared.quantizer.GetParams();
1167
0
  JXL_RETURN_IF_ERROR(
1168
0
      WriteQuantizerParams(params, writer, LayerType::Quant, aux_out));
1169
0
  JXL_RETURN_IF_ERROR(EncodeBlockCtxMap(shared.block_ctx_map, writer, aux_out));
1170
0
  JXL_RETURN_IF_ERROR(ColorCorrelationEncodeDC(shared.cmap.base(), writer,
1171
0
                                               LayerType::Dc, aux_out));
1172
0
  return true;
1173
0
}
1174
1175
// In streaming mode, this function only performs the histogram clustering and
1176
// saves the histogram bitstreams in enc_state, the actual AC global bitstream
1177
// is written in OutputAcGlobal() function after all the groups are processed.
1178
Status EncodeGlobalACInfo(PassesEncoderState* enc_state, BitWriter* writer,
1179
0
                          ModularFrameEncoder* enc_modular, AuxOut* aux_out) {
1180
0
  PassesSharedState& shared = enc_state->shared;
1181
0
  JxlMemoryManager* memory_manager = enc_state->memory_manager();
1182
0
  JXL_RETURN_IF_ERROR(DequantMatricesEncode(memory_manager, shared.matrices,
1183
0
                                            writer, LayerType::Quant, aux_out,
1184
0
                                            enc_modular));
1185
0
  size_t num_histo_bits = CeilLog2Nonzero(shared.frame_dim.num_groups);
1186
0
  if (!enc_state->streaming_mode && num_histo_bits != 0) {
1187
0
    JXL_RETURN_IF_ERROR(
1188
0
        writer->WithMaxBits(num_histo_bits, LayerType::Ac, aux_out, [&] {
1189
0
          writer->Write(num_histo_bits, shared.num_histograms - 1);
1190
0
          return true;
1191
0
        }));
1192
0
  }
1193
1194
0
  for (size_t i = 0; i < enc_state->progressive_splitter.GetNumPasses(); i++) {
1195
    // Encode coefficient orders.
1196
0
    if (!enc_state->streaming_mode) {
1197
0
      size_t order_bits = 0;
1198
0
      JXL_RETURN_IF_ERROR(U32Coder::CanEncode(
1199
0
          kOrderEnc, enc_state->used_orders[i], &order_bits));
1200
0
      JXL_RETURN_IF_ERROR(
1201
0
          writer->WithMaxBits(order_bits, LayerType::Order, aux_out, [&] {
1202
0
            return U32Coder::Write(kOrderEnc, enc_state->used_orders[i],
1203
0
                                   writer);
1204
0
          }));
1205
0
      JXL_RETURN_IF_ERROR(
1206
0
          EncodeCoeffOrders(enc_state->used_orders[i],
1207
0
                            &shared.coeff_orders[i * shared.coeff_order_size],
1208
0
                            writer, LayerType::Order, aux_out));
1209
0
    }
1210
1211
    // Encode histograms.
1212
0
    HistogramParams hist_params(enc_state->cparams.speed_tier,
1213
0
                                shared.block_ctx_map.NumACContexts());
1214
0
    if (enc_state->cparams.speed_tier > SpeedTier::kTortoise) {
1215
0
      hist_params.lz77_method = HistogramParams::LZ77Method::kNone;
1216
0
    }
1217
0
    if (enc_state->cparams.decoding_speed_tier >= 1) {
1218
0
      hist_params.max_histograms = 6;
1219
0
    }
1220
0
    size_t num_histogram_groups = shared.num_histograms;
1221
0
    if (enc_state->streaming_mode) {
1222
0
      size_t prev_num_histograms =
1223
0
          enc_state->passes[i].codes.encoding_info.size();
1224
0
      if (enc_state->initialize_global_state) {
1225
0
        prev_num_histograms += kNumFixedHistograms;
1226
0
        hist_params.add_fixed_histograms = true;
1227
0
      }
1228
0
      size_t remaining_histograms = kClustersLimit - prev_num_histograms;
1229
      // Heuristic to assign budget of new histograms to DC groups.
1230
      // TODO(szabadka) Tune this together with the DC group ordering.
1231
0
      size_t max_histograms = remaining_histograms < 20
1232
0
                                  ? std::min<size_t>(remaining_histograms, 4)
1233
0
                                  : remaining_histograms / 4;
1234
0
      hist_params.max_histograms =
1235
0
          std::min(max_histograms, hist_params.max_histograms);
1236
0
      num_histogram_groups = 1;
1237
0
    }
1238
0
    hist_params.streaming_mode = enc_state->streaming_mode;
1239
0
    hist_params.initialize_global_state = enc_state->initialize_global_state;
1240
0
    JXL_ASSIGN_OR_RETURN(
1241
0
        size_t cost,
1242
0
        BuildAndEncodeHistograms(
1243
0
            memory_manager, hist_params,
1244
0
            num_histogram_groups * shared.block_ctx_map.NumACContexts(),
1245
0
            enc_state->passes[i].ac_tokens, &enc_state->passes[i].codes,
1246
0
            &enc_state->passes[i].context_map, writer, LayerType::Ac, aux_out));
1247
0
    (void)cost;
1248
0
  }
1249
1250
0
  return true;
1251
0
}
1252
1253
Status EncodeGroups(const FrameHeader& frame_header,
1254
                    PassesEncoderState* enc_state,
1255
                    ModularFrameEncoder* enc_modular, ThreadPool* pool,
1256
                    std::vector<std::unique_ptr<BitWriter>>* group_codes,
1257
0
                    AuxOut* aux_out) {
1258
0
  const PassesSharedState& shared = enc_state->shared;
1259
0
  JxlMemoryManager* memory_manager = shared.memory_manager;
1260
0
  const FrameDimensions& frame_dim = shared.frame_dim;
1261
0
  const size_t num_groups = frame_dim.num_groups;
1262
0
  const size_t num_passes = enc_state->progressive_splitter.GetNumPasses();
1263
0
  const size_t global_ac_index = frame_dim.num_dc_groups + 1;
1264
0
  const bool is_small_image =
1265
0
      !enc_state->streaming_mode && num_groups == 1 && num_passes == 1;
1266
0
  const size_t num_toc_entries =
1267
0
      is_small_image ? 1
1268
0
                     : AcGroupIndex(0, 0, num_groups, frame_dim.num_dc_groups) +
1269
0
                           num_groups * num_passes;
1270
0
  JXL_ENSURE(group_codes->empty());
1271
0
  group_codes->reserve(num_toc_entries);
1272
0
  for (size_t i = 0; i < num_toc_entries; ++i) {
1273
0
    group_codes->emplace_back(jxl::make_unique<BitWriter>(memory_manager));
1274
0
  }
1275
1276
0
  const auto get_output = [&](const size_t index) -> BitWriter* {
1277
0
    return (*group_codes)[is_small_image ? 0 : index].get();
1278
0
  };
1279
0
  auto ac_group_code = [&](size_t pass, size_t group) {
1280
0
    return get_output(AcGroupIndex(pass, group, frame_dim.num_groups,
1281
0
                                   frame_dim.num_dc_groups));
1282
0
  };
1283
1284
0
  if (enc_state->initialize_global_state) {
1285
0
    if (frame_header.flags & FrameHeader::kPatches) {
1286
0
      JXL_RETURN_IF_ERROR(PatchDictionaryEncoder::Encode(
1287
0
          shared.image_features.patches, get_output(0), LayerType::Dictionary,
1288
0
          aux_out));
1289
0
    }
1290
0
    if (frame_header.flags & FrameHeader::kSplines) {
1291
0
      JXL_RETURN_IF_ERROR(EncodeSplines(shared.image_features.splines,
1292
0
                                        get_output(0), LayerType::Splines,
1293
0
                                        HistogramParams(), aux_out));
1294
0
    }
1295
0
    if (frame_header.flags & FrameHeader::kNoise) {
1296
0
      JXL_RETURN_IF_ERROR(EncodeNoise(shared.image_features.noise_params,
1297
0
                                      get_output(0), LayerType::Noise,
1298
0
                                      aux_out));
1299
0
    }
1300
1301
0
    JXL_RETURN_IF_ERROR(DequantMatricesEncodeDC(shared.matrices, get_output(0),
1302
0
                                                LayerType::Quant, aux_out));
1303
0
    if (frame_header.encoding == FrameEncoding::kVarDCT) {
1304
0
      JXL_RETURN_IF_ERROR(EncodeGlobalDCInfo(shared, get_output(0), aux_out));
1305
0
    }
1306
0
    JXL_RETURN_IF_ERROR(enc_modular->EncodeGlobalInfo(enc_state->streaming_mode,
1307
0
                                                      get_output(0), aux_out));
1308
0
    JXL_RETURN_IF_ERROR(enc_modular->EncodeStream(get_output(0), aux_out,
1309
0
                                                  LayerType::ModularGlobal,
1310
0
                                                  ModularStreamId::Global()));
1311
0
  }
1312
1313
0
  std::vector<std::unique_ptr<AuxOut>> aux_outs;
1314
0
  auto resize_aux_outs = [&aux_outs,
1315
0
                          aux_out](const size_t num_threads) -> Status {
1316
0
    if (aux_out == nullptr) {
1317
0
      aux_outs.resize(num_threads);
1318
0
    } else {
1319
0
      while (aux_outs.size() > num_threads) {
1320
0
        aux_out->Assimilate(*aux_outs.back());
1321
0
        aux_outs.pop_back();
1322
0
      }
1323
0
      while (num_threads > aux_outs.size()) {
1324
0
        aux_outs.emplace_back(jxl::make_unique<AuxOut>());
1325
0
      }
1326
0
    }
1327
0
    return true;
1328
0
  };
1329
1330
0
  std::atomic<bool> has_error{false};
1331
0
  const auto process_dc_group = [&](const uint32_t group_index,
1332
0
                                    const size_t thread) -> Status {
1333
0
    AuxOut* my_aux_out = aux_outs[thread].get();
1334
0
    uint32_t input_index = enc_state->streaming_mode ? 0 : group_index;
1335
0
    BitWriter* output = get_output(input_index + 1);
1336
0
    if (frame_header.encoding == FrameEncoding::kVarDCT &&
1337
0
        !(frame_header.flags & FrameHeader::kUseDcFrame)) {
1338
0
      JXL_RETURN_IF_ERROR(
1339
0
          output->WithMaxBits(2, LayerType::Dc, my_aux_out, [&] {
1340
0
            output->Write(2, enc_modular->extra_dc_precision[group_index]);
1341
0
            return true;
1342
0
          }));
1343
0
      JXL_RETURN_IF_ERROR(
1344
0
          enc_modular->EncodeStream(output, my_aux_out, LayerType::Dc,
1345
0
                                    ModularStreamId::VarDCTDC(group_index)));
1346
0
    }
1347
0
    JXL_RETURN_IF_ERROR(
1348
0
        enc_modular->EncodeStream(output, my_aux_out, LayerType::ModularDcGroup,
1349
0
                                  ModularStreamId::ModularDC(group_index)));
1350
0
    if (frame_header.encoding == FrameEncoding::kVarDCT) {
1351
0
      const Rect& rect = enc_state->shared.frame_dim.DCGroupRect(input_index);
1352
0
      size_t nb_bits = CeilLog2Nonzero(rect.xsize() * rect.ysize());
1353
0
      if (nb_bits != 0) {
1354
0
        JXL_RETURN_IF_ERROR(output->WithMaxBits(
1355
0
            nb_bits, LayerType::ControlFields, my_aux_out, [&] {
1356
0
              output->Write(nb_bits,
1357
0
                            enc_modular->ac_metadata_size[group_index] - 1);
1358
0
              return true;
1359
0
            }));
1360
0
      }
1361
0
      JXL_RETURN_IF_ERROR(enc_modular->EncodeStream(
1362
0
          output, my_aux_out, LayerType::ControlFields,
1363
0
          ModularStreamId::ACMetadata(group_index)));
1364
0
    }
1365
0
    return true;
1366
0
  };
1367
0
  if (enc_state->streaming_mode) {
1368
0
    JXL_ENSURE(frame_dim.num_dc_groups == 1);
1369
0
    JXL_RETURN_IF_ERROR(resize_aux_outs(1));
1370
0
    JXL_RETURN_IF_ERROR(process_dc_group(enc_state->dc_group_index, 0));
1371
0
  } else {
1372
0
    JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, frame_dim.num_dc_groups,
1373
0
                                  resize_aux_outs, process_dc_group,
1374
0
                                  "EncodeDCGroup"));
1375
0
  }
1376
0
  if (has_error) return JXL_FAILURE("EncodeDCGroup failed");
1377
0
  if (frame_header.encoding == FrameEncoding::kVarDCT) {
1378
0
    JXL_RETURN_IF_ERROR(EncodeGlobalACInfo(
1379
0
        enc_state, get_output(global_ac_index), enc_modular, aux_out));
1380
0
  }
1381
1382
0
  const auto process_group = [&](const uint32_t group_index,
1383
0
                                 const size_t thread) -> Status {
1384
0
    AuxOut* my_aux_out = aux_outs[thread].get();
1385
1386
0
    size_t ac_group_id =
1387
0
        enc_state->streaming_mode
1388
0
            ? enc_modular->ComputeStreamingAbsoluteAcGroupId(
1389
0
                  enc_state->dc_group_index, group_index, shared.frame_dim)
1390
0
            : group_index;
1391
1392
0
    for (size_t i = 0; i < num_passes; i++) {
1393
0
      JXL_DEBUG_V(2, "Encoding AC group %u [abs %" PRIuS "] pass %" PRIuS,
1394
0
                  group_index, ac_group_id, i);
1395
0
      if (frame_header.encoding == FrameEncoding::kVarDCT) {
1396
0
        JXL_RETURN_IF_ERROR(EncodeGroupTokenizedCoefficients(
1397
0
            group_index, i, enc_state->histogram_idx[group_index], *enc_state,
1398
0
            ac_group_code(i, group_index), my_aux_out));
1399
0
      }
1400
      // Write all modular encoded data (color?, alpha, depth, extra channels)
1401
0
      JXL_RETURN_IF_ERROR(enc_modular->EncodeStream(
1402
0
          ac_group_code(i, group_index), my_aux_out, LayerType::ModularAcGroup,
1403
0
          ModularStreamId::ModularAC(ac_group_id, i)));
1404
0
      JXL_DEBUG_V(2,
1405
0
                  "AC group %u [abs %" PRIuS "] pass %" PRIuS
1406
0
                  " encoded size is %" PRIuS " bits",
1407
0
                  group_index, ac_group_id, i,
1408
0
                  ac_group_code(i, group_index)->BitsWritten());
1409
0
    }
1410
0
    return true;
1411
0
  };
1412
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, num_groups, resize_aux_outs,
1413
0
                                process_group, "EncodeGroupCoefficients"));
1414
  // Resizing aux_outs to 0 also Assimilates the array.
1415
0
  static_cast<void>(resize_aux_outs(0));
1416
1417
0
  for (std::unique_ptr<BitWriter>& bw : *group_codes) {
1418
0
    JXL_RETURN_IF_ERROR(bw->WithMaxBits(8, LayerType::Ac, aux_out, [&] {
1419
0
      bw->ZeroPadToByte();  // end of group.
1420
0
      return true;
1421
0
    }));
1422
0
  }
1423
0
  return true;
1424
0
}
1425
1426
Status ComputeEncodingData(
1427
    const CompressParams& cparams, const FrameInfo& frame_info,
1428
    const CodecMetadata* metadata, JxlEncoderChunkedFrameAdapter& frame_data,
1429
    const jpeg::JPEGData* jpeg_data, size_t x0, size_t y0, size_t xsize,
1430
    size_t ysize, const JxlCmsInterface& cms, ThreadPool* pool,
1431
    FrameHeader& mutable_frame_header, ModularFrameEncoder& enc_modular,
1432
    PassesEncoderState& enc_state,
1433
0
    std::vector<std::unique_ptr<BitWriter>>* group_codes, AuxOut* aux_out) {
1434
0
  JXL_ENSURE(x0 + xsize <= frame_data.xsize);
1435
0
  JXL_ENSURE(y0 + ysize <= frame_data.ysize);
1436
0
  JxlMemoryManager* memory_manager = enc_state.memory_manager();
1437
0
  const FrameHeader& frame_header = mutable_frame_header;
1438
0
  PassesSharedState& shared = enc_state.shared;
1439
0
  shared.metadata = metadata;
1440
0
  if (enc_state.streaming_mode) {
1441
0
    shared.frame_dim.Set(
1442
0
        xsize, ysize, frame_header.group_size_shift,
1443
0
        /*max_hshift=*/0, /*max_vshift=*/0,
1444
0
        mutable_frame_header.encoding == FrameEncoding::kModular,
1445
0
        /*upsampling=*/1);
1446
0
  } else {
1447
0
    shared.frame_dim = frame_header.ToFrameDimensions();
1448
0
  }
1449
1450
0
  shared.image_features.patches.SetShared(&shared.reference_frames);
1451
0
  const FrameDimensions& frame_dim = shared.frame_dim;
1452
0
  JXL_ASSIGN_OR_RETURN(
1453
0
      shared.ac_strategy,
1454
0
      AcStrategyImage::Create(memory_manager, frame_dim.xsize_blocks,
1455
0
                              frame_dim.ysize_blocks));
1456
0
  JXL_ASSIGN_OR_RETURN(shared.raw_quant_field,
1457
0
                       ImageI::Create(memory_manager, frame_dim.xsize_blocks,
1458
0
                                      frame_dim.ysize_blocks));
1459
0
  JXL_ASSIGN_OR_RETURN(shared.epf_sharpness,
1460
0
                       ImageB::Create(memory_manager, frame_dim.xsize_blocks,
1461
0
                                      frame_dim.ysize_blocks));
1462
0
  JXL_ASSIGN_OR_RETURN(
1463
0
      shared.cmap, ColorCorrelationMap::Create(memory_manager, frame_dim.xsize,
1464
0
                                               frame_dim.ysize));
1465
0
  shared.coeff_order_size = kCoeffOrderMaxSize;
1466
0
  if (frame_header.encoding == FrameEncoding::kVarDCT) {
1467
0
    shared.coeff_orders.resize(frame_header.passes.num_passes *
1468
0
                               kCoeffOrderMaxSize);
1469
0
  }
1470
1471
0
  JXL_ASSIGN_OR_RETURN(shared.quant_dc,
1472
0
                       ImageB::Create(memory_manager, frame_dim.xsize_blocks,
1473
0
                                      frame_dim.ysize_blocks));
1474
0
  JXL_ASSIGN_OR_RETURN(shared.dc_storage,
1475
0
                       Image3F::Create(memory_manager, frame_dim.xsize_blocks,
1476
0
                                       frame_dim.ysize_blocks));
1477
0
  shared.dc = &shared.dc_storage;
1478
1479
0
  const size_t num_extra_channels = metadata->m.num_extra_channels;
1480
0
  const ExtraChannelInfo* alpha_eci = metadata->m.Find(ExtraChannel::kAlpha);
1481
0
  const ExtraChannelInfo* black_eci = metadata->m.Find(ExtraChannel::kBlack);
1482
0
  const size_t alpha_idx = alpha_eci - metadata->m.extra_channel_info.data();
1483
0
  const size_t black_idx = black_eci - metadata->m.extra_channel_info.data();
1484
0
  const ColorEncoding c_enc = metadata->m.color_encoding;
1485
1486
  // Make the image patch bigger than the currently processed group in streaming
1487
  // mode so that we can take into account border pixels around the group when
1488
  // computing inverse Gaborish and adaptive quantization map.
1489
0
  int max_border = enc_state.streaming_mode ? kBlockDim : 0;
1490
0
  Rect frame_rect(0, 0, frame_data.xsize, frame_data.ysize);
1491
0
  Rect frame_area_rect = Rect(x0, y0, xsize, ysize);
1492
0
  Rect patch_rect = frame_area_rect.Extend(max_border, frame_rect);
1493
0
  JXL_ENSURE(patch_rect.IsInside(frame_rect));
1494
1495
  // Allocating a large enough image avoids a copy when padding.
1496
0
  JXL_ASSIGN_OR_RETURN(
1497
0
      Image3F color,
1498
0
      Image3F::Create(memory_manager, RoundUpToBlockDim(patch_rect.xsize()),
1499
0
                      RoundUpToBlockDim(patch_rect.ysize())));
1500
0
  JXL_RETURN_IF_ERROR(color.ShrinkTo(patch_rect.xsize(), patch_rect.ysize()));
1501
0
  std::vector<ImageF> extra_channels(num_extra_channels);
1502
0
  for (auto& extra_channel : extra_channels) {
1503
0
    JXL_ASSIGN_OR_RETURN(
1504
0
        extra_channel,
1505
0
        ImageF::Create(memory_manager, patch_rect.xsize(), patch_rect.ysize()));
1506
0
  }
1507
0
  ImageF* alpha = alpha_eci ? &extra_channels[alpha_idx] : nullptr;
1508
0
  ImageF* black = black_eci ? &extra_channels[black_idx] : nullptr;
1509
0
  bool has_interleaved_alpha = false;
1510
0
  JxlChunkedFrameInputSource input = frame_data.GetInputSource();
1511
0
  if (!jpeg_data) {
1512
0
    JXL_RETURN_IF_ERROR(CopyColorChannels(input, patch_rect, frame_info,
1513
0
                                          metadata->m, pool, &color, alpha,
1514
0
                                          &has_interleaved_alpha));
1515
0
  }
1516
0
  JXL_RETURN_IF_ERROR(CopyExtraChannels(input, patch_rect, frame_info,
1517
0
                                        metadata->m, has_interleaved_alpha,
1518
0
                                        pool, &extra_channels));
1519
1520
0
  enc_state.cparams = cparams;
1521
1522
0
  Image3F linear_storage;
1523
0
  Image3F* linear = nullptr;
1524
1525
0
  if (!jpeg_data) {
1526
0
    if (frame_header.color_transform == ColorTransform::kXYB &&
1527
0
        frame_info.ib_needs_color_transform) {
1528
0
      if (frame_header.encoding == FrameEncoding::kVarDCT &&
1529
0
          cparams.speed_tier <= SpeedTier::kKitten) {
1530
0
        JXL_ASSIGN_OR_RETURN(linear_storage,
1531
0
                             Image3F::Create(memory_manager, patch_rect.xsize(),
1532
0
                                             patch_rect.ysize()));
1533
0
        linear = &linear_storage;
1534
0
      }
1535
0
      JXL_RETURN_IF_ERROR(ToXYB(c_enc, metadata->m.IntensityTarget(), black,
1536
0
                                pool, &color, cms, linear));
1537
0
    } else {
1538
      // Nothing to do.
1539
      // RGB or YCbCr: forward YCbCr is not implemented, this is only used when
1540
      // the input is already in YCbCr
1541
      // If encoding a special DC or reference frame: input is already in XYB.
1542
0
    }
1543
0
    bool lossless = cparams.IsLossless();
1544
0
    if (alpha && !alpha_eci->alpha_associated &&
1545
0
        frame_header.frame_type == FrameType::kRegularFrame &&
1546
0
        !ApplyOverride(cparams.keep_invisible, cparams.IsLossless()) &&
1547
0
        cparams.ec_resampling == cparams.resampling &&
1548
0
        !cparams.disable_perceptual_optimizations) {
1549
      // simplify invisible pixels
1550
0
      SimplifyInvisible(&color, *alpha, lossless);
1551
0
      if (linear) {
1552
0
        SimplifyInvisible(linear, *alpha, lossless);
1553
0
      }
1554
0
    }
1555
0
    JXL_RETURN_IF_ERROR(PadImageToBlockMultipleInPlace(&color));
1556
0
  }
1557
1558
  // Rectangle within color that corresponds to the currently processed group in
1559
  // streaming mode.
1560
0
  Rect group_rect(x0 - patch_rect.x0(), y0 - patch_rect.y0(),
1561
0
                  RoundUpToBlockDim(xsize), RoundUpToBlockDim(ysize));
1562
1563
0
  if (enc_state.initialize_global_state && !jpeg_data) {
1564
0
    ComputeChromacityAdjustments(cparams, color, group_rect,
1565
0
                                 &mutable_frame_header);
1566
0
  }
1567
1568
0
  bool has_jpeg_data = (jpeg_data != nullptr);
1569
0
  ComputeNoiseParams(cparams, enc_state.streaming_mode, has_jpeg_data, color,
1570
0
                     frame_dim, &mutable_frame_header,
1571
0
                     &shared.image_features.noise_params);
1572
1573
0
  JXL_RETURN_IF_ERROR(
1574
0
      DownsampleColorChannels(cparams, frame_header, has_jpeg_data, &color));
1575
1576
0
  if (cparams.ec_resampling != 1 && !cparams.already_downsampled) {
1577
0
    for (ImageF& ec : extra_channels) {
1578
0
      JXL_ASSIGN_OR_RETURN(ec, DownsampleImage(ec, cparams.ec_resampling));
1579
0
    }
1580
0
  }
1581
1582
0
  if (!enc_state.streaming_mode) {
1583
0
    group_rect = Rect(color);
1584
0
  }
1585
1586
0
  if (frame_header.encoding == FrameEncoding::kVarDCT) {
1587
0
    enc_state.passes.resize(enc_state.progressive_splitter.GetNumPasses());
1588
0
    for (PassesEncoderState::PassData& pass : enc_state.passes) {
1589
0
      pass.ac_tokens.resize(shared.frame_dim.num_groups);
1590
0
    }
1591
0
    if (jpeg_data) {
1592
0
      JXL_RETURN_IF_ERROR(ComputeJPEGTranscodingData(
1593
0
          *jpeg_data, frame_header, pool, &enc_modular, &enc_state));
1594
0
    } else {
1595
0
      JXL_RETURN_IF_ERROR(ComputeVarDCTEncodingData(
1596
0
          frame_header, linear, &color, group_rect, cms, pool, &enc_modular,
1597
0
          &enc_state, aux_out));
1598
0
    }
1599
0
    JXL_RETURN_IF_ERROR(ComputeAllCoeffOrders(enc_state, frame_dim));
1600
0
    if (!enc_state.streaming_mode) {
1601
0
      shared.num_histograms = 1;
1602
0
      enc_state.histogram_idx.resize(frame_dim.num_groups);
1603
0
    }
1604
0
    JXL_RETURN_IF_ERROR(
1605
0
        TokenizeAllCoefficients(frame_header, pool, &enc_state));
1606
0
  }
1607
1608
0
  if (cparams.modular_mode || !extra_channels.empty()) {
1609
0
    JXL_RETURN_IF_ERROR(enc_modular.ComputeEncodingData(
1610
0
        frame_header, metadata->m, &color, extra_channels, group_rect,
1611
0
        frame_dim, frame_area_rect, &enc_state, cms, pool, aux_out,
1612
0
        /*do_color=*/cparams.modular_mode));
1613
0
  }
1614
1615
0
  if (!enc_state.streaming_mode) {
1616
0
    if (cparams.speed_tier < SpeedTier::kTortoise ||
1617
0
        !cparams.ModularPartIsLossless() || cparams.responsive ||
1618
0
        !cparams.custom_fixed_tree.empty()) {
1619
      // Use local trees if doing lossless modular, unless at very slow speeds.
1620
0
      JXL_RETURN_IF_ERROR(enc_modular.ComputeTree(pool));
1621
0
      JXL_RETURN_IF_ERROR(enc_modular.ComputeTokens(pool));
1622
0
    }
1623
0
    mutable_frame_header.UpdateFlag(shared.image_features.patches.HasAny(),
1624
0
                                    FrameHeader::kPatches);
1625
0
    mutable_frame_header.UpdateFlag(shared.image_features.splines.HasAny(),
1626
0
                                    FrameHeader::kSplines);
1627
0
  }
1628
1629
0
  JXL_RETURN_IF_ERROR(EncodeGroups(frame_header, &enc_state, &enc_modular, pool,
1630
0
                                   group_codes, aux_out));
1631
0
  if (enc_state.streaming_mode) {
1632
0
    const size_t group_index = enc_state.dc_group_index;
1633
0
    enc_modular.ClearStreamData(ModularStreamId::VarDCTDC(group_index));
1634
0
    enc_modular.ClearStreamData(ModularStreamId::ACMetadata(group_index));
1635
0
    enc_modular.ClearModularStreamData();
1636
0
  }
1637
0
  return true;
1638
0
}
1639
1640
Status PermuteGroups(const CompressParams& cparams,
1641
                     const FrameDimensions& frame_dim, size_t num_passes,
1642
                     std::vector<coeff_order_t>* permutation,
1643
0
                     std::vector<std::unique_ptr<BitWriter>>* group_codes) {
1644
0
  const size_t num_groups = frame_dim.num_groups;
1645
0
  if (!cparams.centerfirst || (num_passes == 1 && num_groups == 1)) {
1646
0
    return true;
1647
0
  }
1648
  // Don't permute global DC/AC or DC.
1649
0
  permutation->resize(frame_dim.num_dc_groups + 2);
1650
0
  std::iota(permutation->begin(), permutation->end(), 0);
1651
0
  std::vector<coeff_order_t> ac_group_order(num_groups);
1652
0
  std::iota(ac_group_order.begin(), ac_group_order.end(), 0);
1653
0
  size_t group_dim = frame_dim.group_dim;
1654
1655
  // The center of the image is either given by parameters or chosen
1656
  // to be the middle of the image by default if center_x, center_y resp.
1657
  // are not provided.
1658
1659
0
  int64_t imag_cx;
1660
0
  if (cparams.center_x != static_cast<size_t>(-1)) {
1661
0
    JXL_RETURN_IF_ERROR(cparams.center_x < frame_dim.xsize);
1662
0
    imag_cx = cparams.center_x;
1663
0
  } else {
1664
0
    imag_cx = frame_dim.xsize / 2;
1665
0
  }
1666
1667
0
  int64_t imag_cy;
1668
0
  if (cparams.center_y != static_cast<size_t>(-1)) {
1669
0
    JXL_RETURN_IF_ERROR(cparams.center_y < frame_dim.ysize);
1670
0
    imag_cy = cparams.center_y;
1671
0
  } else {
1672
0
    imag_cy = frame_dim.ysize / 2;
1673
0
  }
1674
1675
  // The center of the group containing the center of the image.
1676
0
  int64_t cx = (imag_cx / group_dim) * group_dim + group_dim / 2;
1677
0
  int64_t cy = (imag_cy / group_dim) * group_dim + group_dim / 2;
1678
  // This identifies in what area of the central group the center of the image
1679
  // lies in.
1680
0
  double direction = -std::atan2(imag_cy - cy, imag_cx - cx);
1681
  // This identifies the side of the central group the center of the image
1682
  // lies closest to. This can take values 0, 1, 2, 3 corresponding to left,
1683
  // bottom, right, top.
1684
0
  int64_t side = std::fmod((direction + 5 * kPi / 4), 2 * kPi) * 2 / kPi;
1685
0
  auto get_distance_from_center = [&](size_t gid) {
1686
0
    Rect r = frame_dim.GroupRect(gid);
1687
0
    int64_t gcx = r.x0() + group_dim / 2;
1688
0
    int64_t gcy = r.y0() + group_dim / 2;
1689
0
    int64_t dx = gcx - cx;
1690
0
    int64_t dy = gcy - cy;
1691
    // The angle is determined by taking atan2 and adding an appropriate
1692
    // starting point depending on the side we want to start on.
1693
0
    double angle = std::remainder(
1694
0
        std::atan2(dy, dx) + kPi / 4 + side * (kPi / 2), 2 * kPi);
1695
    // Concentric squares in clockwise order.
1696
0
    return std::make_pair(std::max(std::abs(dx), std::abs(dy)), angle);
1697
0
  };
1698
0
  std::sort(ac_group_order.begin(), ac_group_order.end(),
1699
0
            [&](coeff_order_t a, coeff_order_t b) {
1700
0
              return get_distance_from_center(a) < get_distance_from_center(b);
1701
0
            });
1702
0
  std::vector<coeff_order_t> inv_ac_group_order(ac_group_order.size(), 0);
1703
0
  for (size_t i = 0; i < ac_group_order.size(); i++) {
1704
0
    inv_ac_group_order[ac_group_order[i]] = i;
1705
0
  }
1706
0
  for (size_t i = 0; i < num_passes; i++) {
1707
0
    size_t pass_start = permutation->size();
1708
0
    for (coeff_order_t v : inv_ac_group_order) {
1709
0
      permutation->push_back(pass_start + v);
1710
0
    }
1711
0
  }
1712
0
  std::vector<std::unique_ptr<BitWriter>> new_group_codes(group_codes->size());
1713
0
  for (size_t i = 0; i < permutation->size(); i++) {
1714
0
    new_group_codes[(*permutation)[i]] = std::move((*group_codes)[i]);
1715
0
  }
1716
0
  group_codes->swap(new_group_codes);
1717
0
  return true;
1718
0
}
1719
1720
bool CanDoStreamingEncoding(const CompressParams& cparams,
1721
                            const FrameInfo& frame_info,
1722
                            const CodecMetadata& metadata,
1723
0
                            const JxlEncoderChunkedFrameAdapter& frame_data) {
1724
0
  if (cparams.buffering == 0) {
1725
0
    return false;
1726
0
  }
1727
0
  if (cparams.buffering == -1) {
1728
0
    if (cparams.speed_tier < SpeedTier::kTortoise) return false;
1729
0
    if (cparams.speed_tier < SpeedTier::kSquirrel &&
1730
0
        cparams.butteraugli_distance > 0.5f) {
1731
0
      return false;
1732
0
    }
1733
0
    if (cparams.speed_tier == SpeedTier::kSquirrel &&
1734
0
        cparams.butteraugli_distance >= 3.f) {
1735
0
      return false;
1736
0
    }
1737
0
  }
1738
1739
  // TODO(veluca): handle different values of `buffering`.
1740
0
  if (frame_data.xsize <= 2048 && frame_data.ysize <= 2048) {
1741
0
    return false;
1742
0
  }
1743
0
  if (frame_data.IsJPEG()) {
1744
0
    return false;
1745
0
  }
1746
0
  if (cparams.noise == Override::kOn || cparams.patches == Override::kOn) {
1747
0
    return false;
1748
0
  }
1749
0
  if (cparams.progressive_dc != 0 || frame_info.dc_level != 0) {
1750
0
    return false;
1751
0
  }
1752
0
  if (cparams.resampling != 1 || cparams.ec_resampling != 1) {
1753
0
    return false;
1754
0
  }
1755
0
  if (cparams.max_error_mode) {
1756
0
    return false;
1757
0
  }
1758
0
  if (!cparams.ModularPartIsLossless() || cparams.responsive > 0) {
1759
0
    if (metadata.m.num_extra_channels > 0 || cparams.modular_mode) {
1760
0
      return false;
1761
0
    }
1762
0
  }
1763
0
  ColorTransform ok_color_transform =
1764
0
      cparams.modular_mode ? ColorTransform::kNone : ColorTransform::kXYB;
1765
0
  if (cparams.color_transform != ok_color_transform) {
1766
0
    return false;
1767
0
  }
1768
0
  return true;
1769
0
}
1770
1771
Status ComputePermutationForStreaming(size_t xsize, size_t ysize,
1772
                                      size_t group_size, size_t num_passes,
1773
                                      std::vector<coeff_order_t>& permutation,
1774
0
                                      std::vector<size_t>& dc_group_order) {
1775
  // This is only valid in VarDCT mode, otherwise there can be group shift.
1776
0
  const size_t dc_group_size = group_size * kBlockDim;
1777
0
  const size_t group_xsize = DivCeil(xsize, group_size);
1778
0
  const size_t group_ysize = DivCeil(ysize, group_size);
1779
0
  const size_t dc_group_xsize = DivCeil(xsize, dc_group_size);
1780
0
  const size_t dc_group_ysize = DivCeil(ysize, dc_group_size);
1781
0
  const size_t num_groups = group_xsize * group_ysize;
1782
0
  const size_t num_dc_groups = dc_group_xsize * dc_group_ysize;
1783
0
  const size_t num_sections = 2 + num_dc_groups + num_passes * num_groups;
1784
0
  permutation.resize(num_sections);
1785
0
  size_t new_ix = 0;
1786
  // DC Global is first
1787
0
  permutation[0] = new_ix++;
1788
  // TODO(szabadka) Change the dc group order to center-first.
1789
0
  for (size_t dc_y = 0; dc_y < dc_group_ysize; ++dc_y) {
1790
0
    for (size_t dc_x = 0; dc_x < dc_group_xsize; ++dc_x) {
1791
0
      size_t dc_ix = dc_y * dc_group_xsize + dc_x;
1792
0
      dc_group_order.push_back(dc_ix);
1793
0
      permutation[1 + dc_ix] = new_ix++;
1794
0
      size_t ac_y0 = dc_y * kBlockDim;
1795
0
      size_t ac_x0 = dc_x * kBlockDim;
1796
0
      size_t ac_y1 = std::min<size_t>(group_ysize, ac_y0 + kBlockDim);
1797
0
      size_t ac_x1 = std::min<size_t>(group_xsize, ac_x0 + kBlockDim);
1798
0
      for (size_t pass = 0; pass < num_passes; ++pass) {
1799
0
        for (size_t ac_y = ac_y0; ac_y < ac_y1; ++ac_y) {
1800
0
          for (size_t ac_x = ac_x0; ac_x < ac_x1; ++ac_x) {
1801
0
            size_t group_ix = ac_y * group_xsize + ac_x;
1802
0
            size_t old_ix =
1803
0
                AcGroupIndex(pass, group_ix, num_groups, num_dc_groups);
1804
0
            permutation[old_ix] = new_ix++;
1805
0
          }
1806
0
        }
1807
0
      }
1808
0
    }
1809
0
  }
1810
  // AC Global is last
1811
0
  permutation[1 + num_dc_groups] = new_ix++;
1812
0
  JXL_ENSURE(new_ix == num_sections);
1813
0
  return true;
1814
0
}
1815
1816
constexpr size_t kGroupSizeOffset[4] = {
1817
    static_cast<size_t>(0),
1818
    static_cast<size_t>(1024),
1819
    static_cast<size_t>(17408),
1820
    static_cast<size_t>(4211712),
1821
};
1822
constexpr size_t kTOCBits[4] = {12, 16, 24, 32};
1823
1824
0
size_t TOCBucket(size_t group_size) {
1825
0
  size_t bucket = 0;
1826
0
  while (bucket < 3 && group_size >= kGroupSizeOffset[bucket + 1]) ++bucket;
1827
0
  return bucket;
1828
0
}
1829
1830
0
size_t TOCSize(const std::vector<size_t>& group_sizes) {
1831
0
  size_t toc_bits = 0;
1832
0
  for (size_t group_size : group_sizes) {
1833
0
    toc_bits += kTOCBits[TOCBucket(group_size)];
1834
0
  }
1835
0
  return (toc_bits + 7) / 8;
1836
0
}
1837
1838
StatusOr<PaddedBytes> EncodeTOC(JxlMemoryManager* memory_manager,
1839
                                const std::vector<size_t>& group_sizes,
1840
0
                                AuxOut* aux_out) {
1841
0
  BitWriter writer{memory_manager};
1842
0
  JXL_RETURN_IF_ERROR(writer.WithMaxBits(
1843
0
      32 * group_sizes.size(), LayerType::Toc, aux_out, [&]() -> Status {
1844
0
        for (size_t group_size : group_sizes) {
1845
0
          JXL_RETURN_IF_ERROR(U32Coder::Write(kTocDist, group_size, &writer));
1846
0
        }
1847
0
        writer.ZeroPadToByte();  // before first group
1848
0
        return true;
1849
0
      }));
1850
0
  return std::move(writer).TakeBytes();
1851
0
}
1852
1853
Status ComputeGroupDataOffset(size_t frame_header_size, size_t dc_global_size,
1854
                              size_t num_sections, size_t& min_dc_global_size,
1855
0
                              size_t& group_offset) {
1856
0
  size_t max_toc_bits = (num_sections - 1) * 32;
1857
0
  size_t min_toc_bits = (num_sections - 1) * 12;
1858
0
  size_t max_padding = (max_toc_bits - min_toc_bits + 7) / 8;
1859
0
  min_dc_global_size = dc_global_size;
1860
0
  size_t dc_global_bucket = TOCBucket(min_dc_global_size);
1861
0
  while (TOCBucket(min_dc_global_size + max_padding) > dc_global_bucket) {
1862
0
    dc_global_bucket = TOCBucket(min_dc_global_size + max_padding);
1863
0
    min_dc_global_size = kGroupSizeOffset[dc_global_bucket];
1864
0
  }
1865
0
  JXL_ENSURE(TOCBucket(min_dc_global_size) == dc_global_bucket);
1866
0
  JXL_ENSURE(TOCBucket(min_dc_global_size + max_padding) == dc_global_bucket);
1867
0
  max_toc_bits += kTOCBits[dc_global_bucket];
1868
0
  size_t max_toc_size = (max_toc_bits + 7) / 8;
1869
0
  group_offset = frame_header_size + max_toc_size + min_dc_global_size;
1870
0
  return true;
1871
0
}
1872
1873
size_t ComputeDcGlobalPadding(const std::vector<size_t>& group_sizes,
1874
                              size_t frame_header_size,
1875
                              size_t group_data_offset,
1876
0
                              size_t min_dc_global_size) {
1877
0
  std::vector<size_t> new_group_sizes = group_sizes;
1878
0
  new_group_sizes[0] = min_dc_global_size;
1879
0
  size_t toc_size = TOCSize(new_group_sizes);
1880
0
  size_t actual_offset = frame_header_size + toc_size + group_sizes[0];
1881
0
  return group_data_offset - actual_offset;
1882
0
}
1883
1884
Status OutputGroups(std::vector<std::unique_ptr<BitWriter>>&& group_codes,
1885
                    std::vector<size_t>* group_sizes,
1886
0
                    JxlEncoderOutputProcessorWrapper* output_processor) {
1887
0
  JXL_ENSURE(group_codes.size() >= 4);
1888
0
  {
1889
0
    PaddedBytes dc_group = std::move(*group_codes[1]).TakeBytes();
1890
0
    group_sizes->push_back(dc_group.size());
1891
0
    JXL_RETURN_IF_ERROR(AppendData(*output_processor, dc_group));
1892
0
  }
1893
0
  for (size_t i = 3; i < group_codes.size(); ++i) {
1894
0
    PaddedBytes ac_group = std::move(*group_codes[i]).TakeBytes();
1895
0
    group_sizes->push_back(ac_group.size());
1896
0
    JXL_RETURN_IF_ERROR(AppendData(*output_processor, ac_group));
1897
0
  }
1898
0
  return true;
1899
0
}
1900
1901
void RemoveUnusedHistograms(std::vector<uint8_t>& context_map,
1902
0
                            EntropyEncodingData& codes) {
1903
0
  std::vector<int> remap(256, -1);
1904
0
  std::vector<uint8_t> inv_remap;
1905
0
  for (uint8_t& context : context_map) {
1906
0
    const uint8_t histo_ix = context;
1907
0
    if (remap[histo_ix] == -1) {
1908
0
      remap[histo_ix] = inv_remap.size();
1909
0
      inv_remap.push_back(histo_ix);
1910
0
    }
1911
0
    context = remap[histo_ix];
1912
0
  }
1913
0
  EntropyEncodingData new_codes;
1914
0
  new_codes.use_prefix_code = codes.use_prefix_code;
1915
0
  new_codes.lz77 = codes.lz77;
1916
0
  for (uint8_t histo_idx : inv_remap) {
1917
0
    new_codes.encoding_info.emplace_back(
1918
0
        std::move(codes.encoding_info[histo_idx]));
1919
0
    new_codes.uint_config.emplace_back(codes.uint_config[histo_idx]);
1920
0
    new_codes.encoded_histograms.emplace_back(
1921
0
        std::move(codes.encoded_histograms[histo_idx]));
1922
0
  }
1923
0
  codes = std::move(new_codes);
1924
0
}
1925
1926
Status OutputAcGlobal(PassesEncoderState& enc_state,
1927
                      const FrameDimensions& frame_dim,
1928
                      std::vector<size_t>* group_sizes,
1929
                      JxlEncoderOutputProcessorWrapper* output_processor,
1930
0
                      AuxOut* aux_out) {
1931
0
  JXL_ENSURE(frame_dim.num_groups > 1);
1932
0
  JxlMemoryManager* memory_manager = enc_state.memory_manager();
1933
0
  BitWriter writer{memory_manager};
1934
0
  {
1935
0
    size_t num_histo_bits = CeilLog2Nonzero(frame_dim.num_groups);
1936
0
    JXL_RETURN_IF_ERROR(
1937
0
        writer.WithMaxBits(num_histo_bits + 1, LayerType::Ac, aux_out, [&] {
1938
0
          writer.Write(1, 1);  // default dequant matrices
1939
0
          writer.Write(num_histo_bits, frame_dim.num_dc_groups - 1);
1940
0
          return true;
1941
0
        }));
1942
0
  }
1943
0
  const PassesSharedState& shared = enc_state.shared;
1944
0
  for (size_t i = 0; i < enc_state.progressive_splitter.GetNumPasses(); i++) {
1945
    // Encode coefficient orders.
1946
0
    size_t order_bits = 0;
1947
0
    JXL_RETURN_IF_ERROR(
1948
0
        U32Coder::CanEncode(kOrderEnc, enc_state.used_orders[i], &order_bits));
1949
0
    JXL_RETURN_IF_ERROR(
1950
0
        writer.WithMaxBits(order_bits, LayerType::Order, aux_out, [&] {
1951
0
          return U32Coder::Write(kOrderEnc, enc_state.used_orders[i], &writer);
1952
0
        }));
1953
0
    JXL_RETURN_IF_ERROR(
1954
0
        EncodeCoeffOrders(enc_state.used_orders[i],
1955
0
                          &shared.coeff_orders[i * shared.coeff_order_size],
1956
0
                          &writer, LayerType::Order, aux_out));
1957
    // Fix up context map and entropy codes to remove any fix histograms that
1958
    // were not selected by clustering.
1959
0
    RemoveUnusedHistograms(enc_state.passes[i].context_map,
1960
0
                           enc_state.passes[i].codes);
1961
0
    JXL_RETURN_IF_ERROR(EncodeHistograms(enc_state.passes[i].context_map,
1962
0
                                         enc_state.passes[i].codes, &writer,
1963
0
                                         LayerType::Ac, aux_out));
1964
0
  }
1965
0
  JXL_RETURN_IF_ERROR(writer.WithMaxBits(8, LayerType::Ac, aux_out, [&] {
1966
0
    writer.ZeroPadToByte();  // end of group.
1967
0
    return true;
1968
0
  }));
1969
0
  PaddedBytes ac_global = std::move(writer).TakeBytes();
1970
0
  group_sizes->push_back(ac_global.size());
1971
0
  JXL_RETURN_IF_ERROR(AppendData(*output_processor, ac_global));
1972
0
  return true;
1973
0
}
1974
1975
Status EncodeFrameStreaming(JxlMemoryManager* memory_manager,
1976
                            const CompressParams& cparams,
1977
                            const FrameInfo& frame_info,
1978
                            const CodecMetadata* metadata,
1979
                            JxlEncoderChunkedFrameAdapter& frame_data,
1980
                            const JxlCmsInterface& cms, ThreadPool* pool,
1981
                            JxlEncoderOutputProcessorWrapper* output_processor,
1982
0
                            AuxOut* aux_out) {
1983
0
  PassesEncoderState enc_state{memory_manager};
1984
0
  SetProgressiveMode(cparams, &enc_state.progressive_splitter);
1985
0
  FrameHeader frame_header(metadata);
1986
0
  std::unique_ptr<jpeg::JPEGData> jpeg_data;
1987
0
  if (frame_data.IsJPEG()) {
1988
0
    jpeg_data = frame_data.TakeJPEGData();
1989
0
    JXL_ENSURE(jpeg_data);
1990
0
  }
1991
0
  JXL_RETURN_IF_ERROR(MakeFrameHeader(frame_data.xsize, frame_data.ysize,
1992
0
                                      cparams, enc_state.progressive_splitter,
1993
0
                                      frame_info, jpeg_data.get(), true,
1994
0
                                      &frame_header));
1995
0
  const size_t num_passes = enc_state.progressive_splitter.GetNumPasses();
1996
0
  JXL_ASSIGN_OR_RETURN(
1997
0
      ModularFrameEncoder enc_modular,
1998
0
      ModularFrameEncoder::Create(memory_manager, frame_header, cparams, true));
1999
0
  std::vector<coeff_order_t> permutation;
2000
0
  std::vector<size_t> dc_group_order;
2001
0
  size_t group_size = frame_header.ToFrameDimensions().group_dim;
2002
0
  JXL_RETURN_IF_ERROR(ComputePermutationForStreaming(
2003
0
      frame_data.xsize, frame_data.ysize, group_size, num_passes, permutation,
2004
0
      dc_group_order));
2005
0
  enc_state.shared.num_histograms = dc_group_order.size();
2006
0
  size_t dc_group_size = group_size * kBlockDim;
2007
0
  size_t dc_group_xsize = DivCeil(frame_data.xsize, dc_group_size);
2008
0
  size_t min_dc_global_size = 0;
2009
0
  size_t group_data_offset = 0;
2010
0
  PaddedBytes frame_header_bytes{memory_manager};
2011
0
  PaddedBytes dc_global_bytes{memory_manager};
2012
0
  std::vector<size_t> group_sizes;
2013
0
  size_t start_pos = output_processor->CurrentPosition();
2014
0
  for (size_t i = 0; i < dc_group_order.size(); ++i) {
2015
0
    size_t dc_ix = dc_group_order[i];
2016
0
    size_t dc_y = dc_ix / dc_group_xsize;
2017
0
    size_t dc_x = dc_ix % dc_group_xsize;
2018
0
    size_t y0 = dc_y * dc_group_size;
2019
0
    size_t x0 = dc_x * dc_group_size;
2020
0
    size_t ysize = std::min<size_t>(dc_group_size, frame_data.ysize - y0);
2021
0
    size_t xsize = std::min<size_t>(dc_group_size, frame_data.xsize - x0);
2022
0
    size_t group_xsize = DivCeil(xsize, group_size);
2023
0
    size_t group_ysize = DivCeil(ysize, group_size);
2024
0
    JXL_DEBUG_V(2,
2025
0
                "Encoding DC group #%" PRIuS " dc_y = %" PRIuS " dc_x = %" PRIuS
2026
0
                " (x0, y0) = (%" PRIuS ", %" PRIuS ") (xsize, ysize) = (%" PRIuS
2027
0
                ", %" PRIuS ")",
2028
0
                dc_ix, dc_y, dc_x, x0, y0, xsize, ysize);
2029
0
    enc_state.streaming_mode = true;
2030
0
    enc_state.initialize_global_state = (i == 0);
2031
0
    enc_state.dc_group_index = dc_ix;
2032
0
    enc_state.histogram_idx = std::vector<size_t>(group_xsize * group_ysize, i);
2033
0
    std::vector<std::unique_ptr<BitWriter>> group_codes;
2034
0
    JXL_RETURN_IF_ERROR(ComputeEncodingData(
2035
0
        cparams, frame_info, metadata, frame_data, jpeg_data.get(), x0, y0,
2036
0
        xsize, ysize, cms, pool, frame_header, enc_modular, enc_state,
2037
0
        &group_codes, aux_out));
2038
0
    JXL_ENSURE(enc_state.special_frames.empty());
2039
0
    if (i == 0) {
2040
0
      BitWriter writer{memory_manager};
2041
0
      JXL_RETURN_IF_ERROR(WriteFrameHeader(frame_header, &writer, aux_out));
2042
0
      JXL_RETURN_IF_ERROR(
2043
0
          writer.WithMaxBits(8, LayerType::Header, aux_out, [&]() -> Status {
2044
0
            writer.Write(1, 1);  // write permutation
2045
0
            JXL_RETURN_IF_ERROR(EncodePermutation(
2046
0
                permutation.data(), /*skip=*/0, permutation.size(), &writer,
2047
0
                LayerType::Header, aux_out));
2048
0
            writer.ZeroPadToByte();
2049
0
            return true;
2050
0
          }));
2051
0
      frame_header_bytes = std::move(writer).TakeBytes();
2052
0
      dc_global_bytes = std::move(*group_codes[0]).TakeBytes();
2053
0
      JXL_RETURN_IF_ERROR(ComputeGroupDataOffset(
2054
0
          frame_header_bytes.size(), dc_global_bytes.size(), permutation.size(),
2055
0
          min_dc_global_size, group_data_offset));
2056
0
      JXL_DEBUG_V(2, "Frame header size: %" PRIuS, frame_header_bytes.size());
2057
0
      JXL_DEBUG_V(2, "DC global size: %" PRIuS ", min size for TOC: %" PRIuS,
2058
0
                  dc_global_bytes.size(), min_dc_global_size);
2059
0
      JXL_DEBUG_V(2, "Num groups: %" PRIuS " group data offset: %" PRIuS,
2060
0
                  permutation.size(), group_data_offset);
2061
0
      group_sizes.push_back(dc_global_bytes.size());
2062
0
      JXL_RETURN_IF_ERROR(
2063
0
          output_processor->Seek(start_pos + group_data_offset));
2064
0
    }
2065
0
    JXL_RETURN_IF_ERROR(
2066
0
        OutputGroups(std::move(group_codes), &group_sizes, output_processor));
2067
0
  }
2068
0
  if (frame_header.encoding == FrameEncoding::kVarDCT) {
2069
0
    JXL_RETURN_IF_ERROR(
2070
0
        OutputAcGlobal(enc_state, frame_header.ToFrameDimensions(),
2071
0
                       &group_sizes, output_processor, aux_out));
2072
0
  } else {
2073
0
    group_sizes.push_back(0);
2074
0
  }
2075
0
  JXL_ENSURE(group_sizes.size() == permutation.size());
2076
0
  size_t end_pos = output_processor->CurrentPosition();
2077
0
  JXL_RETURN_IF_ERROR(output_processor->Seek(start_pos));
2078
0
  size_t padding_size =
2079
0
      ComputeDcGlobalPadding(group_sizes, frame_header_bytes.size(),
2080
0
                             group_data_offset, min_dc_global_size);
2081
0
  group_sizes[0] += padding_size;
2082
0
  JXL_ASSIGN_OR_RETURN(PaddedBytes toc_bytes,
2083
0
                       EncodeTOC(memory_manager, group_sizes, aux_out));
2084
0
  std::vector<uint8_t> padding_bytes(padding_size);
2085
0
  JXL_RETURN_IF_ERROR(AppendData(*output_processor, frame_header_bytes));
2086
0
  JXL_RETURN_IF_ERROR(AppendData(*output_processor, toc_bytes));
2087
0
  JXL_RETURN_IF_ERROR(AppendData(*output_processor, dc_global_bytes));
2088
0
  JXL_RETURN_IF_ERROR(AppendData(*output_processor, padding_bytes));
2089
0
  JXL_DEBUG_V(2, "TOC size: %" PRIuS " padding bytes after DC global: %" PRIuS,
2090
0
              toc_bytes.size(), padding_size);
2091
0
  JXL_ENSURE(output_processor->CurrentPosition() ==
2092
0
             start_pos + group_data_offset);
2093
0
  JXL_RETURN_IF_ERROR(output_processor->Seek(end_pos));
2094
0
  return true;
2095
0
}
2096
2097
Status EncodeFrameOneShot(JxlMemoryManager* memory_manager,
2098
                          const CompressParams& cparams,
2099
                          const FrameInfo& frame_info,
2100
                          const CodecMetadata* metadata,
2101
                          JxlEncoderChunkedFrameAdapter& frame_data,
2102
                          const JxlCmsInterface& cms, ThreadPool* pool,
2103
                          JxlEncoderOutputProcessorWrapper* output_processor,
2104
0
                          AuxOut* aux_out) {
2105
0
  PassesEncoderState enc_state{memory_manager};
2106
0
  SetProgressiveMode(cparams, &enc_state.progressive_splitter);
2107
0
  FrameHeader frame_header(metadata);
2108
0
  std::unique_ptr<jpeg::JPEGData> jpeg_data;
2109
0
  if (frame_data.IsJPEG()) {
2110
0
    jpeg_data = frame_data.TakeJPEGData();
2111
0
    JXL_ENSURE(jpeg_data);
2112
0
  }
2113
0
  JXL_RETURN_IF_ERROR(MakeFrameHeader(frame_data.xsize, frame_data.ysize,
2114
0
                                      cparams, enc_state.progressive_splitter,
2115
0
                                      frame_info, jpeg_data.get(), false,
2116
0
                                      &frame_header));
2117
0
  const size_t num_passes = enc_state.progressive_splitter.GetNumPasses();
2118
0
  JXL_ASSIGN_OR_RETURN(ModularFrameEncoder enc_modular,
2119
0
                       ModularFrameEncoder::Create(memory_manager, frame_header,
2120
0
                                                   cparams, false));
2121
0
  std::vector<std::unique_ptr<BitWriter>> group_codes;
2122
0
  JXL_RETURN_IF_ERROR(ComputeEncodingData(
2123
0
      cparams, frame_info, metadata, frame_data, jpeg_data.get(), 0, 0,
2124
0
      frame_data.xsize, frame_data.ysize, cms, pool, frame_header, enc_modular,
2125
0
      enc_state, &group_codes, aux_out));
2126
2127
0
  BitWriter writer{memory_manager};
2128
0
  JXL_RETURN_IF_ERROR(writer.AppendByteAligned(enc_state.special_frames));
2129
0
  JXL_RETURN_IF_ERROR(WriteFrameHeader(frame_header, &writer, aux_out));
2130
2131
0
  std::vector<coeff_order_t> permutation;
2132
0
  JXL_RETURN_IF_ERROR(PermuteGroups(cparams, enc_state.shared.frame_dim,
2133
0
                                    num_passes, &permutation, &group_codes));
2134
2135
0
  JXL_RETURN_IF_ERROR(
2136
0
      WriteGroupOffsets(group_codes, permutation, &writer, aux_out));
2137
2138
0
  JXL_RETURN_IF_ERROR(writer.AppendByteAligned(group_codes));
2139
0
  PaddedBytes frame_bytes = std::move(writer).TakeBytes();
2140
0
  JXL_RETURN_IF_ERROR(AppendData(*output_processor, frame_bytes));
2141
2142
0
  return true;
2143
0
}
2144
2145
}  // namespace
2146
2147
std::vector<CompressParams> TectonicPlateSettingsLessPalette(
2148
0
    const CompressParams& cparams_orig) {
2149
0
  std::vector<CompressParams> all_params;
2150
0
  CompressParams cparams_attempt = cparams_orig;
2151
0
  cparams_attempt.speed_tier = SpeedTier::kGlacier;
2152
2153
0
  cparams_attempt.options.max_properties = 4;
2154
0
  cparams_attempt.options.nb_repeats = 1.0f;
2155
0
  cparams_attempt.modular_group_size_shift = 0;
2156
0
  cparams_attempt.channel_colors_percent = 0;
2157
0
  cparams_attempt.options.predictor = Predictor::Variable;
2158
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2159
0
  cparams_attempt.palette_colors = 1024;
2160
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2161
0
  cparams_attempt.patches = Override::kDefault;
2162
0
  all_params.push_back(cparams_attempt);
2163
0
  cparams_attempt.channel_colors_percent = 80.f;
2164
0
  cparams_attempt.modular_group_size_shift = 1;
2165
0
  cparams_attempt.palette_colors = 0;
2166
0
  cparams_attempt.channel_colors_pre_transform_percent = 0;
2167
0
  all_params.push_back(cparams_attempt);
2168
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2169
0
  cparams_attempt.modular_group_size_shift = 2;
2170
0
  all_params.push_back(cparams_attempt);
2171
0
  cparams_attempt.modular_group_size_shift = 3;
2172
0
  cparams_attempt.patches = Override::kOff;
2173
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2174
0
  all_params.push_back(cparams_attempt);
2175
0
  cparams_attempt.palette_colors = 1024;
2176
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2177
0
  all_params.push_back(cparams_attempt);
2178
0
  cparams_attempt.patches = Override::kDefault;
2179
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2180
0
  all_params.push_back(cparams_attempt);
2181
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2182
0
  cparams_attempt.channel_colors_pre_transform_percent = 0;
2183
0
  all_params.push_back(cparams_attempt);
2184
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2185
0
  cparams_attempt.options.nb_repeats = 0.9f;
2186
0
  cparams_attempt.modular_group_size_shift = 2;
2187
0
  all_params.push_back(cparams_attempt);
2188
0
  cparams_attempt.modular_group_size_shift = 3;
2189
0
  cparams_attempt.palette_colors = 0;
2190
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2191
0
  all_params.push_back(cparams_attempt);
2192
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2193
0
  cparams_attempt.channel_colors_pre_transform_percent = 0;
2194
0
  all_params.push_back(cparams_attempt);
2195
0
  cparams_attempt.palette_colors = 1024;
2196
0
  cparams_attempt.options.nb_repeats = 0.95f;
2197
0
  cparams_attempt.modular_group_size_shift = 1;
2198
0
  cparams_attempt.channel_colors_percent = 0;
2199
0
  all_params.push_back(cparams_attempt);
2200
0
  cparams_attempt.modular_group_size_shift = 2;
2201
0
  cparams_attempt.palette_colors = 0;
2202
0
  all_params.push_back(cparams_attempt);
2203
0
  cparams_attempt.channel_colors_percent = 80.f;
2204
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2205
0
  all_params.push_back(cparams_attempt);
2206
0
  cparams_attempt.palette_colors = 1024;
2207
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2208
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2209
0
  cparams_attempt.modular_group_size_shift = 3;
2210
0
  all_params.push_back(cparams_attempt);
2211
0
  cparams_attempt.palette_colors = 0;
2212
0
  cparams_attempt.patches = Override::kOff;
2213
0
  all_params.push_back(cparams_attempt);
2214
0
  cparams_attempt.patches = Override::kDefault;
2215
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2216
0
  all_params.push_back(cparams_attempt);
2217
0
  cparams_attempt.palette_colors = 1024;
2218
0
  cparams_attempt.patches = Override::kOff;
2219
0
  all_params.push_back(cparams_attempt);
2220
0
  cparams_attempt.options.nb_repeats = 0.5f;
2221
0
  cparams_attempt.patches = Override::kDefault;
2222
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2223
0
  all_params.push_back(cparams_attempt);
2224
0
  cparams_attempt.options.predictor = Predictor::Zero;
2225
0
  cparams_attempt.options.nb_repeats = 0;
2226
0
  cparams_attempt.channel_colors_percent = 0;
2227
0
  cparams_attempt.channel_colors_pre_transform_percent = 0;
2228
0
  cparams_attempt.patches = Override::kOff;
2229
0
  all_params.push_back(cparams_attempt);
2230
0
  cparams_attempt.channel_colors_percent = 80.f;
2231
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2232
0
  cparams_attempt.options.nb_repeats = 1.0f;
2233
0
  cparams_attempt.palette_colors = 0;
2234
0
  all_params.push_back(cparams_attempt);
2235
0
  cparams_attempt.patches = Override::kDefault;
2236
0
  cparams_attempt.options.predictor = Predictor::Best;
2237
0
  all_params.push_back(cparams_attempt);
2238
0
  cparams_attempt.options.nb_repeats = 0.9f;
2239
0
  cparams_attempt.patches = Override::kOff;
2240
0
  all_params.push_back(cparams_attempt);
2241
0
  cparams_attempt.palette_colors = 1024;
2242
0
  cparams_attempt.patches = Override::kDefault;
2243
0
  cparams_attempt.options.predictor = Predictor::Weighted;
2244
0
  cparams_attempt.options.nb_repeats = 1.0f;
2245
0
  all_params.push_back(cparams_attempt);
2246
0
  cparams_attempt.options.nb_repeats = 0.95f;
2247
0
  cparams_attempt.modular_group_size_shift = 2;
2248
0
  cparams_attempt.palette_colors = 0;
2249
0
  cparams_attempt.channel_colors_pre_transform_percent = 0;
2250
0
  all_params.push_back(cparams_attempt);
2251
0
  return all_params;
2252
0
}
2253
2254
std::vector<CompressParams> TectonicPlateSettingsMorePalette(
2255
0
    const CompressParams& cparams_orig) {
2256
0
  std::vector<CompressParams> all_params;
2257
0
  CompressParams cparams_attempt = cparams_orig;
2258
0
  cparams_attempt.speed_tier = SpeedTier::kGlacier;
2259
2260
0
  cparams_attempt.options.max_properties = 4;
2261
0
  cparams_attempt.options.nb_repeats = 1.0f;
2262
0
  cparams_attempt.modular_group_size_shift = 0;
2263
0
  cparams_attempt.palette_colors = 70000;
2264
0
  cparams_attempt.options.predictor = Predictor::Variable;
2265
0
  cparams_attempt.channel_colors_percent = 80.f;
2266
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2267
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2268
0
  cparams_attempt.patches = Override::kDefault;
2269
0
  all_params.push_back(cparams_attempt);
2270
0
  cparams_attempt.modular_group_size_shift = 2;
2271
0
  cparams_attempt.channel_colors_percent = 0;
2272
0
  cparams_attempt.patches = Override::kOff;
2273
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2274
0
  all_params.push_back(cparams_attempt);
2275
0
  cparams_attempt.channel_colors_percent = 80.f;
2276
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2277
0
  cparams_attempt.modular_group_size_shift = 3;
2278
0
  all_params.push_back(cparams_attempt);
2279
0
  cparams_attempt.options.nb_repeats = 0.9f;
2280
0
  all_params.push_back(cparams_attempt);
2281
0
  cparams_attempt.patches = Override::kDefault;
2282
0
  cparams_attempt.options.nb_repeats = 0.95f;
2283
0
  cparams_attempt.modular_group_size_shift = 0;
2284
0
  all_params.push_back(cparams_attempt);
2285
0
  cparams_attempt.modular_group_size_shift = 3;
2286
0
  all_params.push_back(cparams_attempt);
2287
0
  cparams_attempt.patches = Override::kOff;
2288
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2289
0
  all_params.push_back(cparams_attempt);
2290
0
  cparams_attempt.options.nb_repeats = 0.5f;
2291
0
  all_params.push_back(cparams_attempt);
2292
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2293
0
  cparams_attempt.options.predictor = Predictor::Zero;
2294
0
  cparams_attempt.options.nb_repeats = 0;
2295
0
  all_params.push_back(cparams_attempt);
2296
0
  cparams_attempt.patches = Override::kDefault;
2297
0
  cparams_attempt.channel_colors_pre_transform_percent = 0;
2298
0
  all_params.push_back(cparams_attempt);
2299
0
  cparams_attempt.options.nb_repeats = 0.01f;
2300
0
  cparams_attempt.palette_colors = 0;
2301
0
  cparams_attempt.patches = Override::kOff;
2302
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2303
0
  all_params.push_back(cparams_attempt);
2304
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2305
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2306
0
  cparams_attempt.palette_colors = 70000;
2307
0
  all_params.push_back(cparams_attempt);
2308
0
  cparams_attempt.options.nb_repeats = 1.0f;
2309
0
  cparams_attempt.modular_group_size_shift = 0;
2310
0
  cparams_attempt.channel_colors_percent = 0;
2311
0
  cparams_attempt.channel_colors_pre_transform_percent = 0;
2312
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2313
0
  all_params.push_back(cparams_attempt);
2314
0
  cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2315
0
  cparams_attempt.modular_group_size_shift = 1;
2316
0
  all_params.push_back(cparams_attempt);
2317
0
  cparams_attempt.modular_group_size_shift = 2;
2318
0
  all_params.push_back(cparams_attempt);
2319
0
  cparams_attempt.channel_colors_percent = 80.f;
2320
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2321
0
  cparams_attempt.modular_group_size_shift = 3;
2322
0
  all_params.push_back(cparams_attempt);
2323
0
  cparams_attempt.options.nb_repeats = 0.5f;
2324
0
  cparams_attempt.modular_group_size_shift = 1;
2325
0
  cparams_attempt.channel_colors_percent = 0;
2326
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2327
0
  all_params.push_back(cparams_attempt);
2328
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2329
0
  cparams_attempt.modular_group_size_shift = 2;
2330
0
  all_params.push_back(cparams_attempt);
2331
0
  cparams_attempt.channel_colors_percent = 80.f;
2332
0
  cparams_attempt.modular_group_size_shift = 3;
2333
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2334
0
  all_params.push_back(cparams_attempt);
2335
0
  cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2336
0
  cparams_attempt.options.predictor = Predictor::Select;
2337
0
  cparams_attempt.options.nb_repeats = 1.0f;
2338
0
  all_params.push_back(cparams_attempt);
2339
0
  return all_params;
2340
0
}
2341
2342
Status EncodeFrame(JxlMemoryManager* memory_manager,
2343
                   const CompressParams& cparams_orig,
2344
                   const FrameInfo& frame_info, const CodecMetadata* metadata,
2345
                   JxlEncoderChunkedFrameAdapter& frame_data,
2346
                   const JxlCmsInterface& cms, ThreadPool* pool,
2347
                   JxlEncoderOutputProcessorWrapper* output_processor,
2348
0
                   AuxOut* aux_out) {
2349
0
  CompressParams cparams = cparams_orig;
2350
0
  if (cparams.speed_tier == SpeedTier::kTectonicPlate &&
2351
0
      !cparams.IsLossless()) {
2352
0
    cparams.speed_tier = SpeedTier::kGlacier;
2353
0
  }
2354
  // Lightning mode is handled externally, so switch to Thunder mode to handle
2355
  // potentially weird cases.
2356
0
  if (cparams.speed_tier == SpeedTier::kLightning) {
2357
0
    cparams.speed_tier = SpeedTier::kThunder;
2358
0
  }
2359
0
  if (cparams.speed_tier == SpeedTier::kTectonicPlate) {
2360
    // Test palette performance to inform later trials.
2361
0
    std::vector<CompressParams> all_params;
2362
0
    CompressParams cparams_attempt = cparams_orig;
2363
0
    cparams_attempt.speed_tier = SpeedTier::kGlacier;
2364
2365
0
    cparams_attempt.options.max_properties = 4;
2366
0
    cparams_attempt.options.nb_repeats = 1.0f;
2367
0
    cparams_attempt.modular_group_size_shift = 3;
2368
0
    cparams_attempt.palette_colors = 0;
2369
0
    cparams_attempt.options.predictor = Predictor::Variable;
2370
0
    cparams_attempt.channel_colors_percent = 80.f;
2371
0
    cparams_attempt.channel_colors_pre_transform_percent = 95.f;
2372
0
    cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kDefault;
2373
0
    cparams_attempt.patches = Override::kDefault;
2374
0
    all_params.push_back(cparams_attempt);
2375
0
    cparams_attempt.options.predictor = Predictor::Zero;
2376
0
    cparams_attempt.options.nb_repeats = 0.01f;
2377
0
    cparams_attempt.palette_colors = 70000;
2378
0
    cparams_attempt.patches = Override::kOff;
2379
0
    cparams_attempt.options.wp_tree_mode = ModularOptions::TreeMode::kNoWP;
2380
0
    all_params.push_back(cparams_attempt);
2381
2382
0
    std::vector<size_t> size;
2383
0
    size.resize(all_params.size());
2384
2385
0
    const auto process_variant = [&](size_t task, size_t) -> Status {
2386
0
      std::vector<uint8_t> output(64);
2387
0
      uint8_t* next_out = output.data();
2388
0
      size_t avail_out = output.size();
2389
0
      JxlEncoderOutputProcessorWrapper local_output(memory_manager);
2390
0
      JXL_RETURN_IF_ERROR(local_output.SetAvailOut(&next_out, &avail_out));
2391
0
      JXL_RETURN_IF_ERROR(EncodeFrame(memory_manager, all_params[task],
2392
0
                                      frame_info, metadata, frame_data, cms,
2393
0
                                      nullptr, &local_output, aux_out));
2394
0
      size[task] = local_output.CurrentPosition();
2395
0
      return true;
2396
0
    };
2397
0
    JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, all_params.size(),
2398
0
                                  ThreadPool::NoInit, process_variant,
2399
0
                                  "Compress kTectonicPlate"));
2400
2401
0
    std::vector<CompressParams> all_params_test = all_params;
2402
0
    std::vector<size_t> size_test = size;
2403
0
    size_t best_idx_test = 0;
2404
2405
0
    if (size_test[0] <= size_test[1]) {
2406
0
      all_params = TectonicPlateSettingsLessPalette(cparams_orig);
2407
0
    } else {
2408
0
      best_idx_test = 1;
2409
0
      all_params = TectonicPlateSettingsMorePalette(cparams_orig);
2410
0
    }
2411
2412
0
    size.clear();
2413
0
    size.resize(all_params.size());
2414
2415
0
    JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, all_params.size(),
2416
0
                                  ThreadPool::NoInit, process_variant,
2417
0
                                  "Compress kTectonicPlate"));
2418
2419
0
    size_t best_idx = 0;
2420
0
    for (size_t i = 1; i < all_params.size(); i++) {
2421
0
      if (size[best_idx] > size[i]) {
2422
0
        best_idx = i;
2423
0
      }
2424
0
    }
2425
0
    if (size[best_idx] < size_test[best_idx_test]) {
2426
0
      cparams = all_params[best_idx];
2427
0
    } else {
2428
0
      cparams = all_params_test[best_idx_test];
2429
0
    }
2430
0
  }
2431
2432
0
  JXL_RETURN_IF_ERROR(ParamsPostInit(&cparams));
2433
2434
0
  if (cparams.butteraugli_distance < 0) {
2435
0
    return JXL_FAILURE("Expected non-negative distance");
2436
0
  }
2437
2438
0
  if (cparams.progressive_dc < 0) {
2439
0
    if (cparams.progressive_dc != -1) {
2440
0
      return JXL_FAILURE("Invalid progressive DC setting value (%d)",
2441
0
                         cparams.progressive_dc);
2442
0
    }
2443
0
    cparams.progressive_dc = 0;
2444
0
  }
2445
0
  if (cparams.ec_resampling < cparams.resampling) {
2446
0
    cparams.ec_resampling = cparams.resampling;
2447
0
  }
2448
0
  if (cparams.resampling > 1 || frame_info.is_preview) {
2449
0
    cparams.progressive_dc = 0;
2450
0
  }
2451
2452
0
  if (frame_info.dc_level + cparams.progressive_dc > 4) {
2453
0
    return JXL_FAILURE("Too many levels of progressive DC");
2454
0
  }
2455
2456
0
  if (cparams.butteraugli_distance != 0 &&
2457
0
      cparams.butteraugli_distance < kMinButteraugliDistance) {
2458
0
    return JXL_FAILURE("Butteraugli distance is too low (%f)",
2459
0
                       cparams.butteraugli_distance);
2460
0
  }
2461
2462
0
  if (frame_data.IsJPEG()) {
2463
0
    cparams.gaborish = Override::kOff;
2464
0
    cparams.epf = 0;
2465
0
    cparams.modular_mode = false;
2466
0
  }
2467
2468
0
  if (frame_data.xsize == 0 || frame_data.ysize == 0) {
2469
0
    return JXL_FAILURE("Empty image");
2470
0
  }
2471
2472
  // Assert that this metadata is correctly set up for the compression params,
2473
  // this should have been done by enc_file.cc
2474
0
  JXL_ENSURE(metadata->m.xyb_encoded ==
2475
0
             (cparams.color_transform == ColorTransform::kXYB));
2476
2477
0
  if (frame_data.IsJPEG() && cparams.color_transform == ColorTransform::kXYB) {
2478
0
    return JXL_FAILURE("Can't add JPEG frame to XYB codestream");
2479
0
  }
2480
2481
0
  if (CanDoStreamingEncoding(cparams, frame_info, *metadata, frame_data)) {
2482
0
    return EncodeFrameStreaming(memory_manager, cparams, frame_info, metadata,
2483
0
                                frame_data, cms, pool, output_processor,
2484
0
                                aux_out);
2485
0
  } else {
2486
0
    return EncodeFrameOneShot(memory_manager, cparams, frame_info, metadata,
2487
0
                              frame_data, cms, pool, output_processor, aux_out);
2488
0
  }
2489
0
}
2490
2491
Status EncodeFrame(JxlMemoryManager* memory_manager,
2492
                   const CompressParams& cparams_orig,
2493
                   const FrameInfo& frame_info, const CodecMetadata* metadata,
2494
                   ImageBundle& ib, const JxlCmsInterface& cms,
2495
0
                   ThreadPool* pool, BitWriter* writer, AuxOut* aux_out) {
2496
0
  JxlEncoderChunkedFrameAdapter frame_data(ib.xsize(), ib.ysize(),
2497
0
                                           ib.extra_channels().size());
2498
0
  std::vector<uint8_t> color;
2499
0
  if (ib.IsJPEG()) {
2500
0
    frame_data.SetJPEGData(std::move(ib.jpeg_data));
2501
0
  } else {
2502
0
    uint32_t num_channels =
2503
0
        ib.IsGray() && frame_info.ib_needs_color_transform ? 1 : 3;
2504
0
    size_t stride = ib.xsize() * num_channels * 4;
2505
0
    color.resize(ib.ysize() * stride);
2506
0
    JXL_RETURN_IF_ERROR(ConvertToExternal(
2507
0
        ib, /*bits_per_sample=*/32, /*float_out=*/true, num_channels,
2508
0
        JXL_NATIVE_ENDIAN, stride, pool, color.data(), color.size(),
2509
0
        /*out_callback=*/{}, Orientation::kIdentity));
2510
0
    JxlPixelFormat format{num_channels, JXL_TYPE_FLOAT, JXL_NATIVE_ENDIAN, 0};
2511
0
    frame_data.SetFromBuffer(0, color.data(), color.size(), format);
2512
0
  }
2513
0
  for (size_t ec = 0; ec < ib.extra_channels().size(); ++ec) {
2514
0
    JxlPixelFormat ec_format{1, JXL_TYPE_FLOAT, JXL_NATIVE_ENDIAN, 0};
2515
0
    size_t ec_stride = ib.xsize() * 4;
2516
0
    std::vector<uint8_t> ec_data(ib.ysize() * ec_stride);
2517
0
    const ImageF* channel = &ib.extra_channels()[ec];
2518
0
    JXL_RETURN_IF_ERROR(ConvertChannelsToExternal(
2519
0
        &channel, 1,
2520
0
        /*bits_per_sample=*/32,
2521
0
        /*float_out=*/true, JXL_NATIVE_ENDIAN, ec_stride, pool, ec_data.data(),
2522
0
        ec_data.size(), /*out_callback=*/{}, Orientation::kIdentity));
2523
0
    frame_data.SetFromBuffer(1 + ec, ec_data.data(), ec_data.size(), ec_format);
2524
0
  }
2525
0
  FrameInfo fi = frame_info;
2526
0
  fi.origin = ib.origin;
2527
0
  fi.blend = ib.blend;
2528
0
  fi.blendmode = ib.blendmode;
2529
0
  fi.duration = ib.duration;
2530
0
  fi.timecode = ib.timecode;
2531
0
  fi.name = ib.name;
2532
0
  std::vector<uint8_t> output(64);
2533
0
  uint8_t* next_out = output.data();
2534
0
  size_t avail_out = output.size();
2535
0
  JxlEncoderOutputProcessorWrapper output_processor(memory_manager);
2536
0
  JXL_RETURN_IF_ERROR(output_processor.SetAvailOut(&next_out, &avail_out));
2537
0
  JXL_RETURN_IF_ERROR(EncodeFrame(memory_manager, cparams_orig, fi, metadata,
2538
0
                                  frame_data, cms, pool, &output_processor,
2539
0
                                  aux_out));
2540
0
  JXL_RETURN_IF_ERROR(output_processor.SetFinalizedPosition());
2541
0
  JXL_RETURN_IF_ERROR(output_processor.CopyOutput(output, next_out, avail_out));
2542
0
  JXL_RETURN_IF_ERROR(writer->AppendByteAligned(Bytes(output)));
2543
0
  return true;
2544
0
}
2545
2546
}  // namespace jxl