alpha_processing_sse2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright 2014 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.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE2)
  15. #include <emmintrin.h>
  16. //------------------------------------------------------------------------------
  17. static int DispatchAlpha(const uint8_t* alpha, int alpha_stride,
  18. int width, int height,
  19. uint8_t* dst, int dst_stride) {
  20. // alpha_and stores an 'and' operation of all the alpha[] values. The final
  21. // value is not 0xff if any of the alpha[] is not equal to 0xff.
  22. uint32_t alpha_and = 0xff;
  23. int i, j;
  24. const __m128i zero = _mm_setzero_si128();
  25. const __m128i rgb_mask = _mm_set1_epi32(0xffffff00u); // to preserve RGB
  26. const __m128i all_0xff = _mm_set_epi32(0, 0, ~0u, ~0u);
  27. __m128i all_alphas = all_0xff;
  28. // We must be able to access 3 extra bytes after the last written byte
  29. // 'dst[4 * width - 4]', because we don't know if alpha is the first or the
  30. // last byte of the quadruplet.
  31. const int limit = (width - 1) & ~7;
  32. for (j = 0; j < height; ++j) {
  33. __m128i* out = (__m128i*)dst;
  34. for (i = 0; i < limit; i += 8) {
  35. // load 8 alpha bytes
  36. const __m128i a0 = _mm_loadl_epi64((const __m128i*)&alpha[i]);
  37. const __m128i a1 = _mm_unpacklo_epi8(a0, zero);
  38. const __m128i a2_lo = _mm_unpacklo_epi16(a1, zero);
  39. const __m128i a2_hi = _mm_unpackhi_epi16(a1, zero);
  40. // load 8 dst pixels (32 bytes)
  41. const __m128i b0_lo = _mm_loadu_si128(out + 0);
  42. const __m128i b0_hi = _mm_loadu_si128(out + 1);
  43. // mask dst alpha values
  44. const __m128i b1_lo = _mm_and_si128(b0_lo, rgb_mask);
  45. const __m128i b1_hi = _mm_and_si128(b0_hi, rgb_mask);
  46. // combine
  47. const __m128i b2_lo = _mm_or_si128(b1_lo, a2_lo);
  48. const __m128i b2_hi = _mm_or_si128(b1_hi, a2_hi);
  49. // store
  50. _mm_storeu_si128(out + 0, b2_lo);
  51. _mm_storeu_si128(out + 1, b2_hi);
  52. // accumulate eight alpha 'and' in parallel
  53. all_alphas = _mm_and_si128(all_alphas, a0);
  54. out += 2;
  55. }
  56. for (; i < width; ++i) {
  57. const uint32_t alpha_value = alpha[i];
  58. dst[4 * i] = alpha_value;
  59. alpha_and &= alpha_value;
  60. }
  61. alpha += alpha_stride;
  62. dst += dst_stride;
  63. }
  64. // Combine the eight alpha 'and' into a 8-bit mask.
  65. alpha_and &= _mm_movemask_epi8(_mm_cmpeq_epi8(all_alphas, all_0xff));
  66. return (alpha_and != 0xff);
  67. }
  68. static void DispatchAlphaToGreen(const uint8_t* alpha, int alpha_stride,
  69. int width, int height,
  70. uint32_t* dst, int dst_stride) {
  71. int i, j;
  72. const __m128i zero = _mm_setzero_si128();
  73. const int limit = width & ~15;
  74. for (j = 0; j < height; ++j) {
  75. for (i = 0; i < limit; i += 16) { // process 16 alpha bytes
  76. const __m128i a0 = _mm_loadu_si128((const __m128i*)&alpha[i]);
  77. const __m128i a1 = _mm_unpacklo_epi8(zero, a0); // note the 'zero' first!
  78. const __m128i b1 = _mm_unpackhi_epi8(zero, a0);
  79. const __m128i a2_lo = _mm_unpacklo_epi16(a1, zero);
  80. const __m128i b2_lo = _mm_unpacklo_epi16(b1, zero);
  81. const __m128i a2_hi = _mm_unpackhi_epi16(a1, zero);
  82. const __m128i b2_hi = _mm_unpackhi_epi16(b1, zero);
  83. _mm_storeu_si128((__m128i*)&dst[i + 0], a2_lo);
  84. _mm_storeu_si128((__m128i*)&dst[i + 4], a2_hi);
  85. _mm_storeu_si128((__m128i*)&dst[i + 8], b2_lo);
  86. _mm_storeu_si128((__m128i*)&dst[i + 12], b2_hi);
  87. }
  88. for (; i < width; ++i) dst[i] = alpha[i] << 8;
  89. alpha += alpha_stride;
  90. dst += dst_stride;
  91. }
  92. }
  93. static int ExtractAlpha(const uint8_t* argb, int argb_stride,
  94. int width, int height,
  95. uint8_t* alpha, int alpha_stride) {
  96. // alpha_and stores an 'and' operation of all the alpha[] values. The final
  97. // value is not 0xff if any of the alpha[] is not equal to 0xff.
  98. uint32_t alpha_and = 0xff;
  99. int i, j;
  100. const __m128i a_mask = _mm_set1_epi32(0xffu); // to preserve alpha
  101. const __m128i all_0xff = _mm_set_epi32(0, 0, ~0u, ~0u);
  102. __m128i all_alphas = all_0xff;
  103. // We must be able to access 3 extra bytes after the last written byte
  104. // 'src[4 * width - 4]', because we don't know if alpha is the first or the
  105. // last byte of the quadruplet.
  106. const int limit = (width - 1) & ~7;
  107. for (j = 0; j < height; ++j) {
  108. const __m128i* src = (const __m128i*)argb;
  109. for (i = 0; i < limit; i += 8) {
  110. // load 32 argb bytes
  111. const __m128i a0 = _mm_loadu_si128(src + 0);
  112. const __m128i a1 = _mm_loadu_si128(src + 1);
  113. const __m128i b0 = _mm_and_si128(a0, a_mask);
  114. const __m128i b1 = _mm_and_si128(a1, a_mask);
  115. const __m128i c0 = _mm_packs_epi32(b0, b1);
  116. const __m128i d0 = _mm_packus_epi16(c0, c0);
  117. // store
  118. _mm_storel_epi64((__m128i*)&alpha[i], d0);
  119. // accumulate eight alpha 'and' in parallel
  120. all_alphas = _mm_and_si128(all_alphas, d0);
  121. src += 2;
  122. }
  123. for (; i < width; ++i) {
  124. const uint32_t alpha_value = argb[4 * i];
  125. alpha[i] = alpha_value;
  126. alpha_and &= alpha_value;
  127. }
  128. argb += argb_stride;
  129. alpha += alpha_stride;
  130. }
  131. // Combine the eight alpha 'and' into a 8-bit mask.
  132. alpha_and &= _mm_movemask_epi8(_mm_cmpeq_epi8(all_alphas, all_0xff));
  133. return (alpha_and == 0xff);
  134. }
  135. //------------------------------------------------------------------------------
  136. // Non-dither premultiplied modes
  137. #define MULTIPLIER(a) ((a) * 0x8081)
  138. #define PREMULTIPLY(x, m) (((x) * (m)) >> 23)
  139. // We can't use a 'const int' for the SHUFFLE value, because it has to be an
  140. // immediate in the _mm_shufflexx_epi16() instruction. We really need a macro.
  141. // We use: v / 255 = (v * 0x8081) >> 23, where v = alpha * {r,g,b} is a 16bit
  142. // value.
  143. #define APPLY_ALPHA(RGBX, SHUFFLE) do { \
  144. const __m128i argb0 = _mm_loadu_si128((const __m128i*)&(RGBX)); \
  145. const __m128i argb1_lo = _mm_unpacklo_epi8(argb0, zero); \
  146. const __m128i argb1_hi = _mm_unpackhi_epi8(argb0, zero); \
  147. const __m128i alpha0_lo = _mm_or_si128(argb1_lo, kMask); \
  148. const __m128i alpha0_hi = _mm_or_si128(argb1_hi, kMask); \
  149. const __m128i alpha1_lo = _mm_shufflelo_epi16(alpha0_lo, SHUFFLE); \
  150. const __m128i alpha1_hi = _mm_shufflelo_epi16(alpha0_hi, SHUFFLE); \
  151. const __m128i alpha2_lo = _mm_shufflehi_epi16(alpha1_lo, SHUFFLE); \
  152. const __m128i alpha2_hi = _mm_shufflehi_epi16(alpha1_hi, SHUFFLE); \
  153. /* alpha2 = [ff a0 a0 a0][ff a1 a1 a1] */ \
  154. const __m128i A0_lo = _mm_mullo_epi16(alpha2_lo, argb1_lo); \
  155. const __m128i A0_hi = _mm_mullo_epi16(alpha2_hi, argb1_hi); \
  156. const __m128i A1_lo = _mm_mulhi_epu16(A0_lo, kMult); \
  157. const __m128i A1_hi = _mm_mulhi_epu16(A0_hi, kMult); \
  158. const __m128i A2_lo = _mm_srli_epi16(A1_lo, 7); \
  159. const __m128i A2_hi = _mm_srli_epi16(A1_hi, 7); \
  160. const __m128i A3 = _mm_packus_epi16(A2_lo, A2_hi); \
  161. _mm_storeu_si128((__m128i*)&(RGBX), A3); \
  162. } while (0)
  163. static void ApplyAlphaMultiply_SSE2(uint8_t* rgba, int alpha_first,
  164. int w, int h, int stride) {
  165. const __m128i zero = _mm_setzero_si128();
  166. const __m128i kMult = _mm_set1_epi16(0x8081u);
  167. const __m128i kMask = _mm_set_epi16(0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0);
  168. const int kSpan = 4;
  169. while (h-- > 0) {
  170. uint32_t* const rgbx = (uint32_t*)rgba;
  171. int i;
  172. if (!alpha_first) {
  173. for (i = 0; i + kSpan <= w; i += kSpan) {
  174. APPLY_ALPHA(rgbx[i], _MM_SHUFFLE(2, 3, 3, 3));
  175. }
  176. } else {
  177. for (i = 0; i + kSpan <= w; i += kSpan) {
  178. APPLY_ALPHA(rgbx[i], _MM_SHUFFLE(0, 0, 0, 1));
  179. }
  180. }
  181. // Finish with left-overs.
  182. for (; i < w; ++i) {
  183. uint8_t* const rgb = rgba + (alpha_first ? 1 : 0);
  184. const uint8_t* const alpha = rgba + (alpha_first ? 0 : 3);
  185. const uint32_t a = alpha[4 * i];
  186. if (a != 0xff) {
  187. const uint32_t mult = MULTIPLIER(a);
  188. rgb[4 * i + 0] = PREMULTIPLY(rgb[4 * i + 0], mult);
  189. rgb[4 * i + 1] = PREMULTIPLY(rgb[4 * i + 1], mult);
  190. rgb[4 * i + 2] = PREMULTIPLY(rgb[4 * i + 2], mult);
  191. }
  192. }
  193. rgba += stride;
  194. }
  195. }
  196. #undef MULTIPLIER
  197. #undef PREMULTIPLY
  198. // -----------------------------------------------------------------------------
  199. // Apply alpha value to rows
  200. static void MultARGBRow_SSE2(uint32_t* const ptr, int width, int inverse) {
  201. int x = 0;
  202. if (!inverse) {
  203. const int kSpan = 2;
  204. const __m128i zero = _mm_setzero_si128();
  205. const __m128i k128 = _mm_set1_epi16(128);
  206. const __m128i kMult = _mm_set1_epi16(0x0101);
  207. const __m128i kMask = _mm_set_epi16(0, 0xff, 0, 0, 0, 0xff, 0, 0);
  208. for (x = 0; x + kSpan <= width; x += kSpan) {
  209. // To compute 'result = (int)(a * x / 255. + .5)', we use:
  210. // tmp = a * v + 128, result = (tmp * 0x0101u) >> 16
  211. const __m128i A0 = _mm_loadl_epi64((const __m128i*)&ptr[x]);
  212. const __m128i A1 = _mm_unpacklo_epi8(A0, zero);
  213. const __m128i A2 = _mm_or_si128(A1, kMask);
  214. const __m128i A3 = _mm_shufflelo_epi16(A2, _MM_SHUFFLE(2, 3, 3, 3));
  215. const __m128i A4 = _mm_shufflehi_epi16(A3, _MM_SHUFFLE(2, 3, 3, 3));
  216. // here, A4 = [ff a0 a0 a0][ff a1 a1 a1]
  217. const __m128i A5 = _mm_mullo_epi16(A4, A1);
  218. const __m128i A6 = _mm_add_epi16(A5, k128);
  219. const __m128i A7 = _mm_mulhi_epu16(A6, kMult);
  220. const __m128i A10 = _mm_packus_epi16(A7, zero);
  221. _mm_storel_epi64((__m128i*)&ptr[x], A10);
  222. }
  223. }
  224. width -= x;
  225. if (width > 0) WebPMultARGBRowC(ptr + x, width, inverse);
  226. }
  227. static void MultRow_SSE2(uint8_t* const ptr, const uint8_t* const alpha,
  228. int width, int inverse) {
  229. int x = 0;
  230. if (!inverse) {
  231. const __m128i zero = _mm_setzero_si128();
  232. const __m128i k128 = _mm_set1_epi16(128);
  233. const __m128i kMult = _mm_set1_epi16(0x0101);
  234. for (x = 0; x + 8 <= width; x += 8) {
  235. const __m128i v0 = _mm_loadl_epi64((__m128i*)&ptr[x]);
  236. const __m128i a0 = _mm_loadl_epi64((const __m128i*)&alpha[x]);
  237. const __m128i v1 = _mm_unpacklo_epi8(v0, zero);
  238. const __m128i a1 = _mm_unpacklo_epi8(a0, zero);
  239. const __m128i v2 = _mm_mullo_epi16(v1, a1);
  240. const __m128i v3 = _mm_add_epi16(v2, k128);
  241. const __m128i v4 = _mm_mulhi_epu16(v3, kMult);
  242. const __m128i v5 = _mm_packus_epi16(v4, zero);
  243. _mm_storel_epi64((__m128i*)&ptr[x], v5);
  244. }
  245. }
  246. width -= x;
  247. if (width > 0) WebPMultRowC(ptr + x, alpha + x, width, inverse);
  248. }
  249. //------------------------------------------------------------------------------
  250. // Entry point
  251. extern void WebPInitAlphaProcessingSSE2(void);
  252. WEBP_TSAN_IGNORE_FUNCTION void WebPInitAlphaProcessingSSE2(void) {
  253. WebPMultARGBRow = MultARGBRow_SSE2;
  254. WebPMultRow = MultRow_SSE2;
  255. WebPApplyAlphaMultiply = ApplyAlphaMultiply_SSE2;
  256. WebPDispatchAlpha = DispatchAlpha;
  257. WebPDispatchAlphaToGreen = DispatchAlphaToGreen;
  258. WebPExtractAlpha = ExtractAlpha;
  259. }
  260. #else // !WEBP_USE_SSE2
  261. WEBP_DSP_INIT_STUB(WebPInitAlphaProcessingSSE2)
  262. #endif // WEBP_USE_SSE2