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 #ifndef VPX_TEST_VPX_SCALE_TEST_H_
12 #define VPX_TEST_VPX_SCALE_TEST_H_
13 
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15 
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"
23 
24 using libvpx_test::ACMRandom;
25 
26 namespace libvpx_test {
27 
28 class VpxScaleBase {
29  public:
~VpxScaleBase()30   virtual ~VpxScaleBase() { libvpx_test::ClearSystemState(); }
31 
ResetImage(YV12_BUFFER_CONFIG * const img,const int width,const int height)32   void ResetImage(YV12_BUFFER_CONFIG *const img, const int width,
33                   const int height) {
34     memset(img, 0, sizeof(*img));
35     ASSERT_EQ(
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);
39   }
40 
ResetImages(const int width,const int height)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);
45 
46     FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
47               img_.y_stride);
48     FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
49               img_.uv_stride);
50     FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
51               img_.uv_stride);
52   }
53 
ResetScaleImage(YV12_BUFFER_CONFIG * const img,const int width,const int height)54   void ResetScaleImage(YV12_BUFFER_CONFIG *const img, const int width,
55                        const int height) {
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));
60 #else
61     ASSERT_EQ(0, vpx_alloc_frame_buffer(img, width, height, 1, 1,
62                                         VP9_ENC_BORDER_IN_PIXELS, 0));
63 #endif
64     memset(img->buffer_alloc, kBufFiller, img->frame_size);
65   }
66 
ResetScaleImages(const int src_width,const int src_height,const int dst_width,const int dst_height)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,
73                      img_.y_stride);
74     FillPlaneExtreme(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
75                      img_.uv_stride);
76     FillPlaneExtreme(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
77                      img_.uv_stride);
78   }
79 
DeallocImages()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_);
84   }
85 
DeallocScaleImages()86   void DeallocScaleImages() {
87     vpx_free_frame_buffer(&img_);
88     vpx_free_frame_buffer(&ref_img_);
89     vpx_free_frame_buffer(&dst_img_);
90   }
91 
92  protected:
93   static const int kBufFiller = 123;
94   static const int kBufMax = kBufFiller - 1;
95 
FillPlane(uint8_t * const buf,const int width,const int height,const int stride)96   static void FillPlane(uint8_t *const buf, const int width, const int height,
97                         const int stride) {
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;
101       }
102     }
103   }
104 
FillPlaneExtreme(uint8_t * const buf,const int width,const int height,const int stride)105   static void FillPlaneExtreme(uint8_t *const buf, const int width,
106                                const int height, const int stride) {
107     ACMRandom rnd;
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;
111       }
112     }
113   }
114 
ExtendPlane(uint8_t * buf,int crop_width,int crop_height,int width,int height,int stride,int padding)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);
125 
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);
130       left += stride;
131       right += stride;
132     }
133 
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;
139 
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);
143       top += stride;
144     }
145 
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);
149       bottom += stride;
150     }
151   }
152 
ReferenceExtendBorder()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);
163   }
164 
ReferenceCopyFrame()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];
173       }
174     }
175 
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];
182       }
183     }
184 
185     ReferenceExtendBorder();
186   }
187 
CompareImages(const YV12_BUFFER_CONFIG actual)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));
192   }
193 
194   YV12_BUFFER_CONFIG img_;
195   YV12_BUFFER_CONFIG ref_img_;
196   YV12_BUFFER_CONFIG dst_img_;
197 };
198 
199 }  // namespace libvpx_test
200 
201 #endif  // VPX_TEST_VPX_SCALE_TEST_H_
202