]> granicus.if.org Git - libvpx/blob - test/blockiness_test.cc
Merge "vp9: Use source_sad only on top temporal enhancement layer."
[libvpx] / test / blockiness_test.cc
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 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);
32
33 using libvpx_test::ACMRandom;
34
35 namespace {
36 class BlockinessTestBase : public ::testing::Test {
37  public:
38   BlockinessTestBase(int width, int height) : width_(width), height_(height) {}
39
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));
45   }
46
47   static void TearDownTestCase() {
48     vpx_free(source_data_);
49     source_data_ = NULL;
50     vpx_free(reference_data_);
51     reference_data_ = NULL;
52   }
53
54   virtual void TearDown() { libvpx_test::ClearSystemState(); }
55
56  protected:
57   // Handle frames up to 640x480
58   static const int kDataAlignment = 16;
59   static const int kDataBufferSize = 640 * 480;
60
61   virtual void SetUp() {
62     source_stride_ = (width_ + 31) & ~31;
63     reference_stride_ = width_ * 2;
64     rnd_.Reset(ACMRandom::DeterministicSeed());
65   }
66
67   void FillConstant(uint8_t *data, int stride, uint8_t fill_constant, int width,
68                     int height) {
69     for (int h = 0; h < height; ++h) {
70       for (int w = 0; w < width; ++w) {
71         data[h * stride + w] = fill_constant;
72       }
73     }
74   }
75
76   void FillConstant(uint8_t *data, int stride, uint8_t fill_constant) {
77     FillConstant(data, stride, fill_constant, width_, height_);
78   }
79
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();
84       }
85     }
86   }
87
88   void FillRandom(uint8_t *data, int stride) {
89     FillRandom(data, stride, width_, height_);
90   }
91
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);
96       }
97     }
98   }
99
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);
105         } else {
106           FillConstant(data + h * stride + w, stride, 0, 4, 4);
107         }
108       }
109     }
110   }
111
112   void Blur(uint8_t *data, int stride, int taps) {
113     int sum = 0;
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];
118       }
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;
122       }
123     }
124     for (int w = 0; w < width_; ++w) {
125       for (int h = 0; h < taps; ++h) {
126         sum += data[h + w * stride];
127       }
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;
131       }
132     }
133   }
134   int width_, height_;
135   static uint8_t *source_data_;
136   int source_stride_;
137   static uint8_t *reference_data_;
138   int reference_stride_;
139
140   ACMRandom rnd_;
141 };
142
143 #if CONFIG_VP9_ENCODER
144 typedef std::tr1::tuple<int, int> BlockinessParam;
145 class BlockinessVP9Test
146     : public BlockinessTestBase,
147       public ::testing::WithParamInterface<BlockinessParam> {
148  public:
149   BlockinessVP9Test() : BlockinessTestBase(GET_PARAM(0), GET_PARAM(1)) {}
150
151  protected:
152   double GetBlockiness() const {
153     return vp9_get_blockiness(source_data_, source_stride_, reference_data_,
154                               reference_stride_, width_, height_);
155   }
156 };
157 #endif  // CONFIG_VP9_ENCODER
158
159 uint8_t *BlockinessTestBase::source_data_ = NULL;
160 uint8_t *BlockinessTestBase::reference_data_ = NULL;
161
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();
168
169   EXPECT_DOUBLE_EQ(0.0, super_blocky)
170       << "Blocky source should produce 0 blockiness.";
171 }
172
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();
178
179   EXPECT_GT(super_blocky, 0.0)
180       << "Blocky reference should score high for blockiness.";
181 }
182
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();
188
189   Blur(reference_data_, reference_stride_, 4);
190   const double less_blocky = GetBlockiness();
191
192   EXPECT_GT(super_blocky, less_blocky)
193       << "A straight blur should decrease blockiness.";
194 }
195
196 TEST_P(BlockinessVP9Test, WorstCaseBlockiness) {
197   // Source is blockier than reference.
198   FillConstant(source_data_, source_stride_, 128);
199   FillCheckerboard(reference_data_, reference_stride_);
200
201   const double super_blocky = GetBlockiness();
202
203   Blur(reference_data_, reference_stride_, 4);
204   const double less_blocky = GetBlockiness();
205
206   EXPECT_GT(super_blocky, less_blocky)
207       << "A straight blur should decrease blockiness.";
208 }
209 #endif  // CONFIG_VP9_ENCODER
210
211 using std::tr1::make_tuple;
212
213 //------------------------------------------------------------------------------
214 // C functions
215
216 #if CONFIG_VP9_ENCODER
217 const BlockinessParam c_vp9_tests[] = {
218   make_tuple(320, 240), make_tuple(318, 242), make_tuple(318, 238),
219 };
220 INSTANTIATE_TEST_CASE_P(C, BlockinessVP9Test, ::testing::ValuesIn(c_vp9_tests));
221 #endif
222
223 }  // namespace