Coverage Report

Created: 2026-06-16 07:20

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
50.3k
                               size_t num_passes, size_t used_acs) {
53
68.2k
  for (size_t i = 0; i < num_passes; i++) {
54
17.9k
    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.0k
      JXL_ASSIGN_OR_RETURN(num_nzeroes[i],
60
10.0k
                           Image3I::Create(memory_manager, kGroupDimInBlocks,
61
10.0k
                                           kGroupDimInBlocks));
62
10.0k
    }
63
17.9k
  }
64
50.3k
  size_t max_block_area = 0;
65
66
1.40M
  for (uint8_t o = 0; o < AcStrategy::kNumValidStrategies; ++o) {
67
1.35M
    AcStrategy acs = AcStrategy::FromRawStrategy(o);
68
1.35M
    if ((used_acs & (1 << o)) == 0) continue;
69
959k
    size_t area =
70
959k
        acs.covered_blocks_x() * acs.covered_blocks_y() * kDCTBlockSize;
71
959k
    max_block_area = std::max(area, max_block_area);
72
959k
  }
73
74
50.3k
  if (max_block_area > max_block_area_) {
75
23.2k
    max_block_area_ = max_block_area;
76
    // We need 3x float blocks for dequantized coefficients and 1x for scratch
77
    // space for transforms.
78
23.2k
    JXL_ASSIGN_OR_RETURN(
79
23.2k
        float_memory_,
80
23.2k
        AlignedMemory::Create(memory_manager,
81
23.2k
                              max_block_area_ * 7 * sizeof(float)));
82
    // We need 3x int32 or int16 blocks for quantized coefficients.
83
23.2k
    JXL_ASSIGN_OR_RETURN(
84
23.2k
        int32_memory_,
85
23.2k
        AlignedMemory::Create(memory_manager,
86
23.2k
                              max_block_area_ * 3 * sizeof(int32_t)));
87
23.2k
    JXL_ASSIGN_OR_RETURN(
88
23.2k
        int16_memory_,
89
23.2k
        AlignedMemory::Create(memory_manager,
90
23.2k
                              max_block_area_ * 3 * sizeof(int16_t)));
91
23.2k
  }
92
93
50.3k
  dec_group_block = float_memory_.address<float>();
94
50.3k
  scratch_space = dec_group_block + max_block_area_ * 3;
95
50.3k
  dec_group_qblock = int32_memory_.address<int32_t>();
96
50.3k
  dec_group_qblock16 = int16_memory_.address<int16_t>();
97
50.3k
  return true;
98
50.3k
}
99
100
// Initialize the decoder state after all of DC is decoded.
101
87.5k
Status PassesDecoderState::InitForAC(size_t num_passes, ThreadPool* pool) {
102
87.5k
  shared_storage.coeff_order_size = 0;
103
2.45M
  for (uint8_t o = 0; o < AcStrategy::kNumValidStrategies; ++o) {
104
2.36M
    if (((1 << o) & used_acs) == 0) continue;
105
59.5k
    uint8_t ord = kStrategyOrder[o];
106
59.5k
    shared_storage.coeff_order_size =
107
59.5k
        std::max(kCoeffOrderOffset[3 * (ord + 1)] * kDCTBlockSize,
108
59.5k
                 shared_storage.coeff_order_size);
109
59.5k
  }
110
87.5k
  size_t sz = num_passes * shared_storage.coeff_order_size;
111
87.5k
  if (sz > shared_storage.coeff_orders.size()) {
112
30.8k
    shared_storage.coeff_orders.resize(sz);
113
30.8k
  }
114
87.5k
  return true;
115
87.5k
}
116
117
Status PassesDecoderState::PreparePipeline(const FrameHeader& frame_header,
118
                                           const ImageMetadata* metadata,
119
                                           ImageBundle* decoded,
120
101k
                                           PipelineOptions options) {
121
101k
  JxlMemoryManager* memory_manager = this->memory_manager();
122
101k
  size_t num_c = 3 + frame_header.nonserialized_metadata->m.num_extra_channels;
123
101k
  size_t num_tmp_c = options.render_noise ? 3 : 0;
124
125
101k
  if (frame_header.CanBeReferenced()) {
126
    // Necessary so that SetInputSizes() can allocate output buffers as needed.
127
37.9k
    frame_storage_for_referencing = ImageBundle(memory_manager, metadata);
128
37.9k
  }
129
130
101k
  RenderPipeline::Builder builder(memory_manager, num_c + num_tmp_c);
131
132
101k
  if (options.use_slow_render_pipeline) {
133
0
    builder.UseSimpleImplementation();
134
0
  }
135
136
101k
  if (!frame_header.chroma_subsampling.Is444()) {
137
37.8k
    for (size_t c = 0; c < 3; c++) {
138
28.4k
      if (frame_header.chroma_subsampling.HShift(c) != 0) {
139
14.7k
        JXL_RETURN_IF_ERROR(
140
14.7k
            builder.AddStage(GetChromaUpsamplingStage(c, /*horizontal=*/true)));
141
14.7k
      }
142
28.4k
      if (frame_header.chroma_subsampling.VShift(c) != 0) {
143
8.78k
        JXL_RETURN_IF_ERROR(builder.AddStage(
144
8.78k
            GetChromaUpsamplingStage(c, /*horizontal=*/false)));
145
8.78k
      }
146
28.4k
    }
147
9.46k
  }
148
149
101k
  if (frame_header.loop_filter.gab) {
150
69.1k
    JXL_RETURN_IF_ERROR(
151
69.1k
        builder.AddStage(GetGaborishStage(frame_header.loop_filter)));
152
69.1k
  }
153
154
101k
  {
155
101k
    const LoopFilter& lf = frame_header.loop_filter;
156
101k
    if (lf.epf_iters >= 3) {
157
1.70k
      JXL_RETURN_IF_ERROR(
158
1.70k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::Zero)));
159
1.70k
    }
160
101k
    if (lf.epf_iters >= 1) {
161
67.6k
      JXL_RETURN_IF_ERROR(
162
67.6k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::One)));
163
67.6k
    }
164
101k
    if (lf.epf_iters >= 2) {
165
64.8k
      JXL_RETURN_IF_ERROR(
166
64.8k
          builder.AddStage(GetEPFStage(lf, sigma, EpfStage::Two)));
167
64.8k
    }
168
101k
  }
169
170
101k
  bool late_ec_upsample = frame_header.upsampling != 1;
171
101k
  for (auto ecups : frame_header.extra_channel_upsampling) {
172
18.4k
    if (ecups != frame_header.upsampling) {
173
      // If patches are applied, either frame_header.upsampling == 1 or
174
      // late_ec_upsample is true.
175
7.09k
      late_ec_upsample = false;
176
7.09k
    }
177
18.4k
  }
178
179
101k
  if (!late_ec_upsample) {
180
79.1k
    for (size_t ec = 0; ec < frame_header.extra_channel_upsampling.size();
181
61.9k
         ec++) {
182
17.1k
      if (frame_header.extra_channel_upsampling[ec] != 1) {
183
7.11k
        JXL_RETURN_IF_ERROR(builder.AddStage(GetUpsamplingStage(
184
7.11k
            memory_manager, frame_header.nonserialized_metadata->transform_data,
185
7.11k
            3 + ec,
186
7.11k
            CeilLog2Nonzero(frame_header.extra_channel_upsampling[ec]))));
187
7.11k
      }
188
17.1k
    }
189
61.9k
  }
190
191
101k
  if ((frame_header.flags & FrameHeader::kPatches) != 0) {
192
8.08k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetPatchesStage(
193
8.08k
        &shared->image_features.patches,
194
8.08k
        &frame_header.nonserialized_metadata->m.extra_channel_info)));
195
8.08k
  }
196
101k
  if ((frame_header.flags & FrameHeader::kSplines) != 0) {
197
3.41k
    JXL_RETURN_IF_ERROR(
198
3.41k
        builder.AddStage(GetSplineStage(&shared->image_features.splines)));
199
3.41k
  }
200
201
101k
  if (frame_header.upsampling != 1) {
202
39.3k
    size_t nb_channels =
203
39.3k
        3 +
204
39.3k
        (late_ec_upsample ? frame_header.extra_channel_upsampling.size() : 0);
205
158k
    for (size_t c = 0; c < nb_channels; c++) {
206
119k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetUpsamplingStage(
207
119k
          memory_manager, frame_header.nonserialized_metadata->transform_data,
208
119k
          c, CeilLog2Nonzero(frame_header.upsampling))));
209
119k
    }
210
39.3k
  }
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
101k
  if (options.render_noise) {
215
10.2k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetConvolveNoiseStage(num_c)));
216
10.2k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetAddNoiseStage(
217
10.2k
        shared->image_features.noise_params, shared->cmap.base(), num_c)));
218
10.2k
  }
219
101k
  if (frame_header.dc_level != 0) {
220
9.98k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImage3FStage(
221
9.98k
        memory_manager, &shared_storage.dc_frames[frame_header.dc_level - 1])));
222
9.98k
  }
223
224
101k
  if (frame_header.CanBeReferenced() &&
225
37.9k
      frame_header.save_before_color_transform) {
226
13.2k
    JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImageBundleStage(
227
13.2k
        &frame_storage_for_referencing, output_encoding_info)));
228
13.2k
  }
229
230
101k
  bool has_alpha = false;
231
101k
  size_t alpha_c = 0;
232
112k
  for (size_t i = 0; i < metadata->extra_channel_info.size(); i++) {
233
14.7k
    if (metadata->extra_channel_info[i].type == ExtraChannel::kAlpha) {
234
3.39k
      has_alpha = true;
235
3.39k
      alpha_c = 3 + i;
236
3.39k
      break;
237
3.39k
    }
238
14.7k
  }
239
240
101k
  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
101k
  } else {
254
101k
    bool linear = false;
255
101k
    if (frame_header.color_transform == ColorTransform::kYCbCr) {
256
9.58k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetYCbCrStage()));
257
91.4k
    } else if (frame_header.color_transform == ColorTransform::kXYB) {
258
53.4k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetXYBStage(output_encoding_info)));
259
53.4k
      if (output_encoding_info.color_encoding.GetColorSpace() !=
260
53.4k
          ColorSpace::kXYB) {
261
53.4k
        linear = true;
262
53.4k
      }
263
53.4k
    }  // Nothing to do for kNone.
264
265
101k
    if (options.coalescing && NeedsBlending(frame_header)) {
266
24.0k
      if (linear) {
267
10.6k
        JXL_RETURN_IF_ERROR(
268
10.6k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
269
10.6k
        linear = false;
270
10.6k
      }
271
24.0k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetBlendingStage(
272
24.0k
          frame_header, this, output_encoding_info.color_encoding)));
273
24.0k
    }
274
275
101k
    if (options.coalescing && frame_header.CanBeReferenced() &&
276
37.9k
        !frame_header.save_before_color_transform) {
277
24.7k
      if (linear) {
278
2.03k
        JXL_RETURN_IF_ERROR(
279
2.03k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
280
2.03k
        linear = false;
281
2.03k
      }
282
24.7k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToImageBundleStage(
283
24.7k
          &frame_storage_for_referencing, output_encoding_info)));
284
24.7k
    }
285
286
101k
    if (options.render_spotcolors &&
287
87.6k
        frame_header.nonserialized_metadata->m.Find(ExtraChannel::kSpotColor)) {
288
12.4k
      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
9.93k
        const ExtraChannelInfo& eci = metadata->extra_channel_info[i];
291
9.93k
        if (eci.type == ExtraChannel::kSpotColor) {
292
2.56k
          JXL_RETURN_IF_ERROR(
293
2.56k
              builder.AddStage(GetSpotColorStage(i, eci.spot_color)));
294
2.56k
        }
295
9.93k
      }
296
2.56k
    }
297
298
101k
    auto tone_mapping_stage = GetToneMappingStage(output_encoding_info);
299
101k
    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
101k
    if (linear) {
319
40.7k
      const size_t channels_src =
320
40.7k
          (output_encoding_info.orig_color_encoding.IsCMYK()
321
40.7k
               ? 4
322
40.7k
               : output_encoding_info.orig_color_encoding.Channels());
323
40.7k
      const size_t channels_dst =
324
40.7k
          output_encoding_info.color_encoding.Channels();
325
40.7k
      bool mixing_color_and_grey = (channels_dst != channels_src);
326
40.7k
      if ((output_encoding_info.color_encoding_is_original) ||
327
40.7k
          (!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
40.7k
        JXL_RETURN_IF_ERROR(
338
40.7k
            builder.AddStage(GetFromLinearStage(output_encoding_info)));
339
40.7k
      } else {
340
0
        if (!output_encoding_info.linear_color_encoding.CreateICC()) {
341
0
          return JXL_FAILURE("Failed to create ICC");
342
0
        }
343
0
        auto cms_stage = GetCmsStage(output_encoding_info);
344
0
        if (cms_stage) {
345
0
          JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
346
0
        }
347
0
      }
348
40.7k
      linear = false;
349
60.3k
    } else {
350
60.3k
      auto cms_stage = GetCmsStage(output_encoding_info, false);
351
60.3k
      if (cms_stage) {
352
0
        JXL_RETURN_IF_ERROR(builder.AddStage(std::move(cms_stage)));
353
0
      }
354
60.3k
    }
355
101k
    (void)linear;
356
357
101k
    if (main_output.callback.IsPresent() || main_output.buffer) {
358
39.6k
      JXL_RETURN_IF_ERROR(builder.AddStage(GetWriteToOutputStage(
359
39.6k
          main_output, width, height, has_alpha, unpremul_alpha, alpha_c,
360
39.6k
          undo_orientation, extra_output, memory_manager)));
361
61.4k
    } else {
362
61.4k
      JXL_RETURN_IF_ERROR(builder.AddStage(
363
61.4k
          GetWriteToImageBundleStage(decoded, output_encoding_info)));
364
61.4k
    }
365
101k
  }
366
101k
  JXL_ASSIGN_OR_RETURN(render_pipeline,
367
101k
                       std::move(builder).Finalize(shared->frame_dim));
368
101k
  return render_pipeline->IsInitialized();
369
101k
}
370
371
}  // namespace jxl