Coverage Report

Created: 2025-12-13 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
3.49M
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
3.49M
  return Max(a, b);
61
3.49M
#endif
62
3.49M
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 1ul> jxl::N_SSE4::MaxWorkaround<hwy::N_SSE4::Vec128<float, 1ul> >(hwy::N_SSE4::Vec128<float, 1ul>, hwy::N_SSE4::Vec128<float, 1ul>)
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::MaxWorkaround<hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Vec128<float, 4ul>, hwy::N_SSE4::Vec128<float, 4ul>)
hwy::N_AVX2::Vec128<float, 1ul> jxl::N_AVX2::MaxWorkaround<hwy::N_AVX2::Vec128<float, 1ul> >(hwy::N_AVX2::Vec128<float, 1ul>, hwy::N_AVX2::Vec128<float, 1ul>)
Line
Count
Source
55
2.43M
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
2.43M
  return Max(a, b);
61
2.43M
#endif
62
2.43M
}
hwy::N_AVX2::Vec256<float> jxl::N_AVX2::MaxWorkaround<hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Vec256<float>, hwy::N_AVX2::Vec256<float>)
Line
Count
Source
55
1.06M
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
1.06M
  return Max(a, b);
61
1.06M
#endif
62
1.06M
}
Unexecuted instantiation: 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>)
Unexecuted instantiation: 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>)
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
3.49M
                                    Vec<D>* JXL_RESTRICT gap, size_t x) {
72
3.49M
  const auto tl = LoadU(d, row_top + x - 1);
73
3.49M
  const auto tc = Load(d, row_top + x);
74
3.49M
  const auto tr = LoadU(d, row_top + x + 1);
75
76
3.49M
  const auto ml = LoadU(d, row + x - 1);
77
3.49M
  *mc = Load(d, row + x);
78
3.49M
  const auto mr = LoadU(d, row + x + 1);
79
80
3.49M
  const auto bl = LoadU(d, row_bottom + x - 1);
81
3.49M
  const auto bc = Load(d, row_bottom + x);
82
3.49M
  const auto br = LoadU(d, row_bottom + x + 1);
83
84
3.49M
  const auto w_center = Set(d, w0);
85
3.49M
  const auto w_side = Set(d, w1);
86
3.49M
  const auto w_corner = Set(d, w2);
87
88
3.49M
  const auto corner = Add(Add(tl, tr), Add(bl, br));
89
3.49M
  const auto side = Add(Add(ml, mr), Add(tc, bc));
90
3.49M
  *sm = MulAdd(corner, w_corner, MulAdd(side, w_side, Mul(*mc, w_center)));
91
92
3.49M
  const auto dc_quant = Set(d, dc_factor);
93
3.49M
  *gap = MaxWorkaround(*gap, Abs(Div(Sub(*mc, *sm), dc_quant)));
94
3.49M
}
Unexecuted instantiation: void jxl::N_SSE4::ComputePixelChannel<hwy::N_SSE4::Simd<float, 1ul, 0> >(hwy::N_SSE4::Simd<float, 1ul, 0>, float, float const*, float const*, float const*, decltype (Zero((hwy::N_SSE4::Simd<float, 1ul, 0>)()))*, decltype (Zero((hwy::N_SSE4::Simd<float, 1ul, 0>)()))*, decltype (Zero((hwy::N_SSE4::Simd<float, 1ul, 0>)()))*, unsigned long)
Unexecuted instantiation: void jxl::N_SSE4::ComputePixelChannel<hwy::N_SSE4::Simd<float, 4ul, 0> >(hwy::N_SSE4::Simd<float, 4ul, 0>, float, float const*, float const*, float const*, decltype (Zero((hwy::N_SSE4::Simd<float, 4ul, 0>)()))*, decltype (Zero((hwy::N_SSE4::Simd<float, 4ul, 0>)()))*, decltype (Zero((hwy::N_SSE4::Simd<float, 4ul, 0>)()))*, unsigned long)
void jxl::N_AVX2::ComputePixelChannel<hwy::N_AVX2::Simd<float, 1ul, 0> >(hwy::N_AVX2::Simd<float, 1ul, 0>, float, float const*, float const*, float const*, decltype (Zero((hwy::N_AVX2::Simd<float, 1ul, 0>)()))*, decltype (Zero((hwy::N_AVX2::Simd<float, 1ul, 0>)()))*, decltype (Zero((hwy::N_AVX2::Simd<float, 1ul, 0>)()))*, unsigned long)
Line
Count
Source
71
2.43M
                                    Vec<D>* JXL_RESTRICT gap, size_t x) {
72
2.43M
  const auto tl = LoadU(d, row_top + x - 1);
73
2.43M
  const auto tc = Load(d, row_top + x);
74
2.43M
  const auto tr = LoadU(d, row_top + x + 1);
75
76
2.43M
  const auto ml = LoadU(d, row + x - 1);
77
2.43M
  *mc = Load(d, row + x);
78
2.43M
  const auto mr = LoadU(d, row + x + 1);
79
80
2.43M
  const auto bl = LoadU(d, row_bottom + x - 1);
81
2.43M
  const auto bc = Load(d, row_bottom + x);
82
2.43M
  const auto br = LoadU(d, row_bottom + x + 1);
83
84
2.43M
  const auto w_center = Set(d, w0);
85
2.43M
  const auto w_side = Set(d, w1);
86
2.43M
  const auto w_corner = Set(d, w2);
87
88
2.43M
  const auto corner = Add(Add(tl, tr), Add(bl, br));
89
2.43M
  const auto side = Add(Add(ml, mr), Add(tc, bc));
90
2.43M
  *sm = MulAdd(corner, w_corner, MulAdd(side, w_side, Mul(*mc, w_center)));
91
92
2.43M
  const auto dc_quant = Set(d, dc_factor);
93
2.43M
  *gap = MaxWorkaround(*gap, Abs(Div(Sub(*mc, *sm), dc_quant)));
94
2.43M
}
void jxl::N_AVX2::ComputePixelChannel<hwy::N_AVX2::Simd<float, 8ul, 0> >(hwy::N_AVX2::Simd<float, 8ul, 0>, float, float const*, float const*, float const*, decltype (Zero((hwy::N_AVX2::Simd<float, 8ul, 0>)()))*, decltype (Zero((hwy::N_AVX2::Simd<float, 8ul, 0>)()))*, decltype (Zero((hwy::N_AVX2::Simd<float, 8ul, 0>)()))*, unsigned long)
Line
Count
Source
71
1.06M
                                    Vec<D>* JXL_RESTRICT gap, size_t x) {
72
1.06M
  const auto tl = LoadU(d, row_top + x - 1);
73
1.06M
  const auto tc = Load(d, row_top + x);
74
1.06M
  const auto tr = LoadU(d, row_top + x + 1);
75
76
1.06M
  const auto ml = LoadU(d, row + x - 1);
77
1.06M
  *mc = Load(d, row + x);
78
1.06M
  const auto mr = LoadU(d, row + x + 1);
79
80
1.06M
  const auto bl = LoadU(d, row_bottom + x - 1);
81
1.06M
  const auto bc = Load(d, row_bottom + x);
82
1.06M
  const auto br = LoadU(d, row_bottom + x + 1);
83
84
1.06M
  const auto w_center = Set(d, w0);
85
1.06M
  const auto w_side = Set(d, w1);
86
1.06M
  const auto w_corner = Set(d, w2);
87
88
1.06M
  const auto corner = Add(Add(tl, tr), Add(bl, br));
89
1.06M
  const auto side = Add(Add(ml, mr), Add(tc, bc));
90
1.06M
  *sm = MulAdd(corner, w_corner, MulAdd(side, w_side, Mul(*mc, w_center)));
91
92
1.06M
  const auto dc_quant = Set(d, dc_factor);
93
1.06M
  *gap = MaxWorkaround(*gap, Abs(Div(Sub(*mc, *sm), dc_quant)));
94
1.06M
}
Unexecuted instantiation: 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)
Unexecuted instantiation: 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)
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
1.16M
    float* JXL_RESTRICT* JXL_RESTRICT out_rows, size_t x) {
103
1.16M
  const D d;
104
1.16M
  auto mc_x = Undefined(d);
105
1.16M
  auto mc_y = Undefined(d);
106
1.16M
  auto mc_b = Undefined(d);
107
1.16M
  auto sm_x = Undefined(d);
108
1.16M
  auto sm_y = Undefined(d);
109
1.16M
  auto sm_b = Undefined(d);
110
1.16M
  auto gap = Set(d, 0.5f);
111
1.16M
  ComputePixelChannel(d, dc_factors[0], rows_top[0], rows[0], rows_bottom[0],
112
1.16M
                      &mc_x, &sm_x, &gap, x);
113
1.16M
  ComputePixelChannel(d, dc_factors[1], rows_top[1], rows[1], rows_bottom[1],
114
1.16M
                      &mc_y, &sm_y, &gap, x);
115
1.16M
  ComputePixelChannel(d, dc_factors[2], rows_top[2], rows[2], rows_bottom[2],
116
1.16M
                      &mc_b, &sm_b, &gap, x);
117
1.16M
  auto factor = MulAdd(Set(d, -4.0f), gap, Set(d, 3.0f));
118
1.16M
  factor = ZeroIfNegative(factor);
119
120
1.16M
  auto out = MulAdd(Sub(sm_x, mc_x), factor, mc_x);
121
1.16M
  Store(out, d, out_rows[0] + x);
122
1.16M
  out = MulAdd(Sub(sm_y, mc_y), factor, mc_y);
123
1.16M
  Store(out, d, out_rows[1] + x);
124
1.16M
  out = MulAdd(Sub(sm_b, mc_b), factor, mc_b);
125
1.16M
  Store(out, d, out_rows[2] + x);
126
1.16M
}
Unexecuted instantiation: void jxl::N_SSE4::ComputePixel<hwy::N_SSE4::Simd<float, 1ul, 0> >(float const*, float const* restrict*, float const* restrict*, float const* restrict*, float* restrict*, unsigned long)
Unexecuted instantiation: void jxl::N_SSE4::ComputePixel<hwy::N_SSE4::Simd<float, 4ul, 0> >(float const*, float const* restrict*, float const* restrict*, float const* restrict*, float* restrict*, unsigned long)
void jxl::N_AVX2::ComputePixel<hwy::N_AVX2::Simd<float, 1ul, 0> >(float const*, float const* restrict*, float const* restrict*, float const* restrict*, float* restrict*, unsigned long)
Line
Count
Source
102
812k
    float* JXL_RESTRICT* JXL_RESTRICT out_rows, size_t x) {
103
812k
  const D d;
104
812k
  auto mc_x = Undefined(d);
105
812k
  auto mc_y = Undefined(d);
106
812k
  auto mc_b = Undefined(d);
107
812k
  auto sm_x = Undefined(d);
108
812k
  auto sm_y = Undefined(d);
109
812k
  auto sm_b = Undefined(d);
110
812k
  auto gap = Set(d, 0.5f);
111
812k
  ComputePixelChannel(d, dc_factors[0], rows_top[0], rows[0], rows_bottom[0],
112
812k
                      &mc_x, &sm_x, &gap, x);
113
812k
  ComputePixelChannel(d, dc_factors[1], rows_top[1], rows[1], rows_bottom[1],
114
812k
                      &mc_y, &sm_y, &gap, x);
115
812k
  ComputePixelChannel(d, dc_factors[2], rows_top[2], rows[2], rows_bottom[2],
116
812k
                      &mc_b, &sm_b, &gap, x);
117
812k
  auto factor = MulAdd(Set(d, -4.0f), gap, Set(d, 3.0f));
118
812k
  factor = ZeroIfNegative(factor);
119
120
812k
  auto out = MulAdd(Sub(sm_x, mc_x), factor, mc_x);
121
812k
  Store(out, d, out_rows[0] + x);
122
812k
  out = MulAdd(Sub(sm_y, mc_y), factor, mc_y);
123
812k
  Store(out, d, out_rows[1] + x);
124
812k
  out = MulAdd(Sub(sm_b, mc_b), factor, mc_b);
125
812k
  Store(out, d, out_rows[2] + x);
126
812k
}
void jxl::N_AVX2::ComputePixel<hwy::N_AVX2::Simd<float, 8ul, 0> >(float const*, float const* restrict*, float const* restrict*, float const* restrict*, float* restrict*, unsigned long)
Line
Count
Source
102
353k
    float* JXL_RESTRICT* JXL_RESTRICT out_rows, size_t x) {
103
353k
  const D d;
104
353k
  auto mc_x = Undefined(d);
105
353k
  auto mc_y = Undefined(d);
106
353k
  auto mc_b = Undefined(d);
107
353k
  auto sm_x = Undefined(d);
108
353k
  auto sm_y = Undefined(d);
109
353k
  auto sm_b = Undefined(d);
110
353k
  auto gap = Set(d, 0.5f);
111
353k
  ComputePixelChannel(d, dc_factors[0], rows_top[0], rows[0], rows_bottom[0],
112
353k
                      &mc_x, &sm_x, &gap, x);
113
353k
  ComputePixelChannel(d, dc_factors[1], rows_top[1], rows[1], rows_bottom[1],
114
353k
                      &mc_y, &sm_y, &gap, x);
115
353k
  ComputePixelChannel(d, dc_factors[2], rows_top[2], rows[2], rows_bottom[2],
116
353k
                      &mc_b, &sm_b, &gap, x);
117
353k
  auto factor = MulAdd(Set(d, -4.0f), gap, Set(d, 3.0f));
118
353k
  factor = ZeroIfNegative(factor);
119
120
353k
  auto out = MulAdd(Sub(sm_x, mc_x), factor, mc_x);
121
353k
  Store(out, d, out_rows[0] + x);
122
353k
  out = MulAdd(Sub(sm_y, mc_y), factor, mc_y);
123
353k
  Store(out, d, out_rows[1] + x);
124
353k
  out = MulAdd(Sub(sm_b, mc_b), factor, mc_b);
125
353k
  Store(out, d, out_rows[2] + x);
126
353k
}
Unexecuted instantiation: 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)
Unexecuted instantiation: 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)
127
128
Status AdaptiveDCSmoothing(JxlMemoryManager* memory_manager,
129
                           const float* dc_factors, Image3F* dc,
130
6.48k
                           ThreadPool* pool) {
131
6.48k
  const size_t xsize = dc->xsize();
132
6.48k
  const size_t ysize = dc->ysize();
133
6.48k
  if (ysize <= 2 || xsize <= 2) return true;
134
135
  // TODO(veluca): use tile-based processing?
136
  // TODO(veluca): decide if changes to the y channel should be propagated to
137
  // the x and b channels through color correlation.
138
2.38k
  JXL_ENSURE(w1 + w2 < 0.25f);
139
140
4.76k
  JXL_ASSIGN_OR_RETURN(Image3F smoothed,
141
4.76k
                       Image3F::Create(memory_manager, xsize, ysize));
142
  // Fill in borders that the loop below will not. First and last are unused.
143
9.52k
  for (size_t c = 0; c < 3; c++) {
144
14.2k
    for (size_t y : {static_cast<size_t>(0), ysize - 1}) {
145
14.2k
      memcpy(smoothed.PlaneRow(c, y), dc->PlaneRow(c, y),
146
14.2k
             xsize * sizeof(float));
147
14.2k
    }
148
7.14k
  }
149
77.6k
  auto process_row = [&](const uint32_t y, size_t /*thread*/) -> Status {
150
77.6k
    const float* JXL_RESTRICT rows_top[3]{
151
77.6k
        dc->ConstPlaneRow(0, y - 1),
152
77.6k
        dc->ConstPlaneRow(1, y - 1),
153
77.6k
        dc->ConstPlaneRow(2, y - 1),
154
77.6k
    };
155
77.6k
    const float* JXL_RESTRICT rows[3] = {
156
77.6k
        dc->ConstPlaneRow(0, y),
157
77.6k
        dc->ConstPlaneRow(1, y),
158
77.6k
        dc->ConstPlaneRow(2, y),
159
77.6k
    };
160
77.6k
    const float* JXL_RESTRICT rows_bottom[3] = {
161
77.6k
        dc->ConstPlaneRow(0, y + 1),
162
77.6k
        dc->ConstPlaneRow(1, y + 1),
163
77.6k
        dc->ConstPlaneRow(2, y + 1),
164
77.6k
    };
165
77.6k
    float* JXL_RESTRICT rows_out[3] = {
166
77.6k
        smoothed.PlaneRow(0, y),
167
77.6k
        smoothed.PlaneRow(1, y),
168
77.6k
        smoothed.PlaneRow(2, y),
169
77.6k
    };
170
155k
    for (size_t x : {static_cast<size_t>(0), xsize - 1}) {
171
621k
      for (size_t c = 0; c < 3; c++) {
172
466k
        rows_out[c][x] = rows[c][x];
173
466k
      }
174
155k
    }
175
176
77.6k
    size_t x = 1;
177
    // First pixels
178
77.6k
    const size_t N = Lanes(D());
179
610k
    for (; x < std::min(N, xsize - 1); x++) {
180
532k
      ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out,
181
532k
                            x);
182
532k
    }
183
    // Full vectors.
184
431k
    for (; x + N <= xsize - 1; x += N) {
185
353k
      ComputePixel<D>(dc_factors, rows_top, rows, rows_bottom, rows_out, x);
186
353k
    }
187
    // Last pixels.
188
357k
    for (; x < xsize - 1; x++) {
189
279k
      ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out,
190
279k
                            x);
191
279k
    }
192
77.6k
    return true;
193
77.6k
  };
Unexecuted instantiation: compressed_dc.cc:jxl::N_SSE4::AdaptiveDCSmoothing(JxlMemoryManagerStruct*, float const*, jxl::Image3<float>*, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
compressed_dc.cc:jxl::N_AVX2::AdaptiveDCSmoothing(JxlMemoryManagerStruct*, float const*, jxl::Image3<float>*, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
Line
Count
Source
149
77.6k
  auto process_row = [&](const uint32_t y, size_t /*thread*/) -> Status {
150
77.6k
    const float* JXL_RESTRICT rows_top[3]{
151
77.6k
        dc->ConstPlaneRow(0, y - 1),
152
77.6k
        dc->ConstPlaneRow(1, y - 1),
153
77.6k
        dc->ConstPlaneRow(2, y - 1),
154
77.6k
    };
155
77.6k
    const float* JXL_RESTRICT rows[3] = {
156
77.6k
        dc->ConstPlaneRow(0, y),
157
77.6k
        dc->ConstPlaneRow(1, y),
158
77.6k
        dc->ConstPlaneRow(2, y),
159
77.6k
    };
160
77.6k
    const float* JXL_RESTRICT rows_bottom[3] = {
161
77.6k
        dc->ConstPlaneRow(0, y + 1),
162
77.6k
        dc->ConstPlaneRow(1, y + 1),
163
77.6k
        dc->ConstPlaneRow(2, y + 1),
164
77.6k
    };
165
77.6k
    float* JXL_RESTRICT rows_out[3] = {
166
77.6k
        smoothed.PlaneRow(0, y),
167
77.6k
        smoothed.PlaneRow(1, y),
168
77.6k
        smoothed.PlaneRow(2, y),
169
77.6k
    };
170
155k
    for (size_t x : {static_cast<size_t>(0), xsize - 1}) {
171
621k
      for (size_t c = 0; c < 3; c++) {
172
466k
        rows_out[c][x] = rows[c][x];
173
466k
      }
174
155k
    }
175
176
77.6k
    size_t x = 1;
177
    // First pixels
178
77.6k
    const size_t N = Lanes(D());
179
610k
    for (; x < std::min(N, xsize - 1); x++) {
180
532k
      ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out,
181
532k
                            x);
182
532k
    }
183
    // Full vectors.
184
431k
    for (; x + N <= xsize - 1; x += N) {
185
353k
      ComputePixel<D>(dc_factors, rows_top, rows, rows_bottom, rows_out, x);
186
353k
    }
187
    // Last pixels.
188
357k
    for (; x < xsize - 1; x++) {
189
279k
      ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out,
190
279k
                            x);
191
279k
    }
192
77.6k
    return true;
193
77.6k
  };
Unexecuted instantiation: compressed_dc.cc:jxl::N_SSE2::AdaptiveDCSmoothing(JxlMemoryManagerStruct*, float const*, jxl::Image3<float>*, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
194
4.76k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 1, ysize - 1, ThreadPool::NoInit,
195
4.76k
                                process_row, "DCSmoothingRow"));
196
2.38k
  dc->Swap(smoothed);
197
2.38k
  return true;
198
4.76k
}
Unexecuted instantiation: jxl::N_SSE4::AdaptiveDCSmoothing(JxlMemoryManagerStruct*, float const*, jxl::Image3<float>*, jxl::ThreadPool*)
jxl::N_AVX2::AdaptiveDCSmoothing(JxlMemoryManagerStruct*, float const*, jxl::Image3<float>*, jxl::ThreadPool*)
Line
Count
Source
130
6.48k
                           ThreadPool* pool) {
131
6.48k
  const size_t xsize = dc->xsize();
132
6.48k
  const size_t ysize = dc->ysize();
133
6.48k
  if (ysize <= 2 || xsize <= 2) return true;
134
135
  // TODO(veluca): use tile-based processing?
136
  // TODO(veluca): decide if changes to the y channel should be propagated to
137
  // the x and b channels through color correlation.
138
2.38k
  JXL_ENSURE(w1 + w2 < 0.25f);
139
140
4.76k
  JXL_ASSIGN_OR_RETURN(Image3F smoothed,
141
4.76k
                       Image3F::Create(memory_manager, xsize, ysize));
142
  // Fill in borders that the loop below will not. First and last are unused.
143
9.52k
  for (size_t c = 0; c < 3; c++) {
144
14.2k
    for (size_t y : {static_cast<size_t>(0), ysize - 1}) {
145
14.2k
      memcpy(smoothed.PlaneRow(c, y), dc->PlaneRow(c, y),
146
14.2k
             xsize * sizeof(float));
147
14.2k
    }
148
7.14k
  }
149
4.76k
  auto process_row = [&](const uint32_t y, size_t /*thread*/) -> Status {
150
4.76k
    const float* JXL_RESTRICT rows_top[3]{
151
4.76k
        dc->ConstPlaneRow(0, y - 1),
152
4.76k
        dc->ConstPlaneRow(1, y - 1),
153
4.76k
        dc->ConstPlaneRow(2, y - 1),
154
4.76k
    };
155
4.76k
    const float* JXL_RESTRICT rows[3] = {
156
4.76k
        dc->ConstPlaneRow(0, y),
157
4.76k
        dc->ConstPlaneRow(1, y),
158
4.76k
        dc->ConstPlaneRow(2, y),
159
4.76k
    };
160
4.76k
    const float* JXL_RESTRICT rows_bottom[3] = {
161
4.76k
        dc->ConstPlaneRow(0, y + 1),
162
4.76k
        dc->ConstPlaneRow(1, y + 1),
163
4.76k
        dc->ConstPlaneRow(2, y + 1),
164
4.76k
    };
165
4.76k
    float* JXL_RESTRICT rows_out[3] = {
166
4.76k
        smoothed.PlaneRow(0, y),
167
4.76k
        smoothed.PlaneRow(1, y),
168
4.76k
        smoothed.PlaneRow(2, y),
169
4.76k
    };
170
4.76k
    for (size_t x : {static_cast<size_t>(0), xsize - 1}) {
171
4.76k
      for (size_t c = 0; c < 3; c++) {
172
4.76k
        rows_out[c][x] = rows[c][x];
173
4.76k
      }
174
4.76k
    }
175
176
4.76k
    size_t x = 1;
177
    // First pixels
178
4.76k
    const size_t N = Lanes(D());
179
4.76k
    for (; x < std::min(N, xsize - 1); x++) {
180
4.76k
      ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out,
181
4.76k
                            x);
182
4.76k
    }
183
    // Full vectors.
184
4.76k
    for (; x + N <= xsize - 1; x += N) {
185
4.76k
      ComputePixel<D>(dc_factors, rows_top, rows, rows_bottom, rows_out, x);
186
4.76k
    }
187
    // Last pixels.
188
4.76k
    for (; x < xsize - 1; x++) {
189
4.76k
      ComputePixel<DScalar>(dc_factors, rows_top, rows, rows_bottom, rows_out,
190
4.76k
                            x);
191
4.76k
    }
192
4.76k
    return true;
193
4.76k
  };
194
4.76k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 1, ysize - 1, ThreadPool::NoInit,
195
4.76k
                                process_row, "DCSmoothingRow"));
196
2.38k
  dc->Swap(smoothed);
197
2.38k
  return true;
198
4.76k
}
Unexecuted instantiation: jxl::N_SSE2::AdaptiveDCSmoothing(JxlMemoryManagerStruct*, float const*, jxl::Image3<float>*, jxl::ThreadPool*)
199
200
// DC dequantization.
201
void DequantDC(const Rect& r, Image3F* dc, ImageB* quant_dc, const Image& in,
202
               const float* dc_factors, float mul, const float* cfl_factors,
203
               const YCbCrChromaSubsampling& chroma_subsampling,
204
7.29k
               const BlockCtxMap& bctx) {
205
7.29k
  const HWY_FULL(float) df;
206
7.29k
  const Rebind<pixel_type, HWY_FULL(float)> di;  // assumes pixel_type <= float
207
7.29k
  if (chroma_subsampling.Is444()) {
208
7.04k
    const auto fac_x = Set(df, dc_factors[0] * mul);
209
7.04k
    const auto fac_y = Set(df, dc_factors[1] * mul);
210
7.04k
    const auto fac_b = Set(df, dc_factors[2] * mul);
211
7.04k
    const auto cfl_fac_x = Set(df, cfl_factors[0]);
212
7.04k
    const auto cfl_fac_b = Set(df, cfl_factors[2]);
213
101k
    for (size_t y = 0; y < r.ysize(); y++) {
214
94.2k
      float* dec_row_x = r.PlaneRow(dc, 0, y);
215
94.2k
      float* dec_row_y = r.PlaneRow(dc, 1, y);
216
94.2k
      float* dec_row_b = r.PlaneRow(dc, 2, y);
217
94.2k
      const int32_t* quant_row_x = in.channel[1].plane.Row(y);
218
94.2k
      const int32_t* quant_row_y = in.channel[0].plane.Row(y);
219
94.2k
      const int32_t* quant_row_b = in.channel[2].plane.Row(y);
220
638k
      for (size_t x = 0; x < r.xsize(); x += Lanes(di)) {
221
544k
        const auto in_q_x = Load(di, quant_row_x + x);
222
544k
        const auto in_q_y = Load(di, quant_row_y + x);
223
544k
        const auto in_q_b = Load(di, quant_row_b + x);
224
544k
        const auto in_x = Mul(ConvertTo(df, in_q_x), fac_x);
225
544k
        const auto in_y = Mul(ConvertTo(df, in_q_y), fac_y);
226
544k
        const auto in_b = Mul(ConvertTo(df, in_q_b), fac_b);
227
544k
        Store(in_y, df, dec_row_y + x);
228
544k
        Store(MulAdd(in_y, cfl_fac_x, in_x), df, dec_row_x + x);
229
544k
        Store(MulAdd(in_y, cfl_fac_b, in_b), df, dec_row_b + x);
230
544k
      }
231
94.2k
    }
232
7.04k
  } else {
233
744
    for (size_t c : {1, 0, 2}) {
234
744
      Rect rect(r.x0() >> chroma_subsampling.HShift(c),
235
744
                r.y0() >> chroma_subsampling.VShift(c),
236
744
                r.xsize() >> chroma_subsampling.HShift(c),
237
744
                r.ysize() >> chroma_subsampling.VShift(c));
238
744
      const auto fac = Set(df, dc_factors[c] * mul);
239
744
      const Channel& ch = in.channel[c < 2 ? c ^ 1 : c];
240
25.1k
      for (size_t y = 0; y < rect.ysize(); y++) {
241
24.4k
        const int32_t* quant_row = ch.plane.Row(y);
242
24.4k
        float* row = rect.PlaneRow(dc, c, y);
243
205k
        for (size_t x = 0; x < rect.xsize(); x += Lanes(di)) {
244
180k
          const auto in_q = Load(di, quant_row + x);
245
180k
          const auto out = Mul(ConvertTo(df, in_q), fac);
246
180k
          Store(out, df, row + x);
247
180k
        }
248
24.4k
      }
249
744
    }
250
248
  }
251
7.29k
  if (bctx.num_dc_ctxs <= 1) {
252
103k
    for (size_t y = 0; y < r.ysize(); y++) {
253
97.5k
      uint8_t* qdc_row = r.Row(quant_dc, y);
254
97.5k
      memset(qdc_row, 0, sizeof(*qdc_row) * r.xsize());
255
97.5k
    }
256
6.03k
  } else {
257
7.87k
    for (size_t y = 0; y < r.ysize(); y++) {
258
6.61k
      uint8_t* qdc_row_val = r.Row(quant_dc, y);
259
6.61k
      const int32_t* quant_row_x =
260
6.61k
          in.channel[1].plane.Row(y >> chroma_subsampling.VShift(0));
261
6.61k
      const int32_t* quant_row_y =
262
6.61k
          in.channel[0].plane.Row(y >> chroma_subsampling.VShift(1));
263
6.61k
      const int32_t* quant_row_b =
264
6.61k
          in.channel[2].plane.Row(y >> chroma_subsampling.VShift(2));
265
261k
      for (size_t x = 0; x < r.xsize(); x++) {
266
255k
        int bucket_x = 0;
267
255k
        int bucket_y = 0;
268
255k
        int bucket_b = 0;
269
1.15M
        for (int t : bctx.dc_thresholds[0]) {
270
1.15M
          if (quant_row_x[x >> chroma_subsampling.HShift(0)] > t) bucket_x++;
271
1.15M
        }
272
255k
        for (int t : bctx.dc_thresholds[1]) {
273
238k
          if (quant_row_y[x >> chroma_subsampling.HShift(1)] > t) bucket_y++;
274
238k
        }
275
255k
        for (int t : bctx.dc_thresholds[2]) {
276
166k
          if (quant_row_b[x >> chroma_subsampling.HShift(2)] > t) bucket_b++;
277
166k
        }
278
255k
        int bucket = bucket_x;
279
255k
        bucket *= bctx.dc_thresholds[2].size() + 1;
280
255k
        bucket += bucket_b;
281
255k
        bucket *= bctx.dc_thresholds[1].size() + 1;
282
255k
        bucket += bucket_y;
283
255k
        qdc_row_val[x] = bucket;
284
255k
      }
285
6.61k
    }
286
1.25k
  }
287
7.29k
}
Unexecuted instantiation: jxl::N_SSE4::DequantDC(jxl::RectT<unsigned long> const&, jxl::Image3<float>*, jxl::Plane<unsigned char>*, jxl::Image const&, float const*, float, float const*, jxl::YCbCrChromaSubsampling const&, jxl::BlockCtxMap const&)
jxl::N_AVX2::DequantDC(jxl::RectT<unsigned long> const&, jxl::Image3<float>*, jxl::Plane<unsigned char>*, jxl::Image const&, float const*, float, float const*, jxl::YCbCrChromaSubsampling const&, jxl::BlockCtxMap const&)
Line
Count
Source
204
7.29k
               const BlockCtxMap& bctx) {
205
7.29k
  const HWY_FULL(float) df;
206
7.29k
  const Rebind<pixel_type, HWY_FULL(float)> di;  // assumes pixel_type <= float
207
7.29k
  if (chroma_subsampling.Is444()) {
208
7.04k
    const auto fac_x = Set(df, dc_factors[0] * mul);
209
7.04k
    const auto fac_y = Set(df, dc_factors[1] * mul);
210
7.04k
    const auto fac_b = Set(df, dc_factors[2] * mul);
211
7.04k
    const auto cfl_fac_x = Set(df, cfl_factors[0]);
212
7.04k
    const auto cfl_fac_b = Set(df, cfl_factors[2]);
213
101k
    for (size_t y = 0; y < r.ysize(); y++) {
214
94.2k
      float* dec_row_x = r.PlaneRow(dc, 0, y);
215
94.2k
      float* dec_row_y = r.PlaneRow(dc, 1, y);
216
94.2k
      float* dec_row_b = r.PlaneRow(dc, 2, y);
217
94.2k
      const int32_t* quant_row_x = in.channel[1].plane.Row(y);
218
94.2k
      const int32_t* quant_row_y = in.channel[0].plane.Row(y);
219
94.2k
      const int32_t* quant_row_b = in.channel[2].plane.Row(y);
220
638k
      for (size_t x = 0; x < r.xsize(); x += Lanes(di)) {
221
544k
        const auto in_q_x = Load(di, quant_row_x + x);
222
544k
        const auto in_q_y = Load(di, quant_row_y + x);
223
544k
        const auto in_q_b = Load(di, quant_row_b + x);
224
544k
        const auto in_x = Mul(ConvertTo(df, in_q_x), fac_x);
225
544k
        const auto in_y = Mul(ConvertTo(df, in_q_y), fac_y);
226
544k
        const auto in_b = Mul(ConvertTo(df, in_q_b), fac_b);
227
544k
        Store(in_y, df, dec_row_y + x);
228
544k
        Store(MulAdd(in_y, cfl_fac_x, in_x), df, dec_row_x + x);
229
544k
        Store(MulAdd(in_y, cfl_fac_b, in_b), df, dec_row_b + x);
230
544k
      }
231
94.2k
    }
232
7.04k
  } else {
233
744
    for (size_t c : {1, 0, 2}) {
234
744
      Rect rect(r.x0() >> chroma_subsampling.HShift(c),
235
744
                r.y0() >> chroma_subsampling.VShift(c),
236
744
                r.xsize() >> chroma_subsampling.HShift(c),
237
744
                r.ysize() >> chroma_subsampling.VShift(c));
238
744
      const auto fac = Set(df, dc_factors[c] * mul);
239
744
      const Channel& ch = in.channel[c < 2 ? c ^ 1 : c];
240
25.1k
      for (size_t y = 0; y < rect.ysize(); y++) {
241
24.4k
        const int32_t* quant_row = ch.plane.Row(y);
242
24.4k
        float* row = rect.PlaneRow(dc, c, y);
243
205k
        for (size_t x = 0; x < rect.xsize(); x += Lanes(di)) {
244
180k
          const auto in_q = Load(di, quant_row + x);
245
180k
          const auto out = Mul(ConvertTo(df, in_q), fac);
246
180k
          Store(out, df, row + x);
247
180k
        }
248
24.4k
      }
249
744
    }
250
248
  }
251
7.29k
  if (bctx.num_dc_ctxs <= 1) {
252
103k
    for (size_t y = 0; y < r.ysize(); y++) {
253
97.5k
      uint8_t* qdc_row = r.Row(quant_dc, y);
254
97.5k
      memset(qdc_row, 0, sizeof(*qdc_row) * r.xsize());
255
97.5k
    }
256
6.03k
  } else {
257
7.87k
    for (size_t y = 0; y < r.ysize(); y++) {
258
6.61k
      uint8_t* qdc_row_val = r.Row(quant_dc, y);
259
6.61k
      const int32_t* quant_row_x =
260
6.61k
          in.channel[1].plane.Row(y >> chroma_subsampling.VShift(0));
261
6.61k
      const int32_t* quant_row_y =
262
6.61k
          in.channel[0].plane.Row(y >> chroma_subsampling.VShift(1));
263
6.61k
      const int32_t* quant_row_b =
264
6.61k
          in.channel[2].plane.Row(y >> chroma_subsampling.VShift(2));
265
261k
      for (size_t x = 0; x < r.xsize(); x++) {
266
255k
        int bucket_x = 0;
267
255k
        int bucket_y = 0;
268
255k
        int bucket_b = 0;
269
1.15M
        for (int t : bctx.dc_thresholds[0]) {
270
1.15M
          if (quant_row_x[x >> chroma_subsampling.HShift(0)] > t) bucket_x++;
271
1.15M
        }
272
255k
        for (int t : bctx.dc_thresholds[1]) {
273
238k
          if (quant_row_y[x >> chroma_subsampling.HShift(1)] > t) bucket_y++;
274
238k
        }
275
255k
        for (int t : bctx.dc_thresholds[2]) {
276
166k
          if (quant_row_b[x >> chroma_subsampling.HShift(2)] > t) bucket_b++;
277
166k
        }
278
255k
        int bucket = bucket_x;
279
255k
        bucket *= bctx.dc_thresholds[2].size() + 1;
280
255k
        bucket += bucket_b;
281
255k
        bucket *= bctx.dc_thresholds[1].size() + 1;
282
255k
        bucket += bucket_y;
283
255k
        qdc_row_val[x] = bucket;
284
255k
      }
285
6.61k
    }
286
1.25k
  }
287
7.29k
}
Unexecuted instantiation: jxl::N_SSE2::DequantDC(jxl::RectT<unsigned long> const&, jxl::Image3<float>*, jxl::Plane<unsigned char>*, jxl::Image const&, float const*, float, float const*, jxl::YCbCrChromaSubsampling const&, jxl::BlockCtxMap const&)
288
289
// NOLINTNEXTLINE(google-readability-namespace-comments)
290
}  // namespace HWY_NAMESPACE
291
}  // namespace jxl
292
HWY_AFTER_NAMESPACE();
293
294
#if HWY_ONCE
295
namespace jxl {
296
297
HWY_EXPORT(DequantDC);
298
HWY_EXPORT(AdaptiveDCSmoothing);
299
Status AdaptiveDCSmoothing(JxlMemoryManager* memory_manager,
300
                           const float* dc_factors, Image3F* dc,
301
6.48k
                           ThreadPool* pool) {
302
6.48k
  return HWY_DYNAMIC_DISPATCH(AdaptiveDCSmoothing)(memory_manager, dc_factors,
303
6.48k
                                                   dc, pool);
304
6.48k
}
305
306
void DequantDC(const Rect& r, Image3F* dc, ImageB* quant_dc, const Image& in,
307
               const float* dc_factors, float mul, const float* cfl_factors,
308
               const YCbCrChromaSubsampling& chroma_subsampling,
309
7.29k
               const BlockCtxMap& bctx) {
310
7.29k
  HWY_DYNAMIC_DISPATCH(DequantDC)
311
7.29k
  (r, dc, quant_dc, in, dc_factors, mul, cfl_factors, chroma_subsampling, bctx);
312
7.29k
}
313
314
}  // namespace jxl
315
#endif  // HWY_ONCE