Coverage Report

Created: 2026-02-14 07:11

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