2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
15 #include "third_party/googletest/src/include/gtest/gtest.h"
17 #include "./vpx_config.h"
18 #if CONFIG_VP9_ENCODER
19 #include "./vp9_rtcd.h"
22 #include "test/acm_random.h"
23 #include "test/clear_system_state.h"
24 #include "test/register_state_check.h"
25 #include "test/util.h"
27 #include "vpx_mem/vpx_mem.h"
29 extern "C" double vp9_get_blockiness(const unsigned char *img1, int img1_pitch,
30 const unsigned char *img2, int img2_pitch,
31 int width, int height);
33 using libvpx_test::ACMRandom;
36 class BlockinessTestBase : public ::testing::Test {
38 BlockinessTestBase(int width, int height) : width_(width), height_(height) {}
40 static void SetUpTestCase() {
41 source_data_ = reinterpret_cast<uint8_t *>(
42 vpx_memalign(kDataAlignment, kDataBufferSize));
43 reference_data_ = reinterpret_cast<uint8_t *>(
44 vpx_memalign(kDataAlignment, kDataBufferSize));
47 static void TearDownTestCase() {
48 vpx_free(source_data_);
50 vpx_free(reference_data_);
51 reference_data_ = NULL;
54 virtual void TearDown() { libvpx_test::ClearSystemState(); }
57 // Handle frames up to 640x480
58 static const int kDataAlignment = 16;
59 static const int kDataBufferSize = 640 * 480;
61 virtual void SetUp() {
62 source_stride_ = (width_ + 31) & ~31;
63 reference_stride_ = width_ * 2;
64 rnd_.Reset(ACMRandom::DeterministicSeed());
67 void FillConstant(uint8_t *data, int stride, uint8_t fill_constant, int width,
69 for (int h = 0; h < height; ++h) {
70 for (int w = 0; w < width; ++w) {
71 data[h * stride + w] = fill_constant;
76 void FillConstant(uint8_t *data, int stride, uint8_t fill_constant) {
77 FillConstant(data, stride, fill_constant, width_, height_);
80 void FillRandom(uint8_t *data, int stride, int width, int height) {
81 for (int h = 0; h < height; ++h) {
82 for (int w = 0; w < width; ++w) {
83 data[h * stride + w] = rnd_.Rand8();
88 void FillRandom(uint8_t *data, int stride) {
89 FillRandom(data, stride, width_, height_);
92 void FillRandomBlocky(uint8_t *data, int stride) {
93 for (int h = 0; h < height_; h += 4) {
94 for (int w = 0; w < width_; w += 4) {
95 FillRandom(data + h * stride + w, stride, 4, 4);
100 void FillCheckerboard(uint8_t *data, int stride) {
101 for (int h = 0; h < height_; h += 4) {
102 for (int w = 0; w < width_; w += 4) {
103 if (((h / 4) ^ (w / 4)) & 1) {
104 FillConstant(data + h * stride + w, stride, 255, 4, 4);
106 FillConstant(data + h * stride + w, stride, 0, 4, 4);
112 void Blur(uint8_t *data, int stride, int taps) {
114 int half_taps = taps / 2;
115 for (int h = 0; h < height_; ++h) {
116 for (int w = 0; w < taps; ++w) {
117 sum += data[w + h * stride];
119 for (int w = taps; w < width_; ++w) {
120 sum += data[w + h * stride] - data[w - taps + h * stride];
121 data[w - half_taps + h * stride] = (sum + half_taps) / taps;
124 for (int w = 0; w < width_; ++w) {
125 for (int h = 0; h < taps; ++h) {
126 sum += data[h + w * stride];
128 for (int h = taps; h < height_; ++h) {
129 sum += data[w + h * stride] - data[(h - taps) * stride + w];
130 data[(h - half_taps) * stride + w] = (sum + half_taps) / taps;
135 static uint8_t *source_data_;
137 static uint8_t *reference_data_;
138 int reference_stride_;
143 #if CONFIG_VP9_ENCODER
144 typedef std::tr1::tuple<int, int> BlockinessParam;
145 class BlockinessVP9Test
146 : public BlockinessTestBase,
147 public ::testing::WithParamInterface<BlockinessParam> {
149 BlockinessVP9Test() : BlockinessTestBase(GET_PARAM(0), GET_PARAM(1)) {}
152 double GetBlockiness() const {
153 return vp9_get_blockiness(source_data_, source_stride_, reference_data_,
154 reference_stride_, width_, height_);
157 #endif // CONFIG_VP9_ENCODER
159 uint8_t *BlockinessTestBase::source_data_ = NULL;
160 uint8_t *BlockinessTestBase::reference_data_ = NULL;
162 #if CONFIG_VP9_ENCODER
163 TEST_P(BlockinessVP9Test, SourceBlockierThanReference) {
164 // Source is blockier than reference.
165 FillRandomBlocky(source_data_, source_stride_);
166 FillConstant(reference_data_, reference_stride_, 128);
167 const double super_blocky = GetBlockiness();
169 EXPECT_DOUBLE_EQ(0.0, super_blocky)
170 << "Blocky source should produce 0 blockiness.";
173 TEST_P(BlockinessVP9Test, ReferenceBlockierThanSource) {
174 // Source is blockier than reference.
175 FillConstant(source_data_, source_stride_, 128);
176 FillRandomBlocky(reference_data_, reference_stride_);
177 const double super_blocky = GetBlockiness();
179 EXPECT_GT(super_blocky, 0.0)
180 << "Blocky reference should score high for blockiness.";
183 TEST_P(BlockinessVP9Test, BlurringDecreasesBlockiness) {
184 // Source is blockier than reference.
185 FillConstant(source_data_, source_stride_, 128);
186 FillRandomBlocky(reference_data_, reference_stride_);
187 const double super_blocky = GetBlockiness();
189 Blur(reference_data_, reference_stride_, 4);
190 const double less_blocky = GetBlockiness();
192 EXPECT_GT(super_blocky, less_blocky)
193 << "A straight blur should decrease blockiness.";
196 TEST_P(BlockinessVP9Test, WorstCaseBlockiness) {
197 // Source is blockier than reference.
198 FillConstant(source_data_, source_stride_, 128);
199 FillCheckerboard(reference_data_, reference_stride_);
201 const double super_blocky = GetBlockiness();
203 Blur(reference_data_, reference_stride_, 4);
204 const double less_blocky = GetBlockiness();
206 EXPECT_GT(super_blocky, less_blocky)
207 << "A straight blur should decrease blockiness.";
209 #endif // CONFIG_VP9_ENCODER
211 using std::tr1::make_tuple;
213 //------------------------------------------------------------------------------
216 #if CONFIG_VP9_ENCODER
217 const BlockinessParam c_vp9_tests[] = {
218 make_tuple(320, 240), make_tuple(318, 242), make_tuple(318, 238),
220 INSTANTIATE_TEST_CASE_P(C, BlockinessVP9Test, ::testing::ValuesIn(c_vp9_tests));