lossless_enc_neon.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // NEON variant of methods for lossless encoder
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_NEON)
  15. #include <arm_neon.h>
  16. #include "./lossless.h"
  17. #include "./neon.h"
  18. //------------------------------------------------------------------------------
  19. // Subtract-Green Transform
  20. // vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use
  21. // non-standard versions there.
  22. #if defined(__APPLE__) && defined(__aarch64__) && \
  23. defined(__apple_build_version__) && (__apple_build_version__< 6020037)
  24. #define USE_VTBLQ
  25. #endif
  26. #ifdef USE_VTBLQ
  27. // 255 = byte will be zeroed
  28. static const uint8_t kGreenShuffle[16] = {
  29. 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255
  30. };
  31. static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,
  32. const uint8x16_t shuffle) {
  33. return vcombine_u8(vtbl1q_u8(argb, vget_low_u8(shuffle)),
  34. vtbl1q_u8(argb, vget_high_u8(shuffle)));
  35. }
  36. #else // !USE_VTBLQ
  37. // 255 = byte will be zeroed
  38. static const uint8_t kGreenShuffle[8] = { 1, 255, 1, 255, 5, 255, 5, 255 };
  39. static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,
  40. const uint8x8_t shuffle) {
  41. return vcombine_u8(vtbl1_u8(vget_low_u8(argb), shuffle),
  42. vtbl1_u8(vget_high_u8(argb), shuffle));
  43. }
  44. #endif // USE_VTBLQ
  45. static void SubtractGreenFromBlueAndRed_NEON(uint32_t* argb_data,
  46. int num_pixels) {
  47. const uint32_t* const end = argb_data + (num_pixels & ~3);
  48. #ifdef USE_VTBLQ
  49. const uint8x16_t shuffle = vld1q_u8(kGreenShuffle);
  50. #else
  51. const uint8x8_t shuffle = vld1_u8(kGreenShuffle);
  52. #endif
  53. for (; argb_data < end; argb_data += 4) {
  54. const uint8x16_t argb = vld1q_u8((uint8_t*)argb_data);
  55. const uint8x16_t greens = DoGreenShuffle_NEON(argb, shuffle);
  56. vst1q_u8((uint8_t*)argb_data, vsubq_u8(argb, greens));
  57. }
  58. // fallthrough and finish off with plain-C
  59. VP8LSubtractGreenFromBlueAndRed_C(argb_data, num_pixels & 3);
  60. }
  61. //------------------------------------------------------------------------------
  62. // Color Transform
  63. static void TransformColor_NEON(const VP8LMultipliers* const m,
  64. uint32_t* argb_data, int num_pixels) {
  65. // sign-extended multiplying constants, pre-shifted by 6.
  66. #define CST(X) (((int16_t)(m->X << 8)) >> 6)
  67. const int16_t rb[8] = {
  68. CST(green_to_blue_), CST(green_to_red_),
  69. CST(green_to_blue_), CST(green_to_red_),
  70. CST(green_to_blue_), CST(green_to_red_),
  71. CST(green_to_blue_), CST(green_to_red_)
  72. };
  73. const int16x8_t mults_rb = vld1q_s16(rb);
  74. const int16_t b2[8] = {
  75. 0, CST(red_to_blue_), 0, CST(red_to_blue_),
  76. 0, CST(red_to_blue_), 0, CST(red_to_blue_),
  77. };
  78. const int16x8_t mults_b2 = vld1q_s16(b2);
  79. #undef CST
  80. #ifdef USE_VTBLQ
  81. static const uint8_t kg0g0[16] = {
  82. 255, 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13
  83. };
  84. const uint8x16_t shuffle = vld1q_u8(kg0g0);
  85. #else
  86. static const uint8_t k0g0g[8] = { 255, 1, 255, 1, 255, 5, 255, 5 };
  87. const uint8x8_t shuffle = vld1_u8(k0g0g);
  88. #endif
  89. const uint32x4_t mask_rb = vdupq_n_u32(0x00ff00ffu); // red-blue masks
  90. int i;
  91. for (i = 0; i + 4 <= num_pixels; i += 4) {
  92. const uint8x16_t in = vld1q_u8((uint8_t*)(argb_data + i));
  93. // 0 g 0 g
  94. const uint8x16_t greens = DoGreenShuffle_NEON(in, shuffle);
  95. // x dr x db1
  96. const int16x8_t A = vqdmulhq_s16(vreinterpretq_s16_u8(greens), mults_rb);
  97. // r 0 b 0
  98. const int16x8_t B = vshlq_n_s16(vreinterpretq_s16_u8(in), 8);
  99. // x db2 0 0
  100. const int16x8_t C = vqdmulhq_s16(B, mults_b2);
  101. // 0 0 x db2
  102. const uint32x4_t D = vshrq_n_u32(vreinterpretq_u32_s16(C), 16);
  103. // x dr x db
  104. const int8x16_t E = vaddq_s8(vreinterpretq_s8_u32(D),
  105. vreinterpretq_s8_s16(A));
  106. // 0 dr 0 db
  107. const uint32x4_t F = vandq_u32(vreinterpretq_u32_s8(E), mask_rb);
  108. const int8x16_t out = vsubq_s8(vreinterpretq_s8_u8(in),
  109. vreinterpretq_s8_u32(F));
  110. vst1q_s8((int8_t*)(argb_data + i), out);
  111. }
  112. // fallthrough and finish off with plain-C
  113. VP8LTransformColor_C(m, argb_data + i, num_pixels - i);
  114. }
  115. #undef USE_VTBLQ
  116. //------------------------------------------------------------------------------
  117. // Entry point
  118. extern void VP8LEncDspInitNEON(void);
  119. WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitNEON(void) {
  120. VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_NEON;
  121. VP8LTransformColor = TransformColor_NEON;
  122. }
  123. #else // !WEBP_USE_NEON
  124. WEBP_DSP_INIT_STUB(VP8LEncDspInitNEON)
  125. #endif // WEBP_USE_NEON