Coverage Report

Created: 2026-09-14 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/dec_cache.cc
Line
Count
Source
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/dec_cache.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <cstddef>
12
#include <cstdint>
13
#include <utility>
14
15
#include "lib/jxl/ac_strategy.h"
16
#include "lib/jxl/base/bits.h"
17
#include "lib/jxl/base/data_parallel.h"
18
#include "lib/jxl/base/status.h"
19
#include "lib/jxl/blending.h"
20
#include "lib/jxl/cms/color_encoding_cms.h"
21
#include "lib/jxl/coeff_order.h"
22
#include "lib/jxl/color_encoding_internal.h"
23
#include "lib/jxl/common.h"  // JXL_HIGH_PRECISION
24
#include "lib/jxl/frame_dimensions.h"
25
#include "lib/jxl/frame_header.h"
26
#include "lib/jxl/image.h"
27
#include "lib/jxl/image_bundle.h"
28
#include "lib/jxl/image_metadata.h"
29
#include "lib/jxl/loop_filter.h"
30
#include "lib/jxl/memory_manager_internal.h"
31
#include "lib/jxl/render_pipeline/render_pipeline.h"
32
#include "lib/jxl/render_pipeline/stage_blending.h"
33
#include "lib/jxl/render_pipeline/stage_chroma_upsampling.h"
34
#include "lib/jxl/render_pipeline/stage_cms.h"
35
#include "lib/jxl/render_pipeline/stage_epf.h"
36
#include "lib/jxl/render_pipeline/stage_from_linear.h"
37
#include "lib/jxl/render_pipeline/stage_gaborish.h"
38
#include "lib/jxl/render_pipeline/stage_noise.h"
39
#include "lib/jxl/render_pipeline/stage_patches.h"
40
#include "lib/jxl/render_pipeline/stage_splines.h"
41
#include "lib/jxl/render_pipeline/stage_spot.h"
42
#include "lib/jxl/render_pipeline/stage_to_linear.h"
43
#include "lib/jxl/render_pipeline/stage_tone_mapping.h"
44
#include "lib/jxl/render_pipeline/stage_upsampling.h"
45
#include "lib/jxl/render_pipeline/stage_write.h"
46
#include "lib/jxl/render_pipeline/stage_xyb.h"
47
#include "lib/jxl/render_pipeline/stage_ycbcr.h"
48
49
namespace jxl {
50
51
Status GroupDecCache::InitOnce(JxlMemoryManager* memory_manager,
52
11.6k
                               size_t num_passes, size_t used_acs) {
53
23.2k
  for (size_t i = 0; i < num_passes; i++) {
54
11.6k
    if (num_nzeroes[i].xsize() == 0) {
55
      // Allocate enough for a whole group - partial groups on the
56
      // right/bottom border just use a subset. The valid size is passed via
57
      // Rect.
58
59
10.6k
      JXL_ASSIGN_OR_RETURN(num_nzeroes[i],
60
10.6k
                           Image3I::Create(memory_manager, kGroupDimInBlocks,
61
10.6k
                                           kGroupDimInBlocks));
62
10.6k
    }
63
11.6k
  }
64
11.6k
  size_t max_block_area = 0;
65
66
318k
  for (uint8_t o = 0; o < AcStrategy::kNumValidStrategies; ++o) {
67
307k
    AcStrategy acs = AcStrategy::FromRawStrategy(o);
68
307k
    if ((used_acs & (1 << o)) == 0) continue;
69
24.4k
    size_t area =
70
24.4k
        acs.covered_blocks_x() * acs.covered_blocks_y() * kDCTBlockSize;
71
24.4k
    max_block_area = std::max(area, max_block_area);
72
24.4k
  }
73
74
11.6k
  if (max_block_area > max_block_area_) {
75
10.6k
    max_block_area_ = max_block_area;
76
    // We need 3x float blocks for dequantized coefficients and 1x for scratch
77
    // space for transforms.
78
10.6k
    JXL_ASSIGN_OR_RETURN(
79
10.6k
        float_memory_,
80
10.6k
        AlignedMemory::Create(memory_manager,
81
10.6k
                              max_block_area_ * 7 * sizeof(float)));
82
    // We need 3x int32 or int16 blocks for quantized coefficients.
83
10.6k
    JXL_ASSIGN_OR_RETURN(
84
10.6k
        int32_memory_,
85
10.6k
        AlignedMemory::Create(memory_manager,
86
10.6k
                              max_block_area_ * 3 * sizeof(int32_t)));
87
10.6k
    JXL_ASSIGN_OR_RETURN(
88
10.6k
        int16_memory_,
89
10.6k
        AlignedMemory::Create(memory_manager,
90
10.6k
                              max_block_area_ * 3 * sizeof(int16_t)));
91
10.6k
  }
92
93
11.6k
  dec_group_block = float_memory_.address<float>();
94
11.6k
  scratch_space = dec_group_block + max_block_area_ * 3;
95
11.6k
  dec_group_qblock = int32_memory_.address<int32_t>();
96
11.6k
  dec_group_qblock16 = int16_memory_.address<int16_t>();
97
11.6k
  return true;
98
11.6k
}
99
100
// Initialize the decoder state after all of DC is decoded.
101
24.4k
Status PassesDecoderState::InitForAC(size_t num_passes, ThreadPool* pool) {
102
24.4k
  shared_storage.coeff_order_size = 0;
103
684k
  for (uint8_t o = 0; o < AcStrategy::kNumValidStrategies; ++o) {
104
660k
    if (((1 << o) & used_acs) == 0) continue;
105
15.2k
    uint8_t ord = kStrategyOrder[o];
106
15.2k
    shared_storage.coeff_order_size =
107
15.2k
        std::max(kCoeffOrderOffset[3 * (ord + 1)] * kDCTBlockSize,
108
15.2k
                 shared_storage.coeff_order_size);
109
15.2k
  }
110
24.4k
  size_t sz = num_passes * shared_storage.coeff_order_size;
111
24.4k
  if (sz > shared_storage.coeff_orders.size()) {
112
210
    shared_storage.coeff_orders.resize(sz);
113
210
  }
114
24.4k
  return true;
115
24.4k
}
116
117
Status PassesDecoderState::PreparePipeline(const FrameHeader& frame_header,
118
                                           const ImageMetadata* metadata,
119
                                           ImageBundle* decoded,
120
24.4k
                                           PipelineOptions options) {
121
24.4k
  JxlMemoryManager* memory_manager = this->memory_manager();
122
24.4k
  size_t num_c = 3 + frame_header.nonserialized_metadata->m.num_extra_channels;
123
24.4k
  bool render_noise =
124
24.4k
      (options.render_noise && (frame_header.flags & FrameHeader::kNoise) != 0);
125
24.4k
  size_t num_tmp_c = render_noise ? 3 : 0;
126
127
24.4k
  if (frame_header.CanBeReferenced()) {
128
    // Necessary so that SetInputSizes() can allocate output buffers as needed.
129
16.3k
    frame_storage_for_referencing = ImageBundle(memory_manager, metadata);
130
16.3k
  }
131
132
24.4k
  RenderPipeline::Builder builder(memory_manager, num_c + num_tmp_c);
133
134
24.4k
  if (options.use_slow_render_pipeline) {
135
0
    builder.UseSimpleImplementation();
136
0
  }
137
138
24.4k
  if (!frame_header.chroma_subsampling.Is444()) {
139
21.3k
    for (size_t c = 0; c < 3; c++) {
140
15.9k
      if (frame_header.chroma_subsampling.HShift(c) != 0) {
141
8.54k
        JXL_RETURN_IF_ERROR(
142
8.54k
            builder.AddStage(GetChromaUpsamplingStage(c, /*horizontal=*/true)));
143
8.54k
      }
144
15.9k
      if (frame_header.chroma_subsampling.VShift(c) != 0) {
145
4.07k
        JXL_RETURN_IF_ERROR(builder.AddStage(
146
4.07k
            GetChromaUpsamplingStage(c, /*horizontal=*/false)));
147
4.07k
      }
148
15.9k
    }
149
5.32k
  }
150
151
24.4k
  if (frame_header.loop_filter.gab) {
152
5.71k
    JXL_RETURN_IF_ERROR(
153
5.71k
        builder.AddStage(GetGaborishStage(frame_header.loop_filter)));
154
5.71k
  }
155
156
24.4k
  {
157
24.4k
    const LoopFilter& lf = frame_header.loop_filter;
158
24.4k
    if (lf.epf_iters >= 3) {
159
1.44k
      JXL_RETURN_IF_ERROR(
160
1.44k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::Zero)));
161
1.44k
    }
162
24.4k
    if (lf.epf_iters >= 1) {
163
11.3k
      JXL_RETURN_IF_ERROR(
164
11.3k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::One)));
165
11.3k
    }
166
24.4k
    if (lf.epf_iters >= 2) {
167
11.3k
      JXL_RETURN_IF_ERROR(
168
11.3k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::Two)));
169
11.3k
    }
170
24.4k
  }
171
172
24.4k
  bool late_ec_upsample = frame_header.upsampling != 1;
173
24.4k
  for (auto ecups : frame_header.extra_channel_upsampling) {
174
9.19k
    if (ecups != frame_header.upsampling) {
175
      // If patches are applied, either frame_header.upsampling == 1 or
176
      // late_ec_upsample is true.
177
3.11k
      late_ec_upsample = false;
178
3.11k
    }
179
9.19k
  }
180
181
24.4k
  if (!late_ec_upsample) {
182
17.6k
    for (size_t ec = 0; ec < frame_header.extra_channel_upsampling.size();
183
9.19k
         ec++) {
184
8.45k
      if (frame_header.extra_channel_upsampling[ec] != 1) {
185
3.11k
        JXL_RETURN_IF_ERROR(builder.AddStage(GetUpsamplingStage(
186
3.11k
            memory_manager, frame_header.nonserialized_metadata->transform_data,
187
3.11k
            3 + ec,
188
3.11k
            CeilLog2Nonzero(frame_header.extra_channel_upsampling[ec]))));
189
3.11k
      }
190
8.45k
    }
191
9.19k
  }
192
193
24.4k
  if ((frame_header.flags & FrameHeader::kPatches) != 0) {
194
1.39k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetPatchesStage(
195
1.39k
        &shared->image_features.patches,
196
1.39k
        &frame_header.nonserialized_metadata->m.extra_channel_info)));
197
1.39k
  }
198
24.4k
  if ((frame_header.flags & FrameHeader::kSplines) != 0) {
199
1.03k
    JXL_RETURN_IF_ERROR(
200
1.03k
        builder.AddStage(GetSplineStage(&shared->image_features.splines)));
201
1.03k
  }
202
203
24.4k
  if (frame_header.upsampling != 1) {
204
15.4k
    size_t nb_channels =
205
15.4k
        3 +
206
15.4k
        (late_ec_upsample ? frame_header.extra_channel_upsampling.size() : 0);
207
62.3k
    for (size_t c = 0; c < nb_channels; c++) {
208
46.9k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetUpsamplingStage(
209
46.9k
          memory_manager, frame_header.nonserialized_metadata->transform_data,
210
46.9k
          c, CeilLog2Nonzero(frame_header.upsampling))));
211
46.9k
    }
212
15.4k
  }
213
  // Starting from this line all the stages considered to have zero xextra.
214
  // Upsampling does not have xextra as well (even if it happens before
215
  // splines/patches for EC).
216
24.4k
  if (render_noise) {
217
5.67k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetConvolveNoiseStage(num_c)));
218
5.67k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetAddNoiseStage(
219
5.67k
        shared->image_features.noise_params, shared->cmap.base(), num_c)));
220
5.67k
  }
221
24.4k
  if (frame_header.dc_level != 0) {
222
6.80k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImage3FStage(
223
6.80k
        memory_manager, &shared_storage.dc_frames[frame_header.dc_level - 1])));
224
6.80k
  }
225
226
24.4k
  if (frame_header.CanBeReferenced() &&
227
16.3k
      frame_header.save_before_color_transform) {
228
2.10k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImageBundleStage(
229
2.10k
        &frame_storage_for_referencing, &metadata->color_encoding)));
230
2.10k
  }
231
232
24.4k
  bool has_alpha = false;
233
24.4k
  size_t alpha_c = 0;
234
26.4k
  for (size_t i = 0; i < metadata->extra_channel_info.size(); i++) {
235
6.17k
    if (metadata->extra_channel_info[i].type == ExtraChannel::kAlpha) {
236
4.22k
      has_alpha = true;
237
4.22k
      alpha_c = 3 + i;
238
4.22k
      break;
239
4.22k
    }
240
6.17k
  }
241
242
24.4k
  if (fast_xyb_srgb8_conversion) {
243
#if !JXL_HIGH_PRECISION
244
    JXL_ENSURE(!NeedsBlending(frame_header));
245
    JXL_ENSURE(!frame_header.CanBeReferenced() ||
246
               frame_header.save_before_color_transform);
247
    JXL_ENSURE(!options.render_spotcolors ||
248
               !metadata->Find(ExtraChannel::kSpotColor));
249
    bool is_rgba = (main_output.format.num_channels == 4);
250
    uint8_t* rgb_output = reinterpret_cast<uint8_t*>(main_output.buffer);
251
    JXL_RETURN_IF_ERROR(builder.AddStage(
252
        GetFastXYBTosRGB8Stage(rgb_output, main_output.stride, width, height,
253
                               is_rgba, has_alpha, alpha_c)));
254
#endif
255
24.4k
  } else {
256
24.4k
    bool linear = false;
257
24.4k
    if (frame_header.color_transform == ColorTransform::kYCbCr) {
258
5.35k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetYCbCrStage()));
259
19.1k
    } else if (frame_header.color_transform == ColorTransform::kXYB) {
260
9.87k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetXYBStage(output_encoding_info)));
261
9.87k
      if (output_encoding_info.color_encoding.GetColorSpace() !=
262
9.87k
          ColorSpace::kXYB) {
263
9.87k
        linear = true;
264
9.87k
      }
265
9.87k
    }  // Nothing to do for kNone.
266
267
24.4k
    if (options.coalescing && NeedsBlending(frame_header)) {
268
4.45k
      if (linear) {
269
2.34k
        JXL_RETURN_IF_ERROR(
270
2.34k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
271
2.34k
        linear = false;
272
2.34k
      }
273
4.45k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetBlendingStage(
274
4.45k
          frame_header, this, output_encoding_info.color_encoding)));
275
4.45k
    }
276
277
24.4k
    if (options.coalescing && frame_header.CanBeReferenced() &&
278
16.3k
        !frame_header.save_before_color_transform) {
279
14.2k
      if (linear) {
280
2.49k
        JXL_RETURN_IF_ERROR(
281
2.49k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
282
2.49k
        linear = false;
283
2.49k
      }
284
14.2k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImageBundleStage(
285
14.2k
          &frame_storage_for_referencing, &metadata->color_encoding)));
286
14.2k
    }
287
288
24.4k
    if (options.render_spotcolors &&
289
24.4k
        frame_header.nonserialized_metadata->m.Find(ExtraChannel::kSpotColor)) {
290
2.13k
      for (size_t i = 0; i < metadata->extra_channel_info.size(); i++) {
291
        // Don't use Find() because there may be multiple spot color channels.
292
1.57k
        const ExtraChannelInfo& eci = metadata->extra_channel_info[i];
293
1.57k
        if (eci.type == ExtraChannel::kSpotColor) {
294
559
          JXL_RETURN_IF_ERROR(
295
559
              builder.AddStage(GetSpotColorStage(i, eci.spot_color)));
296
559
        }
297
1.57k
      }
298
559
    }
299
300
24.4k
    auto tone_mapping_stage = GetToneMappingStage(output_encoding_info);
301
24.4k
    if (tone_mapping_stage) {
302
0
      if (!linear) {
303
0
        auto to_linear_stage = GetToLinearStage(output_encoding_info);
304
0
        if (!to_linear_stage) {
305
0
          if (!output_encoding_info.cms_set) {
306
0
            return JXL_FAILURE("Cannot tonemap this colorspace without a CMS");
307
0
          }
308
0
          auto cms_stage = GetCmsStage(output_encoding_info);
309
0
          if (cms_stage) {
310
0
            JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
311
0
          }
312
0
        } else {
313
0
          JXL_RETURN_IF_ERROR(builder.AddStage(std::move(to_linear_stage)));
314
0
        }
315
0
        linear = true;
316
0
      }
317
0
      JXL_RETURN_IF_ERROR(builder.AddStage(std::move(tone_mapping_stage)));
318
0
    }
319
320
24.4k
    if (linear) {
321
5.03k
      const size_t channels_src =
322
5.03k
          (output_encoding_info.orig_color_encoding.IsCMYK()
323
5.03k
               ? 4
324
5.03k
               : output_encoding_info.orig_color_encoding.Channels());
325
5.03k
      const size_t channels_dst =
326
5.03k
          output_encoding_info.color_encoding.Channels();
327
5.03k
      bool mixing_color_and_grey = (channels_dst != channels_src);
328
5.03k
      if ((output_encoding_info.color_encoding_is_original) ||
329
4.09k
          (!output_encoding_info.cms_set) || mixing_color_and_grey) {
330
        // in those cases we only need a linear stage in other cases we attempt
331
        // to obtain a cms stage: the cases are
332
        // - output_encoding_info.color_encoding_is_original: no cms stage
333
        // needed because it would be a no-op
334
        // - !output_encoding_info.cms_set: can't use the cms, so no point in
335
        // trying to add a cms stage
336
        // - mixing_color_and_grey: cms stage can't handle that
337
        // TODO(firsching): remove "mixing_color_and_grey" condition after
338
        // adding support for greyscale to cms stage.
339
4.09k
        JXL_RETURN_IF_ERROR(
340
4.09k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
341
4.09k
      } else {
342
935
        if (!output_encoding_info.linear_color_encoding.CreateICC()) {
343
0
          return JXL_FAILURE("Failed to create ICC");
344
0
        }
345
935
        auto cms_stage = GetCmsStage(output_encoding_info);
346
935
        if (cms_stage) {
347
935
          JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
348
935
        }
349
935
      }
350
5.03k
      linear = false;
351
19.4k
    } else {
352
19.4k
      auto cms_stage = GetCmsStage(output_encoding_info, false);
353
19.4k
      if (cms_stage) {
354
1.54k
        JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
355
1.54k
      }
356
19.4k
    }
357
24.4k
    (void)linear;
358
359
24.4k
    if (main_output.callback.IsPresent() || main_output.buffer) {
360
1.33k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToOutputStage(
361
1.33k
          main_output, width, height, has_alpha, unpremul_alpha, alpha_c,
362
1.33k
          undo_orientation, extra_output, memory_manager)));
363
23.1k
    } else {
364
23.1k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImageBundleStage(
365
23.1k
          decoded, &output_encoding_info.color_encoding)));
366
23.1k
    }
367
24.4k
  }
368
24.4k
  JXL_ASSIGN_OR_RETURN(render_pipeline,
369
24.4k
                       std::move(builder).Finalize(shared->frame_dim));
370
24.4k
  return render_pipeline->IsInitialized();
371
24.4k
}
372
373
}  // namespace jxl