quantize_ssse3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. void vpx_quantize_b_ssse3(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  16. int skip_block, const int16_t *zbin_ptr,
  17. const int16_t *round_ptr, const int16_t *quant_ptr,
  18. const int16_t *quant_shift_ptr,
  19. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  20. const int16_t *dequant_ptr, uint16_t *eob_ptr,
  21. const int16_t *scan_ptr, const int16_t *iscan_ptr) {
  22. const __m128i zero = _mm_setzero_si128();
  23. intptr_t index = 16;
  24. __m128i zbin, round, quant, dequant, shift;
  25. __m128i coeff0, coeff1;
  26. __m128i qcoeff0, qcoeff1;
  27. __m128i cmp_mask0, cmp_mask1;
  28. __m128i qtmp0, qtmp1;
  29. __m128i zero_coeff0, zero_coeff1, iscan0, iscan1;
  30. __m128i eob, eob0, eob1;
  31. (void)scan_ptr;
  32. (void)skip_block;
  33. assert(!skip_block);
  34. // Setup global values.
  35. zbin = _mm_load_si128((const __m128i *)zbin_ptr);
  36. // x86 has no "greater *or equal*" comparison. Subtract 1 from zbin so
  37. // it is a strict "greater" comparison.
  38. zbin = _mm_sub_epi16(zbin, _mm_set1_epi16(1));
  39. round = _mm_load_si128((const __m128i *)round_ptr);
  40. quant = _mm_load_si128((const __m128i *)quant_ptr);
  41. dequant = _mm_load_si128((const __m128i *)dequant_ptr);
  42. shift = _mm_load_si128((const __m128i *)quant_shift_ptr);
  43. // Do DC and first 15 AC.
  44. coeff0 = load_tran_low(coeff_ptr);
  45. coeff1 = load_tran_low(coeff_ptr + 8);
  46. qcoeff0 = _mm_abs_epi16(coeff0);
  47. qcoeff1 = _mm_abs_epi16(coeff1);
  48. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  49. zbin = _mm_unpackhi_epi64(zbin, zbin); // Switch DC to AC
  50. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  51. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  52. round = _mm_unpackhi_epi64(round, round);
  53. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  54. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  55. quant = _mm_unpackhi_epi64(quant, quant);
  56. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  57. qtmp0 = _mm_add_epi16(qtmp0, qcoeff0);
  58. qtmp1 = _mm_add_epi16(qtmp1, qcoeff1);
  59. qcoeff0 = _mm_mulhi_epi16(qtmp0, shift);
  60. shift = _mm_unpackhi_epi64(shift, shift);
  61. qcoeff1 = _mm_mulhi_epi16(qtmp1, shift);
  62. // Reinsert signs
  63. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  64. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  65. // Mask out zbin threshold coeffs
  66. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  67. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  68. store_tran_low(qcoeff0, qcoeff_ptr);
  69. store_tran_low(qcoeff1, qcoeff_ptr + 8);
  70. coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
  71. dequant = _mm_unpackhi_epi64(dequant, dequant);
  72. coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
  73. store_tran_low(coeff0, dqcoeff_ptr);
  74. store_tran_low(coeff1, dqcoeff_ptr + 8);
  75. // Scan for eob.
  76. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  77. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  78. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr));
  79. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + 8));
  80. // Add one to convert from indices to counts
  81. iscan0 = _mm_sub_epi16(iscan0, cmp_mask0);
  82. iscan1 = _mm_sub_epi16(iscan1, cmp_mask1);
  83. eob = _mm_andnot_si128(zero_coeff0, iscan0);
  84. eob1 = _mm_andnot_si128(zero_coeff1, iscan1);
  85. eob = _mm_max_epi16(eob, eob1);
  86. // AC only loop.
  87. while (index < n_coeffs) {
  88. coeff0 = load_tran_low(coeff_ptr + index);
  89. coeff1 = load_tran_low(coeff_ptr + index + 8);
  90. qcoeff0 = _mm_abs_epi16(coeff0);
  91. qcoeff1 = _mm_abs_epi16(coeff1);
  92. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  93. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  94. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  95. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  96. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  97. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  98. qtmp0 = _mm_add_epi16(qtmp0, qcoeff0);
  99. qtmp1 = _mm_add_epi16(qtmp1, qcoeff1);
  100. qcoeff0 = _mm_mulhi_epi16(qtmp0, shift);
  101. qcoeff1 = _mm_mulhi_epi16(qtmp1, shift);
  102. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  103. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  104. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  105. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  106. store_tran_low(qcoeff0, qcoeff_ptr + index);
  107. store_tran_low(qcoeff1, qcoeff_ptr + index + 8);
  108. coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
  109. coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
  110. store_tran_low(coeff0, dqcoeff_ptr + index);
  111. store_tran_low(coeff1, dqcoeff_ptr + index + 8);
  112. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  113. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  114. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + index));
  115. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + index + 8));
  116. iscan0 = _mm_sub_epi16(iscan0, cmp_mask0);
  117. iscan1 = _mm_sub_epi16(iscan1, cmp_mask1);
  118. eob0 = _mm_andnot_si128(zero_coeff0, iscan0);
  119. eob1 = _mm_andnot_si128(zero_coeff1, iscan1);
  120. eob0 = _mm_max_epi16(eob0, eob1);
  121. eob = _mm_max_epi16(eob, eob0);
  122. index += 16;
  123. }
  124. // Accumulate eob.
  125. {
  126. __m128i eob_shuffled;
  127. eob_shuffled = _mm_shuffle_epi32(eob, 0xe);
  128. eob = _mm_max_epi16(eob, eob_shuffled);
  129. eob_shuffled = _mm_shufflelo_epi16(eob, 0xe);
  130. eob = _mm_max_epi16(eob, eob_shuffled);
  131. eob_shuffled = _mm_shufflelo_epi16(eob, 0x1);
  132. eob = _mm_max_epi16(eob, eob_shuffled);
  133. *eob_ptr = _mm_extract_epi16(eob, 1);
  134. }
  135. }
  136. void vpx_quantize_b_32x32_ssse3(
  137. const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
  138. const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr,
  139. const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
  140. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
  141. const int16_t *scan_ptr, const int16_t *iscan_ptr) {
  142. const __m128i zero = _mm_setzero_si128();
  143. const __m128i one = _mm_set1_epi16(1);
  144. intptr_t index = 16;
  145. __m128i zbin, round, quant, dequant, shift;
  146. __m128i coeff0, coeff1;
  147. __m128i qcoeff0, qcoeff1;
  148. __m128i cmp_mask0, cmp_mask1;
  149. __m128i all_zero;
  150. __m128i qtmp0, qtmp1;
  151. __m128i zero_coeff0, zero_coeff1, iscan0, iscan1;
  152. __m128i eob = zero, eob0, eob1;
  153. (void)scan_ptr;
  154. (void)n_coeffs;
  155. (void)skip_block;
  156. assert(!skip_block);
  157. // Setup global values.
  158. // The 32x32 halves zbin and round.
  159. zbin = _mm_load_si128((const __m128i *)zbin_ptr);
  160. // Shift with rounding.
  161. zbin = _mm_add_epi16(zbin, one);
  162. zbin = _mm_srli_epi16(zbin, 1);
  163. // x86 has no "greater *or equal*" comparison. Subtract 1 from zbin so
  164. // it is a strict "greater" comparison.
  165. zbin = _mm_sub_epi16(zbin, one);
  166. round = _mm_load_si128((const __m128i *)round_ptr);
  167. round = _mm_add_epi16(round, one);
  168. round = _mm_srli_epi16(round, 1);
  169. quant = _mm_load_si128((const __m128i *)quant_ptr);
  170. dequant = _mm_load_si128((const __m128i *)dequant_ptr);
  171. shift = _mm_load_si128((const __m128i *)quant_shift_ptr);
  172. // I suspect this is not technically OK because quant_shift can be up
  173. // to 1 << 16 and shifting up again will outrange that, but the test is not
  174. // comprehensive enough to catch that and "it's been that way forever"
  175. shift = _mm_slli_epi16(shift, 1);
  176. // Do DC and first 15 AC.
  177. coeff0 = load_tran_low(coeff_ptr);
  178. coeff1 = load_tran_low(coeff_ptr + 8);
  179. qcoeff0 = _mm_abs_epi16(coeff0);
  180. qcoeff1 = _mm_abs_epi16(coeff1);
  181. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  182. zbin = _mm_unpackhi_epi64(zbin, zbin); // Switch DC to AC.
  183. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  184. all_zero = _mm_or_si128(cmp_mask0, cmp_mask1);
  185. if (_mm_movemask_epi8(all_zero) == 0) {
  186. _mm_store_si128((__m128i *)(qcoeff_ptr), zero);
  187. _mm_store_si128((__m128i *)(qcoeff_ptr + 8), zero);
  188. _mm_store_si128((__m128i *)(dqcoeff_ptr), zero);
  189. _mm_store_si128((__m128i *)(dqcoeff_ptr + 8), zero);
  190. #if CONFIG_VP9_HIGHBITDEPTH
  191. _mm_store_si128((__m128i *)(qcoeff_ptr + 4), zero);
  192. _mm_store_si128((__m128i *)(qcoeff_ptr + 12), zero);
  193. _mm_store_si128((__m128i *)(dqcoeff_ptr + 4), zero);
  194. _mm_store_si128((__m128i *)(dqcoeff_ptr + 12), zero);
  195. #endif
  196. round = _mm_unpackhi_epi64(round, round);
  197. quant = _mm_unpackhi_epi64(quant, quant);
  198. shift = _mm_unpackhi_epi64(shift, shift);
  199. dequant = _mm_unpackhi_epi64(dequant, dequant);
  200. } else {
  201. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  202. round = _mm_unpackhi_epi64(round, round);
  203. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  204. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  205. quant = _mm_unpackhi_epi64(quant, quant);
  206. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  207. qtmp0 = _mm_add_epi16(qtmp0, qcoeff0);
  208. qtmp1 = _mm_add_epi16(qtmp1, qcoeff1);
  209. qcoeff0 = _mm_mulhi_epi16(qtmp0, shift);
  210. shift = _mm_unpackhi_epi64(shift, shift);
  211. qcoeff1 = _mm_mulhi_epi16(qtmp1, shift);
  212. // Reinsert signs.
  213. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  214. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  215. // Mask out zbin threshold coeffs.
  216. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  217. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  218. store_tran_low(qcoeff0, qcoeff_ptr);
  219. store_tran_low(qcoeff1, qcoeff_ptr + 8);
  220. // Un-sign to bias rounding like C.
  221. // dequant is almost always negative, so this is probably the backwards way
  222. // to handle the sign. However, it matches the previous assembly.
  223. coeff0 = _mm_abs_epi16(qcoeff0);
  224. coeff1 = _mm_abs_epi16(qcoeff1);
  225. coeff0 = _mm_mullo_epi16(coeff0, dequant);
  226. dequant = _mm_unpackhi_epi64(dequant, dequant);
  227. coeff1 = _mm_mullo_epi16(coeff1, dequant);
  228. // "Divide" by 2.
  229. coeff0 = _mm_srli_epi16(coeff0, 1);
  230. coeff1 = _mm_srli_epi16(coeff1, 1);
  231. coeff0 = _mm_sign_epi16(coeff0, qcoeff0);
  232. coeff1 = _mm_sign_epi16(coeff1, qcoeff1);
  233. store_tran_low(coeff0, dqcoeff_ptr);
  234. store_tran_low(coeff1, dqcoeff_ptr + 8);
  235. // Scan for eob.
  236. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  237. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  238. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr));
  239. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + 8));
  240. // Add one to convert from indices to counts.
  241. iscan0 = _mm_sub_epi16(iscan0, cmp_mask0);
  242. iscan1 = _mm_sub_epi16(iscan1, cmp_mask1);
  243. eob = _mm_andnot_si128(zero_coeff0, iscan0);
  244. eob1 = _mm_andnot_si128(zero_coeff1, iscan1);
  245. eob = _mm_max_epi16(eob, eob1);
  246. }
  247. // AC only loop.
  248. for (index = 16; index < 32 * 32; index += 16) {
  249. coeff0 = load_tran_low(coeff_ptr + index);
  250. coeff1 = load_tran_low(coeff_ptr + index + 8);
  251. qcoeff0 = _mm_abs_epi16(coeff0);
  252. qcoeff1 = _mm_abs_epi16(coeff1);
  253. cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
  254. cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
  255. all_zero = _mm_or_si128(cmp_mask0, cmp_mask1);
  256. if (_mm_movemask_epi8(all_zero) == 0) {
  257. _mm_store_si128((__m128i *)(qcoeff_ptr + index), zero);
  258. _mm_store_si128((__m128i *)(qcoeff_ptr + index + 8), zero);
  259. _mm_store_si128((__m128i *)(dqcoeff_ptr + index), zero);
  260. _mm_store_si128((__m128i *)(dqcoeff_ptr + index + 8), zero);
  261. #if CONFIG_VP9_HIGHBITDEPTH
  262. _mm_store_si128((__m128i *)(qcoeff_ptr + index + 4), zero);
  263. _mm_store_si128((__m128i *)(qcoeff_ptr + index + 12), zero);
  264. _mm_store_si128((__m128i *)(dqcoeff_ptr + index + 4), zero);
  265. _mm_store_si128((__m128i *)(dqcoeff_ptr + index + 12), zero);
  266. #endif
  267. continue;
  268. }
  269. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  270. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  271. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  272. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  273. qtmp0 = _mm_add_epi16(qtmp0, qcoeff0);
  274. qtmp1 = _mm_add_epi16(qtmp1, qcoeff1);
  275. qcoeff0 = _mm_mulhi_epi16(qtmp0, shift);
  276. qcoeff1 = _mm_mulhi_epi16(qtmp1, shift);
  277. qcoeff0 = _mm_sign_epi16(qcoeff0, coeff0);
  278. qcoeff1 = _mm_sign_epi16(qcoeff1, coeff1);
  279. qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
  280. qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
  281. store_tran_low(qcoeff0, qcoeff_ptr + index);
  282. store_tran_low(qcoeff1, qcoeff_ptr + index + 8);
  283. coeff0 = _mm_abs_epi16(qcoeff0);
  284. coeff1 = _mm_abs_epi16(qcoeff1);
  285. coeff0 = _mm_mullo_epi16(coeff0, dequant);
  286. coeff1 = _mm_mullo_epi16(coeff1, dequant);
  287. coeff0 = _mm_srli_epi16(coeff0, 1);
  288. coeff1 = _mm_srli_epi16(coeff1, 1);
  289. coeff0 = _mm_sign_epi16(coeff0, qcoeff0);
  290. coeff1 = _mm_sign_epi16(coeff1, qcoeff1);
  291. store_tran_low(coeff0, dqcoeff_ptr + index);
  292. store_tran_low(coeff1, dqcoeff_ptr + index + 8);
  293. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  294. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  295. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + index));
  296. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + index + 8));
  297. iscan0 = _mm_sub_epi16(iscan0, cmp_mask0);
  298. iscan1 = _mm_sub_epi16(iscan1, cmp_mask1);
  299. eob0 = _mm_andnot_si128(zero_coeff0, iscan0);
  300. eob1 = _mm_andnot_si128(zero_coeff1, iscan1);
  301. eob0 = _mm_max_epi16(eob0, eob1);
  302. eob = _mm_max_epi16(eob, eob0);
  303. }
  304. {
  305. __m128i eob_shuffled;
  306. eob_shuffled = _mm_shuffle_epi32(eob, 0xe);
  307. eob = _mm_max_epi16(eob, eob_shuffled);
  308. eob_shuffled = _mm_shufflelo_epi16(eob, 0xe);
  309. eob = _mm_max_epi16(eob, eob_shuffled);
  310. eob_shuffled = _mm_shufflelo_epi16(eob, 0x1);
  311. eob = _mm_max_epi16(eob, eob_shuffled);
  312. *eob_ptr = _mm_extract_epi16(eob, 1);
  313. }
  314. }