vpx_scaled_convolve8_neon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <string.h>
  13. #include "./vpx_config.h"
  14. #include "./vpx_dsp_rtcd.h"
  15. #include "vpx/vpx_integer.h"
  16. #include "vpx_dsp/arm/transpose_neon.h"
  17. #include "vpx_dsp/arm/vpx_convolve8_neon.h"
  18. #include "vpx_ports/mem.h"
  19. static INLINE void scaledconvolve_horiz_w4(
  20. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
  21. const ptrdiff_t dst_stride, const InterpKernel *const x_filters,
  22. const int x0_q4, const int x_step_q4, const int w, const int h) {
  23. DECLARE_ALIGNED(16, uint8_t, temp[4 * 4]);
  24. int x, y, z;
  25. src -= SUBPEL_TAPS / 2 - 1;
  26. y = h;
  27. do {
  28. int x_q4 = x0_q4;
  29. x = 0;
  30. do {
  31. // process 4 src_x steps
  32. for (z = 0; z < 4; ++z) {
  33. const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
  34. if (x_q4 & SUBPEL_MASK) {
  35. const int16x8_t filters = vld1q_s16(x_filters[x_q4 & SUBPEL_MASK]);
  36. const int16x4_t filter3 = vdup_lane_s16(vget_low_s16(filters), 3);
  37. const int16x4_t filter4 = vdup_lane_s16(vget_high_s16(filters), 0);
  38. uint8x8_t s[8], d;
  39. int16x8_t ss[4];
  40. int16x4_t t[8], tt;
  41. load_u8_8x4(src_x, src_stride, &s[0], &s[1], &s[2], &s[3]);
  42. transpose_u8_8x4(&s[0], &s[1], &s[2], &s[3]);
  43. ss[0] = vreinterpretq_s16_u16(vmovl_u8(s[0]));
  44. ss[1] = vreinterpretq_s16_u16(vmovl_u8(s[1]));
  45. ss[2] = vreinterpretq_s16_u16(vmovl_u8(s[2]));
  46. ss[3] = vreinterpretq_s16_u16(vmovl_u8(s[3]));
  47. t[0] = vget_low_s16(ss[0]);
  48. t[1] = vget_low_s16(ss[1]);
  49. t[2] = vget_low_s16(ss[2]);
  50. t[3] = vget_low_s16(ss[3]);
  51. t[4] = vget_high_s16(ss[0]);
  52. t[5] = vget_high_s16(ss[1]);
  53. t[6] = vget_high_s16(ss[2]);
  54. t[7] = vget_high_s16(ss[3]);
  55. tt = convolve8_4(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7],
  56. filters, filter3, filter4);
  57. d = vqrshrun_n_s16(vcombine_s16(tt, tt), 7);
  58. vst1_lane_u32((uint32_t *)&temp[4 * z], vreinterpret_u32_u8(d), 0);
  59. } else {
  60. int i;
  61. for (i = 0; i < 4; ++i) {
  62. temp[z * 4 + i] = src_x[i * src_stride + 3];
  63. }
  64. }
  65. x_q4 += x_step_q4;
  66. }
  67. // transpose the 4x4 filters values back to dst
  68. {
  69. const uint8x8x4_t d4 = vld4_u8(temp);
  70. vst1_lane_u32((uint32_t *)&dst[x + 0 * dst_stride],
  71. vreinterpret_u32_u8(d4.val[0]), 0);
  72. vst1_lane_u32((uint32_t *)&dst[x + 1 * dst_stride],
  73. vreinterpret_u32_u8(d4.val[1]), 0);
  74. vst1_lane_u32((uint32_t *)&dst[x + 2 * dst_stride],
  75. vreinterpret_u32_u8(d4.val[2]), 0);
  76. vst1_lane_u32((uint32_t *)&dst[x + 3 * dst_stride],
  77. vreinterpret_u32_u8(d4.val[3]), 0);
  78. }
  79. x += 4;
  80. } while (x < w);
  81. src += src_stride * 4;
  82. dst += dst_stride * 4;
  83. y -= 4;
  84. } while (y > 0);
  85. }
  86. static INLINE void scaledconvolve_horiz_w8(
  87. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
  88. const ptrdiff_t dst_stride, const InterpKernel *const x_filters,
  89. const int x0_q4, const int x_step_q4, const int w, const int h) {
  90. DECLARE_ALIGNED(16, uint8_t, temp[8 * 8]);
  91. int x, y, z;
  92. src -= SUBPEL_TAPS / 2 - 1;
  93. // This function processes 8x8 areas. The intermediate height is not always
  94. // a multiple of 8, so force it to be a multiple of 8 here.
  95. y = (h + 7) & ~7;
  96. do {
  97. int x_q4 = x0_q4;
  98. x = 0;
  99. do {
  100. uint8x8_t d[8];
  101. // process 8 src_x steps
  102. for (z = 0; z < 8; ++z) {
  103. const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
  104. if (x_q4 & SUBPEL_MASK) {
  105. const int16x8_t filters = vld1q_s16(x_filters[x_q4 & SUBPEL_MASK]);
  106. uint8x8_t s[8];
  107. load_u8_8x8(src_x, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4],
  108. &s[5], &s[6], &s[7]);
  109. transpose_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
  110. &s[7]);
  111. d[0] = scale_filter_8(s, filters);
  112. vst1_u8(&temp[8 * z], d[0]);
  113. } else {
  114. int i;
  115. for (i = 0; i < 8; ++i) {
  116. temp[z * 8 + i] = src_x[i * src_stride + 3];
  117. }
  118. }
  119. x_q4 += x_step_q4;
  120. }
  121. // transpose the 8x8 filters values back to dst
  122. load_u8_8x8(temp, 8, &d[0], &d[1], &d[2], &d[3], &d[4], &d[5], &d[6],
  123. &d[7]);
  124. transpose_u8_8x8(&d[0], &d[1], &d[2], &d[3], &d[4], &d[5], &d[6], &d[7]);
  125. vst1_u8(&dst[x + 0 * dst_stride], d[0]);
  126. vst1_u8(&dst[x + 1 * dst_stride], d[1]);
  127. vst1_u8(&dst[x + 2 * dst_stride], d[2]);
  128. vst1_u8(&dst[x + 3 * dst_stride], d[3]);
  129. vst1_u8(&dst[x + 4 * dst_stride], d[4]);
  130. vst1_u8(&dst[x + 5 * dst_stride], d[5]);
  131. vst1_u8(&dst[x + 6 * dst_stride], d[6]);
  132. vst1_u8(&dst[x + 7 * dst_stride], d[7]);
  133. x += 8;
  134. } while (x < w);
  135. src += src_stride * 8;
  136. dst += dst_stride * 8;
  137. } while (y -= 8);
  138. }
  139. static INLINE void scaledconvolve_vert_w4(
  140. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
  141. const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
  142. const int y0_q4, const int y_step_q4, const int w, const int h) {
  143. int y;
  144. int y_q4 = y0_q4;
  145. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  146. y = h;
  147. do {
  148. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  149. if (y_q4 & SUBPEL_MASK) {
  150. const int16x8_t filters = vld1q_s16(y_filters[y_q4 & SUBPEL_MASK]);
  151. const int16x4_t filter3 = vdup_lane_s16(vget_low_s16(filters), 3);
  152. const int16x4_t filter4 = vdup_lane_s16(vget_high_s16(filters), 0);
  153. uint8x8_t s[8], d;
  154. int16x4_t t[8], tt;
  155. load_u8_8x8(src_y, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
  156. &s[6], &s[7]);
  157. t[0] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[0])));
  158. t[1] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[1])));
  159. t[2] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[2])));
  160. t[3] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[3])));
  161. t[4] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[4])));
  162. t[5] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[5])));
  163. t[6] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[6])));
  164. t[7] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[7])));
  165. tt = convolve8_4(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7], filters,
  166. filter3, filter4);
  167. d = vqrshrun_n_s16(vcombine_s16(tt, tt), 7);
  168. vst1_lane_u32((uint32_t *)dst, vreinterpret_u32_u8(d), 0);
  169. } else {
  170. memcpy(dst, &src_y[3 * src_stride], w);
  171. }
  172. dst += dst_stride;
  173. y_q4 += y_step_q4;
  174. } while (--y);
  175. }
  176. static INLINE void scaledconvolve_vert_w8(
  177. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
  178. const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
  179. const int y0_q4, const int y_step_q4, const int w, const int h) {
  180. int y;
  181. int y_q4 = y0_q4;
  182. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  183. y = h;
  184. do {
  185. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  186. if (y_q4 & SUBPEL_MASK) {
  187. const int16x8_t filters = vld1q_s16(y_filters[y_q4 & SUBPEL_MASK]);
  188. uint8x8_t s[8], d;
  189. load_u8_8x8(src_y, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
  190. &s[6], &s[7]);
  191. d = scale_filter_8(s, filters);
  192. vst1_u8(dst, d);
  193. } else {
  194. memcpy(dst, &src_y[3 * src_stride], w);
  195. }
  196. dst += dst_stride;
  197. y_q4 += y_step_q4;
  198. } while (--y);
  199. }
  200. static INLINE void scaledconvolve_vert_w16(
  201. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
  202. const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
  203. const int y0_q4, const int y_step_q4, const int w, const int h) {
  204. int x, y;
  205. int y_q4 = y0_q4;
  206. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  207. y = h;
  208. do {
  209. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  210. if (y_q4 & SUBPEL_MASK) {
  211. x = 0;
  212. do {
  213. const int16x8_t filters = vld1q_s16(y_filters[y_q4 & SUBPEL_MASK]);
  214. uint8x16_t ss[8];
  215. uint8x8_t s[8], d[2];
  216. load_u8_16x8(src_y, src_stride, &ss[0], &ss[1], &ss[2], &ss[3], &ss[4],
  217. &ss[5], &ss[6], &ss[7]);
  218. s[0] = vget_low_u8(ss[0]);
  219. s[1] = vget_low_u8(ss[1]);
  220. s[2] = vget_low_u8(ss[2]);
  221. s[3] = vget_low_u8(ss[3]);
  222. s[4] = vget_low_u8(ss[4]);
  223. s[5] = vget_low_u8(ss[5]);
  224. s[6] = vget_low_u8(ss[6]);
  225. s[7] = vget_low_u8(ss[7]);
  226. d[0] = scale_filter_8(s, filters);
  227. s[0] = vget_high_u8(ss[0]);
  228. s[1] = vget_high_u8(ss[1]);
  229. s[2] = vget_high_u8(ss[2]);
  230. s[3] = vget_high_u8(ss[3]);
  231. s[4] = vget_high_u8(ss[4]);
  232. s[5] = vget_high_u8(ss[5]);
  233. s[6] = vget_high_u8(ss[6]);
  234. s[7] = vget_high_u8(ss[7]);
  235. d[1] = scale_filter_8(s, filters);
  236. vst1q_u8(&dst[x], vcombine_u8(d[0], d[1]));
  237. src_y += 16;
  238. x += 16;
  239. } while (x < w);
  240. } else {
  241. memcpy(dst, &src_y[3 * src_stride], w);
  242. }
  243. dst += dst_stride;
  244. y_q4 += y_step_q4;
  245. } while (--y);
  246. }
  247. void vpx_scaled_2d_neon(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
  248. ptrdiff_t dst_stride, const InterpKernel *filter,
  249. int x0_q4, int x_step_q4, int y0_q4, int y_step_q4,
  250. int w, int h) {
  251. // Note: Fixed size intermediate buffer, temp, places limits on parameters.
  252. // 2d filtering proceeds in 2 steps:
  253. // (1) Interpolate horizontally into an intermediate buffer, temp.
  254. // (2) Interpolate temp vertically to derive the sub-pixel result.
  255. // Deriving the maximum number of rows in the temp buffer (135):
  256. // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
  257. // --Largest block size is 64x64 pixels.
  258. // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
  259. // original frame (in 1/16th pixel units).
  260. // --Must round-up because block may be located at sub-pixel position.
  261. // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
  262. // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
  263. // --Require an additional 8 rows for the horiz_w8 transpose tail.
  264. // When calling in frame scaling function, the smallest scaling factor is x1/4
  265. // ==> y_step_q4 = 64. Since w and h are at most 16, the temp buffer is still
  266. // big enough.
  267. DECLARE_ALIGNED(16, uint8_t, temp[(135 + 8) * 64]);
  268. const int intermediate_height =
  269. (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
  270. assert(w <= 64);
  271. assert(h <= 64);
  272. assert(y_step_q4 <= 32 || (y_step_q4 <= 64 && h <= 32));
  273. assert(x_step_q4 <= 64);
  274. if (w >= 8) {
  275. scaledconvolve_horiz_w8(src - src_stride * (SUBPEL_TAPS / 2 - 1),
  276. src_stride, temp, 64, filter, x0_q4, x_step_q4, w,
  277. intermediate_height);
  278. } else {
  279. scaledconvolve_horiz_w4(src - src_stride * (SUBPEL_TAPS / 2 - 1),
  280. src_stride, temp, 64, filter, x0_q4, x_step_q4, w,
  281. intermediate_height);
  282. }
  283. if (w >= 16) {
  284. scaledconvolve_vert_w16(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  285. dst_stride, filter, y0_q4, y_step_q4, w, h);
  286. } else if (w == 8) {
  287. scaledconvolve_vert_w8(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  288. dst_stride, filter, y0_q4, y_step_q4, w, h);
  289. } else {
  290. scaledconvolve_vert_w4(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  291. dst_stride, filter, y0_q4, y_step_q4, w, h);
  292. }
  293. }