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