2 * Copyright (c) 2016 The WebM project authors. All Rights Reserved.
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.
13 #include "test/clear_system_state.h"
14 #include "test/register_state_check.h"
15 #include "test/util.h"
16 #include "third_party/googletest/src/include/gtest/gtest.h"
17 #include "./vpx_dsp_rtcd.h"
18 #include "vpx/vpx_integer.h"
19 #include "vpx_dsp/postproc.h"
20 #include "vpx_mem/vpx_mem.h"
24 static const int kNoiseSize = 3072;
26 // TODO(jimbankoski): make width and height integers not unsigned.
27 typedef void (*AddNoiseFunc)(uint8_t *start, const int8_t *noise,
28 int blackclamp, int whiteclamp, int width,
29 int height, int pitch);
31 typedef std::tuple<double, AddNoiseFunc> AddNoiseTestFPParam;
33 class AddNoiseTest : public ::testing::Test,
34 public ::testing::WithParamInterface<AddNoiseTestFPParam> {
36 virtual void TearDown() { libvpx_test::ClearSystemState(); }
37 virtual ~AddNoiseTest() {}
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)) /
48 TEST_P(AddNoiseTest, CheckNoiseAdded) {
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);
55 reinterpret_cast<uint8_t *>(vpx_calloc(image_size, sizeof(*s)));
56 ASSERT_TRUE(s != NULL);
57 memset(s, 99, image_size * sizeof(*s));
59 ASM_REGISTER_STATE_CHECK(
60 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
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);
75 // Initialize pixels in the image to 255 and check for roll over.
76 memset(s, 255, image_size);
78 ASM_REGISTER_STATE_CHECK(
79 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
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;
86 // Initialize pixels in the image to 0 and check for roll under.
87 memset(s, 0, image_size);
89 ASM_REGISTER_STATE_CHECK(
90 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
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;
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);
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_TRUE(s != NULL);
110 ASSERT_TRUE(d != NULL);
112 memset(s, 99, image_size);
113 memset(d, 99, image_size);
116 ASM_REGISTER_STATE_CHECK(
117 GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
119 ASM_REGISTER_STATE_CHECK(
120 vpx_plane_add_noise_c(d, noise, clamp, clamp, width, height, width));
122 for (int i = 0; i < image_size; ++i) {
123 EXPECT_EQ(static_cast<int>(s[i]), static_cast<int>(d[i])) << "i = " << i;
130 using std::make_tuple;
132 INSTANTIATE_TEST_CASE_P(
134 ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_c),
135 make_tuple(4.4, vpx_plane_add_noise_c)));
138 INSTANTIATE_TEST_CASE_P(
140 ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_sse2),
141 make_tuple(4.4, vpx_plane_add_noise_sse2)));
145 INSTANTIATE_TEST_CASE_P(
147 ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_msa),
148 make_tuple(4.4, vpx_plane_add_noise_msa)));