/src/libjxl/lib/jxl/compressed_dc.cc
Line | Count | Source |
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/compressed_dc.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <algorithm> |
11 | | #include <cstdint> |
12 | | #include <cstdlib> |
13 | | #include <cstring> |
14 | | #include <vector> |
15 | | |
16 | | #include "lib/jxl/ac_context.h" |
17 | | #include "lib/jxl/frame_header.h" |
18 | | #include "lib/jxl/modular/modular_image.h" |
19 | | |
20 | | #undef HWY_TARGET_INCLUDE |
21 | | #define HWY_TARGET_INCLUDE "lib/jxl/compressed_dc.cc" |
22 | | #include <hwy/foreach_target.h> |
23 | | #include <hwy/highway.h> |
24 | | |
25 | | #include "lib/jxl/base/compiler_specific.h" |
26 | | #include "lib/jxl/base/data_parallel.h" |
27 | | #include "lib/jxl/base/rect.h" |
28 | | #include "lib/jxl/base/status.h" |
29 | | #include "lib/jxl/image.h" |
30 | | HWY_BEFORE_NAMESPACE(); |
31 | | namespace jxl { |
32 | | namespace HWY_NAMESPACE { |
33 | | |
34 | | using D = HWY_FULL(float); |
35 | | using DScalar = HWY_CAPPED(float, 1); |
36 | | |
37 | | // These templates are not found via ADL. |
38 | | using hwy::HWY_NAMESPACE::Abs; |
39 | | using hwy::HWY_NAMESPACE::Add; |
40 | | using hwy::HWY_NAMESPACE::Div; |
41 | | using hwy::HWY_NAMESPACE::Max; |
42 | | using hwy::HWY_NAMESPACE::Mul; |
43 | | using hwy::HWY_NAMESPACE::MulAdd; |
44 | | using hwy::HWY_NAMESPACE::Rebind; |
45 | | using hwy::HWY_NAMESPACE::Sub; |
46 | | using hwy::HWY_NAMESPACE::Vec; |
47 | | using hwy::HWY_NAMESPACE::ZeroIfNegative; |
48 | | |
49 | | // TODO(veluca): optimize constants. |
50 | | const float w1 = 0.20345139757231578f; |
51 | | const float w2 = 0.0334829185968739f; |
52 | | const float w0 = 1.0f - 4.0f * (w1 + w2); |
53 | | |
54 | | template <class V> |
55 | 438k | V MaxWorkaround(V a, V b) { |
56 | | #if (HWY_TARGET == HWY_AVX3) && HWY_COMPILER_CLANG <= 800 |
57 | | // Prevents "Do not know how to split the result of this operator" error |
58 | | return IfThenElse(a > b, a, b); |
59 | | #else |
60 | 438k | return Max(a, b); |
61 | 438k | #endif |
62 | 438k | } hwy::N_SSE2::Vec128<float, 1ul> jxl::N_SSE2::MaxWorkaround<hwy::N_SSE2::Vec128<float, 1ul> >(hwy::N_SSE2::Vec128<float, 1ul>, hwy::N_SSE2::Vec128<float, 1ul>) Line | Count | Source | 55 | 174k | V MaxWorkaround(V a, V b) { | 56 | | #if (HWY_TARGET == HWY_AVX3) && HWY_COMPILER_CLANG <= 800 | 57 | | // Prevents "Do not know how to split the result of this operator" error | 58 | | return IfThenElse(a > b, a, b); | 59 | | #else | 60 | 174k | return Max(a, b); | 61 | 174k | #endif | 62 | 174k | } |
hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::MaxWorkaround<hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Vec128<float, 4ul>, hwy::N_SSE2::Vec128<float, 4ul>) Line | Count | Source | 55 | 264k | V MaxWorkaround(V a, V b) { | 56 | | #if (HWY_TARGET == HWY_AVX3) && HWY_COMPILER_CLANG <= 800 | 57 | | // Prevents "Do not know how to split the result of this operator" error | 58 | | return IfThenElse(a > b, a, b); | 59 | | #else | 60 | 264k | return Max(a, b); | 61 | 264k | #endif | 62 | 264k | } |
|
63 | | |
64 | | template <typename D> |
65 | | JXL_INLINE void ComputePixelChannel(const D d, const float dc_factor, |
66 | | const float* JXL_RESTRICT row_top, |
67 | | const float* JXL_RESTRICT row, |
68 | | const float* JXL_RESTRICT row_bottom, |
69 | | Vec<D>* JXL_RESTRICT mc, |
70 | | Vec<D>* JXL_RESTRICT sm, |
71 | 438k | Vec<D>* JXL_RESTRICT gap, size_t x) { |
72 | 438k | const auto tl = LoadU(d, row_top + x - 1); |
73 | 438k | const auto tc = Load(d, row_top + x); |
74 | 438k | const auto tr = LoadU(d, row_top + x + 1); |
75 | | |
76 | 438k | const auto ml = LoadU(d, row + x - 1); |
77 | 438k | *mc = Load(d, row + x); |
78 | 438k | const auto mr = LoadU(d, row + x + 1); |
79 | | |
80 | 438k | const auto bl = LoadU(d, row_bottom + x - 1); |
81 | 438k | const auto bc = Load(d, row_bottom + x); |
82 | 438k | const auto br = LoadU(d, row_bottom + x + 1); |
83 | | |
84 | 438k | const auto w_center = Set(d, w0); |
85 | 438k | const auto w_side = Set(d, w1); |
86 | 438k | const auto w_corner = Set(d, w2); |
87 | | |
88 | 438k | const auto corner = Add(Add(tl, tr), Add(bl, br)); |
89 | 438k | const auto side = Add(Add(ml, mr), Add(tc, bc)); |
90 | 438k | *sm = MulAdd(corner, w_corner, MulAdd(side, w_side, Mul(*mc, w_center))); |
91 | | |
92 | 438k | const auto dc_quant = Set(d, dc_factor); |
93 | 438k | *gap = MaxWorkaround(*gap, Abs(Div(Sub(*mc, *sm), dc_quant))); |
94 | 438k | } void jxl::N_SSE2::ComputePixelChannel<hwy::N_SSE2::Simd<float, 1ul, 0> >(hwy::N_SSE2::Simd<float, 1ul, 0>, float, float const*, float const*, float const*, decltype (Zero((hwy::N_SSE2::Simd<float, 1ul, 0>)()))*, decltype (Zero((hwy::N_SSE2::Simd<float, 1ul, 0>)()))*, decltype (Zero((hwy::N_SSE2::Simd<float, 1ul, 0>)()))*, unsigned long) Line | Count | Source | 71 | 146k | Vec<D>* JXL_RESTRICT gap, size_t x) { | 72 | 146k | const auto tl = LoadU(d, row_top + x - 1); | 73 | 146k | const auto tc = Load(d, row_top + x); | 74 | 146k | const auto tr = LoadU(d, row_top + x + 1); | 75 | | | 76 | 146k | const auto ml = LoadU(d, row + x - 1); | 77 | 146k | *mc = Load(d, row + x); | 78 | 146k | const auto mr = LoadU(d, row + x + 1); | 79 | | | 80 | 146k | const auto bl = LoadU(d, row_bottom + x - 1); | 81 | 146k | const auto bc = Load(d, row_bottom + x); | 82 | 146k | const auto br = LoadU(d, row_bottom + x + 1); | 83 | | | 84 | 146k | const auto w_center = Set(d, w0); | 85 | 146k | const auto w_side = Set(d, w1); | 86 | 146k | const auto w_corner = Set(d, w2); | 87 | | | 88 | 146k | const auto corner = Add(Add(tl, tr), Add(bl, br)); | 89 | 146k | const auto side = Add(Add(ml, mr), Add(tc, bc)); | 90 | 146k | *sm = MulAdd(corner, w_corner, MulAdd(side, w_side, Mul(*mc, w_center))); | 91 | | | 92 | 146k | const auto dc_quant = Set(d, dc_factor); | 93 | 146k | *gap = MaxWorkaround(*gap, Abs(Div(Sub(*mc, *sm), dc_quant))); | 94 | 146k | } |
void jxl::N_SSE2::ComputePixelChannel<hwy::N_SSE2::Simd<float, 4ul, 0> >(hwy::N_SSE2::Simd<float, 4ul, 0>, float, float const*, float const*, float const*, decltype (Zero((hwy::N_SSE2::Simd<float, 4ul, 0>)()))*, decltype (Zero((hwy::N_SSE2::Simd<float, 4ul, 0>)()))*, decltype (Zero((hwy::N_SSE2::Simd<float, 4ul, 0>)()))*, unsigned long) Line | Count | Source | 71 | 292k | Vec<D>* JXL_RESTRICT gap, size_t x) { | 72 | 292k | const auto tl = LoadU(d, row_top + x - 1); | 73 | 292k | const auto tc = Load(d, row_top + x); | 74 | 292k | const auto tr = LoadU(d, row_top + x + 1); | 75 | | | 76 | 292k | const auto ml = LoadU(d, row + x - 1); | 77 | 292k | *mc = Load(d, row + x); | 78 | 292k | const auto mr = LoadU(d, row + x + 1); | 79 | | | 80 | 292k | const auto bl = LoadU(d, row_bottom + x - 1); | 81 | 292k | const auto bc = Load(d, row_bottom + x); | 82 | 292k | const auto br = LoadU(d, row_bottom + x + 1); | 83 | | | 84 | 292k | const auto w_center = Set(d, w0); | 85 | 292k | const auto w_side = Set(d, w1); | 86 | 292k | const auto w_corner = Set(d, w2); | 87 | | | 88 | 292k | const auto corner = Add(Add(tl, tr), Add(bl, br)); | 89 | 292k | const auto side = Add(Add(ml, mr), Add(tc, bc)); | 90 | 292k | *sm = MulAdd(corner, w_corner, MulAdd(side, w_side, Mul(*mc, w_center))); | 91 | | | 92 | 292k | const auto dc_quant = Set(d, dc_factor); | 93 | 292k | *gap = MaxWorkaround(*gap, Abs(Div(Sub(*mc, *sm), dc_quant))); | 94 | 292k | } |
|
95 | | |
96 | | template <typename D> |
97 | | JXL_INLINE void ComputePixel( |
98 | | const float* JXL_RESTRICT dc_factors, |
99 | | const float* JXL_RESTRICT* JXL_RESTRICT rows_top, |
100 | | const float* JXL_RESTRICT* JXL_RESTRICT rows, |
101 | | const float* JXL_RESTRICT* JXL_RESTRICT rows_bottom, |
102 | 183k | float* JXL_RESTRICT* JXL_RESTRICT out_rows, size_t x) { |
103 | 183k | const D d; |
104 | 183k | auto mc_x = Undefined(d); |
105 | 183k | auto mc_y = Undefined(d); |
106 | 183k | auto mc_b = Undefined(d); |
107 | 183k | auto sm_x = Undefined(d); |
108 | 183k | auto sm_y = Undefined(d); |
109 | 183k | auto sm_b = Undefined(d); |
110 | 183k | auto gap = Set(d, 0.5f); |
111 | 183k | ComputePixelChannel(d, dc_factors[0], rows_top[0], rows[0], rows_bottom[0], |
112 | 183k | &mc_x, &sm_x, &gap, x); |
113 | 183k | ComputePixelChannel(d, dc_factors[1], rows_top[1], rows[1], rows_bottom[1], |
114 | 183k | &mc_y, &sm_y, &gap, x); |
115 | 183k | ComputePixelChannel(d, dc_factors[2], rows_top[2], rows[2], rows_bottom[2], |
116 | 183k | &mc_b, &sm_b, &gap, x); |
117 | 183k | auto factor = MulAdd(Set(d, -4.0f), gap, Set(d, 3.0f)); |
118 | 183k | factor = ZeroIfNegative(factor); |
119 | | |
120 | 183k | auto out = MulAdd(Sub(sm_x, mc_x), factor, mc_x); |
121 | 183k | Store(out, d, out_rows[0] + x); |
122 | 183k | out = MulAdd(Sub(sm_y, mc_y), factor, mc_y); |
123 | 183k | Store(out, d, out_rows[1] + x); |
124 | 183k | out = MulAdd(Sub(sm_b, mc_b), factor, mc_b); |
125 | 183k | Store(out, d, out_rows[2] + x); |
126 | 183k | } void jxl::N_SSE2::ComputePixel<hwy::N_SSE2::Simd<float, 1ul, 0> >(float const*, float const* restrict*, float const* restrict*, float const* restrict*, float* restrict*, unsigned long) Line | Count | Source | 102 | 84.2k | float* JXL_RESTRICT* JXL_RESTRICT out_rows, size_t x) { | 103 | 84.2k | const D d; | 104 | 84.2k | auto mc_x = Undefined(d); | 105 | 84.2k | auto mc_y = Undefined(d); | 106 | 84.2k | auto mc_b = Undefined(d); | 107 | 84.2k | auto sm_x = Undefined(d); | 108 | 84.2k | auto sm_y = Undefined(d); | 109 | 84.2k | auto sm_b = Undefined(d); | 110 | 84.2k | auto gap = Set(d, 0.5f); | 111 | 84.2k | ComputePixelChannel(d, dc_factors[0], rows_top[0], rows[0], rows_bottom[0], | 112 | 84.2k | &mc_x, &sm_x, &gap, x); | 113 | 84.2k | ComputePixelChannel(d, dc_factors[1], rows_top[1], rows[1], rows_bottom[1], | 114 | 84.2k | &mc_y, &sm_y, &gap, x); | 115 | 84.2k | ComputePixelChannel(d, dc_factors[2], rows_top[2], rows[2], rows_bottom[2], | 116 | 84.2k | &mc_b, &sm_b, &gap, x); | 117 | 84.2k | auto factor = MulAdd(Set(d, -4.0f), gap, Set(d, 3.0f)); | 118 | 84.2k | factor = ZeroIfNegative(factor); | 119 | | | 120 | 84.2k | auto out = MulAdd(Sub(sm_x, mc_x), factor, mc_x); | 121 | 84.2k | Store(out, d, out_rows[0] + x); | 122 | 84.2k | out = MulAdd(Sub(sm_y, mc_y), factor, mc_y); | 123 | 84.2k | Store(out, d, out_rows[1] + x); | 124 | 84.2k | out = MulAdd(Sub(sm_b, mc_b), factor, mc_b); | 125 | 84.2k | Store(out, d, out_rows[2] + x); | 126 | 84.2k | } |
void jxl::N_SSE2::ComputePixel<hwy::N_SSE2::Simd<float, 4ul, 0> >(float const*, float const* restrict*, float const* restrict*, float const* restrict*, float* restrict*, unsigned long) Line | Count | Source | 102 | 99.5k | float* JXL_RESTRICT* JXL_RESTRICT out_rows, size_t x) { | 103 | 99.5k | const D d; | 104 | 99.5k | auto mc_x = Undefined(d); | 105 | 99.5k | auto mc_y = Undefined(d); | 106 | 99.5k | auto mc_b = Undefined(d); | 107 | 99.5k | auto sm_x = Undefined(d); | 108 | 99.5k | auto sm_y = Undefined(d); | 109 | 99.5k | auto sm_b = Undefined(d); | 110 | 99.5k | auto gap = Set(d, 0.5f); | 111 | 99.5k | ComputePixelChannel(d, dc_factors[0], rows_top[0], rows[0], rows_bottom[0], | 112 | 99.5k | &mc_x, &sm_x, &gap, x); | 113 | 99.5k | ComputePixelChannel(d, dc_factors[1], rows_top[1], rows[1], rows_bottom[1], | 114 | 99.5k | &mc_y, &sm_y, &gap, x); | 115 | 99.5k | ComputePixelChannel(d, dc_factors[2], rows_top[2], rows[2], rows_bottom[2], | 116 | 99.5k | &mc_b, &sm_b, &gap, x); | 117 | 99.5k | auto factor = MulAdd(Set(d, -4.0f), gap, Set(d, 3.0f)); | 118 | 99.5k | factor = ZeroIfNegative(factor); | 119 | | | 120 | 99.5k | auto out = MulAdd(Sub(sm_x, mc_x), factor, mc_x); | 121 | 99.5k | Store(out, d, out_rows[0] + x); | 122 | 99.5k | out = MulAdd(Sub(sm_y, mc_y), factor, mc_y); | 123 | 99.5k | Store(out, d, out_rows[1] + x); | 124 | 99.5k | out = MulAdd(Sub(sm_b, mc_b), factor, mc_b); | 125 | 99.5k | Store(out, d, out_rows[2] + x); | 126 | 99.5k | } |
|
127 | | |
128 | | template <typename D> |
129 | | JXL_INLINE void ComputePixelSingleChannel(const float dc_factor, |
130 | | const float* JXL_RESTRICT row_top, |
131 | | const float* JXL_RESTRICT row, |
132 | | const float* JXL_RESTRICT row_bottom, |
133 | | float* JXL_RESTRICT out_row, |
134 | 0 | size_t x) { |
135 | 0 | const D d; |
136 | 0 | auto mc = Undefined(d); |
137 | 0 | auto sm = Undefined(d); |
138 | 0 | auto gap = Set(d, 0.5f); |
139 | 0 | ComputePixelChannel(d, dc_factor, row_top, row, row_bottom, &mc, &sm, &gap, |
140 | 0 | x); |
141 | 0 | auto factor = MulAdd(Set(d, -4.0f), gap, Set(d, 3.0f)); |
142 | 0 | factor = ZeroIfNegative(factor); |
143 | |
|
144 | 0 | auto out = MulAdd(Sub(sm, mc), factor, mc); |
145 | 0 | Store(out, d, out_row + x); |
146 | 0 | } Unexecuted instantiation: void jxl::N_SSE2::ComputePixelSingleChannel<hwy::N_SSE2::Simd<float, 1ul, 0> >(float, float const*, float const*, float const*, float*, unsigned long) Unexecuted instantiation: void jxl::N_SSE2::ComputePixelSingleChannel<hwy::N_SSE2::Simd<float, 4ul, 0> >(float, float const*, float const*, float const*, float*, unsigned long) |
147 | | |
148 | | Status AdaptiveDCSmoothing(JxlMemoryManager* memory_manager, |
149 | | const float* dc_factors, Image3F* dc, |
150 | | const YCbCrChromaSubsampling& chroma_subsampling, |
151 | 5.77k | ThreadPool* pool) { |
152 | 5.77k | const size_t xsize = dc->xsize(); |
153 | 5.77k | const size_t ysize = dc->ysize(); |
154 | 5.77k | if (ysize <= 2 || xsize <= 2) return true; |
155 | | |
156 | | // TODO(veluca): use tile-based processing? |
157 | | // TODO(veluca): decide if changes to the y channel should be propagated to |
158 | | // the x and b channels through color correlation. |
159 | 1.44k | JXL_ENSURE(w1 + w2 < 0.25f); |
160 | | |
161 | 1.44k | if (chroma_subsampling.Is444()) { |
162 | 1.44k | JXL_ASSIGN_OR_RETURN(Image3F smoothed, |
163 | 1.44k | Image3F::Create(memory_manager, xsize, ysize)); |
164 | | // Fill in borders that the loop below will not. First and last are unused. |
165 | 5.76k | for (size_t c = 0; c < 3; c++) { |
166 | 8.65k | for (size_t y : {static_cast<size_t>(0), ysize - 1}) { |
167 | 8.65k | memcpy(smoothed.PlaneRow(c, y), dc->PlaneRow(c, y), |
168 | 8.65k | xsize * sizeof(float)); |
169 | 8.65k | } |
170 | 4.32k | } |
171 | 39.4k | auto process_row = [&](const uint32_t y, size_t /*thread*/) -> Status { |
172 | 39.4k | const float* JXL_RESTRICT rows_top[3]{ |
173 | 39.4k | dc->ConstPlaneRow(0, y - 1), |
174 | 39.4k | dc->ConstPlaneRow(1, y - 1), |
175 | 39.4k | dc->ConstPlaneRow(2, y - 1), |
176 | 39.4k | }; |
177 | 39.4k | const float* JXL_RESTRICT rows[3] = { |
178 | 39.4k | dc->ConstPlaneRow(0, y), |
179 | 39.4k | dc->ConstPlaneRow(1, y), |
180 | 39.4k | dc->ConstPlaneRow(2, y), |
181 | 39.4k | }; |
182 | 39.4k | const float* JXL_RESTRICT rows_bottom[3] = { |
183 | 39.4k | dc->ConstPlaneRow(0, y + 1), |
184 | 39.4k | dc->ConstPlaneRow(1, y + 1), |
185 | 39.4k | dc->ConstPlaneRow(2, y + 1), |
186 | 39.4k | }; |
187 | 39.4k | float* JXL_RESTRICT rows_out[3] = { |
188 | 39.4k | smoothed.PlaneRow(0, y), |
189 | 39.4k | smoothed.PlaneRow(1, y), |
190 | 39.4k | smoothed.PlaneRow(2, y), |
191 | 39.4k | }; |
192 | 76.6k | for (size_t x : {static_cast<size_t>(0), xsize - 1}) { |
193 | 305k | for (size_t c = 0; c < 3; c++) { |
194 | 228k | rows_out[c][x] = rows[c][x]; |
195 | 228k | } |
196 | 76.6k | } |
197 | | |
198 | 39.4k | size_t x = 1; |
199 | | // First pixels |
200 | 39.4k | const size_t N = Lanes(D()); |
201 | 109k | for (; x < std::min(N, xsize - 1); x++) { |
202 | 69.6k | ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out, |
203 | 69.6k | x); |
204 | 69.6k | } |
205 | | // Full vectors. |
206 | 165k | for (; x + N <= xsize - 1; x += N) { |
207 | 126k | ComputePixel<D>(dc_factors, rows_top, rows, rows_bottom, rows_out, x); |
208 | 126k | } |
209 | | // Last pixels. |
210 | 69.9k | for (; x < xsize - 1; x++) { |
211 | 30.4k | ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out, |
212 | 30.4k | x); |
213 | 30.4k | } |
214 | 39.4k | return true; |
215 | 39.4k | }; |
216 | 1.44k | JXL_RETURN_IF_ERROR(RunOnPool(pool, 1, ysize - 1, ThreadPool::NoInit, |
217 | 1.44k | process_row, "DCSmoothingRow")); |
218 | 1.44k | dc->Swap(smoothed); |
219 | 1.44k | } else { |
220 | 0 | JXL_ASSIGN_OR_RETURN(ImageF smoothed, |
221 | 0 | ImageF::Create(memory_manager, xsize, ysize)); |
222 | 0 | for (size_t c = 0; c < 3; c++) { |
223 | 0 | const size_t xsize_c = xsize >> chroma_subsampling.HShift(c); |
224 | 0 | const size_t ysize_c = ysize >> chroma_subsampling.VShift(c); |
225 | 0 | if (ysize_c <= 2 || xsize_c <= 2) continue; |
226 | 0 | for (size_t y : {static_cast<size_t>(0), ysize_c - 1}) { |
227 | 0 | memcpy(smoothed.Row(y), dc->PlaneRow(c, y), xsize_c * sizeof(float)); |
228 | 0 | } |
229 | 0 | auto process_row = [&](const uint32_t y, size_t /*thread*/) -> Status { |
230 | 0 | const float* JXL_RESTRICT row_top = dc->ConstPlaneRow(c, y - 1); |
231 | 0 | const float* JXL_RESTRICT row = dc->ConstPlaneRow(c, y); |
232 | 0 | const float* JXL_RESTRICT row_bottom = dc->ConstPlaneRow(c, y + 1); |
233 | 0 | float* JXL_RESTRICT row_out = smoothed.Row(y); |
234 | 0 | for (size_t x : {static_cast<size_t>(0), xsize_c - 1}) { |
235 | 0 | row_out[x] = row[x]; |
236 | 0 | } |
237 | |
|
238 | 0 | size_t x = 1; |
239 | | // First pixels |
240 | 0 | const size_t N = Lanes(D()); |
241 | 0 | for (; x < std::min(N, xsize_c - 1); x++) { |
242 | 0 | ComputePixelSingleChannel<DScalar>(dc_factors[c], row_top, row, |
243 | 0 | row_bottom, row_out, x); |
244 | 0 | } |
245 | | // Full vectors. |
246 | 0 | for (; x + N <= xsize_c - 1; x += N) { |
247 | 0 | ComputePixelSingleChannel<D>(dc_factors[c], row_top, row, row_bottom, |
248 | 0 | row_out, x); |
249 | 0 | } |
250 | | // Last pixels. |
251 | 0 | for (; x < xsize_c - 1; x++) { |
252 | 0 | ComputePixelSingleChannel<DScalar>(dc_factors[c], row_top, row, |
253 | 0 | row_bottom, row_out, x); |
254 | 0 | } |
255 | 0 | return true; |
256 | 0 | }; |
257 | 0 | JXL_RETURN_IF_ERROR(RunOnPool(pool, 1, ysize_c - 1, ThreadPool::NoInit, |
258 | 0 | process_row, "DCSmoothingRow")); |
259 | 0 | dc->Plane(c).Swap(smoothed); |
260 | 0 | } |
261 | 0 | } |
262 | 1.44k | return true; |
263 | 1.44k | } |
264 | | |
265 | | // DC dequantization. |
266 | | void DequantDC(const Rect& r, Image3F* dc, ImageB* quant_dc, const Image& in, |
267 | | const float* dc_factors, float mul, const float* cfl_factors, |
268 | | const YCbCrChromaSubsampling& chroma_subsampling, |
269 | 6.39k | const BlockCtxMap& bctx) { |
270 | 6.39k | const HWY_FULL(float) df; |
271 | 6.39k | const Rebind<pixel_type, HWY_FULL(float)> di; // assumes pixel_type <= float |
272 | 6.39k | if (chroma_subsampling.Is444()) { |
273 | 6.10k | const auto fac_x = Set(df, dc_factors[0] * mul); |
274 | 6.10k | const auto fac_y = Set(df, dc_factors[1] * mul); |
275 | 6.10k | const auto fac_b = Set(df, dc_factors[2] * mul); |
276 | 6.10k | const auto cfl_fac_x = Set(df, cfl_factors[0]); |
277 | 6.10k | const auto cfl_fac_b = Set(df, cfl_factors[2]); |
278 | 79.2k | for (size_t y = 0; y < r.ysize(); y++) { |
279 | 73.1k | float* dec_row_x = r.PlaneRow(dc, 0, y); |
280 | 73.1k | float* dec_row_y = r.PlaneRow(dc, 1, y); |
281 | 73.1k | float* dec_row_b = r.PlaneRow(dc, 2, y); |
282 | 73.1k | const int32_t* quant_row_x = in.channel[1].plane.Row(y); |
283 | 73.1k | const int32_t* quant_row_y = in.channel[0].plane.Row(y); |
284 | 73.1k | const int32_t* quant_row_b = in.channel[2].plane.Row(y); |
285 | 491k | for (size_t x = 0; x < r.xsize(); x += Lanes(di)) { |
286 | 418k | const auto in_q_x = Load(di, quant_row_x + x); |
287 | 418k | const auto in_q_y = Load(di, quant_row_y + x); |
288 | 418k | const auto in_q_b = Load(di, quant_row_b + x); |
289 | 418k | const auto in_x = Mul(ConvertTo(df, in_q_x), fac_x); |
290 | 418k | const auto in_y = Mul(ConvertTo(df, in_q_y), fac_y); |
291 | 418k | const auto in_b = Mul(ConvertTo(df, in_q_b), fac_b); |
292 | 418k | Store(in_y, df, dec_row_y + x); |
293 | 418k | Store(MulAdd(in_y, cfl_fac_x, in_x), df, dec_row_x + x); |
294 | 418k | Store(MulAdd(in_y, cfl_fac_b, in_b), df, dec_row_b + x); |
295 | 418k | } |
296 | 73.1k | } |
297 | 6.10k | } else { |
298 | 879 | for (size_t c : {1, 0, 2}) { |
299 | 879 | Rect rect(r.x0() >> chroma_subsampling.HShift(c), |
300 | 879 | r.y0() >> chroma_subsampling.VShift(c), |
301 | 879 | r.xsize() >> chroma_subsampling.HShift(c), |
302 | 879 | r.ysize() >> chroma_subsampling.VShift(c)); |
303 | 879 | const auto fac = Set(df, dc_factors[c] * mul); |
304 | 879 | const Channel& ch = in.channel[c < 2 ? c ^ 1 : c]; |
305 | 11.1k | for (size_t y = 0; y < rect.ysize(); y++) { |
306 | 10.3k | const int32_t* quant_row = ch.plane.Row(y); |
307 | 10.3k | float* row = rect.PlaneRow(dc, c, y); |
308 | 75.3k | for (size_t x = 0; x < rect.xsize(); x += Lanes(di)) { |
309 | 65.0k | const auto in_q = Load(di, quant_row + x); |
310 | 65.0k | const auto out = Mul(ConvertTo(df, in_q), fac); |
311 | 65.0k | Store(out, df, row + x); |
312 | 65.0k | } |
313 | 10.3k | } |
314 | 879 | } |
315 | 293 | } |
316 | 6.39k | if (bctx.num_dc_ctxs <= 1) { |
317 | 80.3k | for (size_t y = 0; y < r.ysize(); y++) { |
318 | 74.7k | uint8_t* qdc_row = r.Row(quant_dc, y); |
319 | 74.7k | memset(qdc_row, 0, sizeof(*qdc_row) * r.xsize()); |
320 | 74.7k | } |
321 | 5.60k | } else { |
322 | 790 | JXL_DASSERT(r.ysize() == 0 || |
323 | 790 | (r.ysize() - 1) >> chroma_subsampling.VShift(0) < |
324 | 790 | in.channel[1].plane.ysize()); |
325 | 790 | JXL_DASSERT(r.ysize() == 0 || |
326 | 790 | (r.ysize() - 1) >> chroma_subsampling.VShift(1) < |
327 | 790 | in.channel[0].plane.ysize()); |
328 | 790 | JXL_DASSERT(r.ysize() == 0 || |
329 | 790 | (r.ysize() - 1) >> chroma_subsampling.VShift(2) < |
330 | 790 | in.channel[2].plane.ysize()); |
331 | 3.52k | for (size_t y = 0; y < r.ysize(); y++) { |
332 | 2.73k | uint8_t* qdc_row_val = r.Row(quant_dc, y); |
333 | 2.73k | const int32_t* quant_row_x = |
334 | 2.73k | in.channel[1].plane.Row(y >> chroma_subsampling.VShift(0)); |
335 | 2.73k | const int32_t* quant_row_y = |
336 | 2.73k | in.channel[0].plane.Row(y >> chroma_subsampling.VShift(1)); |
337 | 2.73k | const int32_t* quant_row_b = |
338 | 2.73k | in.channel[2].plane.Row(y >> chroma_subsampling.VShift(2)); |
339 | 15.9k | for (size_t x = 0; x < r.xsize(); x++) { |
340 | 13.1k | int bucket_x = 0; |
341 | 13.1k | int bucket_y = 0; |
342 | 13.1k | int bucket_b = 0; |
343 | 13.1k | for (int t : bctx.dc_thresholds[0]) { |
344 | 13.0k | if (quant_row_x[x >> chroma_subsampling.HShift(0)] > t) bucket_x++; |
345 | 13.0k | } |
346 | 13.2k | for (int t : bctx.dc_thresholds[1]) { |
347 | 13.2k | if (quant_row_y[x >> chroma_subsampling.HShift(1)] > t) bucket_y++; |
348 | 13.2k | } |
349 | 13.1k | for (int t : bctx.dc_thresholds[2]) { |
350 | 7.95k | if (quant_row_b[x >> chroma_subsampling.HShift(2)] > t) bucket_b++; |
351 | 7.95k | } |
352 | 13.1k | int bucket = bucket_x; |
353 | 13.1k | bucket *= bctx.dc_thresholds[2].size() + 1; |
354 | 13.1k | bucket += bucket_b; |
355 | 13.1k | bucket *= bctx.dc_thresholds[1].size() + 1; |
356 | 13.1k | bucket += bucket_y; |
357 | 13.1k | qdc_row_val[x] = bucket; |
358 | 13.1k | } |
359 | 2.73k | } |
360 | 790 | } |
361 | 6.39k | } |
362 | | |
363 | | // NOLINTNEXTLINE(google-readability-namespace-comments) |
364 | | } // namespace HWY_NAMESPACE |
365 | | } // namespace jxl |
366 | | HWY_AFTER_NAMESPACE(); |
367 | | |
368 | | #if HWY_ONCE |
369 | | namespace jxl { |
370 | | |
371 | | HWY_EXPORT(DequantDC); |
372 | | HWY_EXPORT(AdaptiveDCSmoothing); |
373 | | Status AdaptiveDCSmoothing(JxlMemoryManager* memory_manager, |
374 | | const float* dc_factors, Image3F* dc, |
375 | | const YCbCrChromaSubsampling& chroma_subsampling, |
376 | 5.77k | ThreadPool* pool) { |
377 | 5.77k | return HWY_DYNAMIC_DISPATCH(AdaptiveDCSmoothing)( |
378 | 5.77k | memory_manager, dc_factors, dc, chroma_subsampling, pool); |
379 | 5.77k | } |
380 | | |
381 | | void DequantDC(const Rect& r, Image3F* dc, ImageB* quant_dc, const Image& in, |
382 | | const float* dc_factors, float mul, const float* cfl_factors, |
383 | | const YCbCrChromaSubsampling& chroma_subsampling, |
384 | 6.39k | const BlockCtxMap& bctx) { |
385 | 6.39k | HWY_DYNAMIC_DISPATCH(DequantDC) |
386 | 6.39k | (r, dc, quant_dc, in, dc_factors, mul, cfl_factors, chroma_subsampling, bctx); |
387 | 6.39k | } |
388 | | |
389 | | } // namespace jxl |
390 | | #endif // HWY_ONCE |