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 #include <tuple>
15 
16 #include "third_party/googletest/src/include/gtest/gtest.h"
17 
18 #include "./vpx_config.h"
19 #if CONFIG_VP9_ENCODER
20 #include "./vp9_rtcd.h"
21 #endif
22 
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"
29 
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);
34 
35 using libvpx_test::ACMRandom;
36 
37 namespace {
38 class ConsistencyTestBase : public ::testing::Test {
39  public:
ConsistencyTestBase(int width,int height)40   ConsistencyTestBase(int width, int height) : width_(width), height_(height) {}
41 
SetUpTestCase()42   static void SetUpTestCase() {
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];
52   }
53 
ClearSsim()54   static void ClearSsim() { memset(ssim_array_, 0, kDataBufferSize / 16); }
TearDownTestCase()55   static void TearDownTestCase() {
56     vpx_free(source_data_[0]);
57     source_data_[0] = NULL;
58     vpx_free(reference_data_[0]);
59     reference_data_[0] = NULL;
60     vpx_free(source_data_[1]);
61     source_data_[1] = NULL;
62     vpx_free(reference_data_[1]);
63     reference_data_[1] = NULL;
64 
65     delete[] ssim_array_;
66   }
67 
TearDown()68   virtual void TearDown() { libvpx_test::ClearSystemState(); }
69 
70  protected:
71   // Handle frames up to 640x480
72   static const int kDataAlignment = 16;
73   static const int kDataBufferSize = 640 * 480;
74 
SetUp()75   virtual void SetUp() {
76     source_stride_ = (width_ + 31) & ~31;
77     reference_stride_ = width_ * 2;
78     rnd_.Reset(ACMRandom::DeterministicSeed());
79   }
80 
FillRandom(uint8_t * data,int stride,int width,int height)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();
85       }
86     }
87   }
88 
FillRandom(uint8_t * data,int stride)89   void FillRandom(uint8_t *data, int stride) {
90     FillRandom(data, stride, width_, height_);
91   }
92 
Copy(uint8_t * reference,uint8_t * source)93   void Copy(uint8_t *reference, uint8_t *source) {
94     memcpy(reference, source, kDataBufferSize);
95   }
96 
Blur(uint8_t * data,int stride,int taps)97   void Blur(uint8_t *data, int stride, int taps) {
98     int sum = 0;
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];
103       }
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;
107       }
108     }
109     for (int w = 0; w < width_; ++w) {
110       for (int h = 0; h < taps; ++h) {
111         sum += data[h + w * stride];
112       }
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;
116       }
117     }
118   }
119   int width_, height_;
120   static uint8_t *source_data_[2];
121   int source_stride_;
122   static uint8_t *reference_data_[2];
123   int reference_stride_;
124   static Ssimv *ssim_array_;
125   Metrics metrics_;
126 
127   ACMRandom rnd_;
128 };
129 
130 #if CONFIG_VP9_ENCODER
131 typedef std::tuple<int, int> ConsistencyParam;
132 class ConsistencyVP9Test
133     : public ConsistencyTestBase,
134       public ::testing::WithParamInterface<ConsistencyParam> {
135  public:
ConsistencyVP9Test()136   ConsistencyVP9Test() : ConsistencyTestBase(GET_PARAM(0), GET_PARAM(1)) {}
137 
138  protected:
CheckConsistency(int frame)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);
144   }
145 };
146 #endif  // CONFIG_VP9_ENCODER
147 
148 uint8_t *ConsistencyTestBase::source_data_[2] = { NULL, NULL };
149 uint8_t *ConsistencyTestBase::reference_data_[2] = { NULL, NULL };
150 Ssimv *ConsistencyTestBase::ssim_array_ = NULL;
151 
152 #if CONFIG_VP9_ENCODER
TEST_P(ConsistencyVP9Test,ConsistencyIsZero)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);
160 
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.";
165 
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_);
172   CheckConsistency(0);
173   inconsistency = CheckConsistency(1);
174 
175   Copy(source_data_[1], source_data_[0]);
176   CheckConsistency(0);
177   double inconsistency2 = CheckConsistency(1);
178   EXPECT_LT(inconsistency, inconsistency2)
179       << "Should have less inconsistency if source itself is inconsistent.";
180 
181   // Less of a blur should be less inconsistent than more blur coming off a
182   // a frame with no blur.
183   ClearSsim();
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);
189   CheckConsistency(0);
190   inconsistency = CheckConsistency(1);
191   ClearSsim();
192   Copy(reference_data_[1], source_data_[0]);
193   Blur(reference_data_[1], reference_stride_, 8);
194   CheckConsistency(0);
195   inconsistency2 = CheckConsistency(1);
196 
197   EXPECT_LT(inconsistency, inconsistency2)
198       << "Stronger Blur should produce more inconsistency.";
199 }
200 #endif  // CONFIG_VP9_ENCODER
201 
202 using std::make_tuple;
203 
204 //------------------------------------------------------------------------------
205 // C functions
206 
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_CASE_P(C, ConsistencyVP9Test,
212                         ::testing::ValuesIn(c_vp9_tests));
213 #endif
214 
215 }  // namespace
216