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