Merge "Simplify rd_pick_intra_sby_mode()"
[platform/upstream/libvpx.git] / test / vpx_scale_test.cc
1 /*
2  *  Copyright (c) 2014 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 "third_party/googletest/src/include/gtest/gtest.h"
12
13 #include "test/clear_system_state.h"
14 #include "test/register_state_check.h"
15
16 #include "./vpx_config.h"
17 #include "./vpx_scale_rtcd.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_scale/yv12config.h"
20
21 namespace {
22
23 typedef void (*ExtendFrameBorderFunc)(YV12_BUFFER_CONFIG *ybf);
24 typedef void (*CopyFrameFunc)(const YV12_BUFFER_CONFIG *src_ybf,
25                               YV12_BUFFER_CONFIG *dst_ybf);
26
27 class VpxScaleBase {
28  public:
29   virtual ~VpxScaleBase() {
30     libvpx_test::ClearSystemState();
31   }
32
33   void ResetImage(int width, int height) {
34     width_ = width;
35     height_ = height;
36     vpx_memset(&img_, 0, sizeof(img_));
37     ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&img_, width_, height_,
38                                              VP8BORDERINPIXELS));
39     vpx_memset(img_.buffer_alloc, kBufFiller, img_.frame_size);
40     FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
41               img_.y_stride);
42     FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
43               img_.uv_stride);
44     FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
45               img_.uv_stride);
46
47     vpx_memset(&ref_img_, 0, sizeof(ref_img_));
48     ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&ref_img_, width_, height_,
49                                              VP8BORDERINPIXELS));
50     vpx_memset(ref_img_.buffer_alloc, kBufFiller, ref_img_.frame_size);
51
52     vpx_memset(&cpy_img_, 0, sizeof(cpy_img_));
53     ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&cpy_img_, width_, height_,
54                                              VP8BORDERINPIXELS));
55     vpx_memset(cpy_img_.buffer_alloc, kBufFiller, cpy_img_.frame_size);
56     ReferenceCopyFrame();
57   }
58
59   void DeallocImage() {
60     vp8_yv12_de_alloc_frame_buffer(&img_);
61     vp8_yv12_de_alloc_frame_buffer(&ref_img_);
62     vp8_yv12_de_alloc_frame_buffer(&cpy_img_);
63   }
64
65  protected:
66   static const int kBufFiller = 123;
67   static const int kBufMax = kBufFiller - 1;
68
69   static void FillPlane(uint8_t *buf, int width, int height, int stride) {
70     for (int y = 0; y < height; ++y) {
71       for (int x = 0; x < width; ++x) {
72         buf[x + (y * stride)] = (x + (width * y)) % kBufMax;
73       }
74     }
75   }
76
77   static void ExtendPlane(uint8_t *buf, int crop_width, int crop_height,
78                           int width, int height, int stride, int padding) {
79     // Copy the outermost visible pixel to a distance of at least 'padding.'
80     // The buffers are allocated such that there may be excess space outside the
81     // padding. As long as the minimum amount of padding is achieved it is not
82     // necessary to fill this space as well.
83     uint8_t *left = buf - padding;
84     uint8_t *right = buf + crop_width;
85     const int right_extend = padding + (width - crop_width);
86     const int bottom_extend = padding + (height - crop_height);
87
88     // Fill the border pixels from the nearest image pixel.
89     for (int y = 0; y < crop_height; ++y) {
90       vpx_memset(left, left[padding], padding);
91       vpx_memset(right, right[-1], right_extend);
92       left += stride;
93       right += stride;
94     }
95
96     left = buf - padding;
97     uint8_t *top = left - (stride * padding);
98     // The buffer does not always extend as far as the stride.
99     // Equivalent to padding + width + padding.
100     const int extend_width = padding + crop_width + right_extend;
101
102     // The first row was already extended to the left and right. Copy it up.
103     for (int y = 0; y < padding; ++y) {
104       vpx_memcpy(top, left, extend_width);
105       top += stride;
106     }
107
108     uint8_t *bottom = left + (crop_height * stride);
109     for (int y = 0; y <  bottom_extend; ++y) {
110       vpx_memcpy(bottom, left + (crop_height - 1) * stride, extend_width);
111       bottom += stride;
112     }
113   }
114
115   void ReferenceExtendBorder() {
116     ExtendPlane(ref_img_.y_buffer,
117                 ref_img_.y_crop_width, ref_img_.y_crop_height,
118                 ref_img_.y_width, ref_img_.y_height,
119                 ref_img_.y_stride,
120                 ref_img_.border);
121     ExtendPlane(ref_img_.u_buffer,
122                 ref_img_.uv_crop_width, ref_img_.uv_crop_height,
123                 ref_img_.uv_width, ref_img_.uv_height,
124                 ref_img_.uv_stride,
125                 ref_img_.border / 2);
126     ExtendPlane(ref_img_.v_buffer,
127                 ref_img_.uv_crop_width, ref_img_.uv_crop_height,
128                 ref_img_.uv_width, ref_img_.uv_height,
129                 ref_img_.uv_stride,
130                 ref_img_.border / 2);
131   }
132
133   void ReferenceCopyFrame() {
134     // Copy img_ to ref_img_ and extend frame borders. This will be used for
135     // verifying extend_fn_ as well as copy_frame_fn_.
136     EXPECT_EQ(ref_img_.frame_size, img_.frame_size);
137     for (int y = 0; y < img_.y_crop_height; ++y) {
138       for (int x = 0; x < img_.y_crop_width; ++x) {
139         ref_img_.y_buffer[x + y * ref_img_.y_stride] =
140             img_.y_buffer[x + y * img_.y_stride];
141       }
142     }
143
144     for (int y = 0; y < img_.uv_crop_height; ++y) {
145       for (int x = 0; x < img_.uv_crop_width; ++x) {
146         ref_img_.u_buffer[x + y * ref_img_.uv_stride] =
147             img_.u_buffer[x + y * img_.uv_stride];
148         ref_img_.v_buffer[x + y * ref_img_.uv_stride] =
149             img_.v_buffer[x + y * img_.uv_stride];
150       }
151     }
152
153     ReferenceExtendBorder();
154   }
155
156   void CompareImages(const YV12_BUFFER_CONFIG actual) {
157     EXPECT_EQ(ref_img_.frame_size, actual.frame_size);
158     EXPECT_EQ(0, memcmp(ref_img_.buffer_alloc, actual.buffer_alloc,
159                         ref_img_.frame_size));
160   }
161
162   YV12_BUFFER_CONFIG img_;
163   YV12_BUFFER_CONFIG ref_img_;
164   YV12_BUFFER_CONFIG cpy_img_;
165   int width_;
166   int height_;
167 };
168
169 class ExtendBorderTest
170     : public VpxScaleBase,
171       public ::testing::TestWithParam<ExtendFrameBorderFunc> {
172  public:
173   virtual ~ExtendBorderTest() {}
174
175  protected:
176   virtual void SetUp() {
177     extend_fn_ = GetParam();
178   }
179
180   void ExtendBorder() {
181     ASM_REGISTER_STATE_CHECK(extend_fn_(&img_));
182   }
183
184   void RunTest() {
185 #if ARCH_ARM
186     // Some arm devices OOM when trying to allocate the largest buffers.
187     static const int kNumSizesToTest = 6;
188 #else
189     static const int kNumSizesToTest = 7;
190 #endif
191     static const int kSizesToTest[] = {1, 15, 33, 145, 512, 1025, 16383};
192     for (int h = 0; h < kNumSizesToTest; ++h) {
193       for (int w = 0; w < kNumSizesToTest; ++w) {
194         ResetImage(kSizesToTest[w], kSizesToTest[h]);
195         ExtendBorder();
196         ReferenceExtendBorder();
197         CompareImages(img_);
198         DeallocImage();
199       }
200     }
201   }
202
203   ExtendFrameBorderFunc extend_fn_;
204 };
205
206 TEST_P(ExtendBorderTest, ExtendBorder) {
207   ASSERT_NO_FATAL_FAILURE(RunTest());
208 }
209
210 INSTANTIATE_TEST_CASE_P(C, ExtendBorderTest,
211                         ::testing::Values(vp8_yv12_extend_frame_borders_c));
212
213 class CopyFrameTest
214     : public VpxScaleBase,
215       public ::testing::TestWithParam<CopyFrameFunc> {
216  public:
217   virtual ~CopyFrameTest() {}
218
219  protected:
220   virtual void SetUp() {
221     copy_frame_fn_ = GetParam();
222   }
223
224   void CopyFrame() {
225     ASM_REGISTER_STATE_CHECK(copy_frame_fn_(&img_, &cpy_img_));
226   }
227
228   void RunTest() {
229 #if ARCH_ARM
230     // Some arm devices OOM when trying to allocate the largest buffers.
231     static const int kNumSizesToTest = 6;
232 #else
233     static const int kNumSizesToTest = 7;
234 #endif
235     static const int kSizesToTest[] = {1, 15, 33, 145, 512, 1025, 16383};
236     for (int h = 0; h < kNumSizesToTest; ++h) {
237       for (int w = 0; w < kNumSizesToTest; ++w) {
238         ResetImage(kSizesToTest[w], kSizesToTest[h]);
239         ReferenceCopyFrame();
240         CopyFrame();
241         CompareImages(cpy_img_);
242         DeallocImage();
243       }
244     }
245   }
246
247   CopyFrameFunc copy_frame_fn_;
248 };
249
250 TEST_P(CopyFrameTest, CopyFrame) {
251   ASSERT_NO_FATAL_FAILURE(RunTest());
252 }
253
254 INSTANTIATE_TEST_CASE_P(C, CopyFrameTest,
255                         ::testing::Values(vp8_yv12_copy_frame_c));
256 }  // namespace