avg_test.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Copyright (c) 2012 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. #include <limits.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "third_party/googletest/src/include/gtest/gtest.h"
  14. #include "./vp9_rtcd.h"
  15. #include "./vpx_config.h"
  16. #include "./vpx_dsp_rtcd.h"
  17. #include "test/acm_random.h"
  18. #include "test/clear_system_state.h"
  19. #include "test/register_state_check.h"
  20. #include "test/util.h"
  21. #include "vpx_mem/vpx_mem.h"
  22. using libvpx_test::ACMRandom;
  23. namespace {
  24. class AverageTestBase : public ::testing::Test {
  25. public:
  26. AverageTestBase(int width, int height) : width_(width), height_(height) {}
  27. static void SetUpTestCase() {
  28. source_data_ = reinterpret_cast<uint8_t *>(
  29. vpx_memalign(kDataAlignment, kDataBlockSize));
  30. }
  31. static void TearDownTestCase() {
  32. vpx_free(source_data_);
  33. source_data_ = NULL;
  34. }
  35. virtual void TearDown() { libvpx_test::ClearSystemState(); }
  36. protected:
  37. // Handle blocks up to 4 blocks 64x64 with stride up to 128
  38. static const int kDataAlignment = 16;
  39. static const int kDataBlockSize = 64 * 128;
  40. virtual void SetUp() {
  41. source_stride_ = (width_ + 31) & ~31;
  42. rnd_.Reset(ACMRandom::DeterministicSeed());
  43. }
  44. // Sum Pixels
  45. static unsigned int ReferenceAverage8x8(const uint8_t *source, int pitch) {
  46. unsigned int average = 0;
  47. for (int h = 0; h < 8; ++h) {
  48. for (int w = 0; w < 8; ++w) average += source[h * pitch + w];
  49. }
  50. return ((average + 32) >> 6);
  51. }
  52. static unsigned int ReferenceAverage4x4(const uint8_t *source, int pitch) {
  53. unsigned int average = 0;
  54. for (int h = 0; h < 4; ++h) {
  55. for (int w = 0; w < 4; ++w) average += source[h * pitch + w];
  56. }
  57. return ((average + 8) >> 4);
  58. }
  59. void FillConstant(uint8_t fill_constant) {
  60. for (int i = 0; i < width_ * height_; ++i) {
  61. source_data_[i] = fill_constant;
  62. }
  63. }
  64. void FillRandom() {
  65. for (int i = 0; i < width_ * height_; ++i) {
  66. source_data_[i] = rnd_.Rand8();
  67. }
  68. }
  69. int width_, height_;
  70. static uint8_t *source_data_;
  71. int source_stride_;
  72. ACMRandom rnd_;
  73. };
  74. typedef unsigned int (*AverageFunction)(const uint8_t *s, int pitch);
  75. typedef std::tr1::tuple<int, int, int, int, AverageFunction> AvgFunc;
  76. class AverageTest : public AverageTestBase,
  77. public ::testing::WithParamInterface<AvgFunc> {
  78. public:
  79. AverageTest() : AverageTestBase(GET_PARAM(0), GET_PARAM(1)) {}
  80. protected:
  81. void CheckAverages() {
  82. const int block_size = GET_PARAM(3);
  83. unsigned int expected = 0;
  84. if (block_size == 8) {
  85. expected =
  86. ReferenceAverage8x8(source_data_ + GET_PARAM(2), source_stride_);
  87. } else if (block_size == 4) {
  88. expected =
  89. ReferenceAverage4x4(source_data_ + GET_PARAM(2), source_stride_);
  90. }
  91. ASM_REGISTER_STATE_CHECK(
  92. GET_PARAM(4)(source_data_ + GET_PARAM(2), source_stride_));
  93. unsigned int actual =
  94. GET_PARAM(4)(source_data_ + GET_PARAM(2), source_stride_);
  95. EXPECT_EQ(expected, actual);
  96. }
  97. };
  98. typedef void (*IntProRowFunc)(int16_t hbuf[16], uint8_t const *ref,
  99. const int ref_stride, const int height);
  100. typedef std::tr1::tuple<int, IntProRowFunc, IntProRowFunc> IntProRowParam;
  101. class IntProRowTest : public AverageTestBase,
  102. public ::testing::WithParamInterface<IntProRowParam> {
  103. public:
  104. IntProRowTest()
  105. : AverageTestBase(16, GET_PARAM(0)), hbuf_asm_(NULL), hbuf_c_(NULL) {
  106. asm_func_ = GET_PARAM(1);
  107. c_func_ = GET_PARAM(2);
  108. }
  109. protected:
  110. virtual void SetUp() {
  111. hbuf_asm_ = reinterpret_cast<int16_t *>(
  112. vpx_memalign(kDataAlignment, sizeof(*hbuf_asm_) * 16));
  113. hbuf_c_ = reinterpret_cast<int16_t *>(
  114. vpx_memalign(kDataAlignment, sizeof(*hbuf_c_) * 16));
  115. }
  116. virtual void TearDown() {
  117. vpx_free(hbuf_c_);
  118. hbuf_c_ = NULL;
  119. vpx_free(hbuf_asm_);
  120. hbuf_asm_ = NULL;
  121. }
  122. void RunComparison() {
  123. ASM_REGISTER_STATE_CHECK(c_func_(hbuf_c_, source_data_, 0, height_));
  124. ASM_REGISTER_STATE_CHECK(asm_func_(hbuf_asm_, source_data_, 0, height_));
  125. EXPECT_EQ(0, memcmp(hbuf_c_, hbuf_asm_, sizeof(*hbuf_c_) * 16))
  126. << "Output mismatch";
  127. }
  128. private:
  129. IntProRowFunc asm_func_;
  130. IntProRowFunc c_func_;
  131. int16_t *hbuf_asm_;
  132. int16_t *hbuf_c_;
  133. };
  134. typedef int16_t (*IntProColFunc)(uint8_t const *ref, const int width);
  135. typedef std::tr1::tuple<int, IntProColFunc, IntProColFunc> IntProColParam;
  136. class IntProColTest : public AverageTestBase,
  137. public ::testing::WithParamInterface<IntProColParam> {
  138. public:
  139. IntProColTest() : AverageTestBase(GET_PARAM(0), 1), sum_asm_(0), sum_c_(0) {
  140. asm_func_ = GET_PARAM(1);
  141. c_func_ = GET_PARAM(2);
  142. }
  143. protected:
  144. void RunComparison() {
  145. ASM_REGISTER_STATE_CHECK(sum_c_ = c_func_(source_data_, width_));
  146. ASM_REGISTER_STATE_CHECK(sum_asm_ = asm_func_(source_data_, width_));
  147. EXPECT_EQ(sum_c_, sum_asm_) << "Output mismatch";
  148. }
  149. private:
  150. IntProColFunc asm_func_;
  151. IntProColFunc c_func_;
  152. int16_t sum_asm_;
  153. int16_t sum_c_;
  154. };
  155. typedef int (*SatdFunc)(const tran_low_t *coeffs, int length);
  156. typedef std::tr1::tuple<int, SatdFunc> SatdTestParam;
  157. class SatdTest : public ::testing::Test,
  158. public ::testing::WithParamInterface<SatdTestParam> {
  159. protected:
  160. virtual void SetUp() {
  161. satd_size_ = GET_PARAM(0);
  162. satd_func_ = GET_PARAM(1);
  163. rnd_.Reset(ACMRandom::DeterministicSeed());
  164. src_ = reinterpret_cast<tran_low_t *>(
  165. vpx_memalign(16, sizeof(*src_) * satd_size_));
  166. ASSERT_TRUE(src_ != NULL);
  167. }
  168. virtual void TearDown() {
  169. libvpx_test::ClearSystemState();
  170. vpx_free(src_);
  171. }
  172. void FillConstant(const tran_low_t val) {
  173. for (int i = 0; i < satd_size_; ++i) src_[i] = val;
  174. }
  175. void FillRandom() {
  176. for (int i = 0; i < satd_size_; ++i) {
  177. const int16_t tmp = rnd_.Rand16();
  178. src_[i] = (tran_low_t)tmp;
  179. }
  180. }
  181. void Check(const int expected) {
  182. int total;
  183. ASM_REGISTER_STATE_CHECK(total = satd_func_(src_, satd_size_));
  184. EXPECT_EQ(expected, total);
  185. }
  186. int satd_size_;
  187. private:
  188. tran_low_t *src_;
  189. SatdFunc satd_func_;
  190. ACMRandom rnd_;
  191. };
  192. typedef int64_t (*BlockErrorFunc)(const tran_low_t *coeff,
  193. const tran_low_t *dqcoeff, int block_size);
  194. typedef std::tr1::tuple<int, BlockErrorFunc> BlockErrorTestFPParam;
  195. class BlockErrorTestFP
  196. : public ::testing::Test,
  197. public ::testing::WithParamInterface<BlockErrorTestFPParam> {
  198. protected:
  199. virtual void SetUp() {
  200. txfm_size_ = GET_PARAM(0);
  201. block_error_func_ = GET_PARAM(1);
  202. rnd_.Reset(ACMRandom::DeterministicSeed());
  203. coeff_ = reinterpret_cast<tran_low_t *>(
  204. vpx_memalign(16, sizeof(*coeff_) * txfm_size_));
  205. dqcoeff_ = reinterpret_cast<tran_low_t *>(
  206. vpx_memalign(16, sizeof(*dqcoeff_) * txfm_size_));
  207. ASSERT_TRUE(coeff_ != NULL);
  208. ASSERT_TRUE(dqcoeff_ != NULL);
  209. }
  210. virtual void TearDown() {
  211. libvpx_test::ClearSystemState();
  212. vpx_free(coeff_);
  213. vpx_free(dqcoeff_);
  214. }
  215. void FillConstant(const tran_low_t coeff_val, const tran_low_t dqcoeff_val) {
  216. for (int i = 0; i < txfm_size_; ++i) coeff_[i] = coeff_val;
  217. for (int i = 0; i < txfm_size_; ++i) dqcoeff_[i] = dqcoeff_val;
  218. }
  219. void FillRandom() {
  220. // Just two fixed seeds
  221. rnd_.Reset(0xb0b9);
  222. for (int i = 0; i < txfm_size_; ++i) coeff_[i] = rnd_.Rand16() >> 1;
  223. rnd_.Reset(0xb0c8);
  224. for (int i = 0; i < txfm_size_; ++i) dqcoeff_[i] = rnd_.Rand16() >> 1;
  225. }
  226. void Check(const int64_t expected) {
  227. int64_t total;
  228. ASM_REGISTER_STATE_CHECK(
  229. total = block_error_func_(coeff_, dqcoeff_, txfm_size_));
  230. EXPECT_EQ(expected, total);
  231. }
  232. int txfm_size_;
  233. private:
  234. tran_low_t *coeff_;
  235. tran_low_t *dqcoeff_;
  236. BlockErrorFunc block_error_func_;
  237. ACMRandom rnd_;
  238. };
  239. uint8_t *AverageTestBase::source_data_ = NULL;
  240. TEST_P(AverageTest, MinValue) {
  241. FillConstant(0);
  242. CheckAverages();
  243. }
  244. TEST_P(AverageTest, MaxValue) {
  245. FillConstant(255);
  246. CheckAverages();
  247. }
  248. TEST_P(AverageTest, Random) {
  249. // The reference frame, but not the source frame, may be unaligned for
  250. // certain types of searches.
  251. for (int i = 0; i < 1000; i++) {
  252. FillRandom();
  253. CheckAverages();
  254. }
  255. }
  256. TEST_P(IntProRowTest, MinValue) {
  257. FillConstant(0);
  258. RunComparison();
  259. }
  260. TEST_P(IntProRowTest, MaxValue) {
  261. FillConstant(255);
  262. RunComparison();
  263. }
  264. TEST_P(IntProRowTest, Random) {
  265. FillRandom();
  266. RunComparison();
  267. }
  268. TEST_P(IntProColTest, MinValue) {
  269. FillConstant(0);
  270. RunComparison();
  271. }
  272. TEST_P(IntProColTest, MaxValue) {
  273. FillConstant(255);
  274. RunComparison();
  275. }
  276. TEST_P(IntProColTest, Random) {
  277. FillRandom();
  278. RunComparison();
  279. }
  280. TEST_P(SatdTest, MinValue) {
  281. const int kMin = -32640;
  282. const int expected = -kMin * satd_size_;
  283. FillConstant(kMin);
  284. Check(expected);
  285. }
  286. TEST_P(SatdTest, MaxValue) {
  287. const int kMax = 32640;
  288. const int expected = kMax * satd_size_;
  289. FillConstant(kMax);
  290. Check(expected);
  291. }
  292. TEST_P(SatdTest, Random) {
  293. int expected;
  294. switch (satd_size_) {
  295. case 16: expected = 205298; break;
  296. case 64: expected = 1113950; break;
  297. case 256: expected = 4268415; break;
  298. case 1024: expected = 16954082; break;
  299. default:
  300. FAIL() << "Invalid satd size (" << satd_size_
  301. << ") valid: 16/64/256/1024";
  302. }
  303. FillRandom();
  304. Check(expected);
  305. }
  306. TEST_P(BlockErrorTestFP, MinValue) {
  307. const int64_t kMin = -32640;
  308. const int64_t expected = kMin * kMin * txfm_size_;
  309. FillConstant(kMin, 0);
  310. Check(expected);
  311. }
  312. TEST_P(BlockErrorTestFP, MaxValue) {
  313. const int64_t kMax = 32640;
  314. const int64_t expected = kMax * kMax * txfm_size_;
  315. FillConstant(kMax, 0);
  316. Check(expected);
  317. }
  318. TEST_P(BlockErrorTestFP, Random) {
  319. int64_t expected;
  320. switch (txfm_size_) {
  321. case 16: expected = 2051681432; break;
  322. case 64: expected = 11075114379; break;
  323. case 256: expected = 44386271116; break;
  324. case 1024: expected = 184774996089; break;
  325. default:
  326. FAIL() << "Invalid satd size (" << txfm_size_
  327. << ") valid: 16/64/256/1024";
  328. }
  329. FillRandom();
  330. Check(expected);
  331. }
  332. using std::tr1::make_tuple;
  333. INSTANTIATE_TEST_CASE_P(
  334. C, AverageTest,
  335. ::testing::Values(make_tuple(16, 16, 1, 8, &vpx_avg_8x8_c),
  336. make_tuple(16, 16, 1, 4, &vpx_avg_4x4_c)));
  337. INSTANTIATE_TEST_CASE_P(C, SatdTest,
  338. ::testing::Values(make_tuple(16, &vpx_satd_c),
  339. make_tuple(64, &vpx_satd_c),
  340. make_tuple(256, &vpx_satd_c),
  341. make_tuple(1024, &vpx_satd_c)));
  342. INSTANTIATE_TEST_CASE_P(
  343. C, BlockErrorTestFP,
  344. ::testing::Values(make_tuple(16, &vp9_block_error_fp_c),
  345. make_tuple(64, &vp9_block_error_fp_c),
  346. make_tuple(256, &vp9_block_error_fp_c),
  347. make_tuple(1024, &vp9_block_error_fp_c)));
  348. #if HAVE_SSE2
  349. INSTANTIATE_TEST_CASE_P(
  350. SSE2, AverageTest,
  351. ::testing::Values(make_tuple(16, 16, 0, 8, &vpx_avg_8x8_sse2),
  352. make_tuple(16, 16, 5, 8, &vpx_avg_8x8_sse2),
  353. make_tuple(32, 32, 15, 8, &vpx_avg_8x8_sse2),
  354. make_tuple(16, 16, 0, 4, &vpx_avg_4x4_sse2),
  355. make_tuple(16, 16, 5, 4, &vpx_avg_4x4_sse2),
  356. make_tuple(32, 32, 15, 4, &vpx_avg_4x4_sse2)));
  357. INSTANTIATE_TEST_CASE_P(
  358. SSE2, IntProRowTest,
  359. ::testing::Values(make_tuple(16, &vpx_int_pro_row_sse2, &vpx_int_pro_row_c),
  360. make_tuple(32, &vpx_int_pro_row_sse2, &vpx_int_pro_row_c),
  361. make_tuple(64, &vpx_int_pro_row_sse2,
  362. &vpx_int_pro_row_c)));
  363. INSTANTIATE_TEST_CASE_P(
  364. SSE2, IntProColTest,
  365. ::testing::Values(make_tuple(16, &vpx_int_pro_col_sse2, &vpx_int_pro_col_c),
  366. make_tuple(32, &vpx_int_pro_col_sse2, &vpx_int_pro_col_c),
  367. make_tuple(64, &vpx_int_pro_col_sse2,
  368. &vpx_int_pro_col_c)));
  369. INSTANTIATE_TEST_CASE_P(SSE2, SatdTest,
  370. ::testing::Values(make_tuple(16, &vpx_satd_sse2),
  371. make_tuple(64, &vpx_satd_sse2),
  372. make_tuple(256, &vpx_satd_sse2),
  373. make_tuple(1024, &vpx_satd_sse2)));
  374. INSTANTIATE_TEST_CASE_P(
  375. SSE2, BlockErrorTestFP,
  376. ::testing::Values(make_tuple(16, &vp9_block_error_fp_sse2),
  377. make_tuple(64, &vp9_block_error_fp_sse2),
  378. make_tuple(256, &vp9_block_error_fp_sse2),
  379. make_tuple(1024, &vp9_block_error_fp_sse2)));
  380. #endif // HAVE_SSE2
  381. #if HAVE_NEON
  382. INSTANTIATE_TEST_CASE_P(
  383. NEON, AverageTest,
  384. ::testing::Values(make_tuple(16, 16, 0, 8, &vpx_avg_8x8_neon),
  385. make_tuple(16, 16, 5, 8, &vpx_avg_8x8_neon),
  386. make_tuple(32, 32, 15, 8, &vpx_avg_8x8_neon),
  387. make_tuple(16, 16, 0, 4, &vpx_avg_4x4_neon),
  388. make_tuple(16, 16, 5, 4, &vpx_avg_4x4_neon),
  389. make_tuple(32, 32, 15, 4, &vpx_avg_4x4_neon)));
  390. INSTANTIATE_TEST_CASE_P(
  391. NEON, IntProRowTest,
  392. ::testing::Values(make_tuple(16, &vpx_int_pro_row_neon, &vpx_int_pro_row_c),
  393. make_tuple(32, &vpx_int_pro_row_neon, &vpx_int_pro_row_c),
  394. make_tuple(64, &vpx_int_pro_row_neon,
  395. &vpx_int_pro_row_c)));
  396. INSTANTIATE_TEST_CASE_P(
  397. NEON, IntProColTest,
  398. ::testing::Values(make_tuple(16, &vpx_int_pro_col_neon, &vpx_int_pro_col_c),
  399. make_tuple(32, &vpx_int_pro_col_neon, &vpx_int_pro_col_c),
  400. make_tuple(64, &vpx_int_pro_col_neon,
  401. &vpx_int_pro_col_c)));
  402. INSTANTIATE_TEST_CASE_P(NEON, SatdTest,
  403. ::testing::Values(make_tuple(16, &vpx_satd_neon),
  404. make_tuple(64, &vpx_satd_neon),
  405. make_tuple(256, &vpx_satd_neon),
  406. make_tuple(1024, &vpx_satd_neon)));
  407. // TODO(jianj): Remove the highbitdepth flag once the SIMD functions are
  408. // in place.
  409. #if !CONFIG_VP9_HIGHBITDEPTH
  410. INSTANTIATE_TEST_CASE_P(
  411. NEON, BlockErrorTestFP,
  412. ::testing::Values(make_tuple(16, &vp9_block_error_fp_neon),
  413. make_tuple(64, &vp9_block_error_fp_neon),
  414. make_tuple(256, &vp9_block_error_fp_neon),
  415. make_tuple(1024, &vp9_block_error_fp_neon)));
  416. #endif // !CONFIG_VP9_HIGHBITDEPTH
  417. #endif // HAVE_NEON
  418. #if HAVE_MSA
  419. INSTANTIATE_TEST_CASE_P(
  420. MSA, AverageTest,
  421. ::testing::Values(make_tuple(16, 16, 0, 8, &vpx_avg_8x8_msa),
  422. make_tuple(16, 16, 5, 8, &vpx_avg_8x8_msa),
  423. make_tuple(32, 32, 15, 8, &vpx_avg_8x8_msa),
  424. make_tuple(16, 16, 0, 4, &vpx_avg_4x4_msa),
  425. make_tuple(16, 16, 5, 4, &vpx_avg_4x4_msa),
  426. make_tuple(32, 32, 15, 4, &vpx_avg_4x4_msa)));
  427. INSTANTIATE_TEST_CASE_P(
  428. MSA, IntProRowTest,
  429. ::testing::Values(make_tuple(16, &vpx_int_pro_row_msa, &vpx_int_pro_row_c),
  430. make_tuple(32, &vpx_int_pro_row_msa, &vpx_int_pro_row_c),
  431. make_tuple(64, &vpx_int_pro_row_msa,
  432. &vpx_int_pro_row_c)));
  433. INSTANTIATE_TEST_CASE_P(
  434. MSA, IntProColTest,
  435. ::testing::Values(make_tuple(16, &vpx_int_pro_col_msa, &vpx_int_pro_col_c),
  436. make_tuple(32, &vpx_int_pro_col_msa, &vpx_int_pro_col_c),
  437. make_tuple(64, &vpx_int_pro_col_msa,
  438. &vpx_int_pro_col_c)));
  439. // TODO(jingning): Remove the highbitdepth flag once the SIMD functions are
  440. // in place.
  441. #if !CONFIG_VP9_HIGHBITDEPTH
  442. INSTANTIATE_TEST_CASE_P(MSA, SatdTest,
  443. ::testing::Values(make_tuple(16, &vpx_satd_msa),
  444. make_tuple(64, &vpx_satd_msa),
  445. make_tuple(256, &vpx_satd_msa),
  446. make_tuple(1024, &vpx_satd_msa)));
  447. #endif // !CONFIG_VP9_HIGHBITDEPTH
  448. #endif // HAVE_MSA
  449. } // namespace