20 #include <winpr/platform.h>
21 #include <freerdp/config.h>
23 #include "../nsc_types.h"
26 #if defined(WITH_SSE2)
27 #if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) || defined(_M_IX86_AMD64)
32 #if defined(SSE2_ENABLED)
37 #include <xmmintrin.h>
38 #include <emmintrin.h>
40 #include <freerdp/codec/color.h>
41 #include <winpr/crt.h>
42 #include <winpr/sysinfo.h>
44 static BOOL nsc_encode_argb_to_aycocg_sse2(NSC_CONTEXT* context,
const BYTE* data, UINT32 scanline)
49 const BYTE* src = NULL;
63 if (!context || !data || (scanline == 0))
66 tempWidth = ROUND_UP_TO(context->width, 8);
67 rw = (context->ChromaSubsamplingLevel > 0 ? tempWidth : context->width);
68 ccl = context->ColorLossLevel;
70 for (; y < context->height; y++)
72 src = data + (context->height - 1 - y) * scanline;
73 yplane = context->priv->PlaneBuffers[0] + y * rw;
74 coplane = context->priv->PlaneBuffers[1] + y * rw;
75 cgplane = context->priv->PlaneBuffers[2] + y * rw;
76 aplane = context->priv->PlaneBuffers[3] + y * context->width;
78 for (UINT16 x = 0; x < context->width; x += 8)
80 switch (context->format)
82 case PIXEL_FORMAT_BGRX32:
83 b_val = _mm_set_epi16(*(src + 28), *(src + 24), *(src + 20), *(src + 16),
84 *(src + 12), *(src + 8), *(src + 4), *src);
85 g_val = _mm_set_epi16(*(src + 29), *(src + 25), *(src + 21), *(src + 17),
86 *(src + 13), *(src + 9), *(src + 5), *(src + 1));
87 r_val = _mm_set_epi16(*(src + 30), *(src + 26), *(src + 22), *(src + 18),
88 *(src + 14), *(src + 10), *(src + 6), *(src + 2));
89 a_val = _mm_set1_epi16(0xFF);
93 case PIXEL_FORMAT_BGRA32:
94 b_val = _mm_set_epi16(*(src + 28), *(src + 24), *(src + 20), *(src + 16),
95 *(src + 12), *(src + 8), *(src + 4), *src);
96 g_val = _mm_set_epi16(*(src + 29), *(src + 25), *(src + 21), *(src + 17),
97 *(src + 13), *(src + 9), *(src + 5), *(src + 1));
98 r_val = _mm_set_epi16(*(src + 30), *(src + 26), *(src + 22), *(src + 18),
99 *(src + 14), *(src + 10), *(src + 6), *(src + 2));
100 a_val = _mm_set_epi16(*(src + 31), *(src + 27), *(src + 23), *(src + 19),
101 *(src + 15), *(src + 11), *(src + 7), *(src + 3));
105 case PIXEL_FORMAT_RGBX32:
106 r_val = _mm_set_epi16(*(src + 28), *(src + 24), *(src + 20), *(src + 16),
107 *(src + 12), *(src + 8), *(src + 4), *src);
108 g_val = _mm_set_epi16(*(src + 29), *(src + 25), *(src + 21), *(src + 17),
109 *(src + 13), *(src + 9), *(src + 5), *(src + 1));
110 b_val = _mm_set_epi16(*(src + 30), *(src + 26), *(src + 22), *(src + 18),
111 *(src + 14), *(src + 10), *(src + 6), *(src + 2));
112 a_val = _mm_set1_epi16(0xFF);
116 case PIXEL_FORMAT_RGBA32:
117 r_val = _mm_set_epi16(*(src + 28), *(src + 24), *(src + 20), *(src + 16),
118 *(src + 12), *(src + 8), *(src + 4), *src);
119 g_val = _mm_set_epi16(*(src + 29), *(src + 25), *(src + 21), *(src + 17),
120 *(src + 13), *(src + 9), *(src + 5), *(src + 1));
121 b_val = _mm_set_epi16(*(src + 30), *(src + 26), *(src + 22), *(src + 18),
122 *(src + 14), *(src + 10), *(src + 6), *(src + 2));
123 a_val = _mm_set_epi16(*(src + 31), *(src + 27), *(src + 23), *(src + 19),
124 *(src + 15), *(src + 11), *(src + 7), *(src + 3));
128 case PIXEL_FORMAT_BGR24:
129 b_val = _mm_set_epi16(*(src + 21), *(src + 18), *(src + 15), *(src + 12),
130 *(src + 9), *(src + 6), *(src + 3), *src);
131 g_val = _mm_set_epi16(*(src + 22), *(src + 19), *(src + 16), *(src + 13),
132 *(src + 10), *(src + 7), *(src + 4), *(src + 1));
133 r_val = _mm_set_epi16(*(src + 23), *(src + 20), *(src + 17), *(src + 14),
134 *(src + 11), *(src + 8), *(src + 5), *(src + 2));
135 a_val = _mm_set1_epi16(0xFF);
139 case PIXEL_FORMAT_RGB24:
140 r_val = _mm_set_epi16(*(src + 21), *(src + 18), *(src + 15), *(src + 12),
141 *(src + 9), *(src + 6), *(src + 3), *src);
142 g_val = _mm_set_epi16(*(src + 22), *(src + 19), *(src + 16), *(src + 13),
143 *(src + 10), *(src + 7), *(src + 4), *(src + 1));
144 b_val = _mm_set_epi16(*(src + 23), *(src + 20), *(src + 17), *(src + 14),
145 *(src + 11), *(src + 8), *(src + 5), *(src + 2));
146 a_val = _mm_set1_epi16(0xFF);
150 case PIXEL_FORMAT_BGR16:
151 b_val = _mm_set_epi16((((*(src + 15)) & 0xF8) | ((*(src + 15)) >> 5)),
152 (((*(src + 13)) & 0xF8) | ((*(src + 13)) >> 5)),
153 (((*(src + 11)) & 0xF8) | ((*(src + 11)) >> 5)),
154 (((*(src + 9)) & 0xF8) | ((*(src + 9)) >> 5)),
155 (((*(src + 7)) & 0xF8) | ((*(src + 7)) >> 5)),
156 (((*(src + 5)) & 0xF8) | ((*(src + 5)) >> 5)),
157 (((*(src + 3)) & 0xF8) | ((*(src + 3)) >> 5)),
158 (((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5)));
159 g_val = _mm_set_epi16(
160 ((((*(src + 15)) & 0x07) << 5) | (((*(src + 14)) & 0xE0) >> 3)),
161 ((((*(src + 13)) & 0x07) << 5) | (((*(src + 12)) & 0xE0) >> 3)),
162 ((((*(src + 11)) & 0x07) << 5) | (((*(src + 10)) & 0xE0) >> 3)),
163 ((((*(src + 9)) & 0x07) << 5) | (((*(src + 8)) & 0xE0) >> 3)),
164 ((((*(src + 7)) & 0x07) << 5) | (((*(src + 6)) & 0xE0) >> 3)),
165 ((((*(src + 5)) & 0x07) << 5) | (((*(src + 4)) & 0xE0) >> 3)),
166 ((((*(src + 3)) & 0x07) << 5) | (((*(src + 2)) & 0xE0) >> 3)),
167 ((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3)));
168 r_val = _mm_set_epi16(
169 ((((*(src + 14)) & 0x1F) << 3) | (((*(src + 14)) >> 2) & 0x07)),
170 ((((*(src + 12)) & 0x1F) << 3) | (((*(src + 12)) >> 2) & 0x07)),
171 ((((*(src + 10)) & 0x1F) << 3) | (((*(src + 10)) >> 2) & 0x07)),
172 ((((*(src + 8)) & 0x1F) << 3) | (((*(src + 8)) >> 2) & 0x07)),
173 ((((*(src + 6)) & 0x1F) << 3) | (((*(src + 6)) >> 2) & 0x07)),
174 ((((*(src + 4)) & 0x1F) << 3) | (((*(src + 4)) >> 2) & 0x07)),
175 ((((*(src + 2)) & 0x1F) << 3) | (((*(src + 2)) >> 2) & 0x07)),
176 ((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07)));
177 a_val = _mm_set1_epi16(0xFF);
181 case PIXEL_FORMAT_RGB16:
182 r_val = _mm_set_epi16((((*(src + 15)) & 0xF8) | ((*(src + 15)) >> 5)),
183 (((*(src + 13)) & 0xF8) | ((*(src + 13)) >> 5)),
184 (((*(src + 11)) & 0xF8) | ((*(src + 11)) >> 5)),
185 (((*(src + 9)) & 0xF8) | ((*(src + 9)) >> 5)),
186 (((*(src + 7)) & 0xF8) | ((*(src + 7)) >> 5)),
187 (((*(src + 5)) & 0xF8) | ((*(src + 5)) >> 5)),
188 (((*(src + 3)) & 0xF8) | ((*(src + 3)) >> 5)),
189 (((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5)));
190 g_val = _mm_set_epi16(
191 ((((*(src + 15)) & 0x07) << 5) | (((*(src + 14)) & 0xE0) >> 3)),
192 ((((*(src + 13)) & 0x07) << 5) | (((*(src + 12)) & 0xE0) >> 3)),
193 ((((*(src + 11)) & 0x07) << 5) | (((*(src + 10)) & 0xE0) >> 3)),
194 ((((*(src + 9)) & 0x07) << 5) | (((*(src + 8)) & 0xE0) >> 3)),
195 ((((*(src + 7)) & 0x07) << 5) | (((*(src + 6)) & 0xE0) >> 3)),
196 ((((*(src + 5)) & 0x07) << 5) | (((*(src + 4)) & 0xE0) >> 3)),
197 ((((*(src + 3)) & 0x07) << 5) | (((*(src + 2)) & 0xE0) >> 3)),
198 ((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3)));
199 b_val = _mm_set_epi16(
200 ((((*(src + 14)) & 0x1F) << 3) | (((*(src + 14)) >> 2) & 0x07)),
201 ((((*(src + 12)) & 0x1F) << 3) | (((*(src + 12)) >> 2) & 0x07)),
202 ((((*(src + 10)) & 0x1F) << 3) | (((*(src + 10)) >> 2) & 0x07)),
203 ((((*(src + 8)) & 0x1F) << 3) | (((*(src + 8)) >> 2) & 0x07)),
204 ((((*(src + 6)) & 0x1F) << 3) | (((*(src + 6)) >> 2) & 0x07)),
205 ((((*(src + 4)) & 0x1F) << 3) | (((*(src + 4)) >> 2) & 0x07)),
206 ((((*(src + 2)) & 0x1F) << 3) | (((*(src + 2)) >> 2) & 0x07)),
207 ((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07)));
208 a_val = _mm_set1_epi16(0xFF);
212 case PIXEL_FORMAT_A4:
216 for (
int shift = 7; shift >= 0; shift--)
218 idx[shift] = ((*src) >> shift) & 1;
219 idx[shift] |= (((*(src + 1)) >> shift) & 1) << 1;
220 idx[shift] |= (((*(src + 2)) >> shift) & 1) << 2;
221 idx[shift] |= (((*(src + 3)) >> shift) & 1) << 3;
225 r_val = _mm_set_epi16(context->palette[idx[0]], context->palette[idx[1]],
226 context->palette[idx[2]], context->palette[idx[3]],
227 context->palette[idx[4]], context->palette[idx[5]],
228 context->palette[idx[6]], context->palette[idx[7]]);
230 _mm_set_epi16(context->palette[idx[0] + 1], context->palette[idx[1] + 1],
231 context->palette[idx[2] + 1], context->palette[idx[3] + 1],
232 context->palette[idx[4] + 1], context->palette[idx[5] + 1],
233 context->palette[idx[6] + 1], context->palette[idx[7] + 1]);
235 _mm_set_epi16(context->palette[idx[0] + 2], context->palette[idx[1] + 2],
236 context->palette[idx[2] + 2], context->palette[idx[3] + 2],
237 context->palette[idx[4] + 2], context->palette[idx[5] + 2],
238 context->palette[idx[6] + 2], context->palette[idx[7] + 2]);
242 a_val = _mm_set1_epi16(0xFF);
245 case PIXEL_FORMAT_RGB8:
247 r_val = _mm_set_epi16(context->palette[(*(src + 7ULL)) * 3ULL],
248 context->palette[(*(src + 6ULL)) * 3ULL],
249 context->palette[(*(src + 5ULL)) * 3ULL],
250 context->palette[(*(src + 4ULL)) * 3ULL],
251 context->palette[(*(src + 3ULL)) * 3ULL],
252 context->palette[(*(src + 2ULL)) * 3ULL],
253 context->palette[(*(src + 1ULL)) * 3ULL],
254 context->palette[(*src) * 3ULL]);
255 g_val = _mm_set_epi16(context->palette[(*(src + 7ULL)) * 3ULL + 1ULL],
256 context->palette[(*(src + 6ULL)) * 3ULL + 1ULL],
257 context->palette[(*(src + 5ULL)) * 3ULL + 1ULL],
258 context->palette[(*(src + 4ULL)) * 3ULL + 1ULL],
259 context->palette[(*(src + 3ULL)) * 3ULL + 1ULL],
260 context->palette[(*(src + 2ULL)) * 3ULL + 1ULL],
261 context->palette[(*(src + 1ULL)) * 3ULL + 1ULL],
262 context->palette[(*src) * 3ULL + 1ULL]);
263 b_val = _mm_set_epi16(context->palette[(*(src + 7ULL)) * 3ULL + 2ULL],
264 context->palette[(*(src + 6ULL)) * 3ULL + 2ULL],
265 context->palette[(*(src + 5ULL)) * 3ULL + 2ULL],
266 context->palette[(*(src + 4ULL)) * 3ULL + 2ULL],
267 context->palette[(*(src + 3ULL)) * 3ULL + 2ULL],
268 context->palette[(*(src + 2ULL)) * 3ULL + 2ULL],
269 context->palette[(*(src + 1ULL)) * 3ULL + 2ULL],
270 context->palette[(*src) * 3ULL + 2ULL]);
274 a_val = _mm_set1_epi16(0xFF);
278 r_val = g_val = b_val = a_val = _mm_set1_epi16(0);
282 y_val = _mm_srai_epi16(r_val, 2);
283 y_val = _mm_add_epi16(y_val, _mm_srai_epi16(g_val, 1));
284 y_val = _mm_add_epi16(y_val, _mm_srai_epi16(b_val, 2));
285 co_val = _mm_sub_epi16(r_val, b_val);
286 co_val = _mm_srai_epi16(co_val, ccl);
287 cg_val = _mm_sub_epi16(g_val, _mm_srai_epi16(r_val, 1));
288 cg_val = _mm_sub_epi16(cg_val, _mm_srai_epi16(b_val, 1));
289 cg_val = _mm_srai_epi16(cg_val, ccl);
290 y_val = _mm_packus_epi16(y_val, y_val);
291 _mm_storeu_si128((__m128i*)yplane, y_val);
292 co_val = _mm_packs_epi16(co_val, co_val);
293 _mm_storeu_si128((__m128i*)coplane, co_val);
294 cg_val = _mm_packs_epi16(cg_val, cg_val);
295 _mm_storeu_si128((__m128i*)cgplane, cg_val);
296 a_val = _mm_packus_epi16(a_val, a_val);
297 _mm_storeu_si128((__m128i*)aplane, a_val);
304 if (context->ChromaSubsamplingLevel > 0 && (context->width % 2) == 1)
306 context->priv->PlaneBuffers[0][y * rw + context->width] =
307 context->priv->PlaneBuffers[0][y * rw + context->width - 1];
308 context->priv->PlaneBuffers[1][y * rw + context->width] =
309 context->priv->PlaneBuffers[1][y * rw + context->width - 1];
310 context->priv->PlaneBuffers[2][y * rw + context->width] =
311 context->priv->PlaneBuffers[2][y * rw + context->width - 1];
315 if (context->ChromaSubsamplingLevel > 0 && (y % 2) == 1)
317 yplane = context->priv->PlaneBuffers[0] + y * rw;
318 coplane = context->priv->PlaneBuffers[1] + y * rw;
319 cgplane = context->priv->PlaneBuffers[2] + y * rw;
320 CopyMemory(yplane, yplane - rw, rw);
321 CopyMemory(coplane, coplane - rw, rw);
322 CopyMemory(cgplane, cgplane - rw, rw);
328 static void nsc_encode_subsampling_sse2(NSC_CONTEXT* context)
332 INT8* co_src0 = NULL;
333 INT8* co_src1 = NULL;
334 INT8* cg_src0 = NULL;
335 INT8* cg_src1 = NULL;
336 UINT32 tempWidth = 0;
337 UINT32 tempHeight = 0;
340 __m128i mask = _mm_set1_epi16(0xFF);
341 tempWidth = ROUND_UP_TO(context->width, 8);
342 tempHeight = ROUND_UP_TO(context->height, 2);
344 for (
size_t y = 0; y < tempHeight >> 1; y++)
346 co_dst = context->priv->PlaneBuffers[1] + y * (tempWidth >> 1);
347 cg_dst = context->priv->PlaneBuffers[2] + y * (tempWidth >> 1);
348 co_src0 = (INT8*)context->priv->PlaneBuffers[1] + (y << 1) * tempWidth;
349 co_src1 = co_src0 + tempWidth;
350 cg_src0 = (INT8*)context->priv->PlaneBuffers[2] + (y << 1) * tempWidth;
351 cg_src1 = cg_src0 + tempWidth;
353 for (UINT32 x = 0; x < tempWidth >> 1; x += 8)
355 t = _mm_loadu_si128((__m128i*)co_src0);
356 t = _mm_avg_epu8(t, _mm_loadu_si128((__m128i*)co_src1));
357 val = _mm_and_si128(_mm_srli_si128(t, 1), mask);
358 val = _mm_avg_epu16(val, _mm_and_si128(t, mask));
359 val = _mm_packus_epi16(val, val);
360 _mm_storeu_si128((__m128i*)co_dst, val);
364 t = _mm_loadu_si128((__m128i*)cg_src0);
365 t = _mm_avg_epu8(t, _mm_loadu_si128((__m128i*)cg_src1));
366 val = _mm_and_si128(_mm_srli_si128(t, 1), mask);
367 val = _mm_avg_epu16(val, _mm_and_si128(t, mask));
368 val = _mm_packus_epi16(val, val);
369 _mm_storeu_si128((__m128i*)cg_dst, val);
377 static BOOL nsc_encode_sse2(NSC_CONTEXT* context,
const BYTE* data, UINT32 scanline)
379 if (!nsc_encode_argb_to_aycocg_sse2(context, data, scanline))
382 if (context->ChromaSubsamplingLevel > 0)
383 nsc_encode_subsampling_sse2(context);
389 void nsc_init_sse2(NSC_CONTEXT* context)
391 #if defined(SSE2_ENABLED)
392 if (!IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE))
395 PROFILER_RENAME(context->priv->prof_nsc_encode,
"nsc_encode_sse2")
396 context->encode = nsc_encode_sse2;
398 WINPR_UNUSED(context);