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