Coverage Report

Created: 2026-11-12 07:35

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
128k
Status InvHSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
129
128k
  JXL_ENSURE(c < input.channel.size());
130
128k
  JXL_ENSURE(rc < input.channel.size());
131
128k
  const Channel& chin = input.channel[c];
132
128k
  const Channel &chin_residual = input.channel[rc];
133
  // These must be valid since we ran MetaApply already.
134
128k
  JXL_ENSURE(chin.w == DivCeil(chin.w + chin_residual.w, 2));
135
128k
  JXL_ENSURE(chin.h == chin_residual.h);
136
128k
  JXL_ENSURE(chin.hshift != 0);
137
128k
  JxlMemoryManager *memory_manager = input.memory_manager();
138
128k
  int new_hshift = (chin.hshift > 0) ? chin.hshift - 1 : chin.hshift;
139
140
128k
  if (chin_residual.w == 0) {
141
    // Short-circuit: output channel has same dimensions as input.
142
2.24k
    input.channel[c].hshift = new_hshift;
143
2.24k
    return true;
144
2.24k
  }
145
146
  // Note: chin.w >= chin_residual.w and at most 1 different.
147
252k
  JXL_ASSIGN_OR_RETURN(Channel chout,
148
252k
                       Channel::Create(memory_manager, chin.w + chin_residual.w,
149
252k
                                       chin.h, new_hshift, chin.vshift));
150
252k
  JXL_DEBUG_V(4,
151
252k
              "Undoing horizontal squeeze of channel %i using residuals in "
152
252k
              "channel %i (going from width %" PRIuS " to %" PRIuS ")",
153
252k
              c, rc, chin.w, chout.w);
154
155
252k
  if (chin_residual.h == 0) {
156
    // Short-circuit: channel with no pixels.
157
0
    input.channel[c] = std::move(chout);
158
0
    return true;
159
0
  }
160
4.70M
  auto unsqueeze_row = [&](size_t y, size_t x0) {
161
4.70M
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
162
4.70M
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
163
4.70M
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
164
41.1M
    for (size_t x = x0; x < chin_residual.w; x++) {
165
36.3M
      pixel_type_w diff_minus_tendency = p_residual[x];
166
36.3M
      pixel_type_w avg = p_avg[x];
167
36.3M
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
168
36.3M
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
169
36.3M
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
170
36.3M
      pixel_type_w diff = diff_minus_tendency + tendency;
171
36.3M
      pixel_type_w A = avg + (diff / 2);
172
36.3M
      p_out[(x << 1)] = A;
173
36.3M
      pixel_type_w B = A - diff;
174
36.3M
      p_out[(x << 1) + 1] = B;
175
36.3M
    }
176
4.70M
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
177
4.70M
  };
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
160
4.70M
  auto unsqueeze_row = [&](size_t y, size_t x0) {
161
4.70M
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
162
4.70M
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
163
4.70M
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
164
41.1M
    for (size_t x = x0; x < chin_residual.w; x++) {
165
36.3M
      pixel_type_w diff_minus_tendency = p_residual[x];
166
36.3M
      pixel_type_w avg = p_avg[x];
167
36.3M
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
168
36.3M
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
169
36.3M
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
170
36.3M
      pixel_type_w diff = diff_minus_tendency + tendency;
171
36.3M
      pixel_type_w A = avg + (diff / 2);
172
36.3M
      p_out[(x << 1)] = A;
173
36.3M
      pixel_type_w B = A - diff;
174
36.3M
      p_out[(x << 1) + 1] = B;
175
36.3M
    }
176
4.70M
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
177
4.70M
  };
Unexecuted instantiation: squeeze.cc:jxl::N_SSE2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned long, unsigned long) const
178
179
  // somewhat complicated trickery just to be able to SIMD this.
180
  // Horizontal unsqueeze has horizontal data dependencies, so we do
181
  // 8 rows at a time and treat it as a vertical unsqueeze of a
182
  // transposed 8x8 block (or 9x8 for one input).
183
126k
  static constexpr const size_t kRowsPerThread = 8;
184
126k
  const auto unsqueeze_span = [&](const uint32_t task,
185
624k
                                  size_t /* thread */) -> Status {
186
624k
    const size_t y0 = task * kRowsPerThread;
187
624k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
188
624k
    size_t x = 0;
189
190
624k
#if HWY_TARGET != HWY_SCALAR
191
624k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
192
624k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
193
624k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
194
624k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
195
624k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
196
624k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
197
624k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
198
624k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
199
624k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
200
624k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
201
624k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
202
624k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
203
624k
    const size_t N = Lanes(d);
204
624k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
205
5.57M
      for (; x < chin_residual.w - 9; x += 8) {
206
5.12M
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
207
5.12M
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
208
46.1M
        for (size_t y = 0; y < kRowsPerThread; y++) {
209
41.0M
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
210
41.0M
        }
211
46.1M
        for (size_t i = 0; i < 8; i++) {
212
41.0M
          FastUnsqueeze(
213
41.0M
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
214
41.0M
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
215
41.0M
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
216
41.0M
        }
217
218
5.12M
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
219
5.12M
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
220
46.1M
        for (size_t y = 0; y < kRowsPerThread; y++) {
221
82.0M
          for (size_t i = 0; i < kRowsPerThread; i += N) {
222
41.0M
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
223
41.0M
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
224
41.0M
            StoreInterleaved(d, even, odd,
225
41.0M
                             p_out + ((x + i) << 1) + onerow_out * y);
226
41.0M
          }
227
41.0M
        }
228
5.12M
      }
229
443k
    }
230
624k
#endif  // HWY_TARGET != HWY_SCALAR
231
5.33M
    for (size_t y = 0; y < rows; y++) {
232
4.70M
      unsqueeze_row(y0 + y, x);
233
4.70M
    }
234
624k
    return true;
235
624k
  };
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
185
624k
                                  size_t /* thread */) -> Status {
186
624k
    const size_t y0 = task * kRowsPerThread;
187
624k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
188
624k
    size_t x = 0;
189
190
624k
#if HWY_TARGET != HWY_SCALAR
191
624k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
192
624k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
193
624k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
194
624k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
195
624k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
196
624k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
197
624k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
198
624k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
199
624k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
200
624k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
201
624k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
202
624k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
203
624k
    const size_t N = Lanes(d);
204
624k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
205
5.57M
      for (; x < chin_residual.w - 9; x += 8) {
206
5.12M
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
207
5.12M
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
208
46.1M
        for (size_t y = 0; y < kRowsPerThread; y++) {
209
41.0M
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
210
41.0M
        }
211
46.1M
        for (size_t i = 0; i < 8; i++) {
212
41.0M
          FastUnsqueeze(
213
41.0M
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
214
41.0M
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
215
41.0M
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
216
41.0M
        }
217
218
5.12M
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
219
5.12M
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
220
46.1M
        for (size_t y = 0; y < kRowsPerThread; y++) {
221
82.0M
          for (size_t i = 0; i < kRowsPerThread; i += N) {
222
41.0M
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
223
41.0M
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
224
41.0M
            StoreInterleaved(d, even, odd,
225
41.0M
                             p_out + ((x + i) << 1) + onerow_out * y);
226
41.0M
          }
227
41.0M
        }
228
5.12M
      }
229
443k
    }
230
624k
#endif  // HWY_TARGET != HWY_SCALAR
231
5.33M
    for (size_t y = 0; y < rows; y++) {
232
4.70M
      unsqueeze_row(y0 + y, x);
233
4.70M
    }
234
624k
    return true;
235
624k
  };
Unexecuted instantiation: squeeze.cc:jxl::N_SSE2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_1::operator()(unsigned int, unsigned long) const
236
126k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.h, kRowsPerThread),
237
126k
                                ThreadPool::NoInit, unsqueeze_span,
238
126k
                                "InvHorizontalSqueeze"));
239
126k
  input.channel[c] = std::move(chout);
240
126k
  return true;
241
126k
}
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
128k
Status InvHSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
129
128k
  JXL_ENSURE(c < input.channel.size());
130
128k
  JXL_ENSURE(rc < input.channel.size());
131
128k
  const Channel& chin = input.channel[c];
132
128k
  const Channel &chin_residual = input.channel[rc];
133
  // These must be valid since we ran MetaApply already.
134
128k
  JXL_ENSURE(chin.w == DivCeil(chin.w + chin_residual.w, 2));
135
128k
  JXL_ENSURE(chin.h == chin_residual.h);
136
128k
  JXL_ENSURE(chin.hshift != 0);
137
128k
  JxlMemoryManager *memory_manager = input.memory_manager();
138
128k
  int new_hshift = (chin.hshift > 0) ? chin.hshift - 1 : chin.hshift;
139
140
128k
  if (chin_residual.w == 0) {
141
    // Short-circuit: output channel has same dimensions as input.
142
2.24k
    input.channel[c].hshift = new_hshift;
143
2.24k
    return true;
144
2.24k
  }
145
146
  // Note: chin.w >= chin_residual.w and at most 1 different.
147
252k
  JXL_ASSIGN_OR_RETURN(Channel chout,
148
252k
                       Channel::Create(memory_manager, chin.w + chin_residual.w,
149
252k
                                       chin.h, new_hshift, chin.vshift));
150
252k
  JXL_DEBUG_V(4,
151
252k
              "Undoing horizontal squeeze of channel %i using residuals in "
152
252k
              "channel %i (going from width %" PRIuS " to %" PRIuS ")",
153
252k
              c, rc, chin.w, chout.w);
154
155
252k
  if (chin_residual.h == 0) {
156
    // Short-circuit: channel with no pixels.
157
0
    input.channel[c] = std::move(chout);
158
0
    return true;
159
0
  }
160
126k
  auto unsqueeze_row = [&](size_t y, size_t x0) {
161
126k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y);
162
126k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y);
163
126k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y);
164
126k
    for (size_t x = x0; x < chin_residual.w; x++) {
165
126k
      pixel_type_w diff_minus_tendency = p_residual[x];
166
126k
      pixel_type_w avg = p_avg[x];
167
126k
      pixel_type_w next_avg = (x + 1 < chin.w ? p_avg[x + 1] : avg);
168
126k
      pixel_type_w left = (x ? p_out[(x << 1) - 1] : avg);
169
126k
      pixel_type_w tendency = SmoothTendency(left, avg, next_avg);
170
126k
      pixel_type_w diff = diff_minus_tendency + tendency;
171
126k
      pixel_type_w A = avg + (diff / 2);
172
126k
      p_out[(x << 1)] = A;
173
126k
      pixel_type_w B = A - diff;
174
126k
      p_out[(x << 1) + 1] = B;
175
126k
    }
176
126k
    if (chout.w & 1) p_out[chout.w - 1] = p_avg[chin.w - 1];
177
126k
  };
178
179
  // somewhat complicated trickery just to be able to SIMD this.
180
  // Horizontal unsqueeze has horizontal data dependencies, so we do
181
  // 8 rows at a time and treat it as a vertical unsqueeze of a
182
  // transposed 8x8 block (or 9x8 for one input).
183
126k
  static constexpr const size_t kRowsPerThread = 8;
184
126k
  const auto unsqueeze_span = [&](const uint32_t task,
185
126k
                                  size_t /* thread */) -> Status {
186
126k
    const size_t y0 = task * kRowsPerThread;
187
126k
    const size_t rows = std::min(kRowsPerThread, chin.h - y0);
188
126k
    size_t x = 0;
189
190
126k
#if HWY_TARGET != HWY_SCALAR
191
126k
    ptrdiff_t onerow_in = chin.plane.PixelsPerRow();
192
126k
    ptrdiff_t onerow_inr = chin_residual.plane.PixelsPerRow();
193
126k
    ptrdiff_t onerow_out = chout.plane.PixelsPerRow();
194
126k
    const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y0);
195
126k
    const pixel_type *JXL_RESTRICT p_avg = chin.Row(y0);
196
126k
    pixel_type *JXL_RESTRICT p_out = chout.Row(y0);
197
126k
    HWY_ALIGN pixel_type b_p_avg[9 * kRowsPerThread];
198
126k
    HWY_ALIGN pixel_type b_p_residual[8 * kRowsPerThread];
199
126k
    HWY_ALIGN pixel_type b_p_out_even[8 * kRowsPerThread];
200
126k
    HWY_ALIGN pixel_type b_p_out_odd[8 * kRowsPerThread];
201
126k
    HWY_ALIGN pixel_type b_p_out_evenT[8 * kRowsPerThread];
202
126k
    HWY_ALIGN pixel_type b_p_out_oddT[8 * kRowsPerThread];
203
126k
    const size_t N = Lanes(d);
204
126k
    if (chin_residual.w > 16 && rows == kRowsPerThread) {
205
126k
      for (; x < chin_residual.w - 9; x += 8) {
206
126k
        Transpose8x8Block(p_residual + x, b_p_residual, onerow_inr);
207
126k
        Transpose8x8Block(p_avg + x, b_p_avg, onerow_in);
208
126k
        for (size_t y = 0; y < kRowsPerThread; y++) {
209
126k
          b_p_avg[8 * 8 + y] = p_avg[x + 8 + onerow_in * y];
210
126k
        }
211
126k
        for (size_t i = 0; i < 8; i++) {
212
126k
          FastUnsqueeze(
213
126k
              b_p_residual + 8 * i, b_p_avg + 8 * i, b_p_avg + 8 * (i + 1),
214
126k
              (x + i ? b_p_out_odd + 8 * ((x + i - 1) & 7) : b_p_avg + 8 * i),
215
126k
              b_p_out_even + 8 * i, b_p_out_odd + 8 * i);
216
126k
        }
217
218
126k
        Transpose8x8Block(b_p_out_even, b_p_out_evenT, 8);
219
126k
        Transpose8x8Block(b_p_out_odd, b_p_out_oddT, 8);
220
126k
        for (size_t y = 0; y < kRowsPerThread; y++) {
221
126k
          for (size_t i = 0; i < kRowsPerThread; i += N) {
222
126k
            auto even = Load(d, b_p_out_evenT + 8 * y + i);
223
126k
            auto odd = Load(d, b_p_out_oddT + 8 * y + i);
224
126k
            StoreInterleaved(d, even, odd,
225
126k
                             p_out + ((x + i) << 1) + onerow_out * y);
226
126k
          }
227
126k
        }
228
126k
      }
229
126k
    }
230
126k
#endif  // HWY_TARGET != HWY_SCALAR
231
126k
    for (size_t y = 0; y < rows; y++) {
232
126k
      unsqueeze_row(y0 + y, x);
233
126k
    }
234
126k
    return true;
235
126k
  };
236
126k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.h, kRowsPerThread),
237
126k
                                ThreadPool::NoInit, unsqueeze_span,
238
126k
                                "InvHorizontalSqueeze"));
239
126k
  input.channel[c] = std::move(chout);
240
126k
  return true;
241
126k
}
Unexecuted instantiation: jxl::N_SSE2::InvHSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
242
243
119k
Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
244
119k
  JXL_ENSURE(c < input.channel.size());
245
119k
  JXL_ENSURE(rc < input.channel.size());
246
119k
  const Channel &chin = input.channel[c];
247
119k
  const Channel &chin_residual = input.channel[rc];
248
  // These must be valid since we ran MetaApply already.
249
119k
  JXL_ENSURE(chin.h == DivCeil(chin.h + chin_residual.h, 2));
250
119k
  JXL_ENSURE(chin.w == chin_residual.w);
251
119k
  JXL_ENSURE(chin.vshift != 0);
252
119k
  JxlMemoryManager *memory_manager = input.memory_manager();
253
119k
  int new_vshift = (chin.vshift > 0) ? chin.vshift - 1 : chin.vshift;
254
255
119k
  if (chin_residual.h == 0) {
256
    // Short-circuit: output channel has same dimensions as input.
257
1.49k
    input.channel[c].vshift = new_vshift;
258
1.49k
    return true;
259
1.49k
  }
260
261
  // Note: chin.h >= chin_residual.h and at most 1 different.
262
236k
  JXL_ASSIGN_OR_RETURN(Channel chout, Channel::Create(memory_manager, chin.w,
263
236k
                                                      chin.h + chin_residual.h,
264
236k
                                                      chin.hshift, new_vshift));
265
236k
  JXL_DEBUG_V(
266
236k
      4,
267
236k
      "Undoing vertical squeeze of channel %i using residuals in channel "
268
236k
      "%i (going from height %" PRIuS " to %" PRIuS ")",
269
236k
      c, rc, chin.h, chout.h);
270
271
236k
  if (chin_residual.w == 0) {
272
    // Short-circuit: channel with no pixels.
273
0
    input.channel[c] = std::move(chout);
274
0
    return true;
275
0
  }
276
277
118k
  static constexpr const int kColsPerThread = 64;
278
118k
  const auto unsqueeze_slice = [&](const uint32_t task,
279
145k
                                   size_t /* thread */) -> Status {
280
145k
    const size_t x0 = task * kColsPerThread;
281
145k
    const size_t x1 =
282
145k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
283
145k
    const size_t w = x1 - x0;
284
    // We only iterate up to std::min(chin_residual.h, chin.h) which is
285
    // always chin_residual.h.
286
6.22M
    for (size_t y = 0; y < chin_residual.h; y++) {
287
6.07M
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
288
6.07M
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
289
6.07M
      const pixel_type *JXL_RESTRICT p_navg =
290
6.07M
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
291
6.07M
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
292
6.07M
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
293
6.07M
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
294
6.07M
      size_t x = 0;
295
6.07M
#if HWY_TARGET != HWY_SCALAR
296
44.2M
      for (; x + 7 < w; x += 8) {
297
38.2M
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
298
38.2M
                      p_out + x, p_nout + x);
299
38.2M
      }
300
6.07M
#endif
301
12.7M
      for (; x < w; x++) {
302
6.65M
        pixel_type_w avg = p_avg[x];
303
6.65M
        pixel_type_w next_avg = p_navg[x];
304
6.65M
        pixel_type_w top = p_pout[x];
305
6.65M
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
306
6.65M
        pixel_type_w diff_minus_tendency = p_residual[x];
307
6.65M
        pixel_type_w diff = diff_minus_tendency + tendency;
308
6.65M
        pixel_type_w out = avg + (diff / 2);
309
6.65M
        p_out[x] = out;
310
        // If the chin_residual.h == chin.h, the output has an even number
311
        // of rows so the next line is fine. Otherwise, this loop won't
312
        // write to the last output row which is handled separately.
313
6.65M
        p_nout[x] = out - diff;
314
6.65M
      }
315
6.07M
    }
316
145k
    return true;
317
145k
  };
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
279
145k
                                   size_t /* thread */) -> Status {
280
145k
    const size_t x0 = task * kColsPerThread;
281
145k
    const size_t x1 =
282
145k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
283
145k
    const size_t w = x1 - x0;
284
    // We only iterate up to std::min(chin_residual.h, chin.h) which is
285
    // always chin_residual.h.
286
6.22M
    for (size_t y = 0; y < chin_residual.h; y++) {
287
6.07M
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
288
6.07M
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
289
6.07M
      const pixel_type *JXL_RESTRICT p_navg =
290
6.07M
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
291
6.07M
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
292
6.07M
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
293
6.07M
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
294
6.07M
      size_t x = 0;
295
6.07M
#if HWY_TARGET != HWY_SCALAR
296
44.2M
      for (; x + 7 < w; x += 8) {
297
38.2M
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
298
38.2M
                      p_out + x, p_nout + x);
299
38.2M
      }
300
6.07M
#endif
301
12.7M
      for (; x < w; x++) {
302
6.65M
        pixel_type_w avg = p_avg[x];
303
6.65M
        pixel_type_w next_avg = p_navg[x];
304
6.65M
        pixel_type_w top = p_pout[x];
305
6.65M
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
306
6.65M
        pixel_type_w diff_minus_tendency = p_residual[x];
307
6.65M
        pixel_type_w diff = diff_minus_tendency + tendency;
308
6.65M
        pixel_type_w out = avg + (diff / 2);
309
6.65M
        p_out[x] = out;
310
        // If the chin_residual.h == chin.h, the output has an even number
311
        // of rows so the next line is fine. Otherwise, this loop won't
312
        // write to the last output row which is handled separately.
313
6.65M
        p_nout[x] = out - diff;
314
6.65M
      }
315
6.07M
    }
316
145k
    return true;
317
145k
  };
Unexecuted instantiation: squeeze.cc:jxl::N_SSE2::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)::$_0::operator()(unsigned int, unsigned long) const
318
118k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.w, kColsPerThread),
319
118k
                                ThreadPool::NoInit, unsqueeze_slice,
320
118k
                                "InvVertSqueeze"));
321
322
118k
  if (chout.h & 1) {
323
26.4k
    size_t y = chin.h - 1;
324
26.4k
    const pixel_type *p_avg = chin.Row(y);
325
26.4k
    pixel_type *p_out = chout.Row(y << 1);
326
748k
    for (size_t x = 0; x < chin.w; x++) {
327
721k
      p_out[x] = p_avg[x];
328
721k
    }
329
26.4k
  }
330
118k
  input.channel[c] = std::move(chout);
331
118k
  return true;
332
118k
}
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
243
119k
Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
244
119k
  JXL_ENSURE(c < input.channel.size());
245
119k
  JXL_ENSURE(rc < input.channel.size());
246
119k
  const Channel &chin = input.channel[c];
247
119k
  const Channel &chin_residual = input.channel[rc];
248
  // These must be valid since we ran MetaApply already.
249
119k
  JXL_ENSURE(chin.h == DivCeil(chin.h + chin_residual.h, 2));
250
119k
  JXL_ENSURE(chin.w == chin_residual.w);
251
119k
  JXL_ENSURE(chin.vshift != 0);
252
119k
  JxlMemoryManager *memory_manager = input.memory_manager();
253
119k
  int new_vshift = (chin.vshift > 0) ? chin.vshift - 1 : chin.vshift;
254
255
119k
  if (chin_residual.h == 0) {
256
    // Short-circuit: output channel has same dimensions as input.
257
1.49k
    input.channel[c].vshift = new_vshift;
258
1.49k
    return true;
259
1.49k
  }
260
261
  // Note: chin.h >= chin_residual.h and at most 1 different.
262
236k
  JXL_ASSIGN_OR_RETURN(Channel chout, Channel::Create(memory_manager, chin.w,
263
236k
                                                      chin.h + chin_residual.h,
264
236k
                                                      chin.hshift, new_vshift));
265
236k
  JXL_DEBUG_V(
266
236k
      4,
267
236k
      "Undoing vertical squeeze of channel %i using residuals in channel "
268
236k
      "%i (going from height %" PRIuS " to %" PRIuS ")",
269
236k
      c, rc, chin.h, chout.h);
270
271
236k
  if (chin_residual.w == 0) {
272
    // Short-circuit: channel with no pixels.
273
0
    input.channel[c] = std::move(chout);
274
0
    return true;
275
0
  }
276
277
118k
  static constexpr const int kColsPerThread = 64;
278
118k
  const auto unsqueeze_slice = [&](const uint32_t task,
279
118k
                                   size_t /* thread */) -> Status {
280
118k
    const size_t x0 = task * kColsPerThread;
281
118k
    const size_t x1 =
282
118k
        std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
283
118k
    const size_t w = x1 - x0;
284
    // We only iterate up to std::min(chin_residual.h, chin.h) which is
285
    // always chin_residual.h.
286
118k
    for (size_t y = 0; y < chin_residual.h; y++) {
287
118k
      const pixel_type *JXL_RESTRICT p_residual = chin_residual.Row(y) + x0;
288
118k
      const pixel_type *JXL_RESTRICT p_avg = chin.Row(y) + x0;
289
118k
      const pixel_type *JXL_RESTRICT p_navg =
290
118k
          chin.Row(y + 1 < chin.h ? y + 1 : y) + x0;
291
118k
      pixel_type *JXL_RESTRICT p_out = chout.Row(y << 1) + x0;
292
118k
      pixel_type *JXL_RESTRICT p_nout = chout.Row((y << 1) + 1) + x0;
293
118k
      const pixel_type *p_pout = y > 0 ? chout.Row((y << 1) - 1) + x0 : p_avg;
294
118k
      size_t x = 0;
295
118k
#if HWY_TARGET != HWY_SCALAR
296
118k
      for (; x + 7 < w; x += 8) {
297
118k
        FastUnsqueeze(p_residual + x, p_avg + x, p_navg + x, p_pout + x,
298
118k
                      p_out + x, p_nout + x);
299
118k
      }
300
118k
#endif
301
118k
      for (; x < w; x++) {
302
118k
        pixel_type_w avg = p_avg[x];
303
118k
        pixel_type_w next_avg = p_navg[x];
304
118k
        pixel_type_w top = p_pout[x];
305
118k
        pixel_type_w tendency = SmoothTendency(top, avg, next_avg);
306
118k
        pixel_type_w diff_minus_tendency = p_residual[x];
307
118k
        pixel_type_w diff = diff_minus_tendency + tendency;
308
118k
        pixel_type_w out = avg + (diff / 2);
309
118k
        p_out[x] = out;
310
        // If the chin_residual.h == chin.h, the output has an even number
311
        // of rows so the next line is fine. Otherwise, this loop won't
312
        // write to the last output row which is handled separately.
313
118k
        p_nout[x] = out - diff;
314
118k
      }
315
118k
    }
316
118k
    return true;
317
118k
  };
318
118k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, DivCeil(chin.w, kColsPerThread),
319
118k
                                ThreadPool::NoInit, unsqueeze_slice,
320
118k
                                "InvVertSqueeze"));
321
322
118k
  if (chout.h & 1) {
323
26.4k
    size_t y = chin.h - 1;
324
26.4k
    const pixel_type *p_avg = chin.Row(y);
325
26.4k
    pixel_type *p_out = chout.Row(y << 1);
326
748k
    for (size_t x = 0; x < chin.w; x++) {
327
721k
      p_out[x] = p_avg[x];
328
721k
    }
329
26.4k
  }
330
118k
  input.channel[c] = std::move(chout);
331
118k
  return true;
332
118k
}
Unexecuted instantiation: jxl::N_SSE2::InvVSqueeze(jxl::Image&, unsigned int, unsigned int, jxl::ThreadPool*)
333
334
Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
335
17.1k
                  ThreadPool *pool) {
336
94.8k
  for (int i = parameters.size() - 1; i >= 0; i--) {
337
77.6k
    JXL_RETURN_IF_ERROR(
338
77.6k
        CheckMetaSqueezeParams(parameters[i], input.channel.size()));
339
77.6k
    bool horizontal = parameters[i].horizontal;
340
77.6k
    bool in_place = parameters[i].in_place;
341
77.6k
    uint32_t beginc = parameters[i].begin_c;
342
77.6k
    uint32_t endc = parameters[i].begin_c + parameters[i].num_c - 1;
343
77.6k
    uint32_t offset;
344
77.6k
    if (in_place) {
345
54.1k
      offset = endc + 1;
346
54.1k
    } else {
347
23.5k
      offset = input.channel.size() + beginc - endc - 1;
348
23.5k
    }
349
77.6k
    if (beginc < input.nb_meta_channels) {
350
      // This is checked in MetaSqueeze.
351
10
      JXL_ENSURE(input.nb_meta_channels > parameters[i].num_c);
352
10
      input.nb_meta_channels -= parameters[i].num_c;
353
10
    }
354
355
325k
    for (uint32_t c = beginc; c <= endc; c++) {
356
248k
      uint32_t rc = offset + c - beginc;
357
      // MetaApply should imply that `rc` is within range, otherwise there's a
358
      // programming bug.
359
248k
      JXL_ENSURE(rc < input.channel.size());
360
248k
      if ((input.channel[c].w < input.channel[rc].w) ||
361
248k
          (input.channel[c].h < input.channel[rc].h)) {
362
0
        return JXL_FAILURE("Corrupted squeeze transform");
363
0
      }
364
248k
      if (horizontal) {
365
128k
        JXL_RETURN_IF_ERROR(InvHSqueeze(input, c, rc, pool));
366
128k
      } else {
367
119k
        JXL_RETURN_IF_ERROR(InvVSqueeze(input, c, rc, pool));
368
119k
      }
369
248k
    }
370
77.6k
    input.channel.erase(input.channel.begin() + offset,
371
77.6k
                        input.channel.begin() + offset + (endc - beginc + 1));
372
77.6k
  }
373
17.1k
  return true;
374
17.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
335
17.1k
                  ThreadPool *pool) {
336
94.8k
  for (int i = parameters.size() - 1; i >= 0; i--) {
337
77.6k
    JXL_RETURN_IF_ERROR(
338
77.6k
        CheckMetaSqueezeParams(parameters[i], input.channel.size()));
339
77.6k
    bool horizontal = parameters[i].horizontal;
340
77.6k
    bool in_place = parameters[i].in_place;
341
77.6k
    uint32_t beginc = parameters[i].begin_c;
342
77.6k
    uint32_t endc = parameters[i].begin_c + parameters[i].num_c - 1;
343
77.6k
    uint32_t offset;
344
77.6k
    if (in_place) {
345
54.1k
      offset = endc + 1;
346
54.1k
    } else {
347
23.5k
      offset = input.channel.size() + beginc - endc - 1;
348
23.5k
    }
349
77.6k
    if (beginc < input.nb_meta_channels) {
350
      // This is checked in MetaSqueeze.
351
10
      JXL_ENSURE(input.nb_meta_channels > parameters[i].num_c);
352
10
      input.nb_meta_channels -= parameters[i].num_c;
353
10
    }
354
355
325k
    for (uint32_t c = beginc; c <= endc; c++) {
356
248k
      uint32_t rc = offset + c - beginc;
357
      // MetaApply should imply that `rc` is within range, otherwise there's a
358
      // programming bug.
359
248k
      JXL_ENSURE(rc < input.channel.size());
360
248k
      if ((input.channel[c].w < input.channel[rc].w) ||
361
248k
          (input.channel[c].h < input.channel[rc].h)) {
362
0
        return JXL_FAILURE("Corrupted squeeze transform");
363
0
      }
364
248k
      if (horizontal) {
365
128k
        JXL_RETURN_IF_ERROR(InvHSqueeze(input, c, rc, pool));
366
128k
      } else {
367
119k
        JXL_RETURN_IF_ERROR(InvVSqueeze(input, c, rc, pool));
368
119k
      }
369
248k
    }
370
77.6k
    input.channel.erase(input.channel.begin() + offset,
371
77.6k
                        input.channel.begin() + offset + (endc - beginc + 1));
372
77.6k
  }
373
17.1k
  return true;
374
17.1k
}
Unexecuted instantiation: jxl::N_SSE2::InvSqueeze(jxl::Image&, std::__1::vector<jxl::SqueezeParams, std::__1::allocator<jxl::SqueezeParams> > const&, jxl::ThreadPool*)
375
376
}  // namespace HWY_NAMESPACE
377
}  // namespace jxl
378
HWY_AFTER_NAMESPACE();
379
380
#if HWY_ONCE
381
382
namespace jxl {
383
384
HWY_EXPORT(InvSqueeze);
385
Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
386
17.1k
                  ThreadPool *pool) {
387
17.1k
  return HWY_DYNAMIC_DISPATCH(InvSqueeze)(input, parameters, pool);
388
17.1k
}
389
390
void DefaultSqueezeParameters(std::vector<SqueezeParams> *parameters,
391
17.1k
                              const Image &image) {
392
17.1k
  int nb_channels = image.channel.size() - image.nb_meta_channels;
393
394
17.1k
  parameters->clear();
395
17.1k
  size_t w = image.channel[image.nb_meta_channels].w;
396
17.1k
  size_t h = image.channel[image.nb_meta_channels].h;
397
17.1k
  JXL_DEBUG_V(
398
17.1k
      7, "Default squeeze parameters for %" PRIuS "x%" PRIuS " image: ", w, h);
399
400
  // do horizontal first on wide images; vertical first on tall images
401
17.1k
  bool wide = (w > h);
402
403
17.1k
  if (nb_channels > 2 && image.channel[image.nb_meta_channels + 1].w == w &&
404
12.0k
      image.channel[image.nb_meta_channels + 1].h == h) {
405
    // assume channels 1 and 2 are chroma, and can be squeezed first for 4:2:0
406
    // previews
407
9.05k
    JXL_DEBUG_V(7, "(4:2:0 chroma), %" PRIuS "x%" PRIuS " image", w, h);
408
9.05k
    SqueezeParams params;
409
    // horizontal chroma squeeze
410
9.05k
    params.horizontal = true;
411
9.05k
    params.in_place = false;
412
9.05k
    params.begin_c = image.nb_meta_channels + 1;
413
9.05k
    params.num_c = 2;
414
9.05k
    parameters->push_back(params);
415
9.05k
    params.horizontal = false;
416
    // vertical chroma squeeze
417
9.05k
    parameters->push_back(params);
418
9.05k
  }
419
17.1k
  SqueezeParams params;
420
17.1k
  params.begin_c = image.nb_meta_channels;
421
17.1k
  params.num_c = nb_channels;
422
17.1k
  params.in_place = true;
423
424
17.1k
  if (!wide) {
425
10.0k
    if (h > kMaxFirstPreviewSize) {
426
7.48k
      params.horizontal = false;
427
7.48k
      parameters->push_back(params);
428
7.48k
      h = (h + 1) / 2;
429
7.48k
      JXL_DEBUG_V(7, "Vertical (%" PRIuS "x%" PRIuS "), ", w, h);
430
7.48k
    }
431
10.0k
  }
432
47.4k
  while (w > kMaxFirstPreviewSize || h > kMaxFirstPreviewSize) {
433
30.3k
    if (w > kMaxFirstPreviewSize) {
434
28.3k
      params.horizontal = true;
435
28.3k
      parameters->push_back(params);
436
28.3k
      w = (w + 1) / 2;
437
28.3k
      JXL_DEBUG_V(7, "Horizontal (%" PRIuS "x%" PRIuS "), ", w, h);
438
28.3k
    }
439
30.3k
    if (h > kMaxFirstPreviewSize) {
440
19.8k
      params.horizontal = false;
441
19.8k
      parameters->push_back(params);
442
19.8k
      h = (h + 1) / 2;
443
19.8k
      JXL_DEBUG_V(7, "Vertical (%" PRIuS "x%" PRIuS "), ", w, h);
444
19.8k
    }
445
30.3k
  }
446
17.1k
  JXL_DEBUG_V(7, "that's it");
447
17.1k
}
448
449
Status CheckMetaSqueezeParams(const SqueezeParams &parameter,
450
160k
                              int num_channels) {
451
160k
  int c1 = parameter.begin_c;
452
160k
  int c2 = parameter.begin_c + parameter.num_c - 1;
453
160k
  if (c1 < 0 || c1 >= num_channels || c2 < 0 || c2 >= num_channels || c2 < c1) {
454
51
    return JXL_FAILURE("Invalid channel range");
455
51
  }
456
160k
  return true;
457
160k
}
458
459
18.5k
Status MetaSqueeze(Image &image, std::vector<SqueezeParams> *parameters) {
460
18.5k
  JxlMemoryManager *memory_manager = image.memory_manager();
461
18.5k
  if (parameters->empty()) {
462
17.1k
    DefaultSqueezeParameters(parameters, image);
463
17.1k
  }
464
465
82.9k
  for (auto &parameter : *parameters) {
466
82.9k
    JXL_RETURN_IF_ERROR(
467
82.9k
        CheckMetaSqueezeParams(parameter, image.channel.size()));
468
82.8k
    bool horizontal = parameter.horizontal;
469
82.8k
    bool in_place = parameter.in_place;
470
82.8k
    uint32_t beginc = parameter.begin_c;
471
82.8k
    uint32_t endc = parameter.begin_c + parameter.num_c - 1;
472
473
82.8k
    uint32_t offset;
474
82.8k
    if (beginc < image.nb_meta_channels) {
475
35
      if (endc >= image.nb_meta_channels) {
476
2
        return JXL_FAILURE("Invalid squeeze: mix of meta and nonmeta channels");
477
2
      }
478
33
      if (!in_place) {
479
3
        return JXL_FAILURE(
480
3
            "Invalid squeeze: meta channels require in-place residuals");
481
3
      }
482
30
      image.nb_meta_channels += parameter.num_c;
483
30
    }
484
82.8k
    if (in_place) {
485
56.4k
      offset = endc + 1;
486
56.4k
    } else {
487
26.3k
      offset = image.channel.size();
488
26.3k
    }
489
349k
    for (uint32_t c = beginc; c <= endc; c++) {
490
266k
      if (image.channel[c].hshift > 30 || image.channel[c].vshift > 30) {
491
5
        return JXL_FAILURE("Too many squeezes: shift > 30");
492
5
      }
493
266k
      size_t w = image.channel[c].w;
494
266k
      size_t h = image.channel[c].h;
495
266k
      if (w == 0 || h == 0) return JXL_FAILURE("Squeezing empty channel");
496
266k
      if (horizontal) {
497
137k
        image.channel[c].w = (w + 1) / 2;
498
137k
        if (image.channel[c].hshift >= 0) image.channel[c].hshift++;
499
137k
        w = w - (w + 1) / 2;
500
137k
      } else {
501
128k
        image.channel[c].h = (h + 1) / 2;
502
128k
        if (image.channel[c].vshift >= 0) image.channel[c].vshift++;
503
128k
        h = h - (h + 1) / 2;
504
128k
      }
505
266k
      JXL_RETURN_IF_ERROR(image.channel[c].shrink());
506
532k
      JXL_ASSIGN_OR_RETURN(Channel placeholder,
507
532k
                           Channel::Create(memory_manager, w, h));
508
532k
      placeholder.hshift = image.channel[c].hshift;
509
532k
      placeholder.vshift = image.channel[c].vshift;
510
532k
      placeholder.component = image.channel[c].component;
511
532k
      image.channel.insert(image.channel.begin() + offset + (c - beginc),
512
532k
                           std::move(placeholder));
513
532k
      JXL_DEBUG_V(0, "MetaSqueeze applied, current image: %s",
514
532k
                  image.DebugString().c_str());
515
532k
    }
516
82.8k
  }
517
18.5k
  return true;
518
18.5k
}
519
520
}  // namespace jxl
521
522
#endif