blockiness_test.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2012 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 <limits.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "third_party/googletest/src/include/gtest/gtest.h"
  14. #include "./vpx_config.h"
  15. #if CONFIG_VP9_ENCODER
  16. #include "./vp9_rtcd.h"
  17. #endif
  18. #include "test/acm_random.h"
  19. #include "test/clear_system_state.h"
  20. #include "test/register_state_check.h"
  21. #include "test/util.h"
  22. #include "vpx_mem/vpx_mem.h"
  23. extern "C" double vp9_get_blockiness(const unsigned char *img1, int img1_pitch,
  24. const unsigned char *img2, int img2_pitch,
  25. int width, int height);
  26. using libvpx_test::ACMRandom;
  27. namespace {
  28. class BlockinessTestBase : public ::testing::Test {
  29. public:
  30. BlockinessTestBase(int width, int height) : width_(width), height_(height) {}
  31. static void SetUpTestCase() {
  32. source_data_ = reinterpret_cast<uint8_t *>(
  33. vpx_memalign(kDataAlignment, kDataBufferSize));
  34. reference_data_ = reinterpret_cast<uint8_t *>(
  35. vpx_memalign(kDataAlignment, kDataBufferSize));
  36. }
  37. static void TearDownTestCase() {
  38. vpx_free(source_data_);
  39. source_data_ = NULL;
  40. vpx_free(reference_data_);
  41. reference_data_ = NULL;
  42. }
  43. virtual void TearDown() { libvpx_test::ClearSystemState(); }
  44. protected:
  45. // Handle frames up to 640x480
  46. static const int kDataAlignment = 16;
  47. static const int kDataBufferSize = 640 * 480;
  48. virtual void SetUp() {
  49. source_stride_ = (width_ + 31) & ~31;
  50. reference_stride_ = width_ * 2;
  51. rnd_.Reset(ACMRandom::DeterministicSeed());
  52. }
  53. void FillConstant(uint8_t *data, int stride, uint8_t fill_constant, int width,
  54. int height) {
  55. for (int h = 0; h < height; ++h) {
  56. for (int w = 0; w < width; ++w) {
  57. data[h * stride + w] = fill_constant;
  58. }
  59. }
  60. }
  61. void FillConstant(uint8_t *data, int stride, uint8_t fill_constant) {
  62. FillConstant(data, stride, fill_constant, width_, height_);
  63. }
  64. void FillRandom(uint8_t *data, int stride, int width, int height) {
  65. for (int h = 0; h < height; ++h) {
  66. for (int w = 0; w < width; ++w) {
  67. data[h * stride + w] = rnd_.Rand8();
  68. }
  69. }
  70. }
  71. void FillRandom(uint8_t *data, int stride) {
  72. FillRandom(data, stride, width_, height_);
  73. }
  74. void FillRandomBlocky(uint8_t *data, int stride) {
  75. for (int h = 0; h < height_; h += 4) {
  76. for (int w = 0; w < width_; w += 4) {
  77. FillRandom(data + h * stride + w, stride, 4, 4);
  78. }
  79. }
  80. }
  81. void FillCheckerboard(uint8_t *data, int stride) {
  82. for (int h = 0; h < height_; h += 4) {
  83. for (int w = 0; w < width_; w += 4) {
  84. if (((h / 4) ^ (w / 4)) & 1) {
  85. FillConstant(data + h * stride + w, stride, 255, 4, 4);
  86. } else {
  87. FillConstant(data + h * stride + w, stride, 0, 4, 4);
  88. }
  89. }
  90. }
  91. }
  92. void Blur(uint8_t *data, int stride, int taps) {
  93. int sum = 0;
  94. int half_taps = taps / 2;
  95. for (int h = 0; h < height_; ++h) {
  96. for (int w = 0; w < taps; ++w) {
  97. sum += data[w + h * stride];
  98. }
  99. for (int w = taps; w < width_; ++w) {
  100. sum += data[w + h * stride] - data[w - taps + h * stride];
  101. data[w - half_taps + h * stride] = (sum + half_taps) / taps;
  102. }
  103. }
  104. for (int w = 0; w < width_; ++w) {
  105. for (int h = 0; h < taps; ++h) {
  106. sum += data[h + w * stride];
  107. }
  108. for (int h = taps; h < height_; ++h) {
  109. sum += data[w + h * stride] - data[(h - taps) * stride + w];
  110. data[(h - half_taps) * stride + w] = (sum + half_taps) / taps;
  111. }
  112. }
  113. }
  114. int width_, height_;
  115. static uint8_t *source_data_;
  116. int source_stride_;
  117. static uint8_t *reference_data_;
  118. int reference_stride_;
  119. ACMRandom rnd_;
  120. };
  121. #if CONFIG_VP9_ENCODER
  122. typedef std::tr1::tuple<int, int> BlockinessParam;
  123. class BlockinessVP9Test
  124. : public BlockinessTestBase,
  125. public ::testing::WithParamInterface<BlockinessParam> {
  126. public:
  127. BlockinessVP9Test() : BlockinessTestBase(GET_PARAM(0), GET_PARAM(1)) {}
  128. protected:
  129. double GetBlockiness() const {
  130. return vp9_get_blockiness(source_data_, source_stride_, reference_data_,
  131. reference_stride_, width_, height_);
  132. }
  133. };
  134. #endif // CONFIG_VP9_ENCODER
  135. uint8_t *BlockinessTestBase::source_data_ = NULL;
  136. uint8_t *BlockinessTestBase::reference_data_ = NULL;
  137. #if CONFIG_VP9_ENCODER
  138. TEST_P(BlockinessVP9Test, SourceBlockierThanReference) {
  139. // Source is blockier than reference.
  140. FillRandomBlocky(source_data_, source_stride_);
  141. FillConstant(reference_data_, reference_stride_, 128);
  142. const double super_blocky = GetBlockiness();
  143. EXPECT_DOUBLE_EQ(0.0, super_blocky)
  144. << "Blocky source should produce 0 blockiness.";
  145. }
  146. TEST_P(BlockinessVP9Test, ReferenceBlockierThanSource) {
  147. // Source is blockier than reference.
  148. FillConstant(source_data_, source_stride_, 128);
  149. FillRandomBlocky(reference_data_, reference_stride_);
  150. const double super_blocky = GetBlockiness();
  151. EXPECT_GT(super_blocky, 0.0)
  152. << "Blocky reference should score high for blockiness.";
  153. }
  154. TEST_P(BlockinessVP9Test, BlurringDecreasesBlockiness) {
  155. // Source is blockier than reference.
  156. FillConstant(source_data_, source_stride_, 128);
  157. FillRandomBlocky(reference_data_, reference_stride_);
  158. const double super_blocky = GetBlockiness();
  159. Blur(reference_data_, reference_stride_, 4);
  160. const double less_blocky = GetBlockiness();
  161. EXPECT_GT(super_blocky, less_blocky)
  162. << "A straight blur should decrease blockiness.";
  163. }
  164. TEST_P(BlockinessVP9Test, WorstCaseBlockiness) {
  165. // Source is blockier than reference.
  166. FillConstant(source_data_, source_stride_, 128);
  167. FillCheckerboard(reference_data_, reference_stride_);
  168. const double super_blocky = GetBlockiness();
  169. Blur(reference_data_, reference_stride_, 4);
  170. const double less_blocky = GetBlockiness();
  171. EXPECT_GT(super_blocky, less_blocky)
  172. << "A straight blur should decrease blockiness.";
  173. }
  174. #endif // CONFIG_VP9_ENCODER
  175. using std::tr1::make_tuple;
  176. //------------------------------------------------------------------------------
  177. // C functions
  178. #if CONFIG_VP9_ENCODER
  179. const BlockinessParam c_vp9_tests[] = {
  180. make_tuple(320, 240), make_tuple(318, 242), make_tuple(318, 238),
  181. };
  182. INSTANTIATE_TEST_CASE_P(C, BlockinessVP9Test, ::testing::ValuesIn(c_vp9_tests));
  183. #endif
  184. } // namespace