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 "test/clear_system_state.h"
14 #include "test/register_state_check.h"
16 #include "./vpx_config.h"
17 #include "./vpx_scale_rtcd.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_scale/yv12config.h"
23 typedef void (*ExtendFrameBorderFunc)(YV12_BUFFER_CONFIG *ybf);
24 typedef void (*CopyFrameFunc)(const YV12_BUFFER_CONFIG *src_ybf,
25 YV12_BUFFER_CONFIG *dst_ybf);
29 virtual ~VpxScaleBase() {
30 libvpx_test::ClearSystemState();
33 void ResetImage(int width, int height) {
36 vpx_memset(&img_, 0, sizeof(img_));
37 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&img_, width_, height_,
39 vpx_memset(img_.buffer_alloc, kBufFiller, img_.frame_size);
40 FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
42 FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
44 FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
47 vpx_memset(&ref_img_, 0, sizeof(ref_img_));
48 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&ref_img_, width_, height_,
50 vpx_memset(ref_img_.buffer_alloc, kBufFiller, ref_img_.frame_size);
52 vpx_memset(&cpy_img_, 0, sizeof(cpy_img_));
53 ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&cpy_img_, width_, height_,
55 vpx_memset(cpy_img_.buffer_alloc, kBufFiller, cpy_img_.frame_size);
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_);
66 static const int kBufFiller = 123;
67 static const int kBufMax = kBufFiller - 1;
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;
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);
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);
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;
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);
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);
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,
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,
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,
130 ref_img_.border / 2);
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];
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];
153 ReferenceExtendBorder();
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));
162 YV12_BUFFER_CONFIG img_;
163 YV12_BUFFER_CONFIG ref_img_;
164 YV12_BUFFER_CONFIG cpy_img_;
169 class ExtendBorderTest
170 : public VpxScaleBase,
171 public ::testing::TestWithParam<ExtendFrameBorderFunc> {
173 virtual ~ExtendBorderTest() {}
176 virtual void SetUp() {
177 extend_fn_ = GetParam();
180 void ExtendBorder() {
181 ASM_REGISTER_STATE_CHECK(extend_fn_(&img_));
186 // Some arm devices OOM when trying to allocate the largest buffers.
187 static const int kNumSizesToTest = 6;
189 static const int kNumSizesToTest = 7;
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]);
196 ReferenceExtendBorder();
203 ExtendFrameBorderFunc extend_fn_;
206 TEST_P(ExtendBorderTest, ExtendBorder) {
207 ASSERT_NO_FATAL_FAILURE(RunTest());
210 INSTANTIATE_TEST_CASE_P(C, ExtendBorderTest,
211 ::testing::Values(vp8_yv12_extend_frame_borders_c));
214 : public VpxScaleBase,
215 public ::testing::TestWithParam<CopyFrameFunc> {
217 virtual ~CopyFrameTest() {}
220 virtual void SetUp() {
221 copy_frame_fn_ = GetParam();
225 ASM_REGISTER_STATE_CHECK(copy_frame_fn_(&img_, &cpy_img_));
230 // Some arm devices OOM when trying to allocate the largest buffers.
231 static const int kNumSizesToTest = 6;
233 static const int kNumSizesToTest = 7;
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();
241 CompareImages(cpy_img_);
247 CopyFrameFunc copy_frame_fn_;
250 TEST_P(CopyFrameTest, CopyFrame) {
251 ASSERT_NO_FATAL_FAILURE(RunTest());
254 INSTANTIATE_TEST_CASE_P(C, CopyFrameTest,
255 ::testing::Values(vp8_yv12_copy_frame_c));