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