quantize_neon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <arm_neon.h>
  11. #include <assert.h>
  12. #include "./vpx_config.h"
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "vpx_dsp/arm/mem_neon.h"
  15. void vpx_quantize_b_neon(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, tran_low_t *qcoeff_ptr,
  19. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
  20. uint16_t *eob_ptr, const int16_t *scan_ptr,
  21. const int16_t *iscan_ptr) {
  22. const int16x8_t one = vdupq_n_s16(1);
  23. const int16x8_t neg_one = vdupq_n_s16(-1);
  24. uint16x8_t eob_max;
  25. (void)scan_ptr;
  26. (void)skip_block;
  27. assert(!skip_block);
  28. // Process first 8 values which include a dc component.
  29. {
  30. // Only the first element of each vector is DC.
  31. const int16x8_t zbin = vld1q_s16(zbin_ptr);
  32. const int16x8_t round = vld1q_s16(round_ptr);
  33. const int16x8_t quant = vld1q_s16(quant_ptr);
  34. const int16x8_t quant_shift = vld1q_s16(quant_shift_ptr);
  35. const int16x8_t dequant = vld1q_s16(dequant_ptr);
  36. // Add one because the eob does not index from 0.
  37. const uint16x8_t iscan =
  38. vreinterpretq_u16_s16(vaddq_s16(vld1q_s16(iscan_ptr), one));
  39. const int16x8_t coeff = load_tran_low_to_s16q(coeff_ptr);
  40. const int16x8_t coeff_sign = vshrq_n_s16(coeff, 15);
  41. const int16x8_t coeff_abs = vabsq_s16(coeff);
  42. const int16x8_t zbin_mask =
  43. vreinterpretq_s16_u16(vcgeq_s16(coeff_abs, zbin));
  44. const int16x8_t rounded = vqaddq_s16(coeff_abs, round);
  45. // (round * quant * 2) >> 16 >> 1 == (round * quant) >> 16
  46. int16x8_t qcoeff = vshrq_n_s16(vqdmulhq_s16(rounded, quant), 1);
  47. qcoeff = vaddq_s16(qcoeff, rounded);
  48. // (qcoeff * quant_shift * 2) >> 16 >> 1 == (qcoeff * quant_shift) >> 16
  49. qcoeff = vshrq_n_s16(vqdmulhq_s16(qcoeff, quant_shift), 1);
  50. // Restore the sign bit.
  51. qcoeff = veorq_s16(qcoeff, coeff_sign);
  52. qcoeff = vsubq_s16(qcoeff, coeff_sign);
  53. qcoeff = vandq_s16(qcoeff, zbin_mask);
  54. // Set non-zero elements to -1 and use that to extract values for eob.
  55. eob_max = vandq_u16(vtstq_s16(qcoeff, neg_one), iscan);
  56. coeff_ptr += 8;
  57. iscan_ptr += 8;
  58. store_s16q_to_tran_low(qcoeff_ptr, qcoeff);
  59. qcoeff_ptr += 8;
  60. qcoeff = vmulq_s16(qcoeff, dequant);
  61. store_s16q_to_tran_low(dqcoeff_ptr, qcoeff);
  62. dqcoeff_ptr += 8;
  63. }
  64. n_coeffs -= 8;
  65. {
  66. const int16x8_t zbin = vdupq_n_s16(zbin_ptr[1]);
  67. const int16x8_t round = vdupq_n_s16(round_ptr[1]);
  68. const int16x8_t quant = vdupq_n_s16(quant_ptr[1]);
  69. const int16x8_t quant_shift = vdupq_n_s16(quant_shift_ptr[1]);
  70. const int16x8_t dequant = vdupq_n_s16(dequant_ptr[1]);
  71. do {
  72. // Add one because the eob is not its index.
  73. const uint16x8_t iscan =
  74. vreinterpretq_u16_s16(vaddq_s16(vld1q_s16(iscan_ptr), one));
  75. const int16x8_t coeff = load_tran_low_to_s16q(coeff_ptr);
  76. const int16x8_t coeff_sign = vshrq_n_s16(coeff, 15);
  77. const int16x8_t coeff_abs = vabsq_s16(coeff);
  78. const int16x8_t zbin_mask =
  79. vreinterpretq_s16_u16(vcgeq_s16(coeff_abs, zbin));
  80. const int16x8_t rounded = vqaddq_s16(coeff_abs, round);
  81. // (round * quant * 2) >> 16 >> 1 == (round * quant) >> 16
  82. int16x8_t qcoeff = vshrq_n_s16(vqdmulhq_s16(rounded, quant), 1);
  83. qcoeff = vaddq_s16(qcoeff, rounded);
  84. // (qcoeff * quant_shift * 2) >> 16 >> 1 == (qcoeff * quant_shift) >> 16
  85. qcoeff = vshrq_n_s16(vqdmulhq_s16(qcoeff, quant_shift), 1);
  86. // Restore the sign bit.
  87. qcoeff = veorq_s16(qcoeff, coeff_sign);
  88. qcoeff = vsubq_s16(qcoeff, coeff_sign);
  89. qcoeff = vandq_s16(qcoeff, zbin_mask);
  90. // Set non-zero elements to -1 and use that to extract values for eob.
  91. eob_max =
  92. vmaxq_u16(eob_max, vandq_u16(vtstq_s16(qcoeff, neg_one), iscan));
  93. coeff_ptr += 8;
  94. iscan_ptr += 8;
  95. store_s16q_to_tran_low(qcoeff_ptr, qcoeff);
  96. qcoeff_ptr += 8;
  97. qcoeff = vmulq_s16(qcoeff, dequant);
  98. store_s16q_to_tran_low(dqcoeff_ptr, qcoeff);
  99. dqcoeff_ptr += 8;
  100. n_coeffs -= 8;
  101. } while (n_coeffs > 0);
  102. }
  103. {
  104. const uint16x4_t eob_max_0 =
  105. vmax_u16(vget_low_u16(eob_max), vget_high_u16(eob_max));
  106. const uint16x4_t eob_max_1 = vpmax_u16(eob_max_0, eob_max_0);
  107. const uint16x4_t eob_max_2 = vpmax_u16(eob_max_1, eob_max_1);
  108. vst1_lane_u16(eob_ptr, eob_max_2, 0);
  109. }
  110. }
  111. static INLINE int32x4_t extract_sign_bit(int32x4_t a) {
  112. return vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_s32(a), 31));
  113. }
  114. // Main difference is that zbin values are halved before comparison and dqcoeff
  115. // values are divided by 2. zbin is rounded but dqcoeff is not.
  116. void vpx_quantize_b_32x32_neon(
  117. const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
  118. const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr,
  119. const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
  120. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
  121. const int16_t *scan_ptr, const int16_t *iscan_ptr) {
  122. const int16x8_t one = vdupq_n_s16(1);
  123. const int16x8_t neg_one = vdupq_n_s16(-1);
  124. uint16x8_t eob_max;
  125. int i;
  126. (void)scan_ptr;
  127. (void)n_coeffs; // Because we will always calculate 32*32.
  128. (void)skip_block;
  129. assert(!skip_block);
  130. // Process first 8 values which include a dc component.
  131. {
  132. // Only the first element of each vector is DC.
  133. const int16x8_t zbin = vrshrq_n_s16(vld1q_s16(zbin_ptr), 1);
  134. const int16x8_t round = vrshrq_n_s16(vld1q_s16(round_ptr), 1);
  135. const int16x8_t quant = vld1q_s16(quant_ptr);
  136. const int16x8_t quant_shift = vld1q_s16(quant_shift_ptr);
  137. const int16x8_t dequant = vld1q_s16(dequant_ptr);
  138. // Add one because the eob does not index from 0.
  139. const uint16x8_t iscan =
  140. vreinterpretq_u16_s16(vaddq_s16(vld1q_s16(iscan_ptr), one));
  141. const int16x8_t coeff = load_tran_low_to_s16q(coeff_ptr);
  142. const int16x8_t coeff_sign = vshrq_n_s16(coeff, 15);
  143. const int16x8_t coeff_abs = vabsq_s16(coeff);
  144. const int16x8_t zbin_mask =
  145. vreinterpretq_s16_u16(vcgeq_s16(coeff_abs, zbin));
  146. const int16x8_t rounded = vqaddq_s16(coeff_abs, round);
  147. // (round * quant * 2) >> 16 >> 1 == (round * quant) >> 16
  148. int16x8_t qcoeff = vshrq_n_s16(vqdmulhq_s16(rounded, quant), 1);
  149. int16x8_t dqcoeff;
  150. int32x4_t dqcoeff_0, dqcoeff_1;
  151. qcoeff = vaddq_s16(qcoeff, rounded);
  152. // (qcoeff * quant_shift * 2) >> 16 == (qcoeff * quant_shift) >> 15
  153. qcoeff = vqdmulhq_s16(qcoeff, quant_shift);
  154. // Restore the sign bit.
  155. qcoeff = veorq_s16(qcoeff, coeff_sign);
  156. qcoeff = vsubq_s16(qcoeff, coeff_sign);
  157. qcoeff = vandq_s16(qcoeff, zbin_mask);
  158. // Set non-zero elements to -1 and use that to extract values for eob.
  159. eob_max = vandq_u16(vtstq_s16(qcoeff, neg_one), iscan);
  160. coeff_ptr += 8;
  161. iscan_ptr += 8;
  162. store_s16q_to_tran_low(qcoeff_ptr, qcoeff);
  163. qcoeff_ptr += 8;
  164. dqcoeff_0 = vmull_s16(vget_low_s16(qcoeff), vget_low_s16(dequant));
  165. dqcoeff_1 = vmull_s16(vget_high_s16(qcoeff), vget_high_s16(dequant));
  166. // Add 1 if negative to round towards zero because the C uses division.
  167. dqcoeff_0 = vaddq_s32(dqcoeff_0, extract_sign_bit(dqcoeff_0));
  168. dqcoeff_1 = vaddq_s32(dqcoeff_1, extract_sign_bit(dqcoeff_1));
  169. dqcoeff =
  170. vcombine_s16(vshrn_n_s32(dqcoeff_0, 1), vshrn_n_s32(dqcoeff_1, 1));
  171. store_s16q_to_tran_low(dqcoeff_ptr, dqcoeff);
  172. dqcoeff_ptr += 8;
  173. }
  174. {
  175. const int16x8_t zbin = vrshrq_n_s16(vdupq_n_s16(zbin_ptr[1]), 1);
  176. const int16x8_t round = vrshrq_n_s16(vdupq_n_s16(round_ptr[1]), 1);
  177. const int16x8_t quant = vdupq_n_s16(quant_ptr[1]);
  178. const int16x8_t quant_shift = vdupq_n_s16(quant_shift_ptr[1]);
  179. const int16x8_t dequant = vdupq_n_s16(dequant_ptr[1]);
  180. for (i = 1; i < 32 * 32 / 8; ++i) {
  181. // Add one because the eob is not its index.
  182. const uint16x8_t iscan =
  183. vreinterpretq_u16_s16(vaddq_s16(vld1q_s16(iscan_ptr), one));
  184. const int16x8_t coeff = load_tran_low_to_s16q(coeff_ptr);
  185. const int16x8_t coeff_sign = vshrq_n_s16(coeff, 15);
  186. const int16x8_t coeff_abs = vabsq_s16(coeff);
  187. const int16x8_t zbin_mask =
  188. vreinterpretq_s16_u16(vcgeq_s16(coeff_abs, zbin));
  189. const int16x8_t rounded = vqaddq_s16(coeff_abs, round);
  190. // (round * quant * 2) >> 16 >> 1 == (round * quant) >> 16
  191. int16x8_t qcoeff = vshrq_n_s16(vqdmulhq_s16(rounded, quant), 1);
  192. int16x8_t dqcoeff;
  193. int32x4_t dqcoeff_0, dqcoeff_1;
  194. qcoeff = vaddq_s16(qcoeff, rounded);
  195. // (qcoeff * quant_shift * 2) >> 16 == (qcoeff * quant_shift) >> 15
  196. qcoeff = vqdmulhq_s16(qcoeff, quant_shift);
  197. // Restore the sign bit.
  198. qcoeff = veorq_s16(qcoeff, coeff_sign);
  199. qcoeff = vsubq_s16(qcoeff, coeff_sign);
  200. qcoeff = vandq_s16(qcoeff, zbin_mask);
  201. // Set non-zero elements to -1 and use that to extract values for eob.
  202. eob_max =
  203. vmaxq_u16(eob_max, vandq_u16(vtstq_s16(qcoeff, neg_one), iscan));
  204. coeff_ptr += 8;
  205. iscan_ptr += 8;
  206. store_s16q_to_tran_low(qcoeff_ptr, qcoeff);
  207. qcoeff_ptr += 8;
  208. dqcoeff_0 = vmull_s16(vget_low_s16(qcoeff), vget_low_s16(dequant));
  209. dqcoeff_1 = vmull_s16(vget_high_s16(qcoeff), vget_high_s16(dequant));
  210. dqcoeff_0 = vaddq_s32(dqcoeff_0, extract_sign_bit(dqcoeff_0));
  211. dqcoeff_1 = vaddq_s32(dqcoeff_1, extract_sign_bit(dqcoeff_1));
  212. dqcoeff =
  213. vcombine_s16(vshrn_n_s32(dqcoeff_0, 1), vshrn_n_s32(dqcoeff_1, 1));
  214. store_s16q_to_tran_low(dqcoeff_ptr, dqcoeff);
  215. dqcoeff_ptr += 8;
  216. }
  217. }
  218. {
  219. const uint16x4_t eob_max_0 =
  220. vmax_u16(vget_low_u16(eob_max), vget_high_u16(eob_max));
  221. const uint16x4_t eob_max_1 = vpmax_u16(eob_max_0, eob_max_0);
  222. const uint16x4_t eob_max_2 = vpmax_u16(eob_max_1, eob_max_1);
  223. vst1_lane_u16(eob_ptr, eob_max_2, 0);
  224. }
  225. }