• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
12 #include <stdbool.h>
13 #include <memory>
14 #include <tuple>
15 #include "aom_mem/aom_mem.h"
16 #include "av1/encoder/rdopt.h"
17 #include "test/util.h"
18 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
19 
20 namespace {
21 
22 using std::get;
23 using std::tuple;
24 
get_pix(uint8_t * buf,int i,bool high_bd)25 static int get_pix(uint8_t *buf, int i, bool high_bd) {
26   if (high_bd) {
27     return *CONVERT_TO_SHORTPTR(buf + i);
28   } else {
29     return buf[i];
30   }
31 }
32 
33 /** Get the (i, j) value from the input; if i or j is outside of the width
34  * or height, the nearest pixel value is returned.
35  */
get_nearest_pix(const int * buf,int w,int h,int i,int j)36 static int get_nearest_pix(const int *buf, int w, int h, int i, int j) {
37   int offset = AOMMAX(AOMMIN(i, w - 1), 0) + w * AOMMAX(AOMMIN(j, h - 1), 0);
38   return buf[offset];
39 }
40 
41 /** Given the image data, creates a new image with padded values, so an
42  * 8-tap filter can be convolved. The padded value is the same as the closest
43  * value in the image. Returns a pointer to the start of the image in the
44  * padded data. Must be freed with free_pad_8tap. The output will be either
45  * 8-bit or 16-bit, depending on the high bit-depth (high_bd) field.
46  */
pad_8tap_convolve(const int * data,int w,int h,bool high_bd)47 static uint8_t *pad_8tap_convolve(const int *data, int w, int h, bool high_bd) {
48   // SIMD optimizations require the width to be a multiple of 8 and the height
49   // to be multiples of 4.
50   assert(w % 8 == 0);
51   assert(h % 4 == 0);
52   // For an 8-tap filter, we need to pad with 3 lines on top and on the left,
53   // and 4 lines on the right and bottom, for 7 extra lines.
54   const int pad_w = w + 7;
55   const int pad_h = h + 7;
56 
57   uint8_t *dst;
58   if (high_bd) {
59     dst =
60         CONVERT_TO_BYTEPTR(aom_memalign(32, sizeof(uint16_t) * pad_w * pad_h));
61   } else {
62     dst = (uint8_t *)aom_memalign(32, sizeof(uint8_t) * pad_w * pad_h);
63   }
64   if (dst == nullptr) {
65     EXPECT_NE(dst, nullptr);
66     return nullptr;
67   }
68 
69   for (int j = 0; j < pad_h; ++j) {
70     for (int i = 0; i < pad_w; ++i) {
71       const int v = get_nearest_pix(data, w, h, i - 3, j - 3);
72       if (high_bd) {
73         *CONVERT_TO_SHORTPTR(dst + i + j * pad_w) = v;
74       } else {
75         dst[i + j * pad_w] = static_cast<uint8_t>(v);
76       }
77     }
78   }
79   return dst + (w + 7) * 3 + 3;
80 }
81 
stride_8tap(int width)82 static int stride_8tap(int width) { return width + 7; }
83 
free_pad_8tap(uint8_t * padded,int width,bool high_bd)84 static void free_pad_8tap(uint8_t *padded, int width, bool high_bd) {
85   if (high_bd) {
86     aom_free(CONVERT_TO_SHORTPTR(padded - (width + 7) * 3 - 3));
87   } else {
88     aom_free(padded - (width + 7) * 3 - 3);
89   }
90 }
91 
92 struct Pad8TapConvolveDeleter {
Pad8TapConvolveDeleter__anon772126b20111::Pad8TapConvolveDeleter93   Pad8TapConvolveDeleter(const int width, const bool high_bd)
94       : width(width), high_bd(high_bd) {}
operator ()__anon772126b20111::Pad8TapConvolveDeleter95   void operator()(uint8_t *p) {
96     if (p != nullptr) {
97       free_pad_8tap(p, width, high_bd);
98     }
99   }
100   const int width;
101   const bool high_bd;
102 };
103 
malloc_bd(int num_entries,bool high_bd)104 static uint8_t *malloc_bd(int num_entries, bool high_bd) {
105   const int bytes_per_entry = high_bd ? sizeof(uint16_t) : sizeof(uint8_t);
106 
107   uint8_t *buf = (uint8_t *)aom_memalign(32, bytes_per_entry * num_entries);
108   if (high_bd) {
109     return CONVERT_TO_BYTEPTR(buf);
110   } else {
111     return buf;
112   }
113 }
114 
free_bd(uint8_t * p,bool high_bd)115 static void free_bd(uint8_t *p, bool high_bd) {
116   if (high_bd) {
117     aom_free(CONVERT_TO_SHORTPTR(p));
118   } else {
119     aom_free(p);
120   }
121 }
122 
123 struct MallocBdDeleter {
MallocBdDeleter__anon772126b20111::MallocBdDeleter124   explicit MallocBdDeleter(const bool high_bd) : high_bd(high_bd) {}
operator ()__anon772126b20111::MallocBdDeleter125   void operator()(uint8_t *p) { free_bd(p, high_bd); }
126   const bool high_bd;
127 };
128 
129 class EdgeDetectBrightnessTest :
130     // Parameters are (brightness, width, height, high bit depth representation,
131     // bit depth).
132     public ::testing::TestWithParam<tuple<int, int, int, bool, int> > {
133  protected:
SetUp()134   void SetUp() override {
135     // Allocate a (width by height) array of luma values in orig_.
136     // padded_ will be filled by the pad() call, which adds a border around
137     // the orig_. The output_ array has enough space for the computation.
138     const int brightness = GET_PARAM(0);
139     const int width = GET_PARAM(1);
140     const int height = GET_PARAM(2);
141     const bool high_bd = GET_PARAM(3);
142 
143     // Create the padded image of uniform brightness.
144     std::unique_ptr<int[]> orig(new int[width * height]);
145     ASSERT_NE(orig, nullptr);
146     for (int i = 0; i < width * height; ++i) {
147       orig[i] = brightness;
148     }
149     input_ = pad_8tap_convolve(orig.get(), width, height, high_bd);
150     ASSERT_NE(input_, nullptr);
151     output_ = malloc_bd(width * height, high_bd);
152     ASSERT_NE(output_, nullptr);
153   }
154 
TearDown()155   void TearDown() override {
156     const int width = GET_PARAM(1);
157     const bool high_bd = GET_PARAM(3);
158     free_pad_8tap(input_, width, high_bd);
159     free_bd(output_, high_bd);
160   }
161 
162   // Skip the tests where brightness exceeds the bit-depth; we run into this
163   // issue because of gtest's limitation on valid combinations of test
164   // parameters. Also skip the tests where bit depth is greater than 8, but
165   // high bit depth representation is not set.
should_skip() const166   bool should_skip() const {
167     const int brightness = GET_PARAM(0);
168     const int bd = GET_PARAM(4);
169     if (brightness >= (1 << bd)) {
170       return true;
171     }
172     const bool high_bd = GET_PARAM(3);
173     if (bd > 8 && !high_bd) {
174       return true;
175     }
176     return false;
177   }
178 
179   uint8_t *input_;
180   uint8_t *output_;
181 };
182 
TEST_P(EdgeDetectBrightnessTest,BlurUniformBrightness)183 TEST_P(EdgeDetectBrightnessTest, BlurUniformBrightness) {
184   // Some combination of parameters are non-sensical, due to limitations
185   // of the testing framework. Ignore these.
186   if (should_skip()) {
187     return;
188   }
189 
190   // For varying levels of brightness, the algorithm should
191   // produce the same output.
192   const int brightness = GET_PARAM(0);
193   const int width = GET_PARAM(1);
194   const int height = GET_PARAM(2);
195   const bool high_bd = GET_PARAM(3);
196   const int bd = GET_PARAM(4);
197 
198   av1_gaussian_blur(input_, stride_8tap(width), width, height, output_, high_bd,
199                     bd);
200   for (int i = 0; i < width * height; ++i) {
201     ASSERT_EQ(brightness, get_pix(output_, i, high_bd));
202   }
203 }
204 
205 // No edges on a uniformly bright image.
TEST_P(EdgeDetectBrightnessTest,DetectUniformBrightness)206 TEST_P(EdgeDetectBrightnessTest, DetectUniformBrightness) {
207   if (should_skip()) {
208     return;
209   }
210   const int width = GET_PARAM(1);
211   const int height = GET_PARAM(2);
212   const bool high_bd = GET_PARAM(3);
213   const int bd = GET_PARAM(4);
214 
215   ASSERT_EQ(
216       0, av1_edge_exists(input_, stride_8tap(width), width, height, high_bd, bd)
217              .magnitude);
218 }
219 
220 #if CONFIG_AV1_HIGHBITDEPTH
221 INSTANTIATE_TEST_SUITE_P(ImageBrightnessTests, EdgeDetectBrightnessTest,
222                          ::testing::Combine(
223                              // Brightness
224                              ::testing::Values(0, 1, 2, 127, 128, 129, 254, 255,
225                                                256, 511, 512, 1023, 1024, 2048,
226                                                4095),
227                              // Width
228                              ::testing::Values(8, 16, 32),
229                              // Height
230                              ::testing::Values(4, 8, 12, 32),
231                              // High bit depth representation
232                              ::testing::Bool(),
233                              // Bit depth
234                              ::testing::Values(8, 10, 12)));
235 #else
236 INSTANTIATE_TEST_SUITE_P(ImageBrightnessTests, EdgeDetectBrightnessTest,
237                          ::testing::Combine(
238                              // Brightness
239                              ::testing::Values(0, 1, 2, 127, 128, 129, 254, 255,
240                                                256, 511, 512, 1023, 1024, 2048,
241                                                4095),
242                              // Width
243                              ::testing::Values(8, 16, 32),
244                              // Height
245                              ::testing::Values(4, 8, 12, 32),
246                              // High bit depth representation
247                              ::testing::Values(false),
248                              // Bit depth
249                              ::testing::Values(8)));
250 #endif
251 
252 class EdgeDetectImageTest :
253     // Parameters are (width, height, high bit depth representation, bit depth).
254     public ::testing::TestWithParam<tuple<int, int, bool, int> > {
255  protected:
256   // Skip the tests where bit depth is greater than 8, but high bit depth
257   // representation is not set (limitation of testing framework).
should_skip() const258   bool should_skip() const {
259     const bool high_bd = GET_PARAM(2);
260     const int bd = GET_PARAM(3);
261     return bd > 8 && !high_bd;
262   }
263 };
264 
265 // Generate images with black on one side and white on the other.
TEST_P(EdgeDetectImageTest,BlackWhite)266 TEST_P(EdgeDetectImageTest, BlackWhite) {
267   // Some combination of parameters are non-sensical, due to limitations
268   // of the testing framework. Ignore these.
269   if (should_skip()) {
270     return;
271   }
272 
273   const int width = GET_PARAM(0);
274   const int height = GET_PARAM(1);
275   const bool high_bd = GET_PARAM(2);
276   const int bd = GET_PARAM(3);
277 
278   const int white = (1 << bd) - 1;
279   std::unique_ptr<int[]> orig(new int[width * height]);
280   for (int j = 0; j < height; ++j) {
281     for (int i = 0; i < width; ++i) {
282       if (i < width / 2) {
283         orig[i + j * width] = 0;
284       } else {
285         orig[i + j * width] = white;
286       }
287     }
288   }
289 
290   std::unique_ptr<uint8_t[], Pad8TapConvolveDeleter> padded(
291       pad_8tap_convolve(orig.get(), width, height, high_bd),
292       Pad8TapConvolveDeleter(width, high_bd));
293   ASSERT_NE(padded, nullptr);
294   // Value should be between 556 and 560.
295   ASSERT_LE(556, av1_edge_exists(padded.get(), stride_8tap(width), width,
296                                  height, high_bd, bd)
297                      .magnitude);
298   ASSERT_GE(560, av1_edge_exists(padded.get(), stride_8tap(width), width,
299                                  height, high_bd, bd)
300                      .magnitude);
301 }
302 
303 // Hardcoded blur tests.
304 static const int luma[32] = { 241, 147, 7,   90,  184, 103, 28,  186,
305                               2,   248, 49,  242, 114, 146, 127, 22,
306                               121, 228, 167, 108, 158, 174, 41,  168,
307                               214, 99,  184, 109, 114, 247, 117, 119 };
308 static const uint8_t expected[] = { 161, 138, 119, 118, 123, 118, 113, 122,
309                                     143, 140, 134, 133, 134, 126, 116, 114,
310                                     147, 149, 145, 142, 143, 138, 126, 118,
311                                     164, 156, 148, 144, 148, 148, 138, 126 };
312 
hardcoded_blur_test_aux(const bool high_bd)313 static void hardcoded_blur_test_aux(const bool high_bd) {
314   const int w = 8;
315   const int h = 4;
316   for (int bd = 8; bd <= 12; bd += 2) {
317     // Skip the tests where bit depth is greater than 8, but high bit depth
318     // representation is not set.
319     if (bd > 8 && !high_bd) {
320       break;
321     }
322     std::unique_ptr<uint8_t[], MallocBdDeleter> output(
323         malloc_bd(w * h, high_bd), MallocBdDeleter(high_bd));
324     ASSERT_NE(output, nullptr);
325     std::unique_ptr<uint8_t[], Pad8TapConvolveDeleter> padded(
326         pad_8tap_convolve(luma, w, h, high_bd),
327         Pad8TapConvolveDeleter(w, high_bd));
328     ASSERT_NE(padded, nullptr);
329     av1_gaussian_blur(padded.get(), stride_8tap(w), w, h, output.get(), high_bd,
330                       bd);
331     for (int i = 0; i < w * h; ++i) {
332       ASSERT_EQ(expected[i], get_pix(output.get(), i, high_bd));
333     }
334 
335     // If we multiply the inputs by a constant factor, the output should not
336     // vary more than 0.5 * factor.
337     for (int c = 2; c < (1 << (bd - 8)); ++c) {
338       int scaled_luma[32];
339       for (int i = 0; i < 32; ++i) {
340         scaled_luma[i] = luma[i] * c;
341       }
342       padded.reset(pad_8tap_convolve(scaled_luma, w, h, high_bd));
343       ASSERT_NE(padded, nullptr);
344       av1_gaussian_blur(padded.get(), stride_8tap(w), w, h, output.get(),
345                         high_bd, bd);
346       for (int i = 0; i < w * h; ++i) {
347         ASSERT_GE(c / 2,
348                   abs(expected[i] * c - get_pix(output.get(), i, high_bd)));
349       }
350     }
351   }
352 }
353 
TEST(EdgeDetectImageTest,HardcodedBlurTest)354 TEST(EdgeDetectImageTest, HardcodedBlurTest) {
355   hardcoded_blur_test_aux(false);
356 #if CONFIG_AV1_HIGHBITDEPTH
357   hardcoded_blur_test_aux(true);
358 #endif
359 }
360 
TEST(EdgeDetectImageTest,SobelTest)361 TEST(EdgeDetectImageTest, SobelTest) {
362   // Randomly generated 3x3. Compute Sobel for middle value.
363   const uint8_t buf[9] = { 241, 147, 7, 90, 184, 103, 28, 186, 2 };
364   const int stride = 3;
365   bool high_bd = false;
366   sobel_xy result = av1_sobel(buf, stride, 1, 1, high_bd);
367   ASSERT_EQ(234, result.x);
368   ASSERT_EQ(140, result.y);
369 
370 #if CONFIG_AV1_HIGHBITDEPTH
371   // Verify it works for 8-bit values in a high bit-depth buffer.
372   const uint16_t buf8_16[9] = { 241, 147, 7, 90, 184, 103, 28, 186, 2 };
373   high_bd = true;
374   result = av1_sobel(CONVERT_TO_BYTEPTR(buf8_16), stride, 1, 1, high_bd);
375   ASSERT_EQ(234, result.x);
376   ASSERT_EQ(140, result.y);
377 
378   // Verify it works for high bit-depth values as well.
379   const uint16_t buf16[9] = { 241, 147, 7, 90, 184, 2003, 1028, 186, 2 };
380   result = av1_sobel(CONVERT_TO_BYTEPTR(buf16), stride, 1, 1, high_bd);
381   ASSERT_EQ(-2566, result.x);
382   ASSERT_EQ(-860, result.y);
383 #endif
384 }
385 
386 #if CONFIG_AV1_HIGHBITDEPTH
387 INSTANTIATE_TEST_SUITE_P(EdgeDetectImages, EdgeDetectImageTest,
388                          ::testing::Combine(
389                              // Width
390                              ::testing::Values(8, 16, 32),
391                              // Height
392                              ::testing::Values(4, 8, 12, 32),
393                              // High bit depth representation
394                              ::testing::Bool(),
395                              // Bit depth
396                              ::testing::Values(8, 10, 12)));
397 #else
398 INSTANTIATE_TEST_SUITE_P(EdgeDetectImages, EdgeDetectImageTest,
399                          ::testing::Combine(
400                              // Width
401                              ::testing::Values(8, 16, 32),
402                              // Height
403                              ::testing::Values(4, 8, 12, 32),
404                              // High bit depth representation
405                              ::testing::Values(false),
406                              // Bit depth
407                              ::testing::Values(8)));
408 #endif
409 }  // namespace
410