Coverage Report

Created: 2025-10-12 07:48

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