upsampling_sse41.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2011 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. // SSE41 version of YUV to RGB upsampling functions.
  11. //
  12. // Author: somnath@google.com (Somnath Banerjee)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE41)
  15. #include <assert.h>
  16. #include <smmintrin.h>
  17. #include <string.h>
  18. #include "./yuv.h"
  19. #ifdef FANCY_UPSAMPLING
  20. #if !defined(WEBP_REDUCE_CSP)
  21. // We compute (9*a + 3*b + 3*c + d + 8) / 16 as follows
  22. // u = (9*a + 3*b + 3*c + d + 8) / 16
  23. // = (a + (a + 3*b + 3*c + d) / 8 + 1) / 2
  24. // = (a + m + 1) / 2
  25. // where m = (a + 3*b + 3*c + d) / 8
  26. // = ((a + b + c + d) / 2 + b + c) / 4
  27. //
  28. // Let's say k = (a + b + c + d) / 4.
  29. // We can compute k as
  30. // k = (s + t + 1) / 2 - ((a^d) | (b^c) | (s^t)) & 1
  31. // where s = (a + d + 1) / 2 and t = (b + c + 1) / 2
  32. //
  33. // Then m can be written as
  34. // m = (k + t + 1) / 2 - (((b^c) & (s^t)) | (k^t)) & 1
  35. // Computes out = (k + in + 1) / 2 - ((ij & (s^t)) | (k^in)) & 1
  36. #define GET_M(ij, in, out) do { \
  37. const __m128i tmp0 = _mm_avg_epu8(k, (in)); /* (k + in + 1) / 2 */ \
  38. const __m128i tmp1 = _mm_and_si128((ij), st); /* (ij) & (s^t) */ \
  39. const __m128i tmp2 = _mm_xor_si128(k, (in)); /* (k^in) */ \
  40. const __m128i tmp3 = _mm_or_si128(tmp1, tmp2); /* ((ij) & (s^t)) | (k^in) */\
  41. const __m128i tmp4 = _mm_and_si128(tmp3, one); /* & 1 -> lsb_correction */ \
  42. (out) = _mm_sub_epi8(tmp0, tmp4); /* (k + in + 1) / 2 - lsb_correction */ \
  43. } while (0)
  44. // pack and store two alternating pixel rows
  45. #define PACK_AND_STORE(a, b, da, db, out) do { \
  46. const __m128i t_a = _mm_avg_epu8(a, da); /* (9a + 3b + 3c + d + 8) / 16 */ \
  47. const __m128i t_b = _mm_avg_epu8(b, db); /* (3a + 9b + c + 3d + 8) / 16 */ \
  48. const __m128i t_1 = _mm_unpacklo_epi8(t_a, t_b); \
  49. const __m128i t_2 = _mm_unpackhi_epi8(t_a, t_b); \
  50. _mm_store_si128(((__m128i*)(out)) + 0, t_1); \
  51. _mm_store_si128(((__m128i*)(out)) + 1, t_2); \
  52. } while (0)
  53. // Loads 17 pixels each from rows r1 and r2 and generates 32 pixels.
  54. #define UPSAMPLE_32PIXELS(r1, r2, out) { \
  55. const __m128i one = _mm_set1_epi8(1); \
  56. const __m128i a = _mm_loadu_si128((const __m128i*)&(r1)[0]); \
  57. const __m128i b = _mm_loadu_si128((const __m128i*)&(r1)[1]); \
  58. const __m128i c = _mm_loadu_si128((const __m128i*)&(r2)[0]); \
  59. const __m128i d = _mm_loadu_si128((const __m128i*)&(r2)[1]); \
  60. \
  61. const __m128i s = _mm_avg_epu8(a, d); /* s = (a + d + 1) / 2 */ \
  62. const __m128i t = _mm_avg_epu8(b, c); /* t = (b + c + 1) / 2 */ \
  63. const __m128i st = _mm_xor_si128(s, t); /* st = s^t */ \
  64. \
  65. const __m128i ad = _mm_xor_si128(a, d); /* ad = a^d */ \
  66. const __m128i bc = _mm_xor_si128(b, c); /* bc = b^c */ \
  67. \
  68. const __m128i t1 = _mm_or_si128(ad, bc); /* (a^d) | (b^c) */ \
  69. const __m128i t2 = _mm_or_si128(t1, st); /* (a^d) | (b^c) | (s^t) */ \
  70. const __m128i t3 = _mm_and_si128(t2, one); /* (a^d) | (b^c) | (s^t) & 1 */ \
  71. const __m128i t4 = _mm_avg_epu8(s, t); \
  72. const __m128i k = _mm_sub_epi8(t4, t3); /* k = (a + b + c + d) / 4 */ \
  73. __m128i diag1, diag2; \
  74. \
  75. GET_M(bc, t, diag1); /* diag1 = (a + 3b + 3c + d) / 8 */ \
  76. GET_M(ad, s, diag2); /* diag2 = (3a + b + c + 3d) / 8 */ \
  77. \
  78. /* pack the alternate pixels */ \
  79. PACK_AND_STORE(a, b, diag1, diag2, (out) + 0); /* store top */ \
  80. PACK_AND_STORE(c, d, diag2, diag1, (out) + 2 * 32); /* store bottom */ \
  81. }
  82. // Turn the macro into a function for reducing code-size when non-critical
  83. static void Upsample32Pixels_SSE41(const uint8_t r1[], const uint8_t r2[],
  84. uint8_t* const out) {
  85. UPSAMPLE_32PIXELS(r1, r2, out);
  86. }
  87. #define UPSAMPLE_LAST_BLOCK(tb, bb, num_pixels, out) { \
  88. uint8_t r1[17], r2[17]; \
  89. memcpy(r1, (tb), (num_pixels)); \
  90. memcpy(r2, (bb), (num_pixels)); \
  91. /* replicate last byte */ \
  92. memset(r1 + (num_pixels), r1[(num_pixels) - 1], 17 - (num_pixels)); \
  93. memset(r2 + (num_pixels), r2[(num_pixels) - 1], 17 - (num_pixels)); \
  94. /* using the shared function instead of the macro saves ~3k code size */ \
  95. Upsample32Pixels_SSE41(r1, r2, out); \
  96. }
  97. #define CONVERT2RGB_32(FUNC, XSTEP, top_y, bottom_y, \
  98. top_dst, bottom_dst, cur_x) do { \
  99. FUNC##32_SSE41((top_y) + (cur_x), r_u, r_v, (top_dst) + (cur_x) * (XSTEP)); \
  100. if ((bottom_y) != NULL) { \
  101. FUNC##32_SSE41((bottom_y) + (cur_x), r_u + 64, r_v + 64, \
  102. (bottom_dst) + (cur_x) * (XSTEP)); \
  103. } \
  104. } while (0)
  105. #define SSE4_UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
  106. static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
  107. const uint8_t* top_u, const uint8_t* top_v, \
  108. const uint8_t* cur_u, const uint8_t* cur_v, \
  109. uint8_t* top_dst, uint8_t* bottom_dst, int len) { \
  110. int uv_pos, pos; \
  111. /* 16byte-aligned array to cache reconstructed u and v */ \
  112. uint8_t uv_buf[14 * 32 + 15] = { 0 }; \
  113. uint8_t* const r_u = (uint8_t*)((uintptr_t)(uv_buf + 15) & ~15); \
  114. uint8_t* const r_v = r_u + 32; \
  115. \
  116. assert(top_y != NULL); \
  117. { /* Treat the first pixel in regular way */ \
  118. const int u_diag = ((top_u[0] + cur_u[0]) >> 1) + 1; \
  119. const int v_diag = ((top_v[0] + cur_v[0]) >> 1) + 1; \
  120. const int u0_t = (top_u[0] + u_diag) >> 1; \
  121. const int v0_t = (top_v[0] + v_diag) >> 1; \
  122. FUNC(top_y[0], u0_t, v0_t, top_dst); \
  123. if (bottom_y != NULL) { \
  124. const int u0_b = (cur_u[0] + u_diag) >> 1; \
  125. const int v0_b = (cur_v[0] + v_diag) >> 1; \
  126. FUNC(bottom_y[0], u0_b, v0_b, bottom_dst); \
  127. } \
  128. } \
  129. /* For UPSAMPLE_32PIXELS, 17 u/v values must be read-able for each block */ \
  130. for (pos = 1, uv_pos = 0; pos + 32 + 1 <= len; pos += 32, uv_pos += 16) { \
  131. UPSAMPLE_32PIXELS(top_u + uv_pos, cur_u + uv_pos, r_u); \
  132. UPSAMPLE_32PIXELS(top_v + uv_pos, cur_v + uv_pos, r_v); \
  133. CONVERT2RGB_32(FUNC, XSTEP, top_y, bottom_y, top_dst, bottom_dst, pos); \
  134. } \
  135. if (len > 1) { \
  136. const int left_over = ((len + 1) >> 1) - (pos >> 1); \
  137. uint8_t* const tmp_top_dst = r_u + 4 * 32; \
  138. uint8_t* const tmp_bottom_dst = tmp_top_dst + 4 * 32; \
  139. uint8_t* const tmp_top = tmp_bottom_dst + 4 * 32; \
  140. uint8_t* const tmp_bottom = (bottom_y == NULL) ? NULL : tmp_top + 32; \
  141. assert(left_over > 0); \
  142. UPSAMPLE_LAST_BLOCK(top_u + uv_pos, cur_u + uv_pos, left_over, r_u); \
  143. UPSAMPLE_LAST_BLOCK(top_v + uv_pos, cur_v + uv_pos, left_over, r_v); \
  144. memcpy(tmp_top, top_y + pos, len - pos); \
  145. if (bottom_y != NULL) memcpy(tmp_bottom, bottom_y + pos, len - pos); \
  146. CONVERT2RGB_32(FUNC, XSTEP, tmp_top, tmp_bottom, tmp_top_dst, \
  147. tmp_bottom_dst, 0); \
  148. memcpy(top_dst + pos * (XSTEP), tmp_top_dst, (len - pos) * (XSTEP)); \
  149. if (bottom_y != NULL) { \
  150. memcpy(bottom_dst + pos * (XSTEP), tmp_bottom_dst, \
  151. (len - pos) * (XSTEP)); \
  152. } \
  153. } \
  154. }
  155. // SSE4 variants of the fancy upsampler.
  156. SSE4_UPSAMPLE_FUNC(UpsampleRgbLinePair_SSE41, VP8YuvToRgb, 3)
  157. SSE4_UPSAMPLE_FUNC(UpsampleBgrLinePair_SSE41, VP8YuvToBgr, 3)
  158. #undef GET_M
  159. #undef PACK_AND_STORE
  160. #undef UPSAMPLE_32PIXELS
  161. #undef UPSAMPLE_LAST_BLOCK
  162. #undef CONVERT2RGB
  163. #undef CONVERT2RGB_32
  164. #undef SSE4_UPSAMPLE_FUNC
  165. #endif // WEBP_REDUCE_CSP
  166. //------------------------------------------------------------------------------
  167. // Entry point
  168. extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
  169. extern void WebPInitUpsamplersSSE41(void);
  170. WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersSSE41(void) {
  171. #if !defined(WEBP_REDUCE_CSP)
  172. WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair_SSE41;
  173. WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair_SSE41;
  174. #endif // WEBP_REDUCE_CSP
  175. }
  176. #endif // FANCY_UPSAMPLING
  177. //------------------------------------------------------------------------------
  178. extern WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
  179. extern void WebPInitYUV444ConvertersSSE41(void);
  180. #define YUV444_FUNC(FUNC_NAME, CALL, CALL_C, XSTEP) \
  181. extern void CALL_C(const uint8_t* y, const uint8_t* u, const uint8_t* v, \
  182. uint8_t* dst, int len); \
  183. static void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \
  184. uint8_t* dst, int len) { \
  185. int i; \
  186. const int max_len = len & ~31; \
  187. for (i = 0; i < max_len; i += 32) { \
  188. CALL(y + i, u + i, v + i, dst + i * (XSTEP)); \
  189. } \
  190. if (i < len) { /* C-fallback */ \
  191. CALL_C(y + i, u + i, v + i, dst + i * (XSTEP), len - i); \
  192. } \
  193. }
  194. #if !defined(WEBP_REDUCE_CSP)
  195. YUV444_FUNC(Yuv444ToRgb_SSE41, VP8YuvToRgb32_SSE41, WebPYuv444ToRgb_C, 3);
  196. YUV444_FUNC(Yuv444ToBgr_SSE41, VP8YuvToBgr32_SSE41, WebPYuv444ToBgr_C, 3);
  197. #endif // WEBP_REDUCE_CSP
  198. WEBP_TSAN_IGNORE_FUNCTION void WebPInitYUV444ConvertersSSE41(void) {
  199. #if !defined(WEBP_REDUCE_CSP)
  200. WebPYUV444Converters[MODE_RGB] = Yuv444ToRgb_SSE41;
  201. WebPYUV444Converters[MODE_BGR] = Yuv444ToBgr_SSE41;
  202. #endif // WEBP_REDUCE_CSP
  203. }
  204. #else
  205. WEBP_DSP_INIT_STUB(WebPInitYUV444ConvertersSSE41)
  206. #endif // WEBP_USE_SSE41
  207. #if !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_SSE41))
  208. WEBP_DSP_INIT_STUB(WebPInitUpsamplersSSE41)
  209. #endif