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 #include <limits.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "third_party/googletest/src/include/gtest/gtest.h"
16
17 #include "./vpx_config.h"
18 #if CONFIG_VP9_ENCODER
19 #include "./vp9_rtcd.h"
20 #endif
21
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"
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:
BlockinessTestBase(int width,int height)40 BlockinessTestBase(int width, int height) : width_(width), height_(height) {}
41
SetUpTestCase()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
TearDownTestCase()49 static void TearDownTestCase() {
50 vpx_free(source_data_);
51 source_data_ = NULL;
52 vpx_free(reference_data_);
53 reference_data_ = NULL;
54 }
55
TearDown()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
SetUp()65 virtual void SetUp() {
66 source_stride_ = (width_ + 31) & ~31;
67 reference_stride_ = width_ * 2;
68 rnd_.Reset(ACMRandom::DeterministicSeed());
69 }
70
FillConstant(uint8_t * data,int stride,uint8_t fill_constant,int width,int height)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
FillConstant(uint8_t * data,int stride,uint8_t fill_constant)80 void FillConstant(uint8_t *data, int stride, uint8_t fill_constant) {
81 FillConstant(data, stride, fill_constant, width_, height_);
82 }
83
FillRandom(uint8_t * data,int stride,int width,int height)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
FillRandom(uint8_t * data,int stride)92 void FillRandom(uint8_t *data, int stride) {
93 FillRandom(data, stride, width_, height_);
94 }
95
FillRandomBlocky(uint8_t * data,int stride)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
FillCheckerboard(uint8_t * data,int stride)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
Blur(uint8_t * data,int stride,int taps)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:
BlockinessVP9Test()152 BlockinessVP9Test() : BlockinessTestBase(GET_PARAM(0), GET_PARAM(1)) {}
153
154 protected:
CheckBlockiness()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
TEST_P(BlockinessVP9Test,SourceBlockierThanReference)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
TEST_P(BlockinessVP9Test,ReferenceBlockierThanSource)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
TEST_P(BlockinessVP9Test,BlurringDecreasesBlockiness)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
TEST_P(BlockinessVP9Test,WorstCaseBlockiness)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
230