deblock_neon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (c) 2016 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/vpx_integer.h"
  14. #include "vpx_dsp/arm/transpose_neon.h"
  15. extern const int16_t vpx_rv[];
  16. static uint8x8_t average_k_out(const uint8x8_t a2, const uint8x8_t a1,
  17. const uint8x8_t v0, const uint8x8_t b1,
  18. const uint8x8_t b2) {
  19. const uint8x8_t k1 = vrhadd_u8(a2, a1);
  20. const uint8x8_t k2 = vrhadd_u8(b2, b1);
  21. const uint8x8_t k3 = vrhadd_u8(k1, k2);
  22. return vrhadd_u8(k3, v0);
  23. }
  24. static uint8x8_t generate_mask(const uint8x8_t a2, const uint8x8_t a1,
  25. const uint8x8_t v0, const uint8x8_t b1,
  26. const uint8x8_t b2, const uint8x8_t filter) {
  27. const uint8x8_t a2_v0 = vabd_u8(a2, v0);
  28. const uint8x8_t a1_v0 = vabd_u8(a1, v0);
  29. const uint8x8_t b1_v0 = vabd_u8(b1, v0);
  30. const uint8x8_t b2_v0 = vabd_u8(b2, v0);
  31. uint8x8_t max = vmax_u8(a2_v0, a1_v0);
  32. max = vmax_u8(b1_v0, max);
  33. max = vmax_u8(b2_v0, max);
  34. return vclt_u8(max, filter);
  35. }
  36. static uint8x8_t generate_output(const uint8x8_t a2, const uint8x8_t a1,
  37. const uint8x8_t v0, const uint8x8_t b1,
  38. const uint8x8_t b2, const uint8x8_t filter) {
  39. const uint8x8_t k_out = average_k_out(a2, a1, v0, b1, b2);
  40. const uint8x8_t mask = generate_mask(a2, a1, v0, b1, b2, filter);
  41. return vbsl_u8(mask, k_out, v0);
  42. }
  43. // Same functions but for uint8x16_t.
  44. static uint8x16_t average_k_outq(const uint8x16_t a2, const uint8x16_t a1,
  45. const uint8x16_t v0, const uint8x16_t b1,
  46. const uint8x16_t b2) {
  47. const uint8x16_t k1 = vrhaddq_u8(a2, a1);
  48. const uint8x16_t k2 = vrhaddq_u8(b2, b1);
  49. const uint8x16_t k3 = vrhaddq_u8(k1, k2);
  50. return vrhaddq_u8(k3, v0);
  51. }
  52. static uint8x16_t generate_maskq(const uint8x16_t a2, const uint8x16_t a1,
  53. const uint8x16_t v0, const uint8x16_t b1,
  54. const uint8x16_t b2, const uint8x16_t filter) {
  55. const uint8x16_t a2_v0 = vabdq_u8(a2, v0);
  56. const uint8x16_t a1_v0 = vabdq_u8(a1, v0);
  57. const uint8x16_t b1_v0 = vabdq_u8(b1, v0);
  58. const uint8x16_t b2_v0 = vabdq_u8(b2, v0);
  59. uint8x16_t max = vmaxq_u8(a2_v0, a1_v0);
  60. max = vmaxq_u8(b1_v0, max);
  61. max = vmaxq_u8(b2_v0, max);
  62. return vcltq_u8(max, filter);
  63. }
  64. static uint8x16_t generate_outputq(const uint8x16_t a2, const uint8x16_t a1,
  65. const uint8x16_t v0, const uint8x16_t b1,
  66. const uint8x16_t b2,
  67. const uint8x16_t filter) {
  68. const uint8x16_t k_out = average_k_outq(a2, a1, v0, b1, b2);
  69. const uint8x16_t mask = generate_maskq(a2, a1, v0, b1, b2, filter);
  70. return vbslq_u8(mask, k_out, v0);
  71. }
  72. void vpx_post_proc_down_and_across_mb_row_neon(uint8_t *src_ptr,
  73. uint8_t *dst_ptr, int src_stride,
  74. int dst_stride, int cols,
  75. uint8_t *f, int size) {
  76. uint8_t *src, *dst;
  77. int row;
  78. int col;
  79. // While columns of length 16 can be processed, load them.
  80. for (col = 0; col < cols - 8; col += 16) {
  81. uint8x16_t a0, a1, a2, a3, a4, a5, a6, a7;
  82. src = src_ptr - 2 * src_stride;
  83. dst = dst_ptr;
  84. a0 = vld1q_u8(src);
  85. src += src_stride;
  86. a1 = vld1q_u8(src);
  87. src += src_stride;
  88. a2 = vld1q_u8(src);
  89. src += src_stride;
  90. a3 = vld1q_u8(src);
  91. src += src_stride;
  92. for (row = 0; row < size; row += 4) {
  93. uint8x16_t v_out_0, v_out_1, v_out_2, v_out_3;
  94. const uint8x16_t filterq = vld1q_u8(f + col);
  95. a4 = vld1q_u8(src);
  96. src += src_stride;
  97. a5 = vld1q_u8(src);
  98. src += src_stride;
  99. a6 = vld1q_u8(src);
  100. src += src_stride;
  101. a7 = vld1q_u8(src);
  102. src += src_stride;
  103. v_out_0 = generate_outputq(a0, a1, a2, a3, a4, filterq);
  104. v_out_1 = generate_outputq(a1, a2, a3, a4, a5, filterq);
  105. v_out_2 = generate_outputq(a2, a3, a4, a5, a6, filterq);
  106. v_out_3 = generate_outputq(a3, a4, a5, a6, a7, filterq);
  107. vst1q_u8(dst, v_out_0);
  108. dst += dst_stride;
  109. vst1q_u8(dst, v_out_1);
  110. dst += dst_stride;
  111. vst1q_u8(dst, v_out_2);
  112. dst += dst_stride;
  113. vst1q_u8(dst, v_out_3);
  114. dst += dst_stride;
  115. // Rotate over to the next slot.
  116. a0 = a4;
  117. a1 = a5;
  118. a2 = a6;
  119. a3 = a7;
  120. }
  121. src_ptr += 16;
  122. dst_ptr += 16;
  123. }
  124. // Clean up any left over column of length 8.
  125. if (col != cols) {
  126. uint8x8_t a0, a1, a2, a3, a4, a5, a6, a7;
  127. src = src_ptr - 2 * src_stride;
  128. dst = dst_ptr;
  129. a0 = vld1_u8(src);
  130. src += src_stride;
  131. a1 = vld1_u8(src);
  132. src += src_stride;
  133. a2 = vld1_u8(src);
  134. src += src_stride;
  135. a3 = vld1_u8(src);
  136. src += src_stride;
  137. for (row = 0; row < size; row += 4) {
  138. uint8x8_t v_out_0, v_out_1, v_out_2, v_out_3;
  139. const uint8x8_t filter = vld1_u8(f + col);
  140. a4 = vld1_u8(src);
  141. src += src_stride;
  142. a5 = vld1_u8(src);
  143. src += src_stride;
  144. a6 = vld1_u8(src);
  145. src += src_stride;
  146. a7 = vld1_u8(src);
  147. src += src_stride;
  148. v_out_0 = generate_output(a0, a1, a2, a3, a4, filter);
  149. v_out_1 = generate_output(a1, a2, a3, a4, a5, filter);
  150. v_out_2 = generate_output(a2, a3, a4, a5, a6, filter);
  151. v_out_3 = generate_output(a3, a4, a5, a6, a7, filter);
  152. vst1_u8(dst, v_out_0);
  153. dst += dst_stride;
  154. vst1_u8(dst, v_out_1);
  155. dst += dst_stride;
  156. vst1_u8(dst, v_out_2);
  157. dst += dst_stride;
  158. vst1_u8(dst, v_out_3);
  159. dst += dst_stride;
  160. // Rotate over to the next slot.
  161. a0 = a4;
  162. a1 = a5;
  163. a2 = a6;
  164. a3 = a7;
  165. }
  166. // Not strictly necessary but makes resetting dst_ptr easier.
  167. dst_ptr += 8;
  168. }
  169. dst_ptr -= cols;
  170. for (row = 0; row < size; row += 8) {
  171. uint8x8_t a0, a1, a2, a3;
  172. uint8x8_t b0, b1, b2, b3, b4, b5, b6, b7;
  173. src = dst_ptr;
  174. dst = dst_ptr;
  175. // Load 8 values, transpose 4 of them, and discard 2 because they will be
  176. // reloaded later.
  177. load_and_transpose_u8_4x8(src, dst_stride, &a0, &a1, &a2, &a3);
  178. a3 = a1;
  179. a2 = a1 = a0; // Extend left border.
  180. src += 2;
  181. for (col = 0; col < cols; col += 8) {
  182. uint8x8_t v_out_0, v_out_1, v_out_2, v_out_3, v_out_4, v_out_5, v_out_6,
  183. v_out_7;
  184. // Although the filter is meant to be applied vertically and is instead
  185. // being applied horizontally here it's OK because it's set in blocks of 8
  186. // (or 16).
  187. const uint8x8_t filter = vld1_u8(f + col);
  188. load_and_transpose_u8_8x8(src, dst_stride, &b0, &b1, &b2, &b3, &b4, &b5,
  189. &b6, &b7);
  190. if (col + 8 == cols) {
  191. // Last row. Extend border (b5).
  192. b6 = b7 = b5;
  193. }
  194. v_out_0 = generate_output(a0, a1, a2, a3, b0, filter);
  195. v_out_1 = generate_output(a1, a2, a3, b0, b1, filter);
  196. v_out_2 = generate_output(a2, a3, b0, b1, b2, filter);
  197. v_out_3 = generate_output(a3, b0, b1, b2, b3, filter);
  198. v_out_4 = generate_output(b0, b1, b2, b3, b4, filter);
  199. v_out_5 = generate_output(b1, b2, b3, b4, b5, filter);
  200. v_out_6 = generate_output(b2, b3, b4, b5, b6, filter);
  201. v_out_7 = generate_output(b3, b4, b5, b6, b7, filter);
  202. transpose_and_store_u8_8x8(dst, dst_stride, v_out_0, v_out_1, v_out_2,
  203. v_out_3, v_out_4, v_out_5, v_out_6, v_out_7);
  204. a0 = b4;
  205. a1 = b5;
  206. a2 = b6;
  207. a3 = b7;
  208. src += 8;
  209. dst += 8;
  210. }
  211. dst_ptr += 8 * dst_stride;
  212. }
  213. }
  214. // sum += x;
  215. // sumsq += x * y;
  216. static void accumulate_sum_sumsq(const int16x4_t x, const int32x4_t xy,
  217. int16x4_t *const sum, int32x4_t *const sumsq) {
  218. const int16x4_t zero = vdup_n_s16(0);
  219. const int32x4_t zeroq = vdupq_n_s32(0);
  220. // Add in the first set because vext doesn't work with '0'.
  221. *sum = vadd_s16(*sum, x);
  222. *sumsq = vaddq_s32(*sumsq, xy);
  223. // Shift x and xy to the right and sum. vext requires an immediate.
  224. *sum = vadd_s16(*sum, vext_s16(zero, x, 1));
  225. *sumsq = vaddq_s32(*sumsq, vextq_s32(zeroq, xy, 1));
  226. *sum = vadd_s16(*sum, vext_s16(zero, x, 2));
  227. *sumsq = vaddq_s32(*sumsq, vextq_s32(zeroq, xy, 2));
  228. *sum = vadd_s16(*sum, vext_s16(zero, x, 3));
  229. *sumsq = vaddq_s32(*sumsq, vextq_s32(zeroq, xy, 3));
  230. }
  231. // Generate mask based on (sumsq * 15 - sum * sum < flimit)
  232. static uint16x4_t calculate_mask(const int16x4_t sum, const int32x4_t sumsq,
  233. const int32x4_t f, const int32x4_t fifteen) {
  234. const int32x4_t a = vmulq_s32(sumsq, fifteen);
  235. const int32x4_t b = vmlsl_s16(a, sum, sum);
  236. const uint32x4_t mask32 = vcltq_s32(b, f);
  237. return vmovn_u32(mask32);
  238. }
  239. static uint8x8_t combine_mask(const int16x4_t sum_low, const int16x4_t sum_high,
  240. const int32x4_t sumsq_low,
  241. const int32x4_t sumsq_high, const int32x4_t f) {
  242. const int32x4_t fifteen = vdupq_n_s32(15);
  243. const uint16x4_t mask16_low = calculate_mask(sum_low, sumsq_low, f, fifteen);
  244. const uint16x4_t mask16_high =
  245. calculate_mask(sum_high, sumsq_high, f, fifteen);
  246. return vmovn_u16(vcombine_u16(mask16_low, mask16_high));
  247. }
  248. // Apply filter of (8 + sum + s[c]) >> 4.
  249. static uint8x8_t filter_pixels(const int16x8_t sum, const uint8x8_t s) {
  250. const int16x8_t s16 = vreinterpretq_s16_u16(vmovl_u8(s));
  251. const int16x8_t sum_s = vaddq_s16(sum, s16);
  252. return vqrshrun_n_s16(sum_s, 4);
  253. }
  254. void vpx_mbpost_proc_across_ip_neon(uint8_t *src, int pitch, int rows, int cols,
  255. int flimit) {
  256. int row, col;
  257. const int32x4_t f = vdupq_n_s32(flimit);
  258. assert(cols % 8 == 0);
  259. for (row = 0; row < rows; ++row) {
  260. // Sum the first 8 elements, which are extended from s[0].
  261. // sumsq gets primed with +16.
  262. int sumsq = src[0] * src[0] * 9 + 16;
  263. int sum = src[0] * 9;
  264. uint8x8_t left_context, s, right_context;
  265. int16x4_t sum_low, sum_high;
  266. int32x4_t sumsq_low, sumsq_high;
  267. // Sum (+square) the next 6 elements.
  268. // Skip [0] because it's included above.
  269. for (col = 1; col <= 6; ++col) {
  270. sumsq += src[col] * src[col];
  271. sum += src[col];
  272. }
  273. // Prime the sums. Later the loop uses the _high values to prime the new
  274. // vectors.
  275. sumsq_high = vdupq_n_s32(sumsq);
  276. sum_high = vdup_n_s16(sum);
  277. // Manually extend the left border.
  278. left_context = vdup_n_u8(src[0]);
  279. for (col = 0; col < cols; col += 8) {
  280. uint8x8_t mask, output;
  281. int16x8_t x, y;
  282. int32x4_t xy_low, xy_high;
  283. s = vld1_u8(src + col);
  284. if (col + 8 == cols) {
  285. // Last row. Extend border.
  286. right_context = vdup_n_u8(src[col + 7]);
  287. } else {
  288. right_context = vld1_u8(src + col + 7);
  289. }
  290. x = vreinterpretq_s16_u16(vsubl_u8(right_context, left_context));
  291. y = vreinterpretq_s16_u16(vaddl_u8(right_context, left_context));
  292. xy_low = vmull_s16(vget_low_s16(x), vget_low_s16(y));
  293. xy_high = vmull_s16(vget_high_s16(x), vget_high_s16(y));
  294. // Catch up to the last sum'd value.
  295. sum_low = vdup_lane_s16(sum_high, 3);
  296. sumsq_low = vdupq_lane_s32(vget_high_s32(sumsq_high), 1);
  297. accumulate_sum_sumsq(vget_low_s16(x), xy_low, &sum_low, &sumsq_low);
  298. // Need to do this sequentially because we need the max value from
  299. // sum_low.
  300. sum_high = vdup_lane_s16(sum_low, 3);
  301. sumsq_high = vdupq_lane_s32(vget_high_s32(sumsq_low), 1);
  302. accumulate_sum_sumsq(vget_high_s16(x), xy_high, &sum_high, &sumsq_high);
  303. mask = combine_mask(sum_low, sum_high, sumsq_low, sumsq_high, f);
  304. output = filter_pixels(vcombine_s16(sum_low, sum_high), s);
  305. output = vbsl_u8(mask, output, s);
  306. vst1_u8(src + col, output);
  307. left_context = s;
  308. }
  309. src += pitch;
  310. }
  311. }
  312. // Apply filter of (vpx_rv + sum + s[c]) >> 4.
  313. static uint8x8_t filter_pixels_rv(const int16x8_t sum, const uint8x8_t s,
  314. const int16x8_t rv) {
  315. const int16x8_t s16 = vreinterpretq_s16_u16(vmovl_u8(s));
  316. const int16x8_t sum_s = vaddq_s16(sum, s16);
  317. const int16x8_t rounded = vaddq_s16(sum_s, rv);
  318. return vqshrun_n_s16(rounded, 4);
  319. }
  320. void vpx_mbpost_proc_down_neon(uint8_t *dst, int pitch, int rows, int cols,
  321. int flimit) {
  322. int row, col, i;
  323. const int32x4_t f = vdupq_n_s32(flimit);
  324. uint8x8_t below_context = vdup_n_u8(0);
  325. // 8 columns are processed at a time.
  326. // If rows is less than 8 the bottom border extension fails.
  327. assert(cols % 8 == 0);
  328. assert(rows >= 8);
  329. // Load and keep the first 8 values in memory. Process a vertical stripe that
  330. // is 8 wide.
  331. for (col = 0; col < cols; col += 8) {
  332. uint8x8_t s, above_context[8];
  333. int16x8_t sum, sum_tmp;
  334. int32x4_t sumsq_low, sumsq_high;
  335. // Load and extend the top border.
  336. s = vld1_u8(dst);
  337. for (i = 0; i < 8; i++) {
  338. above_context[i] = s;
  339. }
  340. sum_tmp = vreinterpretq_s16_u16(vmovl_u8(s));
  341. // sum * 9
  342. sum = vmulq_n_s16(sum_tmp, 9);
  343. // (sum * 9) * sum == sum * sum * 9
  344. sumsq_low = vmull_s16(vget_low_s16(sum), vget_low_s16(sum_tmp));
  345. sumsq_high = vmull_s16(vget_high_s16(sum), vget_high_s16(sum_tmp));
  346. // Load and discard the next 6 values to prime sum and sumsq.
  347. for (i = 1; i <= 6; ++i) {
  348. const uint8x8_t a = vld1_u8(dst + i * pitch);
  349. const int16x8_t b = vreinterpretq_s16_u16(vmovl_u8(a));
  350. sum = vaddq_s16(sum, b);
  351. sumsq_low = vmlal_s16(sumsq_low, vget_low_s16(b), vget_low_s16(b));
  352. sumsq_high = vmlal_s16(sumsq_high, vget_high_s16(b), vget_high_s16(b));
  353. }
  354. for (row = 0; row < rows; ++row) {
  355. uint8x8_t mask, output;
  356. int16x8_t x, y;
  357. int32x4_t xy_low, xy_high;
  358. s = vld1_u8(dst + row * pitch);
  359. // Extend the bottom border.
  360. if (row + 7 < rows) {
  361. below_context = vld1_u8(dst + (row + 7) * pitch);
  362. }
  363. x = vreinterpretq_s16_u16(vsubl_u8(below_context, above_context[0]));
  364. y = vreinterpretq_s16_u16(vaddl_u8(below_context, above_context[0]));
  365. xy_low = vmull_s16(vget_low_s16(x), vget_low_s16(y));
  366. xy_high = vmull_s16(vget_high_s16(x), vget_high_s16(y));
  367. sum = vaddq_s16(sum, x);
  368. sumsq_low = vaddq_s32(sumsq_low, xy_low);
  369. sumsq_high = vaddq_s32(sumsq_high, xy_high);
  370. mask = combine_mask(vget_low_s16(sum), vget_high_s16(sum), sumsq_low,
  371. sumsq_high, f);
  372. output = filter_pixels_rv(sum, s, vld1q_s16(vpx_rv + (row & 127)));
  373. output = vbsl_u8(mask, output, s);
  374. vst1_u8(dst + row * pitch, output);
  375. above_context[0] = above_context[1];
  376. above_context[1] = above_context[2];
  377. above_context[2] = above_context[3];
  378. above_context[3] = above_context[4];
  379. above_context[4] = above_context[5];
  380. above_context[5] = above_context[6];
  381. above_context[6] = above_context[7];
  382. above_context[7] = s;
  383. }
  384. dst += 8;
  385. }
  386. }