variance_neon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <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/mem_neon.h"
  16. #include "vpx_dsp/arm/sum_neon.h"
  17. #include "vpx_ports/mem.h"
  18. // The variance helper functions use int16_t for sum. 8 values are accumulated
  19. // and then added (at which point they expand up to int32_t). To avoid overflow,
  20. // there can be no more than 32767 / 255 ~= 128 values accumulated in each
  21. // column. For a 32x32 buffer, this results in 32 / 8 = 4 values per row * 32
  22. // rows = 128. Asserts have been added to each function to warn against reaching
  23. // this limit.
  24. // Process a block of width 4 four rows at a time.
  25. static void variance_neon_w4x4(const uint8_t *src_ptr, int src_stride,
  26. const uint8_t *ref_ptr, int ref_stride, int h,
  27. uint32_t *sse, int *sum) {
  28. int i;
  29. int16x8_t sum_s16 = vdupq_n_s16(0);
  30. int32x4_t sse_lo_s32 = vdupq_n_s32(0);
  31. int32x4_t sse_hi_s32 = vdupq_n_s32(0);
  32. // Since width is only 4, sum_s16 only loads a half row per loop.
  33. assert(h <= 256);
  34. for (i = 0; i < h; i += 4) {
  35. const uint8x16_t a_u8 = load_unaligned_u8q(src_ptr, src_stride);
  36. const uint8x16_t b_u8 = load_unaligned_u8q(ref_ptr, ref_stride);
  37. const uint16x8_t diff_lo_u16 =
  38. vsubl_u8(vget_low_u8(a_u8), vget_low_u8(b_u8));
  39. const uint16x8_t diff_hi_u16 =
  40. vsubl_u8(vget_high_u8(a_u8), vget_high_u8(b_u8));
  41. const int16x8_t diff_lo_s16 = vreinterpretq_s16_u16(diff_lo_u16);
  42. const int16x8_t diff_hi_s16 = vreinterpretq_s16_u16(diff_hi_u16);
  43. sum_s16 = vaddq_s16(sum_s16, diff_lo_s16);
  44. sum_s16 = vaddq_s16(sum_s16, diff_hi_s16);
  45. sse_lo_s32 = vmlal_s16(sse_lo_s32, vget_low_s16(diff_lo_s16),
  46. vget_low_s16(diff_lo_s16));
  47. sse_lo_s32 = vmlal_s16(sse_lo_s32, vget_high_s16(diff_lo_s16),
  48. vget_high_s16(diff_lo_s16));
  49. sse_hi_s32 = vmlal_s16(sse_hi_s32, vget_low_s16(diff_hi_s16),
  50. vget_low_s16(diff_hi_s16));
  51. sse_hi_s32 = vmlal_s16(sse_hi_s32, vget_high_s16(diff_hi_s16),
  52. vget_high_s16(diff_hi_s16));
  53. src_ptr += 4 * src_stride;
  54. ref_ptr += 4 * ref_stride;
  55. }
  56. *sum = vget_lane_s32(horizontal_add_int16x8(sum_s16), 0);
  57. *sse = vget_lane_u32(horizontal_add_uint32x4(vreinterpretq_u32_s32(
  58. vaddq_s32(sse_lo_s32, sse_hi_s32))),
  59. 0);
  60. }
  61. // Process a block of any size where the width is divisible by 16.
  62. static void variance_neon_w16(const uint8_t *src_ptr, int src_stride,
  63. const uint8_t *ref_ptr, int ref_stride, int w,
  64. int h, uint32_t *sse, int *sum) {
  65. int i, j;
  66. int16x8_t sum_s16 = vdupq_n_s16(0);
  67. int32x4_t sse_lo_s32 = vdupq_n_s32(0);
  68. int32x4_t sse_hi_s32 = vdupq_n_s32(0);
  69. // The loop loads 16 values at a time but doubles them up when accumulating
  70. // into sum_s16.
  71. assert(w / 8 * h <= 128);
  72. for (i = 0; i < h; ++i) {
  73. for (j = 0; j < w; j += 16) {
  74. const uint8x16_t a_u8 = vld1q_u8(src_ptr + j);
  75. const uint8x16_t b_u8 = vld1q_u8(ref_ptr + j);
  76. const uint16x8_t diff_lo_u16 =
  77. vsubl_u8(vget_low_u8(a_u8), vget_low_u8(b_u8));
  78. const uint16x8_t diff_hi_u16 =
  79. vsubl_u8(vget_high_u8(a_u8), vget_high_u8(b_u8));
  80. const int16x8_t diff_lo_s16 = vreinterpretq_s16_u16(diff_lo_u16);
  81. const int16x8_t diff_hi_s16 = vreinterpretq_s16_u16(diff_hi_u16);
  82. sum_s16 = vaddq_s16(sum_s16, diff_lo_s16);
  83. sum_s16 = vaddq_s16(sum_s16, diff_hi_s16);
  84. sse_lo_s32 = vmlal_s16(sse_lo_s32, vget_low_s16(diff_lo_s16),
  85. vget_low_s16(diff_lo_s16));
  86. sse_lo_s32 = vmlal_s16(sse_lo_s32, vget_high_s16(diff_lo_s16),
  87. vget_high_s16(diff_lo_s16));
  88. sse_hi_s32 = vmlal_s16(sse_hi_s32, vget_low_s16(diff_hi_s16),
  89. vget_low_s16(diff_hi_s16));
  90. sse_hi_s32 = vmlal_s16(sse_hi_s32, vget_high_s16(diff_hi_s16),
  91. vget_high_s16(diff_hi_s16));
  92. }
  93. src_ptr += src_stride;
  94. ref_ptr += ref_stride;
  95. }
  96. *sum = vget_lane_s32(horizontal_add_int16x8(sum_s16), 0);
  97. *sse = vget_lane_u32(horizontal_add_uint32x4(vreinterpretq_u32_s32(
  98. vaddq_s32(sse_lo_s32, sse_hi_s32))),
  99. 0);
  100. }
  101. // Process a block of width 8 two rows at a time.
  102. static void variance_neon_w8x2(const uint8_t *src_ptr, int src_stride,
  103. const uint8_t *ref_ptr, int ref_stride, int h,
  104. uint32_t *sse, int *sum) {
  105. int i = 0;
  106. int16x8_t sum_s16 = vdupq_n_s16(0);
  107. int32x4_t sse_lo_s32 = vdupq_n_s32(0);
  108. int32x4_t sse_hi_s32 = vdupq_n_s32(0);
  109. // Each column has it's own accumulator entry in sum_s16.
  110. assert(h <= 128);
  111. do {
  112. const uint8x8_t a_0_u8 = vld1_u8(src_ptr);
  113. const uint8x8_t a_1_u8 = vld1_u8(src_ptr + src_stride);
  114. const uint8x8_t b_0_u8 = vld1_u8(ref_ptr);
  115. const uint8x8_t b_1_u8 = vld1_u8(ref_ptr + ref_stride);
  116. const uint16x8_t diff_0_u16 = vsubl_u8(a_0_u8, b_0_u8);
  117. const uint16x8_t diff_1_u16 = vsubl_u8(a_1_u8, b_1_u8);
  118. const int16x8_t diff_0_s16 = vreinterpretq_s16_u16(diff_0_u16);
  119. const int16x8_t diff_1_s16 = vreinterpretq_s16_u16(diff_1_u16);
  120. sum_s16 = vaddq_s16(sum_s16, diff_0_s16);
  121. sum_s16 = vaddq_s16(sum_s16, diff_1_s16);
  122. sse_lo_s32 = vmlal_s16(sse_lo_s32, vget_low_s16(diff_0_s16),
  123. vget_low_s16(diff_0_s16));
  124. sse_lo_s32 = vmlal_s16(sse_lo_s32, vget_low_s16(diff_1_s16),
  125. vget_low_s16(diff_1_s16));
  126. sse_hi_s32 = vmlal_s16(sse_hi_s32, vget_high_s16(diff_0_s16),
  127. vget_high_s16(diff_0_s16));
  128. sse_hi_s32 = vmlal_s16(sse_hi_s32, vget_high_s16(diff_1_s16),
  129. vget_high_s16(diff_1_s16));
  130. src_ptr += src_stride + src_stride;
  131. ref_ptr += ref_stride + ref_stride;
  132. i += 2;
  133. } while (i < h);
  134. *sum = vget_lane_s32(horizontal_add_int16x8(sum_s16), 0);
  135. *sse = vget_lane_u32(horizontal_add_uint32x4(vreinterpretq_u32_s32(
  136. vaddq_s32(sse_lo_s32, sse_hi_s32))),
  137. 0);
  138. }
  139. void vpx_get8x8var_neon(const uint8_t *src_ptr, int src_stride,
  140. const uint8_t *ref_ptr, int ref_stride,
  141. unsigned int *sse, int *sum) {
  142. variance_neon_w8x2(src_ptr, src_stride, ref_ptr, ref_stride, 8, sse, sum);
  143. }
  144. void vpx_get16x16var_neon(const uint8_t *src_ptr, int src_stride,
  145. const uint8_t *ref_ptr, int ref_stride,
  146. unsigned int *sse, int *sum) {
  147. variance_neon_w16(src_ptr, src_stride, ref_ptr, ref_stride, 16, 16, sse, sum);
  148. }
  149. #define varianceNxM(n, m, shift) \
  150. unsigned int vpx_variance##n##x##m##_neon( \
  151. const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr, \
  152. int ref_stride, unsigned int *sse) { \
  153. int sum; \
  154. if (n == 4) \
  155. variance_neon_w4x4(src_ptr, src_stride, ref_ptr, ref_stride, m, sse, \
  156. &sum); \
  157. else if (n == 8) \
  158. variance_neon_w8x2(src_ptr, src_stride, ref_ptr, ref_stride, m, sse, \
  159. &sum); \
  160. else \
  161. variance_neon_w16(src_ptr, src_stride, ref_ptr, ref_stride, n, m, sse, \
  162. &sum); \
  163. if (n * m < 16 * 16) \
  164. return *sse - ((sum * sum) >> shift); \
  165. else \
  166. return *sse - (uint32_t)(((int64_t)sum * sum) >> shift); \
  167. }
  168. varianceNxM(4, 4, 4);
  169. varianceNxM(4, 8, 5);
  170. varianceNxM(8, 4, 5);
  171. varianceNxM(8, 8, 6);
  172. varianceNxM(8, 16, 7);
  173. varianceNxM(16, 8, 7);
  174. varianceNxM(16, 16, 8);
  175. varianceNxM(16, 32, 9);
  176. varianceNxM(32, 16, 9);
  177. varianceNxM(32, 32, 10);
  178. unsigned int vpx_variance32x64_neon(const uint8_t *src_ptr, int src_stride,
  179. const uint8_t *ref_ptr, int ref_stride,
  180. unsigned int *sse) {
  181. int sum1, sum2;
  182. uint32_t sse1, sse2;
  183. variance_neon_w16(src_ptr, src_stride, ref_ptr, ref_stride, 32, 32, &sse1,
  184. &sum1);
  185. variance_neon_w16(src_ptr + (32 * src_stride), src_stride,
  186. ref_ptr + (32 * ref_stride), ref_stride, 32, 32, &sse2,
  187. &sum2);
  188. *sse = sse1 + sse2;
  189. sum1 += sum2;
  190. return *sse - (unsigned int)(((int64_t)sum1 * sum1) >> 11);
  191. }
  192. unsigned int vpx_variance64x32_neon(const uint8_t *src_ptr, int src_stride,
  193. const uint8_t *ref_ptr, int ref_stride,
  194. unsigned int *sse) {
  195. int sum1, sum2;
  196. uint32_t sse1, sse2;
  197. variance_neon_w16(src_ptr, src_stride, ref_ptr, ref_stride, 64, 16, &sse1,
  198. &sum1);
  199. variance_neon_w16(src_ptr + (16 * src_stride), src_stride,
  200. ref_ptr + (16 * ref_stride), ref_stride, 64, 16, &sse2,
  201. &sum2);
  202. *sse = sse1 + sse2;
  203. sum1 += sum2;
  204. return *sse - (unsigned int)(((int64_t)sum1 * sum1) >> 11);
  205. }
  206. unsigned int vpx_variance64x64_neon(const uint8_t *src_ptr, int src_stride,
  207. const uint8_t *ref_ptr, int ref_stride,
  208. unsigned int *sse) {
  209. int sum1, sum2;
  210. uint32_t sse1, sse2;
  211. variance_neon_w16(src_ptr, src_stride, ref_ptr, ref_stride, 64, 16, &sse1,
  212. &sum1);
  213. variance_neon_w16(src_ptr + (16 * src_stride), src_stride,
  214. ref_ptr + (16 * ref_stride), ref_stride, 64, 16, &sse2,
  215. &sum2);
  216. sse1 += sse2;
  217. sum1 += sum2;
  218. variance_neon_w16(src_ptr + (16 * 2 * src_stride), src_stride,
  219. ref_ptr + (16 * 2 * ref_stride), ref_stride, 64, 16, &sse2,
  220. &sum2);
  221. sse1 += sse2;
  222. sum1 += sum2;
  223. variance_neon_w16(src_ptr + (16 * 3 * src_stride), src_stride,
  224. ref_ptr + (16 * 3 * ref_stride), ref_stride, 64, 16, &sse2,
  225. &sum2);
  226. *sse = sse1 + sse2;
  227. sum1 += sum2;
  228. return *sse - (unsigned int)(((int64_t)sum1 * sum1) >> 12);
  229. }
  230. unsigned int vpx_mse16x16_neon(const unsigned char *src_ptr, int src_stride,
  231. const unsigned char *ref_ptr, int ref_stride,
  232. unsigned int *sse) {
  233. int i;
  234. int16x4_t d22s16, d23s16, d24s16, d25s16, d26s16, d27s16, d28s16, d29s16;
  235. int64x1_t d0s64;
  236. uint8x16_t q0u8, q1u8, q2u8, q3u8;
  237. int32x4_t q7s32, q8s32, q9s32, q10s32;
  238. uint16x8_t q11u16, q12u16, q13u16, q14u16;
  239. int64x2_t q1s64;
  240. q7s32 = vdupq_n_s32(0);
  241. q8s32 = vdupq_n_s32(0);
  242. q9s32 = vdupq_n_s32(0);
  243. q10s32 = vdupq_n_s32(0);
  244. for (i = 0; i < 8; i++) { // mse16x16_neon_loop
  245. q0u8 = vld1q_u8(src_ptr);
  246. src_ptr += src_stride;
  247. q1u8 = vld1q_u8(src_ptr);
  248. src_ptr += src_stride;
  249. q2u8 = vld1q_u8(ref_ptr);
  250. ref_ptr += ref_stride;
  251. q3u8 = vld1q_u8(ref_ptr);
  252. ref_ptr += ref_stride;
  253. q11u16 = vsubl_u8(vget_low_u8(q0u8), vget_low_u8(q2u8));
  254. q12u16 = vsubl_u8(vget_high_u8(q0u8), vget_high_u8(q2u8));
  255. q13u16 = vsubl_u8(vget_low_u8(q1u8), vget_low_u8(q3u8));
  256. q14u16 = vsubl_u8(vget_high_u8(q1u8), vget_high_u8(q3u8));
  257. d22s16 = vreinterpret_s16_u16(vget_low_u16(q11u16));
  258. d23s16 = vreinterpret_s16_u16(vget_high_u16(q11u16));
  259. q7s32 = vmlal_s16(q7s32, d22s16, d22s16);
  260. q8s32 = vmlal_s16(q8s32, d23s16, d23s16);
  261. d24s16 = vreinterpret_s16_u16(vget_low_u16(q12u16));
  262. d25s16 = vreinterpret_s16_u16(vget_high_u16(q12u16));
  263. q9s32 = vmlal_s16(q9s32, d24s16, d24s16);
  264. q10s32 = vmlal_s16(q10s32, d25s16, d25s16);
  265. d26s16 = vreinterpret_s16_u16(vget_low_u16(q13u16));
  266. d27s16 = vreinterpret_s16_u16(vget_high_u16(q13u16));
  267. q7s32 = vmlal_s16(q7s32, d26s16, d26s16);
  268. q8s32 = vmlal_s16(q8s32, d27s16, d27s16);
  269. d28s16 = vreinterpret_s16_u16(vget_low_u16(q14u16));
  270. d29s16 = vreinterpret_s16_u16(vget_high_u16(q14u16));
  271. q9s32 = vmlal_s16(q9s32, d28s16, d28s16);
  272. q10s32 = vmlal_s16(q10s32, d29s16, d29s16);
  273. }
  274. q7s32 = vaddq_s32(q7s32, q8s32);
  275. q9s32 = vaddq_s32(q9s32, q10s32);
  276. q10s32 = vaddq_s32(q7s32, q9s32);
  277. q1s64 = vpaddlq_s32(q10s32);
  278. d0s64 = vadd_s64(vget_low_s64(q1s64), vget_high_s64(q1s64));
  279. vst1_lane_u32((uint32_t *)sse, vreinterpret_u32_s64(d0s64), 0);
  280. return vget_lane_u32(vreinterpret_u32_s64(d0s64), 0);
  281. }
  282. unsigned int vpx_get4x4sse_cs_neon(const unsigned char *src_ptr, int src_stride,
  283. const unsigned char *ref_ptr,
  284. int ref_stride) {
  285. int16x4_t d22s16, d24s16, d26s16, d28s16;
  286. int64x1_t d0s64;
  287. uint8x8_t d0u8, d1u8, d2u8, d3u8, d4u8, d5u8, d6u8, d7u8;
  288. int32x4_t q7s32, q8s32, q9s32, q10s32;
  289. uint16x8_t q11u16, q12u16, q13u16, q14u16;
  290. int64x2_t q1s64;
  291. d0u8 = vld1_u8(src_ptr);
  292. src_ptr += src_stride;
  293. d4u8 = vld1_u8(ref_ptr);
  294. ref_ptr += ref_stride;
  295. d1u8 = vld1_u8(src_ptr);
  296. src_ptr += src_stride;
  297. d5u8 = vld1_u8(ref_ptr);
  298. ref_ptr += ref_stride;
  299. d2u8 = vld1_u8(src_ptr);
  300. src_ptr += src_stride;
  301. d6u8 = vld1_u8(ref_ptr);
  302. ref_ptr += ref_stride;
  303. d3u8 = vld1_u8(src_ptr);
  304. src_ptr += src_stride;
  305. d7u8 = vld1_u8(ref_ptr);
  306. ref_ptr += ref_stride;
  307. q11u16 = vsubl_u8(d0u8, d4u8);
  308. q12u16 = vsubl_u8(d1u8, d5u8);
  309. q13u16 = vsubl_u8(d2u8, d6u8);
  310. q14u16 = vsubl_u8(d3u8, d7u8);
  311. d22s16 = vget_low_s16(vreinterpretq_s16_u16(q11u16));
  312. d24s16 = vget_low_s16(vreinterpretq_s16_u16(q12u16));
  313. d26s16 = vget_low_s16(vreinterpretq_s16_u16(q13u16));
  314. d28s16 = vget_low_s16(vreinterpretq_s16_u16(q14u16));
  315. q7s32 = vmull_s16(d22s16, d22s16);
  316. q8s32 = vmull_s16(d24s16, d24s16);
  317. q9s32 = vmull_s16(d26s16, d26s16);
  318. q10s32 = vmull_s16(d28s16, d28s16);
  319. q7s32 = vaddq_s32(q7s32, q8s32);
  320. q9s32 = vaddq_s32(q9s32, q10s32);
  321. q9s32 = vaddq_s32(q7s32, q9s32);
  322. q1s64 = vpaddlq_s32(q9s32);
  323. d0s64 = vadd_s64(vget_low_s64(q1s64), vget_high_s64(q1s64));
  324. return vget_lane_u32(vreinterpret_u32_s64(d0s64), 0);
  325. }