highbd_quantize_intrin_sse2.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2015 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 <emmintrin.h>
  12. #include "vpx_dsp/vpx_dsp_common.h"
  13. #include "vpx_mem/vpx_mem.h"
  14. #include "vpx_ports/mem.h"
  15. #if CONFIG_VP9_HIGHBITDEPTH
  16. void vpx_highbd_quantize_b_sse2(const tran_low_t *coeff_ptr, intptr_t count,
  17. int skip_block, const int16_t *zbin_ptr,
  18. const int16_t *round_ptr,
  19. const int16_t *quant_ptr,
  20. const int16_t *quant_shift_ptr,
  21. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  22. const int16_t *dequant_ptr, uint16_t *eob_ptr,
  23. const int16_t *scan, const int16_t *iscan) {
  24. int i, j, non_zero_regs = (int)count / 4, eob_i = -1;
  25. __m128i zbins[2];
  26. __m128i nzbins[2];
  27. zbins[0] = _mm_set_epi32((int)zbin_ptr[1], (int)zbin_ptr[1], (int)zbin_ptr[1],
  28. (int)zbin_ptr[0]);
  29. zbins[1] = _mm_set1_epi32((int)zbin_ptr[1]);
  30. nzbins[0] = _mm_setzero_si128();
  31. nzbins[1] = _mm_setzero_si128();
  32. nzbins[0] = _mm_sub_epi32(nzbins[0], zbins[0]);
  33. nzbins[1] = _mm_sub_epi32(nzbins[1], zbins[1]);
  34. (void)scan;
  35. (void)skip_block;
  36. assert(!skip_block);
  37. memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr));
  38. memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr));
  39. // Pre-scan pass
  40. for (i = ((int)count / 4) - 1; i >= 0; i--) {
  41. __m128i coeffs, cmp1, cmp2;
  42. int test;
  43. coeffs = _mm_load_si128((const __m128i *)(coeff_ptr + i * 4));
  44. cmp1 = _mm_cmplt_epi32(coeffs, zbins[i != 0]);
  45. cmp2 = _mm_cmpgt_epi32(coeffs, nzbins[i != 0]);
  46. cmp1 = _mm_and_si128(cmp1, cmp2);
  47. test = _mm_movemask_epi8(cmp1);
  48. if (test == 0xffff)
  49. non_zero_regs--;
  50. else
  51. break;
  52. }
  53. // Quantization pass:
  54. for (i = 0; i < non_zero_regs; i++) {
  55. __m128i coeffs, coeffs_sign, tmp1, tmp2;
  56. int test;
  57. int abs_coeff[4];
  58. int coeff_sign[4];
  59. coeffs = _mm_load_si128((const __m128i *)(coeff_ptr + i * 4));
  60. coeffs_sign = _mm_srai_epi32(coeffs, 31);
  61. coeffs = _mm_sub_epi32(_mm_xor_si128(coeffs, coeffs_sign), coeffs_sign);
  62. tmp1 = _mm_cmpgt_epi32(coeffs, zbins[i != 0]);
  63. tmp2 = _mm_cmpeq_epi32(coeffs, zbins[i != 0]);
  64. tmp1 = _mm_or_si128(tmp1, tmp2);
  65. test = _mm_movemask_epi8(tmp1);
  66. _mm_storeu_si128((__m128i *)abs_coeff, coeffs);
  67. _mm_storeu_si128((__m128i *)coeff_sign, coeffs_sign);
  68. for (j = 0; j < 4; j++) {
  69. if (test & (1 << (4 * j))) {
  70. int k = 4 * i + j;
  71. const int64_t tmp3 = abs_coeff[j] + round_ptr[k != 0];
  72. const int64_t tmp4 = ((tmp3 * quant_ptr[k != 0]) >> 16) + tmp3;
  73. const uint32_t abs_qcoeff =
  74. (uint32_t)((tmp4 * quant_shift_ptr[k != 0]) >> 16);
  75. qcoeff_ptr[k] = (int)(abs_qcoeff ^ coeff_sign[j]) - coeff_sign[j];
  76. dqcoeff_ptr[k] = qcoeff_ptr[k] * dequant_ptr[k != 0];
  77. if (abs_qcoeff) eob_i = iscan[k] > eob_i ? iscan[k] : eob_i;
  78. }
  79. }
  80. }
  81. *eob_ptr = eob_i + 1;
  82. }
  83. void vpx_highbd_quantize_b_32x32_sse2(
  84. const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
  85. const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr,
  86. const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
  87. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
  88. const int16_t *scan, const int16_t *iscan) {
  89. __m128i zbins[2];
  90. __m128i nzbins[2];
  91. int idx = 0;
  92. int idx_arr[1024];
  93. int i, eob = -1;
  94. const int zbin0_tmp = ROUND_POWER_OF_TWO(zbin_ptr[0], 1);
  95. const int zbin1_tmp = ROUND_POWER_OF_TWO(zbin_ptr[1], 1);
  96. (void)scan;
  97. (void)skip_block;
  98. assert(!skip_block);
  99. zbins[0] = _mm_set_epi32(zbin1_tmp, zbin1_tmp, zbin1_tmp, zbin0_tmp);
  100. zbins[1] = _mm_set1_epi32(zbin1_tmp);
  101. nzbins[0] = _mm_setzero_si128();
  102. nzbins[1] = _mm_setzero_si128();
  103. nzbins[0] = _mm_sub_epi32(nzbins[0], zbins[0]);
  104. nzbins[1] = _mm_sub_epi32(nzbins[1], zbins[1]);
  105. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  106. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  107. // Pre-scan pass
  108. for (i = 0; i < n_coeffs / 4; i++) {
  109. __m128i coeffs, cmp1, cmp2;
  110. int test;
  111. coeffs = _mm_load_si128((const __m128i *)(coeff_ptr + i * 4));
  112. cmp1 = _mm_cmplt_epi32(coeffs, zbins[i != 0]);
  113. cmp2 = _mm_cmpgt_epi32(coeffs, nzbins[i != 0]);
  114. cmp1 = _mm_and_si128(cmp1, cmp2);
  115. test = _mm_movemask_epi8(cmp1);
  116. if (!(test & 0xf)) idx_arr[idx++] = i * 4;
  117. if (!(test & 0xf0)) idx_arr[idx++] = i * 4 + 1;
  118. if (!(test & 0xf00)) idx_arr[idx++] = i * 4 + 2;
  119. if (!(test & 0xf000)) idx_arr[idx++] = i * 4 + 3;
  120. }
  121. // Quantization pass: only process the coefficients selected in
  122. // pre-scan pass. Note: idx can be zero.
  123. for (i = 0; i < idx; i++) {
  124. const int rc = idx_arr[i];
  125. const int coeff = coeff_ptr[rc];
  126. const int coeff_sign = (coeff >> 31);
  127. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  128. const int64_t tmp1 = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
  129. const int64_t tmp2 = ((tmp1 * quant_ptr[rc != 0]) >> 16) + tmp1;
  130. const uint32_t abs_qcoeff =
  131. (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> 15);
  132. qcoeff_ptr[rc] = (int)(abs_qcoeff ^ coeff_sign) - coeff_sign;
  133. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
  134. if (abs_qcoeff) eob = iscan[idx_arr[i]] > eob ? iscan[idx_arr[i]] : eob;
  135. }
  136. *eob_ptr = eob + 1;
  137. }
  138. #endif