lossless_neon.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. // NEON variant of methods for lossless decoder
  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. // Colorspace conversion functions
  20. #if !defined(WORK_AROUND_GCC)
  21. // gcc 4.6.0 had some trouble (NDK-r9) with this code. We only use it for
  22. // gcc-4.8.x at least.
  23. static void ConvertBGRAToRGBA(const uint32_t* src,
  24. int num_pixels, uint8_t* dst) {
  25. const uint32_t* const end = src + (num_pixels & ~15);
  26. for (; src < end; src += 16) {
  27. uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);
  28. // swap B and R. (VSWP d0,d2 has no intrinsics equivalent!)
  29. const uint8x16_t tmp = pixel.val[0];
  30. pixel.val[0] = pixel.val[2];
  31. pixel.val[2] = tmp;
  32. vst4q_u8(dst, pixel);
  33. dst += 64;
  34. }
  35. VP8LConvertBGRAToRGBA_C(src, num_pixels & 15, dst); // left-overs
  36. }
  37. static void ConvertBGRAToBGR(const uint32_t* src,
  38. int num_pixels, uint8_t* dst) {
  39. const uint32_t* const end = src + (num_pixels & ~15);
  40. for (; src < end; src += 16) {
  41. const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);
  42. const uint8x16x3_t tmp = { { pixel.val[0], pixel.val[1], pixel.val[2] } };
  43. vst3q_u8(dst, tmp);
  44. dst += 48;
  45. }
  46. VP8LConvertBGRAToBGR_C(src, num_pixels & 15, dst); // left-overs
  47. }
  48. static void ConvertBGRAToRGB(const uint32_t* src,
  49. int num_pixels, uint8_t* dst) {
  50. const uint32_t* const end = src + (num_pixels & ~15);
  51. for (; src < end; src += 16) {
  52. const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);
  53. const uint8x16x3_t tmp = { { pixel.val[2], pixel.val[1], pixel.val[0] } };
  54. vst3q_u8(dst, tmp);
  55. dst += 48;
  56. }
  57. VP8LConvertBGRAToRGB_C(src, num_pixels & 15, dst); // left-overs
  58. }
  59. #else // WORK_AROUND_GCC
  60. // gcc-4.6.0 fallback
  61. static const uint8_t kRGBAShuffle[8] = { 2, 1, 0, 3, 6, 5, 4, 7 };
  62. static void ConvertBGRAToRGBA(const uint32_t* src,
  63. int num_pixels, uint8_t* dst) {
  64. const uint32_t* const end = src + (num_pixels & ~1);
  65. const uint8x8_t shuffle = vld1_u8(kRGBAShuffle);
  66. for (; src < end; src += 2) {
  67. const uint8x8_t pixels = vld1_u8((uint8_t*)src);
  68. vst1_u8(dst, vtbl1_u8(pixels, shuffle));
  69. dst += 8;
  70. }
  71. VP8LConvertBGRAToRGBA_C(src, num_pixels & 1, dst); // left-overs
  72. }
  73. static const uint8_t kBGRShuffle[3][8] = {
  74. { 0, 1, 2, 4, 5, 6, 8, 9 },
  75. { 10, 12, 13, 14, 16, 17, 18, 20 },
  76. { 21, 22, 24, 25, 26, 28, 29, 30 }
  77. };
  78. static void ConvertBGRAToBGR(const uint32_t* src,
  79. int num_pixels, uint8_t* dst) {
  80. const uint32_t* const end = src + (num_pixels & ~7);
  81. const uint8x8_t shuffle0 = vld1_u8(kBGRShuffle[0]);
  82. const uint8x8_t shuffle1 = vld1_u8(kBGRShuffle[1]);
  83. const uint8x8_t shuffle2 = vld1_u8(kBGRShuffle[2]);
  84. for (; src < end; src += 8) {
  85. uint8x8x4_t pixels;
  86. INIT_VECTOR4(pixels,
  87. vld1_u8((const uint8_t*)(src + 0)),
  88. vld1_u8((const uint8_t*)(src + 2)),
  89. vld1_u8((const uint8_t*)(src + 4)),
  90. vld1_u8((const uint8_t*)(src + 6)));
  91. vst1_u8(dst + 0, vtbl4_u8(pixels, shuffle0));
  92. vst1_u8(dst + 8, vtbl4_u8(pixels, shuffle1));
  93. vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));
  94. dst += 8 * 3;
  95. }
  96. VP8LConvertBGRAToBGR_C(src, num_pixels & 7, dst); // left-overs
  97. }
  98. static const uint8_t kRGBShuffle[3][8] = {
  99. { 2, 1, 0, 6, 5, 4, 10, 9 },
  100. { 8, 14, 13, 12, 18, 17, 16, 22 },
  101. { 21, 20, 26, 25, 24, 30, 29, 28 }
  102. };
  103. static void ConvertBGRAToRGB(const uint32_t* src,
  104. int num_pixels, uint8_t* dst) {
  105. const uint32_t* const end = src + (num_pixels & ~7);
  106. const uint8x8_t shuffle0 = vld1_u8(kRGBShuffle[0]);
  107. const uint8x8_t shuffle1 = vld1_u8(kRGBShuffle[1]);
  108. const uint8x8_t shuffle2 = vld1_u8(kRGBShuffle[2]);
  109. for (; src < end; src += 8) {
  110. uint8x8x4_t pixels;
  111. INIT_VECTOR4(pixels,
  112. vld1_u8((const uint8_t*)(src + 0)),
  113. vld1_u8((const uint8_t*)(src + 2)),
  114. vld1_u8((const uint8_t*)(src + 4)),
  115. vld1_u8((const uint8_t*)(src + 6)));
  116. vst1_u8(dst + 0, vtbl4_u8(pixels, shuffle0));
  117. vst1_u8(dst + 8, vtbl4_u8(pixels, shuffle1));
  118. vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));
  119. dst += 8 * 3;
  120. }
  121. VP8LConvertBGRAToRGB_C(src, num_pixels & 7, dst); // left-overs
  122. }
  123. #endif // !WORK_AROUND_GCC
  124. //------------------------------------------------------------------------------
  125. // Predictor Transform
  126. #define LOAD_U32_AS_U8(IN) vreinterpret_u8_u32(vdup_n_u32((IN)))
  127. #define LOAD_U32P_AS_U8(IN) vreinterpret_u8_u32(vld1_u32((IN)))
  128. #define LOADQ_U32_AS_U8(IN) vreinterpretq_u8_u32(vdupq_n_u32((IN)))
  129. #define LOADQ_U32P_AS_U8(IN) vreinterpretq_u8_u32(vld1q_u32((IN)))
  130. #define GET_U8_AS_U32(IN) vget_lane_u32(vreinterpret_u32_u8((IN)), 0);
  131. #define GETQ_U8_AS_U32(IN) vgetq_lane_u32(vreinterpretq_u32_u8((IN)), 0);
  132. #define STOREQ_U8_AS_U32P(OUT, IN) vst1q_u32((OUT), vreinterpretq_u32_u8((IN)));
  133. #define ROTATE32_LEFT(L) vextq_u8((L), (L), 12) // D|C|B|A -> C|B|A|D
  134. static WEBP_INLINE uint8x8_t Average2_u8_NEON(uint32_t a0, uint32_t a1) {
  135. const uint8x8_t A0 = LOAD_U32_AS_U8(a0);
  136. const uint8x8_t A1 = LOAD_U32_AS_U8(a1);
  137. return vhadd_u8(A0, A1);
  138. }
  139. static WEBP_INLINE uint32_t ClampedAddSubtractHalf_NEON(uint32_t c0,
  140. uint32_t c1,
  141. uint32_t c2) {
  142. const uint8x8_t avg = Average2_u8_NEON(c0, c1);
  143. // Remove one to c2 when bigger than avg.
  144. const uint8x8_t C2 = LOAD_U32_AS_U8(c2);
  145. const uint8x8_t cmp = vcgt_u8(C2, avg);
  146. const uint8x8_t C2_1 = vadd_u8(C2, cmp);
  147. // Compute half of the difference between avg and c2.
  148. const int8x8_t diff_avg = vreinterpret_s8_u8(vhsub_u8(avg, C2_1));
  149. // Compute the sum with avg and saturate.
  150. const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(avg));
  151. const uint8x8_t res = vqmovun_s16(vaddw_s8(avg_16, diff_avg));
  152. const uint32_t output = GET_U8_AS_U32(res);
  153. return output;
  154. }
  155. static WEBP_INLINE uint32_t Average2_NEON(uint32_t a0, uint32_t a1) {
  156. const uint8x8_t avg_u8x8 = Average2_u8_NEON(a0, a1);
  157. const uint32_t avg = GET_U8_AS_U32(avg_u8x8);
  158. return avg;
  159. }
  160. static WEBP_INLINE uint32_t Average3_NEON(uint32_t a0, uint32_t a1,
  161. uint32_t a2) {
  162. const uint8x8_t avg0 = Average2_u8_NEON(a0, a2);
  163. const uint8x8_t A1 = LOAD_U32_AS_U8(a1);
  164. const uint32_t avg = GET_U8_AS_U32(vhadd_u8(avg0, A1));
  165. return avg;
  166. }
  167. static uint32_t Predictor5_NEON(uint32_t left, const uint32_t* const top) {
  168. return Average3_NEON(left, top[0], top[1]);
  169. }
  170. static uint32_t Predictor6_NEON(uint32_t left, const uint32_t* const top) {
  171. return Average2_NEON(left, top[-1]);
  172. }
  173. static uint32_t Predictor7_NEON(uint32_t left, const uint32_t* const top) {
  174. return Average2_NEON(left, top[0]);
  175. }
  176. static uint32_t Predictor13_NEON(uint32_t left, const uint32_t* const top) {
  177. return ClampedAddSubtractHalf_NEON(left, top[0], top[-1]);
  178. }
  179. // Batch versions of those functions.
  180. // Predictor0: ARGB_BLACK.
  181. static void PredictorAdd0_NEON(const uint32_t* in, const uint32_t* upper,
  182. int num_pixels, uint32_t* out) {
  183. int i;
  184. const uint8x16_t black = vreinterpretq_u8_u32(vdupq_n_u32(ARGB_BLACK));
  185. for (i = 0; i + 4 <= num_pixels; i += 4) {
  186. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  187. const uint8x16_t res = vaddq_u8(src, black);
  188. STOREQ_U8_AS_U32P(&out[i], res);
  189. }
  190. VP8LPredictorsAdd_C[0](in + i, upper + i, num_pixels - i, out + i);
  191. }
  192. // Predictor1: left.
  193. static void PredictorAdd1_NEON(const uint32_t* in, const uint32_t* upper,
  194. int num_pixels, uint32_t* out) {
  195. int i;
  196. const uint8x16_t zero = LOADQ_U32_AS_U8(0);
  197. for (i = 0; i + 4 <= num_pixels; i += 4) {
  198. // a | b | c | d
  199. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  200. // 0 | a | b | c
  201. const uint8x16_t shift0 = vextq_u8(zero, src, 12);
  202. // a | a + b | b + c | c + d
  203. const uint8x16_t sum0 = vaddq_u8(src, shift0);
  204. // 0 | 0 | a | a + b
  205. const uint8x16_t shift1 = vextq_u8(zero, sum0, 8);
  206. // a | a + b | a + b + c | a + b + c + d
  207. const uint8x16_t sum1 = vaddq_u8(sum0, shift1);
  208. const uint8x16_t prev = LOADQ_U32_AS_U8(out[i - 1]);
  209. const uint8x16_t res = vaddq_u8(sum1, prev);
  210. STOREQ_U8_AS_U32P(&out[i], res);
  211. }
  212. VP8LPredictorsAdd_C[1](in + i, upper + i, num_pixels - i, out + i);
  213. }
  214. // Macro that adds 32-bit integers from IN using mod 256 arithmetic
  215. // per 8 bit channel.
  216. #define GENERATE_PREDICTOR_1(X, IN) \
  217. static void PredictorAdd##X##_NEON(const uint32_t* in, \
  218. const uint32_t* upper, int num_pixels, \
  219. uint32_t* out) { \
  220. int i; \
  221. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  222. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \
  223. const uint8x16_t other = LOADQ_U32P_AS_U8(&(IN)); \
  224. const uint8x16_t res = vaddq_u8(src, other); \
  225. STOREQ_U8_AS_U32P(&out[i], res); \
  226. } \
  227. VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \
  228. }
  229. // Predictor2: Top.
  230. GENERATE_PREDICTOR_1(2, upper[i])
  231. // Predictor3: Top-right.
  232. GENERATE_PREDICTOR_1(3, upper[i + 1])
  233. // Predictor4: Top-left.
  234. GENERATE_PREDICTOR_1(4, upper[i - 1])
  235. #undef GENERATE_PREDICTOR_1
  236. // Predictor5: average(average(left, TR), T)
  237. #define DO_PRED5(LANE) do { \
  238. const uint8x16_t avgLTR = vhaddq_u8(L, TR); \
  239. const uint8x16_t avg = vhaddq_u8(avgLTR, T); \
  240. const uint8x16_t res = vaddq_u8(avg, src); \
  241. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  242. L = ROTATE32_LEFT(res); \
  243. } while (0)
  244. static void PredictorAdd5_NEON(const uint32_t* in, const uint32_t* upper,
  245. int num_pixels, uint32_t* out) {
  246. int i;
  247. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  248. for (i = 0; i + 4 <= num_pixels; i += 4) {
  249. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  250. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i + 0]);
  251. const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);
  252. DO_PRED5(0);
  253. DO_PRED5(1);
  254. DO_PRED5(2);
  255. DO_PRED5(3);
  256. }
  257. VP8LPredictorsAdd_C[5](in + i, upper + i, num_pixels - i, out + i);
  258. }
  259. #undef DO_PRED5
  260. #define DO_PRED67(LANE) do { \
  261. const uint8x16_t avg = vhaddq_u8(L, top); \
  262. const uint8x16_t res = vaddq_u8(avg, src); \
  263. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  264. L = ROTATE32_LEFT(res); \
  265. } while (0)
  266. // Predictor6: average(left, TL)
  267. static void PredictorAdd6_NEON(const uint32_t* in, const uint32_t* upper,
  268. int num_pixels, uint32_t* out) {
  269. int i;
  270. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  271. for (i = 0; i + 4 <= num_pixels; i += 4) {
  272. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  273. const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i - 1]);
  274. DO_PRED67(0);
  275. DO_PRED67(1);
  276. DO_PRED67(2);
  277. DO_PRED67(3);
  278. }
  279. VP8LPredictorsAdd_C[6](in + i, upper + i, num_pixels - i, out + i);
  280. }
  281. // Predictor7: average(left, T)
  282. static void PredictorAdd7_NEON(const uint32_t* in, const uint32_t* upper,
  283. int num_pixels, uint32_t* out) {
  284. int i;
  285. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  286. for (i = 0; i + 4 <= num_pixels; i += 4) {
  287. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  288. const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i]);
  289. DO_PRED67(0);
  290. DO_PRED67(1);
  291. DO_PRED67(2);
  292. DO_PRED67(3);
  293. }
  294. VP8LPredictorsAdd_C[7](in + i, upper + i, num_pixels - i, out + i);
  295. }
  296. #undef DO_PRED67
  297. #define GENERATE_PREDICTOR_2(X, IN) \
  298. static void PredictorAdd##X##_NEON(const uint32_t* in, \
  299. const uint32_t* upper, int num_pixels, \
  300. uint32_t* out) { \
  301. int i; \
  302. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  303. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \
  304. const uint8x16_t Tother = LOADQ_U32P_AS_U8(&(IN)); \
  305. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]); \
  306. const uint8x16_t avg = vhaddq_u8(T, Tother); \
  307. const uint8x16_t res = vaddq_u8(avg, src); \
  308. STOREQ_U8_AS_U32P(&out[i], res); \
  309. } \
  310. VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \
  311. }
  312. // Predictor8: average TL T.
  313. GENERATE_PREDICTOR_2(8, upper[i - 1])
  314. // Predictor9: average T TR.
  315. GENERATE_PREDICTOR_2(9, upper[i + 1])
  316. #undef GENERATE_PREDICTOR_2
  317. // Predictor10: average of (average of (L,TL), average of (T, TR)).
  318. #define DO_PRED10(LANE) do { \
  319. const uint8x16_t avgLTL = vhaddq_u8(L, TL); \
  320. const uint8x16_t avg = vhaddq_u8(avgTTR, avgLTL); \
  321. const uint8x16_t res = vaddq_u8(avg, src); \
  322. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  323. L = ROTATE32_LEFT(res); \
  324. } while (0)
  325. static void PredictorAdd10_NEON(const uint32_t* in, const uint32_t* upper,
  326. int num_pixels, uint32_t* out) {
  327. int i;
  328. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  329. for (i = 0; i + 4 <= num_pixels; i += 4) {
  330. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  331. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  332. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  333. const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);
  334. const uint8x16_t avgTTR = vhaddq_u8(T, TR);
  335. DO_PRED10(0);
  336. DO_PRED10(1);
  337. DO_PRED10(2);
  338. DO_PRED10(3);
  339. }
  340. VP8LPredictorsAdd_C[10](in + i, upper + i, num_pixels - i, out + i);
  341. }
  342. #undef DO_PRED10
  343. // Predictor11: select.
  344. #define DO_PRED11(LANE) do { \
  345. const uint8x16_t sumLin = vaddq_u8(L, src); /* in + L */ \
  346. const uint8x16_t pLTL = vabdq_u8(L, TL); /* |L - TL| */ \
  347. const uint16x8_t sum_LTL = vpaddlq_u8(pLTL); \
  348. const uint32x4_t pa = vpaddlq_u16(sum_LTL); \
  349. const uint32x4_t mask = vcleq_u32(pa, pb); \
  350. const uint8x16_t res = vbslq_u8(vreinterpretq_u8_u32(mask), sumTin, sumLin); \
  351. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  352. L = ROTATE32_LEFT(res); \
  353. } while (0)
  354. static void PredictorAdd11_NEON(const uint32_t* in, const uint32_t* upper,
  355. int num_pixels, uint32_t* out) {
  356. int i;
  357. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  358. for (i = 0; i + 4 <= num_pixels; i += 4) {
  359. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  360. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  361. const uint8x16_t pTTL = vabdq_u8(T, TL); // |T - TL|
  362. const uint16x8_t sum_TTL = vpaddlq_u8(pTTL);
  363. const uint32x4_t pb = vpaddlq_u16(sum_TTL);
  364. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  365. const uint8x16_t sumTin = vaddq_u8(T, src); // in + T
  366. DO_PRED11(0);
  367. DO_PRED11(1);
  368. DO_PRED11(2);
  369. DO_PRED11(3);
  370. }
  371. VP8LPredictorsAdd_C[11](in + i, upper + i, num_pixels - i, out + i);
  372. }
  373. #undef DO_PRED11
  374. // Predictor12: ClampedAddSubtractFull.
  375. #define DO_PRED12(DIFF, LANE) do { \
  376. const uint8x8_t pred = \
  377. vqmovun_s16(vaddq_s16(vreinterpretq_s16_u16(L), (DIFF))); \
  378. const uint8x8_t res = \
  379. vadd_u8(pred, (LANE <= 1) ? vget_low_u8(src) : vget_high_u8(src)); \
  380. const uint16x8_t res16 = vmovl_u8(res); \
  381. vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \
  382. /* rotate in the left predictor for next iteration */ \
  383. L = vextq_u16(res16, res16, 4); \
  384. } while (0)
  385. static void PredictorAdd12_NEON(const uint32_t* in, const uint32_t* upper,
  386. int num_pixels, uint32_t* out) {
  387. int i;
  388. uint16x8_t L = vmovl_u8(LOAD_U32_AS_U8(out[-1]));
  389. for (i = 0; i + 4 <= num_pixels; i += 4) {
  390. // load four pixels of source
  391. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  392. // precompute the difference T - TL once for all, stored as s16
  393. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  394. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  395. const int16x8_t diff_lo =
  396. vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(T), vget_low_u8(TL)));
  397. const int16x8_t diff_hi =
  398. vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(T), vget_high_u8(TL)));
  399. // loop over the four reconstructed pixels
  400. DO_PRED12(diff_lo, 0);
  401. DO_PRED12(diff_lo, 1);
  402. DO_PRED12(diff_hi, 2);
  403. DO_PRED12(diff_hi, 3);
  404. }
  405. VP8LPredictorsAdd_C[12](in + i, upper + i, num_pixels - i, out + i);
  406. }
  407. #undef DO_PRED12
  408. // Predictor13: ClampedAddSubtractHalf
  409. #define DO_PRED13(LANE, LOW_OR_HI) do { \
  410. const uint8x16_t avg = vhaddq_u8(L, T); \
  411. const uint8x16_t cmp = vcgtq_u8(TL, avg); \
  412. const uint8x16_t TL_1 = vaddq_u8(TL, cmp); \
  413. /* Compute half of the difference between avg and TL'. */ \
  414. const int8x8_t diff_avg = \
  415. vreinterpret_s8_u8(LOW_OR_HI(vhsubq_u8(avg, TL_1))); \
  416. /* Compute the sum with avg and saturate. */ \
  417. const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(LOW_OR_HI(avg))); \
  418. const uint8x8_t delta = vqmovun_s16(vaddw_s8(avg_16, diff_avg)); \
  419. const uint8x8_t res = vadd_u8(LOW_OR_HI(src), delta); \
  420. const uint8x16_t res2 = vcombine_u8(res, res); \
  421. vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \
  422. L = ROTATE32_LEFT(res2); \
  423. } while (0)
  424. static void PredictorAdd13_NEON(const uint32_t* in, const uint32_t* upper,
  425. int num_pixels, uint32_t* out) {
  426. int i;
  427. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  428. for (i = 0; i + 4 <= num_pixels; i += 4) {
  429. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  430. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  431. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  432. DO_PRED13(0, vget_low_u8);
  433. DO_PRED13(1, vget_low_u8);
  434. DO_PRED13(2, vget_high_u8);
  435. DO_PRED13(3, vget_high_u8);
  436. }
  437. VP8LPredictorsAdd_C[13](in + i, upper + i, num_pixels - i, out + i);
  438. }
  439. #undef DO_PRED13
  440. #undef LOAD_U32_AS_U8
  441. #undef LOAD_U32P_AS_U8
  442. #undef LOADQ_U32_AS_U8
  443. #undef LOADQ_U32P_AS_U8
  444. #undef GET_U8_AS_U32
  445. #undef GETQ_U8_AS_U32
  446. #undef STOREQ_U8_AS_U32P
  447. #undef ROTATE32_LEFT
  448. //------------------------------------------------------------------------------
  449. // Subtract-Green Transform
  450. // vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use
  451. // non-standard versions there.
  452. #if defined(__APPLE__) && defined(__aarch64__) && \
  453. defined(__apple_build_version__) && (__apple_build_version__< 6020037)
  454. #define USE_VTBLQ
  455. #endif
  456. #ifdef USE_VTBLQ
  457. // 255 = byte will be zeroed
  458. static const uint8_t kGreenShuffle[16] = {
  459. 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255
  460. };
  461. static WEBP_INLINE uint8x16_t DoGreenShuffle(const uint8x16_t argb,
  462. const uint8x16_t shuffle) {
  463. return vcombine_u8(vtbl1q_u8(argb, vget_low_u8(shuffle)),
  464. vtbl1q_u8(argb, vget_high_u8(shuffle)));
  465. }
  466. #else // !USE_VTBLQ
  467. // 255 = byte will be zeroed
  468. static const uint8_t kGreenShuffle[8] = { 1, 255, 1, 255, 5, 255, 5, 255 };
  469. static WEBP_INLINE uint8x16_t DoGreenShuffle(const uint8x16_t argb,
  470. const uint8x8_t shuffle) {
  471. return vcombine_u8(vtbl1_u8(vget_low_u8(argb), shuffle),
  472. vtbl1_u8(vget_high_u8(argb), shuffle));
  473. }
  474. #endif // USE_VTBLQ
  475. static void AddGreenToBlueAndRed(const uint32_t* src, int num_pixels,
  476. uint32_t* dst) {
  477. const uint32_t* const end = src + (num_pixels & ~3);
  478. #ifdef USE_VTBLQ
  479. const uint8x16_t shuffle = vld1q_u8(kGreenShuffle);
  480. #else
  481. const uint8x8_t shuffle = vld1_u8(kGreenShuffle);
  482. #endif
  483. for (; src < end; src += 4, dst += 4) {
  484. const uint8x16_t argb = vld1q_u8((const uint8_t*)src);
  485. const uint8x16_t greens = DoGreenShuffle(argb, shuffle);
  486. vst1q_u8((uint8_t*)dst, vaddq_u8(argb, greens));
  487. }
  488. // fallthrough and finish off with plain-C
  489. VP8LAddGreenToBlueAndRed_C(src, num_pixels & 3, dst);
  490. }
  491. //------------------------------------------------------------------------------
  492. // Color Transform
  493. static void TransformColorInverse(const VP8LMultipliers* const m,
  494. const uint32_t* const src, int num_pixels,
  495. uint32_t* dst) {
  496. // sign-extended multiplying constants, pre-shifted by 6.
  497. #define CST(X) (((int16_t)(m->X << 8)) >> 6)
  498. const int16_t rb[8] = {
  499. CST(green_to_blue_), CST(green_to_red_),
  500. CST(green_to_blue_), CST(green_to_red_),
  501. CST(green_to_blue_), CST(green_to_red_),
  502. CST(green_to_blue_), CST(green_to_red_)
  503. };
  504. const int16x8_t mults_rb = vld1q_s16(rb);
  505. const int16_t b2[8] = {
  506. 0, CST(red_to_blue_), 0, CST(red_to_blue_),
  507. 0, CST(red_to_blue_), 0, CST(red_to_blue_),
  508. };
  509. const int16x8_t mults_b2 = vld1q_s16(b2);
  510. #undef CST
  511. #ifdef USE_VTBLQ
  512. static const uint8_t kg0g0[16] = {
  513. 255, 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13
  514. };
  515. const uint8x16_t shuffle = vld1q_u8(kg0g0);
  516. #else
  517. static const uint8_t k0g0g[8] = { 255, 1, 255, 1, 255, 5, 255, 5 };
  518. const uint8x8_t shuffle = vld1_u8(k0g0g);
  519. #endif
  520. const uint32x4_t mask_ag = vdupq_n_u32(0xff00ff00u);
  521. int i;
  522. for (i = 0; i + 4 <= num_pixels; i += 4) {
  523. const uint8x16_t in = vld1q_u8((const uint8_t*)(src + i));
  524. const uint32x4_t a0g0 = vandq_u32(vreinterpretq_u32_u8(in), mask_ag);
  525. // 0 g 0 g
  526. const uint8x16_t greens = DoGreenShuffle(in, shuffle);
  527. // x dr x db1
  528. const int16x8_t A = vqdmulhq_s16(vreinterpretq_s16_u8(greens), mults_rb);
  529. // x r' x b'
  530. const int8x16_t B = vaddq_s8(vreinterpretq_s8_u8(in),
  531. vreinterpretq_s8_s16(A));
  532. // r' 0 b' 0
  533. const int16x8_t C = vshlq_n_s16(vreinterpretq_s16_s8(B), 8);
  534. // x db2 0 0
  535. const int16x8_t D = vqdmulhq_s16(C, mults_b2);
  536. // 0 x db2 0
  537. const uint32x4_t E = vshrq_n_u32(vreinterpretq_u32_s16(D), 8);
  538. // r' x b'' 0
  539. const int8x16_t F = vaddq_s8(vreinterpretq_s8_u32(E),
  540. vreinterpretq_s8_s16(C));
  541. // 0 r' 0 b''
  542. const uint16x8_t G = vshrq_n_u16(vreinterpretq_u16_s8(F), 8);
  543. const uint32x4_t out = vorrq_u32(vreinterpretq_u32_u16(G), a0g0);
  544. vst1q_u32(dst + i, out);
  545. }
  546. // Fall-back to C-version for left-overs.
  547. VP8LTransformColorInverse_C(m, src + i, num_pixels - i, dst + i);
  548. }
  549. #undef USE_VTBLQ
  550. //------------------------------------------------------------------------------
  551. // Entry point
  552. extern void VP8LDspInitNEON(void);
  553. WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitNEON(void) {
  554. VP8LPredictors[5] = Predictor5_NEON;
  555. VP8LPredictors[6] = Predictor6_NEON;
  556. VP8LPredictors[7] = Predictor7_NEON;
  557. VP8LPredictors[13] = Predictor13_NEON;
  558. VP8LPredictorsAdd[0] = PredictorAdd0_NEON;
  559. VP8LPredictorsAdd[1] = PredictorAdd1_NEON;
  560. VP8LPredictorsAdd[2] = PredictorAdd2_NEON;
  561. VP8LPredictorsAdd[3] = PredictorAdd3_NEON;
  562. VP8LPredictorsAdd[4] = PredictorAdd4_NEON;
  563. VP8LPredictorsAdd[5] = PredictorAdd5_NEON;
  564. VP8LPredictorsAdd[6] = PredictorAdd6_NEON;
  565. VP8LPredictorsAdd[7] = PredictorAdd7_NEON;
  566. VP8LPredictorsAdd[8] = PredictorAdd8_NEON;
  567. VP8LPredictorsAdd[9] = PredictorAdd9_NEON;
  568. VP8LPredictorsAdd[10] = PredictorAdd10_NEON;
  569. VP8LPredictorsAdd[11] = PredictorAdd11_NEON;
  570. VP8LPredictorsAdd[12] = PredictorAdd12_NEON;
  571. VP8LPredictorsAdd[13] = PredictorAdd13_NEON;
  572. VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA;
  573. VP8LConvertBGRAToBGR = ConvertBGRAToBGR;
  574. VP8LConvertBGRAToRGB = ConvertBGRAToRGB;
  575. VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed;
  576. VP8LTransformColorInverse = TransformColorInverse;
  577. }
  578. #else // !WEBP_USE_NEON
  579. WEBP_DSP_INIT_STUB(VP8LDspInitNEON)
  580. #endif // WEBP_USE_NEON