xref: /aosp_15_r20/external/libgav1/src/dsp/cdef_test.cc (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1 // Copyright 2021 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/dsp/cdef.h"
16 
17 #include <cstdint>
18 #include <cstring>
19 #include <ostream>
20 
21 #include "absl/strings/match.h"
22 #include "absl/time/clock.h"
23 #include "absl/time/time.h"
24 #include "gtest/gtest.h"
25 #include "src/dsp/dsp.h"
26 #include "src/utils/common.h"
27 #include "src/utils/constants.h"
28 #include "src/utils/cpu.h"
29 #include "src/utils/memory.h"
30 #include "tests/third_party/libvpx/acm_random.h"
31 #include "tests/third_party/libvpx/md5_helper.h"
32 #include "tests/utils.h"
33 
34 namespace libgav1 {
35 namespace dsp {
36 namespace {
37 
38 constexpr char kCdef[] = "Cdef";
39 constexpr char kCdefDirectionName[] = "Cdef Direction";
40 constexpr char kCdefFilterName[] = "Cdef Filtering";
41 constexpr int kTestBufferStride = 8;
42 constexpr int kTestBufferSize = 64;
43 constexpr int kSourceStride = kMaxSuperBlockSizeInPixels + 2 * 8;
44 constexpr int kSourceBufferSize =
45     (kMaxSuperBlockSizeInPixels + 2 * 3) * kSourceStride;
46 constexpr int kNumSpeedTests = 5000;
47 
GetDirectionDigest(const int bitdepth,const int num_runs)48 const char* GetDirectionDigest(const int bitdepth, const int num_runs) {
49   static const char* const kDigest[3][2] = {
50       {"de78c820a1fec7e81385aa0a615dbf8c", "7bfc543244f932a542691480dc4541b2"},
51       {"b54236de5d25e16c0f8678d9784cb85e", "559144cf183f3c69cb0e5d98cbf532ff"},
52       {"5532919a157c4f937da9e822bdb105f7", "dd9dfca6dfca83777d942e693c17627a"}};
53   const int bitdepth_index = (bitdepth - 8) / 2;
54   const int run_index = (num_runs == 1) ? 0 : 1;
55   return kDigest[bitdepth_index][run_index];
56 }
57 
58 // The 'int' parameter is unused but required to allow for instantiations of C,
59 // NEON, etc.
60 template <int bitdepth, typename Pixel>
61 class CdefDirectionTest : public testing::TestWithParam<int> {
62  public:
63   static_assert(bitdepth >= kBitdepth8 && bitdepth <= LIBGAV1_MAX_BITDEPTH, "");
64   CdefDirectionTest() = default;
65   CdefDirectionTest(const CdefDirectionTest&) = delete;
66   CdefDirectionTest& operator=(const CdefDirectionTest&) = delete;
67   ~CdefDirectionTest() override = default;
68 
69  protected:
SetUp()70   void SetUp() override {
71     test_utils::ResetDspTable(bitdepth);
72     CdefInit_C();
73 
74     const Dsp* const dsp = GetDspTable(bitdepth);
75     ASSERT_NE(dsp, nullptr);
76     base_cdef_direction_ = nullptr;
77     const testing::TestInfo* const test_info =
78         testing::UnitTest::GetInstance()->current_test_info();
79     const char* const test_case = test_info->test_suite_name();
80     if (absl::StartsWith(test_case, "C/")) {
81     } else if (absl::StartsWith(test_case, "SSE41/")) {
82       if ((GetCpuInfo() & kSSE4_1) == 0) GTEST_SKIP() << "No SSE4.1 support!";
83       CdefInit_SSE4_1();
84     } else if (absl::StartsWith(test_case, "AVX2/")) {
85       if ((GetCpuInfo() & kAVX2) == 0) GTEST_SKIP() << "No AVX2 support!";
86       CdefInit_AVX2();
87     } else if (absl::StartsWith(test_case, "NEON/")) {
88       CdefInit_NEON();
89     } else {
90       FAIL() << "Unrecognized architecture prefix in test case name: "
91              << test_case;
92     }
93     cur_cdef_direction_ = dsp->cdef_direction;
94   }
95 
96   void TestRandomValues(int num_runs);
97 
98   Pixel buffer_[kTestBufferSize];
99   int strength_;
100   int size_;
101 
102   CdefDirectionFunc base_cdef_direction_;
103   CdefDirectionFunc cur_cdef_direction_;
104 };
105 
106 template <int bitdepth, typename Pixel>
TestRandomValues(int num_runs)107 void CdefDirectionTest<bitdepth, Pixel>::TestRandomValues(int num_runs) {
108   if (cur_cdef_direction_ == nullptr) return;
109   libvpx_test::ACMRandom rnd(libvpx_test::ACMRandom::DeterministicSeed());
110   absl::Duration elapsed_time;
111   libvpx_test::MD5 actual_digest;
112   for (int num_tests = 0; num_tests < num_runs; ++num_tests) {
113     for (int level = 0; level < (1 << bitdepth); level += 1 + (bitdepth - 8)) {
114       for (int bits = 0; bits <= bitdepth; ++bits) {
115         for (auto& pixel : buffer_) {
116           pixel = Clip3((rnd.Rand16() & ((1 << bits) - 1)) + level, 0,
117                         (1 << bitdepth) - 1);
118         }
119         int output[2] = {};
120         const absl::Time start = absl::Now();
121         cur_cdef_direction_(buffer_, kTestBufferStride * sizeof(Pixel),
122                             reinterpret_cast<uint8_t*>(&output[0]), &output[1]);
123         elapsed_time += absl::Now() - start;
124         actual_digest.Add(reinterpret_cast<const uint8_t*>(output),
125                           sizeof(output));
126       }
127     }
128   }
129   test_utils::CheckMd5Digest(kCdef, kCdefDirectionName,
130                              GetDirectionDigest(bitdepth, num_runs),
131                              actual_digest.Get(), elapsed_time);
132 }
133 
134 using CdefDirectionTest8bpp = CdefDirectionTest<8, uint8_t>;
135 
TEST_P(CdefDirectionTest8bpp,Correctness)136 TEST_P(CdefDirectionTest8bpp, Correctness) { TestRandomValues(1); }
137 
TEST_P(CdefDirectionTest8bpp,DISABLED_Speed)138 TEST_P(CdefDirectionTest8bpp, DISABLED_Speed) {
139   TestRandomValues(kNumSpeedTests / 100);
140 }
141 
142 INSTANTIATE_TEST_SUITE_P(C, CdefDirectionTest8bpp, testing::Values(0));
143 
144 #if LIBGAV1_ENABLE_NEON
145 INSTANTIATE_TEST_SUITE_P(NEON, CdefDirectionTest8bpp, testing::Values(0));
146 #endif
147 
148 #if LIBGAV1_ENABLE_SSE4_1
149 INSTANTIATE_TEST_SUITE_P(SSE41, CdefDirectionTest8bpp, testing::Values(0));
150 #endif
151 
152 #if LIBGAV1_ENABLE_AVX2
153 INSTANTIATE_TEST_SUITE_P(AVX2, CdefDirectionTest8bpp, testing::Values(0));
154 #endif  // LIBGAV1_ENABLE_AVX2
155 
156 #if LIBGAV1_MAX_BITDEPTH >= 10
157 using CdefDirectionTest10bpp = CdefDirectionTest<10, uint16_t>;
158 
TEST_P(CdefDirectionTest10bpp,Correctness)159 TEST_P(CdefDirectionTest10bpp, Correctness) { TestRandomValues(1); }
160 
TEST_P(CdefDirectionTest10bpp,DISABLED_Speed)161 TEST_P(CdefDirectionTest10bpp, DISABLED_Speed) {
162   TestRandomValues(kNumSpeedTests / 100);
163 }
164 
165 INSTANTIATE_TEST_SUITE_P(C, CdefDirectionTest10bpp, testing::Values(0));
166 
167 #if LIBGAV1_ENABLE_NEON
168 INSTANTIATE_TEST_SUITE_P(NEON, CdefDirectionTest10bpp, testing::Values(0));
169 #endif
170 #endif  // LIBGAV1_MAX_BITDEPTH >= 10
171 
172 #if LIBGAV1_MAX_BITDEPTH == 12
173 using CdefDirectionTest12bpp = CdefDirectionTest<12, uint16_t>;
174 
TEST_P(CdefDirectionTest12bpp,Correctness)175 TEST_P(CdefDirectionTest12bpp, Correctness) { TestRandomValues(1); }
176 
TEST_P(CdefDirectionTest12bpp,DISABLED_Speed)177 TEST_P(CdefDirectionTest12bpp, DISABLED_Speed) {
178   TestRandomValues(kNumSpeedTests / 100);
179 }
180 
181 INSTANTIATE_TEST_SUITE_P(C, CdefDirectionTest12bpp, testing::Values(0));
182 #endif  // LIBGAV1_MAX_BITDEPTH == 12
183 
GetDigest8bpp(int id)184 const char* GetDigest8bpp(int id) {
185   static const char* const kDigest[] = {
186       "b6fe1a1f5bbb23e35197160ce57d90bd", "8aed39871b19184f1d381b145779bc33",
187       "82653dd66072e8ebd967083a0413ab03", "421c048396bc66ffaa6aafa016c7bc54",
188       "1f70ba51091e8c6034c3f0974af241c3", "8f700997452a24091136ca58890a5be4",
189       "9e3dea21ee4246172121f0420eccd899", "0848bdeffa74145758ef47992e1035c4",
190       "0bb55818de986e9d988b0c1cc6883887", "9b558a7eefc934f90cd09ca26b998bfd",
191       "3a38670f8c5f0c61cc47c9c79da728d2", "ed18fe91180e78008ccb98e9019bed69",
192       "2aa4bbcb6fb088ad42bde76be014dff0", "88f746f0d6c079ab8e9ecc7ff67524c7",
193       "7cffa948f5ddbccc7c6b07d15ca9eb69", "5e22c1c89735965dda935d1249129548",
194       "e765133d133b94e1578c8c5616248a96", "da95d47cad74eb4a075893ca98e658ab",
195   };
196   return kDigest[id];
197 }
198 
199 #if LIBGAV1_MAX_BITDEPTH >= 10
GetDigest10bpp(int id)200 const char* GetDigest10bpp(int id) {
201   static const char* const kDigest[] = {
202       "0a9630b39974850998db653b07e09ab4", "97a924661d931b23ee57893da617ae70",
203       "0d79516b9a491ce5112eb00bbae5eb80", "d5801fd96029a7509cf66dde61e8e2d8",
204       "5bf5c0ea5a85e9b6c1e6991619c34ebc", "e2f1c08a8b3cd93b3a85511493a0ee31",
205       "45c047d2be5e2dcf6094937780a3f88a", "346caf437c1ad85862de72a622e29845",
206       "0e9cb69d24d9badbe956da779d912b05", "81803dcb00971237b3fe6372564a842f",
207       "17681ad2ed4a2456d70760852af6c6fd", "5312f8049a08a5f9b1708fda936f7a55",
208       "3f0f522f3a33e4ff2a97bdc1e614c5c4", "3818a50be7fe16aa0c636a7392d1eceb",
209       "c6849b8cd77a076dc7e3c26e8cd55b9e", "223c0dd685bbc74aec1d088356708433",
210       "90992957cb8103222aa2fb43c6cd2fc4", "a4ba6edcefe4130851c4c2607b147f95",
211   };
212   return kDigest[id];
213 }
214 #endif  // LIBGAV1_MAX_BITDEPTH >= 10
215 
216 #if LIBGAV1_MAX_BITDEPTH == 12
GetDigest12bpp(int id)217 const char* GetDigest12bpp(int id) {
218   static const char* const kDigest[] = {
219       "a32569989c42fd4254979f70c1c65f5a", "dc389048217633e2dd64126376be7d25",
220       "3b0e8dae294895330f349863b1773c39", "9741fe8d27d109cb99b7a9cdc030f52a",
221       "ab70f3729b52287c6432ba7624280a68", "c1e5cf39cbc8030b82e09633c6c67d42",
222       "d5120a196164ff5a0ad7aa8c02e9b064", "1133759f3aee3a362a0ab668f6faf843",
223       "feb0ab7f515665f79fce213e8cd2fb10", "e86ea55c2d6d5cc69716535bd455c99f",
224       "e463da1b9d089b6ee82c041794257fd7", "27800e4af0cceeaf0a95c96275a7befe",
225       "f42e426481db00582b327eb2971bca96", "6127ff289833dde0270000d8240f36b7",
226       "cc5dbaf70e2fef7729a8e2ea9937fbcf", "51850b4e3e2a3919e110376fcb6318d3",
227       "d5ac7ac25eb1b5aee293b2a2ec9de775", "64ecc00b2e24a2f07df833fb50ce09c3",
228   };
229   return kDigest[id];
230 }
231 #endif  // LIBGAV1_MAX_BITDEPTH == 12
232 
233 struct CdefTestParam {
CdefTestParamlibgav1::dsp::__anon31afbc4e0111::CdefTestParam234   CdefTestParam(int subsampling_x, int subsampling_y, int rows4x4,
235                 int columns4x4)
236       : subsampling_x(subsampling_x),
237         subsampling_y(subsampling_y),
238         rows4x4(rows4x4),
239         columns4x4(columns4x4) {}
240   int subsampling_x;
241   int subsampling_y;
242   int rows4x4;
243   int columns4x4;
244 };
245 
operator <<(std::ostream & os,const CdefTestParam & param)246 std::ostream& operator<<(std::ostream& os, const CdefTestParam& param) {
247   return os << "subsampling(x/y): " << param.subsampling_x << "/"
248             << param.subsampling_y << ", (rows,columns)4x4: " << param.rows4x4
249             << ", " << param.columns4x4;
250 }
251 
252 // TODO(b/154245961): rework the parameters for this test to match
253 // CdefFilteringFuncs. It should cover 4x4, 8x4, 8x8 blocks and
254 // primary/secondary strength combinations for both Y and UV.
255 template <int bitdepth, typename Pixel>
256 class CdefFilteringTest : public testing::TestWithParam<CdefTestParam> {
257  public:
258   static_assert(bitdepth >= kBitdepth8 && bitdepth <= LIBGAV1_MAX_BITDEPTH, "");
259   CdefFilteringTest() = default;
260   CdefFilteringTest(const CdefFilteringTest&) = delete;
261   CdefFilteringTest& operator=(const CdefFilteringTest&) = delete;
262   ~CdefFilteringTest() override = default;
263 
264  protected:
SetUp()265   void SetUp() override {
266     test_utils::ResetDspTable(bitdepth);
267     CdefInit_C();
268 
269     const Dsp* const dsp = GetDspTable(bitdepth);
270     ASSERT_NE(dsp, nullptr);
271     const testing::TestInfo* const test_info =
272         testing::UnitTest::GetInstance()->current_test_info();
273     const char* const test_case = test_info->test_suite_name();
274     if (absl::StartsWith(test_case, "C/")) {
275     } else if (absl::StartsWith(test_case, "NEON/")) {
276       CdefInit_NEON();
277     } else if (absl::StartsWith(test_case, "SSE41/")) {
278       if ((GetCpuInfo() & kSSE4_1) == 0) GTEST_SKIP() << "No SSE4.1 support!";
279       CdefInit_SSE4_1();
280     } else if (absl::StartsWith(test_case, "AVX2/")) {
281       if ((GetCpuInfo() & kAVX2) == 0) GTEST_SKIP() << "No AVX2 support!";
282       CdefInit_AVX2();
283     } else {
284       FAIL() << "Unrecognized architecture prefix in test case name: "
285              << test_case;
286     }
287     memcpy(cur_cdef_filter_, dsp->cdef_filters, sizeof(cur_cdef_filter_));
288   }
289 
290   void TestRandomValues(int num_runs);
291 
292   uint16_t source_[kSourceBufferSize];
293   Pixel dest_[kMaxPlanes][kTestBufferSize];
294   int primary_strength_;
295   int secondary_strength_;
296   int damping_;
297   int direction_;
298   CdefTestParam param_ = GetParam();
299 
300   CdefFilteringFuncs cur_cdef_filter_;
301 };
302 
303 template <int bitdepth, typename Pixel>
TestRandomValues(int num_runs)304 void CdefFilteringTest<bitdepth, Pixel>::TestRandomValues(int num_runs) {
305   const int id = static_cast<int>(param_.rows4x4 < 4) * 3 +
306                  (param_.subsampling_x + param_.subsampling_y) * 6;
307   absl::Duration elapsed_time[kMaxPlanes];
308   for (int num_tests = 0; num_tests < num_runs; ++num_tests) {
309     for (int plane = kPlaneY; plane < kMaxPlanes; ++plane) {
310       const int subsampling_x = (plane == kPlaneY) ? 0 : param_.subsampling_x;
311       const int subsampling_y = (plane == kPlaneY) ? 0 : param_.subsampling_y;
312       const int block_width = 8 >> subsampling_x;
313       const int block_height = 8 >> subsampling_y;
314       libvpx_test::ACMRandom rnd(libvpx_test::ACMRandom::DeterministicSeed() +
315                                  id + plane);
316       const int offset = 2 * kSourceStride + 2;
317       // Fill boundaries with a large value such that cdef does not take them
318       // into calculation.
319       const int plane_width = MultiplyBy4(param_.columns4x4) >> subsampling_x;
320       const int plane_height = MultiplyBy4(param_.rows4x4) >> subsampling_y;
321       for (int y = 0; y < plane_height; ++y) {
322         for (int x = 0; x < plane_width; ++x) {
323           source_[y * kSourceStride + x + offset] =
324               rnd.Rand16() & ((1 << bitdepth) - 1);
325         }
326       }
327       for (int y = 0; y < 2; ++y) {
328         Memset(&source_[y * kSourceStride], kCdefLargeValue, kSourceStride);
329         Memset(&source_[(y + plane_height + 2) * kSourceStride],
330                kCdefLargeValue, kSourceStride);
331       }
332       for (int y = 0; y < plane_height; ++y) {
333         Memset(&source_[y * kSourceStride + offset - 2], kCdefLargeValue, 2);
334         Memset(&source_[y * kSourceStride + offset + plane_width],
335                kCdefLargeValue, 2);
336       }
337       do {
338         int strength = rnd.Rand16() & 15;
339         if (strength == 3) ++strength;
340         primary_strength_ = strength << (bitdepth - 8);
341       } while (primary_strength_ == 0);
342       do {
343         int strength = rnd.Rand16() & 3;
344         if (strength == 3) ++strength;
345         secondary_strength_ = strength << (bitdepth - 8);
346       } while (secondary_strength_ == 0);
347       damping_ = (rnd.Rand16() & 3) + 3;
348       direction_ = (rnd.Rand16() & 7);
349 
350       memset(dest_[plane], 0, sizeof(dest_[plane]));
351       const absl::Time start = absl::Now();
352       const int width_index = block_width >> 3;
353       if (cur_cdef_filter_[width_index][0] == nullptr) return;
354       cur_cdef_filter_[width_index][0](
355           source_ + offset, kSourceStride, block_height, primary_strength_,
356           secondary_strength_, damping_, direction_, dest_[plane],
357           kTestBufferStride * sizeof(dest_[0][0]));
358       elapsed_time[plane] += absl::Now() - start;
359     }
360   }
361 
362   for (int plane = kPlaneY; plane < kMaxPlanes; ++plane) {
363     const char* expected_digest = nullptr;
364     switch (bitdepth) {
365       case 8:
366         expected_digest = GetDigest8bpp(id + plane);
367         break;
368 #if LIBGAV1_MAX_BITDEPTH >= 10
369       case 10:
370         expected_digest = GetDigest10bpp(id + plane);
371         break;
372 #endif
373 #if LIBGAV1_MAX_BITDEPTH == 12
374       case 12:
375         expected_digest = GetDigest12bpp(id + plane);
376         break;
377 #endif
378     }
379     ASSERT_NE(expected_digest, nullptr);
380     test_utils::CheckMd5Digest(kCdef, kCdefFilterName, expected_digest,
381                                reinterpret_cast<uint8_t*>(dest_[plane]),
382                                sizeof(dest_[plane]), elapsed_time[plane]);
383   }
384 }
385 
386 // Do not test single blocks with any subsampling. 2xH and Wx2 blocks are not
387 // supported.
388 const CdefTestParam cdef_test_param[] = {
389     CdefTestParam(0, 0, 4, 4), CdefTestParam(0, 0, 2, 2),
390     CdefTestParam(1, 0, 4, 4), CdefTestParam(1, 0, 2, 2),
391     CdefTestParam(1, 1, 4, 4), CdefTestParam(1, 1, 2, 2),
392 };
393 
394 using CdefFilteringTest8bpp = CdefFilteringTest<8, uint8_t>;
395 
TEST_P(CdefFilteringTest8bpp,Correctness)396 TEST_P(CdefFilteringTest8bpp, Correctness) { TestRandomValues(1); }
397 
TEST_P(CdefFilteringTest8bpp,DISABLED_Speed)398 TEST_P(CdefFilteringTest8bpp, DISABLED_Speed) {
399   TestRandomValues(kNumSpeedTests);
400 }
401 
402 INSTANTIATE_TEST_SUITE_P(C, CdefFilteringTest8bpp,
403                          testing::ValuesIn(cdef_test_param));
404 
405 #if LIBGAV1_ENABLE_NEON
406 INSTANTIATE_TEST_SUITE_P(NEON, CdefFilteringTest8bpp,
407                          testing::ValuesIn(cdef_test_param));
408 #endif
409 
410 #if LIBGAV1_ENABLE_SSE4_1
411 INSTANTIATE_TEST_SUITE_P(SSE41, CdefFilteringTest8bpp,
412                          testing::ValuesIn(cdef_test_param));
413 #endif
414 
415 #if LIBGAV1_ENABLE_AVX2
416 INSTANTIATE_TEST_SUITE_P(AVX2, CdefFilteringTest8bpp,
417                          testing::ValuesIn(cdef_test_param));
418 #endif  // LIBGAV1_ENABLE_AVX2
419 
420 #if LIBGAV1_MAX_BITDEPTH >= 10
421 using CdefFilteringTest10bpp = CdefFilteringTest<10, uint16_t>;
422 
TEST_P(CdefFilteringTest10bpp,Correctness)423 TEST_P(CdefFilteringTest10bpp, Correctness) { TestRandomValues(1); }
424 
TEST_P(CdefFilteringTest10bpp,DISABLED_Speed)425 TEST_P(CdefFilteringTest10bpp, DISABLED_Speed) {
426   TestRandomValues(kNumSpeedTests);
427 }
428 
429 INSTANTIATE_TEST_SUITE_P(C, CdefFilteringTest10bpp,
430                          testing::ValuesIn(cdef_test_param));
431 
432 #if LIBGAV1_ENABLE_NEON
433 INSTANTIATE_TEST_SUITE_P(NEON, CdefFilteringTest10bpp,
434                          testing::ValuesIn(cdef_test_param));
435 #endif
436 #endif  // LIBGAV1_MAX_BITDEPTH >= 10
437 
438 #if LIBGAV1_MAX_BITDEPTH == 12
439 using CdefFilteringTest12bpp = CdefFilteringTest<12, uint16_t>;
440 
TEST_P(CdefFilteringTest12bpp,Correctness)441 TEST_P(CdefFilteringTest12bpp, Correctness) { TestRandomValues(1); }
442 
TEST_P(CdefFilteringTest12bpp,DISABLED_Speed)443 TEST_P(CdefFilteringTest12bpp, DISABLED_Speed) {
444   TestRandomValues(kNumSpeedTests);
445 }
446 
447 INSTANTIATE_TEST_SUITE_P(C, CdefFilteringTest12bpp,
448                          testing::ValuesIn(cdef_test_param));
449 #endif  // LIBGAV1_MAX_BITDEPTH == 12
450 
451 }  // namespace
452 }  // namespace dsp
453 }  // namespace libgav1
454