avg_neon.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <arm_neon.h>
  11. #include <assert.h>
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "./vpx_config.h"
  14. #include "vpx/vpx_integer.h"
  15. #include "vpx_dsp/arm/idct_neon.h"
  16. #include "vpx_dsp/arm/mem_neon.h"
  17. #include "vpx_dsp/arm/sum_neon.h"
  18. uint32_t vpx_avg_4x4_neon(const uint8_t *a, int a_stride) {
  19. const uint8x16_t b = load_unaligned_u8q(a, a_stride);
  20. const uint16x8_t c = vaddl_u8(vget_low_u8(b), vget_high_u8(b));
  21. const uint32x2_t d = horizontal_add_uint16x8(c);
  22. return vget_lane_u32(vrshr_n_u32(d, 4), 0);
  23. }
  24. uint32_t vpx_avg_8x8_neon(const uint8_t *a, int a_stride) {
  25. int i;
  26. uint8x8_t b, c;
  27. uint16x8_t sum;
  28. uint32x2_t d;
  29. b = vld1_u8(a);
  30. a += a_stride;
  31. c = vld1_u8(a);
  32. a += a_stride;
  33. sum = vaddl_u8(b, c);
  34. for (i = 0; i < 6; ++i) {
  35. const uint8x8_t d = vld1_u8(a);
  36. a += a_stride;
  37. sum = vaddw_u8(sum, d);
  38. }
  39. d = horizontal_add_uint16x8(sum);
  40. return vget_lane_u32(vrshr_n_u32(d, 6), 0);
  41. }
  42. // coeff: 16 bits, dynamic range [-32640, 32640].
  43. // length: value range {16, 64, 256, 1024}.
  44. int vpx_satd_neon(const tran_low_t *coeff, int length) {
  45. const int16x4_t zero = vdup_n_s16(0);
  46. int32x4_t accum = vdupq_n_s32(0);
  47. do {
  48. const int16x8_t src0 = load_tran_low_to_s16q(coeff);
  49. const int16x8_t src8 = load_tran_low_to_s16q(coeff + 8);
  50. accum = vabal_s16(accum, vget_low_s16(src0), zero);
  51. accum = vabal_s16(accum, vget_high_s16(src0), zero);
  52. accum = vabal_s16(accum, vget_low_s16(src8), zero);
  53. accum = vabal_s16(accum, vget_high_s16(src8), zero);
  54. length -= 16;
  55. coeff += 16;
  56. } while (length != 0);
  57. {
  58. // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
  59. const int64x2_t s0 = vpaddlq_s32(accum); // cascading summation of 'accum'.
  60. const int32x2_t s1 = vadd_s32(vreinterpret_s32_s64(vget_low_s64(s0)),
  61. vreinterpret_s32_s64(vget_high_s64(s0)));
  62. const int satd = vget_lane_s32(s1, 0);
  63. return satd;
  64. }
  65. }
  66. void vpx_int_pro_row_neon(int16_t hbuf[16], uint8_t const *ref,
  67. const int ref_stride, const int height) {
  68. int i;
  69. uint16x8_t vec_sum_lo = vdupq_n_u16(0);
  70. uint16x8_t vec_sum_hi = vdupq_n_u16(0);
  71. const int shift_factor = ((height >> 5) + 3) * -1;
  72. const int16x8_t vec_shift = vdupq_n_s16(shift_factor);
  73. for (i = 0; i < height; i += 8) {
  74. const uint8x16_t vec_row1 = vld1q_u8(ref);
  75. const uint8x16_t vec_row2 = vld1q_u8(ref + ref_stride);
  76. const uint8x16_t vec_row3 = vld1q_u8(ref + ref_stride * 2);
  77. const uint8x16_t vec_row4 = vld1q_u8(ref + ref_stride * 3);
  78. const uint8x16_t vec_row5 = vld1q_u8(ref + ref_stride * 4);
  79. const uint8x16_t vec_row6 = vld1q_u8(ref + ref_stride * 5);
  80. const uint8x16_t vec_row7 = vld1q_u8(ref + ref_stride * 6);
  81. const uint8x16_t vec_row8 = vld1q_u8(ref + ref_stride * 7);
  82. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row1));
  83. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row1));
  84. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row2));
  85. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row2));
  86. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row3));
  87. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row3));
  88. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row4));
  89. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row4));
  90. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row5));
  91. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row5));
  92. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row6));
  93. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row6));
  94. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row7));
  95. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row7));
  96. vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row8));
  97. vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row8));
  98. ref += ref_stride * 8;
  99. }
  100. vec_sum_lo = vshlq_u16(vec_sum_lo, vec_shift);
  101. vec_sum_hi = vshlq_u16(vec_sum_hi, vec_shift);
  102. vst1q_s16(hbuf, vreinterpretq_s16_u16(vec_sum_lo));
  103. hbuf += 8;
  104. vst1q_s16(hbuf, vreinterpretq_s16_u16(vec_sum_hi));
  105. }
  106. int16_t vpx_int_pro_col_neon(uint8_t const *ref, const int width) {
  107. int i;
  108. uint16x8_t vec_sum = vdupq_n_u16(0);
  109. for (i = 0; i < width; i += 16) {
  110. const uint8x16_t vec_row = vld1q_u8(ref);
  111. vec_sum = vaddw_u8(vec_sum, vget_low_u8(vec_row));
  112. vec_sum = vaddw_u8(vec_sum, vget_high_u8(vec_row));
  113. ref += 16;
  114. }
  115. return vget_lane_s16(vreinterpret_s16_u32(horizontal_add_uint16x8(vec_sum)),
  116. 0);
  117. }
  118. // ref, src = [0, 510] - max diff = 16-bits
  119. // bwl = {2, 3, 4}, width = {16, 32, 64}
  120. int vpx_vector_var_neon(int16_t const *ref, int16_t const *src, const int bwl) {
  121. int width = 4 << bwl;
  122. int32x4_t sse = vdupq_n_s32(0);
  123. int16x8_t total = vdupq_n_s16(0);
  124. assert(width >= 8);
  125. assert((width % 8) == 0);
  126. do {
  127. const int16x8_t r = vld1q_s16(ref);
  128. const int16x8_t s = vld1q_s16(src);
  129. const int16x8_t diff = vsubq_s16(r, s); // [-510, 510], 10 bits.
  130. const int16x4_t diff_lo = vget_low_s16(diff);
  131. const int16x4_t diff_hi = vget_high_s16(diff);
  132. sse = vmlal_s16(sse, diff_lo, diff_lo); // dynamic range 26 bits.
  133. sse = vmlal_s16(sse, diff_hi, diff_hi);
  134. total = vaddq_s16(total, diff); // dynamic range 16 bits.
  135. ref += 8;
  136. src += 8;
  137. width -= 8;
  138. } while (width != 0);
  139. {
  140. // Note: 'total''s pairwise addition could be implemented similarly to
  141. // horizontal_add_uint16x8(), but one less vpaddl with 'total' when paired
  142. // with the summation of 'sse' performed better on a Cortex-A15.
  143. const int32x4_t t0 = vpaddlq_s16(total); // cascading summation of 'total'
  144. const int32x2_t t1 = vadd_s32(vget_low_s32(t0), vget_high_s32(t0));
  145. const int32x2_t t2 = vpadd_s32(t1, t1);
  146. const int t = vget_lane_s32(t2, 0);
  147. const int64x2_t s0 = vpaddlq_s32(sse); // cascading summation of 'sse'.
  148. const int32x2_t s1 = vadd_s32(vreinterpret_s32_s64(vget_low_s64(s0)),
  149. vreinterpret_s32_s64(vget_high_s64(s0)));
  150. const int s = vget_lane_s32(s1, 0);
  151. const int shift_factor = bwl + 2;
  152. return s - ((t * t) >> shift_factor);
  153. }
  154. }
  155. void vpx_minmax_8x8_neon(const uint8_t *a, int a_stride, const uint8_t *b,
  156. int b_stride, int *min, int *max) {
  157. // Load and concatenate.
  158. const uint8x16_t a01 = vcombine_u8(vld1_u8(a), vld1_u8(a + a_stride));
  159. const uint8x16_t a23 =
  160. vcombine_u8(vld1_u8(a + 2 * a_stride), vld1_u8(a + 3 * a_stride));
  161. const uint8x16_t a45 =
  162. vcombine_u8(vld1_u8(a + 4 * a_stride), vld1_u8(a + 5 * a_stride));
  163. const uint8x16_t a67 =
  164. vcombine_u8(vld1_u8(a + 6 * a_stride), vld1_u8(a + 7 * a_stride));
  165. const uint8x16_t b01 = vcombine_u8(vld1_u8(b), vld1_u8(b + b_stride));
  166. const uint8x16_t b23 =
  167. vcombine_u8(vld1_u8(b + 2 * b_stride), vld1_u8(b + 3 * b_stride));
  168. const uint8x16_t b45 =
  169. vcombine_u8(vld1_u8(b + 4 * b_stride), vld1_u8(b + 5 * b_stride));
  170. const uint8x16_t b67 =
  171. vcombine_u8(vld1_u8(b + 6 * b_stride), vld1_u8(b + 7 * b_stride));
  172. // Absolute difference.
  173. const uint8x16_t ab01_diff = vabdq_u8(a01, b01);
  174. const uint8x16_t ab23_diff = vabdq_u8(a23, b23);
  175. const uint8x16_t ab45_diff = vabdq_u8(a45, b45);
  176. const uint8x16_t ab67_diff = vabdq_u8(a67, b67);
  177. // Max values between the Q vectors.
  178. const uint8x16_t ab0123_max = vmaxq_u8(ab01_diff, ab23_diff);
  179. const uint8x16_t ab4567_max = vmaxq_u8(ab45_diff, ab67_diff);
  180. const uint8x16_t ab0123_min = vminq_u8(ab01_diff, ab23_diff);
  181. const uint8x16_t ab4567_min = vminq_u8(ab45_diff, ab67_diff);
  182. const uint8x16_t ab07_max = vmaxq_u8(ab0123_max, ab4567_max);
  183. const uint8x16_t ab07_min = vminq_u8(ab0123_min, ab4567_min);
  184. // Split to D and start doing pairwise.
  185. uint8x8_t ab_max = vmax_u8(vget_high_u8(ab07_max), vget_low_u8(ab07_max));
  186. uint8x8_t ab_min = vmin_u8(vget_high_u8(ab07_min), vget_low_u8(ab07_min));
  187. // Enough runs of vpmax/min propogate the max/min values to every position.
  188. ab_max = vpmax_u8(ab_max, ab_max);
  189. ab_min = vpmin_u8(ab_min, ab_min);
  190. ab_max = vpmax_u8(ab_max, ab_max);
  191. ab_min = vpmin_u8(ab_min, ab_min);
  192. ab_max = vpmax_u8(ab_max, ab_max);
  193. ab_min = vpmin_u8(ab_min, ab_min);
  194. *min = *max = 0; // Clear high bits
  195. // Store directly to avoid costly neon->gpr transfer.
  196. vst1_lane_u8((uint8_t *)max, ab_max, 0);
  197. vst1_lane_u8((uint8_t *)min, ab_min, 0);
  198. }