add_noise_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include "test/clear_system_state.h"
  13. #include "test/register_state_check.h"
  14. #include "test/util.h"
  15. #include "third_party/googletest/src/include/gtest/gtest.h"
  16. #include "./vpx_dsp_rtcd.h"
  17. #include "vpx/vpx_integer.h"
  18. #include "vpx_dsp/postproc.h"
  19. #include "vpx_mem/vpx_mem.h"
  20. namespace {
  21. static const int kNoiseSize = 3072;
  22. // TODO(jimbankoski): make width and height integers not unsigned.
  23. typedef void (*AddNoiseFunc)(uint8_t *start, const int8_t *noise,
  24. int blackclamp, int whiteclamp, int width,
  25. int height, int pitch);
  26. typedef std::tuple<double, AddNoiseFunc> AddNoiseTestFPParam;
  27. class AddNoiseTest : public ::testing::Test,
  28. public ::testing::WithParamInterface<AddNoiseTestFPParam> {
  29. public:
  30. virtual void TearDown() { libvpx_test::ClearSystemState(); }
  31. virtual ~AddNoiseTest() {}
  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. TEST_P(AddNoiseTest, CheckNoiseAdded) {
  41. const int width = 64;
  42. const int height = 64;
  43. const int image_size = width * height;
  44. int8_t noise[kNoiseSize];
  45. const int clamp = vpx_setup_noise(GET_PARAM(0), noise, kNoiseSize);
  46. uint8_t *const s =
  47. reinterpret_cast<uint8_t *>(vpx_calloc(image_size, sizeof(*s)));
  48. ASSERT_TRUE(s != NULL);
  49. memset(s, 99, image_size * sizeof(*s));
  50. ASM_REGISTER_STATE_CHECK(
  51. GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
  52. // Check to make sure we don't end up having either the same or no added
  53. // noise either vertically or horizontally.
  54. for (int i = 0; i < image_size - 6 * width - 6; ++i) {
  55. const double hd = stddev6(s[i] - 99, s[i + 1] - 99, s[i + 2] - 99,
  56. s[i + 3] - 99, s[i + 4] - 99, s[i + 5] - 99);
  57. const double vd = stddev6(s[i] - 99, s[i + width] - 99,
  58. s[i + 2 * width] - 99, s[i + 3 * width] - 99,
  59. s[i + 4 * width] - 99, s[i + 5 * width] - 99);
  60. EXPECT_NE(hd, 0);
  61. EXPECT_NE(vd, 0);
  62. }
  63. // Initialize pixels in the image to 255 and check for roll over.
  64. memset(s, 255, image_size);
  65. ASM_REGISTER_STATE_CHECK(
  66. GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
  67. // Check to make sure don't roll over.
  68. for (int i = 0; i < image_size; ++i) {
  69. EXPECT_GT(static_cast<int>(s[i]), clamp) << "i = " << i;
  70. }
  71. // Initialize pixels in the image to 0 and check for roll under.
  72. memset(s, 0, image_size);
  73. ASM_REGISTER_STATE_CHECK(
  74. GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
  75. // Check to make sure don't roll under.
  76. for (int i = 0; i < image_size; ++i) {
  77. EXPECT_LT(static_cast<int>(s[i]), 255 - clamp) << "i = " << i;
  78. }
  79. vpx_free(s);
  80. }
  81. TEST_P(AddNoiseTest, CheckCvsAssembly) {
  82. const int width = 64;
  83. const int height = 64;
  84. const int image_size = width * height;
  85. int8_t noise[kNoiseSize];
  86. const int clamp = vpx_setup_noise(4.4, noise, kNoiseSize);
  87. uint8_t *const s = reinterpret_cast<uint8_t *>(vpx_calloc(image_size, 1));
  88. uint8_t *const d = reinterpret_cast<uint8_t *>(vpx_calloc(image_size, 1));
  89. ASSERT_TRUE(s != NULL);
  90. ASSERT_TRUE(d != NULL);
  91. memset(s, 99, image_size);
  92. memset(d, 99, image_size);
  93. srand(0);
  94. ASM_REGISTER_STATE_CHECK(
  95. GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
  96. srand(0);
  97. ASM_REGISTER_STATE_CHECK(
  98. vpx_plane_add_noise_c(d, noise, clamp, clamp, width, height, width));
  99. for (int i = 0; i < image_size; ++i) {
  100. EXPECT_EQ(static_cast<int>(s[i]), static_cast<int>(d[i])) << "i = " << i;
  101. }
  102. vpx_free(d);
  103. vpx_free(s);
  104. }
  105. using std::make_tuple;
  106. INSTANTIATE_TEST_CASE_P(
  107. C, AddNoiseTest,
  108. ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_c),
  109. make_tuple(4.4, vpx_plane_add_noise_c)));
  110. #if HAVE_SSE2
  111. INSTANTIATE_TEST_CASE_P(
  112. SSE2, AddNoiseTest,
  113. ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_sse2),
  114. make_tuple(4.4, vpx_plane_add_noise_sse2)));
  115. #endif
  116. #if HAVE_MSA
  117. INSTANTIATE_TEST_CASE_P(
  118. MSA, AddNoiseTest,
  119. ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_msa),
  120. make_tuple(4.4, vpx_plane_add_noise_msa)));
  121. #endif
  122. } // namespace