Coverage Report

Created: 2025-12-31 07:53

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
18.9M
                              pixel_type *p_nout) {
68
18.9M
  const size_t N = Lanes(d);
69
18.9M
  auto onethird = Set(d, 0x55555556);
70
37.8M
  for (size_t x = 0; x < 8; x += N) {
71
18.9M
    auto avg = Load(d, p_avg + x);
72
18.9M
    auto next_avg = Load(d, p_navg + x);
73
18.9M
    auto top = Load(d, p_pout + x);
74
    // Equivalent to SmoothTendency(top,avg,next_avg), but without branches
75
    // typo:off
76
18.9M
    auto Ba = Sub(top, avg);
77
18.9M
    auto an = Sub(avg, next_avg);
78
18.9M
    auto nonmono = Xor(Ba, an);
79
18.9M
    auto absBa = Abs(Ba);
80
18.9M
    auto absan = Abs(an);
81
18.9M
    auto absBn = Abs(Sub(top, next_avg));
82
    // Compute a3 = absBa / 3
83
18.9M
    auto a3eh = MulEven(absBa, onethird);
84
18.9M
    auto a3oh = MulOdd(absBa, onethird);
85
86
18.9M
#if (HWY_MAJOR > 1 || (HWY_MAJOR == 1 && HWY_MINOR >= 2))
87
18.9M
#if HWY_IS_LITTLE_ENDIAN
88
18.9M
    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
18.9M
    a3 = Add(a3, Add(absBn, Set(d, 2)));
101
18.9M
    auto absdiff = ShiftRight<2>(a3);
102
18.9M
    auto skipdiff = Ne(Ba, Zero(d));
103
18.9M
    skipdiff = And(skipdiff, Ne(an, Zero(d)));
104
18.9M
    skipdiff = And(skipdiff, Lt(nonmono, Zero(d)));
105
18.9M
    auto absBa2 = Add(ShiftLeft<1>(absBa), And(absdiff, Set(d, 1)));
106
18.9M
    absdiff = IfThenElse(Gt(absdiff, absBa2),
107
18.9M
                         Add(ShiftLeft<1>(absBa), Set(d, 1)), absdiff);
108
    // typo:on
109
18.9M
    auto absan2 = ShiftLeft<1>(absan);
110
18.9M
    absdiff = IfThenElse(Gt(Add(absdiff, And(absdiff, Set(d, 1))), absan2),
111
18.9M
                         absan2, absdiff);
112
18.9M
    auto diff1 = IfThenElse(Lt(top, next_avg), Neg(absdiff), absdiff);
113
18.9M
    auto tendency = IfThenZeroElse(skipdiff, diff1);
114
115
18.9M
    auto diff_minus_tendency = Load(d, p_residual + x);
116
18.9M
    auto diff = Add(diff_minus_tendency, tendency);
117
18.9M
    auto out =
118
18.9M
        Add(avg, ShiftRight<1>(
119
18.9M
                     Add(diff, BitCast(d, ShiftRight<31>(BitCast(du, diff))))));
120
18.9M
    Store(out, d, p_out + x);
121
18.9M
    Store(Sub(out, diff), d, p_nout + x);
122
18.9M
  }
123
18.9M
}
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
18.9M
                              pixel_type *p_nout) {
68
18.9M
  const size_t N = Lanes(d);
69
18.9M
  auto onethird = Set(d, 0x55555556);
70
37.8M
  for (size_t x = 0; x < 8; x += N) {
71
18.9M
    auto avg = Load(d, p_avg + x);
72
18.9M
    auto next_avg = Load(d, p_navg + x);
73
18.9M
    auto top = Load(d, p_pout + x);
74
    // Equivalent to SmoothTendency(top,avg,next_avg), but without branches
75
    // typo:off
76
18.9M
    auto Ba = Sub(top, avg);
77
18.9M
    auto an = Sub(avg, next_avg);
78
18.9M
    auto nonmono = Xor(Ba, an);
79
18.9M
    auto absBa = Abs(Ba);
80
18.9M
    auto absan = Abs(an);
81
18.9M
    auto absBn = Abs(Sub(top, next_avg));
82
    // Compute a3 = absBa / 3
83
18.9M
    auto a3eh = MulEven(absBa, onethird);
84
18.9M
    auto a3oh = MulOdd(absBa, onethird);
85
86
18.9M
#if (HWY_MAJOR > 1 || (HWY_MAJOR == 1 && HWY_MINOR >= 2))
87
18.9M
#if HWY_IS_LITTLE_ENDIAN
88
18.9M
    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
18.9M
    a3 = Add(a3, Add(absBn, Set(d, 2)));
101
18.9M
    auto absdiff = ShiftRight<2>(a3);
102
18.9M
    auto skipdiff = Ne(Ba, Zero(d));
103
18.9M
    skipdiff = And(skipdiff, Ne(an, Zero(d)));
104
18.9M
    skipdiff = And(skipdiff, Lt(nonmono, Zero(d)));
105
18.9M
    auto absBa2 = Add(ShiftLeft<1>(absBa), And(absdiff, Set(d, 1)));
106
18.9M
    absdiff = IfThenElse(Gt(absdiff, absBa2),
107
18.9M
                         Add(ShiftLeft<1>(absBa), Set(d, 1)), absdiff);
108
    // typo:on
109
18.9M
    auto absan2 = ShiftLeft<1>(absan);
110
18.9M
    absdiff = IfThenElse(Gt(Add(absdiff, And(absdiff, Set(d, 1))), absan2),
111
18.9M
                         absan2, absdiff);
112
18.9M
    auto diff1 = IfThenElse(Lt(top, next_avg), Neg(absdiff), absdiff);
113
18.9M
    auto tendency = IfThenZeroElse(skipdiff, diff1);
114
115
18.9M
    auto diff_minus_tendency = Load(d, p_residual + x);
116
18.9M
    auto diff = Add(diff_minus_tendency, tendency);
117
18.9M
    auto out =
118
18.9M
        Add(avg, ShiftRight<1>(
119
18.9M
                     Add(diff, BitCast(d, ShiftRight<31>(BitCast(du, diff))))));
120
18.9M
    Store(out, d, p_out + x);
121
18.9M
    Store(Sub(out, diff), d, p_nout + x);
122
18.9M
  }
123
18.9M
}
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
63.8k
Status InvHSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
128
63.8k
  JXL_ENSURE(c < input.channel.size());
129
63.8k
  JXL_ENSURE(rc < input.channel.size());
130
63.8k
  Channel &chin = input.channel[c];
131
63.8k
  const Channel &chin_residual = input.channel[rc];
132
  // These must be valid since we ran MetaApply already.
133
63.8k
  JXL_ENSURE(chin.w == DivCeil(chin.w + chin_residual.w, 2));
134
63.8k
  JXL_ENSURE(chin.h == chin_residual.h);
135
63.8k
  JxlMemoryManager *memory_manager = input.memory_manager();
136
137
63.8k
  if (chin_residual.w == 0) {
138
    // Short-circuit: output channel has same dimensions as input.
139
893
    input.channel[c].hshift--;
140
893
    return true;
141
893
  }
142
143
  // Note: chin.w >= chin_residual.w and at most 1 different.
144
125k
  JXL_ASSIGN_OR_RETURN(Channel chout,
145
125k
                       Channel::Create(memory_manager, chin.w + chin_residual.w,
146
125k
                                       chin.h, chin.hshift - 1, chin.vshift));
147
125k
  JXL_DEBUG_V(4,
148
125k
              "Undoing horizontal squeeze of channel %i using residuals in "
149
125k
              "channel %i (going from width %" PRIuS " to %" PRIuS ")",
150
125k
              c, rc, chin.w, chout.w);
151
152
125k
  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.12M
  auto unsqueeze_row = [&](size_t y, size_t x0) {
158
2.12M
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
159
2.12M
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
160
2.12M
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
161
18.0M
    for (size_t x = x0; x < chin_residual.w; x++) {
162
15.9M
      pixel_type_w diff_minus_tendency = p_residual[x];
163
15.9M
      pixel_type_w avg = p_avg[x];
164
15.9M
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
165
15.9M
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
166
15.9M
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
167
15.9M
      pixel_type_w diff = diff_minus_tendency + tendency;
168
15.9M
      pixel_type_w A = avg + (diff / 2);
169
15.9M
      p_out[(x << 1)] = A;
170
15.9M
      pixel_type_w B = A - diff;
171
15.9M
      p_out[(x << 1) + 1] = B;
172
15.9M
    }
173
2.12M
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
174
2.12M
  };
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.12M
  auto unsqueeze_row = [&](size_t y, size_t x0) {
158
2.12M
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
159
2.12M
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
160
2.12M
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
161
18.0M
    for (size_t x = x0; x < chin_residual.w; x++) {
162
15.9M
      pixel_type_w diff_minus_tendency = p_residual[x];
163
15.9M
      pixel_type_w avg = p_avg[x];
164
15.9M
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
165
15.9M
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
166
15.9M
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
167
15.9M
      pixel_type_w diff = diff_minus_tendency + tendency;
168
15.9M
      pixel_type_w A = avg + (diff / 2);
169
15.9M
      p_out[(x << 1)] = A;
170
15.9M
      pixel_type_w B = A - diff;
171
15.9M
      p_out[(x << 1) + 1] = B;
172
15.9M
    }
173
2.12M
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
174
2.12M
  };
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
62.9k
  static constexpr const size_t kRowsPerThread = 8;
181
62.9k
  const auto unsqueeze_span = [&](const uint32_t task,
182
290k
                                  size_t /* thread */) -> Status {
183
290k
    const size_t y0 = task * kRowsPerThread;
184
290k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
185
290k
    size_t x = 0;
186
187
290k
#if HWY_TARGET != HWY_SCALAR
188
290k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
189
290k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
190
290k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
191
290k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
192
290k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
193
290k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
194
290k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
195
290k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
196
290k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
197
290k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
198
290k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
199
290k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
200
290k
    const size_t N = Lanes(d);
201
290k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
202
1.42M
      for (; x < chin_residual.w - 9; x += 8) {
203
1.22M
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
204
1.22M
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
205
10.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
206
9.77M
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
207
9.77M
        }
208
10.9M
        for (size_t i = 0; i < 8; i++) {
209
9.77M
          FastUnsqueeze(
210
9.77M
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
211
9.77M
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
212
9.77M
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
213
9.77M
        }
214
215
1.22M
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
216
1.22M
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
217
10.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
218
19.5M
          for (size_t i = 0; i < kRowsPerThread; i += N) {
219
9.77M
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
220
9.77M
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
221
9.77M
            StoreInterleaved(d, even, odd,
222
9.77M
                             p_out + ((x + i) << 1) + onerow_out * y);
223
9.77M
          }
224
9.77M
        }
225
1.22M
      }
226
199k
    }
227
290k
#endif  // HWY_TARGET != HWY_SCALAR
228
2.41M
    for (size_t y = 0; y < rows; y++) {
229
2.12M
      unsqueeze_row(y0 + y, x);
230
2.12M
    }
231
290k
    return true;
232
290k
  };
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
290k
                                  size_t /* thread */) -> Status {
183
290k
    const size_t y0 = task * kRowsPerThread;
184
290k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
185
290k
    size_t x = 0;
186
187
290k
#if HWY_TARGET != HWY_SCALAR
188
290k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
189
290k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
190
290k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
191
290k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
192
290k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
193
290k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
194
290k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
195
290k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
196
290k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
197
290k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
198
290k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
199
290k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
200
290k
    const size_t N = Lanes(d);
201
290k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
202
1.42M
      for (; x < chin_residual.w - 9; x += 8) {
203
1.22M
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
204
1.22M
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
205
10.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
206
9.77M
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
207
9.77M
        }
208
10.9M
        for (size_t i = 0; i < 8; i++) {
209
9.77M
          FastUnsqueeze(
210
9.77M
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
211
9.77M
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
212
9.77M
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
213
9.77M
        }
214
215
1.22M
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
216
1.22M
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
217
10.9M
        for (size_t y = 0; y < kRowsPerThread; y++) {
218
19.5M
          for (size_t i = 0; i < kRowsPerThread; i += N) {
219
9.77M
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
220
9.77M
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
221
9.77M
            StoreInterleaved(d, even, odd,
222
9.77M
                             p_out + ((x + i) << 1) + onerow_out * y);
223
9.77M
          }
224
9.77M
        }
225
1.22M
      }
226
199k
    }
227
290k
#endif  // HWY_TARGET != HWY_SCALAR
228
2.41M
    for (size_t y = 0; y < rows; y++) {
229
2.12M
      unsqueeze_row(y0 + y, x);
230
2.12M
    }
231
290k
    return true;
232
290k
  };
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
62.9k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.h, kRowsPerThread),
234
62.9k
                                ThreadPool::NoInit, unsqueeze_span,
235
62.9k
                                "InvHorizontalSqueeze"));
236
62.9k
  input.channel[c] = std::move(chout);
237
62.9k
  return true;
238
62.9k
}
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
63.8k
Status InvHSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
128
63.8k
  JXL_ENSURE(c < input.channel.size());
129
63.8k
  JXL_ENSURE(rc < input.channel.size());
130
63.8k
  Channel &chin = input.channel[c];
131
63.8k
  const Channel &chin_residual = input.channel[rc];
132
  // These must be valid since we ran MetaApply already.
133
63.8k
  JXL_ENSURE(chin.w == DivCeil(chin.w + chin_residual.w, 2));
134
63.8k
  JXL_ENSURE(chin.h == chin_residual.h);
135
63.8k
  JxlMemoryManager *memory_manager = input.memory_manager();
136
137
63.8k
  if (chin_residual.w == 0) {
138
    // Short-circuit: output channel has same dimensions as input.
139
893
    input.channel[c].hshift--;
140
893
    return true;
141
893
  }
142
143
  // Note: chin.w >= chin_residual.w and at most 1 different.
144
125k
  JXL_ASSIGN_OR_RETURN(Channel chout,
145
125k
                       Channel::Create(memory_manager, chin.w + chin_residual.w,
146
125k
                                       chin.h, chin.hshift - 1, chin.vshift));
147
125k
  JXL_DEBUG_V(4,
148
125k
              "Undoing horizontal squeeze of channel %i using residuals in "
149
125k
              "channel %i (going from width %" PRIuS " to %" PRIuS ")",
150
125k
              c, rc, chin.w, chout.w);
151
152
125k
  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
62.9k
  auto unsqueeze_row = [&](size_t y, size_t x0) {
158
62.9k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
159
62.9k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
160
62.9k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
161
62.9k
    for (size_t x = x0; x < chin_residual.w; x++) {
162
62.9k
      pixel_type_w diff_minus_tendency = p_residual[x];
163
62.9k
      pixel_type_w avg = p_avg[x];
164
62.9k
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
165
62.9k
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
166
62.9k
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
167
62.9k
      pixel_type_w diff = diff_minus_tendency + tendency;
168
62.9k
      pixel_type_w A = avg + (diff / 2);
169
62.9k
      p_out[(x << 1)] = A;
170
62.9k
      pixel_type_w B = A - diff;
171
62.9k
      p_out[(x << 1) + 1] = B;
172
62.9k
    }
173
62.9k
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
174
62.9k
  };
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
62.9k
  static constexpr const size_t kRowsPerThread = 8;
181
62.9k
  const auto unsqueeze_span = [&](const uint32_t task,
182
62.9k
                                  size_t /* thread */) -> Status {
183
62.9k
    const size_t y0 = task * kRowsPerThread;
184
62.9k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
185
62.9k
    size_t x = 0;
186
187
62.9k
#if HWY_TARGET != HWY_SCALAR
188
62.9k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
189
62.9k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
190
62.9k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
191
62.9k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
192
62.9k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
193
62.9k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
194
62.9k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
195
62.9k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
196
62.9k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
197
62.9k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
198
62.9k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
199
62.9k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
200
62.9k
    const size_t N = Lanes(d);
201
62.9k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
202
62.9k
      for (; x < chin_residual.w - 9; x += 8) {
203
62.9k
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
204
62.9k
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
205
62.9k
        for (size_t y = 0; y < kRowsPerThread; y++) {
206
62.9k
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
207
62.9k
        }
208
62.9k
        for (size_t i = 0; i < 8; i++) {
209
62.9k
          FastUnsqueeze(
210
62.9k
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
211
62.9k
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
212
62.9k
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
213
62.9k
        }
214
215
62.9k
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
216
62.9k
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
217
62.9k
        for (size_t y = 0; y < kRowsPerThread; y++) {
218
62.9k
          for (size_t i = 0; i < kRowsPerThread; i += N) {
219
62.9k
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
220
62.9k
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
221
62.9k
            StoreInterleaved(d, even, odd,
222
62.9k
                             p_out + ((x + i) << 1) + onerow_out * y);
223
62.9k
          }
224
62.9k
        }
225
62.9k
      }
226
62.9k
    }
227
62.9k
#endif  // HWY_TARGET != HWY_SCALAR
228
62.9k
    for (size_t y = 0; y < rows; y++) {
229
62.9k
      unsqueeze_row(y0 + y, x);
230
62.9k
    }
231
62.9k
    return true;
232
62.9k
  };
233
62.9k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.h, kRowsPerThread),
234
62.9k
                                ThreadPool::NoInit, unsqueeze_span,
235
62.9k
                                "InvHorizontalSqueeze"));
236
62.9k
  input.channel[c] = std::move(chout);
237
62.9k
  return true;
238
62.9k
}
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
77.4k
Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
241
77.4k
  JXL_ENSURE(c < input.channel.size());
242
77.4k
  JXL_ENSURE(rc < input.channel.size());
243
77.4k
  const Channel &chin = input.channel[c];
244
77.4k
  const Channel &chin_residual = input.channel[rc];
245
  // These must be valid since we ran MetaApply already.
246
77.4k
  JXL_ENSURE(chin.h == DivCeil(chin.h + chin_residual.h, 2));
247
77.4k
  JXL_ENSURE(chin.w == chin_residual.w);
248
77.4k
  JxlMemoryManager *memory_manager = input.memory_manager();
249
250
77.4k
  if (chin_residual.h == 0) {
251
    // Short-circuit: output channel has same dimensions as input.
252
16.5k
    input.channel[c].vshift--;
253
16.5k
    return true;
254
16.5k
  }
255
256
  // Note: chin.h >= chin_residual.h and at most 1 different.
257
121k
  JXL_ASSIGN_OR_RETURN(
258
121k
      Channel chout,
259
121k
      Channel::Create(memory_manager, chin.w, chin.h + chin_residual.h,
260
121k
                      chin.hshift, chin.vshift - 1));
261
121k
  JXL_DEBUG_V(
262
121k
      4,
263
121k
      "Undoing vertical squeeze of channel %i using residuals in channel "
264
121k
      "%i (going from height %" PRIuS " to %" PRIuS ")",
265
121k
      c, rc, chin.h, chout.h);
266
267
121k
  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
60.8k
  static constexpr const int kColsPerThread = 64;
274
60.8k
  const auto unsqueeze_slice = [&](const uint32_t task,
275
67.7k
                                   size_t /* thread */) -> Status {
276
67.7k
    const size_t x0 = task * kColsPerThread;
277
67.7k
    const size_t x1 =
278
67.7k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
279
67.7k
    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
1.91M
    for (size_t y = 0; y < chin_residual.h; y++) {
283
1.84M
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
284
1.84M
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
285
1.84M
      const pixel_type *JXL_RESTRICT p_navg =
286
1.84M
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
287
1.84M
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
288
1.84M
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
289
1.84M
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
290
1.84M
      size_t x = 0;
291
1.84M
#if HWY_TARGET != HWY_SCALAR
292
10.9M
      for (; x + 7 < w; x += 8) {
293
9.15M
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
294
9.15M
                      p_out + x, p_nout + x);
295
9.15M
      }
296
1.84M
#endif
297
5.51M
      for (; x < w; x++) {
298
3.67M
        pixel_type_w avg = p_avg[x];
299
3.67M
        pixel_type_w next_avg = p_navg[x];
300
3.67M
        pixel_type_w top = p_pout[x];
301
3.67M
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
302
3.67M
        pixel_type_w diff_minus_tendency = p_residual[x];
303
3.67M
        pixel_type_w diff = diff_minus_tendency + tendency;
304
3.67M
        pixel_type_w out = avg + (diff / 2);
305
3.67M
        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
3.67M
        p_nout[x] = out - diff;
310
3.67M
      }
311
1.84M
    }
312
67.7k
    return true;
313
67.7k
  };
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
67.7k
                                   size_t /* thread */) -> Status {
276
67.7k
    const size_t x0 = task * kColsPerThread;
277
67.7k
    const size_t x1 =
278
67.7k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
279
67.7k
    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
1.91M
    for (size_t y = 0; y < chin_residual.h; y++) {
283
1.84M
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
284
1.84M
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
285
1.84M
      const pixel_type *JXL_RESTRICT p_navg =
286
1.84M
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
287
1.84M
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
288
1.84M
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
289
1.84M
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
290
1.84M
      size_t x = 0;
291
1.84M
#if HWY_TARGET != HWY_SCALAR
292
10.9M
      for (; x + 7 < w; x += 8) {
293
9.15M
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
294
9.15M
                      p_out + x, p_nout + x);
295
9.15M
      }
296
1.84M
#endif
297
5.51M
      for (; x < w; x++) {
298
3.67M
        pixel_type_w avg = p_avg[x];
299
3.67M
        pixel_type_w next_avg = p_navg[x];
300
3.67M
        pixel_type_w top = p_pout[x];
301
3.67M
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
302
3.67M
        pixel_type_w diff_minus_tendency = p_residual[x];
303
3.67M
        pixel_type_w diff = diff_minus_tendency + tendency;
304
3.67M
        pixel_type_w out = avg + (diff / 2);
305
3.67M
        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
3.67M
        p_nout[x] = out - diff;
310
3.67M
      }
311
1.84M
    }
312
67.7k
    return true;
313
67.7k
  };
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
60.8k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.w, kColsPerThread),
315
60.8k
                                ThreadPool::NoInit, unsqueeze_slice,
316
60.8k
                                "InvVertSqueeze"));
317
318
60.8k
  if (chout.h & 1) {
319
29.1k
    size_t y = chin.h - 1;
320
29.1k
    const pixel_type *p_avg = chin.Row(y);
321
29.1k
    pixel_type *p_out = chout.Row(y << 1);
322
938k
    for (size_t x = 0; x < chin.w; x++) {
323
909k
      p_out[x] = p_avg[x];
324
909k
    }
325
29.1k
  }
326
60.8k
  input.channel[c] = std::move(chout);
327
60.8k
  return true;
328
60.8k
}
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
77.4k
Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
241
77.4k
  JXL_ENSURE(c < input.channel.size());
242
77.4k
  JXL_ENSURE(rc < input.channel.size());
243
77.4k
  const Channel &chin = input.channel[c];
244
77.4k
  const Channel &chin_residual = input.channel[rc];
245
  // These must be valid since we ran MetaApply already.
246
77.4k
  JXL_ENSURE(chin.h == DivCeil(chin.h + chin_residual.h, 2));
247
77.4k
  JXL_ENSURE(chin.w == chin_residual.w);
248
77.4k
  JxlMemoryManager *memory_manager = input.memory_manager();
249
250
77.4k
  if (chin_residual.h == 0) {
251
    // Short-circuit: output channel has same dimensions as input.
252
16.5k
    input.channel[c].vshift--;
253
16.5k
    return true;
254
16.5k
  }
255
256
  // Note: chin.h >= chin_residual.h and at most 1 different.
257
121k
  JXL_ASSIGN_OR_RETURN(
258
121k
      Channel chout,
259
121k
      Channel::Create(memory_manager, chin.w, chin.h + chin_residual.h,
260
121k
                      chin.hshift, chin.vshift - 1));
261
121k
  JXL_DEBUG_V(
262
121k
      4,
263
121k
      "Undoing vertical squeeze of channel %i using residuals in channel "
264
121k
      "%i (going from height %" PRIuS " to %" PRIuS ")",
265
121k
      c, rc, chin.h, chout.h);
266
267
121k
  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
60.8k
  static constexpr const int kColsPerThread = 64;
274
60.8k
  const auto unsqueeze_slice = [&](const uint32_t task,
275
60.8k
                                   size_t /* thread */) -> Status {
276
60.8k
    const size_t x0 = task * kColsPerThread;
277
60.8k
    const size_t x1 =
278
60.8k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
279
60.8k
    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
60.8k
    for (size_t y = 0; y < chin_residual.h; y++) {
283
60.8k
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
284
60.8k
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
285
60.8k
      const pixel_type *JXL_RESTRICT p_navg =
286
60.8k
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
287
60.8k
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
288
60.8k
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
289
60.8k
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
290
60.8k
      size_t x = 0;
291
60.8k
#if HWY_TARGET != HWY_SCALAR
292
60.8k
      for (; x + 7 < w; x += 8) {
293
60.8k
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
294
60.8k
                      p_out + x, p_nout + x);
295
60.8k
      }
296
60.8k
#endif
297
60.8k
      for (; x < w; x++) {
298
60.8k
        pixel_type_w avg = p_avg[x];
299
60.8k
        pixel_type_w next_avg = p_navg[x];
300
60.8k
        pixel_type_w top = p_pout[x];
301
60.8k
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
302
60.8k
        pixel_type_w diff_minus_tendency = p_residual[x];
303
60.8k
        pixel_type_w diff = diff_minus_tendency + tendency;
304
60.8k
        pixel_type_w out = avg + (diff / 2);
305
60.8k
        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
60.8k
        p_nout[x] = out - diff;
310
60.8k
      }
311
60.8k
    }
312
60.8k
    return true;
313
60.8k
  };
314
60.8k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.w, kColsPerThread),
315
60.8k
                                ThreadPool::NoInit, unsqueeze_slice,
316
60.8k
                                "InvVertSqueeze"));
317
318
60.8k
  if (chout.h & 1) {
319
29.1k
    size_t y = chin.h - 1;
320
29.1k
    const pixel_type *p_avg = chin.Row(y);
321
29.1k
    pixel_type *p_out = chout.Row(y << 1);
322
938k
    for (size_t x = 0; x < chin.w; x++) {
323
909k
      p_out[x] = p_avg[x];
324
909k
    }
325
29.1k
  }
326
60.8k
  input.channel[c] = std::move(chout);
327
60.8k
  return true;
328
60.8k
}
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
10.3k
                  ThreadPool *pool) {
332
71.0k
  for (int i = parameters.size() - 1; i >= 0; i--) {
333
60.7k
    JXL_RETURN_IF_ERROR(
334
60.7k
        CheckMetaSqueezeParams(parameters[i], input.channel.size()));
335
60.7k
    bool horizontal = parameters[i].horizontal;
336
60.7k
    bool in_place = parameters[i].in_place;
337
60.7k
    uint32_t beginc = parameters[i].begin_c;
338
60.7k
    uint32_t endc = parameters[i].begin_c + parameters[i].num_c - 1;
339
60.7k
    uint32_t offset;
340
60.7k
    if (in_place) {
341
33.2k
      offset = endc + 1;
342
33.2k
    } else {
343
27.4k
      offset = input.channel.size() + beginc - endc - 1;
344
27.4k
    }
345
60.7k
    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
201k
    for (uint32_t c = beginc; c <= endc; c++) {
352
141k
      uint32_t rc = offset + c - beginc;
353
      // MetaApply should imply that `rc` is within range, otherwise there's a
354
      // programming bug.
355
141k
      JXL_ENSURE(rc < input.channel.size());
356
141k
      if ((input.channel[c].w < input.channel[rc].w) ||
357
141k
          (input.channel[c].h < input.channel[rc].h)) {
358
0
        return JXL_FAILURE("Corrupted squeeze transform");
359
0
      }
360
141k
      if (horizontal) {
361
63.8k
        JXL_RETURN_IF_ERROR(InvHSqueeze(input, c, rc, pool));
362
77.4k
      } else {
363
77.4k
        JXL_RETURN_IF_ERROR(InvVSqueeze(input, c, rc, pool));
364
77.4k
      }
365
141k
    }
366
60.7k
    input.channel.erase(input.channel.begin() + offset,
367
60.7k
                        input.channel.begin() + offset + (endc - beginc + 1));
368
60.7k
  }
369
10.3k
  return true;
370
10.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
10.3k
                  ThreadPool *pool) {
332
71.0k
  for (int i = parameters.size() - 1; i >= 0; i--) {
333
60.7k
    JXL_RETURN_IF_ERROR(
334
60.7k
        CheckMetaSqueezeParams(parameters[i], input.channel.size()));
335
60.7k
    bool horizontal = parameters[i].horizontal;
336
60.7k
    bool in_place = parameters[i].in_place;
337
60.7k
    uint32_t beginc = parameters[i].begin_c;
338
60.7k
    uint32_t endc = parameters[i].begin_c + parameters[i].num_c - 1;
339
60.7k
    uint32_t offset;
340
60.7k
    if (in_place) {
341
33.2k
      offset = endc + 1;
342
33.2k
    } else {
343
27.4k
      offset = input.channel.size() + beginc - endc - 1;
344
27.4k
    }
345
60.7k
    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
201k
    for (uint32_t c = beginc; c <= endc; c++) {
352
141k
      uint32_t rc = offset + c - beginc;
353
      // MetaApply should imply that `rc` is within range, otherwise there's a
354
      // programming bug.
355
141k
      JXL_ENSURE(rc < input.channel.size());
356
141k
      if ((input.channel[c].w < input.channel[rc].w) ||
357
141k
          (input.channel[c].h < input.channel[rc].h)) {
358
0
        return JXL_FAILURE("Corrupted squeeze transform");
359
0
      }
360
141k
      if (horizontal) {
361
63.8k
        JXL_RETURN_IF_ERROR(InvHSqueeze(input, c, rc, pool));
362
77.4k
      } else {
363
77.4k
        JXL_RETURN_IF_ERROR(InvVSqueeze(input, c, rc, pool));
364
77.4k
      }
365
141k
    }
366
60.7k
    input.channel.erase(input.channel.begin() + offset,
367
60.7k
                        input.channel.begin() + offset + (endc - beginc + 1));
368
60.7k
  }
369
10.3k
  return true;
370
10.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
10.3k
                  ThreadPool *pool) {
383
10.3k
  return HWY_DYNAMIC_DISPATCH(InvSqueeze)(input, parameters, pool);
384
10.3k
}
385
386
void DefaultSqueezeParameters(std::vector<SqueezeParams> *parameters,
387
10.2k
                              const Image &image) {
388
10.2k
  int nb_channels = image.channel.size() - image.nb_meta_channels;
389
390
10.2k
  parameters->clear();
391
10.2k
  size_t w = image.channel[image.nb_meta_channels].w;
392
10.2k
  size_t h = image.channel[image.nb_meta_channels].h;
393
10.2k
  JXL_DEBUG_V(
394
10.2k
      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.2k
  bool wide = (w > h);
398
399
10.2k
  if (nb_channels > 2 && image.channel[image.nb_meta_channels + 1].w == w &&
400
6.84k
      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.55k
    JXL_DEBUG_V(7, "(4:2:0 chroma), %" PRIuS "x%" PRIuS " image", w, h);
404
6.55k
    SqueezeParams params;
405
    // horizontal chroma squeeze
406
6.55k
    params.horizontal = true;
407
6.55k
    params.in_place = false;
408
6.55k
    params.begin_c = image.nb_meta_channels + 1;
409
6.55k
    params.num_c = 2;
410
6.55k
    parameters->push_back(params);
411
6.55k
    params.horizontal = false;
412
    // vertical chroma squeeze
413
6.55k
    parameters->push_back(params);
414
6.55k
  }
415
10.2k
  SqueezeParams params;
416
10.2k
  params.begin_c = image.nb_meta_channels;
417
10.2k
  params.num_c = nb_channels;
418
10.2k
  params.in_place = true;
419
420
10.2k
  if (!wide) {
421
6.15k
    if (h > kMaxFirstPreviewSize) {
422
2.83k
      params.horizontal = false;
423
2.83k
      parameters->push_back(params);
424
2.83k
      h = (h + 1) / 2;
425
2.83k
      JXL_DEBUG_V(7, "Vertical (%" PRIuS "x%" PRIuS "), ", w, h);
426
2.83k
    }
427
6.15k
  }
428
30.8k
  while (w > kMaxFirstPreviewSize || h > kMaxFirstPreviewSize) {
429
20.6k
    if (w > kMaxFirstPreviewSize) {
430
19.9k
      params.horizontal = true;
431
19.9k
      parameters->push_back(params);
432
19.9k
      w = (w + 1) / 2;
433
19.9k
      JXL_DEBUG_V(7, "Horizontal (%" PRIuS "x%" PRIuS "), ", w, h);
434
19.9k
    }
435
20.6k
    if (h > kMaxFirstPreviewSize) {
436
16.0k
      params.horizontal = false;
437
16.0k
      parameters->push_back(params);
438
16.0k
      h = (h + 1) / 2;
439
16.0k
      JXL_DEBUG_V(7, "Vertical (%" PRIuS "x%" PRIuS "), ", w, h);
440
16.0k
    }
441
20.6k
  }
442
10.2k
  JXL_DEBUG_V(7, "that's it");
443
10.2k
}
444
445
Status CheckMetaSqueezeParams(const SqueezeParams &parameter,
446
131k
                              int num_channels) {
447
131k
  int c1 = parameter.begin_c;
448
131k
  int c2 = parameter.begin_c + parameter.num_c - 1;
449
131k
  if (c1 < 0 || c1 >= num_channels || c2 < 0 || c2 >= num_channels || c2 < c1) {
450
148
    return JXL_FAILURE("Invalid channel range");
451
148
  }
452
131k
  return true;
453
131k
}
454
455
12.4k
Status MetaSqueeze(Image &image, std::vector<SqueezeParams> *parameters) {
456
12.4k
  JxlMemoryManager *memory_manager = image.memory_manager();
457
12.4k
  if (parameters->empty()) {
458
10.2k
    DefaultSqueezeParameters(parameters, image);
459
10.2k
  }
460
461
70.4k
  for (auto &parameter : *parameters) {
462
70.4k
    JXL_RETURN_IF_ERROR(
463
70.4k
        CheckMetaSqueezeParams(parameter, image.channel.size()));
464
70.3k
    bool horizontal = parameter.horizontal;
465
70.3k
    bool in_place = parameter.in_place;
466
70.3k
    uint32_t beginc = parameter.begin_c;
467
70.3k
    uint32_t endc = parameter.begin_c + parameter.num_c - 1;
468
469
70.3k
    uint32_t offset;
470
70.3k
    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
70.3k
    if (in_place) {
481
39.0k
      offset = endc + 1;
482
39.0k
    } else {
483
31.2k
      offset = image.channel.size();
484
31.2k
    }
485
243k
    for (uint32_t c = beginc; c <= endc; c++) {
486
173k
      if (image.channel[c].hshift > 30 || image.channel[c].vshift > 30) {
487
6
        return JXL_FAILURE("Too many squeezes: shift > 30");
488
6
      }
489
173k
      size_t w = image.channel[c].w;
490
173k
      size_t h = image.channel[c].h;
491
173k
      if (w == 0 || h == 0) return JXL_FAILURE("Squeezing empty channel");
492
173k
      if (horizontal) {
493
79.5k
        image.channel[c].w = (w + 1) / 2;
494
79.5k
        if (image.channel[c].hshift >= 0) image.channel[c].hshift++;
495
79.5k
        w = w - (w + 1) / 2;
496
93.6k
      } else {
497
93.6k
        image.channel[c].h = (h + 1) / 2;
498
93.6k
        if (image.channel[c].vshift >= 0) image.channel[c].vshift++;
499
93.6k
        h = h - (h + 1) / 2;
500
93.6k
      }
501
173k
      JXL_RETURN_IF_ERROR(image.channel[c].shrink());
502
346k
      JXL_ASSIGN_OR_RETURN(Channel placeholder,
503
346k
                           Channel::Create(memory_manager, w, h));
504
346k
      placeholder.hshift = image.channel[c].hshift;
505
346k
      placeholder.vshift = image.channel[c].vshift;
506
346k
      placeholder.component = image.channel[c].component;
507
346k
      image.channel.insert(image.channel.begin() + offset + (c - beginc),
508
346k
                           std::move(placeholder));
509
346k
      JXL_DEBUG_V(0, "MetaSqueeze applied, current image: %s",
510
346k
                  image.DebugString().c_str());
511
346k
    }
512
70.3k
  }
513
12.2k
  return true;
514
12.4k
}
515
516
}  // namespace jxl
517
518
#endif