Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 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 "test/clear_system_state.h" |
| 12 | #include "test/register_state_check.h" |
| 13 | #include "third_party/googletest/src/include/gtest/gtest.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 14 | #include "./aom_dsp_rtcd.h" |
| 15 | #include "aom/aom_integer.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 16 | #include "aom_dsp/postproc.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 17 | #include "aom_mem/aom_mem.h" |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // TODO(jimbankoski): make width and height integers not unsigned. |
| 22 | typedef void (*AddNoiseFunc)(unsigned char *start, char *noise, |
| 23 | char blackclamp[16], char whiteclamp[16], |
| 24 | char bothclamp[16], unsigned int width, |
| 25 | unsigned int height, int pitch); |
| 26 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 27 | class AddNoiseTest : public ::testing::TestWithParam<AddNoiseFunc> { |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 28 | public: |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 29 | virtual void TearDown() { libaom_test::ClearSystemState(); } |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 30 | virtual ~AddNoiseTest() {} |
| 31 | }; |
| 32 | |
| 33 | double stddev6(char a, char b, char c, char d, char e, char f) { |
| 34 | const double n = (a + b + c + d + e + f) / 6.0; |
| 35 | const double v = ((a - n) * (a - n) + (b - n) * (b - n) + (c - n) * (c - n) + |
| 36 | (d - n) * (d - n) + (e - n) * (e - n) + (f - n) * (f - n)) / |
| 37 | 6.0; |
| 38 | return sqrt(v); |
| 39 | } |
| 40 | |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 41 | TEST_P(AddNoiseTest, CheckNoiseAdded) { |
| 42 | DECLARE_ALIGNED(16, char, blackclamp[16]); |
| 43 | DECLARE_ALIGNED(16, char, whiteclamp[16]); |
| 44 | DECLARE_ALIGNED(16, char, bothclamp[16]); |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 45 | const int width = 64; |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 46 | const int height = 64; |
| 47 | const int image_size = width * height; |
| 48 | char noise[3072]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 49 | const int clamp = aom_setup_noise(4.4, sizeof(noise), noise); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 50 | |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 51 | for (int i = 0; i < 16; i++) { |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 52 | blackclamp[i] = clamp; |
| 53 | whiteclamp[i] = clamp; |
| 54 | bothclamp[i] = 2 * clamp; |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 57 | uint8_t *const s = reinterpret_cast<uint8_t *>(aom_calloc(image_size, 1)); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 58 | memset(s, 99, image_size); |
| 59 | |
| 60 | ASM_REGISTER_STATE_CHECK(GetParam()(s, noise, blackclamp, whiteclamp, |
| 61 | bothclamp, width, height, width)); |
| 62 | |
| 63 | // Check to make sure we don't end up having either the same or no added |
| 64 | // noise either vertically or horizontally. |
| 65 | for (int i = 0; i < image_size - 6 * width - 6; ++i) { |
| 66 | const double hd = stddev6(s[i] - 99, s[i + 1] - 99, s[i + 2] - 99, |
| 67 | s[i + 3] - 99, s[i + 4] - 99, s[i + 5] - 99); |
| 68 | const double vd = stddev6(s[i] - 99, s[i + width] - 99, |
| 69 | s[i + 2 * width] - 99, s[i + 3 * width] - 99, |
| 70 | s[i + 4 * width] - 99, s[i + 5 * width] - 99); |
| 71 | |
| 72 | EXPECT_NE(hd, 0); |
| 73 | EXPECT_NE(vd, 0); |
| 74 | } |
| 75 | |
| 76 | // Initialize pixels in the image to 255 and check for roll over. |
| 77 | memset(s, 255, image_size); |
| 78 | |
| 79 | ASM_REGISTER_STATE_CHECK(GetParam()(s, noise, blackclamp, whiteclamp, |
| 80 | bothclamp, width, height, width)); |
| 81 | |
| 82 | // Check to make sure don't roll over. |
| 83 | for (int i = 0; i < image_size; ++i) { |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 84 | EXPECT_GT(static_cast<int>(s[i]), clamp) << "i = " << i; |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Initialize pixels in the image to 0 and check for roll under. |
| 88 | memset(s, 0, image_size); |
| 89 | |
| 90 | ASM_REGISTER_STATE_CHECK(GetParam()(s, noise, blackclamp, whiteclamp, |
| 91 | bothclamp, width, height, width)); |
| 92 | |
| 93 | // Check to make sure don't roll under. |
| 94 | for (int i = 0; i < image_size; ++i) { |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 95 | EXPECT_LT(static_cast<int>(s[i]), 255 - clamp) << "i = " << i; |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 98 | aom_free(s); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jim Bankoski | 7a91d21 | 2016-05-07 12:47:49 -0700 | [diff] [blame] | 101 | TEST_P(AddNoiseTest, CheckCvsAssembly) { |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 102 | DECLARE_ALIGNED(16, char, blackclamp[16]); |
| 103 | DECLARE_ALIGNED(16, char, whiteclamp[16]); |
| 104 | DECLARE_ALIGNED(16, char, bothclamp[16]); |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 105 | const int width = 64; |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 106 | const int height = 64; |
| 107 | const int image_size = width * height; |
| 108 | char noise[3072]; |
| 109 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 110 | const int clamp = aom_setup_noise(4.4, sizeof(noise), noise); |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 111 | |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 112 | for (int i = 0; i < 16; i++) { |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 113 | blackclamp[i] = clamp; |
| 114 | whiteclamp[i] = clamp; |
| 115 | bothclamp[i] = 2 * clamp; |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 118 | uint8_t *const s = reinterpret_cast<uint8_t *>(aom_calloc(image_size, 1)); |
| 119 | uint8_t *const d = reinterpret_cast<uint8_t *>(aom_calloc(image_size, 1)); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 120 | |
| 121 | memset(s, 99, image_size); |
| 122 | memset(d, 99, image_size); |
| 123 | |
Jim Bankoski | 7a91d21 | 2016-05-07 12:47:49 -0700 | [diff] [blame] | 124 | srand(0); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 125 | ASM_REGISTER_STATE_CHECK(GetParam()(s, noise, blackclamp, whiteclamp, |
| 126 | bothclamp, width, height, width)); |
Jim Bankoski | 7a91d21 | 2016-05-07 12:47:49 -0700 | [diff] [blame] | 127 | srand(0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 128 | ASM_REGISTER_STATE_CHECK(aom_plane_add_noise_c( |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 129 | d, noise, blackclamp, whiteclamp, bothclamp, width, height, width)); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 130 | |
| 131 | for (int i = 0; i < image_size; ++i) { |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 132 | EXPECT_EQ(static_cast<int>(s[i]), static_cast<int>(d[i])) << "i = " << i; |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 135 | aom_free(d); |
| 136 | aom_free(s); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | INSTANTIATE_TEST_CASE_P(C, AddNoiseTest, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 140 | ::testing::Values(aom_plane_add_noise_c)); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 141 | |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 142 | #if HAVE_SSE2 |
| 143 | INSTANTIATE_TEST_CASE_P(SSE2, AddNoiseTest, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 144 | ::testing::Values(aom_plane_add_noise_sse2)); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 145 | #endif |
| 146 | |
| 147 | #if HAVE_MSA |
| 148 | INSTANTIATE_TEST_CASE_P(MSA, AddNoiseTest, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 149 | ::testing::Values(aom_plane_add_noise_msa)); |
Jim Bankoski | 34d5aff | 2016-05-03 16:23:06 -0700 | [diff] [blame] | 150 | #endif |
| 151 | } // namespace |