/src/libjxl/lib/jxl/dec_patch_dictionary.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_patch_dictionary.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <algorithm> |
11 | | #include <cstdint> |
12 | | #include <cstdlib> |
13 | | #include <utility> |
14 | | #include <vector> |
15 | | |
16 | | #include "lib/jxl/base/compiler_specific.h" // ssize_t |
17 | | #include "lib/jxl/base/printf_macros.h" |
18 | | #include "lib/jxl/base/status.h" |
19 | | #include "lib/jxl/blending.h" |
20 | | #include "lib/jxl/common.h" // kMaxNumReferenceFrames |
21 | | #include "lib/jxl/dec_ans.h" |
22 | | #include "lib/jxl/dec_bit_reader.h" |
23 | | #include "lib/jxl/image.h" |
24 | | #include "lib/jxl/image_bundle.h" |
25 | | #include "lib/jxl/image_metadata.h" |
26 | | #include "lib/jxl/pack_signed.h" |
27 | | #include "lib/jxl/patch_dictionary_internal.h" |
28 | | |
29 | | namespace jxl { |
30 | | |
31 | | Status PatchDictionary::Decode(JxlMemoryManager* memory_manager, BitReader* br, |
32 | | size_t xsize, size_t ysize, |
33 | | size_t num_extra_channels, |
34 | 606 | bool* uses_extra_channels) { |
35 | 606 | positions_.clear(); |
36 | 606 | blendings_stride_ = num_extra_channels + 1; |
37 | 606 | std::vector<uint8_t> context_map; |
38 | 606 | ANSCode code; |
39 | 606 | JXL_RETURN_IF_ERROR(DecodeHistograms( |
40 | 606 | memory_manager, br, kNumPatchDictionaryContexts, &code, &context_map)); |
41 | 1.17k | JXL_ASSIGN_OR_RETURN(ANSSymbolReader decoder, |
42 | 1.17k | ANSSymbolReader::Create(&code, br)); |
43 | | |
44 | 1.25M | auto read_num = [&](size_t context) -> size_t { |
45 | 1.25M | size_t r = decoder.ReadHybridUint(context, br, context_map); |
46 | 1.25M | return r; |
47 | 1.25M | }; |
48 | | |
49 | 1.17k | size_t num_ref_patch = read_num(kNumRefPatchContext); |
50 | | // Limit max memory usage of patches to about 66 bytes per pixel (assuming 8 |
51 | | // bytes per size_t) |
52 | 1.17k | const size_t num_pixels = xsize * ysize; |
53 | 1.17k | const size_t max_ref_patches = 1024 + num_pixels / 4; |
54 | 1.17k | const size_t max_patches = max_ref_patches * 4; |
55 | 1.17k | const size_t max_blending_infos = max_patches * 4; |
56 | 1.17k | if (num_ref_patch > max_ref_patches) { |
57 | 7 | return JXL_FAILURE("Too many patches in dictionary"); |
58 | 7 | } |
59 | | |
60 | 580 | size_t total_patches = 0; |
61 | 580 | size_t next_size = 1; |
62 | | |
63 | 29.0k | for (size_t id = 0; id < num_ref_patch; id++) { |
64 | 28.5k | PatchReferencePosition ref_pos; |
65 | 28.5k | ref_pos.ref = read_num(kReferenceFrameContext); |
66 | 28.5k | if (ref_pos.ref >= kMaxNumReferenceFrames || |
67 | 28.5k | reference_frames_->at(ref_pos.ref).frame->xsize() == 0) { |
68 | 24 | return JXL_FAILURE("Invalid reference frame ID"); |
69 | 24 | } |
70 | 28.5k | if (!reference_frames_->at(ref_pos.ref).ib_is_in_xyb) { |
71 | 2 | return JXL_FAILURE( |
72 | 2 | "Patches cannot use frames saved post color transforms"); |
73 | 2 | } |
74 | 28.5k | const ImageBundle& ib = *reference_frames_->at(ref_pos.ref).frame; |
75 | 28.5k | ref_pos.x0 = read_num(kPatchReferencePositionContext); |
76 | 28.5k | ref_pos.y0 = read_num(kPatchReferencePositionContext); |
77 | 28.5k | ref_pos.xsize = read_num(kPatchSizeContext) + 1; |
78 | 28.5k | ref_pos.ysize = read_num(kPatchSizeContext) + 1; |
79 | 28.5k | if (ref_pos.x0 + ref_pos.xsize > ib.xsize()) { |
80 | 1 | return JXL_FAILURE("Invalid position specified in reference frame"); |
81 | 1 | } |
82 | 28.5k | if (ref_pos.y0 + ref_pos.ysize > ib.ysize()) { |
83 | 1 | return JXL_FAILURE("Invalid position specified in reference frame"); |
84 | 1 | } |
85 | 28.5k | size_t id_count = read_num(kPatchCountContext); |
86 | 28.5k | if (id_count > max_patches) { |
87 | 2 | return JXL_FAILURE("Too many patches in dictionary"); |
88 | 2 | } |
89 | 28.5k | id_count++; |
90 | 28.5k | total_patches += id_count; |
91 | 28.5k | if (total_patches > max_patches) { |
92 | 1 | return JXL_FAILURE("Too many patches in dictionary"); |
93 | 1 | } |
94 | 28.5k | if (next_size < total_patches) { |
95 | 2.76k | next_size *= 2; |
96 | 2.76k | next_size = std::min<size_t>(next_size, max_patches); |
97 | 2.76k | } |
98 | 28.5k | if (next_size * blendings_stride_ > max_blending_infos) { |
99 | 0 | return JXL_FAILURE("Too many patches in dictionary"); |
100 | 0 | } |
101 | 28.5k | positions_.reserve(next_size); |
102 | 28.5k | blendings_.reserve(next_size * blendings_stride_); |
103 | 28.5k | bool choose_alpha = (num_extra_channels > 1); |
104 | 384k | for (size_t i = 0; i < id_count; i++) { |
105 | 356k | PatchPosition pos; |
106 | 356k | pos.ref_pos_idx = ref_positions_.size(); |
107 | 356k | if (i == 0) { |
108 | 28.5k | pos.x = read_num(kPatchPositionContext); |
109 | 28.5k | pos.y = read_num(kPatchPositionContext); |
110 | 327k | } else { |
111 | 327k | ssize_t deltax = UnpackSigned(read_num(kPatchOffsetContext)); |
112 | 327k | if (deltax < 0 && static_cast<size_t>(-deltax) > positions_.back().x) { |
113 | 8 | return JXL_FAILURE("Invalid patch: negative x coordinate (%" PRIuS |
114 | 8 | " base x %" PRIdS " delta x)", |
115 | 8 | positions_.back().x, deltax); |
116 | 8 | } |
117 | 327k | pos.x = positions_.back().x + deltax; |
118 | 327k | ssize_t deltay = UnpackSigned(read_num(kPatchOffsetContext)); |
119 | 327k | if (deltay < 0 && static_cast<size_t>(-deltay) > positions_.back().y) { |
120 | 6 | return JXL_FAILURE("Invalid patch: negative y coordinate (%" PRIuS |
121 | 6 | " base y %" PRIdS " delta y)", |
122 | 6 | positions_.back().y, deltay); |
123 | 6 | } |
124 | 327k | pos.y = positions_.back().y + deltay; |
125 | 327k | } |
126 | 356k | if (pos.x + ref_pos.xsize > xsize) { |
127 | 3 | return JXL_FAILURE("Invalid patch x: at %" PRIuS " + %" PRIuS |
128 | 3 | " > %" PRIuS, |
129 | 3 | pos.x, ref_pos.xsize, xsize); |
130 | 3 | } |
131 | 356k | if (pos.y + ref_pos.ysize > ysize) { |
132 | 4 | return JXL_FAILURE("Invalid patch y: at %" PRIuS " + %" PRIuS |
133 | 4 | " > %" PRIuS, |
134 | 4 | pos.y, ref_pos.ysize, ysize); |
135 | 4 | } |
136 | 712k | for (size_t j = 0; j < blendings_stride_; j++) { |
137 | 356k | uint32_t blend_mode = read_num(kPatchBlendModeContext); |
138 | 356k | if (blend_mode >= kNumPatchBlendModes) { |
139 | 1 | return JXL_FAILURE("Invalid patch blend mode: %u", blend_mode); |
140 | 1 | } |
141 | 356k | PatchBlending info; |
142 | 356k | info.mode = static_cast<PatchBlendMode>(blend_mode); |
143 | 356k | if (UsesAlpha(info.mode)) { |
144 | 11.7k | *uses_extra_channels = true; |
145 | 11.7k | } |
146 | 356k | if (info.mode != PatchBlendMode::kNone && j > 0) { |
147 | 0 | *uses_extra_channels = true; |
148 | 0 | } |
149 | 356k | if (UsesAlpha(info.mode) && choose_alpha) { |
150 | 0 | info.alpha_channel = read_num(kPatchAlphaChannelContext); |
151 | 0 | if (info.alpha_channel >= num_extra_channels) { |
152 | 0 | return JXL_FAILURE( |
153 | 0 | "Invalid alpha channel for blending: %u out of %u\n", |
154 | 0 | info.alpha_channel, static_cast<uint32_t>(num_extra_channels)); |
155 | 0 | } |
156 | 356k | } else { |
157 | 356k | info.alpha_channel = 0; |
158 | 356k | } |
159 | 356k | if (UsesClamp(info.mode)) { |
160 | 14.1k | info.clamp = static_cast<bool>(read_num(kPatchClampContext)); |
161 | 341k | } else { |
162 | 341k | info.clamp = false; |
163 | 341k | } |
164 | 356k | blendings_.push_back(info); |
165 | 356k | } |
166 | 356k | positions_.emplace_back(pos); |
167 | 356k | } |
168 | 28.5k | ref_positions_.emplace_back(ref_pos); |
169 | 28.5k | } |
170 | 527 | positions_.shrink_to_fit(); |
171 | | |
172 | 527 | if (!decoder.CheckANSFinalState()) { |
173 | 0 | return JXL_FAILURE("ANS checksum failure."); |
174 | 0 | } |
175 | | |
176 | 527 | ComputePatchTree(); |
177 | 527 | return true; |
178 | 527 | } |
179 | | |
180 | 198 | int PatchDictionary::GetReferences() const { |
181 | 198 | int result = 0; |
182 | 919 | for (const auto& ref_pos : ref_positions_) { |
183 | 919 | result |= (1 << static_cast<int>(ref_pos.ref)); |
184 | 919 | } |
185 | 198 | return result; |
186 | 198 | } |
187 | | |
188 | | namespace { |
189 | | struct PatchInterval { |
190 | | size_t idx; |
191 | | size_t y0, y1; |
192 | | }; |
193 | | } // namespace |
194 | | |
195 | 53.8k | void PatchDictionary::ComputePatchTree() { |
196 | 53.8k | patch_tree_.clear(); |
197 | 53.8k | num_patches_.clear(); |
198 | 53.8k | sorted_patches_y0_.clear(); |
199 | 53.8k | sorted_patches_y1_.clear(); |
200 | 53.8k | if (positions_.empty()) { |
201 | 52.4k | return; |
202 | 52.4k | } |
203 | | // Create a y-interval for each patch. |
204 | 1.40k | std::vector<PatchInterval> intervals(positions_.size()); |
205 | 1.55M | for (size_t i = 0; i < positions_.size(); ++i) { |
206 | 1.55M | const auto& pos = positions_[i]; |
207 | 1.55M | intervals[i].idx = i; |
208 | 1.55M | intervals[i].y0 = pos.y; |
209 | 1.55M | intervals[i].y1 = pos.y + ref_positions_[pos.ref_pos_idx].ysize; |
210 | 1.55M | } |
211 | 234k | auto sort_by_y0 = [&intervals](size_t start, size_t end) { |
212 | 234k | std::sort(intervals.data() + start, intervals.data() + end, |
213 | 55.4M | [](const PatchInterval& i0, const PatchInterval& i1) { |
214 | 55.4M | return i0.y0 < i1.y0; |
215 | 55.4M | }); |
216 | 234k | }; |
217 | 118k | auto sort_by_y1 = [&intervals](size_t start, size_t end) { |
218 | 118k | std::sort(intervals.data() + start, intervals.data() + end, |
219 | 43.5M | [](const PatchInterval& i0, const PatchInterval& i1) { |
220 | 43.5M | return i0.y1 < i1.y1; |
221 | 43.5M | }); |
222 | 118k | }; |
223 | | // Count the number of patches for each row. |
224 | 1.40k | sort_by_y1(0, intervals.size()); |
225 | 1.40k | num_patches_.resize(intervals.back().y1); |
226 | 1.55M | for (auto iv : intervals) { |
227 | 8.79M | for (size_t y = iv.y0; y < iv.y1; ++y) num_patches_[y]++; |
228 | 1.55M | } |
229 | 1.40k | PatchTreeNode root; |
230 | 1.40k | root.start = 0; |
231 | 1.40k | root.num = intervals.size(); |
232 | 1.40k | patch_tree_.push_back(root); |
233 | 1.40k | size_t next = 0; |
234 | 118k | while (next < patch_tree_.size()) { |
235 | 117k | auto& node = patch_tree_[next]; |
236 | 117k | size_t start = node.start; |
237 | 117k | size_t end = node.start + node.num; |
238 | | // Choose the y_center for this node to be the median of interval starts. |
239 | 117k | sort_by_y0(start, end); |
240 | 117k | size_t middle_idx = start + node.num / 2; |
241 | 117k | node.y_center = intervals[middle_idx].y0; |
242 | | // Divide the intervals in [start, end) into three groups: |
243 | | // * those completely to the right of y_center: [right_start, end) |
244 | | // * those overlapping y_center: [left_end, right_start) |
245 | | // * those completely to the left of y_center: [start, left_end) |
246 | 117k | size_t right_start = middle_idx; |
247 | 799k | while (right_start < end && intervals[right_start].y0 == node.y_center) { |
248 | 682k | ++right_start; |
249 | 682k | } |
250 | 117k | sort_by_y1(start, right_start); |
251 | 117k | size_t left_end = right_start; |
252 | 1.67M | while (left_end > start && intervals[left_end - 1].y1 > node.y_center) { |
253 | 1.55M | --left_end; |
254 | 1.55M | } |
255 | | // Fill in sorted_patches_y0_ and sorted_patches_y1_ for the current node. |
256 | 117k | node.num = right_start - left_end; |
257 | 117k | node.start = sorted_patches_y0_.size(); |
258 | 117k | for (ssize_t i = static_cast<ssize_t>(right_start) - 1; |
259 | 1.67M | i >= static_cast<ssize_t>(left_end); --i) { |
260 | 1.55M | sorted_patches_y1_.emplace_back(intervals[i].y1, intervals[i].idx); |
261 | 1.55M | } |
262 | 117k | sort_by_y0(left_end, right_start); |
263 | 1.67M | for (size_t i = left_end; i < right_start; ++i) { |
264 | 1.55M | sorted_patches_y0_.emplace_back(intervals[i].y0, intervals[i].idx); |
265 | 1.55M | } |
266 | | // Create the left and right nodes (if not empty). |
267 | 117k | node.left_child = node.right_child = -1; |
268 | 117k | if (left_end > start) { |
269 | 56.4k | PatchTreeNode left; |
270 | 56.4k | left.start = start; |
271 | 56.4k | left.num = left_end - left.start; |
272 | 56.4k | patch_tree_[next].left_child = patch_tree_.size(); |
273 | 56.4k | patch_tree_.push_back(left); |
274 | 56.4k | } |
275 | 117k | if (right_start < end) { |
276 | 59.5k | PatchTreeNode right; |
277 | 59.5k | right.start = right_start; |
278 | 59.5k | right.num = end - right.start; |
279 | 59.5k | patch_tree_[next].right_child = patch_tree_.size(); |
280 | 59.5k | patch_tree_.push_back(right); |
281 | 59.5k | } |
282 | 117k | ++next; |
283 | 117k | } |
284 | 1.40k | } |
285 | | |
286 | 2.52M | std::vector<size_t> PatchDictionary::GetPatchesForRow(size_t y) const { |
287 | 2.52M | std::vector<size_t> result; |
288 | 2.52M | if (y < num_patches_.size() && num_patches_[y] > 0) { |
289 | 1.47M | result.reserve(num_patches_[y]); |
290 | 10.9M | for (ssize_t tree_idx = 0; tree_idx != -1;) { |
291 | 9.47M | JXL_DASSERT(tree_idx < static_cast<ssize_t>(patch_tree_.size())); |
292 | 9.47M | const auto& node = patch_tree_[tree_idx]; |
293 | 9.47M | if (y <= node.y_center) { |
294 | 16.7M | for (size_t i = 0; i < node.num; ++i) { |
295 | 16.0M | const auto& p = sorted_patches_y0_[node.start + i]; |
296 | 16.0M | if (y < p.first) break; |
297 | 11.9M | result.push_back(p.second); |
298 | 11.9M | } |
299 | 4.76M | tree_idx = y < node.y_center ? node.left_child : -1; |
300 | 4.76M | } else { |
301 | 14.1M | for (size_t i = 0; i < node.num; ++i) { |
302 | 13.5M | const auto& p = sorted_patches_y1_[node.start + i]; |
303 | 13.5M | if (y >= p.first) break; |
304 | 9.47M | result.push_back(p.second); |
305 | 9.47M | } |
306 | 4.71M | tree_idx = node.right_child; |
307 | 4.71M | } |
308 | 9.47M | } |
309 | | // Ensure that he relative order of patches that affect the same pixels is |
310 | | // preserved. This is important for patches that have a blend mode |
311 | | // different from kAdd. |
312 | 1.47M | std::sort(result.begin(), result.end()); |
313 | 1.47M | } |
314 | 2.52M | return result; |
315 | 2.52M | } |
316 | | |
317 | | // Adds patches to a segment of `xsize` pixels, starting at `inout`, assumed |
318 | | // to be located at position (x0, y) in the frame. |
319 | | Status PatchDictionary::AddOneRow( |
320 | | float* const* inout, size_t y, size_t x0, size_t xsize, |
321 | 1.94M | const std::vector<ExtraChannelInfo>& extra_channel_info) const { |
322 | 1.94M | size_t num_ec = extra_channel_info.size(); |
323 | 1.94M | JXL_ENSURE(num_ec + 1 <= blendings_stride_); |
324 | 1.94M | std::vector<const float*> fg_ptrs(3 + num_ec); |
325 | 18.3M | for (size_t pos_idx : GetPatchesForRow(y)) { |
326 | 18.3M | const size_t blending_idx = pos_idx * blendings_stride_; |
327 | 18.3M | const PatchPosition& pos = positions_[pos_idx]; |
328 | 18.3M | const PatchReferencePosition& ref_pos = ref_positions_[pos.ref_pos_idx]; |
329 | 18.3M | size_t by = pos.y; |
330 | 18.3M | size_t bx = pos.x; |
331 | 18.3M | size_t patch_xsize = ref_pos.xsize; |
332 | 18.3M | JXL_ENSURE(y >= by); |
333 | 18.3M | JXL_ENSURE(y < by + ref_pos.ysize); |
334 | 18.3M | size_t iy = y - by; |
335 | 18.3M | size_t ref = ref_pos.ref; |
336 | 18.3M | if (bx >= x0 + xsize) continue; |
337 | 14.0M | if (bx + patch_xsize < x0) continue; |
338 | 9.46M | size_t patch_x0 = std::max(bx, x0); |
339 | 9.46M | size_t patch_x1 = std::min(bx + patch_xsize, x0 + xsize); |
340 | 37.8M | for (size_t c = 0; c < 3; c++) { |
341 | 28.4M | fg_ptrs[c] = reference_frames_->at(ref).frame->color()->ConstPlaneRow( |
342 | 28.4M | c, ref_pos.y0 + iy) + |
343 | 28.4M | ref_pos.x0 + x0 - bx; |
344 | 28.4M | } |
345 | 9.46M | for (size_t i = 0; i < num_ec; i++) { |
346 | 0 | fg_ptrs[3 + i] = |
347 | 0 | reference_frames_->at(ref).frame->extra_channels()[i].ConstRow( |
348 | 0 | ref_pos.y0 + iy) + |
349 | 0 | ref_pos.x0 + x0 - bx; |
350 | 0 | } |
351 | 9.46M | JXL_RETURN_IF_ERROR(PerformBlending( |
352 | 9.46M | memory_manager_, inout, fg_ptrs.data(), inout, patch_x0 - x0, |
353 | 9.46M | patch_x1 - patch_x0, blendings_[blending_idx], |
354 | 9.46M | blendings_.data() + blending_idx + 1, extra_channel_info)); |
355 | 9.46M | } |
356 | 1.94M | return true; |
357 | 1.94M | } |
358 | | } // namespace jxl |