]> granicus.if.org Git - libvpx/blob - test/vpx_scale_test.cc
Merge "Cosmetics - Fix header file order in unit tests"
[libvpx] / test / vpx_scale_test.cc
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:
28   virtual ~VpxScaleBase() {
29     libvpx_test::ClearSystemState();
30   }
31
32   void ResetImage(int width, int height) {
33     width_ = width;
34     height_ = height;
35     memset(&img_, 0, sizeof(img_));
36     ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&img_, width_, height_,
37                                              VP8BORDERINPIXELS));
38     memset(img_.buffer_alloc, kBufFiller, img_.frame_size);
39     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
46     memset(&ref_img_, 0, sizeof(ref_img_));
47     ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&ref_img_, width_, height_,
48                                              VP8BORDERINPIXELS));
49     memset(ref_img_.buffer_alloc, kBufFiller, ref_img_.frame_size);
50
51     memset(&cpy_img_, 0, sizeof(cpy_img_));
52     ASSERT_EQ(0, vp8_yv12_alloc_frame_buffer(&cpy_img_, width_, height_,
53                                              VP8BORDERINPIXELS));
54     memset(cpy_img_.buffer_alloc, kBufFiller, cpy_img_.frame_size);
55     ReferenceCopyFrame();
56   }
57
58   void DeallocImage() {
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_);
62   }
63
64  protected:
65   static const int kBufFiller = 123;
66   static const int kBufMax = kBufFiller - 1;
67
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;
72       }
73     }
74   }
75
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);
86
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);
91       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) {
103       memcpy(top, left, extend_width);
104       top += stride;
105     }
106
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);
110       bottom += stride;
111     }
112   }
113
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,
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
168 class 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
179   void ExtendBorder() {
180     ASM_REGISTER_STATE_CHECK(extend_fn_(&img_));
181   }
182
183   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();
196         CompareImages(img_);
197         DeallocImage();
198       }
199     }
200   }
201
202   ExtendFrameBorderFunc extend_fn_;
203 };
204
205 TEST_P(ExtendBorderTest, ExtendBorder) {
206   ASSERT_NO_FATAL_FAILURE(RunTest());
207 }
208
209 INSTANTIATE_TEST_CASE_P(C, ExtendBorderTest,
210                         ::testing::Values(vp8_yv12_extend_frame_borders_c));
211
212 class 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
249 TEST_P(CopyFrameTest, CopyFrame) {
250   ASSERT_NO_FATAL_FAILURE(RunTest());
251 }
252
253 INSTANTIATE_TEST_CASE_P(C, CopyFrameTest,
254                         ::testing::Values(vp8_yv12_copy_frame_c));
255 }  // namespace