summaryrefslogtreecommitdiff
path: root/lib/jpegli/downsample.cc
blob: df2c156972a5b41fedeb017a0b30047313eb68e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jpegli/downsample.h"

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "lib/jpegli/downsample.cc"
#include <hwy/foreach_target.h>
#include <hwy/highway.h>

#include "lib/jpegli/encode_internal.h"
#include "lib/jpegli/error.h"

HWY_BEFORE_NAMESPACE();
namespace jpegli {
namespace HWY_NAMESPACE {

// These templates are not found via ADL.
using hwy::HWY_NAMESPACE::Add;
using hwy::HWY_NAMESPACE::Mul;
using hwy::HWY_NAMESPACE::Vec;

using D = HWY_CAPPED(float, 8);
constexpr D d;

void DownsampleRow2x1(const float* row_in, size_t len, float* row_out) {
  const size_t N = Lanes(d);
  const size_t len_out = len / 2;
  const auto mul = Set(d, 0.5f);
  Vec<D> v0, v1;
  for (size_t x = 0; x < len_out; x += N) {
    LoadInterleaved2(d, row_in + 2 * x, v0, v1);
    Store(Mul(mul, Add(v0, v1)), d, row_out + x);
  }
}

void DownsampleRow3x1(const float* row_in, size_t len, float* row_out) {
  const size_t N = Lanes(d);
  const size_t len_out = len / 3;
  const auto mul = Set(d, 1.0f / 3);
  Vec<D> v0, v1, v2;
  for (size_t x = 0; x < len_out; x += N) {
    LoadInterleaved3(d, row_in + 3 * x, v0, v1, v2);
    Store(Mul(mul, Add(Add(v0, v1), v2)), d, row_out + x);
  }
}

void DownsampleRow4x1(const float* row_in, size_t len, float* row_out) {
  const size_t N = Lanes(d);
  const size_t len_out = len / 4;
  const auto mul = Set(d, 0.25f);
  Vec<D> v0, v1, v2, v3;
  for (size_t x = 0; x < len_out; x += N) {
    LoadInterleaved4(d, row_in + 4 * x, v0, v1, v2, v3);
    Store(Mul(mul, Add(Add(v0, v1), Add(v2, v3))), d, row_out + x);
  }
}

void Downsample2x1(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow2x1(rows_in[0], len, row_out);
}

void Downsample3x1(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow3x1(rows_in[0], len, row_out);
}

void Downsample4x1(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow4x1(rows_in[0], len, row_out);
}

void Downsample1x2(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  const size_t N = Lanes(d);
  const auto mul = Set(d, 0.5f);
  float* row0 = rows_in[0];
  float* row1 = rows_in[1];
  for (size_t x = 0; x < len; x += N) {
    Store(Mul(mul, Add(Load(d, row0 + x), Load(d, row1 + x))), d, row_out + x);
  }
}

void Downsample2x2(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  const size_t N = Lanes(d);
  const size_t len_out = len / 2;
  const auto mul = Set(d, 0.25f);
  float* row0 = rows_in[0];
  float* row1 = rows_in[1];
  Vec<D> v0, v1, v2, v3;
  for (size_t x = 0; x < len_out; x += N) {
    LoadInterleaved2(d, row0 + 2 * x, v0, v1);
    LoadInterleaved2(d, row1 + 2 * x, v2, v3);
    Store(Mul(mul, Add(Add(v0, v1), Add(v2, v3))), d, row_out + x);
  }
}

void Downsample3x2(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow3x1(rows_in[0], len, rows_in[0]);
  DownsampleRow3x1(rows_in[1], len, rows_in[1]);
  Downsample1x2(rows_in, len / 3, row_out);
}

void Downsample4x2(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow4x1(rows_in[0], len, rows_in[0]);
  DownsampleRow4x1(rows_in[1], len, rows_in[1]);
  Downsample1x2(rows_in, len / 4, row_out);
}

void Downsample1x3(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  const size_t N = Lanes(d);
  const auto mul = Set(d, 1.0f / 3);
  float* row0 = rows_in[0];
  float* row1 = rows_in[1];
  float* row2 = rows_in[2];
  for (size_t x = 0; x < len; x += N) {
    const auto in0 = Load(d, row0 + x);
    const auto in1 = Load(d, row1 + x);
    const auto in2 = Load(d, row2 + x);
    Store(Mul(mul, Add(Add(in0, in1), in2)), d, row_out + x);
  }
}

void Downsample2x3(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow2x1(rows_in[0], len, rows_in[0]);
  DownsampleRow2x1(rows_in[1], len, rows_in[1]);
  DownsampleRow2x1(rows_in[2], len, rows_in[2]);
  Downsample1x3(rows_in, len / 2, row_out);
}

void Downsample3x3(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow3x1(rows_in[0], len, rows_in[0]);
  DownsampleRow3x1(rows_in[1], len, rows_in[1]);
  DownsampleRow3x1(rows_in[2], len, rows_in[2]);
  Downsample1x3(rows_in, len / 3, row_out);
}

void Downsample4x3(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow4x1(rows_in[0], len, rows_in[0]);
  DownsampleRow4x1(rows_in[1], len, rows_in[1]);
  DownsampleRow4x1(rows_in[2], len, rows_in[2]);
  Downsample1x3(rows_in, len / 4, row_out);
}

void Downsample1x4(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  const size_t N = Lanes(d);
  const auto mul = Set(d, 0.25f);
  float* row0 = rows_in[0];
  float* row1 = rows_in[1];
  float* row2 = rows_in[2];
  float* row3 = rows_in[3];
  for (size_t x = 0; x < len; x += N) {
    const auto in0 = Load(d, row0 + x);
    const auto in1 = Load(d, row1 + x);
    const auto in2 = Load(d, row2 + x);
    const auto in3 = Load(d, row3 + x);
    Store(Mul(mul, Add(Add(in0, in1), Add(in2, in3))), d, row_out + x);
  }
}

void Downsample2x4(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow2x1(rows_in[0], len, rows_in[0]);
  DownsampleRow2x1(rows_in[1], len, rows_in[1]);
  DownsampleRow2x1(rows_in[2], len, rows_in[2]);
  DownsampleRow2x1(rows_in[3], len, rows_in[3]);
  Downsample1x4(rows_in, len / 2, row_out);
}

void Downsample3x4(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow3x1(rows_in[0], len, rows_in[0]);
  DownsampleRow3x1(rows_in[1], len, rows_in[1]);
  DownsampleRow3x1(rows_in[2], len, rows_in[2]);
  DownsampleRow3x1(rows_in[3], len, rows_in[3]);
  Downsample1x4(rows_in, len / 3, row_out);
}

void Downsample4x4(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                   float* row_out) {
  DownsampleRow4x1(rows_in[0], len, rows_in[0]);
  DownsampleRow4x1(rows_in[1], len, rows_in[1]);
  DownsampleRow4x1(rows_in[2], len, rows_in[2]);
  DownsampleRow4x1(rows_in[3], len, rows_in[3]);
  Downsample1x4(rows_in, len / 4, row_out);
}

// NOLINTNEXTLINE(google-readability-namespace-comments)
}  // namespace HWY_NAMESPACE
}  // namespace jpegli
HWY_AFTER_NAMESPACE();

#if HWY_ONCE
namespace jpegli {

HWY_EXPORT(Downsample1x2);
HWY_EXPORT(Downsample1x3);
HWY_EXPORT(Downsample1x4);
HWY_EXPORT(Downsample2x1);
HWY_EXPORT(Downsample2x2);
HWY_EXPORT(Downsample2x3);
HWY_EXPORT(Downsample2x4);
HWY_EXPORT(Downsample3x1);
HWY_EXPORT(Downsample3x2);
HWY_EXPORT(Downsample3x3);
HWY_EXPORT(Downsample3x4);
HWY_EXPORT(Downsample4x1);
HWY_EXPORT(Downsample4x2);
HWY_EXPORT(Downsample4x3);
HWY_EXPORT(Downsample4x4);

void NullDownsample(float* rows_in[MAX_SAMP_FACTOR], size_t len,
                    float* row_out) {}

void ChooseDownsampleMethods(j_compress_ptr cinfo) {
  jpeg_comp_master* m = cinfo->master;
  for (int c = 0; c < cinfo->num_components; c++) {
    m->downsample_method[c] = nullptr;
    jpeg_component_info* comp = &cinfo->comp_info[c];
    const int h_factor = cinfo->max_h_samp_factor / comp->h_samp_factor;
    const int v_factor = cinfo->max_v_samp_factor / comp->v_samp_factor;
    if (v_factor == 1) {
      if (h_factor == 1) {
        m->downsample_method[c] = NullDownsample;
      } else if (h_factor == 2) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample2x1);
      } else if (h_factor == 3) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample3x1);
      } else if (h_factor == 4) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample4x1);
      }
    } else if (v_factor == 2) {
      if (h_factor == 1) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample1x2);
      } else if (h_factor == 2) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample2x2);
      } else if (h_factor == 3) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample3x2);
      } else if (h_factor == 4) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample4x2);
      }
    } else if (v_factor == 3) {
      if (h_factor == 1) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample1x2);
      } else if (h_factor == 2) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample2x2);
      } else if (h_factor == 3) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample3x2);
      } else if (h_factor == 4) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample4x2);
      }
    } else if (v_factor == 4) {
      if (h_factor == 1) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample1x4);
      } else if (h_factor == 2) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample2x4);
      } else if (h_factor == 3) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample3x4);
      } else if (h_factor == 4) {
        m->downsample_method[c] = HWY_DYNAMIC_DISPATCH(Downsample4x4);
      }
    }
    if (m->downsample_method[c] == nullptr) {
      JPEGLI_ERROR("Unsupported downsampling ratio %dx%d", h_factor, v_factor);
    }
  }
}

void DownsampleInputBuffer(j_compress_ptr cinfo) {
  if (cinfo->max_h_samp_factor == 1 && cinfo->max_v_samp_factor == 1) {
    return;
  }
  jpeg_comp_master* m = cinfo->master;
  const size_t iMCU_height = DCTSIZE * cinfo->max_v_samp_factor;
  const size_t y0 = m->next_iMCU_row * iMCU_height;
  const size_t y1 = y0 + iMCU_height;
  const size_t xsize_padded = m->xsize_blocks * DCTSIZE;
  for (int c = 0; c < cinfo->num_components; c++) {
    jpeg_component_info* comp = &cinfo->comp_info[c];
    const int h_factor = cinfo->max_h_samp_factor / comp->h_samp_factor;
    const int v_factor = cinfo->max_v_samp_factor / comp->v_samp_factor;
    if (h_factor == 1 && v_factor == 1) {
      continue;
    }
    auto& input = *m->smooth_input[c];
    auto& output = *m->raw_data[c];
    const size_t yout0 = y0 / v_factor;
    float* rows_in[MAX_SAMP_FACTOR];
    for (size_t yin = y0, yout = yout0; yin < y1; yin += v_factor, ++yout) {
      for (int iy = 0; iy < v_factor; ++iy) {
        rows_in[iy] = input.Row(yin + iy);
      }
      float* row_out = output.Row(yout);
      (*m->downsample_method[c])(rows_in, xsize_padded, row_out);
    }
  }
}

void ApplyInputSmoothing(j_compress_ptr cinfo) {
  if (!cinfo->smoothing_factor) {
    return;
  }
  jpeg_comp_master* m = cinfo->master;
  const float kW1 = cinfo->smoothing_factor / 1024.0;
  const float kW0 = 1.0f - 8.0f * kW1;
  const size_t iMCU_height = DCTSIZE * cinfo->max_v_samp_factor;
  const ssize_t y0 = m->next_iMCU_row * iMCU_height;
  const ssize_t y1 = y0 + iMCU_height;
  const ssize_t xsize_padded = m->xsize_blocks * DCTSIZE;
  for (int c = 0; c < cinfo->num_components; c++) {
    auto& input = m->input_buffer[c];
    auto& output = *m->smooth_input[c];
    if (m->next_iMCU_row == 0) {
      input.CopyRow(-1, 0, 1);
    }
    if (m->next_iMCU_row + 1 == cinfo->total_iMCU_rows) {
      size_t last_row = m->ysize_blocks * DCTSIZE - 1;
      input.CopyRow(last_row + 1, last_row, 1);
    }
    // TODO(szabadka) SIMDify this.
    for (ssize_t y = y0; y < y1; ++y) {
      const float* row_t = input.Row(y - 1);
      const float* row_m = input.Row(y);
      const float* row_b = input.Row(y + 1);
      float* row_out = output.Row(y);
      for (ssize_t x = 0; x < xsize_padded; ++x) {
        float val_tl = row_t[x - 1];
        float val_tm = row_t[x];
        float val_tr = row_t[x + 1];
        float val_ml = row_m[x - 1];
        float val_mm = row_m[x];
        float val_mr = row_m[x + 1];
        float val_bl = row_b[x - 1];
        float val_bm = row_b[x];
        float val_br = row_b[x + 1];
        float val1 = (val_tl + val_tm + val_tr + val_ml + val_mr + val_bl +
                      val_bm + val_br);
        row_out[x] = val_mm * kW0 + val1 * kW1;
      }
    }
  }
}

}  // namespace jpegli
#endif  // HWY_ONCE