quantize_ssse3.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <tmmintrin.h>
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx/vpx_integer.h"
  14. #include "vpx_dsp/x86/bitdepth_conversion_sse2.h"
  15. #include "vpx_dsp/x86/quantize_sse2.h"
  16. #include "vpx_dsp/x86/quantize_ssse3.h"
  17. void vpx_quantize_b_ssse3(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  18. int skip_block, const int16_t *zbin_ptr,
  19. const int16_t *round_ptr, 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. const __m128i zero = _mm_setzero_si128();
  25. int index = 16;
  26. __m128i zbin, round, quant, dequant, shift;
  27. __m128i coeff0, coeff1;
  28. __m128i qcoeff0, qcoeff1;
  29. __m128i cmp_mask0, cmp_mask1;
  30. __m128i eob, eob0;
  31. (void)scan;
  32. (void)skip_block;
  33. assert(!skip_block);
  34. load_b_values(zbin_ptr, &zbin, round_ptr, &round, quant_ptr, &quant,
  35. dequant_ptr, &dequant, quant_shift_ptr, &shift);
  36. // Do DC and first 15 AC.
  37. coeff0 = load_tran_low(coeff_ptr);
  38. coeff1 = load_tran_low(coeff_ptr + 8);
  39. qcoeff0 = _mm_abs_epi16(coeff0);
  40. qcoeff1 = _mm_abs_epi16(coeff1);
  41. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  42. zbin = _mm_unpackhi_epi64(zbin, zbin); // Switch DC to AC
  43. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  44. calculate_qcoeff(&qcoeff0, round, quant, shift);
  45. round = _mm_unpackhi_epi64(round, round);
  46. quant = _mm_unpackhi_epi64(quant, quant);
  47. shift = _mm_unpackhi_epi64(shift, shift);
  48. calculate_qcoeff(&qcoeff1, round, quant, shift);
  49. // Reinsert signs
  50. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  51. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  52. // Mask out zbin threshold coeffs
  53. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  54. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  55. store_tran_low(qcoeff0, qcoeff_ptr);
  56. store_tran_low(qcoeff1, qcoeff_ptr + 8);
  57. calculate_dqcoeff_and_store(qcoeff0, dequant, dqcoeff_ptr);
  58. dequant = _mm_unpackhi_epi64(dequant, dequant);
  59. calculate_dqcoeff_and_store(qcoeff1, dequant, dqcoeff_ptr + 8);
  60. eob = scan_for_eob(&qcoeff0, &qcoeff1, cmp_mask0, cmp_mask1, iscan, 0, zero);
  61. // AC only loop.
  62. while (index < n_coeffs) {
  63. coeff0 = load_tran_low(coeff_ptr + index);
  64. coeff1 = load_tran_low(coeff_ptr + index + 8);
  65. qcoeff0 = _mm_abs_epi16(coeff0);
  66. qcoeff1 = _mm_abs_epi16(coeff1);
  67. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  68. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  69. calculate_qcoeff(&qcoeff0, round, quant, shift);
  70. calculate_qcoeff(&qcoeff1, round, quant, shift);
  71. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  72. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  73. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  74. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  75. store_tran_low(qcoeff0, qcoeff_ptr + index);
  76. store_tran_low(qcoeff1, qcoeff_ptr + index + 8);
  77. calculate_dqcoeff_and_store(qcoeff0, dequant, dqcoeff_ptr + index);
  78. calculate_dqcoeff_and_store(qcoeff1, dequant, dqcoeff_ptr + index + 8);
  79. eob0 = scan_for_eob(&qcoeff0, &qcoeff1, cmp_mask0, cmp_mask1, iscan, index,
  80. zero);
  81. eob = _mm_max_epi16(eob, eob0);
  82. index += 16;
  83. }
  84. *eob_ptr = accumulate_eob(eob);
  85. }
  86. void vpx_quantize_b_32x32_ssse3(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  87. int skip_block, const int16_t *zbin_ptr,
  88. const int16_t *round_ptr,
  89. const int16_t *quant_ptr,
  90. const int16_t *quant_shift_ptr,
  91. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  92. const int16_t *dequant_ptr, uint16_t *eob_ptr,
  93. const int16_t *scan, const int16_t *iscan) {
  94. const __m128i zero = _mm_setzero_si128();
  95. const __m128i one = _mm_set1_epi16(1);
  96. int index;
  97. __m128i zbin, round, quant, dequant, shift;
  98. __m128i coeff0, coeff1;
  99. __m128i qcoeff0, qcoeff1;
  100. __m128i cmp_mask0, cmp_mask1;
  101. __m128i all_zero;
  102. __m128i eob = zero, eob0;
  103. (void)scan;
  104. (void)n_coeffs;
  105. (void)skip_block;
  106. assert(!skip_block);
  107. // Setup global values.
  108. // The 32x32 halves zbin and round.
  109. zbin = _mm_load_si128((const __m128i *)zbin_ptr);
  110. // Shift with rounding.
  111. zbin = _mm_add_epi16(zbin, one);
  112. zbin = _mm_srli_epi16(zbin, 1);
  113. // x86 has no "greater *or equal*" comparison. Subtract 1 from zbin so
  114. // it is a strict "greater" comparison.
  115. zbin = _mm_sub_epi16(zbin, one);
  116. round = _mm_load_si128((const __m128i *)round_ptr);
  117. round = _mm_add_epi16(round, one);
  118. round = _mm_srli_epi16(round, 1);
  119. quant = _mm_load_si128((const __m128i *)quant_ptr);
  120. dequant = _mm_load_si128((const __m128i *)dequant_ptr);
  121. shift = _mm_load_si128((const __m128i *)quant_shift_ptr);
  122. // I suspect this is not technically OK because quant_shift can be up
  123. // to 1 << 16 and shifting up again will outrange that, but the test is not
  124. // comprehensive enough to catch that and "it's been that way forever"
  125. shift = _mm_slli_epi16(shift, 1);
  126. // Do DC and first 15 AC.
  127. coeff0 = load_tran_low(coeff_ptr);
  128. coeff1 = load_tran_low(coeff_ptr + 8);
  129. qcoeff0 = _mm_abs_epi16(coeff0);
  130. qcoeff1 = _mm_abs_epi16(coeff1);
  131. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  132. zbin = _mm_unpackhi_epi64(zbin, zbin); // Switch DC to AC.
  133. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  134. all_zero = _mm_or_si128(cmp_mask0, cmp_mask1);
  135. if (_mm_movemask_epi8(all_zero) == 0) {
  136. _mm_store_si128((__m128i *)(qcoeff_ptr), zero);
  137. _mm_store_si128((__m128i *)(qcoeff_ptr + 8), zero);
  138. _mm_store_si128((__m128i *)(dqcoeff_ptr), zero);
  139. _mm_store_si128((__m128i *)(dqcoeff_ptr + 8), zero);
  140. #if CONFIG_VP9_HIGHBITDEPTH
  141. _mm_store_si128((__m128i *)(qcoeff_ptr + 4), zero);
  142. _mm_store_si128((__m128i *)(qcoeff_ptr + 12), zero);
  143. _mm_store_si128((__m128i *)(dqcoeff_ptr + 4), zero);
  144. _mm_store_si128((__m128i *)(dqcoeff_ptr + 12), zero);
  145. #endif // CONFIG_HIGHBITDEPTH
  146. round = _mm_unpackhi_epi64(round, round);
  147. quant = _mm_unpackhi_epi64(quant, quant);
  148. shift = _mm_unpackhi_epi64(shift, shift);
  149. dequant = _mm_unpackhi_epi64(dequant, dequant);
  150. } else {
  151. calculate_qcoeff(&qcoeff0, round, quant, shift);
  152. round = _mm_unpackhi_epi64(round, round);
  153. quant = _mm_unpackhi_epi64(quant, quant);
  154. shift = _mm_unpackhi_epi64(shift, shift);
  155. calculate_qcoeff(&qcoeff1, round, quant, shift);
  156. // Reinsert signs.
  157. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  158. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  159. // Mask out zbin threshold coeffs.
  160. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  161. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  162. store_tran_low(qcoeff0, qcoeff_ptr);
  163. store_tran_low(qcoeff1, qcoeff_ptr + 8);
  164. calculate_dqcoeff_and_store_32x32(qcoeff0, dequant, zero, dqcoeff_ptr);
  165. dequant = _mm_unpackhi_epi64(dequant, dequant);
  166. calculate_dqcoeff_and_store_32x32(qcoeff1, dequant, zero, dqcoeff_ptr + 8);
  167. eob =
  168. scan_for_eob(&qcoeff0, &qcoeff1, cmp_mask0, cmp_mask1, iscan, 0, zero);
  169. }
  170. // AC only loop.
  171. for (index = 16; index < 32 * 32; index += 16) {
  172. coeff0 = load_tran_low(coeff_ptr + index);
  173. coeff1 = load_tran_low(coeff_ptr + index + 8);
  174. qcoeff0 = _mm_abs_epi16(coeff0);
  175. qcoeff1 = _mm_abs_epi16(coeff1);
  176. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  177. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  178. all_zero = _mm_or_si128(cmp_mask0, cmp_mask1);
  179. if (_mm_movemask_epi8(all_zero) == 0) {
  180. _mm_store_si128((__m128i *)(qcoeff_ptr + index), zero);
  181. _mm_store_si128((__m128i *)(qcoeff_ptr + index + 8), zero);
  182. _mm_store_si128((__m128i *)(dqcoeff_ptr + index), zero);
  183. _mm_store_si128((__m128i *)(dqcoeff_ptr + index + 8), zero);
  184. #if CONFIG_VP9_HIGHBITDEPTH
  185. _mm_store_si128((__m128i *)(qcoeff_ptr + index + 4), zero);
  186. _mm_store_si128((__m128i *)(qcoeff_ptr + index + 12), zero);
  187. _mm_store_si128((__m128i *)(dqcoeff_ptr + index + 4), zero);
  188. _mm_store_si128((__m128i *)(dqcoeff_ptr + index + 12), zero);
  189. #endif // CONFIG_VP9_HIGHBITDEPTH
  190. continue;
  191. }
  192. calculate_qcoeff(&qcoeff0, round, quant, shift);
  193. calculate_qcoeff(&qcoeff1, round, quant, shift);
  194. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  195. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  196. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  197. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  198. store_tran_low(qcoeff0, qcoeff_ptr + index);
  199. store_tran_low(qcoeff1, qcoeff_ptr + index + 8);
  200. calculate_dqcoeff_and_store_32x32(qcoeff0, dequant, zero,
  201. dqcoeff_ptr + index);
  202. calculate_dqcoeff_and_store_32x32(qcoeff1, dequant, zero,
  203. dqcoeff_ptr + 8 + index);
  204. eob0 = scan_for_eob(&qcoeff0, &qcoeff1, cmp_mask0, cmp_mask1, iscan, index,
  205. zero);
  206. eob = _mm_max_epi16(eob, eob0);
  207. }
  208. *eob_ptr = accumulate_eob(eob);
  209. }