blob: ef716fc80f7bd301ff43239ca46ba74392491bf2 [file] [log] [blame]
Johanna04ace22014-07-16 03:47:27 -07001/*
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
Johanna04ace22014-07-16 03:47:27 -070013#include "./vpx_config.h"
14#include "./vpx_scale_rtcd.h"
Jingning Han097d59c2015-07-29 14:51:36 -070015#include "test/clear_system_state.h"
16#include "test/register_state_check.h"
Johanna04ace22014-07-16 03:47:27 -070017#include "vpx_mem/vpx_mem.h"
18#include "vpx_scale/yv12config.h"
19
20namespace {
21
22typedef void (*ExtendFrameBorderFunc)(YV12_BUFFER_CONFIG *ybf);
Johann3f701df2014-07-18 13:53:53 -070023typedef void (*CopyFrameFunc)(const YV12_BUFFER_CONFIG *src_ybf,
24 YV12_BUFFER_CONFIG *dst_ybf);
Johanna04ace22014-07-16 03:47:27 -070025
Johann3f701df2014-07-18 13:53:53 -070026class VpxScaleBase {
Johanna04ace22014-07-16 03:47:27 -070027 public:
Johann3f701df2014-07-18 13:53:53 -070028 virtual ~VpxScaleBase() {
Johanna04ace22014-07-16 03:47:27 -070029 libvpx_test::ClearSystemState();
30 }
31
32 void ResetImage(int width, int height) {
33 width_ = width;
34 height_ = height;
James Zernf58011a2015-04-23 20:47:40 -070035 memset(&img_, 0, sizeof(img_));
Johanna04ace22014-07-16 03:47:27 -070036 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&img_, width_, height_,
37 VP8BORDERINPIXELS));
James Zernf58011a2015-04-23 20:47:40 -070038 memset(img_.buffer_alloc, kBufFiller, img_.frame_size);
Johanna04ace22014-07-16 03:47:27 -070039 FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
40 img_.y_stride);
41 FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
42 img_.uv_stride);
43 FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
44 img_.uv_stride);
45
James Zernf58011a2015-04-23 20:47:40 -070046 memset(&ref_img_, 0, sizeof(ref_img_));
Johanna04ace22014-07-16 03:47:27 -070047 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&ref_img_, width_, height_,
48 VP8BORDERINPIXELS));
James Zernf58011a2015-04-23 20:47:40 -070049 memset(ref_img_.buffer_alloc, kBufFiller, ref_img_.frame_size);
Johann3f701df2014-07-18 13:53:53 -070050
James Zernf58011a2015-04-23 20:47:40 -070051 memset(&cpy_img_, 0, sizeof(cpy_img_));
Johann3f701df2014-07-18 13:53:53 -070052 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&cpy_img_, width_, height_,
53 VP8BORDERINPIXELS));
James Zernf58011a2015-04-23 20:47:40 -070054 memset(cpy_img_.buffer_alloc, kBufFiller, cpy_img_.frame_size);
Johann3f701df2014-07-18 13:53:53 -070055 ReferenceCopyFrame();
Johanna04ace22014-07-16 03:47:27 -070056 }
57
58 void DeallocImage() {
59 vp8_yv12_de_alloc_frame_buffer(&img_);
60 vp8_yv12_de_alloc_frame_buffer(&ref_img_);
Johann3f701df2014-07-18 13:53:53 -070061 vp8_yv12_de_alloc_frame_buffer(&cpy_img_);
Johanna04ace22014-07-16 03:47:27 -070062 }
63
Johann3f701df2014-07-18 13:53:53 -070064 protected:
Johanna04ace22014-07-16 03:47:27 -070065 static const int kBufFiller = 123;
66 static const int kBufMax = kBufFiller - 1;
67
Johanna04ace22014-07-16 03:47:27 -070068 static void FillPlane(uint8_t *buf, int width, int height, int stride) {
69 for (int y = 0; y < height; ++y) {
70 for (int x = 0; x < width; ++x) {
71 buf[x + (y * stride)] = (x + (width * y)) % kBufMax;
72 }
73 }
74 }
75
Johanna04ace22014-07-16 03:47:27 -070076 static void ExtendPlane(uint8_t *buf, int crop_width, int crop_height,
77 int width, int height, int stride, int padding) {
78 // Copy the outermost visible pixel to a distance of at least 'padding.'
79 // The buffers are allocated such that there may be excess space outside the
80 // padding. As long as the minimum amount of padding is achieved it is not
81 // necessary to fill this space as well.
82 uint8_t *left = buf - padding;
83 uint8_t *right = buf + crop_width;
84 const int right_extend = padding + (width - crop_width);
85 const int bottom_extend = padding + (height - crop_height);
86
87 // Fill the border pixels from the nearest image pixel.
88 for (int y = 0; y < crop_height; ++y) {
James Zernf58011a2015-04-23 20:47:40 -070089 memset(left, left[padding], padding);
90 memset(right, right[-1], right_extend);
Johanna04ace22014-07-16 03:47:27 -070091 left += stride;
92 right += stride;
93 }
94
95 left = buf - padding;
96 uint8_t *top = left - (stride * padding);
97 // The buffer does not always extend as far as the stride.
98 // Equivalent to padding + width + padding.
99 const int extend_width = padding + crop_width + right_extend;
100
101 // The first row was already extended to the left and right. Copy it up.
102 for (int y = 0; y < padding; ++y) {
James Zernf274c212015-04-23 20:42:19 -0700103 memcpy(top, left, extend_width);
Johanna04ace22014-07-16 03:47:27 -0700104 top += stride;
105 }
106
107 uint8_t *bottom = left + (crop_height * stride);
108 for (int y = 0; y < bottom_extend; ++y) {
James Zernf274c212015-04-23 20:42:19 -0700109 memcpy(bottom, left + (crop_height - 1) * stride, extend_width);
Johanna04ace22014-07-16 03:47:27 -0700110 bottom += stride;
111 }
112 }
113
Johann3f701df2014-07-18 13:53:53 -0700114 void ReferenceExtendBorder() {
115 ExtendPlane(ref_img_.y_buffer,
116 ref_img_.y_crop_width, ref_img_.y_crop_height,
117 ref_img_.y_width, ref_img_.y_height,
118 ref_img_.y_stride,
119 ref_img_.border);
120 ExtendPlane(ref_img_.u_buffer,
121 ref_img_.uv_crop_width, ref_img_.uv_crop_height,
122 ref_img_.uv_width, ref_img_.uv_height,
123 ref_img_.uv_stride,
124 ref_img_.border / 2);
125 ExtendPlane(ref_img_.v_buffer,
126 ref_img_.uv_crop_width, ref_img_.uv_crop_height,
127 ref_img_.uv_width, ref_img_.uv_height,
128 ref_img_.uv_stride,
129 ref_img_.border / 2);
130 }
131
132 void ReferenceCopyFrame() {
133 // Copy img_ to ref_img_ and extend frame borders. This will be used for
134 // verifying extend_fn_ as well as copy_frame_fn_.
135 EXPECT_EQ(ref_img_.frame_size, img_.frame_size);
136 for (int y = 0; y < img_.y_crop_height; ++y) {
137 for (int x = 0; x < img_.y_crop_width; ++x) {
138 ref_img_.y_buffer[x + y * ref_img_.y_stride] =
139 img_.y_buffer[x + y * img_.y_stride];
140 }
141 }
142
143 for (int y = 0; y < img_.uv_crop_height; ++y) {
144 for (int x = 0; x < img_.uv_crop_width; ++x) {
145 ref_img_.u_buffer[x + y * ref_img_.uv_stride] =
146 img_.u_buffer[x + y * img_.uv_stride];
147 ref_img_.v_buffer[x + y * ref_img_.uv_stride] =
148 img_.v_buffer[x + y * img_.uv_stride];
149 }
150 }
151
152 ReferenceExtendBorder();
153 }
154
155 void CompareImages(const YV12_BUFFER_CONFIG actual) {
156 EXPECT_EQ(ref_img_.frame_size, actual.frame_size);
157 EXPECT_EQ(0, memcmp(ref_img_.buffer_alloc, actual.buffer_alloc,
158 ref_img_.frame_size));
159 }
160
161 YV12_BUFFER_CONFIG img_;
162 YV12_BUFFER_CONFIG ref_img_;
163 YV12_BUFFER_CONFIG cpy_img_;
164 int width_;
165 int height_;
166};
167
168class ExtendBorderTest
169 : public VpxScaleBase,
170 public ::testing::TestWithParam<ExtendFrameBorderFunc> {
171 public:
172 virtual ~ExtendBorderTest() {}
173
174 protected:
175 virtual void SetUp() {
176 extend_fn_ = GetParam();
177 }
178
Johanna04ace22014-07-16 03:47:27 -0700179 void ExtendBorder() {
180 ASM_REGISTER_STATE_CHECK(extend_fn_(&img_));
181 }
182
Johanna04ace22014-07-16 03:47:27 -0700183 void RunTest() {
184#if ARCH_ARM
185 // Some arm devices OOM when trying to allocate the largest buffers.
186 static const int kNumSizesToTest = 6;
187#else
188 static const int kNumSizesToTest = 7;
189#endif
190 static const int kSizesToTest[] = {1, 15, 33, 145, 512, 1025, 16383};
191 for (int h = 0; h < kNumSizesToTest; ++h) {
192 for (int w = 0; w < kNumSizesToTest; ++w) {
193 ResetImage(kSizesToTest[w], kSizesToTest[h]);
194 ExtendBorder();
195 ReferenceExtendBorder();
Johann3f701df2014-07-18 13:53:53 -0700196 CompareImages(img_);
Johanna04ace22014-07-16 03:47:27 -0700197 DeallocImage();
198 }
199 }
200 }
201
Johanna04ace22014-07-16 03:47:27 -0700202 ExtendFrameBorderFunc extend_fn_;
Johanna04ace22014-07-16 03:47:27 -0700203};
204
205TEST_P(ExtendBorderTest, ExtendBorder) {
206 ASSERT_NO_FATAL_FAILURE(RunTest());
207}
208
209INSTANTIATE_TEST_CASE_P(C, ExtendBorderTest,
210 ::testing::Values(vp8_yv12_extend_frame_borders_c));
Johann3f701df2014-07-18 13:53:53 -0700211
212class CopyFrameTest
213 : public VpxScaleBase,
214 public ::testing::TestWithParam<CopyFrameFunc> {
215 public:
216 virtual ~CopyFrameTest() {}
217
218 protected:
219 virtual void SetUp() {
220 copy_frame_fn_ = GetParam();
221 }
222
223 void CopyFrame() {
224 ASM_REGISTER_STATE_CHECK(copy_frame_fn_(&img_, &cpy_img_));
225 }
226
227 void RunTest() {
228#if ARCH_ARM
229 // Some arm devices OOM when trying to allocate the largest buffers.
230 static const int kNumSizesToTest = 6;
231#else
232 static const int kNumSizesToTest = 7;
233#endif
234 static const int kSizesToTest[] = {1, 15, 33, 145, 512, 1025, 16383};
235 for (int h = 0; h < kNumSizesToTest; ++h) {
236 for (int w = 0; w < kNumSizesToTest; ++w) {
237 ResetImage(kSizesToTest[w], kSizesToTest[h]);
238 ReferenceCopyFrame();
239 CopyFrame();
240 CompareImages(cpy_img_);
241 DeallocImage();
242 }
243 }
244 }
245
246 CopyFrameFunc copy_frame_fn_;
247};
248
249TEST_P(CopyFrameTest, CopyFrame) {
250 ASSERT_NO_FATAL_FAILURE(RunTest());
251}
252
253INSTANTIATE_TEST_CASE_P(C, CopyFrameTest,
254 ::testing::Values(vp8_yv12_copy_frame_c));
Johanna04ace22014-07-16 03:47:27 -0700255} // namespace