Coverage Report

Created: 2026-06-30 07:12

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