vp9_quantize_avx2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2017 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 <assert.h>
  11. #include <immintrin.h> // AVX2
  12. #include "./vp9_rtcd.h"
  13. #include "vpx/vpx_integer.h"
  14. #include "vpx_dsp/vpx_dsp_common.h"
  15. #include "vpx_dsp/x86/bitdepth_conversion_avx2.h"
  16. #include "vpx_dsp/x86/quantize_sse2.h"
  17. // Zero fill 8 positions in the output buffer.
  18. static INLINE void store_zero_tran_low(tran_low_t *a) {
  19. const __m256i zero = _mm256_setzero_si256();
  20. #if CONFIG_VP9_HIGHBITDEPTH
  21. _mm256_storeu_si256((__m256i *)(a), zero);
  22. _mm256_storeu_si256((__m256i *)(a + 8), zero);
  23. #else
  24. _mm256_storeu_si256((__m256i *)(a), zero);
  25. #endif
  26. }
  27. static INLINE __m256i scan_eob_256(const __m256i *iscan_ptr,
  28. __m256i *coeff256) {
  29. const __m256i iscan = _mm256_loadu_si256(iscan_ptr);
  30. const __m256i zero256 = _mm256_setzero_si256();
  31. #if CONFIG_VP9_HIGHBITDEPTH
  32. // The _mm256_packs_epi32() in load_tran_low() packs the 64 bit coeff as
  33. // B1 A1 B0 A0. Shuffle to B1 B0 A1 A0 in order to scan eob correctly.
  34. const __m256i _coeff256 = _mm256_permute4x64_epi64(*coeff256, 0xd8);
  35. const __m256i zero_coeff0 = _mm256_cmpeq_epi16(_coeff256, zero256);
  36. #else
  37. const __m256i zero_coeff0 = _mm256_cmpeq_epi16(*coeff256, zero256);
  38. #endif
  39. const __m256i nzero_coeff0 = _mm256_cmpeq_epi16(zero_coeff0, zero256);
  40. // Add one to convert from indices to counts
  41. const __m256i iscan_plus_one = _mm256_sub_epi16(iscan, nzero_coeff0);
  42. return _mm256_and_si256(iscan_plus_one, nzero_coeff0);
  43. }
  44. void vp9_quantize_fp_avx2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  45. int skip_block, const int16_t *round_ptr,
  46. const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
  47. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
  48. uint16_t *eob_ptr, const int16_t *scan,
  49. const int16_t *iscan) {
  50. __m128i eob;
  51. __m256i round256, quant256, dequant256;
  52. __m256i eob256, thr256;
  53. (void)scan;
  54. (void)skip_block;
  55. assert(!skip_block);
  56. coeff_ptr += n_coeffs;
  57. iscan += n_coeffs;
  58. qcoeff_ptr += n_coeffs;
  59. dqcoeff_ptr += n_coeffs;
  60. n_coeffs = -n_coeffs;
  61. {
  62. __m256i coeff256;
  63. // Setup global values
  64. {
  65. const __m128i round = _mm_load_si128((const __m128i *)round_ptr);
  66. const __m128i quant = _mm_load_si128((const __m128i *)quant_ptr);
  67. const __m128i dequant = _mm_load_si128((const __m128i *)dequant_ptr);
  68. round256 = _mm256_castsi128_si256(round);
  69. round256 = _mm256_permute4x64_epi64(round256, 0x54);
  70. quant256 = _mm256_castsi128_si256(quant);
  71. quant256 = _mm256_permute4x64_epi64(quant256, 0x54);
  72. dequant256 = _mm256_castsi128_si256(dequant);
  73. dequant256 = _mm256_permute4x64_epi64(dequant256, 0x54);
  74. }
  75. {
  76. __m256i qcoeff256;
  77. __m256i qtmp256;
  78. coeff256 = load_tran_low(coeff_ptr + n_coeffs);
  79. qcoeff256 = _mm256_abs_epi16(coeff256);
  80. qcoeff256 = _mm256_adds_epi16(qcoeff256, round256);
  81. qtmp256 = _mm256_mulhi_epi16(qcoeff256, quant256);
  82. qcoeff256 = _mm256_sign_epi16(qtmp256, coeff256);
  83. store_tran_low(qcoeff256, qcoeff_ptr + n_coeffs);
  84. coeff256 = _mm256_mullo_epi16(qcoeff256, dequant256);
  85. store_tran_low(coeff256, dqcoeff_ptr + n_coeffs);
  86. }
  87. eob256 = scan_eob_256((const __m256i *)(iscan + n_coeffs), &coeff256);
  88. n_coeffs += 8 * 2;
  89. }
  90. // remove dc constants
  91. dequant256 = _mm256_permute2x128_si256(dequant256, dequant256, 0x31);
  92. quant256 = _mm256_permute2x128_si256(quant256, quant256, 0x31);
  93. round256 = _mm256_permute2x128_si256(round256, round256, 0x31);
  94. thr256 = _mm256_srai_epi16(dequant256, 1);
  95. // AC only loop
  96. while (n_coeffs < 0) {
  97. __m256i coeff256 = load_tran_low(coeff_ptr + n_coeffs);
  98. __m256i qcoeff256 = _mm256_abs_epi16(coeff256);
  99. int32_t nzflag =
  100. _mm256_movemask_epi8(_mm256_cmpgt_epi16(qcoeff256, thr256));
  101. if (nzflag) {
  102. __m256i qtmp256;
  103. qcoeff256 = _mm256_adds_epi16(qcoeff256, round256);
  104. qtmp256 = _mm256_mulhi_epi16(qcoeff256, quant256);
  105. qcoeff256 = _mm256_sign_epi16(qtmp256, coeff256);
  106. store_tran_low(qcoeff256, qcoeff_ptr + n_coeffs);
  107. coeff256 = _mm256_mullo_epi16(qcoeff256, dequant256);
  108. store_tran_low(coeff256, dqcoeff_ptr + n_coeffs);
  109. eob256 = _mm256_max_epi16(
  110. eob256, scan_eob_256((const __m256i *)(iscan + n_coeffs), &coeff256));
  111. } else {
  112. store_zero_tran_low(qcoeff_ptr + n_coeffs);
  113. store_zero_tran_low(dqcoeff_ptr + n_coeffs);
  114. }
  115. n_coeffs += 8 * 2;
  116. }
  117. eob = _mm_max_epi16(_mm256_castsi256_si128(eob256),
  118. _mm256_extracti128_si256(eob256, 1));
  119. *eob_ptr = accumulate_eob(eob);
  120. }