vp9_quantize_test.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (c) 2014 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 <math.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "third_party/googletest/src/include/gtest/gtest.h"
  14. #include "./vpx_config.h"
  15. #include "./vpx_dsp_rtcd.h"
  16. #include "./vp9_rtcd.h"
  17. #include "test/acm_random.h"
  18. #include "test/buffer.h"
  19. #include "test/clear_system_state.h"
  20. #include "test/register_state_check.h"
  21. #include "test/util.h"
  22. #include "vp9/common/vp9_entropy.h"
  23. #include "vp9/common/vp9_scan.h"
  24. #include "vpx/vpx_codec.h"
  25. #include "vpx/vpx_integer.h"
  26. #include "vpx_ports/vpx_timer.h"
  27. using libvpx_test::ACMRandom;
  28. using libvpx_test::Buffer;
  29. namespace {
  30. const int number_of_iterations = 100;
  31. typedef void (*QuantizeFunc)(const tran_low_t *coeff, intptr_t count,
  32. int skip_block, const int16_t *zbin,
  33. const int16_t *round, const int16_t *quant,
  34. const int16_t *quant_shift, tran_low_t *qcoeff,
  35. tran_low_t *dqcoeff, const int16_t *dequant,
  36. uint16_t *eob, const int16_t *scan,
  37. const int16_t *iscan);
  38. typedef std::tr1::tuple<QuantizeFunc, QuantizeFunc, vpx_bit_depth_t,
  39. int /*max_size*/>
  40. QuantizeParam;
  41. // Wrapper for FP version which does not use zbin or quant_shift.
  42. typedef void (*QuantizeFPFunc)(const tran_low_t *coeff, intptr_t count,
  43. int skip_block, const int16_t *round,
  44. const int16_t *quant, tran_low_t *qcoeff,
  45. tran_low_t *dqcoeff, const int16_t *dequant,
  46. uint16_t *eob, const int16_t *scan,
  47. const int16_t *iscan);
  48. template <QuantizeFPFunc fn>
  49. void QuantFPWrapper(const tran_low_t *coeff, intptr_t count, int skip_block,
  50. const int16_t *zbin, const int16_t *round,
  51. const int16_t *quant, const int16_t *quant_shift,
  52. tran_low_t *qcoeff, tran_low_t *dqcoeff,
  53. const int16_t *dequant, uint16_t *eob, const int16_t *scan,
  54. const int16_t *iscan) {
  55. (void)zbin;
  56. (void)quant_shift;
  57. fn(coeff, count, skip_block, round, quant, qcoeff, dqcoeff, dequant, eob,
  58. scan, iscan);
  59. }
  60. class VP9QuantizeBase {
  61. public:
  62. VP9QuantizeBase(vpx_bit_depth_t bit_depth, int max_size)
  63. : bit_depth_(bit_depth), max_size_(max_size) {
  64. max_value_ = (1 << bit_depth_) - 1;
  65. zbin_ptr_ =
  66. reinterpret_cast<int16_t *>(vpx_memalign(16, 8 * sizeof(*zbin_ptr_)));
  67. round_ptr_ =
  68. reinterpret_cast<int16_t *>(vpx_memalign(16, 8 * sizeof(*round_ptr_)));
  69. quant_ptr_ =
  70. reinterpret_cast<int16_t *>(vpx_memalign(16, 8 * sizeof(*quant_ptr_)));
  71. quant_shift_ptr_ = reinterpret_cast<int16_t *>(
  72. vpx_memalign(16, 8 * sizeof(*quant_shift_ptr_)));
  73. dequant_ptr_ = reinterpret_cast<int16_t *>(
  74. vpx_memalign(16, 8 * sizeof(*dequant_ptr_)));
  75. }
  76. ~VP9QuantizeBase() {
  77. vpx_free(zbin_ptr_);
  78. vpx_free(round_ptr_);
  79. vpx_free(quant_ptr_);
  80. vpx_free(quant_shift_ptr_);
  81. vpx_free(dequant_ptr_);
  82. zbin_ptr_ = NULL;
  83. round_ptr_ = NULL;
  84. quant_ptr_ = NULL;
  85. quant_shift_ptr_ = NULL;
  86. dequant_ptr_ = NULL;
  87. libvpx_test::ClearSystemState();
  88. }
  89. protected:
  90. int16_t *zbin_ptr_;
  91. int16_t *round_ptr_;
  92. int16_t *quant_ptr_;
  93. int16_t *quant_shift_ptr_;
  94. int16_t *dequant_ptr_;
  95. const vpx_bit_depth_t bit_depth_;
  96. int max_value_;
  97. const int max_size_;
  98. };
  99. class VP9QuantizeTest : public VP9QuantizeBase,
  100. public ::testing::TestWithParam<QuantizeParam> {
  101. public:
  102. VP9QuantizeTest()
  103. : VP9QuantizeBase(GET_PARAM(2), GET_PARAM(3)), quantize_op_(GET_PARAM(0)),
  104. ref_quantize_op_(GET_PARAM(1)) {}
  105. protected:
  106. const QuantizeFunc quantize_op_;
  107. const QuantizeFunc ref_quantize_op_;
  108. };
  109. void GenerateHelperArrays(ACMRandom *rnd, int16_t *zbin, int16_t *round,
  110. int16_t *quant, int16_t *quant_shift,
  111. int16_t *dequant) {
  112. for (int j = 0; j < 2; j++) {
  113. // Values determined by deconstructing vp9_init_quantizer().
  114. // zbin may be up to 1143 for 8 and 10 bit Y values, or 1200 for 12 bit Y
  115. // values or U/V values of any bit depth. This is because y_delta is not
  116. // factored into the vp9_ac_quant() call.
  117. zbin[j] = rnd->RandRange(1200);
  118. // round may be up to 685 for Y values or 914 for U/V.
  119. round[j] = rnd->RandRange(914);
  120. // quant ranges from 1 to -32703
  121. quant[j] = static_cast<int>(rnd->RandRange(32704)) - 32703;
  122. // quant_shift goes up to 1 << 16.
  123. quant_shift[j] = rnd->RandRange(16384);
  124. // dequant maxes out at 1828 for all cases.
  125. dequant[j] = rnd->RandRange(1828);
  126. }
  127. for (int j = 2; j < 8; j++) {
  128. zbin[j] = zbin[1];
  129. round[j] = round[1];
  130. quant[j] = quant[1];
  131. quant_shift[j] = quant_shift[1];
  132. dequant[j] = dequant[1];
  133. }
  134. }
  135. TEST_P(VP9QuantizeTest, OperationCheck) {
  136. ACMRandom rnd(ACMRandom::DeterministicSeed());
  137. Buffer<tran_low_t> coeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 16);
  138. ASSERT_TRUE(coeff.Init());
  139. Buffer<tran_low_t> qcoeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  140. ASSERT_TRUE(qcoeff.Init());
  141. Buffer<tran_low_t> dqcoeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  142. ASSERT_TRUE(dqcoeff.Init());
  143. Buffer<tran_low_t> ref_qcoeff =
  144. Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  145. ASSERT_TRUE(ref_qcoeff.Init());
  146. Buffer<tran_low_t> ref_dqcoeff =
  147. Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  148. ASSERT_TRUE(ref_dqcoeff.Init());
  149. uint16_t eob, ref_eob;
  150. for (int i = 0; i < number_of_iterations; ++i) {
  151. // Test skip block for the first three iterations to catch all the different
  152. // sizes.
  153. const int skip_block = 0;
  154. TX_SIZE sz;
  155. if (max_size_ == 16) {
  156. sz = static_cast<TX_SIZE>(i % 3); // TX_4X4, TX_8X8 TX_16X16
  157. } else {
  158. sz = TX_32X32;
  159. }
  160. const TX_TYPE tx_type = static_cast<TX_TYPE>((i >> 2) % 3);
  161. const scan_order *scan_order = &vp9_scan_orders[sz][tx_type];
  162. const int count = (4 << sz) * (4 << sz);
  163. coeff.Set(&rnd, -max_value_, max_value_);
  164. GenerateHelperArrays(&rnd, zbin_ptr_, round_ptr_, quant_ptr_,
  165. quant_shift_ptr_, dequant_ptr_);
  166. ref_quantize_op_(coeff.TopLeftPixel(), count, skip_block, zbin_ptr_,
  167. round_ptr_, quant_ptr_, quant_shift_ptr_,
  168. ref_qcoeff.TopLeftPixel(), ref_dqcoeff.TopLeftPixel(),
  169. dequant_ptr_, &ref_eob, scan_order->scan,
  170. scan_order->iscan);
  171. ASM_REGISTER_STATE_CHECK(
  172. quantize_op_(coeff.TopLeftPixel(), count, skip_block, zbin_ptr_,
  173. round_ptr_, quant_ptr_, quant_shift_ptr_,
  174. qcoeff.TopLeftPixel(), dqcoeff.TopLeftPixel(),
  175. dequant_ptr_, &eob, scan_order->scan, scan_order->iscan));
  176. EXPECT_TRUE(qcoeff.CheckValues(ref_qcoeff));
  177. EXPECT_TRUE(dqcoeff.CheckValues(ref_dqcoeff));
  178. EXPECT_EQ(eob, ref_eob);
  179. if (HasFailure()) {
  180. printf("Failure on iteration %d.\n", i);
  181. qcoeff.PrintDifference(ref_qcoeff);
  182. dqcoeff.PrintDifference(ref_dqcoeff);
  183. return;
  184. }
  185. }
  186. }
  187. TEST_P(VP9QuantizeTest, EOBCheck) {
  188. ACMRandom rnd(ACMRandom::DeterministicSeed());
  189. Buffer<tran_low_t> coeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 16);
  190. ASSERT_TRUE(coeff.Init());
  191. Buffer<tran_low_t> qcoeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  192. ASSERT_TRUE(qcoeff.Init());
  193. Buffer<tran_low_t> dqcoeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  194. ASSERT_TRUE(dqcoeff.Init());
  195. Buffer<tran_low_t> ref_qcoeff =
  196. Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  197. ASSERT_TRUE(ref_qcoeff.Init());
  198. Buffer<tran_low_t> ref_dqcoeff =
  199. Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  200. ASSERT_TRUE(ref_dqcoeff.Init());
  201. uint16_t eob, ref_eob;
  202. for (int i = 0; i < number_of_iterations; ++i) {
  203. const int skip_block = 0;
  204. TX_SIZE sz;
  205. if (max_size_ == 16) {
  206. sz = static_cast<TX_SIZE>(i % 3); // TX_4X4, TX_8X8 TX_16X16
  207. } else {
  208. sz = TX_32X32;
  209. }
  210. const TX_TYPE tx_type = static_cast<TX_TYPE>((i >> 2) % 3);
  211. const scan_order *scan_order = &vp9_scan_orders[sz][tx_type];
  212. int count = (4 << sz) * (4 << sz);
  213. // Two random entries
  214. coeff.Set(0);
  215. coeff.TopLeftPixel()[rnd(count)] =
  216. static_cast<int>(rnd.RandRange(max_value_ * 2)) - max_value_;
  217. coeff.TopLeftPixel()[rnd(count)] =
  218. static_cast<int>(rnd.RandRange(max_value_ * 2)) - max_value_;
  219. GenerateHelperArrays(&rnd, zbin_ptr_, round_ptr_, quant_ptr_,
  220. quant_shift_ptr_, dequant_ptr_);
  221. ref_quantize_op_(coeff.TopLeftPixel(), count, skip_block, zbin_ptr_,
  222. round_ptr_, quant_ptr_, quant_shift_ptr_,
  223. ref_qcoeff.TopLeftPixel(), ref_dqcoeff.TopLeftPixel(),
  224. dequant_ptr_, &ref_eob, scan_order->scan,
  225. scan_order->iscan);
  226. ASM_REGISTER_STATE_CHECK(
  227. quantize_op_(coeff.TopLeftPixel(), count, skip_block, zbin_ptr_,
  228. round_ptr_, quant_ptr_, quant_shift_ptr_,
  229. qcoeff.TopLeftPixel(), dqcoeff.TopLeftPixel(),
  230. dequant_ptr_, &eob, scan_order->scan, scan_order->iscan));
  231. EXPECT_TRUE(qcoeff.CheckValues(ref_qcoeff));
  232. EXPECT_TRUE(dqcoeff.CheckValues(ref_dqcoeff));
  233. EXPECT_EQ(eob, ref_eob);
  234. if (HasFailure()) {
  235. printf("Failure on iteration %d.\n", i);
  236. qcoeff.PrintDifference(ref_qcoeff);
  237. dqcoeff.PrintDifference(ref_dqcoeff);
  238. return;
  239. }
  240. }
  241. }
  242. TEST_P(VP9QuantizeTest, DISABLED_Speed) {
  243. ACMRandom rnd(ACMRandom::DeterministicSeed());
  244. Buffer<tran_low_t> coeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 16);
  245. ASSERT_TRUE(coeff.Init());
  246. Buffer<tran_low_t> qcoeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  247. ASSERT_TRUE(qcoeff.Init());
  248. Buffer<tran_low_t> dqcoeff = Buffer<tran_low_t>(max_size_, max_size_, 0, 32);
  249. ASSERT_TRUE(dqcoeff.Init());
  250. uint16_t eob;
  251. TX_SIZE starting_sz, ending_sz;
  252. if (max_size_ == 16) {
  253. starting_sz = TX_4X4;
  254. ending_sz = TX_16X16;
  255. } else {
  256. starting_sz = TX_32X32;
  257. ending_sz = TX_32X32;
  258. }
  259. for (TX_SIZE sz = starting_sz; sz <= ending_sz; ++sz) {
  260. // zbin > coeff, zbin < coeff.
  261. for (int i = 0; i < 2; ++i) {
  262. const int skip_block = 0;
  263. // TX_TYPE defines the scan order. That is not relevant to the speed test.
  264. // Pick the first one.
  265. const TX_TYPE tx_type = DCT_DCT;
  266. const scan_order *scan_order = &vp9_scan_orders[sz][tx_type];
  267. const int count = (4 << sz) * (4 << sz);
  268. GenerateHelperArrays(&rnd, zbin_ptr_, round_ptr_, quant_ptr_,
  269. quant_shift_ptr_, dequant_ptr_);
  270. if (i == 0) {
  271. // When |coeff values| are less than zbin the results are 0.
  272. int threshold = 100;
  273. if (max_size_ == 32) {
  274. // For 32x32, the threshold is halved. Double it to keep the values
  275. // from clearing it.
  276. threshold = 200;
  277. }
  278. for (int j = 0; j < 8; ++j) zbin_ptr_[j] = threshold;
  279. coeff.Set(&rnd, -99, 99);
  280. } else if (i == 1) {
  281. for (int j = 0; j < 8; ++j) zbin_ptr_[j] = 50;
  282. coeff.Set(&rnd, -500, 500);
  283. }
  284. vpx_usec_timer timer;
  285. vpx_usec_timer_start(&timer);
  286. for (int j = 0; j < 100000000 / count; ++j) {
  287. quantize_op_(coeff.TopLeftPixel(), count, skip_block, zbin_ptr_,
  288. round_ptr_, quant_ptr_, quant_shift_ptr_,
  289. qcoeff.TopLeftPixel(), dqcoeff.TopLeftPixel(),
  290. dequant_ptr_, &eob, scan_order->scan, scan_order->iscan);
  291. }
  292. vpx_usec_timer_mark(&timer);
  293. const int elapsed_time = static_cast<int>(vpx_usec_timer_elapsed(&timer));
  294. if (i == 0) printf("Bypass calculations.\n");
  295. if (i == 1) printf("Full calculations.\n");
  296. printf("Quantize %dx%d time: %5d ms\n", 4 << sz, 4 << sz,
  297. elapsed_time / 1000);
  298. }
  299. printf("\n");
  300. }
  301. }
  302. using std::tr1::make_tuple;
  303. #if HAVE_SSE2
  304. #if CONFIG_VP9_HIGHBITDEPTH
  305. // TODO(johannkoenig): Fix vpx_quantize_b_sse2 in highbitdepth builds.
  306. // make_tuple(&vpx_quantize_b_sse2, &vpx_highbd_quantize_b_c, VPX_BITS_8),
  307. INSTANTIATE_TEST_CASE_P(
  308. SSE2, VP9QuantizeTest,
  309. ::testing::Values(
  310. make_tuple(&vpx_highbd_quantize_b_sse2, &vpx_highbd_quantize_b_c,
  311. VPX_BITS_8, 16),
  312. make_tuple(&vpx_highbd_quantize_b_sse2, &vpx_highbd_quantize_b_c,
  313. VPX_BITS_10, 16),
  314. make_tuple(&vpx_highbd_quantize_b_sse2, &vpx_highbd_quantize_b_c,
  315. VPX_BITS_12, 16),
  316. make_tuple(&vpx_highbd_quantize_b_32x32_sse2,
  317. &vpx_highbd_quantize_b_32x32_c, VPX_BITS_8, 32),
  318. make_tuple(&vpx_highbd_quantize_b_32x32_sse2,
  319. &vpx_highbd_quantize_b_32x32_c, VPX_BITS_10, 32),
  320. make_tuple(&vpx_highbd_quantize_b_32x32_sse2,
  321. &vpx_highbd_quantize_b_32x32_c, VPX_BITS_12, 32)));
  322. #else
  323. INSTANTIATE_TEST_CASE_P(SSE2, VP9QuantizeTest,
  324. ::testing::Values(make_tuple(&vpx_quantize_b_sse2,
  325. &vpx_quantize_b_c,
  326. VPX_BITS_8, 16)));
  327. #endif // CONFIG_VP9_HIGHBITDEPTH
  328. INSTANTIATE_TEST_CASE_P(
  329. DISABLED_SSE2, VP9QuantizeTest,
  330. ::testing::Values(make_tuple(&QuantFPWrapper<vp9_quantize_fp_sse2>,
  331. &QuantFPWrapper<vp9_quantize_fp_c>, VPX_BITS_8,
  332. 16)));
  333. #endif // HAVE_SSE2
  334. #if HAVE_SSSE3 && !CONFIG_VP9_HIGHBITDEPTH
  335. INSTANTIATE_TEST_CASE_P(SSSE3, VP9QuantizeTest,
  336. ::testing::Values(make_tuple(&vpx_quantize_b_ssse3,
  337. &vpx_quantize_b_c,
  338. VPX_BITS_8, 16)));
  339. #if ARCH_X86_64
  340. // TODO(johannkoenig): SSSE3 optimizations do not yet pass this test.
  341. INSTANTIATE_TEST_CASE_P(
  342. DISABLED_SSSE3, VP9QuantizeTest,
  343. ::testing::Values(make_tuple(&vpx_quantize_b_32x32_ssse3,
  344. &vpx_quantize_b_32x32_c, VPX_BITS_8, 32),
  345. make_tuple(&QuantFPWrapper<vp9_quantize_fp_ssse3>,
  346. &QuantFPWrapper<vp9_quantize_fp_c>, VPX_BITS_8,
  347. 16),
  348. make_tuple(&QuantFPWrapper<vp9_quantize_fp_32x32_ssse3>,
  349. &QuantFPWrapper<vp9_quantize_fp_32x32_c>,
  350. VPX_BITS_8, 32)));
  351. #endif // ARCH_X86_64
  352. #endif // HAVE_SSSE3 && !CONFIG_VP9_HIGHBITDEPTH
  353. // TODO(johannkoenig): AVX optimizations do not yet pass the 32x32 test or
  354. // highbitdepth configurations.
  355. #if HAVE_AVX && !CONFIG_VP9_HIGHBITDEPTH
  356. INSTANTIATE_TEST_CASE_P(
  357. AVX, VP9QuantizeTest,
  358. ::testing::Values(make_tuple(&vpx_quantize_b_avx, &vpx_quantize_b_c,
  359. VPX_BITS_8, 16),
  360. // Even though SSSE3 and AVX do not match the reference
  361. // code, we can keep them in sync with each other.
  362. make_tuple(&vpx_quantize_b_32x32_avx,
  363. &vpx_quantize_b_32x32_ssse3, VPX_BITS_8, 32)));
  364. #endif // HAVE_AVX && !CONFIG_VP9_HIGHBITDEPTH
  365. // TODO(webm:1448): dqcoeff is not handled correctly in HBD builds.
  366. #if HAVE_NEON && !CONFIG_VP9_HIGHBITDEPTH
  367. INSTANTIATE_TEST_CASE_P(
  368. NEON, VP9QuantizeTest,
  369. ::testing::Values(
  370. make_tuple(&vpx_quantize_b_neon, &vpx_quantize_b_c, VPX_BITS_8, 16),
  371. make_tuple(&vpx_quantize_b_32x32_neon, &vpx_quantize_b_32x32_c,
  372. VPX_BITS_8, 32),
  373. make_tuple(&QuantFPWrapper<vp9_quantize_fp_neon>,
  374. &QuantFPWrapper<vp9_quantize_fp_c>, VPX_BITS_8, 16),
  375. make_tuple(&QuantFPWrapper<vp9_quantize_fp_32x32_neon>,
  376. &QuantFPWrapper<vp9_quantize_fp_32x32_c>, VPX_BITS_8, 32)));
  377. #endif // HAVE_NEON && !CONFIG_VP9_HIGHBITDEPTH
  378. // Only useful to compare "Speed" test results.
  379. INSTANTIATE_TEST_CASE_P(
  380. DISABLED_C, VP9QuantizeTest,
  381. ::testing::Values(
  382. make_tuple(&vpx_quantize_b_c, &vpx_quantize_b_c, VPX_BITS_8, 16),
  383. make_tuple(&vpx_quantize_b_32x32_c, &vpx_quantize_b_32x32_c, VPX_BITS_8,
  384. 32),
  385. make_tuple(&QuantFPWrapper<vp9_quantize_fp_c>,
  386. &QuantFPWrapper<vp9_quantize_fp_c>, VPX_BITS_8, 16),
  387. make_tuple(&QuantFPWrapper<vp9_quantize_fp_32x32_c>,
  388. &QuantFPWrapper<vp9_quantize_fp_32x32_c>, VPX_BITS_8, 32)));
  389. } // namespace