add_noise_test.cc 4.4 KB

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