Coverage Report

Created: 2026-07-20 07:19

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