Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/modular/transform/squeeze.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/modular/transform/squeeze.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <cstdint>
12
#include <cstdlib>
13
#include <utility>
14
#include <vector>
15
16
#include "lib/jxl/base/common.h"
17
#include "lib/jxl/base/compiler_specific.h"
18
#include "lib/jxl/base/data_parallel.h"
19
#include "lib/jxl/base/printf_macros.h"
20
#include "lib/jxl/base/status.h"
21
#include "lib/jxl/modular/modular_image.h"
22
#include "lib/jxl/modular/transform/squeeze_params.h"
23
#undef HWY_TARGET_INCLUDE
24
#define HWY_TARGET_INCLUDE "lib/jxl/modular/transform/squeeze.cc"
25
#include <hwy/foreach_target.h>
26
#include <hwy/highway.h>
27
28
#include "lib/jxl/simd_util-inl.h"
29
30
HWY_BEFORE_NAMESPACE();
31
namespace jxl {
32
namespace HWY_NAMESPACE {
33
34
#if HWY_TARGET != HWY_SCALAR
35
36
// These templates are not found via ADL.
37
using hwy::HWY_NAMESPACE::Abs;
38
using hwy::HWY_NAMESPACE::Add;
39
using hwy::HWY_NAMESPACE::And;
40
using hwy::HWY_NAMESPACE::DupEven;
41
using hwy::HWY_NAMESPACE::DupOdd;
42
using hwy::HWY_NAMESPACE::Gt;
43
using hwy::HWY_NAMESPACE::IfThenElse;
44
using hwy::HWY_NAMESPACE::IfThenZeroElse;
45
using hwy::HWY_NAMESPACE::Lt;
46
using hwy::HWY_NAMESPACE::MulEven;
47
using hwy::HWY_NAMESPACE::MulOdd;
48
using hwy::HWY_NAMESPACE::Ne;
49
using hwy::HWY_NAMESPACE::Neg;
50
using hwy::HWY_NAMESPACE::OddEven;
51
using hwy::HWY_NAMESPACE::RebindToUnsigned;
52
using hwy::HWY_NAMESPACE::ShiftLeft;
53
using hwy::HWY_NAMESPACE::ShiftRight;
54
using hwy::HWY_NAMESPACE::Sub;
55
using hwy::HWY_NAMESPACE::Xor;
56
57
using D = HWY_CAPPED(pixel_type, 8);
58
using DU = RebindToUnsigned<D>;
59
constexpr D d;
60
constexpr DU du;
61
62
JXL_INLINE void FastUnsqueeze(const pixel_type *JXL_RESTRICT p_residual,
63
                              const pixel_type *JXL_RESTRICT p_avg,
64
                              const pixel_type *JXL_RESTRICT p_navg,
65
                              const pixel_type *p_pout,
66
                              pixel_type *JXL_RESTRICT p_out,
67
20.6M
                              pixel_type *p_nout) {
68
20.6M
  const size_t N = Lanes(d);
69
20.6M
  auto onethird = Set(d, 0x55555556);
70
41.3M
  for (size_t x = 0; x < 8; x += N) {
71
20.6M
    auto avg = Load(d, p_avg + x);
72
20.6M
    auto next_avg = Load(d, p_navg + x);
73
20.6M
    auto top = Load(d, p_pout + x);
74
    // Equivalent to SmoothTendency(top,avg,next_avg), but without branches
75
    // typo:off
76
20.6M
    auto Ba = Sub(top, avg);
77
20.6M
    auto an = Sub(avg, next_avg);
78
20.6M
    auto nonmono = Xor(Ba, an);
79
20.6M
    auto absBa = Abs(Ba);
80
20.6M
    auto absan = Abs(an);
81
20.6M
    auto absBn = Abs(Sub(top, next_avg));
82
    // Compute a3 = absBa / 3
83
20.6M
    auto a3eh = MulEven(absBa, onethird);
84
20.6M
    auto a3oh = MulOdd(absBa, onethird);
85
86
20.6M
#if (HWY_MAJOR > 1 || (HWY_MAJOR == 1 && HWY_MINOR >= 2))
87
20.6M
#if HWY_IS_LITTLE_ENDIAN
88
20.6M
    auto a3 = InterleaveOdd(d, BitCast(d, a3eh), BitCast(d, a3oh));
89
#else  // not little endian
90
    auto a3 = InterleaveEven(d, BitCast(d, a3eh), BitCast(d, a3oh));
91
#endif  // endianness
92
#else  // hwy < 1.2
93
#if HWY_IS_LITTLE_ENDIAN
94
    auto a3 = OddEven(BitCast(d, a3oh), DupOdd(BitCast(d, a3eh)));
95
#else  // not little endian
96
    auto a3 = OddEven(DupEven(BitCast(d, a3oh)), BitCast(d, a3eh))
97
#endif  // endianness
98
#endif  // hwy version
99
100
20.6M
    a3 = Add(a3, Add(absBn, Set(d, 2)));
101
20.6M
    auto absdiff = ShiftRight<2>(a3);
102
20.6M
    auto skipdiff = Ne(Ba, Zero(d));
103
20.6M
    skipdiff = And(skipdiff, Ne(an, Zero(d)));
104
20.6M
    skipdiff = And(skipdiff, Lt(nonmono, Zero(d)));
105
20.6M
    auto absBa2 = Add(ShiftLeft<1>(absBa), And(absdiff, Set(d, 1)));
106
20.6M
    absdiff = IfThenElse(Gt(absdiff, absBa2),
107
20.6M
                         Add(ShiftLeft<1>(absBa), Set(d, 1)), absdiff);
108
    // typo:on
109
20.6M
    auto absan2 = ShiftLeft<1>(absan);
110
20.6M
    absdiff = IfThenElse(Gt(Add(absdiff, And(absdiff, Set(d, 1))), absan2),
111
20.6M
                         absan2, absdiff);
112
20.6M
    auto diff1 = IfThenElse(Lt(top, next_avg), Neg(absdiff), absdiff);
113
20.6M
    auto tendency = IfThenZeroElse(skipdiff, diff1);
114
115
20.6M
    auto diff_minus_tendency = Load(d, p_residual + x);
116
20.6M
    auto diff = Add(diff_minus_tendency, tendency);
117
20.6M
    auto out =
118
20.6M
        Add(avg, ShiftRight<1>(
119
20.6M
                     Add(diff, BitCast(d, ShiftRight<31>(BitCast(du, diff))))));
120
20.6M
    Store(out, d, p_out + x);
121
20.6M
    Store(Sub(out, diff), d, p_nout + x);
122
20.6M
  }
123
20.6M
}
Unexecuted instantiation: jxl::N_SSE4::FastUnsqueeze(int const*, int const*, int const*, int const*, int*, int*)
jxl::N_AVX2::FastUnsqueeze(int const*, int const*, int const*, int const*, int*, int*)
Line
Count
Source
67
20.6M
                              pixel_type *p_nout) {
68
20.6M
  const size_t N = Lanes(d);
69
20.6M
  auto onethird = Set(d, 0x55555556);
70
41.3M
  for (size_t x = 0; x < 8; x += N) {
71
20.6M
    auto avg = Load(d, p_avg + x);
72
20.6M
    auto next_avg = Load(d, p_navg + x);
73
20.6M
    auto top = Load(d, p_pout + x);
74
    // Equivalent to SmoothTendency(top,avg,next_avg), but without branches
75
    // typo:off
76
20.6M
    auto Ba = Sub(top, avg);
77
20.6M
    auto an = Sub(avg, next_avg);
78
20.6M
    auto nonmono = Xor(Ba, an);
79
20.6M
    auto absBa = Abs(Ba);
80
20.6M
    auto absan = Abs(an);
81
20.6M
    auto absBn = Abs(Sub(top, next_avg));
82
    // Compute a3 = absBa / 3
83
20.6M
    auto a3eh = MulEven(absBa, onethird);
84
20.6M
    auto a3oh = MulOdd(absBa, onethird);
85
86
20.6M
#if (HWY_MAJOR > 1 || (HWY_MAJOR == 1 && HWY_MINOR >= 2))
87
20.6M
#if HWY_IS_LITTLE_ENDIAN
88
20.6M
    auto a3 = InterleaveOdd(d, BitCast(d, a3eh), BitCast(d, a3oh));
89
#else  // not little endian
90
    auto a3 = InterleaveEven(d, BitCast(d, a3eh), BitCast(d, a3oh));
91
#endif  // endianness
92
#else  // hwy < 1.2
93
#if HWY_IS_LITTLE_ENDIAN
94
    auto a3 = OddEven(BitCast(d, a3oh), DupOdd(BitCast(d, a3eh)));
95
#else  // not little endian
96
    auto a3 = OddEven(DupEven(BitCast(d, a3oh)), BitCast(d, a3eh))
97
#endif  // endianness
98
#endif  // hwy version
99
100
20.6M
    a3 = Add(a3, Add(absBn, Set(d, 2)));
101
20.6M
    auto absdiff = ShiftRight<2>(a3);
102
20.6M
    auto skipdiff = Ne(Ba, Zero(d));
103
20.6M
    skipdiff = And(skipdiff, Ne(an, Zero(d)));
104
20.6M
    skipdiff = And(skipdiff, Lt(nonmono, Zero(d)));
105
20.6M
    auto absBa2 = Add(ShiftLeft<1>(absBa), And(absdiff, Set(d, 1)));
106
20.6M
    absdiff = IfThenElse(Gt(absdiff, absBa2),
107
20.6M
                         Add(ShiftLeft<1>(absBa), Set(d, 1)), absdiff);
108
    // typo:on
109
20.6M
    auto absan2 = ShiftLeft<1>(absan);
110
20.6M
    absdiff = IfThenElse(Gt(Add(absdiff, And(absdiff, Set(d, 1))), absan2),
111
20.6M
                         absan2, absdiff);
112
20.6M
    auto diff1 = IfThenElse(Lt(top, next_avg), Neg(absdiff), absdiff);
113
20.6M
    auto tendency = IfThenZeroElse(skipdiff, diff1);
114
115
20.6M
    auto diff_minus_tendency = Load(d, p_residual + x);
116
20.6M
    auto diff = Add(diff_minus_tendency, tendency);
117
20.6M
    auto out =
118
20.6M
        Add(avg, ShiftRight<1>(
119
20.6M
                     Add(diff, BitCast(d, ShiftRight<31>(BitCast(du, diff))))));
120
20.6M
    Store(out, d, p_out + x);
121
20.6M
    Store(Sub(out, diff), d, p_nout + x);
122
20.6M
  }
123
20.6M
}
Unexecuted instantiation: jxl::N_AVX3::FastUnsqueeze(int const*, int const*, int const*, int const*, int*, int*)
Unexecuted instantiation: jxl::N_AVX3_ZEN4::FastUnsqueeze(int const*, int const*, int const*, int const*, int*, int*)
Unexecuted instantiation: jxl::N_AVX3_SPR::FastUnsqueeze(int const*, int const*, int const*, int const*, int*, int*)
Unexecuted instantiation: jxl::N_SSE2::FastUnsqueeze(int const*, int const*, int const*, int const*, int*, int*)
124
125
#endif  // HWY_TARGET != HWY_SCALAR
126
127
66.9k
Status InvHSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
128
66.9k
  JXL_ENSURE(c < input.channel.size());
129
66.9k
  JXL_ENSURE(rc < input.channel.size());
130
66.9k
  Channel &chin = input.channel[c];
131
66.9k
  const Channel &chin_residual = input.channel[rc];
132
  // These must be valid since we ran MetaApply already.
133
66.9k
  JXL_ENSURE(chin.w == DivCeil(chin.w + chin_residual.w, 2));
134
66.9k
  JXL_ENSURE(chin.h == chin_residual.h);
135
66.9k
  JxlMemoryManager *memory_manager = input.memory_manager();
136
137
66.9k
  if (chin_residual.w == 0) {
138
    // Short-circuit: output channel has same dimensions as input.
139
908
    input.channel[c].hshift--;
140
908
    return true;
141
908
  }
142
143
  // Note: chin.w >= chin_residual.w and at most 1 different.
144
132k
  JXL_ASSIGN_OR_RETURN(Channel chout,
145
132k
                       Channel::Create(memory_manager, chin.w + chin_residual.w,
146
132k
                                       chin.h, chin.hshift - 1, chin.vshift));
147
132k
  JXL_DEBUG_V(4,
148
132k
              "Undoing horizontal squeeze of channel %i using residuals in "
149
132k
              "channel %i (going from width %" PRIuS " to %" PRIuS ")",
150
132k
              c, rc, chin.w, chout.w);
151
152
132k
  if (chin_residual.h == 0) {
153
    // Short-circuit: channel with no pixels.
154
0
    input.channel[c] = std::move(chout);
155
0
    return true;
156
0
  }
157
2.29M
  auto unsqueeze_row = [&](size_t y, size_t x0) {
158
2.29M
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
159
2.29M
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
160
2.29M
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
161
19.4M
    for (size_t x = x0; x < chin_residual.w; x++) {
162
17.1M
      pixel_type_w diff_minus_tendency = p_residual[x];
163
17.1M
      pixel_type_w avg = p_avg[x];
164
17.1M
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
165
17.1M
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
166
17.1M
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
167
17.1M
      pixel_type_w diff = diff_minus_tendency + tendency;
168
17.1M
      pixel_type_w A = avg + (diff / 2);
169
17.1M
      p_out[(x << 1)] = A;
170
17.1M
      pixel_type_w B = A - diff;
171
17.1M
      p_out[(x << 1) + 1] = B;
172
17.1M
    }
173
2.29M
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
174
2.29M
  };
Unexecuted instantiation: squeeze.cc:jxl::N_SSE4::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned long, unsigned long) const
squeeze.cc:jxl::N_AVX2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned long, unsigned long) const
Line
Count
Source
157
2.29M
  auto unsqueeze_row = [&](size_t y, size_t x0) {
158
2.29M
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
159
2.29M
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
160
2.29M
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
161
19.4M
    for (size_t x = x0; x < chin_residual.w; x++) {
162
17.1M
      pixel_type_w diff_minus_tendency = p_residual[x];
163
17.1M
      pixel_type_w avg = p_avg[x];
164
17.1M
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
165
17.1M
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
166
17.1M
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
167
17.1M
      pixel_type_w diff = diff_minus_tendency + tendency;
168
17.1M
      pixel_type_w A = avg + (diff / 2);
169
17.1M
      p_out[(x << 1)] = A;
170
17.1M
      pixel_type_w B = A - diff;
171
17.1M
      p_out[(x << 1) + 1] = B;
172
17.1M
    }
173
2.29M
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
174
2.29M
  };
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned long, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3_ZEN4::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned long, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3_SPR::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned long, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_SSE2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned long, unsigned long) const
175
176
  // somewhat complicated trickery just to be able to SIMD this.
177
  // Horizontal unsqueeze has horizontal data dependencies, so we do
178
  // 8 rows at a time and treat it as a vertical unsqueeze of a
179
  // transposed 8x8 block (or 9x8 for one input).
180
66.0k
  static constexpr const size_t kRowsPerThread = 8;
181
66.0k
  const auto unsqueeze_span = [&](const uint32_t task,
182
312k
                                  size_t /* thread */) -> Status {
183
312k
    const size_t y0 = task * kRowsPerThread;
184
312k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
185
312k
    size_t x = 0;
186
187
312k
#if HWY_TARGET != HWY_SCALAR
188
312k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
189
312k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
190
312k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
191
312k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
192
312k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
193
312k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
194
312k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
195
312k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
196
312k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
197
312k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
198
312k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
199
312k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
200
312k
    const size_t N = Lanes(d);
201
312k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
202
1.54M
      for (; x < chin_residual.w - 9; x += 8) {
203
1.33M
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
204
1.33M
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
205
11.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
206
10.6M
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
207
10.6M
        }
208
11.9M
        for (size_t i = 0; i < 8; i++) {
209
10.6M
          FastUnsqueeze(
210
10.6M
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
211
10.6M
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
212
10.6M
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
213
10.6M
        }
214
215
1.33M
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
216
1.33M
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
217
11.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
218
21.2M
          for (size_t i = 0; i < kRowsPerThread; i += N) {
219
10.6M
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
220
10.6M
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
221
10.6M
            StoreInterleaved(d, even, odd,
222
10.6M
                             p_out + ((x + i) << 1) + onerow_out * y);
223
10.6M
          }
224
10.6M
        }
225
1.33M
      }
226
215k
    }
227
312k
#endif  // HWY_TARGET != HWY_SCALAR
228
2.60M
    for (size_t y = 0; y < rows; y++) {
229
2.29M
      unsqueeze_row(y0 + y, x);
230
2.29M
    }
231
312k
    return true;
232
312k
  };
Unexecuted instantiation: squeeze.cc:jxl::N_SSE4::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_1::operator()(unsigned int, unsigned long) const
squeeze.cc:jxl::N_AVX2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_1::operator()(unsigned int, unsigned long) const
Line
Count
Source
182
312k
                                  size_t /* thread */) -> Status {
183
312k
    const size_t y0 = task * kRowsPerThread;
184
312k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
185
312k
    size_t x = 0;
186
187
312k
#if HWY_TARGET != HWY_SCALAR
188
312k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
189
312k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
190
312k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
191
312k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
192
312k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
193
312k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
194
312k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
195
312k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
196
312k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
197
312k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
198
312k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
199
312k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
200
312k
    const size_t N = Lanes(d);
201
312k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
202
1.54M
      for (; x < chin_residual.w - 9; x += 8) {
203
1.33M
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
204
1.33M
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
205
11.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
206
10.6M
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
207
10.6M
        }
208
11.9M
        for (size_t i = 0; i < 8; i++) {
209
10.6M
          FastUnsqueeze(
210
10.6M
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
211
10.6M
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
212
10.6M
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
213
10.6M
        }
214
215
1.33M
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
216
1.33M
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
217
11.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
218
21.2M
          for (size_t i = 0; i < kRowsPerThread; i += N) {
219
10.6M
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
220
10.6M
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
221
10.6M
            StoreInterleaved(d, even, odd,
222
10.6M
                             p_out + ((x + i) << 1) + onerow_out * y);
223
10.6M
          }
224
10.6M
        }
225
1.33M
      }
226
215k
    }
227
312k
#endif  // HWY_TARGET != HWY_SCALAR
228
2.60M
    for (size_t y = 0; y < rows; y++) {
229
2.29M
      unsqueeze_row(y0 + y, x);
230
2.29M
    }
231
312k
    return true;
232
312k
  };
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_1::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3_ZEN4::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_1::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3_SPR::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_1::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_SSE2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_1::operator()(unsigned int, unsigned long) const
233
66.0k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.h, kRowsPerThread),
234
66.0k
                                ThreadPool::NoInit, unsqueeze_span,
235
66.0k
                                "InvHorizontalSqueeze"));
236
66.0k
  input.channel[c] = std::move(chout);
237
66.0k
  return true;
238
66.0k
}
Unexecuted instantiation: jxl::N_SSE4::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
jxl::N_AVX2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Line
Count
Source
127
66.9k
Status InvHSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
128
66.9k
  JXL_ENSURE(c < input.channel.size());
129
66.9k
  JXL_ENSURE(rc < input.channel.size());
130
66.9k
  Channel &chin = input.channel[c];
131
66.9k
  const Channel &chin_residual = input.channel[rc];
132
  // These must be valid since we ran MetaApply already.
133
66.9k
  JXL_ENSURE(chin.w == DivCeil(chin.w + chin_residual.w, 2));
134
66.9k
  JXL_ENSURE(chin.h == chin_residual.h);
135
66.9k
  JxlMemoryManager *memory_manager = input.memory_manager();
136
137
66.9k
  if (chin_residual.w == 0) {
138
    // Short-circuit: output channel has same dimensions as input.
139
908
    input.channel[c].hshift--;
140
908
    return true;
141
908
  }
142
143
  // Note: chin.w >= chin_residual.w and at most 1 different.
144
132k
  JXL_ASSIGN_OR_RETURN(Channel chout,
145
132k
                       Channel::Create(memory_manager, chin.w + chin_residual.w,
146
132k
                                       chin.h, chin.hshift - 1, chin.vshift));
147
132k
  JXL_DEBUG_V(4,
148
132k
              "Undoing horizontal squeeze of channel %i using residuals in "
149
132k
              "channel %i (going from width %" PRIuS " to %" PRIuS ")",
150
132k
              c, rc, chin.w, chout.w);
151
152
132k
  if (chin_residual.h == 0) {
153
    // Short-circuit: channel with no pixels.
154
0
    input.channel[c] = std::move(chout);
155
0
    return true;
156
0
  }
157
66.0k
  auto unsqueeze_row = [&](size_t y, size_t x0) {
158
66.0k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
159
66.0k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
160
66.0k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
161
66.0k
    for (size_t x = x0; x < chin_residual.w; x++) {
162
66.0k
      pixel_type_w diff_minus_tendency = p_residual[x];
163
66.0k
      pixel_type_w avg = p_avg[x];
164
66.0k
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
165
66.0k
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
166
66.0k
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
167
66.0k
      pixel_type_w diff = diff_minus_tendency + tendency;
168
66.0k
      pixel_type_w A = avg + (diff / 2);
169
66.0k
      p_out[(x << 1)] = A;
170
66.0k
      pixel_type_w B = A - diff;
171
66.0k
      p_out[(x << 1) + 1] = B;
172
66.0k
    }
173
66.0k
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
174
66.0k
  };
175
176
  // somewhat complicated trickery just to be able to SIMD this.
177
  // Horizontal unsqueeze has horizontal data dependencies, so we do
178
  // 8 rows at a time and treat it as a vertical unsqueeze of a
179
  // transposed 8x8 block (or 9x8 for one input).
180
66.0k
  static constexpr const size_t kRowsPerThread = 8;
181
66.0k
  const auto unsqueeze_span = [&](const uint32_t task,
182
66.0k
                                  size_t /* thread */) -> Status {
183
66.0k
    const size_t y0 = task * kRowsPerThread;
184
66.0k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
185
66.0k
    size_t x = 0;
186
187
66.0k
#if HWY_TARGET != HWY_SCALAR
188
66.0k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
189
66.0k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
190
66.0k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
191
66.0k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
192
66.0k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
193
66.0k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
194
66.0k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
195
66.0k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
196
66.0k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
197
66.0k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
198
66.0k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
199
66.0k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
200
66.0k
    const size_t N = Lanes(d);
201
66.0k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
202
66.0k
      for (; x < chin_residual.w - 9; x += 8) {
203
66.0k
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
204
66.0k
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
205
66.0k
        for (size_t y = 0; y < kRowsPerThread; y++) {
206
66.0k
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
207
66.0k
        }
208
66.0k
        for (size_t i = 0; i < 8; i++) {
209
66.0k
          FastUnsqueeze(
210
66.0k
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
211
66.0k
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
212
66.0k
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
213
66.0k
        }
214
215
66.0k
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
216
66.0k
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
217
66.0k
        for (size_t y = 0; y < kRowsPerThread; y++) {
218
66.0k
          for (size_t i = 0; i < kRowsPerThread; i += N) {
219
66.0k
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
220
66.0k
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
221
66.0k
            StoreInterleaved(d, even, odd,
222
66.0k
                             p_out + ((x + i) << 1) + onerow_out * y);
223
66.0k
          }
224
66.0k
        }
225
66.0k
      }
226
66.0k
    }
227
66.0k
#endif  // HWY_TARGET != HWY_SCALAR
228
66.0k
    for (size_t y = 0; y < rows; y++) {
229
66.0k
      unsqueeze_row(y0 + y, x);
230
66.0k
    }
231
66.0k
    return true;
232
66.0k
  };
233
66.0k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.h, kRowsPerThread),
234
66.0k
                                ThreadPool::NoInit, unsqueeze_span,
235
66.0k
                                "InvHorizontalSqueeze"));
236
66.0k
  input.channel[c] = std::move(chout);
237
66.0k
  return true;
238
66.0k
}
Unexecuted instantiation: jxl::N_AVX3::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_AVX3_ZEN4::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_AVX3_SPR::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_SSE2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
239
240
87.6k
Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
241
87.6k
  JXL_ENSURE(c < input.channel.size());
242
87.6k
  JXL_ENSURE(rc < input.channel.size());
243
87.6k
  const Channel &chin = input.channel[c];
244
87.6k
  const Channel &chin_residual = input.channel[rc];
245
  // These must be valid since we ran MetaApply already.
246
87.6k
  JXL_ENSURE(chin.h == DivCeil(chin.h + chin_residual.h, 2));
247
87.6k
  JXL_ENSURE(chin.w == chin_residual.w);
248
87.6k
  JxlMemoryManager *memory_manager = input.memory_manager();
249
250
87.6k
  if (chin_residual.h == 0) {
251
    // Short-circuit: output channel has same dimensions as input.
252
23.5k
    input.channel[c].vshift--;
253
23.5k
    return true;
254
23.5k
  }
255
256
  // Note: chin.h >= chin_residual.h and at most 1 different.
257
128k
  JXL_ASSIGN_OR_RETURN(
258
128k
      Channel chout,
259
128k
      Channel::Create(memory_manager, chin.w, chin.h + chin_residual.h,
260
128k
                      chin.hshift, chin.vshift - 1));
261
128k
  JXL_DEBUG_V(
262
128k
      4,
263
128k
      "Undoing vertical squeeze of channel %i using residuals in channel "
264
128k
      "%i (going from height %" PRIuS " to %" PRIuS ")",
265
128k
      c, rc, chin.h, chout.h);
266
267
128k
  if (chin_residual.w == 0) {
268
    // Short-circuit: channel with no pixels.
269
0
    input.channel[c] = std::move(chout);
270
0
    return true;
271
0
  }
272
273
64.1k
  static constexpr const int kColsPerThread = 64;
274
64.1k
  const auto unsqueeze_slice = [&](const uint32_t task,
275
71.4k
                                   size_t /* thread */) -> Status {
276
71.4k
    const size_t x0 = task * kColsPerThread;
277
71.4k
    const size_t x1 =
278
71.4k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
279
71.4k
    const size_t w = x1 - x0;
280
    // We only iterate up to std::min(chin_residual.h, chin.h) which is
281
    // always chin_residual.h.
282
2.07M
    for (size_t y = 0; y < chin_residual.h; y++) {
283
2.00M
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
284
2.00M
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
285
2.00M
      const pixel_type *JXL_RESTRICT p_navg =
286
2.00M
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
287
2.00M
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
288
2.00M
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
289
2.00M
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
290
2.00M
      size_t x = 0;
291
2.00M
#if HWY_TARGET != HWY_SCALAR
292
12.0M
      for (; x + 7 < w; x += 8) {
293
10.0M
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
294
10.0M
                      p_out + x, p_nout + x);
295
10.0M
      }
296
2.00M
#endif
297
6.04M
      for (; x < w; x++) {
298
4.04M
        pixel_type_w avg = p_avg[x];
299
4.04M
        pixel_type_w next_avg = p_navg[x];
300
4.04M
        pixel_type_w top = p_pout[x];
301
4.04M
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
302
4.04M
        pixel_type_w diff_minus_tendency = p_residual[x];
303
4.04M
        pixel_type_w diff = diff_minus_tendency + tendency;
304
4.04M
        pixel_type_w out = avg + (diff / 2);
305
4.04M
        p_out[x] = out;
306
        // If the chin_residual.h == chin.h, the output has an even number
307
        // of rows so the next line is fine. Otherwise, this loop won't
308
        // write to the last output row which is handled separately.
309
4.04M
        p_nout[x] = out - diff;
310
4.04M
      }
311
2.00M
    }
312
71.4k
    return true;
313
71.4k
  };
Unexecuted instantiation: squeeze.cc:jxl::N_SSE4::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
squeeze.cc:jxl::N_AVX2::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
Line
Count
Source
275
71.4k
                                   size_t /* thread */) -> Status {
276
71.4k
    const size_t x0 = task * kColsPerThread;
277
71.4k
    const size_t x1 =
278
71.4k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
279
71.4k
    const size_t w = x1 - x0;
280
    // We only iterate up to std::min(chin_residual.h, chin.h) which is
281
    // always chin_residual.h.
282
2.07M
    for (size_t y = 0; y < chin_residual.h; y++) {
283
2.00M
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
284
2.00M
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
285
2.00M
      const pixel_type *JXL_RESTRICT p_navg =
286
2.00M
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
287
2.00M
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
288
2.00M
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
289
2.00M
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
290
2.00M
      size_t x = 0;
291
2.00M
#if HWY_TARGET != HWY_SCALAR
292
12.0M
      for (; x + 7 < w; x += 8) {
293
10.0M
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
294
10.0M
                      p_out + x, p_nout + x);
295
10.0M
      }
296
2.00M
#endif
297
6.04M
      for (; x < w; x++) {
298
4.04M
        pixel_type_w avg = p_avg[x];
299
4.04M
        pixel_type_w next_avg = p_navg[x];
300
4.04M
        pixel_type_w top = p_pout[x];
301
4.04M
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
302
4.04M
        pixel_type_w diff_minus_tendency = p_residual[x];
303
4.04M
        pixel_type_w diff = diff_minus_tendency + tendency;
304
4.04M
        pixel_type_w out = avg + (diff / 2);
305
4.04M
        p_out[x] = out;
306
        // If the chin_residual.h == chin.h, the output has an even number
307
        // of rows so the next line is fine. Otherwise, this loop won't
308
        // write to the last output row which is handled separately.
309
4.04M
        p_nout[x] = out - diff;
310
4.04M
      }
311
2.00M
    }
312
71.4k
    return true;
313
71.4k
  };
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3_ZEN4::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_AVX3_SPR::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: squeeze.cc:jxl::N_SSE2::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
314
64.1k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.w, kColsPerThread),
315
64.1k
                                ThreadPool::NoInit, unsqueeze_slice,
316
64.1k
                                "InvVertSqueeze"));
317
318
64.1k
  if (chout.h & 1) {
319
31.1k
    size_t y = chin.h - 1;
320
31.1k
    const pixel_type *p_avg = chin.Row(y);
321
31.1k
    pixel_type *p_out = chout.Row(y << 1);
322
1.04M
    for (size_t x = 0; x < chin.w; x++) {
323
1.00M
      p_out[x] = p_avg[x];
324
1.00M
    }
325
31.1k
  }
326
64.1k
  input.channel[c] = std::move(chout);
327
64.1k
  return true;
328
64.1k
}
Unexecuted instantiation: jxl::N_SSE4::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
jxl::N_AVX2::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Line
Count
Source
240
87.6k
Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
241
87.6k
  JXL_ENSURE(c < input.channel.size());
242
87.6k
  JXL_ENSURE(rc < input.channel.size());
243
87.6k
  const Channel &chin = input.channel[c];
244
87.6k
  const Channel &chin_residual = input.channel[rc];
245
  // These must be valid since we ran MetaApply already.
246
87.6k
  JXL_ENSURE(chin.h == DivCeil(chin.h + chin_residual.h, 2));
247
87.6k
  JXL_ENSURE(chin.w == chin_residual.w);
248
87.6k
  JxlMemoryManager *memory_manager = input.memory_manager();
249
250
87.6k
  if (chin_residual.h == 0) {
251
    // Short-circuit: output channel has same dimensions as input.
252
23.5k
    input.channel[c].vshift--;
253
23.5k
    return true;
254
23.5k
  }
255
256
  // Note: chin.h >= chin_residual.h and at most 1 different.
257
128k
  JXL_ASSIGN_OR_RETURN(
258
128k
      Channel chout,
259
128k
      Channel::Create(memory_manager, chin.w, chin.h + chin_residual.h,
260
128k
                      chin.hshift, chin.vshift - 1));
261
128k
  JXL_DEBUG_V(
262
128k
      4,
263
128k
      "Undoing vertical squeeze of channel %i using residuals in channel "
264
128k
      "%i (going from height %" PRIuS " to %" PRIuS ")",
265
128k
      c, rc, chin.h, chout.h);
266
267
128k
  if (chin_residual.w == 0) {
268
    // Short-circuit: channel with no pixels.
269
0
    input.channel[c] = std::move(chout);
270
0
    return true;
271
0
  }
272
273
64.1k
  static constexpr const int kColsPerThread = 64;
274
64.1k
  const auto unsqueeze_slice = [&](const uint32_t task,
275
64.1k
                                   size_t /* thread */) -> Status {
276
64.1k
    const size_t x0 = task * kColsPerThread;
277
64.1k
    const size_t x1 =
278
64.1k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
279
64.1k
    const size_t w = x1 - x0;
280
    // We only iterate up to std::min(chin_residual.h, chin.h) which is
281
    // always chin_residual.h.
282
64.1k
    for (size_t y = 0; y < chin_residual.h; y++) {
283
64.1k
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
284
64.1k
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
285
64.1k
      const pixel_type *JXL_RESTRICT p_navg =
286
64.1k
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
287
64.1k
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
288
64.1k
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
289
64.1k
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
290
64.1k
      size_t x = 0;
291
64.1k
#if HWY_TARGET != HWY_SCALAR
292
64.1k
      for (; x + 7 < w; x += 8) {
293
64.1k
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
294
64.1k
                      p_out + x, p_nout + x);
295
64.1k
      }
296
64.1k
#endif
297
64.1k
      for (; x < w; x++) {
298
64.1k
        pixel_type_w avg = p_avg[x];
299
64.1k
        pixel_type_w next_avg = p_navg[x];
300
64.1k
        pixel_type_w top = p_pout[x];
301
64.1k
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
302
64.1k
        pixel_type_w diff_minus_tendency = p_residual[x];
303
64.1k
        pixel_type_w diff = diff_minus_tendency + tendency;
304
64.1k
        pixel_type_w out = avg + (diff / 2);
305
64.1k
        p_out[x] = out;
306
        // If the chin_residual.h == chin.h, the output has an even number
307
        // of rows so the next line is fine. Otherwise, this loop won't
308
        // write to the last output row which is handled separately.
309
64.1k
        p_nout[x] = out - diff;
310
64.1k
      }
311
64.1k
    }
312
64.1k
    return true;
313
64.1k
  };
314
64.1k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.w, kColsPerThread),
315
64.1k
                                ThreadPool::NoInit, unsqueeze_slice,
316
64.1k
                                "InvVertSqueeze"));
317
318
64.1k
  if (chout.h & 1) {
319
31.1k
    size_t y = chin.h - 1;
320
31.1k
    const pixel_type *p_avg = chin.Row(y);
321
31.1k
    pixel_type *p_out = chout.Row(y << 1);
322
1.04M
    for (size_t x = 0; x < chin.w; x++) {
323
1.00M
      p_out[x] = p_avg[x];
324
1.00M
    }
325
31.1k
  }
326
64.1k
  input.channel[c] = std::move(chout);
327
64.1k
  return true;
328
64.1k
}
Unexecuted instantiation: jxl::N_AVX3::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_AVX3_ZEN4::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_AVX3_SPR::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_SSE2::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
329
330
Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
331
11.3k
                  ThreadPool *pool) {
332
81.2k
  for (int i = parameters.size() - 1; i >= 0; i--) {
333
69.9k
    JXL_RETURN_IF_ERROR(
334
69.9k
        CheckMetaSqueezeParams(parameters[i], input.channel.size()));
335
69.9k
    bool horizontal = parameters[i].horizontal;
336
69.9k
    bool in_place = parameters[i].in_place;
337
69.9k
    uint32_t beginc = parameters[i].begin_c;
338
69.9k
    uint32_t endc = parameters[i].begin_c + parameters[i].num_c - 1;
339
69.9k
    uint32_t offset;
340
69.9k
    if (in_place) {
341
35.0k
      offset = endc + 1;
342
35.0k
    } else {
343
34.8k
      offset = input.channel.size() + beginc - endc - 1;
344
34.8k
    }
345
69.9k
    if (beginc < input.nb_meta_channels) {
346
      // This is checked in MetaSqueeze.
347
3
      JXL_ENSURE(input.nb_meta_channels > parameters[i].num_c);
348
3
      input.nb_meta_channels -= parameters[i].num_c;
349
3
    }
350
351
224k
    for (uint32_t c = beginc; c <= endc; c++) {
352
154k
      uint32_t rc = offset + c - beginc;
353
      // MetaApply should imply that `rc` is within range, otherwise there's a
354
      // programming bug.
355
154k
      JXL_ENSURE(rc < input.channel.size());
356
154k
      if ((input.channel[c].w < input.channel[rc].w) ||
357
154k
          (input.channel[c].h < input.channel[rc].h)) {
358
0
        return JXL_FAILURE("Corrupted squeeze transform");
359
0
      }
360
154k
      if (horizontal) {
361
66.9k
        JXL_RETURN_IF_ERROR(InvHSqueeze(input, c, rc, pool));
362
87.6k
      } else {
363
87.6k
        JXL_RETURN_IF_ERROR(InvVSqueeze(input, c, rc, pool));
364
87.6k
      }
365
154k
    }
366
69.9k
    input.channel.erase(input.channel.begin() + offset,
367
69.9k
                        input.channel.begin() + offset + (endc - beginc + 1));
368
69.9k
  }
369
11.3k
  return true;
370
11.3k
}
Unexecuted instantiation: jxl::N_SSE4::InvSqueeze(jxl::Image&, std::__1::vector<jxl::SqueezeParams, std::__1::allocator<jxl::SqueezeParams> > const&, jxl::ThreadPool*)
jxl::N_AVX2::InvSqueeze(jxl::Image&, std::__1::vector<jxl::SqueezeParams, std::__1::allocator<jxl::SqueezeParams> > const&, jxl::ThreadPool*)
Line
Count
Source
331
11.3k
                  ThreadPool *pool) {
332
81.2k
  for (int i = parameters.size() - 1; i >= 0; i--) {
333
69.9k
    JXL_RETURN_IF_ERROR(
334
69.9k
        CheckMetaSqueezeParams(parameters[i], input.channel.size()));
335
69.9k
    bool horizontal = parameters[i].horizontal;
336
69.9k
    bool in_place = parameters[i].in_place;
337
69.9k
    uint32_t beginc = parameters[i].begin_c;
338
69.9k
    uint32_t endc = parameters[i].begin_c + parameters[i].num_c - 1;
339
69.9k
    uint32_t offset;
340
69.9k
    if (in_place) {
341
35.0k
      offset = endc + 1;
342
35.0k
    } else {
343
34.8k
      offset = input.channel.size() + beginc - endc - 1;
344
34.8k
    }
345
69.9k
    if (beginc < input.nb_meta_channels) {
346
      // This is checked in MetaSqueeze.
347
3
      JXL_ENSURE(input.nb_meta_channels > parameters[i].num_c);
348
3
      input.nb_meta_channels -= parameters[i].num_c;
349
3
    }
350
351
224k
    for (uint32_t c = beginc; c <= endc; c++) {
352
154k
      uint32_t rc = offset + c - beginc;
353
      // MetaApply should imply that `rc` is within range, otherwise there's a
354
      // programming bug.
355
154k
      JXL_ENSURE(rc < input.channel.size());
356
154k
      if ((input.channel[c].w < input.channel[rc].w) ||
357
154k
          (input.channel[c].h < input.channel[rc].h)) {
358
0
        return JXL_FAILURE("Corrupted squeeze transform");
359
0
      }
360
154k
      if (horizontal) {
361
66.9k
        JXL_RETURN_IF_ERROR(InvHSqueeze(input, c, rc, pool));
362
87.6k
      } else {
363
87.6k
        JXL_RETURN_IF_ERROR(InvVSqueeze(input, c, rc, pool));
364
87.6k
      }
365
154k
    }
366
69.9k
    input.channel.erase(input.channel.begin() + offset,
367
69.9k
                        input.channel.begin() + offset + (endc - beginc + 1));
368
69.9k
  }
369
11.3k
  return true;
370
11.3k
}
Unexecuted instantiation: jxl::N_AVX3::InvSqueeze(jxl::Image&, std::__1::vector<jxl::SqueezeParams, std::__1::allocator<jxl::SqueezeParams> > const&, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_AVX3_ZEN4::InvSqueeze(jxl::Image&, std::__1::vector<jxl::SqueezeParams, std::__1::allocator<jxl::SqueezeParams> > const&, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_AVX3_SPR::InvSqueeze(jxl::Image&, std::__1::vector<jxl::SqueezeParams, std::__1::allocator<jxl::SqueezeParams> > const&, jxl::ThreadPool*)
Unexecuted instantiation: jxl::N_SSE2::InvSqueeze(jxl::Image&, std::__1::vector<jxl::SqueezeParams, std::__1::allocator<jxl::SqueezeParams> > const&, jxl::ThreadPool*)
371
372
}  // namespace HWY_NAMESPACE
373
}  // namespace jxl
374
HWY_AFTER_NAMESPACE();
375
376
#if HWY_ONCE
377
378
namespace jxl {
379
380
HWY_EXPORT(InvSqueeze);
381
Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
382
11.3k
                  ThreadPool *pool) {
383
11.3k
  return HWY_DYNAMIC_DISPATCH(InvSqueeze)(input, parameters, pool);
384
11.3k
}
385
386
void DefaultSqueezeParameters(std::vector<SqueezeParams> *parameters,
387
10.5k
                              const Image &image) {
388
10.5k
  int nb_channels = image.channel.size() - image.nb_meta_channels;
389
390
10.5k
  parameters->clear();
391
10.5k
  size_t w = image.channel[image.nb_meta_channels].w;
392
10.5k
  size_t h = image.channel[image.nb_meta_channels].h;
393
10.5k
  JXL_DEBUG_V(
394
10.5k
      7, "Default squeeze parameters for %" PRIuS "x%" PRIuS " image: ", w, h);
395
396
  // do horizontal first on wide images; vertical first on tall images
397
10.5k
  bool wide = (w > h);
398
399
10.5k
  if (nb_channels > 2 && image.channel[image.nb_meta_channels + 1].w == w &&
400
7.12k
      image.channel[image.nb_meta_channels + 1].h == h) {
401
    // assume channels 1 and 2 are chroma, and can be squeezed first for 4:2:0
402
    // previews
403
6.85k
    JXL_DEBUG_V(7, "(4:2:0 chroma), %" PRIuS "x%" PRIuS " image", w, h);
404
6.85k
    SqueezeParams params;
405
    // horizontal chroma squeeze
406
6.85k
    params.horizontal = true;
407
6.85k
    params.in_place = false;
408
6.85k
    params.begin_c = image.nb_meta_channels + 1;
409
6.85k
    params.num_c = 2;
410
6.85k
    parameters->push_back(params);
411
6.85k
    params.horizontal = false;
412
    // vertical chroma squeeze
413
6.85k
    parameters->push_back(params);
414
6.85k
  }
415
10.5k
  SqueezeParams params;
416
10.5k
  params.begin_c = image.nb_meta_channels;
417
10.5k
  params.num_c = nb_channels;
418
10.5k
  params.in_place = true;
419
420
10.5k
  if (!wide) {
421
6.40k
    if (h > kMaxFirstPreviewSize) {
422
2.98k
      params.horizontal = false;
423
2.98k
      parameters->push_back(params);
424
2.98k
      h = (h + 1) / 2;
425
2.98k
      JXL_DEBUG_V(7, "Vertical (%" PRIuS "x%" PRIuS "), ", w, h);
426
2.98k
    }
427
6.40k
  }
428
32.2k
  while (w > kMaxFirstPreviewSize || h > kMaxFirstPreviewSize) {
429
21.6k
    if (w > kMaxFirstPreviewSize) {
430
20.9k
      params.horizontal = true;
431
20.9k
      parameters->push_back(params);
432
20.9k
      w = (w + 1) / 2;
433
20.9k
      JXL_DEBUG_V(7, "Horizontal (%" PRIuS "x%" PRIuS "), ", w, h);
434
20.9k
    }
435
21.6k
    if (h > kMaxFirstPreviewSize) {
436
16.9k
      params.horizontal = false;
437
16.9k
      parameters->push_back(params);
438
16.9k
      h = (h + 1) / 2;
439
16.9k
      JXL_DEBUG_V(7, "Vertical (%" PRIuS "x%" PRIuS "), ", w, h);
440
16.9k
    }
441
21.6k
  }
442
10.5k
  JXL_DEBUG_V(7, "that's it");
443
10.5k
}
444
445
Status CheckMetaSqueezeParams(const SqueezeParams &parameter,
446
150k
                              int num_channels) {
447
150k
  int c1 = parameter.begin_c;
448
150k
  int c2 = parameter.begin_c + parameter.num_c - 1;
449
150k
  if (c1 < 0 || c1 >= num_channels || c2 < 0 || c2 >= num_channels || c2 < c1) {
450
159
    return JXL_FAILURE("Invalid channel range");
451
159
  }
452
150k
  return true;
453
150k
}
454
455
13.5k
Status MetaSqueeze(Image &image, std::vector<SqueezeParams> *parameters) {
456
13.5k
  JxlMemoryManager *memory_manager = image.memory_manager();
457
13.5k
  if (parameters->empty()) {
458
10.5k
    DefaultSqueezeParameters(parameters, image);
459
10.5k
  }
460
461
80.6k
  for (auto &parameter : *parameters) {
462
80.6k
    JXL_RETURN_IF_ERROR(
463
80.6k
        CheckMetaSqueezeParams(parameter, image.channel.size()));
464
80.4k
    bool horizontal = parameter.horizontal;
465
80.4k
    bool in_place = parameter.in_place;
466
80.4k
    uint32_t beginc = parameter.begin_c;
467
80.4k
    uint32_t endc = parameter.begin_c + parameter.num_c - 1;
468
469
80.4k
    uint32_t offset;
470
80.4k
    if (beginc < image.nb_meta_channels) {
471
40
      if (endc >= image.nb_meta_channels) {
472
5
        return JXL_FAILURE("Invalid squeeze: mix of meta and nonmeta channels");
473
5
      }
474
35
      if (!in_place) {
475
8
        return JXL_FAILURE(
476
8
            "Invalid squeeze: meta channels require in-place residuals");
477
8
      }
478
27
      image.nb_meta_channels += parameter.num_c;
479
27
    }
480
80.4k
    if (in_place) {
481
41.1k
      offset = endc + 1;
482
41.1k
    } else {
483
39.3k
      offset = image.channel.size();
484
39.3k
    }
485
268k
    for (uint32_t c = beginc; c <= endc; c++) {
486
188k
      if (image.channel[c].hshift > 30 || image.channel[c].vshift > 30) {
487
6
        return JXL_FAILURE("Too many squeezes: shift > 30");
488
6
      }
489
188k
      size_t w = image.channel[c].w;
490
188k
      size_t h = image.channel[c].h;
491
188k
      if (w == 0 || h == 0) return JXL_FAILURE("Squeezing empty channel");
492
188k
      if (horizontal) {
493
83.2k
        image.channel[c].w = (w + 1) / 2;
494
83.2k
        if (image.channel[c].hshift >= 0) image.channel[c].hshift++;
495
83.2k
        w = w - (w + 1) / 2;
496
104k
      } else {
497
104k
        image.channel[c].h = (h + 1) / 2;
498
104k
        if (image.channel[c].vshift >= 0) image.channel[c].vshift++;
499
104k
        h = h - (h + 1) / 2;
500
104k
      }
501
188k
      JXL_RETURN_IF_ERROR(image.channel[c].shrink());
502
376k
      JXL_ASSIGN_OR_RETURN(Channel placeholder,
503
376k
                           Channel::Create(memory_manager, w, h));
504
376k
      placeholder.hshift = image.channel[c].hshift;
505
376k
      placeholder.vshift = image.channel[c].vshift;
506
376k
      placeholder.component = image.channel[c].component;
507
376k
      image.channel.insert(image.channel.begin() + offset + (c - beginc),
508
376k
                           std::move(placeholder));
509
376k
      JXL_DEBUG_V(0, "MetaSqueeze applied, current image: %s",
510
376k
                  image.DebugString().c_str());
511
376k
    }
512
80.4k
  }
513
13.3k
  return true;
514
13.5k
}
515
516
}  // namespace jxl
517
518
#endif