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