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 #ifndef VPX_TEST_VPX_SCALE_TEST_H_
12 #define VPX_TEST_VPX_SCALE_TEST_H_
14 #include "third_party/googletest/src/include/gtest/gtest.h"
16 #include "./vpx_config.h"
17 #include "./vpx_scale_rtcd.h"
18 #include "test/acm_random.h"
19 #include "test/clear_system_state.h"
20 #include "test/register_state_check.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "vpx_scale/yv12config.h"
24 using libvpx_test::ACMRandom;
26 namespace libvpx_test {
30 virtual ~VpxScaleBase() { libvpx_test::ClearSystemState(); }
32 void ResetImage(YV12_BUFFER_CONFIG *const img, const int width,
34 memset(img, 0, sizeof(*img));
36 0, vp8_yv12_alloc_frame_buffer(img, width, height, VP8BORDERINPIXELS))
37 << "for width: " << width << " height: " << height;
38 memset(img->buffer_alloc, kBufFiller, img->frame_size);
41 void ResetImages(const int width, const int height) {
42 ResetImage(&img_, width, height);
43 ResetImage(&ref_img_, width, height);
44 ResetImage(&dst_img_, width, height);
46 FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
48 FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
50 FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
54 void ResetScaleImage(YV12_BUFFER_CONFIG *const img, const int width,
56 memset(img, 0, sizeof(*img));
57 #if CONFIG_VP9_HIGHBITDEPTH
58 ASSERT_EQ(0, vpx_alloc_frame_buffer(img, width, height, 1, 1, 0,
59 VP9_ENC_BORDER_IN_PIXELS, 0));
61 ASSERT_EQ(0, vpx_alloc_frame_buffer(img, width, height, 1, 1,
62 VP9_ENC_BORDER_IN_PIXELS, 0));
64 memset(img->buffer_alloc, kBufFiller, img->frame_size);
67 void ResetScaleImages(const int src_width, const int src_height,
68 const int dst_width, const int dst_height) {
69 ResetScaleImage(&img_, src_width, src_height);
70 ResetScaleImage(&ref_img_, dst_width, dst_height);
71 ResetScaleImage(&dst_img_, dst_width, dst_height);
72 FillPlaneExtreme(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
74 FillPlaneExtreme(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
76 FillPlaneExtreme(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
80 void DeallocImages() {
81 vp8_yv12_de_alloc_frame_buffer(&img_);
82 vp8_yv12_de_alloc_frame_buffer(&ref_img_);
83 vp8_yv12_de_alloc_frame_buffer(&dst_img_);
86 void DeallocScaleImages() {
87 vpx_free_frame_buffer(&img_);
88 vpx_free_frame_buffer(&ref_img_);
89 vpx_free_frame_buffer(&dst_img_);
93 static const int kBufFiller = 123;
94 static const int kBufMax = kBufFiller - 1;
96 static void FillPlane(uint8_t *const buf, const int width, const int height,
98 for (int y = 0; y < height; ++y) {
99 for (int x = 0; x < width; ++x) {
100 buf[x + (y * stride)] = (x + (width * y)) % kBufMax;
105 static void FillPlaneExtreme(uint8_t *const buf, const int width,
106 const int height, const int stride) {
108 for (int y = 0; y < height; ++y) {
109 for (int x = 0; x < width; ++x) {
110 buf[x + (y * stride)] = rnd.Rand8() % 2 ? 255 : 0;
115 static void ExtendPlane(uint8_t *buf, int crop_width, int crop_height,
116 int width, int height, int stride, int padding) {
117 // Copy the outermost visible pixel to a distance of at least 'padding.'
118 // The buffers are allocated such that there may be excess space outside the
119 // padding. As long as the minimum amount of padding is achieved it is not
120 // necessary to fill this space as well.
121 uint8_t *left = buf - padding;
122 uint8_t *right = buf + crop_width;
123 const int right_extend = padding + (width - crop_width);
124 const int bottom_extend = padding + (height - crop_height);
126 // Fill the border pixels from the nearest image pixel.
127 for (int y = 0; y < crop_height; ++y) {
128 memset(left, left[padding], padding);
129 memset(right, right[-1], right_extend);
134 left = buf - padding;
135 uint8_t *top = left - (stride * padding);
136 // The buffer does not always extend as far as the stride.
137 // Equivalent to padding + width + padding.
138 const int extend_width = padding + crop_width + right_extend;
140 // The first row was already extended to the left and right. Copy it up.
141 for (int y = 0; y < padding; ++y) {
142 memcpy(top, left, extend_width);
146 uint8_t *bottom = left + (crop_height * stride);
147 for (int y = 0; y < bottom_extend; ++y) {
148 memcpy(bottom, left + (crop_height - 1) * stride, extend_width);
153 void ReferenceExtendBorder() {
154 ExtendPlane(ref_img_.y_buffer, ref_img_.y_crop_width,
155 ref_img_.y_crop_height, ref_img_.y_width, ref_img_.y_height,
156 ref_img_.y_stride, ref_img_.border);
157 ExtendPlane(ref_img_.u_buffer, ref_img_.uv_crop_width,
158 ref_img_.uv_crop_height, ref_img_.uv_width, ref_img_.uv_height,
159 ref_img_.uv_stride, ref_img_.border / 2);
160 ExtendPlane(ref_img_.v_buffer, ref_img_.uv_crop_width,
161 ref_img_.uv_crop_height, ref_img_.uv_width, ref_img_.uv_height,
162 ref_img_.uv_stride, ref_img_.border / 2);
165 void ReferenceCopyFrame() {
166 // Copy img_ to ref_img_ and extend frame borders. This will be used for
167 // verifying extend_fn_ as well as copy_frame_fn_.
168 EXPECT_EQ(ref_img_.frame_size, img_.frame_size);
169 for (int y = 0; y < img_.y_crop_height; ++y) {
170 for (int x = 0; x < img_.y_crop_width; ++x) {
171 ref_img_.y_buffer[x + y * ref_img_.y_stride] =
172 img_.y_buffer[x + y * img_.y_stride];
176 for (int y = 0; y < img_.uv_crop_height; ++y) {
177 for (int x = 0; x < img_.uv_crop_width; ++x) {
178 ref_img_.u_buffer[x + y * ref_img_.uv_stride] =
179 img_.u_buffer[x + y * img_.uv_stride];
180 ref_img_.v_buffer[x + y * ref_img_.uv_stride] =
181 img_.v_buffer[x + y * img_.uv_stride];
185 ReferenceExtendBorder();
188 void CompareImages(const YV12_BUFFER_CONFIG actual) {
189 EXPECT_EQ(ref_img_.frame_size, actual.frame_size);
190 EXPECT_EQ(0, memcmp(ref_img_.buffer_alloc, actual.buffer_alloc,
191 ref_img_.frame_size));
194 YV12_BUFFER_CONFIG img_;
195 YV12_BUFFER_CONFIG ref_img_;
196 YV12_BUFFER_CONFIG dst_img_;
199 } // namespace libvpx_test
201 #endif // VPX_TEST_VPX_SCALE_TEST_H_