vp9_quantize_sse2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <emmintrin.h>
  11. #include <xmmintrin.h>
  12. #include "./vp9_rtcd.h"
  13. #include "vpx/vpx_integer.h"
  14. void vp9_quantize_fp_sse2(const int16_t *coeff_ptr, intptr_t n_coeffs,
  15. int skip_block, const int16_t *zbin_ptr,
  16. const int16_t *round_ptr, const int16_t *quant_ptr,
  17. const int16_t *quant_shift_ptr, int16_t *qcoeff_ptr,
  18. int16_t *dqcoeff_ptr, const int16_t *dequant_ptr,
  19. uint16_t *eob_ptr, const int16_t *scan_ptr,
  20. const int16_t *iscan_ptr) {
  21. __m128i zero;
  22. __m128i thr;
  23. int16_t nzflag;
  24. (void)scan_ptr;
  25. (void)zbin_ptr;
  26. (void)quant_shift_ptr;
  27. coeff_ptr += n_coeffs;
  28. iscan_ptr += n_coeffs;
  29. qcoeff_ptr += n_coeffs;
  30. dqcoeff_ptr += n_coeffs;
  31. n_coeffs = -n_coeffs;
  32. zero = _mm_setzero_si128();
  33. if (!skip_block) {
  34. __m128i eob;
  35. __m128i round, quant, dequant;
  36. {
  37. __m128i coeff0, coeff1;
  38. // Setup global values
  39. {
  40. round = _mm_load_si128((const __m128i *)round_ptr);
  41. quant = _mm_load_si128((const __m128i *)quant_ptr);
  42. dequant = _mm_load_si128((const __m128i *)dequant_ptr);
  43. }
  44. {
  45. __m128i coeff0_sign, coeff1_sign;
  46. __m128i qcoeff0, qcoeff1;
  47. __m128i qtmp0, qtmp1;
  48. // Do DC and first 15 AC
  49. coeff0 = _mm_load_si128((const __m128i *)(coeff_ptr + n_coeffs));
  50. coeff1 = _mm_load_si128((const __m128i *)(coeff_ptr + n_coeffs) + 1);
  51. // Poor man's sign extract
  52. coeff0_sign = _mm_srai_epi16(coeff0, 15);
  53. coeff1_sign = _mm_srai_epi16(coeff1, 15);
  54. qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
  55. qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
  56. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  57. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  58. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  59. round = _mm_unpackhi_epi64(round, round);
  60. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  61. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  62. quant = _mm_unpackhi_epi64(quant, quant);
  63. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  64. // Reinsert signs
  65. qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
  66. qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
  67. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  68. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  69. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs), qcoeff0);
  70. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs) + 1, qcoeff1);
  71. coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
  72. dequant = _mm_unpackhi_epi64(dequant, dequant);
  73. coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
  74. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs), coeff0);
  75. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs) + 1, coeff1);
  76. }
  77. {
  78. // Scan for eob
  79. __m128i zero_coeff0, zero_coeff1;
  80. __m128i nzero_coeff0, nzero_coeff1;
  81. __m128i iscan0, iscan1;
  82. __m128i eob1;
  83. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  84. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  85. nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
  86. nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
  87. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs));
  88. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs) + 1);
  89. // Add one to convert from indices to counts
  90. iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
  91. iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
  92. eob = _mm_and_si128(iscan0, nzero_coeff0);
  93. eob1 = _mm_and_si128(iscan1, nzero_coeff1);
  94. eob = _mm_max_epi16(eob, eob1);
  95. }
  96. n_coeffs += 8 * 2;
  97. }
  98. thr = _mm_srai_epi16(dequant, 1);
  99. // AC only loop
  100. while (n_coeffs < 0) {
  101. __m128i coeff0, coeff1;
  102. {
  103. __m128i coeff0_sign, coeff1_sign;
  104. __m128i qcoeff0, qcoeff1;
  105. __m128i qtmp0, qtmp1;
  106. coeff0 = _mm_load_si128((const __m128i *)(coeff_ptr + n_coeffs));
  107. coeff1 = _mm_load_si128((const __m128i *)(coeff_ptr + n_coeffs) + 1);
  108. // Poor man's sign extract
  109. coeff0_sign = _mm_srai_epi16(coeff0, 15);
  110. coeff1_sign = _mm_srai_epi16(coeff1, 15);
  111. qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
  112. qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
  113. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  114. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  115. nzflag = _mm_movemask_epi8(_mm_cmpgt_epi16(qcoeff0, thr)) |
  116. _mm_movemask_epi8(_mm_cmpgt_epi16(qcoeff1, thr));
  117. if (nzflag) {
  118. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  119. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  120. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  121. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  122. // Reinsert signs
  123. qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
  124. qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
  125. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  126. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  127. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs), qcoeff0);
  128. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs) + 1, qcoeff1);
  129. coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
  130. coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
  131. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs), coeff0);
  132. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs) + 1, coeff1);
  133. } else {
  134. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs), zero);
  135. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs) + 1, zero);
  136. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs), zero);
  137. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs) + 1, zero);
  138. }
  139. }
  140. if (nzflag) {
  141. // Scan for eob
  142. __m128i zero_coeff0, zero_coeff1;
  143. __m128i nzero_coeff0, nzero_coeff1;
  144. __m128i iscan0, iscan1;
  145. __m128i eob0, eob1;
  146. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  147. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  148. nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
  149. nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
  150. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs));
  151. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs) + 1);
  152. // Add one to convert from indices to counts
  153. iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
  154. iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
  155. eob0 = _mm_and_si128(iscan0, nzero_coeff0);
  156. eob1 = _mm_and_si128(iscan1, nzero_coeff1);
  157. eob0 = _mm_max_epi16(eob0, eob1);
  158. eob = _mm_max_epi16(eob, eob0);
  159. }
  160. n_coeffs += 8 * 2;
  161. }
  162. // Accumulate EOB
  163. {
  164. __m128i eob_shuffled;
  165. eob_shuffled = _mm_shuffle_epi32(eob, 0xe);
  166. eob = _mm_max_epi16(eob, eob_shuffled);
  167. eob_shuffled = _mm_shufflelo_epi16(eob, 0xe);
  168. eob = _mm_max_epi16(eob, eob_shuffled);
  169. eob_shuffled = _mm_shufflelo_epi16(eob, 0x1);
  170. eob = _mm_max_epi16(eob, eob_shuffled);
  171. *eob_ptr = _mm_extract_epi16(eob, 1);
  172. }
  173. } else {
  174. do {
  175. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs), zero);
  176. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs) + 1, zero);
  177. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs), zero);
  178. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs) + 1, zero);
  179. n_coeffs += 8 * 2;
  180. } while (n_coeffs < 0);
  181. *eob_ptr = 0;
  182. }
  183. }