blob: 768ac362d36588c008b4de2687f8d22ce86dad40 [file] [log] [blame]
Jim Bankoski34d5aff2016-05-03 16:23:06 -07001/*
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 Xuf883b422016-08-30 14:01:10 -070014#include "./aom_dsp_rtcd.h"
15#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016#include "aom_dsp/postproc.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom_mem/aom_mem.h"
Jim Bankoski34d5aff2016-05-03 16:23:06 -070018
19namespace {
20
21// TODO(jimbankoski): make width and height integers not unsigned.
22typedef 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-format3a826f12016-08-11 17:46:05 -070027class AddNoiseTest : public ::testing::TestWithParam<AddNoiseFunc> {
Jim Bankoski34d5aff2016-05-03 16:23:06 -070028 public:
Yaowu Xuc27fc142016-08-22 16:08:15 -070029 virtual void TearDown() { libaom_test::ClearSystemState(); }
Jim Bankoski34d5aff2016-05-03 16:23:06 -070030 virtual ~AddNoiseTest() {}
31};
32
33double 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 Bankoski34d5aff2016-05-03 16:23:06 -070041TEST_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-format3a826f12016-08-11 17:46:05 -070045 const int width = 64;
Jim Bankoski34d5aff2016-05-03 16:23:06 -070046 const int height = 64;
47 const int image_size = width * height;
48 char noise[3072];
Yaowu Xuf883b422016-08-30 14:01:10 -070049 const int clamp = aom_setup_noise(4.4, sizeof(noise), noise);
Jim Bankoski34d5aff2016-05-03 16:23:06 -070050
Jim Bankoski34d5aff2016-05-03 16:23:06 -070051 for (int i = 0; i < 16; i++) {
Johann2967bf32016-06-22 16:08:10 -070052 blackclamp[i] = clamp;
53 whiteclamp[i] = clamp;
54 bothclamp[i] = 2 * clamp;
Jim Bankoski34d5aff2016-05-03 16:23:06 -070055 }
56
Yaowu Xuf883b422016-08-30 14:01:10 -070057 uint8_t *const s = reinterpret_cast<uint8_t *>(aom_calloc(image_size, 1));
Jim Bankoski34d5aff2016-05-03 16:23:06 -070058 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) {
Johann2967bf32016-06-22 16:08:10 -070084 EXPECT_GT(static_cast<int>(s[i]), clamp) << "i = " << i;
Jim Bankoski34d5aff2016-05-03 16:23:06 -070085 }
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) {
Johann2967bf32016-06-22 16:08:10 -070095 EXPECT_LT(static_cast<int>(s[i]), 255 - clamp) << "i = " << i;
Jim Bankoski34d5aff2016-05-03 16:23:06 -070096 }
97
Yaowu Xuf883b422016-08-30 14:01:10 -070098 aom_free(s);
Jim Bankoski34d5aff2016-05-03 16:23:06 -070099}
100
Jim Bankoski7a91d212016-05-07 12:47:49 -0700101TEST_P(AddNoiseTest, CheckCvsAssembly) {
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700102 DECLARE_ALIGNED(16, char, blackclamp[16]);
103 DECLARE_ALIGNED(16, char, whiteclamp[16]);
104 DECLARE_ALIGNED(16, char, bothclamp[16]);
clang-format3a826f12016-08-11 17:46:05 -0700105 const int width = 64;
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700106 const int height = 64;
107 const int image_size = width * height;
108 char noise[3072];
109
Yaowu Xuf883b422016-08-30 14:01:10 -0700110 const int clamp = aom_setup_noise(4.4, sizeof(noise), noise);
Johann2967bf32016-06-22 16:08:10 -0700111
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700112 for (int i = 0; i < 16; i++) {
Johann2967bf32016-06-22 16:08:10 -0700113 blackclamp[i] = clamp;
114 whiteclamp[i] = clamp;
115 bothclamp[i] = 2 * clamp;
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700116 }
117
Yaowu Xuf883b422016-08-30 14:01:10 -0700118 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 Bankoski34d5aff2016-05-03 16:23:06 -0700120
121 memset(s, 99, image_size);
122 memset(d, 99, image_size);
123
Jim Bankoski7a91d212016-05-07 12:47:49 -0700124 srand(0);
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700125 ASM_REGISTER_STATE_CHECK(GetParam()(s, noise, blackclamp, whiteclamp,
126 bothclamp, width, height, width));
Jim Bankoski7a91d212016-05-07 12:47:49 -0700127 srand(0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700128 ASM_REGISTER_STATE_CHECK(aom_plane_add_noise_c(
clang-format3a826f12016-08-11 17:46:05 -0700129 d, noise, blackclamp, whiteclamp, bothclamp, width, height, width));
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700130
131 for (int i = 0; i < image_size; ++i) {
Johann2967bf32016-06-22 16:08:10 -0700132 EXPECT_EQ(static_cast<int>(s[i]), static_cast<int>(d[i])) << "i = " << i;
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700133 }
134
Yaowu Xuf883b422016-08-30 14:01:10 -0700135 aom_free(d);
136 aom_free(s);
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700137}
138
139INSTANTIATE_TEST_CASE_P(C, AddNoiseTest,
Yaowu Xuf883b422016-08-30 14:01:10 -0700140 ::testing::Values(aom_plane_add_noise_c));
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700141
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700142#if HAVE_SSE2
143INSTANTIATE_TEST_CASE_P(SSE2, AddNoiseTest,
Yaowu Xuf883b422016-08-30 14:01:10 -0700144 ::testing::Values(aom_plane_add_noise_sse2));
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700145#endif
146
147#if HAVE_MSA
148INSTANTIATE_TEST_CASE_P(MSA, AddNoiseTest,
Yaowu Xuf883b422016-08-30 14:01:10 -0700149 ::testing::Values(aom_plane_add_noise_msa));
Jim Bankoski34d5aff2016-05-03 16:23:06 -0700150#endif
151} // namespace