alpha_processing_neon.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Utilities for processing transparent channel, NEON version.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_NEON)
  15. #include "./neon.h"
  16. //------------------------------------------------------------------------------
  17. #define MULTIPLIER(a) ((a) * 0x8081)
  18. #define PREMULTIPLY(x, m) (((x) * (m)) >> 23)
  19. #define MULTIPLY_BY_ALPHA(V, ALPHA, OTHER) do { \
  20. const uint8x8_t alpha = (V).val[(ALPHA)]; \
  21. const uint16x8_t r1 = vmull_u8((V).val[1], alpha); \
  22. const uint16x8_t g1 = vmull_u8((V).val[2], alpha); \
  23. const uint16x8_t b1 = vmull_u8((V).val[(OTHER)], alpha); \
  24. /* we use: v / 255 = (v + 1 + (v >> 8)) >> 8 */ \
  25. const uint16x8_t r2 = vsraq_n_u16(r1, r1, 8); \
  26. const uint16x8_t g2 = vsraq_n_u16(g1, g1, 8); \
  27. const uint16x8_t b2 = vsraq_n_u16(b1, b1, 8); \
  28. const uint16x8_t r3 = vaddq_u16(r2, kOne); \
  29. const uint16x8_t g3 = vaddq_u16(g2, kOne); \
  30. const uint16x8_t b3 = vaddq_u16(b2, kOne); \
  31. (V).val[1] = vshrn_n_u16(r3, 8); \
  32. (V).val[2] = vshrn_n_u16(g3, 8); \
  33. (V).val[(OTHER)] = vshrn_n_u16(b3, 8); \
  34. } while (0)
  35. static void ApplyAlphaMultiply_NEON(uint8_t* rgba, int alpha_first,
  36. int w, int h, int stride) {
  37. const uint16x8_t kOne = vdupq_n_u16(1u);
  38. while (h-- > 0) {
  39. uint32_t* const rgbx = (uint32_t*)rgba;
  40. int i = 0;
  41. if (alpha_first) {
  42. for (; i + 8 <= w; i += 8) {
  43. // load aaaa...|rrrr...|gggg...|bbbb...
  44. uint8x8x4_t RGBX = vld4_u8((const uint8_t*)(rgbx + i));
  45. MULTIPLY_BY_ALPHA(RGBX, 0, 3);
  46. vst4_u8((uint8_t*)(rgbx + i), RGBX);
  47. }
  48. } else {
  49. for (; i + 8 <= w; i += 8) {
  50. uint8x8x4_t RGBX = vld4_u8((const uint8_t*)(rgbx + i));
  51. MULTIPLY_BY_ALPHA(RGBX, 3, 0);
  52. vst4_u8((uint8_t*)(rgbx + i), RGBX);
  53. }
  54. }
  55. // Finish with left-overs.
  56. for (; i < w; ++i) {
  57. uint8_t* const rgb = rgba + (alpha_first ? 1 : 0);
  58. const uint8_t* const alpha = rgba + (alpha_first ? 0 : 3);
  59. const uint32_t a = alpha[4 * i];
  60. if (a != 0xff) {
  61. const uint32_t mult = MULTIPLIER(a);
  62. rgb[4 * i + 0] = PREMULTIPLY(rgb[4 * i + 0], mult);
  63. rgb[4 * i + 1] = PREMULTIPLY(rgb[4 * i + 1], mult);
  64. rgb[4 * i + 2] = PREMULTIPLY(rgb[4 * i + 2], mult);
  65. }
  66. }
  67. rgba += stride;
  68. }
  69. }
  70. #undef MULTIPLY_BY_ALPHA
  71. #undef MULTIPLIER
  72. #undef PREMULTIPLY
  73. //------------------------------------------------------------------------------
  74. static int DispatchAlpha_NEON(const uint8_t* WEBP_RESTRICT alpha,
  75. int alpha_stride, int width, int height,
  76. uint8_t* WEBP_RESTRICT dst, int dst_stride) {
  77. uint32_t alpha_mask = 0xffffffffu;
  78. uint8x8_t mask8 = vdup_n_u8(0xff);
  79. uint32_t tmp[2];
  80. int i, j;
  81. for (j = 0; j < height; ++j) {
  82. // We don't know if alpha is first or last in dst[] (depending on rgbA/Argb
  83. // mode). So we must be sure dst[4*i + 8 - 1] is writable for the store.
  84. // Hence the test with 'width - 1' instead of just 'width'.
  85. for (i = 0; i + 8 <= width - 1; i += 8) {
  86. uint8x8x4_t rgbX = vld4_u8((const uint8_t*)(dst + 4 * i));
  87. const uint8x8_t alphas = vld1_u8(alpha + i);
  88. rgbX.val[0] = alphas;
  89. vst4_u8((uint8_t*)(dst + 4 * i), rgbX);
  90. mask8 = vand_u8(mask8, alphas);
  91. }
  92. for (; i < width; ++i) {
  93. const uint32_t alpha_value = alpha[i];
  94. dst[4 * i] = alpha_value;
  95. alpha_mask &= alpha_value;
  96. }
  97. alpha += alpha_stride;
  98. dst += dst_stride;
  99. }
  100. vst1_u8((uint8_t*)tmp, mask8);
  101. alpha_mask &= tmp[0];
  102. alpha_mask &= tmp[1];
  103. return (alpha_mask != 0xffffffffu);
  104. }
  105. static void DispatchAlphaToGreen_NEON(const uint8_t* WEBP_RESTRICT alpha,
  106. int alpha_stride, int width, int height,
  107. uint32_t* WEBP_RESTRICT dst,
  108. int dst_stride) {
  109. int i, j;
  110. uint8x8x4_t greens; // leave A/R/B channels zero'd.
  111. greens.val[0] = vdup_n_u8(0);
  112. greens.val[2] = vdup_n_u8(0);
  113. greens.val[3] = vdup_n_u8(0);
  114. for (j = 0; j < height; ++j) {
  115. for (i = 0; i + 8 <= width; i += 8) {
  116. greens.val[1] = vld1_u8(alpha + i);
  117. vst4_u8((uint8_t*)(dst + i), greens);
  118. }
  119. for (; i < width; ++i) dst[i] = alpha[i] << 8;
  120. alpha += alpha_stride;
  121. dst += dst_stride;
  122. }
  123. }
  124. static int ExtractAlpha_NEON(const uint8_t* WEBP_RESTRICT argb, int argb_stride,
  125. int width, int height,
  126. uint8_t* WEBP_RESTRICT alpha, int alpha_stride) {
  127. uint32_t alpha_mask = 0xffffffffu;
  128. uint8x8_t mask8 = vdup_n_u8(0xff);
  129. uint32_t tmp[2];
  130. int i, j;
  131. for (j = 0; j < height; ++j) {
  132. // We don't know if alpha is first or last in dst[] (depending on rgbA/Argb
  133. // mode). So we must be sure dst[4*i + 8 - 1] is writable for the store.
  134. // Hence the test with 'width - 1' instead of just 'width'.
  135. for (i = 0; i + 8 <= width - 1; i += 8) {
  136. const uint8x8x4_t rgbX = vld4_u8((const uint8_t*)(argb + 4 * i));
  137. const uint8x8_t alphas = rgbX.val[0];
  138. vst1_u8((uint8_t*)(alpha + i), alphas);
  139. mask8 = vand_u8(mask8, alphas);
  140. }
  141. for (; i < width; ++i) {
  142. alpha[i] = argb[4 * i];
  143. alpha_mask &= alpha[i];
  144. }
  145. argb += argb_stride;
  146. alpha += alpha_stride;
  147. }
  148. vst1_u8((uint8_t*)tmp, mask8);
  149. alpha_mask &= tmp[0];
  150. alpha_mask &= tmp[1];
  151. return (alpha_mask == 0xffffffffu);
  152. }
  153. static void ExtractGreen_NEON(const uint32_t* WEBP_RESTRICT argb,
  154. uint8_t* WEBP_RESTRICT alpha, int size) {
  155. int i;
  156. for (i = 0; i + 16 <= size; i += 16) {
  157. const uint8x16x4_t rgbX = vld4q_u8((const uint8_t*)(argb + i));
  158. const uint8x16_t greens = rgbX.val[1];
  159. vst1q_u8(alpha + i, greens);
  160. }
  161. for (; i < size; ++i) alpha[i] = (argb[i] >> 8) & 0xff;
  162. }
  163. //------------------------------------------------------------------------------
  164. extern void WebPInitAlphaProcessingNEON(void);
  165. WEBP_TSAN_IGNORE_FUNCTION void WebPInitAlphaProcessingNEON(void) {
  166. WebPApplyAlphaMultiply = ApplyAlphaMultiply_NEON;
  167. WebPDispatchAlpha = DispatchAlpha_NEON;
  168. WebPDispatchAlphaToGreen = DispatchAlphaToGreen_NEON;
  169. WebPExtractAlpha = ExtractAlpha_NEON;
  170. WebPExtractGreen = ExtractGreen_NEON;
  171. }
  172. #else // !WEBP_USE_NEON
  173. WEBP_DSP_INIT_STUB(WebPInitAlphaProcessingNEON)
  174. #endif // WEBP_USE_NEON