Coverage Report

Created: 2025-07-23 07:47

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