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