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"
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
);
35 using libvpx_test::ACMRandom
;
38 class BlockinessTestBase
: public ::testing::Test
{
40 BlockinessTestBase(int width
, int height
) : width_(width
), height_(height
) {}
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
));
49 static void TearDownTestCase() {
50 vpx_free(source_data_
);
52 vpx_free(reference_data_
);
53 reference_data_
= NULL
;
56 virtual void TearDown() {
57 libvpx_test::ClearSystemState();
61 // Handle frames up to 640x480
62 static const int kDataAlignment
= 16;
63 static const int kDataBufferSize
= 640*480;
65 virtual void SetUp() {
66 source_stride_
= (width_
+ 31) & ~31;
67 reference_stride_
= width_
* 2;
68 rnd_
.Reset(ACMRandom::DeterministicSeed());
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
;
80 void FillConstant(uint8_t *data
, int stride
, uint8_t fill_constant
) {
81 FillConstant(data
, stride
, fill_constant
, width_
, 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();
92 void FillRandom(uint8_t *data
, int stride
) {
93 FillRandom(data
, stride
, width_
, height_
);
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);
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);
110 FillConstant(data
+ h
* stride
+ w
, stride
, 0, 4, 4);
115 void Blur(uint8_t *data
, int stride
, int taps
) {
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
];
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
;
127 for (int w
= 0; w
< width_
; ++w
) {
128 for (int h
= 0; h
< taps
; ++h
) {
129 sum
+= data
[h
+ w
* stride
];
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
;
138 static uint8_t* source_data_
;
140 static uint8_t* reference_data_
;
141 int reference_stride_
;
146 #if CONFIG_VP9_ENCODER
147 typedef std::tr1::tuple
<int, int> BlockinessParam
;
148 class BlockinessVP9Test
149 : public BlockinessTestBase
,
150 public ::testing::WithParamInterface
<BlockinessParam
> {
152 BlockinessVP9Test() : BlockinessTestBase(GET_PARAM(0), GET_PARAM(1)) {}
155 int CheckBlockiness() {
156 return vp9_get_blockiness(source_data_
, source_stride_
,
157 reference_data_
, reference_stride_
,
161 #endif // CONFIG_VP9_ENCODER
163 uint8_t* BlockinessTestBase::source_data_
= NULL
;
164 uint8_t* BlockinessTestBase::reference_data_
= NULL
;
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();
173 EXPECT_EQ(0, super_blocky
) << "Blocky source should produce 0 blockiness.";
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();
182 EXPECT_GT(super_blocky
, 0.0)
183 << "Blocky reference should score high for blockiness.";
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();
192 Blur(reference_data_
, reference_stride_
, 4);
193 int less_blocky
= CheckBlockiness();
195 EXPECT_GT(super_blocky
, less_blocky
)
196 << "A straight blur should decrease blockiness.";
199 TEST_P(BlockinessVP9Test
, WorstCaseBlockiness
) {
200 // Source is blockier than reference.
201 FillConstant(source_data_
, source_stride_
, 128);
202 FillCheckerboard(reference_data_
, reference_stride_
);
204 int super_blocky
= CheckBlockiness();
206 Blur(reference_data_
, reference_stride_
, 4);
207 int less_blocky
= CheckBlockiness();
209 EXPECT_GT(super_blocky
, less_blocky
)
210 << "A straight blur should decrease blockiness.";
212 #endif // CONFIG_VP9_ENCODER
215 using std::tr1::make_tuple
;
217 //------------------------------------------------------------------------------
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),
226 INSTANTIATE_TEST_CASE_P(C
, BlockinessVP9Test
, ::testing::ValuesIn(c_vp9_tests
));