vpx_convolve_vsx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <string.h>
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx_dsp/vpx_filter.h"
  14. #include "vpx_dsp/ppc/types_vsx.h"
  15. // TODO(lu_zero): unroll
  16. static inline void copy_w16(const uint8_t *src, ptrdiff_t src_stride,
  17. uint8_t *dst, ptrdiff_t dst_stride, int32_t h) {
  18. int i;
  19. for (i = h; i--;) {
  20. vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
  21. src += src_stride;
  22. dst += dst_stride;
  23. }
  24. }
  25. static inline void copy_w32(const uint8_t *src, ptrdiff_t src_stride,
  26. uint8_t *dst, ptrdiff_t dst_stride, int32_t h) {
  27. int i;
  28. for (i = h; i--;) {
  29. vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
  30. vec_vsx_st(vec_vsx_ld(16, src), 16, dst);
  31. src += src_stride;
  32. dst += dst_stride;
  33. }
  34. }
  35. static inline void copy_w64(const uint8_t *src, ptrdiff_t src_stride,
  36. uint8_t *dst, ptrdiff_t dst_stride, int32_t h) {
  37. int i;
  38. for (i = h; i--;) {
  39. vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
  40. vec_vsx_st(vec_vsx_ld(16, src), 16, dst);
  41. vec_vsx_st(vec_vsx_ld(32, src), 32, dst);
  42. vec_vsx_st(vec_vsx_ld(48, src), 48, dst);
  43. src += src_stride;
  44. dst += dst_stride;
  45. }
  46. }
  47. void vpx_convolve_copy_vsx(const uint8_t *src, ptrdiff_t src_stride,
  48. uint8_t *dst, ptrdiff_t dst_stride,
  49. const InterpKernel *filter, int x0_q4, int x_step_q4,
  50. int y0_q4, int32_t y_step_q4, int32_t w, int32_t h) {
  51. (void)filter;
  52. (void)x0_q4;
  53. (void)x_step_q4;
  54. (void)y0_q4;
  55. (void)y_step_q4;
  56. switch (w) {
  57. case 16: {
  58. copy_w16(src, src_stride, dst, dst_stride, h);
  59. break;
  60. }
  61. case 32: {
  62. copy_w32(src, src_stride, dst, dst_stride, h);
  63. break;
  64. }
  65. case 64: {
  66. copy_w64(src, src_stride, dst, dst_stride, h);
  67. break;
  68. }
  69. default: {
  70. int i;
  71. for (i = h; i--;) {
  72. memcpy(dst, src, w);
  73. src += src_stride;
  74. dst += dst_stride;
  75. }
  76. break;
  77. }
  78. }
  79. }
  80. static inline void avg_w16(const uint8_t *src, ptrdiff_t src_stride,
  81. uint8_t *dst, ptrdiff_t dst_stride, int32_t h) {
  82. int i;
  83. for (i = h; i--;) {
  84. const uint8x16_t v = vec_avg(vec_vsx_ld(0, src), vec_vsx_ld(0, dst));
  85. vec_vsx_st(v, 0, dst);
  86. src += src_stride;
  87. dst += dst_stride;
  88. }
  89. }
  90. static inline void avg_w32(const uint8_t *src, ptrdiff_t src_stride,
  91. uint8_t *dst, ptrdiff_t dst_stride, int32_t h) {
  92. int i;
  93. for (i = h; i--;) {
  94. const uint8x16_t v0 = vec_avg(vec_vsx_ld(0, src), vec_vsx_ld(0, dst));
  95. const uint8x16_t v1 = vec_avg(vec_vsx_ld(16, src), vec_vsx_ld(16, dst));
  96. vec_vsx_st(v0, 0, dst);
  97. vec_vsx_st(v1, 16, dst);
  98. src += src_stride;
  99. dst += dst_stride;
  100. }
  101. }
  102. static inline void avg_w64(const uint8_t *src, ptrdiff_t src_stride,
  103. uint8_t *dst, ptrdiff_t dst_stride, int32_t h) {
  104. int i;
  105. for (i = h; i--;) {
  106. const uint8x16_t v0 = vec_avg(vec_vsx_ld(0, src), vec_vsx_ld(0, dst));
  107. const uint8x16_t v1 = vec_avg(vec_vsx_ld(16, src), vec_vsx_ld(16, dst));
  108. const uint8x16_t v2 = vec_avg(vec_vsx_ld(32, src), vec_vsx_ld(32, dst));
  109. const uint8x16_t v3 = vec_avg(vec_vsx_ld(48, src), vec_vsx_ld(48, dst));
  110. vec_vsx_st(v0, 0, dst);
  111. vec_vsx_st(v1, 16, dst);
  112. vec_vsx_st(v2, 32, dst);
  113. vec_vsx_st(v3, 48, dst);
  114. src += src_stride;
  115. dst += dst_stride;
  116. }
  117. }
  118. void vpx_convolve_avg_vsx(const uint8_t *src, ptrdiff_t src_stride,
  119. uint8_t *dst, ptrdiff_t dst_stride,
  120. const InterpKernel *filter, int x0_q4, int x_step_q4,
  121. int y0_q4, int32_t y_step_q4, int32_t w, int32_t h) {
  122. switch (w) {
  123. case 16: {
  124. avg_w16(src, src_stride, dst, dst_stride, h);
  125. break;
  126. }
  127. case 32: {
  128. avg_w32(src, src_stride, dst, dst_stride, h);
  129. break;
  130. }
  131. case 64: {
  132. avg_w64(src, src_stride, dst, dst_stride, h);
  133. break;
  134. }
  135. default: {
  136. vpx_convolve_avg_c(src, src_stride, dst, dst_stride, filter, x0_q4,
  137. x_step_q4, y0_q4, y_step_q4, w, h);
  138. break;
  139. }
  140. }
  141. }
  142. static inline void convolve_line(uint8_t *dst, const int16x8_t s,
  143. const int16x8_t f) {
  144. const int32x4_t sum = vec_msum(s, f, vec_splat_s32(0));
  145. const int32x4_t bias =
  146. vec_sl(vec_splat_s32(1), vec_splat_u32(FILTER_BITS - 1));
  147. const int32x4_t avg = vec_sr(vec_sums(sum, bias), vec_splat_u32(FILTER_BITS));
  148. const uint8x16_t v = vec_splat(
  149. vec_packsu(vec_pack(avg, vec_splat_s32(0)), vec_splat_s16(0)), 3);
  150. vec_ste(v, 0, dst);
  151. }
  152. static inline void convolve_line_h(uint8_t *dst, const uint8_t *const src_x,
  153. const int16_t *const x_filter) {
  154. const int16x8_t s = unpack_to_s16_h(vec_vsx_ld(0, src_x));
  155. const int16x8_t f = vec_vsx_ld(0, x_filter);
  156. convolve_line(dst, s, f);
  157. }
  158. // TODO(lu_zero): Implement 8x8 and bigger block special cases
  159. static inline void convolve_horiz(const uint8_t *src, ptrdiff_t src_stride,
  160. uint8_t *dst, ptrdiff_t dst_stride,
  161. const InterpKernel *x_filters, int x0_q4,
  162. int x_step_q4, int w, int h) {
  163. int x, y;
  164. src -= SUBPEL_TAPS / 2 - 1;
  165. for (y = 0; y < h; ++y) {
  166. int x_q4 = x0_q4;
  167. for (x = 0; x < w; ++x) {
  168. convolve_line_h(dst + x, &src[x_q4 >> SUBPEL_BITS],
  169. x_filters[x_q4 & SUBPEL_MASK]);
  170. x_q4 += x_step_q4;
  171. }
  172. src += src_stride;
  173. dst += dst_stride;
  174. }
  175. }
  176. static inline void convolve_avg_horiz(const uint8_t *src, ptrdiff_t src_stride,
  177. uint8_t *dst, ptrdiff_t dst_stride,
  178. const InterpKernel *x_filters, int x0_q4,
  179. int x_step_q4, int w, int h) {
  180. int x, y;
  181. src -= SUBPEL_TAPS / 2 - 1;
  182. for (y = 0; y < h; ++y) {
  183. int x_q4 = x0_q4;
  184. for (x = 0; x < w; ++x) {
  185. uint8_t v;
  186. convolve_line_h(&v, &src[x_q4 >> SUBPEL_BITS],
  187. x_filters[x_q4 & SUBPEL_MASK]);
  188. dst[x] = ROUND_POWER_OF_TWO(dst[x] + v, 1);
  189. x_q4 += x_step_q4;
  190. }
  191. src += src_stride;
  192. dst += dst_stride;
  193. }
  194. }
  195. static uint8x16_t transpose_line_u8_8x8(uint8x16_t a, uint8x16_t b,
  196. uint8x16_t c, uint8x16_t d,
  197. uint8x16_t e, uint8x16_t f,
  198. uint8x16_t g, uint8x16_t h) {
  199. uint16x8_t ab = (uint16x8_t)vec_mergeh(a, b);
  200. uint16x8_t cd = (uint16x8_t)vec_mergeh(c, d);
  201. uint16x8_t ef = (uint16x8_t)vec_mergeh(e, f);
  202. uint16x8_t gh = (uint16x8_t)vec_mergeh(g, h);
  203. uint32x4_t abcd = (uint32x4_t)vec_mergeh(ab, cd);
  204. uint32x4_t efgh = (uint32x4_t)vec_mergeh(ef, gh);
  205. return (uint8x16_t)vec_mergeh(abcd, efgh);
  206. }
  207. static inline void convolve_line_v(uint8_t *dst, const uint8_t *const src_y,
  208. ptrdiff_t src_stride,
  209. const int16_t *const y_filter) {
  210. uint8x16_t s0 = vec_vsx_ld(0, src_y + 0 * src_stride);
  211. uint8x16_t s1 = vec_vsx_ld(0, src_y + 1 * src_stride);
  212. uint8x16_t s2 = vec_vsx_ld(0, src_y + 2 * src_stride);
  213. uint8x16_t s3 = vec_vsx_ld(0, src_y + 3 * src_stride);
  214. uint8x16_t s4 = vec_vsx_ld(0, src_y + 4 * src_stride);
  215. uint8x16_t s5 = vec_vsx_ld(0, src_y + 5 * src_stride);
  216. uint8x16_t s6 = vec_vsx_ld(0, src_y + 6 * src_stride);
  217. uint8x16_t s7 = vec_vsx_ld(0, src_y + 7 * src_stride);
  218. const int16x8_t f = vec_vsx_ld(0, y_filter);
  219. uint8_t buf[16];
  220. const uint8x16_t s = transpose_line_u8_8x8(s0, s1, s2, s3, s4, s5, s6, s7);
  221. vec_vsx_st(s, 0, buf);
  222. convolve_line(dst, unpack_to_s16_h(s), f);
  223. }
  224. static inline void convolve_vert(const uint8_t *src, ptrdiff_t src_stride,
  225. uint8_t *dst, ptrdiff_t dst_stride,
  226. const InterpKernel *y_filters, int y0_q4,
  227. int y_step_q4, int w, int h) {
  228. int x, y;
  229. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  230. for (x = 0; x < w; ++x) {
  231. int y_q4 = y0_q4;
  232. for (y = 0; y < h; ++y) {
  233. convolve_line_v(dst + y * dst_stride,
  234. &src[(y_q4 >> SUBPEL_BITS) * src_stride], src_stride,
  235. y_filters[y_q4 & SUBPEL_MASK]);
  236. y_q4 += y_step_q4;
  237. }
  238. ++src;
  239. ++dst;
  240. }
  241. }
  242. static inline void convolve_avg_vert(const uint8_t *src, ptrdiff_t src_stride,
  243. uint8_t *dst, ptrdiff_t dst_stride,
  244. const InterpKernel *y_filters, int y0_q4,
  245. int y_step_q4, int w, int h) {
  246. int x, y;
  247. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  248. for (x = 0; x < w; ++x) {
  249. int y_q4 = y0_q4;
  250. for (y = 0; y < h; ++y) {
  251. uint8_t v;
  252. convolve_line_v(&v, &src[(y_q4 >> SUBPEL_BITS) * src_stride], src_stride,
  253. y_filters[y_q4 & SUBPEL_MASK]);
  254. dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] + v, 1);
  255. y_q4 += y_step_q4;
  256. }
  257. ++src;
  258. ++dst;
  259. }
  260. }
  261. static inline void convolve(const uint8_t *src, ptrdiff_t src_stride,
  262. uint8_t *dst, ptrdiff_t dst_stride,
  263. const InterpKernel *const filter, int x0_q4,
  264. int x_step_q4, int y0_q4, int y_step_q4, int w,
  265. int h) {
  266. // Note: Fixed size intermediate buffer, temp, places limits on parameters.
  267. // 2d filtering proceeds in 2 steps:
  268. // (1) Interpolate horizontally into an intermediate buffer, temp.
  269. // (2) Interpolate temp vertically to derive the sub-pixel result.
  270. // Deriving the maximum number of rows in the temp buffer (135):
  271. // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
  272. // --Largest block size is 64x64 pixels.
  273. // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
  274. // original frame (in 1/16th pixel units).
  275. // --Must round-up because block may be located at sub-pixel position.
  276. // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
  277. // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
  278. DECLARE_ALIGNED(16, uint8_t, temp[64 * 135]);
  279. const int intermediate_height =
  280. (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
  281. assert(w <= 64);
  282. assert(h <= 64);
  283. assert(y_step_q4 <= 32);
  284. assert(x_step_q4 <= 32);
  285. convolve_horiz(src - src_stride * (SUBPEL_TAPS / 2 - 1), src_stride, temp, 64,
  286. filter, x0_q4, x_step_q4, w, intermediate_height);
  287. convolve_vert(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst, dst_stride, filter,
  288. y0_q4, y_step_q4, w, h);
  289. }
  290. void vpx_convolve8_horiz_vsx(const uint8_t *src, ptrdiff_t src_stride,
  291. uint8_t *dst, ptrdiff_t dst_stride,
  292. const InterpKernel *filter, int x0_q4,
  293. int x_step_q4, int y0_q4, int y_step_q4, int w,
  294. int h) {
  295. (void)y0_q4;
  296. (void)y_step_q4;
  297. convolve_horiz(src, src_stride, dst, dst_stride, filter, x0_q4, x_step_q4, w,
  298. h);
  299. }
  300. void vpx_convolve8_avg_horiz_vsx(const uint8_t *src, ptrdiff_t src_stride,
  301. uint8_t *dst, ptrdiff_t dst_stride,
  302. const InterpKernel *filter, int x0_q4,
  303. int x_step_q4, int y0_q4, int y_step_q4, int w,
  304. int h) {
  305. (void)y0_q4;
  306. (void)y_step_q4;
  307. convolve_avg_horiz(src, src_stride, dst, dst_stride, filter, x0_q4, x_step_q4,
  308. w, h);
  309. }
  310. void vpx_convolve8_vert_vsx(const uint8_t *src, ptrdiff_t src_stride,
  311. uint8_t *dst, ptrdiff_t dst_stride,
  312. const InterpKernel *filter, int x0_q4,
  313. int x_step_q4, int y0_q4, int y_step_q4, int w,
  314. int h) {
  315. (void)x0_q4;
  316. (void)x_step_q4;
  317. convolve_vert(src, src_stride, dst, dst_stride, filter, y0_q4, y_step_q4, w,
  318. h);
  319. }
  320. void vpx_convolve8_avg_vert_vsx(const uint8_t *src, ptrdiff_t src_stride,
  321. uint8_t *dst, ptrdiff_t dst_stride,
  322. const InterpKernel *filter, int x0_q4,
  323. int x_step_q4, int y0_q4, int y_step_q4, int w,
  324. int h) {
  325. (void)x0_q4;
  326. (void)x_step_q4;
  327. convolve_avg_vert(src, src_stride, dst, dst_stride, filter, y0_q4, y_step_q4,
  328. w, h);
  329. }
  330. void vpx_convolve8_vsx(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
  331. ptrdiff_t dst_stride, const InterpKernel *filter,
  332. int x0_q4, int x_step_q4, int y0_q4, int y_step_q4,
  333. int w, int h) {
  334. convolve(src, src_stride, dst, dst_stride, filter, x0_q4, x_step_q4, y0_q4,
  335. y_step_q4, w, h);
  336. }
  337. void vpx_convolve8_avg_vsx(const uint8_t *src, ptrdiff_t src_stride,
  338. uint8_t *dst, ptrdiff_t dst_stride,
  339. const InterpKernel *filter, int x0_q4, int x_step_q4,
  340. int y0_q4, int y_step_q4, int w, int h) {
  341. // Fixed size intermediate buffer places limits on parameters.
  342. DECLARE_ALIGNED(16, uint8_t, temp[64 * 64]);
  343. assert(w <= 64);
  344. assert(h <= 64);
  345. vpx_convolve8_vsx(src, src_stride, temp, 64, filter, x0_q4, x_step_q4, y0_q4,
  346. y_step_q4, w, h);
  347. vpx_convolve_avg_vsx(temp, 64, dst, dst_stride, NULL, 0, 0, 0, 0, w, h);
  348. }