yuv_sse41.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. // YUV->RGB conversion functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./yuv.h"
  14. #if defined(WEBP_USE_SSE41)
  15. #include "./common_sse41.h"
  16. #include <stdlib.h>
  17. #include <smmintrin.h>
  18. //-----------------------------------------------------------------------------
  19. // Convert spans of 32 pixels to various RGB formats for the fancy upsampler.
  20. // These constants are 14b fixed-point version of ITU-R BT.601 constants.
  21. // R = (19077 * y + 26149 * v - 14234) >> 6
  22. // G = (19077 * y - 6419 * u - 13320 * v + 8708) >> 6
  23. // B = (19077 * y + 33050 * u - 17685) >> 6
  24. static void ConvertYUV444ToRGB_SSE41(const __m128i* const Y0,
  25. const __m128i* const U0,
  26. const __m128i* const V0,
  27. __m128i* const R,
  28. __m128i* const G,
  29. __m128i* const B) {
  30. const __m128i k19077 = _mm_set1_epi16(19077);
  31. const __m128i k26149 = _mm_set1_epi16(26149);
  32. const __m128i k14234 = _mm_set1_epi16(14234);
  33. // 33050 doesn't fit in a signed short: only use this with unsigned arithmetic
  34. const __m128i k33050 = _mm_set1_epi16((short)33050);
  35. const __m128i k17685 = _mm_set1_epi16(17685);
  36. const __m128i k6419 = _mm_set1_epi16(6419);
  37. const __m128i k13320 = _mm_set1_epi16(13320);
  38. const __m128i k8708 = _mm_set1_epi16(8708);
  39. const __m128i Y1 = _mm_mulhi_epu16(*Y0, k19077);
  40. const __m128i R0 = _mm_mulhi_epu16(*V0, k26149);
  41. const __m128i R1 = _mm_sub_epi16(Y1, k14234);
  42. const __m128i R2 = _mm_add_epi16(R1, R0);
  43. const __m128i G0 = _mm_mulhi_epu16(*U0, k6419);
  44. const __m128i G1 = _mm_mulhi_epu16(*V0, k13320);
  45. const __m128i G2 = _mm_add_epi16(Y1, k8708);
  46. const __m128i G3 = _mm_add_epi16(G0, G1);
  47. const __m128i G4 = _mm_sub_epi16(G2, G3);
  48. // be careful with the saturated *unsigned* arithmetic here!
  49. const __m128i B0 = _mm_mulhi_epu16(*U0, k33050);
  50. const __m128i B1 = _mm_adds_epu16(B0, Y1);
  51. const __m128i B2 = _mm_subs_epu16(B1, k17685);
  52. // use logical shift for B2, which can be larger than 32767
  53. *R = _mm_srai_epi16(R2, 6); // range: [-14234, 30815]
  54. *G = _mm_srai_epi16(G4, 6); // range: [-10953, 27710]
  55. *B = _mm_srli_epi16(B2, 6); // range: [0, 34238]
  56. }
  57. // Load the bytes into the *upper* part of 16b words. That's "<< 8", basically.
  58. static WEBP_INLINE __m128i Load_HI_16_SSE41(const uint8_t* src) {
  59. const __m128i zero = _mm_setzero_si128();
  60. return _mm_unpacklo_epi8(zero, _mm_loadl_epi64((const __m128i*)src));
  61. }
  62. // Load and replicate the U/V samples
  63. static WEBP_INLINE __m128i Load_UV_HI_8_SSE41(const uint8_t* src) {
  64. const __m128i zero = _mm_setzero_si128();
  65. const __m128i tmp0 = _mm_cvtsi32_si128(*(const uint32_t*)src);
  66. const __m128i tmp1 = _mm_unpacklo_epi8(zero, tmp0);
  67. return _mm_unpacklo_epi16(tmp1, tmp1); // replicate samples
  68. }
  69. // Convert 32 samples of YUV444 to R/G/B
  70. static void YUV444ToRGB_SSE41(const uint8_t* const y,
  71. const uint8_t* const u,
  72. const uint8_t* const v,
  73. __m128i* const R, __m128i* const G,
  74. __m128i* const B) {
  75. const __m128i Y0 = Load_HI_16_SSE41(y), U0 = Load_HI_16_SSE41(u),
  76. V0 = Load_HI_16_SSE41(v);
  77. ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B);
  78. }
  79. // Convert 32 samples of YUV420 to R/G/B
  80. static void YUV420ToRGB_SSE41(const uint8_t* const y,
  81. const uint8_t* const u,
  82. const uint8_t* const v,
  83. __m128i* const R, __m128i* const G,
  84. __m128i* const B) {
  85. const __m128i Y0 = Load_HI_16_SSE41(y), U0 = Load_UV_HI_8_SSE41(u),
  86. V0 = Load_UV_HI_8_SSE41(v);
  87. ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B);
  88. }
  89. // Pack the planar buffers
  90. // rrrr... rrrr... gggg... gggg... bbbb... bbbb....
  91. // triplet by triplet in the output buffer rgb as rgbrgbrgbrgb ...
  92. static WEBP_INLINE void PlanarTo24b_SSE41(
  93. __m128i* const in0, __m128i* const in1, __m128i* const in2,
  94. __m128i* const in3, __m128i* const in4, __m128i* const in5,
  95. uint8_t* const rgb) {
  96. // The input is 6 registers of sixteen 8b but for the sake of explanation,
  97. // let's take 6 registers of four 8b values.
  98. // To pack, we will keep taking one every two 8b integer and move it
  99. // around as follows:
  100. // Input:
  101. // r0r1r2r3 | r4r5r6r7 | g0g1g2g3 | g4g5g6g7 | b0b1b2b3 | b4b5b6b7
  102. // Split the 6 registers in two sets of 3 registers: the first set as the even
  103. // 8b bytes, the second the odd ones:
  104. // r0r2r4r6 | g0g2g4g6 | b0b2b4b6 | r1r3r5r7 | g1g3g5g7 | b1b3b5b7
  105. // Repeat the same permutations twice more:
  106. // r0r4g0g4 | b0b4r1r5 | g1g5b1b5 | r2r6g2g6 | b2b6r3r7 | g3g7b3b7
  107. // r0g0b0r1 | g1b1r2g2 | b2r3g3b3 | r4g4b4r5 | g5b5r6g6 | b6r7g7b7
  108. VP8PlanarTo24b_SSE41(in0, in1, in2, in3, in4, in5);
  109. _mm_storeu_si128((__m128i*)(rgb + 0), *in0);
  110. _mm_storeu_si128((__m128i*)(rgb + 16), *in1);
  111. _mm_storeu_si128((__m128i*)(rgb + 32), *in2);
  112. _mm_storeu_si128((__m128i*)(rgb + 48), *in3);
  113. _mm_storeu_si128((__m128i*)(rgb + 64), *in4);
  114. _mm_storeu_si128((__m128i*)(rgb + 80), *in5);
  115. }
  116. void VP8YuvToRgb32_SSE41(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  117. uint8_t* dst) {
  118. __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
  119. __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
  120. YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
  121. YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1);
  122. YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2);
  123. YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3);
  124. // Cast to 8b and store as RRRRGGGGBBBB.
  125. rgb0 = _mm_packus_epi16(R0, R1);
  126. rgb1 = _mm_packus_epi16(R2, R3);
  127. rgb2 = _mm_packus_epi16(G0, G1);
  128. rgb3 = _mm_packus_epi16(G2, G3);
  129. rgb4 = _mm_packus_epi16(B0, B1);
  130. rgb5 = _mm_packus_epi16(B2, B3);
  131. // Pack as RGBRGBRGBRGB.
  132. PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
  133. }
  134. void VP8YuvToBgr32_SSE41(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  135. uint8_t* dst) {
  136. __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
  137. __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
  138. YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
  139. YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1);
  140. YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2);
  141. YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3);
  142. // Cast to 8b and store as BBBBGGGGRRRR.
  143. bgr0 = _mm_packus_epi16(B0, B1);
  144. bgr1 = _mm_packus_epi16(B2, B3);
  145. bgr2 = _mm_packus_epi16(G0, G1);
  146. bgr3 = _mm_packus_epi16(G2, G3);
  147. bgr4 = _mm_packus_epi16(R0, R1);
  148. bgr5= _mm_packus_epi16(R2, R3);
  149. // Pack as BGRBGRBGRBGR.
  150. PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Arbitrary-length row conversion functions
  154. static void YuvToRgbRow_SSE41(const uint8_t* y,
  155. const uint8_t* u, const uint8_t* v,
  156. uint8_t* dst, int len) {
  157. int n;
  158. for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
  159. __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
  160. __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
  161. YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
  162. YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1);
  163. YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2);
  164. YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3);
  165. // Cast to 8b and store as RRRRGGGGBBBB.
  166. rgb0 = _mm_packus_epi16(R0, R1);
  167. rgb1 = _mm_packus_epi16(R2, R3);
  168. rgb2 = _mm_packus_epi16(G0, G1);
  169. rgb3 = _mm_packus_epi16(G2, G3);
  170. rgb4 = _mm_packus_epi16(B0, B1);
  171. rgb5 = _mm_packus_epi16(B2, B3);
  172. // Pack as RGBRGBRGBRGB.
  173. PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
  174. y += 32;
  175. u += 16;
  176. v += 16;
  177. }
  178. for (; n < len; ++n) { // Finish off
  179. VP8YuvToRgb(y[0], u[0], v[0], dst);
  180. dst += 3;
  181. y += 1;
  182. u += (n & 1);
  183. v += (n & 1);
  184. }
  185. }
  186. static void YuvToBgrRow_SSE41(const uint8_t* y,
  187. const uint8_t* u, const uint8_t* v,
  188. uint8_t* dst, int len) {
  189. int n;
  190. for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
  191. __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
  192. __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
  193. YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
  194. YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1);
  195. YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2);
  196. YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3);
  197. // Cast to 8b and store as BBBBGGGGRRRR.
  198. bgr0 = _mm_packus_epi16(B0, B1);
  199. bgr1 = _mm_packus_epi16(B2, B3);
  200. bgr2 = _mm_packus_epi16(G0, G1);
  201. bgr3 = _mm_packus_epi16(G2, G3);
  202. bgr4 = _mm_packus_epi16(R0, R1);
  203. bgr5 = _mm_packus_epi16(R2, R3);
  204. // Pack as BGRBGRBGRBGR.
  205. PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
  206. y += 32;
  207. u += 16;
  208. v += 16;
  209. }
  210. for (; n < len; ++n) { // Finish off
  211. VP8YuvToBgr(y[0], u[0], v[0], dst);
  212. dst += 3;
  213. y += 1;
  214. u += (n & 1);
  215. v += (n & 1);
  216. }
  217. }
  218. //------------------------------------------------------------------------------
  219. // Entry point
  220. extern void WebPInitSamplersSSE41(void);
  221. WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplersSSE41(void) {
  222. WebPSamplers[MODE_RGB] = YuvToRgbRow_SSE41;
  223. WebPSamplers[MODE_BGR] = YuvToBgrRow_SSE41;
  224. }
  225. //------------------------------------------------------------------------------
  226. // RGB24/32 -> YUV converters
  227. // Load eight 16b-words from *src.
  228. #define LOAD_16(src) _mm_loadu_si128((const __m128i*)(src))
  229. // Store either 16b-words into *dst
  230. #define STORE_16(V, dst) _mm_storeu_si128((__m128i*)(dst), (V))
  231. #define WEBP_SSE41_SHUFF(OUT) do { \
  232. const __m128i tmp0 = _mm_shuffle_epi8(A0, shuff0); \
  233. const __m128i tmp1 = _mm_shuffle_epi8(A1, shuff1); \
  234. const __m128i tmp2 = _mm_shuffle_epi8(A2, shuff2); \
  235. const __m128i tmp3 = _mm_shuffle_epi8(A3, shuff0); \
  236. const __m128i tmp4 = _mm_shuffle_epi8(A4, shuff1); \
  237. const __m128i tmp5 = _mm_shuffle_epi8(A5, shuff2); \
  238. \
  239. /* OR everything to get one channel */ \
  240. const __m128i tmp6 = _mm_or_si128(tmp0, tmp1); \
  241. const __m128i tmp7 = _mm_or_si128(tmp3, tmp4); \
  242. out[OUT + 0] = _mm_or_si128(tmp6, tmp2); \
  243. out[OUT + 1] = _mm_or_si128(tmp7, tmp5); \
  244. } while (0);
  245. // Unpack the 8b input rgbrgbrgbrgb ... as contiguous registers:
  246. // rrrr... rrrr... gggg... gggg... bbbb... bbbb....
  247. // Similar to PlanarTo24bHelper(), but in reverse order.
  248. static WEBP_INLINE void RGB24PackedToPlanar_SSE41(
  249. const uint8_t* const rgb, __m128i* const out /*out[6]*/) {
  250. const __m128i A0 = _mm_loadu_si128((const __m128i*)(rgb + 0));
  251. const __m128i A1 = _mm_loadu_si128((const __m128i*)(rgb + 16));
  252. const __m128i A2 = _mm_loadu_si128((const __m128i*)(rgb + 32));
  253. const __m128i A3 = _mm_loadu_si128((const __m128i*)(rgb + 48));
  254. const __m128i A4 = _mm_loadu_si128((const __m128i*)(rgb + 64));
  255. const __m128i A5 = _mm_loadu_si128((const __m128i*)(rgb + 80));
  256. // Compute RR.
  257. {
  258. const __m128i shuff0 = _mm_set_epi8(
  259. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, 12, 9, 6, 3, 0);
  260. const __m128i shuff1 = _mm_set_epi8(
  261. -1, -1, -1, -1, -1, 14, 11, 8, 5, 2, -1, -1, -1, -1, -1, -1);
  262. const __m128i shuff2 = _mm_set_epi8(
  263. 13, 10, 7, 4, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  264. WEBP_SSE41_SHUFF(0)
  265. }
  266. // Compute GG.
  267. {
  268. const __m128i shuff0 = _mm_set_epi8(
  269. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 13, 10, 7, 4, 1);
  270. const __m128i shuff1 = _mm_set_epi8(
  271. -1, -1, -1, -1, -1, 15, 12, 9, 6, 3, 0, -1, -1, -1, -1, -1);
  272. const __m128i shuff2 = _mm_set_epi8(
  273. 14, 11, 8, 5, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  274. WEBP_SSE41_SHUFF(2)
  275. }
  276. // Compute BB.
  277. {
  278. const __m128i shuff0 = _mm_set_epi8(
  279. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 14, 11, 8, 5, 2);
  280. const __m128i shuff1 = _mm_set_epi8(
  281. -1, -1, -1, -1, -1, -1, 13, 10, 7, 4, 1, -1, -1, -1, -1, -1);
  282. const __m128i shuff2 = _mm_set_epi8(
  283. 15, 12, 9, 6, 3, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  284. WEBP_SSE41_SHUFF(4)
  285. }
  286. }
  287. #undef WEBP_SSE41_SHUFF
  288. // Convert 8 packed ARGB to r[], g[], b[]
  289. static WEBP_INLINE void RGB32PackedToPlanar_SSE41(
  290. const uint32_t* const argb, __m128i* const rgb /*in[6]*/) {
  291. const __m128i zero = _mm_setzero_si128();
  292. __m128i a0 = LOAD_16(argb + 0);
  293. __m128i a1 = LOAD_16(argb + 4);
  294. __m128i a2 = LOAD_16(argb + 8);
  295. __m128i a3 = LOAD_16(argb + 12);
  296. VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3);
  297. rgb[0] = _mm_unpacklo_epi8(a1, zero);
  298. rgb[1] = _mm_unpackhi_epi8(a1, zero);
  299. rgb[2] = _mm_unpacklo_epi8(a2, zero);
  300. rgb[3] = _mm_unpackhi_epi8(a2, zero);
  301. rgb[4] = _mm_unpacklo_epi8(a3, zero);
  302. rgb[5] = _mm_unpackhi_epi8(a3, zero);
  303. }
  304. // This macro computes (RG * MULT_RG + GB * MULT_GB + ROUNDER) >> DESCALE_FIX
  305. // It's a macro and not a function because we need to use immediate values with
  306. // srai_epi32, e.g.
  307. #define TRANSFORM(RG_LO, RG_HI, GB_LO, GB_HI, MULT_RG, MULT_GB, \
  308. ROUNDER, DESCALE_FIX, OUT) do { \
  309. const __m128i V0_lo = _mm_madd_epi16(RG_LO, MULT_RG); \
  310. const __m128i V0_hi = _mm_madd_epi16(RG_HI, MULT_RG); \
  311. const __m128i V1_lo = _mm_madd_epi16(GB_LO, MULT_GB); \
  312. const __m128i V1_hi = _mm_madd_epi16(GB_HI, MULT_GB); \
  313. const __m128i V2_lo = _mm_add_epi32(V0_lo, V1_lo); \
  314. const __m128i V2_hi = _mm_add_epi32(V0_hi, V1_hi); \
  315. const __m128i V3_lo = _mm_add_epi32(V2_lo, ROUNDER); \
  316. const __m128i V3_hi = _mm_add_epi32(V2_hi, ROUNDER); \
  317. const __m128i V5_lo = _mm_srai_epi32(V3_lo, DESCALE_FIX); \
  318. const __m128i V5_hi = _mm_srai_epi32(V3_hi, DESCALE_FIX); \
  319. (OUT) = _mm_packs_epi32(V5_lo, V5_hi); \
  320. } while (0)
  321. #define MK_CST_16(A, B) _mm_set_epi16((B), (A), (B), (A), (B), (A), (B), (A))
  322. static WEBP_INLINE void ConvertRGBToY_SSE41(const __m128i* const R,
  323. const __m128i* const G,
  324. const __m128i* const B,
  325. __m128i* const Y) {
  326. const __m128i kRG_y = MK_CST_16(16839, 33059 - 16384);
  327. const __m128i kGB_y = MK_CST_16(16384, 6420);
  328. const __m128i kHALF_Y = _mm_set1_epi32((16 << YUV_FIX) + YUV_HALF);
  329. const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
  330. const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
  331. const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
  332. const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
  333. TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_y, kGB_y, kHALF_Y, YUV_FIX, *Y);
  334. }
  335. static WEBP_INLINE void ConvertRGBToUV_SSE41(const __m128i* const R,
  336. const __m128i* const G,
  337. const __m128i* const B,
  338. __m128i* const U,
  339. __m128i* const V) {
  340. const __m128i kRG_u = MK_CST_16(-9719, -19081);
  341. const __m128i kGB_u = MK_CST_16(0, 28800);
  342. const __m128i kRG_v = MK_CST_16(28800, 0);
  343. const __m128i kGB_v = MK_CST_16(-24116, -4684);
  344. const __m128i kHALF_UV = _mm_set1_epi32(((128 << YUV_FIX) + YUV_HALF) << 2);
  345. const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
  346. const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
  347. const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
  348. const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
  349. TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_u, kGB_u,
  350. kHALF_UV, YUV_FIX + 2, *U);
  351. TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_v, kGB_v,
  352. kHALF_UV, YUV_FIX + 2, *V);
  353. }
  354. #undef MK_CST_16
  355. #undef TRANSFORM
  356. static void ConvertRGB24ToY_SSE41(const uint8_t* rgb, uint8_t* y, int width) {
  357. const int max_width = width & ~31;
  358. int i;
  359. for (i = 0; i < max_width; rgb += 3 * 16 * 2) {
  360. __m128i rgb_plane[6];
  361. int j;
  362. RGB24PackedToPlanar_SSE41(rgb, rgb_plane);
  363. for (j = 0; j < 2; ++j, i += 16) {
  364. const __m128i zero = _mm_setzero_si128();
  365. __m128i r, g, b, Y0, Y1;
  366. // Convert to 16-bit Y.
  367. r = _mm_unpacklo_epi8(rgb_plane[0 + j], zero);
  368. g = _mm_unpacklo_epi8(rgb_plane[2 + j], zero);
  369. b = _mm_unpacklo_epi8(rgb_plane[4 + j], zero);
  370. ConvertRGBToY_SSE41(&r, &g, &b, &Y0);
  371. // Convert to 16-bit Y.
  372. r = _mm_unpackhi_epi8(rgb_plane[0 + j], zero);
  373. g = _mm_unpackhi_epi8(rgb_plane[2 + j], zero);
  374. b = _mm_unpackhi_epi8(rgb_plane[4 + j], zero);
  375. ConvertRGBToY_SSE41(&r, &g, &b, &Y1);
  376. // Cast to 8-bit and store.
  377. STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
  378. }
  379. }
  380. for (; i < width; ++i, rgb += 3) { // left-over
  381. y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
  382. }
  383. }
  384. static void ConvertBGR24ToY_SSE41(const uint8_t* bgr, uint8_t* y, int width) {
  385. const int max_width = width & ~31;
  386. int i;
  387. for (i = 0; i < max_width; bgr += 3 * 16 * 2) {
  388. __m128i bgr_plane[6];
  389. int j;
  390. RGB24PackedToPlanar_SSE41(bgr, bgr_plane);
  391. for (j = 0; j < 2; ++j, i += 16) {
  392. const __m128i zero = _mm_setzero_si128();
  393. __m128i r, g, b, Y0, Y1;
  394. // Convert to 16-bit Y.
  395. b = _mm_unpacklo_epi8(bgr_plane[0 + j], zero);
  396. g = _mm_unpacklo_epi8(bgr_plane[2 + j], zero);
  397. r = _mm_unpacklo_epi8(bgr_plane[4 + j], zero);
  398. ConvertRGBToY_SSE41(&r, &g, &b, &Y0);
  399. // Convert to 16-bit Y.
  400. b = _mm_unpackhi_epi8(bgr_plane[0 + j], zero);
  401. g = _mm_unpackhi_epi8(bgr_plane[2 + j], zero);
  402. r = _mm_unpackhi_epi8(bgr_plane[4 + j], zero);
  403. ConvertRGBToY_SSE41(&r, &g, &b, &Y1);
  404. // Cast to 8-bit and store.
  405. STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
  406. }
  407. }
  408. for (; i < width; ++i, bgr += 3) { // left-over
  409. y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
  410. }
  411. }
  412. static void ConvertARGBToY_SSE41(const uint32_t* argb, uint8_t* y, int width) {
  413. const int max_width = width & ~15;
  414. int i;
  415. for (i = 0; i < max_width; i += 16) {
  416. __m128i Y0, Y1, rgb[6];
  417. RGB32PackedToPlanar_SSE41(&argb[i], rgb);
  418. ConvertRGBToY_SSE41(&rgb[0], &rgb[2], &rgb[4], &Y0);
  419. ConvertRGBToY_SSE41(&rgb[1], &rgb[3], &rgb[5], &Y1);
  420. STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
  421. }
  422. for (; i < width; ++i) { // left-over
  423. const uint32_t p = argb[i];
  424. y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff,
  425. YUV_HALF);
  426. }
  427. }
  428. // Horizontal add (doubled) of two 16b values, result is 16b.
  429. // in: A | B | C | D | ... -> out: 2*(A+B) | 2*(C+D) | ...
  430. static void HorizontalAddPack_SSE41(const __m128i* const A,
  431. const __m128i* const B,
  432. __m128i* const out) {
  433. const __m128i k2 = _mm_set1_epi16(2);
  434. const __m128i C = _mm_madd_epi16(*A, k2);
  435. const __m128i D = _mm_madd_epi16(*B, k2);
  436. *out = _mm_packs_epi32(C, D);
  437. }
  438. static void ConvertARGBToUV_SSE41(const uint32_t* argb,
  439. uint8_t* u, uint8_t* v,
  440. int src_width, int do_store) {
  441. const int max_width = src_width & ~31;
  442. int i;
  443. for (i = 0; i < max_width; i += 32, u += 16, v += 16) {
  444. __m128i rgb[6], U0, V0, U1, V1;
  445. RGB32PackedToPlanar_SSE41(&argb[i], rgb);
  446. HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]);
  447. HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]);
  448. HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]);
  449. ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U0, &V0);
  450. RGB32PackedToPlanar_SSE41(&argb[i + 16], rgb);
  451. HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]);
  452. HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]);
  453. HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]);
  454. ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U1, &V1);
  455. U0 = _mm_packus_epi16(U0, U1);
  456. V0 = _mm_packus_epi16(V0, V1);
  457. if (!do_store) {
  458. const __m128i prev_u = LOAD_16(u);
  459. const __m128i prev_v = LOAD_16(v);
  460. U0 = _mm_avg_epu8(U0, prev_u);
  461. V0 = _mm_avg_epu8(V0, prev_v);
  462. }
  463. STORE_16(U0, u);
  464. STORE_16(V0, v);
  465. }
  466. if (i < src_width) { // left-over
  467. WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store);
  468. }
  469. }
  470. // Convert 16 packed ARGB 16b-values to r[], g[], b[]
  471. static WEBP_INLINE void RGBA32PackedToPlanar_16b_SSE41(
  472. const uint16_t* const rgbx,
  473. __m128i* const r, __m128i* const g, __m128i* const b) {
  474. const __m128i in0 = LOAD_16(rgbx + 0); // r0 | g0 | b0 |x| r1 | g1 | b1 |x
  475. const __m128i in1 = LOAD_16(rgbx + 8); // r2 | g2 | b2 |x| r3 | g3 | b3 |x
  476. const __m128i in2 = LOAD_16(rgbx + 16); // r4 | ...
  477. const __m128i in3 = LOAD_16(rgbx + 24); // r6 | ...
  478. // aarrggbb as 16-bit.
  479. const __m128i shuff0 =
  480. _mm_set_epi8(-1, -1, -1, -1, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0);
  481. const __m128i shuff1 =
  482. _mm_set_epi8(13, 12, 5, 4, -1, -1, -1, -1, 11, 10, 3, 2, 9, 8, 1, 0);
  483. const __m128i A0 = _mm_shuffle_epi8(in0, shuff0);
  484. const __m128i A1 = _mm_shuffle_epi8(in1, shuff1);
  485. const __m128i A2 = _mm_shuffle_epi8(in2, shuff0);
  486. const __m128i A3 = _mm_shuffle_epi8(in3, shuff1);
  487. // R0R1G0G1
  488. // B0B1****
  489. // R2R3G2G3
  490. // B2B3****
  491. // (OR is used to free port 5 for the unpack)
  492. const __m128i B0 = _mm_unpacklo_epi32(A0, A1);
  493. const __m128i B1 = _mm_or_si128(A0, A1);
  494. const __m128i B2 = _mm_unpacklo_epi32(A2, A3);
  495. const __m128i B3 = _mm_or_si128(A2, A3);
  496. // Gather the channels.
  497. *r = _mm_unpacklo_epi64(B0, B2);
  498. *g = _mm_unpackhi_epi64(B0, B2);
  499. *b = _mm_unpackhi_epi64(B1, B3);
  500. }
  501. static void ConvertRGBA32ToUV_SSE41(const uint16_t* rgb,
  502. uint8_t* u, uint8_t* v, int width) {
  503. const int max_width = width & ~15;
  504. const uint16_t* const last_rgb = rgb + 4 * max_width;
  505. while (rgb < last_rgb) {
  506. __m128i r, g, b, U0, V0, U1, V1;
  507. RGBA32PackedToPlanar_16b_SSE41(rgb + 0, &r, &g, &b);
  508. ConvertRGBToUV_SSE41(&r, &g, &b, &U0, &V0);
  509. RGBA32PackedToPlanar_16b_SSE41(rgb + 32, &r, &g, &b);
  510. ConvertRGBToUV_SSE41(&r, &g, &b, &U1, &V1);
  511. STORE_16(_mm_packus_epi16(U0, U1), u);
  512. STORE_16(_mm_packus_epi16(V0, V1), v);
  513. u += 16;
  514. v += 16;
  515. rgb += 2 * 32;
  516. }
  517. if (max_width < width) { // left-over
  518. WebPConvertRGBA32ToUV_C(rgb, u, v, width - max_width);
  519. }
  520. }
  521. //------------------------------------------------------------------------------
  522. extern void WebPInitConvertARGBToYUVSSE41(void);
  523. WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVSSE41(void) {
  524. WebPConvertARGBToY = ConvertARGBToY_SSE41;
  525. WebPConvertARGBToUV = ConvertARGBToUV_SSE41;
  526. WebPConvertRGB24ToY = ConvertRGB24ToY_SSE41;
  527. WebPConvertBGR24ToY = ConvertBGR24ToY_SSE41;
  528. WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_SSE41;
  529. }
  530. //------------------------------------------------------------------------------
  531. #else // !WEBP_USE_SSE41
  532. WEBP_DSP_INIT_STUB(WebPInitSamplersSSE41)
  533. WEBP_DSP_INIT_STUB(WebPInitConvertARGBToYUVSSE41)
  534. #endif // WEBP_USE_SSE41