FreeRDP
Loading...
Searching...
No Matches
prim_internal.h
1/* prim_internal.h
2 * vi:ts=4 sw=4
3 *
4 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing
12 * permissions and limitations under the License. Algorithms used by
13 * this code may be covered by patents by HP, Microsoft, or other parties.
14 *
15 */
16
17#ifndef FREERDP_LIB_PRIM_INTERNAL_H
18#define FREERDP_LIB_PRIM_INTERNAL_H
19
20#include <winpr/platform.h>
21#include <freerdp/config.h>
22
23#include <freerdp/primitives.h>
24#include <freerdp/api.h>
25
26#include "../core/simd.h"
27
28#define PRIM_ALIGN_128 DECLSPEC_ALIGN(16)
29
30#if defined(SSE_AVX_INTRINSICS_ENABLED) || defined(NEON_INTRINSICS_ENABLED) || defined(WITH_OPENCL)
31#define HAVE_OPTIMIZED_PRIMITIVES 1
32#endif
33
34#if defined(SSE_AVX_INTRINSICS_ENABLED) || defined(NEON_INTRINSICS_ENABLED)
35#define HAVE_CPU_OPTIMIZED_PRIMITIVES 1
36#endif
37
38WINPR_ATTR_NODISCARD
39static inline BYTE* writePixelBGRA(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
40 BYTE B, BYTE A)
41{
42 WINPR_UNUSED(formatSize);
43 WINPR_UNUSED(format);
44
45 *dst++ = B;
46 *dst++ = G;
47 *dst++ = R;
48 *dst++ = A;
49 return dst;
50}
51
52WINPR_ATTR_NODISCARD
53static inline BYTE* writePixelBGRX(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
54 BYTE B, BYTE A)
55{
56 WINPR_UNUSED(formatSize);
57 WINPR_UNUSED(format);
58 WINPR_UNUSED(A);
59
60 *dst++ = B;
61 *dst++ = G;
62 *dst++ = R;
63 dst++; /* Do not touch alpha */
64
65 return dst;
66}
67
68WINPR_ATTR_NODISCARD
69static inline BYTE* writePixelRGBA(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
70 BYTE B, BYTE A)
71{
72 WINPR_UNUSED(formatSize);
73 WINPR_UNUSED(format);
74
75 *dst++ = R;
76 *dst++ = G;
77 *dst++ = B;
78 *dst++ = A;
79 return dst;
80}
81
82WINPR_ATTR_NODISCARD
83static inline BYTE* writePixelRGBX(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
84 BYTE B, BYTE A)
85{
86 WINPR_UNUSED(formatSize);
87 WINPR_UNUSED(format);
88 WINPR_UNUSED(A);
89
90 *dst++ = R;
91 *dst++ = G;
92 *dst++ = B;
93 dst++; /* Do not touch alpha */
94
95 return dst;
96}
97
98WINPR_ATTR_NODISCARD
99static inline BYTE* writePixelABGR(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
100 BYTE B, BYTE A)
101{
102 WINPR_UNUSED(formatSize);
103 WINPR_UNUSED(format);
104
105 *dst++ = A;
106 *dst++ = B;
107 *dst++ = G;
108 *dst++ = R;
109 return dst;
110}
111
112WINPR_ATTR_NODISCARD
113static inline BYTE* writePixelXBGR(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
114 BYTE B, BYTE A)
115{
116 WINPR_UNUSED(formatSize);
117 WINPR_UNUSED(format);
118 WINPR_UNUSED(A);
119
120 dst++; /* Do not touch alpha */
121 *dst++ = B;
122 *dst++ = G;
123 *dst++ = R;
124 return dst;
125}
126
127WINPR_ATTR_NODISCARD
128static inline BYTE* writePixelARGB(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
129 BYTE B, BYTE A)
130{
131 WINPR_UNUSED(formatSize);
132 WINPR_UNUSED(format);
133
134 *dst++ = A;
135 *dst++ = R;
136 *dst++ = G;
137 *dst++ = B;
138 return dst;
139}
140
141WINPR_ATTR_NODISCARD
142static inline BYTE* writePixelXRGB(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
143 BYTE B, BYTE A)
144{
145 WINPR_UNUSED(formatSize);
146 WINPR_UNUSED(format);
147 WINPR_UNUSED(A);
148
149 dst++; /* Do not touch alpha */
150 *dst++ = R;
151 *dst++ = G;
152 *dst++ = B;
153 return dst;
154}
155
156WINPR_ATTR_NODISCARD
157static inline BYTE* writePixelGenericAlpha(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R,
158 BYTE G, BYTE B, BYTE A)
159{
160 UINT32 color = FreeRDPGetColor(format, R, G, B, A);
161 FreeRDPWriteColor(dst, format, color);
162 return dst + formatSize;
163}
164
165WINPR_ATTR_NODISCARD
166static inline BYTE* writePixelGeneric(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
167 BYTE B, BYTE A)
168{
169 UINT32 color = FreeRDPGetColor(format, R, G, B, A);
170 FreeRDPWriteColorIgnoreAlpha(dst, format, color);
171 return dst + formatSize;
172}
173
174typedef BYTE* (*fkt_writePixel)(BYTE*, DWORD, UINT32, BYTE, BYTE, BYTE, BYTE);
175
176WINPR_ATTR_NODISCARD
177static inline fkt_writePixel getPixelWriteFunction(DWORD format, BOOL useAlpha)
178{
179 switch (format)
180 {
181 case PIXEL_FORMAT_ARGB32:
182 case PIXEL_FORMAT_XRGB32:
183 return useAlpha ? writePixelARGB : writePixelXRGB;
184
185 case PIXEL_FORMAT_ABGR32:
186 case PIXEL_FORMAT_XBGR32:
187 return useAlpha ? writePixelABGR : writePixelXBGR;
188
189 case PIXEL_FORMAT_RGBA32:
190 case PIXEL_FORMAT_RGBX32:
191 return useAlpha ? writePixelRGBA : writePixelRGBX;
192
193 case PIXEL_FORMAT_BGRA32:
194 case PIXEL_FORMAT_BGRX32:
195 return useAlpha ? writePixelBGRA : writePixelBGRX;
196
197 default:
198 return useAlpha ? writePixelGenericAlpha : writePixelGeneric;
199 }
200}
201
202WINPR_ATTR_NODISCARD
203static inline BYTE CLIP(INT64 X)
204{
205 if (X > 255L)
206 return 255L;
207
208 if (X < 0L)
209 return 0L;
210
211 return (BYTE)X;
212}
213
214WINPR_ATTR_NODISCARD
215static inline BYTE CONDITIONAL_CLIP(INT32 in, BYTE original)
216{
217 BYTE out = CLIP(in);
218 BYTE diff = 0;
219 if (out > original)
220 diff = out - original;
221 else
222 diff = original - out;
223 if (diff < 30)
224 return original;
225 return out;
226}
227
233static inline INT32 C(INT32 Y)
234{
235 return (Y)-0;
236}
237
238static inline INT32 D(INT32 U)
239{
240 return (U)-128;
241}
242
243static inline INT32 E(INT32 V)
244{
245 return (V)-128;
246}
247
248WINPR_ATTR_NODISCARD
249static inline BYTE YUV2R(INT32 Y, INT32 U, INT32 V)
250{
251 const INT32 r = (256 * C(Y) + 0 * D(U) + 403 * E(V));
252 const INT32 r8 = r >> 8;
253 return CLIP(r8);
254}
255
256WINPR_ATTR_NODISCARD
257static inline BYTE YUV2G(INT32 Y, INT32 U, INT32 V)
258{
259 const INT32 g = (256 * C(Y) - 48 * D(U) - 120 * E(V));
260 const INT32 g8 = g >> 8;
261 return CLIP(g8);
262}
263
264WINPR_ATTR_NODISCARD
265static inline BYTE YUV2B(INT32 Y, INT32 U, INT32 V)
266{
267 const INT32 b = (256 * C(Y) + 475 * D(U) + 0 * E(V));
268 const INT32 b8 = b >> 8;
269 return CLIP(b8);
270}
271
277WINPR_ATTR_NODISCARD
278static inline BYTE RGB2Y(INT32 R, INT32 G, INT32 B)
279{
280 const INT32 val = ((54 * R + 183 * G + 18 * B) >> 8);
281 return WINPR_ASSERTING_INT_CAST(BYTE, val);
282}
283
284WINPR_ATTR_NODISCARD
285static inline BYTE RGB2U(INT32 R, INT32 G, INT32 B)
286{
287 const INT32 val = (((-29 * R - 99 * G + 128 * B) >> 8) + 128);
288 return WINPR_ASSERTING_INT_CAST(BYTE, val);
289}
290
291WINPR_ATTR_NODISCARD
292static inline BYTE RGB2V(INT32 R, INT32 G, INT32 B)
293{
294 const INT32 val = (((128 * R - 116 * G - 12 * B) >> 8) + 128);
295 return WINPR_ASSERTING_INT_CAST(BYTE, val);
296}
297
298static inline BYTE* writeYUVPixel(BYTE* dst, UINT32 DstFormat, INT32 y, INT32 u, INT32 v,
299 fkt_writePixel fkt)
300{
301 WINPR_ASSERT(fkt);
302 const BYTE r = YUV2R(y, u, v);
303 const BYTE g = YUV2G(y, u, v);
304 const BYTE b = YUV2B(y, u, v);
305 const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
306 return fkt(dst, formatSize, DstFormat, r, g, b, 0);
307}
308
309FREERDP_LOCAL void general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(
310 size_t offset, const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd,
311 BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
312 BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
313 BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width);
314
315FREERDP_LOCAL void general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
316 size_t offset, const BYTE* WINPR_RESTRICT pSrcEven, const BYTE* WINPR_RESTRICT pSrcOdd,
317 BYTE* WINPR_RESTRICT yLumaDstEven, BYTE* WINPR_RESTRICT yLumaDstOdd,
318 BYTE* WINPR_RESTRICT uLumaDst, BYTE* WINPR_RESTRICT vLumaDst,
319 BYTE* WINPR_RESTRICT yEvenChromaDst1, BYTE* WINPR_RESTRICT yEvenChromaDst2,
320 BYTE* WINPR_RESTRICT yOddChromaDst1, BYTE* WINPR_RESTRICT yOddChromaDst2,
321 BYTE* WINPR_RESTRICT uChromaDst1, BYTE* WINPR_RESTRICT uChromaDst2,
322 BYTE* WINPR_RESTRICT vChromaDst1, BYTE* WINPR_RESTRICT vChromaDst2, UINT32 width);
323
324/* Function prototypes for all the init/deinit routines. */
325FREERDP_LOCAL void primitives_init_copy(primitives_t* WINPR_RESTRICT prims);
326FREERDP_LOCAL void primitives_init_set(primitives_t* WINPR_RESTRICT prims);
327FREERDP_LOCAL void primitives_init_add(primitives_t* WINPR_RESTRICT prims);
328FREERDP_LOCAL void primitives_init_andor(primitives_t* WINPR_RESTRICT prims);
329FREERDP_LOCAL void primitives_init_shift(primitives_t* WINPR_RESTRICT prims);
330FREERDP_LOCAL void primitives_init_sign(primitives_t* WINPR_RESTRICT prims);
331FREERDP_LOCAL void primitives_init_alphaComp(primitives_t* WINPR_RESTRICT prims);
332FREERDP_LOCAL void primitives_init_colors(primitives_t* WINPR_RESTRICT prims);
333FREERDP_LOCAL void primitives_init_YCoCg(primitives_t* WINPR_RESTRICT prims);
334FREERDP_LOCAL void primitives_init_YUV(primitives_t* WINPR_RESTRICT prims);
335
336FREERDP_LOCAL void primitives_init_copy_opt(primitives_t* WINPR_RESTRICT prims);
337FREERDP_LOCAL void primitives_init_set_opt(primitives_t* WINPR_RESTRICT prims);
338FREERDP_LOCAL void primitives_init_add_opt(primitives_t* WINPR_RESTRICT prims);
339FREERDP_LOCAL void primitives_init_andor_opt(primitives_t* WINPR_RESTRICT prims);
340FREERDP_LOCAL void primitives_init_shift_opt(primitives_t* WINPR_RESTRICT prims);
341FREERDP_LOCAL void primitives_init_sign_opt(primitives_t* WINPR_RESTRICT prims);
342FREERDP_LOCAL void primitives_init_alphaComp_opt(primitives_t* WINPR_RESTRICT prims);
343FREERDP_LOCAL void primitives_init_colors_opt(primitives_t* WINPR_RESTRICT prims);
344FREERDP_LOCAL void primitives_init_YCoCg_opt(primitives_t* WINPR_RESTRICT prims);
345FREERDP_LOCAL void primitives_init_YUV_opt(primitives_t* WINPR_RESTRICT prims);
346
347#if defined(WITH_OPENCL)
348WINPR_ATTR_NODISCARD
349FREERDP_LOCAL BOOL primitives_init_opencl(primitives_t* WINPR_RESTRICT prims);
350#endif
351
352#endif /* FREERDP_LIB_PRIM_INTERNAL_H */