/src/FreeRDP/libfreerdp/codec/rfx.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * RemoteFX Codec Library |
4 | | * |
5 | | * Copyright 2011 Vic Lee |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 Norbert Federa <norbert.federa@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 <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 | | #include <winpr/tchar.h> |
32 | | #include <winpr/sysinfo.h> |
33 | | #include <winpr/registry.h> |
34 | | |
35 | | #include <freerdp/log.h> |
36 | | #include <freerdp/settings.h> |
37 | | #include <freerdp/codec/rfx.h> |
38 | | #include <freerdp/constants.h> |
39 | | #include <freerdp/primitives.h> |
40 | | #include <freerdp/codec/region.h> |
41 | | #include <freerdp/build-config.h> |
42 | | |
43 | | #include "rfx_constants.h" |
44 | | #include "rfx_types.h" |
45 | | #include "rfx_decode.h" |
46 | | #include "rfx_encode.h" |
47 | | #include "rfx_quantization.h" |
48 | | #include "rfx_dwt.h" |
49 | | #include "rfx_rlgr.h" |
50 | | |
51 | | #include "sse/rfx_sse2.h" |
52 | | #include "neon/rfx_neon.h" |
53 | | |
54 | | #define TAG FREERDP_TAG("codec") |
55 | | |
56 | 0 | #define RFX_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\RemoteFX" |
57 | | |
58 | | /** |
59 | | * The quantization values control the compression rate and quality. The value |
60 | | * range is between 6 and 15. The higher value, the higher compression rate |
61 | | * and lower quality. |
62 | | * |
63 | | * This is the default values being use by the MS RDP server, and we will also |
64 | | * use it as our default values for the encoder. It can be overridden by setting |
65 | | * the context->num_quants and context->quants member. |
66 | | * |
67 | | * The order of the values are: |
68 | | * LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1 |
69 | | */ |
70 | | static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 }; |
71 | | |
72 | | static INLINE BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx, |
73 | | wStream* WINPR_RESTRICT s, |
74 | | const RFX_TILE* WINPR_RESTRICT tile); |
75 | | |
76 | | static INLINE void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context) |
77 | 0 | { |
78 | 0 | if (!context || !context->priv) |
79 | 0 | return; |
80 | 0 | PROFILER_CREATE(context->priv->prof_rfx_decode_rgb, "rfx_decode_rgb") |
81 | 0 | PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component") |
82 | 0 | PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode") |
83 | 0 | PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode") |
84 | 0 | PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode") |
85 | 0 | PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode") |
86 | 0 | PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB") |
87 | 0 | PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb") |
88 | 0 | PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component") |
89 | 0 | PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode") |
90 | 0 | PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode") |
91 | 0 | PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode") |
92 | 0 | PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode") |
93 | 0 | PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr") |
94 | 0 | PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb") |
95 | 0 | } |
96 | | |
97 | | static INLINE void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context) |
98 | 0 | { |
99 | 0 | if (!context || !context->priv) |
100 | 0 | return; |
101 | 0 | PROFILER_FREE(context->priv->prof_rfx_decode_rgb) |
102 | 0 | PROFILER_FREE(context->priv->prof_rfx_decode_component) |
103 | 0 | PROFILER_FREE(context->priv->prof_rfx_rlgr_decode) |
104 | 0 | PROFILER_FREE(context->priv->prof_rfx_differential_decode) |
105 | 0 | PROFILER_FREE(context->priv->prof_rfx_quantization_decode) |
106 | 0 | PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode) |
107 | 0 | PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb) |
108 | 0 | PROFILER_FREE(context->priv->prof_rfx_encode_rgb) |
109 | 0 | PROFILER_FREE(context->priv->prof_rfx_encode_component) |
110 | 0 | PROFILER_FREE(context->priv->prof_rfx_rlgr_encode) |
111 | 0 | PROFILER_FREE(context->priv->prof_rfx_differential_encode) |
112 | 0 | PROFILER_FREE(context->priv->prof_rfx_quantization_encode) |
113 | 0 | PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode) |
114 | 0 | PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr) |
115 | 0 | PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb) |
116 | 0 | } |
117 | | |
118 | | static INLINE void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context) |
119 | 0 | { |
120 | 0 | if (!context || !context->priv) |
121 | 0 | return; |
122 | | |
123 | 0 | PROFILER_PRINT_HEADER |
124 | 0 | PROFILER_PRINT(context->priv->prof_rfx_decode_rgb) |
125 | 0 | PROFILER_PRINT(context->priv->prof_rfx_decode_component) |
126 | 0 | PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode) |
127 | 0 | PROFILER_PRINT(context->priv->prof_rfx_differential_decode) |
128 | 0 | PROFILER_PRINT(context->priv->prof_rfx_quantization_decode) |
129 | 0 | PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode) |
130 | 0 | PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb) |
131 | 0 | PROFILER_PRINT(context->priv->prof_rfx_encode_rgb) |
132 | 0 | PROFILER_PRINT(context->priv->prof_rfx_encode_component) |
133 | 0 | PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode) |
134 | 0 | PROFILER_PRINT(context->priv->prof_rfx_differential_encode) |
135 | 0 | PROFILER_PRINT(context->priv->prof_rfx_quantization_encode) |
136 | 0 | PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode) |
137 | 0 | PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr) |
138 | 0 | PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb) |
139 | 0 | PROFILER_PRINT_FOOTER |
140 | 0 | } |
141 | | |
142 | | static INLINE void rfx_tile_init(void* obj) |
143 | 0 | { |
144 | 0 | RFX_TILE* tile = (RFX_TILE*)obj; |
145 | 0 | if (tile) |
146 | 0 | { |
147 | 0 | tile->x = 0; |
148 | 0 | tile->y = 0; |
149 | 0 | tile->YLen = 0; |
150 | 0 | tile->YData = NULL; |
151 | 0 | tile->CbLen = 0; |
152 | 0 | tile->CbData = NULL; |
153 | 0 | tile->CrLen = 0; |
154 | 0 | tile->CrData = NULL; |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | | static INLINE void* rfx_decoder_tile_new(const void* val) |
159 | 0 | { |
160 | 0 | const size_t size = 4ULL * 64ULL * 64ULL; |
161 | 0 | RFX_TILE* tile = NULL; |
162 | 0 | WINPR_UNUSED(val); |
163 | |
|
164 | 0 | if (!(tile = (RFX_TILE*)winpr_aligned_calloc(1, sizeof(RFX_TILE), 32))) |
165 | 0 | return NULL; |
166 | | |
167 | 0 | if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16))) |
168 | 0 | { |
169 | 0 | winpr_aligned_free(tile); |
170 | 0 | return NULL; |
171 | 0 | } |
172 | 0 | memset(tile->data, 0xff, size); |
173 | 0 | tile->allocated = TRUE; |
174 | 0 | return tile; |
175 | 0 | } |
176 | | |
177 | | static INLINE void rfx_decoder_tile_free(void* obj) |
178 | 0 | { |
179 | 0 | RFX_TILE* tile = (RFX_TILE*)obj; |
180 | |
|
181 | 0 | if (tile) |
182 | 0 | { |
183 | 0 | if (tile->allocated) |
184 | 0 | winpr_aligned_free(tile->data); |
185 | |
|
186 | 0 | winpr_aligned_free(tile); |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | static INLINE void* rfx_encoder_tile_new(const void* val) |
191 | 0 | { |
192 | 0 | WINPR_UNUSED(val); |
193 | 0 | return winpr_aligned_calloc(1, sizeof(RFX_TILE), 32); |
194 | 0 | } |
195 | | |
196 | | static INLINE void rfx_encoder_tile_free(void* obj) |
197 | 0 | { |
198 | 0 | winpr_aligned_free(obj); |
199 | 0 | } |
200 | | |
201 | | RFX_CONTEXT* rfx_context_new(BOOL encoder) |
202 | 0 | { |
203 | 0 | return rfx_context_new_ex(encoder, 0); |
204 | 0 | } |
205 | | |
206 | | RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags) |
207 | 0 | { |
208 | 0 | HKEY hKey = NULL; |
209 | 0 | LONG status = 0; |
210 | 0 | DWORD dwType = 0; |
211 | 0 | DWORD dwSize = 0; |
212 | 0 | DWORD dwValue = 0; |
213 | 0 | SYSTEM_INFO sysinfo; |
214 | 0 | RFX_CONTEXT* context = NULL; |
215 | 0 | wObject* pool = NULL; |
216 | 0 | RFX_CONTEXT_PRIV* priv = NULL; |
217 | 0 | context = (RFX_CONTEXT*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT), 32); |
218 | |
|
219 | 0 | if (!context) |
220 | 0 | return NULL; |
221 | | |
222 | 0 | context->encoder = encoder; |
223 | 0 | context->currentMessage.freeArray = TRUE; |
224 | 0 | context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT_PRIV), 32); |
225 | |
|
226 | 0 | if (!priv) |
227 | 0 | goto fail; |
228 | | |
229 | 0 | priv->log = WLog_Get("com.freerdp.codec.rfx"); |
230 | 0 | WLog_OpenAppender(priv->log); |
231 | 0 | priv->TilePool = ObjectPool_New(TRUE); |
232 | |
|
233 | 0 | if (!priv->TilePool) |
234 | 0 | goto fail; |
235 | | |
236 | 0 | pool = ObjectPool_Object(priv->TilePool); |
237 | 0 | pool->fnObjectInit = rfx_tile_init; |
238 | |
|
239 | 0 | if (context->encoder) |
240 | 0 | { |
241 | 0 | pool->fnObjectNew = rfx_encoder_tile_new; |
242 | 0 | pool->fnObjectFree = rfx_encoder_tile_free; |
243 | 0 | } |
244 | 0 | else |
245 | 0 | { |
246 | 0 | pool->fnObjectNew = rfx_decoder_tile_new; |
247 | 0 | pool->fnObjectFree = rfx_decoder_tile_free; |
248 | 0 | } |
249 | | |
250 | | /* |
251 | | * align buffers to 16 byte boundary (needed for SSE/NEON instructions) |
252 | | * |
253 | | * y_r_buffer, cb_g_buffer, cr_b_buffer: 64 * 64 * sizeof(INT16) = 8192 (0x2000) |
254 | | * dwt_buffer: 32 * 32 * 2 * 2 * sizeof(INT16) = 8192, maximum sub-band width is 32 |
255 | | * |
256 | | * Additionally we add 32 bytes (16 in front and 16 at the back of the buffer) |
257 | | * in order to allow optimized functions (SEE, NEON) to read from positions |
258 | | * that are actually in front/beyond the buffer. Offset calculations are |
259 | | * performed at the BufferPool_Take function calls in rfx_encode/decode.c. |
260 | | * |
261 | | * We then multiply by 3 to use a single, partionned buffer for all 3 channels. |
262 | | */ |
263 | 0 | priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16); |
264 | |
|
265 | 0 | if (!priv->BufferPool) |
266 | 0 | goto fail; |
267 | | |
268 | 0 | if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS)) |
269 | 0 | { |
270 | 0 | priv->UseThreads = TRUE; |
271 | |
|
272 | 0 | GetNativeSystemInfo(&sysinfo); |
273 | 0 | priv->MinThreadCount = sysinfo.dwNumberOfProcessors; |
274 | 0 | priv->MaxThreadCount = 0; |
275 | 0 | status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey); |
276 | |
|
277 | 0 | if (status == ERROR_SUCCESS) |
278 | 0 | { |
279 | 0 | dwSize = sizeof(dwValue); |
280 | |
|
281 | 0 | if (RegQueryValueEx(hKey, _T("UseThreads"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) == |
282 | 0 | ERROR_SUCCESS) |
283 | 0 | priv->UseThreads = dwValue ? 1 : 0; |
284 | |
|
285 | 0 | if (RegQueryValueEx(hKey, _T("MinThreadCount"), NULL, &dwType, (BYTE*)&dwValue, |
286 | 0 | &dwSize) == ERROR_SUCCESS) |
287 | 0 | priv->MinThreadCount = dwValue; |
288 | |
|
289 | 0 | if (RegQueryValueEx(hKey, _T("MaxThreadCount"), NULL, &dwType, (BYTE*)&dwValue, |
290 | 0 | &dwSize) == ERROR_SUCCESS) |
291 | 0 | priv->MaxThreadCount = dwValue; |
292 | |
|
293 | 0 | RegCloseKey(hKey); |
294 | 0 | } |
295 | 0 | } |
296 | 0 | else |
297 | 0 | { |
298 | 0 | priv->UseThreads = FALSE; |
299 | 0 | } |
300 | |
|
301 | 0 | if (priv->UseThreads) |
302 | 0 | { |
303 | | /* Call primitives_get here in order to avoid race conditions when using primitives_get */ |
304 | | /* from multiple threads. This call will initialize all function pointers correctly */ |
305 | | /* before any decoding threads are started */ |
306 | 0 | primitives_get(); |
307 | 0 | priv->ThreadPool = CreateThreadpool(NULL); |
308 | |
|
309 | 0 | if (!priv->ThreadPool) |
310 | 0 | goto fail; |
311 | | |
312 | 0 | InitializeThreadpoolEnvironment(&priv->ThreadPoolEnv); |
313 | 0 | SetThreadpoolCallbackPool(&priv->ThreadPoolEnv, priv->ThreadPool); |
314 | |
|
315 | 0 | if (priv->MinThreadCount) |
316 | 0 | if (!SetThreadpoolThreadMinimum(priv->ThreadPool, priv->MinThreadCount)) |
317 | 0 | goto fail; |
318 | | |
319 | 0 | if (priv->MaxThreadCount) |
320 | 0 | SetThreadpoolThreadMaximum(priv->ThreadPool, priv->MaxThreadCount); |
321 | 0 | } |
322 | | |
323 | | /* initialize the default pixel format */ |
324 | 0 | rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32); |
325 | | /* create profilers for default decoding routines */ |
326 | 0 | rfx_profiler_create(context); |
327 | | /* set up default routines */ |
328 | 0 | context->quantization_decode = rfx_quantization_decode; |
329 | 0 | context->quantization_encode = rfx_quantization_encode; |
330 | 0 | context->dwt_2d_decode = rfx_dwt_2d_decode; |
331 | 0 | context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode; |
332 | 0 | context->dwt_2d_encode = rfx_dwt_2d_encode; |
333 | 0 | context->rlgr_decode = rfx_rlgr_decode; |
334 | 0 | context->rlgr_encode = rfx_rlgr_encode; |
335 | 0 | rfx_init_sse2(context); |
336 | 0 | rfx_init_neon(context); |
337 | 0 | context->state = RFX_STATE_SEND_HEADERS; |
338 | 0 | context->expectedDataBlockType = WBT_FRAME_BEGIN; |
339 | 0 | return context; |
340 | 0 | fail: |
341 | 0 | WINPR_PRAGMA_DIAG_PUSH |
342 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
343 | 0 | rfx_context_free(context); |
344 | 0 | WINPR_PRAGMA_DIAG_POP |
345 | 0 | return NULL; |
346 | 0 | } |
347 | | |
348 | | void rfx_context_free(RFX_CONTEXT* context) |
349 | 0 | { |
350 | 0 | RFX_CONTEXT_PRIV* priv = NULL; |
351 | |
|
352 | 0 | if (!context) |
353 | 0 | return; |
354 | | |
355 | 0 | WINPR_ASSERT(NULL != context); |
356 | | |
357 | 0 | priv = context->priv; |
358 | 0 | WINPR_ASSERT(NULL != priv); |
359 | 0 | WINPR_ASSERT(NULL != priv->TilePool); |
360 | 0 | WINPR_ASSERT(NULL != priv->BufferPool); |
361 | | |
362 | | /* coverity[address_free] */ |
363 | 0 | rfx_message_free(context, &context->currentMessage); |
364 | 0 | winpr_aligned_free(context->quants); |
365 | 0 | rfx_profiler_print(context); |
366 | 0 | rfx_profiler_free(context); |
367 | |
|
368 | 0 | if (priv) |
369 | 0 | { |
370 | 0 | ObjectPool_Free(priv->TilePool); |
371 | 0 | if (priv->UseThreads) |
372 | 0 | { |
373 | 0 | if (priv->ThreadPool) |
374 | 0 | CloseThreadpool(priv->ThreadPool); |
375 | 0 | DestroyThreadpoolEnvironment(&priv->ThreadPoolEnv); |
376 | 0 | winpr_aligned_free((void*)priv->workObjects); |
377 | 0 | winpr_aligned_free(priv->tileWorkParams); |
378 | | #ifdef WITH_PROFILER |
379 | | WLog_VRB( |
380 | | TAG, |
381 | | "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!"); |
382 | | #endif |
383 | 0 | } |
384 | |
|
385 | 0 | BufferPool_Free(priv->BufferPool); |
386 | 0 | winpr_aligned_free(priv); |
387 | 0 | } |
388 | 0 | winpr_aligned_free(context); |
389 | 0 | } |
390 | | |
391 | | static INLINE RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index) |
392 | 0 | { |
393 | 0 | WINPR_ASSERT(message); |
394 | 0 | WINPR_ASSERT(message->tiles); |
395 | 0 | WINPR_ASSERT(index < message->numTiles); |
396 | 0 | return message->tiles[index]; |
397 | 0 | } |
398 | | |
399 | | static INLINE const RFX_RECT* rfx_message_get_rect_const(const RFX_MESSAGE* WINPR_RESTRICT message, |
400 | | UINT32 index) |
401 | 0 | { |
402 | 0 | WINPR_ASSERT(message); |
403 | 0 | WINPR_ASSERT(message->rects); |
404 | 0 | WINPR_ASSERT(index < message->numRects); |
405 | 0 | return &message->rects[index]; |
406 | 0 | } |
407 | | |
408 | | static INLINE RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index) |
409 | 0 | { |
410 | 0 | WINPR_ASSERT(message); |
411 | 0 | WINPR_ASSERT(message->rects); |
412 | 0 | WINPR_ASSERT(index < message->numRects); |
413 | 0 | return &message->rects[index]; |
414 | 0 | } |
415 | | |
416 | | void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format) |
417 | 0 | { |
418 | 0 | WINPR_ASSERT(context); |
419 | 0 | context->pixel_format = pixel_format; |
420 | 0 | const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format); |
421 | 0 | context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp); |
422 | 0 | } |
423 | | |
424 | | UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context) |
425 | 0 | { |
426 | 0 | WINPR_ASSERT(context); |
427 | 0 | return context->pixel_format; |
428 | 0 | } |
429 | | |
430 | | void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context, |
431 | | const BYTE* WINPR_RESTRICT palette) |
432 | 0 | { |
433 | 0 | WINPR_ASSERT(context); |
434 | 0 | context->palette = palette; |
435 | 0 | } |
436 | | |
437 | | const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context) |
438 | 0 | { |
439 | 0 | WINPR_ASSERT(context); |
440 | 0 | return context->palette; |
441 | 0 | } |
442 | | |
443 | | BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height) |
444 | 0 | { |
445 | 0 | if (!context) |
446 | 0 | return FALSE; |
447 | | |
448 | 0 | context->width = WINPR_ASSERTING_INT_CAST(UINT16, width); |
449 | 0 | context->height = WINPR_ASSERTING_INT_CAST(UINT16, height); |
450 | 0 | context->state = RFX_STATE_SEND_HEADERS; |
451 | 0 | context->expectedDataBlockType = WBT_FRAME_BEGIN; |
452 | 0 | context->frameIdx = 0; |
453 | 0 | return TRUE; |
454 | 0 | } |
455 | | |
456 | | static INLINE BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context, |
457 | | wStream* WINPR_RESTRICT s) |
458 | 0 | { |
459 | 0 | UINT32 magic = 0; |
460 | |
|
461 | 0 | WINPR_ASSERT(context); |
462 | 0 | WINPR_ASSERT(context->priv); |
463 | 0 | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC; |
464 | | |
465 | | /* RFX_SYNC */ |
466 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
467 | 0 | return FALSE; |
468 | | |
469 | 0 | Stream_Read_UINT32(s, magic); /* magic (4 bytes), 0xCACCACCA */ |
470 | 0 | if (magic != WF_MAGIC) |
471 | 0 | { |
472 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid magic number 0x%08" PRIX32 "", magic); |
473 | 0 | return FALSE; |
474 | 0 | } |
475 | | |
476 | 0 | Stream_Read_UINT16(s, context->version); /* version (2 bytes), WF_VERSION_1_0 (0x0100) */ |
477 | 0 | if (context->version != WF_VERSION_1_0) |
478 | 0 | { |
479 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid version number 0x%08" PRIX32 "", |
480 | 0 | context->version); |
481 | 0 | return FALSE; |
482 | 0 | } |
483 | | |
484 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "version 0x%08" PRIX32 "", context->version); |
485 | 0 | context->decodedHeaderBlocks |= RFX_DECODED_SYNC; |
486 | 0 | return TRUE; |
487 | 0 | } |
488 | | |
489 | | static INLINE BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context, |
490 | | wStream* WINPR_RESTRICT s) |
491 | 0 | { |
492 | 0 | BYTE numCodecs = 0; |
493 | |
|
494 | 0 | WINPR_ASSERT(context); |
495 | 0 | WINPR_ASSERT(context->priv); |
496 | 0 | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS; |
497 | |
|
498 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4)) |
499 | 0 | return FALSE; |
500 | | |
501 | 0 | Stream_Read_UINT8(s, numCodecs); /* numCodecs (1 byte), must be set to 0x01 */ |
502 | 0 | Stream_Read_UINT8(s, context->codec_id); /* codecId (1 byte), must be set to 0x01 */ |
503 | 0 | Stream_Read_UINT16( |
504 | 0 | s, context->codec_version); /* version (2 bytes), must be set to WF_VERSION_1_0 (0x0100) */ |
505 | |
|
506 | 0 | if (numCodecs != 1) |
507 | 0 | { |
508 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "numCodes is 0x%02" PRIX8 " (must be 0x01)", |
509 | 0 | numCodecs); |
510 | 0 | return FALSE; |
511 | 0 | } |
512 | | |
513 | 0 | if (context->codec_id != 0x01) |
514 | 0 | { |
515 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec id (0x%02" PRIX32 ")", |
516 | 0 | context->codec_id); |
517 | 0 | return FALSE; |
518 | 0 | } |
519 | | |
520 | 0 | if (context->codec_version != WF_VERSION_1_0) |
521 | 0 | { |
522 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec version (0x%08" PRIX32 ")", |
523 | 0 | context->codec_version); |
524 | 0 | return FALSE; |
525 | 0 | } |
526 | | |
527 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "id %" PRIu32 " version 0x%" PRIX32 ".", |
528 | 0 | context->codec_id, context->codec_version); |
529 | 0 | context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS; |
530 | 0 | return TRUE; |
531 | 0 | } |
532 | | |
533 | | static INLINE BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context, |
534 | | wStream* WINPR_RESTRICT s) |
535 | 0 | { |
536 | 0 | BYTE channelId = 0; |
537 | 0 | BYTE numChannels = 0; |
538 | |
|
539 | 0 | WINPR_ASSERT(context); |
540 | 0 | WINPR_ASSERT(context->priv); |
541 | 0 | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS; |
542 | |
|
543 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1)) |
544 | 0 | return FALSE; |
545 | | |
546 | 0 | Stream_Read_UINT8(s, numChannels); /* numChannels (1 byte), must bet set to 0x01 */ |
547 | | |
548 | | /* In RDVH sessions, numChannels will represent the number of virtual monitors |
549 | | * configured and does not always be set to 0x01 as [MS-RDPRFX] said. |
550 | | */ |
551 | 0 | if (numChannels < 1) |
552 | 0 | { |
553 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "no channels announced"); |
554 | 0 | return FALSE; |
555 | 0 | } |
556 | | |
557 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull)) |
558 | 0 | return FALSE; |
559 | | |
560 | | /* RFX_CHANNELT */ |
561 | 0 | Stream_Read_UINT8(s, channelId); /* channelId (1 byte), must be set to 0x00 */ |
562 | |
|
563 | 0 | if (channelId != 0x00) |
564 | 0 | { |
565 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "channelId:0x%02" PRIX8 ", expected:0x00", |
566 | 0 | channelId); |
567 | 0 | return FALSE; |
568 | 0 | } |
569 | | |
570 | 0 | Stream_Read_UINT16(s, context->width); /* width (2 bytes) */ |
571 | 0 | Stream_Read_UINT16(s, context->height); /* height (2 bytes) */ |
572 | |
|
573 | 0 | if (!context->width || !context->height) |
574 | 0 | { |
575 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
576 | 0 | "invalid channel with/height: %" PRIu16 "x%" PRIu16 "", context->width, |
577 | 0 | context->height); |
578 | 0 | return FALSE; |
579 | 0 | } |
580 | | |
581 | | /* Now, only the first monitor can be used, therefore the other channels will be ignored. */ |
582 | 0 | Stream_Seek(s, 5ULL * (numChannels - 1)); |
583 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
584 | 0 | "numChannels %" PRIu8 " id %" PRIu8 ", %" PRIu16 "x%" PRIu16 ".", numChannels, |
585 | 0 | channelId, context->width, context->height); |
586 | 0 | context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS; |
587 | 0 | return TRUE; |
588 | 0 | } |
589 | | |
590 | | static INLINE BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context, |
591 | | wStream* WINPR_RESTRICT s) |
592 | 0 | { |
593 | 0 | BYTE ctxId = 0; |
594 | 0 | UINT16 tileSize = 0; |
595 | 0 | UINT16 properties = 0; |
596 | |
|
597 | 0 | WINPR_ASSERT(context); |
598 | 0 | WINPR_ASSERT(context->priv); |
599 | 0 | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT; |
600 | |
|
601 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5)) |
602 | 0 | return FALSE; |
603 | | |
604 | 0 | Stream_Read_UINT8(s, ctxId); /* ctxId (1 byte), must be set to 0x00 */ |
605 | 0 | Stream_Read_UINT16(s, tileSize); /* tileSize (2 bytes), must be set to CT_TILE_64x64 (0x0040) */ |
606 | 0 | Stream_Read_UINT16(s, properties); /* properties (2 bytes) */ |
607 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
608 | 0 | "ctxId %" PRIu8 " tileSize %" PRIu16 " properties 0x%04" PRIX16 ".", ctxId, tileSize, |
609 | 0 | properties); |
610 | 0 | context->properties = properties; |
611 | 0 | context->flags = (properties & 0x0007); |
612 | |
|
613 | 0 | if (context->flags == CODEC_MODE) |
614 | 0 | { |
615 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in image mode."); |
616 | 0 | } |
617 | 0 | else |
618 | 0 | { |
619 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in video mode."); |
620 | 0 | } |
621 | |
|
622 | 0 | switch ((properties & 0x1E00) >> 9) |
623 | 0 | { |
624 | 0 | case CLW_ENTROPY_RLGR1: |
625 | 0 | context->mode = RLGR1; |
626 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR1."); |
627 | 0 | break; |
628 | | |
629 | 0 | case CLW_ENTROPY_RLGR3: |
630 | 0 | context->mode = RLGR3; |
631 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR3."); |
632 | 0 | break; |
633 | | |
634 | 0 | default: |
635 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "unknown RLGR algorithm."); |
636 | 0 | return FALSE; |
637 | 0 | } |
638 | | |
639 | 0 | context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT; |
640 | 0 | return TRUE; |
641 | 0 | } |
642 | | |
643 | | static INLINE BOOL rfx_process_message_frame_begin( |
644 | | RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message, |
645 | | wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType) |
646 | 0 | { |
647 | 0 | UINT32 frameIdx = 0; |
648 | 0 | UINT16 numRegions = 0; |
649 | |
|
650 | 0 | WINPR_ASSERT(context); |
651 | 0 | WINPR_ASSERT(context->priv); |
652 | 0 | WINPR_ASSERT(message); |
653 | 0 | WINPR_ASSERT(pExpectedBlockType); |
654 | | |
655 | 0 | if (*pExpectedBlockType != WBT_FRAME_BEGIN) |
656 | 0 | { |
657 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_FRAME_BEGIN"); |
658 | 0 | return FALSE; |
659 | 0 | } |
660 | | |
661 | 0 | *pExpectedBlockType = WBT_REGION; |
662 | |
|
663 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
664 | 0 | return FALSE; |
665 | | |
666 | 0 | Stream_Read_UINT32( |
667 | 0 | s, frameIdx); /* frameIdx (4 bytes), if codec is in video mode, must be ignored */ |
668 | 0 | Stream_Read_UINT16(s, numRegions); /* numRegions (2 bytes) */ |
669 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
670 | 0 | "RFX_FRAME_BEGIN: frameIdx: %" PRIu32 " numRegions: %" PRIu16 "", frameIdx, |
671 | 0 | numRegions); |
672 | 0 | return TRUE; |
673 | 0 | } |
674 | | |
675 | | static INLINE BOOL rfx_process_message_frame_end( |
676 | | RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message, |
677 | | WINPR_ATTR_UNUSED wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType) |
678 | 0 | { |
679 | 0 | WINPR_ASSERT(context); |
680 | 0 | WINPR_ASSERT(context->priv); |
681 | 0 | WINPR_ASSERT(message); |
682 | 0 | WINPR_ASSERT(s); |
683 | 0 | WINPR_ASSERT(pExpectedBlockType); |
684 | | |
685 | 0 | if (*pExpectedBlockType != WBT_FRAME_END) |
686 | 0 | { |
687 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected, wants WBT_FRAME_END"); |
688 | 0 | return FALSE; |
689 | 0 | } |
690 | | |
691 | 0 | *pExpectedBlockType = WBT_FRAME_BEGIN; |
692 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "RFX_FRAME_END"); |
693 | 0 | return TRUE; |
694 | 0 | } |
695 | | |
696 | | static INLINE BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message) |
697 | 0 | { |
698 | 0 | WINPR_ASSERT(message); |
699 | | |
700 | 0 | RFX_RECT* tmpRects = |
701 | 0 | winpr_aligned_recalloc(message->rects, message->numRects, sizeof(RFX_RECT), 32); |
702 | 0 | if (!tmpRects) |
703 | 0 | return FALSE; |
704 | 0 | message->rects = tmpRects; |
705 | 0 | return TRUE; |
706 | 0 | } |
707 | | |
708 | | static INLINE BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context, |
709 | | RFX_MESSAGE* WINPR_RESTRICT message, |
710 | | wStream* WINPR_RESTRICT s, |
711 | | UINT16* WINPR_RESTRICT pExpectedBlockType) |
712 | 0 | { |
713 | 0 | UINT16 regionType = 0; |
714 | 0 | UINT16 numTileSets = 0; |
715 | |
|
716 | 0 | WINPR_ASSERT(context); |
717 | 0 | WINPR_ASSERT(context->priv); |
718 | 0 | WINPR_ASSERT(message); |
719 | 0 | WINPR_ASSERT(pExpectedBlockType); |
720 | | |
721 | 0 | if (*pExpectedBlockType != WBT_REGION) |
722 | 0 | { |
723 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_REGION"); |
724 | 0 | return FALSE; |
725 | 0 | } |
726 | | |
727 | 0 | *pExpectedBlockType = WBT_EXTENSION; |
728 | |
|
729 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3)) |
730 | 0 | return FALSE; |
731 | | |
732 | 0 | Stream_Seek_UINT8(s); /* regionFlags (1 byte) */ |
733 | 0 | Stream_Read_UINT16(s, message->numRects); /* numRects (2 bytes) */ |
734 | |
|
735 | 0 | if (message->numRects < 1) |
736 | 0 | { |
737 | | /* |
738 | | If numRects is zero the decoder must generate a rectangle with |
739 | | coordinates (0, 0, width, height). |
740 | | See [MS-RDPRFX] (revision >= 17.0) 2.2.2.3.3 TS_RFX_REGION |
741 | | https://msdn.microsoft.com/en-us/library/ff635233.aspx |
742 | | */ |
743 | 0 | message->numRects = 1; |
744 | 0 | if (!rfx_resize_rects(message)) |
745 | 0 | return FALSE; |
746 | | |
747 | 0 | message->rects->x = 0; |
748 | 0 | message->rects->y = 0; |
749 | 0 | message->rects->width = context->width; |
750 | 0 | message->rects->height = context->height; |
751 | 0 | return TRUE; |
752 | 0 | } |
753 | | |
754 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull)) |
755 | 0 | return FALSE; |
756 | | |
757 | 0 | if (!rfx_resize_rects(message)) |
758 | 0 | return FALSE; |
759 | | |
760 | | /* rects */ |
761 | 0 | for (UINT16 i = 0; i < message->numRects; i++) |
762 | 0 | { |
763 | 0 | RFX_RECT* rect = rfx_message_get_rect(message, i); |
764 | | /* RFX_RECT */ |
765 | 0 | Stream_Read_UINT16(s, rect->x); /* x (2 bytes) */ |
766 | 0 | Stream_Read_UINT16(s, rect->y); /* y (2 bytes) */ |
767 | 0 | Stream_Read_UINT16(s, rect->width); /* width (2 bytes) */ |
768 | 0 | Stream_Read_UINT16(s, rect->height); /* height (2 bytes) */ |
769 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
770 | 0 | "rect %" PRIu16 " (x,y=%" PRIu16 ",%" PRIu16 " w,h=%" PRIu16 " %" PRIu16 ").", i, |
771 | 0 | rect->x, rect->y, rect->width, rect->height); |
772 | 0 | } |
773 | |
|
774 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4)) |
775 | 0 | return FALSE; |
776 | | |
777 | 0 | Stream_Read_UINT16(s, regionType); /*regionType (2 bytes): MUST be set to CBT_REGION (0xCAC1)*/ |
778 | 0 | Stream_Read_UINT16(s, numTileSets); /*numTilesets (2 bytes): MUST be set to 0x0001.*/ |
779 | |
|
780 | 0 | if (regionType != CBT_REGION) |
781 | 0 | { |
782 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid region type 0x%04" PRIX16 "", |
783 | 0 | regionType); |
784 | 0 | return TRUE; |
785 | 0 | } |
786 | | |
787 | 0 | if (numTileSets != 0x0001) |
788 | 0 | { |
789 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid number of tilesets (%" PRIu16 ")", |
790 | 0 | numTileSets); |
791 | 0 | return FALSE; |
792 | 0 | } |
793 | | |
794 | 0 | return TRUE; |
795 | 0 | } |
796 | | |
797 | | typedef struct |
798 | | { |
799 | | RFX_TILE* tile; |
800 | | RFX_CONTEXT* context; |
801 | | } RFX_TILE_PROCESS_WORK_PARAM; |
802 | | |
803 | | static INLINE void CALLBACK |
804 | | rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance, |
805 | | void* context, WINPR_ATTR_UNUSED PTP_WORK work) |
806 | 0 | { |
807 | 0 | RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context; |
808 | 0 | WINPR_ASSERT(param); |
809 | 0 | rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4); |
810 | 0 | } |
811 | | |
812 | | static INLINE BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message, size_t count, |
813 | | BOOL allocOnly) |
814 | 0 | { |
815 | 0 | WINPR_ASSERT(message); |
816 | | |
817 | 0 | RFX_TILE** tmpTiles = |
818 | 0 | (RFX_TILE**)winpr_aligned_recalloc((void*)message->tiles, count, sizeof(RFX_TILE*), 32); |
819 | 0 | if (!tmpTiles && (count != 0)) |
820 | 0 | return FALSE; |
821 | | |
822 | 0 | message->tiles = tmpTiles; |
823 | 0 | if (!allocOnly) |
824 | 0 | message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count); |
825 | 0 | else |
826 | 0 | { |
827 | 0 | WINPR_ASSERT(message->numTiles <= count); |
828 | 0 | } |
829 | 0 | message->allocatedTiles = count; |
830 | |
|
831 | 0 | return TRUE; |
832 | 0 | } |
833 | | |
834 | | static INLINE BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context, |
835 | | RFX_MESSAGE* WINPR_RESTRICT message, |
836 | | wStream* WINPR_RESTRICT s, |
837 | | UINT16* WINPR_RESTRICT pExpectedBlockType) |
838 | 0 | { |
839 | 0 | BOOL rc = 0; |
840 | 0 | size_t close_cnt = 0; |
841 | 0 | BYTE quant = 0; |
842 | 0 | RFX_TILE* tile = NULL; |
843 | 0 | UINT32* quants = NULL; |
844 | 0 | UINT16 subtype = 0; |
845 | 0 | UINT16 numTiles = 0; |
846 | 0 | UINT32 blockLen = 0; |
847 | 0 | UINT32 blockType = 0; |
848 | 0 | UINT32 tilesDataSize = 0; |
849 | 0 | PTP_WORK* work_objects = NULL; |
850 | 0 | RFX_TILE_PROCESS_WORK_PARAM* params = NULL; |
851 | 0 | void* pmem = NULL; |
852 | |
|
853 | 0 | WINPR_ASSERT(context); |
854 | 0 | WINPR_ASSERT(context->priv); |
855 | 0 | WINPR_ASSERT(message); |
856 | 0 | WINPR_ASSERT(pExpectedBlockType); |
857 | | |
858 | 0 | if (*pExpectedBlockType != WBT_EXTENSION) |
859 | 0 | { |
860 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants a tileset"); |
861 | 0 | return FALSE; |
862 | 0 | } |
863 | | |
864 | 0 | *pExpectedBlockType = WBT_FRAME_END; |
865 | |
|
866 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14)) |
867 | 0 | return FALSE; |
868 | | |
869 | 0 | Stream_Read_UINT16(s, subtype); /* subtype (2 bytes) must be set to CBT_TILESET (0xCAC2) */ |
870 | 0 | if (subtype != CBT_TILESET) |
871 | 0 | { |
872 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid subtype, expected CBT_TILESET."); |
873 | 0 | return FALSE; |
874 | 0 | } |
875 | | |
876 | 0 | Stream_Seek_UINT16(s); /* idx (2 bytes), must be set to 0x0000 */ |
877 | 0 | Stream_Seek_UINT16(s); /* properties (2 bytes) */ |
878 | 0 | Stream_Read_UINT8(s, context->numQuant); /* numQuant (1 byte) */ |
879 | 0 | Stream_Seek_UINT8(s); /* tileSize (1 byte), must be set to 0x40 */ |
880 | |
|
881 | 0 | if (context->numQuant < 1) |
882 | 0 | { |
883 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "no quantization value."); |
884 | 0 | return FALSE; |
885 | 0 | } |
886 | | |
887 | 0 | Stream_Read_UINT16(s, numTiles); /* numTiles (2 bytes) */ |
888 | 0 | if (numTiles < 1) |
889 | 0 | { |
890 | | /* Windows Server 2012 (not R2) can send empty tile sets */ |
891 | 0 | return TRUE; |
892 | 0 | } |
893 | | |
894 | 0 | Stream_Read_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */ |
895 | |
|
896 | 0 | if (!(pmem = |
897 | 0 | winpr_aligned_recalloc(context->quants, context->numQuant, 10 * sizeof(UINT32), 32))) |
898 | 0 | return FALSE; |
899 | | |
900 | 0 | quants = context->quants = (UINT32*)pmem; |
901 | | |
902 | | /* quantVals */ |
903 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull)) |
904 | 0 | return FALSE; |
905 | | |
906 | 0 | for (size_t i = 0; i < context->numQuant; i++) |
907 | 0 | { |
908 | | /* RFX_CODEC_QUANT */ |
909 | 0 | Stream_Read_UINT8(s, quant); |
910 | 0 | *quants++ = (quant & 0x0F); |
911 | 0 | *quants++ = (quant >> 4); |
912 | 0 | Stream_Read_UINT8(s, quant); |
913 | 0 | *quants++ = (quant & 0x0F); |
914 | 0 | *quants++ = (quant >> 4); |
915 | 0 | Stream_Read_UINT8(s, quant); |
916 | 0 | *quants++ = (quant & 0x0F); |
917 | 0 | *quants++ = (quant >> 4); |
918 | 0 | Stream_Read_UINT8(s, quant); |
919 | 0 | *quants++ = (quant & 0x0F); |
920 | 0 | *quants++ = (quant >> 4); |
921 | 0 | Stream_Read_UINT8(s, quant); |
922 | 0 | *quants++ = (quant & 0x0F); |
923 | 0 | *quants++ = (quant >> 4); |
924 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
925 | 0 | "quant %d (%" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 |
926 | 0 | " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 ").", |
927 | 0 | i, context->quants[i * 10], context->quants[i * 10 + 1], |
928 | 0 | context->quants[i * 10 + 2], context->quants[i * 10 + 3], |
929 | 0 | context->quants[i * 10 + 4], context->quants[i * 10 + 5], |
930 | 0 | context->quants[i * 10 + 6], context->quants[i * 10 + 7], |
931 | 0 | context->quants[i * 10 + 8], context->quants[i * 10 + 9]); |
932 | 0 | } |
933 | |
|
934 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
935 | 0 | { |
936 | 0 | ObjectPool_Return(context->priv->TilePool, message->tiles[i]); |
937 | 0 | message->tiles[i] = NULL; |
938 | 0 | } |
939 | |
|
940 | 0 | if (!rfx_allocate_tiles(message, numTiles, FALSE)) |
941 | 0 | return FALSE; |
942 | | |
943 | 0 | if (context->priv->UseThreads) |
944 | 0 | { |
945 | 0 | work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles, sizeof(PTP_WORK), 32); |
946 | 0 | params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc( |
947 | 0 | NULL, message->numTiles, sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32); |
948 | |
|
949 | 0 | if (!work_objects) |
950 | 0 | { |
951 | 0 | winpr_aligned_free(params); |
952 | 0 | return FALSE; |
953 | 0 | } |
954 | | |
955 | 0 | if (!params) |
956 | 0 | { |
957 | 0 | winpr_aligned_free((void*)work_objects); |
958 | 0 | return FALSE; |
959 | 0 | } |
960 | 0 | } |
961 | | |
962 | | /* tiles */ |
963 | 0 | close_cnt = 0; |
964 | 0 | rc = FALSE; |
965 | |
|
966 | 0 | if (Stream_GetRemainingLength(s) >= tilesDataSize) |
967 | 0 | { |
968 | 0 | rc = TRUE; |
969 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
970 | 0 | { |
971 | 0 | wStream subBuffer; |
972 | 0 | wStream* sub = NULL; |
973 | |
|
974 | 0 | if (!(tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool))) |
975 | 0 | { |
976 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
977 | 0 | "RfxMessageTileSet failed to get tile from object pool"); |
978 | 0 | rc = FALSE; |
979 | 0 | break; |
980 | 0 | } |
981 | | |
982 | 0 | message->tiles[i] = tile; |
983 | | |
984 | | /* RFX_TILE */ |
985 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
986 | 0 | { |
987 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
988 | 0 | "RfxMessageTileSet packet too small to read tile %d/%" PRIu16 "", i, |
989 | 0 | message->numTiles); |
990 | 0 | rc = FALSE; |
991 | 0 | break; |
992 | 0 | } |
993 | | |
994 | 0 | sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s)); |
995 | 0 | Stream_Read_UINT16( |
996 | 0 | sub, blockType); /* blockType (2 bytes), must be set to CBT_TILE (0xCAC3) */ |
997 | 0 | Stream_Read_UINT32(sub, blockLen); /* blockLen (4 bytes) */ |
998 | |
|
999 | 0 | if (!Stream_SafeSeek(s, blockLen)) |
1000 | 0 | { |
1001 | 0 | rc = FALSE; |
1002 | 0 | break; |
1003 | 0 | } |
1004 | 0 | if ((blockLen < 6 + 13) || |
1005 | 0 | (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6))) |
1006 | 0 | { |
1007 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1008 | 0 | "RfxMessageTileSet not enough bytes to read tile %d/%" PRIu16 |
1009 | 0 | " with blocklen=%" PRIu32 "", |
1010 | 0 | i, message->numTiles, blockLen); |
1011 | 0 | rc = FALSE; |
1012 | 0 | break; |
1013 | 0 | } |
1014 | | |
1015 | 0 | if (blockType != CBT_TILE) |
1016 | 0 | { |
1017 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1018 | 0 | "unknown block type 0x%" PRIX32 ", expected CBT_TILE (0xCAC3).", |
1019 | 0 | blockType); |
1020 | 0 | rc = FALSE; |
1021 | 0 | break; |
1022 | 0 | } |
1023 | | |
1024 | 0 | Stream_Read_UINT8(sub, tile->quantIdxY); /* quantIdxY (1 byte) */ |
1025 | 0 | Stream_Read_UINT8(sub, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
1026 | 0 | Stream_Read_UINT8(sub, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
1027 | 0 | if (tile->quantIdxY >= context->numQuant) |
1028 | 0 | { |
1029 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1030 | 0 | "quantIdxY %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxY, |
1031 | 0 | context->numQuant); |
1032 | 0 | rc = FALSE; |
1033 | 0 | break; |
1034 | 0 | } |
1035 | 0 | if (tile->quantIdxCb >= context->numQuant) |
1036 | 0 | { |
1037 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1038 | 0 | "quantIdxCb %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCb, |
1039 | 0 | context->numQuant); |
1040 | 0 | rc = FALSE; |
1041 | 0 | break; |
1042 | 0 | } |
1043 | 0 | if (tile->quantIdxCr >= context->numQuant) |
1044 | 0 | { |
1045 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1046 | 0 | "quantIdxCr %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCr, |
1047 | 0 | context->numQuant); |
1048 | 0 | rc = FALSE; |
1049 | 0 | break; |
1050 | 0 | } |
1051 | | |
1052 | 0 | Stream_Read_UINT16(sub, tile->xIdx); /* xIdx (2 bytes) */ |
1053 | 0 | Stream_Read_UINT16(sub, tile->yIdx); /* yIdx (2 bytes) */ |
1054 | 0 | Stream_Read_UINT16(sub, tile->YLen); /* YLen (2 bytes) */ |
1055 | 0 | Stream_Read_UINT16(sub, tile->CbLen); /* CbLen (2 bytes) */ |
1056 | 0 | Stream_Read_UINT16(sub, tile->CrLen); /* CrLen (2 bytes) */ |
1057 | 0 | Stream_GetPointer(sub, tile->YData); |
1058 | 0 | if (!Stream_SafeSeek(sub, tile->YLen)) |
1059 | 0 | { |
1060 | 0 | rc = FALSE; |
1061 | 0 | break; |
1062 | 0 | } |
1063 | 0 | Stream_GetPointer(sub, tile->CbData); |
1064 | 0 | if (!Stream_SafeSeek(sub, tile->CbLen)) |
1065 | 0 | { |
1066 | 0 | rc = FALSE; |
1067 | 0 | break; |
1068 | 0 | } |
1069 | 0 | Stream_GetPointer(sub, tile->CrData); |
1070 | 0 | if (!Stream_SafeSeek(sub, tile->CrLen)) |
1071 | 0 | { |
1072 | 0 | rc = FALSE; |
1073 | 0 | break; |
1074 | 0 | } |
1075 | 0 | tile->x = tile->xIdx * 64; |
1076 | 0 | tile->y = tile->yIdx * 64; |
1077 | |
|
1078 | 0 | if (context->priv->UseThreads) |
1079 | 0 | { |
1080 | 0 | if (!params) |
1081 | 0 | { |
1082 | 0 | rc = FALSE; |
1083 | 0 | break; |
1084 | 0 | } |
1085 | | |
1086 | 0 | params[i].context = context; |
1087 | 0 | params[i].tile = message->tiles[i]; |
1088 | |
|
1089 | 0 | if (!(work_objects[i] = |
1090 | 0 | CreateThreadpoolWork(rfx_process_message_tile_work_callback, |
1091 | 0 | (void*)¶ms[i], &context->priv->ThreadPoolEnv))) |
1092 | 0 | { |
1093 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "CreateThreadpoolWork failed."); |
1094 | 0 | rc = FALSE; |
1095 | 0 | break; |
1096 | 0 | } |
1097 | | |
1098 | 0 | SubmitThreadpoolWork(work_objects[i]); |
1099 | 0 | close_cnt = i + 1; |
1100 | 0 | } |
1101 | 0 | else |
1102 | 0 | { |
1103 | 0 | rfx_decode_rgb(context, tile, tile->data, 64 * 4); |
1104 | 0 | } |
1105 | 0 | } |
1106 | 0 | } |
1107 | |
|
1108 | 0 | if (context->priv->UseThreads) |
1109 | 0 | { |
1110 | 0 | for (size_t i = 0; i < close_cnt; i++) |
1111 | 0 | { |
1112 | 0 | WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE); |
1113 | 0 | CloseThreadpoolWork(work_objects[i]); |
1114 | 0 | } |
1115 | 0 | } |
1116 | |
|
1117 | 0 | winpr_aligned_free((void*)work_objects); |
1118 | 0 | winpr_aligned_free(params); |
1119 | |
|
1120 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
1121 | 0 | { |
1122 | 0 | if (!(tile = message->tiles[i])) |
1123 | 0 | continue; |
1124 | | |
1125 | 0 | tile->YLen = tile->CbLen = tile->CrLen = 0; |
1126 | 0 | tile->YData = tile->CbData = tile->CrData = NULL; |
1127 | 0 | } |
1128 | |
|
1129 | 0 | return rc; |
1130 | 0 | } |
1131 | | |
1132 | | BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context, const BYTE* WINPR_RESTRICT data, |
1133 | | UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst, |
1134 | | UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight, |
1135 | | REGION16* WINPR_RESTRICT invalidRegion) |
1136 | 0 | { |
1137 | 0 | REGION16 updateRegion = { 0 }; |
1138 | 0 | wStream inStream = { 0 }; |
1139 | 0 | BOOL ok = TRUE; |
1140 | |
|
1141 | 0 | if (!context || !data || !length) |
1142 | 0 | return FALSE; |
1143 | | |
1144 | 0 | WINPR_ASSERT(context->priv); |
1145 | 0 | RFX_MESSAGE* message = &context->currentMessage; |
1146 | |
|
1147 | 0 | wStream* s = Stream_StaticConstInit(&inStream, data, length); |
1148 | |
|
1149 | 0 | while (ok && Stream_GetRemainingLength(s) > 6) |
1150 | 0 | { |
1151 | 0 | wStream subStreamBuffer = { 0 }; |
1152 | 0 | size_t extraBlockLen = 0; |
1153 | 0 | UINT32 blockLen = 0; |
1154 | 0 | UINT32 blockType = 0; |
1155 | | |
1156 | | /* RFX_BLOCKT */ |
1157 | 0 | Stream_Read_UINT16(s, blockType); /* blockType (2 bytes) */ |
1158 | 0 | Stream_Read_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
1159 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "blockType 0x%" PRIX32 " blockLen %" PRIu32 "", |
1160 | 0 | blockType, blockLen); |
1161 | |
|
1162 | 0 | if (blockLen < 6) |
1163 | 0 | { |
1164 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "blockLen too small(%" PRIu32 ")", blockLen); |
1165 | 0 | return FALSE; |
1166 | 0 | } |
1167 | | |
1168 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6)) |
1169 | 0 | return FALSE; |
1170 | | |
1171 | 0 | if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS) |
1172 | 0 | { |
1173 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "incomplete header blocks processing"); |
1174 | 0 | return FALSE; |
1175 | 0 | } |
1176 | | |
1177 | 0 | if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION) |
1178 | 0 | { |
1179 | | /* RFX_CODEC_CHANNELT */ |
1180 | 0 | UINT8 codecId = 0; |
1181 | 0 | UINT8 channelId = 0; |
1182 | |
|
1183 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2)) |
1184 | 0 | return FALSE; |
1185 | | |
1186 | 0 | extraBlockLen = 2; |
1187 | 0 | Stream_Read_UINT8(s, codecId); /* codecId (1 byte) must be set to 0x01 */ |
1188 | 0 | Stream_Read_UINT8(s, channelId); /* channelId (1 byte) 0xFF or 0x00, see below */ |
1189 | |
|
1190 | 0 | if (codecId != 0x01) |
1191 | 0 | { |
1192 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codecId 0x%02" PRIX8 "", |
1193 | 0 | codecId); |
1194 | 0 | return FALSE; |
1195 | 0 | } |
1196 | | |
1197 | 0 | if (blockType == WBT_CONTEXT) |
1198 | 0 | { |
1199 | | /* If the blockType is set to WBT_CONTEXT, then channelId MUST be set to 0xFF.*/ |
1200 | 0 | if (channelId != 0xFF) |
1201 | 0 | { |
1202 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1203 | 0 | "invalid channelId 0x%02" PRIX8 " for blockType 0x%08" PRIX32 "", |
1204 | 0 | channelId, blockType); |
1205 | 0 | return FALSE; |
1206 | 0 | } |
1207 | 0 | } |
1208 | 0 | else |
1209 | 0 | { |
1210 | | /* For all other values of blockType, channelId MUST be set to 0x00. */ |
1211 | 0 | if (channelId != 0x00) |
1212 | 0 | { |
1213 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1214 | 0 | "invalid channelId 0x%02" PRIX8 " for blockType WBT_CONTEXT", |
1215 | 0 | channelId); |
1216 | 0 | return FALSE; |
1217 | 0 | } |
1218 | 0 | } |
1219 | 0 | } |
1220 | | |
1221 | 0 | const size_t blockLenNoHeader = blockLen - 6; |
1222 | 0 | if (blockLenNoHeader < extraBlockLen) |
1223 | 0 | { |
1224 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1225 | 0 | "blockLen too small(%" PRIu32 "), must be >= 6 + %" PRIu16, blockLen, |
1226 | 0 | extraBlockLen); |
1227 | 0 | return FALSE; |
1228 | 0 | } |
1229 | | |
1230 | 0 | const size_t subStreamLen = blockLenNoHeader - extraBlockLen; |
1231 | 0 | wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen); |
1232 | 0 | Stream_Seek(s, subStreamLen); |
1233 | |
|
1234 | 0 | switch (blockType) |
1235 | 0 | { |
1236 | | /* Header messages: |
1237 | | * The stream MUST start with the header messages and any of these headers can appear |
1238 | | * in the stream at a later stage. The header messages can be repeated. |
1239 | | */ |
1240 | 0 | case WBT_SYNC: |
1241 | 0 | ok = rfx_process_message_sync(context, subStream); |
1242 | 0 | break; |
1243 | | |
1244 | 0 | case WBT_CONTEXT: |
1245 | 0 | ok = rfx_process_message_context(context, subStream); |
1246 | 0 | break; |
1247 | | |
1248 | 0 | case WBT_CODEC_VERSIONS: |
1249 | 0 | ok = rfx_process_message_codec_versions(context, subStream); |
1250 | 0 | break; |
1251 | | |
1252 | 0 | case WBT_CHANNELS: |
1253 | 0 | ok = rfx_process_message_channels(context, subStream); |
1254 | 0 | break; |
1255 | | |
1256 | | /* Data messages: |
1257 | | * The data associated with each encoded frame or image is always bracketed by the |
1258 | | * TS_RFX_FRAME_BEGIN (section 2.2.2.3.1) and TS_RFX_FRAME_END (section 2.2.2.3.2) |
1259 | | * messages. There MUST only be one TS_RFX_REGION (section 2.2.2.3.3) message per |
1260 | | * frame and one TS_RFX_TILESET (section 2.2.2.3.4) message per TS_RFX_REGION. |
1261 | | */ |
1262 | | |
1263 | 0 | case WBT_FRAME_BEGIN: |
1264 | 0 | ok = rfx_process_message_frame_begin(context, message, subStream, |
1265 | 0 | &context->expectedDataBlockType); |
1266 | 0 | break; |
1267 | | |
1268 | 0 | case WBT_REGION: |
1269 | 0 | ok = rfx_process_message_region(context, message, subStream, |
1270 | 0 | &context->expectedDataBlockType); |
1271 | 0 | break; |
1272 | | |
1273 | 0 | case WBT_EXTENSION: |
1274 | 0 | ok = rfx_process_message_tileset(context, message, subStream, |
1275 | 0 | &context->expectedDataBlockType); |
1276 | 0 | break; |
1277 | | |
1278 | 0 | case WBT_FRAME_END: |
1279 | 0 | ok = rfx_process_message_frame_end(context, message, subStream, |
1280 | 0 | &context->expectedDataBlockType); |
1281 | 0 | break; |
1282 | | |
1283 | 0 | default: |
1284 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "unknown blockType 0x%" PRIX32 "", |
1285 | 0 | blockType); |
1286 | 0 | return FALSE; |
1287 | 0 | } |
1288 | 0 | } |
1289 | | |
1290 | 0 | if (ok) |
1291 | 0 | { |
1292 | 0 | UINT32 nbUpdateRects = 0; |
1293 | 0 | REGION16 clippingRects = { 0 }; |
1294 | 0 | const RECTANGLE_16* updateRects = NULL; |
1295 | 0 | const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format); |
1296 | 0 | const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat); |
1297 | 0 | region16_init(&clippingRects); |
1298 | |
|
1299 | 0 | WINPR_ASSERT(dstWidth <= UINT16_MAX); |
1300 | 0 | WINPR_ASSERT(dstHeight <= UINT16_MAX); |
1301 | 0 | for (UINT32 i = 0; i < message->numRects; i++) |
1302 | 0 | { |
1303 | 0 | RECTANGLE_16 clippingRect = { 0 }; |
1304 | 0 | const RFX_RECT* rect = &(message->rects[i]); |
1305 | |
|
1306 | 0 | WINPR_ASSERT(left + rect->x <= UINT16_MAX); |
1307 | 0 | WINPR_ASSERT(top + rect->y <= UINT16_MAX); |
1308 | 0 | WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX); |
1309 | 0 | WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX); |
1310 | | |
1311 | 0 | clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth)); |
1312 | 0 | clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight)); |
1313 | | |
1314 | 0 | const UINT32 rw = 1UL * clippingRect.left + rect->width; |
1315 | 0 | const UINT32 rh = 1UL * clippingRect.top + rect->height; |
1316 | 0 | const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth)); |
1317 | 0 | const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight)); |
1318 | 0 | clippingRect.right = right; |
1319 | 0 | clippingRect.bottom = bottom; |
1320 | 0 | region16_union_rect(&clippingRects, &clippingRects, &clippingRect); |
1321 | 0 | } |
1322 | | |
1323 | 0 | for (UINT32 i = 0; i < message->numTiles; i++) |
1324 | 0 | { |
1325 | 0 | RECTANGLE_16 updateRect = { 0 }; |
1326 | 0 | const RFX_TILE* tile = rfx_message_get_tile(message, i); |
1327 | |
|
1328 | 0 | WINPR_ASSERT(left + tile->x <= UINT16_MAX); |
1329 | 0 | WINPR_ASSERT(top + tile->y <= UINT16_MAX); |
1330 | | |
1331 | 0 | updateRect.left = (UINT16)left + tile->x; |
1332 | 0 | updateRect.top = (UINT16)top + tile->y; |
1333 | 0 | updateRect.right = updateRect.left + 64; |
1334 | 0 | updateRect.bottom = updateRect.top + 64; |
1335 | 0 | region16_init(&updateRegion); |
1336 | 0 | region16_intersect_rect(&updateRegion, &clippingRects, &updateRect); |
1337 | 0 | updateRects = region16_rects(&updateRegion, &nbUpdateRects); |
1338 | |
|
1339 | 0 | for (UINT32 j = 0; j < nbUpdateRects; j++) |
1340 | 0 | { |
1341 | 0 | const RECTANGLE_16* cur = &updateRects[j]; |
1342 | 0 | const UINT32 stride = 64 * formatSize; |
1343 | 0 | const UINT32 nXDst = cur->left; |
1344 | 0 | const UINT32 nYDst = cur->top; |
1345 | 0 | const UINT32 nXSrc = nXDst - updateRect.left; |
1346 | 0 | const UINT32 nYSrc = nYDst - updateRect.top; |
1347 | 0 | const UINT32 nWidth = cur->right - cur->left; |
1348 | 0 | const UINT32 nHeight = cur->bottom - cur->top; |
1349 | |
|
1350 | 0 | if (!freerdp_image_copy_no_overlap(dst, dstFormat, dstStride, nXDst, nYDst, nWidth, |
1351 | 0 | nHeight, tile->data, context->pixel_format, |
1352 | 0 | stride, nXSrc, nYSrc, NULL, FREERDP_FLIP_NONE)) |
1353 | 0 | { |
1354 | 0 | region16_uninit(&updateRegion); |
1355 | 0 | region16_uninit(&clippingRects); |
1356 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1357 | 0 | "nbUpdateRectx[%" PRIu32 " (%" PRIu32 ")] freerdp_image_copy failed", |
1358 | 0 | j, nbUpdateRects); |
1359 | 0 | return FALSE; |
1360 | 0 | } |
1361 | | |
1362 | 0 | if (invalidRegion) |
1363 | 0 | region16_union_rect(invalidRegion, invalidRegion, cur); |
1364 | 0 | } |
1365 | | |
1366 | 0 | region16_uninit(&updateRegion); |
1367 | 0 | } |
1368 | | |
1369 | 0 | region16_uninit(&clippingRects); |
1370 | 0 | return TRUE; |
1371 | 0 | } |
1372 | 0 | else |
1373 | 0 | { |
1374 | 0 | rfx_message_free(context, message); |
1375 | 0 | context->currentMessage.freeArray = TRUE; |
1376 | 0 | } |
1377 | | |
1378 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "failed"); |
1379 | 0 | return FALSE; |
1380 | 0 | } |
1381 | | |
1382 | | const UINT32* rfx_message_get_quants(const RFX_MESSAGE* WINPR_RESTRICT message, |
1383 | | UINT16* WINPR_RESTRICT numQuantVals) |
1384 | 0 | { |
1385 | 0 | WINPR_ASSERT(message); |
1386 | 0 | if (numQuantVals) |
1387 | 0 | *numQuantVals = message->numQuant; |
1388 | 0 | return message->quantVals; |
1389 | 0 | } |
1390 | | |
1391 | | const RFX_TILE** rfx_message_get_tiles(const RFX_MESSAGE* WINPR_RESTRICT message, |
1392 | | UINT16* WINPR_RESTRICT numTiles) |
1393 | 0 | { |
1394 | 0 | WINPR_ASSERT(message); |
1395 | 0 | if (numTiles) |
1396 | 0 | *numTiles = message->numTiles; |
1397 | |
|
1398 | 0 | union |
1399 | 0 | { |
1400 | 0 | RFX_TILE** pp; |
1401 | 0 | const RFX_TILE** ppc; |
1402 | 0 | } cnv; |
1403 | 0 | cnv.pp = message->tiles; |
1404 | 0 | return cnv.ppc; |
1405 | 0 | } |
1406 | | |
1407 | | UINT16 rfx_message_get_tile_count(const RFX_MESSAGE* WINPR_RESTRICT message) |
1408 | 0 | { |
1409 | 0 | WINPR_ASSERT(message); |
1410 | 0 | return message->numTiles; |
1411 | 0 | } |
1412 | | |
1413 | | const RFX_RECT* rfx_message_get_rects(const RFX_MESSAGE* WINPR_RESTRICT message, |
1414 | | UINT16* WINPR_RESTRICT numRects) |
1415 | 0 | { |
1416 | 0 | WINPR_ASSERT(message); |
1417 | 0 | if (numRects) |
1418 | 0 | *numRects = message->numRects; |
1419 | 0 | return message->rects; |
1420 | 0 | } |
1421 | | |
1422 | | UINT16 rfx_message_get_rect_count(const RFX_MESSAGE* WINPR_RESTRICT message) |
1423 | 0 | { |
1424 | 0 | WINPR_ASSERT(message); |
1425 | 0 | return message->numRects; |
1426 | 0 | } |
1427 | | |
1428 | | void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message) |
1429 | 0 | { |
1430 | 0 | if (!message) |
1431 | 0 | return; |
1432 | | |
1433 | 0 | winpr_aligned_free(message->rects); |
1434 | |
|
1435 | 0 | if (message->tiles) |
1436 | 0 | { |
1437 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
1438 | 0 | { |
1439 | 0 | RFX_TILE* tile = message->tiles[i]; |
1440 | 0 | if (!tile) |
1441 | 0 | continue; |
1442 | | |
1443 | 0 | if (tile->YCbCrData) |
1444 | 0 | { |
1445 | 0 | BufferPool_Return(context->priv->BufferPool, tile->YCbCrData); |
1446 | 0 | tile->YCbCrData = NULL; |
1447 | 0 | } |
1448 | |
|
1449 | 0 | ObjectPool_Return(context->priv->TilePool, (void*)tile); |
1450 | 0 | } |
1451 | |
|
1452 | 0 | rfx_allocate_tiles(message, 0, FALSE); |
1453 | 0 | } |
1454 | |
|
1455 | 0 | const BOOL freeArray = message->freeArray; |
1456 | 0 | const RFX_MESSAGE empty = { 0 }; |
1457 | 0 | *message = empty; |
1458 | |
|
1459 | 0 | if (!freeArray) |
1460 | 0 | winpr_aligned_free(message); |
1461 | 0 | } |
1462 | | |
1463 | | static INLINE void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context) |
1464 | 0 | { |
1465 | 0 | UINT16 properties = 0; |
1466 | |
|
1467 | 0 | WINPR_ASSERT(context); |
1468 | | /* properties in tilesets: note that this has different format from the one in TS_RFX_CONTEXT */ |
1469 | 0 | properties = 1; /* lt */ |
1470 | 0 | properties |= (context->flags << 1); /* flags */ |
1471 | 0 | properties |= (COL_CONV_ICT << 4); /* cct */ |
1472 | 0 | properties |= (CLW_XFORM_DWT_53_A << 6); /* xft */ |
1473 | 0 | properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10); /* et */ |
1474 | 0 | properties |= (SCALAR_QUANTIZATION << 14); /* qt */ |
1475 | 0 | context->properties = properties; |
1476 | 0 | } |
1477 | | |
1478 | | static INLINE void |
1479 | | rfx_write_message_sync(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context, |
1480 | | WINPR_ATTR_UNUSED wStream* WINPR_RESTRICT s) |
1481 | 0 | { |
1482 | 0 | WINPR_ASSERT(context); |
1483 | | |
1484 | 0 | Stream_Write_UINT16(s, WBT_SYNC); /* BlockT.blockType (2 bytes) */ |
1485 | 0 | Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */ |
1486 | 0 | Stream_Write_UINT32(s, WF_MAGIC); /* magic (4 bytes) */ |
1487 | 0 | Stream_Write_UINT16(s, WF_VERSION_1_0); /* version (2 bytes) */ |
1488 | 0 | } |
1489 | | |
1490 | | static INLINE void |
1491 | | rfx_write_message_codec_versions(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context, |
1492 | | wStream* WINPR_RESTRICT s) |
1493 | 0 | { |
1494 | 0 | WINPR_ASSERT(context); |
1495 | | |
1496 | 0 | Stream_Write_UINT16(s, WBT_CODEC_VERSIONS); /* BlockT.blockType (2 bytes) */ |
1497 | 0 | Stream_Write_UINT32(s, 10); /* BlockT.blockLen (4 bytes) */ |
1498 | 0 | Stream_Write_UINT8(s, 1); /* numCodecs (1 byte) */ |
1499 | 0 | Stream_Write_UINT8(s, 1); /* codecs.codecId (1 byte) */ |
1500 | 0 | Stream_Write_UINT16(s, WF_VERSION_1_0); /* codecs.version (2 bytes) */ |
1501 | 0 | } |
1502 | | |
1503 | | static INLINE void rfx_write_message_channels(const RFX_CONTEXT* WINPR_RESTRICT context, |
1504 | | wStream* WINPR_RESTRICT s) |
1505 | 0 | { |
1506 | 0 | WINPR_ASSERT(context); |
1507 | | |
1508 | 0 | Stream_Write_UINT16(s, WBT_CHANNELS); /* BlockT.blockType (2 bytes) */ |
1509 | 0 | Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */ |
1510 | 0 | Stream_Write_UINT8(s, 1); /* numChannels (1 byte) */ |
1511 | 0 | Stream_Write_UINT8(s, 0); /* Channel.channelId (1 byte) */ |
1512 | 0 | Stream_Write_UINT16(s, context->width); /* Channel.width (2 bytes) */ |
1513 | 0 | Stream_Write_UINT16(s, context->height); /* Channel.height (2 bytes) */ |
1514 | 0 | } |
1515 | | |
1516 | | static INLINE void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context, |
1517 | | wStream* WINPR_RESTRICT s) |
1518 | 0 | { |
1519 | 0 | UINT16 properties = 0; |
1520 | 0 | WINPR_ASSERT(context); |
1521 | | |
1522 | 0 | Stream_Write_UINT16(s, WBT_CONTEXT); /* CodecChannelT.blockType (2 bytes) */ |
1523 | 0 | Stream_Write_UINT32(s, 13); /* CodecChannelT.blockLen (4 bytes) */ |
1524 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
1525 | 0 | Stream_Write_UINT8(s, 0xFF); /* CodecChannelT.channelId (1 byte) */ |
1526 | 0 | Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */ |
1527 | 0 | Stream_Write_UINT16(s, CT_TILE_64x64); /* tileSize (2 bytes) */ |
1528 | | /* properties */ |
1529 | 0 | properties = context->flags; /* flags */ |
1530 | 0 | properties |= (COL_CONV_ICT << 3); /* cct */ |
1531 | 0 | properties |= (CLW_XFORM_DWT_53_A << 5); /* xft */ |
1532 | 0 | properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9); /* et */ |
1533 | 0 | properties |= (SCALAR_QUANTIZATION << 13); /* qt */ |
1534 | 0 | Stream_Write_UINT16(s, properties); /* properties (2 bytes) */ |
1535 | 0 | rfx_update_context_properties(context); |
1536 | 0 | } |
1537 | | |
1538 | | static INLINE BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context, |
1539 | | wStream* WINPR_RESTRICT s) |
1540 | 0 | { |
1541 | 0 | WINPR_ASSERT(context); |
1542 | 0 | if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13)) |
1543 | 0 | return FALSE; |
1544 | | |
1545 | 0 | rfx_write_message_sync(context, s); |
1546 | 0 | rfx_write_message_context(context, s); |
1547 | 0 | rfx_write_message_codec_versions(context, s); |
1548 | 0 | rfx_write_message_channels(context, s); |
1549 | 0 | return TRUE; |
1550 | 0 | } |
1551 | | |
1552 | | static INLINE size_t rfx_tile_length(const RFX_TILE* WINPR_RESTRICT tile) |
1553 | 0 | { |
1554 | 0 | WINPR_ASSERT(tile); |
1555 | 0 | return 19ull + tile->YLen + tile->CbLen + tile->CrLen; |
1556 | 0 | } |
1557 | | |
1558 | | static INLINE BOOL rfx_write_tile(wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile) |
1559 | 0 | { |
1560 | 0 | const size_t blockLen = rfx_tile_length(tile); |
1561 | 0 | if (blockLen > UINT32_MAX) |
1562 | 0 | return FALSE; |
1563 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
1564 | 0 | return FALSE; |
1565 | | |
1566 | 0 | Stream_Write_UINT16(s, CBT_TILE); /* BlockT.blockType (2 bytes) */ |
1567 | 0 | Stream_Write_UINT32(s, (UINT32)blockLen); /* BlockT.blockLen (4 bytes) */ |
1568 | 0 | Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */ |
1569 | 0 | Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
1570 | 0 | Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
1571 | 0 | Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */ |
1572 | 0 | Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */ |
1573 | 0 | Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */ |
1574 | 0 | Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */ |
1575 | 0 | Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */ |
1576 | 0 | Stream_Write(s, tile->YData, tile->YLen); /* YData */ |
1577 | 0 | Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */ |
1578 | 0 | Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */ |
1579 | 0 | return TRUE; |
1580 | 0 | } |
1581 | | |
1582 | | struct S_RFX_TILE_COMPOSE_WORK_PARAM |
1583 | | { |
1584 | | RFX_TILE* tile; |
1585 | | RFX_CONTEXT* context; |
1586 | | }; |
1587 | | |
1588 | | static INLINE void CALLBACK |
1589 | | rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance, |
1590 | | void* context, WINPR_ATTR_UNUSED PTP_WORK work) |
1591 | 0 | { |
1592 | 0 | RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context; |
1593 | 0 | WINPR_ASSERT(param); |
1594 | 0 | rfx_encode_rgb(param->context, param->tile); |
1595 | 0 | } |
1596 | | |
1597 | | static INLINE BOOL computeRegion(const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
1598 | | REGION16* WINPR_RESTRICT region, size_t width, size_t height) |
1599 | 0 | { |
1600 | 0 | const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width), |
1601 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, height) }; |
1602 | | |
1603 | 0 | WINPR_ASSERT(rects); |
1604 | 0 | for (size_t i = 0; i < numRects; i++) |
1605 | 0 | { |
1606 | 0 | const RFX_RECT* rect = &rects[i]; |
1607 | 0 | RECTANGLE_16 rect16 = { 0 }; |
1608 | 0 | rect16.left = rect->x; |
1609 | 0 | rect16.top = rect->y; |
1610 | 0 | rect16.right = rect->x + rect->width; |
1611 | 0 | rect16.bottom = rect->y + rect->height; |
1612 | |
|
1613 | 0 | if (!region16_union_rect(region, region, &rect16)) |
1614 | 0 | return FALSE; |
1615 | 0 | } |
1616 | | |
1617 | 0 | return region16_intersect_rect(region, region, &mainRect); |
1618 | 0 | } |
1619 | | |
1620 | 0 | #define TILE_NO(v) ((v) / 64) |
1621 | | |
1622 | | static INLINE BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context, size_t nbTiles) |
1623 | 0 | { |
1624 | 0 | WINPR_ASSERT(context); |
1625 | | |
1626 | 0 | RFX_CONTEXT_PRIV* priv = context->priv; |
1627 | 0 | WINPR_ASSERT(priv); |
1628 | | |
1629 | 0 | void* pmem = NULL; |
1630 | |
|
1631 | 0 | if (!context->priv->UseThreads) |
1632 | 0 | return TRUE; |
1633 | | |
1634 | 0 | if (!(pmem = winpr_aligned_recalloc((void*)priv->workObjects, nbTiles, sizeof(PTP_WORK), 32))) |
1635 | 0 | return FALSE; |
1636 | | |
1637 | 0 | priv->workObjects = (PTP_WORK*)pmem; |
1638 | |
|
1639 | 0 | if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles, |
1640 | 0 | sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32))) |
1641 | 0 | return FALSE; |
1642 | | |
1643 | 0 | priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem; |
1644 | 0 | return TRUE; |
1645 | 0 | } |
1646 | | |
1647 | | static INLINE BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message, size_t count) |
1648 | 0 | { |
1649 | 0 | WINPR_ASSERT(message); |
1650 | | |
1651 | 0 | if (message->numTiles + count <= message->allocatedTiles) |
1652 | 0 | return TRUE; |
1653 | | |
1654 | 0 | const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count); |
1655 | 0 | return rfx_allocate_tiles(message, alloc, TRUE); |
1656 | 0 | } |
1657 | | |
1658 | | RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context, |
1659 | | const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
1660 | | const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h, size_t s) |
1661 | 0 | { |
1662 | 0 | const UINT32 width = w; |
1663 | 0 | const UINT32 height = h; |
1664 | 0 | const UINT32 scanline = (UINT32)s; |
1665 | 0 | RFX_MESSAGE* message = NULL; |
1666 | 0 | PTP_WORK* workObject = NULL; |
1667 | 0 | RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL; |
1668 | 0 | BOOL success = FALSE; |
1669 | 0 | REGION16 rectsRegion = { 0 }; |
1670 | 0 | REGION16 tilesRegion = { 0 }; |
1671 | 0 | RECTANGLE_16 currentTileRect = { 0 }; |
1672 | 0 | const RECTANGLE_16* regionRect = NULL; |
1673 | |
|
1674 | 0 | WINPR_ASSERT(data); |
1675 | 0 | WINPR_ASSERT(rects); |
1676 | 0 | WINPR_ASSERT(numRects > 0); |
1677 | 0 | WINPR_ASSERT(w > 0); |
1678 | 0 | WINPR_ASSERT(h > 0); |
1679 | 0 | WINPR_ASSERT(s > 0); |
1680 | | |
1681 | 0 | if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1, sizeof(RFX_MESSAGE), 32))) |
1682 | 0 | return NULL; |
1683 | | |
1684 | 0 | region16_init(&tilesRegion); |
1685 | 0 | region16_init(&rectsRegion); |
1686 | |
|
1687 | 0 | if (context->state == RFX_STATE_SEND_HEADERS) |
1688 | 0 | rfx_update_context_properties(context); |
1689 | |
|
1690 | 0 | message->frameIdx = context->frameIdx++; |
1691 | |
|
1692 | 0 | if (!context->numQuant) |
1693 | 0 | { |
1694 | 0 | WINPR_ASSERT(context->quants == NULL); |
1695 | 0 | if (!(context->quants = |
1696 | 0 | (UINT32*)winpr_aligned_malloc(sizeof(rfx_default_quantization_values), 32))) |
1697 | 0 | goto skip_encoding_loop; |
1698 | | |
1699 | 0 | CopyMemory(context->quants, &rfx_default_quantization_values, |
1700 | 0 | sizeof(rfx_default_quantization_values)); |
1701 | 0 | context->numQuant = 1; |
1702 | 0 | context->quantIdxY = 0; |
1703 | 0 | context->quantIdxCb = 0; |
1704 | 0 | context->quantIdxCr = 0; |
1705 | 0 | } |
1706 | | |
1707 | 0 | message->numQuant = context->numQuant; |
1708 | 0 | message->quantVals = context->quants; |
1709 | 0 | const UINT32 bytesPerPixel = (context->bits_per_pixel / 8); |
1710 | |
|
1711 | 0 | if (!computeRegion(rects, numRects, &rectsRegion, width, height)) |
1712 | 0 | goto skip_encoding_loop; |
1713 | | |
1714 | 0 | const RECTANGLE_16* extents = region16_extents(&rectsRegion); |
1715 | 0 | WINPR_ASSERT((INT32)extents->right - extents->left > 0); |
1716 | 0 | WINPR_ASSERT((INT32)extents->bottom - extents->top > 0); |
1717 | 0 | const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left); |
1718 | 0 | const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top); |
1719 | 0 | const UINT32 maxNbTiles = maxTilesX * maxTilesY; |
1720 | |
|
1721 | 0 | if (!rfx_ensure_tiles(message, maxNbTiles)) |
1722 | 0 | goto skip_encoding_loop; |
1723 | | |
1724 | 0 | if (!setupWorkers(context, maxNbTiles)) |
1725 | 0 | goto skip_encoding_loop; |
1726 | | |
1727 | 0 | if (context->priv->UseThreads) |
1728 | 0 | { |
1729 | 0 | workObject = context->priv->workObjects; |
1730 | 0 | workParam = context->priv->tileWorkParams; |
1731 | 0 | } |
1732 | |
|
1733 | 0 | UINT32 regionNbRects = 0; |
1734 | 0 | regionRect = region16_rects(&rectsRegion, ®ionNbRects); |
1735 | |
|
1736 | 0 | if (!(message->rects = winpr_aligned_calloc(regionNbRects, sizeof(RFX_RECT), 32))) |
1737 | 0 | goto skip_encoding_loop; |
1738 | | |
1739 | 0 | message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects); |
1740 | | |
1741 | 0 | for (UINT32 i = 0; i < regionNbRects; i++, regionRect++) |
1742 | 0 | { |
1743 | 0 | RFX_RECT* rfxRect = &message->rects[i]; |
1744 | 0 | UINT32 startTileX = regionRect->left / 64; |
1745 | 0 | UINT32 endTileX = (regionRect->right - 1) / 64; |
1746 | 0 | UINT32 startTileY = regionRect->top / 64; |
1747 | 0 | UINT32 endTileY = (regionRect->bottom - 1) / 64; |
1748 | 0 | rfxRect->x = regionRect->left; |
1749 | 0 | rfxRect->y = regionRect->top; |
1750 | 0 | rfxRect->width = (regionRect->right - regionRect->left); |
1751 | 0 | rfxRect->height = (regionRect->bottom - regionRect->top); |
1752 | |
|
1753 | 0 | for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY; |
1754 | 0 | yIdx++, gridRelY += 64) |
1755 | 0 | { |
1756 | 0 | UINT32 tileHeight = 64; |
1757 | |
|
1758 | 0 | if ((yIdx == endTileY) && (gridRelY + 64 > height)) |
1759 | 0 | tileHeight = height - gridRelY; |
1760 | |
|
1761 | 0 | currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY); |
1762 | 0 | currentTileRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight); |
1763 | | |
1764 | 0 | for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX; |
1765 | 0 | xIdx++, gridRelX += 64) |
1766 | 0 | { |
1767 | 0 | union |
1768 | 0 | { |
1769 | 0 | const BYTE* cpv; |
1770 | 0 | BYTE* pv; |
1771 | 0 | } cnv; |
1772 | 0 | UINT32 tileWidth = 64; |
1773 | |
|
1774 | 0 | if ((xIdx == endTileX) && (gridRelX + 64 > width)) |
1775 | 0 | { |
1776 | 0 | tileWidth = (width - gridRelX); |
1777 | 0 | } |
1778 | |
|
1779 | 0 | currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX); |
1780 | 0 | currentTileRect.right = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth); |
1781 | | |
1782 | | /* checks if this tile is already treated */ |
1783 | 0 | if (region16_intersects_rect(&tilesRegion, ¤tTileRect)) |
1784 | 0 | continue; |
1785 | | |
1786 | 0 | RFX_TILE* tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool); |
1787 | 0 | if (!tile) |
1788 | 0 | goto skip_encoding_loop; |
1789 | | |
1790 | 0 | tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx); |
1791 | 0 | tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx); |
1792 | 0 | tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX); |
1793 | 0 | tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY); |
1794 | 0 | tile->scanline = scanline; |
1795 | |
|
1796 | 0 | tile->width = tileWidth; |
1797 | 0 | tile->height = tileHeight; |
1798 | 0 | const UINT32 ax = gridRelX; |
1799 | 0 | const UINT32 ay = gridRelY; |
1800 | |
|
1801 | 0 | if (tile->data && tile->allocated) |
1802 | 0 | { |
1803 | 0 | winpr_aligned_free(tile->data); |
1804 | 0 | tile->allocated = FALSE; |
1805 | 0 | } |
1806 | | |
1807 | | /* Cast away const */ |
1808 | 0 | cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)]; |
1809 | 0 | tile->data = cnv.pv; |
1810 | 0 | tile->quantIdxY = context->quantIdxY; |
1811 | 0 | tile->quantIdxCb = context->quantIdxCb; |
1812 | 0 | tile->quantIdxCr = context->quantIdxCr; |
1813 | 0 | tile->YLen = tile->CbLen = tile->CrLen = 0; |
1814 | |
|
1815 | 0 | if (!(tile->YCbCrData = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1))) |
1816 | 0 | goto skip_encoding_loop; |
1817 | | |
1818 | 0 | tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]); |
1819 | 0 | tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]); |
1820 | 0 | tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]); |
1821 | |
|
1822 | 0 | if (!rfx_ensure_tiles(message, 1)) |
1823 | 0 | goto skip_encoding_loop; |
1824 | 0 | message->tiles[message->numTiles++] = tile; |
1825 | |
|
1826 | 0 | if (context->priv->UseThreads) |
1827 | 0 | { |
1828 | 0 | workParam->context = context; |
1829 | 0 | workParam->tile = tile; |
1830 | |
|
1831 | 0 | if (!(*workObject = CreateThreadpoolWork(rfx_compose_message_tile_work_callback, |
1832 | 0 | (void*)workParam, |
1833 | 0 | &context->priv->ThreadPoolEnv))) |
1834 | 0 | { |
1835 | 0 | goto skip_encoding_loop; |
1836 | 0 | } |
1837 | | |
1838 | 0 | SubmitThreadpoolWork(*workObject); |
1839 | 0 | workObject++; |
1840 | 0 | workParam++; |
1841 | 0 | } |
1842 | 0 | else |
1843 | 0 | { |
1844 | 0 | rfx_encode_rgb(context, tile); |
1845 | 0 | } |
1846 | | |
1847 | 0 | if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect)) |
1848 | 0 | goto skip_encoding_loop; |
1849 | 0 | } /* xIdx */ |
1850 | 0 | } /* yIdx */ |
1851 | 0 | } /* rects */ |
1852 | | |
1853 | 0 | success = TRUE; |
1854 | 0 | skip_encoding_loop: |
1855 | | |
1856 | | /* when using threads ensure all computations are done */ |
1857 | 0 | if (success) |
1858 | 0 | { |
1859 | 0 | message->tilesDataSize = 0; |
1860 | 0 | workObject = context->priv->workObjects; |
1861 | |
|
1862 | 0 | for (UINT32 i = 0; i < message->numTiles; i++) |
1863 | 0 | { |
1864 | 0 | if (context->priv->UseThreads) |
1865 | 0 | { |
1866 | 0 | if (*workObject) |
1867 | 0 | { |
1868 | 0 | WaitForThreadpoolWorkCallbacks(*workObject, FALSE); |
1869 | 0 | CloseThreadpoolWork(*workObject); |
1870 | 0 | } |
1871 | |
|
1872 | 0 | workObject++; |
1873 | 0 | } |
1874 | |
|
1875 | 0 | const RFX_TILE* tile = message->tiles[i]; |
1876 | 0 | const size_t tlen = rfx_tile_length(tile); |
1877 | 0 | message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen); |
1878 | 0 | } |
1879 | | |
1880 | 0 | region16_uninit(&tilesRegion); |
1881 | 0 | region16_uninit(&rectsRegion); |
1882 | |
|
1883 | 0 | return message; |
1884 | 0 | } |
1885 | | |
1886 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "failed"); |
1887 | |
|
1888 | 0 | rfx_message_free(context, message); |
1889 | 0 | region16_uninit(&tilesRegion); |
1890 | 0 | region16_uninit(&rectsRegion); |
1891 | 0 | return NULL; |
1892 | 0 | } |
1893 | | |
1894 | | static INLINE BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst, |
1895 | | const RFX_MESSAGE* WINPR_RESTRICT src) |
1896 | 0 | { |
1897 | 0 | WINPR_ASSERT(dst); |
1898 | 0 | WINPR_ASSERT(src); |
1899 | | |
1900 | 0 | WINPR_ASSERT(dst->rects == NULL); |
1901 | 0 | WINPR_ASSERT(dst->numRects == 0); |
1902 | | |
1903 | 0 | if (src->numRects == 0) |
1904 | 0 | return TRUE; |
1905 | | |
1906 | 0 | dst->rects = winpr_aligned_calloc(src->numRects, sizeof(RECTANGLE_16), 32); |
1907 | 0 | if (!dst->rects) |
1908 | 0 | return FALSE; |
1909 | 0 | dst->numRects = src->numRects; |
1910 | 0 | for (size_t x = 0; x < src->numRects; x++) |
1911 | 0 | { |
1912 | 0 | dst->rects[x] = src->rects[x]; |
1913 | 0 | } |
1914 | 0 | return TRUE; |
1915 | 0 | } |
1916 | | |
1917 | | static INLINE BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst, |
1918 | | const RFX_MESSAGE* WINPR_RESTRICT src) |
1919 | 0 | { |
1920 | 0 | WINPR_ASSERT(dst); |
1921 | 0 | WINPR_ASSERT(src); |
1922 | | |
1923 | 0 | WINPR_ASSERT(dst->quantVals == NULL); |
1924 | 0 | WINPR_ASSERT(dst->numQuant == 0); |
1925 | | |
1926 | 0 | if (src->numQuant == 0) |
1927 | 0 | return TRUE; |
1928 | | |
1929 | | /* quantVals are part of context */ |
1930 | 0 | dst->quantVals = src->quantVals; |
1931 | 0 | dst->numQuant = src->numQuant; |
1932 | |
|
1933 | 0 | return TRUE; |
1934 | 0 | } |
1935 | | |
1936 | | static INLINE RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context, |
1937 | | RFX_MESSAGE* WINPR_RESTRICT message, |
1938 | | size_t* WINPR_RESTRICT numMessages, size_t maxDataSize) |
1939 | 0 | { |
1940 | 0 | WINPR_ASSERT(context); |
1941 | 0 | WINPR_ASSERT(message); |
1942 | 0 | WINPR_ASSERT(numMessages); |
1943 | | |
1944 | 0 | maxDataSize -= 1024; /* reserve enough space for headers */ |
1945 | 0 | *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull; |
1946 | |
|
1947 | 0 | RFX_MESSAGE* messages = |
1948 | 0 | (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages), sizeof(RFX_MESSAGE), 32); |
1949 | 0 | if (!messages) |
1950 | 0 | return NULL; |
1951 | | |
1952 | 0 | UINT32 j = 0; |
1953 | 0 | for (UINT16 i = 0; i < message->numTiles; i++) |
1954 | 0 | { |
1955 | 0 | RFX_TILE* tile = message->tiles[i]; |
1956 | 0 | RFX_MESSAGE* msg = &messages[j]; |
1957 | |
|
1958 | 0 | WINPR_ASSERT(tile); |
1959 | 0 | WINPR_ASSERT(msg); |
1960 | | |
1961 | 0 | const size_t tileDataSize = rfx_tile_length(tile); |
1962 | |
|
1963 | 0 | if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize)) |
1964 | 0 | j++; |
1965 | |
|
1966 | 0 | if (msg->numTiles == 0) |
1967 | 0 | { |
1968 | 0 | msg->frameIdx = message->frameIdx + j; |
1969 | 0 | if (!rfx_clone_quants(msg, message)) |
1970 | 0 | goto free_messages; |
1971 | 0 | if (!rfx_clone_rects(msg, message)) |
1972 | 0 | goto free_messages; |
1973 | 0 | msg->freeArray = TRUE; |
1974 | 0 | if (!rfx_allocate_tiles(msg, message->numTiles, TRUE)) |
1975 | 0 | goto free_messages; |
1976 | 0 | } |
1977 | | |
1978 | 0 | msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize); |
1979 | | |
1980 | 0 | WINPR_ASSERT(msg->numTiles < msg->allocatedTiles); |
1981 | 0 | msg->tiles[msg->numTiles++] = message->tiles[i]; |
1982 | 0 | message->tiles[i] = NULL; |
1983 | 0 | } |
1984 | | |
1985 | 0 | *numMessages = j + 1ULL; |
1986 | 0 | context->frameIdx += j; |
1987 | 0 | message->numTiles = 0; |
1988 | 0 | return messages; |
1989 | 0 | free_messages: |
1990 | |
|
1991 | 0 | for (size_t i = 0; i < j; i++) |
1992 | 0 | rfx_allocate_tiles(&messages[i], 0, FALSE); |
1993 | |
|
1994 | 0 | winpr_aligned_free(messages); |
1995 | 0 | return NULL; |
1996 | 0 | } |
1997 | | |
1998 | | const RFX_MESSAGE* rfx_message_list_get(const RFX_MESSAGE_LIST* WINPR_RESTRICT messages, size_t idx) |
1999 | 0 | { |
2000 | 0 | WINPR_ASSERT(messages); |
2001 | 0 | if (idx >= messages->count) |
2002 | 0 | return NULL; |
2003 | 0 | WINPR_ASSERT(messages->list); |
2004 | 0 | return &messages->list[idx]; |
2005 | 0 | } |
2006 | | |
2007 | | void rfx_message_list_free(RFX_MESSAGE_LIST* messages) |
2008 | 0 | { |
2009 | 0 | if (!messages) |
2010 | 0 | return; |
2011 | 0 | for (size_t x = 0; x < messages->count; x++) |
2012 | 0 | rfx_message_free(messages->context, &messages->list[x]); |
2013 | 0 | free(messages); |
2014 | 0 | } |
2015 | | |
2016 | | static INLINE RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context, |
2017 | | RFX_MESSAGE* WINPR_RESTRICT messages, |
2018 | | size_t count) |
2019 | 0 | { |
2020 | 0 | WINPR_ASSERT(context); |
2021 | 0 | RFX_MESSAGE_LIST* msg = calloc(1, sizeof(RFX_MESSAGE_LIST)); |
2022 | 0 | WINPR_ASSERT(msg); |
2023 | | |
2024 | 0 | msg->context = context; |
2025 | 0 | msg->count = count; |
2026 | 0 | msg->list = messages; |
2027 | 0 | return msg; |
2028 | 0 | } |
2029 | | |
2030 | | RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context, |
2031 | | const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
2032 | | const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height, |
2033 | | UINT32 scanline, size_t* WINPR_RESTRICT numMessages, |
2034 | | size_t maxDataSize) |
2035 | 0 | { |
2036 | 0 | WINPR_ASSERT(context); |
2037 | 0 | WINPR_ASSERT(numMessages); |
2038 | | |
2039 | 0 | RFX_MESSAGE* message = |
2040 | 0 | rfx_encode_message(context, rects, numRects, data, width, height, scanline); |
2041 | 0 | if (!message) |
2042 | 0 | return NULL; |
2043 | | |
2044 | 0 | RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize); |
2045 | 0 | rfx_message_free(context, message); |
2046 | 0 | if (!list) |
2047 | 0 | return NULL; |
2048 | | |
2049 | 0 | return rfx_message_list_new(context, list, *numMessages); |
2050 | 0 | } |
2051 | | |
2052 | | static INLINE BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context, |
2053 | | wStream* WINPR_RESTRICT s, |
2054 | | const RFX_MESSAGE* WINPR_RESTRICT message) |
2055 | 0 | { |
2056 | 0 | WINPR_ASSERT(context); |
2057 | 0 | WINPR_ASSERT(message); |
2058 | | |
2059 | 0 | const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize; |
2060 | |
|
2061 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2062 | 0 | return FALSE; |
2063 | | |
2064 | 0 | Stream_Write_UINT16(s, WBT_EXTENSION); /* CodecChannelT.blockType (2 bytes) */ |
2065 | 0 | Stream_Write_UINT32(s, blockLen); /* set CodecChannelT.blockLen (4 bytes) */ |
2066 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
2067 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */ |
2068 | 0 | Stream_Write_UINT16(s, CBT_TILESET); /* subtype (2 bytes) */ |
2069 | 0 | Stream_Write_UINT16(s, 0); /* idx (2 bytes) */ |
2070 | 0 | Stream_Write_UINT16(s, context->properties); /* properties (2 bytes) */ |
2071 | 0 | Stream_Write_UINT8( |
2072 | 0 | s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant)); /* numQuant (1 byte) */ |
2073 | 0 | Stream_Write_UINT8(s, 0x40); /* tileSize (1 byte) */ |
2074 | 0 | Stream_Write_UINT16(s, message->numTiles); /* numTiles (2 bytes) */ |
2075 | 0 | Stream_Write_UINT32(s, message->tilesDataSize); /* tilesDataSize (4 bytes) */ |
2076 | | |
2077 | 0 | UINT32* quantVals = message->quantVals; |
2078 | 0 | for (size_t i = 0; i < message->numQuant * 5ul; i++) |
2079 | 0 | { |
2080 | 0 | WINPR_ASSERT(quantVals); |
2081 | 0 | Stream_Write_UINT8(s, |
2082 | 0 | WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4))); |
2083 | 0 | quantVals += 2; |
2084 | 0 | } |
2085 | | |
2086 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
2087 | 0 | { |
2088 | 0 | RFX_TILE* tile = message->tiles[i]; |
2089 | 0 | if (!tile) |
2090 | 0 | return FALSE; |
2091 | | |
2092 | 0 | if (!rfx_write_tile(s, tile)) |
2093 | 0 | return FALSE; |
2094 | 0 | } |
2095 | | |
2096 | | #ifdef WITH_DEBUG_RFX |
2097 | | WLog_Print(context->priv->log, WLOG_DEBUG, |
2098 | | "numQuant: %" PRIu16 " numTiles: %" PRIu16 " tilesDataSize: %" PRIu32 "", |
2099 | | message->numQuant, message->numTiles, message->tilesDataSize); |
2100 | | #endif |
2101 | 0 | return TRUE; |
2102 | 0 | } |
2103 | | |
2104 | | static INLINE BOOL |
2105 | | rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context, |
2106 | | wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT message) |
2107 | 0 | { |
2108 | 0 | WINPR_ASSERT(context); |
2109 | 0 | WINPR_ASSERT(message); |
2110 | | |
2111 | 0 | if (!Stream_EnsureRemainingCapacity(s, 14)) |
2112 | 0 | return FALSE; |
2113 | | |
2114 | 0 | Stream_Write_UINT16(s, WBT_FRAME_BEGIN); /* CodecChannelT.blockType */ |
2115 | 0 | Stream_Write_UINT32(s, 14); /* CodecChannelT.blockLen */ |
2116 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */ |
2117 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */ |
2118 | 0 | Stream_Write_UINT32(s, message->frameIdx); /* frameIdx */ |
2119 | 0 | Stream_Write_UINT16(s, 1); /* numRegions */ |
2120 | 0 | return TRUE; |
2121 | 0 | } |
2122 | | |
2123 | | static INLINE BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context, |
2124 | | wStream* WINPR_RESTRICT s, |
2125 | | const RFX_MESSAGE* WINPR_RESTRICT message) |
2126 | 0 | { |
2127 | 0 | WINPR_ASSERT(context); |
2128 | 0 | WINPR_ASSERT(message); |
2129 | | |
2130 | 0 | const size_t blockLen = 15 + (message->numRects * 8); |
2131 | 0 | if (blockLen > UINT32_MAX) |
2132 | 0 | return FALSE; |
2133 | | |
2134 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2135 | 0 | return FALSE; |
2136 | | |
2137 | 0 | Stream_Write_UINT16(s, WBT_REGION); /* CodecChannelT.blockType (2 bytes) */ |
2138 | 0 | Stream_Write_UINT32(s, (UINT32)blockLen); /* set CodecChannelT.blockLen (4 bytes) */ |
2139 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
2140 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */ |
2141 | 0 | Stream_Write_UINT8(s, 1); /* regionFlags (1 byte) */ |
2142 | 0 | Stream_Write_UINT16(s, message->numRects); /* numRects (2 bytes) */ |
2143 | | |
2144 | 0 | for (UINT16 i = 0; i < message->numRects; i++) |
2145 | 0 | { |
2146 | 0 | const RFX_RECT* rect = rfx_message_get_rect_const(message, i); |
2147 | 0 | WINPR_ASSERT(rect); |
2148 | | |
2149 | | /* Clipping rectangles are relative to destLeft, destTop */ |
2150 | 0 | Stream_Write_UINT16(s, rect->x); /* x (2 bytes) */ |
2151 | 0 | Stream_Write_UINT16(s, rect->y); /* y (2 bytes) */ |
2152 | 0 | Stream_Write_UINT16(s, rect->width); /* width (2 bytes) */ |
2153 | 0 | Stream_Write_UINT16(s, rect->height); /* height (2 bytes) */ |
2154 | 0 | } |
2155 | | |
2156 | 0 | Stream_Write_UINT16(s, CBT_REGION); /* regionType (2 bytes) */ |
2157 | 0 | Stream_Write_UINT16(s, 1); /* numTilesets (2 bytes) */ |
2158 | 0 | return TRUE; |
2159 | 0 | } |
2160 | | |
2161 | | static INLINE BOOL rfx_write_message_frame_end( |
2162 | | WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s, |
2163 | | WINPR_ATTR_UNUSED const RFX_MESSAGE* WINPR_RESTRICT message) |
2164 | 0 | { |
2165 | 0 | WINPR_ASSERT(context); |
2166 | 0 | WINPR_ASSERT(message); |
2167 | | |
2168 | 0 | if (!Stream_EnsureRemainingCapacity(s, 8)) |
2169 | 0 | return FALSE; |
2170 | | |
2171 | 0 | Stream_Write_UINT16(s, WBT_FRAME_END); /* CodecChannelT.blockType */ |
2172 | 0 | Stream_Write_UINT32(s, 8); /* CodecChannelT.blockLen */ |
2173 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */ |
2174 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */ |
2175 | 0 | return TRUE; |
2176 | 0 | } |
2177 | | |
2178 | | BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s, |
2179 | | const RFX_MESSAGE* WINPR_RESTRICT message) |
2180 | 0 | { |
2181 | 0 | WINPR_ASSERT(context); |
2182 | 0 | WINPR_ASSERT(message); |
2183 | | |
2184 | 0 | if (context->state == RFX_STATE_SEND_HEADERS) |
2185 | 0 | { |
2186 | 0 | if (!rfx_compose_message_header(context, s)) |
2187 | 0 | return FALSE; |
2188 | | |
2189 | 0 | context->state = RFX_STATE_SEND_FRAME_DATA; |
2190 | 0 | } |
2191 | | |
2192 | 0 | if (!rfx_write_message_frame_begin(context, s, message) || |
2193 | 0 | !rfx_write_message_region(context, s, message) || |
2194 | 0 | !rfx_write_message_tileset(context, s, message) || |
2195 | 0 | !rfx_write_message_frame_end(context, s, message)) |
2196 | 0 | { |
2197 | 0 | return FALSE; |
2198 | 0 | } |
2199 | | |
2200 | 0 | return TRUE; |
2201 | 0 | } |
2202 | | |
2203 | | BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s, |
2204 | | const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
2205 | | const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height, |
2206 | | UINT32 scanline) |
2207 | 0 | { |
2208 | 0 | WINPR_ASSERT(context); |
2209 | 0 | RFX_MESSAGE* message = |
2210 | 0 | rfx_encode_message(context, rects, numRects, data, width, height, scanline); |
2211 | 0 | if (!message) |
2212 | 0 | return FALSE; |
2213 | | |
2214 | 0 | const BOOL ret = rfx_write_message(context, s, message); |
2215 | 0 | rfx_message_free(context, message); |
2216 | 0 | return ret; |
2217 | 0 | } |
2218 | | |
2219 | | BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode) |
2220 | 0 | { |
2221 | 0 | WINPR_ASSERT(context); |
2222 | 0 | context->mode = mode; |
2223 | 0 | return TRUE; |
2224 | 0 | } |
2225 | | |
2226 | | RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context) |
2227 | 0 | { |
2228 | 0 | WINPR_ASSERT(context); |
2229 | 0 | return context->mode; |
2230 | 0 | } |
2231 | | |
2232 | | UINT32 rfx_context_get_frame_idx(const RFX_CONTEXT* WINPR_RESTRICT context) |
2233 | 0 | { |
2234 | 0 | WINPR_ASSERT(context); |
2235 | 0 | return context->frameIdx; |
2236 | 0 | } |
2237 | | |
2238 | | UINT32 rfx_message_get_frame_idx(const RFX_MESSAGE* WINPR_RESTRICT message) |
2239 | 0 | { |
2240 | 0 | WINPR_ASSERT(message); |
2241 | 0 | return message->frameIdx; |
2242 | 0 | } |
2243 | | |
2244 | | static INLINE BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2245 | | wStream* WINPR_RESTRICT s) |
2246 | 0 | { |
2247 | 0 | const UINT32 blockLen = 12; |
2248 | 0 | WINPR_ASSERT(rfx); |
2249 | 0 | WINPR_ASSERT(s); |
2250 | | |
2251 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2252 | 0 | return FALSE; |
2253 | | |
2254 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC); /* blockType (2 bytes) */ |
2255 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2256 | 0 | Stream_Write_UINT32(s, 0xCACCACCA); /* magic (4 bytes) */ |
2257 | 0 | Stream_Write_UINT16(s, 0x0100); /* version (2 bytes) */ |
2258 | 0 | return TRUE; |
2259 | 0 | } |
2260 | | |
2261 | | static INLINE BOOL rfx_write_progressive_wb_context( |
2262 | | WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, wStream* WINPR_RESTRICT s) |
2263 | 0 | { |
2264 | 0 | const UINT32 blockLen = 10; |
2265 | 0 | WINPR_ASSERT(rfx); |
2266 | 0 | WINPR_ASSERT(s); |
2267 | | |
2268 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2269 | 0 | return FALSE; |
2270 | | |
2271 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT); /* blockType (2 bytes) */ |
2272 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2273 | 0 | Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */ |
2274 | 0 | Stream_Write_UINT16(s, 64); /* tileSize (2 bytes) */ |
2275 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2276 | 0 | return TRUE; |
2277 | 0 | } |
2278 | | |
2279 | | static INLINE BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx, |
2280 | | wStream* WINPR_RESTRICT s, |
2281 | | const RFX_MESSAGE* WINPR_RESTRICT msg) |
2282 | 0 | { |
2283 | | /* RFX_REGION */ |
2284 | 0 | UINT32 blockLen = 18; |
2285 | 0 | UINT32 tilesDataSize = 0; |
2286 | 0 | const size_t start = Stream_GetPosition(s); |
2287 | |
|
2288 | 0 | WINPR_ASSERT(rfx); |
2289 | 0 | WINPR_ASSERT(s); |
2290 | 0 | WINPR_ASSERT(msg); |
2291 | | |
2292 | 0 | blockLen += msg->numRects * 8; |
2293 | 0 | blockLen += msg->numQuant * 5; |
2294 | 0 | tilesDataSize = msg->numTiles * 22UL; |
2295 | 0 | for (UINT16 i = 0; i < msg->numTiles; i++) |
2296 | 0 | { |
2297 | 0 | const RFX_TILE* tile = msg->tiles[i]; |
2298 | 0 | WINPR_ASSERT(tile); |
2299 | 0 | tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen; |
2300 | 0 | } |
2301 | 0 | blockLen += tilesDataSize; |
2302 | |
|
2303 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2304 | 0 | return FALSE; |
2305 | | |
2306 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION); /* blockType (2 bytes) */ |
2307 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2308 | 0 | Stream_Write_UINT8(s, 64); /* tileSize (1 byte) */ |
2309 | 0 | Stream_Write_UINT16(s, msg->numRects); /* numRects (2 bytes) */ |
2310 | 0 | WINPR_ASSERT(msg->numQuant <= UINT8_MAX); |
2311 | 0 | Stream_Write_UINT8(s, (UINT8)msg->numQuant); /* numQuant (1 byte) */ |
2312 | 0 | Stream_Write_UINT8(s, 0); /* numProgQuant (1 byte) */ |
2313 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2314 | 0 | Stream_Write_UINT16(s, msg->numTiles); /* numTiles (2 bytes) */ |
2315 | 0 | Stream_Write_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */ |
2316 | | |
2317 | 0 | for (UINT16 i = 0; i < msg->numRects; i++) |
2318 | 0 | { |
2319 | | /* TS_RFX_RECT */ |
2320 | 0 | const RFX_RECT* r = &msg->rects[i]; |
2321 | 0 | Stream_Write_UINT16(s, r->x); /* x (2 bytes) */ |
2322 | 0 | Stream_Write_UINT16(s, r->y); /* y (2 bytes) */ |
2323 | 0 | Stream_Write_UINT16(s, r->width); /* width (2 bytes) */ |
2324 | 0 | Stream_Write_UINT16(s, r->height); /* height (2 bytes) */ |
2325 | 0 | } |
2326 | | |
2327 | | /** |
2328 | | * Note: The RFX_COMPONENT_CODEC_QUANT structure differs from the |
2329 | | * TS_RFX_CODEC_QUANT ([MS-RDPRFX] section 2.2.2.1.5) structure with respect |
2330 | | * to the order of the bands. |
2331 | | * 0 1 2 3 4 5 6 7 8 9 |
2332 | | * RDPRFX: LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1 |
2333 | | * RDPEGFX: LL3, HL3, LH3, HH3, HL2, LH2, HH2, HL1, LH1, HH1 |
2334 | | */ |
2335 | 0 | for (UINT16 i = 0; i < msg->numQuant; i++) |
2336 | 0 | { |
2337 | 0 | const UINT32* qv = &msg->quantVals[10ULL * i]; |
2338 | | /* RFX_COMPONENT_CODEC_QUANT */ |
2339 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4))); /* LL3 (4-bit), HL3 (4-bit) */ |
2340 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4))); /* LH3 (4-bit), HH3 (4-bit) */ |
2341 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4))); /* HL2 (4-bit), LH2 (4-bit) */ |
2342 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4))); /* HH2 (4-bit), HL1 (4-bit) */ |
2343 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4))); /* LH1 (4-bit), HH1 (4-bit) */ |
2344 | 0 | } |
2345 | | |
2346 | 0 | for (UINT16 i = 0; i < msg->numTiles; i++) |
2347 | 0 | { |
2348 | 0 | const RFX_TILE* tile = msg->tiles[i]; |
2349 | 0 | if (!rfx_write_progressive_tile_simple(rfx, s, tile)) |
2350 | 0 | return FALSE; |
2351 | 0 | } |
2352 | | |
2353 | 0 | const size_t end = Stream_GetPosition(s); |
2354 | 0 | const size_t used = end - start; |
2355 | 0 | return (used == blockLen); |
2356 | 0 | } |
2357 | | |
2358 | | static INLINE BOOL |
2359 | | rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2360 | | wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT msg) |
2361 | 0 | { |
2362 | 0 | const UINT32 blockLen = 12; |
2363 | 0 | WINPR_ASSERT(rfx); |
2364 | 0 | WINPR_ASSERT(s); |
2365 | 0 | WINPR_ASSERT(msg); |
2366 | | |
2367 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2368 | 0 | return FALSE; |
2369 | | |
2370 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN); /* blockType (2 bytes) */ |
2371 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2372 | 0 | Stream_Write_UINT32(s, msg->frameIdx); /* frameIndex (4 bytes) */ |
2373 | 0 | Stream_Write_UINT16(s, 1); /* regionCount (2 bytes) */ |
2374 | | |
2375 | 0 | return TRUE; |
2376 | 0 | } |
2377 | | |
2378 | | static INLINE BOOL rfx_write_progressive_frame_end( |
2379 | | WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, wStream* WINPR_RESTRICT s) |
2380 | 0 | { |
2381 | 0 | const UINT32 blockLen = 6; |
2382 | 0 | WINPR_ASSERT(rfx); |
2383 | 0 | WINPR_ASSERT(s); |
2384 | | |
2385 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2386 | 0 | return FALSE; |
2387 | | |
2388 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END); /* blockType (2 bytes) */ |
2389 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2390 | | |
2391 | 0 | return TRUE; |
2392 | 0 | } |
2393 | | |
2394 | | static INLINE BOOL |
2395 | | rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2396 | | wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile) |
2397 | 0 | { |
2398 | 0 | UINT32 blockLen = 0; |
2399 | 0 | WINPR_ASSERT(rfx); |
2400 | 0 | WINPR_ASSERT(s); |
2401 | 0 | WINPR_ASSERT(tile); |
2402 | | |
2403 | 0 | blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen; |
2404 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2405 | 0 | return FALSE; |
2406 | | |
2407 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE); /* blockType (2 bytes) */ |
2408 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2409 | 0 | Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */ |
2410 | 0 | Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
2411 | 0 | Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
2412 | 0 | Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */ |
2413 | 0 | Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */ |
2414 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2415 | 0 | Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */ |
2416 | 0 | Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */ |
2417 | 0 | Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */ |
2418 | 0 | Stream_Write_UINT16(s, 0); /* tailLen (2 bytes) */ |
2419 | 0 | Stream_Write(s, tile->YData, tile->YLen); /* YData */ |
2420 | 0 | Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */ |
2421 | 0 | Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */ |
2422 | |
|
2423 | 0 | return TRUE; |
2424 | 0 | } |
2425 | | |
2426 | | const char* rfx_get_progressive_block_type_string(UINT16 blockType) |
2427 | 0 | { |
2428 | 0 | switch (blockType) |
2429 | 0 | { |
2430 | 0 | case PROGRESSIVE_WBT_SYNC: |
2431 | 0 | return "PROGRESSIVE_WBT_SYNC"; |
2432 | | |
2433 | 0 | case PROGRESSIVE_WBT_FRAME_BEGIN: |
2434 | 0 | return "PROGRESSIVE_WBT_FRAME_BEGIN"; |
2435 | | |
2436 | 0 | case PROGRESSIVE_WBT_FRAME_END: |
2437 | 0 | return "PROGRESSIVE_WBT_FRAME_END"; |
2438 | | |
2439 | 0 | case PROGRESSIVE_WBT_CONTEXT: |
2440 | 0 | return "PROGRESSIVE_WBT_CONTEXT"; |
2441 | | |
2442 | 0 | case PROGRESSIVE_WBT_REGION: |
2443 | 0 | return "PROGRESSIVE_WBT_REGION"; |
2444 | | |
2445 | 0 | case PROGRESSIVE_WBT_TILE_SIMPLE: |
2446 | 0 | return "PROGRESSIVE_WBT_TILE_SIMPLE"; |
2447 | | |
2448 | 0 | case PROGRESSIVE_WBT_TILE_FIRST: |
2449 | 0 | return "PROGRESSIVE_WBT_TILE_FIRST"; |
2450 | | |
2451 | 0 | case PROGRESSIVE_WBT_TILE_UPGRADE: |
2452 | 0 | return "PROGRESSIVE_WBT_TILE_UPGRADE"; |
2453 | | |
2454 | 0 | default: |
2455 | 0 | return "PROGRESSIVE_WBT_UNKNOWN"; |
2456 | 0 | } |
2457 | 0 | } |
2458 | | |
2459 | | BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT context, |
2460 | | wStream* WINPR_RESTRICT s, |
2461 | | const RFX_MESSAGE* WINPR_RESTRICT msg) |
2462 | 0 | { |
2463 | 0 | WINPR_ASSERT(s); |
2464 | 0 | WINPR_ASSERT(msg); |
2465 | 0 | WINPR_ASSERT(context); |
2466 | | |
2467 | 0 | if (context->mode != RLGR1) |
2468 | 0 | { |
2469 | 0 | WLog_ERR(TAG, "error, RLGR1 mode is required!"); |
2470 | 0 | return FALSE; |
2471 | 0 | } |
2472 | | |
2473 | 0 | if (!rfx_write_progressive_wb_sync(context, s)) |
2474 | 0 | return FALSE; |
2475 | | |
2476 | 0 | if (!rfx_write_progressive_wb_context(context, s)) |
2477 | 0 | return FALSE; |
2478 | | |
2479 | 0 | if (!rfx_write_progressive_frame_begin(context, s, msg)) |
2480 | 0 | return FALSE; |
2481 | | |
2482 | 0 | if (!rfx_write_progressive_region(context, s, msg)) |
2483 | 0 | return FALSE; |
2484 | | |
2485 | 0 | if (!rfx_write_progressive_frame_end(context, s)) |
2486 | 0 | return FALSE; |
2487 | | |
2488 | 0 | return TRUE; |
2489 | 0 | } |