sharpyuv_neon.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2022 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Speed-critical functions for Sharp YUV.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "sharpyuv/sharpyuv_dsp.h"
  14. #if defined(WEBP_USE_NEON)
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <arm_neon.h>
  18. #endif
  19. extern void InitSharpYuvNEON(void);
  20. #if defined(WEBP_USE_NEON)
  21. static uint16_t clip_NEON(int v, int max) {
  22. return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
  23. }
  24. static uint64_t SharpYuvUpdateY_NEON(const uint16_t* ref, const uint16_t* src,
  25. uint16_t* dst, int len, int bit_depth) {
  26. const int max_y = (1 << bit_depth) - 1;
  27. int i;
  28. const int16x8_t zero = vdupq_n_s16(0);
  29. const int16x8_t max = vdupq_n_s16(max_y);
  30. uint64x2_t sum = vdupq_n_u64(0);
  31. uint64_t diff;
  32. for (i = 0; i + 8 <= len; i += 8) {
  33. const int16x8_t A = vreinterpretq_s16_u16(vld1q_u16(ref + i));
  34. const int16x8_t B = vreinterpretq_s16_u16(vld1q_u16(src + i));
  35. const int16x8_t C = vreinterpretq_s16_u16(vld1q_u16(dst + i));
  36. const int16x8_t D = vsubq_s16(A, B); // diff_y
  37. const int16x8_t F = vaddq_s16(C, D); // new_y
  38. const uint16x8_t H =
  39. vreinterpretq_u16_s16(vmaxq_s16(vminq_s16(F, max), zero));
  40. const int16x8_t I = vabsq_s16(D); // abs(diff_y)
  41. vst1q_u16(dst + i, H);
  42. sum = vpadalq_u32(sum, vpaddlq_u16(vreinterpretq_u16_s16(I)));
  43. }
  44. diff = vgetq_lane_u64(sum, 0) + vgetq_lane_u64(sum, 1);
  45. for (; i < len; ++i) {
  46. const int diff_y = ref[i] - src[i];
  47. const int new_y = (int)(dst[i]) + diff_y;
  48. dst[i] = clip_NEON(new_y, max_y);
  49. diff += (uint64_t)(abs(diff_y));
  50. }
  51. return diff;
  52. }
  53. static void SharpYuvUpdateRGB_NEON(const int16_t* ref, const int16_t* src,
  54. int16_t* dst, int len) {
  55. int i;
  56. for (i = 0; i + 8 <= len; i += 8) {
  57. const int16x8_t A = vld1q_s16(ref + i);
  58. const int16x8_t B = vld1q_s16(src + i);
  59. const int16x8_t C = vld1q_s16(dst + i);
  60. const int16x8_t D = vsubq_s16(A, B); // diff_uv
  61. const int16x8_t E = vaddq_s16(C, D); // new_uv
  62. vst1q_s16(dst + i, E);
  63. }
  64. for (; i < len; ++i) {
  65. const int diff_uv = ref[i] - src[i];
  66. dst[i] += diff_uv;
  67. }
  68. }
  69. static void SharpYuvFilterRow16_NEON(const int16_t* A, const int16_t* B,
  70. int len, const uint16_t* best_y,
  71. uint16_t* out, int bit_depth) {
  72. const int max_y = (1 << bit_depth) - 1;
  73. int i;
  74. const int16x8_t max = vdupq_n_s16(max_y);
  75. const int16x8_t zero = vdupq_n_s16(0);
  76. for (i = 0; i + 8 <= len; i += 8) {
  77. const int16x8_t a0 = vld1q_s16(A + i + 0);
  78. const int16x8_t a1 = vld1q_s16(A + i + 1);
  79. const int16x8_t b0 = vld1q_s16(B + i + 0);
  80. const int16x8_t b1 = vld1q_s16(B + i + 1);
  81. const int16x8_t a0b1 = vaddq_s16(a0, b1);
  82. const int16x8_t a1b0 = vaddq_s16(a1, b0);
  83. const int16x8_t a0a1b0b1 = vaddq_s16(a0b1, a1b0); // A0+A1+B0+B1
  84. const int16x8_t a0b1_2 = vaddq_s16(a0b1, a0b1); // 2*(A0+B1)
  85. const int16x8_t a1b0_2 = vaddq_s16(a1b0, a1b0); // 2*(A1+B0)
  86. const int16x8_t c0 = vshrq_n_s16(vaddq_s16(a0b1_2, a0a1b0b1), 3);
  87. const int16x8_t c1 = vshrq_n_s16(vaddq_s16(a1b0_2, a0a1b0b1), 3);
  88. const int16x8_t e0 = vrhaddq_s16(c1, a0);
  89. const int16x8_t e1 = vrhaddq_s16(c0, a1);
  90. const int16x8x2_t f = vzipq_s16(e0, e1);
  91. const int16x8_t g0 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 0));
  92. const int16x8_t g1 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 8));
  93. const int16x8_t h0 = vaddq_s16(g0, f.val[0]);
  94. const int16x8_t h1 = vaddq_s16(g1, f.val[1]);
  95. const int16x8_t i0 = vmaxq_s16(vminq_s16(h0, max), zero);
  96. const int16x8_t i1 = vmaxq_s16(vminq_s16(h1, max), zero);
  97. vst1q_u16(out + 2 * i + 0, vreinterpretq_u16_s16(i0));
  98. vst1q_u16(out + 2 * i + 8, vreinterpretq_u16_s16(i1));
  99. }
  100. for (; i < len; ++i) {
  101. const int a0b1 = A[i + 0] + B[i + 1];
  102. const int a1b0 = A[i + 1] + B[i + 0];
  103. const int a0a1b0b1 = a0b1 + a1b0 + 8;
  104. const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
  105. const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
  106. out[2 * i + 0] = clip_NEON(best_y[2 * i + 0] + v0, max_y);
  107. out[2 * i + 1] = clip_NEON(best_y[2 * i + 1] + v1, max_y);
  108. }
  109. }
  110. static void SharpYuvFilterRow32_NEON(const int16_t* A, const int16_t* B,
  111. int len, const uint16_t* best_y,
  112. uint16_t* out, int bit_depth) {
  113. const int max_y = (1 << bit_depth) - 1;
  114. int i;
  115. const uint16x8_t max = vdupq_n_u16(max_y);
  116. for (i = 0; i + 4 <= len; i += 4) {
  117. const int16x4_t a0 = vld1_s16(A + i + 0);
  118. const int16x4_t a1 = vld1_s16(A + i + 1);
  119. const int16x4_t b0 = vld1_s16(B + i + 0);
  120. const int16x4_t b1 = vld1_s16(B + i + 1);
  121. const int32x4_t a0b1 = vaddl_s16(a0, b1);
  122. const int32x4_t a1b0 = vaddl_s16(a1, b0);
  123. const int32x4_t a0a1b0b1 = vaddq_s32(a0b1, a1b0); // A0+A1+B0+B1
  124. const int32x4_t a0b1_2 = vaddq_s32(a0b1, a0b1); // 2*(A0+B1)
  125. const int32x4_t a1b0_2 = vaddq_s32(a1b0, a1b0); // 2*(A1+B0)
  126. const int32x4_t c0 = vshrq_n_s32(vaddq_s32(a0b1_2, a0a1b0b1), 3);
  127. const int32x4_t c1 = vshrq_n_s32(vaddq_s32(a1b0_2, a0a1b0b1), 3);
  128. const int32x4_t e0 = vrhaddq_s32(c1, vmovl_s16(a0));
  129. const int32x4_t e1 = vrhaddq_s32(c0, vmovl_s16(a1));
  130. const int32x4x2_t f = vzipq_s32(e0, e1);
  131. const int16x8_t g = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i));
  132. const int32x4_t h0 = vaddw_s16(f.val[0], vget_low_s16(g));
  133. const int32x4_t h1 = vaddw_s16(f.val[1], vget_high_s16(g));
  134. const uint16x8_t i_16 = vcombine_u16(vqmovun_s32(h0), vqmovun_s32(h1));
  135. const uint16x8_t i_clamped = vminq_u16(i_16, max);
  136. vst1q_u16(out + 2 * i + 0, i_clamped);
  137. }
  138. for (; i < len; ++i) {
  139. const int a0b1 = A[i + 0] + B[i + 1];
  140. const int a1b0 = A[i + 1] + B[i + 0];
  141. const int a0a1b0b1 = a0b1 + a1b0 + 8;
  142. const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
  143. const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
  144. out[2 * i + 0] = clip_NEON(best_y[2 * i + 0] + v0, max_y);
  145. out[2 * i + 1] = clip_NEON(best_y[2 * i + 1] + v1, max_y);
  146. }
  147. }
  148. static void SharpYuvFilterRow_NEON(const int16_t* A, const int16_t* B, int len,
  149. const uint16_t* best_y, uint16_t* out,
  150. int bit_depth) {
  151. if (bit_depth <= 10) {
  152. SharpYuvFilterRow16_NEON(A, B, len, best_y, out, bit_depth);
  153. } else {
  154. SharpYuvFilterRow32_NEON(A, B, len, best_y, out, bit_depth);
  155. }
  156. }
  157. //------------------------------------------------------------------------------
  158. WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvNEON(void) {
  159. SharpYuvUpdateY = SharpYuvUpdateY_NEON;
  160. SharpYuvUpdateRGB = SharpYuvUpdateRGB_NEON;
  161. SharpYuvFilterRow = SharpYuvFilterRow_NEON;
  162. }
  163. #else // !WEBP_USE_NEON
  164. void InitSharpYuvNEON(void) {}
  165. #endif // WEBP_USE_NEON