lossless_neon.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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_NEON(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_NEON(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_NEON(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_NEON(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_NEON(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_NEON(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(const uint32_t* const left,
  168. const uint32_t* const top) {
  169. return Average3_NEON(*left, top[0], top[1]);
  170. }
  171. static uint32_t Predictor6_NEON(const uint32_t* const left,
  172. const uint32_t* const top) {
  173. return Average2_NEON(*left, top[-1]);
  174. }
  175. static uint32_t Predictor7_NEON(const uint32_t* const left,
  176. const uint32_t* const top) {
  177. return Average2_NEON(*left, top[0]);
  178. }
  179. static uint32_t Predictor13_NEON(const uint32_t* const left,
  180. const uint32_t* const top) {
  181. return ClampedAddSubtractHalf_NEON(*left, top[0], top[-1]);
  182. }
  183. // Batch versions of those functions.
  184. // Predictor0: ARGB_BLACK.
  185. static void PredictorAdd0_NEON(const uint32_t* in, const uint32_t* upper,
  186. int num_pixels, uint32_t* out) {
  187. int i;
  188. const uint8x16_t black = vreinterpretq_u8_u32(vdupq_n_u32(ARGB_BLACK));
  189. for (i = 0; i + 4 <= num_pixels; i += 4) {
  190. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  191. const uint8x16_t res = vaddq_u8(src, black);
  192. STOREQ_U8_AS_U32P(&out[i], res);
  193. }
  194. VP8LPredictorsAdd_C[0](in + i, upper + i, num_pixels - i, out + i);
  195. }
  196. // Predictor1: left.
  197. static void PredictorAdd1_NEON(const uint32_t* in, const uint32_t* upper,
  198. int num_pixels, uint32_t* out) {
  199. int i;
  200. const uint8x16_t zero = LOADQ_U32_AS_U8(0);
  201. for (i = 0; i + 4 <= num_pixels; i += 4) {
  202. // a | b | c | d
  203. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  204. // 0 | a | b | c
  205. const uint8x16_t shift0 = vextq_u8(zero, src, 12);
  206. // a | a + b | b + c | c + d
  207. const uint8x16_t sum0 = vaddq_u8(src, shift0);
  208. // 0 | 0 | a | a + b
  209. const uint8x16_t shift1 = vextq_u8(zero, sum0, 8);
  210. // a | a + b | a + b + c | a + b + c + d
  211. const uint8x16_t sum1 = vaddq_u8(sum0, shift1);
  212. const uint8x16_t prev = LOADQ_U32_AS_U8(out[i - 1]);
  213. const uint8x16_t res = vaddq_u8(sum1, prev);
  214. STOREQ_U8_AS_U32P(&out[i], res);
  215. }
  216. VP8LPredictorsAdd_C[1](in + i, upper + i, num_pixels - i, out + i);
  217. }
  218. // Macro that adds 32-bit integers from IN using mod 256 arithmetic
  219. // per 8 bit channel.
  220. #define GENERATE_PREDICTOR_1(X, IN) \
  221. static void PredictorAdd##X##_NEON(const uint32_t* in, \
  222. const uint32_t* upper, int num_pixels, \
  223. uint32_t* out) { \
  224. int i; \
  225. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  226. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \
  227. const uint8x16_t other = LOADQ_U32P_AS_U8(&(IN)); \
  228. const uint8x16_t res = vaddq_u8(src, other); \
  229. STOREQ_U8_AS_U32P(&out[i], res); \
  230. } \
  231. VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \
  232. }
  233. // Predictor2: Top.
  234. GENERATE_PREDICTOR_1(2, upper[i])
  235. // Predictor3: Top-right.
  236. GENERATE_PREDICTOR_1(3, upper[i + 1])
  237. // Predictor4: Top-left.
  238. GENERATE_PREDICTOR_1(4, upper[i - 1])
  239. #undef GENERATE_PREDICTOR_1
  240. // Predictor5: average(average(left, TR), T)
  241. #define DO_PRED5(LANE) do { \
  242. const uint8x16_t avgLTR = vhaddq_u8(L, TR); \
  243. const uint8x16_t avg = vhaddq_u8(avgLTR, T); \
  244. const uint8x16_t res = vaddq_u8(avg, src); \
  245. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  246. L = ROTATE32_LEFT(res); \
  247. } while (0)
  248. static void PredictorAdd5_NEON(const uint32_t* in, const uint32_t* upper,
  249. int num_pixels, uint32_t* out) {
  250. int i;
  251. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  252. for (i = 0; i + 4 <= num_pixels; i += 4) {
  253. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  254. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i + 0]);
  255. const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);
  256. DO_PRED5(0);
  257. DO_PRED5(1);
  258. DO_PRED5(2);
  259. DO_PRED5(3);
  260. }
  261. VP8LPredictorsAdd_C[5](in + i, upper + i, num_pixels - i, out + i);
  262. }
  263. #undef DO_PRED5
  264. #define DO_PRED67(LANE) do { \
  265. const uint8x16_t avg = vhaddq_u8(L, top); \
  266. const uint8x16_t res = vaddq_u8(avg, src); \
  267. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  268. L = ROTATE32_LEFT(res); \
  269. } while (0)
  270. // Predictor6: average(left, TL)
  271. static void PredictorAdd6_NEON(const uint32_t* in, const uint32_t* upper,
  272. int num_pixels, uint32_t* out) {
  273. int i;
  274. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  275. for (i = 0; i + 4 <= num_pixels; i += 4) {
  276. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  277. const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i - 1]);
  278. DO_PRED67(0);
  279. DO_PRED67(1);
  280. DO_PRED67(2);
  281. DO_PRED67(3);
  282. }
  283. VP8LPredictorsAdd_C[6](in + i, upper + i, num_pixels - i, out + i);
  284. }
  285. // Predictor7: average(left, T)
  286. static void PredictorAdd7_NEON(const uint32_t* in, const uint32_t* upper,
  287. int num_pixels, uint32_t* out) {
  288. int i;
  289. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  290. for (i = 0; i + 4 <= num_pixels; i += 4) {
  291. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  292. const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i]);
  293. DO_PRED67(0);
  294. DO_PRED67(1);
  295. DO_PRED67(2);
  296. DO_PRED67(3);
  297. }
  298. VP8LPredictorsAdd_C[7](in + i, upper + i, num_pixels - i, out + i);
  299. }
  300. #undef DO_PRED67
  301. #define GENERATE_PREDICTOR_2(X, IN) \
  302. static void PredictorAdd##X##_NEON(const uint32_t* in, \
  303. const uint32_t* upper, int num_pixels, \
  304. uint32_t* out) { \
  305. int i; \
  306. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  307. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \
  308. const uint8x16_t Tother = LOADQ_U32P_AS_U8(&(IN)); \
  309. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]); \
  310. const uint8x16_t avg = vhaddq_u8(T, Tother); \
  311. const uint8x16_t res = vaddq_u8(avg, src); \
  312. STOREQ_U8_AS_U32P(&out[i], res); \
  313. } \
  314. VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \
  315. }
  316. // Predictor8: average TL T.
  317. GENERATE_PREDICTOR_2(8, upper[i - 1])
  318. // Predictor9: average T TR.
  319. GENERATE_PREDICTOR_2(9, upper[i + 1])
  320. #undef GENERATE_PREDICTOR_2
  321. // Predictor10: average of (average of (L,TL), average of (T, TR)).
  322. #define DO_PRED10(LANE) do { \
  323. const uint8x16_t avgLTL = vhaddq_u8(L, TL); \
  324. const uint8x16_t avg = vhaddq_u8(avgTTR, avgLTL); \
  325. const uint8x16_t res = vaddq_u8(avg, src); \
  326. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  327. L = ROTATE32_LEFT(res); \
  328. } while (0)
  329. static void PredictorAdd10_NEON(const uint32_t* in, const uint32_t* upper,
  330. int num_pixels, uint32_t* out) {
  331. int i;
  332. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  333. for (i = 0; i + 4 <= num_pixels; i += 4) {
  334. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  335. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  336. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  337. const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);
  338. const uint8x16_t avgTTR = vhaddq_u8(T, TR);
  339. DO_PRED10(0);
  340. DO_PRED10(1);
  341. DO_PRED10(2);
  342. DO_PRED10(3);
  343. }
  344. VP8LPredictorsAdd_C[10](in + i, upper + i, num_pixels - i, out + i);
  345. }
  346. #undef DO_PRED10
  347. // Predictor11: select.
  348. #define DO_PRED11(LANE) do { \
  349. const uint8x16_t sumLin = vaddq_u8(L, src); /* in + L */ \
  350. const uint8x16_t pLTL = vabdq_u8(L, TL); /* |L - TL| */ \
  351. const uint16x8_t sum_LTL = vpaddlq_u8(pLTL); \
  352. const uint32x4_t pa = vpaddlq_u16(sum_LTL); \
  353. const uint32x4_t mask = vcleq_u32(pa, pb); \
  354. const uint8x16_t res = vbslq_u8(vreinterpretq_u8_u32(mask), sumTin, sumLin); \
  355. vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \
  356. L = ROTATE32_LEFT(res); \
  357. } while (0)
  358. static void PredictorAdd11_NEON(const uint32_t* in, const uint32_t* upper,
  359. int num_pixels, uint32_t* out) {
  360. int i;
  361. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  362. for (i = 0; i + 4 <= num_pixels; i += 4) {
  363. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  364. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  365. const uint8x16_t pTTL = vabdq_u8(T, TL); // |T - TL|
  366. const uint16x8_t sum_TTL = vpaddlq_u8(pTTL);
  367. const uint32x4_t pb = vpaddlq_u16(sum_TTL);
  368. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  369. const uint8x16_t sumTin = vaddq_u8(T, src); // in + T
  370. DO_PRED11(0);
  371. DO_PRED11(1);
  372. DO_PRED11(2);
  373. DO_PRED11(3);
  374. }
  375. VP8LPredictorsAdd_C[11](in + i, upper + i, num_pixels - i, out + i);
  376. }
  377. #undef DO_PRED11
  378. // Predictor12: ClampedAddSubtractFull.
  379. #define DO_PRED12(DIFF, LANE) do { \
  380. const uint8x8_t pred = \
  381. vqmovun_s16(vaddq_s16(vreinterpretq_s16_u16(L), (DIFF))); \
  382. const uint8x8_t res = \
  383. vadd_u8(pred, (LANE <= 1) ? vget_low_u8(src) : vget_high_u8(src)); \
  384. const uint16x8_t res16 = vmovl_u8(res); \
  385. vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \
  386. /* rotate in the left predictor for next iteration */ \
  387. L = vextq_u16(res16, res16, 4); \
  388. } while (0)
  389. static void PredictorAdd12_NEON(const uint32_t* in, const uint32_t* upper,
  390. int num_pixels, uint32_t* out) {
  391. int i;
  392. uint16x8_t L = vmovl_u8(LOAD_U32_AS_U8(out[-1]));
  393. for (i = 0; i + 4 <= num_pixels; i += 4) {
  394. // load four pixels of source
  395. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  396. // precompute the difference T - TL once for all, stored as s16
  397. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  398. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  399. const int16x8_t diff_lo =
  400. vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(T), vget_low_u8(TL)));
  401. const int16x8_t diff_hi =
  402. vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(T), vget_high_u8(TL)));
  403. // loop over the four reconstructed pixels
  404. DO_PRED12(diff_lo, 0);
  405. DO_PRED12(diff_lo, 1);
  406. DO_PRED12(diff_hi, 2);
  407. DO_PRED12(diff_hi, 3);
  408. }
  409. VP8LPredictorsAdd_C[12](in + i, upper + i, num_pixels - i, out + i);
  410. }
  411. #undef DO_PRED12
  412. // Predictor13: ClampedAddSubtractHalf
  413. #define DO_PRED13(LANE, LOW_OR_HI) do { \
  414. const uint8x16_t avg = vhaddq_u8(L, T); \
  415. const uint8x16_t cmp = vcgtq_u8(TL, avg); \
  416. const uint8x16_t TL_1 = vaddq_u8(TL, cmp); \
  417. /* Compute half of the difference between avg and TL'. */ \
  418. const int8x8_t diff_avg = \
  419. vreinterpret_s8_u8(LOW_OR_HI(vhsubq_u8(avg, TL_1))); \
  420. /* Compute the sum with avg and saturate. */ \
  421. const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(LOW_OR_HI(avg))); \
  422. const uint8x8_t delta = vqmovun_s16(vaddw_s8(avg_16, diff_avg)); \
  423. const uint8x8_t res = vadd_u8(LOW_OR_HI(src), delta); \
  424. const uint8x16_t res2 = vcombine_u8(res, res); \
  425. vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \
  426. L = ROTATE32_LEFT(res2); \
  427. } while (0)
  428. static void PredictorAdd13_NEON(const uint32_t* in, const uint32_t* upper,
  429. int num_pixels, uint32_t* out) {
  430. int i;
  431. uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
  432. for (i = 0; i + 4 <= num_pixels; i += 4) {
  433. const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
  434. const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
  435. const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
  436. DO_PRED13(0, vget_low_u8);
  437. DO_PRED13(1, vget_low_u8);
  438. DO_PRED13(2, vget_high_u8);
  439. DO_PRED13(3, vget_high_u8);
  440. }
  441. VP8LPredictorsAdd_C[13](in + i, upper + i, num_pixels - i, out + i);
  442. }
  443. #undef DO_PRED13
  444. #undef LOAD_U32_AS_U8
  445. #undef LOAD_U32P_AS_U8
  446. #undef LOADQ_U32_AS_U8
  447. #undef LOADQ_U32P_AS_U8
  448. #undef GET_U8_AS_U32
  449. #undef GETQ_U8_AS_U32
  450. #undef STOREQ_U8_AS_U32P
  451. #undef ROTATE32_LEFT
  452. //------------------------------------------------------------------------------
  453. // Subtract-Green Transform
  454. // vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use
  455. // non-standard versions there.
  456. #if defined(__APPLE__) && defined(__aarch64__) && \
  457. defined(__apple_build_version__) && (__apple_build_version__< 6020037)
  458. #define USE_VTBLQ
  459. #endif
  460. #ifdef USE_VTBLQ
  461. // 255 = byte will be zeroed
  462. static const uint8_t kGreenShuffle[16] = {
  463. 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255
  464. };
  465. static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,
  466. const uint8x16_t shuffle) {
  467. return vcombine_u8(vtbl1q_u8(argb, vget_low_u8(shuffle)),
  468. vtbl1q_u8(argb, vget_high_u8(shuffle)));
  469. }
  470. #else // !USE_VTBLQ
  471. // 255 = byte will be zeroed
  472. static const uint8_t kGreenShuffle[8] = { 1, 255, 1, 255, 5, 255, 5, 255 };
  473. static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,
  474. const uint8x8_t shuffle) {
  475. return vcombine_u8(vtbl1_u8(vget_low_u8(argb), shuffle),
  476. vtbl1_u8(vget_high_u8(argb), shuffle));
  477. }
  478. #endif // USE_VTBLQ
  479. static void AddGreenToBlueAndRed_NEON(const uint32_t* src, int num_pixels,
  480. uint32_t* dst) {
  481. const uint32_t* const end = src + (num_pixels & ~3);
  482. #ifdef USE_VTBLQ
  483. const uint8x16_t shuffle = vld1q_u8(kGreenShuffle);
  484. #else
  485. const uint8x8_t shuffle = vld1_u8(kGreenShuffle);
  486. #endif
  487. for (; src < end; src += 4, dst += 4) {
  488. const uint8x16_t argb = vld1q_u8((const uint8_t*)src);
  489. const uint8x16_t greens = DoGreenShuffle_NEON(argb, shuffle);
  490. vst1q_u8((uint8_t*)dst, vaddq_u8(argb, greens));
  491. }
  492. // fallthrough and finish off with plain-C
  493. VP8LAddGreenToBlueAndRed_C(src, num_pixels & 3, dst);
  494. }
  495. //------------------------------------------------------------------------------
  496. // Color Transform
  497. static void TransformColorInverse_NEON(const VP8LMultipliers* const m,
  498. const uint32_t* const src,
  499. int num_pixels, uint32_t* dst) {
  500. // sign-extended multiplying constants, pre-shifted by 6.
  501. #define CST(X) (((int16_t)(m->X << 8)) >> 6)
  502. const int16_t rb[8] = {
  503. CST(green_to_blue_), CST(green_to_red_),
  504. CST(green_to_blue_), CST(green_to_red_),
  505. CST(green_to_blue_), CST(green_to_red_),
  506. CST(green_to_blue_), CST(green_to_red_)
  507. };
  508. const int16x8_t mults_rb = vld1q_s16(rb);
  509. const int16_t b2[8] = {
  510. 0, CST(red_to_blue_), 0, CST(red_to_blue_),
  511. 0, CST(red_to_blue_), 0, CST(red_to_blue_),
  512. };
  513. const int16x8_t mults_b2 = vld1q_s16(b2);
  514. #undef CST
  515. #ifdef USE_VTBLQ
  516. static const uint8_t kg0g0[16] = {
  517. 255, 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13
  518. };
  519. const uint8x16_t shuffle = vld1q_u8(kg0g0);
  520. #else
  521. static const uint8_t k0g0g[8] = { 255, 1, 255, 1, 255, 5, 255, 5 };
  522. const uint8x8_t shuffle = vld1_u8(k0g0g);
  523. #endif
  524. const uint32x4_t mask_ag = vdupq_n_u32(0xff00ff00u);
  525. int i;
  526. for (i = 0; i + 4 <= num_pixels; i += 4) {
  527. const uint8x16_t in = vld1q_u8((const uint8_t*)(src + i));
  528. const uint32x4_t a0g0 = vandq_u32(vreinterpretq_u32_u8(in), mask_ag);
  529. // 0 g 0 g
  530. const uint8x16_t greens = DoGreenShuffle_NEON(in, shuffle);
  531. // x dr x db1
  532. const int16x8_t A = vqdmulhq_s16(vreinterpretq_s16_u8(greens), mults_rb);
  533. // x r' x b'
  534. const int8x16_t B = vaddq_s8(vreinterpretq_s8_u8(in),
  535. vreinterpretq_s8_s16(A));
  536. // r' 0 b' 0
  537. const int16x8_t C = vshlq_n_s16(vreinterpretq_s16_s8(B), 8);
  538. // x db2 0 0
  539. const int16x8_t D = vqdmulhq_s16(C, mults_b2);
  540. // 0 x db2 0
  541. const uint32x4_t E = vshrq_n_u32(vreinterpretq_u32_s16(D), 8);
  542. // r' x b'' 0
  543. const int8x16_t F = vaddq_s8(vreinterpretq_s8_u32(E),
  544. vreinterpretq_s8_s16(C));
  545. // 0 r' 0 b''
  546. const uint16x8_t G = vshrq_n_u16(vreinterpretq_u16_s8(F), 8);
  547. const uint32x4_t out = vorrq_u32(vreinterpretq_u32_u16(G), a0g0);
  548. vst1q_u32(dst + i, out);
  549. }
  550. // Fall-back to C-version for left-overs.
  551. VP8LTransformColorInverse_C(m, src + i, num_pixels - i, dst + i);
  552. }
  553. #undef USE_VTBLQ
  554. //------------------------------------------------------------------------------
  555. // Entry point
  556. extern void VP8LDspInitNEON(void);
  557. WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitNEON(void) {
  558. VP8LPredictors[5] = Predictor5_NEON;
  559. VP8LPredictors[6] = Predictor6_NEON;
  560. VP8LPredictors[7] = Predictor7_NEON;
  561. VP8LPredictors[13] = Predictor13_NEON;
  562. VP8LPredictorsAdd[0] = PredictorAdd0_NEON;
  563. VP8LPredictorsAdd[1] = PredictorAdd1_NEON;
  564. VP8LPredictorsAdd[2] = PredictorAdd2_NEON;
  565. VP8LPredictorsAdd[3] = PredictorAdd3_NEON;
  566. VP8LPredictorsAdd[4] = PredictorAdd4_NEON;
  567. VP8LPredictorsAdd[5] = PredictorAdd5_NEON;
  568. VP8LPredictorsAdd[6] = PredictorAdd6_NEON;
  569. VP8LPredictorsAdd[7] = PredictorAdd7_NEON;
  570. VP8LPredictorsAdd[8] = PredictorAdd8_NEON;
  571. VP8LPredictorsAdd[9] = PredictorAdd9_NEON;
  572. VP8LPredictorsAdd[10] = PredictorAdd10_NEON;
  573. VP8LPredictorsAdd[11] = PredictorAdd11_NEON;
  574. VP8LPredictorsAdd[12] = PredictorAdd12_NEON;
  575. VP8LPredictorsAdd[13] = PredictorAdd13_NEON;
  576. VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA_NEON;
  577. VP8LConvertBGRAToBGR = ConvertBGRAToBGR_NEON;
  578. VP8LConvertBGRAToRGB = ConvertBGRAToRGB_NEON;
  579. VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed_NEON;
  580. VP8LTransformColorInverse = TransformColorInverse_NEON;
  581. }
  582. #else // !WEBP_USE_NEON
  583. WEBP_DSP_INIT_STUB(VP8LDspInitNEON)
  584. #endif // WEBP_USE_NEON