lossless_enc_sse41.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2015 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. // SSE4.1 variant of methods for lossless encoder
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE41)
  15. #include <assert.h>
  16. #include <smmintrin.h>
  17. #include "./lossless.h"
  18. // For sign-extended multiplying constants, pre-shifted by 5:
  19. #define CST_5b(X) (((int16_t)((uint16_t)(X) << 8)) >> 5)
  20. //------------------------------------------------------------------------------
  21. // Subtract-Green Transform
  22. static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,
  23. int num_pixels) {
  24. int i;
  25. const __m128i kCstShuffle = _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9,
  26. -1, 5, -1, 5, -1, 1, -1, 1);
  27. for (i = 0; i + 4 <= num_pixels; i += 4) {
  28. const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);
  29. const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);
  30. const __m128i out = _mm_sub_epi8(in, in_0g0g);
  31. _mm_storeu_si128((__m128i*)&argb_data[i], out);
  32. }
  33. // fallthrough and finish off with plain-C
  34. if (i != num_pixels) {
  35. VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);
  36. }
  37. }
  38. //------------------------------------------------------------------------------
  39. // Color Transform
  40. #define MK_CST_16(HI, LO) \
  41. _mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff)))
  42. static void CollectColorBlueTransforms_SSE41(const uint32_t* argb, int stride,
  43. int tile_width, int tile_height,
  44. int green_to_blue, int red_to_blue,
  45. int histo[]) {
  46. const __m128i mult =
  47. MK_CST_16(CST_5b(red_to_blue) + 256,CST_5b(green_to_blue));
  48. const __m128i perm =
  49. _mm_setr_epi8(-1, 1, -1, 2, -1, 5, -1, 6, -1, 9, -1, 10, -1, 13, -1, 14);
  50. if (tile_width >= 4) {
  51. int y;
  52. for (y = 0; y < tile_height; ++y) {
  53. const uint32_t* const src = argb + y * stride;
  54. const __m128i A1 = _mm_loadu_si128((const __m128i*)src);
  55. const __m128i B1 = _mm_shuffle_epi8(A1, perm);
  56. const __m128i C1 = _mm_mulhi_epi16(B1, mult);
  57. const __m128i D1 = _mm_sub_epi16(A1, C1);
  58. __m128i E = _mm_add_epi16(_mm_srli_epi32(D1, 16), D1);
  59. int x;
  60. for (x = 4; x + 4 <= tile_width; x += 4) {
  61. const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));
  62. __m128i B2, C2, D2;
  63. ++histo[_mm_extract_epi8(E, 0)];
  64. B2 = _mm_shuffle_epi8(A2, perm);
  65. ++histo[_mm_extract_epi8(E, 4)];
  66. C2 = _mm_mulhi_epi16(B2, mult);
  67. ++histo[_mm_extract_epi8(E, 8)];
  68. D2 = _mm_sub_epi16(A2, C2);
  69. ++histo[_mm_extract_epi8(E, 12)];
  70. E = _mm_add_epi16(_mm_srli_epi32(D2, 16), D2);
  71. }
  72. ++histo[_mm_extract_epi8(E, 0)];
  73. ++histo[_mm_extract_epi8(E, 4)];
  74. ++histo[_mm_extract_epi8(E, 8)];
  75. ++histo[_mm_extract_epi8(E, 12)];
  76. }
  77. }
  78. {
  79. const int left_over = tile_width & 3;
  80. if (left_over > 0) {
  81. VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,
  82. left_over, tile_height,
  83. green_to_blue, red_to_blue, histo);
  84. }
  85. }
  86. }
  87. static void CollectColorRedTransforms_SSE41(const uint32_t* argb, int stride,
  88. int tile_width, int tile_height,
  89. int green_to_red, int histo[]) {
  90. const __m128i mult = MK_CST_16(0, CST_5b(green_to_red));
  91. const __m128i mask_g = _mm_set1_epi32(0x0000ff00);
  92. if (tile_width >= 4) {
  93. int y;
  94. for (y = 0; y < tile_height; ++y) {
  95. const uint32_t* const src = argb + y * stride;
  96. const __m128i A1 = _mm_loadu_si128((const __m128i*)src);
  97. const __m128i B1 = _mm_and_si128(A1, mask_g);
  98. const __m128i C1 = _mm_madd_epi16(B1, mult);
  99. __m128i D = _mm_sub_epi16(A1, C1);
  100. int x;
  101. for (x = 4; x + 4 <= tile_width; x += 4) {
  102. const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));
  103. __m128i B2, C2;
  104. ++histo[_mm_extract_epi8(D, 2)];
  105. B2 = _mm_and_si128(A2, mask_g);
  106. ++histo[_mm_extract_epi8(D, 6)];
  107. C2 = _mm_madd_epi16(B2, mult);
  108. ++histo[_mm_extract_epi8(D, 10)];
  109. ++histo[_mm_extract_epi8(D, 14)];
  110. D = _mm_sub_epi16(A2, C2);
  111. }
  112. ++histo[_mm_extract_epi8(D, 2)];
  113. ++histo[_mm_extract_epi8(D, 6)];
  114. ++histo[_mm_extract_epi8(D, 10)];
  115. ++histo[_mm_extract_epi8(D, 14)];
  116. }
  117. }
  118. {
  119. const int left_over = tile_width & 3;
  120. if (left_over > 0) {
  121. VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,
  122. left_over, tile_height, green_to_red,
  123. histo);
  124. }
  125. }
  126. }
  127. #undef MK_CST_16
  128. //------------------------------------------------------------------------------
  129. // Entry point
  130. extern void VP8LEncDspInitSSE41(void);
  131. WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {
  132. VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41;
  133. VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41;
  134. VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41;
  135. }
  136. #else // !WEBP_USE_SSE41
  137. WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)
  138. #endif // WEBP_USE_SSE41