lossless_enc_sse2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. // SSE2 variant of methods for lossless encoder
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE2)
  15. #include <assert.h>
  16. #include <emmintrin.h>
  17. #include "./lossless.h"
  18. #include "./common_sse2.h"
  19. #include "./lossless_common.h"
  20. // For sign-extended multiplying constants, pre-shifted by 5:
  21. #define CST_5b(X) (((int16_t)((uint16_t)(X) << 8)) >> 5)
  22. //------------------------------------------------------------------------------
  23. // Subtract-Green Transform
  24. static void SubtractGreenFromBlueAndRed_SSE2(uint32_t* argb_data,
  25. int num_pixels) {
  26. int i;
  27. for (i = 0; i + 4 <= num_pixels; i += 4) {
  28. const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb
  29. const __m128i A = _mm_srli_epi16(in, 8); // 0 a 0 g
  30. const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
  31. const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // 0g0g
  32. const __m128i out = _mm_sub_epi8(in, C);
  33. _mm_storeu_si128((__m128i*)&argb_data[i], out);
  34. }
  35. // fallthrough and finish off with plain-C
  36. if (i != num_pixels) {
  37. VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);
  38. }
  39. }
  40. //------------------------------------------------------------------------------
  41. // Color Transform
  42. #define MK_CST_16(HI, LO) \
  43. _mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff)))
  44. static void TransformColor_SSE2(const VP8LMultipliers* const m,
  45. uint32_t* argb_data, int num_pixels) {
  46. const __m128i mults_rb = MK_CST_16(CST_5b(m->green_to_red_),
  47. CST_5b(m->green_to_blue_));
  48. const __m128i mults_b2 = MK_CST_16(CST_5b(m->red_to_blue_), 0);
  49. const __m128i mask_ag = _mm_set1_epi32(0xff00ff00); // alpha-green masks
  50. const __m128i mask_rb = _mm_set1_epi32(0x00ff00ff); // red-blue masks
  51. int i;
  52. for (i = 0; i + 4 <= num_pixels; i += 4) {
  53. const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb
  54. const __m128i A = _mm_and_si128(in, mask_ag); // a 0 g 0
  55. const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
  56. const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // g0g0
  57. const __m128i D = _mm_mulhi_epi16(C, mults_rb); // x dr x db1
  58. const __m128i E = _mm_slli_epi16(in, 8); // r 0 b 0
  59. const __m128i F = _mm_mulhi_epi16(E, mults_b2); // x db2 0 0
  60. const __m128i G = _mm_srli_epi32(F, 16); // 0 0 x db2
  61. const __m128i H = _mm_add_epi8(G, D); // x dr x db
  62. const __m128i I = _mm_and_si128(H, mask_rb); // 0 dr 0 db
  63. const __m128i out = _mm_sub_epi8(in, I);
  64. _mm_storeu_si128((__m128i*)&argb_data[i], out);
  65. }
  66. // fallthrough and finish off with plain-C
  67. if (i != num_pixels) {
  68. VP8LTransformColor_C(m, argb_data + i, num_pixels - i);
  69. }
  70. }
  71. //------------------------------------------------------------------------------
  72. #define SPAN 8
  73. static void CollectColorBlueTransforms_SSE2(const uint32_t* argb, int stride,
  74. int tile_width, int tile_height,
  75. int green_to_blue, int red_to_blue,
  76. int histo[]) {
  77. const __m128i mults_r = MK_CST_16(CST_5b(red_to_blue), 0);
  78. const __m128i mults_g = MK_CST_16(0, CST_5b(green_to_blue));
  79. const __m128i mask_g = _mm_set1_epi32(0x00ff00); // green mask
  80. const __m128i mask_b = _mm_set1_epi32(0x0000ff); // blue mask
  81. int y;
  82. for (y = 0; y < tile_height; ++y) {
  83. const uint32_t* const src = argb + y * stride;
  84. int i, x;
  85. for (x = 0; x + SPAN <= tile_width; x += SPAN) {
  86. uint16_t values[SPAN];
  87. const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);
  88. const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
  89. const __m128i A0 = _mm_slli_epi16(in0, 8); // r 0 | b 0
  90. const __m128i A1 = _mm_slli_epi16(in1, 8);
  91. const __m128i B0 = _mm_and_si128(in0, mask_g); // 0 0 | g 0
  92. const __m128i B1 = _mm_and_si128(in1, mask_g);
  93. const __m128i C0 = _mm_mulhi_epi16(A0, mults_r); // x db | 0 0
  94. const __m128i C1 = _mm_mulhi_epi16(A1, mults_r);
  95. const __m128i D0 = _mm_mulhi_epi16(B0, mults_g); // 0 0 | x db
  96. const __m128i D1 = _mm_mulhi_epi16(B1, mults_g);
  97. const __m128i E0 = _mm_sub_epi8(in0, D0); // x x | x b'
  98. const __m128i E1 = _mm_sub_epi8(in1, D1);
  99. const __m128i F0 = _mm_srli_epi32(C0, 16); // 0 0 | x db
  100. const __m128i F1 = _mm_srli_epi32(C1, 16);
  101. const __m128i G0 = _mm_sub_epi8(E0, F0); // 0 0 | x b'
  102. const __m128i G1 = _mm_sub_epi8(E1, F1);
  103. const __m128i H0 = _mm_and_si128(G0, mask_b); // 0 0 | 0 b
  104. const __m128i H1 = _mm_and_si128(G1, mask_b);
  105. const __m128i I = _mm_packs_epi32(H0, H1); // 0 b' | 0 b'
  106. _mm_storeu_si128((__m128i*)values, I);
  107. for (i = 0; i < SPAN; ++i) ++histo[values[i]];
  108. }
  109. }
  110. {
  111. const int left_over = tile_width & (SPAN - 1);
  112. if (left_over > 0) {
  113. VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,
  114. left_over, tile_height,
  115. green_to_blue, red_to_blue, histo);
  116. }
  117. }
  118. }
  119. static void CollectColorRedTransforms_SSE2(const uint32_t* argb, int stride,
  120. int tile_width, int tile_height,
  121. int green_to_red, int histo[]) {
  122. const __m128i mults_g = MK_CST_16(0, CST_5b(green_to_red));
  123. const __m128i mask_g = _mm_set1_epi32(0x00ff00); // green mask
  124. const __m128i mask = _mm_set1_epi32(0xff);
  125. int y;
  126. for (y = 0; y < tile_height; ++y) {
  127. const uint32_t* const src = argb + y * stride;
  128. int i, x;
  129. for (x = 0; x + SPAN <= tile_width; x += SPAN) {
  130. uint16_t values[SPAN];
  131. const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);
  132. const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
  133. const __m128i A0 = _mm_and_si128(in0, mask_g); // 0 0 | g 0
  134. const __m128i A1 = _mm_and_si128(in1, mask_g);
  135. const __m128i B0 = _mm_srli_epi32(in0, 16); // 0 0 | x r
  136. const __m128i B1 = _mm_srli_epi32(in1, 16);
  137. const __m128i C0 = _mm_mulhi_epi16(A0, mults_g); // 0 0 | x dr
  138. const __m128i C1 = _mm_mulhi_epi16(A1, mults_g);
  139. const __m128i E0 = _mm_sub_epi8(B0, C0); // x x | x r'
  140. const __m128i E1 = _mm_sub_epi8(B1, C1);
  141. const __m128i F0 = _mm_and_si128(E0, mask); // 0 0 | 0 r'
  142. const __m128i F1 = _mm_and_si128(E1, mask);
  143. const __m128i I = _mm_packs_epi32(F0, F1);
  144. _mm_storeu_si128((__m128i*)values, I);
  145. for (i = 0; i < SPAN; ++i) ++histo[values[i]];
  146. }
  147. }
  148. {
  149. const int left_over = tile_width & (SPAN - 1);
  150. if (left_over > 0) {
  151. VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,
  152. left_over, tile_height,
  153. green_to_red, histo);
  154. }
  155. }
  156. }
  157. #undef SPAN
  158. #undef MK_CST_16
  159. //------------------------------------------------------------------------------
  160. // Note we are adding uint32_t's as *signed* int32's (using _mm_add_epi32). But
  161. // that's ok since the histogram values are less than 1<<28 (max picture size).
  162. #define LINE_SIZE 16 // 8 or 16
  163. static void AddVector_SSE2(const uint32_t* a, const uint32_t* b, uint32_t* out,
  164. int size) {
  165. int i;
  166. for (i = 0; i + LINE_SIZE <= size; i += LINE_SIZE) {
  167. const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i + 0]);
  168. const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]);
  169. #if (LINE_SIZE == 16)
  170. const __m128i a2 = _mm_loadu_si128((const __m128i*)&a[i + 8]);
  171. const __m128i a3 = _mm_loadu_si128((const __m128i*)&a[i + 12]);
  172. #endif
  173. const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[i + 0]);
  174. const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[i + 4]);
  175. #if (LINE_SIZE == 16)
  176. const __m128i b2 = _mm_loadu_si128((const __m128i*)&b[i + 8]);
  177. const __m128i b3 = _mm_loadu_si128((const __m128i*)&b[i + 12]);
  178. #endif
  179. _mm_storeu_si128((__m128i*)&out[i + 0], _mm_add_epi32(a0, b0));
  180. _mm_storeu_si128((__m128i*)&out[i + 4], _mm_add_epi32(a1, b1));
  181. #if (LINE_SIZE == 16)
  182. _mm_storeu_si128((__m128i*)&out[i + 8], _mm_add_epi32(a2, b2));
  183. _mm_storeu_si128((__m128i*)&out[i + 12], _mm_add_epi32(a3, b3));
  184. #endif
  185. }
  186. for (; i < size; ++i) {
  187. out[i] = a[i] + b[i];
  188. }
  189. }
  190. static void AddVectorEq_SSE2(const uint32_t* a, uint32_t* out, int size) {
  191. int i;
  192. for (i = 0; i + LINE_SIZE <= size; i += LINE_SIZE) {
  193. const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i + 0]);
  194. const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]);
  195. #if (LINE_SIZE == 16)
  196. const __m128i a2 = _mm_loadu_si128((const __m128i*)&a[i + 8]);
  197. const __m128i a3 = _mm_loadu_si128((const __m128i*)&a[i + 12]);
  198. #endif
  199. const __m128i b0 = _mm_loadu_si128((const __m128i*)&out[i + 0]);
  200. const __m128i b1 = _mm_loadu_si128((const __m128i*)&out[i + 4]);
  201. #if (LINE_SIZE == 16)
  202. const __m128i b2 = _mm_loadu_si128((const __m128i*)&out[i + 8]);
  203. const __m128i b3 = _mm_loadu_si128((const __m128i*)&out[i + 12]);
  204. #endif
  205. _mm_storeu_si128((__m128i*)&out[i + 0], _mm_add_epi32(a0, b0));
  206. _mm_storeu_si128((__m128i*)&out[i + 4], _mm_add_epi32(a1, b1));
  207. #if (LINE_SIZE == 16)
  208. _mm_storeu_si128((__m128i*)&out[i + 8], _mm_add_epi32(a2, b2));
  209. _mm_storeu_si128((__m128i*)&out[i + 12], _mm_add_epi32(a3, b3));
  210. #endif
  211. }
  212. for (; i < size; ++i) {
  213. out[i] += a[i];
  214. }
  215. }
  216. #undef LINE_SIZE
  217. //------------------------------------------------------------------------------
  218. // Entropy
  219. // TODO(https://crbug.com/webp/499): this function produces different results
  220. // from the C code due to use of double/float resulting in output differences
  221. // when compared to -noasm.
  222. #if !(defined(WEBP_HAVE_SLOW_CLZ_CTZ) || defined(__i386__) || defined(_M_IX86))
  223. static float CombinedShannonEntropy_SSE2(const int X[256], const int Y[256]) {
  224. int i;
  225. double retval = 0.;
  226. int sumX = 0, sumXY = 0;
  227. const __m128i zero = _mm_setzero_si128();
  228. for (i = 0; i < 256; i += 16) {
  229. const __m128i x0 = _mm_loadu_si128((const __m128i*)(X + i + 0));
  230. const __m128i y0 = _mm_loadu_si128((const __m128i*)(Y + i + 0));
  231. const __m128i x1 = _mm_loadu_si128((const __m128i*)(X + i + 4));
  232. const __m128i y1 = _mm_loadu_si128((const __m128i*)(Y + i + 4));
  233. const __m128i x2 = _mm_loadu_si128((const __m128i*)(X + i + 8));
  234. const __m128i y2 = _mm_loadu_si128((const __m128i*)(Y + i + 8));
  235. const __m128i x3 = _mm_loadu_si128((const __m128i*)(X + i + 12));
  236. const __m128i y3 = _mm_loadu_si128((const __m128i*)(Y + i + 12));
  237. const __m128i x4 = _mm_packs_epi16(_mm_packs_epi32(x0, x1),
  238. _mm_packs_epi32(x2, x3));
  239. const __m128i y4 = _mm_packs_epi16(_mm_packs_epi32(y0, y1),
  240. _mm_packs_epi32(y2, y3));
  241. const int32_t mx = _mm_movemask_epi8(_mm_cmpgt_epi8(x4, zero));
  242. int32_t my = _mm_movemask_epi8(_mm_cmpgt_epi8(y4, zero)) | mx;
  243. while (my) {
  244. const int32_t j = BitsCtz(my);
  245. int xy;
  246. if ((mx >> j) & 1) {
  247. const int x = X[i + j];
  248. sumXY += x;
  249. retval -= VP8LFastSLog2(x);
  250. }
  251. xy = X[i + j] + Y[i + j];
  252. sumX += xy;
  253. retval -= VP8LFastSLog2(xy);
  254. my &= my - 1;
  255. }
  256. }
  257. retval += VP8LFastSLog2(sumX) + VP8LFastSLog2(sumXY);
  258. return (float)retval;
  259. }
  260. #else
  261. #define DONT_USE_COMBINED_SHANNON_ENTROPY_SSE2_FUNC // won't be faster
  262. #endif
  263. //------------------------------------------------------------------------------
  264. static int VectorMismatch_SSE2(const uint32_t* const array1,
  265. const uint32_t* const array2, int length) {
  266. int match_len;
  267. if (length >= 12) {
  268. __m128i A0 = _mm_loadu_si128((const __m128i*)&array1[0]);
  269. __m128i A1 = _mm_loadu_si128((const __m128i*)&array2[0]);
  270. match_len = 0;
  271. do {
  272. // Loop unrolling and early load both provide a speedup of 10% for the
  273. // current function. Also, max_limit can be MAX_LENGTH=4096 at most.
  274. const __m128i cmpA = _mm_cmpeq_epi32(A0, A1);
  275. const __m128i B0 =
  276. _mm_loadu_si128((const __m128i*)&array1[match_len + 4]);
  277. const __m128i B1 =
  278. _mm_loadu_si128((const __m128i*)&array2[match_len + 4]);
  279. if (_mm_movemask_epi8(cmpA) != 0xffff) break;
  280. match_len += 4;
  281. {
  282. const __m128i cmpB = _mm_cmpeq_epi32(B0, B1);
  283. A0 = _mm_loadu_si128((const __m128i*)&array1[match_len + 4]);
  284. A1 = _mm_loadu_si128((const __m128i*)&array2[match_len + 4]);
  285. if (_mm_movemask_epi8(cmpB) != 0xffff) break;
  286. match_len += 4;
  287. }
  288. } while (match_len + 12 < length);
  289. } else {
  290. match_len = 0;
  291. // Unroll the potential first two loops.
  292. if (length >= 4 &&
  293. _mm_movemask_epi8(_mm_cmpeq_epi32(
  294. _mm_loadu_si128((const __m128i*)&array1[0]),
  295. _mm_loadu_si128((const __m128i*)&array2[0]))) == 0xffff) {
  296. match_len = 4;
  297. if (length >= 8 &&
  298. _mm_movemask_epi8(_mm_cmpeq_epi32(
  299. _mm_loadu_si128((const __m128i*)&array1[4]),
  300. _mm_loadu_si128((const __m128i*)&array2[4]))) == 0xffff) {
  301. match_len = 8;
  302. }
  303. }
  304. }
  305. while (match_len < length && array1[match_len] == array2[match_len]) {
  306. ++match_len;
  307. }
  308. return match_len;
  309. }
  310. // Bundles multiple (1, 2, 4 or 8) pixels into a single pixel.
  311. static void BundleColorMap_SSE2(const uint8_t* const row, int width, int xbits,
  312. uint32_t* dst) {
  313. int x;
  314. assert(xbits >= 0);
  315. assert(xbits <= 3);
  316. switch (xbits) {
  317. case 0: {
  318. const __m128i ff = _mm_set1_epi16((short)0xff00);
  319. const __m128i zero = _mm_setzero_si128();
  320. // Store 0xff000000 | (row[x] << 8).
  321. for (x = 0; x + 16 <= width; x += 16, dst += 16) {
  322. const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
  323. const __m128i in_lo = _mm_unpacklo_epi8(zero, in);
  324. const __m128i dst0 = _mm_unpacklo_epi16(in_lo, ff);
  325. const __m128i dst1 = _mm_unpackhi_epi16(in_lo, ff);
  326. const __m128i in_hi = _mm_unpackhi_epi8(zero, in);
  327. const __m128i dst2 = _mm_unpacklo_epi16(in_hi, ff);
  328. const __m128i dst3 = _mm_unpackhi_epi16(in_hi, ff);
  329. _mm_storeu_si128((__m128i*)&dst[0], dst0);
  330. _mm_storeu_si128((__m128i*)&dst[4], dst1);
  331. _mm_storeu_si128((__m128i*)&dst[8], dst2);
  332. _mm_storeu_si128((__m128i*)&dst[12], dst3);
  333. }
  334. break;
  335. }
  336. case 1: {
  337. const __m128i ff = _mm_set1_epi16((short)0xff00);
  338. const __m128i mul = _mm_set1_epi16(0x110);
  339. for (x = 0; x + 16 <= width; x += 16, dst += 8) {
  340. // 0a0b | (where a/b are 4 bits).
  341. const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
  342. const __m128i tmp = _mm_mullo_epi16(in, mul); // aba0
  343. const __m128i pack = _mm_and_si128(tmp, ff); // ab00
  344. const __m128i dst0 = _mm_unpacklo_epi16(pack, ff);
  345. const __m128i dst1 = _mm_unpackhi_epi16(pack, ff);
  346. _mm_storeu_si128((__m128i*)&dst[0], dst0);
  347. _mm_storeu_si128((__m128i*)&dst[4], dst1);
  348. }
  349. break;
  350. }
  351. case 2: {
  352. const __m128i mask_or = _mm_set1_epi32(0xff000000);
  353. const __m128i mul_cst = _mm_set1_epi16(0x0104);
  354. const __m128i mask_mul = _mm_set1_epi16(0x0f00);
  355. for (x = 0; x + 16 <= width; x += 16, dst += 4) {
  356. // 000a000b000c000d | (where a/b/c/d are 2 bits).
  357. const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
  358. const __m128i mul = _mm_mullo_epi16(in, mul_cst); // 00ab00b000cd00d0
  359. const __m128i tmp = _mm_and_si128(mul, mask_mul); // 00ab000000cd0000
  360. const __m128i shift = _mm_srli_epi32(tmp, 12); // 00000000ab000000
  361. const __m128i pack = _mm_or_si128(shift, tmp); // 00000000abcd0000
  362. // Convert to 0xff00**00.
  363. const __m128i res = _mm_or_si128(pack, mask_or);
  364. _mm_storeu_si128((__m128i*)dst, res);
  365. }
  366. break;
  367. }
  368. default: {
  369. assert(xbits == 3);
  370. for (x = 0; x + 16 <= width; x += 16, dst += 2) {
  371. // 0000000a00000000b... | (where a/b are 1 bit).
  372. const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]);
  373. const __m128i shift = _mm_slli_epi64(in, 7);
  374. const uint32_t move = _mm_movemask_epi8(shift);
  375. dst[0] = 0xff000000 | ((move & 0xff) << 8);
  376. dst[1] = 0xff000000 | (move & 0xff00);
  377. }
  378. break;
  379. }
  380. }
  381. if (x != width) {
  382. VP8LBundleColorMap_C(row + x, width - x, xbits, dst);
  383. }
  384. }
  385. //------------------------------------------------------------------------------
  386. // Batch version of Predictor Transform subtraction
  387. static WEBP_INLINE void Average2_m128i(const __m128i* const a0,
  388. const __m128i* const a1,
  389. __m128i* const avg) {
  390. // (a + b) >> 1 = ((a + b + 1) >> 1) - ((a ^ b) & 1)
  391. const __m128i ones = _mm_set1_epi8(1);
  392. const __m128i avg1 = _mm_avg_epu8(*a0, *a1);
  393. const __m128i one = _mm_and_si128(_mm_xor_si128(*a0, *a1), ones);
  394. *avg = _mm_sub_epi8(avg1, one);
  395. }
  396. // Predictor0: ARGB_BLACK.
  397. static void PredictorSub0_SSE2(const uint32_t* in, const uint32_t* upper,
  398. int num_pixels, uint32_t* out) {
  399. int i;
  400. const __m128i black = _mm_set1_epi32(ARGB_BLACK);
  401. for (i = 0; i + 4 <= num_pixels; i += 4) {
  402. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  403. const __m128i res = _mm_sub_epi8(src, black);
  404. _mm_storeu_si128((__m128i*)&out[i], res);
  405. }
  406. if (i != num_pixels) {
  407. VP8LPredictorsSub_C[0](in + i, NULL, num_pixels - i, out + i);
  408. }
  409. (void)upper;
  410. }
  411. #define GENERATE_PREDICTOR_1(X, IN) \
  412. static void PredictorSub##X##_SSE2(const uint32_t* const in, \
  413. const uint32_t* const upper, \
  414. int num_pixels, uint32_t* const out) { \
  415. int i; \
  416. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  417. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \
  418. const __m128i pred = _mm_loadu_si128((const __m128i*)&(IN)); \
  419. const __m128i res = _mm_sub_epi8(src, pred); \
  420. _mm_storeu_si128((__m128i*)&out[i], res); \
  421. } \
  422. if (i != num_pixels) { \
  423. VP8LPredictorsSub_C[(X)](in + i, WEBP_OFFSET_PTR(upper, i), \
  424. num_pixels - i, out + i); \
  425. } \
  426. }
  427. GENERATE_PREDICTOR_1(1, in[i - 1]) // Predictor1: L
  428. GENERATE_PREDICTOR_1(2, upper[i]) // Predictor2: T
  429. GENERATE_PREDICTOR_1(3, upper[i + 1]) // Predictor3: TR
  430. GENERATE_PREDICTOR_1(4, upper[i - 1]) // Predictor4: TL
  431. #undef GENERATE_PREDICTOR_1
  432. // Predictor5: avg2(avg2(L, TR), T)
  433. static void PredictorSub5_SSE2(const uint32_t* in, const uint32_t* upper,
  434. int num_pixels, uint32_t* out) {
  435. int i;
  436. for (i = 0; i + 4 <= num_pixels; i += 4) {
  437. const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
  438. const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
  439. const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]);
  440. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  441. __m128i avg, pred, res;
  442. Average2_m128i(&L, &TR, &avg);
  443. Average2_m128i(&avg, &T, &pred);
  444. res = _mm_sub_epi8(src, pred);
  445. _mm_storeu_si128((__m128i*)&out[i], res);
  446. }
  447. if (i != num_pixels) {
  448. VP8LPredictorsSub_C[5](in + i, upper + i, num_pixels - i, out + i);
  449. }
  450. }
  451. #define GENERATE_PREDICTOR_2(X, A, B) \
  452. static void PredictorSub##X##_SSE2(const uint32_t* in, const uint32_t* upper, \
  453. int num_pixels, uint32_t* out) { \
  454. int i; \
  455. for (i = 0; i + 4 <= num_pixels; i += 4) { \
  456. const __m128i tA = _mm_loadu_si128((const __m128i*)&(A)); \
  457. const __m128i tB = _mm_loadu_si128((const __m128i*)&(B)); \
  458. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \
  459. __m128i pred, res; \
  460. Average2_m128i(&tA, &tB, &pred); \
  461. res = _mm_sub_epi8(src, pred); \
  462. _mm_storeu_si128((__m128i*)&out[i], res); \
  463. } \
  464. if (i != num_pixels) { \
  465. VP8LPredictorsSub_C[(X)](in + i, upper + i, num_pixels - i, out + i); \
  466. } \
  467. }
  468. GENERATE_PREDICTOR_2(6, in[i - 1], upper[i - 1]) // Predictor6: avg(L, TL)
  469. GENERATE_PREDICTOR_2(7, in[i - 1], upper[i]) // Predictor7: avg(L, T)
  470. GENERATE_PREDICTOR_2(8, upper[i - 1], upper[i]) // Predictor8: avg(TL, T)
  471. GENERATE_PREDICTOR_2(9, upper[i], upper[i + 1]) // Predictor9: average(T, TR)
  472. #undef GENERATE_PREDICTOR_2
  473. // Predictor10: avg(avg(L,TL), avg(T, TR)).
  474. static void PredictorSub10_SSE2(const uint32_t* in, const uint32_t* upper,
  475. int num_pixels, uint32_t* out) {
  476. int i;
  477. for (i = 0; i + 4 <= num_pixels; i += 4) {
  478. const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
  479. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  480. const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
  481. const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
  482. const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]);
  483. __m128i avgTTR, avgLTL, avg, res;
  484. Average2_m128i(&T, &TR, &avgTTR);
  485. Average2_m128i(&L, &TL, &avgLTL);
  486. Average2_m128i(&avgTTR, &avgLTL, &avg);
  487. res = _mm_sub_epi8(src, avg);
  488. _mm_storeu_si128((__m128i*)&out[i], res);
  489. }
  490. if (i != num_pixels) {
  491. VP8LPredictorsSub_C[10](in + i, upper + i, num_pixels - i, out + i);
  492. }
  493. }
  494. // Predictor11: select.
  495. static void GetSumAbsDiff32_SSE2(const __m128i* const A, const __m128i* const B,
  496. __m128i* const out) {
  497. // We can unpack with any value on the upper 32 bits, provided it's the same
  498. // on both operands (to that their sum of abs diff is zero). Here we use *A.
  499. const __m128i A_lo = _mm_unpacklo_epi32(*A, *A);
  500. const __m128i B_lo = _mm_unpacklo_epi32(*B, *A);
  501. const __m128i A_hi = _mm_unpackhi_epi32(*A, *A);
  502. const __m128i B_hi = _mm_unpackhi_epi32(*B, *A);
  503. const __m128i s_lo = _mm_sad_epu8(A_lo, B_lo);
  504. const __m128i s_hi = _mm_sad_epu8(A_hi, B_hi);
  505. *out = _mm_packs_epi32(s_lo, s_hi);
  506. }
  507. static void PredictorSub11_SSE2(const uint32_t* in, const uint32_t* upper,
  508. int num_pixels, uint32_t* out) {
  509. int i;
  510. for (i = 0; i + 4 <= num_pixels; i += 4) {
  511. const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
  512. const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
  513. const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
  514. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  515. __m128i pa, pb;
  516. GetSumAbsDiff32_SSE2(&T, &TL, &pa); // pa = sum |T-TL|
  517. GetSumAbsDiff32_SSE2(&L, &TL, &pb); // pb = sum |L-TL|
  518. {
  519. const __m128i mask = _mm_cmpgt_epi32(pb, pa);
  520. const __m128i A = _mm_and_si128(mask, L);
  521. const __m128i B = _mm_andnot_si128(mask, T);
  522. const __m128i pred = _mm_or_si128(A, B); // pred = (L > T)? L : T
  523. const __m128i res = _mm_sub_epi8(src, pred);
  524. _mm_storeu_si128((__m128i*)&out[i], res);
  525. }
  526. }
  527. if (i != num_pixels) {
  528. VP8LPredictorsSub_C[11](in + i, upper + i, num_pixels - i, out + i);
  529. }
  530. }
  531. // Predictor12: ClampedSubSubtractFull.
  532. static void PredictorSub12_SSE2(const uint32_t* in, const uint32_t* upper,
  533. int num_pixels, uint32_t* out) {
  534. int i;
  535. const __m128i zero = _mm_setzero_si128();
  536. for (i = 0; i + 4 <= num_pixels; i += 4) {
  537. const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]);
  538. const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]);
  539. const __m128i L_lo = _mm_unpacklo_epi8(L, zero);
  540. const __m128i L_hi = _mm_unpackhi_epi8(L, zero);
  541. const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]);
  542. const __m128i T_lo = _mm_unpacklo_epi8(T, zero);
  543. const __m128i T_hi = _mm_unpackhi_epi8(T, zero);
  544. const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]);
  545. const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero);
  546. const __m128i TL_hi = _mm_unpackhi_epi8(TL, zero);
  547. const __m128i diff_lo = _mm_sub_epi16(T_lo, TL_lo);
  548. const __m128i diff_hi = _mm_sub_epi16(T_hi, TL_hi);
  549. const __m128i pred_lo = _mm_add_epi16(L_lo, diff_lo);
  550. const __m128i pred_hi = _mm_add_epi16(L_hi, diff_hi);
  551. const __m128i pred = _mm_packus_epi16(pred_lo, pred_hi);
  552. const __m128i res = _mm_sub_epi8(src, pred);
  553. _mm_storeu_si128((__m128i*)&out[i], res);
  554. }
  555. if (i != num_pixels) {
  556. VP8LPredictorsSub_C[12](in + i, upper + i, num_pixels - i, out + i);
  557. }
  558. }
  559. // Predictors13: ClampedAddSubtractHalf
  560. static void PredictorSub13_SSE2(const uint32_t* in, const uint32_t* upper,
  561. int num_pixels, uint32_t* out) {
  562. int i;
  563. const __m128i zero = _mm_setzero_si128();
  564. for (i = 0; i + 2 <= num_pixels; i += 2) {
  565. // we can only process two pixels at a time
  566. const __m128i L = _mm_loadl_epi64((const __m128i*)&in[i - 1]);
  567. const __m128i src = _mm_loadl_epi64((const __m128i*)&in[i]);
  568. const __m128i T = _mm_loadl_epi64((const __m128i*)&upper[i]);
  569. const __m128i TL = _mm_loadl_epi64((const __m128i*)&upper[i - 1]);
  570. const __m128i L_lo = _mm_unpacklo_epi8(L, zero);
  571. const __m128i T_lo = _mm_unpacklo_epi8(T, zero);
  572. const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero);
  573. const __m128i sum = _mm_add_epi16(T_lo, L_lo);
  574. const __m128i avg = _mm_srli_epi16(sum, 1);
  575. const __m128i A1 = _mm_sub_epi16(avg, TL_lo);
  576. const __m128i bit_fix = _mm_cmpgt_epi16(TL_lo, avg);
  577. const __m128i A2 = _mm_sub_epi16(A1, bit_fix);
  578. const __m128i A3 = _mm_srai_epi16(A2, 1);
  579. const __m128i A4 = _mm_add_epi16(avg, A3);
  580. const __m128i pred = _mm_packus_epi16(A4, A4);
  581. const __m128i res = _mm_sub_epi8(src, pred);
  582. _mm_storel_epi64((__m128i*)&out[i], res);
  583. }
  584. if (i != num_pixels) {
  585. VP8LPredictorsSub_C[13](in + i, upper + i, num_pixels - i, out + i);
  586. }
  587. }
  588. //------------------------------------------------------------------------------
  589. // Entry point
  590. extern void VP8LEncDspInitSSE2(void);
  591. WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE2(void) {
  592. VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE2;
  593. VP8LTransformColor = TransformColor_SSE2;
  594. VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE2;
  595. VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE2;
  596. VP8LAddVector = AddVector_SSE2;
  597. VP8LAddVectorEq = AddVectorEq_SSE2;
  598. #if !defined(DONT_USE_COMBINED_SHANNON_ENTROPY_SSE2_FUNC)
  599. VP8LCombinedShannonEntropy = CombinedShannonEntropy_SSE2;
  600. #endif
  601. VP8LVectorMismatch = VectorMismatch_SSE2;
  602. VP8LBundleColorMap = BundleColorMap_SSE2;
  603. VP8LPredictorsSub[0] = PredictorSub0_SSE2;
  604. VP8LPredictorsSub[1] = PredictorSub1_SSE2;
  605. VP8LPredictorsSub[2] = PredictorSub2_SSE2;
  606. VP8LPredictorsSub[3] = PredictorSub3_SSE2;
  607. VP8LPredictorsSub[4] = PredictorSub4_SSE2;
  608. VP8LPredictorsSub[5] = PredictorSub5_SSE2;
  609. VP8LPredictorsSub[6] = PredictorSub6_SSE2;
  610. VP8LPredictorsSub[7] = PredictorSub7_SSE2;
  611. VP8LPredictorsSub[8] = PredictorSub8_SSE2;
  612. VP8LPredictorsSub[9] = PredictorSub9_SSE2;
  613. VP8LPredictorsSub[10] = PredictorSub10_SSE2;
  614. VP8LPredictorsSub[11] = PredictorSub11_SSE2;
  615. VP8LPredictorsSub[12] = PredictorSub12_SSE2;
  616. VP8LPredictorsSub[13] = PredictorSub13_SSE2;
  617. VP8LPredictorsSub[14] = PredictorSub0_SSE2; // <- padding security sentinels
  618. VP8LPredictorsSub[15] = PredictorSub0_SSE2;
  619. }
  620. #else // !WEBP_USE_SSE2
  621. WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE2)
  622. #endif // WEBP_USE_SSE2