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