Kitxuuu commited on
Commit
b446696
·
verified ·
1 Parent(s): 8990e02

Add files using upload-large-folder tool

Browse files
Files changed (20) hide show
  1. local-test-freerdp-delta-01/afc-freerdp/client/Android/Studio/freeRDPCore/src/main/res/drawable/button_background.xml +31 -0
  2. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/color.c +1613 -0
  3. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/dsp_fdk_impl.h +45 -0
  4. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/dsp_ffmpeg.h +48 -0
  5. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264.c +837 -0
  6. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264.h +110 -0
  7. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264_ffmpeg.c +820 -0
  8. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264_mediacodec.c +527 -0
  9. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/nsc.c +512 -0
  10. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/nsc_encode.c +541 -0
  11. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/planar.c +1797 -0
  12. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/rfx_bitstream.h +134 -0
  13. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/rfx_decode.c +104 -0
  14. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/rfx_decode.h +33 -0
  15. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/xcrush.h +51 -0
  16. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/core/client.c +1509 -0
  17. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/core/listener.c +552 -0
  18. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/core/proxy.h +32 -0
  19. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/graphics.c +468 -0
  20. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/shape.c +299 -0
local-test-freerdp-delta-01/afc-freerdp/client/Android/Studio/freeRDPCore/src/main/res/drawable/button_background.xml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+
3
+ <selector xmlns:android="http://schemas.android.com/apk/res/android">
4
+
5
+ <item android:state_pressed="true">
6
+ <shape>
7
+ <solid android:color="#99D4FF" />
8
+ <stroke android:width="1dp" android:color="#A7A7A7" />
9
+ <corners android:radius="3dp" />
10
+ <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
11
+ </shape>
12
+ </item>
13
+
14
+ <item android:state_focused="true">
15
+ <shape>
16
+ <solid android:color="#E3E3E3" />
17
+ <stroke android:width="1dp" android:color="#585858" />
18
+ <corners android:radius="3dp" />
19
+ <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
20
+ </shape>
21
+ </item>
22
+
23
+ <item>
24
+ <shape>
25
+ <solid android:color="#E3E3E3" />
26
+ <stroke android:width="1dp" android:color="#585858" />
27
+ <corners android:radius="3dp" />
28
+ <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
29
+ </shape>
30
+ </item>
31
+ </selector>
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/color.c ADDED
@@ -0,0 +1,1613 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Color Conversion Routines
4
+ *
5
+ * Copyright 2010 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <stdlib.h>
27
+
28
+ #include <winpr/crt.h>
29
+
30
+ #include <freerdp/log.h>
31
+ #include <freerdp/freerdp.h>
32
+ #include <freerdp/primitives.h>
33
+
34
+ #if defined(WITH_CAIRO)
35
+ #include <cairo.h>
36
+ #endif
37
+
38
+ #if defined(WITH_SWSCALE)
39
+ #include <libswscale/swscale.h>
40
+ #endif
41
+
42
+ #include "color.h"
43
+
44
+ #define TAG FREERDP_TAG("color")
45
+
46
+ BYTE* freerdp_glyph_convert(UINT32 width, UINT32 height, const BYTE* WINPR_RESTRICT data)
47
+ {
48
+ /*
49
+ * converts a 1-bit-per-pixel glyph to a one-byte-per-pixel glyph:
50
+ * this approach uses a little more memory, but provides faster
51
+ * means of accessing individual pixels in blitting operations
52
+ */
53
+ const UINT32 scanline = (width + 7) / 8;
54
+ BYTE* dstData = (BYTE*)winpr_aligned_malloc(1ull * width * height, 16);
55
+
56
+ if (!dstData)
57
+ return NULL;
58
+
59
+ ZeroMemory(dstData, 1ULL * width * height);
60
+ BYTE* dstp = dstData;
61
+
62
+ for (UINT32 y = 0; y < height; y++)
63
+ {
64
+ const BYTE* srcp = &data[1ull * y * scanline];
65
+
66
+ for (UINT32 x = 0; x < width; x++)
67
+ {
68
+ if ((*srcp & (0x80 >> (x % 8))) != 0)
69
+ *dstp = 0xFF;
70
+
71
+ dstp++;
72
+
73
+ if (((x + 1) % 8 == 0) && x != 0)
74
+ srcp++;
75
+ }
76
+ }
77
+
78
+ return dstData;
79
+ }
80
+
81
+ BOOL freerdp_image_copy_from_monochrome(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
82
+ UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
83
+ UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData,
84
+ UINT32 backColor, UINT32 foreColor,
85
+ const gdiPalette* WINPR_RESTRICT palette)
86
+ {
87
+ const UINT32 dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
88
+
89
+ if (!pDstData || !pSrcData || !palette)
90
+ return FALSE;
91
+
92
+ if (nDstStep == 0)
93
+ nDstStep = dstBytesPerPixel * nWidth;
94
+
95
+ const UINT32 monoStep = (nWidth + 7) / 8;
96
+
97
+ for (size_t y = 0; y < nHeight; y++)
98
+ {
99
+ BYTE* pDstLine = &pDstData[((nYDst + y) * nDstStep)];
100
+ UINT32 monoBit = 0x80;
101
+ const BYTE* monoBits = &pSrcData[monoStep * y];
102
+
103
+ for (size_t x = 0; x < nWidth; x++)
104
+ {
105
+ BYTE* pDstPixel = &pDstLine[((nXDst + x) * FreeRDPGetBytesPerPixel(DstFormat))];
106
+ BOOL monoPixel = (*monoBits & monoBit) ? TRUE : FALSE;
107
+
108
+ if (!(monoBit >>= 1))
109
+ {
110
+ monoBits++;
111
+ monoBit = 0x80;
112
+ }
113
+
114
+ if (monoPixel)
115
+ FreeRDPWriteColor_int(pDstPixel, DstFormat, backColor);
116
+ else
117
+ FreeRDPWriteColor_int(pDstPixel, DstFormat, foreColor);
118
+ }
119
+ }
120
+
121
+ return TRUE;
122
+ }
123
+
124
+ static INLINE UINT32 freerdp_image_inverted_pointer_color(UINT32 x, UINT32 y, UINT32 format)
125
+ {
126
+ #if 1
127
+ /**
128
+ * Inverted pointer colors (where individual pixels can change their
129
+ * color to accommodate the background behind them) only seem to be
130
+ * supported on Windows.
131
+ * Using a static replacement color for these pixels (e.g. black)
132
+ * might result in invisible pointers depending on the background.
133
+ * This function returns either black or white, depending on the
134
+ * pixel's position.
135
+ */
136
+ BYTE fill = (x + y) & 1 ? 0x00 : 0xFF;
137
+ #else
138
+ BYTE fill = 0x00;
139
+ #endif
140
+ return FreeRDPGetColor(format, fill, fill, fill, 0xFF);
141
+ }
142
+
143
+ /*
144
+ * DIB color palettes are arrays of RGBQUAD structs with colors in BGRX format.
145
+ * They are used only by 1, 2, 4, and 8-bit bitmaps.
146
+ */
147
+ static void fill_gdi_palette_for_icon(const BYTE* colorTable, UINT16 cbColorTable,
148
+ gdiPalette* palette)
149
+ {
150
+ WINPR_ASSERT(palette);
151
+
152
+ palette->format = PIXEL_FORMAT_BGRX32;
153
+ ZeroMemory(palette->palette, sizeof(palette->palette));
154
+
155
+ if (!cbColorTable)
156
+ return;
157
+
158
+ if ((cbColorTable % 4 != 0) || (cbColorTable / 4 > 256))
159
+ {
160
+ WLog_WARN(TAG, "weird palette size: %u", cbColorTable);
161
+ return;
162
+ }
163
+
164
+ for (UINT16 i = 0; i < cbColorTable / 4; i++)
165
+ {
166
+ palette->palette[i] = FreeRDPReadColor_int(&colorTable[4ULL * i], palette->format);
167
+ }
168
+ }
169
+
170
+ static INLINE UINT32 div_ceil(UINT32 a, UINT32 b)
171
+ {
172
+ return (a + (b - 1)) / b;
173
+ }
174
+
175
+ static INLINE UINT32 round_up(UINT32 a, UINT32 b)
176
+ {
177
+ return b * div_ceil(a, b);
178
+ }
179
+
180
+ BOOL freerdp_image_copy_from_icon_data(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
181
+ UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT16 nWidth,
182
+ UINT16 nHeight, const BYTE* WINPR_RESTRICT bitsColor,
183
+ UINT16 cbBitsColor, const BYTE* WINPR_RESTRICT bitsMask,
184
+ UINT16 cbBitsMask, const BYTE* WINPR_RESTRICT colorTable,
185
+ UINT16 cbColorTable, UINT32 bpp)
186
+ {
187
+ DWORD format = 0;
188
+ gdiPalette palette;
189
+
190
+ if (!pDstData || !bitsColor)
191
+ return FALSE;
192
+
193
+ /*
194
+ * Color formats used by icons are DIB bitmap formats (2-bit format
195
+ * is not used by MS-RDPERP). Note that 16-bit is RGB555, not RGB565,
196
+ * and that 32-bit format uses BGRA order.
197
+ */
198
+ switch (bpp)
199
+ {
200
+ case 1:
201
+ case 4:
202
+ /*
203
+ * These formats are not supported by freerdp_image_copy().
204
+ * PIXEL_FORMAT_MONO and PIXEL_FORMAT_A4 are *not* correct
205
+ * color formats for this. Please fix freerdp_image_copy()
206
+ * if you came here to fix a broken icon of some weird app
207
+ * that still uses 1 or 4bpp format in the 21st century.
208
+ */
209
+ WLog_WARN(TAG, "1bpp and 4bpp icons are not supported");
210
+ return FALSE;
211
+
212
+ case 8:
213
+ format = PIXEL_FORMAT_RGB8;
214
+ break;
215
+
216
+ case 16:
217
+ format = PIXEL_FORMAT_RGB15;
218
+ break;
219
+
220
+ case 24:
221
+ format = PIXEL_FORMAT_RGB24;
222
+ break;
223
+
224
+ case 32:
225
+ format = PIXEL_FORMAT_BGRA32;
226
+ break;
227
+
228
+ default:
229
+ WLog_WARN(TAG, "invalid icon bpp: %" PRIu32, bpp);
230
+ return FALSE;
231
+ }
232
+
233
+ /* Ensure we have enough source data bytes for image copy. */
234
+ if (cbBitsColor < nWidth * nHeight * FreeRDPGetBytesPerPixel(format))
235
+ return FALSE;
236
+
237
+ fill_gdi_palette_for_icon(colorTable, cbColorTable, &palette);
238
+ if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
239
+ bitsColor, format, 0, 0, 0, &palette, FREERDP_FLIP_VERTICAL))
240
+ return FALSE;
241
+
242
+ /* apply alpha mask */
243
+ if (FreeRDPColorHasAlpha(DstFormat) && cbBitsMask)
244
+ {
245
+ BYTE nextBit = 0;
246
+ const BYTE* maskByte = NULL;
247
+ UINT32 stride = 0;
248
+ BYTE r = 0;
249
+ BYTE g = 0;
250
+ BYTE b = 0;
251
+ BYTE* dstBuf = pDstData;
252
+ UINT32 dstBpp = FreeRDPGetBytesPerPixel(DstFormat);
253
+
254
+ /*
255
+ * Each byte encodes 8 adjacent pixels (with LSB padding as needed).
256
+ * And due to hysterical raisins, stride of DIB bitmaps must be
257
+ * a multiple of 4 bytes.
258
+ */
259
+ stride = round_up(div_ceil(nWidth, 8), 4);
260
+
261
+ for (UINT32 y = 0; y < nHeight; y++)
262
+ {
263
+ maskByte = &bitsMask[1ULL * stride * (nHeight - 1 - y)];
264
+ nextBit = 0x80;
265
+
266
+ for (UINT32 x = 0; x < nWidth; x++)
267
+ {
268
+ UINT32 color = 0;
269
+ BYTE alpha = (*maskByte & nextBit) ? 0x00 : 0xFF;
270
+
271
+ /* read color back, add alpha and write it back */
272
+ color = FreeRDPReadColor_int(dstBuf, DstFormat);
273
+ FreeRDPSplitColor(color, DstFormat, &r, &g, &b, NULL, &palette);
274
+ color = FreeRDPGetColor(DstFormat, r, g, b, alpha);
275
+ FreeRDPWriteColor_int(dstBuf, DstFormat, color);
276
+
277
+ nextBit >>= 1;
278
+ dstBuf += dstBpp;
279
+ if (!nextBit)
280
+ {
281
+ nextBit = 0x80;
282
+ maskByte++;
283
+ }
284
+ }
285
+ }
286
+ }
287
+
288
+ return TRUE;
289
+ }
290
+
291
+ static BOOL freerdp_image_copy_from_pointer_data_1bpp(
292
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
293
+ UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
294
+ const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp)
295
+ {
296
+ BOOL vFlip = 0;
297
+ UINT32 xorStep = 0;
298
+ UINT32 andStep = 0;
299
+ UINT32 xorBit = 0;
300
+ UINT32 andBit = 0;
301
+ UINT32 xorPixel = 0;
302
+ UINT32 andPixel = 0;
303
+
304
+ vFlip = (xorBpp == 1) ? FALSE : TRUE;
305
+ andStep = (nWidth + 7) / 8;
306
+ andStep += (andStep % 2);
307
+
308
+ if (!xorMask || (xorMaskLength == 0))
309
+ return FALSE;
310
+ if (!andMask || (andMaskLength == 0))
311
+ return FALSE;
312
+
313
+ xorStep = (nWidth + 7) / 8;
314
+ xorStep += (xorStep % 2);
315
+
316
+ if (xorStep * nHeight > xorMaskLength)
317
+ return FALSE;
318
+
319
+ if (andStep * nHeight > andMaskLength)
320
+ return FALSE;
321
+
322
+ for (UINT32 y = 0; y < nHeight; y++)
323
+ {
324
+ const BYTE* andBits = NULL;
325
+ const BYTE* xorBits = NULL;
326
+ BYTE* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) +
327
+ (1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat))];
328
+ xorBit = andBit = 0x80;
329
+
330
+ if (!vFlip)
331
+ {
332
+ xorBits = &xorMask[1ULL * xorStep * y];
333
+ andBits = &andMask[1ULL * andStep * y];
334
+ }
335
+ else
336
+ {
337
+ xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
338
+ andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
339
+ }
340
+
341
+ for (UINT32 x = 0; x < nWidth; x++)
342
+ {
343
+ UINT32 color = 0;
344
+ xorPixel = (*xorBits & xorBit) ? 1 : 0;
345
+
346
+ if (!(xorBit >>= 1))
347
+ {
348
+ xorBits++;
349
+ xorBit = 0x80;
350
+ }
351
+
352
+ andPixel = (*andBits & andBit) ? 1 : 0;
353
+
354
+ if (!(andBit >>= 1))
355
+ {
356
+ andBits++;
357
+ andBit = 0x80;
358
+ }
359
+
360
+ if (!andPixel && !xorPixel)
361
+ color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0xFF); /* black */
362
+ else if (!andPixel && xorPixel)
363
+ color = FreeRDPGetColor(DstFormat, 0xFF, 0xFF, 0xFF, 0xFF); /* white */
364
+ else if (andPixel && !xorPixel)
365
+ color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0); /* transparent */
366
+ else if (andPixel && xorPixel)
367
+ color = freerdp_image_inverted_pointer_color(x, y, DstFormat); /* inverted */
368
+
369
+ FreeRDPWriteColor_int(pDstPixel, DstFormat, color);
370
+ pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
371
+ }
372
+ }
373
+
374
+ return TRUE;
375
+ }
376
+
377
+ static BOOL freerdp_image_copy_from_pointer_data_xbpp(
378
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
379
+ UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
380
+ const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp,
381
+ const gdiPalette* palette)
382
+ {
383
+ BOOL vFlip = 0;
384
+ size_t xorStep = 0;
385
+ size_t andStep = 0;
386
+ UINT32 andBit = 0;
387
+ UINT32 xorPixel = 0;
388
+ UINT32 andPixel = 0;
389
+ UINT32 dstBitsPerPixel = 0;
390
+ UINT32 xorBytesPerPixel = 0;
391
+ dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
392
+
393
+ vFlip = (xorBpp == 1) ? FALSE : TRUE;
394
+ andStep = (nWidth + 7) / 8;
395
+ andStep += (andStep % 2);
396
+
397
+ if (!xorMask || (xorMaskLength == 0))
398
+ return FALSE;
399
+
400
+ xorBytesPerPixel = xorBpp >> 3;
401
+ xorStep = 1ULL * nWidth * xorBytesPerPixel;
402
+ xorStep += (xorStep % 2);
403
+
404
+ if (xorBpp == 8 && !palette)
405
+ {
406
+ WLog_ERR(TAG, "null palette in conversion from %" PRIu32 " bpp to %" PRIu32 " bpp", xorBpp,
407
+ dstBitsPerPixel);
408
+ return FALSE;
409
+ }
410
+
411
+ if (xorStep * nHeight > xorMaskLength)
412
+ return FALSE;
413
+
414
+ if (andMask)
415
+ {
416
+ if (andStep * nHeight > andMaskLength)
417
+ return FALSE;
418
+ }
419
+
420
+ for (UINT32 y = 0; y < nHeight; y++)
421
+ {
422
+ const BYTE* xorBits = NULL;
423
+ const BYTE* andBits = NULL;
424
+ BYTE* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) +
425
+ (1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat))];
426
+ andBit = 0x80;
427
+
428
+ if (!vFlip)
429
+ {
430
+ if (andMask)
431
+ andBits = &andMask[andStep * y];
432
+
433
+ xorBits = &xorMask[xorStep * y];
434
+ }
435
+ else
436
+ {
437
+ if (andMask)
438
+ andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
439
+
440
+ xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
441
+ }
442
+
443
+ for (UINT32 x = 0; x < nWidth; x++)
444
+ {
445
+ UINT32 pixelFormat = 0;
446
+ UINT32 color = 0;
447
+
448
+ if (xorBpp == 32)
449
+ {
450
+ pixelFormat = PIXEL_FORMAT_BGRA32;
451
+ xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
452
+ }
453
+ else if (xorBpp == 16)
454
+ {
455
+ pixelFormat = PIXEL_FORMAT_RGB15;
456
+ xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
457
+ }
458
+ else if (xorBpp == 8)
459
+ {
460
+ pixelFormat = palette->format;
461
+ xorPixel = palette->palette[xorBits[0]];
462
+ }
463
+ else
464
+ {
465
+ pixelFormat = PIXEL_FORMAT_BGR24;
466
+ xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
467
+ }
468
+
469
+ xorPixel = FreeRDPConvertColor(xorPixel, pixelFormat, PIXEL_FORMAT_ARGB32, palette);
470
+ xorBits += xorBytesPerPixel;
471
+ andPixel = 0;
472
+
473
+ if (andMask)
474
+ {
475
+ andPixel = (*andBits & andBit) ? 1 : 0;
476
+
477
+ if (!(andBit >>= 1))
478
+ {
479
+ andBits++;
480
+ andBit = 0x80;
481
+ }
482
+ }
483
+
484
+ if (andPixel)
485
+ {
486
+ if (xorPixel == 0xFF000000) /* black -> transparent */
487
+ xorPixel = 0x00000000;
488
+ else if (xorPixel == 0xFFFFFFFF) /* white -> inverted */
489
+ xorPixel = freerdp_image_inverted_pointer_color(x, y, PIXEL_FORMAT_ARGB32);
490
+ }
491
+
492
+ color = FreeRDPConvertColor(xorPixel, PIXEL_FORMAT_ARGB32, DstFormat, palette);
493
+ FreeRDPWriteColor_int(pDstPixel, DstFormat, color);
494
+ pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
495
+ }
496
+ }
497
+
498
+ return TRUE;
499
+ }
500
+
501
+ /**
502
+ * Drawing Monochrome Pointers:
503
+ * http://msdn.microsoft.com/en-us/library/windows/hardware/ff556143/
504
+ *
505
+ * Drawing Color Pointers:
506
+ * http://msdn.microsoft.com/en-us/library/windows/hardware/ff556138/
507
+ */
508
+
509
+ BOOL freerdp_image_copy_from_pointer_data(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
510
+ UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
511
+ UINT32 nWidth, UINT32 nHeight,
512
+ const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
513
+ const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
514
+ UINT32 xorBpp, const gdiPalette* WINPR_RESTRICT palette)
515
+ {
516
+ UINT32 dstBitsPerPixel = 0;
517
+ UINT32 dstBytesPerPixel = 0;
518
+ dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
519
+ dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
520
+
521
+ if (nDstStep <= 0)
522
+ nDstStep = dstBytesPerPixel * nWidth;
523
+
524
+ for (UINT32 y = nYDst; y < nHeight; y++)
525
+ {
526
+ BYTE* WINPR_RESTRICT pDstLine = &pDstData[y * nDstStep + nXDst * dstBytesPerPixel];
527
+ memset(pDstLine, 0, 1ull * dstBytesPerPixel * (nWidth - nXDst));
528
+ }
529
+
530
+ switch (xorBpp)
531
+ {
532
+ case 1:
533
+ return freerdp_image_copy_from_pointer_data_1bpp(
534
+ pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
535
+ xorMaskLength, andMask, andMaskLength, xorBpp);
536
+
537
+ case 8:
538
+ case 16:
539
+ case 24:
540
+ case 32:
541
+ return freerdp_image_copy_from_pointer_data_xbpp(
542
+ pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
543
+ xorMaskLength, andMask, andMaskLength, xorBpp, palette);
544
+
545
+ default:
546
+ WLog_ERR(TAG, "failed to convert from %" PRIu32 " bpp to %" PRIu32 " bpp", xorBpp,
547
+ dstBitsPerPixel);
548
+ return FALSE;
549
+ }
550
+ }
551
+
552
+ static INLINE BOOL overlapping(const BYTE* pDstData, UINT32 nYDst, UINT32 nDstStep,
553
+ const BYTE* pSrcData, UINT32 nYSrc, UINT32 nSrcStep, UINT32 nHeight)
554
+ {
555
+ const uintptr_t src = (uintptr_t)pSrcData;
556
+ const uintptr_t srcstart = src + 1ULL * nSrcStep * nYSrc;
557
+ const uintptr_t srcend = srcstart + 1ULL * nSrcStep * nHeight;
558
+ const uintptr_t dst = (uintptr_t)pDstData;
559
+ const uintptr_t dststart = dst + 1ULL * nDstStep * nYDst;
560
+ const uintptr_t dstend = dststart + 1ULL * nDstStep * nHeight;
561
+
562
+ if ((dststart >= srcstart) && (dststart <= srcend))
563
+ return TRUE;
564
+
565
+ if ((dstend >= srcstart) && (dstend <= srcend))
566
+ return TRUE;
567
+
568
+ return FALSE;
569
+ }
570
+
571
+ static INLINE BOOL freerdp_image_copy_bgr24_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
572
+ UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
573
+ UINT32 nHeight,
574
+ const BYTE* WINPR_RESTRICT pSrcData,
575
+ UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
576
+ SSIZE_T srcVMultiplier, SSIZE_T srcVOffset,
577
+ SSIZE_T dstVMultiplier, SSIZE_T dstVOffset)
578
+ {
579
+
580
+ const SSIZE_T srcByte = 3;
581
+ const SSIZE_T dstByte = 4;
582
+
583
+ for (SSIZE_T y = 0; y < nHeight; y++)
584
+ {
585
+ const BYTE* WINPR_RESTRICT srcLine =
586
+ &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
587
+ BYTE* WINPR_RESTRICT dstLine =
588
+ &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
589
+
590
+ for (SSIZE_T x = 0; x < nWidth; x++)
591
+ {
592
+ dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
593
+ dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
594
+ dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
595
+ }
596
+ }
597
+
598
+ return TRUE;
599
+ }
600
+
601
+ static INLINE BOOL freerdp_image_copy_bgrx32_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
602
+ UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
603
+ UINT32 nHeight,
604
+ const BYTE* WINPR_RESTRICT pSrcData,
605
+ UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
606
+ SSIZE_T srcVMultiplier, SSIZE_T srcVOffset,
607
+ SSIZE_T dstVMultiplier, SSIZE_T dstVOffset)
608
+ {
609
+
610
+ const SSIZE_T srcByte = 4;
611
+ const SSIZE_T dstByte = 4;
612
+
613
+ for (SSIZE_T y = 0; y < nHeight; y++)
614
+ {
615
+ const BYTE* WINPR_RESTRICT srcLine =
616
+ &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
617
+ BYTE* WINPR_RESTRICT dstLine =
618
+ &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
619
+
620
+ for (SSIZE_T x = 0; x < nWidth; x++)
621
+ {
622
+ dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
623
+ dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
624
+ dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
625
+ }
626
+ }
627
+
628
+ return TRUE;
629
+ }
630
+
631
+ static INLINE BOOL freerdp_image_copy_generic(
632
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
633
+ UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
634
+ UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
635
+ SSIZE_T srcVMultiplier, SSIZE_T srcVOffset, SSIZE_T dstVMultiplier, SSIZE_T dstVOffset)
636
+ {
637
+
638
+ const SSIZE_T srcByte = 4;
639
+ const SSIZE_T dstByte = 4;
640
+
641
+ for (SSIZE_T y = 0; y < nHeight; y++)
642
+ {
643
+ const BYTE* WINPR_RESTRICT srcLine =
644
+ &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
645
+ BYTE* WINPR_RESTRICT dstLine =
646
+ &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
647
+
648
+ UINT32 color = FreeRDPReadColor_int(&srcLine[nXSrc * srcByte], SrcFormat);
649
+ UINT32 oldColor = color;
650
+ UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
651
+ FreeRDPWriteColorIgnoreAlpha_int(&dstLine[nXDst * dstByte], DstFormat, dstColor);
652
+ for (SSIZE_T x = 1; x < nWidth; x++)
653
+ {
654
+ color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
655
+ if (color == oldColor)
656
+ {
657
+ FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
658
+ dstColor);
659
+ }
660
+ else
661
+ {
662
+ oldColor = color;
663
+ dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
664
+ FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
665
+ dstColor);
666
+ }
667
+ }
668
+ }
669
+
670
+ return TRUE;
671
+ }
672
+
673
+ static INLINE BOOL freerdp_image_copy_no_overlap_dst_alpha(
674
+ BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
675
+ UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
676
+ UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
677
+ SSIZE_T srcVMultiplier, SSIZE_T srcVOffset, SSIZE_T dstVMultiplier, SSIZE_T dstVOffset)
678
+ {
679
+ WINPR_ASSERT(pDstData);
680
+ WINPR_ASSERT(pSrcData);
681
+
682
+ switch (SrcFormat)
683
+ {
684
+ case PIXEL_FORMAT_BGR24:
685
+ switch (DstFormat)
686
+ {
687
+ case PIXEL_FORMAT_BGRX32:
688
+ case PIXEL_FORMAT_BGRA32:
689
+ return freerdp_image_copy_bgr24_bgrx32(
690
+ pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
691
+ nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
692
+ default:
693
+ break;
694
+ }
695
+ break;
696
+ case PIXEL_FORMAT_BGRX32:
697
+ case PIXEL_FORMAT_BGRA32:
698
+ switch (DstFormat)
699
+ {
700
+ case PIXEL_FORMAT_BGRX32:
701
+ case PIXEL_FORMAT_BGRA32:
702
+ return freerdp_image_copy_bgrx32_bgrx32(
703
+ pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
704
+ nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
705
+ default:
706
+ break;
707
+ }
708
+ break;
709
+ default:
710
+ break;
711
+ }
712
+
713
+ return freerdp_image_copy_generic(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
714
+ pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
715
+ srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
716
+ }
717
+
718
+ BOOL freerdp_image_copy_overlap(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst,
719
+ UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* pSrcData,
720
+ DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
721
+ const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
722
+ {
723
+ const UINT32 dstByte = FreeRDPGetBytesPerPixel(DstFormat);
724
+ const UINT32 srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
725
+ const UINT32 copyDstWidth = nWidth * dstByte;
726
+ const UINT32 xSrcOffset = nXSrc * srcByte;
727
+ const UINT32 xDstOffset = nXDst * dstByte;
728
+ const BOOL vSrcVFlip = (flags & FREERDP_FLIP_VERTICAL) ? TRUE : FALSE;
729
+ SSIZE_T srcVOffset = 0;
730
+ SSIZE_T srcVMultiplier = 1;
731
+ SSIZE_T dstVOffset = 0;
732
+ SSIZE_T dstVMultiplier = 1;
733
+
734
+ WINPR_ASSERT(overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight));
735
+
736
+ if ((nWidth == 0) || (nHeight == 0))
737
+ return TRUE;
738
+
739
+ if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
740
+ return FALSE;
741
+
742
+ if (!pDstData || !pSrcData)
743
+ return FALSE;
744
+
745
+ if (nDstStep == 0)
746
+ nDstStep = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
747
+
748
+ if (nSrcStep == 0)
749
+ nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
750
+
751
+ if (vSrcVFlip)
752
+ {
753
+ srcVOffset = (nHeight - 1ll) * nSrcStep;
754
+ srcVMultiplier = -1;
755
+ }
756
+
757
+ if (((flags & FREERDP_KEEP_DST_ALPHA) != 0) && FreeRDPColorHasAlpha(DstFormat))
758
+ {
759
+ return freerdp_image_copy_no_overlap_dst_alpha(
760
+ pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
761
+ nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier,
762
+ dstVOffset);
763
+ }
764
+ else if (FreeRDPAreColorFormatsEqualNoAlpha_int(SrcFormat, DstFormat))
765
+ {
766
+ /* Copy down */
767
+ if (nYDst < nYSrc)
768
+ {
769
+ for (SSIZE_T y = 0; y < nHeight; y++)
770
+ {
771
+ const BYTE* srcLine =
772
+ &pSrcData[(y + nYSrc) * nSrcStep * srcVMultiplier + srcVOffset];
773
+ BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
774
+ memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
775
+ }
776
+ }
777
+ /* Copy up */
778
+ else if (nYDst > nYSrc)
779
+ {
780
+ for (SSIZE_T y = nHeight - 1; y >= 0; y--)
781
+ {
782
+ const BYTE* srcLine =
783
+ &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
784
+ BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
785
+ memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
786
+ }
787
+ }
788
+ /* Copy left */
789
+ else if (nXSrc > nXDst)
790
+ {
791
+ for (SSIZE_T y = 0; y < nHeight; y++)
792
+ {
793
+ const BYTE* srcLine =
794
+ &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
795
+ BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
796
+ memmove(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
797
+ }
798
+ }
799
+ /* Copy right */
800
+ else if (nXSrc < nXDst)
801
+ {
802
+ for (SSIZE_T y = nHeight - 1; y >= 0; y--)
803
+ {
804
+ const BYTE* srcLine =
805
+ &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
806
+ BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
807
+ memmove(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
808
+ }
809
+ }
810
+ /* Source and destination are equal... */
811
+ else
812
+ {
813
+ }
814
+ }
815
+ else
816
+ {
817
+ for (SSIZE_T y = 0; y < nHeight; y++)
818
+ {
819
+ const BYTE* srcLine = &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
820
+ BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
821
+
822
+ UINT32 color = FreeRDPReadColor_int(&srcLine[1ULL * nXSrc * srcByte], SrcFormat);
823
+ UINT32 oldColor = color;
824
+ UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
825
+ FreeRDPWriteColor_int(&dstLine[1ULL * nXDst * dstByte], DstFormat, dstColor);
826
+ for (SSIZE_T x = 1; x < nWidth; x++)
827
+ {
828
+ color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
829
+ if (color == oldColor)
830
+ {
831
+ FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor);
832
+ }
833
+ else
834
+ {
835
+ oldColor = color;
836
+ dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
837
+ FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor);
838
+ }
839
+ }
840
+ }
841
+ }
842
+
843
+ return TRUE;
844
+ }
845
+
846
+ BOOL freerdp_image_copy(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst,
847
+ UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* pSrcData,
848
+ DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
849
+ const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
850
+ {
851
+ if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
852
+ return FALSE;
853
+
854
+ if (!pDstData || !pSrcData)
855
+ return FALSE;
856
+
857
+ if ((nWidth == 0) || (nHeight == 0))
858
+ return TRUE;
859
+
860
+ if (nDstStep == 0)
861
+ nDstStep = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
862
+
863
+ if (nSrcStep == 0)
864
+ nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
865
+
866
+ const BOOL ovl = overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight);
867
+ if (ovl)
868
+ return freerdp_image_copy_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
869
+ nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
870
+ palette, flags);
871
+ return freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
872
+ nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
873
+ palette, flags);
874
+ }
875
+
876
+ BOOL freerdp_image_fill(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
877
+ UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 color)
878
+ {
879
+ if ((nWidth == 0) || (nHeight == 0))
880
+ return TRUE;
881
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
882
+ BYTE* WINPR_RESTRICT pFirstDstLine = NULL;
883
+ BYTE* WINPR_RESTRICT pFirstDstLineXOffset = NULL;
884
+
885
+ if (nDstStep == 0)
886
+ nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
887
+
888
+ pFirstDstLine = &pDstData[1ULL * nYDst * nDstStep];
889
+ pFirstDstLineXOffset = &pFirstDstLine[1ULL * nXDst * bpp];
890
+
891
+ for (size_t x = 0; x < nWidth; x++)
892
+ {
893
+ BYTE* pDst = &pFirstDstLine[(x + nXDst) * bpp];
894
+ FreeRDPWriteColor_int(pDst, DstFormat, color);
895
+ }
896
+
897
+ for (size_t y = 1; y < nHeight; y++)
898
+ {
899
+ BYTE* pDstLine = &pDstData[(y + nYDst) * nDstStep + 1ULL * nXDst * bpp];
900
+ memcpy(pDstLine, pFirstDstLineXOffset, 1ull * nWidth * bpp);
901
+ }
902
+
903
+ return TRUE;
904
+ }
905
+
906
+ #if defined(WITH_SWSCALE)
907
+ static int av_format_for_buffer(UINT32 format)
908
+ {
909
+ switch (format)
910
+ {
911
+ case PIXEL_FORMAT_ARGB32:
912
+ return AV_PIX_FMT_BGRA;
913
+
914
+ case PIXEL_FORMAT_XRGB32:
915
+ return AV_PIX_FMT_BGR0;
916
+
917
+ case PIXEL_FORMAT_BGRA32:
918
+ return AV_PIX_FMT_RGBA;
919
+
920
+ case PIXEL_FORMAT_BGRX32:
921
+ return AV_PIX_FMT_RGB0;
922
+
923
+ default:
924
+ return AV_PIX_FMT_NONE;
925
+ }
926
+ }
927
+ #endif
928
+
929
+ BOOL freerdp_image_scale(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
930
+ UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
931
+ const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
932
+ UINT32 nXSrc, UINT32 nYSrc, UINT32 nSrcWidth, UINT32 nSrcHeight)
933
+ {
934
+ BOOL rc = FALSE;
935
+
936
+ if (nDstStep == 0)
937
+ nDstStep = nDstWidth * FreeRDPGetBytesPerPixel(DstFormat);
938
+
939
+ if (nSrcStep == 0)
940
+ nSrcStep = nSrcWidth * FreeRDPGetBytesPerPixel(SrcFormat);
941
+
942
+ #if defined(WITH_SWSCALE) || defined(WITH_CAIRO)
943
+ const BYTE* src = &pSrcData[nXSrc * FreeRDPGetBytesPerPixel(SrcFormat) + nYSrc * nSrcStep];
944
+ BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep];
945
+ #endif
946
+
947
+ /* direct copy is much faster than scaling, so check if we can simply copy... */
948
+ if ((nDstWidth == nSrcWidth) && (nDstHeight == nSrcHeight))
949
+ {
950
+ return freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
951
+ nDstHeight, pSrcData, SrcFormat, nSrcStep, nXSrc,
952
+ nYSrc, NULL, FREERDP_FLIP_NONE);
953
+ }
954
+ else
955
+ #if defined(WITH_SWSCALE)
956
+ {
957
+ int res = 0;
958
+ struct SwsContext* resize = NULL;
959
+ int srcFormat = av_format_for_buffer(SrcFormat);
960
+ int dstFormat = av_format_for_buffer(DstFormat);
961
+ const int srcStep[1] = { (int)nSrcStep };
962
+ const int dstStep[1] = { (int)nDstStep };
963
+
964
+ if ((srcFormat == AV_PIX_FMT_NONE) || (dstFormat == AV_PIX_FMT_NONE))
965
+ return FALSE;
966
+
967
+ resize = sws_getContext((int)nSrcWidth, (int)nSrcHeight, srcFormat, (int)nDstWidth,
968
+ (int)nDstHeight, dstFormat, SWS_BILINEAR, NULL, NULL, NULL);
969
+
970
+ if (!resize)
971
+ goto fail;
972
+
973
+ res = sws_scale(resize, &src, srcStep, 0, (int)nSrcHeight, &dst, dstStep);
974
+ rc = (res == ((int)nDstHeight));
975
+ fail:
976
+ sws_freeContext(resize);
977
+ }
978
+
979
+ #elif defined(WITH_CAIRO)
980
+ {
981
+ const double sx = (double)nDstWidth / (double)nSrcWidth;
982
+ const double sy = (double)nDstHeight / (double)nSrcHeight;
983
+ cairo_t* cairo_context;
984
+ cairo_surface_t *csrc, *cdst;
985
+
986
+ if ((nSrcWidth > INT_MAX) || (nSrcHeight > INT_MAX) || (nSrcStep > INT_MAX))
987
+ return FALSE;
988
+
989
+ if ((nDstWidth > INT_MAX) || (nDstHeight > INT_MAX) || (nDstStep > INT_MAX))
990
+ return FALSE;
991
+
992
+ csrc = cairo_image_surface_create_for_data((void*)src, CAIRO_FORMAT_ARGB32, (int)nSrcWidth,
993
+ (int)nSrcHeight, (int)nSrcStep);
994
+ cdst = cairo_image_surface_create_for_data(dst, CAIRO_FORMAT_ARGB32, (int)nDstWidth,
995
+ (int)nDstHeight, (int)nDstStep);
996
+
997
+ if (!csrc || !cdst)
998
+ goto fail;
999
+
1000
+ cairo_context = cairo_create(cdst);
1001
+
1002
+ if (!cairo_context)
1003
+ goto fail2;
1004
+
1005
+ cairo_scale(cairo_context, sx, sy);
1006
+ cairo_set_operator(cairo_context, CAIRO_OPERATOR_SOURCE);
1007
+ cairo_set_source_surface(cairo_context, csrc, 0, 0);
1008
+ cairo_paint(cairo_context);
1009
+ rc = TRUE;
1010
+ fail2:
1011
+ cairo_destroy(cairo_context);
1012
+ fail:
1013
+ cairo_surface_destroy(csrc);
1014
+ cairo_surface_destroy(cdst);
1015
+ }
1016
+ #else
1017
+ {
1018
+ WLog_WARN(TAG, "SmartScaling requested but compiled without libcairo support!");
1019
+ }
1020
+ #endif
1021
+ return rc;
1022
+ }
1023
+
1024
+ DWORD FreeRDPAreColorFormatsEqualNoAlpha(DWORD first, DWORD second)
1025
+ {
1026
+ return FreeRDPAreColorFormatsEqualNoAlpha_int(first, second);
1027
+ }
1028
+
1029
+ const char* FreeRDPGetColorFormatName(UINT32 format)
1030
+ {
1031
+ switch (format)
1032
+ {
1033
+ /* 32bpp formats */
1034
+ case PIXEL_FORMAT_ARGB32:
1035
+ return "PIXEL_FORMAT_ARGB32";
1036
+
1037
+ case PIXEL_FORMAT_XRGB32:
1038
+ return "PIXEL_FORMAT_XRGB32";
1039
+
1040
+ case PIXEL_FORMAT_ABGR32:
1041
+ return "PIXEL_FORMAT_ABGR32";
1042
+
1043
+ case PIXEL_FORMAT_XBGR32:
1044
+ return "PIXEL_FORMAT_XBGR32";
1045
+
1046
+ case PIXEL_FORMAT_BGRA32:
1047
+ return "PIXEL_FORMAT_BGRA32";
1048
+
1049
+ case PIXEL_FORMAT_BGRX32:
1050
+ return "PIXEL_FORMAT_BGRX32";
1051
+
1052
+ case PIXEL_FORMAT_RGBA32:
1053
+ return "PIXEL_FORMAT_RGBA32";
1054
+
1055
+ case PIXEL_FORMAT_RGBX32:
1056
+ return "PIXEL_FORMAT_RGBX32";
1057
+
1058
+ case PIXEL_FORMAT_BGRX32_DEPTH30:
1059
+ return "PIXEL_FORMAT_BGRX32_DEPTH30";
1060
+
1061
+ case PIXEL_FORMAT_RGBX32_DEPTH30:
1062
+ return "PIXEL_FORMAT_RGBX32_DEPTH30";
1063
+
1064
+ /* 24bpp formats */
1065
+ case PIXEL_FORMAT_RGB24:
1066
+ return "PIXEL_FORMAT_RGB24";
1067
+
1068
+ case PIXEL_FORMAT_BGR24:
1069
+ return "PIXEL_FORMAT_BGR24";
1070
+
1071
+ /* 16bpp formats */
1072
+ case PIXEL_FORMAT_RGB16:
1073
+ return "PIXEL_FORMAT_RGB16";
1074
+
1075
+ case PIXEL_FORMAT_BGR16:
1076
+ return "PIXEL_FORMAT_BGR16";
1077
+
1078
+ case PIXEL_FORMAT_ARGB15:
1079
+ return "PIXEL_FORMAT_ARGB15";
1080
+
1081
+ case PIXEL_FORMAT_RGB15:
1082
+ return "PIXEL_FORMAT_RGB15";
1083
+
1084
+ case PIXEL_FORMAT_ABGR15:
1085
+ return "PIXEL_FORMAT_ABGR15";
1086
+
1087
+ case PIXEL_FORMAT_BGR15:
1088
+ return "PIXEL_FORMAT_BGR15";
1089
+
1090
+ /* 8bpp formats */
1091
+ case PIXEL_FORMAT_RGB8:
1092
+ return "PIXEL_FORMAT_RGB8";
1093
+
1094
+ /* 4 bpp formats */
1095
+ case PIXEL_FORMAT_A4:
1096
+ return "PIXEL_FORMAT_A4";
1097
+
1098
+ /* 1bpp formats */
1099
+ case PIXEL_FORMAT_MONO:
1100
+ return "PIXEL_FORMAT_MONO";
1101
+
1102
+ default:
1103
+ return "UNKNOWN";
1104
+ }
1105
+ }
1106
+
1107
+ void FreeRDPSplitColor(UINT32 color, UINT32 format, BYTE* _r, BYTE* _g, BYTE* _b, BYTE* _a,
1108
+ const gdiPalette* palette)
1109
+ {
1110
+ UINT32 tmp = 0;
1111
+
1112
+ switch (format)
1113
+ {
1114
+ /* 32bpp formats */
1115
+ case PIXEL_FORMAT_ARGB32:
1116
+ if (_a)
1117
+ *_a = (BYTE)(color >> 24);
1118
+
1119
+ if (_r)
1120
+ *_r = (BYTE)(color >> 16);
1121
+
1122
+ if (_g)
1123
+ *_g = (BYTE)(color >> 8);
1124
+
1125
+ if (_b)
1126
+ *_b = (BYTE)color;
1127
+
1128
+ break;
1129
+
1130
+ case PIXEL_FORMAT_XRGB32:
1131
+ if (_r)
1132
+ *_r = (BYTE)(color >> 16);
1133
+
1134
+ if (_g)
1135
+ *_g = (BYTE)(color >> 8);
1136
+
1137
+ if (_b)
1138
+ *_b = (BYTE)color;
1139
+
1140
+ if (_a)
1141
+ *_a = 0xFF;
1142
+
1143
+ break;
1144
+
1145
+ case PIXEL_FORMAT_ABGR32:
1146
+ if (_a)
1147
+ *_a = (BYTE)(color >> 24);
1148
+
1149
+ if (_b)
1150
+ *_b = (BYTE)(color >> 16);
1151
+
1152
+ if (_g)
1153
+ *_g = (BYTE)(color >> 8);
1154
+
1155
+ if (_r)
1156
+ *_r = (BYTE)color;
1157
+
1158
+ break;
1159
+
1160
+ case PIXEL_FORMAT_XBGR32:
1161
+ if (_b)
1162
+ *_b = (BYTE)(color >> 16);
1163
+
1164
+ if (_g)
1165
+ *_g = (BYTE)(color >> 8);
1166
+
1167
+ if (_r)
1168
+ *_r = (BYTE)color;
1169
+
1170
+ if (_a)
1171
+ *_a = 0xFF;
1172
+
1173
+ break;
1174
+
1175
+ case PIXEL_FORMAT_RGBA32:
1176
+ if (_r)
1177
+ *_r = (BYTE)(color >> 24);
1178
+
1179
+ if (_g)
1180
+ *_g = (BYTE)(color >> 16);
1181
+
1182
+ if (_b)
1183
+ *_b = (BYTE)(color >> 8);
1184
+
1185
+ if (_a)
1186
+ *_a = (BYTE)color;
1187
+
1188
+ break;
1189
+
1190
+ case PIXEL_FORMAT_RGBX32:
1191
+ if (_r)
1192
+ *_r = (BYTE)(color >> 24);
1193
+
1194
+ if (_g)
1195
+ *_g = (BYTE)(color >> 16);
1196
+
1197
+ if (_b)
1198
+ *_b = (BYTE)(color >> 8);
1199
+
1200
+ if (_a)
1201
+ *_a = 0xFF;
1202
+
1203
+ break;
1204
+
1205
+ case PIXEL_FORMAT_BGRA32:
1206
+ if (_b)
1207
+ *_b = (BYTE)(color >> 24);
1208
+
1209
+ if (_g)
1210
+ *_g = (BYTE)(color >> 16);
1211
+
1212
+ if (_r)
1213
+ *_r = (BYTE)(color >> 8);
1214
+
1215
+ if (_a)
1216
+ *_a = (BYTE)color;
1217
+
1218
+ break;
1219
+
1220
+ case PIXEL_FORMAT_BGRX32:
1221
+ if (_b)
1222
+ *_b = (BYTE)(color >> 24);
1223
+
1224
+ if (_g)
1225
+ *_g = (BYTE)(color >> 16);
1226
+
1227
+ if (_r)
1228
+ *_r = (BYTE)(color >> 8);
1229
+
1230
+ if (_a)
1231
+ *_a = 0xFF;
1232
+
1233
+ break;
1234
+
1235
+ /* 24bpp formats */
1236
+ case PIXEL_FORMAT_RGB24:
1237
+ if (_r)
1238
+ *_r = (BYTE)(color >> 16);
1239
+
1240
+ if (_g)
1241
+ *_g = (BYTE)(color >> 8);
1242
+
1243
+ if (_b)
1244
+ *_b = (BYTE)color;
1245
+
1246
+ if (_a)
1247
+ *_a = 0xFF;
1248
+
1249
+ break;
1250
+
1251
+ case PIXEL_FORMAT_BGR24:
1252
+ if (_b)
1253
+ *_b = (BYTE)(color >> 16);
1254
+
1255
+ if (_g)
1256
+ *_g = (BYTE)(color >> 8);
1257
+
1258
+ if (_r)
1259
+ *_r = (BYTE)color;
1260
+
1261
+ if (_a)
1262
+ *_a = 0xFF;
1263
+
1264
+ break;
1265
+
1266
+ /* 16bpp formats */
1267
+ case PIXEL_FORMAT_RGB16:
1268
+ if (_r)
1269
+ {
1270
+ const UINT32 c = (color >> 11) & 0x1F;
1271
+ const UINT32 val = (c << 3) + c / 4;
1272
+ *_r = (BYTE)(val > 255 ? 255 : val);
1273
+ }
1274
+
1275
+ if (_g)
1276
+ {
1277
+ const UINT32 c = (color >> 5) & 0x3F;
1278
+ const UINT32 val = (c << 2) + c / 4 / 2;
1279
+ *_g = (BYTE)(val > 255 ? 255 : val);
1280
+ }
1281
+
1282
+ if (_b)
1283
+ {
1284
+ const UINT32 c = (color)&0x1F;
1285
+ const UINT32 val = (c << 3) + c / 4;
1286
+ *_b = (BYTE)(val > 255 ? 255 : val);
1287
+ }
1288
+
1289
+ if (_a)
1290
+ *_a = 0xFF;
1291
+
1292
+ break;
1293
+
1294
+ case PIXEL_FORMAT_BGR16:
1295
+ if (_r)
1296
+ {
1297
+ const UINT32 c = (color)&0x1F;
1298
+ const UINT32 val = (c << 3) + c / 4;
1299
+ *_r = (BYTE)(val > 255 ? 255 : val);
1300
+ }
1301
+
1302
+ if (_g)
1303
+ {
1304
+ const UINT32 c = (color >> 5) & 0x3F;
1305
+ const UINT32 val = (c << 2) + c / 4 / 2;
1306
+ *_g = (BYTE)(val > 255 ? 255 : val);
1307
+ }
1308
+
1309
+ if (_b)
1310
+ {
1311
+ const UINT32 c = (color >> 11) & 0x1F;
1312
+ const UINT32 val = (c << 3) + c / 4;
1313
+ *_b = (BYTE)(val > 255 ? 255 : val);
1314
+ }
1315
+
1316
+ if (_a)
1317
+ *_a = 0xFF;
1318
+
1319
+ break;
1320
+
1321
+ case PIXEL_FORMAT_ARGB15:
1322
+ if (_r)
1323
+ {
1324
+ const UINT32 c = (color >> 10) & 0x1F;
1325
+ const UINT32 val = (c << 3) + c / 4;
1326
+ *_r = (BYTE)(val > 255 ? 255 : val);
1327
+ }
1328
+
1329
+ if (_g)
1330
+ {
1331
+ const UINT32 c = (color >> 5) & 0x1F;
1332
+ const UINT32 val = (c << 3) + c / 4;
1333
+ *_g = (BYTE)(val > 255 ? 255 : val);
1334
+ }
1335
+
1336
+ if (_b)
1337
+ {
1338
+ const UINT32 c = (color)&0x1F;
1339
+ const UINT32 val = (c << 3) + c / 4;
1340
+ *_b = (BYTE)(val > 255 ? 255 : val);
1341
+ }
1342
+
1343
+ if (_a)
1344
+ *_a = color & 0x8000 ? 0xFF : 0x00;
1345
+
1346
+ break;
1347
+
1348
+ case PIXEL_FORMAT_ABGR15:
1349
+ if (_r)
1350
+ {
1351
+ const UINT32 c = (color)&0x1F;
1352
+ const UINT32 val = (c << 3) + c / 4;
1353
+ *_r = (BYTE)(val > 255 ? 255 : val);
1354
+ }
1355
+
1356
+ if (_g)
1357
+ {
1358
+ const UINT32 c = (color >> 5) & 0x1F;
1359
+ const UINT32 val = (c << 3) + c / 4;
1360
+ *_g = (BYTE)(val > 255 ? 255 : val);
1361
+ }
1362
+
1363
+ if (_b)
1364
+ {
1365
+ const UINT32 c = (color >> 10) & 0x1F;
1366
+ const UINT32 val = (c << 3) + c / 4;
1367
+ *_b = (BYTE)(val > 255 ? 255 : val);
1368
+ }
1369
+
1370
+ if (_a)
1371
+ *_a = color & 0x8000 ? 0xFF : 0x00;
1372
+
1373
+ break;
1374
+
1375
+ /* 15bpp formats */
1376
+ case PIXEL_FORMAT_RGB15:
1377
+ if (_r)
1378
+ {
1379
+ const UINT32 c = (color >> 10) & 0x1F;
1380
+ const UINT32 val = (c << 3) + c / 4;
1381
+ *_r = (BYTE)(val > 255 ? 255 : val);
1382
+ }
1383
+
1384
+ if (_g)
1385
+ {
1386
+ const UINT32 c = (color >> 5) & 0x1F;
1387
+ const UINT32 val = (c << 3) + c / 4;
1388
+ *_g = (BYTE)(val > 255 ? 255 : val);
1389
+ }
1390
+
1391
+ if (_b)
1392
+ {
1393
+ const UINT32 c = (color)&0x1F;
1394
+ const UINT32 val = (c << 3) + c / 4;
1395
+ *_b = (BYTE)(val > 255 ? 255 : val);
1396
+ }
1397
+
1398
+ if (_a)
1399
+ *_a = 0xFF;
1400
+
1401
+ break;
1402
+
1403
+ case PIXEL_FORMAT_BGR15:
1404
+ if (_r)
1405
+ {
1406
+ const UINT32 c = (color)&0x1F;
1407
+ const UINT32 val = (c << 3) + c / 4;
1408
+ *_r = (BYTE)(val > 255 ? 255 : val);
1409
+ }
1410
+
1411
+ if (_g)
1412
+ {
1413
+ const UINT32 c = (color >> 5) & 0x1F;
1414
+ const UINT32 val = (c << 3) + c / 4;
1415
+ *_g = (BYTE)(val > 255 ? 255 : val);
1416
+ }
1417
+
1418
+ if (_b)
1419
+ {
1420
+ const UINT32 c = (color >> 10) & 0x1F;
1421
+ const UINT32 val = (c << 3) + c / 4;
1422
+ *_b = (BYTE)(val > 255 ? 255 : val);
1423
+ }
1424
+
1425
+ if (_a)
1426
+ *_a = 0xFF;
1427
+
1428
+ break;
1429
+
1430
+ /* 8bpp formats */
1431
+ case PIXEL_FORMAT_RGB8:
1432
+ if (color <= 0xFF)
1433
+ {
1434
+ tmp = palette->palette[color];
1435
+ FreeRDPSplitColor(tmp, palette->format, _r, _g, _b, _a, NULL);
1436
+ }
1437
+ else
1438
+ {
1439
+ if (_r)
1440
+ *_r = 0x00;
1441
+
1442
+ if (_g)
1443
+ *_g = 0x00;
1444
+
1445
+ if (_b)
1446
+ *_b = 0x00;
1447
+
1448
+ if (_a)
1449
+ *_a = 0x00;
1450
+ }
1451
+
1452
+ break;
1453
+
1454
+ /* 1bpp formats */
1455
+ case PIXEL_FORMAT_MONO:
1456
+ if (_r)
1457
+ *_r = (color) ? 0xFF : 0x00;
1458
+
1459
+ if (_g)
1460
+ *_g = (color) ? 0xFF : 0x00;
1461
+
1462
+ if (_b)
1463
+ *_b = (color) ? 0xFF : 0x00;
1464
+
1465
+ if (_a)
1466
+ *_a = (color) ? 0xFF : 0x00;
1467
+
1468
+ break;
1469
+
1470
+ /* 4 bpp formats */
1471
+ case PIXEL_FORMAT_A4:
1472
+ default:
1473
+ if (_r)
1474
+ *_r = 0x00;
1475
+
1476
+ if (_g)
1477
+ *_g = 0x00;
1478
+
1479
+ if (_b)
1480
+ *_b = 0x00;
1481
+
1482
+ if (_a)
1483
+ *_a = 0x00;
1484
+
1485
+ WLog_ERR(TAG, "Unsupported format %s", FreeRDPGetColorFormatName(format));
1486
+ break;
1487
+ }
1488
+ }
1489
+
1490
+ BOOL FreeRDPWriteColorIgnoreAlpha(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1491
+ {
1492
+ return FreeRDPWriteColorIgnoreAlpha_int(dst, format, color);
1493
+ }
1494
+
1495
+ BOOL FreeRDPWriteColor(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1496
+ {
1497
+ return FreeRDPWriteColor_int(dst, format, color);
1498
+ }
1499
+
1500
+ UINT32 FreeRDPReadColor(const BYTE* WINPR_RESTRICT src, UINT32 format)
1501
+ {
1502
+ return FreeRDPReadColor_int(src, format);
1503
+ }
1504
+
1505
+ UINT32 FreeRDPGetColor(UINT32 format, BYTE r, BYTE g, BYTE b, BYTE a)
1506
+ {
1507
+ UINT32 _r = r;
1508
+ UINT32 _g = g;
1509
+ UINT32 _b = b;
1510
+ UINT32 _a = a;
1511
+ UINT32 t = 0;
1512
+
1513
+ switch (format)
1514
+ {
1515
+ /* 32bpp formats */
1516
+ case PIXEL_FORMAT_ARGB32:
1517
+ return (_a << 24) | (_r << 16) | (_g << 8) | _b;
1518
+
1519
+ case PIXEL_FORMAT_XRGB32:
1520
+ return (_r << 16) | (_g << 8) | _b;
1521
+
1522
+ case PIXEL_FORMAT_ABGR32:
1523
+ return (_a << 24) | (_b << 16) | (_g << 8) | _r;
1524
+
1525
+ case PIXEL_FORMAT_XBGR32:
1526
+ return (_b << 16) | (_g << 8) | _r;
1527
+
1528
+ case PIXEL_FORMAT_RGBA32:
1529
+ return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1530
+
1531
+ case PIXEL_FORMAT_RGBX32:
1532
+ return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1533
+
1534
+ case PIXEL_FORMAT_BGRA32:
1535
+ return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1536
+
1537
+ case PIXEL_FORMAT_BGRX32:
1538
+ return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1539
+
1540
+ case PIXEL_FORMAT_RGBX32_DEPTH30:
1541
+ // TODO: Not tested
1542
+ t = (_r << 22) | (_g << 12) | (_b << 2);
1543
+ // NOTE: Swapping byte-order because FreeRDPWriteColor written UINT32 in big-endian
1544
+ return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1545
+ (t >> 24);
1546
+
1547
+ case PIXEL_FORMAT_BGRX32_DEPTH30:
1548
+ // NOTE: Swapping b and r channel (unknown reason)
1549
+ t = (_r << 22) | (_g << 12) | (_b << 2);
1550
+ // NOTE: Swapping byte-order because FreeRDPWriteColor written UINT32 in big-endian
1551
+ return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1552
+ (t >> 24);
1553
+
1554
+ /* 24bpp formats */
1555
+ case PIXEL_FORMAT_RGB24:
1556
+ return (_r << 16) | (_g << 8) | _b;
1557
+
1558
+ case PIXEL_FORMAT_BGR24:
1559
+ return (_b << 16) | (_g << 8) | _r;
1560
+
1561
+ /* 16bpp formats */
1562
+ case PIXEL_FORMAT_RGB16:
1563
+ return (((_r >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_b >> 3) & 0x1F);
1564
+
1565
+ case PIXEL_FORMAT_BGR16:
1566
+ return (((_b >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_r >> 3) & 0x1F);
1567
+
1568
+ case PIXEL_FORMAT_ARGB15:
1569
+ return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F) |
1570
+ (_a ? 0x8000 : 0x0000);
1571
+
1572
+ case PIXEL_FORMAT_ABGR15:
1573
+ return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F) |
1574
+ (_a ? 0x8000 : 0x0000);
1575
+
1576
+ /* 15bpp formats */
1577
+ case PIXEL_FORMAT_RGB15:
1578
+ return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F);
1579
+
1580
+ case PIXEL_FORMAT_BGR15:
1581
+ return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F);
1582
+
1583
+ /* 8bpp formats */
1584
+ case PIXEL_FORMAT_RGB8:
1585
+
1586
+ /* 4 bpp formats */
1587
+ case PIXEL_FORMAT_A4:
1588
+
1589
+ /* 1bpp formats */
1590
+ case PIXEL_FORMAT_MONO:
1591
+ default:
1592
+ WLog_ERR(TAG, "Unsupported format %s", FreeRDPGetColorFormatName(format));
1593
+ return 0;
1594
+ }
1595
+ }
1596
+
1597
+ BOOL freerdp_image_copy_no_overlap(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1598
+ UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
1599
+ const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
1600
+ UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
1601
+ const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
1602
+ {
1603
+ static primitives_t* prims = NULL;
1604
+ if (!prims)
1605
+ prims = primitives_get();
1606
+
1607
+ WINPR_ASSERT(!overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight));
1608
+ WINPR_ASSERT(prims);
1609
+ WINPR_ASSERT(prims->copy_no_overlap);
1610
+ return prims->copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
1611
+ pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
1612
+ flags) == PRIMITIVES_SUCCESS;
1613
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/dsp_fdk_impl.h ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Digital Sound Processing
4
+ *
5
+ * Copyright 2022 Armin Novak <anovak@thincast.com>
6
+ * Copyright 2022 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_DSP_FDK_IMPL_H_
22
+ #define FREERDP_DSP_FDK_IMPL_H_
23
+
24
+ #include <stdlib.h>
25
+
26
+ typedef void (*fdk_log_fkt_t)(unsigned log_level, const char* fmt, ...);
27
+
28
+ int fdk_aac_dsp_impl_init(void** handle, int encoder, fdk_log_fkt_t log);
29
+ void fdk_aac_dsp_impl_uninit(void** handle, int encoder, fdk_log_fkt_t log);
30
+
31
+ ssize_t fdk_aac_dsp_impl_stream_info(void* handle, int encoder, fdk_log_fkt_t log);
32
+
33
+ int fdk_aac_dsp_impl_config(void* handle, size_t* pbuffersize, int encoder, unsigned samplerate,
34
+ unsigned channels, unsigned bytes_per_second,
35
+ unsigned frames_per_packet, fdk_log_fkt_t log);
36
+
37
+ ssize_t fdk_aac_dsp_impl_decode_fill(void* handle, const void* data, size_t size,
38
+ fdk_log_fkt_t log);
39
+
40
+ ssize_t fdk_aac_dsp_impl_encode(void* handle, const void* data, size_t size, void* dst,
41
+ size_t dstSize, fdk_log_fkt_t log);
42
+
43
+ ssize_t fdk_aac_dsp_impl_decode_read(void* handle, void* dst, size_t dstSize, fdk_log_fkt_t log);
44
+
45
+ #endif
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/dsp_ffmpeg.h ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Digital Sound Processing - FFMPEG backend
4
+ *
5
+ * Copyright 2018 Armin Novak <armin.novak@thincast.com>
6
+ * Copyright 2018 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LIB_CODEC_DSP_FFMPEG_H
22
+ #define FREERDP_LIB_CODEC_DSP_FFMPEG_H
23
+
24
+ #include <freerdp/api.h>
25
+ #include <freerdp/codec/audio.h>
26
+ #include <freerdp/codec/dsp.h>
27
+
28
+ #include <libavcodec/version.h>
29
+
30
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
31
+ #error \
32
+ "DSP module requires libavcodec version >= 57.48.101. Upgrade or set WITH_DSP_FFMPEG=OFF to continue"
33
+ #endif
34
+
35
+ void freerdp_dsp_ffmpeg_context_free(FREERDP_DSP_CONTEXT* context);
36
+
37
+ WINPR_ATTR_MALLOC(freerdp_dsp_ffmpeg_context_free, 1)
38
+ FREERDP_DSP_CONTEXT* freerdp_dsp_ffmpeg_context_new(BOOL encode);
39
+ BOOL freerdp_dsp_ffmpeg_supports_format(const AUDIO_FORMAT* format, BOOL encode);
40
+ BOOL freerdp_dsp_ffmpeg_encode(FREERDP_DSP_CONTEXT* context, const AUDIO_FORMAT* srcFormat,
41
+ const BYTE* data, size_t length, wStream* out);
42
+ BOOL freerdp_dsp_ffmpeg_decode(FREERDP_DSP_CONTEXT* context, const AUDIO_FORMAT* srcFormat,
43
+ const BYTE* data, size_t length, wStream* out);
44
+
45
+ BOOL freerdp_dsp_ffmpeg_context_reset(FREERDP_DSP_CONTEXT* context,
46
+ const AUDIO_FORMAT* targetFormat);
47
+
48
+ #endif /* FREERDP_LIB_CODEC_DSP_FFMPEG_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264.c ADDED
@@ -0,0 +1,837 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * H.264 Bitmap Compression
4
+ *
5
+ * Copyright 2014 Mike McDonald <Mike.McDonald@software.dell.com>
6
+ * Copyright 2017 David Fort <contact@hardening-consulting.com>
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #include <freerdp/config.h>
22
+
23
+ #include <winpr/crt.h>
24
+ #include <winpr/print.h>
25
+ #include <winpr/library.h>
26
+ #include <winpr/bitstream.h>
27
+ #include <winpr/synch.h>
28
+
29
+ #include <freerdp/primitives.h>
30
+ #include <freerdp/codec/h264.h>
31
+ #include <freerdp/codec/yuv.h>
32
+ #include <freerdp/log.h>
33
+
34
+ #include "h264.h"
35
+
36
+ #define TAG FREERDP_TAG("codec")
37
+
38
+ static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight);
39
+
40
+ static BOOL yuv_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
41
+ {
42
+ BOOL isNull = FALSE;
43
+ UINT32 pheight = height;
44
+
45
+ if (!h264)
46
+ return FALSE;
47
+
48
+ if (stride == 0)
49
+ stride = width;
50
+
51
+ if (stride % 16 != 0)
52
+ stride += 16 - stride % 16;
53
+
54
+ if (pheight % 16 != 0)
55
+ pheight += 16 - pheight % 16;
56
+
57
+ const size_t nPlanes = h264->hwAccel ? 2 : 3;
58
+
59
+ for (size_t x = 0; x < nPlanes; x++)
60
+ {
61
+ if (!h264->pYUVData[x] || !h264->pOldYUVData[x])
62
+ isNull = TRUE;
63
+ }
64
+
65
+ if (pheight == 0)
66
+ return FALSE;
67
+ if (stride == 0)
68
+ return FALSE;
69
+
70
+ if (isNull || (width != h264->width) || (height != h264->height) ||
71
+ (stride != h264->iStride[0]))
72
+ {
73
+ if (h264->hwAccel) /* NV12 */
74
+ {
75
+ h264->iStride[0] = stride;
76
+ h264->iStride[1] = stride;
77
+ h264->iStride[2] = 0;
78
+ }
79
+ else /* I420 */
80
+ {
81
+ h264->iStride[0] = stride;
82
+ h264->iStride[1] = (stride + 1) / 2;
83
+ h264->iStride[2] = (stride + 1) / 2;
84
+ }
85
+
86
+ h264->width = width;
87
+ h264->height = height;
88
+
89
+ for (size_t x = 0; x < nPlanes; x++)
90
+ {
91
+ BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], h264->iStride[x], pheight, 16);
92
+ BYTE* tmp2 =
93
+ winpr_aligned_recalloc(h264->pOldYUVData[x], h264->iStride[x], pheight, 16);
94
+ if (tmp1)
95
+ h264->pYUVData[x] = tmp1;
96
+ if (tmp2)
97
+ h264->pOldYUVData[x] = tmp2;
98
+ if (!tmp1 || !tmp2)
99
+ return FALSE;
100
+ }
101
+ }
102
+
103
+ return TRUE;
104
+ }
105
+
106
+ BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
107
+ {
108
+ return yuv_ensure_buffer(h264, stride, width, height);
109
+ }
110
+
111
+ INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
112
+ DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
113
+ const RECTANGLE_16* regionRects, UINT32 numRegionRects)
114
+ {
115
+ int status = 0;
116
+ const BYTE* pYUVData[3];
117
+
118
+ if (!h264 || h264->Compressor)
119
+ return -1001;
120
+
121
+ status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
122
+
123
+ if (status == 0)
124
+ return 1;
125
+
126
+ if (status < 0)
127
+ return status;
128
+
129
+ pYUVData[0] = h264->pYUVData[0];
130
+ pYUVData[1] = h264->pYUVData[1];
131
+ pYUVData[2] = h264->pYUVData[2];
132
+ if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->height, DstFormat,
133
+ pDstData, nDstStep, regionRects, numRegionRects))
134
+ return -1002;
135
+
136
+ return 1;
137
+ }
138
+
139
+ static BOOL allocate_h264_metablock(UINT32 QP, RECTANGLE_16* rectangles,
140
+ RDPGFX_H264_METABLOCK* meta, size_t count)
141
+ {
142
+ /* [MS-RDPEGFX] 2.2.4.4.2 RDPGFX_AVC420_QUANT_QUALITY */
143
+ if (!meta || (QP > UINT8_MAX))
144
+ {
145
+ free(rectangles);
146
+ return FALSE;
147
+ }
148
+
149
+ meta->regionRects = rectangles;
150
+ if (count == 0)
151
+ return TRUE;
152
+
153
+ if (count > UINT32_MAX)
154
+ return FALSE;
155
+
156
+ meta->quantQualityVals = calloc(count, sizeof(RDPGFX_H264_QUANT_QUALITY));
157
+
158
+ if (!meta->quantQualityVals || !meta->regionRects)
159
+ return FALSE;
160
+ meta->numRegionRects = (UINT32)count;
161
+ for (size_t x = 0; x < count; x++)
162
+ {
163
+ RDPGFX_H264_QUANT_QUALITY* cur = &meta->quantQualityVals[x];
164
+ cur->qp = (UINT8)QP;
165
+
166
+ /* qpVal bit 6 and 7 are flags, so mask them out here.
167
+ * qualityVal is [0-100] so 100 - qpVal [0-64] is always in range */
168
+ cur->qualityVal = 100 - (QP & 0x3F);
169
+ }
170
+ return TRUE;
171
+ }
172
+
173
+ static INLINE BOOL diff_tile(const RECTANGLE_16* regionRect, BYTE* pYUVData[3],
174
+ BYTE* pOldYUVData[3], UINT32 const iStride[3])
175
+ {
176
+ size_t size = 0;
177
+
178
+ if (!regionRect || !pYUVData || !pOldYUVData || !iStride)
179
+ return FALSE;
180
+ size = regionRect->right - regionRect->left;
181
+ if (regionRect->right > iStride[0])
182
+ return FALSE;
183
+ if (regionRect->right / 2u > iStride[1])
184
+ return FALSE;
185
+ if (regionRect->right / 2u > iStride[2])
186
+ return FALSE;
187
+
188
+ for (size_t y = regionRect->top; y < regionRect->bottom; y++)
189
+ {
190
+ const BYTE* cur0 = &pYUVData[0][y * iStride[0]];
191
+ const BYTE* cur1 = &pYUVData[1][y * iStride[1]];
192
+ const BYTE* cur2 = &pYUVData[2][y * iStride[2]];
193
+ const BYTE* old0 = &pOldYUVData[0][y * iStride[0]];
194
+ const BYTE* old1 = &pOldYUVData[1][y * iStride[1]];
195
+ const BYTE* old2 = &pOldYUVData[2][y * iStride[2]];
196
+
197
+ if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0)
198
+ return TRUE;
199
+ if (memcmp(&cur1[regionRect->left / 2], &old1[regionRect->left / 2], size / 2) != 0)
200
+ return TRUE;
201
+ if (memcmp(&cur2[regionRect->left / 2], &old2[regionRect->left / 2], size / 2) != 0)
202
+ return TRUE;
203
+ }
204
+ return FALSE;
205
+ }
206
+
207
+ static BOOL detect_changes(BOOL firstFrameDone, const UINT32 QP, const RECTANGLE_16* regionRect,
208
+ BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32 const iStride[3],
209
+ RDPGFX_H264_METABLOCK* meta)
210
+ {
211
+ size_t count = 0;
212
+ size_t wc = 0;
213
+ size_t hc = 0;
214
+ RECTANGLE_16* rectangles = NULL;
215
+
216
+ if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta)
217
+ return FALSE;
218
+
219
+ wc = (regionRect->right - regionRect->left) / 64 + 1;
220
+ hc = (regionRect->bottom - regionRect->top) / 64 + 1;
221
+ rectangles = calloc(wc * hc, sizeof(RECTANGLE_16));
222
+ if (!rectangles)
223
+ return FALSE;
224
+ if (!firstFrameDone)
225
+ {
226
+ rectangles[0] = *regionRect;
227
+ count = 1;
228
+ }
229
+ else
230
+ {
231
+ for (size_t y = regionRect->top; y < regionRect->bottom; y += 64)
232
+ {
233
+ for (size_t x = regionRect->left; x < regionRect->right; x += 64)
234
+ {
235
+ RECTANGLE_16 rect;
236
+ rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x);
237
+ rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y);
238
+ rect.right =
239
+ (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right));
240
+ rect.bottom =
241
+ (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom));
242
+ if (diff_tile(&rect, pYUVData, pOldYUVData, iStride))
243
+ rectangles[count++] = rect;
244
+ }
245
+ }
246
+ }
247
+ if (!allocate_h264_metablock(QP, rectangles, meta, count))
248
+ return FALSE;
249
+ return TRUE;
250
+ }
251
+
252
+ INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth,
253
+ UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3])
254
+ {
255
+ if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
256
+ return -1;
257
+
258
+ if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight))
259
+ return -1;
260
+
261
+ for (size_t x = 0; x < 3; x++)
262
+ {
263
+ YUVData[x] = h264->pYUVData[x];
264
+ stride[x] = h264->iStride[x];
265
+ }
266
+
267
+ return 0;
268
+ }
269
+
270
+ INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
271
+ {
272
+ if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
273
+ return -1;
274
+
275
+ const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] };
276
+
277
+ return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
278
+ }
279
+
280
+ INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
281
+ UINT32 nSrcWidth, UINT32 nSrcHeight, const RECTANGLE_16* regionRect,
282
+ BYTE** ppDstData, UINT32* pDstSize, RDPGFX_H264_METABLOCK* meta)
283
+ {
284
+ INT32 rc = -1;
285
+ BYTE* pYUVData[3] = { 0 };
286
+ const BYTE* pcYUVData[3] = { 0 };
287
+ BYTE* pOldYUVData[3] = { 0 };
288
+
289
+ if (!h264 || !regionRect || !meta || !h264->Compressor)
290
+ return -1;
291
+
292
+ if (!h264->subsystem->Compress)
293
+ return -1;
294
+
295
+ if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
296
+ return -1;
297
+
298
+ if (h264->encodingBuffer)
299
+ {
300
+ for (size_t x = 0; x < 3; x++)
301
+ {
302
+ pYUVData[x] = h264->pYUVData[x];
303
+ pOldYUVData[x] = h264->pOldYUVData[x];
304
+ }
305
+ }
306
+ else
307
+ {
308
+ for (size_t x = 0; x < 3; x++)
309
+ {
310
+ pYUVData[x] = h264->pOldYUVData[x];
311
+ pOldYUVData[x] = h264->pYUVData[x];
312
+ }
313
+ }
314
+ h264->encodingBuffer = !h264->encodingBuffer;
315
+
316
+ if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData,
317
+ regionRect, 1))
318
+ goto fail;
319
+
320
+ if (!detect_changes(h264->firstLumaFrameDone, h264->QP, regionRect, pYUVData, pOldYUVData,
321
+ h264->iStride, meta))
322
+ goto fail;
323
+
324
+ if (meta->numRegionRects == 0)
325
+ {
326
+ rc = 0;
327
+ goto fail;
328
+ }
329
+
330
+ for (size_t x = 0; x < 3; x++)
331
+ pcYUVData[x] = pYUVData[x];
332
+
333
+ rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
334
+ if (rc >= 0)
335
+ h264->firstLumaFrameDone = TRUE;
336
+
337
+ fail:
338
+ if (rc < 0)
339
+ free_h264_metablock(meta);
340
+ return rc;
341
+ }
342
+
343
+ INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
344
+ UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version, const RECTANGLE_16* region,
345
+ BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData,
346
+ UINT32* pAuxDstSize, RDPGFX_H264_METABLOCK* meta,
347
+ RDPGFX_H264_METABLOCK* auxMeta)
348
+ {
349
+ int rc = -1;
350
+ BYTE* coded = NULL;
351
+ UINT32 codedSize = 0;
352
+ BYTE** pYUV444Data = NULL;
353
+ BYTE** pOldYUV444Data = NULL;
354
+ BYTE** pYUVData = NULL;
355
+ BYTE** pOldYUVData = NULL;
356
+
357
+ if (!h264 || !h264->Compressor)
358
+ return -1;
359
+
360
+ if (!h264->subsystem->Compress)
361
+ return -1;
362
+
363
+ if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
364
+ return -1;
365
+
366
+ if (!avc444_ensure_buffer(h264, nSrcHeight))
367
+ return -1;
368
+
369
+ if (h264->encodingBuffer)
370
+ {
371
+ pYUV444Data = h264->pOldYUV444Data;
372
+ pOldYUV444Data = h264->pYUV444Data;
373
+ pYUVData = h264->pOldYUVData;
374
+ pOldYUVData = h264->pYUVData;
375
+ }
376
+ else
377
+ {
378
+ pYUV444Data = h264->pYUV444Data;
379
+ pOldYUV444Data = h264->pOldYUV444Data;
380
+ pYUVData = h264->pYUVData;
381
+ pOldYUVData = h264->pOldYUVData;
382
+ }
383
+ h264->encodingBuffer = !h264->encodingBuffer;
384
+
385
+ if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride,
386
+ pYUV444Data, pYUVData, region, 1))
387
+ goto fail;
388
+
389
+ if (!detect_changes(h264->firstLumaFrameDone, h264->QP, region, pYUV444Data, pOldYUV444Data,
390
+ h264->iStride, meta))
391
+ goto fail;
392
+ if (!detect_changes(h264->firstChromaFrameDone, h264->QP, region, pYUVData, pOldYUVData,
393
+ h264->iStride, auxMeta))
394
+ goto fail;
395
+
396
+ /* [MS-RDPEGFX] 2.2.4.5 RFX_AVC444_BITMAP_STREAM
397
+ * LC:
398
+ * 0 ... Luma & Chroma
399
+ * 1 ... Luma
400
+ * 2 ... Chroma
401
+ */
402
+ if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0))
403
+ *op = 0;
404
+ else if (meta->numRegionRects > 0)
405
+ *op = 1;
406
+ else if (auxMeta->numRegionRects > 0)
407
+ *op = 2;
408
+ else
409
+ {
410
+ WLog_INFO(TAG, "no changes detected for luma or chroma frame");
411
+ rc = 0;
412
+ goto fail;
413
+ }
414
+
415
+ if ((*op == 0) || (*op == 1))
416
+ {
417
+ const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] };
418
+
419
+ if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0)
420
+ goto fail;
421
+ h264->firstLumaFrameDone = TRUE;
422
+ memcpy(h264->lumaData, coded, codedSize);
423
+ *ppDstData = h264->lumaData;
424
+ *pDstSize = codedSize;
425
+ }
426
+
427
+ if ((*op == 0) || (*op == 2))
428
+ {
429
+ const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] };
430
+
431
+ if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0)
432
+ goto fail;
433
+ h264->firstChromaFrameDone = TRUE;
434
+ *ppAuxDstData = coded;
435
+ *pAuxDstSize = codedSize;
436
+ }
437
+
438
+ rc = 1;
439
+ fail:
440
+ if (rc < 0)
441
+ {
442
+ free_h264_metablock(meta);
443
+ free_h264_metablock(auxMeta);
444
+ }
445
+ return rc;
446
+ }
447
+
448
+ static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight)
449
+ {
450
+ WINPR_ASSERT(h264);
451
+
452
+ const UINT32* piMainStride = h264->iStride;
453
+ UINT32* piDstSize = h264->iYUV444Size;
454
+ UINT32* piDstStride = h264->iYUV444Stride;
455
+ BYTE** ppYUVDstData = h264->pYUV444Data;
456
+ BYTE** ppOldYUVDstData = h264->pOldYUV444Data;
457
+
458
+ nDstHeight = MAX(h264->height, nDstHeight);
459
+ const UINT32 pad = nDstHeight % 16;
460
+ UINT32 padDstHeight = nDstHeight; /* Need alignment to 16x16 blocks */
461
+
462
+ if (pad != 0)
463
+ padDstHeight += 16 - pad;
464
+
465
+ if ((piMainStride[0] != piDstStride[0]) ||
466
+ (piDstSize[0] != 1ull * piMainStride[0] * padDstHeight))
467
+ {
468
+ for (UINT32 x = 0; x < 3; x++)
469
+ {
470
+ piDstStride[x] = piMainStride[0];
471
+ piDstSize[x] = piDstStride[x] * padDstHeight;
472
+
473
+ if (piDstSize[x] == 0)
474
+ return FALSE;
475
+
476
+ BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], piDstSize[x], 1, 16);
477
+ if (!tmp1)
478
+ return FALSE;
479
+ ppYUVDstData[x] = tmp1;
480
+ BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], piDstSize[x], 1, 16);
481
+ if (!tmp2)
482
+ return FALSE;
483
+ ppOldYUVDstData[x] = tmp2;
484
+ }
485
+
486
+ {
487
+ BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, piDstSize[0], 4, 16);
488
+ if (!tmp)
489
+ goto fail;
490
+ h264->lumaData = tmp;
491
+ }
492
+ }
493
+
494
+ for (UINT32 x = 0; x < 3; x++)
495
+ {
496
+ if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
497
+ {
498
+ WLog_Print(h264->log, WLOG_ERROR,
499
+ "YUV buffer not initialized! check your decoder settings");
500
+ goto fail;
501
+ }
502
+ }
503
+
504
+ if (!h264->lumaData)
505
+ goto fail;
506
+
507
+ return TRUE;
508
+ fail:
509
+ return FALSE;
510
+ }
511
+
512
+ static BOOL avc444_process_rects(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize,
513
+ BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
514
+ UINT32 nDstWidth, UINT32 nDstHeight, const RECTANGLE_16* rects,
515
+ UINT32 nrRects, avc444_frame_type type)
516
+ {
517
+ const BYTE* pYUVData[3];
518
+ BYTE* pYUVDstData[3];
519
+ UINT32* piDstStride = h264->iYUV444Stride;
520
+ BYTE** ppYUVDstData = h264->pYUV444Data;
521
+ const UINT32* piStride = h264->iStride;
522
+
523
+ if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0)
524
+ return FALSE;
525
+
526
+ pYUVData[0] = h264->pYUVData[0];
527
+ pYUVData[1] = h264->pYUVData[1];
528
+ pYUVData[2] = h264->pYUVData[2];
529
+ if (!avc444_ensure_buffer(h264, nDstHeight))
530
+ return FALSE;
531
+
532
+ pYUVDstData[0] = ppYUVDstData[0];
533
+ pYUVDstData[1] = ppYUVDstData[1];
534
+ pYUVDstData[2] = ppYUVDstData[2];
535
+ if (!yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->height, pYUVDstData,
536
+ piDstStride, DstFormat, pDstData, nDstStep, rects, nrRects))
537
+ return FALSE;
538
+
539
+ return TRUE;
540
+ }
541
+
542
+ #if defined(AVC444_FRAME_STAT)
543
+ static UINT64 op1 = 0;
544
+ static double op1sum = 0;
545
+ static UINT64 op2 = 0;
546
+ static double op2sum = 0;
547
+ static UINT64 op3 = 0;
548
+ static double op3sum = 0;
549
+ static double avg(UINT64* count, double old, double size)
550
+ {
551
+ double tmp = size + *count * old;
552
+ (*count)++;
553
+ tmp = tmp / *count;
554
+ return tmp;
555
+ }
556
+ #endif
557
+
558
+ INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op, const RECTANGLE_16* regionRects,
559
+ UINT32 numRegionRects, const BYTE* pSrcData, UINT32 SrcSize,
560
+ const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
561
+ const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat,
562
+ UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId)
563
+ {
564
+ INT32 status = -1;
565
+ avc444_frame_type chroma =
566
+ (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
567
+
568
+ if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
569
+ return -1001;
570
+
571
+ switch (op)
572
+ {
573
+ case 0: /* YUV420 in stream 1
574
+ * Chroma420 in stream 2 */
575
+ if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
576
+ nDstWidth, nDstHeight, regionRects, numRegionRects,
577
+ AVC444_LUMA))
578
+ status = -1;
579
+ else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
580
+ nDstStep, nDstWidth, nDstHeight, auxRegionRects,
581
+ numAuxRegionRect, chroma))
582
+ status = -1;
583
+ else
584
+ status = 0;
585
+
586
+ break;
587
+
588
+ case 2: /* Chroma420 in stream 1 */
589
+ if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
590
+ nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
591
+ status = -1;
592
+ else
593
+ status = 0;
594
+
595
+ break;
596
+
597
+ case 1: /* YUV420 in stream 1 */
598
+ if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
599
+ nDstWidth, nDstHeight, regionRects, numRegionRects,
600
+ AVC444_LUMA))
601
+ status = -1;
602
+ else
603
+ status = 0;
604
+
605
+ break;
606
+
607
+ default: /* WTF? */
608
+ break;
609
+ }
610
+
611
+ #if defined(AVC444_FRAME_STAT)
612
+
613
+ switch (op)
614
+ {
615
+ case 0:
616
+ op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
617
+ break;
618
+
619
+ case 1:
620
+ op2sum = avg(&op2, op2sum, SrcSize);
621
+ break;
622
+
623
+ case 2:
624
+ op3sum = avg(&op3, op3sum, SrcSize);
625
+ break;
626
+
627
+ default:
628
+ break;
629
+ }
630
+
631
+ WLog_Print(h264->log, WLOG_INFO,
632
+ "luma=%" PRIu64 " [avg=%lf] chroma=%" PRIu64 " [avg=%lf] combined=%" PRIu64
633
+ " [avg=%lf]",
634
+ op1, op1sum, op2, op2sum, op3, op3sum);
635
+ #endif
636
+ return status;
637
+ }
638
+
639
+ #define MAX_SUBSYSTEMS 10
640
+ static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT;
641
+ static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = { 0 };
642
+
643
+ static BOOL CALLBACK h264_register_subsystems(PINIT_ONCE once, PVOID param, PVOID* context)
644
+ {
645
+ int i = 0;
646
+
647
+ #ifdef WITH_MEDIACODEC
648
+ {
649
+ subSystems[i] = &g_Subsystem_mediacodec;
650
+ i++;
651
+ }
652
+ #endif
653
+ #if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
654
+ {
655
+ subSystems[i] = &g_Subsystem_MF;
656
+ i++;
657
+ }
658
+ #endif
659
+ #ifdef WITH_OPENH264
660
+ {
661
+ subSystems[i] = &g_Subsystem_OpenH264;
662
+ i++;
663
+ }
664
+ #endif
665
+ #ifdef WITH_VIDEO_FFMPEG
666
+ {
667
+ subSystems[i] = &g_Subsystem_libavcodec;
668
+ i++;
669
+ }
670
+ #endif
671
+ return i > 0;
672
+ }
673
+
674
+ static BOOL h264_context_init(H264_CONTEXT* h264)
675
+ {
676
+ if (!h264)
677
+ return FALSE;
678
+
679
+ h264->subsystem = NULL;
680
+ InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems, NULL, NULL);
681
+
682
+ for (size_t i = 0; i < MAX_SUBSYSTEMS; i++)
683
+ {
684
+ const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
685
+
686
+ if (!subsystem || !subsystem->Init)
687
+ break;
688
+
689
+ if (subsystem->Init(h264))
690
+ {
691
+ h264->subsystem = subsystem;
692
+ return TRUE;
693
+ }
694
+ }
695
+
696
+ return FALSE;
697
+ }
698
+
699
+ BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
700
+ {
701
+ if (!h264)
702
+ return FALSE;
703
+
704
+ h264->width = width;
705
+ h264->height = height;
706
+
707
+ if (h264->subsystem && h264->subsystem->Uninit)
708
+ h264->subsystem->Uninit(h264);
709
+ if (!h264_context_init(h264))
710
+ return FALSE;
711
+
712
+ return yuv_context_reset(h264->yuv, width, height);
713
+ }
714
+
715
+ H264_CONTEXT* h264_context_new(BOOL Compressor)
716
+ {
717
+ H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1, sizeof(H264_CONTEXT));
718
+ if (!h264)
719
+ return NULL;
720
+
721
+ h264->log = WLog_Get(TAG);
722
+
723
+ if (!h264->log)
724
+ goto fail;
725
+
726
+ h264->Compressor = Compressor;
727
+ if (Compressor)
728
+ {
729
+ /* Default compressor settings, may be changed by caller */
730
+ h264->BitRate = 1000000;
731
+ h264->FrameRate = 30;
732
+ }
733
+
734
+ if (!h264_context_init(h264))
735
+ goto fail;
736
+
737
+ h264->yuv = yuv_context_new(Compressor, 0);
738
+ if (!h264->yuv)
739
+ goto fail;
740
+
741
+ return h264;
742
+
743
+ fail:
744
+ WINPR_PRAGMA_DIAG_PUSH
745
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
746
+ h264_context_free(h264);
747
+ WINPR_PRAGMA_DIAG_POP
748
+ return NULL;
749
+ }
750
+
751
+ void h264_context_free(H264_CONTEXT* h264)
752
+ {
753
+ if (h264)
754
+ {
755
+ if (h264->subsystem)
756
+ h264->subsystem->Uninit(h264);
757
+
758
+ for (size_t x = 0; x < 3; x++)
759
+ {
760
+ if (h264->Compressor)
761
+ {
762
+ winpr_aligned_free(h264->pYUVData[x]);
763
+ winpr_aligned_free(h264->pOldYUVData[x]);
764
+ }
765
+ winpr_aligned_free(h264->pYUV444Data[x]);
766
+ winpr_aligned_free(h264->pOldYUV444Data[x]);
767
+ }
768
+ winpr_aligned_free(h264->lumaData);
769
+
770
+ yuv_context_free(h264->yuv);
771
+ free(h264);
772
+ }
773
+ }
774
+
775
+ void free_h264_metablock(RDPGFX_H264_METABLOCK* meta)
776
+ {
777
+ RDPGFX_H264_METABLOCK m = { 0 };
778
+ if (!meta)
779
+ return;
780
+ free(meta->quantQualityVals);
781
+ free(meta->regionRects);
782
+ *meta = m;
783
+ }
784
+
785
+ BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value)
786
+ {
787
+ WINPR_ASSERT(h264);
788
+ switch (option)
789
+ {
790
+ case H264_CONTEXT_OPTION_BITRATE:
791
+ h264->BitRate = value;
792
+ return TRUE;
793
+ case H264_CONTEXT_OPTION_FRAMERATE:
794
+ h264->FrameRate = value;
795
+ return TRUE;
796
+ case H264_CONTEXT_OPTION_RATECONTROL:
797
+ h264->RateControlMode = value;
798
+ return TRUE;
799
+ case H264_CONTEXT_OPTION_QP:
800
+ h264->QP = value;
801
+ return TRUE;
802
+ case H264_CONTEXT_OPTION_USAGETYPE:
803
+ h264->UsageType = value;
804
+ return TRUE;
805
+ case H264_CONTEXT_OPTION_HW_ACCEL:
806
+ h264->hwAccel = value ? TRUE : FALSE;
807
+ return TRUE;
808
+ default:
809
+ WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]",
810
+ option);
811
+ return FALSE;
812
+ }
813
+ }
814
+
815
+ UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
816
+ {
817
+ WINPR_ASSERT(h264);
818
+ switch (option)
819
+ {
820
+ case H264_CONTEXT_OPTION_BITRATE:
821
+ return h264->BitRate;
822
+ case H264_CONTEXT_OPTION_FRAMERATE:
823
+ return h264->FrameRate;
824
+ case H264_CONTEXT_OPTION_RATECONTROL:
825
+ return h264->RateControlMode;
826
+ case H264_CONTEXT_OPTION_QP:
827
+ return h264->QP;
828
+ case H264_CONTEXT_OPTION_USAGETYPE:
829
+ return h264->UsageType;
830
+ case H264_CONTEXT_OPTION_HW_ACCEL:
831
+ return h264->hwAccel;
832
+ default:
833
+ WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]",
834
+ option);
835
+ return 0;
836
+ }
837
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264.h ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library - Decode
4
+ *
5
+ * Copyright 2018 Armin Novak <anovak@thincast.com>
6
+ * Copyright 2018 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LIB_CODEC_H264_H
22
+ #define FREERDP_LIB_CODEC_H264_H
23
+
24
+ #include <freerdp/api.h>
25
+ #include <freerdp/config.h>
26
+ #include <freerdp/codec/h264.h>
27
+
28
+ #ifdef __cplusplus
29
+ extern "C"
30
+ {
31
+ #endif
32
+
33
+ typedef BOOL (*pfnH264SubsystemInit)(H264_CONTEXT* h264);
34
+ typedef void (*pfnH264SubsystemUninit)(H264_CONTEXT* h264);
35
+
36
+ typedef int (*pfnH264SubsystemDecompress)(H264_CONTEXT* WINPR_RESTRICT h264,
37
+ const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize);
38
+ typedef int (*pfnH264SubsystemCompress)(H264_CONTEXT* WINPR_RESTRICT h264,
39
+ const BYTE** WINPR_RESTRICT pSrcYuv,
40
+ const UINT32* WINPR_RESTRICT pStride,
41
+ BYTE** WINPR_RESTRICT ppDstData,
42
+ UINT32* WINPR_RESTRICT pDstSize);
43
+
44
+ struct S_H264_CONTEXT_SUBSYSTEM
45
+ {
46
+ const char* name;
47
+ pfnH264SubsystemInit Init;
48
+ pfnH264SubsystemUninit Uninit;
49
+ pfnH264SubsystemDecompress Decompress;
50
+ pfnH264SubsystemCompress Compress;
51
+ };
52
+
53
+ struct S_H264_CONTEXT
54
+ {
55
+ BOOL Compressor;
56
+
57
+ UINT32 width;
58
+ UINT32 height;
59
+
60
+ H264_RATECONTROL_MODE RateControlMode;
61
+ UINT32 BitRate;
62
+ UINT32 FrameRate;
63
+ UINT32 QP;
64
+ UINT32 UsageType;
65
+ UINT32 hwAccel;
66
+ UINT32 NumberOfThreads;
67
+
68
+ UINT32 iStride[3];
69
+ BYTE* pOldYUVData[3];
70
+ BYTE* pYUVData[3];
71
+
72
+ UINT32 iYUV444Size[3];
73
+ UINT32 iYUV444Stride[3];
74
+ BYTE* pOldYUV444Data[3];
75
+ BYTE* pYUV444Data[3];
76
+
77
+ UINT32 numSystemData;
78
+ void* pSystemData;
79
+ const H264_CONTEXT_SUBSYSTEM* subsystem;
80
+ YUV_CONTEXT* yuv;
81
+
82
+ BOOL encodingBuffer;
83
+ BOOL firstLumaFrameDone;
84
+ BOOL firstChromaFrameDone;
85
+
86
+ void* lumaData;
87
+ wLog* log;
88
+ };
89
+
90
+ FREERDP_LOCAL BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width,
91
+ UINT32 height);
92
+
93
+ #ifdef WITH_MEDIACODEC
94
+ extern const H264_CONTEXT_SUBSYSTEM g_Subsystem_mediacodec;
95
+ #endif
96
+ #if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
97
+ extern const H264_CONTEXT_SUBSYSTEM g_Subsystem_MF;
98
+ #endif
99
+ #ifdef WITH_OPENH264
100
+ extern const H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264;
101
+ #endif
102
+ #ifdef WITH_VIDEO_FFMPEG
103
+ extern const H264_CONTEXT_SUBSYSTEM g_Subsystem_libavcodec;
104
+ #endif
105
+
106
+ #ifdef __cplusplus
107
+ }
108
+ #endif
109
+
110
+ #endif /* FREERDP_LIB_CODEC_H264_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264_ffmpeg.c ADDED
@@ -0,0 +1,820 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * H.264 Bitmap Compression
4
+ *
5
+ * Copyright 2015 Marc-André Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2014 Mike McDonald <Mike.McDonald@software.dell.com>
7
+ * Copyright 2014 erbth <t.erbesdobler@team103.com>
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+ #include <freerdp/config.h>
22
+
23
+ #include <winpr/wlog.h>
24
+ #include <freerdp/log.h>
25
+ #include <freerdp/codec/h264.h>
26
+ #include <libavcodec/avcodec.h>
27
+ #include <libavutil/opt.h>
28
+
29
+ #include "h264.h"
30
+
31
+ #ifdef WITH_VAAPI
32
+ #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 9, 0)
33
+ #include <libavutil/hwcontext.h>
34
+ #else
35
+ #pragma warning You have asked for VA - API decoding, \
36
+ but your version of libavutil is too old !Disabling.
37
+ #undef WITH_VAAPI
38
+ #endif
39
+ #endif
40
+
41
+ /* Fallback support for older libavcodec versions */
42
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
43
+ #define AV_CODEC_ID_H264 CODEC_ID_H264
44
+ #endif
45
+
46
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 34, 2)
47
+ #define AV_CODEC_FLAG_LOOP_FILTER CODEC_FLAG_LOOP_FILTER
48
+ #define AV_CODEC_CAP_TRUNCATED CODEC_CAP_TRUNCATED
49
+ #define AV_CODEC_FLAG_TRUNCATED CODEC_FLAG_TRUNCATED
50
+ #endif
51
+
52
+ #if LIBAVUTIL_VERSION_MAJOR < 52
53
+ #define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
54
+ #endif
55
+
56
+ /* Ubuntu 14.04 ships without the functions provided by avutil,
57
+ * so define error to string methods here. */
58
+ #if !defined(av_err2str)
59
+ static inline char* error_string(char* errbuf, size_t errbuf_size, int errnum)
60
+ {
61
+ av_strerror(errnum, errbuf, errbuf_size);
62
+ return errbuf;
63
+ }
64
+
65
+ #define av_err2str(errnum) error_string((char[64]){ 0 }, 64, errnum)
66
+ #endif
67
+
68
+ #if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING)
69
+ #define VAAPI_DEVICE "/dev/dri/renderD128"
70
+ #endif
71
+
72
+ typedef struct
73
+ {
74
+ const AVCodec* codecDecoder;
75
+ AVCodecContext* codecDecoderContext;
76
+ const AVCodec* codecEncoder;
77
+ AVCodecContext* codecEncoderContext;
78
+ AVCodecParserContext* codecParser;
79
+ AVFrame* videoFrame;
80
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
81
+ AVPacket bufferpacket;
82
+ #endif
83
+ AVPacket* packet;
84
+ #if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING)
85
+ AVBufferRef* hwctx;
86
+ AVFrame* hwVideoFrame;
87
+ enum AVPixelFormat hw_pix_fmt;
88
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100)
89
+ AVBufferRef* hw_frames_ctx;
90
+ #endif
91
+ #endif
92
+ } H264_CONTEXT_LIBAVCODEC;
93
+
94
+ static void libavcodec_destroy_encoder_context(H264_CONTEXT* WINPR_RESTRICT h264)
95
+ {
96
+ H264_CONTEXT_LIBAVCODEC* sys = NULL;
97
+
98
+ if (!h264 || !h264->subsystem)
99
+ return;
100
+
101
+ sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
102
+
103
+ if (sys->codecEncoderContext)
104
+ {
105
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
106
+ avcodec_free_context(&sys->codecEncoderContext);
107
+ #else
108
+ avcodec_close(sys->codecEncoderContext);
109
+ av_free(sys->codecEncoderContext);
110
+ #endif
111
+ }
112
+
113
+ sys->codecEncoderContext = NULL;
114
+ }
115
+
116
+ #ifdef WITH_VAAPI_H264_ENCODING
117
+ static int set_hw_frames_ctx(H264_CONTEXT* WINPR_RESTRICT h264)
118
+ {
119
+ H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
120
+ AVBufferRef* hw_frames_ref = NULL;
121
+ AVHWFramesContext* frames_ctx = NULL;
122
+ int err = 0;
123
+
124
+ if (!(hw_frames_ref = av_hwframe_ctx_alloc(sys->hwctx)))
125
+ {
126
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to create VAAPI frame context");
127
+ return -1;
128
+ }
129
+ frames_ctx = (AVHWFramesContext*)(hw_frames_ref->data);
130
+ frames_ctx->format = AV_PIX_FMT_VAAPI;
131
+ frames_ctx->sw_format = AV_PIX_FMT_NV12;
132
+ frames_ctx->width = sys->codecEncoderContext->width;
133
+ frames_ctx->height = sys->codecEncoderContext->height;
134
+ frames_ctx->initial_pool_size = 20;
135
+ if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0)
136
+ {
137
+ WLog_Print(h264->log, WLOG_ERROR,
138
+ "Failed to initialize VAAPI frame context."
139
+ "Error code: %s",
140
+ av_err2str(err));
141
+ av_buffer_unref(&hw_frames_ref);
142
+ return err;
143
+ }
144
+ sys->codecEncoderContext->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
145
+ if (!sys->codecEncoderContext->hw_frames_ctx)
146
+ err = AVERROR(ENOMEM);
147
+
148
+ av_buffer_unref(&hw_frames_ref);
149
+ return err;
150
+ }
151
+ #endif
152
+
153
+ static BOOL libavcodec_create_encoder_context(H264_CONTEXT* WINPR_RESTRICT h264)
154
+ {
155
+ BOOL recreate = FALSE;
156
+ H264_CONTEXT_LIBAVCODEC* sys = NULL;
157
+
158
+ if (!h264 || !h264->subsystem)
159
+ return FALSE;
160
+
161
+ if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
162
+ return FALSE;
163
+
164
+ sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
165
+ if (!sys || !sys->codecEncoder)
166
+ return FALSE;
167
+
168
+ recreate = !sys->codecEncoderContext;
169
+
170
+ if (sys->codecEncoderContext)
171
+ {
172
+ if ((sys->codecEncoderContext->width != (int)h264->width) ||
173
+ (sys->codecEncoderContext->height != (int)h264->height))
174
+ recreate = TRUE;
175
+ }
176
+
177
+ if (!recreate)
178
+ return TRUE;
179
+
180
+ libavcodec_destroy_encoder_context(h264);
181
+
182
+ sys->codecEncoderContext = avcodec_alloc_context3(sys->codecEncoder);
183
+
184
+ if (!sys->codecEncoderContext)
185
+ goto EXCEPTION;
186
+
187
+ switch (h264->RateControlMode)
188
+ {
189
+ case H264_RATECONTROL_VBR:
190
+ sys->codecEncoderContext->bit_rate = h264->BitRate;
191
+ break;
192
+
193
+ case H264_RATECONTROL_CQP:
194
+ if (av_opt_set_int(sys->codecEncoderContext, "qp", h264->QP, AV_OPT_SEARCH_CHILDREN) <
195
+ 0)
196
+ {
197
+ WLog_Print(h264->log, WLOG_ERROR, "av_opt_set_int failed");
198
+ }
199
+ break;
200
+
201
+ default:
202
+ break;
203
+ }
204
+
205
+ sys->codecEncoderContext->width = (int)MIN(INT32_MAX, h264->width);
206
+ sys->codecEncoderContext->height = (int)MIN(INT32_MAX, h264->height);
207
+ sys->codecEncoderContext->delay = 0;
208
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 13, 100)
209
+ sys->codecEncoderContext->framerate =
210
+ (AVRational){ WINPR_ASSERTING_INT_CAST(int, h264->FrameRate), 1 };
211
+ #endif
212
+ sys->codecEncoderContext->time_base =
213
+ (AVRational){ 1, WINPR_ASSERTING_INT_CAST(int, h264->FrameRate) };
214
+ av_opt_set(sys->codecEncoderContext, "tune", "zerolatency", AV_OPT_SEARCH_CHILDREN);
215
+
216
+ sys->codecEncoderContext->flags |= AV_CODEC_FLAG_LOOP_FILTER;
217
+
218
+ #ifdef WITH_VAAPI_H264_ENCODING
219
+ if (sys->hwctx)
220
+ {
221
+ av_opt_set(sys->codecEncoderContext, "preset", "veryslow", AV_OPT_SEARCH_CHILDREN);
222
+
223
+ sys->codecEncoderContext->pix_fmt = AV_PIX_FMT_VAAPI;
224
+ /* set hw_frames_ctx for encoder's AVCodecContext */
225
+ if (set_hw_frames_ctx(h264) < 0)
226
+ goto EXCEPTION;
227
+ }
228
+ else
229
+ #endif
230
+ {
231
+ av_opt_set(sys->codecEncoderContext, "preset", "medium", AV_OPT_SEARCH_CHILDREN);
232
+ sys->codecEncoderContext->pix_fmt = AV_PIX_FMT_YUV420P;
233
+ }
234
+
235
+ if (avcodec_open2(sys->codecEncoderContext, sys->codecEncoder, NULL) < 0)
236
+ goto EXCEPTION;
237
+
238
+ return TRUE;
239
+ EXCEPTION:
240
+ libavcodec_destroy_encoder_context(h264);
241
+ return FALSE;
242
+ }
243
+
244
+ static int libavcodec_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
245
+ const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
246
+ {
247
+ union
248
+ {
249
+ const BYTE* cpv;
250
+ BYTE* pv;
251
+ } cnv;
252
+ int rc = -1;
253
+ int status = 0;
254
+ int gotFrame = 0;
255
+ AVPacket* packet = NULL;
256
+
257
+ WINPR_ASSERT(h264);
258
+ WINPR_ASSERT(pSrcData || (SrcSize == 0));
259
+
260
+ H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
261
+ BYTE** pYUVData = h264->pYUVData;
262
+ UINT32* iStride = h264->iStride;
263
+
264
+ WINPR_ASSERT(sys);
265
+
266
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
267
+ packet = &sys->bufferpacket;
268
+ WINPR_ASSERT(packet);
269
+ av_init_packet(packet);
270
+ #else
271
+ packet = av_packet_alloc();
272
+ #endif
273
+ if (!packet)
274
+ {
275
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate AVPacket");
276
+ goto fail;
277
+ }
278
+
279
+ cnv.cpv = pSrcData;
280
+ packet->data = cnv.pv;
281
+ packet->size = (int)MIN(SrcSize, INT32_MAX);
282
+
283
+ WINPR_ASSERT(sys->codecDecoderContext);
284
+ /* avcodec_decode_video2 is deprecated with libavcodec 57.48.101 */
285
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
286
+ status = avcodec_send_packet(sys->codecDecoderContext, packet);
287
+
288
+ if (status < 0)
289
+ {
290
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to decode video frame (status=%d)", status);
291
+ goto fail;
292
+ }
293
+
294
+ sys->videoFrame->format = AV_PIX_FMT_YUV420P;
295
+
296
+ #ifdef WITH_VAAPI
297
+ status = avcodec_receive_frame(sys->codecDecoderContext,
298
+ sys->hwctx ? sys->hwVideoFrame : sys->videoFrame);
299
+ #else
300
+ status = avcodec_receive_frame(sys->codecDecoderContext, sys->videoFrame);
301
+ #endif
302
+ if (status == AVERROR(EAGAIN))
303
+ {
304
+ rc = 0;
305
+ goto fail;
306
+ }
307
+
308
+ gotFrame = (status == 0);
309
+ #else
310
+ #ifdef WITH_VAAPI
311
+ status =
312
+ avcodec_decode_video2(sys->codecDecoderContext,
313
+ sys->hwctx ? sys->hwVideoFrame : sys->videoFrame, &gotFrame, packet);
314
+ #else
315
+ status = avcodec_decode_video2(sys->codecDecoderContext, sys->videoFrame, &gotFrame, packet);
316
+ #endif
317
+ #endif
318
+ if (status < 0)
319
+ {
320
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to decode video frame (status=%d)", status);
321
+ goto fail;
322
+ }
323
+
324
+ #ifdef WITH_VAAPI
325
+
326
+ if (sys->hwctx)
327
+ {
328
+ if (sys->hwVideoFrame->format == sys->hw_pix_fmt)
329
+ {
330
+ sys->videoFrame->width = sys->hwVideoFrame->width;
331
+ sys->videoFrame->height = sys->hwVideoFrame->height;
332
+ status = av_hwframe_transfer_data(sys->videoFrame, sys->hwVideoFrame, 0);
333
+ }
334
+ else
335
+ {
336
+ status = av_frame_copy(sys->videoFrame, sys->hwVideoFrame);
337
+ }
338
+ }
339
+
340
+ gotFrame = (status == 0);
341
+
342
+ if (status < 0)
343
+ {
344
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to transfer video frame (status=%d) (%s)", status,
345
+ av_err2str(status));
346
+ goto fail;
347
+ }
348
+
349
+ #endif
350
+ #if 0
351
+ WLog_Print(h264->log, WLOG_INFO,
352
+ "libavcodec_decompress: frame decoded (status=%d, gotFrame=%d, width=%d, height=%d, Y=[%p,%d], U=[%p,%d], V=[%p,%d])",
353
+ status, gotFrame, sys->videoFrame->width, sys->videoFrame->height,
354
+ (void*) sys->videoFrame->data[0], sys->videoFrame->linesize[0],
355
+ (void*) sys->videoFrame->data[1], sys->videoFrame->linesize[1],
356
+ (void*) sys->videoFrame->data[2], sys->videoFrame->linesize[2]);
357
+ #endif
358
+
359
+ if (gotFrame)
360
+ {
361
+ WINPR_ASSERT(sys->videoFrame);
362
+
363
+ pYUVData[0] = sys->videoFrame->data[0];
364
+ pYUVData[1] = sys->videoFrame->data[1];
365
+ pYUVData[2] = sys->videoFrame->data[2];
366
+ iStride[0] = (UINT32)MAX(0, sys->videoFrame->linesize[0]);
367
+ iStride[1] = (UINT32)MAX(0, sys->videoFrame->linesize[1]);
368
+ iStride[2] = (UINT32)MAX(0, sys->videoFrame->linesize[2]);
369
+
370
+ rc = 1;
371
+ }
372
+ else
373
+ rc = -2;
374
+
375
+ fail:
376
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
377
+ av_packet_unref(packet);
378
+ #else
379
+ av_packet_free(&packet);
380
+ #endif
381
+
382
+ return rc;
383
+ }
384
+
385
+ static int libavcodec_compress(H264_CONTEXT* WINPR_RESTRICT h264,
386
+ const BYTE** WINPR_RESTRICT pSrcYuv,
387
+ const UINT32* WINPR_RESTRICT pStride,
388
+ BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
389
+ {
390
+ union
391
+ {
392
+ const BYTE* cpv;
393
+ uint8_t* pv;
394
+ } cnv;
395
+ int rc = -1;
396
+ int status = 0;
397
+ int gotFrame = 0;
398
+
399
+ WINPR_ASSERT(h264);
400
+
401
+ H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
402
+ WINPR_ASSERT(sys);
403
+
404
+ if (!libavcodec_create_encoder_context(h264))
405
+ return -1;
406
+
407
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
408
+ sys->packet = &sys->bufferpacket;
409
+ av_packet_unref(sys->packet);
410
+ av_init_packet(sys->packet);
411
+ #else
412
+ av_packet_free(&sys->packet);
413
+ sys->packet = av_packet_alloc();
414
+ #endif
415
+ if (!sys->packet)
416
+ {
417
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate AVPacket");
418
+ goto fail;
419
+ }
420
+
421
+ WINPR_ASSERT(sys->packet);
422
+ sys->packet->data = NULL;
423
+ sys->packet->size = 0;
424
+
425
+ WINPR_ASSERT(sys->videoFrame);
426
+ WINPR_ASSERT(sys->codecEncoderContext);
427
+ sys->videoFrame->format = AV_PIX_FMT_YUV420P;
428
+ sys->videoFrame->width = sys->codecEncoderContext->width;
429
+ sys->videoFrame->height = sys->codecEncoderContext->height;
430
+ #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 48, 100)
431
+ sys->videoFrame->colorspace = AVCOL_SPC_BT709;
432
+ #endif
433
+ #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 92, 100)
434
+ sys->videoFrame->chroma_location = AVCHROMA_LOC_LEFT;
435
+ #endif
436
+ cnv.cpv = pSrcYuv[0];
437
+ sys->videoFrame->data[0] = cnv.pv;
438
+
439
+ cnv.cpv = pSrcYuv[1];
440
+ sys->videoFrame->data[1] = cnv.pv;
441
+
442
+ cnv.cpv = pSrcYuv[2];
443
+ sys->videoFrame->data[2] = cnv.pv;
444
+
445
+ sys->videoFrame->linesize[0] = (int)pStride[0];
446
+ sys->videoFrame->linesize[1] = (int)pStride[1];
447
+ sys->videoFrame->linesize[2] = (int)pStride[2];
448
+ sys->videoFrame->pts++;
449
+
450
+ #ifdef WITH_VAAPI_H264_ENCODING
451
+ if (sys->hwctx)
452
+ {
453
+ av_frame_unref(sys->hwVideoFrame);
454
+ if ((status = av_hwframe_get_buffer(sys->codecEncoderContext->hw_frames_ctx,
455
+ sys->hwVideoFrame, 0)) < 0 ||
456
+ !sys->hwVideoFrame->hw_frames_ctx)
457
+ {
458
+ WLog_Print(h264->log, WLOG_ERROR, "av_hwframe_get_buffer failed (%s [%d])",
459
+ av_err2str(status), status);
460
+ goto fail;
461
+ }
462
+ sys->videoFrame->format = AV_PIX_FMT_NV12;
463
+ if ((status = av_hwframe_transfer_data(sys->hwVideoFrame, sys->videoFrame, 0)) < 0)
464
+ {
465
+ WLog_Print(h264->log, WLOG_ERROR, "av_hwframe_transfer_data failed (%s [%d])",
466
+ av_err2str(status), status);
467
+ goto fail;
468
+ }
469
+ }
470
+ #endif
471
+
472
+ /* avcodec_encode_video2 is deprecated with libavcodec 57.48.101 */
473
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
474
+ #ifdef WITH_VAAPI_H264_ENCODING
475
+ status = avcodec_send_frame(sys->codecEncoderContext,
476
+ sys->hwctx ? sys->hwVideoFrame : sys->videoFrame);
477
+ #else
478
+ status = avcodec_send_frame(sys->codecEncoderContext, sys->videoFrame);
479
+ #endif
480
+
481
+ if (status < 0)
482
+ {
483
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to encode video frame (%s [%d])",
484
+ av_err2str(status), status);
485
+ goto fail;
486
+ }
487
+
488
+ status = avcodec_receive_packet(sys->codecEncoderContext, sys->packet);
489
+
490
+ if (status < 0)
491
+ {
492
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to encode video frame (%s [%d])",
493
+ av_err2str(status), status);
494
+ goto fail;
495
+ }
496
+
497
+ gotFrame = (status == 0);
498
+ #elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 59, 100)
499
+
500
+ do
501
+ {
502
+ status = avcodec_encode_video2(sys->codecEncoderContext, sys->packet, sys->videoFrame,
503
+ &gotFrame);
504
+ } while ((status >= 0) && (gotFrame == 0));
505
+
506
+ #else
507
+ sys->packet->size =
508
+ avpicture_get_size(sys->codecDecoderContext->pix_fmt, sys->codecDecoderContext->width,
509
+ sys->codecDecoderContext->height);
510
+ sys->packet->data = av_malloc(sys->packet->size);
511
+
512
+ if (!sys->packet->data)
513
+ status = -1;
514
+ else
515
+ {
516
+ status = avcodec_encode_video(sys->codecDecoderContext, sys->packet->data,
517
+ sys->packet->size, sys->videoFrame);
518
+ }
519
+
520
+ #endif
521
+
522
+ if (status < 0)
523
+ {
524
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to encode video frame (%s [%d])",
525
+ av_err2str(status), status);
526
+ goto fail;
527
+ }
528
+
529
+ WINPR_ASSERT(sys->packet);
530
+ *ppDstData = sys->packet->data;
531
+ *pDstSize = (UINT32)MAX(0, sys->packet->size);
532
+
533
+ if (!gotFrame)
534
+ {
535
+ WLog_Print(h264->log, WLOG_ERROR, "Did not get frame! (%s [%d])", av_err2str(status),
536
+ status);
537
+ rc = -2;
538
+ }
539
+ else
540
+ rc = 1;
541
+ fail:
542
+ return rc;
543
+ }
544
+
545
+ static void libavcodec_uninit(H264_CONTEXT* h264)
546
+ {
547
+ WINPR_ASSERT(h264);
548
+
549
+ H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
550
+
551
+ if (!sys)
552
+ return;
553
+
554
+ if (sys->packet)
555
+ {
556
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
557
+ av_packet_unref(sys->packet);
558
+ #else
559
+ av_packet_free(&sys->packet);
560
+ #endif
561
+ }
562
+
563
+ if (sys->videoFrame)
564
+ {
565
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
566
+ av_frame_free(&sys->videoFrame);
567
+ #else
568
+ av_free(sys->videoFrame);
569
+ #endif
570
+ }
571
+
572
+ #if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING)
573
+ if (sys->hwVideoFrame)
574
+ {
575
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
576
+ av_frame_free(&sys->hwVideoFrame);
577
+ #else
578
+ av_free(sys->hwVideoFrame);
579
+ #endif
580
+ }
581
+
582
+ if (sys->hwctx)
583
+ av_buffer_unref(&sys->hwctx);
584
+
585
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100)
586
+
587
+ if (sys->hw_frames_ctx)
588
+ av_buffer_unref(&sys->hw_frames_ctx);
589
+
590
+ #endif
591
+
592
+ #endif
593
+
594
+ if (sys->codecParser)
595
+ av_parser_close(sys->codecParser);
596
+
597
+ if (sys->codecDecoderContext)
598
+ {
599
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
600
+ avcodec_free_context(&sys->codecDecoderContext);
601
+ #else
602
+ avcodec_close(sys->codecDecoderContext);
603
+ av_free(sys->codecDecoderContext);
604
+ #endif
605
+ }
606
+
607
+ libavcodec_destroy_encoder_context(h264);
608
+ free(sys);
609
+ h264->pSystemData = NULL;
610
+ }
611
+
612
+ #ifdef WITH_VAAPI
613
+ static enum AVPixelFormat libavcodec_get_format(struct AVCodecContext* ctx,
614
+ const enum AVPixelFormat* fmts)
615
+ {
616
+ WINPR_ASSERT(ctx);
617
+
618
+ H264_CONTEXT* h264 = (H264_CONTEXT*)ctx->opaque;
619
+ WINPR_ASSERT(h264);
620
+
621
+ H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
622
+ WINPR_ASSERT(sys);
623
+
624
+ for (const enum AVPixelFormat* p = fmts; *p != AV_PIX_FMT_NONE; p++)
625
+ {
626
+ if (*p == sys->hw_pix_fmt)
627
+ {
628
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100)
629
+ sys->hw_frames_ctx = av_hwframe_ctx_alloc(sys->hwctx);
630
+
631
+ if (!sys->hw_frames_ctx)
632
+ {
633
+ return AV_PIX_FMT_NONE;
634
+ }
635
+
636
+ sys->codecDecoderContext->pix_fmt = *p;
637
+ AVHWFramesContext* frames = (AVHWFramesContext*)sys->hw_frames_ctx->data;
638
+ frames->format = *p;
639
+ frames->height = sys->codecDecoderContext->coded_height;
640
+ frames->width = sys->codecDecoderContext->coded_width;
641
+ frames->sw_format =
642
+ (sys->codecDecoderContext->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ? AV_PIX_FMT_P010
643
+ : AV_PIX_FMT_NV12);
644
+ frames->initial_pool_size = 20;
645
+
646
+ if (sys->codecDecoderContext->active_thread_type & FF_THREAD_FRAME)
647
+ frames->initial_pool_size += sys->codecDecoderContext->thread_count;
648
+
649
+ int err = av_hwframe_ctx_init(sys->hw_frames_ctx);
650
+
651
+ if (err < 0)
652
+ {
653
+ WLog_Print(h264->log, WLOG_ERROR, "Could not init hwframes context: %s",
654
+ av_err2str(err));
655
+ return AV_PIX_FMT_NONE;
656
+ }
657
+
658
+ sys->codecDecoderContext->hw_frames_ctx = av_buffer_ref(sys->hw_frames_ctx);
659
+ #endif
660
+ return *p;
661
+ }
662
+ }
663
+
664
+ return AV_PIX_FMT_NONE;
665
+ }
666
+ #endif
667
+
668
+ static BOOL libavcodec_init(H264_CONTEXT* h264)
669
+ {
670
+ H264_CONTEXT_LIBAVCODEC* sys = NULL;
671
+
672
+ WINPR_ASSERT(h264);
673
+ sys = (H264_CONTEXT_LIBAVCODEC*)calloc(1, sizeof(H264_CONTEXT_LIBAVCODEC));
674
+
675
+ if (!sys)
676
+ {
677
+ goto EXCEPTION;
678
+ }
679
+
680
+ h264->pSystemData = (void*)sys;
681
+
682
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
683
+ avcodec_register_all();
684
+ #endif
685
+
686
+ if (!h264->Compressor)
687
+ {
688
+ sys->codecDecoder = avcodec_find_decoder(AV_CODEC_ID_H264);
689
+
690
+ if (!sys->codecDecoder)
691
+ {
692
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to find libav H.264 codec");
693
+ goto EXCEPTION;
694
+ }
695
+
696
+ sys->codecDecoderContext = avcodec_alloc_context3(sys->codecDecoder);
697
+
698
+ if (!sys->codecDecoderContext)
699
+ {
700
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav codec context");
701
+ goto EXCEPTION;
702
+ }
703
+
704
+ #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
705
+ if (sys->codecDecoder->capabilities & AV_CODEC_CAP_TRUNCATED)
706
+ {
707
+ sys->codecDecoderContext->flags |= AV_CODEC_FLAG_TRUNCATED;
708
+ }
709
+ #endif
710
+
711
+ #ifdef WITH_VAAPI
712
+
713
+ if (!sys->hwctx)
714
+ {
715
+ int ret =
716
+ av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VAAPI, VAAPI_DEVICE, NULL, 0);
717
+
718
+ if (ret < 0)
719
+ {
720
+ WLog_Print(h264->log, WLOG_ERROR,
721
+ "Could not initialize hardware decoder, falling back to software: %s",
722
+ av_err2str(ret));
723
+ sys->hwctx = NULL;
724
+ goto fail_hwdevice_create;
725
+ }
726
+ }
727
+ WLog_Print(h264->log, WLOG_INFO, "Using VAAPI for accelerated H264 decoding");
728
+
729
+ sys->codecDecoderContext->get_format = libavcodec_get_format;
730
+ sys->hw_pix_fmt = AV_PIX_FMT_VAAPI;
731
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 80, 100)
732
+ sys->codecDecoderContext->hw_device_ctx = av_buffer_ref(sys->hwctx);
733
+ #endif
734
+ sys->codecDecoderContext->opaque = (void*)h264;
735
+ fail_hwdevice_create:
736
+ #endif
737
+
738
+ if (avcodec_open2(sys->codecDecoderContext, sys->codecDecoder, NULL) < 0)
739
+ {
740
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to open libav codec");
741
+ goto EXCEPTION;
742
+ }
743
+
744
+ sys->codecParser = av_parser_init(AV_CODEC_ID_H264);
745
+
746
+ if (!sys->codecParser)
747
+ {
748
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to initialize libav parser");
749
+ goto EXCEPTION;
750
+ }
751
+ }
752
+ else
753
+ {
754
+ #ifdef WITH_VAAPI_H264_ENCODING
755
+ if (h264->hwAccel) /* user requested hw accel */
756
+ {
757
+ sys->codecEncoder = avcodec_find_encoder_by_name("h264_vaapi");
758
+ if (!sys->codecEncoder)
759
+ {
760
+ WLog_Print(h264->log, WLOG_ERROR, "H264 VAAPI encoder not found");
761
+ }
762
+ else if (av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VAAPI, VAAPI_DEVICE, NULL,
763
+ 0) < 0)
764
+ {
765
+ WLog_Print(h264->log, WLOG_ERROR, "av_hwdevice_ctx_create failed");
766
+ sys->codecEncoder = NULL;
767
+ sys->hwctx = NULL;
768
+ }
769
+ else
770
+ {
771
+ WLog_Print(h264->log, WLOG_INFO, "Using VAAPI for accelerated H264 encoding");
772
+ }
773
+ }
774
+ #endif
775
+ if (!sys->codecEncoder)
776
+ {
777
+ sys->codecEncoder = avcodec_find_encoder(AV_CODEC_ID_H264);
778
+ h264->hwAccel = FALSE; /* not supported */
779
+ }
780
+
781
+ if (!sys->codecEncoder)
782
+ {
783
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to initialize H264 encoder");
784
+ goto EXCEPTION;
785
+ }
786
+ }
787
+
788
+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
789
+ sys->videoFrame = av_frame_alloc();
790
+ #if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING)
791
+ sys->hwVideoFrame = av_frame_alloc();
792
+ #endif
793
+ #else
794
+ sys->videoFrame = avcodec_alloc_frame();
795
+ #endif
796
+
797
+ if (!sys->videoFrame)
798
+ {
799
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav frame");
800
+ goto EXCEPTION;
801
+ }
802
+
803
+ #if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING)
804
+ if (!sys->hwVideoFrame)
805
+ {
806
+ WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav hw frame");
807
+ goto EXCEPTION;
808
+ }
809
+
810
+ #endif
811
+ sys->videoFrame->pts = 0;
812
+ return TRUE;
813
+ EXCEPTION:
814
+ libavcodec_uninit(h264);
815
+ return FALSE;
816
+ }
817
+
818
+ const H264_CONTEXT_SUBSYSTEM g_Subsystem_libavcodec = { "libavcodec", libavcodec_init,
819
+ libavcodec_uninit, libavcodec_decompress,
820
+ libavcodec_compress };
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/h264_mediacodec.c ADDED
@@ -0,0 +1,527 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * H.264 Bitmap Compression
4
+ *
5
+ * Copyright 2022 Ely Ronnen <elyronnen@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <winpr/wlog.h>
21
+ #include <winpr/assert.h>
22
+ #include <winpr/library.h>
23
+
24
+ #include <freerdp/log.h>
25
+ #include <freerdp/codec/h264.h>
26
+
27
+ #include <media/NdkMediaCodec.h>
28
+ #include <media/NdkMediaFormat.h>
29
+
30
+ #include "h264.h"
31
+
32
+ static const char* CODEC_NAME = "video/avc";
33
+
34
+ static const int COLOR_FormatYUV420Planar = 19;
35
+ static const int COLOR_FormatYUV420Flexible = 0x7f420888;
36
+
37
+ /* https://developer.android.com/reference/android/media/MediaCodec#qualityFloor */
38
+ static const int MEDIACODEC_MINIMUM_WIDTH = 320;
39
+ static const int MEDIACODEC_MINIMUM_HEIGHT = 240;
40
+
41
+ typedef struct
42
+ {
43
+ AMediaCodec* decoder;
44
+ AMediaFormat* inputFormat;
45
+ AMediaFormat* outputFormat;
46
+ int32_t width;
47
+ int32_t height;
48
+ int32_t outputWidth;
49
+ int32_t outputHeight;
50
+ ssize_t currentOutputBufferIndex;
51
+ } H264_CONTEXT_MEDIACODEC;
52
+
53
+ static AMediaFormat* mediacodec_format_new(wLog* log, int width, int height)
54
+ {
55
+ const char* media_format;
56
+ AMediaFormat* format = AMediaFormat_new();
57
+ if (format == NULL)
58
+ {
59
+ WLog_Print(log, WLOG_ERROR, "AMediaFormat_new failed");
60
+ return NULL;
61
+ }
62
+
63
+ AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, CODEC_NAME);
64
+ AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, width);
65
+ AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, height);
66
+ AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT, COLOR_FormatYUV420Planar);
67
+
68
+ media_format = AMediaFormat_toString(format);
69
+ if (media_format == NULL)
70
+ {
71
+ WLog_Print(log, WLOG_ERROR, "AMediaFormat_toString failed");
72
+ AMediaFormat_delete(format);
73
+ return NULL;
74
+ }
75
+
76
+ WLog_Print(log, WLOG_DEBUG, "MediaCodec configuring with desired output format [%s]",
77
+ media_format);
78
+
79
+ return format;
80
+ }
81
+
82
+ static void set_mediacodec_format(H264_CONTEXT* h264, AMediaFormat** formatVariable,
83
+ AMediaFormat* newFormat)
84
+ {
85
+ media_status_t status = AMEDIA_OK;
86
+ H264_CONTEXT_MEDIACODEC* sys;
87
+
88
+ WINPR_ASSERT(h264);
89
+ WINPR_ASSERT(formatVariable);
90
+
91
+ sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
92
+ WINPR_ASSERT(sys);
93
+
94
+ if (*formatVariable == newFormat)
95
+ return;
96
+
97
+ if (*formatVariable != NULL)
98
+ {
99
+ status = AMediaFormat_delete(*formatVariable);
100
+ if (status != AMEDIA_OK)
101
+ {
102
+ WLog_Print(h264->log, WLOG_ERROR, "Error AMediaFormat_delete %d", status);
103
+ }
104
+ }
105
+
106
+ *formatVariable = newFormat;
107
+ }
108
+
109
+ static int update_mediacodec_inputformat(H264_CONTEXT* h264)
110
+ {
111
+ H264_CONTEXT_MEDIACODEC* sys;
112
+ AMediaFormat* inputFormat;
113
+ const char* mediaFormatName;
114
+
115
+ WINPR_ASSERT(h264);
116
+
117
+ sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
118
+ WINPR_ASSERT(sys);
119
+
120
+ #if __ANDROID__ >= 21
121
+ inputFormat = AMediaCodec_getInputFormat(sys->decoder);
122
+ if (inputFormat == NULL)
123
+ {
124
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getInputFormat failed");
125
+ return -1;
126
+ }
127
+ #else
128
+ inputFormat = sys->inputFormat;
129
+ #endif
130
+ set_mediacodec_format(h264, &sys->inputFormat, inputFormat);
131
+
132
+ mediaFormatName = AMediaFormat_toString(sys->inputFormat);
133
+ if (mediaFormatName == NULL)
134
+ {
135
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaFormat_toString failed");
136
+ return -1;
137
+ }
138
+ WLog_Print(h264->log, WLOG_DEBUG, "Using MediaCodec with input MediaFormat [%s]",
139
+ mediaFormatName);
140
+
141
+ return 1;
142
+ }
143
+
144
+ static int update_mediacodec_outputformat(H264_CONTEXT* h264)
145
+ {
146
+ H264_CONTEXT_MEDIACODEC* sys;
147
+ AMediaFormat* outputFormat;
148
+ const char* mediaFormatName;
149
+ int32_t outputWidth, outputHeight;
150
+
151
+ WINPR_ASSERT(h264);
152
+
153
+ sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
154
+ WINPR_ASSERT(sys);
155
+
156
+ outputFormat = AMediaCodec_getOutputFormat(sys->decoder);
157
+ if (outputFormat == NULL)
158
+ {
159
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getOutputFormat failed");
160
+ return -1;
161
+ }
162
+ set_mediacodec_format(h264, &sys->outputFormat, outputFormat);
163
+
164
+ mediaFormatName = AMediaFormat_toString(sys->outputFormat);
165
+ if (mediaFormatName == NULL)
166
+ {
167
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaFormat_toString failed");
168
+ return -1;
169
+ }
170
+ WLog_Print(h264->log, WLOG_DEBUG, "Using MediaCodec with output MediaFormat [%s]",
171
+ mediaFormatName);
172
+
173
+ if (!AMediaFormat_getInt32(sys->outputFormat, AMEDIAFORMAT_KEY_WIDTH, &outputWidth))
174
+ {
175
+ WLog_Print(h264->log, WLOG_ERROR, "fnAMediaFormat_getInt32 failed getting width");
176
+ return -1;
177
+ }
178
+
179
+ if (!AMediaFormat_getInt32(sys->outputFormat, AMEDIAFORMAT_KEY_HEIGHT, &outputHeight))
180
+ {
181
+ WLog_Print(h264->log, WLOG_ERROR, "fnAMediaFormat_getInt32 failed getting height");
182
+ return -1;
183
+ }
184
+
185
+ sys->outputWidth = outputWidth;
186
+ sys->outputHeight = outputHeight;
187
+
188
+ return 1;
189
+ }
190
+
191
+ static void release_current_outputbuffer(H264_CONTEXT* h264)
192
+ {
193
+ media_status_t status = AMEDIA_OK;
194
+ H264_CONTEXT_MEDIACODEC* sys;
195
+
196
+ WINPR_ASSERT(h264);
197
+ sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
198
+ WINPR_ASSERT(sys);
199
+
200
+ if (sys->currentOutputBufferIndex < 0)
201
+ {
202
+ return;
203
+ }
204
+
205
+ status = AMediaCodec_releaseOutputBuffer(sys->decoder, sys->currentOutputBufferIndex, FALSE);
206
+ if (status != AMEDIA_OK)
207
+ {
208
+ WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_releaseOutputBuffer %d", status);
209
+ }
210
+
211
+ sys->currentOutputBufferIndex = -1;
212
+ }
213
+
214
+ static int mediacodec_compress(H264_CONTEXT* h264, const BYTE** pSrcYuv, const UINT32* pStride,
215
+ BYTE** ppDstData, UINT32* pDstSize)
216
+ {
217
+ WINPR_ASSERT(h264);
218
+ WINPR_ASSERT(pSrcYuv);
219
+ WINPR_ASSERT(pStride);
220
+ WINPR_ASSERT(ppDstData);
221
+ WINPR_ASSERT(pDstSize);
222
+
223
+ WLog_Print(h264->log, WLOG_ERROR, "MediaCodec is not supported as an encoder");
224
+ return -1;
225
+ }
226
+
227
+ static int mediacodec_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize)
228
+ {
229
+ ssize_t inputBufferId = -1;
230
+ size_t inputBufferSize, outputBufferSize;
231
+ uint8_t* inputBuffer;
232
+ media_status_t status;
233
+ BYTE** pYUVData;
234
+ UINT32* iStride;
235
+ H264_CONTEXT_MEDIACODEC* sys;
236
+
237
+ WINPR_ASSERT(h264);
238
+ WINPR_ASSERT(pSrcData);
239
+
240
+ sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
241
+ WINPR_ASSERT(sys);
242
+
243
+ pYUVData = h264->pYUVData;
244
+ WINPR_ASSERT(pYUVData);
245
+
246
+ iStride = h264->iStride;
247
+ WINPR_ASSERT(iStride);
248
+
249
+ release_current_outputbuffer(h264);
250
+
251
+ if (sys->width != h264->width || sys->height != h264->height)
252
+ {
253
+ sys->width = h264->width;
254
+ sys->height = h264->height;
255
+
256
+ if (sys->width < MEDIACODEC_MINIMUM_WIDTH || sys->height < MEDIACODEC_MINIMUM_HEIGHT)
257
+ {
258
+ WLog_Print(h264->log, WLOG_ERROR,
259
+ "MediaCodec got width or height smaller than minimum [%d,%d]", sys->width,
260
+ sys->height);
261
+ return -1;
262
+ }
263
+
264
+ WLog_Print(h264->log, WLOG_DEBUG, "MediaCodec setting new input width and height [%d,%d]",
265
+ sys->width, sys->height);
266
+
267
+ #if __ANDROID__ >= 26
268
+ AMediaFormat_setInt32(sys->inputFormat, AMEDIAFORMAT_KEY_WIDTH, sys->width);
269
+ AMediaFormat_setInt32(sys->inputFormat, AMEDIAFORMAT_KEY_HEIGHT, sys->height);
270
+ status = AMediaCodec_setParameters(sys->decoder, sys->inputFormat);
271
+ if (status != AMEDIA_OK)
272
+ {
273
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_setParameters failed: %d", status);
274
+ return -1;
275
+ }
276
+ #else
277
+ set_mediacodec_format(h264, &sys->inputFormat,
278
+ mediacodec_format_new(h264->log, sys->width, sys->height));
279
+ #endif
280
+
281
+ // The codec can change output width and height
282
+ if (update_mediacodec_outputformat(h264) < 0)
283
+ {
284
+ WLog_Print(h264->log, WLOG_ERROR, "MediaCodec failed updating input format");
285
+ return -1;
286
+ }
287
+ }
288
+
289
+ while (true)
290
+ {
291
+ UINT32 inputBufferCurrnetOffset = 0;
292
+ while (inputBufferCurrnetOffset < SrcSize)
293
+ {
294
+ UINT32 numberOfBytesToCopy = SrcSize - inputBufferCurrnetOffset;
295
+ inputBufferId = AMediaCodec_dequeueInputBuffer(sys->decoder, -1);
296
+ if (inputBufferId < 0)
297
+ {
298
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_dequeueInputBuffer failed [%d]",
299
+ inputBufferId);
300
+ // TODO: sleep?
301
+ continue;
302
+ }
303
+
304
+ inputBuffer = AMediaCodec_getInputBuffer(sys->decoder, inputBufferId, &inputBufferSize);
305
+ if (inputBuffer == NULL)
306
+ {
307
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getInputBuffer failed");
308
+ return -1;
309
+ }
310
+
311
+ if (numberOfBytesToCopy > inputBufferSize)
312
+ {
313
+ WLog_Print(h264->log, WLOG_WARN,
314
+ "MediaCodec inputBufferSize: got [%d] but wanted [%d]", inputBufferSize,
315
+ numberOfBytesToCopy);
316
+ numberOfBytesToCopy = inputBufferSize;
317
+ }
318
+
319
+ memcpy(inputBuffer, pSrcData + inputBufferCurrnetOffset, numberOfBytesToCopy);
320
+ inputBufferCurrnetOffset += numberOfBytesToCopy;
321
+
322
+ status = AMediaCodec_queueInputBuffer(sys->decoder, inputBufferId, 0,
323
+ numberOfBytesToCopy, 0, 0);
324
+ if (status != AMEDIA_OK)
325
+ {
326
+ WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_queueInputBuffer %d", status);
327
+ return -1;
328
+ }
329
+ }
330
+
331
+ while (true)
332
+ {
333
+ AMediaCodecBufferInfo bufferInfo;
334
+ ssize_t outputBufferId = AMediaCodec_dequeueOutputBuffer(sys->decoder, &bufferInfo, -1);
335
+ if (outputBufferId >= 0)
336
+ {
337
+ sys->currentOutputBufferIndex = outputBufferId;
338
+
339
+ uint8_t* outputBuffer;
340
+ outputBuffer =
341
+ AMediaCodec_getOutputBuffer(sys->decoder, outputBufferId, &outputBufferSize);
342
+ sys->currentOutputBufferIndex = outputBufferId;
343
+
344
+ if (outputBufferSize !=
345
+ (sys->outputWidth * sys->outputHeight +
346
+ ((sys->outputWidth + 1) / 2) * ((sys->outputHeight + 1) / 2) * 2))
347
+ {
348
+ WLog_Print(h264->log, WLOG_ERROR,
349
+ "Error MediaCodec unexpected output buffer size %d",
350
+ outputBufferSize);
351
+ return -1;
352
+ }
353
+
354
+ // TODO: work with AImageReader and get AImage object instead of
355
+ // COLOR_FormatYUV420Planar buffer.
356
+ iStride[0] = sys->outputWidth;
357
+ iStride[1] = (sys->outputWidth + 1) / 2;
358
+ iStride[2] = (sys->outputWidth + 1) / 2;
359
+ pYUVData[0] = outputBuffer;
360
+ pYUVData[1] = outputBuffer + iStride[0] * sys->outputHeight;
361
+ pYUVData[2] = outputBuffer + iStride[0] * sys->outputHeight +
362
+ iStride[1] * ((sys->outputHeight + 1) / 2);
363
+ break;
364
+ }
365
+ else if (outputBufferId == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED)
366
+ {
367
+ if (update_mediacodec_outputformat(h264) < 0)
368
+ {
369
+ WLog_Print(h264->log, WLOG_ERROR,
370
+ "MediaCodec failed updating output format in decompress");
371
+ return -1;
372
+ }
373
+ }
374
+ else if (outputBufferId == AMEDIACODEC_INFO_TRY_AGAIN_LATER)
375
+ {
376
+ WLog_Print(h264->log, WLOG_WARN,
377
+ "AMediaCodec_dequeueOutputBuffer need to try again later");
378
+ // TODO: sleep?
379
+ }
380
+ else if (outputBufferId == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED)
381
+ {
382
+ WLog_Print(h264->log, WLOG_WARN,
383
+ "AMediaCodec_dequeueOutputBuffer returned deprecated value "
384
+ "AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED, ignoring");
385
+ }
386
+ else
387
+ {
388
+ WLog_Print(h264->log, WLOG_ERROR,
389
+ "AMediaCodec_dequeueOutputBuffer returned unknown value [%d]",
390
+ outputBufferId);
391
+ return -1;
392
+ }
393
+ }
394
+
395
+ break;
396
+ }
397
+
398
+ return 1;
399
+ }
400
+
401
+ static void mediacodec_uninit(H264_CONTEXT* h264)
402
+ {
403
+ media_status_t status = AMEDIA_OK;
404
+ H264_CONTEXT_MEDIACODEC* sys;
405
+
406
+ WINPR_ASSERT(h264);
407
+
408
+ sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
409
+
410
+ WLog_Print(h264->log, WLOG_DEBUG, "Uninitializing MediaCodec");
411
+
412
+ if (!sys)
413
+ return;
414
+
415
+ if (sys->decoder != NULL)
416
+ {
417
+ release_current_outputbuffer(h264);
418
+ status = AMediaCodec_stop(sys->decoder);
419
+ if (status != AMEDIA_OK)
420
+ {
421
+ WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_stop %d", status);
422
+ }
423
+
424
+ status = AMediaCodec_delete(sys->decoder);
425
+ if (status != AMEDIA_OK)
426
+ {
427
+ WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_delete %d", status);
428
+ }
429
+
430
+ sys->decoder = NULL;
431
+ }
432
+
433
+ set_mediacodec_format(h264, &sys->inputFormat, NULL);
434
+ set_mediacodec_format(h264, &sys->outputFormat, NULL);
435
+
436
+ free(sys);
437
+ h264->pSystemData = NULL;
438
+ }
439
+
440
+ static BOOL mediacodec_init(H264_CONTEXT* h264)
441
+ {
442
+ H264_CONTEXT_MEDIACODEC* sys;
443
+ media_status_t status;
444
+
445
+ WINPR_ASSERT(h264);
446
+
447
+ if (h264->Compressor)
448
+ {
449
+ WLog_Print(h264->log, WLOG_ERROR, "MediaCodec is not supported as an encoder");
450
+ goto EXCEPTION;
451
+ }
452
+
453
+ WLog_Print(h264->log, WLOG_DEBUG, "Initializing MediaCodec");
454
+
455
+ sys = (H264_CONTEXT_MEDIACODEC*)calloc(1, sizeof(H264_CONTEXT_MEDIACODEC));
456
+
457
+ if (!sys)
458
+ {
459
+ goto EXCEPTION;
460
+ }
461
+
462
+ h264->pSystemData = (void*)sys;
463
+
464
+ sys->currentOutputBufferIndex = -1;
465
+
466
+ // updated when we're given the height and width for the first time
467
+ sys->width = sys->outputWidth = MEDIACODEC_MINIMUM_WIDTH;
468
+ sys->height = sys->outputHeight = MEDIACODEC_MINIMUM_HEIGHT;
469
+ sys->decoder = AMediaCodec_createDecoderByType(CODEC_NAME);
470
+ if (sys->decoder == NULL)
471
+ {
472
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_createCodecByName failed");
473
+ goto EXCEPTION;
474
+ }
475
+
476
+ #if __ANDROID_API__ >= 28
477
+ char* codec_name;
478
+ status = AMediaCodec_getName(sys->decoder, &codec_name);
479
+ if (status != AMEDIA_OK)
480
+ {
481
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getName failed: %d", status);
482
+ goto EXCEPTION;
483
+ }
484
+
485
+ WLog_Print(h264->log, WLOG_DEBUG, "MediaCodec using %s codec [%s]", CODEC_NAME, codec_name);
486
+ AMediaCodec_releaseName(sys->decoder, codec_name);
487
+ #endif
488
+
489
+ set_mediacodec_format(h264, &sys->inputFormat,
490
+ mediacodec_format_new(h264->log, sys->width, sys->height));
491
+
492
+ status = AMediaCodec_configure(sys->decoder, sys->inputFormat, NULL, NULL, 0);
493
+ if (status != AMEDIA_OK)
494
+ {
495
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_configure failed: %d", status);
496
+ goto EXCEPTION;
497
+ }
498
+
499
+ if (update_mediacodec_inputformat(h264) < 0)
500
+ {
501
+ WLog_Print(h264->log, WLOG_ERROR, "MediaCodec failed updating input format");
502
+ goto EXCEPTION;
503
+ }
504
+
505
+ if (update_mediacodec_outputformat(h264) < 0)
506
+ {
507
+ WLog_Print(h264->log, WLOG_ERROR, "MediaCodec failed updating output format");
508
+ goto EXCEPTION;
509
+ }
510
+
511
+ WLog_Print(h264->log, WLOG_DEBUG, "Starting MediaCodec");
512
+ status = AMediaCodec_start(sys->decoder);
513
+ if (status != AMEDIA_OK)
514
+ {
515
+ WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_start failed %d", status);
516
+ goto EXCEPTION;
517
+ }
518
+
519
+ return TRUE;
520
+ EXCEPTION:
521
+ mediacodec_uninit(h264);
522
+ return FALSE;
523
+ }
524
+
525
+ const H264_CONTEXT_SUBSYSTEM g_Subsystem_mediacodec = { "MediaCodec", mediacodec_init,
526
+ mediacodec_uninit, mediacodec_decompress,
527
+ mediacodec_compress };
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/nsc.c ADDED
@@ -0,0 +1,512 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * NSCodec Codec
4
+ *
5
+ * Copyright 2011 Samsung, Author Jiten Pathy
6
+ * Copyright 2012 Vic Lee
7
+ * Copyright 2016 Armin Novak <armin.novak@thincast.com>
8
+ * Copyright 2016 Thincast Technologies GmbH
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+
23
+ #include <freerdp/config.h>
24
+
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+
29
+ #include <winpr/assert.h>
30
+ #include <winpr/cast.h>
31
+ #include <winpr/crt.h>
32
+
33
+ #include <freerdp/codec/nsc.h>
34
+ #include <freerdp/codec/color.h>
35
+
36
+ #include "nsc_types.h"
37
+ #include "nsc_encode.h"
38
+
39
+ #include "sse/nsc_sse2.h"
40
+ #include "neon/nsc_neon.h"
41
+
42
+ #include <freerdp/log.h>
43
+
44
+ static BOOL nsc_decode(NSC_CONTEXT* WINPR_RESTRICT context)
45
+ {
46
+ size_t pos = 0;
47
+
48
+ if (!context)
49
+ return FALSE;
50
+
51
+ const UINT16 rw = ROUND_UP_TO(context->width, 8);
52
+ WINPR_ASSERT(context->ColorLossLevel >= 1);
53
+ const BYTE shift = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel -
54
+ 1); /* colorloss recovery + YCoCg shift */
55
+ BYTE* bmpdata = context->BitmapData;
56
+
57
+ if (!bmpdata)
58
+ return FALSE;
59
+
60
+ for (size_t y = 0; y < context->height; y++)
61
+ {
62
+ const BYTE* yplane = NULL;
63
+ const BYTE* coplane = NULL;
64
+ const BYTE* cgplane = NULL;
65
+ const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width; /* A */
66
+
67
+ if (context->ChromaSubsamplingLevel)
68
+ {
69
+ yplane = context->priv->PlaneBuffers[0] + y * rw; /* Y */
70
+ coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
71
+ cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
72
+ }
73
+ else
74
+ {
75
+ yplane = context->priv->PlaneBuffers[0] + y * context->width; /* Y */
76
+ coplane = context->priv->PlaneBuffers[1] + y * context->width; /* Co */
77
+ cgplane = context->priv->PlaneBuffers[2] + y * context->width; /* Cg */
78
+ }
79
+
80
+ for (UINT32 x = 0; x < context->width; x++)
81
+ {
82
+ INT16 y_val = (INT16)*yplane;
83
+ INT16 co_val = (INT16)(INT8)(((INT16)*coplane) << shift);
84
+ INT16 cg_val = (INT16)(INT8)(((INT16)*cgplane) << shift);
85
+ INT16 r_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + co_val - cg_val);
86
+ INT16 g_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + cg_val);
87
+ INT16 b_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val - co_val - cg_val);
88
+
89
+ if (pos + 4 > context->BitmapDataLength)
90
+ return FALSE;
91
+
92
+ pos += 4;
93
+ *bmpdata++ = MINMAX(b_val, 0, 0xFF);
94
+ *bmpdata++ = MINMAX(g_val, 0, 0xFF);
95
+ *bmpdata++ = MINMAX(r_val, 0, 0xFF);
96
+ *bmpdata++ = *aplane;
97
+ yplane++;
98
+ coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
99
+ cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
100
+ aplane++;
101
+ }
102
+ }
103
+
104
+ return TRUE;
105
+ }
106
+
107
+ static BOOL nsc_rle_decode(const BYTE* WINPR_RESTRICT in, size_t inSize, BYTE* WINPR_RESTRICT out,
108
+ UINT32 outSize, UINT32 originalSize)
109
+ {
110
+ UINT32 left = originalSize;
111
+
112
+ while (left > 4)
113
+ {
114
+ if (inSize < 1)
115
+ return FALSE;
116
+ inSize--;
117
+
118
+ const BYTE value = *in++;
119
+ UINT32 len = 0;
120
+
121
+ if (left == 5)
122
+ {
123
+ if (outSize < 1)
124
+ return FALSE;
125
+
126
+ outSize--;
127
+ *out++ = value;
128
+ left--;
129
+ }
130
+ else if (inSize < 1)
131
+ return FALSE;
132
+ else if (value == *in)
133
+ {
134
+ inSize--;
135
+ in++;
136
+
137
+ if (inSize < 1)
138
+ return FALSE;
139
+ else if (*in < 0xFF)
140
+ {
141
+ inSize--;
142
+ len = (UINT32)*in++;
143
+ len += 2;
144
+ }
145
+ else
146
+ {
147
+ if (inSize < 5)
148
+ return FALSE;
149
+ inSize -= 5;
150
+ in++;
151
+ len = ((UINT32)(*in++));
152
+ len |= ((UINT32)(*in++)) << 8U;
153
+ len |= ((UINT32)(*in++)) << 16U;
154
+ len |= ((UINT32)(*in++)) << 24U;
155
+ }
156
+
157
+ if ((outSize < len) || (left < len))
158
+ return FALSE;
159
+
160
+ outSize -= len;
161
+ FillMemory(out, len, value);
162
+ out += len;
163
+ left -= len;
164
+ }
165
+ else
166
+ {
167
+ if (outSize < 1)
168
+ return FALSE;
169
+
170
+ outSize--;
171
+ *out++ = value;
172
+ left--;
173
+ }
174
+ }
175
+
176
+ if ((outSize < 4) || (left < 4))
177
+ return FALSE;
178
+
179
+ if (inSize < 4)
180
+ return FALSE;
181
+ memcpy(out, in, 4);
182
+ return TRUE;
183
+ }
184
+
185
+ static BOOL nsc_rle_decompress_data(NSC_CONTEXT* WINPR_RESTRICT context)
186
+ {
187
+ if (!context)
188
+ return FALSE;
189
+
190
+ const BYTE* rle = context->Planes;
191
+ size_t rleSize = context->PlanesSize;
192
+ WINPR_ASSERT(rle);
193
+
194
+ for (size_t i = 0; i < 4; i++)
195
+ {
196
+ const UINT32 originalSize = context->OrgByteCount[i];
197
+ const UINT32 planeSize = context->PlaneByteCount[i];
198
+
199
+ if (rleSize < planeSize)
200
+ return FALSE;
201
+
202
+ if (planeSize == 0)
203
+ {
204
+ if (context->priv->PlaneBuffersLength < originalSize)
205
+ return FALSE;
206
+
207
+ FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
208
+ }
209
+ else if (planeSize < originalSize)
210
+ {
211
+ if (!nsc_rle_decode(rle, rleSize, context->priv->PlaneBuffers[i],
212
+ context->priv->PlaneBuffersLength, originalSize))
213
+ return FALSE;
214
+ }
215
+ else
216
+ {
217
+ if (context->priv->PlaneBuffersLength < originalSize)
218
+ return FALSE;
219
+
220
+ if (rleSize < originalSize)
221
+ return FALSE;
222
+
223
+ CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
224
+ }
225
+
226
+ rle += planeSize;
227
+ rleSize -= planeSize;
228
+ }
229
+
230
+ return TRUE;
231
+ }
232
+
233
+ static BOOL nsc_stream_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
234
+ {
235
+ WINPR_ASSERT(context);
236
+ WINPR_ASSERT(context->priv);
237
+ if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 20))
238
+ return FALSE;
239
+
240
+ size_t total = 0;
241
+ for (size_t i = 0; i < 4; i++)
242
+ {
243
+ Stream_Read_UINT32(s, context->PlaneByteCount[i]);
244
+ total += context->PlaneByteCount[i];
245
+ }
246
+
247
+ Stream_Read_UINT8(s, context->ColorLossLevel); /* ColorLossLevel (1 byte) */
248
+ if ((context->ColorLossLevel < 1) || (context->ColorLossLevel > 7))
249
+ {
250
+ WLog_Print(context->priv->log, WLOG_ERROR,
251
+ "ColorLossLevel=%" PRIu8 " out of range, must be [1,7] inclusive",
252
+ context->ColorLossLevel);
253
+ return FALSE;
254
+ }
255
+ Stream_Read_UINT8(s, context->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
256
+ Stream_Seek(s, 2); /* Reserved (2 bytes) */
257
+ context->Planes = Stream_Pointer(s);
258
+ context->PlanesSize = total;
259
+ return Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, total);
260
+ }
261
+
262
+ static BOOL nsc_context_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
263
+ {
264
+ if (!nsc_stream_initialize(context, s))
265
+ return FALSE;
266
+
267
+ const size_t blength = 4ull * context->width * context->height;
268
+
269
+ if (!context->BitmapData || (blength > context->BitmapDataLength))
270
+ {
271
+ void* tmp = winpr_aligned_recalloc(context->BitmapData, blength + 16, sizeof(BYTE), 32);
272
+
273
+ if (!tmp)
274
+ return FALSE;
275
+
276
+ context->BitmapData = tmp;
277
+ context->BitmapDataLength = blength;
278
+ }
279
+
280
+ const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
281
+ const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
282
+ /* The maximum length a decoded plane can reach in all cases */
283
+ const size_t plength = 1ull * tempWidth * tempHeight;
284
+ if (plength > UINT32_MAX)
285
+ return FALSE;
286
+
287
+ if (plength > context->priv->PlaneBuffersLength)
288
+ {
289
+ for (size_t i = 0; i < 4; i++)
290
+ {
291
+ void* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], plength,
292
+ sizeof(BYTE), 32);
293
+
294
+ if (!tmp)
295
+ return FALSE;
296
+
297
+ context->priv->PlaneBuffers[i] = tmp;
298
+ }
299
+
300
+ context->priv->PlaneBuffersLength = (UINT32)plength;
301
+ }
302
+
303
+ for (size_t i = 0; i < 4; i++)
304
+ context->OrgByteCount[i] = context->width * context->height;
305
+
306
+ if (context->ChromaSubsamplingLevel)
307
+ {
308
+ context->OrgByteCount[0] = tempWidth * context->height;
309
+ context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
310
+ context->OrgByteCount[2] = context->OrgByteCount[1];
311
+ }
312
+
313
+ return TRUE;
314
+ }
315
+
316
+ static void nsc_profiler_print(NSC_CONTEXT_PRIV* WINPR_RESTRICT priv)
317
+ {
318
+ WINPR_UNUSED(priv);
319
+
320
+ PROFILER_PRINT_HEADER
321
+ PROFILER_PRINT(priv->prof_nsc_rle_decompress_data)
322
+ PROFILER_PRINT(priv->prof_nsc_decode)
323
+ PROFILER_PRINT(priv->prof_nsc_rle_compress_data)
324
+ PROFILER_PRINT(priv->prof_nsc_encode)
325
+ PROFILER_PRINT_FOOTER
326
+ }
327
+
328
+ BOOL nsc_context_reset(NSC_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
329
+ {
330
+ if (!context)
331
+ return FALSE;
332
+
333
+ if ((width > UINT16_MAX) || (height > UINT16_MAX))
334
+ return FALSE;
335
+
336
+ context->width = (UINT16)width;
337
+ context->height = (UINT16)height;
338
+ return TRUE;
339
+ }
340
+
341
+ NSC_CONTEXT* nsc_context_new(void)
342
+ {
343
+ NSC_CONTEXT* context = (NSC_CONTEXT*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT), 32);
344
+
345
+ if (!context)
346
+ return NULL;
347
+
348
+ context->priv = (NSC_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT_PRIV), 32);
349
+
350
+ if (!context->priv)
351
+ goto error;
352
+
353
+ context->priv->log = WLog_Get("com.freerdp.codec.nsc");
354
+ WLog_OpenAppender(context->priv->log);
355
+ context->BitmapData = NULL;
356
+ context->decode = nsc_decode;
357
+ context->encode = nsc_encode;
358
+
359
+ PROFILER_CREATE(context->priv->prof_nsc_rle_decompress_data, "nsc_rle_decompress_data")
360
+ PROFILER_CREATE(context->priv->prof_nsc_decode, "nsc_decode")
361
+ PROFILER_CREATE(context->priv->prof_nsc_rle_compress_data, "nsc_rle_compress_data")
362
+ PROFILER_CREATE(context->priv->prof_nsc_encode, "nsc_encode")
363
+ /* Default encoding parameters */
364
+ context->ColorLossLevel = 3;
365
+ context->ChromaSubsamplingLevel = 1;
366
+ /* init optimized methods */
367
+ nsc_init_sse2(context);
368
+ nsc_init_neon(context);
369
+ return context;
370
+ error:
371
+ WINPR_PRAGMA_DIAG_PUSH
372
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
373
+ nsc_context_free(context);
374
+ WINPR_PRAGMA_DIAG_POP
375
+ return NULL;
376
+ }
377
+
378
+ void nsc_context_free(NSC_CONTEXT* context)
379
+ {
380
+ if (!context)
381
+ return;
382
+
383
+ if (context->priv)
384
+ {
385
+ for (size_t i = 0; i < 5; i++)
386
+ winpr_aligned_free(context->priv->PlaneBuffers[i]);
387
+
388
+ nsc_profiler_print(context->priv);
389
+ PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
390
+ PROFILER_FREE(context->priv->prof_nsc_decode)
391
+ PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
392
+ PROFILER_FREE(context->priv->prof_nsc_encode)
393
+ winpr_aligned_free(context->priv);
394
+ }
395
+
396
+ winpr_aligned_free(context->BitmapData);
397
+ winpr_aligned_free(context);
398
+ }
399
+
400
+ #if defined(WITH_FREERDP_DEPRECATED)
401
+ BOOL nsc_context_set_pixel_format(NSC_CONTEXT* context, UINT32 pixel_format)
402
+ {
403
+ return nsc_context_set_parameters(context, NSC_COLOR_FORMAT, pixel_format);
404
+ }
405
+ #endif
406
+
407
+ BOOL nsc_context_set_parameters(NSC_CONTEXT* WINPR_RESTRICT context, NSC_PARAMETER what,
408
+ UINT32 value)
409
+ {
410
+ if (!context)
411
+ return FALSE;
412
+
413
+ switch (what)
414
+ {
415
+ case NSC_COLOR_LOSS_LEVEL:
416
+ context->ColorLossLevel = value;
417
+ break;
418
+ case NSC_ALLOW_SUBSAMPLING:
419
+ context->ChromaSubsamplingLevel = value;
420
+ break;
421
+ case NSC_DYNAMIC_COLOR_FIDELITY:
422
+ context->DynamicColorFidelity = value != 0;
423
+ break;
424
+ case NSC_COLOR_FORMAT:
425
+ context->format = value;
426
+ break;
427
+ default:
428
+ return FALSE;
429
+ }
430
+ return TRUE;
431
+ }
432
+
433
+ BOOL nsc_process_message(NSC_CONTEXT* WINPR_RESTRICT context, UINT16 bpp, UINT32 width,
434
+ UINT32 height, const BYTE* data, UINT32 length,
435
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStride,
436
+ UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 flip)
437
+ {
438
+ wStream* s = NULL;
439
+ wStream sbuffer = { 0 };
440
+ BOOL ret = 0;
441
+ if (!context || !data || !pDstData)
442
+ return FALSE;
443
+
444
+ s = Stream_StaticConstInit(&sbuffer, data, length);
445
+
446
+ if (!s)
447
+ return FALSE;
448
+
449
+ if (nDstStride == 0)
450
+ nDstStride = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
451
+
452
+ switch (bpp)
453
+ {
454
+ case 32:
455
+ context->format = PIXEL_FORMAT_BGRA32;
456
+ break;
457
+
458
+ case 24:
459
+ context->format = PIXEL_FORMAT_BGR24;
460
+ break;
461
+
462
+ case 16:
463
+ context->format = PIXEL_FORMAT_BGR16;
464
+ break;
465
+
466
+ case 8:
467
+ context->format = PIXEL_FORMAT_RGB8;
468
+ break;
469
+
470
+ case 4:
471
+ context->format = PIXEL_FORMAT_A4;
472
+ break;
473
+
474
+ default:
475
+ return FALSE;
476
+ }
477
+
478
+ context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
479
+ context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
480
+ ret = nsc_context_initialize(context, s);
481
+
482
+ if (!ret)
483
+ return FALSE;
484
+
485
+ /* RLE decode */
486
+ {
487
+ BOOL rc = 0;
488
+ PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
489
+ rc = nsc_rle_decompress_data(context);
490
+ PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
491
+
492
+ if (!rc)
493
+ return FALSE;
494
+ }
495
+ /* Colorloss recover, Chroma supersample and AYCoCg to ARGB Conversion in one step */
496
+ {
497
+ BOOL rc = 0;
498
+ PROFILER_ENTER(context->priv->prof_nsc_decode)
499
+ rc = context->decode(context);
500
+ PROFILER_EXIT(context->priv->prof_nsc_decode)
501
+
502
+ if (!rc)
503
+ return FALSE;
504
+ }
505
+
506
+ if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStride, nXDst, nYDst, width, height,
507
+ context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0, 0, NULL,
508
+ flip))
509
+ return FALSE;
510
+
511
+ return TRUE;
512
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/nsc_encode.c ADDED
@@ -0,0 +1,541 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * NSCodec Encoder
4
+ *
5
+ * Copyright 2012 Vic Lee
6
+ * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <stdio.h>
25
+ #include <stdlib.h>
26
+ #include <string.h>
27
+
28
+ #include <winpr/assert.h>
29
+ #include <winpr/cast.h>
30
+ #include <winpr/crt.h>
31
+
32
+ #include <freerdp/codec/nsc.h>
33
+ #include <freerdp/codec/color.h>
34
+
35
+ #include "nsc_types.h"
36
+ #include "nsc_encode.h"
37
+
38
+ typedef struct
39
+ {
40
+ UINT32 x;
41
+ UINT32 y;
42
+ UINT32 width;
43
+ UINT32 height;
44
+ const BYTE* data;
45
+ UINT32 scanline;
46
+ BYTE* PlaneBuffer;
47
+ UINT32 MaxPlaneSize;
48
+ BYTE* PlaneBuffers[5];
49
+ UINT32 OrgByteCount[4];
50
+
51
+ UINT32 LumaPlaneByteCount;
52
+ UINT32 OrangeChromaPlaneByteCount;
53
+ UINT32 GreenChromaPlaneByteCount;
54
+ UINT32 AlphaPlaneByteCount;
55
+ UINT8 ColorLossLevel;
56
+ UINT8 ChromaSubsamplingLevel;
57
+ } NSC_MESSAGE;
58
+
59
+ static BOOL nsc_write_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
60
+ const NSC_MESSAGE* WINPR_RESTRICT message);
61
+
62
+ static BOOL nsc_context_initialize_encode(NSC_CONTEXT* WINPR_RESTRICT context)
63
+ {
64
+ UINT32 length = 0;
65
+ UINT32 tempWidth = 0;
66
+ UINT32 tempHeight = 0;
67
+ tempWidth = ROUND_UP_TO(context->width, 8);
68
+ tempHeight = ROUND_UP_TO(context->height, 2);
69
+ /* The maximum length a decoded plane can reach in all cases */
70
+ length = tempWidth * tempHeight + 16;
71
+
72
+ if (length > context->priv->PlaneBuffersLength)
73
+ {
74
+ for (int i = 0; i < 5; i++)
75
+ {
76
+ BYTE* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], length,
77
+ sizeof(BYTE), 32);
78
+
79
+ if (!tmp)
80
+ goto fail;
81
+
82
+ context->priv->PlaneBuffers[i] = tmp;
83
+ }
84
+
85
+ context->priv->PlaneBuffersLength = length;
86
+ }
87
+
88
+ if (context->ChromaSubsamplingLevel)
89
+ {
90
+ context->OrgByteCount[0] = tempWidth * context->height;
91
+ context->OrgByteCount[1] = tempWidth * tempHeight / 4;
92
+ context->OrgByteCount[2] = tempWidth * tempHeight / 4;
93
+ context->OrgByteCount[3] = context->width * context->height;
94
+ }
95
+ else
96
+ {
97
+ context->OrgByteCount[0] = context->width * context->height;
98
+ context->OrgByteCount[1] = context->width * context->height;
99
+ context->OrgByteCount[2] = context->width * context->height;
100
+ context->OrgByteCount[3] = context->width * context->height;
101
+ }
102
+
103
+ return TRUE;
104
+ fail:
105
+
106
+ if (length > context->priv->PlaneBuffersLength)
107
+ {
108
+ for (int i = 0; i < 5; i++)
109
+ winpr_aligned_free(context->priv->PlaneBuffers[i]);
110
+ }
111
+
112
+ return FALSE;
113
+ }
114
+
115
+ static BOOL nsc_encode_argb_to_aycocg(NSC_CONTEXT* WINPR_RESTRICT context,
116
+ const BYTE* WINPR_RESTRICT data, UINT32 scanline)
117
+ {
118
+ size_t y = 0;
119
+ const BYTE* src = NULL;
120
+ BYTE* yplane = NULL;
121
+ BYTE* coplane = NULL;
122
+ BYTE* cgplane = NULL;
123
+ BYTE* aplane = NULL;
124
+ INT16 r_val = 0;
125
+ INT16 g_val = 0;
126
+ INT16 b_val = 0;
127
+ BYTE a_val = 0;
128
+
129
+ UINT16 tempWidth = ROUND_UP_TO(context->width, 8);
130
+ const UINT16 rw = (context->ChromaSubsamplingLevel ? tempWidth : context->width);
131
+ const BYTE ccl = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel);
132
+
133
+ for (; y < context->height; y++)
134
+ {
135
+ src = data + (context->height - 1 - y) * scanline;
136
+ yplane = context->priv->PlaneBuffers[0] + y * rw;
137
+ coplane = context->priv->PlaneBuffers[1] + y * rw;
138
+ cgplane = context->priv->PlaneBuffers[2] + y * rw;
139
+ aplane = context->priv->PlaneBuffers[3] + y * context->width;
140
+
141
+ UINT16 x = 0;
142
+ for (; x < context->width; x++)
143
+ {
144
+ switch (context->format)
145
+ {
146
+ case PIXEL_FORMAT_BGRX32:
147
+ b_val = *src++;
148
+ g_val = *src++;
149
+ r_val = *src++;
150
+ src++;
151
+ a_val = 0xFF;
152
+ break;
153
+
154
+ case PIXEL_FORMAT_BGRA32:
155
+ b_val = *src++;
156
+ g_val = *src++;
157
+ r_val = *src++;
158
+ a_val = *src++;
159
+ break;
160
+
161
+ case PIXEL_FORMAT_RGBX32:
162
+ r_val = *src++;
163
+ g_val = *src++;
164
+ b_val = *src++;
165
+ src++;
166
+ a_val = 0xFF;
167
+ break;
168
+
169
+ case PIXEL_FORMAT_RGBA32:
170
+ r_val = *src++;
171
+ g_val = *src++;
172
+ b_val = *src++;
173
+ a_val = *src++;
174
+ break;
175
+
176
+ case PIXEL_FORMAT_BGR24:
177
+ b_val = *src++;
178
+ g_val = *src++;
179
+ r_val = *src++;
180
+ a_val = 0xFF;
181
+ break;
182
+
183
+ case PIXEL_FORMAT_RGB24:
184
+ r_val = *src++;
185
+ g_val = *src++;
186
+ b_val = *src++;
187
+ a_val = 0xFF;
188
+ break;
189
+
190
+ case PIXEL_FORMAT_BGR16:
191
+ b_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
192
+ g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
193
+ r_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
194
+ a_val = 0xFF;
195
+ src += 2;
196
+ break;
197
+
198
+ case PIXEL_FORMAT_RGB16:
199
+ r_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
200
+ g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
201
+ b_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
202
+ a_val = 0xFF;
203
+ src += 2;
204
+ break;
205
+
206
+ case PIXEL_FORMAT_A4:
207
+ {
208
+ int shift = 0;
209
+ BYTE idx = 0;
210
+ shift = (7 - (x % 8));
211
+ idx = ((*src) >> shift) & 1;
212
+ idx |= (((*(src + 1)) >> shift) & 1) << 1;
213
+ idx |= (((*(src + 2)) >> shift) & 1) << 2;
214
+ idx |= (((*(src + 3)) >> shift) & 1) << 3;
215
+ idx *= 3;
216
+ r_val = (INT16)context->palette[idx];
217
+ g_val = (INT16)context->palette[idx + 1];
218
+ b_val = (INT16)context->palette[idx + 2];
219
+
220
+ if (shift == 0)
221
+ src += 4;
222
+ }
223
+
224
+ a_val = 0xFF;
225
+ break;
226
+
227
+ case PIXEL_FORMAT_RGB8:
228
+ {
229
+ int idx = (*src) * 3;
230
+ r_val = (INT16)context->palette[idx];
231
+ g_val = (INT16)context->palette[idx + 1];
232
+ b_val = (INT16)context->palette[idx + 2];
233
+ src++;
234
+ }
235
+
236
+ a_val = 0xFF;
237
+ break;
238
+
239
+ default:
240
+ r_val = g_val = b_val = a_val = 0;
241
+ break;
242
+ }
243
+
244
+ *yplane++ = (BYTE)((r_val >> 2) + (g_val >> 1) + (b_val >> 2));
245
+ /* Perform color loss reduction here */
246
+ *coplane++ = (BYTE)((r_val - b_val) >> ccl);
247
+ *cgplane++ = (BYTE)((-(r_val >> 1) + g_val - (b_val >> 1)) >> ccl);
248
+ *aplane++ = a_val;
249
+ }
250
+
251
+ if (context->ChromaSubsamplingLevel && (x % 2) == 1)
252
+ {
253
+ *yplane = *(yplane - 1);
254
+ *coplane = *(coplane - 1);
255
+ *cgplane = *(cgplane - 1);
256
+ }
257
+ }
258
+
259
+ if (context->ChromaSubsamplingLevel && (y % 2) == 1)
260
+ {
261
+ yplane = context->priv->PlaneBuffers[0] + y * rw;
262
+ coplane = context->priv->PlaneBuffers[1] + y * rw;
263
+ cgplane = context->priv->PlaneBuffers[2] + y * rw;
264
+ CopyMemory(yplane, yplane - rw, rw);
265
+ CopyMemory(coplane, coplane - rw, rw);
266
+ CopyMemory(cgplane, cgplane - rw, rw);
267
+ }
268
+
269
+ return TRUE;
270
+ }
271
+
272
+ static BOOL nsc_encode_subsampling(NSC_CONTEXT* WINPR_RESTRICT context)
273
+ {
274
+ UINT32 tempWidth = 0;
275
+ UINT32 tempHeight = 0;
276
+
277
+ if (!context)
278
+ return FALSE;
279
+
280
+ tempWidth = ROUND_UP_TO(context->width, 8);
281
+ tempHeight = ROUND_UP_TO(context->height, 2);
282
+
283
+ if (tempHeight == 0)
284
+ return FALSE;
285
+
286
+ if (tempWidth > context->priv->PlaneBuffersLength / tempHeight)
287
+ return FALSE;
288
+
289
+ for (size_t y = 0; y < tempHeight >> 1; y++)
290
+ {
291
+ BYTE* co_dst = context->priv->PlaneBuffers[1] + y * (tempWidth >> 1);
292
+ BYTE* cg_dst = context->priv->PlaneBuffers[2] + y * (tempWidth >> 1);
293
+ const INT8* co_src0 = (INT8*)context->priv->PlaneBuffers[1] + (y << 1) * tempWidth;
294
+ const INT8* co_src1 = co_src0 + tempWidth;
295
+ const INT8* cg_src0 = (INT8*)context->priv->PlaneBuffers[2] + (y << 1) * tempWidth;
296
+ const INT8* cg_src1 = cg_src0 + tempWidth;
297
+
298
+ for (UINT32 x = 0; x < tempWidth >> 1; x++)
299
+ {
300
+ *co_dst++ = (BYTE)(((INT16)*co_src0 + (INT16) * (co_src0 + 1) + (INT16)*co_src1 +
301
+ (INT16) * (co_src1 + 1)) >>
302
+ 2);
303
+ *cg_dst++ = (BYTE)(((INT16)*cg_src0 + (INT16) * (cg_src0 + 1) + (INT16)*cg_src1 +
304
+ (INT16) * (cg_src1 + 1)) >>
305
+ 2);
306
+ co_src0 += 2;
307
+ co_src1 += 2;
308
+ cg_src0 += 2;
309
+ cg_src1 += 2;
310
+ }
311
+ }
312
+
313
+ return TRUE;
314
+ }
315
+
316
+ BOOL nsc_encode(NSC_CONTEXT* WINPR_RESTRICT context, const BYTE* WINPR_RESTRICT bmpdata,
317
+ UINT32 rowstride)
318
+ {
319
+ if (!context || !bmpdata || (rowstride == 0))
320
+ return FALSE;
321
+
322
+ if (!nsc_encode_argb_to_aycocg(context, bmpdata, rowstride))
323
+ return FALSE;
324
+
325
+ if (context->ChromaSubsamplingLevel)
326
+ {
327
+ if (!nsc_encode_subsampling(context))
328
+ return FALSE;
329
+ }
330
+
331
+ return TRUE;
332
+ }
333
+
334
+ static UINT32 nsc_rle_encode(const BYTE* WINPR_RESTRICT in, BYTE* WINPR_RESTRICT out,
335
+ UINT32 originalSize)
336
+ {
337
+ UINT32 left = 0;
338
+ UINT32 runlength = 1;
339
+ UINT32 planeSize = 0;
340
+ left = originalSize;
341
+
342
+ /**
343
+ * We quit the loop if the running compressed size is larger than the original.
344
+ * In such cases data will be sent uncompressed.
345
+ */
346
+ while (left > 4 && planeSize < originalSize - 4)
347
+ {
348
+ if (left > 5 && *in == *(in + 1))
349
+ {
350
+ runlength++;
351
+ }
352
+ else if (runlength == 1)
353
+ {
354
+ *out++ = *in;
355
+ planeSize++;
356
+ }
357
+ else if (runlength < 256)
358
+ {
359
+ *out++ = *in;
360
+ *out++ = *in;
361
+ WINPR_ASSERT(runlength >= 2);
362
+ *out++ = WINPR_ASSERTING_INT_CAST(BYTE, runlength - 2);
363
+ runlength = 1;
364
+ planeSize += 3;
365
+ }
366
+ else
367
+ {
368
+ *out++ = *in;
369
+ *out++ = *in;
370
+ *out++ = 0xFF;
371
+ *out++ = (runlength & 0x000000FF);
372
+ *out++ = (runlength & 0x0000FF00) >> 8;
373
+ *out++ = (runlength & 0x00FF0000) >> 16;
374
+ *out++ = (runlength & 0xFF000000) >> 24;
375
+ runlength = 1;
376
+ planeSize += 7;
377
+ }
378
+
379
+ in++;
380
+ left--;
381
+ }
382
+
383
+ if (planeSize < originalSize - 4)
384
+ CopyMemory(out, in, 4);
385
+
386
+ planeSize += 4;
387
+ return planeSize;
388
+ }
389
+
390
+ static void nsc_rle_compress_data(NSC_CONTEXT* WINPR_RESTRICT context)
391
+ {
392
+ UINT32 planeSize = 0;
393
+ UINT32 originalSize = 0;
394
+
395
+ for (UINT16 i = 0; i < 4; i++)
396
+ {
397
+ originalSize = context->OrgByteCount[i];
398
+
399
+ if (originalSize == 0)
400
+ {
401
+ planeSize = 0;
402
+ }
403
+ else
404
+ {
405
+ planeSize = nsc_rle_encode(context->priv->PlaneBuffers[i],
406
+ context->priv->PlaneBuffers[4], originalSize);
407
+
408
+ if (planeSize < originalSize)
409
+ CopyMemory(context->priv->PlaneBuffers[i], context->priv->PlaneBuffers[4],
410
+ planeSize);
411
+ else
412
+ planeSize = originalSize;
413
+ }
414
+
415
+ context->PlaneByteCount[i] = planeSize;
416
+ }
417
+ }
418
+
419
+ static UINT32 nsc_compute_byte_count(NSC_CONTEXT* WINPR_RESTRICT context,
420
+ UINT32* WINPR_RESTRICT ByteCount, UINT32 width, UINT32 height)
421
+ {
422
+ UINT32 tempWidth = 0;
423
+ UINT32 tempHeight = 0;
424
+ UINT32 maxPlaneSize = 0;
425
+ tempWidth = ROUND_UP_TO(width, 8);
426
+ tempHeight = ROUND_UP_TO(height, 2);
427
+ maxPlaneSize = tempWidth * tempHeight + 16;
428
+
429
+ if (context->ChromaSubsamplingLevel)
430
+ {
431
+ ByteCount[0] = tempWidth * height;
432
+ ByteCount[1] = tempWidth * tempHeight / 4;
433
+ ByteCount[2] = tempWidth * tempHeight / 4;
434
+ ByteCount[3] = width * height;
435
+ }
436
+ else
437
+ {
438
+ ByteCount[0] = width * height;
439
+ ByteCount[1] = width * height;
440
+ ByteCount[2] = width * height;
441
+ ByteCount[3] = width * height;
442
+ }
443
+
444
+ return maxPlaneSize;
445
+ }
446
+
447
+ BOOL nsc_write_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
448
+ const NSC_MESSAGE* WINPR_RESTRICT message)
449
+ {
450
+ UINT32 totalPlaneByteCount = 0;
451
+ totalPlaneByteCount = message->LumaPlaneByteCount + message->OrangeChromaPlaneByteCount +
452
+ message->GreenChromaPlaneByteCount + message->AlphaPlaneByteCount;
453
+
454
+ if (!Stream_EnsureRemainingCapacity(s, 20 + totalPlaneByteCount))
455
+ return FALSE;
456
+
457
+ Stream_Write_UINT32(s, message->LumaPlaneByteCount); /* LumaPlaneByteCount (4 bytes) */
458
+ Stream_Write_UINT32(
459
+ s, message->OrangeChromaPlaneByteCount); /* OrangeChromaPlaneByteCount (4 bytes) */
460
+ Stream_Write_UINT32(
461
+ s, message->GreenChromaPlaneByteCount); /* GreenChromaPlaneByteCount (4 bytes) */
462
+ Stream_Write_UINT32(s, message->AlphaPlaneByteCount); /* AlphaPlaneByteCount (4 bytes) */
463
+ Stream_Write_UINT8(s, message->ColorLossLevel); /* ColorLossLevel (1 byte) */
464
+ Stream_Write_UINT8(s, message->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
465
+ Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
466
+
467
+ if (message->LumaPlaneByteCount)
468
+ Stream_Write(s, message->PlaneBuffers[0], message->LumaPlaneByteCount); /* LumaPlane */
469
+
470
+ if (message->OrangeChromaPlaneByteCount)
471
+ Stream_Write(s, message->PlaneBuffers[1],
472
+ message->OrangeChromaPlaneByteCount); /* OrangeChromaPlane */
473
+
474
+ if (message->GreenChromaPlaneByteCount)
475
+ Stream_Write(s, message->PlaneBuffers[2],
476
+ message->GreenChromaPlaneByteCount); /* GreenChromaPlane */
477
+
478
+ if (message->AlphaPlaneByteCount)
479
+ Stream_Write(s, message->PlaneBuffers[3], message->AlphaPlaneByteCount); /* AlphaPlane */
480
+
481
+ return TRUE;
482
+ }
483
+
484
+ BOOL nsc_compose_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
485
+ const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
486
+ UINT32 scanline)
487
+ {
488
+ BOOL rc = 0;
489
+ NSC_MESSAGE message = { 0 };
490
+
491
+ if (!context || !s || !data)
492
+ return FALSE;
493
+
494
+ context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
495
+ context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
496
+
497
+ if (!nsc_context_initialize_encode(context))
498
+ return FALSE;
499
+
500
+ /* ARGB to AYCoCg conversion, chroma subsampling and colorloss reduction */
501
+ PROFILER_ENTER(context->priv->prof_nsc_encode)
502
+ rc = context->encode(context, data, scanline);
503
+ PROFILER_EXIT(context->priv->prof_nsc_encode)
504
+ if (!rc)
505
+ return FALSE;
506
+
507
+ /* RLE encode */
508
+ PROFILER_ENTER(context->priv->prof_nsc_rle_compress_data)
509
+ nsc_rle_compress_data(context);
510
+ PROFILER_EXIT(context->priv->prof_nsc_rle_compress_data)
511
+ message.PlaneBuffers[0] = context->priv->PlaneBuffers[0];
512
+ message.PlaneBuffers[1] = context->priv->PlaneBuffers[1];
513
+ message.PlaneBuffers[2] = context->priv->PlaneBuffers[2];
514
+ message.PlaneBuffers[3] = context->priv->PlaneBuffers[3];
515
+ message.LumaPlaneByteCount = context->PlaneByteCount[0];
516
+ message.OrangeChromaPlaneByteCount = context->PlaneByteCount[1];
517
+ message.GreenChromaPlaneByteCount = context->PlaneByteCount[2];
518
+ message.AlphaPlaneByteCount = context->PlaneByteCount[3];
519
+
520
+ message.ColorLossLevel = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel);
521
+ message.ChromaSubsamplingLevel =
522
+ WINPR_ASSERTING_INT_CAST(BYTE, context->ChromaSubsamplingLevel);
523
+ return nsc_write_message(context, s, &message);
524
+ }
525
+
526
+ BOOL nsc_decompose_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
527
+ BYTE* WINPR_RESTRICT bmpdata, UINT32 x, UINT32 y, UINT32 width,
528
+ UINT32 height, UINT32 rowstride, UINT32 format, UINT32 flip)
529
+ {
530
+ size_t size = Stream_GetRemainingLength(s);
531
+
532
+ if (size > UINT32_MAX)
533
+ return FALSE;
534
+
535
+ if (!nsc_process_message(context, (UINT16)FreeRDPGetBitsPerPixel(context->format), width,
536
+ height, Stream_Pointer(s), (UINT32)size, bmpdata, format, rowstride, x,
537
+ y, width, height, flip))
538
+ return FALSE;
539
+ Stream_Seek(s, size);
540
+ return TRUE;
541
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/planar.c ADDED
@@ -0,0 +1,1797 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RDP6 Planar Codec
4
+ *
5
+ * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <winpr/crt.h>
25
+ #include <winpr/wtypes.h>
26
+ #include <winpr/assert.h>
27
+ #include <winpr/cast.h>
28
+ #include <winpr/print.h>
29
+
30
+ #include <freerdp/primitives.h>
31
+ #include <freerdp/log.h>
32
+ #include <freerdp/codec/bitmap.h>
33
+ #include <freerdp/codec/planar.h>
34
+
35
+ #define TAG FREERDP_TAG("codec")
36
+
37
+ #define PLANAR_ALIGN(val, align) \
38
+ ((val) % (align) == 0) ? (val) : ((val) + (align) - (val) % (align))
39
+
40
+ typedef struct
41
+ {
42
+ /**
43
+ * controlByte:
44
+ * [0-3]: nRunLength
45
+ * [4-7]: cRawBytes
46
+ */
47
+ BYTE controlByte;
48
+ BYTE* rawValues;
49
+ } RDP6_RLE_SEGMENT;
50
+
51
+ typedef struct
52
+ {
53
+ UINT32 cSegments;
54
+ RDP6_RLE_SEGMENT* segments;
55
+ } RDP6_RLE_SEGMENTS;
56
+
57
+ typedef struct
58
+ {
59
+ /**
60
+ * formatHeader:
61
+ * [0-2]: Color Loss Level (CLL)
62
+ * [3] : Chroma Subsampling (CS)
63
+ * [4] : Run Length Encoding (RLE)
64
+ * [5] : No Alpha (NA)
65
+ * [6-7]: Reserved
66
+ */
67
+ BYTE formatHeader;
68
+ } RDP6_BITMAP_STREAM;
69
+
70
+ struct S_BITMAP_PLANAR_CONTEXT
71
+ {
72
+ UINT32 maxWidth;
73
+ UINT32 maxHeight;
74
+ UINT32 maxPlaneSize;
75
+
76
+ BOOL AllowSkipAlpha;
77
+ BOOL AllowRunLengthEncoding;
78
+ BOOL AllowColorSubsampling;
79
+ BOOL AllowDynamicColorFidelity;
80
+
81
+ UINT32 ColorLossLevel;
82
+
83
+ BYTE* planes[4];
84
+ BYTE* planesBuffer;
85
+
86
+ BYTE* deltaPlanes[4];
87
+ BYTE* deltaPlanesBuffer;
88
+
89
+ BYTE* rlePlanes[4];
90
+ BYTE* rlePlanesBuffer;
91
+
92
+ BYTE* pTempData;
93
+ UINT32 nTempStep;
94
+
95
+ BOOL bgr;
96
+ BOOL topdown;
97
+ };
98
+
99
+ static INLINE UINT32 planar_invert_format(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL alpha,
100
+ UINT32 DstFormat)
101
+ {
102
+
103
+ if (planar->bgr && alpha)
104
+ {
105
+ switch (DstFormat)
106
+ {
107
+ case PIXEL_FORMAT_ARGB32:
108
+ DstFormat = PIXEL_FORMAT_ABGR32;
109
+ break;
110
+ case PIXEL_FORMAT_XRGB32:
111
+ DstFormat = PIXEL_FORMAT_XBGR32;
112
+ break;
113
+ case PIXEL_FORMAT_ABGR32:
114
+ DstFormat = PIXEL_FORMAT_ARGB32;
115
+ break;
116
+ case PIXEL_FORMAT_XBGR32:
117
+ DstFormat = PIXEL_FORMAT_XRGB32;
118
+ break;
119
+ case PIXEL_FORMAT_BGRA32:
120
+ DstFormat = PIXEL_FORMAT_RGBA32;
121
+ break;
122
+ case PIXEL_FORMAT_BGRX32:
123
+ DstFormat = PIXEL_FORMAT_RGBX32;
124
+ break;
125
+ case PIXEL_FORMAT_RGBA32:
126
+ DstFormat = PIXEL_FORMAT_BGRA32;
127
+ break;
128
+ case PIXEL_FORMAT_RGBX32:
129
+ DstFormat = PIXEL_FORMAT_BGRX32;
130
+ break;
131
+ case PIXEL_FORMAT_RGB24:
132
+ DstFormat = PIXEL_FORMAT_BGR24;
133
+ break;
134
+ case PIXEL_FORMAT_BGR24:
135
+ DstFormat = PIXEL_FORMAT_RGB24;
136
+ break;
137
+ case PIXEL_FORMAT_RGB16:
138
+ DstFormat = PIXEL_FORMAT_BGR16;
139
+ break;
140
+ case PIXEL_FORMAT_BGR16:
141
+ DstFormat = PIXEL_FORMAT_RGB16;
142
+ break;
143
+ case PIXEL_FORMAT_ARGB15:
144
+ DstFormat = PIXEL_FORMAT_ABGR15;
145
+ break;
146
+ case PIXEL_FORMAT_RGB15:
147
+ DstFormat = PIXEL_FORMAT_BGR15;
148
+ break;
149
+ case PIXEL_FORMAT_ABGR15:
150
+ DstFormat = PIXEL_FORMAT_ARGB15;
151
+ break;
152
+ case PIXEL_FORMAT_BGR15:
153
+ DstFormat = PIXEL_FORMAT_RGB15;
154
+ break;
155
+ default:
156
+ break;
157
+ }
158
+ }
159
+ return DstFormat;
160
+ }
161
+
162
+ static INLINE BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane,
163
+ UINT32 width, UINT32 height,
164
+ BYTE* WINPR_RESTRICT outPlane,
165
+ UINT32* WINPR_RESTRICT dstSize);
166
+ static INLINE BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane,
167
+ UINT32 width, UINT32 height,
168
+ BYTE* WINPR_RESTRICT outPlane);
169
+
170
+ static INLINE INT32 planar_skip_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
171
+ UINT32 nWidth, UINT32 nHeight)
172
+ {
173
+ UINT32 used = 0;
174
+
175
+ WINPR_ASSERT(pSrcData);
176
+
177
+ for (UINT32 y = 0; y < nHeight; y++)
178
+ {
179
+ for (UINT32 x = 0; x < nWidth;)
180
+ {
181
+ if (used >= SrcSize)
182
+ {
183
+ WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
184
+ SrcSize);
185
+ return -1;
186
+ }
187
+
188
+ const uint8_t controlByte = pSrcData[used++];
189
+ uint32_t nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
190
+ uint32_t cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
191
+
192
+ if (nRunLength == 1)
193
+ {
194
+ nRunLength = cRawBytes + 16;
195
+ cRawBytes = 0;
196
+ }
197
+ else if (nRunLength == 2)
198
+ {
199
+ nRunLength = cRawBytes + 32;
200
+ cRawBytes = 0;
201
+ }
202
+
203
+ used += cRawBytes;
204
+ x += cRawBytes;
205
+ x += nRunLength;
206
+
207
+ if (x > nWidth)
208
+ {
209
+ WLog_ERR(TAG, "planar plane x %" PRIu32 " exceeds width %" PRIu32, x, nWidth);
210
+ return -1;
211
+ }
212
+
213
+ if (used > SrcSize)
214
+ {
215
+ WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
216
+ INT32_MAX);
217
+ return -1;
218
+ }
219
+ }
220
+ }
221
+
222
+ if (used > INT32_MAX)
223
+ {
224
+ WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, SrcSize);
225
+ return -1;
226
+ }
227
+ return (INT32)used;
228
+ }
229
+
230
+ static inline UINT8 clamp(INT16 val)
231
+ {
232
+ return (UINT8)val;
233
+ }
234
+
235
+ static INLINE INT32 planar_decompress_plane_rle_only(const BYTE* WINPR_RESTRICT pSrcData,
236
+ UINT32 SrcSize, BYTE* WINPR_RESTRICT pDstData,
237
+ UINT32 nWidth, UINT32 nHeight)
238
+ {
239
+ BYTE* previousScanline = NULL;
240
+ const BYTE* srcp = pSrcData;
241
+
242
+ WINPR_ASSERT(nHeight <= INT32_MAX);
243
+ WINPR_ASSERT(nWidth <= INT32_MAX);
244
+
245
+ for (UINT32 y = 0; y < nHeight; y++)
246
+ {
247
+ BYTE* dstp = &pDstData[1ULL * (y)*nWidth];
248
+ INT16 pixel = 0;
249
+ BYTE* currentScanline = dstp;
250
+
251
+ for (UINT32 x = 0; x < nWidth;)
252
+ {
253
+ BYTE controlByte = *srcp;
254
+ srcp++;
255
+
256
+ if ((srcp - pSrcData) > SrcSize * 1ll)
257
+ {
258
+ WLog_ERR(TAG, "error reading input buffer");
259
+ return -1;
260
+ }
261
+
262
+ UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
263
+ UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
264
+
265
+ if (nRunLength == 1)
266
+ {
267
+ nRunLength = cRawBytes + 16;
268
+ cRawBytes = 0;
269
+ }
270
+ else if (nRunLength == 2)
271
+ {
272
+ nRunLength = cRawBytes + 32;
273
+ cRawBytes = 0;
274
+ }
275
+
276
+ if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 1ll)
277
+ {
278
+ WLog_ERR(TAG, "too many pixels in scanline");
279
+ return -1;
280
+ }
281
+
282
+ if (!previousScanline)
283
+ {
284
+ /* first scanline, absolute values */
285
+ while (cRawBytes > 0)
286
+ {
287
+ pixel = *srcp;
288
+ srcp++;
289
+ *dstp = clamp(pixel);
290
+ dstp++;
291
+ x++;
292
+ cRawBytes--;
293
+ }
294
+
295
+ while (nRunLength > 0)
296
+ {
297
+ *dstp = clamp(pixel);
298
+ dstp++;
299
+ x++;
300
+ nRunLength--;
301
+ }
302
+ }
303
+ else
304
+ {
305
+ /* delta values relative to previous scanline */
306
+ while (cRawBytes > 0)
307
+ {
308
+ UINT8 deltaValue = *srcp;
309
+ srcp++;
310
+
311
+ if (deltaValue & 1)
312
+ {
313
+ deltaValue = deltaValue >> 1;
314
+ deltaValue = deltaValue + 1;
315
+ pixel = WINPR_ASSERTING_INT_CAST(int16_t, -1 * (int16_t)deltaValue);
316
+ }
317
+ else
318
+ {
319
+ deltaValue = deltaValue >> 1;
320
+ pixel = WINPR_ASSERTING_INT_CAST(INT16, deltaValue);
321
+ }
322
+
323
+ const INT16 delta =
324
+ WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
325
+ *dstp = clamp(delta);
326
+ dstp++;
327
+ x++;
328
+ cRawBytes--;
329
+ }
330
+
331
+ while (nRunLength > 0)
332
+ {
333
+ const INT16 deltaValue =
334
+ WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
335
+ *dstp = clamp(deltaValue);
336
+ dstp++;
337
+ x++;
338
+ nRunLength--;
339
+ }
340
+ }
341
+ }
342
+
343
+ previousScanline = currentScanline;
344
+ }
345
+
346
+ return (INT32)(srcp - pSrcData);
347
+ }
348
+
349
+ static INLINE INT32 planar_decompress_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
350
+ BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
351
+ UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
352
+ UINT32 nHeight, UINT32 nChannel, BOOL vFlip)
353
+ {
354
+ INT32 beg = 0;
355
+ INT32 end = 0;
356
+ INT32 inc = 0;
357
+ BYTE* previousScanline = NULL;
358
+ const BYTE* srcp = pSrcData;
359
+
360
+ WINPR_ASSERT(nHeight <= INT32_MAX);
361
+ WINPR_ASSERT(nWidth <= INT32_MAX);
362
+ WINPR_ASSERT(nDstStep <= INT32_MAX);
363
+
364
+ previousScanline = NULL;
365
+
366
+ if (vFlip)
367
+ {
368
+ beg = (INT32)nHeight - 1;
369
+ end = -1;
370
+ inc = -1;
371
+ }
372
+ else
373
+ {
374
+ beg = 0;
375
+ end = (INT32)nHeight;
376
+ inc = 1;
377
+ }
378
+
379
+ for (INT32 y = beg; y != end; y += inc)
380
+ {
381
+ const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
382
+ BYTE* dstp = &pDstData[off];
383
+ BYTE* currentScanline = dstp;
384
+ INT16 pixel = 0;
385
+
386
+ for (INT32 x = 0; x < (INT32)nWidth;)
387
+ {
388
+ const BYTE controlByte = *srcp;
389
+ srcp++;
390
+
391
+ if ((srcp - pSrcData) > SrcSize * 1ll)
392
+ {
393
+ WLog_ERR(TAG, "error reading input buffer");
394
+ return -1;
395
+ }
396
+
397
+ UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
398
+ UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
399
+
400
+ if (nRunLength == 1)
401
+ {
402
+ nRunLength = cRawBytes + 16;
403
+ cRawBytes = 0;
404
+ }
405
+ else if (nRunLength == 2)
406
+ {
407
+ nRunLength = cRawBytes + 32;
408
+ cRawBytes = 0;
409
+ }
410
+
411
+ if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 4ll)
412
+ {
413
+ WLog_ERR(TAG, "too many pixels in scanline");
414
+ return -1;
415
+ }
416
+
417
+ if (!previousScanline)
418
+ {
419
+ /* first scanline, absolute values */
420
+ while (cRawBytes > 0)
421
+ {
422
+ pixel = *srcp;
423
+ srcp++;
424
+ *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
425
+ dstp += 4;
426
+ x++;
427
+ cRawBytes--;
428
+ }
429
+
430
+ while (nRunLength > 0)
431
+ {
432
+ *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
433
+ dstp += 4;
434
+ x++;
435
+ nRunLength--;
436
+ }
437
+ }
438
+ else
439
+ {
440
+ /* delta values relative to previous scanline */
441
+ while (cRawBytes > 0)
442
+ {
443
+ BYTE deltaValue = *srcp;
444
+ srcp++;
445
+
446
+ if (deltaValue & 1)
447
+ {
448
+ deltaValue = deltaValue >> 1;
449
+ deltaValue = deltaValue + 1;
450
+ pixel = WINPR_ASSERTING_INT_CAST(int16_t, -deltaValue);
451
+ }
452
+ else
453
+ {
454
+ deltaValue = deltaValue >> 1;
455
+ pixel = deltaValue;
456
+ }
457
+
458
+ const INT16 delta =
459
+ WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[4LL * x] + pixel);
460
+ *dstp = clamp(delta);
461
+ dstp += 4;
462
+ x++;
463
+ cRawBytes--;
464
+ }
465
+
466
+ while (nRunLength > 0)
467
+ {
468
+ const INT16 deltaValue =
469
+ WINPR_ASSERTING_INT_CAST(int16_t, pixel + previousScanline[4LL * x]);
470
+ *dstp = clamp(deltaValue);
471
+ dstp += 4;
472
+ x++;
473
+ nRunLength--;
474
+ }
475
+ }
476
+ }
477
+
478
+ previousScanline = currentScanline;
479
+ }
480
+
481
+ return (INT32)(srcp - pSrcData);
482
+ }
483
+
484
+ static INLINE INT32 planar_set_plane(BYTE bValue, BYTE* pDstData, UINT32 nDstStep, UINT32 nXDst,
485
+ UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 nChannel,
486
+ BOOL vFlip)
487
+ {
488
+ INT32 beg = 0;
489
+ INT32 end = 0;
490
+ INT32 inc = 0;
491
+
492
+ WINPR_ASSERT(nHeight <= INT32_MAX);
493
+ WINPR_ASSERT(nWidth <= INT32_MAX);
494
+ WINPR_ASSERT(nDstStep <= INT32_MAX);
495
+
496
+ if (vFlip)
497
+ {
498
+ beg = (INT32)nHeight - 1;
499
+ end = -1;
500
+ inc = -1;
501
+ }
502
+ else
503
+ {
504
+ beg = 0;
505
+ end = (INT32)nHeight;
506
+ inc = 1;
507
+ }
508
+
509
+ for (INT32 y = beg; y != end; y += inc)
510
+ {
511
+ const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
512
+ BYTE* dstp = &pDstData[off];
513
+
514
+ for (INT32 x = 0; x < (INT32)nWidth; ++x)
515
+ {
516
+ *dstp = bValue;
517
+ dstp += 4;
518
+ }
519
+ }
520
+
521
+ return 0;
522
+ }
523
+
524
+ static INLINE BOOL writeLine(BYTE** WINPR_RESTRICT ppRgba, UINT32 DstFormat, UINT32 width,
525
+ const BYTE** WINPR_RESTRICT ppR, const BYTE** WINPR_RESTRICT ppG,
526
+ const BYTE** WINPR_RESTRICT ppB, const BYTE** WINPR_RESTRICT ppA)
527
+ {
528
+ WINPR_ASSERT(ppRgba);
529
+ WINPR_ASSERT(ppR);
530
+ WINPR_ASSERT(ppG);
531
+ WINPR_ASSERT(ppB);
532
+
533
+ switch (DstFormat)
534
+ {
535
+ case PIXEL_FORMAT_BGRA32:
536
+ for (UINT32 x = 0; x < width; x++)
537
+ {
538
+ *(*ppRgba)++ = *(*ppB)++;
539
+ *(*ppRgba)++ = *(*ppG)++;
540
+ *(*ppRgba)++ = *(*ppR)++;
541
+ *(*ppRgba)++ = *(*ppA)++;
542
+ }
543
+
544
+ return TRUE;
545
+
546
+ case PIXEL_FORMAT_BGRX32:
547
+ for (UINT32 x = 0; x < width; x++)
548
+ {
549
+ *(*ppRgba)++ = *(*ppB)++;
550
+ *(*ppRgba)++ = *(*ppG)++;
551
+ *(*ppRgba)++ = *(*ppR)++;
552
+ *(*ppRgba)++ = 0xFF;
553
+ }
554
+
555
+ return TRUE;
556
+
557
+ default:
558
+ if (ppA)
559
+ {
560
+ for (UINT32 x = 0; x < width; x++)
561
+ {
562
+ BYTE alpha = *(*ppA)++;
563
+ UINT32 color =
564
+ FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha);
565
+ FreeRDPWriteColor(*ppRgba, DstFormat, color);
566
+ *ppRgba += FreeRDPGetBytesPerPixel(DstFormat);
567
+ }
568
+ }
569
+ else
570
+ {
571
+ const BYTE alpha = 0xFF;
572
+
573
+ for (UINT32 x = 0; x < width; x++)
574
+ {
575
+ UINT32 color =
576
+ FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha);
577
+ FreeRDPWriteColor(*ppRgba, DstFormat, color);
578
+ *ppRgba += FreeRDPGetBytesPerPixel(DstFormat);
579
+ }
580
+ }
581
+
582
+ return TRUE;
583
+ }
584
+ }
585
+
586
+ static INLINE BOOL planar_decompress_planes_raw(const BYTE* WINPR_RESTRICT pSrcData[4],
587
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
588
+ UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
589
+ UINT32 nWidth, UINT32 nHeight, BOOL vFlip,
590
+ UINT32 totalHeight)
591
+ {
592
+ INT32 beg = 0;
593
+ INT32 end = 0;
594
+ INT32 inc = 0;
595
+ const BYTE* pR = pSrcData[0];
596
+ const BYTE* pG = pSrcData[1];
597
+ const BYTE* pB = pSrcData[2];
598
+ const BYTE* pA = pSrcData[3];
599
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
600
+
601
+ if (vFlip)
602
+ {
603
+ beg = WINPR_ASSERTING_INT_CAST(int32_t, nHeight - 1);
604
+ end = -1;
605
+ inc = -1;
606
+ }
607
+ else
608
+ {
609
+ beg = 0;
610
+ end = WINPR_ASSERTING_INT_CAST(int32_t, nHeight);
611
+ inc = 1;
612
+ }
613
+
614
+ if (nYDst + nHeight > totalHeight)
615
+ {
616
+ WLog_ERR(TAG,
617
+ "planar plane destination Y %" PRIu32 " + height %" PRIu32
618
+ " exceeds totalHeight %" PRIu32,
619
+ nYDst, nHeight, totalHeight);
620
+ return FALSE;
621
+ }
622
+
623
+ if ((nXDst + nWidth) * bpp > nDstStep)
624
+ {
625
+ WLog_ERR(TAG,
626
+ "planar plane destination (X %" PRIu32 " + width %" PRIu32 ") * bpp %" PRIu32
627
+ " exceeds stride %" PRIu32,
628
+ nXDst, nWidth, bpp, nDstStep);
629
+ return FALSE;
630
+ }
631
+
632
+ for (INT32 y = beg; y != end; y += inc)
633
+ {
634
+ BYTE* pRGB = NULL;
635
+
636
+ if (y > WINPR_ASSERTING_INT_CAST(INT64, nHeight))
637
+ {
638
+ WLog_ERR(TAG, "planar plane destination Y %" PRId32 " exceeds height %" PRIu32, y,
639
+ nHeight);
640
+ return FALSE;
641
+ }
642
+
643
+ const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (1LL * nXDst * bpp);
644
+ pRGB = &pDstData[off];
645
+
646
+ if (!writeLine(&pRGB, DstFormat, nWidth, &pR, &pG, &pB, &pA))
647
+ return FALSE;
648
+ }
649
+
650
+ return TRUE;
651
+ }
652
+
653
+ static BOOL planar_subsample_expand(const BYTE* WINPR_RESTRICT plane, size_t planeLength,
654
+ UINT32 nWidth, UINT32 nHeight, UINT32 nPlaneWidth,
655
+ UINT32 nPlaneHeight, BYTE* WINPR_RESTRICT deltaPlane)
656
+ {
657
+ size_t pos = 0;
658
+ WINPR_UNUSED(planeLength);
659
+
660
+ WINPR_ASSERT(plane);
661
+ WINPR_ASSERT(deltaPlane);
662
+
663
+ if (nWidth > nPlaneWidth * 2)
664
+ {
665
+ WLog_ERR(TAG, "planar subsample width %" PRIu32 " > PlaneWidth %" PRIu32 " * 2", nWidth,
666
+ nPlaneWidth);
667
+ return FALSE;
668
+ }
669
+
670
+ if (nHeight > nPlaneHeight * 2)
671
+ {
672
+ WLog_ERR(TAG, "planar subsample height %" PRIu32 " > PlaneHeight %" PRIu32 " * 2", nHeight,
673
+ nPlaneHeight);
674
+ return FALSE;
675
+ }
676
+
677
+ for (size_t y = 0; y < nHeight; y++)
678
+ {
679
+ const BYTE* src = plane + y / 2 * nPlaneWidth;
680
+
681
+ for (UINT32 x = 0; x < nWidth; x++)
682
+ {
683
+ deltaPlane[pos++] = src[x / 2];
684
+ }
685
+ }
686
+
687
+ return TRUE;
688
+ }
689
+
690
+ BOOL planar_decompress(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
691
+ const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 nSrcWidth,
692
+ UINT32 nSrcHeight, BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
693
+ UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth,
694
+ UINT32 nDstHeight, BOOL vFlip)
695
+ {
696
+ BOOL useAlpha = FALSE;
697
+ INT32 status = 0;
698
+ INT32 rleSizes[4] = { 0, 0, 0, 0 };
699
+ UINT32 rawSizes[4] = { 0 };
700
+ UINT32 rawWidths[4] = { 0 };
701
+ UINT32 rawHeights[4] = { 0 };
702
+ const BYTE* planes[4] = { 0 };
703
+ const UINT32 w = MIN(nSrcWidth, nDstWidth);
704
+ const UINT32 h = MIN(nSrcHeight, nDstHeight);
705
+ const primitives_t* prims = primitives_get();
706
+
707
+ WINPR_ASSERT(planar);
708
+ WINPR_ASSERT(prims);
709
+
710
+ if (nDstStep <= 0)
711
+ nDstStep = nDstWidth * FreeRDPGetBytesPerPixel(DstFormat);
712
+
713
+ const BYTE* srcp = pSrcData;
714
+
715
+ if (!pSrcData)
716
+ {
717
+ WLog_ERR(TAG, "Invalid argument pSrcData=NULL");
718
+ return FALSE;
719
+ }
720
+
721
+ if (!pDstData)
722
+ {
723
+ WLog_ERR(TAG, "Invalid argument pDstData=NULL");
724
+ return FALSE;
725
+ }
726
+
727
+ const BYTE FormatHeader = *srcp++;
728
+ const BYTE cll = (FormatHeader & PLANAR_FORMAT_HEADER_CLL_MASK);
729
+ const BYTE cs = (FormatHeader & PLANAR_FORMAT_HEADER_CS) ? TRUE : FALSE;
730
+ const BYTE rle = (FormatHeader & PLANAR_FORMAT_HEADER_RLE) ? TRUE : FALSE;
731
+ const BYTE alpha = (FormatHeader & PLANAR_FORMAT_HEADER_NA) ? FALSE : TRUE;
732
+
733
+ DstFormat = planar_invert_format(planar, alpha, DstFormat);
734
+
735
+ if (alpha)
736
+ useAlpha = FreeRDPColorHasAlpha(DstFormat);
737
+
738
+ // WLog_INFO(TAG, "CLL: %"PRIu32" CS: %"PRIu8" RLE: %"PRIu8" ALPHA: %"PRIu8"", cll, cs, rle,
739
+ // alpha);
740
+
741
+ if (!cll && cs)
742
+ {
743
+ WLog_ERR(TAG, "Chroma subsampling requires YCoCg and does not work with RGB data");
744
+ return FALSE; /* Chroma subsampling requires YCoCg */
745
+ }
746
+
747
+ const UINT32 subWidth = (nSrcWidth / 2) + (nSrcWidth % 2);
748
+ const UINT32 subHeight = (nSrcHeight / 2) + (nSrcHeight % 2);
749
+ const UINT32 planeSize = nSrcWidth * nSrcHeight;
750
+ const UINT32 subSize = subWidth * subHeight;
751
+
752
+ if (!cs)
753
+ {
754
+ rawSizes[0] = planeSize; /* LumaOrRedPlane */
755
+ rawWidths[0] = nSrcWidth;
756
+ rawHeights[0] = nSrcHeight;
757
+ rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
758
+ rawWidths[1] = nSrcWidth;
759
+ rawHeights[1] = nSrcHeight;
760
+ rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
761
+ rawWidths[2] = nSrcWidth;
762
+ rawHeights[2] = nSrcHeight;
763
+ rawSizes[3] = planeSize; /* AlphaPlane */
764
+ rawWidths[3] = nSrcWidth;
765
+ rawHeights[3] = nSrcHeight;
766
+ }
767
+ else /* Chroma Subsampling */
768
+ {
769
+ rawSizes[0] = planeSize; /* LumaOrRedPlane */
770
+ rawWidths[0] = nSrcWidth;
771
+ rawHeights[0] = nSrcHeight;
772
+ rawSizes[1] = subSize; /* OrangeChromaOrGreenPlane */
773
+ rawWidths[1] = subWidth;
774
+ rawHeights[1] = subHeight;
775
+ rawSizes[2] = subSize; /* GreenChromaOrBluePlane */
776
+ rawWidths[2] = subWidth;
777
+ rawHeights[2] = subHeight;
778
+ rawSizes[3] = planeSize; /* AlphaPlane */
779
+ rawWidths[3] = nSrcWidth;
780
+ rawHeights[3] = nSrcHeight;
781
+ }
782
+
783
+ const size_t diff = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(srcp - pSrcData));
784
+ if (SrcSize < diff)
785
+ {
786
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
787
+ return FALSE;
788
+ }
789
+
790
+ if (!rle) /* RAW */
791
+ {
792
+
793
+ UINT32 base = planeSize * 3;
794
+ if (cs)
795
+ base = planeSize + planeSize / 2;
796
+
797
+ if (alpha)
798
+ {
799
+ if ((SrcSize - diff) < (planeSize + base))
800
+ {
801
+ WLog_ERR(TAG, "Alpha plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff,
802
+ (planeSize + base));
803
+ return FALSE;
804
+ }
805
+
806
+ planes[3] = srcp; /* AlphaPlane */
807
+ planes[0] = planes[3] + rawSizes[3]; /* LumaOrRedPlane */
808
+ planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
809
+ planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
810
+
811
+ if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
812
+ {
813
+ WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
814
+ &pSrcData[SrcSize]);
815
+ return FALSE;
816
+ }
817
+ }
818
+ else
819
+ {
820
+ if ((SrcSize - diff) < base)
821
+ {
822
+ WLog_ERR(TAG, "plane size mismatch %" PRIu32 " < %" PRIu32, SrcSize - diff, base);
823
+ return FALSE;
824
+ }
825
+
826
+ planes[0] = srcp; /* LumaOrRedPlane */
827
+ planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
828
+ planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
829
+
830
+ if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
831
+ {
832
+ WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
833
+ &pSrcData[SrcSize]);
834
+ return FALSE;
835
+ }
836
+ }
837
+ }
838
+ else /* RLE */
839
+ {
840
+ if (alpha)
841
+ {
842
+ planes[3] = srcp;
843
+ rleSizes[3] = planar_skip_plane_rle(planes[3], (UINT32)(SrcSize - diff), rawWidths[3],
844
+ rawHeights[3]); /* AlphaPlane */
845
+
846
+ if (rleSizes[3] < 0)
847
+ return FALSE;
848
+
849
+ planes[0] = planes[3] + rleSizes[3];
850
+ }
851
+ else
852
+ planes[0] = srcp;
853
+
854
+ const size_t diff0 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[0] - pSrcData));
855
+ if (SrcSize < diff0)
856
+ {
857
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff0);
858
+ return FALSE;
859
+ }
860
+ rleSizes[0] = planar_skip_plane_rle(planes[0], (UINT32)(SrcSize - diff0), rawWidths[0],
861
+ rawHeights[0]); /* RedPlane */
862
+
863
+ if (rleSizes[0] < 0)
864
+ return FALSE;
865
+
866
+ planes[1] = planes[0] + rleSizes[0];
867
+
868
+ const size_t diff1 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[1] - pSrcData));
869
+ if (SrcSize < diff1)
870
+ {
871
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff1);
872
+ return FALSE;
873
+ }
874
+ rleSizes[1] = planar_skip_plane_rle(planes[1], (UINT32)(SrcSize - diff1), rawWidths[1],
875
+ rawHeights[1]); /* GreenPlane */
876
+
877
+ if (rleSizes[1] < 1)
878
+ return FALSE;
879
+
880
+ planes[2] = planes[1] + rleSizes[1];
881
+ const size_t diff2 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[2] - pSrcData));
882
+ if (SrcSize < diff2)
883
+ {
884
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
885
+ return FALSE;
886
+ }
887
+ rleSizes[2] = planar_skip_plane_rle(planes[2], (UINT32)(SrcSize - diff2), rawWidths[2],
888
+ rawHeights[2]); /* BluePlane */
889
+
890
+ if (rleSizes[2] < 1)
891
+ return FALSE;
892
+ }
893
+
894
+ if (!cll) /* RGB */
895
+ {
896
+ UINT32 TempFormat = 0;
897
+ BYTE* pTempData = pDstData;
898
+ UINT32 nTempStep = nDstStep;
899
+ UINT32 nTotalHeight = nYDst + nDstHeight;
900
+
901
+ if (useAlpha)
902
+ TempFormat = PIXEL_FORMAT_BGRA32;
903
+ else
904
+ TempFormat = PIXEL_FORMAT_BGRX32;
905
+
906
+ TempFormat = planar_invert_format(planar, alpha, TempFormat);
907
+
908
+ if ((TempFormat != DstFormat) || (nSrcWidth != nDstWidth) || (nSrcHeight != nDstHeight))
909
+ {
910
+ pTempData = planar->pTempData;
911
+ nTempStep = planar->nTempStep;
912
+ nTotalHeight = planar->maxHeight;
913
+ }
914
+
915
+ if (!rle) /* RAW */
916
+ {
917
+ if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
918
+ nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
919
+ return FALSE;
920
+
921
+ if (alpha)
922
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
923
+ else /* NoAlpha */
924
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
925
+
926
+ if ((SrcSize - (srcp - pSrcData)) == 1)
927
+ srcp++; /* pad */
928
+ }
929
+ else /* RLE */
930
+ {
931
+ status = planar_decompress_plane_rle(
932
+ planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), pTempData, nTempStep,
933
+ nXDst, nYDst, nSrcWidth, nSrcHeight, 2, vFlip); /* RedPlane */
934
+
935
+ if (status < 0)
936
+ return FALSE;
937
+
938
+ status = planar_decompress_plane_rle(
939
+ planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), pTempData, nTempStep,
940
+ nXDst, nYDst, nSrcWidth, nSrcHeight, 1, vFlip); /* GreenPlane */
941
+
942
+ if (status < 0)
943
+ return FALSE;
944
+
945
+ status = planar_decompress_plane_rle(
946
+ planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), pTempData, nTempStep,
947
+ nXDst, nYDst, nSrcWidth, nSrcHeight, 0, vFlip); /* BluePlane */
948
+
949
+ if (status < 0)
950
+ return FALSE;
951
+
952
+ srcp += rleSizes[0] + rleSizes[1] + rleSizes[2];
953
+
954
+ if (useAlpha)
955
+ {
956
+ status = planar_decompress_plane_rle(
957
+ planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), pTempData,
958
+ nTempStep, nXDst, nYDst, nSrcWidth, nSrcHeight, 3, vFlip); /* AlphaPlane */
959
+ }
960
+ else
961
+ status = planar_set_plane(0xFF, pTempData, nTempStep, nXDst, nYDst, nSrcWidth,
962
+ nSrcHeight, 3, vFlip);
963
+
964
+ if (status < 0)
965
+ return FALSE;
966
+
967
+ if (alpha)
968
+ srcp += rleSizes[3];
969
+ }
970
+
971
+ if (pTempData != pDstData)
972
+ {
973
+ if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, w, h,
974
+ pTempData, TempFormat, nTempStep, nXDst, nYDst, NULL,
975
+ FREERDP_FLIP_NONE))
976
+ {
977
+ WLog_ERR(TAG, "planar image copy failed");
978
+ return FALSE;
979
+ }
980
+ }
981
+ }
982
+ else /* YCoCg */
983
+ {
984
+ UINT32 TempFormat = 0;
985
+ BYTE* pTempData = planar->pTempData;
986
+ UINT32 nTempStep = planar->nTempStep;
987
+ UINT32 nTotalHeight = planar->maxHeight;
988
+ BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep];
989
+
990
+ if (useAlpha)
991
+ TempFormat = PIXEL_FORMAT_BGRA32;
992
+ else
993
+ TempFormat = PIXEL_FORMAT_BGRX32;
994
+
995
+ if (!pTempData)
996
+ return FALSE;
997
+
998
+ if (rle) /* RLE encoded data. Decode and handle it like raw data. */
999
+ {
1000
+ BYTE* rleBuffer[4] = { 0 };
1001
+
1002
+ if (!planar->rlePlanesBuffer)
1003
+ return FALSE;
1004
+
1005
+ rleBuffer[3] = planar->rlePlanesBuffer; /* AlphaPlane */
1006
+ rleBuffer[0] = rleBuffer[3] + planeSize; /* LumaOrRedPlane */
1007
+ rleBuffer[1] = rleBuffer[0] + planeSize; /* OrangeChromaOrGreenPlane */
1008
+ rleBuffer[2] = rleBuffer[1] + planeSize; /* GreenChromaOrBluePlane */
1009
+ if (useAlpha)
1010
+ {
1011
+ status = planar_decompress_plane_rle_only(
1012
+ planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), rleBuffer[3],
1013
+ rawWidths[3], rawHeights[3]); /* AlphaPlane */
1014
+
1015
+ if (status < 0)
1016
+ return FALSE;
1017
+ }
1018
+
1019
+ if (alpha)
1020
+ srcp += rleSizes[3];
1021
+
1022
+ status = planar_decompress_plane_rle_only(
1023
+ planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), rleBuffer[0],
1024
+ rawWidths[0], rawHeights[0]); /* LumaPlane */
1025
+
1026
+ if (status < 0)
1027
+ return FALSE;
1028
+
1029
+ status = planar_decompress_plane_rle_only(
1030
+ planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), rleBuffer[1],
1031
+ rawWidths[1], rawHeights[1]); /* OrangeChromaPlane */
1032
+
1033
+ if (status < 0)
1034
+ return FALSE;
1035
+
1036
+ status = planar_decompress_plane_rle_only(
1037
+ planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), rleBuffer[2],
1038
+ rawWidths[2], rawHeights[2]); /* GreenChromaPlane */
1039
+
1040
+ if (status < 0)
1041
+ return FALSE;
1042
+
1043
+ planes[0] = rleBuffer[0];
1044
+ planes[1] = rleBuffer[1];
1045
+ planes[2] = rleBuffer[2];
1046
+ planes[3] = rleBuffer[3];
1047
+ }
1048
+
1049
+ /* RAW */
1050
+ {
1051
+ if (cs)
1052
+ { /* Chroma subsampling for Co and Cg:
1053
+ * Each pixel contains the value that should be expanded to
1054
+ * [2x,2y;2x+1,2y;2x+1,2y+1;2x;2y+1] */
1055
+ if (!planar_subsample_expand(planes[1], rawSizes[1], nSrcWidth, nSrcHeight,
1056
+ rawWidths[1], rawHeights[1], planar->deltaPlanes[0]))
1057
+ return FALSE;
1058
+
1059
+ planes[1] = planar->deltaPlanes[0];
1060
+ rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
1061
+ rawWidths[1] = nSrcWidth;
1062
+ rawHeights[1] = nSrcHeight;
1063
+
1064
+ if (!planar_subsample_expand(planes[2], rawSizes[2], nSrcWidth, nSrcHeight,
1065
+ rawWidths[2], rawHeights[2], planar->deltaPlanes[1]))
1066
+ return FALSE;
1067
+
1068
+ planes[2] = planar->deltaPlanes[1];
1069
+ rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
1070
+ rawWidths[2] = nSrcWidth;
1071
+ rawHeights[2] = nSrcHeight;
1072
+ }
1073
+
1074
+ if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
1075
+ nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
1076
+ return FALSE;
1077
+
1078
+ if (alpha)
1079
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
1080
+ else /* NoAlpha */
1081
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
1082
+
1083
+ if ((SrcSize - (srcp - pSrcData)) == 1)
1084
+ srcp++; /* pad */
1085
+ }
1086
+
1087
+ WINPR_ASSERT(prims->YCoCgToRGB_8u_AC4R);
1088
+ int rc = prims->YCoCgToRGB_8u_AC4R(
1089
+ pTempData, WINPR_ASSERTING_INT_CAST(int32_t, nTempStep), dst, DstFormat,
1090
+ WINPR_ASSERTING_INT_CAST(int32_t, nDstStep), w, h, cll, useAlpha);
1091
+ if (rc != PRIMITIVES_SUCCESS)
1092
+ {
1093
+ WLog_ERR(TAG, "YCoCgToRGB_8u_AC4R failed with %d", rc);
1094
+ return FALSE;
1095
+ }
1096
+ }
1097
+
1098
+ WINPR_UNUSED(srcp);
1099
+ return TRUE;
1100
+ }
1101
+
1102
+ static INLINE BOOL freerdp_split_color_planes(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
1103
+ const BYTE* WINPR_RESTRICT data, UINT32 format,
1104
+ UINT32 width, UINT32 height, UINT32 scanline,
1105
+ BYTE* WINPR_RESTRICT planes[4])
1106
+ {
1107
+ WINPR_ASSERT(planar);
1108
+
1109
+ if ((width > INT32_MAX) || (height > INT32_MAX) || (scanline > INT32_MAX))
1110
+ return FALSE;
1111
+
1112
+ if (scanline == 0)
1113
+ scanline = width * FreeRDPGetBytesPerPixel(format);
1114
+
1115
+ if (planar->topdown)
1116
+ {
1117
+ UINT32 k = 0;
1118
+ for (UINT32 i = 0; i < height; i++)
1119
+ {
1120
+ const BYTE* pixel = &data[1ULL * scanline * i];
1121
+
1122
+ for (UINT32 j = 0; j < width; j++)
1123
+ {
1124
+ const UINT32 color = FreeRDPReadColor(pixel, format);
1125
+ pixel += FreeRDPGetBytesPerPixel(format);
1126
+ FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1127
+ &planes[0][k], NULL);
1128
+ k++;
1129
+ }
1130
+ }
1131
+ }
1132
+ else
1133
+ {
1134
+ UINT32 k = 0;
1135
+
1136
+ for (INT64 i = (INT64)height - 1; i >= 0; i--)
1137
+ {
1138
+ const BYTE* pixel = &data[1ULL * scanline * (UINT32)i];
1139
+
1140
+ for (UINT32 j = 0; j < width; j++)
1141
+ {
1142
+ const UINT32 color = FreeRDPReadColor(pixel, format);
1143
+ pixel += FreeRDPGetBytesPerPixel(format);
1144
+ FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1145
+ &planes[0][k], NULL);
1146
+ k++;
1147
+ }
1148
+ }
1149
+ }
1150
+ return TRUE;
1151
+ }
1152
+
1153
+ static INLINE UINT32 freerdp_bitmap_planar_write_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1154
+ UINT32 cRawBytes, UINT32 nRunLength,
1155
+ BYTE* WINPR_RESTRICT pOutBuffer,
1156
+ UINT32 outBufferSize)
1157
+ {
1158
+ const BYTE* pInput = NULL;
1159
+ BYTE* pOutput = NULL;
1160
+ BYTE controlByte = 0;
1161
+ UINT32 nBytesToWrite = 0;
1162
+ pInput = pInBuffer;
1163
+ pOutput = pOutBuffer;
1164
+
1165
+ if (!cRawBytes && !nRunLength)
1166
+ return 0;
1167
+
1168
+ if (nRunLength < 3)
1169
+ {
1170
+ cRawBytes += nRunLength;
1171
+ nRunLength = 0;
1172
+ }
1173
+
1174
+ while (cRawBytes)
1175
+ {
1176
+ if (cRawBytes < 16)
1177
+ {
1178
+ if (nRunLength > 15)
1179
+ {
1180
+ if (nRunLength < 18)
1181
+ {
1182
+ controlByte = PLANAR_CONTROL_BYTE(13, cRawBytes);
1183
+ nRunLength -= 13;
1184
+ cRawBytes = 0;
1185
+ }
1186
+ else
1187
+ {
1188
+ controlByte = PLANAR_CONTROL_BYTE(15, cRawBytes);
1189
+ nRunLength -= 15;
1190
+ cRawBytes = 0;
1191
+ }
1192
+ }
1193
+ else
1194
+ {
1195
+ controlByte = PLANAR_CONTROL_BYTE(nRunLength, cRawBytes);
1196
+ nRunLength = 0;
1197
+ cRawBytes = 0;
1198
+ }
1199
+ }
1200
+ else
1201
+ {
1202
+ controlByte = PLANAR_CONTROL_BYTE(0, 15);
1203
+ cRawBytes -= 15;
1204
+ }
1205
+
1206
+ if (outBufferSize < 1)
1207
+ return 0;
1208
+
1209
+ outBufferSize--;
1210
+ *pOutput = controlByte;
1211
+ pOutput++;
1212
+ nBytesToWrite = (controlByte >> 4);
1213
+
1214
+ if (nBytesToWrite)
1215
+ {
1216
+ if (outBufferSize < nBytesToWrite)
1217
+ return 0;
1218
+
1219
+ outBufferSize -= nBytesToWrite;
1220
+ CopyMemory(pOutput, pInput, nBytesToWrite);
1221
+ pOutput += nBytesToWrite;
1222
+ pInput += nBytesToWrite;
1223
+ }
1224
+ }
1225
+
1226
+ while (nRunLength)
1227
+ {
1228
+ if (nRunLength > 47)
1229
+ {
1230
+ if (nRunLength < 50)
1231
+ {
1232
+ controlByte = PLANAR_CONTROL_BYTE(2, 13);
1233
+ nRunLength -= 45;
1234
+ }
1235
+ else
1236
+ {
1237
+ controlByte = PLANAR_CONTROL_BYTE(2, 15);
1238
+ nRunLength -= 47;
1239
+ }
1240
+ }
1241
+ else if (nRunLength > 31)
1242
+ {
1243
+ controlByte = PLANAR_CONTROL_BYTE(2, (nRunLength - 32));
1244
+ nRunLength = 0;
1245
+ }
1246
+ else if (nRunLength > 15)
1247
+ {
1248
+ controlByte = PLANAR_CONTROL_BYTE(1, (nRunLength - 16));
1249
+ nRunLength = 0;
1250
+ }
1251
+ else
1252
+ {
1253
+ controlByte = PLANAR_CONTROL_BYTE(nRunLength, 0);
1254
+ nRunLength = 0;
1255
+ }
1256
+
1257
+ if (outBufferSize < 1)
1258
+ return 0;
1259
+
1260
+ --outBufferSize;
1261
+ *pOutput = controlByte;
1262
+ pOutput++;
1263
+ }
1264
+
1265
+ const intptr_t diff = (pOutput - pOutBuffer);
1266
+ if ((diff < 0) || (diff > UINT32_MAX))
1267
+ return 0;
1268
+ return (UINT32)diff;
1269
+ }
1270
+
1271
+ static INLINE UINT32 freerdp_bitmap_planar_encode_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1272
+ UINT32 inBufferSize,
1273
+ BYTE* WINPR_RESTRICT pOutBuffer,
1274
+ UINT32 outBufferSize)
1275
+ {
1276
+ BYTE symbol = 0;
1277
+ const BYTE* pInput = NULL;
1278
+ BYTE* pOutput = NULL;
1279
+ const BYTE* pBytes = NULL;
1280
+ UINT32 cRawBytes = 0;
1281
+ UINT32 nRunLength = 0;
1282
+ UINT32 bSymbolMatch = 0;
1283
+ UINT32 nBytesWritten = 0;
1284
+ UINT32 nTotalBytesWritten = 0;
1285
+ symbol = 0;
1286
+ cRawBytes = 0;
1287
+ nRunLength = 0;
1288
+ pInput = pInBuffer;
1289
+ pOutput = pOutBuffer;
1290
+ nTotalBytesWritten = 0;
1291
+
1292
+ if (!outBufferSize)
1293
+ return 0;
1294
+
1295
+ do
1296
+ {
1297
+ if (!inBufferSize)
1298
+ break;
1299
+
1300
+ bSymbolMatch = (symbol == *pInput) ? TRUE : FALSE;
1301
+ symbol = *pInput;
1302
+ pInput++;
1303
+ inBufferSize--;
1304
+
1305
+ if (nRunLength && !bSymbolMatch)
1306
+ {
1307
+ if (nRunLength < 3)
1308
+ {
1309
+ cRawBytes += nRunLength;
1310
+ nRunLength = 0;
1311
+ }
1312
+ else
1313
+ {
1314
+ pBytes = pInput - (cRawBytes + nRunLength + 1);
1315
+ nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1316
+ pOutput, outBufferSize);
1317
+ nRunLength = 0;
1318
+
1319
+ if (!nBytesWritten || (nBytesWritten > outBufferSize))
1320
+ return nRunLength;
1321
+
1322
+ nTotalBytesWritten += nBytesWritten;
1323
+ outBufferSize -= nBytesWritten;
1324
+ pOutput += nBytesWritten;
1325
+ cRawBytes = 0;
1326
+ }
1327
+ }
1328
+
1329
+ nRunLength += bSymbolMatch;
1330
+ cRawBytes += (!bSymbolMatch) ? TRUE : FALSE;
1331
+ } while (outBufferSize);
1332
+
1333
+ if (cRawBytes || nRunLength)
1334
+ {
1335
+ pBytes = pInput - (cRawBytes + nRunLength);
1336
+ nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1337
+ pOutput, outBufferSize);
1338
+
1339
+ if (!nBytesWritten)
1340
+ return 0;
1341
+
1342
+ nTotalBytesWritten += nBytesWritten;
1343
+ }
1344
+
1345
+ if (inBufferSize)
1346
+ return 0;
1347
+
1348
+ return nTotalBytesWritten;
1349
+ }
1350
+
1351
+ BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1352
+ UINT32 height, BYTE* WINPR_RESTRICT outPlane,
1353
+ UINT32* WINPR_RESTRICT dstSize)
1354
+ {
1355
+ UINT32 index = 0;
1356
+ const BYTE* pInput = NULL;
1357
+ BYTE* pOutput = NULL;
1358
+ UINT32 outBufferSize = 0;
1359
+ UINT32 nBytesWritten = 0;
1360
+ UINT32 nTotalBytesWritten = 0;
1361
+
1362
+ if (!outPlane)
1363
+ return FALSE;
1364
+
1365
+ index = 0;
1366
+ pInput = inPlane;
1367
+ pOutput = outPlane;
1368
+ outBufferSize = *dstSize;
1369
+ nTotalBytesWritten = 0;
1370
+
1371
+ while (outBufferSize)
1372
+ {
1373
+ nBytesWritten =
1374
+ freerdp_bitmap_planar_encode_rle_bytes(pInput, width, pOutput, outBufferSize);
1375
+
1376
+ if ((!nBytesWritten) || (nBytesWritten > outBufferSize))
1377
+ return FALSE;
1378
+
1379
+ outBufferSize -= nBytesWritten;
1380
+ nTotalBytesWritten += nBytesWritten;
1381
+ pOutput += nBytesWritten;
1382
+ pInput += width;
1383
+ index++;
1384
+
1385
+ if (index >= height)
1386
+ break;
1387
+ }
1388
+
1389
+ *dstSize = nTotalBytesWritten;
1390
+ return TRUE;
1391
+ }
1392
+
1393
+ static INLINE BOOL freerdp_bitmap_planar_compress_planes_rle(BYTE* WINPR_RESTRICT inPlanes[4],
1394
+ UINT32 width, UINT32 height,
1395
+ BYTE* WINPR_RESTRICT outPlanes,
1396
+ UINT32* WINPR_RESTRICT dstSizes,
1397
+ BOOL skipAlpha)
1398
+ {
1399
+ UINT32 outPlanesSize = width * height * 4;
1400
+
1401
+ /* AlphaPlane */
1402
+ if (skipAlpha)
1403
+ {
1404
+ dstSizes[0] = 0;
1405
+ }
1406
+ else
1407
+ {
1408
+ dstSizes[0] = outPlanesSize;
1409
+
1410
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[0], width, height, outPlanes,
1411
+ &dstSizes[0]))
1412
+ return FALSE;
1413
+
1414
+ outPlanes += dstSizes[0];
1415
+ outPlanesSize -= dstSizes[0];
1416
+ }
1417
+
1418
+ /* LumaOrRedPlane */
1419
+ dstSizes[1] = outPlanesSize;
1420
+
1421
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[1], width, height, outPlanes,
1422
+ &dstSizes[1]))
1423
+ return FALSE;
1424
+
1425
+ outPlanes += dstSizes[1];
1426
+ outPlanesSize -= dstSizes[1];
1427
+ /* OrangeChromaOrGreenPlane */
1428
+ dstSizes[2] = outPlanesSize;
1429
+
1430
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[2], width, height, outPlanes,
1431
+ &dstSizes[2]))
1432
+ return FALSE;
1433
+
1434
+ outPlanes += dstSizes[2];
1435
+ outPlanesSize -= dstSizes[2];
1436
+ /* GreenChromeOrBluePlane */
1437
+ dstSizes[3] = outPlanesSize;
1438
+
1439
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[3], width, height, outPlanes,
1440
+ &dstSizes[3]))
1441
+ return FALSE;
1442
+
1443
+ return TRUE;
1444
+ }
1445
+
1446
+ BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1447
+ UINT32 height, BYTE* WINPR_RESTRICT outPlane)
1448
+ {
1449
+ BYTE* outPtr = NULL;
1450
+ const BYTE* srcPtr = NULL;
1451
+ const BYTE* prevLinePtr = NULL;
1452
+
1453
+ if (!outPlane)
1454
+ {
1455
+ if (width * height == 0)
1456
+ return NULL;
1457
+
1458
+ if (!(outPlane = (BYTE*)calloc(height, width)))
1459
+ return NULL;
1460
+ }
1461
+
1462
+ // first line is copied as is
1463
+ CopyMemory(outPlane, inPlane, width);
1464
+ outPtr = outPlane + width;
1465
+ srcPtr = inPlane + width;
1466
+ prevLinePtr = inPlane;
1467
+
1468
+ for (UINT32 y = 1; y < height; y++)
1469
+ {
1470
+ for (UINT32 x = 0; x < width; x++, outPtr++, srcPtr++, prevLinePtr++)
1471
+ {
1472
+ INT32 delta = *srcPtr - *prevLinePtr;
1473
+ BYTE s2c = WINPR_ASSERTING_INT_CAST(
1474
+ BYTE, (delta >= 0) ? delta : (~((BYTE)(-delta)) + 1) & 0xFF);
1475
+ s2c = WINPR_ASSERTING_INT_CAST(
1476
+ BYTE,
1477
+ (s2c >= 0) ? (s2c << 1) & 0xFF : (((UINT32)(~((BYTE)s2c) + 1) << 1) - 1) & 0xFF);
1478
+ *outPtr = s2c;
1479
+ }
1480
+ }
1481
+
1482
+ return outPlane;
1483
+ }
1484
+
1485
+ static INLINE BOOL freerdp_bitmap_planar_delta_encode_planes(BYTE* WINPR_RESTRICT inPlanes[4],
1486
+ UINT32 width, UINT32 height,
1487
+ BYTE* WINPR_RESTRICT outPlanes[4])
1488
+ {
1489
+ for (UINT32 i = 0; i < 4; i++)
1490
+ {
1491
+ outPlanes[i] =
1492
+ freerdp_bitmap_planar_delta_encode_plane(inPlanes[i], width, height, outPlanes[i]);
1493
+
1494
+ if (!outPlanes[i])
1495
+ return FALSE;
1496
+ }
1497
+
1498
+ return TRUE;
1499
+ }
1500
+
1501
+ BYTE* freerdp_bitmap_compress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1502
+ const BYTE* WINPR_RESTRICT data, UINT32 format, UINT32 width,
1503
+ UINT32 height, UINT32 scanline, BYTE* WINPR_RESTRICT dstData,
1504
+ UINT32* WINPR_RESTRICT pDstSize)
1505
+ {
1506
+ UINT32 size = 0;
1507
+ BYTE* dstp = NULL;
1508
+ UINT32 planeSize = 0;
1509
+ UINT32 dstSizes[4] = { 0 };
1510
+ BYTE FormatHeader = 0;
1511
+
1512
+ if (!context || !context->rlePlanesBuffer)
1513
+ return NULL;
1514
+
1515
+ if (context->AllowSkipAlpha)
1516
+ FormatHeader |= PLANAR_FORMAT_HEADER_NA;
1517
+
1518
+ planeSize = width * height;
1519
+
1520
+ if (!context->AllowSkipAlpha)
1521
+ format = planar_invert_format(context, TRUE, format);
1522
+
1523
+ if (!freerdp_split_color_planes(context, data, format, width, height, scanline,
1524
+ context->planes))
1525
+ return NULL;
1526
+
1527
+ if (context->AllowRunLengthEncoding)
1528
+ {
1529
+ if (!freerdp_bitmap_planar_delta_encode_planes(context->planes, width, height,
1530
+ context->deltaPlanes))
1531
+ return NULL;
1532
+
1533
+ if (!freerdp_bitmap_planar_compress_planes_rle(context->deltaPlanes, width, height,
1534
+ context->rlePlanesBuffer, dstSizes,
1535
+ context->AllowSkipAlpha))
1536
+ return NULL;
1537
+
1538
+ {
1539
+ uint32_t offset = 0;
1540
+ FormatHeader |= PLANAR_FORMAT_HEADER_RLE;
1541
+ context->rlePlanes[0] = &context->rlePlanesBuffer[offset];
1542
+ offset += dstSizes[0];
1543
+ context->rlePlanes[1] = &context->rlePlanesBuffer[offset];
1544
+ offset += dstSizes[1];
1545
+ context->rlePlanes[2] = &context->rlePlanesBuffer[offset];
1546
+ offset += dstSizes[2];
1547
+ context->rlePlanes[3] = &context->rlePlanesBuffer[offset];
1548
+
1549
+ #if defined(WITH_DEBUG_CODECS)
1550
+ WLog_DBG(TAG,
1551
+ "R: [%" PRIu32 "/%" PRIu32 "] G: [%" PRIu32 "/%" PRIu32 "] B: [%" PRIu32
1552
+ " / %" PRIu32 "] ",
1553
+ dstSizes[1], planeSize, dstSizes[2], planeSize, dstSizes[3], planeSize);
1554
+ #endif
1555
+ }
1556
+ }
1557
+
1558
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1559
+ {
1560
+ if (!context->AllowRunLengthEncoding)
1561
+ return NULL;
1562
+
1563
+ if (context->rlePlanes[0] == NULL)
1564
+ return NULL;
1565
+
1566
+ if (context->rlePlanes[1] == NULL)
1567
+ return NULL;
1568
+
1569
+ if (context->rlePlanes[2] == NULL)
1570
+ return NULL;
1571
+
1572
+ if (context->rlePlanes[3] == NULL)
1573
+ return NULL;
1574
+ }
1575
+
1576
+ if (!dstData)
1577
+ {
1578
+ size = 1;
1579
+
1580
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1581
+ {
1582
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1583
+ size += dstSizes[0];
1584
+ else
1585
+ size += planeSize;
1586
+ }
1587
+
1588
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1589
+ size += (dstSizes[1] + dstSizes[2] + dstSizes[3]);
1590
+ else
1591
+ size += (planeSize * 3);
1592
+
1593
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1594
+ size++;
1595
+
1596
+ dstData = malloc(size);
1597
+
1598
+ if (!dstData)
1599
+ return NULL;
1600
+
1601
+ *pDstSize = size;
1602
+ }
1603
+
1604
+ dstp = dstData;
1605
+ *dstp = FormatHeader; /* FormatHeader */
1606
+ dstp++;
1607
+
1608
+ /* AlphaPlane */
1609
+
1610
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1611
+ {
1612
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1613
+ {
1614
+ CopyMemory(dstp, context->rlePlanes[0], dstSizes[0]); /* Alpha */
1615
+ dstp += dstSizes[0];
1616
+ }
1617
+ else
1618
+ {
1619
+ CopyMemory(dstp, context->planes[0], planeSize); /* Alpha */
1620
+ dstp += planeSize;
1621
+ }
1622
+ }
1623
+
1624
+ /* LumaOrRedPlane */
1625
+
1626
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1627
+ {
1628
+ CopyMemory(dstp, context->rlePlanes[1], dstSizes[1]); /* Red */
1629
+ dstp += dstSizes[1];
1630
+ }
1631
+ else
1632
+ {
1633
+ CopyMemory(dstp, context->planes[1], planeSize); /* Red */
1634
+ dstp += planeSize;
1635
+ }
1636
+
1637
+ /* OrangeChromaOrGreenPlane */
1638
+
1639
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1640
+ {
1641
+ CopyMemory(dstp, context->rlePlanes[2], dstSizes[2]); /* Green */
1642
+ dstp += dstSizes[2];
1643
+ }
1644
+ else
1645
+ {
1646
+ CopyMemory(dstp, context->planes[2], planeSize); /* Green */
1647
+ dstp += planeSize;
1648
+ }
1649
+
1650
+ /* GreenChromeOrBluePlane */
1651
+
1652
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1653
+ {
1654
+ CopyMemory(dstp, context->rlePlanes[3], dstSizes[3]); /* Blue */
1655
+ dstp += dstSizes[3];
1656
+ }
1657
+ else
1658
+ {
1659
+ CopyMemory(dstp, context->planes[3], planeSize); /* Blue */
1660
+ dstp += planeSize;
1661
+ }
1662
+
1663
+ /* Pad1 (1 byte) */
1664
+
1665
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1666
+ {
1667
+ *dstp = 0;
1668
+ dstp++;
1669
+ }
1670
+
1671
+ const intptr_t diff = (dstp - dstData);
1672
+ if ((diff < 0) || (diff > UINT32_MAX))
1673
+ {
1674
+ free(dstData);
1675
+ return NULL;
1676
+ }
1677
+ size = (UINT32)diff;
1678
+ *pDstSize = size;
1679
+ return dstData;
1680
+ }
1681
+
1682
+ BOOL freerdp_bitmap_planar_context_reset(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1683
+ UINT32 width, UINT32 height)
1684
+ {
1685
+ if (!context)
1686
+ return FALSE;
1687
+
1688
+ context->bgr = FALSE;
1689
+ context->maxWidth = PLANAR_ALIGN(width, 4);
1690
+ context->maxHeight = PLANAR_ALIGN(height, 4);
1691
+ {
1692
+ const UINT64 tmp = (UINT64)context->maxWidth * context->maxHeight;
1693
+ if (tmp > UINT32_MAX)
1694
+ return FALSE;
1695
+ context->maxPlaneSize = (UINT32)tmp;
1696
+ }
1697
+
1698
+ if (context->maxWidth > UINT32_MAX / 4)
1699
+ return FALSE;
1700
+ context->nTempStep = context->maxWidth * 4;
1701
+
1702
+ memset((void*)context->planes, 0, sizeof(context->planes));
1703
+ memset((void*)context->rlePlanes, 0, sizeof(context->rlePlanes));
1704
+ memset((void*)context->deltaPlanes, 0, sizeof(context->deltaPlanes));
1705
+
1706
+ if (context->maxPlaneSize > 0)
1707
+ {
1708
+ void* tmp = winpr_aligned_recalloc(context->planesBuffer, context->maxPlaneSize, 4, 32);
1709
+ if (!tmp)
1710
+ return FALSE;
1711
+ context->planesBuffer = tmp;
1712
+
1713
+ tmp = winpr_aligned_recalloc(context->pTempData, context->maxPlaneSize, 6, 32);
1714
+ if (!tmp)
1715
+ return FALSE;
1716
+ context->pTempData = tmp;
1717
+
1718
+ tmp = winpr_aligned_recalloc(context->deltaPlanesBuffer, context->maxPlaneSize, 4, 32);
1719
+ if (!tmp)
1720
+ return FALSE;
1721
+ context->deltaPlanesBuffer = tmp;
1722
+
1723
+ tmp = winpr_aligned_recalloc(context->rlePlanesBuffer, context->maxPlaneSize, 4, 32);
1724
+ if (!tmp)
1725
+ return FALSE;
1726
+ context->rlePlanesBuffer = tmp;
1727
+
1728
+ context->planes[0] = &context->planesBuffer[0ULL * context->maxPlaneSize];
1729
+ context->planes[1] = &context->planesBuffer[1ULL * context->maxPlaneSize];
1730
+ context->planes[2] = &context->planesBuffer[2ULL * context->maxPlaneSize];
1731
+ context->planes[3] = &context->planesBuffer[3ULL * context->maxPlaneSize];
1732
+ context->deltaPlanes[0] = &context->deltaPlanesBuffer[0ULL * context->maxPlaneSize];
1733
+ context->deltaPlanes[1] = &context->deltaPlanesBuffer[1ULL * context->maxPlaneSize];
1734
+ context->deltaPlanes[2] = &context->deltaPlanesBuffer[2ULL * context->maxPlaneSize];
1735
+ context->deltaPlanes[3] = &context->deltaPlanesBuffer[3ULL * context->maxPlaneSize];
1736
+ }
1737
+ return TRUE;
1738
+ }
1739
+
1740
+ BITMAP_PLANAR_CONTEXT* freerdp_bitmap_planar_context_new(DWORD flags, UINT32 maxWidth,
1741
+ UINT32 maxHeight)
1742
+ {
1743
+ BITMAP_PLANAR_CONTEXT* context =
1744
+ (BITMAP_PLANAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(BITMAP_PLANAR_CONTEXT), 32);
1745
+
1746
+ if (!context)
1747
+ return NULL;
1748
+
1749
+ if (flags & PLANAR_FORMAT_HEADER_NA)
1750
+ context->AllowSkipAlpha = TRUE;
1751
+
1752
+ if (flags & PLANAR_FORMAT_HEADER_RLE)
1753
+ context->AllowRunLengthEncoding = TRUE;
1754
+
1755
+ if (flags & PLANAR_FORMAT_HEADER_CS)
1756
+ context->AllowColorSubsampling = TRUE;
1757
+
1758
+ context->ColorLossLevel = flags & PLANAR_FORMAT_HEADER_CLL_MASK;
1759
+
1760
+ if (context->ColorLossLevel)
1761
+ context->AllowDynamicColorFidelity = TRUE;
1762
+
1763
+ if (!freerdp_bitmap_planar_context_reset(context, maxWidth, maxHeight))
1764
+ {
1765
+ WINPR_PRAGMA_DIAG_PUSH
1766
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1767
+ freerdp_bitmap_planar_context_free(context);
1768
+ WINPR_PRAGMA_DIAG_POP
1769
+ return NULL;
1770
+ }
1771
+
1772
+ return context;
1773
+ }
1774
+
1775
+ void freerdp_bitmap_planar_context_free(BITMAP_PLANAR_CONTEXT* context)
1776
+ {
1777
+ if (!context)
1778
+ return;
1779
+
1780
+ winpr_aligned_free(context->pTempData);
1781
+ winpr_aligned_free(context->planesBuffer);
1782
+ winpr_aligned_free(context->deltaPlanesBuffer);
1783
+ winpr_aligned_free(context->rlePlanesBuffer);
1784
+ winpr_aligned_free(context);
1785
+ }
1786
+
1787
+ void freerdp_planar_switch_bgr(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL bgr)
1788
+ {
1789
+ WINPR_ASSERT(planar);
1790
+ planar->bgr = bgr;
1791
+ }
1792
+
1793
+ void freerdp_planar_topdown_image(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL topdown)
1794
+ {
1795
+ WINPR_ASSERT(planar);
1796
+ planar->topdown = topdown;
1797
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/rfx_bitstream.h ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library - Bit Stream
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CODEC_RFX_BITSTREAM_H
21
+ #define FREERDP_LIB_CODEC_RFX_BITSTREAM_H
22
+
23
+ #include <winpr/assert.h>
24
+ #include <winpr/cast.h>
25
+
26
+ #include <freerdp/codec/rfx.h>
27
+
28
+ #ifdef __cplusplus
29
+ extern "C"
30
+ {
31
+ #endif
32
+
33
+ typedef struct
34
+ {
35
+ BYTE* buffer;
36
+ uint32_t nbytes;
37
+ uint32_t byte_pos;
38
+ uint32_t bits_left;
39
+ } RFX_BITSTREAM;
40
+
41
+ static inline void rfx_bitstream_attach(RFX_BITSTREAM* bs, BYTE* WINPR_RESTRICT buffer,
42
+ size_t nbytes)
43
+ {
44
+ WINPR_ASSERT(bs);
45
+ bs->buffer = (buffer);
46
+
47
+ WINPR_ASSERT(nbytes <= UINT32_MAX);
48
+ bs->nbytes = WINPR_ASSERTING_INT_CAST(uint32_t, nbytes);
49
+ bs->byte_pos = 0;
50
+ bs->bits_left = 8;
51
+ }
52
+
53
+ static inline uint32_t rfx_bitstream_get_bits(RFX_BITSTREAM* bs, uint32_t nbits)
54
+ {
55
+ UINT16 n = 0;
56
+ while (bs->byte_pos < bs->nbytes && nbits > 0)
57
+ {
58
+ uint32_t b = nbits;
59
+ if (b > bs->bits_left)
60
+ b = bs->bits_left;
61
+ if (n)
62
+ n <<= b;
63
+ n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1);
64
+ bs->bits_left -= b;
65
+ nbits -= b;
66
+ if (bs->bits_left == 0)
67
+ {
68
+ bs->bits_left = 8;
69
+ bs->byte_pos++;
70
+ }
71
+ }
72
+ return n;
73
+ }
74
+
75
+ static inline void rfx_bitstream_put_bits(RFX_BITSTREAM* bs, uint32_t _bits, uint32_t _nbits)
76
+ {
77
+ UINT16 bits = WINPR_ASSERTING_INT_CAST(UINT16, _bits);
78
+
79
+ uint32_t nbits = (_nbits);
80
+ while (bs->byte_pos < bs->nbytes && nbits > 0)
81
+ {
82
+ uint32_t b = nbits;
83
+ if (b > bs->bits_left)
84
+ b = bs->bits_left;
85
+ bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1))
86
+ << (bs->bits_left - b);
87
+ bs->bits_left -= b;
88
+ nbits -= b;
89
+ if (bs->bits_left == 0)
90
+ {
91
+ bs->bits_left = 8;
92
+ bs->byte_pos++;
93
+ }
94
+ }
95
+ }
96
+
97
+ static inline void rfx_bitstream_flush(RFX_BITSTREAM* bs)
98
+ {
99
+ WINPR_ASSERT(bs);
100
+ if (bs->bits_left != 8)
101
+ {
102
+ uint32_t _nbits = 8 - bs->bits_left;
103
+ rfx_bitstream_put_bits(bs, 0, _nbits);
104
+ }
105
+ }
106
+
107
+ static inline BOOL rfx_bitstream_eos(RFX_BITSTREAM* bs)
108
+ {
109
+ WINPR_ASSERT(bs);
110
+ return ((bs)->byte_pos >= (bs)->nbytes);
111
+ }
112
+
113
+ static inline uint32_t rfx_bitstream_left(RFX_BITSTREAM* bs)
114
+ {
115
+ WINPR_ASSERT(bs);
116
+
117
+ if ((bs)->byte_pos >= (bs)->nbytes)
118
+ return 0;
119
+
120
+ return ((bs)->nbytes - (bs)->byte_pos - 1) * 8 + (bs)->bits_left;
121
+ }
122
+
123
+ static inline uint32_t rfx_bitstream_get_processed_bytes(RFX_BITSTREAM* bs)
124
+ {
125
+ WINPR_ASSERT(bs);
126
+ if ((bs)->bits_left < 8)
127
+ return (bs)->byte_pos + 1;
128
+ return (bs)->byte_pos;
129
+ }
130
+
131
+ #ifdef __cplusplus
132
+ }
133
+ #endif
134
+ #endif /* FREERDP_LIB_CODEC_RFX_BITSTREAM_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/rfx_decode.c ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library - Decode
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ * Copyright 2011 Norbert Federa <norbert.federa@thincast.com>
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #include <freerdp/config.h>
22
+
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+ #include <string.h>
26
+
27
+ #include <winpr/stream.h>
28
+ #include <freerdp/primitives.h>
29
+
30
+ #include "rfx_types.h"
31
+ #include "rfx_rlgr.h"
32
+ #include "rfx_differential.h"
33
+ #include "rfx_quantization.h"
34
+ #include "rfx_dwt.h"
35
+
36
+ #include "rfx_decode.h"
37
+
38
+ static INLINE void rfx_decode_component(RFX_CONTEXT* WINPR_RESTRICT context,
39
+ const UINT32* WINPR_RESTRICT quantization_values,
40
+ const BYTE* WINPR_RESTRICT data, size_t size,
41
+ INT16* WINPR_RESTRICT buffer)
42
+ {
43
+ INT16* dwt_buffer = NULL;
44
+ dwt_buffer = BufferPool_Take(context->priv->BufferPool, -1); /* dwt_buffer */
45
+ PROFILER_ENTER(context->priv->prof_rfx_decode_component)
46
+ PROFILER_ENTER(context->priv->prof_rfx_rlgr_decode)
47
+ WINPR_ASSERT(size <= UINT32_MAX);
48
+ context->rlgr_decode(context->mode, data, (UINT32)size, buffer, 4096);
49
+ PROFILER_EXIT(context->priv->prof_rfx_rlgr_decode)
50
+ PROFILER_ENTER(context->priv->prof_rfx_differential_decode)
51
+ rfx_differential_decode(buffer + 4032, 64);
52
+ PROFILER_EXIT(context->priv->prof_rfx_differential_decode)
53
+ PROFILER_ENTER(context->priv->prof_rfx_quantization_decode)
54
+ context->quantization_decode(buffer, quantization_values);
55
+ PROFILER_EXIT(context->priv->prof_rfx_quantization_decode)
56
+ PROFILER_ENTER(context->priv->prof_rfx_dwt_2d_decode)
57
+ context->dwt_2d_decode(buffer, dwt_buffer);
58
+ PROFILER_EXIT(context->priv->prof_rfx_dwt_2d_decode)
59
+ PROFILER_EXIT(context->priv->prof_rfx_decode_component)
60
+ BufferPool_Return(context->priv->BufferPool, dwt_buffer);
61
+ }
62
+
63
+ /* rfx_decode_ycbcr_to_rgb code now resides in the primitives library. */
64
+
65
+ /* stride is bytes between rows in the output buffer. */
66
+ BOOL rfx_decode_rgb(RFX_CONTEXT* WINPR_RESTRICT context, const RFX_TILE* WINPR_RESTRICT tile,
67
+ BYTE* WINPR_RESTRICT rgb_buffer, UINT32 stride)
68
+ {
69
+ union
70
+ {
71
+ const INT16** cpv;
72
+ INT16** pv;
73
+ } cnv;
74
+ BOOL rc = TRUE;
75
+ BYTE* pBuffer = NULL;
76
+ INT16* pSrcDst[3];
77
+ UINT32* y_quants = NULL;
78
+ UINT32* cb_quants = NULL;
79
+ UINT32* cr_quants = NULL;
80
+ static const prim_size_t roi_64x64 = { 64, 64 };
81
+ const primitives_t* prims = primitives_get();
82
+ PROFILER_ENTER(context->priv->prof_rfx_decode_rgb)
83
+ y_quants = context->quants + (10ULL * tile->quantIdxY);
84
+ cb_quants = context->quants + (10ULL * tile->quantIdxCb);
85
+ cr_quants = context->quants + (10ULL * tile->quantIdxCr);
86
+ pBuffer = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1);
87
+ pSrcDst[0] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 0ULL) + 16ULL])); /* y_r_buffer */
88
+ pSrcDst[1] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 1ULL) + 16ULL])); /* cb_g_buffer */
89
+ pSrcDst[2] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 2ULL) + 16ULL])); /* cr_b_buffer */
90
+ rfx_decode_component(context, y_quants, tile->YData, tile->YLen, pSrcDst[0]); /* YData */
91
+ rfx_decode_component(context, cb_quants, tile->CbData, tile->CbLen, pSrcDst[1]); /* CbData */
92
+ rfx_decode_component(context, cr_quants, tile->CrData, tile->CrLen, pSrcDst[2]); /* CrData */
93
+ PROFILER_ENTER(context->priv->prof_rfx_ycbcr_to_rgb)
94
+
95
+ cnv.pv = pSrcDst;
96
+ if (prims->yCbCrToRGB_16s8u_P3AC4R(cnv.cpv, 64 * sizeof(INT16), rgb_buffer, stride,
97
+ context->pixel_format, &roi_64x64) != PRIMITIVES_SUCCESS)
98
+ rc = FALSE;
99
+
100
+ PROFILER_EXIT(context->priv->prof_rfx_ycbcr_to_rgb)
101
+ PROFILER_EXIT(context->priv->prof_rfx_decode_rgb)
102
+ BufferPool_Return(context->priv->BufferPool, pBuffer);
103
+ return rc;
104
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/rfx_decode.h ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library - Decode
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CODEC_RFX_DECODE_H
21
+ #define FREERDP_LIB_CODEC_RFX_DECODE_H
22
+
23
+ #include <winpr/wtypes.h>
24
+
25
+ #include <freerdp/codec/rfx.h>
26
+ #include <freerdp/api.h>
27
+
28
+ /* stride is bytes between rows in the output buffer. */
29
+ FREERDP_LOCAL BOOL rfx_decode_rgb(RFX_CONTEXT* WINPR_RESTRICT context,
30
+ const RFX_TILE* WINPR_RESTRICT tile,
31
+ BYTE* WINPR_RESTRICT rgb_buffer, UINT32 stride);
32
+
33
+ #endif /* FREERDP_LIB_CODEC_RFX_DECODE_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/xcrush.h ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * XCrush (RDP6.1) Bulk Data Compression
4
+ *
5
+ * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_CODEC_XCRUSH_H
21
+ #define FREERDP_CODEC_XCRUSH_H
22
+
23
+ #include <freerdp/api.h>
24
+ #include <freerdp/types.h>
25
+
26
+ #include "mppc.h"
27
+
28
+ typedef struct s_XCRUSH_CONTEXT XCRUSH_CONTEXT;
29
+
30
+ #ifdef __cplusplus
31
+ extern "C"
32
+ {
33
+ #endif
34
+
35
+ FREERDP_LOCAL int xcrush_compress(XCRUSH_CONTEXT* xcrush, const BYTE* pSrcData, UINT32 SrcSize,
36
+ BYTE* pDstBuffer, const BYTE** ppDstData, UINT32* pDstSize,
37
+ UINT32* pFlags);
38
+ FREERDP_LOCAL int xcrush_decompress(XCRUSH_CONTEXT* xcrush, const BYTE* pSrcData,
39
+ UINT32 SrcSize, const BYTE** ppDstData, UINT32* pDstSize,
40
+ UINT32 flags);
41
+
42
+ FREERDP_LOCAL void xcrush_context_reset(XCRUSH_CONTEXT* xcrush, BOOL flush);
43
+
44
+ FREERDP_LOCAL XCRUSH_CONTEXT* xcrush_context_new(BOOL Compressor);
45
+ FREERDP_LOCAL void xcrush_context_free(XCRUSH_CONTEXT* xcrush);
46
+
47
+ #ifdef __cplusplus
48
+ }
49
+ #endif
50
+
51
+ #endif /* FREERDP_CODEC_XCRUSH_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/core/client.c ADDED
@@ -0,0 +1,1509 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Client Channels
4
+ *
5
+ * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2015 Thincast Technologies GmbH
7
+ * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include "settings.h"
25
+
26
+ #include <winpr/assert.h>
27
+
28
+ #include <freerdp/log.h>
29
+ #include <freerdp/channels/drdynvc.h>
30
+
31
+ #include "rdp.h"
32
+ #include "client.h"
33
+
34
+ #define TAG FREERDP_TAG("core.client")
35
+
36
+ typedef struct
37
+ {
38
+ freerdp_channel_handle_fkt_t fkt;
39
+ void* userdata;
40
+ } ChannelEventEntry;
41
+
42
+ /* Use this instance to get access to channels in VirtualChannelInit. It is set during
43
+ * freerdp_connect so channels that use VirtualChannelInit must be initialized from the same thread
44
+ * as freerdp_connect was called */
45
+ static WINPR_TLS freerdp* g_Instance = NULL;
46
+
47
+ /* use global counter to ensure uniqueness across channel manager instances */
48
+ static volatile LONG g_OpenHandleSeq = 1;
49
+
50
+ /* HashTable mapping channel handles to CHANNEL_OPEN_DATA */
51
+ static INIT_ONCE g_ChannelHandlesOnce = INIT_ONCE_STATIC_INIT;
52
+ static wHashTable* g_ChannelHandles = NULL;
53
+
54
+ static BOOL freerdp_channels_process_message_free(wMessage* message, DWORD type);
55
+
56
+ static CHANNEL_OPEN_DATA* freerdp_channels_find_channel_open_data_by_name(rdpChannels* channels,
57
+ const char* name)
58
+ {
59
+ for (int index = 0; index < channels->openDataCount; index++)
60
+ {
61
+ CHANNEL_OPEN_DATA* pChannelOpenData = &channels->openDataList[index];
62
+
63
+ if (strncmp(name, pChannelOpenData->name, CHANNEL_NAME_LEN + 1) == 0)
64
+ return pChannelOpenData;
65
+ }
66
+
67
+ return NULL;
68
+ }
69
+
70
+ /* returns rdpChannel for the channel name passed in */
71
+ static rdpMcsChannel* freerdp_channels_find_channel_by_name(rdpRdp* rdp, const char* name)
72
+ {
73
+ rdpMcs* mcs = NULL;
74
+
75
+ if (!rdp)
76
+ return NULL;
77
+
78
+ mcs = rdp->mcs;
79
+
80
+ for (UINT32 index = 0; index < mcs->channelCount; index++)
81
+ {
82
+ rdpMcsChannel* channel = &mcs->channels[index];
83
+
84
+ if (strncmp(name, channel->Name, CHANNEL_NAME_LEN + 1) == 0)
85
+ {
86
+ return channel;
87
+ }
88
+ }
89
+
90
+ return NULL;
91
+ }
92
+
93
+ static rdpMcsChannel* freerdp_channels_find_channel_by_id(rdpRdp* rdp, UINT16 channel_id)
94
+ {
95
+ rdpMcsChannel* channel = NULL;
96
+ rdpMcs* mcs = NULL;
97
+
98
+ if (!rdp)
99
+ return NULL;
100
+
101
+ mcs = rdp->mcs;
102
+
103
+ for (UINT32 index = 0; index < mcs->channelCount; index++)
104
+ {
105
+ channel = &mcs->channels[index];
106
+
107
+ if (channel->ChannelId == channel_id)
108
+ {
109
+ return channel;
110
+ }
111
+ }
112
+
113
+ return NULL;
114
+ }
115
+
116
+ static void channel_queue_message_free(wMessage* msg)
117
+ {
118
+ CHANNEL_OPEN_EVENT* ev = NULL;
119
+
120
+ if (!msg || (msg->id != 0))
121
+ return;
122
+
123
+ ev = (CHANNEL_OPEN_EVENT*)msg->wParam;
124
+ free(ev);
125
+ }
126
+
127
+ static void channel_queue_free(void* obj)
128
+ {
129
+ wMessage* msg = (wMessage*)obj;
130
+ freerdp_channels_process_message_free(msg, CHANNEL_EVENT_WRITE_CANCELLED);
131
+ channel_queue_message_free(msg);
132
+ }
133
+
134
+ static BOOL CALLBACK init_channel_handles_table(PINIT_ONCE once, PVOID param, PVOID* context)
135
+ {
136
+ g_ChannelHandles = HashTable_New(TRUE);
137
+ return TRUE;
138
+ }
139
+
140
+ static void* channel_event_entry_clone(const void* data)
141
+ {
142
+ const ChannelEventEntry* entry = data;
143
+ if (!entry)
144
+ return NULL;
145
+
146
+ ChannelEventEntry* copy = calloc(1, sizeof(ChannelEventEntry));
147
+ if (!copy)
148
+ return NULL;
149
+ *copy = *entry;
150
+ return copy;
151
+ }
152
+
153
+ rdpChannels* freerdp_channels_new(freerdp* instance)
154
+ {
155
+ wObject* obj = NULL;
156
+ rdpChannels* channels = NULL;
157
+ channels = (rdpChannels*)calloc(1, sizeof(rdpChannels));
158
+
159
+ if (!channels)
160
+ return NULL;
161
+
162
+ InitOnceExecuteOnce(&g_ChannelHandlesOnce, init_channel_handles_table, NULL, NULL);
163
+
164
+ if (!g_ChannelHandles)
165
+ goto error;
166
+ if (!InitializeCriticalSectionAndSpinCount(&channels->channelsLock, 4000))
167
+ goto error;
168
+
169
+ channels->instance = instance;
170
+ channels->queue = MessageQueue_New(NULL);
171
+
172
+ if (!channels->queue)
173
+ goto error;
174
+
175
+ obj = MessageQueue_Object(channels->queue);
176
+ obj->fnObjectFree = channel_queue_free;
177
+
178
+ channels->channelEvents = HashTable_New(FALSE);
179
+ if (!channels->channelEvents)
180
+ goto error;
181
+
182
+ obj = HashTable_ValueObject(channels->channelEvents);
183
+ WINPR_ASSERT(obj);
184
+ obj->fnObjectFree = free;
185
+ obj->fnObjectNew = channel_event_entry_clone;
186
+ return channels;
187
+ error:
188
+ WINPR_PRAGMA_DIAG_PUSH
189
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
190
+ freerdp_channels_free(channels);
191
+ WINPR_PRAGMA_DIAG_POP
192
+ return NULL;
193
+ }
194
+
195
+ void freerdp_channels_free(rdpChannels* channels)
196
+ {
197
+ if (!channels)
198
+ return;
199
+
200
+ HashTable_Free(channels->channelEvents);
201
+
202
+ DeleteCriticalSection(&channels->channelsLock);
203
+
204
+ if (channels->queue)
205
+ {
206
+ MessageQueue_Free(channels->queue);
207
+ channels->queue = NULL;
208
+ }
209
+
210
+ free(channels);
211
+ }
212
+
213
+ /**
214
+ * Function description
215
+ *
216
+ * @return 0 on success, otherwise a Win32 error code
217
+ */
218
+ static UINT freerdp_drdynvc_on_channel_connected(DrdynvcClientContext* context, const char* name,
219
+ void* pInterface)
220
+ {
221
+ UINT status = CHANNEL_RC_OK;
222
+ ChannelConnectedEventArgs e = { 0 };
223
+ rdpChannels* channels = (rdpChannels*)context->custom;
224
+ freerdp* instance = channels->instance;
225
+ EventArgsInit(&e, "freerdp");
226
+ e.name = name;
227
+ e.pInterface = pInterface;
228
+ PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e);
229
+ return status;
230
+ }
231
+
232
+ /**
233
+ * Function description
234
+ *
235
+ * @return 0 on success, otherwise a Win32 error code
236
+ */
237
+ static UINT freerdp_drdynvc_on_channel_disconnected(DrdynvcClientContext* context, const char* name,
238
+ void* pInterface)
239
+ {
240
+ UINT status = CHANNEL_RC_OK;
241
+ ChannelDisconnectedEventArgs e = { 0 };
242
+ rdpChannels* channels = (rdpChannels*)context->custom;
243
+ freerdp* instance = channels->instance;
244
+ EventArgsInit(&e, "freerdp");
245
+ e.name = name;
246
+ e.pInterface = pInterface;
247
+ PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e);
248
+ return status;
249
+ }
250
+
251
+ static UINT freerdp_drdynvc_on_channel_attached(DrdynvcClientContext* context, const char* name,
252
+ void* pInterface)
253
+ {
254
+ UINT status = CHANNEL_RC_OK;
255
+ ChannelAttachedEventArgs e = { 0 };
256
+ rdpChannels* channels = (rdpChannels*)context->custom;
257
+ freerdp* instance = channels->instance;
258
+ EventArgsInit(&e, "freerdp");
259
+ e.name = name;
260
+ e.pInterface = pInterface;
261
+ PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e);
262
+ return status;
263
+ }
264
+
265
+ static UINT freerdp_drdynvc_on_channel_detached(DrdynvcClientContext* context, const char* name,
266
+ void* pInterface)
267
+ {
268
+ UINT status = CHANNEL_RC_OK;
269
+ ChannelDetachedEventArgs e = { 0 };
270
+ rdpChannels* channels = (rdpChannels*)context->custom;
271
+ freerdp* instance = channels->instance;
272
+ EventArgsInit(&e, "freerdp");
273
+ e.name = name;
274
+ e.pInterface = pInterface;
275
+ PubSub_OnChannelDetached(instance->context->pubSub, instance->context, &e);
276
+ return status;
277
+ }
278
+
279
+ void freerdp_channels_register_instance(rdpChannels* channels, freerdp* instance)
280
+ {
281
+ /* store instance in TLS so future VirtualChannelInit calls can use it */
282
+ g_Instance = instance;
283
+ }
284
+
285
+ /**
286
+ * go through and inform all the libraries that we are initialized
287
+ * called only from main thread
288
+ */
289
+ UINT freerdp_channels_pre_connect(rdpChannels* channels, freerdp* instance)
290
+ {
291
+ UINT error = CHANNEL_RC_OK;
292
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
293
+
294
+ MessageQueue_Clear(channels->queue);
295
+
296
+ for (int index = 0; index < channels->clientDataCount; index++)
297
+ {
298
+ pChannelClientData = &channels->clientDataList[index];
299
+
300
+ if (pChannelClientData->pChannelInitEventProc)
301
+ {
302
+ pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
303
+ CHANNEL_EVENT_INITIALIZED, 0, 0);
304
+ }
305
+ else if (pChannelClientData->pChannelInitEventProcEx)
306
+ {
307
+ pChannelClientData->pChannelInitEventProcEx(pChannelClientData->lpUserParam,
308
+ pChannelClientData->pInitHandle,
309
+ CHANNEL_EVENT_INITIALIZED, 0, 0);
310
+ }
311
+
312
+ if (CHANNEL_RC_OK != getChannelError(instance->context))
313
+ break;
314
+ }
315
+
316
+ return error;
317
+ }
318
+
319
+ UINT freerdp_channels_attach(freerdp* instance)
320
+ {
321
+ UINT error = CHANNEL_RC_OK;
322
+ const char* hostname = NULL;
323
+ size_t hostnameLength = 0;
324
+ rdpChannels* channels = NULL;
325
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
326
+
327
+ WINPR_ASSERT(instance);
328
+ WINPR_ASSERT(instance->context);
329
+ WINPR_ASSERT(instance->context->settings);
330
+
331
+ channels = instance->context->channels;
332
+ hostname = freerdp_settings_get_string(instance->context->settings, FreeRDP_ServerHostname);
333
+ WINPR_ASSERT(hostname);
334
+ hostnameLength = strnlen(hostname, MAX_PATH);
335
+
336
+ for (int index = 0; index < channels->clientDataCount; index++)
337
+ {
338
+ union
339
+ {
340
+ const void* cpv;
341
+ void* pv;
342
+ } cnv;
343
+ ChannelAttachedEventArgs e = { 0 };
344
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
345
+
346
+ cnv.cpv = hostname;
347
+ pChannelClientData = &channels->clientDataList[index];
348
+
349
+ if (pChannelClientData->pChannelInitEventProc)
350
+ {
351
+
352
+ pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
353
+ CHANNEL_EVENT_ATTACHED, cnv.pv,
354
+ (UINT)hostnameLength);
355
+ }
356
+ else if (pChannelClientData->pChannelInitEventProcEx)
357
+ {
358
+ pChannelClientData->pChannelInitEventProcEx(
359
+ pChannelClientData->lpUserParam, pChannelClientData->pInitHandle,
360
+ CHANNEL_EVENT_ATTACHED, cnv.pv, (UINT)hostnameLength);
361
+ }
362
+
363
+ if (getChannelError(instance->context) != CHANNEL_RC_OK)
364
+ goto fail;
365
+
366
+ pChannelOpenData = &channels->openDataList[index];
367
+ EventArgsInit(&e, "freerdp");
368
+ e.name = pChannelOpenData->name;
369
+ e.pInterface = pChannelOpenData->pInterface;
370
+ PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e);
371
+ }
372
+
373
+ fail:
374
+ return error;
375
+ }
376
+
377
+ UINT freerdp_channels_detach(freerdp* instance)
378
+ {
379
+ UINT error = CHANNEL_RC_OK;
380
+ const char* hostname = NULL;
381
+ size_t hostnameLength = 0;
382
+ rdpChannels* channels = NULL;
383
+ rdpContext* context = NULL;
384
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
385
+
386
+ WINPR_ASSERT(instance);
387
+
388
+ context = instance->context;
389
+ WINPR_ASSERT(context);
390
+
391
+ channels = context->channels;
392
+ WINPR_ASSERT(channels);
393
+
394
+ WINPR_ASSERT(context->settings);
395
+ hostname = freerdp_settings_get_string(context->settings, FreeRDP_ServerHostname);
396
+ WINPR_ASSERT(hostname);
397
+ hostnameLength = strnlen(hostname, MAX_PATH);
398
+
399
+ for (int index = 0; index < channels->clientDataCount; index++)
400
+ {
401
+ union
402
+ {
403
+ const void* cpv;
404
+ void* pv;
405
+ } cnv;
406
+
407
+ ChannelDetachedEventArgs e = { 0 };
408
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
409
+
410
+ cnv.cpv = hostname;
411
+ pChannelClientData = &channels->clientDataList[index];
412
+
413
+ if (pChannelClientData->pChannelInitEventProc)
414
+ {
415
+ pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
416
+ CHANNEL_EVENT_DETACHED, cnv.pv,
417
+ (UINT)hostnameLength);
418
+ }
419
+ else if (pChannelClientData->pChannelInitEventProcEx)
420
+ {
421
+ pChannelClientData->pChannelInitEventProcEx(
422
+ pChannelClientData->lpUserParam, pChannelClientData->pInitHandle,
423
+ CHANNEL_EVENT_DETACHED, cnv.pv, (UINT)hostnameLength);
424
+ }
425
+
426
+ if (getChannelError(context) != CHANNEL_RC_OK)
427
+ goto fail;
428
+
429
+ pChannelOpenData = &channels->openDataList[index];
430
+ EventArgsInit(&e, "freerdp");
431
+ e.name = pChannelOpenData->name;
432
+ e.pInterface = pChannelOpenData->pInterface;
433
+ PubSub_OnChannelDetached(context->pubSub, context, &e);
434
+ }
435
+
436
+ fail:
437
+ return error;
438
+ }
439
+
440
+ /**
441
+ * go through and inform all the libraries that we are connected
442
+ * this will tell the libraries that its ok to call MyVirtualChannelOpen
443
+ * called only from main thread
444
+ */
445
+ UINT freerdp_channels_post_connect(rdpChannels* channels, freerdp* instance)
446
+ {
447
+ UINT error = CHANNEL_RC_OK;
448
+ const char* hostname = NULL;
449
+ size_t hostnameLength = 0;
450
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
451
+
452
+ WINPR_ASSERT(channels);
453
+ WINPR_ASSERT(instance);
454
+ WINPR_ASSERT(instance->context);
455
+ WINPR_ASSERT(instance->context->settings);
456
+
457
+ channels->connected = TRUE;
458
+ hostname = freerdp_settings_get_string(instance->context->settings, FreeRDP_ServerHostname);
459
+ WINPR_ASSERT(hostname);
460
+ hostnameLength = strnlen(hostname, MAX_PATH);
461
+
462
+ for (int index = 0; index < channels->clientDataCount; index++)
463
+ {
464
+ union
465
+ {
466
+ const void* pcb;
467
+ void* pb;
468
+ } cnv;
469
+ ChannelConnectedEventArgs e = { 0 };
470
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
471
+ pChannelClientData = &channels->clientDataList[index];
472
+
473
+ cnv.pcb = hostname;
474
+ if (pChannelClientData->pChannelInitEventProc)
475
+ {
476
+ pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
477
+ CHANNEL_EVENT_CONNECTED, cnv.pb,
478
+ (UINT)hostnameLength);
479
+ }
480
+ else if (pChannelClientData->pChannelInitEventProcEx)
481
+ {
482
+ pChannelClientData->pChannelInitEventProcEx(
483
+ pChannelClientData->lpUserParam, pChannelClientData->pInitHandle,
484
+ CHANNEL_EVENT_CONNECTED, cnv.pb, (UINT)hostnameLength);
485
+ }
486
+
487
+ error = getChannelError(instance->context);
488
+ if (error != CHANNEL_RC_OK)
489
+ goto fail;
490
+
491
+ pChannelOpenData = &channels->openDataList[index];
492
+ EventArgsInit(&e, "freerdp");
493
+ e.name = pChannelOpenData->name;
494
+ e.pInterface = pChannelOpenData->pInterface;
495
+ PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e);
496
+ }
497
+
498
+ channels->drdynvc = (DrdynvcClientContext*)freerdp_channels_get_static_channel_interface(
499
+ channels, DRDYNVC_SVC_CHANNEL_NAME);
500
+
501
+ if (channels->drdynvc)
502
+ {
503
+ channels->drdynvc->custom = (void*)channels;
504
+ channels->drdynvc->OnChannelConnected = freerdp_drdynvc_on_channel_connected;
505
+ channels->drdynvc->OnChannelDisconnected = freerdp_drdynvc_on_channel_disconnected;
506
+ channels->drdynvc->OnChannelAttached = freerdp_drdynvc_on_channel_attached;
507
+ channels->drdynvc->OnChannelDetached = freerdp_drdynvc_on_channel_detached;
508
+ }
509
+
510
+ fail:
511
+ return error;
512
+ }
513
+
514
+ BOOL freerdp_channels_data(freerdp* instance, UINT16 channelId, const BYTE* cdata, size_t dataSize,
515
+ UINT32 flags, size_t totalSize)
516
+ {
517
+ rdpMcs* mcs = NULL;
518
+ rdpChannels* channels = NULL;
519
+ rdpMcsChannel* channel = NULL;
520
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
521
+ union
522
+ {
523
+ const BYTE* pcb;
524
+ BYTE* pb;
525
+ } data;
526
+
527
+ data.pcb = cdata;
528
+ if (!instance || !data.pcb)
529
+ {
530
+ WLog_ERR(TAG, "(%p, %" PRIu16 ", %p, 0x%08x): Invalid arguments", instance, channelId,
531
+ data.pcb, flags);
532
+ return FALSE;
533
+ }
534
+
535
+ mcs = instance->context->rdp->mcs;
536
+ channels = instance->context->channels;
537
+
538
+ if (!channels || !mcs)
539
+ {
540
+ return FALSE;
541
+ }
542
+
543
+ for (UINT32 index = 0; index < mcs->channelCount; index++)
544
+ {
545
+ rdpMcsChannel* cur = &mcs->channels[index];
546
+
547
+ if (cur->ChannelId == channelId)
548
+ {
549
+ channel = cur;
550
+ break;
551
+ }
552
+ }
553
+
554
+ if (!channel)
555
+ {
556
+ return FALSE;
557
+ }
558
+
559
+ pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, channel->Name);
560
+
561
+ if (!pChannelOpenData)
562
+ {
563
+ return FALSE;
564
+ }
565
+
566
+ if (pChannelOpenData->pChannelOpenEventProc)
567
+ {
568
+ pChannelOpenData->pChannelOpenEventProc(pChannelOpenData->OpenHandle,
569
+ CHANNEL_EVENT_DATA_RECEIVED, data.pb,
570
+ (UINT32)dataSize, (UINT32)totalSize, flags);
571
+ }
572
+ else if (pChannelOpenData->pChannelOpenEventProcEx)
573
+ {
574
+ pChannelOpenData->pChannelOpenEventProcEx(
575
+ pChannelOpenData->lpUserParam, pChannelOpenData->OpenHandle,
576
+ CHANNEL_EVENT_DATA_RECEIVED, data.pb, (UINT32)dataSize, (UINT32)totalSize, flags);
577
+ }
578
+
579
+ return TRUE;
580
+ }
581
+
582
+ UINT16 freerdp_channels_get_id_by_name(freerdp* instance, const char* channel_name)
583
+ {
584
+ if (!instance || !channel_name)
585
+ return 0;
586
+
587
+ rdpMcsChannel* mcsChannel =
588
+ freerdp_channels_find_channel_by_name(instance->context->rdp, channel_name);
589
+ if (!mcsChannel)
590
+ return 0;
591
+
592
+ return mcsChannel->ChannelId;
593
+ }
594
+
595
+ const char* freerdp_channels_get_name_by_id(freerdp* instance, UINT16 channelId)
596
+ {
597
+ rdpMcsChannel* mcsChannel = NULL;
598
+ if (!instance)
599
+ return NULL;
600
+
601
+ mcsChannel = freerdp_channels_find_channel_by_id(instance->context->rdp, channelId);
602
+ if (!mcsChannel)
603
+ return NULL;
604
+
605
+ return mcsChannel->Name;
606
+ }
607
+
608
+ BOOL freerdp_channels_process_message_free(wMessage* message, DWORD type)
609
+ {
610
+ if (message->id == WMQ_QUIT)
611
+ {
612
+ return FALSE;
613
+ }
614
+
615
+ if (message->id == 0)
616
+ {
617
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
618
+ CHANNEL_OPEN_EVENT* item = (CHANNEL_OPEN_EVENT*)message->wParam;
619
+
620
+ if (!item)
621
+ return FALSE;
622
+
623
+ pChannelOpenData = item->pChannelOpenData;
624
+
625
+ if (pChannelOpenData->pChannelOpenEventProc)
626
+ {
627
+ pChannelOpenData->pChannelOpenEventProc(pChannelOpenData->OpenHandle, type,
628
+ item->UserData, item->DataLength,
629
+ item->DataLength, 0);
630
+ }
631
+ else if (pChannelOpenData->pChannelOpenEventProcEx)
632
+ {
633
+ pChannelOpenData->pChannelOpenEventProcEx(
634
+ pChannelOpenData->lpUserParam, pChannelOpenData->OpenHandle, type, item->UserData,
635
+ item->DataLength, item->DataLength, 0);
636
+ }
637
+ }
638
+
639
+ return TRUE;
640
+ }
641
+
642
+ static BOOL freerdp_channels_process_message(freerdp* instance, wMessage* message)
643
+ {
644
+ BOOL ret = TRUE;
645
+ BOOL rc = FALSE;
646
+
647
+ WINPR_ASSERT(instance);
648
+ WINPR_ASSERT(message);
649
+
650
+ if (message->id == WMQ_QUIT)
651
+ goto fail;
652
+ else if (message->id == 0)
653
+ {
654
+ rdpMcsChannel* channel = NULL;
655
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
656
+ CHANNEL_OPEN_EVENT* item = (CHANNEL_OPEN_EVENT*)message->wParam;
657
+
658
+ if (!item)
659
+ goto fail;
660
+
661
+ pChannelOpenData = item->pChannelOpenData;
662
+ if (pChannelOpenData->flags != 2)
663
+ {
664
+ freerdp_channels_process_message_free(message, CHANNEL_EVENT_WRITE_CANCELLED);
665
+ goto fail;
666
+ }
667
+ channel =
668
+ freerdp_channels_find_channel_by_name(instance->context->rdp, pChannelOpenData->name);
669
+
670
+ if (channel)
671
+ ret = instance->SendChannelData(instance, channel->ChannelId, item->Data,
672
+ item->DataLength);
673
+ }
674
+
675
+ if (!freerdp_channels_process_message_free(message, CHANNEL_EVENT_WRITE_COMPLETE))
676
+ goto fail;
677
+
678
+ rc = ret;
679
+
680
+ fail:
681
+ IFCALL(message->Free, message);
682
+ return rc;
683
+ }
684
+
685
+ /**
686
+ * called only from main thread
687
+ */
688
+ static BOOL freerdp_channels_process_sync(rdpChannels* channels, freerdp* instance)
689
+ {
690
+ BOOL status = TRUE;
691
+ wMessage message = { 0 };
692
+
693
+ WINPR_ASSERT(channels);
694
+
695
+ while (MessageQueue_Peek(channels->queue, &message, TRUE))
696
+ {
697
+ if (!freerdp_channels_process_message(instance, &message))
698
+ status = FALSE;
699
+ }
700
+
701
+ return status;
702
+ }
703
+
704
+ /**
705
+ * called only from main thread
706
+ */
707
+ #if defined(WITH_FREERDP_DEPRECATED)
708
+ BOOL freerdp_channels_get_fds(rdpChannels* channels, freerdp* instance, void** read_fds,
709
+ int* read_count, void** write_fds, int* write_count)
710
+ {
711
+ void* pfd = NULL;
712
+ pfd = GetEventWaitObject(MessageQueue_Event(channels->queue));
713
+
714
+ if (pfd)
715
+ {
716
+ read_fds[*read_count] = pfd;
717
+ (*read_count)++;
718
+ }
719
+
720
+ return TRUE;
721
+ }
722
+ #endif
723
+
724
+ void* freerdp_channels_get_static_channel_interface(rdpChannels* channels, const char* name)
725
+ {
726
+ void* pInterface = NULL;
727
+ CHANNEL_OPEN_DATA* pChannelOpenData =
728
+ freerdp_channels_find_channel_open_data_by_name(channels, name);
729
+
730
+ if (pChannelOpenData)
731
+ pInterface = pChannelOpenData->pInterface;
732
+
733
+ return pInterface;
734
+ }
735
+
736
+ HANDLE freerdp_channels_get_event_handle(freerdp* instance)
737
+ {
738
+ if (!instance)
739
+ return INVALID_HANDLE_VALUE;
740
+
741
+ WINPR_ASSERT(instance->context);
742
+
743
+ rdpChannels* channels = instance->context->channels;
744
+ WINPR_ASSERT(channels);
745
+
746
+ return MessageQueue_Event(channels->queue);
747
+ }
748
+
749
+ static BOOL channels_process(const void* key, void* value, void* arg)
750
+ {
751
+ ChannelEventEntry* entry = value;
752
+ rdpContext* context = arg;
753
+
754
+ WINPR_UNUSED(key);
755
+
756
+ if (!entry->fkt)
757
+ return FALSE;
758
+ return entry->fkt(context, entry->userdata);
759
+ }
760
+
761
+ int freerdp_channels_process_pending_messages(freerdp* instance)
762
+ {
763
+ if (!instance)
764
+ return -1;
765
+
766
+ WINPR_ASSERT(instance->context);
767
+
768
+ rdpChannels* channels = instance->context->channels;
769
+ WINPR_ASSERT(channels);
770
+
771
+ const DWORD status = WaitForSingleObject(MessageQueue_Event(channels->queue), 0);
772
+ if (status == WAIT_OBJECT_0)
773
+ {
774
+ if (!freerdp_channels_process_sync(channels, instance))
775
+ return -1;
776
+ }
777
+
778
+ if (!HashTable_Foreach(channels->channelEvents, channels_process, instance->context))
779
+ return -1;
780
+
781
+ return 1;
782
+ }
783
+
784
+ /**
785
+ * called only from main thread
786
+ */
787
+ BOOL freerdp_channels_check_fds(rdpChannels* channels, freerdp* instance)
788
+ {
789
+ WINPR_ASSERT(channels);
790
+ WINPR_UNUSED(channels);
791
+
792
+ const int rc = freerdp_channels_process_pending_messages(instance);
793
+ return rc == 1;
794
+ }
795
+
796
+ BOOL freerdp_client_channel_register(rdpChannels* channels, HANDLE handle,
797
+ freerdp_channel_handle_fkt_t fkt, void* userdata)
798
+ {
799
+ if (!channels || (handle == INVALID_HANDLE_VALUE) || !fkt)
800
+ {
801
+ WLog_ERR(TAG, "Invalid function arguments (channels=%p, handle=%p, fkt=%p, userdata=%p",
802
+ channels, handle, fkt, userdata);
803
+ return FALSE;
804
+ }
805
+
806
+ ChannelEventEntry entry = { .fkt = fkt, .userdata = userdata };
807
+ return HashTable_Insert(channels->channelEvents, handle, &entry);
808
+ }
809
+
810
+ BOOL freerdp_client_channel_unregister(rdpChannels* channels, HANDLE handle)
811
+ {
812
+ if (!channels || (handle == INVALID_HANDLE_VALUE))
813
+ {
814
+ WLog_ERR(TAG, "Invalid function arguments (channels=%p, handle=%p", channels, handle);
815
+ return FALSE;
816
+ }
817
+
818
+ return HashTable_Remove(channels->channelEvents, handle);
819
+ }
820
+
821
+ SSIZE_T freerdp_client_channel_get_registered_event_handles(rdpChannels* channels, HANDLE* events,
822
+ DWORD count)
823
+ {
824
+ SSIZE_T rc = -1;
825
+
826
+ WINPR_ASSERT(channels);
827
+ WINPR_ASSERT(events || (count == 0));
828
+
829
+ HashTable_Lock(channels->channelEvents);
830
+ size_t len = HashTable_Count(channels->channelEvents);
831
+ if (len <= count)
832
+ {
833
+ ULONG_PTR* keys = NULL;
834
+ const size_t nrKeys = HashTable_GetKeys(channels->channelEvents, &keys);
835
+ if ((nrKeys <= SSIZE_MAX) && (nrKeys == len))
836
+ {
837
+ for (size_t x = 0; x < nrKeys; x++)
838
+ {
839
+ HANDLE cur = (HANDLE)keys[x];
840
+ events[x] = cur;
841
+ }
842
+ rc = (SSIZE_T)nrKeys;
843
+ }
844
+ free(keys);
845
+ }
846
+ HashTable_Unlock(channels->channelEvents);
847
+ return rc;
848
+ }
849
+
850
+ UINT freerdp_channels_disconnect(rdpChannels* channels, freerdp* instance)
851
+ {
852
+ UINT error = CHANNEL_RC_OK;
853
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
854
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
855
+
856
+ WINPR_ASSERT(channels);
857
+
858
+ if (!channels->connected)
859
+ return 0;
860
+
861
+ (void)freerdp_channels_check_fds(channels, instance);
862
+
863
+ /* tell all libraries we are shutting down */
864
+ for (int index = 0; index < channels->clientDataCount; index++)
865
+ {
866
+ ChannelDisconnectedEventArgs e = { 0 };
867
+ pChannelClientData = &channels->clientDataList[index];
868
+
869
+ if (pChannelClientData->pChannelInitEventProc)
870
+ {
871
+ pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
872
+ CHANNEL_EVENT_DISCONNECTED, 0, 0);
873
+ }
874
+ else if (pChannelClientData->pChannelInitEventProcEx)
875
+ {
876
+ pChannelClientData->pChannelInitEventProcEx(pChannelClientData->lpUserParam,
877
+ pChannelClientData->pInitHandle,
878
+ CHANNEL_EVENT_DISCONNECTED, 0, 0);
879
+ }
880
+
881
+ if (getChannelError(instance->context) != CHANNEL_RC_OK)
882
+ continue;
883
+
884
+ pChannelOpenData = &channels->openDataList[index];
885
+ EventArgsInit(&e, "freerdp");
886
+ e.name = pChannelOpenData->name;
887
+ e.pInterface = pChannelOpenData->pInterface;
888
+ PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e);
889
+ }
890
+
891
+ channels->connected = FALSE;
892
+
893
+ /* Flush pending messages */
894
+ (void)freerdp_channels_check_fds(channels, instance);
895
+ return error;
896
+ }
897
+
898
+ void freerdp_channels_close(rdpChannels* channels, freerdp* instance)
899
+ {
900
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
901
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
902
+
903
+ WINPR_ASSERT(channels);
904
+ WINPR_ASSERT(instance);
905
+
906
+ MessageQueue_PostQuit(channels->queue, 0);
907
+ (void)freerdp_channels_check_fds(channels, instance);
908
+
909
+ /* tell all libraries we are shutting down */
910
+ for (int index = 0; index < channels->clientDataCount; index++)
911
+ {
912
+ pChannelClientData = &channels->clientDataList[index];
913
+
914
+ if (pChannelClientData->pChannelInitEventProc)
915
+ {
916
+ pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
917
+ CHANNEL_EVENT_TERMINATED, 0, 0);
918
+ }
919
+ else if (pChannelClientData->pChannelInitEventProcEx)
920
+ {
921
+ pChannelClientData->pChannelInitEventProcEx(pChannelClientData->lpUserParam,
922
+ pChannelClientData->pInitHandle,
923
+ CHANNEL_EVENT_TERMINATED, 0, 0);
924
+ }
925
+ }
926
+
927
+ for (int index = 0; index < channels->openDataCount; index++)
928
+ {
929
+ pChannelOpenData = &channels->openDataList[index];
930
+ HashTable_Remove(g_ChannelHandles, (void*)(UINT_PTR)pChannelOpenData->OpenHandle);
931
+ }
932
+
933
+ channels->openDataCount = 0;
934
+ channels->initDataCount = 0;
935
+ channels->clientDataCount = 0;
936
+
937
+ WINPR_ASSERT(instance->context);
938
+ WINPR_ASSERT(instance->context->settings);
939
+ instance->context->settings->ChannelCount = 0;
940
+ g_Instance = NULL;
941
+ }
942
+
943
+ static UINT VCAPITYPE FreeRDP_VirtualChannelInitEx(
944
+ LPVOID lpUserParam, LPVOID clientContext, LPVOID pInitHandle, PCHANNEL_DEF pChannel,
945
+ INT channelCount, ULONG versionRequested, PCHANNEL_INIT_EVENT_EX_FN pChannelInitEventProcEx)
946
+ {
947
+ rdpSettings* settings = NULL;
948
+ CHANNEL_INIT_DATA* pChannelInitData = NULL;
949
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
950
+ rdpChannels* channels = NULL;
951
+
952
+ if (!pInitHandle)
953
+ return CHANNEL_RC_BAD_INIT_HANDLE;
954
+
955
+ if (!pChannel)
956
+ return CHANNEL_RC_BAD_CHANNEL;
957
+
958
+ if ((channelCount <= 0) || !pChannelInitEventProcEx)
959
+ return CHANNEL_RC_INITIALIZATION_ERROR;
960
+
961
+ pChannelInitData = (CHANNEL_INIT_DATA*)pInitHandle;
962
+ WINPR_ASSERT(pChannelInitData);
963
+
964
+ channels = pChannelInitData->channels;
965
+ WINPR_ASSERT(channels);
966
+
967
+ if (!channels->can_call_init)
968
+ return CHANNEL_RC_NOT_IN_VIRTUALCHANNELENTRY;
969
+
970
+ if ((channels->openDataCount + channelCount) > CHANNEL_MAX_COUNT)
971
+ return CHANNEL_RC_TOO_MANY_CHANNELS;
972
+
973
+ if (channels->connected)
974
+ return CHANNEL_RC_ALREADY_CONNECTED;
975
+
976
+ if (versionRequested != VIRTUAL_CHANNEL_VERSION_WIN2000)
977
+ {
978
+ }
979
+
980
+ for (int index = 0; index < channelCount; index++)
981
+ {
982
+ const PCHANNEL_DEF pChannelDef = &pChannel[index];
983
+
984
+ if (freerdp_channels_find_channel_open_data_by_name(channels, pChannelDef->name) != 0)
985
+ {
986
+ return CHANNEL_RC_BAD_CHANNEL;
987
+ }
988
+ }
989
+
990
+ pChannelInitData->pInterface = clientContext;
991
+ pChannelClientData = &channels->clientDataList[channels->clientDataCount];
992
+ pChannelClientData->pChannelInitEventProcEx = pChannelInitEventProcEx;
993
+ pChannelClientData->pInitHandle = pInitHandle;
994
+ pChannelClientData->lpUserParam = lpUserParam;
995
+ channels->clientDataCount++;
996
+
997
+ WINPR_ASSERT(channels->instance);
998
+ WINPR_ASSERT(channels->instance->context);
999
+ settings = channels->instance->context->settings;
1000
+ WINPR_ASSERT(settings);
1001
+
1002
+ for (int index = 0; index < channelCount; index++)
1003
+ {
1004
+ const PCHANNEL_DEF pChannelDef = &pChannel[index];
1005
+ CHANNEL_OPEN_DATA* pChannelOpenData = &channels->openDataList[channels->openDataCount];
1006
+
1007
+ WINPR_ASSERT(pChannelOpenData);
1008
+
1009
+ const LONG hdl = InterlockedIncrement(&g_OpenHandleSeq);
1010
+ pChannelOpenData->OpenHandle = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1011
+ pChannelOpenData->channels = channels;
1012
+ pChannelOpenData->lpUserParam = lpUserParam;
1013
+ if (!HashTable_Insert(g_ChannelHandles, (void*)(UINT_PTR)pChannelOpenData->OpenHandle,
1014
+ (void*)pChannelOpenData))
1015
+ {
1016
+ pChannelInitData->pInterface = NULL;
1017
+ return CHANNEL_RC_INITIALIZATION_ERROR;
1018
+ }
1019
+ pChannelOpenData->flags = 1; /* init */
1020
+ strncpy(pChannelOpenData->name, pChannelDef->name, CHANNEL_NAME_LEN);
1021
+ pChannelOpenData->options = pChannelDef->options;
1022
+
1023
+ const UINT32 max = freerdp_settings_get_uint32(settings, FreeRDP_ChannelDefArraySize);
1024
+ WINPR_ASSERT(max >= CHANNEL_MAX_COUNT);
1025
+ if (settings->ChannelCount < max)
1026
+ {
1027
+ CHANNEL_DEF* channel = freerdp_settings_get_pointer_array_writable(
1028
+ settings, FreeRDP_ChannelDefArray, settings->ChannelCount);
1029
+ if (!channel)
1030
+ continue;
1031
+ strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
1032
+ channel->options = pChannelDef->options;
1033
+ settings->ChannelCount++;
1034
+ }
1035
+
1036
+ channels->openDataCount++;
1037
+ }
1038
+
1039
+ return CHANNEL_RC_OK;
1040
+ }
1041
+
1042
+ static UINT VCAPITYPE FreeRDP_VirtualChannelInit(LPVOID* ppInitHandle, PCHANNEL_DEF pChannel,
1043
+ INT channelCount, ULONG versionRequested,
1044
+ PCHANNEL_INIT_EVENT_FN pChannelInitEventProc)
1045
+ {
1046
+ CHANNEL_DEF* channel = NULL;
1047
+ rdpSettings* settings = NULL;
1048
+ PCHANNEL_DEF pChannelDef = NULL;
1049
+ CHANNEL_INIT_DATA* pChannelInitData = NULL;
1050
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
1051
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
1052
+ rdpChannels* channels = NULL;
1053
+
1054
+ /* g_Instance should have been set during freerdp_connect - otherwise VirtualChannelInit was
1055
+ * called from a different thread */
1056
+ if (!g_Instance || !g_Instance->context)
1057
+ return CHANNEL_RC_NOT_INITIALIZED;
1058
+
1059
+ channels = g_Instance->context->channels;
1060
+
1061
+ if (!ppInitHandle || !channels)
1062
+ return CHANNEL_RC_BAD_INIT_HANDLE;
1063
+
1064
+ if (!pChannel)
1065
+ return CHANNEL_RC_BAD_CHANNEL;
1066
+
1067
+ if ((channelCount <= 0) || !pChannelInitEventProc)
1068
+ return CHANNEL_RC_INITIALIZATION_ERROR;
1069
+
1070
+ pChannelInitData = &(channels->initDataList[channels->initDataCount]);
1071
+ *ppInitHandle = pChannelInitData;
1072
+ channels->initDataCount++;
1073
+ pChannelInitData->channels = channels;
1074
+ pChannelInitData->pInterface = NULL;
1075
+
1076
+ if (!channels->can_call_init)
1077
+ return CHANNEL_RC_NOT_IN_VIRTUALCHANNELENTRY;
1078
+
1079
+ if (channels->openDataCount + channelCount > CHANNEL_MAX_COUNT)
1080
+ return CHANNEL_RC_TOO_MANY_CHANNELS;
1081
+
1082
+ if (channels->connected)
1083
+ return CHANNEL_RC_ALREADY_CONNECTED;
1084
+
1085
+ if (versionRequested != VIRTUAL_CHANNEL_VERSION_WIN2000)
1086
+ {
1087
+ }
1088
+
1089
+ for (int index = 0; index < channelCount; index++)
1090
+ {
1091
+ pChannelDef = &pChannel[index];
1092
+
1093
+ if (freerdp_channels_find_channel_open_data_by_name(channels, pChannelDef->name) != 0)
1094
+ {
1095
+ return CHANNEL_RC_BAD_CHANNEL;
1096
+ }
1097
+ }
1098
+
1099
+ pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1100
+ pChannelClientData->pChannelInitEventProc = pChannelInitEventProc;
1101
+ pChannelClientData->pInitHandle = *ppInitHandle;
1102
+ channels->clientDataCount++;
1103
+ settings = channels->instance->context->settings;
1104
+
1105
+ for (int index = 0; index < channelCount; index++)
1106
+ {
1107
+ UINT32 ChannelCount = freerdp_settings_get_uint32(settings, FreeRDP_ChannelCount);
1108
+
1109
+ pChannelDef = &pChannel[index];
1110
+
1111
+ pChannelOpenData = &channels->openDataList[channels->openDataCount];
1112
+
1113
+ const LONG hdl = InterlockedIncrement(&g_OpenHandleSeq);
1114
+ pChannelOpenData->OpenHandle = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1115
+ pChannelOpenData->channels = channels;
1116
+ if (!HashTable_Insert(g_ChannelHandles, (void*)(UINT_PTR)pChannelOpenData->OpenHandle,
1117
+ (void*)pChannelOpenData))
1118
+ return CHANNEL_RC_INITIALIZATION_ERROR;
1119
+ pChannelOpenData->flags = 1; /* init */
1120
+ strncpy(pChannelOpenData->name, pChannelDef->name, CHANNEL_NAME_LEN);
1121
+ pChannelOpenData->options = pChannelDef->options;
1122
+
1123
+ if (ChannelCount < CHANNEL_MAX_COUNT)
1124
+ {
1125
+ channel = freerdp_settings_get_pointer_array_writable(settings, FreeRDP_ChannelDefArray,
1126
+ ChannelCount++);
1127
+ strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
1128
+ channel->options = pChannelDef->options;
1129
+ if (!freerdp_settings_set_uint32(settings, FreeRDP_ChannelCount, ChannelCount))
1130
+ return ERROR_INTERNAL_ERROR;
1131
+ }
1132
+
1133
+ channels->openDataCount++;
1134
+ }
1135
+
1136
+ return CHANNEL_RC_OK;
1137
+ }
1138
+
1139
+ static UINT VCAPITYPE
1140
+ FreeRDP_VirtualChannelOpenEx(LPVOID pInitHandle, LPDWORD pOpenHandle, PCHAR pChannelName,
1141
+ PCHANNEL_OPEN_EVENT_EX_FN pChannelOpenEventProcEx)
1142
+ {
1143
+ void* pInterface = NULL;
1144
+ rdpChannels* channels = NULL;
1145
+ CHANNEL_INIT_DATA* pChannelInitData = NULL;
1146
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
1147
+ pChannelInitData = (CHANNEL_INIT_DATA*)pInitHandle;
1148
+ channels = pChannelInitData->channels;
1149
+ pInterface = pChannelInitData->pInterface;
1150
+
1151
+ if (!pOpenHandle)
1152
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1153
+
1154
+ if (!pChannelOpenEventProcEx)
1155
+ return CHANNEL_RC_BAD_PROC;
1156
+
1157
+ if (!channels->connected)
1158
+ return CHANNEL_RC_NOT_CONNECTED;
1159
+
1160
+ pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, pChannelName);
1161
+
1162
+ if (!pChannelOpenData)
1163
+ return CHANNEL_RC_UNKNOWN_CHANNEL_NAME;
1164
+
1165
+ if (pChannelOpenData->flags == 2)
1166
+ return CHANNEL_RC_ALREADY_OPEN;
1167
+
1168
+ pChannelOpenData->flags = 2; /* open */
1169
+ pChannelOpenData->pInterface = pInterface;
1170
+ pChannelOpenData->pChannelOpenEventProcEx = pChannelOpenEventProcEx;
1171
+ *pOpenHandle = pChannelOpenData->OpenHandle;
1172
+ return CHANNEL_RC_OK;
1173
+ }
1174
+
1175
+ static UINT VCAPITYPE FreeRDP_VirtualChannelOpen(LPVOID pInitHandle, LPDWORD pOpenHandle,
1176
+ PCHAR pChannelName,
1177
+ PCHANNEL_OPEN_EVENT_FN pChannelOpenEventProc)
1178
+ {
1179
+ void* pInterface = NULL;
1180
+ rdpChannels* channels = NULL;
1181
+ CHANNEL_INIT_DATA* pChannelInitData = NULL;
1182
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
1183
+ pChannelInitData = (CHANNEL_INIT_DATA*)pInitHandle;
1184
+ channels = pChannelInitData->channels;
1185
+ pInterface = pChannelInitData->pInterface;
1186
+
1187
+ if (!pOpenHandle)
1188
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1189
+
1190
+ if (!pChannelOpenEventProc)
1191
+ return CHANNEL_RC_BAD_PROC;
1192
+
1193
+ if (!channels->connected)
1194
+ return CHANNEL_RC_NOT_CONNECTED;
1195
+
1196
+ pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, pChannelName);
1197
+
1198
+ if (!pChannelOpenData)
1199
+ return CHANNEL_RC_UNKNOWN_CHANNEL_NAME;
1200
+
1201
+ if (pChannelOpenData->flags == 2)
1202
+ return CHANNEL_RC_ALREADY_OPEN;
1203
+
1204
+ pChannelOpenData->flags = 2; /* open */
1205
+ pChannelOpenData->pInterface = pInterface;
1206
+ pChannelOpenData->pChannelOpenEventProc = pChannelOpenEventProc;
1207
+ *pOpenHandle = pChannelOpenData->OpenHandle;
1208
+ return CHANNEL_RC_OK;
1209
+ }
1210
+
1211
+ static UINT VCAPITYPE FreeRDP_VirtualChannelCloseEx(LPVOID pInitHandle, DWORD openHandle)
1212
+ {
1213
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
1214
+
1215
+ if (!pInitHandle)
1216
+ return CHANNEL_RC_BAD_INIT_HANDLE;
1217
+
1218
+ pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (void*)(UINT_PTR)openHandle);
1219
+
1220
+ if (!pChannelOpenData)
1221
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1222
+
1223
+ if (pChannelOpenData->flags != 2)
1224
+ return CHANNEL_RC_NOT_OPEN;
1225
+
1226
+ pChannelOpenData->flags = 0;
1227
+ return CHANNEL_RC_OK;
1228
+ }
1229
+
1230
+ static UINT VCAPITYPE FreeRDP_VirtualChannelClose(DWORD openHandle)
1231
+ {
1232
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
1233
+
1234
+ pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (void*)(UINT_PTR)openHandle);
1235
+
1236
+ if (!pChannelOpenData)
1237
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1238
+
1239
+ if (pChannelOpenData->flags != 2)
1240
+ return CHANNEL_RC_NOT_OPEN;
1241
+
1242
+ pChannelOpenData->flags = 0;
1243
+ return CHANNEL_RC_OK;
1244
+ }
1245
+
1246
+ static UINT VCAPITYPE FreeRDP_VirtualChannelWriteEx(LPVOID pInitHandle, DWORD openHandle,
1247
+ LPVOID pData, ULONG dataLength,
1248
+ LPVOID pUserData)
1249
+ {
1250
+ rdpChannels* channels = NULL;
1251
+ CHANNEL_INIT_DATA* pChannelInitData = NULL;
1252
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
1253
+ CHANNEL_OPEN_EVENT* pChannelOpenEvent = NULL;
1254
+ wMessage message = { 0 };
1255
+
1256
+ if (!pInitHandle)
1257
+ return CHANNEL_RC_BAD_INIT_HANDLE;
1258
+
1259
+ pChannelInitData = (CHANNEL_INIT_DATA*)pInitHandle;
1260
+ channels = pChannelInitData->channels;
1261
+
1262
+ if (!channels)
1263
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1264
+
1265
+ pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (void*)(UINT_PTR)openHandle);
1266
+
1267
+ if (!pChannelOpenData)
1268
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1269
+
1270
+ if (!channels->connected)
1271
+ return CHANNEL_RC_NOT_CONNECTED;
1272
+
1273
+ if (!pData)
1274
+ return CHANNEL_RC_NULL_DATA;
1275
+
1276
+ if (!dataLength)
1277
+ return CHANNEL_RC_ZERO_LENGTH;
1278
+
1279
+ if (pChannelOpenData->flags != 2)
1280
+ return CHANNEL_RC_NOT_OPEN;
1281
+
1282
+ pChannelOpenEvent = (CHANNEL_OPEN_EVENT*)malloc(sizeof(CHANNEL_OPEN_EVENT));
1283
+
1284
+ if (!pChannelOpenEvent)
1285
+ return CHANNEL_RC_NO_MEMORY;
1286
+
1287
+ pChannelOpenEvent->Data = pData;
1288
+ pChannelOpenEvent->DataLength = dataLength;
1289
+ pChannelOpenEvent->UserData = pUserData;
1290
+ pChannelOpenEvent->pChannelOpenData = pChannelOpenData;
1291
+ message.context = channels;
1292
+ message.id = 0;
1293
+ message.wParam = pChannelOpenEvent;
1294
+ message.lParam = NULL;
1295
+ message.Free = channel_queue_message_free;
1296
+
1297
+ if (!MessageQueue_Dispatch(channels->queue, &message))
1298
+ {
1299
+ free(pChannelOpenEvent);
1300
+ return CHANNEL_RC_NO_MEMORY;
1301
+ }
1302
+
1303
+ return CHANNEL_RC_OK;
1304
+ }
1305
+
1306
+ static UINT VCAPITYPE FreeRDP_VirtualChannelWrite(DWORD openHandle, LPVOID pData, ULONG dataLength,
1307
+ LPVOID pUserData)
1308
+ {
1309
+ wMessage message = { 0 };
1310
+ CHANNEL_OPEN_DATA* pChannelOpenData = NULL;
1311
+ CHANNEL_OPEN_EVENT* pChannelOpenEvent = NULL;
1312
+ rdpChannels* channels = NULL;
1313
+
1314
+ pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (void*)(UINT_PTR)openHandle);
1315
+
1316
+ if (!pChannelOpenData)
1317
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1318
+
1319
+ channels = pChannelOpenData->channels;
1320
+ if (!channels)
1321
+ return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1322
+
1323
+ if (!channels->connected)
1324
+ return CHANNEL_RC_NOT_CONNECTED;
1325
+
1326
+ if (!pData)
1327
+ return CHANNEL_RC_NULL_DATA;
1328
+
1329
+ if (!dataLength)
1330
+ return CHANNEL_RC_ZERO_LENGTH;
1331
+
1332
+ if (pChannelOpenData->flags != 2)
1333
+ return CHANNEL_RC_NOT_OPEN;
1334
+
1335
+ pChannelOpenEvent = (CHANNEL_OPEN_EVENT*)malloc(sizeof(CHANNEL_OPEN_EVENT));
1336
+
1337
+ if (!pChannelOpenEvent)
1338
+ return CHANNEL_RC_NO_MEMORY;
1339
+
1340
+ pChannelOpenEvent->Data = pData;
1341
+ pChannelOpenEvent->DataLength = dataLength;
1342
+ pChannelOpenEvent->UserData = pUserData;
1343
+ pChannelOpenEvent->pChannelOpenData = pChannelOpenData;
1344
+ message.context = channels;
1345
+ message.id = 0;
1346
+ message.wParam = pChannelOpenEvent;
1347
+ message.lParam = NULL;
1348
+ message.Free = channel_queue_message_free;
1349
+
1350
+ if (!MessageQueue_Dispatch(channels->queue, &message))
1351
+ {
1352
+ free(pChannelOpenEvent);
1353
+ return CHANNEL_RC_NO_MEMORY;
1354
+ }
1355
+
1356
+ return CHANNEL_RC_OK;
1357
+ }
1358
+
1359
+ static BOOL freerdp_channels_is_loaded(rdpChannels* channels, PVIRTUALCHANNELENTRY entry)
1360
+ {
1361
+ for (int i = 0; i < channels->clientDataCount; i++)
1362
+ {
1363
+ CHANNEL_CLIENT_DATA* pChannelClientData = &channels->clientDataList[i];
1364
+
1365
+ if (pChannelClientData->entry == entry)
1366
+ return TRUE;
1367
+ }
1368
+
1369
+ return FALSE;
1370
+ }
1371
+
1372
+ static BOOL freerdp_channels_is_loaded_ex(rdpChannels* channels, PVIRTUALCHANNELENTRYEX entryEx)
1373
+ {
1374
+ for (int i = 0; i < channels->clientDataCount; i++)
1375
+ {
1376
+ CHANNEL_CLIENT_DATA* pChannelClientData = &channels->clientDataList[i];
1377
+
1378
+ if (pChannelClientData->entryEx == entryEx)
1379
+ return TRUE;
1380
+ }
1381
+
1382
+ return FALSE;
1383
+ }
1384
+
1385
+ int freerdp_channels_client_load(rdpChannels* channels, rdpSettings* settings,
1386
+ PVIRTUALCHANNELENTRY entry, void* data)
1387
+ {
1388
+ int status = 0;
1389
+ CHANNEL_ENTRY_POINTS_FREERDP EntryPoints = { 0 };
1390
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
1391
+
1392
+ WINPR_ASSERT(channels);
1393
+ WINPR_ASSERT(channels->instance);
1394
+ WINPR_ASSERT(channels->instance->context);
1395
+ WINPR_ASSERT(entry);
1396
+
1397
+ if (channels->clientDataCount + 1 > CHANNEL_MAX_COUNT)
1398
+ {
1399
+ WLog_ERR(TAG, "error: too many channels");
1400
+ return 1;
1401
+ }
1402
+
1403
+ if (freerdp_channels_is_loaded(channels, entry))
1404
+ {
1405
+ WLog_WARN(TAG, "Skipping, channel already loaded");
1406
+ return 0;
1407
+ }
1408
+
1409
+ pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1410
+ pChannelClientData->entry = entry;
1411
+
1412
+ EntryPoints.cbSize = sizeof(EntryPoints);
1413
+ EntryPoints.protocolVersion = VIRTUAL_CHANNEL_VERSION_WIN2000;
1414
+ EntryPoints.pVirtualChannelInit = FreeRDP_VirtualChannelInit;
1415
+ EntryPoints.pVirtualChannelOpen = FreeRDP_VirtualChannelOpen;
1416
+ EntryPoints.pVirtualChannelClose = FreeRDP_VirtualChannelClose;
1417
+ EntryPoints.pVirtualChannelWrite = FreeRDP_VirtualChannelWrite;
1418
+ EntryPoints.MagicNumber = FREERDP_CHANNEL_MAGIC_NUMBER;
1419
+ EntryPoints.pExtendedData = data;
1420
+ EntryPoints.context = channels->instance->context;
1421
+ /* enable VirtualChannelInit */
1422
+ channels->can_call_init = TRUE;
1423
+ EnterCriticalSection(&channels->channelsLock);
1424
+ status = pChannelClientData->entry((PCHANNEL_ENTRY_POINTS)&EntryPoints);
1425
+ LeaveCriticalSection(&channels->channelsLock);
1426
+ /* disable MyVirtualChannelInit */
1427
+ channels->can_call_init = FALSE;
1428
+
1429
+ if (!status)
1430
+ {
1431
+ WLog_ERR(TAG, "error: channel export function call failed");
1432
+ return 1;
1433
+ }
1434
+
1435
+ return 0;
1436
+ }
1437
+
1438
+ int freerdp_channels_client_load_ex(rdpChannels* channels, rdpSettings* settings,
1439
+ PVIRTUALCHANNELENTRYEX entryEx, void* data)
1440
+ {
1441
+ int status = 0;
1442
+ void* pInitHandle = NULL;
1443
+ CHANNEL_ENTRY_POINTS_FREERDP_EX EntryPointsEx = { 0 };
1444
+ CHANNEL_INIT_DATA* pChannelInitData = NULL;
1445
+ CHANNEL_CLIENT_DATA* pChannelClientData = NULL;
1446
+
1447
+ WINPR_ASSERT(channels);
1448
+ WINPR_ASSERT(channels->instance);
1449
+ WINPR_ASSERT(channels->instance->context);
1450
+ WINPR_ASSERT(entryEx);
1451
+
1452
+ if (channels->clientDataCount + 1 > CHANNEL_MAX_COUNT)
1453
+ {
1454
+ WLog_ERR(TAG, "error: too many channels");
1455
+ return 1;
1456
+ }
1457
+
1458
+ if (freerdp_channels_is_loaded_ex(channels, entryEx))
1459
+ {
1460
+ WLog_WARN(TAG, "Skipping, channel already loaded");
1461
+ return 0;
1462
+ }
1463
+
1464
+ pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1465
+ pChannelClientData->entryEx = entryEx;
1466
+ pChannelInitData = &(channels->initDataList[channels->initDataCount++]);
1467
+ pInitHandle = pChannelInitData;
1468
+ pChannelInitData->channels = channels;
1469
+ EntryPointsEx.cbSize = sizeof(EntryPointsEx);
1470
+ EntryPointsEx.protocolVersion = VIRTUAL_CHANNEL_VERSION_WIN2000;
1471
+ EntryPointsEx.pVirtualChannelInitEx = FreeRDP_VirtualChannelInitEx;
1472
+ EntryPointsEx.pVirtualChannelOpenEx = FreeRDP_VirtualChannelOpenEx;
1473
+ EntryPointsEx.pVirtualChannelCloseEx = FreeRDP_VirtualChannelCloseEx;
1474
+ EntryPointsEx.pVirtualChannelWriteEx = FreeRDP_VirtualChannelWriteEx;
1475
+ EntryPointsEx.MagicNumber = FREERDP_CHANNEL_MAGIC_NUMBER;
1476
+ EntryPointsEx.pExtendedData = data;
1477
+ EntryPointsEx.context = channels->instance->context;
1478
+ /* enable VirtualChannelInit */
1479
+ channels->can_call_init = TRUE;
1480
+ EnterCriticalSection(&channels->channelsLock);
1481
+ status = pChannelClientData->entryEx((PCHANNEL_ENTRY_POINTS_EX)&EntryPointsEx, pInitHandle);
1482
+ LeaveCriticalSection(&channels->channelsLock);
1483
+ /* disable MyVirtualChannelInit */
1484
+ channels->can_call_init = FALSE;
1485
+
1486
+ if (!status)
1487
+ {
1488
+ WLog_ERR(TAG, "error: channel export function call failed");
1489
+ return 1;
1490
+ }
1491
+
1492
+ return 0;
1493
+ }
1494
+
1495
+ /**
1496
+ * this is called when processing the command line parameters
1497
+ * called only from main thread
1498
+ */
1499
+ int freerdp_channels_load_plugin(rdpChannels* channels, rdpSettings* settings, const char* name,
1500
+ void* data)
1501
+ {
1502
+ PVIRTUALCHANNELENTRY entry =
1503
+ freerdp_load_channel_addin_entry(name, NULL, NULL, FREERDP_ADDIN_CHANNEL_STATIC);
1504
+
1505
+ if (!entry)
1506
+ return 1;
1507
+
1508
+ return freerdp_channels_client_load(channels, settings, entry, data);
1509
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/core/listener.c ADDED
@@ -0,0 +1,552 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RDP Server Listener
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+ #include <fcntl.h>
26
+ #include <errno.h>
27
+
28
+ #include <winpr/crt.h>
29
+ #include <winpr/windows.h>
30
+ #include <freerdp/log.h>
31
+
32
+ #ifndef _WIN32
33
+ #include <netdb.h>
34
+ #include <unistd.h>
35
+ #include <sys/un.h>
36
+ #include <sys/ioctl.h>
37
+ #include <sys/socket.h>
38
+ #include <arpa/inet.h>
39
+ #include <netinet/in.h>
40
+ #include <net/if.h>
41
+ #endif
42
+
43
+ #if defined(HAVE_AF_VSOCK_H)
44
+ #include <ctype.h>
45
+ #include <linux/vm_sockets.h>
46
+ #endif
47
+
48
+ #include <winpr/handle.h>
49
+
50
+ #include "listener.h"
51
+ #include "utils.h"
52
+
53
+ #define TAG FREERDP_TAG("core.listener")
54
+
55
+ static BOOL freerdp_listener_open_from_vsock(freerdp_listener* instance, const char* bind_address,
56
+ UINT16 port)
57
+ {
58
+ #if defined(HAVE_AF_VSOCK_H)
59
+ rdpListener* listener = (rdpListener*)instance->listener;
60
+ const int sockfd = socket(AF_VSOCK, SOCK_STREAM, 0);
61
+ if (sockfd == -1)
62
+ {
63
+ char ebuffer[256] = { 0 };
64
+ WLog_ERR(TAG, "Error creating socket: %s", winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
65
+ return FALSE;
66
+ }
67
+ const int flags = fcntl(sockfd, F_GETFL, 0);
68
+ if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)
69
+ {
70
+ char ebuffer[256] = { 0 };
71
+ WLog_ERR(TAG, "Error making socket nonblocking: %s",
72
+ winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
73
+ close(sockfd);
74
+ return FALSE;
75
+ }
76
+ struct sockaddr_vm addr = { 0 };
77
+
78
+ addr.svm_family = AF_VSOCK;
79
+ addr.svm_port = port;
80
+
81
+ errno = 0;
82
+ char* ptr = NULL;
83
+ unsigned long val = strtoul(bind_address, &ptr, 10);
84
+ if (errno || (val > UINT32_MAX))
85
+ {
86
+ char ebuffer[256] = { 0 };
87
+ WLog_ERR(TAG, "could not extract port from '%s', value=%ul, error=%s", bind_address, val,
88
+ winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
89
+ close(sockfd);
90
+ return FALSE;
91
+ }
92
+ addr.svm_cid = WINPR_ASSERTING_INT_CAST(unsigned int, val);
93
+ if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_vm)) == -1)
94
+ {
95
+ char ebuffer[256] = { 0 };
96
+ WLog_ERR(TAG, "Error binding vsock at cid %d port %d: %s", addr.svm_cid, port,
97
+ winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
98
+ close(sockfd);
99
+ return FALSE;
100
+ }
101
+
102
+ if (listen(sockfd, 10) == -1)
103
+ {
104
+ char ebuffer[256] = { 0 };
105
+ WLog_ERR(TAG, "Error listening to socket at cid %d port %d: %s", addr.svm_cid, port,
106
+ winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
107
+ close(sockfd);
108
+ return FALSE;
109
+ }
110
+ listener->sockfds[listener->num_sockfds] = sockfd;
111
+ listener->events[listener->num_sockfds] = WSACreateEvent();
112
+
113
+ if (!listener->events[listener->num_sockfds])
114
+ {
115
+ listener->num_sockfds = 0;
116
+ }
117
+
118
+ WSAEventSelect((SOCKET)sockfd, listener->events[listener->num_sockfds],
119
+ FD_READ | FD_ACCEPT | FD_CLOSE);
120
+ listener->num_sockfds++;
121
+
122
+ WLog_INFO(TAG, "Listening on %s:%d", bind_address, port);
123
+ return TRUE;
124
+ #else
125
+ WLog_ERR(TAG, "compiled without AF_VSOCK, '%s' not supported", bind_address);
126
+ return FALSE;
127
+ #endif
128
+ }
129
+
130
+ static BOOL freerdp_listener_open(freerdp_listener* instance, const char* bind_address, UINT16 port)
131
+ {
132
+ int ai_flags = 0;
133
+ int status = 0;
134
+ int sockfd = 0;
135
+ char addr[64];
136
+ void* sin_addr = NULL;
137
+ int option_value = 0;
138
+ struct addrinfo* res = NULL;
139
+ rdpListener* listener = (rdpListener*)instance->listener;
140
+ #ifdef _WIN32
141
+ u_long arg;
142
+ #endif
143
+
144
+ if (!bind_address)
145
+ ai_flags = AI_PASSIVE;
146
+
147
+ if (utils_is_vsock(bind_address))
148
+ {
149
+ bind_address = utils_is_vsock(bind_address);
150
+ return freerdp_listener_open_from_vsock(instance, bind_address, port);
151
+ }
152
+
153
+ res = freerdp_tcp_resolve_host(bind_address, port, ai_flags);
154
+
155
+ if (!res)
156
+ return FALSE;
157
+
158
+ for (struct addrinfo* ai = res; ai && (listener->num_sockfds < 5); ai = ai->ai_next)
159
+ {
160
+ if ((ai->ai_family != AF_INET) && (ai->ai_family != AF_INET6))
161
+ continue;
162
+
163
+ if (listener->num_sockfds == MAX_LISTENER_HANDLES)
164
+ {
165
+ WLog_ERR(TAG, "too many listening sockets");
166
+ continue;
167
+ }
168
+
169
+ sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
170
+
171
+ if (sockfd == -1)
172
+ {
173
+ WLog_ERR(TAG, "socket");
174
+ continue;
175
+ }
176
+
177
+ option_value = 1;
178
+
179
+ if (ai->ai_family == AF_INET)
180
+ sin_addr = &(((struct sockaddr_in*)ai->ai_addr)->sin_addr);
181
+ else
182
+ {
183
+ sin_addr = &(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr);
184
+ if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&option_value,
185
+ sizeof(option_value)) == -1)
186
+ WLog_ERR(TAG, "setsockopt");
187
+ }
188
+
189
+ inet_ntop(ai->ai_family, sin_addr, addr, sizeof(addr));
190
+
191
+ if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&option_value,
192
+ sizeof(option_value)) == -1)
193
+ WLog_ERR(TAG, "setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR)");
194
+
195
+ #ifndef _WIN32
196
+ if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0)
197
+ WLog_ERR(TAG, "fcntl(sockfd, F_SETFL, O_NONBLOCK)");
198
+ #else
199
+ arg = 1;
200
+ ioctlsocket(sockfd, FIONBIO, &arg);
201
+ #endif
202
+ status = _bind((SOCKET)sockfd, ai->ai_addr, WINPR_ASSERTING_INT_CAST(int, ai->ai_addrlen));
203
+
204
+ if (status != 0)
205
+ {
206
+ closesocket((SOCKET)sockfd);
207
+ continue;
208
+ }
209
+
210
+ status = _listen((SOCKET)sockfd, 10);
211
+
212
+ if (status != 0)
213
+ {
214
+ WLog_ERR(TAG, "listen");
215
+ closesocket((SOCKET)sockfd);
216
+ continue;
217
+ }
218
+
219
+ /* FIXME: these file descriptors do not work on Windows */
220
+ listener->sockfds[listener->num_sockfds] = sockfd;
221
+ listener->events[listener->num_sockfds] = WSACreateEvent();
222
+
223
+ if (!listener->events[listener->num_sockfds])
224
+ {
225
+ listener->num_sockfds = 0;
226
+ break;
227
+ }
228
+
229
+ WSAEventSelect((SOCKET)sockfd, listener->events[listener->num_sockfds],
230
+ FD_READ | FD_ACCEPT | FD_CLOSE);
231
+ listener->num_sockfds++;
232
+ WLog_INFO(TAG, "Listening on [%s]:%" PRIu16, addr, port);
233
+ }
234
+
235
+ freeaddrinfo(res);
236
+ return (listener->num_sockfds > 0 ? TRUE : FALSE);
237
+ }
238
+
239
+ static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char* path)
240
+ {
241
+ #ifndef _WIN32
242
+ int status = 0;
243
+ int sockfd = 0;
244
+ struct sockaddr_un addr = { 0 };
245
+ rdpListener* listener = (rdpListener*)instance->listener;
246
+ HANDLE hevent = NULL;
247
+
248
+ if (listener->num_sockfds == MAX_LISTENER_HANDLES)
249
+ {
250
+ WLog_ERR(TAG, "too many listening sockets");
251
+ return FALSE;
252
+ }
253
+
254
+ sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
255
+
256
+ if (sockfd == -1)
257
+ {
258
+ WLog_ERR(TAG, "socket");
259
+ return FALSE;
260
+ }
261
+
262
+ int rc = fcntl(sockfd, F_SETFL, O_NONBLOCK);
263
+ if (rc != 0)
264
+ {
265
+ WLog_ERR(TAG, "fcntl(sockfd, F_SETFL, O_NONBLOCK)");
266
+ closesocket((SOCKET)sockfd);
267
+ return FALSE;
268
+ }
269
+
270
+ addr.sun_family = AF_UNIX;
271
+ strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
272
+ unlink(path);
273
+ status = _bind((SOCKET)sockfd, (struct sockaddr*)&addr, sizeof(addr));
274
+
275
+ if (status != 0)
276
+ {
277
+ WLog_ERR(TAG, "bind");
278
+ closesocket((SOCKET)sockfd);
279
+ return FALSE;
280
+ }
281
+
282
+ status = _listen((SOCKET)sockfd, 10);
283
+
284
+ if (status != 0)
285
+ {
286
+ WLog_ERR(TAG, "listen");
287
+ closesocket((SOCKET)sockfd);
288
+ return FALSE;
289
+ }
290
+
291
+ hevent = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd, WINPR_FD_READ);
292
+
293
+ if (!hevent)
294
+ {
295
+ WLog_ERR(TAG, "failed to create sockfd event");
296
+ closesocket((SOCKET)sockfd);
297
+ return FALSE;
298
+ }
299
+
300
+ listener->sockfds[listener->num_sockfds] = sockfd;
301
+ listener->events[listener->num_sockfds] = hevent;
302
+ listener->num_sockfds++;
303
+ WLog_INFO(TAG, "Listening on socket %s.", addr.sun_path);
304
+ return TRUE;
305
+ #else
306
+ return TRUE;
307
+ #endif
308
+ }
309
+
310
+ static BOOL freerdp_listener_open_from_socket(freerdp_listener* instance, int fd)
311
+ {
312
+ #ifndef _WIN32
313
+ rdpListener* listener = (rdpListener*)instance->listener;
314
+
315
+ if (listener->num_sockfds == MAX_LISTENER_HANDLES)
316
+ {
317
+ WLog_ERR(TAG, "too many listening sockets");
318
+ return FALSE;
319
+ }
320
+
321
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
322
+ return FALSE;
323
+
324
+ listener->sockfds[listener->num_sockfds] = fd;
325
+ listener->events[listener->num_sockfds] = WSACreateEvent();
326
+
327
+ if (!listener->events[listener->num_sockfds])
328
+ return FALSE;
329
+
330
+ WSAEventSelect((SOCKET)fd, listener->events[listener->num_sockfds],
331
+ FD_READ | FD_ACCEPT | FD_CLOSE);
332
+
333
+ listener->num_sockfds++;
334
+ WLog_INFO(TAG, "Listening on socket %d.", fd);
335
+ return TRUE;
336
+ #else
337
+ return FALSE;
338
+ #endif
339
+ }
340
+
341
+ static void freerdp_listener_close(freerdp_listener* instance)
342
+ {
343
+ rdpListener* listener = (rdpListener*)instance->listener;
344
+
345
+ for (int i = 0; i < listener->num_sockfds; i++)
346
+ {
347
+ closesocket((SOCKET)listener->sockfds[i]);
348
+ (void)CloseHandle(listener->events[i]);
349
+ }
350
+
351
+ listener->num_sockfds = 0;
352
+ }
353
+
354
+ #if defined(WITH_FREERDP_DEPRECATED)
355
+ static BOOL freerdp_listener_get_fds(freerdp_listener* instance, void** rfds, int* rcount)
356
+ {
357
+ rdpListener* listener = (rdpListener*)instance->listener;
358
+
359
+ if (listener->num_sockfds < 1)
360
+ return FALSE;
361
+
362
+ for (int index = 0; index < listener->num_sockfds; index++)
363
+ {
364
+ rfds[*rcount] = (void*)(long)(listener->sockfds[index]);
365
+ (*rcount)++;
366
+ }
367
+
368
+ return TRUE;
369
+ }
370
+ #endif
371
+
372
+ static DWORD freerdp_listener_get_event_handles(freerdp_listener* instance, HANDLE* events,
373
+ DWORD nCount)
374
+ {
375
+ rdpListener* listener = (rdpListener*)instance->listener;
376
+
377
+ if (listener->num_sockfds < 1)
378
+ return 0;
379
+
380
+ if (listener->num_sockfds > (INT64)nCount)
381
+ return 0;
382
+
383
+ for (int index = 0; index < listener->num_sockfds; index++)
384
+ {
385
+ events[index] = listener->events[index];
386
+ }
387
+
388
+ return WINPR_ASSERTING_INT_CAST(uint32_t, listener->num_sockfds);
389
+ }
390
+
391
+ BOOL freerdp_peer_set_local_and_hostname(freerdp_peer* client,
392
+ const struct sockaddr_storage* peer_addr)
393
+ {
394
+ const void* sin_addr = NULL;
395
+ const BYTE localhost6_bytes[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
396
+
397
+ WINPR_ASSERT(client);
398
+ WINPR_ASSERT(peer_addr);
399
+
400
+ if (peer_addr->ss_family == AF_INET)
401
+ {
402
+ const UINT32* usin_addr = sin_addr = &(((const struct sockaddr_in*)peer_addr)->sin_addr);
403
+
404
+ if ((*usin_addr) == 0x0100007f)
405
+ client->local = TRUE;
406
+ }
407
+ else if (peer_addr->ss_family == AF_INET6)
408
+ {
409
+ const struct sockaddr_in6* usin_addr = sin_addr =
410
+ &(((const struct sockaddr_in6*)peer_addr)->sin6_addr);
411
+
412
+ if (memcmp(usin_addr, localhost6_bytes, 16) == 0)
413
+ client->local = TRUE;
414
+ }
415
+
416
+ #ifndef _WIN32
417
+ #if defined(HAVE_AF_VSOCK_H)
418
+ else if (peer_addr->ss_family == AF_UNIX || peer_addr->ss_family == AF_VSOCK)
419
+ #else
420
+ else if (peer_addr->ss_family == AF_UNIX)
421
+ #endif
422
+ client->local = TRUE;
423
+ #endif
424
+
425
+ if (client->local)
426
+ WLog_INFO(TAG, "Accepting client from localhost");
427
+
428
+ if (sin_addr)
429
+ inet_ntop(peer_addr->ss_family, sin_addr, client->hostname, sizeof(client->hostname));
430
+
431
+ return TRUE;
432
+ }
433
+
434
+ static BOOL freerdp_check_and_create_client(freerdp_listener* instance, int peer_sockfd,
435
+ const struct sockaddr_storage* peer_addr)
436
+ {
437
+ WINPR_ASSERT(instance);
438
+ WINPR_ASSERT(peer_sockfd >= 0);
439
+ WINPR_ASSERT(peer_addr);
440
+
441
+ const BOOL check = IFCALLRESULT(TRUE, instance->CheckPeerAcceptRestrictions, instance);
442
+ if (!check)
443
+ {
444
+ closesocket((SOCKET)peer_sockfd);
445
+ return TRUE;
446
+ }
447
+
448
+ freerdp_peer* client = freerdp_peer_new(peer_sockfd);
449
+ if (!client)
450
+ {
451
+ closesocket((SOCKET)peer_sockfd);
452
+ return FALSE;
453
+ }
454
+
455
+ if (!freerdp_peer_set_local_and_hostname(client, peer_addr))
456
+ {
457
+ freerdp_peer_free(client);
458
+ return FALSE;
459
+ }
460
+
461
+ const BOOL peer_accepted = IFCALLRESULT(FALSE, instance->PeerAccepted, instance, client);
462
+ if (!peer_accepted)
463
+ {
464
+ WLog_ERR(TAG, "PeerAccepted callback failed");
465
+ freerdp_peer_free(client);
466
+ }
467
+
468
+ return TRUE;
469
+ }
470
+
471
+ static BOOL freerdp_listener_check_fds(freerdp_listener* instance)
472
+ {
473
+ rdpListener* listener = (rdpListener*)instance->listener;
474
+
475
+ if (listener->num_sockfds < 1)
476
+ return FALSE;
477
+
478
+ for (int i = 0; i < listener->num_sockfds; i++)
479
+ {
480
+ struct sockaddr_storage peer_addr = { 0 };
481
+
482
+ (void)WSAResetEvent(listener->events[i]);
483
+ int peer_addr_size = sizeof(peer_addr);
484
+ SOCKET peer_sockfd =
485
+ _accept((SOCKET)listener->sockfds[i], (struct sockaddr*)&peer_addr, &peer_addr_size);
486
+
487
+ if (peer_sockfd == (SOCKET)-1)
488
+ {
489
+ char buffer[128] = { 0 };
490
+ #ifdef _WIN32
491
+ int wsa_error = WSAGetLastError();
492
+
493
+ /* No data available */
494
+ if (wsa_error == WSAEWOULDBLOCK)
495
+ continue;
496
+
497
+ #else
498
+
499
+ if (errno == EAGAIN || errno == EWOULDBLOCK)
500
+ continue;
501
+
502
+ #endif
503
+ WLog_WARN(TAG, "accept failed with %s", winpr_strerror(errno, buffer, sizeof(buffer)));
504
+ return FALSE;
505
+ }
506
+
507
+ if (!freerdp_check_and_create_client(instance, (int)peer_sockfd, &peer_addr))
508
+ return FALSE;
509
+ }
510
+
511
+ return TRUE;
512
+ }
513
+
514
+ freerdp_listener* freerdp_listener_new(void)
515
+ {
516
+ freerdp_listener* instance = NULL;
517
+ rdpListener* listener = NULL;
518
+ instance = (freerdp_listener*)calloc(1, sizeof(freerdp_listener));
519
+
520
+ if (!instance)
521
+ return NULL;
522
+
523
+ instance->Open = freerdp_listener_open;
524
+ instance->OpenLocal = freerdp_listener_open_local;
525
+ instance->OpenFromSocket = freerdp_listener_open_from_socket;
526
+ #if defined(WITH_FREERDP_DEPRECATED)
527
+ instance->GetFileDescriptor = freerdp_listener_get_fds;
528
+ #endif
529
+ instance->GetEventHandles = freerdp_listener_get_event_handles;
530
+ instance->CheckFileDescriptor = freerdp_listener_check_fds;
531
+ instance->Close = freerdp_listener_close;
532
+ listener = (rdpListener*)calloc(1, sizeof(rdpListener));
533
+
534
+ if (!listener)
535
+ {
536
+ free(instance);
537
+ return NULL;
538
+ }
539
+
540
+ listener->instance = instance;
541
+ instance->listener = (void*)listener;
542
+ return instance;
543
+ }
544
+
545
+ void freerdp_listener_free(freerdp_listener* instance)
546
+ {
547
+ if (instance)
548
+ {
549
+ free(instance->listener);
550
+ free(instance);
551
+ }
552
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/core/proxy.h ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * HTTP proxy support
4
+ *
5
+ * Copyright 2014 Christian Plattner <ccpp@gmx.at>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CORE_HTTP_PROXY_H
21
+ #define FREERDP_LIB_CORE_HTTP_PROXY_H
22
+
23
+ #include "freerdp/settings.h"
24
+ #include <openssl/bio.h>
25
+
26
+ BOOL proxy_prepare(rdpSettings* settings, const char** lpPeerHostname, UINT16* lpPeerPort,
27
+ const char** lpProxyUsername, const char** lpProxyPassword);
28
+
29
+ BOOL proxy_connect(rdpContext* context, BIO* bio, const char* proxyUsername,
30
+ const char* proxyPassword, const char* hostname, UINT16 port);
31
+
32
+ #endif /* FREERDP_LIB_CORE_HTTP_PROXY_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/graphics.c ADDED
@@ -0,0 +1,468 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Graphical Objects
4
+ *
5
+ * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <winpr/crt.h>
25
+
26
+ #include <freerdp/log.h>
27
+ #include <freerdp/freerdp.h>
28
+ #include <freerdp/gdi/dc.h>
29
+ #include <freerdp/gdi/shape.h>
30
+ #include <freerdp/gdi/region.h>
31
+ #include <freerdp/gdi/bitmap.h>
32
+
33
+ #include "clipping.h"
34
+ #include "drawing.h"
35
+ #include "brush.h"
36
+ #include "graphics.h"
37
+
38
+ #define TAG FREERDP_TAG("gdi")
39
+ /* Bitmap Class */
40
+
41
+ HGDI_BITMAP gdi_create_bitmap(rdpGdi* gdi, UINT32 nWidth, UINT32 nHeight, UINT32 SrcFormat,
42
+ BYTE* data)
43
+ {
44
+ UINT32 nSrcStep = 0;
45
+ UINT32 nDstStep = 0;
46
+ BYTE* pSrcData = NULL;
47
+ BYTE* pDstData = NULL;
48
+ HGDI_BITMAP bitmap = NULL;
49
+
50
+ if (!gdi)
51
+ return NULL;
52
+
53
+ nDstStep = nWidth * FreeRDPGetBytesPerPixel(gdi->dstFormat);
54
+ pDstData = winpr_aligned_malloc(1ull * nHeight * nDstStep, 16);
55
+
56
+ if (!pDstData)
57
+ return NULL;
58
+
59
+ pSrcData = data;
60
+ nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
61
+
62
+ if (!freerdp_image_copy_no_overlap(pDstData, gdi->dstFormat, nDstStep, 0, 0, nWidth, nHeight,
63
+ pSrcData, SrcFormat, nSrcStep, 0, 0, &gdi->palette,
64
+ FREERDP_FLIP_NONE))
65
+ {
66
+ winpr_aligned_free(pDstData);
67
+ return NULL;
68
+ }
69
+
70
+ bitmap = gdi_CreateBitmap(nWidth, nHeight, gdi->dstFormat, pDstData);
71
+ return bitmap;
72
+ }
73
+
74
+ static BOOL gdi_Bitmap_New(rdpContext* context, rdpBitmap* bitmap)
75
+ {
76
+ gdiBitmap* gdi_bitmap = NULL;
77
+ rdpGdi* gdi = context->gdi;
78
+ gdi_bitmap = (gdiBitmap*)bitmap;
79
+ gdi_bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc);
80
+
81
+ if (!gdi_bitmap->hdc)
82
+ return FALSE;
83
+
84
+ if (!bitmap->data)
85
+ gdi_bitmap->bitmap = gdi_CreateCompatibleBitmap(gdi->hdc, bitmap->width, bitmap->height);
86
+ else
87
+ {
88
+ UINT32 format = bitmap->format;
89
+ gdi_bitmap->bitmap =
90
+ gdi_create_bitmap(gdi, bitmap->width, bitmap->height, format, bitmap->data);
91
+ }
92
+
93
+ if (!gdi_bitmap->bitmap)
94
+ {
95
+ gdi_DeleteDC(gdi_bitmap->hdc);
96
+ gdi_bitmap->hdc = NULL;
97
+ return FALSE;
98
+ }
99
+
100
+ gdi_bitmap->hdc->format = gdi_bitmap->bitmap->format;
101
+ gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->bitmap);
102
+ gdi_bitmap->org_bitmap = NULL;
103
+ return TRUE;
104
+ }
105
+
106
+ static void gdi_Bitmap_Free(rdpContext* context, rdpBitmap* bitmap)
107
+ {
108
+ gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
109
+
110
+ if (gdi_bitmap)
111
+ {
112
+ if (gdi_bitmap->hdc)
113
+ gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->org_bitmap);
114
+
115
+ gdi_DeleteObject((HGDIOBJECT)gdi_bitmap->bitmap);
116
+ gdi_DeleteDC(gdi_bitmap->hdc);
117
+ winpr_aligned_free(bitmap->data);
118
+ }
119
+
120
+ free(bitmap);
121
+ }
122
+
123
+ static BOOL gdi_Bitmap_Paint(rdpContext* context, rdpBitmap* bitmap)
124
+ {
125
+ gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
126
+ UINT32 width = bitmap->right - bitmap->left + 1;
127
+ UINT32 height = bitmap->bottom - bitmap->top + 1;
128
+ return gdi_BitBlt(context->gdi->primary->hdc, WINPR_ASSERTING_INT_CAST(int, bitmap->left),
129
+ WINPR_ASSERTING_INT_CAST(int, bitmap->top),
130
+ WINPR_ASSERTING_INT_CAST(int, width), WINPR_ASSERTING_INT_CAST(int, height),
131
+ gdi_bitmap->hdc, 0, 0, GDI_SRCCOPY, &context->gdi->palette);
132
+ }
133
+
134
+ static BOOL gdi_Bitmap_Decompress(rdpContext* context, rdpBitmap* bitmap, const BYTE* pSrcData,
135
+ UINT32 DstWidth, UINT32 DstHeight, UINT32 bpp, UINT32 length,
136
+ BOOL compressed, UINT32 codecId)
137
+ {
138
+ UINT32 SrcSize = length;
139
+ rdpGdi* gdi = context->gdi;
140
+ UINT32 size = DstWidth * DstHeight;
141
+ bitmap->compressed = FALSE;
142
+ bitmap->format = gdi->dstFormat;
143
+
144
+ if ((FreeRDPGetBytesPerPixel(bitmap->format) == 0) || (DstWidth == 0) || (DstHeight == 0) ||
145
+ (DstWidth > UINT32_MAX / DstHeight) ||
146
+ (size > (UINT32_MAX / FreeRDPGetBytesPerPixel(bitmap->format))))
147
+ {
148
+ WLog_ERR(TAG, "invalid input data");
149
+ return FALSE;
150
+ }
151
+
152
+ size *= FreeRDPGetBytesPerPixel(bitmap->format);
153
+ bitmap->length = size;
154
+ bitmap->data = (BYTE*)winpr_aligned_malloc(bitmap->length, 16);
155
+
156
+ if (!bitmap->data)
157
+ return FALSE;
158
+
159
+ if (compressed)
160
+ {
161
+ if ((codecId == RDP_CODEC_ID_REMOTEFX) || (codecId == RDP_CODEC_ID_IMAGE_REMOTEFX))
162
+ {
163
+ REGION16 invalidRegion = { 0 };
164
+ region16_init(&invalidRegion);
165
+
166
+ const BOOL rc =
167
+ rfx_process_message(context->codecs->rfx, pSrcData, SrcSize, bitmap->left,
168
+ bitmap->top, bitmap->data, bitmap->format, gdi->stride,
169
+ WINPR_ASSERTING_INT_CAST(UINT32, gdi->height), &invalidRegion);
170
+ region16_uninit(&invalidRegion);
171
+
172
+ if (!rc)
173
+ {
174
+ WLog_ERR(TAG, "rfx_process_message failed");
175
+ return FALSE;
176
+ }
177
+ }
178
+ else if (codecId == RDP_CODEC_ID_NSCODEC)
179
+ {
180
+ const int status = nsc_process_message(
181
+ context->codecs->nsc, 32, DstWidth, DstHeight, pSrcData, SrcSize, bitmap->data,
182
+ bitmap->format, 0, 0, 0, DstWidth, DstHeight, FREERDP_FLIP_VERTICAL);
183
+
184
+ if (status < 1)
185
+ {
186
+ WLog_ERR(TAG, "nsc_process_message failed");
187
+ return FALSE;
188
+ }
189
+
190
+ return freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
191
+ DstHeight, pSrcData, PIXEL_FORMAT_XRGB32, 0, 0, 0,
192
+ &gdi->palette, FREERDP_FLIP_VERTICAL);
193
+ }
194
+ else if (bpp < 32)
195
+ {
196
+ if (!interleaved_decompress(context->codecs->interleaved, pSrcData, SrcSize, DstWidth,
197
+ DstHeight, bpp, bitmap->data, bitmap->format, 0, 0, 0,
198
+ DstWidth, DstHeight, &gdi->palette))
199
+ {
200
+ WLog_ERR(TAG, "interleaved_decompress failed");
201
+ return FALSE;
202
+ }
203
+ }
204
+ else
205
+ {
206
+ const BOOL fidelity =
207
+ freerdp_settings_get_bool(context->settings, FreeRDP_DrawAllowDynamicColorFidelity);
208
+ freerdp_planar_switch_bgr(context->codecs->planar, fidelity);
209
+ if (!planar_decompress(context->codecs->planar, pSrcData, SrcSize, DstWidth, DstHeight,
210
+ bitmap->data, bitmap->format, 0, 0, 0, DstWidth, DstHeight,
211
+ TRUE))
212
+ {
213
+ WLog_ERR(TAG, "planar_decompress failed");
214
+ return FALSE;
215
+ }
216
+ }
217
+ }
218
+ else
219
+ {
220
+ const UINT32 SrcFormat = gdi_get_pixel_format(bpp);
221
+ const size_t sbpp = FreeRDPGetBytesPerPixel(SrcFormat);
222
+ const size_t dbpp = FreeRDPGetBytesPerPixel(bitmap->format);
223
+
224
+ if ((sbpp == 0) || (dbpp == 0))
225
+ return FALSE;
226
+ else
227
+ {
228
+ const size_t dstSize = SrcSize * dbpp / sbpp;
229
+
230
+ if (dstSize < bitmap->length)
231
+ {
232
+ WLog_ERR(TAG, "dstSize %" PRIuz " < bitmap->length %" PRIu32, dstSize,
233
+ bitmap->length);
234
+ return FALSE;
235
+ }
236
+ }
237
+
238
+ if (!freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
239
+ DstHeight, pSrcData, SrcFormat, 0, 0, 0, &gdi->palette,
240
+ FREERDP_FLIP_VERTICAL))
241
+ {
242
+ WLog_ERR(TAG, "freerdp_image_copy failed");
243
+ return FALSE;
244
+ }
245
+ }
246
+
247
+ return TRUE;
248
+ }
249
+
250
+ static BOOL gdi_Bitmap_SetSurface(rdpContext* context, rdpBitmap* bitmap, BOOL primary)
251
+ {
252
+ rdpGdi* gdi = NULL;
253
+
254
+ if (!context)
255
+ return FALSE;
256
+
257
+ gdi = context->gdi;
258
+
259
+ if (!gdi)
260
+ return FALSE;
261
+
262
+ if (primary)
263
+ gdi->drawing = gdi->primary;
264
+ else
265
+ gdi->drawing = (gdiBitmap*)bitmap;
266
+
267
+ return TRUE;
268
+ }
269
+
270
+ /* Glyph Class */
271
+ static BOOL gdi_Glyph_New(rdpContext* context, rdpGlyph* glyph)
272
+ {
273
+ BYTE* data = NULL;
274
+ gdiGlyph* gdi_glyph = NULL;
275
+
276
+ if (!context || !glyph)
277
+ return FALSE;
278
+
279
+ gdi_glyph = (gdiGlyph*)glyph;
280
+ gdi_glyph->hdc = gdi_GetDC();
281
+
282
+ if (!gdi_glyph->hdc)
283
+ return FALSE;
284
+
285
+ gdi_glyph->hdc->format = PIXEL_FORMAT_MONO;
286
+ data = freerdp_glyph_convert(glyph->cx, glyph->cy, glyph->aj);
287
+
288
+ if (!data)
289
+ {
290
+ gdi_DeleteDC(gdi_glyph->hdc);
291
+ return FALSE;
292
+ }
293
+
294
+ gdi_glyph->bitmap = gdi_CreateBitmap(glyph->cx, glyph->cy, PIXEL_FORMAT_MONO, data);
295
+
296
+ if (!gdi_glyph->bitmap)
297
+ {
298
+ gdi_DeleteDC(gdi_glyph->hdc);
299
+ winpr_aligned_free(data);
300
+ return FALSE;
301
+ }
302
+
303
+ gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->bitmap);
304
+ gdi_glyph->org_bitmap = NULL;
305
+ return TRUE;
306
+ }
307
+
308
+ static void gdi_Glyph_Free(rdpContext* context, rdpGlyph* glyph)
309
+ {
310
+ gdiGlyph* gdi_glyph = NULL;
311
+ gdi_glyph = (gdiGlyph*)glyph;
312
+
313
+ if (gdi_glyph)
314
+ {
315
+ gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->org_bitmap);
316
+ gdi_DeleteObject((HGDIOBJECT)gdi_glyph->bitmap);
317
+ gdi_DeleteDC(gdi_glyph->hdc);
318
+ free(glyph->aj);
319
+ free(glyph);
320
+ }
321
+ }
322
+
323
+ static BOOL gdi_Glyph_Draw(rdpContext* context, const rdpGlyph* glyph, INT32 x, INT32 y, INT32 w,
324
+ INT32 h, INT32 sx, INT32 sy, BOOL fOpRedundant)
325
+ {
326
+ const gdiGlyph* gdi_glyph = NULL;
327
+ rdpGdi* gdi = NULL;
328
+ HGDI_BRUSH brush = NULL;
329
+ BOOL rc = FALSE;
330
+
331
+ if (!context || !glyph)
332
+ return FALSE;
333
+
334
+ gdi = context->gdi;
335
+ gdi_glyph = (const gdiGlyph*)glyph;
336
+
337
+ if (!fOpRedundant)
338
+ {
339
+ GDI_RECT rect = { 0 };
340
+
341
+ if (x > 0)
342
+ rect.left = x;
343
+
344
+ if (y > 0)
345
+ rect.top = y;
346
+
347
+ if (x + w > 0)
348
+ rect.right = x + w - 1;
349
+
350
+ if (y + h > 0)
351
+ rect.bottom = y + h - 1;
352
+
353
+ if ((rect.left < rect.right) && (rect.top < rect.bottom))
354
+ {
355
+ brush = gdi_CreateSolidBrush(gdi->drawing->hdc->bkColor);
356
+
357
+ if (!brush)
358
+ return FALSE;
359
+
360
+ gdi_FillRect(gdi->drawing->hdc, &rect, brush);
361
+ gdi_DeleteObject((HGDIOBJECT)brush);
362
+ }
363
+ }
364
+
365
+ brush = gdi_CreateSolidBrush(gdi->drawing->hdc->textColor);
366
+
367
+ if (!brush)
368
+ return FALSE;
369
+
370
+ gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)brush);
371
+ rc = gdi_BitBlt(gdi->drawing->hdc, x, y, w, h, gdi_glyph->hdc, sx, sy, GDI_GLYPH_ORDER,
372
+ &context->gdi->palette);
373
+ gdi_DeleteObject((HGDIOBJECT)brush);
374
+ return rc;
375
+ }
376
+
377
+ static BOOL gdi_Glyph_BeginDraw(rdpContext* context, INT32 x, INT32 y, INT32 width, INT32 height,
378
+ UINT32 bgcolor, UINT32 fgcolor, BOOL fOpRedundant)
379
+ {
380
+ rdpGdi* gdi = NULL;
381
+
382
+ if (!context || !context->gdi)
383
+ return FALSE;
384
+
385
+ gdi = context->gdi;
386
+
387
+ if (!gdi->drawing || !gdi->drawing->hdc)
388
+ return FALSE;
389
+
390
+ if (!fOpRedundant)
391
+ {
392
+ if (!gdi_decode_color(gdi, bgcolor, &bgcolor, NULL))
393
+ return FALSE;
394
+
395
+ if (!gdi_decode_color(gdi, fgcolor, &fgcolor, NULL))
396
+ return FALSE;
397
+
398
+ gdi_SetClipRgn(gdi->drawing->hdc, x, y, width, height);
399
+ gdi_SetTextColor(gdi->drawing->hdc, bgcolor);
400
+ gdi_SetBkColor(gdi->drawing->hdc, fgcolor);
401
+
402
+ if (1)
403
+ {
404
+ GDI_RECT rect = { 0 };
405
+ HGDI_BRUSH brush = gdi_CreateSolidBrush(fgcolor);
406
+
407
+ if (!brush)
408
+ return FALSE;
409
+
410
+ if (x > 0)
411
+ rect.left = x;
412
+
413
+ if (y > 0)
414
+ rect.top = y;
415
+
416
+ rect.right = x + width - 1;
417
+ rect.bottom = y + height - 1;
418
+
419
+ if ((x + width > rect.left) && (y + height > rect.top))
420
+ gdi_FillRect(gdi->drawing->hdc, &rect, brush);
421
+
422
+ gdi_DeleteObject((HGDIOBJECT)brush);
423
+ }
424
+
425
+ return gdi_SetNullClipRgn(gdi->drawing->hdc);
426
+ }
427
+
428
+ return TRUE;
429
+ }
430
+
431
+ static BOOL gdi_Glyph_EndDraw(rdpContext* context, INT32 x, INT32 y, INT32 width, INT32 height,
432
+ UINT32 bgcolor, UINT32 fgcolor)
433
+ {
434
+ rdpGdi* gdi = NULL;
435
+
436
+ if (!context || !context->gdi)
437
+ return FALSE;
438
+
439
+ gdi = context->gdi;
440
+
441
+ if (!gdi->drawing || !gdi->drawing->hdc)
442
+ return FALSE;
443
+
444
+ gdi_SetNullClipRgn(gdi->drawing->hdc);
445
+ return TRUE;
446
+ }
447
+
448
+ /* Graphics Module */
449
+ BOOL gdi_register_graphics(rdpGraphics* graphics)
450
+ {
451
+ rdpBitmap bitmap = { 0 };
452
+ rdpGlyph glyph = { 0 };
453
+ bitmap.size = sizeof(gdiBitmap);
454
+ bitmap.New = gdi_Bitmap_New;
455
+ bitmap.Free = gdi_Bitmap_Free;
456
+ bitmap.Paint = gdi_Bitmap_Paint;
457
+ bitmap.Decompress = gdi_Bitmap_Decompress;
458
+ bitmap.SetSurface = gdi_Bitmap_SetSurface;
459
+ graphics_register_bitmap(graphics, &bitmap);
460
+ glyph.size = sizeof(gdiGlyph);
461
+ glyph.New = gdi_Glyph_New;
462
+ glyph.Free = gdi_Glyph_Free;
463
+ glyph.Draw = gdi_Glyph_Draw;
464
+ glyph.BeginDraw = gdi_Glyph_BeginDraw;
465
+ glyph.EndDraw = gdi_Glyph_EndDraw;
466
+ graphics_register_glyph(graphics, &glyph);
467
+ return TRUE;
468
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/shape.c ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Shape Functions
4
+ *
5
+ * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <stdlib.h>
27
+
28
+ #include <freerdp/freerdp.h>
29
+ #include <freerdp/gdi/gdi.h>
30
+
31
+ #include <freerdp/gdi/bitmap.h>
32
+ #include <freerdp/gdi/region.h>
33
+ #include <freerdp/gdi/shape.h>
34
+
35
+ #include <freerdp/log.h>
36
+
37
+ #include "clipping.h"
38
+ #include "../gdi/gdi.h"
39
+
40
+ #define TAG FREERDP_TAG("gdi.shape")
41
+
42
+ static void Ellipse_Bresenham(HGDI_DC hdc, int x1, int y1, int x2, int y2)
43
+ {
44
+ INT32 e = 0;
45
+ INT32 e2 = 0;
46
+ INT32 dx = 0;
47
+ INT32 dy = 0;
48
+ INT32 a = 0;
49
+ INT32 b = 0;
50
+ INT32 c = 0;
51
+ a = (x1 < x2) ? x2 - x1 : x1 - x2;
52
+ b = (y1 < y2) ? y2 - y1 : y1 - y2;
53
+ c = b & 1;
54
+ dx = 4 * (1 - a) * b * b;
55
+ dy = 4 * (c + 1) * a * a;
56
+ e = dx + dy + c * a * a;
57
+
58
+ if (x1 > x2)
59
+ {
60
+ x1 = x2;
61
+ x2 += a;
62
+ }
63
+
64
+ if (y1 > y2)
65
+ y1 = y2;
66
+
67
+ y1 += (b + 1) / 2;
68
+ y2 = y1 - c;
69
+ a *= 8 * a;
70
+ c = 8 * b * b;
71
+
72
+ do
73
+ {
74
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x2),
75
+ WINPR_ASSERTING_INT_CAST(UINT32, y1), 0);
76
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x1),
77
+ WINPR_ASSERTING_INT_CAST(UINT32, y1), 0);
78
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x1),
79
+ WINPR_ASSERTING_INT_CAST(UINT32, y2), 0);
80
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x2),
81
+ WINPR_ASSERTING_INT_CAST(UINT32, y2), 0);
82
+ e2 = 2 * e;
83
+
84
+ if (e2 >= dx)
85
+ {
86
+ x1++;
87
+ x2--;
88
+ e += dx += c;
89
+ }
90
+
91
+ if (e2 <= dy)
92
+ {
93
+ y1++;
94
+ y2--;
95
+ e += dy += a;
96
+ }
97
+ } while (x1 <= x2);
98
+
99
+ while (y1 - y2 < b)
100
+ {
101
+ y1++;
102
+ y2--;
103
+
104
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(uint32_t, x1 - 1),
105
+ WINPR_ASSERTING_INT_CAST(uint32_t, y1), 0);
106
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(uint32_t, x1 - 1),
107
+ WINPR_ASSERTING_INT_CAST(uint32_t, y2), 0);
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Draw an ellipse
113
+ * msdn{dd162510}
114
+ *
115
+ * @param hdc device context
116
+ * @param nLeftRect x1
117
+ * @param nTopRect y1
118
+ * @param nRightRect x2
119
+ * @param nBottomRect y2
120
+ *
121
+ * @return nonzero if successful, 0 otherwise
122
+ */
123
+ BOOL gdi_Ellipse(HGDI_DC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
124
+ {
125
+ Ellipse_Bresenham(hdc, nLeftRect, nTopRect, nRightRect, nBottomRect);
126
+ return TRUE;
127
+ }
128
+
129
+ /**
130
+ * Fill a rectangle with the given brush.
131
+ * msdn{dd162719}
132
+ *
133
+ * @param hdc device context
134
+ * @param rect rectangle
135
+ * @param hbr brush
136
+ *
137
+ * @return nonzero if successful, 0 otherwise
138
+ */
139
+
140
+ BOOL gdi_FillRect(HGDI_DC hdc, const HGDI_RECT rect, HGDI_BRUSH hbr)
141
+ {
142
+ UINT32 color = 0;
143
+ UINT32 dstColor = 0;
144
+ BOOL monochrome = FALSE;
145
+ INT32 nXDest = 0;
146
+ INT32 nYDest = 0;
147
+ INT32 nWidth = 0;
148
+ INT32 nHeight = 0;
149
+ const BYTE* srcp = NULL;
150
+ DWORD formatSize = 0;
151
+ gdi_RectToCRgn(rect, &nXDest, &nYDest, &nWidth, &nHeight);
152
+
153
+ if (!hdc || !hbr)
154
+ return FALSE;
155
+
156
+ if (!gdi_ClipCoords(hdc, &nXDest, &nYDest, &nWidth, &nHeight, NULL, NULL))
157
+ return TRUE;
158
+
159
+ switch (hbr->style)
160
+ {
161
+ case GDI_BS_SOLID:
162
+ color = hbr->color;
163
+
164
+ for (INT32 x = 0; x < nWidth; x++)
165
+ {
166
+ BYTE* dstp = gdi_get_bitmap_pointer(hdc, nXDest + x, nYDest);
167
+
168
+ if (dstp)
169
+ FreeRDPWriteColor(dstp, hdc->format, color);
170
+ }
171
+
172
+ srcp = gdi_get_bitmap_pointer(hdc, nXDest, nYDest);
173
+ formatSize = FreeRDPGetBytesPerPixel(hdc->format);
174
+
175
+ for (INT32 y = 1; y < nHeight; y++)
176
+ {
177
+ BYTE* dstp = gdi_get_bitmap_pointer(hdc, nXDest, nYDest + y);
178
+ memcpy(dstp, srcp, 1ull * WINPR_ASSERTING_INT_CAST(size_t, nWidth) * formatSize);
179
+ }
180
+
181
+ break;
182
+
183
+ case GDI_BS_HATCHED:
184
+ case GDI_BS_PATTERN:
185
+ monochrome = (hbr->pattern->format == PIXEL_FORMAT_MONO);
186
+ formatSize = FreeRDPGetBytesPerPixel(hbr->pattern->format);
187
+
188
+ for (INT32 y = 0; y < nHeight; y++)
189
+ {
190
+ for (INT32 x = 0; x < nWidth; x++)
191
+ {
192
+ const size_t yOffset =
193
+ ((1ULL * WINPR_ASSERTING_INT_CAST(size_t, nYDest) +
194
+ WINPR_ASSERTING_INT_CAST(size_t, y)) *
195
+ WINPR_ASSERTING_INT_CAST(size_t, hbr->pattern->width) %
196
+ WINPR_ASSERTING_INT_CAST(size_t, hbr->pattern->height)) *
197
+ formatSize;
198
+ const size_t xOffset = ((1ULL * WINPR_ASSERTING_INT_CAST(size_t, nXDest) +
199
+ WINPR_ASSERTING_INT_CAST(size_t, x)) %
200
+ WINPR_ASSERTING_INT_CAST(size_t, hbr->pattern->width)) *
201
+ formatSize;
202
+ const BYTE* patp = &hbr->pattern->data[yOffset + xOffset];
203
+
204
+ if (monochrome)
205
+ {
206
+ if (*patp == 0)
207
+ dstColor = hdc->bkColor;
208
+ else
209
+ dstColor = hdc->textColor;
210
+ }
211
+ else
212
+ {
213
+ dstColor = FreeRDPReadColor(patp, hbr->pattern->format);
214
+ dstColor =
215
+ FreeRDPConvertColor(dstColor, hbr->pattern->format, hdc->format, NULL);
216
+ }
217
+
218
+ BYTE* dstp = gdi_get_bitmap_pointer(hdc, nXDest + x, nYDest + y);
219
+ if (dstp)
220
+ FreeRDPWriteColor(dstp, hdc->format, dstColor);
221
+ }
222
+ }
223
+
224
+ break;
225
+
226
+ default:
227
+ break;
228
+ }
229
+
230
+ if (!gdi_InvalidateRegion(hdc, nXDest, nYDest, nWidth, nHeight))
231
+ return FALSE;
232
+
233
+ return TRUE;
234
+ }
235
+
236
+ /**
237
+ * Draw a polygon
238
+ * msdn{dd162814}
239
+ * @param hdc device context
240
+ * @param lpPoints array of points
241
+ * @param nCount number of points
242
+ * @return nonzero if successful, 0 otherwise
243
+ */
244
+ BOOL gdi_Polygon(HGDI_DC hdc, GDI_POINT* lpPoints, int nCount)
245
+ {
246
+ WLog_ERR(TAG, "Not implemented!");
247
+ return FALSE;
248
+ }
249
+
250
+ /**
251
+ * Draw a series of closed polygons
252
+ * msdn{dd162818}
253
+ * @param hdc device context
254
+ * @param lpPoints array of series of points
255
+ * @param lpPolyCounts array of number of points in each series
256
+ * @param nCount count of number of points in lpPolyCounts
257
+ * @return nonzero if successful, 0 otherwise
258
+ */
259
+ BOOL gdi_PolyPolygon(HGDI_DC hdc, GDI_POINT* lpPoints, int* lpPolyCounts, int nCount)
260
+ {
261
+ WLog_ERR(TAG, "Not implemented!");
262
+ return FALSE;
263
+ }
264
+
265
+ BOOL gdi_Rectangle(HGDI_DC hdc, INT32 nXDst, INT32 nYDst, INT32 nWidth, INT32 nHeight)
266
+ {
267
+ UINT32 color = 0;
268
+
269
+ if (!gdi_ClipCoords(hdc, &nXDst, &nYDst, &nWidth, &nHeight, NULL, NULL))
270
+ return TRUE;
271
+
272
+ color = hdc->textColor;
273
+
274
+ for (INT32 y = 0; y < nHeight; y++)
275
+ {
276
+ BYTE* dstLeft = gdi_get_bitmap_pointer(hdc, nXDst, nYDst + y);
277
+ BYTE* dstRight = gdi_get_bitmap_pointer(hdc, nXDst + nWidth - 1, nYDst + y);
278
+
279
+ if (dstLeft)
280
+ FreeRDPWriteColor(dstLeft, hdc->format, color);
281
+
282
+ if (dstRight)
283
+ FreeRDPWriteColor(dstRight, hdc->format, color);
284
+ }
285
+
286
+ for (INT32 x = 0; x < nWidth; x++)
287
+ {
288
+ BYTE* dstTop = gdi_get_bitmap_pointer(hdc, nXDst + x, nYDst);
289
+ BYTE* dstBottom = gdi_get_bitmap_pointer(hdc, nXDst + x, nYDst + nHeight - 1);
290
+
291
+ if (dstTop)
292
+ FreeRDPWriteColor(dstTop, hdc->format, color);
293
+
294
+ if (dstBottom)
295
+ FreeRDPWriteColor(dstBottom, hdc->format, color);
296
+ }
297
+
298
+ return FALSE;
299
+ }