Coverage Report

Created: 2025-07-23 07:47

/src/libjxl/lib/jxl/dec_noise.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/dec_noise.h"
7
8
#include <cstdint>
9
#include <cstdlib>
10
#include <cstring>
11
#include <utility>
12
13
#include "lib/jxl/base/status.h"
14
#include "lib/jxl/dec_bit_reader.h"
15
#include "lib/jxl/dec_cache.h"
16
#include "lib/jxl/frame_header.h"
17
#include "lib/jxl/noise.h"
18
#include "lib/jxl/render_pipeline/render_pipeline.h"
19
20
#undef HWY_TARGET_INCLUDE
21
#define HWY_TARGET_INCLUDE "lib/jxl/dec_noise.cc"
22
#include <hwy/foreach_target.h>
23
#include <hwy/highway.h>
24
25
#include "lib/jxl/base/compiler_specific.h"
26
#include "lib/jxl/base/rect.h"
27
#include "lib/jxl/frame_dimensions.h"
28
#include "lib/jxl/image.h"
29
#include "lib/jxl/xorshift128plus-inl.h"
30
31
HWY_BEFORE_NAMESPACE();
32
namespace jxl {
33
namespace HWY_NAMESPACE {
34
35
// These templates are not found via ADL.
36
using hwy::HWY_NAMESPACE::Or;
37
using hwy::HWY_NAMESPACE::Rebind;
38
using hwy::HWY_NAMESPACE::ShiftRight;
39
using hwy::HWY_NAMESPACE::Vec;
40
41
using D = HWY_CAPPED(float, kBlockDim);
42
using DI = Rebind<int, D>;
43
44
// Converts one vector's worth of random bits to floats in [1, 2).
45
// NOTE: as the convolution kernel sums to 0, it doesn't matter if inputs are in
46
// [0, 1) or in [1, 2).
47
void BitsToFloat(const uint32_t* JXL_RESTRICT random_bits,
48
124M
                 float* JXL_RESTRICT floats) {
49
124M
  const HWY_FULL(float) df;
50
124M
  const HWY_FULL(uint32_t) du;
51
52
124M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
124M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
124M
  Store(rand12, df, floats);
56
124M
}
jxl::N_SSE4::BitsToFloat(unsigned int const*, float*)
Line
Count
Source
48
26.5M
                 float* JXL_RESTRICT floats) {
49
26.5M
  const HWY_FULL(float) df;
50
26.5M
  const HWY_FULL(uint32_t) du;
51
52
26.5M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
26.5M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
26.5M
  Store(rand12, df, floats);
56
26.5M
}
jxl::N_AVX2::BitsToFloat(unsigned int const*, float*)
Line
Count
Source
48
79.7M
                 float* JXL_RESTRICT floats) {
49
79.7M
  const HWY_FULL(float) df;
50
79.7M
  const HWY_FULL(uint32_t) du;
51
52
79.7M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
79.7M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
79.7M
  Store(rand12, df, floats);
56
79.7M
}
jxl::N_SSE2::BitsToFloat(unsigned int const*, float*)
Line
Count
Source
48
18.1M
                 float* JXL_RESTRICT floats) {
49
18.1M
  const HWY_FULL(float) df;
50
18.1M
  const HWY_FULL(uint32_t) du;
51
52
18.1M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
18.1M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
18.1M
  Store(rand12, df, floats);
56
18.1M
}
57
58
void RandomImage(Xorshift128Plus* rng, const Rect& rect,
59
209k
                 ImageF* JXL_RESTRICT noise) {
60
209k
  const size_t xsize = rect.xsize();
61
209k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
209k
  constexpr size_t kFloatsPerBatch =
65
209k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
209k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
209k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
209k
  const HWY_FULL(float) df;
70
209k
  const size_t N = Lanes(df);
71
72
7.67M
  for (size_t y = 0; y < ysize; ++y) {
73
7.46M
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
7.46M
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
55.1M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
47.6M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
47.6M
      memcpy(batch32, batch64, sizeof(batch32));
81
165M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
117M
        BitsToFloat(batch32 + i, row + x + i);
83
117M
      }
84
47.6M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
7.46M
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
7.46M
    memcpy(batch32, batch64, sizeof(batch32));
90
7.46M
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
18.1M
    for (; x < xsize; x += N) {
92
10.7M
      BitsToFloat(batch32 + batch_pos, row + x);
93
10.7M
      batch_pos += N;
94
10.7M
    }
95
7.46M
  }
96
209k
}
dec_noise.cc:jxl::N_SSE4::RandomImage(jxl::N_SSE4::(anonymous namespace)::Xorshift128Plus*, jxl::RectT<unsigned long> const&, jxl::Plane<float>*)
Line
Count
Source
59
30.5k
                 ImageF* JXL_RESTRICT noise) {
60
30.5k
  const size_t xsize = rect.xsize();
61
30.5k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
30.5k
  constexpr size_t kFloatsPerBatch =
65
30.5k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
30.5k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
30.5k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
30.5k
  const HWY_FULL(float) df;
70
30.5k
  const size_t N = Lanes(df);
71
72
943k
  for (size_t y = 0; y < ysize; ++y) {
73
913k
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
913k
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
7.56M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
6.65M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
6.65M
      memcpy(batch32, batch64, sizeof(batch32));
81
33.2M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
26.5M
        BitsToFloat(batch32 + i, row + x + i);
83
26.5M
      }
84
6.65M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
913k
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
913k
    memcpy(batch32, batch64, sizeof(batch32));
90
913k
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
2.64M
    for (; x < xsize; x += N) {
92
1.73M
      BitsToFloat(batch32 + batch_pos, row + x);
93
1.73M
      batch_pos += N;
94
1.73M
    }
95
913k
  }
96
30.5k
}
dec_noise.cc:jxl::N_AVX2::RandomImage(jxl::N_AVX2::(anonymous namespace)::Xorshift128Plus*, jxl::RectT<unsigned long> const&, jxl::Plane<float>*)
Line
Count
Source
59
158k
                 ImageF* JXL_RESTRICT noise) {
60
158k
  const size_t xsize = rect.xsize();
61
158k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
158k
  constexpr size_t kFloatsPerBatch =
65
158k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
158k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
158k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
158k
  const HWY_FULL(float) df;
70
158k
  const size_t N = Lanes(df);
71
72
6.08M
  for (size_t y = 0; y < ysize; ++y) {
73
5.92M
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
5.92M
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
42.4M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
36.5M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
36.5M
      memcpy(batch32, batch64, sizeof(batch32));
81
109M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
73.0M
        BitsToFloat(batch32 + i, row + x + i);
83
73.0M
      }
84
36.5M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
5.92M
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
5.92M
    memcpy(batch32, batch64, sizeof(batch32));
90
5.92M
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
13.5M
    for (; x < xsize; x += N) {
92
7.58M
      BitsToFloat(batch32 + batch_pos, row + x);
93
7.58M
      batch_pos += N;
94
7.58M
    }
95
5.92M
  }
96
158k
}
dec_noise.cc:jxl::N_SSE2::RandomImage(jxl::N_SSE2::(anonymous namespace)::Xorshift128Plus*, jxl::RectT<unsigned long> const&, jxl::Plane<float>*)
Line
Count
Source
59
20.3k
                 ImageF* JXL_RESTRICT noise) {
60
20.3k
  const size_t xsize = rect.xsize();
61
20.3k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
20.3k
  constexpr size_t kFloatsPerBatch =
65
20.3k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
20.3k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
20.3k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
20.3k
  const HWY_FULL(float) df;
70
20.3k
  const size_t N = Lanes(df);
71
72
654k
  for (size_t y = 0; y < ysize; ++y) {
73
634k
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
634k
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
5.12M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
4.48M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
4.48M
      memcpy(batch32, batch64, sizeof(batch32));
81
22.4M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
17.9M
        BitsToFloat(batch32 + i, row + x + i);
83
17.9M
      }
84
4.48M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
634k
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
634k
    memcpy(batch32, batch64, sizeof(batch32));
90
634k
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
2.02M
    for (; x < xsize; x += N) {
92
1.39M
      BitsToFloat(batch32 + batch_pos, row + x);
93
1.39M
      batch_pos += N;
94
1.39M
    }
95
634k
  }
96
20.3k
}
97
void Random3Planes(size_t visible_frame_index, size_t nonvisible_frame_index,
98
                   size_t x0, size_t y0, const std::pair<ImageF*, Rect>& plane0,
99
                   const std::pair<ImageF*, Rect>& plane1,
100
69.7k
                   const std::pair<ImageF*, Rect>& plane2) {
101
69.7k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
69.7k
                                y0);
103
69.7k
  RandomImage(&rng, plane0.second, plane0.first);
104
69.7k
  RandomImage(&rng, plane1.second, plane1.first);
105
69.7k
  RandomImage(&rng, plane2.second, plane2.first);
106
69.7k
}
jxl::N_SSE4::Random3Planes(unsigned long, unsigned long, unsigned long, unsigned long, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&)
Line
Count
Source
100
10.1k
                   const std::pair<ImageF*, Rect>& plane2) {
101
10.1k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
10.1k
                                y0);
103
10.1k
  RandomImage(&rng, plane0.second, plane0.first);
104
10.1k
  RandomImage(&rng, plane1.second, plane1.first);
105
10.1k
  RandomImage(&rng, plane2.second, plane2.first);
106
10.1k
}
jxl::N_AVX2::Random3Planes(unsigned long, unsigned long, unsigned long, unsigned long, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&)
Line
Count
Source
100
52.7k
                   const std::pair<ImageF*, Rect>& plane2) {
101
52.7k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
52.7k
                                y0);
103
52.7k
  RandomImage(&rng, plane0.second, plane0.first);
104
52.7k
  RandomImage(&rng, plane1.second, plane1.first);
105
52.7k
  RandomImage(&rng, plane2.second, plane2.first);
106
52.7k
}
jxl::N_SSE2::Random3Planes(unsigned long, unsigned long, unsigned long, unsigned long, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&, std::__1::pair<jxl::Plane<float>*, jxl::RectT<unsigned long> > const&)
Line
Count
Source
100
6.78k
                   const std::pair<ImageF*, Rect>& plane2) {
101
6.78k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
6.78k
                                y0);
103
6.78k
  RandomImage(&rng, plane0.second, plane0.first);
104
6.78k
  RandomImage(&rng, plane1.second, plane1.first);
105
6.78k
  RandomImage(&rng, plane2.second, plane2.first);
106
6.78k
}
107
108
// NOLINTNEXTLINE(google-readability-namespace-comments)
109
}  // namespace HWY_NAMESPACE
110
}  // namespace jxl
111
HWY_AFTER_NAMESPACE();
112
113
#if HWY_ONCE
114
namespace jxl {
115
116
namespace {
117
HWY_EXPORT(Random3Planes);
118
}  // namespace
119
120
void PrepareNoiseInput(const PassesDecoderState& dec_state,
121
                       const FrameDimensions& frame_dim,
122
                       const FrameHeader& frame_header, size_t group_index,
123
16.5k
                       size_t thread) {
124
16.5k
  size_t group_dim = frame_dim.group_dim;
125
16.5k
  const size_t gx = group_index % frame_dim.xsize_groups;
126
16.5k
  const size_t gy = group_index / frame_dim.xsize_groups;
127
16.5k
  RenderPipelineInput input =
128
16.5k
      dec_state.render_pipeline->GetInputBuffers(group_index, thread);
129
16.5k
  size_t noise_c_start =
130
16.5k
      3 + frame_header.nonserialized_metadata->m.num_extra_channels;
131
  // When the color channels are downsampled, we need to generate more noise
132
  // input for the current group than just the group dimensions.
133
16.5k
  std::pair<ImageF*, Rect> rects[3];
134
43.0k
  for (size_t iy = 0; iy < frame_header.upsampling; iy++) {
135
96.1k
    for (size_t ix = 0; ix < frame_header.upsampling; ix++) {
136
279k
      for (size_t c = 0; c < 3; c++) {
137
209k
        auto r = input.GetBuffer(noise_c_start + c);
138
209k
        rects[c].first = r.first;
139
209k
        size_t x1 = r.second.x0() + r.second.xsize();
140
209k
        size_t y1 = r.second.y0() + r.second.ysize();
141
209k
        rects[c].second =
142
209k
            Rect(r.second.x0() + ix * group_dim, r.second.y0() + iy * group_dim,
143
209k
                 group_dim, group_dim, x1, y1);
144
209k
      }
145
69.7k
      HWY_DYNAMIC_DISPATCH(Random3Planes)
146
69.7k
      (dec_state.visible_frame_index, dec_state.nonvisible_frame_index,
147
69.7k
       (gx * frame_header.upsampling + ix) * group_dim,
148
69.7k
       (gy * frame_header.upsampling + iy) * group_dim, rects[0], rects[1],
149
69.7k
       rects[2]);
150
69.7k
    }
151
26.4k
  }
152
16.5k
}
153
154
115k
void DecodeFloatParam(float precision, float* val, BitReader* br) {
155
115k
  const int absval_quant = br->ReadFixedBits<10>();
156
115k
  *val = absval_quant / precision;
157
115k
}
158
159
14.4k
Status DecodeNoise(BitReader* br, NoiseParams* noise_params) {
160
115k
  for (float& i : noise_params->lut) {
161
115k
    DecodeFloatParam(kNoisePrecision, &i, br);
162
115k
  }
163
14.4k
  return true;
164
14.4k
}
165
166
}  // namespace jxl
167
#endif  // HWY_ONCE