lossless_sse2.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. // SSE2 variant of methods for lossless decoder
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE2)
  15. #include "./common_sse2.h"
  16. #include "./lossless.h"
  17. #include "./lossless_common.h"
  18. #include <emmintrin.h>
  19. //------------------------------------------------------------------------------
  20. // Predictor Transform
  21. static WEBP_INLINE uint32_t ClampedAddSubtractFull_SSE2(uint32_t c0,
  22. uint32_t c1,
  23. uint32_t c2) {
  24. const __m128i zero = _mm_setzero_si128();
  25. const __m128i C0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c0), zero);
  26. const __m128i C1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c1), zero);
  27. const __m128i C2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c2), zero);
  28. const __m128i V1 = _mm_add_epi16(C0, C1);
  29. const __m128i V2 = _mm_sub_epi16(V1, C2);
  30. const __m128i b = _mm_packus_epi16(V2, V2);
  31. const uint32_t output = _mm_cvtsi128_si32(b);
  32. return output;
  33. }
  34. static WEBP_INLINE uint32_t ClampedAddSubtractHalf_SSE2(uint32_t c0,
  35. uint32_t c1,
  36. uint32_t c2) {
  37. const __m128i zero = _mm_setzero_si128();
  38. const __m128i C0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c0), zero);
  39. const __m128i C1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c1), zero);
  40. const __m128i B0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c2), zero);
  41. const __m128i avg = _mm_add_epi16(C1, C0);
  42. const __m128i A0 = _mm_srli_epi16(avg, 1);
  43. const __m128i A1 = _mm_sub_epi16(A0, B0);
  44. const __m128i BgtA = _mm_cmpgt_epi16(B0, A0);
  45. const __m128i A2 = _mm_sub_epi16(A1, BgtA);
  46. const __m128i A3 = _mm_srai_epi16(A2, 1);
  47. const __m128i A4 = _mm_add_epi16(A0, A3);
  48. const __m128i A5 = _mm_packus_epi16(A4, A4);
  49. const uint32_t output = _mm_cvtsi128_si32(A5);
  50. return output;
  51. }
  52. static WEBP_INLINE uint32_t Select_SSE2(uint32_t a, uint32_t b, uint32_t c) {
  53. int pa_minus_pb;
  54. const __m128i zero = _mm_setzero_si128();
  55. const __m128i A0 = _mm_cvtsi32_si128(a);
  56. const __m128i B0 = _mm_cvtsi32_si128(b);
  57. const __m128i C0 = _mm_cvtsi32_si128(c);
  58. const __m128i AC0 = _mm_subs_epu8(A0, C0);
  59. const __m128i CA0 = _mm_subs_epu8(C0, A0);
  60. const __m128i BC0 = _mm_subs_epu8(B0, C0);
  61. const __m128i CB0 = _mm_subs_epu8(C0, B0);
  62. const __m128i AC = _mm_or_si128(AC0, CA0);
  63. const __m128i BC = _mm_or_si128(BC0, CB0);
  64. const __m128i pa = _mm_unpacklo_epi8(AC, zero); // |a - c|
  65. const __m128i pb = _mm_unpacklo_epi8(BC, zero); // |b - c|
  66. const __m128i diff = _mm_sub_epi16(pb, pa);
  67. {
  68. int16_t out[8];
  69. _mm_storeu_si128((__m128i*)out, diff);
  70. pa_minus_pb = out[0] + out[1] + out[2] + out[3];
  71. }
  72. return (pa_minus_pb <= 0) ? a : b;
  73. }
  74. static WEBP_INLINE void Average2_m128i(const __m128i* const a0,
  75. const __m128i* const a1,
  76. __m128i* const avg) {
  77. // (a + b) >> 1 = ((a + b + 1) >> 1) - ((a ^ b) & 1)
  78. const __m128i ones = _mm_set1_epi8(1);
  79. const __m128i avg1 = _mm_avg_epu8(*a0, *a1);
  80. const __m128i one = _mm_and_si128(_mm_xor_si128(*a0, *a1), ones);
  81. *avg = _mm_sub_epi8(avg1, one);
  82. }
  83. static WEBP_INLINE void Average2_uint32_SSE2(const uint32_t a0,
  84. const uint32_t a1,
  85. __m128i* const avg) {
  86. // (a + b) >> 1 = ((a + b + 1) >> 1) - ((a ^ b) & 1)
  87. const __m128i ones = _mm_set1_epi8(1);
  88. const __m128i A0 = _mm_cvtsi32_si128(a0);
  89. const __m128i A1 = _mm_cvtsi32_si128(a1);
  90. const __m128i avg1 = _mm_avg_epu8(A0, A1);
  91. const __m128i one = _mm_and_si128(_mm_xor_si128(A0, A1), ones);
  92. *avg = _mm_sub_epi8(avg1, one);
  93. }
  94. static WEBP_INLINE __m128i Average2_uint32_16_SSE2(uint32_t a0, uint32_t a1) {
  95. const __m128i zero = _mm_setzero_si128();
  96. const __m128i A0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(a0), zero);
  97. const __m128i A1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(a1), zero);
  98. const __m128i sum = _mm_add_epi16(A1, A0);
  99. return _mm_srli_epi16(sum, 1);
  100. }
  101. static WEBP_INLINE uint32_t Average2_SSE2(uint32_t a0, uint32_t a1) {
  102. __m128i output;
  103. Average2_uint32_SSE2(a0, a1, &output);
  104. return _mm_cvtsi128_si32(output);
  105. }
  106. static WEBP_INLINE uint32_t Average3_SSE2(uint32_t a0, uint32_t a1,
  107. uint32_t a2) {
  108. const __m128i zero = _mm_setzero_si128();
  109. const __m128i avg1 = Average2_uint32_16_SSE2(a0, a2);
  110. const __m128i A1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(a1), zero);
  111. const __m128i sum = _mm_add_epi16(avg1, A1);
  112. const __m128i avg2 = _mm_srli_epi16(sum, 1);
  113. const __m128i A2 = _mm_packus_epi16(avg2, avg2);
  114. const uint32_t output = _mm_cvtsi128_si32(A2);
  115. return output;
  116. }
  117. static WEBP_INLINE uint32_t Average4_SSE2(uint32_t a0, uint32_t a1,
  118. uint32_t a2, uint32_t a3) {
  119. const __m128i avg1 = Average2_uint32_16_SSE2(a0, a1);
  120. const __m128i avg2 = Average2_uint32_16_SSE2(a2, a3);
  121. const __m128i sum = _mm_add_epi16(avg2, avg1);
  122. const __m128i avg3 = _mm_srli_epi16(sum, 1);
  123. const __m128i A0 = _mm_packus_epi16(avg3, avg3);
  124. const uint32_t output = _mm_cvtsi128_si32(A0);
  125. return output;
  126. }
  127. static uint32_t Predictor5_SSE2(const uint32_t* const left,
  128. const uint32_t* const top) {
  129. const uint32_t pred = Average3_SSE2(*left, top[0], top[1]);
  130. return pred;
  131. }
  132. static uint32_t Predictor6_SSE2(const uint32_t* const left,
  133. const uint32_t* const top) {
  134. const uint32_t pred = Average2_SSE2(*left, top[-1]);
  135. return pred;
  136. }
  137. static uint32_t Predictor7_SSE2(const uint32_t* const left,
  138. const uint32_t* const top) {
  139. const uint32_t pred = Average2_SSE2(*left, top[0]);
  140. return pred;
  141. }
  142. static uint32_t Predictor8_SSE2(const uint32_t* const left,
  143. const uint32_t* const top) {
  144. const uint32_t pred = Average2_SSE2(top[-1], top[0]);
  145. (void)left;
  146. return pred;
  147. }
  148. static uint32_t Predictor9_SSE2(const uint32_t* const left,
  149. const uint32_t* const top) {
  150. const uint32_t pred = Average2_SSE2(top[0], top[1]);
  151. (void)left;
  152. return pred;
  153. }
  154. static uint32_t Predictor10_SSE2(const uint32_t* const left,
  155. const uint32_t* const top) {
  156. const uint32_t pred = Average4_SSE2(*left, top[-1], top[0], top[1]);
  157. return pred;
  158. }
  159. static uint32_t Predictor11_SSE2(const uint32_t* const left,
  160. const uint32_t* const top) {
  161. const uint32_t pred = Select_SSE2(top[0], *left, top[-1]);
  162. return pred;
  163. }
  164. static uint32_t Predictor12_SSE2(const uint32_t* const left,
  165. const uint32_t* const top) {
  166. const uint32_t pred = ClampedAddSubtractFull_SSE2(*left, top[0], top[-1]);
  167. return pred;
  168. }
  169. static uint32_t Predictor13_SSE2(const uint32_t* const left,
  170. const uint32_t* const top) {
  171. const uint32_t pred = ClampedAddSubtractHalf_SSE2(*left, top[0], top[-1]);
  172. return pred;
  173. }
  174. // Batch versions of those functions.
  175. // Predictor0: ARGB_BLACK.
  176. static void PredictorAdd0_SSE2(const uint32_t* in, const uint32_t* upper,
  177. int num_pixels, uint32_t* out) {
  178. int i;
  179. const __m128i black = _mm_set1_epi32(ARGB_BLACK);
  180. for (i = 0; i + 4 <= num_pixels; i += 4) {
  181. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  182. const __m128i res = _mm_add_epi8(src, black);
  183. _mm_storeu_si128((__m128i*)&out[i], res);
  184. }
  185. if (i != num_pixels) {
  186. VP8LPredictorsAdd_C[0](in + i, NULL, num_pixels - i, out + i);
  187. }
  188. (void)upper;
  189. }
  190. // Predictor1: left.
  191. static void PredictorAdd1_SSE2(const uint32_t* in, const uint32_t* upper,
  192. int num_pixels, uint32_t* out) {
  193. int i;
  194. __m128i prev = _mm_set1_epi32(out[-1]);
  195. for (i = 0; i + 4 <= num_pixels; i += 4) {
  196. // a | b | c | d
  197. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  198. // 0 | a | b | c
  199. const __m128i shift0 = _mm_slli_si128(src, 4);
  200. // a | a + b | b + c | c + d
  201. const __m128i sum0 = _mm_add_epi8(src, shift0);
  202. // 0 | 0 | a | a + b
  203. const __m128i shift1 = _mm_slli_si128(sum0, 8);
  204. // a | a + b | a + b + c | a + b + c + d
  205. const __m128i sum1 = _mm_add_epi8(sum0, shift1);
  206. const __m128i res = _mm_add_epi8(sum1, prev);
  207. _mm_storeu_si128((__m128i*)&out[i], res);
  208. // replicate prev output on the four lanes
  209. prev = _mm_shuffle_epi32(res, (3 << 0) | (3 << 2) | (3 << 4) | (3 << 6));
  210. }
  211. if (i != num_pixels) {
  212. VP8LPredictorsAdd_C[1](in + i, upper + i, num_pixels - i, out + i);
  213. }
  214. }
  215. // Macro that adds 32-bit integers from IN using mod 256 arithmetic
  216. // per 8 bit channel.
  217. #define GENERATE_PREDICTOR_1(X, IN) \
  218. static void PredictorAdd##X##_SSE2(const uint32_t* in, const uint32_t* upper, \
  219. int num_pixels, uint32_t* out) { \
  220. int i; \
  221. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  222. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \
  223. const __m128i other = _mm_loadu_si128((const __m128i*)&(IN)); \
  224. const __m128i res = _mm_add_epi8(src, other); \
  225. _mm_storeu_si128((__m128i*)&out[i], res); \
  226. } \
  227. if (i != num_pixels) { \
  228. VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \
  229. } \
  230. }
  231. // Predictor2: Top.
  232. GENERATE_PREDICTOR_1(2, upper[i])
  233. // Predictor3: Top-right.
  234. GENERATE_PREDICTOR_1(3, upper[i + 1])
  235. // Predictor4: Top-left.
  236. GENERATE_PREDICTOR_1(4, upper[i - 1])
  237. #undef GENERATE_PREDICTOR_1
  238. // Due to averages with integers, values cannot be accumulated in parallel for
  239. // predictors 5 to 7.
  240. GENERATE_PREDICTOR_ADD(Predictor5_SSE2, PredictorAdd5_SSE2)
  241. GENERATE_PREDICTOR_ADD(Predictor6_SSE2, PredictorAdd6_SSE2)
  242. GENERATE_PREDICTOR_ADD(Predictor7_SSE2, PredictorAdd7_SSE2)
  243. #define GENERATE_PREDICTOR_2(X, IN) \
  244. static void PredictorAdd##X##_SSE2(const uint32_t* in, const uint32_t* upper, \
  245. int num_pixels, uint32_t* out) { \
  246. int i; \
  247. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  248. const __m128i Tother = _mm_loadu_si128((const __m128i*)&(IN)); \
  249. const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); \
  250. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \
  251. __m128i avg, res; \
  252. Average2_m128i(&T, &Tother, &avg); \
  253. res = _mm_add_epi8(avg, src); \
  254. _mm_storeu_si128((__m128i*)&out[i], res); \
  255. } \
  256. if (i != num_pixels) { \
  257. VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \
  258. } \
  259. }
  260. // Predictor8: average TL T.
  261. GENERATE_PREDICTOR_2(8, upper[i - 1])
  262. // Predictor9: average T TR.
  263. GENERATE_PREDICTOR_2(9, upper[i + 1])
  264. #undef GENERATE_PREDICTOR_2
  265. // Predictor10: average of (average of (L,TL), average of (T, TR)).
  266. #define DO_PRED10(OUT) do { \
  267. __m128i avgLTL, avg; \
  268. Average2_m128i(&L, &TL, &avgLTL); \
  269. Average2_m128i(&avgTTR, &avgLTL, &avg); \
  270. L = _mm_add_epi8(avg, src); \
  271. out[i + (OUT)] = _mm_cvtsi128_si32(L); \
  272. } while (0)
  273. #define DO_PRED10_SHIFT do { \
  274. /* Rotate the pre-computed values for the next iteration.*/ \
  275. avgTTR = _mm_srli_si128(avgTTR, 4); \
  276. TL = _mm_srli_si128(TL, 4); \
  277. src = _mm_srli_si128(src, 4); \
  278. } while (0)
  279. static void PredictorAdd10_SSE2(const uint32_t* in, const uint32_t* upper,
  280. int num_pixels, uint32_t* out) {
  281. int i;
  282. __m128i L = _mm_cvtsi32_si128(out[-1]);
  283. for (i = 0; i + 4 <= num_pixels; i += 4) {
  284. __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  285. __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
  286. const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
  287. const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]);
  288. __m128i avgTTR;
  289. Average2_m128i(&T, &TR, &avgTTR);
  290. DO_PRED10(0);
  291. DO_PRED10_SHIFT;
  292. DO_PRED10(1);
  293. DO_PRED10_SHIFT;
  294. DO_PRED10(2);
  295. DO_PRED10_SHIFT;
  296. DO_PRED10(3);
  297. }
  298. if (i != num_pixels) {
  299. VP8LPredictorsAdd_C[10](in + i, upper + i, num_pixels - i, out + i);
  300. }
  301. }
  302. #undef DO_PRED10
  303. #undef DO_PRED10_SHIFT
  304. // Predictor11: select.
  305. #define DO_PRED11(OUT) do { \
  306. const __m128i L_lo = _mm_unpacklo_epi32(L, T); \
  307. const __m128i TL_lo = _mm_unpacklo_epi32(TL, T); \
  308. const __m128i pb = _mm_sad_epu8(L_lo, TL_lo); /* pb = sum |L-TL|*/ \
  309. const __m128i mask = _mm_cmpgt_epi32(pb, pa); \
  310. const __m128i A = _mm_and_si128(mask, L); \
  311. const __m128i B = _mm_andnot_si128(mask, T); \
  312. const __m128i pred = _mm_or_si128(A, B); /* pred = (pa > b)? L : T*/ \
  313. L = _mm_add_epi8(src, pred); \
  314. out[i + (OUT)] = _mm_cvtsi128_si32(L); \
  315. } while (0)
  316. #define DO_PRED11_SHIFT do { \
  317. /* Shift the pre-computed value for the next iteration.*/ \
  318. T = _mm_srli_si128(T, 4); \
  319. TL = _mm_srli_si128(TL, 4); \
  320. src = _mm_srli_si128(src, 4); \
  321. pa = _mm_srli_si128(pa, 4); \
  322. } while (0)
  323. static void PredictorAdd11_SSE2(const uint32_t* in, const uint32_t* upper,
  324. int num_pixels, uint32_t* out) {
  325. int i;
  326. __m128i pa;
  327. __m128i L = _mm_cvtsi32_si128(out[-1]);
  328. for (i = 0; i + 4 <= num_pixels; i += 4) {
  329. __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
  330. __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
  331. __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  332. {
  333. // We can unpack with any value on the upper 32 bits, provided it's the
  334. // same on both operands (so that their sum of abs diff is zero). Here we
  335. // use T.
  336. const __m128i T_lo = _mm_unpacklo_epi32(T, T);
  337. const __m128i TL_lo = _mm_unpacklo_epi32(TL, T);
  338. const __m128i T_hi = _mm_unpackhi_epi32(T, T);
  339. const __m128i TL_hi = _mm_unpackhi_epi32(TL, T);
  340. const __m128i s_lo = _mm_sad_epu8(T_lo, TL_lo);
  341. const __m128i s_hi = _mm_sad_epu8(T_hi, TL_hi);
  342. pa = _mm_packs_epi32(s_lo, s_hi); // pa = sum |T-TL|
  343. }
  344. DO_PRED11(0);
  345. DO_PRED11_SHIFT;
  346. DO_PRED11(1);
  347. DO_PRED11_SHIFT;
  348. DO_PRED11(2);
  349. DO_PRED11_SHIFT;
  350. DO_PRED11(3);
  351. }
  352. if (i != num_pixels) {
  353. VP8LPredictorsAdd_C[11](in + i, upper + i, num_pixels - i, out + i);
  354. }
  355. }
  356. #undef DO_PRED11
  357. #undef DO_PRED11_SHIFT
  358. // Predictor12: ClampedAddSubtractFull.
  359. #define DO_PRED12(DIFF, LANE, OUT) do { \
  360. const __m128i all = _mm_add_epi16(L, (DIFF)); \
  361. const __m128i alls = _mm_packus_epi16(all, all); \
  362. const __m128i res = _mm_add_epi8(src, alls); \
  363. out[i + (OUT)] = _mm_cvtsi128_si32(res); \
  364. L = _mm_unpacklo_epi8(res, zero); \
  365. } while (0)
  366. #define DO_PRED12_SHIFT(DIFF, LANE) do { \
  367. /* Shift the pre-computed value for the next iteration.*/ \
  368. if ((LANE) == 0) (DIFF) = _mm_srli_si128((DIFF), 8); \
  369. src = _mm_srli_si128(src, 4); \
  370. } while (0)
  371. static void PredictorAdd12_SSE2(const uint32_t* in, const uint32_t* upper,
  372. int num_pixels, uint32_t* out) {
  373. int i;
  374. const __m128i zero = _mm_setzero_si128();
  375. const __m128i L8 = _mm_cvtsi32_si128(out[-1]);
  376. __m128i L = _mm_unpacklo_epi8(L8, zero);
  377. for (i = 0; i + 4 <= num_pixels; i += 4) {
  378. // Load 4 pixels at a time.
  379. __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  380. const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
  381. const __m128i T_lo = _mm_unpacklo_epi8(T, zero);
  382. const __m128i T_hi = _mm_unpackhi_epi8(T, zero);
  383. const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
  384. const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero);
  385. const __m128i TL_hi = _mm_unpackhi_epi8(TL, zero);
  386. __m128i diff_lo = _mm_sub_epi16(T_lo, TL_lo);
  387. __m128i diff_hi = _mm_sub_epi16(T_hi, TL_hi);
  388. DO_PRED12(diff_lo, 0, 0);
  389. DO_PRED12_SHIFT(diff_lo, 0);
  390. DO_PRED12(diff_lo, 1, 1);
  391. DO_PRED12_SHIFT(diff_lo, 1);
  392. DO_PRED12(diff_hi, 0, 2);
  393. DO_PRED12_SHIFT(diff_hi, 0);
  394. DO_PRED12(diff_hi, 1, 3);
  395. }
  396. if (i != num_pixels) {
  397. VP8LPredictorsAdd_C[12](in + i, upper + i, num_pixels - i, out + i);
  398. }
  399. }
  400. #undef DO_PRED12
  401. #undef DO_PRED12_SHIFT
  402. // Due to averages with integers, values cannot be accumulated in parallel for
  403. // predictors 13.
  404. GENERATE_PREDICTOR_ADD(Predictor13_SSE2, PredictorAdd13_SSE2)
  405. //------------------------------------------------------------------------------
  406. // Subtract-Green Transform
  407. static void AddGreenToBlueAndRed_SSE2(const uint32_t* const src, int num_pixels,
  408. uint32_t* dst) {
  409. int i;
  410. for (i = 0; i + 4 <= num_pixels; i += 4) {
  411. const __m128i in = _mm_loadu_si128((const __m128i*)&src[i]); // argb
  412. const __m128i A = _mm_srli_epi16(in, 8); // 0 a 0 g
  413. const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
  414. const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // 0g0g
  415. const __m128i out = _mm_add_epi8(in, C);
  416. _mm_storeu_si128((__m128i*)&dst[i], out);
  417. }
  418. // fallthrough and finish off with plain-C
  419. if (i != num_pixels) {
  420. VP8LAddGreenToBlueAndRed_C(src + i, num_pixels - i, dst + i);
  421. }
  422. }
  423. //------------------------------------------------------------------------------
  424. // Color Transform
  425. static void TransformColorInverse_SSE2(const VP8LMultipliers* const m,
  426. const uint32_t* const src,
  427. int num_pixels, uint32_t* dst) {
  428. // sign-extended multiplying constants, pre-shifted by 5.
  429. #define CST(X) (((int16_t)(m->X << 8)) >> 5) // sign-extend
  430. #define MK_CST_16(HI, LO) \
  431. _mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff)))
  432. const __m128i mults_rb = MK_CST_16(CST(green_to_red_), CST(green_to_blue_));
  433. const __m128i mults_b2 = MK_CST_16(CST(red_to_blue_), 0);
  434. #undef MK_CST_16
  435. #undef CST
  436. const __m128i mask_ag = _mm_set1_epi32(0xff00ff00); // alpha-green masks
  437. int i;
  438. for (i = 0; i + 4 <= num_pixels; i += 4) {
  439. const __m128i in = _mm_loadu_si128((const __m128i*)&src[i]); // argb
  440. const __m128i A = _mm_and_si128(in, mask_ag); // a 0 g 0
  441. const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
  442. const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // g0g0
  443. const __m128i D = _mm_mulhi_epi16(C, mults_rb); // x dr x db1
  444. const __m128i E = _mm_add_epi8(in, D); // x r' x b'
  445. const __m128i F = _mm_slli_epi16(E, 8); // r' 0 b' 0
  446. const __m128i G = _mm_mulhi_epi16(F, mults_b2); // x db2 0 0
  447. const __m128i H = _mm_srli_epi32(G, 8); // 0 x db2 0
  448. const __m128i I = _mm_add_epi8(H, F); // r' x b'' 0
  449. const __m128i J = _mm_srli_epi16(I, 8); // 0 r' 0 b''
  450. const __m128i out = _mm_or_si128(J, A);
  451. _mm_storeu_si128((__m128i*)&dst[i], out);
  452. }
  453. // Fall-back to C-version for left-overs.
  454. if (i != num_pixels) {
  455. VP8LTransformColorInverse_C(m, src + i, num_pixels - i, dst + i);
  456. }
  457. }
  458. //------------------------------------------------------------------------------
  459. // Color-space conversion functions
  460. static void ConvertBGRAToRGB_SSE2(const uint32_t* src, int num_pixels,
  461. uint8_t* dst) {
  462. const __m128i* in = (const __m128i*)src;
  463. __m128i* out = (__m128i*)dst;
  464. while (num_pixels >= 32) {
  465. // Load the BGRA buffers.
  466. __m128i in0 = _mm_loadu_si128(in + 0);
  467. __m128i in1 = _mm_loadu_si128(in + 1);
  468. __m128i in2 = _mm_loadu_si128(in + 2);
  469. __m128i in3 = _mm_loadu_si128(in + 3);
  470. __m128i in4 = _mm_loadu_si128(in + 4);
  471. __m128i in5 = _mm_loadu_si128(in + 5);
  472. __m128i in6 = _mm_loadu_si128(in + 6);
  473. __m128i in7 = _mm_loadu_si128(in + 7);
  474. VP8L32bToPlanar_SSE2(&in0, &in1, &in2, &in3);
  475. VP8L32bToPlanar_SSE2(&in4, &in5, &in6, &in7);
  476. // At this points, in1/in5 contains red only, in2/in6 green only ...
  477. // Pack the colors in 24b RGB.
  478. VP8PlanarTo24b_SSE2(&in1, &in5, &in2, &in6, &in3, &in7);
  479. _mm_storeu_si128(out + 0, in1);
  480. _mm_storeu_si128(out + 1, in5);
  481. _mm_storeu_si128(out + 2, in2);
  482. _mm_storeu_si128(out + 3, in6);
  483. _mm_storeu_si128(out + 4, in3);
  484. _mm_storeu_si128(out + 5, in7);
  485. in += 8;
  486. out += 6;
  487. num_pixels -= 32;
  488. }
  489. // left-overs
  490. if (num_pixels > 0) {
  491. VP8LConvertBGRAToRGB_C((const uint32_t*)in, num_pixels, (uint8_t*)out);
  492. }
  493. }
  494. static void ConvertBGRAToRGBA_SSE2(const uint32_t* src,
  495. int num_pixels, uint8_t* dst) {
  496. const __m128i red_blue_mask = _mm_set1_epi32(0x00ff00ffu);
  497. const __m128i* in = (const __m128i*)src;
  498. __m128i* out = (__m128i*)dst;
  499. while (num_pixels >= 8) {
  500. const __m128i A1 = _mm_loadu_si128(in++);
  501. const __m128i A2 = _mm_loadu_si128(in++);
  502. const __m128i B1 = _mm_and_si128(A1, red_blue_mask); // R 0 B 0
  503. const __m128i B2 = _mm_and_si128(A2, red_blue_mask); // R 0 B 0
  504. const __m128i C1 = _mm_andnot_si128(red_blue_mask, A1); // 0 G 0 A
  505. const __m128i C2 = _mm_andnot_si128(red_blue_mask, A2); // 0 G 0 A
  506. const __m128i D1 = _mm_shufflelo_epi16(B1, _MM_SHUFFLE(2, 3, 0, 1));
  507. const __m128i D2 = _mm_shufflelo_epi16(B2, _MM_SHUFFLE(2, 3, 0, 1));
  508. const __m128i E1 = _mm_shufflehi_epi16(D1, _MM_SHUFFLE(2, 3, 0, 1));
  509. const __m128i E2 = _mm_shufflehi_epi16(D2, _MM_SHUFFLE(2, 3, 0, 1));
  510. const __m128i F1 = _mm_or_si128(E1, C1);
  511. const __m128i F2 = _mm_or_si128(E2, C2);
  512. _mm_storeu_si128(out++, F1);
  513. _mm_storeu_si128(out++, F2);
  514. num_pixels -= 8;
  515. }
  516. // left-overs
  517. if (num_pixels > 0) {
  518. VP8LConvertBGRAToRGBA_C((const uint32_t*)in, num_pixels, (uint8_t*)out);
  519. }
  520. }
  521. static void ConvertBGRAToRGBA4444_SSE2(const uint32_t* src,
  522. int num_pixels, uint8_t* dst) {
  523. const __m128i mask_0x0f = _mm_set1_epi8(0x0f);
  524. const __m128i mask_0xf0 = _mm_set1_epi8(0xf0);
  525. const __m128i* in = (const __m128i*)src;
  526. __m128i* out = (__m128i*)dst;
  527. while (num_pixels >= 8) {
  528. const __m128i bgra0 = _mm_loadu_si128(in++); // bgra0|bgra1|bgra2|bgra3
  529. const __m128i bgra4 = _mm_loadu_si128(in++); // bgra4|bgra5|bgra6|bgra7
  530. const __m128i v0l = _mm_unpacklo_epi8(bgra0, bgra4); // b0b4g0g4r0r4a0a4...
  531. const __m128i v0h = _mm_unpackhi_epi8(bgra0, bgra4); // b2b6g2g6r2r6a2a6...
  532. const __m128i v1l = _mm_unpacklo_epi8(v0l, v0h); // b0b2b4b6g0g2g4g6...
  533. const __m128i v1h = _mm_unpackhi_epi8(v0l, v0h); // b1b3b5b7g1g3g5g7...
  534. const __m128i v2l = _mm_unpacklo_epi8(v1l, v1h); // b0...b7 | g0...g7
  535. const __m128i v2h = _mm_unpackhi_epi8(v1l, v1h); // r0...r7 | a0...a7
  536. const __m128i ga0 = _mm_unpackhi_epi64(v2l, v2h); // g0...g7 | a0...a7
  537. const __m128i rb0 = _mm_unpacklo_epi64(v2h, v2l); // r0...r7 | b0...b7
  538. const __m128i ga1 = _mm_srli_epi16(ga0, 4); // g0-|g1-|...|a6-|a7-
  539. const __m128i rb1 = _mm_and_si128(rb0, mask_0xf0); // -r0|-r1|...|-b6|-a7
  540. const __m128i ga2 = _mm_and_si128(ga1, mask_0x0f); // g0-|g1-|...|a6-|a7-
  541. const __m128i rgba0 = _mm_or_si128(ga2, rb1); // rg0..rg7 | ba0..ba7
  542. const __m128i rgba1 = _mm_srli_si128(rgba0, 8); // ba0..ba7 | 0
  543. #if (WEBP_SWAP_16BIT_CSP == 1)
  544. const __m128i rgba = _mm_unpacklo_epi8(rgba1, rgba0); // barg0...barg7
  545. #else
  546. const __m128i rgba = _mm_unpacklo_epi8(rgba0, rgba1); // rgba0...rgba7
  547. #endif
  548. _mm_storeu_si128(out++, rgba);
  549. num_pixels -= 8;
  550. }
  551. // left-overs
  552. if (num_pixels > 0) {
  553. VP8LConvertBGRAToRGBA4444_C((const uint32_t*)in, num_pixels, (uint8_t*)out);
  554. }
  555. }
  556. static void ConvertBGRAToRGB565_SSE2(const uint32_t* src,
  557. int num_pixels, uint8_t* dst) {
  558. const __m128i mask_0xe0 = _mm_set1_epi8(0xe0);
  559. const __m128i mask_0xf8 = _mm_set1_epi8(0xf8);
  560. const __m128i mask_0x07 = _mm_set1_epi8(0x07);
  561. const __m128i* in = (const __m128i*)src;
  562. __m128i* out = (__m128i*)dst;
  563. while (num_pixels >= 8) {
  564. const __m128i bgra0 = _mm_loadu_si128(in++); // bgra0|bgra1|bgra2|bgra3
  565. const __m128i bgra4 = _mm_loadu_si128(in++); // bgra4|bgra5|bgra6|bgra7
  566. const __m128i v0l = _mm_unpacklo_epi8(bgra0, bgra4); // b0b4g0g4r0r4a0a4...
  567. const __m128i v0h = _mm_unpackhi_epi8(bgra0, bgra4); // b2b6g2g6r2r6a2a6...
  568. const __m128i v1l = _mm_unpacklo_epi8(v0l, v0h); // b0b2b4b6g0g2g4g6...
  569. const __m128i v1h = _mm_unpackhi_epi8(v0l, v0h); // b1b3b5b7g1g3g5g7...
  570. const __m128i v2l = _mm_unpacklo_epi8(v1l, v1h); // b0...b7 | g0...g7
  571. const __m128i v2h = _mm_unpackhi_epi8(v1l, v1h); // r0...r7 | a0...a7
  572. const __m128i ga0 = _mm_unpackhi_epi64(v2l, v2h); // g0...g7 | a0...a7
  573. const __m128i rb0 = _mm_unpacklo_epi64(v2h, v2l); // r0...r7 | b0...b7
  574. const __m128i rb1 = _mm_and_si128(rb0, mask_0xf8); // -r0..-r7|-b0..-b7
  575. const __m128i g_lo1 = _mm_srli_epi16(ga0, 5);
  576. const __m128i g_lo2 = _mm_and_si128(g_lo1, mask_0x07); // g0-...g7-|xx (3b)
  577. const __m128i g_hi1 = _mm_slli_epi16(ga0, 3);
  578. const __m128i g_hi2 = _mm_and_si128(g_hi1, mask_0xe0); // -g0...-g7|xx (3b)
  579. const __m128i b0 = _mm_srli_si128(rb1, 8); // -b0...-b7|0
  580. const __m128i rg1 = _mm_or_si128(rb1, g_lo2); // gr0...gr7|xx
  581. const __m128i b1 = _mm_srli_epi16(b0, 3);
  582. const __m128i gb1 = _mm_or_si128(b1, g_hi2); // bg0...bg7|xx
  583. #if (WEBP_SWAP_16BIT_CSP == 1)
  584. const __m128i rgba = _mm_unpacklo_epi8(gb1, rg1); // rggb0...rggb7
  585. #else
  586. const __m128i rgba = _mm_unpacklo_epi8(rg1, gb1); // bgrb0...bgrb7
  587. #endif
  588. _mm_storeu_si128(out++, rgba);
  589. num_pixels -= 8;
  590. }
  591. // left-overs
  592. if (num_pixels > 0) {
  593. VP8LConvertBGRAToRGB565_C((const uint32_t*)in, num_pixels, (uint8_t*)out);
  594. }
  595. }
  596. static void ConvertBGRAToBGR_SSE2(const uint32_t* src,
  597. int num_pixels, uint8_t* dst) {
  598. const __m128i mask_l = _mm_set_epi32(0, 0x00ffffff, 0, 0x00ffffff);
  599. const __m128i mask_h = _mm_set_epi32(0x00ffffff, 0, 0x00ffffff, 0);
  600. const __m128i* in = (const __m128i*)src;
  601. const uint8_t* const end = dst + num_pixels * 3;
  602. // the last storel_epi64 below writes 8 bytes starting at offset 18
  603. while (dst + 26 <= end) {
  604. const __m128i bgra0 = _mm_loadu_si128(in++); // bgra0|bgra1|bgra2|bgra3
  605. const __m128i bgra4 = _mm_loadu_si128(in++); // bgra4|bgra5|bgra6|bgra7
  606. const __m128i a0l = _mm_and_si128(bgra0, mask_l); // bgr0|0|bgr0|0
  607. const __m128i a4l = _mm_and_si128(bgra4, mask_l); // bgr0|0|bgr0|0
  608. const __m128i a0h = _mm_and_si128(bgra0, mask_h); // 0|bgr0|0|bgr0
  609. const __m128i a4h = _mm_and_si128(bgra4, mask_h); // 0|bgr0|0|bgr0
  610. const __m128i b0h = _mm_srli_epi64(a0h, 8); // 000b|gr00|000b|gr00
  611. const __m128i b4h = _mm_srli_epi64(a4h, 8); // 000b|gr00|000b|gr00
  612. const __m128i c0 = _mm_or_si128(a0l, b0h); // rgbrgb00|rgbrgb00
  613. const __m128i c4 = _mm_or_si128(a4l, b4h); // rgbrgb00|rgbrgb00
  614. const __m128i c2 = _mm_srli_si128(c0, 8);
  615. const __m128i c6 = _mm_srli_si128(c4, 8);
  616. _mm_storel_epi64((__m128i*)(dst + 0), c0);
  617. _mm_storel_epi64((__m128i*)(dst + 6), c2);
  618. _mm_storel_epi64((__m128i*)(dst + 12), c4);
  619. _mm_storel_epi64((__m128i*)(dst + 18), c6);
  620. dst += 24;
  621. num_pixels -= 8;
  622. }
  623. // left-overs
  624. if (num_pixels > 0) {
  625. VP8LConvertBGRAToBGR_C((const uint32_t*)in, num_pixels, dst);
  626. }
  627. }
  628. //------------------------------------------------------------------------------
  629. // Entry point
  630. extern void VP8LDspInitSSE2(void);
  631. WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitSSE2(void) {
  632. VP8LPredictors[5] = Predictor5_SSE2;
  633. VP8LPredictors[6] = Predictor6_SSE2;
  634. VP8LPredictors[7] = Predictor7_SSE2;
  635. VP8LPredictors[8] = Predictor8_SSE2;
  636. VP8LPredictors[9] = Predictor9_SSE2;
  637. VP8LPredictors[10] = Predictor10_SSE2;
  638. VP8LPredictors[11] = Predictor11_SSE2;
  639. VP8LPredictors[12] = Predictor12_SSE2;
  640. VP8LPredictors[13] = Predictor13_SSE2;
  641. VP8LPredictorsAdd[0] = PredictorAdd0_SSE2;
  642. VP8LPredictorsAdd[1] = PredictorAdd1_SSE2;
  643. VP8LPredictorsAdd[2] = PredictorAdd2_SSE2;
  644. VP8LPredictorsAdd[3] = PredictorAdd3_SSE2;
  645. VP8LPredictorsAdd[4] = PredictorAdd4_SSE2;
  646. VP8LPredictorsAdd[5] = PredictorAdd5_SSE2;
  647. VP8LPredictorsAdd[6] = PredictorAdd6_SSE2;
  648. VP8LPredictorsAdd[7] = PredictorAdd7_SSE2;
  649. VP8LPredictorsAdd[8] = PredictorAdd8_SSE2;
  650. VP8LPredictorsAdd[9] = PredictorAdd9_SSE2;
  651. VP8LPredictorsAdd[10] = PredictorAdd10_SSE2;
  652. VP8LPredictorsAdd[11] = PredictorAdd11_SSE2;
  653. VP8LPredictorsAdd[12] = PredictorAdd12_SSE2;
  654. VP8LPredictorsAdd[13] = PredictorAdd13_SSE2;
  655. VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed_SSE2;
  656. VP8LTransformColorInverse = TransformColorInverse_SSE2;
  657. VP8LConvertBGRAToRGB = ConvertBGRAToRGB_SSE2;
  658. VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA_SSE2;
  659. VP8LConvertBGRAToRGBA4444 = ConvertBGRAToRGBA4444_SSE2;
  660. VP8LConvertBGRAToRGB565 = ConvertBGRAToRGB565_SSE2;
  661. VP8LConvertBGRAToBGR = ConvertBGRAToBGR_SSE2;
  662. }
  663. #else // !WEBP_USE_SSE2
  664. WEBP_DSP_INIT_STUB(VP8LDspInitSSE2)
  665. #endif // WEBP_USE_SSE2