yuv_sse2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_SSE2)
  15. #include <emmintrin.h>
  16. #include <string.h> // for memcpy
  17. typedef union { // handy struct for converting SSE2 registers
  18. int32_t i32[4];
  19. uint8_t u8[16];
  20. __m128i m;
  21. } VP8kCstSSE2;
  22. #if defined(WEBP_YUV_USE_SSE2_TABLES)
  23. #include "./yuv_tables_sse2.h"
  24. void VP8YUVInitSSE2(void) {}
  25. #else
  26. static int done_sse2 = 0;
  27. static VP8kCstSSE2 VP8kUtoRGBA[256], VP8kVtoRGBA[256], VP8kYtoRGBA[256];
  28. void VP8YUVInitSSE2(void) {
  29. if (!done_sse2) {
  30. int i;
  31. for (i = 0; i < 256; ++i) {
  32. VP8kYtoRGBA[i].i32[0] =
  33. VP8kYtoRGBA[i].i32[1] =
  34. VP8kYtoRGBA[i].i32[2] = (i - 16) * kYScale + YUV_HALF2;
  35. VP8kYtoRGBA[i].i32[3] = 0xff << YUV_FIX2;
  36. VP8kUtoRGBA[i].i32[0] = 0;
  37. VP8kUtoRGBA[i].i32[1] = -kUToG * (i - 128);
  38. VP8kUtoRGBA[i].i32[2] = kUToB * (i - 128);
  39. VP8kUtoRGBA[i].i32[3] = 0;
  40. VP8kVtoRGBA[i].i32[0] = kVToR * (i - 128);
  41. VP8kVtoRGBA[i].i32[1] = -kVToG * (i - 128);
  42. VP8kVtoRGBA[i].i32[2] = 0;
  43. VP8kVtoRGBA[i].i32[3] = 0;
  44. }
  45. done_sse2 = 1;
  46. #if 0 // code used to generate 'yuv_tables_sse2.h'
  47. printf("static const VP8kCstSSE2 VP8kYtoRGBA[256] = {\n");
  48. for (i = 0; i < 256; ++i) {
  49. printf(" {{0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x}},\n",
  50. VP8kYtoRGBA[i].i32[0], VP8kYtoRGBA[i].i32[1],
  51. VP8kYtoRGBA[i].i32[2], VP8kYtoRGBA[i].i32[3]);
  52. }
  53. printf("};\n\n");
  54. printf("static const VP8kCstSSE2 VP8kUtoRGBA[256] = {\n");
  55. for (i = 0; i < 256; ++i) {
  56. printf(" {{0, 0x%.8x, 0x%.8x, 0}},\n",
  57. VP8kUtoRGBA[i].i32[1], VP8kUtoRGBA[i].i32[2]);
  58. }
  59. printf("};\n\n");
  60. printf("static VP8kCstSSE2 VP8kVtoRGBA[256] = {\n");
  61. for (i = 0; i < 256; ++i) {
  62. printf(" {{0x%.8x, 0x%.8x, 0, 0}},\n",
  63. VP8kVtoRGBA[i].i32[0], VP8kVtoRGBA[i].i32[1]);
  64. }
  65. printf("};\n\n");
  66. #endif
  67. }
  68. }
  69. #endif // WEBP_YUV_USE_SSE2_TABLES
  70. //-----------------------------------------------------------------------------
  71. static WEBP_INLINE __m128i LoadUVPart(int u, int v) {
  72. const __m128i u_part = _mm_loadu_si128(&VP8kUtoRGBA[u].m);
  73. const __m128i v_part = _mm_loadu_si128(&VP8kVtoRGBA[v].m);
  74. const __m128i uv_part = _mm_add_epi32(u_part, v_part);
  75. return uv_part;
  76. }
  77. static WEBP_INLINE __m128i GetRGBA32bWithUV(int y, const __m128i uv_part) {
  78. const __m128i y_part = _mm_loadu_si128(&VP8kYtoRGBA[y].m);
  79. const __m128i rgba1 = _mm_add_epi32(y_part, uv_part);
  80. const __m128i rgba2 = _mm_srai_epi32(rgba1, YUV_FIX2);
  81. return rgba2;
  82. }
  83. static WEBP_INLINE __m128i GetRGBA32b(int y, int u, int v) {
  84. const __m128i uv_part = LoadUVPart(u, v);
  85. return GetRGBA32bWithUV(y, uv_part);
  86. }
  87. static WEBP_INLINE void YuvToRgbSSE2(uint8_t y, uint8_t u, uint8_t v,
  88. uint8_t* const rgb) {
  89. const __m128i tmp0 = GetRGBA32b(y, u, v);
  90. const __m128i tmp1 = _mm_packs_epi32(tmp0, tmp0);
  91. const __m128i tmp2 = _mm_packus_epi16(tmp1, tmp1);
  92. // Note: we store 8 bytes at a time, not 3 bytes! -> memory stomp
  93. _mm_storel_epi64((__m128i*)rgb, tmp2);
  94. }
  95. static WEBP_INLINE void YuvToBgrSSE2(uint8_t y, uint8_t u, uint8_t v,
  96. uint8_t* const bgr) {
  97. const __m128i tmp0 = GetRGBA32b(y, u, v);
  98. const __m128i tmp1 = _mm_shuffle_epi32(tmp0, _MM_SHUFFLE(3, 0, 1, 2));
  99. const __m128i tmp2 = _mm_packs_epi32(tmp1, tmp1);
  100. const __m128i tmp3 = _mm_packus_epi16(tmp2, tmp2);
  101. // Note: we store 8 bytes at a time, not 3 bytes! -> memory stomp
  102. _mm_storel_epi64((__m128i*)bgr, tmp3);
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Convert spans of 32 pixels to various RGB formats for the fancy upsampler.
  106. #ifdef FANCY_UPSAMPLING
  107. void VP8YuvToRgba32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  108. uint8_t* dst) {
  109. int n;
  110. for (n = 0; n < 32; n += 4) {
  111. const __m128i tmp0_1 = GetRGBA32b(y[n + 0], u[n + 0], v[n + 0]);
  112. const __m128i tmp0_2 = GetRGBA32b(y[n + 1], u[n + 1], v[n + 1]);
  113. const __m128i tmp0_3 = GetRGBA32b(y[n + 2], u[n + 2], v[n + 2]);
  114. const __m128i tmp0_4 = GetRGBA32b(y[n + 3], u[n + 3], v[n + 3]);
  115. const __m128i tmp1_1 = _mm_packs_epi32(tmp0_1, tmp0_2);
  116. const __m128i tmp1_2 = _mm_packs_epi32(tmp0_3, tmp0_4);
  117. const __m128i tmp2 = _mm_packus_epi16(tmp1_1, tmp1_2);
  118. _mm_storeu_si128((__m128i*)dst, tmp2);
  119. dst += 4 * 4;
  120. }
  121. }
  122. void VP8YuvToBgra32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  123. uint8_t* dst) {
  124. int n;
  125. for (n = 0; n < 32; n += 2) {
  126. const __m128i tmp0_1 = GetRGBA32b(y[n + 0], u[n + 0], v[n + 0]);
  127. const __m128i tmp0_2 = GetRGBA32b(y[n + 1], u[n + 1], v[n + 1]);
  128. const __m128i tmp1_1 = _mm_shuffle_epi32(tmp0_1, _MM_SHUFFLE(3, 0, 1, 2));
  129. const __m128i tmp1_2 = _mm_shuffle_epi32(tmp0_2, _MM_SHUFFLE(3, 0, 1, 2));
  130. const __m128i tmp2_1 = _mm_packs_epi32(tmp1_1, tmp1_2);
  131. const __m128i tmp3 = _mm_packus_epi16(tmp2_1, tmp2_1);
  132. _mm_storel_epi64((__m128i*)dst, tmp3);
  133. dst += 4 * 2;
  134. }
  135. }
  136. void VP8YuvToRgb32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  137. uint8_t* dst) {
  138. int n;
  139. uint8_t tmp0[2 * 3 + 5 + 15];
  140. uint8_t* const tmp = (uint8_t*)((uintptr_t)(tmp0 + 15) & ~15); // align
  141. for (n = 0; n < 30; ++n) { // we directly stomp the *dst memory
  142. YuvToRgbSSE2(y[n], u[n], v[n], dst + n * 3);
  143. }
  144. // Last two pixels are special: we write in a tmp buffer before sending
  145. // to dst.
  146. YuvToRgbSSE2(y[n + 0], u[n + 0], v[n + 0], tmp + 0);
  147. YuvToRgbSSE2(y[n + 1], u[n + 1], v[n + 1], tmp + 3);
  148. memcpy(dst + n * 3, tmp, 2 * 3);
  149. }
  150. void VP8YuvToBgr32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  151. uint8_t* dst) {
  152. int n;
  153. uint8_t tmp0[2 * 3 + 5 + 15];
  154. uint8_t* const tmp = (uint8_t*)((uintptr_t)(tmp0 + 15) & ~15); // align
  155. for (n = 0; n < 30; ++n) {
  156. YuvToBgrSSE2(y[n], u[n], v[n], dst + n * 3);
  157. }
  158. YuvToBgrSSE2(y[n + 0], u[n + 0], v[n + 0], tmp + 0);
  159. YuvToBgrSSE2(y[n + 1], u[n + 1], v[n + 1], tmp + 3);
  160. memcpy(dst + n * 3, tmp, 2 * 3);
  161. }
  162. #endif // FANCY_UPSAMPLING
  163. //-----------------------------------------------------------------------------
  164. // Arbitrary-length row conversion functions
  165. static void YuvToRgbaRowSSE2(const uint8_t* y,
  166. const uint8_t* u, const uint8_t* v,
  167. uint8_t* dst, int len) {
  168. int n;
  169. for (n = 0; n + 4 <= len; n += 4) {
  170. const __m128i uv_0 = LoadUVPart(u[0], v[0]);
  171. const __m128i uv_1 = LoadUVPart(u[1], v[1]);
  172. const __m128i tmp0_1 = GetRGBA32bWithUV(y[0], uv_0);
  173. const __m128i tmp0_2 = GetRGBA32bWithUV(y[1], uv_0);
  174. const __m128i tmp0_3 = GetRGBA32bWithUV(y[2], uv_1);
  175. const __m128i tmp0_4 = GetRGBA32bWithUV(y[3], uv_1);
  176. const __m128i tmp1_1 = _mm_packs_epi32(tmp0_1, tmp0_2);
  177. const __m128i tmp1_2 = _mm_packs_epi32(tmp0_3, tmp0_4);
  178. const __m128i tmp2 = _mm_packus_epi16(tmp1_1, tmp1_2);
  179. _mm_storeu_si128((__m128i*)dst, tmp2);
  180. dst += 4 * 4;
  181. y += 4;
  182. u += 2;
  183. v += 2;
  184. }
  185. // Finish off
  186. while (n < len) {
  187. VP8YuvToRgba(y[0], u[0], v[0], dst);
  188. dst += 4;
  189. ++y;
  190. u += (n & 1);
  191. v += (n & 1);
  192. ++n;
  193. }
  194. }
  195. static void YuvToBgraRowSSE2(const uint8_t* y,
  196. const uint8_t* u, const uint8_t* v,
  197. uint8_t* dst, int len) {
  198. int n;
  199. for (n = 0; n + 2 <= len; n += 2) {
  200. const __m128i uv_0 = LoadUVPart(u[0], v[0]);
  201. const __m128i tmp0_1 = GetRGBA32bWithUV(y[0], uv_0);
  202. const __m128i tmp0_2 = GetRGBA32bWithUV(y[1], uv_0);
  203. const __m128i tmp1_1 = _mm_shuffle_epi32(tmp0_1, _MM_SHUFFLE(3, 0, 1, 2));
  204. const __m128i tmp1_2 = _mm_shuffle_epi32(tmp0_2, _MM_SHUFFLE(3, 0, 1, 2));
  205. const __m128i tmp2_1 = _mm_packs_epi32(tmp1_1, tmp1_2);
  206. const __m128i tmp3 = _mm_packus_epi16(tmp2_1, tmp2_1);
  207. _mm_storel_epi64((__m128i*)dst, tmp3);
  208. dst += 4 * 2;
  209. y += 2;
  210. ++u;
  211. ++v;
  212. }
  213. // Finish off
  214. if (len & 1) {
  215. VP8YuvToBgra(y[0], u[0], v[0], dst);
  216. }
  217. }
  218. static void YuvToArgbRowSSE2(const uint8_t* y,
  219. const uint8_t* u, const uint8_t* v,
  220. uint8_t* dst, int len) {
  221. int n;
  222. for (n = 0; n + 2 <= len; n += 2) {
  223. const __m128i uv_0 = LoadUVPart(u[0], v[0]);
  224. const __m128i tmp0_1 = GetRGBA32bWithUV(y[0], uv_0);
  225. const __m128i tmp0_2 = GetRGBA32bWithUV(y[1], uv_0);
  226. const __m128i tmp1_1 = _mm_shuffle_epi32(tmp0_1, _MM_SHUFFLE(2, 1, 0, 3));
  227. const __m128i tmp1_2 = _mm_shuffle_epi32(tmp0_2, _MM_SHUFFLE(2, 1, 0, 3));
  228. const __m128i tmp2_1 = _mm_packs_epi32(tmp1_1, tmp1_2);
  229. const __m128i tmp3 = _mm_packus_epi16(tmp2_1, tmp2_1);
  230. _mm_storel_epi64((__m128i*)dst, tmp3);
  231. dst += 4 * 2;
  232. y += 2;
  233. ++u;
  234. ++v;
  235. }
  236. // Finish off
  237. if (len & 1) {
  238. VP8YuvToArgb(y[0], u[0], v[0], dst);
  239. }
  240. }
  241. static void YuvToRgbRowSSE2(const uint8_t* y,
  242. const uint8_t* u, const uint8_t* v,
  243. uint8_t* dst, int len) {
  244. int n;
  245. for (n = 0; n + 2 < len; ++n) { // we directly stomp the *dst memory
  246. YuvToRgbSSE2(y[0], u[0], v[0], dst); // stomps 8 bytes
  247. dst += 3;
  248. ++y;
  249. u += (n & 1);
  250. v += (n & 1);
  251. }
  252. VP8YuvToRgb(y[0], u[0], v[0], dst);
  253. if (len > 1) {
  254. VP8YuvToRgb(y[1], u[n & 1], v[n & 1], dst + 3);
  255. }
  256. }
  257. static void YuvToBgrRowSSE2(const uint8_t* y,
  258. const uint8_t* u, const uint8_t* v,
  259. uint8_t* dst, int len) {
  260. int n;
  261. for (n = 0; n + 2 < len; ++n) { // we directly stomp the *dst memory
  262. YuvToBgrSSE2(y[0], u[0], v[0], dst); // stomps 8 bytes
  263. dst += 3;
  264. ++y;
  265. u += (n & 1);
  266. v += (n & 1);
  267. }
  268. VP8YuvToBgr(y[0], u[0], v[0], dst + 0);
  269. if (len > 1) {
  270. VP8YuvToBgr(y[1], u[n & 1], v[n & 1], dst + 3);
  271. }
  272. }
  273. #endif // WEBP_USE_SSE2
  274. //------------------------------------------------------------------------------
  275. // Entry point
  276. extern void WebPInitSamplersSSE2(void);
  277. void WebPInitSamplersSSE2(void) {
  278. #if defined(WEBP_USE_SSE2)
  279. WebPSamplers[MODE_RGB] = YuvToRgbRowSSE2;
  280. WebPSamplers[MODE_RGBA] = YuvToRgbaRowSSE2;
  281. WebPSamplers[MODE_BGR] = YuvToBgrRowSSE2;
  282. WebPSamplers[MODE_BGRA] = YuvToBgraRowSSE2;
  283. WebPSamplers[MODE_ARGB] = YuvToArgbRowSSE2;
  284. #endif // WEBP_USE_SSE2
  285. }