yuv_neon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright 2017 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. // YUV->RGB conversion functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./yuv.h"
  14. #if defined(WEBP_USE_NEON)
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include "./neon.h"
  18. //-----------------------------------------------------------------------------
  19. static uint8x8_t ConvertRGBToY_NEON(const uint8x8_t R,
  20. const uint8x8_t G,
  21. const uint8x8_t B) {
  22. const uint16x8_t r = vmovl_u8(R);
  23. const uint16x8_t g = vmovl_u8(G);
  24. const uint16x8_t b = vmovl_u8(B);
  25. const uint16x4_t r_lo = vget_low_u16(r);
  26. const uint16x4_t r_hi = vget_high_u16(r);
  27. const uint16x4_t g_lo = vget_low_u16(g);
  28. const uint16x4_t g_hi = vget_high_u16(g);
  29. const uint16x4_t b_lo = vget_low_u16(b);
  30. const uint16x4_t b_hi = vget_high_u16(b);
  31. const uint32x4_t tmp0_lo = vmull_n_u16( r_lo, 16839u);
  32. const uint32x4_t tmp0_hi = vmull_n_u16( r_hi, 16839u);
  33. const uint32x4_t tmp1_lo = vmlal_n_u16(tmp0_lo, g_lo, 33059u);
  34. const uint32x4_t tmp1_hi = vmlal_n_u16(tmp0_hi, g_hi, 33059u);
  35. const uint32x4_t tmp2_lo = vmlal_n_u16(tmp1_lo, b_lo, 6420u);
  36. const uint32x4_t tmp2_hi = vmlal_n_u16(tmp1_hi, b_hi, 6420u);
  37. const uint16x8_t Y1 = vcombine_u16(vrshrn_n_u32(tmp2_lo, 16),
  38. vrshrn_n_u32(tmp2_hi, 16));
  39. const uint16x8_t Y2 = vaddq_u16(Y1, vdupq_n_u16(16));
  40. return vqmovn_u16(Y2);
  41. }
  42. static void ConvertRGB24ToY_NEON(const uint8_t* rgb, uint8_t* y, int width) {
  43. int i;
  44. for (i = 0; i + 8 <= width; i += 8, rgb += 3 * 8) {
  45. const uint8x8x3_t RGB = vld3_u8(rgb);
  46. const uint8x8_t Y = ConvertRGBToY_NEON(RGB.val[0], RGB.val[1], RGB.val[2]);
  47. vst1_u8(y + i, Y);
  48. }
  49. for (; i < width; ++i, rgb += 3) { // left-over
  50. y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
  51. }
  52. }
  53. static void ConvertBGR24ToY_NEON(const uint8_t* bgr, uint8_t* y, int width) {
  54. int i;
  55. for (i = 0; i + 8 <= width; i += 8, bgr += 3 * 8) {
  56. const uint8x8x3_t BGR = vld3_u8(bgr);
  57. const uint8x8_t Y = ConvertRGBToY_NEON(BGR.val[2], BGR.val[1], BGR.val[0]);
  58. vst1_u8(y + i, Y);
  59. }
  60. for (; i < width; ++i, bgr += 3) { // left-over
  61. y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
  62. }
  63. }
  64. static void ConvertARGBToY_NEON(const uint32_t* argb, uint8_t* y, int width) {
  65. int i;
  66. for (i = 0; i + 8 <= width; i += 8) {
  67. const uint8x8x4_t RGB = vld4_u8((const uint8_t*)&argb[i]);
  68. const uint8x8_t Y = ConvertRGBToY_NEON(RGB.val[2], RGB.val[1], RGB.val[0]);
  69. vst1_u8(y + i, Y);
  70. }
  71. for (; i < width; ++i) { // left-over
  72. const uint32_t p = argb[i];
  73. y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff,
  74. YUV_HALF);
  75. }
  76. }
  77. //-----------------------------------------------------------------------------
  78. // computes: DST_s16 = [(C0 * r + C1 * g + C2 * b) >> 16] + CST
  79. #define MULTIPLY_16b_PREAMBLE(r, g, b) \
  80. const int16x4_t r_lo = vreinterpret_s16_u16(vget_low_u16(r)); \
  81. const int16x4_t r_hi = vreinterpret_s16_u16(vget_high_u16(r)); \
  82. const int16x4_t g_lo = vreinterpret_s16_u16(vget_low_u16(g)); \
  83. const int16x4_t g_hi = vreinterpret_s16_u16(vget_high_u16(g)); \
  84. const int16x4_t b_lo = vreinterpret_s16_u16(vget_low_u16(b)); \
  85. const int16x4_t b_hi = vreinterpret_s16_u16(vget_high_u16(b))
  86. #define MULTIPLY_16b(C0, C1, C2, CST, DST_s16) do { \
  87. const int32x4_t tmp0_lo = vmull_n_s16( r_lo, C0); \
  88. const int32x4_t tmp0_hi = vmull_n_s16( r_hi, C0); \
  89. const int32x4_t tmp1_lo = vmlal_n_s16(tmp0_lo, g_lo, C1); \
  90. const int32x4_t tmp1_hi = vmlal_n_s16(tmp0_hi, g_hi, C1); \
  91. const int32x4_t tmp2_lo = vmlal_n_s16(tmp1_lo, b_lo, C2); \
  92. const int32x4_t tmp2_hi = vmlal_n_s16(tmp1_hi, b_hi, C2); \
  93. const int16x8_t tmp3 = vcombine_s16(vshrn_n_s32(tmp2_lo, 16), \
  94. vshrn_n_s32(tmp2_hi, 16)); \
  95. DST_s16 = vaddq_s16(tmp3, vdupq_n_s16(CST)); \
  96. } while (0)
  97. // This needs to be a macro, since (128 << SHIFT) needs to be an immediate.
  98. #define CONVERT_RGB_TO_UV(r, g, b, SHIFT, U_DST, V_DST) do { \
  99. MULTIPLY_16b_PREAMBLE(r, g, b); \
  100. MULTIPLY_16b(-9719, -19081, 28800, 128 << SHIFT, U_DST); \
  101. MULTIPLY_16b(28800, -24116, -4684, 128 << SHIFT, V_DST); \
  102. } while (0)
  103. static void ConvertRGBA32ToUV_NEON(const uint16_t* rgb,
  104. uint8_t* u, uint8_t* v, int width) {
  105. int i;
  106. for (i = 0; i + 8 <= width; i += 8, rgb += 4 * 8) {
  107. const uint16x8x4_t RGB = vld4q_u16((const uint16_t*)rgb);
  108. int16x8_t U, V;
  109. CONVERT_RGB_TO_UV(RGB.val[0], RGB.val[1], RGB.val[2], 2, U, V);
  110. vst1_u8(u + i, vqrshrun_n_s16(U, 2));
  111. vst1_u8(v + i, vqrshrun_n_s16(V, 2));
  112. }
  113. for (; i < width; i += 1, rgb += 4) {
  114. const int r = rgb[0], g = rgb[1], b = rgb[2];
  115. u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
  116. v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
  117. }
  118. }
  119. static void ConvertARGBToUV_NEON(const uint32_t* argb, uint8_t* u, uint8_t* v,
  120. int src_width, int do_store) {
  121. int i;
  122. for (i = 0; i + 16 <= src_width; i += 16, u += 8, v += 8) {
  123. const uint8x16x4_t RGB = vld4q_u8((const uint8_t*)&argb[i]);
  124. const uint16x8_t R = vpaddlq_u8(RGB.val[2]); // pair-wise adds
  125. const uint16x8_t G = vpaddlq_u8(RGB.val[1]);
  126. const uint16x8_t B = vpaddlq_u8(RGB.val[0]);
  127. int16x8_t U_tmp, V_tmp;
  128. CONVERT_RGB_TO_UV(R, G, B, 1, U_tmp, V_tmp);
  129. {
  130. const uint8x8_t U = vqrshrun_n_s16(U_tmp, 1);
  131. const uint8x8_t V = vqrshrun_n_s16(V_tmp, 1);
  132. if (do_store) {
  133. vst1_u8(u, U);
  134. vst1_u8(v, V);
  135. } else {
  136. const uint8x8_t prev_u = vld1_u8(u);
  137. const uint8x8_t prev_v = vld1_u8(v);
  138. vst1_u8(u, vrhadd_u8(U, prev_u));
  139. vst1_u8(v, vrhadd_u8(V, prev_v));
  140. }
  141. }
  142. }
  143. if (i < src_width) { // left-over
  144. WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store);
  145. }
  146. }
  147. //------------------------------------------------------------------------------
  148. extern void WebPInitConvertARGBToYUVNEON(void);
  149. WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVNEON(void) {
  150. WebPConvertRGB24ToY = ConvertRGB24ToY_NEON;
  151. WebPConvertBGR24ToY = ConvertBGR24ToY_NEON;
  152. WebPConvertARGBToY = ConvertARGBToY_NEON;
  153. WebPConvertARGBToUV = ConvertARGBToUV_NEON;
  154. WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_NEON;
  155. }
  156. //------------------------------------------------------------------------------
  157. #define MAX_Y ((1 << 10) - 1) // 10b precision over 16b-arithmetic
  158. static uint16_t clip_y_NEON(int v) {
  159. return (v < 0) ? 0 : (v > MAX_Y) ? MAX_Y : (uint16_t)v;
  160. }
  161. static uint64_t SharpYUVUpdateY_NEON(const uint16_t* ref, const uint16_t* src,
  162. uint16_t* dst, int len) {
  163. int i;
  164. const int16x8_t zero = vdupq_n_s16(0);
  165. const int16x8_t max = vdupq_n_s16(MAX_Y);
  166. uint64x2_t sum = vdupq_n_u64(0);
  167. uint64_t diff;
  168. for (i = 0; i + 8 <= len; i += 8) {
  169. const int16x8_t A = vreinterpretq_s16_u16(vld1q_u16(ref + i));
  170. const int16x8_t B = vreinterpretq_s16_u16(vld1q_u16(src + i));
  171. const int16x8_t C = vreinterpretq_s16_u16(vld1q_u16(dst + i));
  172. const int16x8_t D = vsubq_s16(A, B); // diff_y
  173. const int16x8_t F = vaddq_s16(C, D); // new_y
  174. const uint16x8_t H =
  175. vreinterpretq_u16_s16(vmaxq_s16(vminq_s16(F, max), zero));
  176. const int16x8_t I = vabsq_s16(D); // abs(diff_y)
  177. vst1q_u16(dst + i, H);
  178. sum = vpadalq_u32(sum, vpaddlq_u16(vreinterpretq_u16_s16(I)));
  179. }
  180. diff = vgetq_lane_u64(sum, 0) + vgetq_lane_u64(sum, 1);
  181. for (; i < len; ++i) {
  182. const int diff_y = ref[i] - src[i];
  183. const int new_y = (int)(dst[i]) + diff_y;
  184. dst[i] = clip_y_NEON(new_y);
  185. diff += (uint64_t)(abs(diff_y));
  186. }
  187. return diff;
  188. }
  189. static void SharpYUVUpdateRGB_NEON(const int16_t* ref, const int16_t* src,
  190. int16_t* dst, int len) {
  191. int i;
  192. for (i = 0; i + 8 <= len; i += 8) {
  193. const int16x8_t A = vld1q_s16(ref + i);
  194. const int16x8_t B = vld1q_s16(src + i);
  195. const int16x8_t C = vld1q_s16(dst + i);
  196. const int16x8_t D = vsubq_s16(A, B); // diff_uv
  197. const int16x8_t E = vaddq_s16(C, D); // new_uv
  198. vst1q_s16(dst + i, E);
  199. }
  200. for (; i < len; ++i) {
  201. const int diff_uv = ref[i] - src[i];
  202. dst[i] += diff_uv;
  203. }
  204. }
  205. static void SharpYUVFilterRow_NEON(const int16_t* A, const int16_t* B, int len,
  206. const uint16_t* best_y, uint16_t* out) {
  207. int i;
  208. const int16x8_t max = vdupq_n_s16(MAX_Y);
  209. const int16x8_t zero = vdupq_n_s16(0);
  210. for (i = 0; i + 8 <= len; i += 8) {
  211. const int16x8_t a0 = vld1q_s16(A + i + 0);
  212. const int16x8_t a1 = vld1q_s16(A + i + 1);
  213. const int16x8_t b0 = vld1q_s16(B + i + 0);
  214. const int16x8_t b1 = vld1q_s16(B + i + 1);
  215. const int16x8_t a0b1 = vaddq_s16(a0, b1);
  216. const int16x8_t a1b0 = vaddq_s16(a1, b0);
  217. const int16x8_t a0a1b0b1 = vaddq_s16(a0b1, a1b0); // A0+A1+B0+B1
  218. const int16x8_t a0b1_2 = vaddq_s16(a0b1, a0b1); // 2*(A0+B1)
  219. const int16x8_t a1b0_2 = vaddq_s16(a1b0, a1b0); // 2*(A1+B0)
  220. const int16x8_t c0 = vshrq_n_s16(vaddq_s16(a0b1_2, a0a1b0b1), 3);
  221. const int16x8_t c1 = vshrq_n_s16(vaddq_s16(a1b0_2, a0a1b0b1), 3);
  222. const int16x8_t d0 = vaddq_s16(c1, a0);
  223. const int16x8_t d1 = vaddq_s16(c0, a1);
  224. const int16x8_t e0 = vrshrq_n_s16(d0, 1);
  225. const int16x8_t e1 = vrshrq_n_s16(d1, 1);
  226. const int16x8x2_t f = vzipq_s16(e0, e1);
  227. const int16x8_t g0 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 0));
  228. const int16x8_t g1 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 8));
  229. const int16x8_t h0 = vaddq_s16(g0, f.val[0]);
  230. const int16x8_t h1 = vaddq_s16(g1, f.val[1]);
  231. const int16x8_t i0 = vmaxq_s16(vminq_s16(h0, max), zero);
  232. const int16x8_t i1 = vmaxq_s16(vminq_s16(h1, max), zero);
  233. vst1q_u16(out + 2 * i + 0, vreinterpretq_u16_s16(i0));
  234. vst1q_u16(out + 2 * i + 8, vreinterpretq_u16_s16(i1));
  235. }
  236. for (; i < len; ++i) {
  237. const int a0b1 = A[i + 0] + B[i + 1];
  238. const int a1b0 = A[i + 1] + B[i + 0];
  239. const int a0a1b0b1 = a0b1 + a1b0 + 8;
  240. const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
  241. const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
  242. out[2 * i + 0] = clip_y_NEON(best_y[2 * i + 0] + v0);
  243. out[2 * i + 1] = clip_y_NEON(best_y[2 * i + 1] + v1);
  244. }
  245. }
  246. #undef MAX_Y
  247. //------------------------------------------------------------------------------
  248. extern void WebPInitSharpYUVNEON(void);
  249. WEBP_TSAN_IGNORE_FUNCTION void WebPInitSharpYUVNEON(void) {
  250. WebPSharpYUVUpdateY = SharpYUVUpdateY_NEON;
  251. WebPSharpYUVUpdateRGB = SharpYUVUpdateRGB_NEON;
  252. WebPSharpYUVFilterRow = SharpYUVFilterRow_NEON;
  253. }
  254. #else // !WEBP_USE_NEON
  255. WEBP_DSP_INIT_STUB(WebPInitConvertARGBToYUVNEON)
  256. WEBP_DSP_INIT_STUB(WebPInitSharpYUVNEON)
  257. #endif // WEBP_USE_NEON