rescaler_neon.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 version of rescaling functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_NEON) && !defined(WEBP_REDUCE_SIZE)
  15. #include <arm_neon.h>
  16. #include <assert.h>
  17. #include "./neon.h"
  18. #include "../utils/rescaler_utils.h"
  19. #define ROUNDER (WEBP_RESCALER_ONE >> 1)
  20. #define MULT_FIX_C(x, y) (((uint64_t)(x) * (y) + ROUNDER) >> WEBP_RESCALER_RFIX)
  21. #define MULT_FIX_FLOOR_C(x, y) (((uint64_t)(x) * (y)) >> WEBP_RESCALER_RFIX)
  22. #define LOAD_32x4(SRC, DST) const uint32x4_t DST = vld1q_u32((SRC))
  23. #define LOAD_32x8(SRC, DST0, DST1) \
  24. LOAD_32x4(SRC + 0, DST0); \
  25. LOAD_32x4(SRC + 4, DST1)
  26. #define STORE_32x8(SRC0, SRC1, DST) do { \
  27. vst1q_u32((DST) + 0, SRC0); \
  28. vst1q_u32((DST) + 4, SRC1); \
  29. } while (0);
  30. #if (WEBP_RESCALER_RFIX == 32)
  31. #define MAKE_HALF_CST(C) vdupq_n_s32((int32_t)((C) >> 1))
  32. // note: B is actualy scale>>1. See MAKE_HALF_CST
  33. #define MULT_FIX(A, B) \
  34. vreinterpretq_u32_s32(vqrdmulhq_s32(vreinterpretq_s32_u32((A)), (B)))
  35. #define MULT_FIX_FLOOR(A, B) \
  36. vreinterpretq_u32_s32(vqdmulhq_s32(vreinterpretq_s32_u32((A)), (B)))
  37. #else
  38. #error "MULT_FIX/WEBP_RESCALER_RFIX need some more work"
  39. #endif
  40. static uint32x4_t Interpolate_NEON(const rescaler_t* const frow,
  41. const rescaler_t* const irow,
  42. uint32_t A, uint32_t B) {
  43. LOAD_32x4(frow, A0);
  44. LOAD_32x4(irow, B0);
  45. const uint64x2_t C0 = vmull_n_u32(vget_low_u32(A0), A);
  46. const uint64x2_t C1 = vmull_n_u32(vget_high_u32(A0), A);
  47. const uint64x2_t D0 = vmlal_n_u32(C0, vget_low_u32(B0), B);
  48. const uint64x2_t D1 = vmlal_n_u32(C1, vget_high_u32(B0), B);
  49. const uint32x4_t E = vcombine_u32(
  50. vrshrn_n_u64(D0, WEBP_RESCALER_RFIX),
  51. vrshrn_n_u64(D1, WEBP_RESCALER_RFIX));
  52. return E;
  53. }
  54. static void RescalerExportRowExpand_NEON(WebPRescaler* const wrk) {
  55. int x_out;
  56. uint8_t* const dst = wrk->dst;
  57. rescaler_t* const irow = wrk->irow;
  58. const int x_out_max = wrk->dst_width * wrk->num_channels;
  59. const int max_span = x_out_max & ~7;
  60. const rescaler_t* const frow = wrk->frow;
  61. const uint32_t fy_scale = wrk->fy_scale;
  62. const int32x4_t fy_scale_half = MAKE_HALF_CST(fy_scale);
  63. assert(!WebPRescalerOutputDone(wrk));
  64. assert(wrk->y_accum <= 0);
  65. assert(wrk->y_expand);
  66. assert(wrk->y_sub != 0);
  67. if (wrk->y_accum == 0) {
  68. for (x_out = 0; x_out < max_span; x_out += 8) {
  69. LOAD_32x4(frow + x_out + 0, A0);
  70. LOAD_32x4(frow + x_out + 4, A1);
  71. const uint32x4_t B0 = MULT_FIX(A0, fy_scale_half);
  72. const uint32x4_t B1 = MULT_FIX(A1, fy_scale_half);
  73. const uint16x4_t C0 = vmovn_u32(B0);
  74. const uint16x4_t C1 = vmovn_u32(B1);
  75. const uint8x8_t D = vqmovn_u16(vcombine_u16(C0, C1));
  76. vst1_u8(dst + x_out, D);
  77. }
  78. for (; x_out < x_out_max; ++x_out) {
  79. const uint32_t J = frow[x_out];
  80. const int v = (int)MULT_FIX_C(J, fy_scale);
  81. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  82. }
  83. } else {
  84. const uint32_t B = WEBP_RESCALER_FRAC(-wrk->y_accum, wrk->y_sub);
  85. const uint32_t A = (uint32_t)(WEBP_RESCALER_ONE - B);
  86. for (x_out = 0; x_out < max_span; x_out += 8) {
  87. const uint32x4_t C0 =
  88. Interpolate_NEON(frow + x_out + 0, irow + x_out + 0, A, B);
  89. const uint32x4_t C1 =
  90. Interpolate_NEON(frow + x_out + 4, irow + x_out + 4, A, B);
  91. const uint32x4_t D0 = MULT_FIX(C0, fy_scale_half);
  92. const uint32x4_t D1 = MULT_FIX(C1, fy_scale_half);
  93. const uint16x4_t E0 = vmovn_u32(D0);
  94. const uint16x4_t E1 = vmovn_u32(D1);
  95. const uint8x8_t F = vqmovn_u16(vcombine_u16(E0, E1));
  96. vst1_u8(dst + x_out, F);
  97. }
  98. for (; x_out < x_out_max; ++x_out) {
  99. const uint64_t I = (uint64_t)A * frow[x_out]
  100. + (uint64_t)B * irow[x_out];
  101. const uint32_t J = (uint32_t)((I + ROUNDER) >> WEBP_RESCALER_RFIX);
  102. const int v = (int)MULT_FIX_C(J, fy_scale);
  103. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  104. }
  105. }
  106. }
  107. static void RescalerExportRowShrink_NEON(WebPRescaler* const wrk) {
  108. int x_out;
  109. uint8_t* const dst = wrk->dst;
  110. rescaler_t* const irow = wrk->irow;
  111. const int x_out_max = wrk->dst_width * wrk->num_channels;
  112. const int max_span = x_out_max & ~7;
  113. const rescaler_t* const frow = wrk->frow;
  114. const uint32_t yscale = wrk->fy_scale * (-wrk->y_accum);
  115. const uint32_t fxy_scale = wrk->fxy_scale;
  116. const uint32x4_t zero = vdupq_n_u32(0);
  117. const int32x4_t yscale_half = MAKE_HALF_CST(yscale);
  118. const int32x4_t fxy_scale_half = MAKE_HALF_CST(fxy_scale);
  119. assert(!WebPRescalerOutputDone(wrk));
  120. assert(wrk->y_accum <= 0);
  121. assert(!wrk->y_expand);
  122. if (yscale) {
  123. for (x_out = 0; x_out < max_span; x_out += 8) {
  124. LOAD_32x8(frow + x_out, in0, in1);
  125. LOAD_32x8(irow + x_out, in2, in3);
  126. const uint32x4_t A0 = MULT_FIX_FLOOR(in0, yscale_half);
  127. const uint32x4_t A1 = MULT_FIX_FLOOR(in1, yscale_half);
  128. const uint32x4_t B0 = vqsubq_u32(in2, A0);
  129. const uint32x4_t B1 = vqsubq_u32(in3, A1);
  130. const uint32x4_t C0 = MULT_FIX(B0, fxy_scale_half);
  131. const uint32x4_t C1 = MULT_FIX(B1, fxy_scale_half);
  132. const uint16x4_t D0 = vmovn_u32(C0);
  133. const uint16x4_t D1 = vmovn_u32(C1);
  134. const uint8x8_t E = vqmovn_u16(vcombine_u16(D0, D1));
  135. vst1_u8(dst + x_out, E);
  136. STORE_32x8(A0, A1, irow + x_out);
  137. }
  138. for (; x_out < x_out_max; ++x_out) {
  139. const uint32_t frac = (uint32_t)MULT_FIX_FLOOR_C(frow[x_out], yscale);
  140. const int v = (int)MULT_FIX_C(irow[x_out] - frac, fxy_scale);
  141. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  142. irow[x_out] = frac; // new fractional start
  143. }
  144. } else {
  145. for (x_out = 0; x_out < max_span; x_out += 8) {
  146. LOAD_32x8(irow + x_out, in0, in1);
  147. const uint32x4_t A0 = MULT_FIX(in0, fxy_scale_half);
  148. const uint32x4_t A1 = MULT_FIX(in1, fxy_scale_half);
  149. const uint16x4_t B0 = vmovn_u32(A0);
  150. const uint16x4_t B1 = vmovn_u32(A1);
  151. const uint8x8_t C = vqmovn_u16(vcombine_u16(B0, B1));
  152. vst1_u8(dst + x_out, C);
  153. STORE_32x8(zero, zero, irow + x_out);
  154. }
  155. for (; x_out < x_out_max; ++x_out) {
  156. const int v = (int)MULT_FIX_C(irow[x_out], fxy_scale);
  157. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  158. irow[x_out] = 0;
  159. }
  160. }
  161. }
  162. #undef MULT_FIX_FLOOR_C
  163. #undef MULT_FIX_C
  164. #undef MULT_FIX_FLOOR
  165. #undef MULT_FIX
  166. #undef ROUNDER
  167. //------------------------------------------------------------------------------
  168. extern void WebPRescalerDspInitNEON(void);
  169. WEBP_TSAN_IGNORE_FUNCTION void WebPRescalerDspInitNEON(void) {
  170. WebPRescalerExportRowExpand = RescalerExportRowExpand_NEON;
  171. WebPRescalerExportRowShrink = RescalerExportRowShrink_NEON;
  172. }
  173. #else // !WEBP_USE_NEON
  174. WEBP_DSP_INIT_STUB(WebPRescalerDspInitNEON)
  175. #endif // WEBP_USE_NEON