Debian Codegraph @

lib/jxl/render_pipeline/stage_xyb.cc

raw
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/render_pipeline/stage_xyb.h"
7
8 #include "lib/jxl/base/common.h"
9 #include "lib/jxl/base/sanitizers.h"
10
11 #undef HWY_TARGET_INCLUDE
12 #define HWY_TARGET_INCLUDE "lib/jxl/render_pipeline/stage_xyb.cc"
13 #include <hwy/foreach_target.h>
14 #include <hwy/highway.h>
15
16 #include "lib/jxl/cms/opsin_params.h"
17 #include "lib/jxl/common.h" // JXL_HIGH_PRECISION
18 #include "lib/jxl/dec_xyb-inl.h"
19
20 HWY_BEFORE_NAMESPACE();
21 namespace jxl {
22 namespace HWY_NAMESPACE {
23
24 class XYBStage : public RenderPipelineStage {
25 public:
26 explicit XYBStage(const OutputEncodingInfo& output_encoding_info)
27 : RenderPipelineStage(RenderPipelineStage::Settings()),
28 opsin_params_(output_encoding_info.opsin_params),
29 output_is_xyb_(output_encoding_info.color_encoding.GetColorSpace() ==
30 ColorSpace::kXYB) {}
31
32 Status ProcessRow(const RowInfo& input_rows, const RowInfo& output_rows,
33 size_t xextra, size_t xsize, size_t xpos, size_t ypos,
34 size_t thread_id) const final {
35 const HWY_FULL(float) d;
36 JXL_ENSURE(xextra == 0);
37 const size_t xsize_v = RoundUpTo(xsize, Lanes(d));
38 float* JXL_RESTRICT row0 = GetInputRow(input_rows, 0, 0);
39 float* JXL_RESTRICT row1 = GetInputRow(input_rows, 1, 0);
40 float* JXL_RESTRICT row2 = GetInputRow(input_rows, 2, 0);
41 // All calculations are lane-wise, still some might require
42 // value-dependent behaviour (e.g. NearestInt). Temporary unpoison last
43 // vector tail.
44 msan::UnpoisonMemory(row0 + xsize, sizeof(float) * (xsize_v - xsize));
45 msan::UnpoisonMemory(row1 + xsize, sizeof(float) * (xsize_v - xsize));
46 msan::UnpoisonMemory(row2 + xsize, sizeof(float) * (xsize_v - xsize));
47 // TODO(eustas): when using frame origin, addresses might be unaligned;
48 // making them aligned will void performance penalty.
49 if (output_is_xyb_) {
50 const auto scale_x = Set(d, jxl::cms::kScaledXYBScale[0]);
51 const auto scale_y = Set(d, jxl::cms::kScaledXYBScale[1]);
52 const auto scale_bmy = Set(d, jxl::cms::kScaledXYBScale[2]);
53 const auto offset_x = Set(d, jxl::cms::kScaledXYBOffset[0]);
54 const auto offset_y = Set(d, jxl::cms::kScaledXYBOffset[1]);
55 const auto offset_bmy = Set(d, jxl::cms::kScaledXYBOffset[2]);
56 for (ssize_t x = -xextra; x < static_cast<ssize_t>(xsize + xextra);
57 x += Lanes(d)) {
58 const auto in_x = LoadU(d, row0 + x);
59 const auto in_y = LoadU(d, row1 + x);
60 const auto in_b = LoadU(d, row2 + x);
61 auto out_x = Mul(Add(in_x, offset_x), scale_x);
62 auto out_y = Mul(Add(in_y, offset_y), scale_y);
63 auto out_b = Mul(Add(Sub(in_b, in_y), offset_bmy), scale_bmy);
64 StoreU(out_x, d, row0 + x);
65 StoreU(out_y, d, row1 + x);
66 StoreU(out_b, d, row2 + x);
67 }
68 } else {
69 for (ssize_t x = -xextra; x < static_cast<ssize_t>(xsize + xextra);
70 x += Lanes(d)) {
71 const auto in_opsin_x = LoadU(d, row0 + x);
72 const auto in_opsin_y = LoadU(d, row1 + x);
73 const auto in_opsin_b = LoadU(d, row2 + x);
74 auto r = Undefined(d);
75 auto g = Undefined(d);
76 auto b = Undefined(d);
77 XybToRgb(d, in_opsin_x, in_opsin_y, in_opsin_b, opsin_params_, &r, &g,
78 &b);
79 StoreU(r, d, row0 + x);
80 StoreU(g, d, row1 + x);
81 StoreU(b, d, row2 + x);
82 }
83 }
84 msan::PoisonMemory(row0 + xsize, sizeof(float) * (xsize_v - xsize));
85 msan::PoisonMemory(row1 + xsize, sizeof(float) * (xsize_v - xsize));
86 msan::PoisonMemory(row2 + xsize, sizeof(float) * (xsize_v - xsize));
87 return true;
88 }
89
90 RenderPipelineChannelMode GetChannelMode(size_t c) const final {
91 return c < 3 ? RenderPipelineChannelMode::kInPlace
92 : RenderPipelineChannelMode::kIgnored;
93 }
94
95 const char* GetName() const override { return "XYB"; }
96
97 private:
98 const OpsinParams opsin_params_;
99 const bool output_is_xyb_;
100 };
101
102 std::unique_ptr<RenderPipelineStage> GetXYBStage(
103 const OutputEncodingInfo& output_encoding_info) {
104 return jxl::make_unique<XYBStage>(output_encoding_info);
105 }
106
107 // NOLINTNEXTLINE(google-readability-namespace-comments)
108 } // namespace HWY_NAMESPACE
109 } // namespace jxl
110 HWY_AFTER_NAMESPACE();
111
112 #if HWY_ONCE
113 namespace jxl {
114
115 HWY_EXPORT(GetXYBStage);
116
117 std::unique_ptr<RenderPipelineStage> GetXYBStage(
118 const OutputEncodingInfo& output_encoding_info) {
119 return HWY_DYNAMIC_DISPATCH(GetXYBStage)(output_encoding_info);
120 }
121
122 #if !JXL_HIGH_PRECISION
123 namespace {
124 class FastXYBStage : public RenderPipelineStage {
125 public:
126 FastXYBStage(uint8_t* rgb, size_t stride, size_t width, size_t height,
127 bool rgba, bool has_alpha, size_t alpha_c)
128 : RenderPipelineStage(RenderPipelineStage::Settings()),
129 rgb_(rgb),
130 stride_(stride),
131 width_(width),
132 height_(height),
133 rgba_(rgba),
134 has_alpha_(has_alpha),
135 alpha_c_(alpha_c) {}
136
137 Status ProcessRow(const RowInfo& input_rows, const RowInfo& output_rows,
138 size_t xextra, size_t xsize, size_t xpos, size_t ypos,
139 size_t thread_id) const final {
140 if (ypos >= height_) return true;
141 JXL_ENSURE(xextra == 0);
142 const float* xyba[4] = {
143 GetInputRow(input_rows, 0, 0), GetInputRow(input_rows, 1, 0),
144 GetInputRow(input_rows, 2, 0),
145 has_alpha_ ? GetInputRow(input_rows, alpha_c_, 0) : nullptr};
146 uint8_t* out_buf = rgb_ + stride_ * ypos + (rgba_ ? 4 : 3) * xpos;
147 return FastXYBTosRGB8(xyba, out_buf, rgba_,
148 xsize + xpos <= width_ ? xsize : width_ - xpos);
149 }
150
151 RenderPipelineChannelMode GetChannelMode(size_t c) const final {
152 return c < 3 || (has_alpha_ && c == alpha_c_)
153 ? RenderPipelineChannelMode::kInput
154 : RenderPipelineChannelMode::kIgnored;
155 }
156
157 const char* GetName() const override { return "FastXYB"; }
158
159 private:
160 uint8_t* rgb_;
161 size_t stride_;
162 size_t width_;
163 size_t height_;
164 bool rgba_;
165 bool has_alpha_;
166 size_t alpha_c_;
167 std::vector<float> opaque_alpha_;
168 };
169
170 } // namespace
171
172 std::unique_ptr<RenderPipelineStage> GetFastXYBTosRGB8Stage(
173 uint8_t* rgb, size_t stride, size_t width, size_t height, bool rgba,
174 bool has_alpha, size_t alpha_c) {
175 if (!HasFastXYBTosRGB8()) return nullptr;
176 return make_unique<FastXYBStage>(rgb, stride, width, height, rgba, has_alpha,
177 alpha_c);
178 }
179 #endif
180
181 } // namespace jxl
182 #endif