Coverage Report

Created: 2026-06-14 06:57

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
6.38k
                               size_t num_passes, size_t used_acs) {
53
12.7k
  for (size_t i = 0; i < num_passes; i++) {
54
6.39k
    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
6.04k
      JXL_ASSIGN_OR_RETURN(num_nzeroes[i],
60
6.04k
                           Image3I::Create(memory_manager, kGroupDimInBlocks,
61
6.04k
                                           kGroupDimInBlocks));
62
6.04k
    }
63
6.39k
  }
64
6.38k
  size_t max_block_area = 0;
65
66
178k
  for (uint8_t o = 0; o < AcStrategy::kNumValidStrategies; ++o) {
67
171k
    AcStrategy acs = AcStrategy::FromRawStrategy(o);
68
171k
    if ((used_acs & (1 << o)) == 0) continue;
69
15.2k
    size_t area =
70
15.2k
        acs.covered_blocks_x() * acs.covered_blocks_y() * kDCTBlockSize;
71
15.2k
    max_block_area = std::max(area, max_block_area);
72
15.2k
  }
73
74
6.38k
  if (max_block_area > max_block_area_) {
75
6.03k
    max_block_area_ = max_block_area;
76
    // We need 3x float blocks for dequantized coefficients and 1x for scratch
77
    // space for transforms.
78
6.03k
    JXL_ASSIGN_OR_RETURN(
79
6.03k
        float_memory_,
80
6.03k
        AlignedMemory::Create(memory_manager,
81
6.03k
                              max_block_area_ * 7 * sizeof(float)));
82
    // We need 3x int32 or int16 blocks for quantized coefficients.
83
6.03k
    JXL_ASSIGN_OR_RETURN(
84
6.03k
        int32_memory_,
85
6.03k
        AlignedMemory::Create(memory_manager,
86
6.03k
                              max_block_area_ * 3 * sizeof(int32_t)));
87
6.03k
    JXL_ASSIGN_OR_RETURN(
88
6.03k
        int16_memory_,
89
6.03k
        AlignedMemory::Create(memory_manager,
90
6.03k
                              max_block_area_ * 3 * sizeof(int16_t)));
91
6.03k
  }
92
93
6.38k
  dec_group_block = float_memory_.address<float>();
94
6.38k
  scratch_space = dec_group_block + max_block_area_ * 3;
95
6.38k
  dec_group_qblock = int32_memory_.address<int32_t>();
96
6.38k
  dec_group_qblock16 = int16_memory_.address<int16_t>();
97
6.38k
  return true;
98
6.38k
}
99
100
// Initialize the decoder state after all of DC is decoded.
101
22.8k
Status PassesDecoderState::InitForAC(size_t num_passes, ThreadPool* pool) {
102
22.8k
  shared_storage.coeff_order_size = 0;
103
640k
  for (uint8_t o = 0; o < AcStrategy::kNumValidStrategies; ++o) {
104
617k
    if (((1 << o) & used_acs) == 0) continue;
105
13.5k
    uint8_t ord = kStrategyOrder[o];
106
13.5k
    shared_storage.coeff_order_size =
107
13.5k
        std::max(kCoeffOrderOffset[3 * (ord + 1)] * kDCTBlockSize,
108
13.5k
                 shared_storage.coeff_order_size);
109
13.5k
  }
110
22.8k
  size_t sz = num_passes * shared_storage.coeff_order_size;
111
22.8k
  if (sz > shared_storage.coeff_orders.size()) {
112
151
    shared_storage.coeff_orders.resize(sz);
113
151
  }
114
22.8k
  return true;
115
22.8k
}
116
117
Status PassesDecoderState::PreparePipeline(const FrameHeader& frame_header,
118
                                           const ImageMetadata* metadata,
119
                                           ImageBundle* decoded,
120
22.8k
                                           PipelineOptions options) {
121
22.8k
  JxlMemoryManager* memory_manager = this->memory_manager();
122
22.8k
  size_t num_c = 3 + frame_header.nonserialized_metadata->m.num_extra_channels;
123
22.8k
  size_t num_tmp_c = options.render_noise ? 3 : 0;
124
125
22.8k
  if (frame_header.CanBeReferenced()) {
126
    // Necessary so that SetInputSizes() can allocate output buffers as needed.
127
15.0k
    frame_storage_for_referencing = ImageBundle(memory_manager, metadata);
128
15.0k
  }
129
130
22.8k
  RenderPipeline::Builder builder(memory_manager, num_c + num_tmp_c);
131
132
22.8k
  if (options.use_slow_render_pipeline) {
133
0
    builder.UseSimpleImplementation();
134
0
  }
135
136
22.8k
  if (!frame_header.chroma_subsampling.Is444()) {
137
16.4k
    for (size_t c = 0; c < 3; c++) {
138
12.3k
      if (frame_header.chroma_subsampling.HShift(c) != 0) {
139
6.36k
        JXL_RETURN_IF_ERROR(
140
6.36k
            builder.AddStage(GetChromaUpsamplingStage(c, /*horizontal=*/true)));
141
6.36k
      }
142
12.3k
      if (frame_header.chroma_subsampling.VShift(c) != 0) {
143
2.46k
        JXL_RETURN_IF_ERROR(builder.AddStage(
144
2.46k
            GetChromaUpsamplingStage(c, /*horizontal=*/false)));
145
2.46k
      }
146
12.3k
    }
147
4.11k
  }
148
149
22.8k
  if (frame_header.loop_filter.gab) {
150
5.26k
    JXL_RETURN_IF_ERROR(
151
5.26k
        builder.AddStage(GetGaborishStage(frame_header.loop_filter)));
152
5.26k
  }
153
154
22.8k
  {
155
22.8k
    const LoopFilter& lf = frame_header.loop_filter;
156
22.8k
    if (lf.epf_iters >= 3) {
157
1.52k
      JXL_RETURN_IF_ERROR(
158
1.52k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::Zero)));
159
1.52k
    }
160
22.8k
    if (lf.epf_iters >= 1) {
161
11.1k
      JXL_RETURN_IF_ERROR(
162
11.1k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::One)));
163
11.1k
    }
164
22.8k
    if (lf.epf_iters >= 2) {
165
11.1k
      JXL_RETURN_IF_ERROR(
166
11.1k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::Two)));
167
11.1k
    }
168
22.8k
  }
169
170
22.8k
  bool late_ec_upsample = frame_header.upsampling != 1;
171
22.8k
  for (auto ecups : frame_header.extra_channel_upsampling) {
172
8.67k
    if (ecups != frame_header.upsampling) {
173
      // If patches are applied, either frame_header.upsampling == 1 or
174
      // late_ec_upsample is true.
175
2.30k
      late_ec_upsample = false;
176
2.30k
    }
177
8.67k
  }
178
179
22.8k
  if (!late_ec_upsample) {
180
15.1k
    for (size_t ec = 0; ec < frame_header.extra_channel_upsampling.size();
181
7.63k
         ec++) {
182
7.63k
      if (frame_header.extra_channel_upsampling[ec] != 1) {
183
2.30k
        JXL_RETURN_IF_ERROR(builder.AddStage(GetUpsamplingStage(
184
2.30k
            memory_manager, frame_header.nonserialized_metadata->transform_data,
185
2.30k
            3 + ec,
186
2.30k
            CeilLog2Nonzero(frame_header.extra_channel_upsampling[ec]))));
187
2.30k
      }
188
7.63k
    }
189
7.48k
  }
190
191
22.8k
  if ((frame_header.flags & FrameHeader::kPatches) != 0) {
192
1.22k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetPatchesStage(
193
1.22k
        &shared->image_features.patches,
194
1.22k
        &frame_header.nonserialized_metadata->m.extra_channel_info)));
195
1.22k
  }
196
22.8k
  if ((frame_header.flags & FrameHeader::kSplines) != 0) {
197
1.12k
    JXL_RETURN_IF_ERROR(
198
1.12k
        builder.AddStage(GetSplineStage(&shared->image_features.splines)));
199
1.12k
  }
200
201
22.8k
  if (frame_header.upsampling != 1) {
202
15.4k
    size_t nb_channels =
203
15.4k
        3 +
204
15.4k
        (late_ec_upsample ? frame_header.extra_channel_upsampling.size() : 0);
205
62.6k
    for (size_t c = 0; c < nb_channels; c++) {
206
47.2k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetUpsamplingStage(
207
47.2k
          memory_manager, frame_header.nonserialized_metadata->transform_data,
208
47.2k
          c, CeilLog2Nonzero(frame_header.upsampling))));
209
47.2k
    }
210
15.4k
  }
211
  // Starting from this line all the stages considered to have zero xextra.
212
  // Upsampling does not have xextra as well (even if it happens before
213
  // splines/patches for EC).
214
22.8k
  if (options.render_noise) {
215
2.81k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetConvolveNoiseStage(num_c)));
216
2.81k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetAddNoiseStage(
217
2.81k
        shared->image_features.noise_params, shared->cmap.base(), num_c)));
218
2.81k
  }
219
22.8k
  if (frame_header.dc_level != 0) {
220
6.62k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImage3FStage(
221
6.62k
        memory_manager, &shared_storage.dc_frames[frame_header.dc_level - 1])));
222
6.62k
  }
223
224
22.8k
  if (frame_header.CanBeReferenced() &&
225
15.0k
      frame_header.save_before_color_transform) {
226
1.73k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImageBundleStage(
227
1.73k
        &frame_storage_for_referencing, output_encoding_info)));
228
1.73k
  }
229
230
22.8k
  bool has_alpha = false;
231
22.8k
  size_t alpha_c = 0;
232
23.9k
  for (size_t i = 0; i < metadata->extra_channel_info.size(); i++) {
233
5.02k
    if (metadata->extra_channel_info[i].type == ExtraChannel::kAlpha) {
234
3.91k
      has_alpha = true;
235
3.91k
      alpha_c = 3 + i;
236
3.91k
      break;
237
3.91k
    }
238
5.02k
  }
239
240
22.8k
  if (fast_xyb_srgb8_conversion) {
241
#if !JXL_HIGH_PRECISION
242
    JXL_ENSURE(!NeedsBlending(frame_header));
243
    JXL_ENSURE(!frame_header.CanBeReferenced() ||
244
               frame_header.save_before_color_transform);
245
    JXL_ENSURE(!options.render_spotcolors ||
246
               !metadata->Find(ExtraChannel::kSpotColor));
247
    bool is_rgba = (main_output.format.num_channels == 4);
248
    uint8_t* rgb_output = reinterpret_cast<uint8_t*>(main_output.buffer);
249
    JXL_RETURN_IF_ERROR(builder.AddStage(
250
        GetFastXYBTosRGB8Stage(rgb_output, main_output.stride, width, height,
251
                               is_rgba, has_alpha, alpha_c)));
252
#endif
253
22.8k
  } else {
254
22.8k
    bool linear = false;
255
22.8k
    if (frame_header.color_transform == ColorTransform::kYCbCr) {
256
4.16k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetYCbCrStage()));
257
18.7k
    } else if (frame_header.color_transform == ColorTransform::kXYB) {
258
9.91k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetXYBStage(output_encoding_info)));
259
9.91k
      if (output_encoding_info.color_encoding.GetColorSpace() !=
260
9.91k
          ColorSpace::kXYB) {
261
9.91k
        linear = true;
262
9.91k
      }
263
9.91k
    }  // Nothing to do for kNone.
264
265
22.8k
    if (options.coalescing && NeedsBlending(frame_header)) {
266
3.91k
      if (linear) {
267
1.86k
        JXL_RETURN_IF_ERROR(
268
1.86k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
269
1.86k
        linear = false;
270
1.86k
      }
271
3.91k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetBlendingStage(
272
3.91k
          frame_header, this, output_encoding_info.color_encoding)));
273
3.91k
    }
274
275
22.8k
    if (options.coalescing && frame_header.CanBeReferenced() &&
276
15.0k
        !frame_header.save_before_color_transform) {
277
13.3k
      if (linear) {
278
2.45k
        JXL_RETURN_IF_ERROR(
279
2.45k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
280
2.45k
        linear = false;
281
2.45k
      }
282
13.3k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImageBundleStage(
283
13.3k
          &frame_storage_for_referencing, output_encoding_info)));
284
13.3k
    }
285
286
22.8k
    if (options.render_spotcolors &&
287
22.8k
        frame_header.nonserialized_metadata->m.Find(ExtraChannel::kSpotColor)) {
288
580
      for (size_t i = 0; i < metadata->extra_channel_info.size(); i++) {
289
        // Don't use Find() because there may be multiple spot color channels.
290
291
        const ExtraChannelInfo& eci = metadata->extra_channel_info[i];
291
291
        if (eci.type == ExtraChannel::kSpotColor) {
292
289
          JXL_RETURN_IF_ERROR(
293
289
              builder.AddStage(GetSpotColorStage(i, eci.spot_color)));
294
289
        }
295
291
      }
296
289
    }
297
298
22.8k
    auto tone_mapping_stage = GetToneMappingStage(output_encoding_info);
299
22.8k
    if (tone_mapping_stage) {
300
0
      if (!linear) {
301
0
        auto to_linear_stage = GetToLinearStage(output_encoding_info);
302
0
        if (!to_linear_stage) {
303
0
          if (!output_encoding_info.cms_set) {
304
0
            return JXL_FAILURE("Cannot tonemap this colorspace without a CMS");
305
0
          }
306
0
          auto cms_stage = GetCmsStage(output_encoding_info);
307
0
          if (cms_stage) {
308
0
            JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
309
0
          }
310
0
        } else {
311
0
          JXL_RETURN_IF_ERROR(builder.AddStage(std::move(to_linear_stage)));
312
0
        }
313
0
        linear = true;
314
0
      }
315
0
      JXL_RETURN_IF_ERROR(builder.AddStage(std::move(tone_mapping_stage)));
316
0
    }
317
318
22.8k
    if (linear) {
319
5.59k
      const size_t channels_src =
320
5.59k
          (output_encoding_info.orig_color_encoding.IsCMYK()
321
5.59k
               ? 4
322
5.59k
               : output_encoding_info.orig_color_encoding.Channels());
323
5.59k
      const size_t channels_dst =
324
5.59k
          output_encoding_info.color_encoding.Channels();
325
5.59k
      bool mixing_color_and_grey = (channels_dst != channels_src);
326
5.59k
      if ((output_encoding_info.color_encoding_is_original) ||
327
4.40k
          (!output_encoding_info.cms_set) || mixing_color_and_grey) {
328
        // in those cases we only need a linear stage in other cases we attempt
329
        // to obtain a cms stage: the cases are
330
        // - output_encoding_info.color_encoding_is_original: no cms stage
331
        // needed because it would be a no-op
332
        // - !output_encoding_info.cms_set: can't use the cms, so no point in
333
        // trying to add a cms stage
334
        // - mixing_color_and_grey: cms stage can't handle that
335
        // TODO(firsching): remove "mixing_color_and_grey" condition after
336
        // adding support for greyscale to cms stage.
337
4.40k
        JXL_RETURN_IF_ERROR(
338
4.40k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
339
4.40k
      } else {
340
1.19k
        if (!output_encoding_info.linear_color_encoding.CreateICC()) {
341
0
          return JXL_FAILURE("Failed to create ICC");
342
0
        }
343
1.19k
        auto cms_stage = GetCmsStage(output_encoding_info);
344
1.19k
        if (cms_stage) {
345
1.19k
          JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
346
1.19k
        }
347
1.19k
      }
348
5.59k
      linear = false;
349
17.2k
    } else {
350
17.2k
      auto cms_stage = GetCmsStage(output_encoding_info, false);
351
17.2k
      if (cms_stage) {
352
1.65k
        JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
353
1.65k
      }
354
17.2k
    }
355
22.8k
    (void)linear;
356
357
22.8k
    if (main_output.callback.IsPresent() || main_output.buffer) {
358
1.18k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToOutputStage(
359
1.18k
          main_output, width, height, has_alpha, unpremul_alpha, alpha_c,
360
1.18k
          undo_orientation, extra_output, memory_manager)));
361
21.6k
    } else {
362
21.6k
      JXL_RETURN_IF_ERROR(builder.AddStage(
363
21.6k
          GetWriteToImageBundleStage(decoded, output_encoding_info)));
364
21.6k
    }
365
22.8k
  }
366
22.8k
  JXL_ASSIGN_OR_RETURN(render_pipeline,
367
22.8k
                       std::move(builder).Finalize(shared->frame_dim));
368
22.8k
  return render_pipeline->IsInitialized();
369
22.8k
}
370
371
}  // namespace jxl