summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/dec_patch_dictionary.cc
blob: 85e5de3c8d8bf4da6d7f09a9dae1c1004d1a7d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jxl/dec_patch_dictionary.h"

#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>

#include <algorithm>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "lib/jxl/ans_params.h"
#include "lib/jxl/base/compiler_specific.h"
#include "lib/jxl/base/override.h"
#include "lib/jxl/base/printf_macros.h"
#include "lib/jxl/base/status.h"
#include "lib/jxl/blending.h"
#include "lib/jxl/chroma_from_luma.h"
#include "lib/jxl/color_management.h"
#include "lib/jxl/common.h"
#include "lib/jxl/dec_ans.h"
#include "lib/jxl/dec_frame.h"
#include "lib/jxl/entropy_coder.h"
#include "lib/jxl/frame_header.h"
#include "lib/jxl/image.h"
#include "lib/jxl/image_bundle.h"
#include "lib/jxl/image_ops.h"
#include "lib/jxl/patch_dictionary_internal.h"

namespace jxl {

Status PatchDictionary::Decode(BitReader* br, size_t xsize, size_t ysize,
                               bool* uses_extra_channels) {
  positions_.clear();
  std::vector<uint8_t> context_map;
  ANSCode code;
  JXL_RETURN_IF_ERROR(
      DecodeHistograms(br, kNumPatchDictionaryContexts, &code, &context_map));
  ANSSymbolReader decoder(&code, br);

  auto read_num = [&](size_t context) {
    size_t r = decoder.ReadHybridUint(context, br, context_map);
    return r;
  };

  size_t num_ref_patch = read_num(kNumRefPatchContext);
  // Limit max memory usage of patches to about 66 bytes per pixel (assuming 8
  // bytes per size_t)
  const size_t num_pixels = xsize * ysize;
  const size_t max_ref_patches = 1024 + num_pixels / 4;
  const size_t max_patches = max_ref_patches * 4;
  const size_t max_blending_infos = max_patches * 4;
  if (num_ref_patch > max_ref_patches) {
    return JXL_FAILURE("Too many patches in dictionary");
  }
  size_t num_ec = shared_->metadata->m.num_extra_channels;

  size_t total_patches = 0;
  size_t next_size = 1;

  for (size_t id = 0; id < num_ref_patch; id++) {
    PatchReferencePosition ref_pos;
    ref_pos.ref = read_num(kReferenceFrameContext);
    if (ref_pos.ref >= kMaxNumReferenceFrames ||
        shared_->reference_frames[ref_pos.ref].frame.xsize() == 0) {
      return JXL_FAILURE("Invalid reference frame ID");
    }
    if (!shared_->reference_frames[ref_pos.ref].ib_is_in_xyb) {
      return JXL_FAILURE(
          "Patches cannot use frames saved post color transforms");
    }
    const ImageBundle& ib = shared_->reference_frames[ref_pos.ref].frame;
    ref_pos.x0 = read_num(kPatchReferencePositionContext);
    ref_pos.y0 = read_num(kPatchReferencePositionContext);
    ref_pos.xsize = read_num(kPatchSizeContext) + 1;
    ref_pos.ysize = read_num(kPatchSizeContext) + 1;
    if (ref_pos.x0 + ref_pos.xsize > ib.xsize()) {
      return JXL_FAILURE("Invalid position specified in reference frame");
    }
    if (ref_pos.y0 + ref_pos.ysize > ib.ysize()) {
      return JXL_FAILURE("Invalid position specified in reference frame");
    }
    size_t id_count = read_num(kPatchCountContext) + 1;
    total_patches += id_count;
    if (total_patches > max_patches) {
      return JXL_FAILURE("Too many patches in dictionary");
    }
    if (next_size < total_patches) {
      next_size *= 2;
      next_size = std::min<size_t>(next_size, max_patches);
    }
    if (next_size * (num_ec + 1) > max_blending_infos) {
      return JXL_FAILURE("Too many patches in dictionary");
    }
    positions_.reserve(next_size);
    blendings_.reserve(next_size * (num_ec + 1));
    for (size_t i = 0; i < id_count; i++) {
      PatchPosition pos;
      pos.ref_pos_idx = ref_positions_.size();
      if (i == 0) {
        pos.x = read_num(kPatchPositionContext);
        pos.y = read_num(kPatchPositionContext);
      } else {
        pos.x =
            positions_.back().x + UnpackSigned(read_num(kPatchOffsetContext));
        pos.y =
            positions_.back().y + UnpackSigned(read_num(kPatchOffsetContext));
      }
      if (pos.x + ref_pos.xsize > xsize) {
        return JXL_FAILURE("Invalid patch x: at %" PRIuS " + %" PRIuS
                           " > %" PRIuS,
                           pos.x, ref_pos.xsize, xsize);
      }
      if (pos.y + ref_pos.ysize > ysize) {
        return JXL_FAILURE("Invalid patch y: at %" PRIuS " + %" PRIuS
                           " > %" PRIuS,
                           pos.y, ref_pos.ysize, ysize);
      }
      for (size_t j = 0; j < num_ec + 1; j++) {
        uint32_t blend_mode = read_num(kPatchBlendModeContext);
        if (blend_mode >= uint32_t(PatchBlendMode::kNumBlendModes)) {
          return JXL_FAILURE("Invalid patch blend mode: %u", blend_mode);
        }
        PatchBlending info;
        info.mode = static_cast<PatchBlendMode>(blend_mode);
        if (UsesAlpha(info.mode)) {
          *uses_extra_channels = true;
        }
        if (info.mode != PatchBlendMode::kNone && j > 0) {
          *uses_extra_channels = true;
        }
        if (UsesAlpha(info.mode) &&
            shared_->metadata->m.extra_channel_info.size() > 1) {
          info.alpha_channel = read_num(kPatchAlphaChannelContext);
          if (info.alpha_channel >=
              shared_->metadata->m.extra_channel_info.size()) {
            return JXL_FAILURE(
                "Invalid alpha channel for blending: %u out of %u\n",
                info.alpha_channel,
                (uint32_t)shared_->metadata->m.extra_channel_info.size());
          }
        } else {
          info.alpha_channel = 0;
        }
        if (UsesClamp(info.mode)) {
          info.clamp = read_num(kPatchClampContext);
        } else {
          info.clamp = false;
        }
        blendings_.push_back(info);
      }
      positions_.push_back(std::move(pos));
    }
    ref_positions_.emplace_back(std::move(ref_pos));
  }
  positions_.shrink_to_fit();

  if (!decoder.CheckANSFinalState()) {
    return JXL_FAILURE("ANS checksum failure.");
  }

  ComputePatchTree();
  return true;
}

int PatchDictionary::GetReferences() const {
  int result = 0;
  for (size_t i = 0; i < ref_positions_.size(); ++i) {
    result |= (1 << static_cast<int>(ref_positions_[i].ref));
  }
  return result;
}

namespace {
struct PatchInterval {
  size_t idx;
  size_t y0, y1;
};
}  // namespace

void PatchDictionary::ComputePatchTree() {
  patch_tree_.clear();
  num_patches_.clear();
  sorted_patches_y0_.clear();
  sorted_patches_y1_.clear();
  if (positions_.empty()) {
    return;
  }
  // Create a y-interval for each patch.
  std::vector<PatchInterval> intervals(positions_.size());
  for (size_t i = 0; i < positions_.size(); ++i) {
    const auto& pos = positions_[i];
    intervals[i].idx = i;
    intervals[i].y0 = pos.y;
    intervals[i].y1 = pos.y + ref_positions_[pos.ref_pos_idx].ysize;
  }
  auto sort_by_y0 = [&intervals](size_t start, size_t end) {
    std::sort(intervals.data() + start, intervals.data() + end,
              [](const PatchInterval& i0, const PatchInterval& i1) {
                return i0.y0 < i1.y0;
              });
  };
  auto sort_by_y1 = [&intervals](size_t start, size_t end) {
    std::sort(intervals.data() + start, intervals.data() + end,
              [](const PatchInterval& i0, const PatchInterval& i1) {
                return i0.y1 < i1.y1;
              });
  };
  // Count the number of patches for each row.
  sort_by_y1(0, intervals.size());
  num_patches_.resize(intervals.back().y1);
  for (auto iv : intervals) {
    for (size_t y = iv.y0; y < iv.y1; ++y) num_patches_[y]++;
  }
  PatchTreeNode root;
  root.start = 0;
  root.num = intervals.size();
  patch_tree_.push_back(root);
  size_t next = 0;
  while (next < patch_tree_.size()) {
    auto& node = patch_tree_[next];
    size_t start = node.start;
    size_t end = node.start + node.num;
    // Choose the y_center for this node to be the median of interval starts.
    sort_by_y0(start, end);
    size_t middle_idx = start + node.num / 2;
    node.y_center = intervals[middle_idx].y0;
    // Divide the intervals in [start, end) into three groups:
    //   * those completely to the right of y_center: [right_start, end)
    //   * those overlapping y_center: [left_end, right_start)
    //   * those completely to the left of y_center: [start, left_end)
    size_t right_start = middle_idx;
    while (right_start < end && intervals[right_start].y0 == node.y_center) {
      ++right_start;
    }
    sort_by_y1(start, right_start);
    size_t left_end = right_start;
    while (left_end > start && intervals[left_end - 1].y1 > node.y_center) {
      --left_end;
    }
    // Fill in sorted_patches_y0_ and sorted_patches_y1_ for the current node.
    node.num = right_start - left_end;
    node.start = sorted_patches_y0_.size();
    for (ssize_t i = static_cast<ssize_t>(right_start) - 1;
         i >= static_cast<ssize_t>(left_end); --i) {
      sorted_patches_y1_.push_back({intervals[i].y1, intervals[i].idx});
    }
    sort_by_y0(left_end, right_start);
    for (size_t i = left_end; i < right_start; ++i) {
      sorted_patches_y0_.push_back({intervals[i].y0, intervals[i].idx});
    }
    // Create the left and right nodes (if not empty).
    node.left_child = node.right_child = -1;
    if (left_end > start) {
      PatchTreeNode left;
      left.start = start;
      left.num = left_end - left.start;
      patch_tree_[next].left_child = patch_tree_.size();
      patch_tree_.push_back(left);
    }
    if (right_start < end) {
      PatchTreeNode right;
      right.start = right_start;
      right.num = end - right.start;
      patch_tree_[next].right_child = patch_tree_.size();
      patch_tree_.push_back(right);
    }
    ++next;
  }
}

std::vector<size_t> PatchDictionary::GetPatchesForRow(size_t y) const {
  std::vector<size_t> result;
  if (y < num_patches_.size() && num_patches_[y] > 0) {
    result.reserve(num_patches_[y]);
    for (ssize_t tree_idx = 0; tree_idx != -1;) {
      JXL_DASSERT(tree_idx < (ssize_t)patch_tree_.size());
      const auto& node = patch_tree_[tree_idx];
      if (y <= node.y_center) {
        for (size_t i = 0; i < node.num; ++i) {
          const auto& p = sorted_patches_y0_[node.start + i];
          if (y < p.first) break;
          result.push_back(p.second);
        }
        tree_idx = y < node.y_center ? node.left_child : -1;
      } else {
        for (size_t i = 0; i < node.num; ++i) {
          const auto& p = sorted_patches_y1_[node.start + i];
          if (y >= p.first) break;
          result.push_back(p.second);
        }
        tree_idx = node.right_child;
      }
    }
    // Ensure that he relative order of patches that affect the same pixels is
    // preserved. This is important for patches that have a blend mode
    // different from kAdd.
    std::sort(result.begin(), result.end());
  }
  return result;
}

// Adds patches to a segment of `xsize` pixels, starting at `inout`, assumed
// to be located at position (x0, y) in the frame.
void PatchDictionary::AddOneRow(float* const* inout, size_t y, size_t x0,
                                size_t xsize) const {
  size_t num_ec = shared_->metadata->m.num_extra_channels;
  std::vector<const float*> fg_ptrs(3 + num_ec);
  for (size_t pos_idx : GetPatchesForRow(y)) {
    const size_t blending_idx = pos_idx * (num_ec + 1);
    const PatchPosition& pos = positions_[pos_idx];
    const PatchReferencePosition& ref_pos = ref_positions_[pos.ref_pos_idx];
    size_t by = pos.y;
    size_t bx = pos.x;
    size_t patch_xsize = ref_pos.xsize;
    JXL_DASSERT(y >= by);
    JXL_DASSERT(y < by + ref_pos.ysize);
    size_t iy = y - by;
    size_t ref = ref_pos.ref;
    if (bx >= x0 + xsize) continue;
    if (bx + patch_xsize < x0) continue;
    size_t patch_x0 = std::max(bx, x0);
    size_t patch_x1 = std::min(bx + patch_xsize, x0 + xsize);
    for (size_t c = 0; c < 3; c++) {
      fg_ptrs[c] = shared_->reference_frames[ref].frame.color().ConstPlaneRow(
                       c, ref_pos.y0 + iy) +
                   ref_pos.x0 + x0 - bx;
    }
    for (size_t i = 0; i < num_ec; i++) {
      fg_ptrs[3 + i] =
          shared_->reference_frames[ref].frame.extra_channels()[i].ConstRow(
              ref_pos.y0 + iy) +
          ref_pos.x0 + x0 - bx;
    }
    PerformBlending(inout, fg_ptrs.data(), inout, patch_x0 - x0,
                    patch_x1 - patch_x0, blendings_[blending_idx],
                    blendings_.data() + blending_idx + 1,
                    shared_->metadata->m.extra_channel_info);
  }
}
}  // namespace jxl