Coverage Report

Created: 2025-12-03 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
152M
                 float* JXL_RESTRICT floats) {
49
152M
  const HWY_FULL(float) df;
50
152M
  const HWY_FULL(uint32_t) du;
51
52
152M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
152M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
152M
  Store(rand12, df, floats);
56
152M
}
jxl::N_SSE4::BitsToFloat(unsigned int const*, float*)
Line
Count
Source
48
30.3M
                 float* JXL_RESTRICT floats) {
49
30.3M
  const HWY_FULL(float) df;
50
30.3M
  const HWY_FULL(uint32_t) du;
51
52
30.3M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
30.3M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
30.3M
  Store(rand12, df, floats);
56
30.3M
}
jxl::N_AVX2::BitsToFloat(unsigned int const*, float*)
Line
Count
Source
48
90.0M
                 float* JXL_RESTRICT floats) {
49
90.0M
  const HWY_FULL(float) df;
50
90.0M
  const HWY_FULL(uint32_t) du;
51
52
90.0M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
90.0M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
90.0M
  Store(rand12, df, floats);
56
90.0M
}
jxl::N_SSE2::BitsToFloat(unsigned int const*, float*)
Line
Count
Source
48
31.9M
                 float* JXL_RESTRICT floats) {
49
31.9M
  const HWY_FULL(float) df;
50
31.9M
  const HWY_FULL(uint32_t) du;
51
52
31.9M
  const auto bits = Load(du, random_bits);
53
  // 1.0 + 23 random mantissa bits = [1, 2)
54
31.9M
  const auto rand12 = BitCast(df, Or(ShiftRight<9>(bits), Set(du, 0x3F800000)));
55
31.9M
  Store(rand12, df, floats);
56
31.9M
}
57
58
void RandomImage(Xorshift128Plus* rng, const Rect& rect,
59
205k
                 ImageF* JXL_RESTRICT noise) {
60
205k
  const size_t xsize = rect.xsize();
61
205k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
205k
  constexpr size_t kFloatsPerBatch =
65
205k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
205k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
205k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
205k
  const HWY_FULL(float) df;
70
205k
  const size_t N = Lanes(df);
71
72
9.37M
  for (size_t y = 0; y < ysize; ++y) {
73
9.17M
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
9.17M
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
67.1M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
57.9M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
57.9M
      memcpy(batch32, batch64, sizeof(batch32));
81
205M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
147M
        BitsToFloat(batch32 + i, row + x + i);
83
147M
      }
84
57.9M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
9.17M
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
9.17M
    memcpy(batch32, batch64, sizeof(batch32));
90
9.17M
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
19.1M
    for (; x < xsize; x += N) {
92
9.94M
      BitsToFloat(batch32 + batch_pos, row + x);
93
9.94M
      batch_pos += N;
94
9.94M
    }
95
9.17M
  }
96
205k
}
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
28.2k
                 ImageF* JXL_RESTRICT noise) {
60
28.2k
  const size_t xsize = rect.xsize();
61
28.2k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
28.2k
  constexpr size_t kFloatsPerBatch =
65
28.2k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
28.2k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
28.2k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
28.2k
  const HWY_FULL(float) df;
70
28.2k
  const size_t N = Lanes(df);
71
72
1.65M
  for (size_t y = 0; y < ysize; ++y) {
73
1.62M
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
1.62M
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
9.52M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
7.90M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
7.90M
      memcpy(batch32, batch64, sizeof(batch32));
81
39.3M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
31.4M
        BitsToFloat(batch32 + i, row + x + i);
83
31.4M
      }
84
7.90M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
1.62M
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
1.62M
    memcpy(batch32, batch64, sizeof(batch32));
90
1.62M
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
3.25M
    for (; x < xsize; x += N) {
92
1.63M
      BitsToFloat(batch32 + batch_pos, row + x);
93
1.63M
      batch_pos += N;
94
1.63M
    }
95
1.62M
  }
96
28.2k
}
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
142k
                 ImageF* JXL_RESTRICT noise) {
60
142k
  const size_t xsize = rect.xsize();
61
142k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
142k
  constexpr size_t kFloatsPerBatch =
65
142k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
142k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
142k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
142k
  const HWY_FULL(float) df;
70
142k
  const size_t N = Lanes(df);
71
72
6.32M
  for (size_t y = 0; y < ysize; ++y) {
73
6.17M
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
6.17M
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
48.0M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
41.8M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
41.8M
      memcpy(batch32, batch64, sizeof(batch32));
81
125M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
83.6M
        BitsToFloat(batch32 + i, row + x + i);
83
83.6M
      }
84
41.8M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
6.17M
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
6.17M
    memcpy(batch32, batch64, sizeof(batch32));
90
6.17M
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
13.0M
    for (; x < xsize; x += N) {
92
6.86M
      BitsToFloat(batch32 + batch_pos, row + x);
93
6.86M
      batch_pos += N;
94
6.86M
    }
95
6.17M
  }
96
142k
}
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
34.4k
                 ImageF* JXL_RESTRICT noise) {
60
34.4k
  const size_t xsize = rect.xsize();
61
34.4k
  const size_t ysize = rect.ysize();
62
63
  // May exceed the vector size, hence we have two loops over x below.
64
34.4k
  constexpr size_t kFloatsPerBatch =
65
34.4k
      Xorshift128Plus::N * sizeof(uint64_t) / sizeof(float);
66
34.4k
  HWY_ALIGN uint64_t batch64[Xorshift128Plus::N] = {};
67
34.4k
  HWY_ALIGN uint32_t batch32[2 * Xorshift128Plus::N];
68
69
34.4k
  const HWY_FULL(float) df;
70
34.4k
  const size_t N = Lanes(df);
71
72
1.40M
  for (size_t y = 0; y < ysize; ++y) {
73
1.37M
    float* JXL_RESTRICT row = rect.Row(noise, y);
74
75
1.37M
    size_t x = 0;
76
    // Only entire batches (avoids exceeding the image padding).
77
9.58M
    for (; x + kFloatsPerBatch < xsize; x += kFloatsPerBatch) {
78
8.21M
      rng->Fill(batch64);
79
      // Workaround for https://github.com/llvm/llvm-project/issues/121229
80
8.21M
      memcpy(batch32, batch64, sizeof(batch32));
81
40.9M
      for (size_t i = 0; i < kFloatsPerBatch; i += Lanes(df)) {
82
32.7M
        BitsToFloat(batch32 + i, row + x + i);
83
32.7M
      }
84
8.21M
    }
85
86
    // Any remaining pixels, rounded up to vectors (safe due to padding).
87
1.37M
    rng->Fill(batch64);
88
    // Workaround for https://github.com/llvm/llvm-project/issues/121229
89
1.37M
    memcpy(batch32, batch64, sizeof(batch32));
90
1.37M
    size_t batch_pos = 0;  // < kFloatsPerBatch
91
2.82M
    for (; x < xsize; x += N) {
92
1.44M
      BitsToFloat(batch32 + batch_pos, row + x);
93
1.44M
      batch_pos += N;
94
1.44M
    }
95
1.37M
  }
96
34.4k
}
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
68.4k
                   const std::pair<ImageF*, Rect>& plane2) {
101
68.4k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
68.4k
                                y0);
103
68.4k
  RandomImage(&rng, plane0.second, plane0.first);
104
68.4k
  RandomImage(&rng, plane1.second, plane1.first);
105
68.4k
  RandomImage(&rng, plane2.second, plane2.first);
106
68.4k
}
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
9.41k
                   const std::pair<ImageF*, Rect>& plane2) {
101
9.41k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
9.41k
                                y0);
103
9.41k
  RandomImage(&rng, plane0.second, plane0.first);
104
9.41k
  RandomImage(&rng, plane1.second, plane1.first);
105
9.41k
  RandomImage(&rng, plane2.second, plane2.first);
106
9.41k
}
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
47.5k
                   const std::pair<ImageF*, Rect>& plane2) {
101
47.5k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
47.5k
                                y0);
103
47.5k
  RandomImage(&rng, plane0.second, plane0.first);
104
47.5k
  RandomImage(&rng, plane1.second, plane1.first);
105
47.5k
  RandomImage(&rng, plane2.second, plane2.first);
106
47.5k
}
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
11.4k
                   const std::pair<ImageF*, Rect>& plane2) {
101
11.4k
  HWY_ALIGN Xorshift128Plus rng(visible_frame_index, nonvisible_frame_index, x0,
102
11.4k
                                y0);
103
11.4k
  RandomImage(&rng, plane0.second, plane0.first);
104
11.4k
  RandomImage(&rng, plane1.second, plane1.first);
105
11.4k
  RandomImage(&rng, plane2.second, plane2.first);
106
11.4k
}
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
14.2k
                       size_t thread) {
124
14.2k
  size_t group_dim = frame_dim.group_dim;
125
14.2k
  const size_t gx = group_index % frame_dim.xsize_groups;
126
14.2k
  const size_t gy = group_index / frame_dim.xsize_groups;
127
14.2k
  RenderPipelineInput input =
128
14.2k
      dec_state.render_pipeline->GetInputBuffers(group_index, thread);
129
14.2k
  size_t noise_c_start =
130
14.2k
      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
14.2k
  std::pair<ImageF*, Rect> rects[3];
134
37.8k
  for (size_t iy = 0; iy < frame_header.upsampling; iy++) {
135
92.0k
    for (size_t ix = 0; ix < frame_header.upsampling; ix++) {
136
273k
      for (size_t c = 0; c < 3; c++) {
137
205k
        auto r = input.GetBuffer(noise_c_start + c);
138
205k
        rects[c].first = r.first;
139
205k
        size_t x1 = r.second.x0() + r.second.xsize();
140
205k
        size_t y1 = r.second.y0() + r.second.ysize();
141
205k
        rects[c].second =
142
205k
            Rect(r.second.x0() + ix * group_dim, r.second.y0() + iy * group_dim,
143
205k
                 group_dim, group_dim, x1, y1);
144
205k
      }
145
68.4k
      HWY_DYNAMIC_DISPATCH(Random3Planes)
146
68.4k
      (dec_state.visible_frame_index, dec_state.nonvisible_frame_index,
147
68.4k
       (gx * frame_header.upsampling + ix) * group_dim,
148
68.4k
       (gy * frame_header.upsampling + iy) * group_dim, rects[0], rects[1],
149
68.4k
       rects[2]);
150
68.4k
    }
151
23.6k
  }
152
14.2k
}
153
154
113k
void DecodeFloatParam(float precision, float* val, BitReader* br) {
155
113k
  const int absval_quant = br->ReadFixedBits<10>();
156
113k
  *val = absval_quant / precision;
157
113k
}
158
159
14.1k
Status DecodeNoise(BitReader* br, NoiseParams* noise_params) {
160
113k
  for (float& i : noise_params->lut) {
161
113k
    DecodeFloatParam(kNoisePrecision, &i, br);
162
113k
  }
163
14.1k
  return true;
164
14.1k
}
165
166
}  // namespace jxl
167
#endif  // HWY_ONCE