Debian Codegraph @

lib/jxl/render_pipeline/stage_ycbcr.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_ycbcr.h"
7
8 #undef HWY_TARGET_INCLUDE
9 #define HWY_TARGET_INCLUDE "lib/jxl/render_pipeline/stage_ycbcr.cc"
10 #include <hwy/foreach_target.h>
11 #include <hwy/highway.h>
12
13 HWY_BEFORE_NAMESPACE();
14 namespace jxl {
15 namespace HWY_NAMESPACE {
16
17 // These templates are not found via ADL.
18 using hwy::HWY_NAMESPACE::Add;
19 using hwy::HWY_NAMESPACE::MulAdd;
20
21 class kYCbCrStage : public RenderPipelineStage {
22 public:
23 kYCbCrStage() : RenderPipelineStage(RenderPipelineStage::Settings()) {}
24
25 Status ProcessRow(const RowInfo& input_rows, const RowInfo& output_rows,
26 size_t xextra, size_t xsize, size_t xpos, size_t ypos,
27 size_t thread_id) const final {
28 const HWY_FULL(float) df;
29
30 // Full-range BT.601 as defined by JFIF Clause 7:
31 // https://www.itu.int/rec/T-REC-T.871-201105-I/en
32 const auto c128 = Set(df, 128.0f / 255);
33 const auto crcr = Set(df, 1.402f);
34 const auto cgcb = Set(df, -0.114f * 1.772f / 0.587f);
35 const auto cgcr = Set(df, -0.299f * 1.402f / 0.587f);
36 const auto cbcb = Set(df, 1.772f);
37
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 // TODO(eustas): when using frame origin, addresses might be unaligned;
42 // making them aligned will void performance penalty.
43 for (size_t x = 0; x < xsize; x += Lanes(df)) {
44 const auto y_vec = Add(LoadU(df, row1 + x), c128);
45 const auto cb_vec = LoadU(df, row0 + x);
46 const auto cr_vec = LoadU(df, row2 + x);
47 const auto r_vec = MulAdd(crcr, cr_vec, y_vec);
48 const auto g_vec = MulAdd(cgcr, cr_vec, MulAdd(cgcb, cb_vec, y_vec));
49 const auto b_vec = MulAdd(cbcb, cb_vec, y_vec);
50 StoreU(r_vec, df, row0 + x);
51 StoreU(g_vec, df, row1 + x);
52 StoreU(b_vec, df, row2 + x);
53 }
54 return true;
55 }
56
57 RenderPipelineChannelMode GetChannelMode(size_t c) const final {
58 return c < 3 ? RenderPipelineChannelMode::kInPlace
59 : RenderPipelineChannelMode::kIgnored;
60 }
61
62 const char* GetName() const override { return "YCbCr"; }
63 };
64
65 std::unique_ptr<RenderPipelineStage> GetYCbCrStage() {
66 return jxl::make_unique<kYCbCrStage>();
67 }
68
69 // NOLINTNEXTLINE(google-readability-namespace-comments)
70 } // namespace HWY_NAMESPACE
71 } // namespace jxl
72 HWY_AFTER_NAMESPACE();
73
74 #if HWY_ONCE
75 namespace jxl {
76
77 HWY_EXPORT(GetYCbCrStage);
78
79 std::unique_ptr<RenderPipelineStage> GetYCbCrStage() {
80 return HWY_DYNAMIC_DISPATCH(GetYCbCrStage)();
81 }
82
83 } // namespace jxl
84 #endif