2 * Copyright (c) 2014 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.
11 #include "third_party/googletest/src/include/gtest/gtest.h"
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"
22 typedef void (*ExtendFrameBorderFunc)(YV12_BUFFER_CONFIG *ybf);
23 typedef void (*CopyFrameFunc)(const YV12_BUFFER_CONFIG *src_ybf,
24 YV12_BUFFER_CONFIG *dst_ybf);
28 virtual ~VpxScaleBase() {
29 libvpx_test::ClearSystemState();
32 void ResetImage(int width, int height) {
35 memset(&img_, 0, sizeof(img_));
36 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&img_, width_, height_,
38 memset(img_.buffer_alloc, kBufFiller, img_.frame_size);
39 FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
41 FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
43 FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
46 memset(&ref_img_, 0, sizeof(ref_img_));
47 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&ref_img_, width_, height_,
49 memset(ref_img_.buffer_alloc, kBufFiller, ref_img_.frame_size);
51 memset(&cpy_img_, 0, sizeof(cpy_img_));
52 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&cpy_img_, width_, height_,
54 memset(cpy_img_.buffer_alloc, kBufFiller, cpy_img_.frame_size);
59 vp8_yv12_de_alloc_frame_buffer(&img_);
60 vp8_yv12_de_alloc_frame_buffer(&ref_img_);
61 vp8_yv12_de_alloc_frame_buffer(&cpy_img_);
65 static const int kBufFiller = 123;
66 static const int kBufMax = kBufFiller - 1;
68 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;
76 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);
87 // Fill the border pixels from the nearest image pixel.
88 for (int y = 0; y < crop_height; ++y) {
89 memset(left, left[padding], padding);
90 memset(right, right[-1], right_extend);
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;
101 // The first row was already extended to the left and right. Copy it up.
102 for (int y = 0; y < padding; ++y) {
103 memcpy(top, left, extend_width);
107 uint8_t *bottom = left + (crop_height * stride);
108 for (int y = 0; y < bottom_extend; ++y) {
109 memcpy(bottom, left + (crop_height - 1) * stride, extend_width);
114 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,
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,
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,
129 ref_img_.border / 2);
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];
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];
152 ReferenceExtendBorder();
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));
161 YV12_BUFFER_CONFIG img_;
162 YV12_BUFFER_CONFIG ref_img_;
163 YV12_BUFFER_CONFIG cpy_img_;
168 class ExtendBorderTest
169 : public VpxScaleBase,
170 public ::testing::TestWithParam<ExtendFrameBorderFunc> {
172 virtual ~ExtendBorderTest() {}
175 virtual void SetUp() {
176 extend_fn_ = GetParam();
179 void ExtendBorder() {
180 ASM_REGISTER_STATE_CHECK(extend_fn_(&img_));
185 // Some arm devices OOM when trying to allocate the largest buffers.
186 static const int kNumSizesToTest = 6;
188 static const int kNumSizesToTest = 7;
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]);
195 ReferenceExtendBorder();
202 ExtendFrameBorderFunc extend_fn_;
205 TEST_P(ExtendBorderTest, ExtendBorder) {
206 ASSERT_NO_FATAL_FAILURE(RunTest());
209 INSTANTIATE_TEST_CASE_P(C, ExtendBorderTest,
210 ::testing::Values(vp8_yv12_extend_frame_borders_c));
213 : public VpxScaleBase,
214 public ::testing::TestWithParam<CopyFrameFunc> {
216 virtual ~CopyFrameTest() {}
219 virtual void SetUp() {
220 copy_frame_fn_ = GetParam();
224 ASM_REGISTER_STATE_CHECK(copy_frame_fn_(&img_, &cpy_img_));
229 // Some arm devices OOM when trying to allocate the largest buffers.
230 static const int kNumSizesToTest = 6;
232 static const int kNumSizesToTest = 7;
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();
240 CompareImages(cpy_img_);
246 CopyFrameFunc copy_frame_fn_;
249 TEST_P(CopyFrameTest, CopyFrame) {
250 ASSERT_NO_FATAL_FAILURE(RunTest());
253 INSTANTIATE_TEST_CASE_P(C, CopyFrameTest,
254 ::testing::Values(vp8_yv12_copy_frame_c));