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.
16 #include "third_party/googletest/src/include/gtest/gtest.h"
18 #include "./vpx_config.h"
19 #if CONFIG_VP9_ENCODER
20 #include "./vp9_rtcd.h"
23 #include "test/acm_random.h"
24 #include "test/clear_system_state.h"
25 #include "test/register_state_check.h"
26 #include "test/util.h"
27 #include "vpx_dsp/ssim.h"
28 #include "vpx_mem/vpx_mem.h"
30 extern "C" double vpx_get_ssim_metrics(uint8_t *img1, int img1_pitch,
31 uint8_t *img2, int img2_pitch, int width,
32 int height, Ssimv *sv2, Metrics *m,
33 int do_inconsistency);
35 using libvpx_test::ACMRandom;
38 class ConsistencyTestBase : public ::testing::Test {
40 ConsistencyTestBase(int width, int height) : width_(width), height_(height) {}
42 static void SetUpTestSuite() {
43 source_data_[0] = reinterpret_cast<uint8_t *>(
44 vpx_memalign(kDataAlignment, kDataBufferSize));
45 reference_data_[0] = reinterpret_cast<uint8_t *>(
46 vpx_memalign(kDataAlignment, kDataBufferSize));
47 source_data_[1] = reinterpret_cast<uint8_t *>(
48 vpx_memalign(kDataAlignment, kDataBufferSize));
49 reference_data_[1] = reinterpret_cast<uint8_t *>(
50 vpx_memalign(kDataAlignment, kDataBufferSize));
51 ssim_array_ = new Ssimv[kDataBufferSize / 16];
54 static void ClearSsim() { memset(ssim_array_, 0, kDataBufferSize / 16); }
55 static void TearDownTestSuite() {
56 vpx_free(source_data_[0]);
57 source_data_[0] = nullptr;
58 vpx_free(reference_data_[0]);
59 reference_data_[0] = nullptr;
60 vpx_free(source_data_[1]);
61 source_data_[1] = nullptr;
62 vpx_free(reference_data_[1]);
63 reference_data_[1] = nullptr;
68 virtual void TearDown() { libvpx_test::ClearSystemState(); }
71 // Handle frames up to 640x480
72 static const int kDataAlignment = 16;
73 static const int kDataBufferSize = 640 * 480;
75 virtual void SetUp() {
76 source_stride_ = (width_ + 31) & ~31;
77 reference_stride_ = width_ * 2;
78 rnd_.Reset(ACMRandom::DeterministicSeed());
81 void FillRandom(uint8_t *data, int stride, int width, int height) {
82 for (int h = 0; h < height; ++h) {
83 for (int w = 0; w < width; ++w) {
84 data[h * stride + w] = rnd_.Rand8();
89 void FillRandom(uint8_t *data, int stride) {
90 FillRandom(data, stride, width_, height_);
93 void Copy(uint8_t *reference, uint8_t *source) {
94 memcpy(reference, source, kDataBufferSize);
97 void Blur(uint8_t *data, int stride, int taps) {
99 int half_taps = taps / 2;
100 for (int h = 0; h < height_; ++h) {
101 for (int w = 0; w < taps; ++w) {
102 sum += data[w + h * stride];
104 for (int w = taps; w < width_; ++w) {
105 sum += data[w + h * stride] - data[w - taps + h * stride];
106 data[w - half_taps + h * stride] = (sum + half_taps) / taps;
109 for (int w = 0; w < width_; ++w) {
110 for (int h = 0; h < taps; ++h) {
111 sum += data[h + w * stride];
113 for (int h = taps; h < height_; ++h) {
114 sum += data[w + h * stride] - data[(h - taps) * stride + w];
115 data[(h - half_taps) * stride + w] = (sum + half_taps) / taps;
120 static uint8_t *source_data_[2];
122 static uint8_t *reference_data_[2];
123 int reference_stride_;
124 static Ssimv *ssim_array_;
130 #if CONFIG_VP9_ENCODER
131 typedef std::tuple<int, int> ConsistencyParam;
132 class ConsistencyVP9Test
133 : public ConsistencyTestBase,
134 public ::testing::WithParamInterface<ConsistencyParam> {
136 ConsistencyVP9Test() : ConsistencyTestBase(GET_PARAM(0), GET_PARAM(1)) {}
139 double CheckConsistency(int frame) {
140 EXPECT_LT(frame, 2) << "Frame to check has to be less than 2.";
141 return vpx_get_ssim_metrics(source_data_[frame], source_stride_,
142 reference_data_[frame], reference_stride_,
143 width_, height_, ssim_array_, &metrics_, 1);
146 #endif // CONFIG_VP9_ENCODER
148 uint8_t *ConsistencyTestBase::source_data_[2] = { nullptr, nullptr };
149 uint8_t *ConsistencyTestBase::reference_data_[2] = { nullptr, nullptr };
150 Ssimv *ConsistencyTestBase::ssim_array_ = nullptr;
152 #if CONFIG_VP9_ENCODER
153 TEST_P(ConsistencyVP9Test, ConsistencyIsZero) {
154 FillRandom(source_data_[0], source_stride_);
155 Copy(source_data_[1], source_data_[0]);
156 Copy(reference_data_[0], source_data_[0]);
157 Blur(reference_data_[0], reference_stride_, 3);
158 Copy(reference_data_[1], source_data_[0]);
159 Blur(reference_data_[1], reference_stride_, 3);
161 double inconsistency = CheckConsistency(1);
162 inconsistency = CheckConsistency(0);
163 EXPECT_EQ(inconsistency, 0.0)
164 << "Should have 0 inconsistency if they are exactly the same.";
166 // If sources are not consistent reference frames inconsistency should
167 // be less than if the source is consistent.
168 FillRandom(source_data_[0], source_stride_);
169 FillRandom(source_data_[1], source_stride_);
170 FillRandom(reference_data_[0], reference_stride_);
171 FillRandom(reference_data_[1], reference_stride_);
173 inconsistency = CheckConsistency(1);
175 Copy(source_data_[1], source_data_[0]);
177 double inconsistency2 = CheckConsistency(1);
178 EXPECT_LT(inconsistency, inconsistency2)
179 << "Should have less inconsistency if source itself is inconsistent.";
181 // Less of a blur should be less inconsistent than more blur coming off a
182 // a frame with no blur.
184 FillRandom(source_data_[0], source_stride_);
185 Copy(source_data_[1], source_data_[0]);
186 Copy(reference_data_[0], source_data_[0]);
187 Copy(reference_data_[1], source_data_[0]);
188 Blur(reference_data_[1], reference_stride_, 4);
190 inconsistency = CheckConsistency(1);
192 Copy(reference_data_[1], source_data_[0]);
193 Blur(reference_data_[1], reference_stride_, 8);
195 inconsistency2 = CheckConsistency(1);
197 EXPECT_LT(inconsistency, inconsistency2)
198 << "Stronger Blur should produce more inconsistency.";
200 #endif // CONFIG_VP9_ENCODER
202 using std::make_tuple;
204 //------------------------------------------------------------------------------
207 #if CONFIG_VP9_ENCODER
208 const ConsistencyParam c_vp9_tests[] = { make_tuple(320, 240),
209 make_tuple(318, 242),
210 make_tuple(318, 238) };
211 INSTANTIATE_TEST_SUITE_P(C, ConsistencyVP9Test,
212 ::testing::ValuesIn(c_vp9_tests));