xref: /aosp_15_r20/external/libvpx/test/vp9_denoiser_test.cc (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <math.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <tuple>
15 
16 #include "gtest/gtest.h"
17 #include "test/acm_random.h"
18 #include "test/clear_system_state.h"
19 #include "test/register_state_check.h"
20 #include "test/util.h"
21 
22 #include "vpx_scale/yv12config.h"
23 #include "vpx/vpx_integer.h"
24 #include "vp9/common/vp9_reconinter.h"
25 #include "vp9/encoder/vp9_context_tree.h"
26 #include "vp9/encoder/vp9_denoiser.h"
27 #include "vpx_config.h"
28 
29 using libvpx_test::ACMRandom;
30 
31 namespace {
32 
33 const int kNumPixels = 64 * 64;
34 
35 typedef int (*Vp9DenoiserFilterFunc)(const uint8_t *sig, int sig_stride,
36                                      const uint8_t *mc_avg, int mc_avg_stride,
37                                      uint8_t *avg, int avg_stride,
38                                      int increase_denoising, BLOCK_SIZE bs,
39                                      int motion_magnitude);
40 typedef std::tuple<Vp9DenoiserFilterFunc, BLOCK_SIZE> VP9DenoiserTestParam;
41 
42 class VP9DenoiserTest
43     : public ::testing::Test,
44       public ::testing::WithParamInterface<VP9DenoiserTestParam> {
45  public:
46   ~VP9DenoiserTest() override = default;
47 
SetUp()48   void SetUp() override { bs_ = GET_PARAM(1); }
49 
TearDown()50   void TearDown() override { libvpx_test::ClearSystemState(); }
51 
52  protected:
53   BLOCK_SIZE bs_;
54 };
55 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VP9DenoiserTest);
56 
TEST_P(VP9DenoiserTest,BitexactCheck)57 TEST_P(VP9DenoiserTest, BitexactCheck) {
58   ACMRandom rnd(ACMRandom::DeterministicSeed());
59   const int count_test_block = 4000;
60 
61   // Allocate the space for input and output,
62   // where sig_block is the block to be denoised,
63   // mc_avg_block is the denoised reference block,
64   // avg_block_c is the denoised result from C code,
65   // avg_block_sse2 is the denoised result from SSE2 code.
66   DECLARE_ALIGNED(16, uint8_t, sig_block[kNumPixels]);
67   DECLARE_ALIGNED(16, uint8_t, mc_avg_block[kNumPixels]);
68   DECLARE_ALIGNED(16, uint8_t, avg_block_c[kNumPixels]);
69   DECLARE_ALIGNED(16, uint8_t, avg_block_sse2[kNumPixels]);
70 
71   for (int i = 0; i < count_test_block; ++i) {
72     // Generate random motion magnitude, 20% of which exceed the threshold.
73     const int motion_magnitude_random =
74         rnd.Rand8() % static_cast<int>(MOTION_MAGNITUDE_THRESHOLD * 1.2);
75 
76     // Initialize a test block with random number in range [0, 255].
77     for (int j = 0; j < kNumPixels; ++j) {
78       int temp = 0;
79       sig_block[j] = rnd.Rand8();
80       // The pixels in mc_avg_block are generated by adding a random
81       // number in range [-19, 19] to corresponding pixels in sig_block.
82       temp =
83           sig_block[j] + ((rnd.Rand8() % 2 == 0) ? -1 : 1) * (rnd.Rand8() % 20);
84       // Clip.
85       mc_avg_block[j] = (temp < 0) ? 0 : ((temp > 255) ? 255 : temp);
86     }
87 
88     ASM_REGISTER_STATE_CHECK(vp9_denoiser_filter_c(sig_block, 64, mc_avg_block,
89                                                    64, avg_block_c, 64, 0, bs_,
90                                                    motion_magnitude_random));
91 
92     ASM_REGISTER_STATE_CHECK(GET_PARAM(0)(sig_block, 64, mc_avg_block, 64,
93                                           avg_block_sse2, 64, 0, bs_,
94                                           motion_magnitude_random));
95 
96     // Test bitexactness.
97     for (int h = 0; h < (4 << b_height_log2_lookup[bs_]); ++h) {
98       for (int w = 0; w < (4 << b_width_log2_lookup[bs_]); ++w) {
99         EXPECT_EQ(avg_block_c[h * 64 + w], avg_block_sse2[h * 64 + w]);
100       }
101     }
102   }
103 }
104 
105 using std::make_tuple;
106 
107 // Test for all block size.
108 #if HAVE_SSE2
109 INSTANTIATE_TEST_SUITE_P(
110     SSE2, VP9DenoiserTest,
111     ::testing::Values(make_tuple(&vp9_denoiser_filter_sse2, BLOCK_8X8),
112                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_8X16),
113                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_16X8),
114                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_16X16),
115                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_16X32),
116                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_32X16),
117                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_32X32),
118                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_32X64),
119                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_64X32),
120                       make_tuple(&vp9_denoiser_filter_sse2, BLOCK_64X64)));
121 #endif  // HAVE_SSE2
122 
123 #if HAVE_NEON
124 INSTANTIATE_TEST_SUITE_P(
125     NEON, VP9DenoiserTest,
126     ::testing::Values(make_tuple(&vp9_denoiser_filter_neon, BLOCK_8X8),
127                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_8X16),
128                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_16X8),
129                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_16X16),
130                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_16X32),
131                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_32X16),
132                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_32X32),
133                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_32X64),
134                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_64X32),
135                       make_tuple(&vp9_denoiser_filter_neon, BLOCK_64X64)));
136 #endif
137 }  // namespace
138