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