1 /*
2 * Copyright (c) 2016 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 #include <math.h>
11 #include <tuple>
12
13 #include "gtest/gtest.h"
14 #include "test/clear_system_state.h"
15 #include "test/register_state_check.h"
16 #include "test/util.h"
17 #include "./vpx_dsp_rtcd.h"
18 #include "vpx/vpx_integer.h"
19 #include "vpx_config.h"
20 #include "vpx_dsp/postproc.h"
21 #include "vpx_mem/vpx_mem.h"
22
23 namespace {
24
25 static const int kNoiseSize = 3072;
26
27 typedef void (*AddNoiseFunc)(uint8_t *start, const int8_t *noise,
28 int blackclamp, int whiteclamp, int width,
29 int height, int pitch);
30
31 typedef std::tuple<double, AddNoiseFunc> AddNoiseTestFPParam;
32
33 class AddNoiseTest : public ::testing::Test,
34 public ::testing::WithParamInterface<AddNoiseTestFPParam> {
35 public:
TearDown()36 void TearDown() override { libvpx_test::ClearSystemState(); }
37 ~AddNoiseTest() override = default;
38 };
39
stddev6(char a,char b,char c,char d,char e,char f)40 double stddev6(char a, char b, char c, char d, char e, char f) {
41 const double n = (a + b + c + d + e + f) / 6.0;
42 const double v = ((a - n) * (a - n) + (b - n) * (b - n) + (c - n) * (c - n) +
43 (d - n) * (d - n) + (e - n) * (e - n) + (f - n) * (f - n)) /
44 6.0;
45 return sqrt(v);
46 }
47
TEST_P(AddNoiseTest,CheckNoiseAdded)48 TEST_P(AddNoiseTest, CheckNoiseAdded) {
49 const int width = 64;
50 const int height = 64;
51 const int image_size = width * height;
52 int8_t noise[kNoiseSize];
53 const int clamp = vpx_setup_noise(GET_PARAM(0), noise, kNoiseSize);
54 uint8_t *const s =
55 reinterpret_cast<uint8_t *>(vpx_calloc(image_size, sizeof(*s)));
56 ASSERT_NE(s, nullptr);
57 memset(s, 99, image_size * sizeof(*s));
58
59 ASM_REGISTER_STATE_CHECK(
60 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
61
62 // Check to make sure we don't end up having either the same or no added
63 // noise either vertically or horizontally.
64 for (int i = 0; i < image_size - 6 * width - 6; ++i) {
65 const double hd = stddev6(s[i] - 99, s[i + 1] - 99, s[i + 2] - 99,
66 s[i + 3] - 99, s[i + 4] - 99, s[i + 5] - 99);
67 const double vd = stddev6(s[i] - 99, s[i + width] - 99,
68 s[i + 2 * width] - 99, s[i + 3 * width] - 99,
69 s[i + 4 * width] - 99, s[i + 5 * width] - 99);
70
71 EXPECT_NE(hd, 0);
72 EXPECT_NE(vd, 0);
73 }
74
75 // Initialize pixels in the image to 255 and check for roll over.
76 memset(s, 255, image_size);
77
78 ASM_REGISTER_STATE_CHECK(
79 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
80
81 // Check to make sure don't roll over.
82 for (int i = 0; i < image_size; ++i) {
83 EXPECT_GT(static_cast<int>(s[i]), clamp) << "i = " << i;
84 }
85
86 // Initialize pixels in the image to 0 and check for roll under.
87 memset(s, 0, image_size);
88
89 ASM_REGISTER_STATE_CHECK(
90 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
91
92 // Check to make sure don't roll under.
93 for (int i = 0; i < image_size; ++i) {
94 EXPECT_LT(static_cast<int>(s[i]), 255 - clamp) << "i = " << i;
95 }
96
97 vpx_free(s);
98 }
99
TEST_P(AddNoiseTest,CheckCvsAssembly)100 TEST_P(AddNoiseTest, CheckCvsAssembly) {
101 const int width = 64;
102 const int height = 64;
103 const int image_size = width * height;
104 int8_t noise[kNoiseSize];
105 const int clamp = vpx_setup_noise(4.4, noise, kNoiseSize);
106
107 uint8_t *const s = reinterpret_cast<uint8_t *>(vpx_calloc(image_size, 1));
108 uint8_t *const d = reinterpret_cast<uint8_t *>(vpx_calloc(image_size, 1));
109 ASSERT_NE(s, nullptr);
110 ASSERT_NE(d, nullptr);
111
112 memset(s, 99, image_size);
113 memset(d, 99, image_size);
114
115 srand(0);
116 ASM_REGISTER_STATE_CHECK(
117 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
118 srand(0);
119 ASM_REGISTER_STATE_CHECK(
120 vpx_plane_add_noise_c(d, noise, clamp, clamp, width, height, width));
121
122 for (int i = 0; i < image_size; ++i) {
123 EXPECT_EQ(static_cast<int>(s[i]), static_cast<int>(d[i])) << "i = " << i;
124 }
125
126 vpx_free(d);
127 vpx_free(s);
128 }
129
130 using std::make_tuple;
131
132 INSTANTIATE_TEST_SUITE_P(
133 C, AddNoiseTest,
134 ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_c),
135 make_tuple(4.4, vpx_plane_add_noise_c)));
136
137 #if HAVE_SSE2
138 INSTANTIATE_TEST_SUITE_P(
139 SSE2, AddNoiseTest,
140 ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_sse2),
141 make_tuple(4.4, vpx_plane_add_noise_sse2)));
142 #endif
143
144 #if HAVE_MSA
145 INSTANTIATE_TEST_SUITE_P(
146 MSA, AddNoiseTest,
147 ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_msa),
148 make_tuple(4.4, vpx_plane_add_noise_msa)));
149 #endif
150 } // namespace
151