argb_sse2.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // ARGB making functions (SSE2 version).
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE2)
  15. #include <assert.h>
  16. #include <emmintrin.h>
  17. #include <string.h>
  18. static WEBP_INLINE uint32_t MakeARGB32(int a, int r, int g, int b) {
  19. return (((uint32_t)a << 24) | (r << 16) | (g << 8) | b);
  20. }
  21. static void PackARGB(const uint8_t* a, const uint8_t* r, const uint8_t* g,
  22. const uint8_t* b, int len, uint32_t* out) {
  23. if (g == r + 1) { // RGBA input order. Need to swap R and B.
  24. int i = 0;
  25. const int len_max = len & ~3; // max length processed in main loop
  26. const __m128i red_blue_mask = _mm_set1_epi32(0x00ff00ffu);
  27. assert(b == r + 2);
  28. assert(a == r + 3);
  29. for (; i < len_max; i += 4) {
  30. const __m128i A = _mm_loadu_si128((const __m128i*)(r + 4 * i));
  31. const __m128i B = _mm_and_si128(A, red_blue_mask); // R 0 B 0
  32. const __m128i C = _mm_andnot_si128(red_blue_mask, A); // 0 G 0 A
  33. const __m128i D = _mm_shufflelo_epi16(B, _MM_SHUFFLE(2, 3, 0, 1));
  34. const __m128i E = _mm_shufflehi_epi16(D, _MM_SHUFFLE(2, 3, 0, 1));
  35. const __m128i F = _mm_or_si128(E, C);
  36. _mm_storeu_si128((__m128i*)(out + i), F);
  37. }
  38. for (; i < len; ++i) {
  39. out[i] = MakeARGB32(a[4 * i], r[4 * i], g[4 * i], b[4 * i]);
  40. }
  41. } else {
  42. assert(g == b + 1);
  43. assert(r == b + 2);
  44. assert(a == b + 3);
  45. memcpy(out, b, len * 4);
  46. }
  47. }
  48. //------------------------------------------------------------------------------
  49. // Entry point
  50. extern void VP8EncDspARGBInitSSE2(void);
  51. WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspARGBInitSSE2(void) {
  52. VP8PackARGB = PackARGB;
  53. }
  54. #else // !WEBP_USE_SSE2
  55. WEBP_DSP_INIT_STUB(VP8EncDspARGBInitSSE2)
  56. #endif // WEBP_USE_SSE2