blob: 77a731f0d5d2bfb380006f60c0280b808fe284df [file] [log] [blame]
elliottk35be78d2018-10-17 16:07:12 -07001/*
2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
elliottk1dbb8c12018-11-06 22:28:59 -080012#include <stdbool.h>
elliottk35be78d2018-10-17 16:07:12 -070013#include "aom_mem/aom_mem.h"
14#include "av1/encoder/rdopt.h"
elliottk84c6daf2018-11-01 17:39:08 -070015#include "test/util.h"
elliottk35be78d2018-10-17 16:07:12 -070016#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
17
18namespace {
19
elliottk9be59222018-10-19 16:17:17 -070020using ::testing::get;
21using ::testing::tuple;
22
elliottk1dbb8c12018-11-06 22:28:59 -080023static int get_pix(uint8_t *buf, int i, bool high_bd) {
24 if (high_bd) {
25 return *CONVERT_TO_SHORTPTR(buf + i);
26 } else {
27 return buf[i];
28 }
29}
30
elliottk84c6daf2018-11-01 17:39:08 -070031/** Get the (i, j) value from the input; if i or j is outside of the width
elliottk35be78d2018-10-17 16:07:12 -070032 * or height, the nearest pixel value is returned.
33 */
elliottk1dbb8c12018-11-06 22:28:59 -080034static int get_nearest_pix(const int *buf, int w, int h, int i, int j) {
elliottk84c6daf2018-11-01 17:39:08 -070035 int offset = AOMMAX(AOMMIN(i, w - 1), 0) + w * AOMMAX(AOMMIN(j, h - 1), 0);
elliottk1dbb8c12018-11-06 22:28:59 -080036 return buf[offset];
elliottk35be78d2018-10-17 16:07:12 -070037}
38
39/** Given the image data, creates a new image with padded values, so an
40 * 8-tap filter can be convolved. The padded value is the same as the closest
41 * value in the image. Returns a pointer to the start of the image in the
elliottk1dbb8c12018-11-06 22:28:59 -080042 * padded data. Must be freed with free_pad_8tap. The output will be either
43 * 8-bit or 16-bit, depending on the high bit-depth (high_bd) field.
elliottk35be78d2018-10-17 16:07:12 -070044 */
elliottk1dbb8c12018-11-06 22:28:59 -080045static uint8_t *pad_8tap_convolve(const int *data, int w, int h, bool high_bd) {
elliottk35be78d2018-10-17 16:07:12 -070046 // SIMD optimizations require the width to be a multiple of 8 and the height
47 // to be multiples of 4.
48 assert(w % 8 == 0);
49 assert(h % 4 == 0);
50 // For an 8-tap filter, we need to pad with 3 lines on top and on the left,
51 // and 4 lines on the right and bottom, for 7 extra lines.
52 const int pad_w = w + 7;
53 const int pad_h = h + 7;
elliottk84c6daf2018-11-01 17:39:08 -070054
55 uint8_t *dst;
elliottk1dbb8c12018-11-06 22:28:59 -080056 if (high_bd) {
elliottk84c6daf2018-11-01 17:39:08 -070057 dst =
58 CONVERT_TO_BYTEPTR(aom_memalign(32, sizeof(uint16_t) * pad_w * pad_h));
elliottk1dbb8c12018-11-06 22:28:59 -080059 } else {
60 dst = (uint8_t *)aom_memalign(32, sizeof(uint8_t) * pad_w * pad_h);
elliottk84c6daf2018-11-01 17:39:08 -070061 }
elliottk35be78d2018-10-17 16:07:12 -070062 for (int j = 0; j < pad_h; ++j) {
63 for (int i = 0; i < pad_w; ++i) {
elliottk1dbb8c12018-11-06 22:28:59 -080064 const int v = get_nearest_pix(data, w, h, i - 3, j - 3);
65 if (high_bd) {
elliottk84c6daf2018-11-01 17:39:08 -070066 *CONVERT_TO_SHORTPTR(dst + i + j * pad_w) = v;
elliottk1dbb8c12018-11-06 22:28:59 -080067 } else {
68 dst[i + j * pad_w] = v;
elliottk84c6daf2018-11-01 17:39:08 -070069 }
elliottk35be78d2018-10-17 16:07:12 -070070 }
71 }
72 return dst + (w + 7) * 3 + 3;
73}
74
elliottk84c6daf2018-11-01 17:39:08 -070075static int stride_8tap(int width) { return width + 7; }
76
elliottk1dbb8c12018-11-06 22:28:59 -080077static void free_pad_8tap(uint8_t *padded, int width, bool high_bd) {
78 if (high_bd) {
elliottk84c6daf2018-11-01 17:39:08 -070079 aom_free(CONVERT_TO_SHORTPTR(padded - (width + 7) * 3 - 3));
elliottk1dbb8c12018-11-06 22:28:59 -080080 } else {
81 aom_free(padded - (width + 7) * 3 - 3);
elliottk84c6daf2018-11-01 17:39:08 -070082 }
elliottk35be78d2018-10-17 16:07:12 -070083}
84
elliottk1dbb8c12018-11-06 22:28:59 -080085static uint8_t *malloc_bd(int num_entries, bool high_bd) {
86 const int bytes_per_entry = high_bd ? sizeof(uint16_t) : sizeof(uint8_t);
elliottk84c6daf2018-11-01 17:39:08 -070087
88 uint8_t *buf = (uint8_t *)aom_memalign(32, bytes_per_entry * num_entries);
elliottk1dbb8c12018-11-06 22:28:59 -080089 if (high_bd) {
elliottk84c6daf2018-11-01 17:39:08 -070090 return CONVERT_TO_BYTEPTR(buf);
elliottk1dbb8c12018-11-06 22:28:59 -080091 } else {
92 return buf;
elliottk84c6daf2018-11-01 17:39:08 -070093 }
94}
95
elliottk1dbb8c12018-11-06 22:28:59 -080096static void free_bd(uint8_t *p, bool high_bd) {
97 if (high_bd) {
elliottk84c6daf2018-11-01 17:39:08 -070098 aom_free(CONVERT_TO_SHORTPTR(p));
elliottk1dbb8c12018-11-06 22:28:59 -080099 } else {
100 aom_free(p);
elliottk84c6daf2018-11-01 17:39:08 -0700101 }
102}
103
104class EdgeDetectBrightnessTest :
elliottk1dbb8c12018-11-06 22:28:59 -0800105 // Parameters are (brightness, width, height, high bit depth representation,
106 // bit depth).
107 public ::testing::TestWithParam<tuple<int, int, int, bool, int> > {
elliottk84c6daf2018-11-01 17:39:08 -0700108 protected:
109 void SetUp() override {
110 // Allocate a (width by height) array of luma values in orig_.
111 // padded_ will be filled by the pad() call, which adds a border around
112 // the orig_. The output_ array has enough space for the computation.
elliottk1dbb8c12018-11-06 22:28:59 -0800113 const int brightness = GET_PARAM(0);
elliottk84c6daf2018-11-01 17:39:08 -0700114 const int width = GET_PARAM(1);
115 const int height = GET_PARAM(2);
elliottk1dbb8c12018-11-06 22:28:59 -0800116 const bool high_bd = GET_PARAM(3);
117
118 // Create the padded image of uniform brightness.
119 int *orig = (int *)malloc(width * height * sizeof(int));
120 for (int i = 0; i < width * height; ++i) {
121 orig[i] = brightness;
122 }
123 input_ = pad_8tap_convolve(orig, width, height, high_bd);
124 free(orig);
125 output_ = malloc_bd(width * height, high_bd);
elliottk84c6daf2018-11-01 17:39:08 -0700126 }
127
128 void TearDown() override {
elliottk84c6daf2018-11-01 17:39:08 -0700129 const int width = GET_PARAM(1);
elliottk1dbb8c12018-11-06 22:28:59 -0800130 const bool high_bd = GET_PARAM(3);
131 free_pad_8tap(input_, width, high_bd);
132 free_bd(output_, high_bd);
elliottk84c6daf2018-11-01 17:39:08 -0700133 }
134
elliottk1dbb8c12018-11-06 22:28:59 -0800135 // Skip the tests where brightness exceeds the bit-depth; we run into this
136 // issue because of gtest's limitation on valid combinations of test
137 // parameters. Also skip the tests where bit depth is greater than 8, but
138 // high bit depth representation is not set.
139 bool should_skip() const {
140 const int brightness = GET_PARAM(0);
141 const int bd = GET_PARAM(4);
142 if (brightness >= (1 << bd)) {
143 return true;
144 }
145 const bool high_bd = GET_PARAM(3);
146 if (bd > 8 && !high_bd) {
147 return true;
148 }
149 return false;
150 }
151
152 uint8_t *input_;
elliottk84c6daf2018-11-01 17:39:08 -0700153 uint8_t *output_;
154};
elliottk35be78d2018-10-17 16:07:12 -0700155
156TEST_P(EdgeDetectBrightnessTest, BlurUniformBrightness) {
elliottk1dbb8c12018-11-06 22:28:59 -0800157 // Some combination of parameters are non-sensical, due to limitations
158 // of the testing framework. Ignore these.
159 if (should_skip()) {
160 return;
161 }
162
elliottk35be78d2018-10-17 16:07:12 -0700163 // For varying levels of brightness, the algorithm should
164 // produce the same output.
elliottk84c6daf2018-11-01 17:39:08 -0700165 const int brightness = GET_PARAM(0);
166 const int width = GET_PARAM(1);
167 const int height = GET_PARAM(2);
elliottk1dbb8c12018-11-06 22:28:59 -0800168 const bool high_bd = GET_PARAM(3);
169 const int bd = GET_PARAM(4);
170
171 gaussian_blur(input_, stride_8tap(width), width, height, output_, high_bd,
172 bd);
elliottk35be78d2018-10-17 16:07:12 -0700173 for (int i = 0; i < width * height; ++i) {
elliottk1dbb8c12018-11-06 22:28:59 -0800174 ASSERT_EQ(brightness, get_pix(output_, i, high_bd));
elliottk84c6daf2018-11-01 17:39:08 -0700175 }
elliottk35be78d2018-10-17 16:07:12 -0700176}
177
178// No edges on a uniformly bright image.
179TEST_P(EdgeDetectBrightnessTest, DetectUniformBrightness) {
elliottk1dbb8c12018-11-06 22:28:59 -0800180 if (should_skip()) {
elliottk84c6daf2018-11-01 17:39:08 -0700181 return;
elliottk35be78d2018-10-17 16:07:12 -0700182 }
elliottk1dbb8c12018-11-06 22:28:59 -0800183 const int width = GET_PARAM(1);
184 const int height = GET_PARAM(2);
185 const bool high_bd = GET_PARAM(3);
186 const int bd = GET_PARAM(4);
187
elliottk17dd3ea2019-02-01 11:59:57 -0800188 ASSERT_EQ(
189 0, av1_edge_exists(input_, stride_8tap(width), width, height, high_bd, bd)
190 .magnitude);
elliottk35be78d2018-10-17 16:07:12 -0700191}
192
193INSTANTIATE_TEST_CASE_P(ImageBrightnessTests, EdgeDetectBrightnessTest,
194 ::testing::Combine(
195 // Brightness
elliottk84c6daf2018-11-01 17:39:08 -0700196 ::testing::Values(0, 1, 2, 127, 128, 129, 254, 255,
197 256, 511, 512, 1023, 1024, 2048,
198 4095),
elliottk35be78d2018-10-17 16:07:12 -0700199 // Width
200 ::testing::Values(8, 16, 32),
201 // Height
elliottk84c6daf2018-11-01 17:39:08 -0700202 ::testing::Values(4, 8, 12, 32),
elliottk1dbb8c12018-11-06 22:28:59 -0800203 // High bit depth representation
204 ::testing::Bool(),
elliottk84c6daf2018-11-01 17:39:08 -0700205 // Bit depth
206 ::testing::Values(8, 10, 12)));
elliottk35be78d2018-10-17 16:07:12 -0700207
208class EdgeDetectImageTest :
elliottk1dbb8c12018-11-06 22:28:59 -0800209 // Parameters are (width, height, high bit depth representation, bit depth).
210 public ::testing::TestWithParam<tuple<int, int, bool, int> > {
211 protected:
212 // Skip the tests where bit depth is greater than 8, but high bit depth
213 // representation is not set (limitation of testing framework).
214 bool should_skip() const {
215 const bool high_bd = GET_PARAM(2);
216 const int bd = GET_PARAM(3);
217 return bd > 8 && !high_bd;
218 }
219};
elliottk35be78d2018-10-17 16:07:12 -0700220
221// Generate images with black on one side and white on the other.
222TEST_P(EdgeDetectImageTest, BlackWhite) {
elliottk1dbb8c12018-11-06 22:28:59 -0800223 // Some combination of parameters are non-sensical, due to limitations
224 // of the testing framework. Ignore these.
225 if (should_skip()) {
226 return;
227 }
228
elliottk84c6daf2018-11-01 17:39:08 -0700229 const int width = GET_PARAM(0);
230 const int height = GET_PARAM(1);
elliottk1dbb8c12018-11-06 22:28:59 -0800231 const bool high_bd = GET_PARAM(2);
232 const int bd = GET_PARAM(3);
233
elliottk84c6daf2018-11-01 17:39:08 -0700234 const int white = (1 << bd) - 1;
elliottk1dbb8c12018-11-06 22:28:59 -0800235 int *orig = (int *)malloc(width * height * sizeof(int));
elliottk35be78d2018-10-17 16:07:12 -0700236 for (int j = 0; j < height; ++j) {
237 for (int i = 0; i < width; ++i) {
238 if (i < width / 2) {
elliottk1dbb8c12018-11-06 22:28:59 -0800239 orig[i + j * width] = 0;
elliottk35be78d2018-10-17 16:07:12 -0700240 } else {
elliottk1dbb8c12018-11-06 22:28:59 -0800241 orig[i + j * width] = white;
elliottk35be78d2018-10-17 16:07:12 -0700242 }
243 }
244 }
elliottk1dbb8c12018-11-06 22:28:59 -0800245 uint8_t *padded = pad_8tap_convolve(orig, width, height, high_bd);
246 free(orig);
elliottk84c6daf2018-11-01 17:39:08 -0700247 // Value should be between 556 and 560.
elliottk1dbb8c12018-11-06 22:28:59 -0800248 ASSERT_LE(556, av1_edge_exists(padded, stride_8tap(width), width, height,
elliottk17dd3ea2019-02-01 11:59:57 -0800249 high_bd, bd)
250 .magnitude);
elliottk1dbb8c12018-11-06 22:28:59 -0800251 ASSERT_GE(560, av1_edge_exists(padded, stride_8tap(width), width, height,
elliottk17dd3ea2019-02-01 11:59:57 -0800252 high_bd, bd)
253 .magnitude);
elliottk84c6daf2018-11-01 17:39:08 -0700254
elliottk1dbb8c12018-11-06 22:28:59 -0800255 free_pad_8tap(padded, width, high_bd);
elliottk35be78d2018-10-17 16:07:12 -0700256}
257
elliottk84c6daf2018-11-01 17:39:08 -0700258// Hardcoded blur tests.
elliottk1dbb8c12018-11-06 22:28:59 -0800259static const int luma[32] = { 241, 147, 7, 90, 184, 103, 28, 186,
260 2, 248, 49, 242, 114, 146, 127, 22,
261 121, 228, 167, 108, 158, 174, 41, 168,
262 214, 99, 184, 109, 114, 247, 117, 119 };
elliottk84c6daf2018-11-01 17:39:08 -0700263static const uint8_t expected[] = { 161, 138, 119, 118, 123, 118, 113, 122,
264 143, 140, 134, 133, 134, 126, 116, 114,
265 147, 149, 145, 142, 143, 138, 126, 118,
266 164, 156, 148, 144, 148, 148, 138, 126 };
267
elliottk1dbb8c12018-11-06 22:28:59 -0800268static void hardcoded_blur_test_aux(const bool high_bd) {
elliottk35be78d2018-10-17 16:07:12 -0700269 const int w = 8;
270 const int h = 4;
elliottk1dbb8c12018-11-06 22:28:59 -0800271 for (int bd = 8; bd <= 12; bd += 2) {
272 // Skip the tests where bit depth is greater than 8, but high bit depth
273 // representation is not set.
274 if (bd > 8 && !high_bd) {
275 break;
elliottk84c6daf2018-11-01 17:39:08 -0700276 }
elliottk1dbb8c12018-11-06 22:28:59 -0800277 uint8_t *output = malloc_bd(w * h, high_bd);
278 uint8_t *padded = pad_8tap_convolve(luma, w, h, high_bd);
279 gaussian_blur(padded, stride_8tap(w), w, h, output, high_bd, bd);
elliottk84c6daf2018-11-01 17:39:08 -0700280 for (int i = 0; i < w * h; ++i) {
elliottk1dbb8c12018-11-06 22:28:59 -0800281 ASSERT_EQ(expected[i], get_pix(output, i, high_bd));
elliottk84c6daf2018-11-01 17:39:08 -0700282 }
elliottk1dbb8c12018-11-06 22:28:59 -0800283 free_pad_8tap(padded, w, high_bd);
284 free_bd(output, high_bd);
285
286 // If we multiply the inputs by a constant factor, the output should not
287 // vary more than 0.5 * factor.
elliottk84c6daf2018-11-01 17:39:08 -0700288 for (int c = 2; c < (1 << (bd - 8)); ++c) {
elliottk1dbb8c12018-11-06 22:28:59 -0800289 int scaled_luma[32];
elliottk84c6daf2018-11-01 17:39:08 -0700290 for (int i = 0; i < 32; ++i) {
elliottk1dbb8c12018-11-06 22:28:59 -0800291 scaled_luma[i] = luma[i] * c;
elliottk84c6daf2018-11-01 17:39:08 -0700292 }
elliottk1dbb8c12018-11-06 22:28:59 -0800293 uint8_t *output = malloc_bd(w * h, high_bd);
294 uint8_t *padded = pad_8tap_convolve(scaled_luma, w, h, high_bd);
295 gaussian_blur(padded, stride_8tap(w), w, h, output, high_bd, bd);
elliottk84c6daf2018-11-01 17:39:08 -0700296 for (int i = 0; i < w * h; ++i) {
elliottk1dbb8c12018-11-06 22:28:59 -0800297 ASSERT_GE(c / 2, abs(expected[i] * c - get_pix(output, i, high_bd)));
elliottk84c6daf2018-11-01 17:39:08 -0700298 }
elliottk1dbb8c12018-11-06 22:28:59 -0800299 free_pad_8tap(padded, w, high_bd);
300 free_bd(output, high_bd);
elliottk84c6daf2018-11-01 17:39:08 -0700301 }
302 }
303}
304
elliottk1dbb8c12018-11-06 22:28:59 -0800305TEST(EdgeDetectImageTest, HardcodedBlurTest) {
306 hardcoded_blur_test_aux(false);
307 hardcoded_blur_test_aux(true);
elliottk84c6daf2018-11-01 17:39:08 -0700308}
309
310TEST(EdgeDetectImageTest, SobelTest) {
311 // Randomly generated 3x3. Compute Sobel for middle value.
312 const uint8_t buf[9] = { 241, 147, 7, 90, 184, 103, 28, 186, 2 };
313 const int stride = 3;
elliottk1dbb8c12018-11-06 22:28:59 -0800314 bool high_bd = false;
315 sobel_xy result = sobel(buf, stride, 1, 1, high_bd);
316 ASSERT_EQ(234, result.x);
317 ASSERT_EQ(140, result.y);
318
319 // Verify it works for 8-bit values in a high bit-depth buffer.
320 const uint16_t buf8_16[9] = { 241, 147, 7, 90, 184, 103, 28, 186, 2 };
321 high_bd = true;
322 result = sobel(CONVERT_TO_BYTEPTR(buf8_16), stride, 1, 1, high_bd);
elliottk84c6daf2018-11-01 17:39:08 -0700323 ASSERT_EQ(234, result.x);
324 ASSERT_EQ(140, result.y);
325
326 // Verify it works for high bit-depth values as well.
327 const uint16_t buf16[9] = { 241, 147, 7, 90, 184, 2003, 1028, 186, 2 };
elliottk1dbb8c12018-11-06 22:28:59 -0800328 result = sobel(CONVERT_TO_BYTEPTR(buf16), stride, 1, 1, high_bd);
329 ASSERT_EQ(-2566, result.x);
330 ASSERT_EQ(-860, result.y);
elliottk35be78d2018-10-17 16:07:12 -0700331}
332
333INSTANTIATE_TEST_CASE_P(EdgeDetectImages, EdgeDetectImageTest,
334 ::testing::Combine(
335 // Width
336 ::testing::Values(8, 16, 32),
337 // Height
elliottk84c6daf2018-11-01 17:39:08 -0700338 ::testing::Values(4, 8, 12, 32),
elliottk1dbb8c12018-11-06 22:28:59 -0800339 // High bit depth representation
340 ::testing::Bool(),
elliottk84c6daf2018-11-01 17:39:08 -0700341 // Bit depth
342 ::testing::Values(8, 10, 12)));
elliottk35be78d2018-10-17 16:07:12 -0700343} // namespace