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