sharpyuv_neon.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. static uint16_t clip_NEON(int v, int max) {
  19. return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
  20. }
  21. static uint64_t SharpYuvUpdateY_NEON(const uint16_t* ref, const uint16_t* src,
  22. uint16_t* dst, int len, int bit_depth) {
  23. const int max_y = (1 << bit_depth) - 1;
  24. int i;
  25. const int16x8_t zero = vdupq_n_s16(0);
  26. const int16x8_t max = vdupq_n_s16(max_y);
  27. uint64x2_t sum = vdupq_n_u64(0);
  28. uint64_t diff;
  29. for (i = 0; i + 8 <= len; i += 8) {
  30. const int16x8_t A = vreinterpretq_s16_u16(vld1q_u16(ref + i));
  31. const int16x8_t B = vreinterpretq_s16_u16(vld1q_u16(src + i));
  32. const int16x8_t C = vreinterpretq_s16_u16(vld1q_u16(dst + i));
  33. const int16x8_t D = vsubq_s16(A, B); // diff_y
  34. const int16x8_t F = vaddq_s16(C, D); // new_y
  35. const uint16x8_t H =
  36. vreinterpretq_u16_s16(vmaxq_s16(vminq_s16(F, max), zero));
  37. const int16x8_t I = vabsq_s16(D); // abs(diff_y)
  38. vst1q_u16(dst + i, H);
  39. sum = vpadalq_u32(sum, vpaddlq_u16(vreinterpretq_u16_s16(I)));
  40. }
  41. diff = vgetq_lane_u64(sum, 0) + vgetq_lane_u64(sum, 1);
  42. for (; i < len; ++i) {
  43. const int diff_y = ref[i] - src[i];
  44. const int new_y = (int)(dst[i]) + diff_y;
  45. dst[i] = clip_NEON(new_y, max_y);
  46. diff += (uint64_t)(abs(diff_y));
  47. }
  48. return diff;
  49. }
  50. static void SharpYuvUpdateRGB_NEON(const int16_t* ref, const int16_t* src,
  51. int16_t* dst, int len) {
  52. int i;
  53. for (i = 0; i + 8 <= len; i += 8) {
  54. const int16x8_t A = vld1q_s16(ref + i);
  55. const int16x8_t B = vld1q_s16(src + i);
  56. const int16x8_t C = vld1q_s16(dst + i);
  57. const int16x8_t D = vsubq_s16(A, B); // diff_uv
  58. const int16x8_t E = vaddq_s16(C, D); // new_uv
  59. vst1q_s16(dst + i, E);
  60. }
  61. for (; i < len; ++i) {
  62. const int diff_uv = ref[i] - src[i];
  63. dst[i] += diff_uv;
  64. }
  65. }
  66. static void SharpYuvFilterRow16_NEON(const int16_t* A, const int16_t* B,
  67. int len, const uint16_t* best_y,
  68. uint16_t* out, int bit_depth) {
  69. const int max_y = (1 << bit_depth) - 1;
  70. int i;
  71. const int16x8_t max = vdupq_n_s16(max_y);
  72. const int16x8_t zero = vdupq_n_s16(0);
  73. for (i = 0; i + 8 <= len; i += 8) {
  74. const int16x8_t a0 = vld1q_s16(A + i + 0);
  75. const int16x8_t a1 = vld1q_s16(A + i + 1);
  76. const int16x8_t b0 = vld1q_s16(B + i + 0);
  77. const int16x8_t b1 = vld1q_s16(B + i + 1);
  78. const int16x8_t a0b1 = vaddq_s16(a0, b1);
  79. const int16x8_t a1b0 = vaddq_s16(a1, b0);
  80. const int16x8_t a0a1b0b1 = vaddq_s16(a0b1, a1b0); // A0+A1+B0+B1
  81. const int16x8_t a0b1_2 = vaddq_s16(a0b1, a0b1); // 2*(A0+B1)
  82. const int16x8_t a1b0_2 = vaddq_s16(a1b0, a1b0); // 2*(A1+B0)
  83. const int16x8_t c0 = vshrq_n_s16(vaddq_s16(a0b1_2, a0a1b0b1), 3);
  84. const int16x8_t c1 = vshrq_n_s16(vaddq_s16(a1b0_2, a0a1b0b1), 3);
  85. const int16x8_t e0 = vrhaddq_s16(c1, a0);
  86. const int16x8_t e1 = vrhaddq_s16(c0, a1);
  87. const int16x8x2_t f = vzipq_s16(e0, e1);
  88. const int16x8_t g0 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 0));
  89. const int16x8_t g1 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 8));
  90. const int16x8_t h0 = vaddq_s16(g0, f.val[0]);
  91. const int16x8_t h1 = vaddq_s16(g1, f.val[1]);
  92. const int16x8_t i0 = vmaxq_s16(vminq_s16(h0, max), zero);
  93. const int16x8_t i1 = vmaxq_s16(vminq_s16(h1, max), zero);
  94. vst1q_u16(out + 2 * i + 0, vreinterpretq_u16_s16(i0));
  95. vst1q_u16(out + 2 * i + 8, vreinterpretq_u16_s16(i1));
  96. }
  97. for (; i < len; ++i) {
  98. const int a0b1 = A[i + 0] + B[i + 1];
  99. const int a1b0 = A[i + 1] + B[i + 0];
  100. const int a0a1b0b1 = a0b1 + a1b0 + 8;
  101. const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
  102. const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
  103. out[2 * i + 0] = clip_NEON(best_y[2 * i + 0] + v0, max_y);
  104. out[2 * i + 1] = clip_NEON(best_y[2 * i + 1] + v1, max_y);
  105. }
  106. }
  107. static void SharpYuvFilterRow32_NEON(const int16_t* A, const int16_t* B,
  108. int len, const uint16_t* best_y,
  109. uint16_t* out, int bit_depth) {
  110. const int max_y = (1 << bit_depth) - 1;
  111. int i;
  112. const uint16x8_t max = vdupq_n_u16(max_y);
  113. for (i = 0; i + 4 <= len; i += 4) {
  114. const int16x4_t a0 = vld1_s16(A + i + 0);
  115. const int16x4_t a1 = vld1_s16(A + i + 1);
  116. const int16x4_t b0 = vld1_s16(B + i + 0);
  117. const int16x4_t b1 = vld1_s16(B + i + 1);
  118. const int32x4_t a0b1 = vaddl_s16(a0, b1);
  119. const int32x4_t a1b0 = vaddl_s16(a1, b0);
  120. const int32x4_t a0a1b0b1 = vaddq_s32(a0b1, a1b0); // A0+A1+B0+B1
  121. const int32x4_t a0b1_2 = vaddq_s32(a0b1, a0b1); // 2*(A0+B1)
  122. const int32x4_t a1b0_2 = vaddq_s32(a1b0, a1b0); // 2*(A1+B0)
  123. const int32x4_t c0 = vshrq_n_s32(vaddq_s32(a0b1_2, a0a1b0b1), 3);
  124. const int32x4_t c1 = vshrq_n_s32(vaddq_s32(a1b0_2, a0a1b0b1), 3);
  125. const int32x4_t e0 = vrhaddq_s32(c1, vmovl_s16(a0));
  126. const int32x4_t e1 = vrhaddq_s32(c0, vmovl_s16(a1));
  127. const int32x4x2_t f = vzipq_s32(e0, e1);
  128. const int16x8_t g = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i));
  129. const int32x4_t h0 = vaddw_s16(f.val[0], vget_low_s16(g));
  130. const int32x4_t h1 = vaddw_s16(f.val[1], vget_high_s16(g));
  131. const uint16x8_t i_16 = vcombine_u16(vqmovun_s32(h0), vqmovun_s32(h1));
  132. const uint16x8_t i_clamped = vminq_u16(i_16, max);
  133. vst1q_u16(out + 2 * i + 0, i_clamped);
  134. }
  135. for (; i < len; ++i) {
  136. const int a0b1 = A[i + 0] + B[i + 1];
  137. const int a1b0 = A[i + 1] + B[i + 0];
  138. const int a0a1b0b1 = a0b1 + a1b0 + 8;
  139. const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
  140. const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
  141. out[2 * i + 0] = clip_NEON(best_y[2 * i + 0] + v0, max_y);
  142. out[2 * i + 1] = clip_NEON(best_y[2 * i + 1] + v1, max_y);
  143. }
  144. }
  145. static void SharpYuvFilterRow_NEON(const int16_t* A, const int16_t* B, int len,
  146. const uint16_t* best_y, uint16_t* out,
  147. int bit_depth) {
  148. if (bit_depth <= 10) {
  149. SharpYuvFilterRow16_NEON(A, B, len, best_y, out, bit_depth);
  150. } else {
  151. SharpYuvFilterRow32_NEON(A, B, len, best_y, out, bit_depth);
  152. }
  153. }
  154. //------------------------------------------------------------------------------
  155. extern void InitSharpYuvNEON(void);
  156. WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvNEON(void) {
  157. SharpYuvUpdateY = SharpYuvUpdateY_NEON;
  158. SharpYuvUpdateRGB = SharpYuvUpdateRGB_NEON;
  159. SharpYuvFilterRow = SharpYuvFilterRow_NEON;
  160. }
  161. #else // !WEBP_USE_NEON
  162. extern void InitSharpYuvNEON(void);
  163. void InitSharpYuvNEON(void) {}
  164. #endif // WEBP_USE_NEON