blob: 5fd68a52ae0720a2f8e403a666033c889a20965a [file] [log] [blame]
PENGBINffda3772018-02-26 17:36:37 +08001/*
James Zernb7c05bd2024-06-11 19:15:10 -07002 * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
PENGBINffda3772018-02-26 17:36:37 +08003 *
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 <cstdlib>
13#include <new>
sarahparkera543df52018-11-02 16:02:05 -070014#include <tuple>
PENGBINffda3772018-02-26 17:36:37 +080015
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/av1_rtcd.h"
Tom Finegan60e653d2018-05-22 11:34:58 -070018
PENGBINffda3772018-02-26 17:36:37 +080019#include "aom_ports/aom_timer.h"
20#include "av1/encoder/hash.h"
21#include "test/acm_random.h"
22#include "test/util.h"
23#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
24
Peng Binfdfdb192018-03-01 13:24:42 +080025namespace {
PENGBINffda3772018-02-26 17:36:37 +080026
Peng Bin8a204cd2018-04-08 13:07:35 +080027typedef uint32_t (*get_crc32c_value_func)(void *calculator, uint8_t *p,
Hien Ho5fa56f12019-04-26 13:29:26 -070028 size_t length);
PENGBINffda3772018-02-26 17:36:37 +080029
sarahparkera543df52018-11-02 16:02:05 -070030typedef std::tuple<get_crc32c_value_func, int> HashParam;
PENGBINffda3772018-02-26 17:36:37 +080031
Peng Bin8a204cd2018-04-08 13:07:35 +080032class AV1Crc32cHashTest : public ::testing::TestWithParam<HashParam> {
PENGBINffda3772018-02-26 17:36:37 +080033 public:
James Zernfaa2dcf2023-07-24 18:29:51 -070034 ~AV1Crc32cHashTest() override;
35 void SetUp() override;
PENGBINffda3772018-02-26 17:36:37 +080036
James Zernfaa2dcf2023-07-24 18:29:51 -070037 void TearDown() override;
PENGBINffda3772018-02-26 17:36:37 +080038
39 protected:
Peng Bin8a204cd2018-04-08 13:07:35 +080040 void RunCheckOutput(get_crc32c_value_func test_impl);
41 void RunSpeedTest(get_crc32c_value_func test_impl);
Peng Bin3fdd2552018-04-02 16:38:34 +080042
43 void RunZeroTest(get_crc32c_value_func test_impl);
44
PENGBINffda3772018-02-26 17:36:37 +080045 libaom_test::ACMRandom rnd_;
Peng Bin8a204cd2018-04-08 13:07:35 +080046 CRC32C calc_;
PENGBINffda3772018-02-26 17:36:37 +080047 uint8_t *buffer_;
48 int bsize_;
Hien Ho5fa56f12019-04-26 13:29:26 -070049 size_t length_;
PENGBINffda3772018-02-26 17:36:37 +080050};
51
James Zernf1fa1eb2023-07-25 15:34:13 -070052AV1Crc32cHashTest::~AV1Crc32cHashTest() = default;
PENGBINffda3772018-02-26 17:36:37 +080053
Peng Bin8a204cd2018-04-08 13:07:35 +080054void AV1Crc32cHashTest::SetUp() {
PENGBINffda3772018-02-26 17:36:37 +080055 rnd_.Reset(libaom_test::ACMRandom::DeterministicSeed());
Peng Bin8a204cd2018-04-08 13:07:35 +080056 av1_crc32c_calculator_init(&calc_);
57
58 bsize_ = GET_PARAM(1);
PENGBINffda3772018-02-26 17:36:37 +080059 length_ = bsize_ * bsize_ * sizeof(uint16_t);
Peng Binfdfdb192018-03-01 13:24:42 +080060 buffer_ = new uint8_t[length_];
James Zern9dea04e2022-04-28 13:18:36 -070061 ASSERT_NE(buffer_, nullptr);
Hien Ho5fa56f12019-04-26 13:29:26 -070062 for (size_t i = 0; i < length_; ++i) {
PENGBINffda3772018-02-26 17:36:37 +080063 buffer_[i] = rnd_.Rand8();
64 }
65}
66
Peng Bin8a204cd2018-04-08 13:07:35 +080067void AV1Crc32cHashTest::TearDown() { delete[] buffer_; }
PENGBINffda3772018-02-26 17:36:37 +080068
Peng Bin8a204cd2018-04-08 13:07:35 +080069void AV1Crc32cHashTest::RunCheckOutput(get_crc32c_value_func test_impl) {
70 get_crc32c_value_func ref_impl = av1_get_crc32c_value_c;
PENGBINffda3772018-02-26 17:36:37 +080071 // for the same buffer crc should be the same
72 uint32_t crc0 = test_impl(&calc_, buffer_, length_);
73 uint32_t crc1 = test_impl(&calc_, buffer_, length_);
74 uint32_t crc2 = ref_impl(&calc_, buffer_, length_);
75 ASSERT_EQ(crc0, crc1);
76 ASSERT_EQ(crc0, crc2); // should equal to software version
77 // modify buffer
78 buffer_[0] += 1;
79 uint32_t crc3 = test_impl(&calc_, buffer_, length_);
80 uint32_t crc4 = ref_impl(&calc_, buffer_, length_);
Peng Binfdfdb192018-03-01 13:24:42 +080081 ASSERT_NE(crc0, crc3); // crc shoud not equal to previous one
PENGBINffda3772018-02-26 17:36:37 +080082 ASSERT_EQ(crc3, crc4);
83}
84
Peng Bin8a204cd2018-04-08 13:07:35 +080085void AV1Crc32cHashTest::RunSpeedTest(get_crc32c_value_func test_impl) {
86 get_crc32c_value_func impls[] = { av1_get_crc32c_value_c, test_impl };
PENGBINffda3772018-02-26 17:36:37 +080087 const int repeat = 10000000 / (bsize_ + bsize_);
88
89 aom_usec_timer timer;
Peng Binfdfdb192018-03-01 13:24:42 +080090 double time[2];
PENGBINffda3772018-02-26 17:36:37 +080091 for (int i = 0; i < 2; ++i) {
92 aom_usec_timer_start(&timer);
93 for (int j = 0; j < repeat; ++j) {
94 impls[i](&calc_, buffer_, length_);
95 }
96 aom_usec_timer_mark(&timer);
97 time[i] = static_cast<double>(aom_usec_timer_elapsed(&timer));
98 }
99 printf("hash %3dx%-3d:%7.2f/%7.2fus", bsize_, bsize_, time[0], time[1]);
100 printf("(%3.2f)\n", time[0] / time[1]);
101}
102
Peng Bin3fdd2552018-04-02 16:38:34 +0800103void AV1Crc32cHashTest::RunZeroTest(get_crc32c_value_func test_impl) {
104 uint8_t buffer0[1024] = { 0 };
105 // for buffer with different size the crc should not be the same
106 const uint32_t crc0 = test_impl(&calc_, buffer0, 32);
107 const uint32_t crc1 = test_impl(&calc_, buffer0, 128);
108 const uint32_t crc2 = test_impl(&calc_, buffer0, 1024);
109 ASSERT_NE(crc0, crc1);
110 ASSERT_NE(crc0, crc2);
111 ASSERT_NE(crc1, crc2);
112}
113
Peng Bin8a204cd2018-04-08 13:07:35 +0800114TEST_P(AV1Crc32cHashTest, CheckOutput) { RunCheckOutput(GET_PARAM(0)); }
PENGBINffda3772018-02-26 17:36:37 +0800115
Peng Bin3fdd2552018-04-02 16:38:34 +0800116TEST_P(AV1Crc32cHashTest, CheckZero) { RunZeroTest(GET_PARAM(0)); }
117
Peng Bin8a204cd2018-04-08 13:07:35 +0800118TEST_P(AV1Crc32cHashTest, DISABLED_Speed) { RunSpeedTest(GET_PARAM(0)); }
PENGBINffda3772018-02-26 17:36:37 +0800119
120const int kValidBlockSize[] = { 64, 32, 8, 4 };
121
Cheng Chen96786fe2020-02-14 17:28:25 -0800122INSTANTIATE_TEST_SUITE_P(
Peng Bin8a204cd2018-04-08 13:07:35 +0800123 C, AV1Crc32cHashTest,
124 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_c),
PENGBINffda3772018-02-26 17:36:37 +0800125 ::testing::ValuesIn(kValidBlockSize)));
126
127#if HAVE_SSE4_2
Cheng Chen96786fe2020-02-14 17:28:25 -0800128INSTANTIATE_TEST_SUITE_P(
Peng Bin8a204cd2018-04-08 13:07:35 +0800129 SSE4_2, AV1Crc32cHashTest,
130 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_sse4_2),
PENGBINffda3772018-02-26 17:36:37 +0800131 ::testing::ValuesIn(kValidBlockSize)));
132#endif
133
Jonathan Wright6f223932022-05-31 23:07:50 +0100134#if HAVE_ARM_CRC32
135INSTANTIATE_TEST_SUITE_P(
136 ARM_CRC32, AV1Crc32cHashTest,
137 ::testing::Combine(::testing::Values(&av1_get_crc32c_value_arm_crc32),
138 ::testing::ValuesIn(kValidBlockSize)));
139#endif
140
Peng Binfdfdb192018-03-01 13:24:42 +0800141} // namespace