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