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 const UINT32 rw = 1UL * clippingRect.left + rect->width;
1312 const UINT32 rh = 1UL * clippingRect.top + rect->width;
1313 const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth));
1314 const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight));
1315 clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth));
1316 clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight));
1317 clippingRect.right = right;
1318 clippingRect.bottom = bottom;
1319 region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
1320 }
1321
1322 for (UINT32 i = 0; i < message->numTiles; i++)
1323 {
1324 RECTANGLE_16 updateRect = { 0 };
1325 const RFX_TILE* tile = rfx_message_get_tile(message, i);
1326
1327 WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1328 WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1329
1330 updateRect.left = (UINT16)left + tile->x;
1331 updateRect.top = (UINT16)top + tile->y;
1332 updateRect.right = updateRect.left + 64;
1333 updateRect.bottom = updateRect.top + 64;
1334 region16_init(&updateRegion);
1335 region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
1336 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1337
1338 for (UINT32 j = 0; j < nbUpdateRects; j++)
1339 {
1340 const UINT32 stride = 64 * formatSize;
1341 const UINT32 nXDst = updateRects[j].left;
1342 const UINT32 nYDst = updateRects[j].top;
1343 const UINT32 nXSrc = nXDst - updateRect.left;
1344 const UINT32 nYSrc = nYDst - updateRect.top;
1345 const UINT32 nWidth = updateRects[j].right - updateRects[j].left;
1346 const UINT32 nHeight = updateRects[j].bottom - updateRects[j].top;
1347
1348 if (!freerdp_image_copy_no_overlap(dst, dstFormat, dstStride, nXDst, nYDst, nWidth,
1349 nHeight, tile->data, context->pixel_format,
1350 stride, nXSrc, nYSrc, NULL, FREERDP_FLIP_NONE))
1351 {
1352 region16_uninit(&updateRegion);
1353 region16_uninit(&clippingRects);
1354 WLog_Print(context->priv->log, WLOG_ERROR,
1355 "nbUpdateRectx[%" PRIu32 " (%" PRIu32 ")] freerdp_image_copy failed",
1356 j, nbUpdateRects);
1357 return FALSE;
1358 }
1359
1360 if (invalidRegion)
1361 region16_union_rect(invalidRegion, invalidRegion, &updateRects[j]);
1362 }
1363
1364 region16_uninit(&updateRegion);
1365 }
1366
1367 region16_uninit(&clippingRects);
1368 return TRUE;
1369 }
1370 else
1371 {
1372 rfx_message_free(context, message);
1373 context->currentMessage.freeArray = TRUE;
1374 }
1375
1376 WLog_Print(context->priv->log, WLOG_ERROR, "failed");
1377 return FALSE;
1378}
1379
1380const UINT32* rfx_message_get_quants(const RFX_MESSAGE* WINPR_RESTRICT message,
1381 UINT16* WINPR_RESTRICT numQuantVals)
1382{
1383 WINPR_ASSERT(message);
1384 if (numQuantVals)
1385 *numQuantVals = message->numQuant;
1386 return message->quantVals;
1387}
1388
1389const RFX_TILE** rfx_message_get_tiles(const RFX_MESSAGE* WINPR_RESTRICT message,
1390 UINT16* WINPR_RESTRICT numTiles)
1391{
1392 WINPR_ASSERT(message);
1393 if (numTiles)
1394 *numTiles = message->numTiles;
1395
1396 union
1397 {
1398 RFX_TILE** pp;
1399 const RFX_TILE** ppc;
1400 } cnv;
1401 cnv.pp = message->tiles;
1402 return cnv.ppc;
1403}
1404
1405UINT16 rfx_message_get_tile_count(const RFX_MESSAGE* WINPR_RESTRICT message)
1406{
1407 WINPR_ASSERT(message);
1408 return message->numTiles;
1409}
1410
1411const RFX_RECT* rfx_message_get_rects(const RFX_MESSAGE* WINPR_RESTRICT message,
1412 UINT16* WINPR_RESTRICT numRects)
1413{
1414 WINPR_ASSERT(message);
1415 if (numRects)
1416 *numRects = message->numRects;
1417 return message->rects;
1418}
1419
1420UINT16 rfx_message_get_rect_count(const RFX_MESSAGE* WINPR_RESTRICT message)
1421{
1422 WINPR_ASSERT(message);
1423 return message->numRects;
1424}
1425
1426void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1427{
1428 if (!message)
1429 return;
1430
1431 winpr_aligned_free(message->rects);
1432
1433 if (message->tiles)
1434 {
1435 for (size_t i = 0; i < message->numTiles; i++)
1436 {
1437 RFX_TILE* tile = message->tiles[i];
1438 if (!tile)
1439 continue;
1440
1441 if (tile->YCbCrData)
1442 {
1443 BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1444 tile->YCbCrData = NULL;
1445 }
1446
1447 ObjectPool_Return(context->priv->TilePool, (void*)tile);
1448 }
1449
1450 rfx_allocate_tiles(message, 0, FALSE);
1451 }
1452
1453 const BOOL freeArray = message->freeArray;
1454 const RFX_MESSAGE empty = { 0 };
1455 *message = empty;
1456
1457 if (!freeArray)
1458 winpr_aligned_free(message);
1459}
1460
1461static INLINE void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1462{
1463 UINT16 properties = 0;
1464
1465 WINPR_ASSERT(context);
1466 /* properties in tilesets: note that this has different format from the one in TS_RFX_CONTEXT */
1467 properties = 1; /* lt */
1468 properties |= (context->flags << 1); /* flags */
1469 properties |= (COL_CONV_ICT << 4); /* cct */
1470 properties |= (CLW_XFORM_DWT_53_A << 6); /* xft */
1471 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10); /* et */
1472 properties |= (SCALAR_QUANTIZATION << 14); /* qt */
1473 context->properties = properties;
1474}
1475
1476static INLINE void
1477rfx_write_message_sync(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context,
1478 WINPR_ATTR_UNUSED wStream* WINPR_RESTRICT s)
1479{
1480 WINPR_ASSERT(context);
1481
1482 Stream_Write_UINT16(s, WBT_SYNC); /* BlockT.blockType (2 bytes) */
1483 Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */
1484 Stream_Write_UINT32(s, WF_MAGIC); /* magic (4 bytes) */
1485 Stream_Write_UINT16(s, WF_VERSION_1_0); /* version (2 bytes) */
1486}
1487
1488static INLINE void
1489rfx_write_message_codec_versions(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context,
1490 wStream* WINPR_RESTRICT s)
1491{
1492 WINPR_ASSERT(context);
1493
1494 Stream_Write_UINT16(s, WBT_CODEC_VERSIONS); /* BlockT.blockType (2 bytes) */
1495 Stream_Write_UINT32(s, 10); /* BlockT.blockLen (4 bytes) */
1496 Stream_Write_UINT8(s, 1); /* numCodecs (1 byte) */
1497 Stream_Write_UINT8(s, 1); /* codecs.codecId (1 byte) */
1498 Stream_Write_UINT16(s, WF_VERSION_1_0); /* codecs.version (2 bytes) */
1499}
1500
1501static INLINE void rfx_write_message_channels(const RFX_CONTEXT* WINPR_RESTRICT context,
1502 wStream* WINPR_RESTRICT s)
1503{
1504 WINPR_ASSERT(context);
1505
1506 Stream_Write_UINT16(s, WBT_CHANNELS); /* BlockT.blockType (2 bytes) */
1507 Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */
1508 Stream_Write_UINT8(s, 1); /* numChannels (1 byte) */
1509 Stream_Write_UINT8(s, 0); /* Channel.channelId (1 byte) */
1510 Stream_Write_UINT16(s, context->width); /* Channel.width (2 bytes) */
1511 Stream_Write_UINT16(s, context->height); /* Channel.height (2 bytes) */
1512}
1513
1514static INLINE void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1515 wStream* WINPR_RESTRICT s)
1516{
1517 UINT16 properties = 0;
1518 WINPR_ASSERT(context);
1519
1520 Stream_Write_UINT16(s, WBT_CONTEXT); /* CodecChannelT.blockType (2 bytes) */
1521 Stream_Write_UINT32(s, 13); /* CodecChannelT.blockLen (4 bytes) */
1522 Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */
1523 Stream_Write_UINT8(s, 0xFF); /* CodecChannelT.channelId (1 byte) */
1524 Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */
1525 Stream_Write_UINT16(s, CT_TILE_64x64); /* tileSize (2 bytes) */
1526 /* properties */
1527 properties = context->flags; /* flags */
1528 properties |= (COL_CONV_ICT << 3); /* cct */
1529 properties |= (CLW_XFORM_DWT_53_A << 5); /* xft */
1530 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9); /* et */
1531 properties |= (SCALAR_QUANTIZATION << 13); /* qt */
1532 Stream_Write_UINT16(s, properties); /* properties (2 bytes) */
1533 rfx_update_context_properties(context);
1534}
1535
1536static INLINE BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1537 wStream* WINPR_RESTRICT s)
1538{
1539 WINPR_ASSERT(context);
1540 if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
1541 return FALSE;
1542
1543 rfx_write_message_sync(context, s);
1544 rfx_write_message_context(context, s);
1545 rfx_write_message_codec_versions(context, s);
1546 rfx_write_message_channels(context, s);
1547 return TRUE;
1548}
1549
1550static INLINE size_t rfx_tile_length(const RFX_TILE* WINPR_RESTRICT tile)
1551{
1552 WINPR_ASSERT(tile);
1553 return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1554}
1555
1556static INLINE BOOL rfx_write_tile(wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile)
1557{
1558 const size_t blockLen = rfx_tile_length(tile);
1559 if (blockLen > UINT32_MAX)
1560 return FALSE;
1561 if (!Stream_EnsureRemainingCapacity(s, blockLen))
1562 return FALSE;
1563
1564 Stream_Write_UINT16(s, CBT_TILE); /* BlockT.blockType (2 bytes) */
1565 Stream_Write_UINT32(s, (UINT32)blockLen); /* BlockT.blockLen (4 bytes) */
1566 Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */
1567 Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */
1568 Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */
1569 Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */
1570 Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */
1571 Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */
1572 Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */
1573 Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */
1574 Stream_Write(s, tile->YData, tile->YLen); /* YData */
1575 Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */
1576 Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */
1577 return TRUE;
1578}
1579
1580struct S_RFX_TILE_COMPOSE_WORK_PARAM
1581{
1582 RFX_TILE* tile;
1583 RFX_CONTEXT* context;
1584};
1585
1586static INLINE void CALLBACK
1587rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
1588 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
1589{
1590 RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1591 WINPR_ASSERT(param);
1592 rfx_encode_rgb(param->context, param->tile);
1593}
1594
1595static INLINE BOOL computeRegion(const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
1596 REGION16* WINPR_RESTRICT region, size_t width, size_t height)
1597{
1598 const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1599 WINPR_ASSERTING_INT_CAST(UINT16, height) };
1600
1601 WINPR_ASSERT(rects);
1602 for (size_t i = 0; i < numRects; i++)
1603 {
1604 const RFX_RECT* rect = &rects[i];
1605 RECTANGLE_16 rect16 = { 0 };
1606 rect16.left = rect->x;
1607 rect16.top = rect->y;
1608 rect16.right = rect->x + rect->width;
1609 rect16.bottom = rect->y + rect->height;
1610
1611 if (!region16_union_rect(region, region, &rect16))
1612 return FALSE;
1613 }
1614
1615 return region16_intersect_rect(region, region, &mainRect);
1616}
1617
1618#define TILE_NO(v) ((v) / 64)
1619
1620static INLINE BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context, size_t nbTiles)
1621{
1622 WINPR_ASSERT(context);
1623
1624 RFX_CONTEXT_PRIV* priv = context->priv;
1625 WINPR_ASSERT(priv);
1626
1627 void* pmem = NULL;
1628
1629 if (!context->priv->UseThreads)
1630 return TRUE;
1631
1632 if (!(pmem = winpr_aligned_recalloc((void*)priv->workObjects, nbTiles, sizeof(PTP_WORK), 32)))
1633 return FALSE;
1634
1635 priv->workObjects = (PTP_WORK*)pmem;
1636
1637 if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1638 sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1639 return FALSE;
1640
1641 priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1642 return TRUE;
1643}
1644
1645static INLINE BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message, size_t count)
1646{
1647 WINPR_ASSERT(message);
1648
1649 if (message->numTiles + count <= message->allocatedTiles)
1650 return TRUE;
1651
1652 const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1653 return rfx_allocate_tiles(message, alloc, TRUE);
1654}
1655
1656RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1657 const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
1658 const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h, size_t s)
1659{
1660 const UINT32 width = w;
1661 const UINT32 height = h;
1662 const UINT32 scanline = (UINT32)s;
1663 RFX_MESSAGE* message = NULL;
1664 PTP_WORK* workObject = NULL;
1665 RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL;
1666 BOOL success = FALSE;
1667 REGION16 rectsRegion = { 0 };
1668 REGION16 tilesRegion = { 0 };
1669 RECTANGLE_16 currentTileRect = { 0 };
1670 const RECTANGLE_16* regionRect = NULL;
1671
1672 WINPR_ASSERT(data);
1673 WINPR_ASSERT(rects);
1674 WINPR_ASSERT(numRects > 0);
1675 WINPR_ASSERT(w > 0);
1676 WINPR_ASSERT(h > 0);
1677 WINPR_ASSERT(s > 0);
1678
1679 if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1, sizeof(RFX_MESSAGE), 32)))
1680 return NULL;
1681
1682 region16_init(&tilesRegion);
1683 region16_init(&rectsRegion);
1684
1685 if (context->state == RFX_STATE_SEND_HEADERS)
1686 rfx_update_context_properties(context);
1687
1688 message->frameIdx = context->frameIdx++;
1689
1690 if (!context->numQuant)
1691 {
1692 WINPR_ASSERT(context->quants == NULL);
1693 if (!(context->quants =
1694 (UINT32*)winpr_aligned_malloc(sizeof(rfx_default_quantization_values), 32)))
1695 goto skip_encoding_loop;
1696
1697 CopyMemory(context->quants, &rfx_default_quantization_values,
1698 sizeof(rfx_default_quantization_values));
1699 context->numQuant = 1;
1700 context->quantIdxY = 0;
1701 context->quantIdxCb = 0;
1702 context->quantIdxCr = 0;
1703 }
1704
1705 message->numQuant = context->numQuant;
1706 message->quantVals = context->quants;
1707 const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1708
1709 if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1710 goto skip_encoding_loop;
1711
1712 const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1713 WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1714 WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1715 const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1716 const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1717 const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1718
1719 if (!rfx_ensure_tiles(message, maxNbTiles))
1720 goto skip_encoding_loop;
1721
1722 if (!setupWorkers(context, maxNbTiles))
1723 goto skip_encoding_loop;
1724
1725 if (context->priv->UseThreads)
1726 {
1727 workObject = context->priv->workObjects;
1728 workParam = context->priv->tileWorkParams;
1729 }
1730
1731 UINT32 regionNbRects = 0;
1732 regionRect = region16_rects(&rectsRegion, &regionNbRects);
1733
1734 if (!(message->rects = winpr_aligned_calloc(regionNbRects, sizeof(RFX_RECT), 32)))
1735 goto skip_encoding_loop;
1736
1737 message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1738
1739 for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1740 {
1741 RFX_RECT* rfxRect = &message->rects[i];
1742 UINT32 startTileX = regionRect->left / 64;
1743 UINT32 endTileX = (regionRect->right - 1) / 64;
1744 UINT32 startTileY = regionRect->top / 64;
1745 UINT32 endTileY = (regionRect->bottom - 1) / 64;
1746 rfxRect->x = regionRect->left;
1747 rfxRect->y = regionRect->top;
1748 rfxRect->width = (regionRect->right - regionRect->left);
1749 rfxRect->height = (regionRect->bottom - regionRect->top);
1750
1751 for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1752 yIdx++, gridRelY += 64)
1753 {
1754 UINT32 tileHeight = 64;
1755
1756 if ((yIdx == endTileY) && (gridRelY + 64 > height))
1757 tileHeight = height - gridRelY;
1758
1759 currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1760 currentTileRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1761
1762 for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1763 xIdx++, gridRelX += 64)
1764 {
1765 union
1766 {
1767 const BYTE* cpv;
1768 BYTE* pv;
1769 } cnv;
1770 UINT32 tileWidth = 64;
1771
1772 if ((xIdx == endTileX) && (gridRelX + 64 > width))
1773 {
1774 tileWidth = (width - gridRelX);
1775 }
1776
1777 currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1778 currentTileRect.right = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1779
1780 /* checks if this tile is already treated */
1781 if (region16_intersects_rect(&tilesRegion, &currentTileRect))
1782 continue;
1783
1784 RFX_TILE* tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool);
1785 if (!tile)
1786 goto skip_encoding_loop;
1787
1788 tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1789 tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1790 tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1791 tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1792 tile->scanline = scanline;
1793
1794 tile->width = tileWidth;
1795 tile->height = tileHeight;
1796 const UINT32 ax = gridRelX;
1797 const UINT32 ay = gridRelY;
1798
1799 if (tile->data && tile->allocated)
1800 {
1801 winpr_aligned_free(tile->data);
1802 tile->allocated = FALSE;
1803 }
1804
1805 /* Cast away const */
1806 cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1807 tile->data = cnv.pv;
1808 tile->quantIdxY = context->quantIdxY;
1809 tile->quantIdxCb = context->quantIdxCb;
1810 tile->quantIdxCr = context->quantIdxCr;
1811 tile->YLen = tile->CbLen = tile->CrLen = 0;
1812
1813 if (!(tile->YCbCrData = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1814 goto skip_encoding_loop;
1815
1816 tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1817 tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1818 tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1819
1820 if (!rfx_ensure_tiles(message, 1))
1821 goto skip_encoding_loop;
1822 message->tiles[message->numTiles++] = tile;
1823
1824 if (context->priv->UseThreads)
1825 {
1826 workParam->context = context;
1827 workParam->tile = tile;
1828
1829 if (!(*workObject = CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1830 (void*)workParam,
1831 &context->priv->ThreadPoolEnv)))
1832 {
1833 goto skip_encoding_loop;
1834 }
1835
1836 SubmitThreadpoolWork(*workObject);
1837 workObject++;
1838 workParam++;
1839 }
1840 else
1841 {
1842 rfx_encode_rgb(context, tile);
1843 }
1844
1845 if (!region16_union_rect(&tilesRegion, &tilesRegion, &currentTileRect))
1846 goto skip_encoding_loop;
1847 } /* xIdx */
1848 } /* yIdx */
1849 } /* rects */
1850
1851 success = TRUE;
1852skip_encoding_loop:
1853
1854 /* when using threads ensure all computations are done */
1855 if (success)
1856 {
1857 message->tilesDataSize = 0;
1858 workObject = context->priv->workObjects;
1859
1860 for (UINT32 i = 0; i < message->numTiles; i++)
1861 {
1862 if (context->priv->UseThreads)
1863 {
1864 if (*workObject)
1865 {
1866 WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1867 CloseThreadpoolWork(*workObject);
1868 }
1869
1870 workObject++;
1871 }
1872
1873 const RFX_TILE* tile = message->tiles[i];
1874 const size_t tlen = rfx_tile_length(tile);
1875 message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen);
1876 }
1877
1878 region16_uninit(&tilesRegion);
1879 region16_uninit(&rectsRegion);
1880
1881 return message;
1882 }
1883
1884 WLog_Print(context->priv->log, WLOG_ERROR, "failed");
1885
1886 rfx_message_free(context, message);
1887 region16_uninit(&tilesRegion);
1888 region16_uninit(&rectsRegion);
1889 return NULL;
1890}
1891
1892static INLINE BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1893 const RFX_MESSAGE* WINPR_RESTRICT src)
1894{
1895 WINPR_ASSERT(dst);
1896 WINPR_ASSERT(src);
1897
1898 WINPR_ASSERT(dst->rects == NULL);
1899 WINPR_ASSERT(dst->numRects == 0);
1900
1901 if (src->numRects == 0)
1902 return TRUE;
1903
1904 dst->rects = winpr_aligned_calloc(src->numRects, sizeof(RECTANGLE_16), 32);
1905 if (!dst->rects)
1906 return FALSE;
1907 dst->numRects = src->numRects;
1908 for (size_t x = 0; x < src->numRects; x++)
1909 {
1910 dst->rects[x] = src->rects[x];
1911 }
1912 return TRUE;
1913}
1914
1915static INLINE BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1916 const RFX_MESSAGE* WINPR_RESTRICT src)
1917{
1918 WINPR_ASSERT(dst);
1919 WINPR_ASSERT(src);
1920
1921 WINPR_ASSERT(dst->quantVals == NULL);
1922 WINPR_ASSERT(dst->numQuant == 0);
1923
1924 if (src->numQuant == 0)
1925 return TRUE;
1926
1927 /* quantVals are part of context */
1928 dst->quantVals = src->quantVals;
1929 dst->numQuant = src->numQuant;
1930
1931 return TRUE;
1932}
1933
1934static INLINE RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
1935 RFX_MESSAGE* WINPR_RESTRICT message,
1936 size_t* WINPR_RESTRICT numMessages, size_t maxDataSize)
1937{
1938 WINPR_ASSERT(context);
1939 WINPR_ASSERT(message);
1940 WINPR_ASSERT(numMessages);
1941
1942 maxDataSize -= 1024; /* reserve enough space for headers */
1943 *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
1944
1945 RFX_MESSAGE* messages =
1946 (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages), sizeof(RFX_MESSAGE), 32);
1947 if (!messages)
1948 return NULL;
1949
1950 UINT32 j = 0;
1951 for (UINT16 i = 0; i < message->numTiles; i++)
1952 {
1953 RFX_TILE* tile = message->tiles[i];
1954 RFX_MESSAGE* msg = &messages[j];
1955
1956 WINPR_ASSERT(tile);
1957 WINPR_ASSERT(msg);
1958
1959 const size_t tileDataSize = rfx_tile_length(tile);
1960
1961 if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
1962 j++;
1963
1964 if (msg->numTiles == 0)
1965 {
1966 msg->frameIdx = message->frameIdx + j;
1967 if (!rfx_clone_quants(msg, message))
1968 goto free_messages;
1969 if (!rfx_clone_rects(msg, message))
1970 goto free_messages;
1971 msg->freeArray = TRUE;
1972 if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
1973 goto free_messages;
1974 }
1975
1976 msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize);
1977
1978 WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
1979 msg->tiles[msg->numTiles++] = message->tiles[i];
1980 message->tiles[i] = NULL;
1981 }
1982
1983 *numMessages = j + 1ULL;
1984 context->frameIdx += j;
1985 message->numTiles = 0;
1986 return messages;
1987free_messages:
1988
1989 for (size_t i = 0; i < j; i++)
1990 rfx_allocate_tiles(&messages[i], 0, FALSE);
1991
1992 winpr_aligned_free(messages);
1993 return NULL;
1994}
1995
1996const RFX_MESSAGE* rfx_message_list_get(const RFX_MESSAGE_LIST* WINPR_RESTRICT messages, size_t idx)
1997{
1998 WINPR_ASSERT(messages);
1999 if (idx >= messages->count)
2000 return NULL;
2001 WINPR_ASSERT(messages->list);
2002 return &messages->list[idx];
2003}
2004
2005void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
2006{
2007 if (!messages)
2008 return;
2009 for (size_t x = 0; x < messages->count; x++)
2010 rfx_message_free(messages->context, &messages->list[x]);
2011 free(messages);
2012}
2013
2014static INLINE RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
2015 RFX_MESSAGE* WINPR_RESTRICT messages,
2016 size_t count)
2017{
2018 WINPR_ASSERT(context);
2019 RFX_MESSAGE_LIST* msg = calloc(1, sizeof(RFX_MESSAGE_LIST));
2020 WINPR_ASSERT(msg);
2021
2022 msg->context = context;
2023 msg->count = count;
2024 msg->list = messages;
2025 return msg;
2026}
2027
2028RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
2029 const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
2030 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2031 UINT32 scanline, size_t* WINPR_RESTRICT numMessages,
2032 size_t maxDataSize)
2033{
2034 WINPR_ASSERT(context);
2035 WINPR_ASSERT(numMessages);
2036
2037 RFX_MESSAGE* message =
2038 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2039 if (!message)
2040 return NULL;
2041
2042 RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2043 rfx_message_free(context, message);
2044 if (!list)
2045 return NULL;
2046
2047 return rfx_message_list_new(context, list, *numMessages);
2048}
2049
2050static INLINE BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2051 wStream* WINPR_RESTRICT s,
2052 const RFX_MESSAGE* WINPR_RESTRICT message)
2053{
2054 WINPR_ASSERT(context);
2055 WINPR_ASSERT(message);
2056
2057 const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2058
2059 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2060 return FALSE;
2061
2062 Stream_Write_UINT16(s, WBT_EXTENSION); /* CodecChannelT.blockType (2 bytes) */
2063 Stream_Write_UINT32(s, blockLen); /* set CodecChannelT.blockLen (4 bytes) */
2064 Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */
2065 Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */
2066 Stream_Write_UINT16(s, CBT_TILESET); /* subtype (2 bytes) */
2067 Stream_Write_UINT16(s, 0); /* idx (2 bytes) */
2068 Stream_Write_UINT16(s, context->properties); /* properties (2 bytes) */
2069 Stream_Write_UINT8(
2070 s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant)); /* numQuant (1 byte) */
2071 Stream_Write_UINT8(s, 0x40); /* tileSize (1 byte) */
2072 Stream_Write_UINT16(s, message->numTiles); /* numTiles (2 bytes) */
2073 Stream_Write_UINT32(s, message->tilesDataSize); /* tilesDataSize (4 bytes) */
2074
2075 UINT32* quantVals = message->quantVals;
2076 for (size_t i = 0; i < message->numQuant * 5ul; i++)
2077 {
2078 WINPR_ASSERT(quantVals);
2079 Stream_Write_UINT8(s,
2080 WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2081 quantVals += 2;
2082 }
2083
2084 for (size_t i = 0; i < message->numTiles; i++)
2085 {
2086 RFX_TILE* tile = message->tiles[i];
2087 if (!tile)
2088 return FALSE;
2089
2090 if (!rfx_write_tile(s, tile))
2091 return FALSE;
2092 }
2093
2094#ifdef WITH_DEBUG_RFX
2095 WLog_Print(context->priv->log, WLOG_DEBUG,
2096 "numQuant: %" PRIu16 " numTiles: %" PRIu16 " tilesDataSize: %" PRIu32 "",
2097 message->numQuant, message->numTiles, message->tilesDataSize);
2098#endif
2099 return TRUE;
2100}
2101
2102static INLINE BOOL
2103rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2104 wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT message)
2105{
2106 WINPR_ASSERT(context);
2107 WINPR_ASSERT(message);
2108
2109 if (!Stream_EnsureRemainingCapacity(s, 14))
2110 return FALSE;
2111
2112 Stream_Write_UINT16(s, WBT_FRAME_BEGIN); /* CodecChannelT.blockType */
2113 Stream_Write_UINT32(s, 14); /* CodecChannelT.blockLen */
2114 Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */
2115 Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */
2116 Stream_Write_UINT32(s, message->frameIdx); /* frameIdx */
2117 Stream_Write_UINT16(s, 1); /* numRegions */
2118 return TRUE;
2119}
2120
2121static INLINE BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2122 wStream* WINPR_RESTRICT s,
2123 const RFX_MESSAGE* WINPR_RESTRICT message)
2124{
2125 WINPR_ASSERT(context);
2126 WINPR_ASSERT(message);
2127
2128 const size_t blockLen = 15 + (message->numRects * 8);
2129 if (blockLen > UINT32_MAX)
2130 return FALSE;
2131
2132 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2133 return FALSE;
2134
2135 Stream_Write_UINT16(s, WBT_REGION); /* CodecChannelT.blockType (2 bytes) */
2136 Stream_Write_UINT32(s, (UINT32)blockLen); /* set CodecChannelT.blockLen (4 bytes) */
2137 Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */
2138 Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */
2139 Stream_Write_UINT8(s, 1); /* regionFlags (1 byte) */
2140 Stream_Write_UINT16(s, message->numRects); /* numRects (2 bytes) */
2141
2142 for (UINT16 i = 0; i < message->numRects; i++)
2143 {
2144 const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2145 WINPR_ASSERT(rect);
2146
2147 /* Clipping rectangles are relative to destLeft, destTop */
2148 Stream_Write_UINT16(s, rect->x); /* x (2 bytes) */
2149 Stream_Write_UINT16(s, rect->y); /* y (2 bytes) */
2150 Stream_Write_UINT16(s, rect->width); /* width (2 bytes) */
2151 Stream_Write_UINT16(s, rect->height); /* height (2 bytes) */
2152 }
2153
2154 Stream_Write_UINT16(s, CBT_REGION); /* regionType (2 bytes) */
2155 Stream_Write_UINT16(s, 1); /* numTilesets (2 bytes) */
2156 return TRUE;
2157}
2158
2159static INLINE BOOL rfx_write_message_frame_end(
2160 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
2161 WINPR_ATTR_UNUSED const RFX_MESSAGE* WINPR_RESTRICT message)
2162{
2163 WINPR_ASSERT(context);
2164 WINPR_ASSERT(message);
2165
2166 if (!Stream_EnsureRemainingCapacity(s, 8))
2167 return FALSE;
2168
2169 Stream_Write_UINT16(s, WBT_FRAME_END); /* CodecChannelT.blockType */
2170 Stream_Write_UINT32(s, 8); /* CodecChannelT.blockLen */
2171 Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */
2172 Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */
2173 return TRUE;
2174}
2175
2176BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
2177 const RFX_MESSAGE* WINPR_RESTRICT message)
2178{
2179 WINPR_ASSERT(context);
2180 WINPR_ASSERT(message);
2181
2182 if (context->state == RFX_STATE_SEND_HEADERS)
2183 {
2184 if (!rfx_compose_message_header(context, s))
2185 return FALSE;
2186
2187 context->state = RFX_STATE_SEND_FRAME_DATA;
2188 }
2189
2190 if (!rfx_write_message_frame_begin(context, s, message) ||
2191 !rfx_write_message_region(context, s, message) ||
2192 !rfx_write_message_tileset(context, s, message) ||
2193 !rfx_write_message_frame_end(context, s, message))
2194 {
2195 return FALSE;
2196 }
2197
2198 return TRUE;
2199}
2200
2201BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
2202 const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
2203 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2204 UINT32 scanline)
2205{
2206 WINPR_ASSERT(context);
2207 RFX_MESSAGE* message =
2208 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2209 if (!message)
2210 return FALSE;
2211
2212 const BOOL ret = rfx_write_message(context, s, message);
2213 rfx_message_free(context, message);
2214 return ret;
2215}
2216
2217BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2218{
2219 WINPR_ASSERT(context);
2220 context->mode = mode;
2221 return TRUE;
2222}
2223
2224RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2225{
2226 WINPR_ASSERT(context);
2227 return context->mode;
2228}
2229
2230UINT32 rfx_context_get_frame_idx(const RFX_CONTEXT* WINPR_RESTRICT context)
2231{
2232 WINPR_ASSERT(context);
2233 return context->frameIdx;
2234}
2235
2236UINT32 rfx_message_get_frame_idx(const RFX_MESSAGE* WINPR_RESTRICT message)
2237{
2238 WINPR_ASSERT(message);
2239 return message->frameIdx;
2240}
2241
2242static INLINE BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2243 wStream* WINPR_RESTRICT s)
2244{
2245 const UINT32 blockLen = 12;
2246 WINPR_ASSERT(rfx);
2247 WINPR_ASSERT(s);
2248
2249 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2250 return FALSE;
2251
2252 Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC); /* blockType (2 bytes) */
2253 Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */
2254 Stream_Write_UINT32(s, 0xCACCACCA); /* magic (4 bytes) */
2255 Stream_Write_UINT16(s, 0x0100); /* version (2 bytes) */
2256 return TRUE;
2257}
2258
2259static INLINE BOOL rfx_write_progressive_wb_context(
2260 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, wStream* WINPR_RESTRICT s)
2261{
2262 const UINT32 blockLen = 10;
2263 WINPR_ASSERT(rfx);
2264 WINPR_ASSERT(s);
2265
2266 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2267 return FALSE;
2268
2269 Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT); /* blockType (2 bytes) */
2270 Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */
2271 Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */
2272 Stream_Write_UINT16(s, 64); /* tileSize (2 bytes) */
2273 Stream_Write_UINT8(s, 0); /* flags (1 byte) */
2274 return TRUE;
2275}
2276
2277static INLINE BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2278 wStream* WINPR_RESTRICT s,
2279 const RFX_MESSAGE* WINPR_RESTRICT msg)
2280{
2281 /* RFX_REGION */
2282 UINT32 blockLen = 18;
2283 UINT32 tilesDataSize = 0;
2284 const size_t start = Stream_GetPosition(s);
2285
2286 WINPR_ASSERT(rfx);
2287 WINPR_ASSERT(s);
2288 WINPR_ASSERT(msg);
2289
2290 blockLen += msg->numRects * 8;
2291 blockLen += msg->numQuant * 5;
2292 tilesDataSize = msg->numTiles * 22UL;
2293 for (UINT16 i = 0; i < msg->numTiles; i++)
2294 {
2295 const RFX_TILE* tile = msg->tiles[i];
2296 WINPR_ASSERT(tile);
2297 tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2298 }
2299 blockLen += tilesDataSize;
2300
2301 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2302 return FALSE;
2303
2304 Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION); /* blockType (2 bytes) */
2305 Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */
2306 Stream_Write_UINT8(s, 64); /* tileSize (1 byte) */
2307 Stream_Write_UINT16(s, msg->numRects); /* numRects (2 bytes) */
2308 WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2309 Stream_Write_UINT8(s, (UINT8)msg->numQuant); /* numQuant (1 byte) */
2310 Stream_Write_UINT8(s, 0); /* numProgQuant (1 byte) */
2311 Stream_Write_UINT8(s, 0); /* flags (1 byte) */
2312 Stream_Write_UINT16(s, msg->numTiles); /* numTiles (2 bytes) */
2313 Stream_Write_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */
2314
2315 for (UINT16 i = 0; i < msg->numRects; i++)
2316 {
2317 /* TS_RFX_RECT */
2318 const RFX_RECT* r = &msg->rects[i];
2319 Stream_Write_UINT16(s, r->x); /* x (2 bytes) */
2320 Stream_Write_UINT16(s, r->y); /* y (2 bytes) */
2321 Stream_Write_UINT16(s, r->width); /* width (2 bytes) */
2322 Stream_Write_UINT16(s, r->height); /* height (2 bytes) */
2323 }
2324
2333 for (UINT16 i = 0; i < msg->numQuant; i++)
2334 {
2335 const UINT32* qv = &msg->quantVals[10ULL * i];
2336 /* RFX_COMPONENT_CODEC_QUANT */
2337 Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4))); /* LL3 (4-bit), HL3 (4-bit) */
2338 Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4))); /* LH3 (4-bit), HH3 (4-bit) */
2339 Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4))); /* HL2 (4-bit), LH2 (4-bit) */
2340 Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4))); /* HH2 (4-bit), HL1 (4-bit) */
2341 Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4))); /* LH1 (4-bit), HH1 (4-bit) */
2342 }
2343
2344 for (UINT16 i = 0; i < msg->numTiles; i++)
2345 {
2346 const RFX_TILE* tile = msg->tiles[i];
2347 if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2348 return FALSE;
2349 }
2350
2351 const size_t end = Stream_GetPosition(s);
2352 const size_t used = end - start;
2353 return (used == blockLen);
2354}
2355
2356static INLINE BOOL
2357rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2358 wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT msg)
2359{
2360 const UINT32 blockLen = 12;
2361 WINPR_ASSERT(rfx);
2362 WINPR_ASSERT(s);
2363 WINPR_ASSERT(msg);
2364
2365 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2366 return FALSE;
2367
2368 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN); /* blockType (2 bytes) */
2369 Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */
2370 Stream_Write_UINT32(s, msg->frameIdx); /* frameIndex (4 bytes) */
2371 Stream_Write_UINT16(s, 1); /* regionCount (2 bytes) */
2372
2373 return TRUE;
2374}
2375
2376static INLINE BOOL rfx_write_progressive_frame_end(
2377 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, wStream* WINPR_RESTRICT s)
2378{
2379 const UINT32 blockLen = 6;
2380 WINPR_ASSERT(rfx);
2381 WINPR_ASSERT(s);
2382
2383 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2384 return FALSE;
2385
2386 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END); /* blockType (2 bytes) */
2387 Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */
2388
2389 return TRUE;
2390}
2391
2392static INLINE BOOL
2393rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2394 wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile)
2395{
2396 UINT32 blockLen = 0;
2397 WINPR_ASSERT(rfx);
2398 WINPR_ASSERT(s);
2399 WINPR_ASSERT(tile);
2400
2401 blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2402 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2403 return FALSE;
2404
2405 Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE); /* blockType (2 bytes) */
2406 Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */
2407 Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */
2408 Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */
2409 Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */
2410 Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */
2411 Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */
2412 Stream_Write_UINT8(s, 0); /* flags (1 byte) */
2413 Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */
2414 Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */
2415 Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */
2416 Stream_Write_UINT16(s, 0); /* tailLen (2 bytes) */
2417 Stream_Write(s, tile->YData, tile->YLen); /* YData */
2418 Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */
2419 Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */
2420
2421 return TRUE;
2422}
2423
2424const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2425{
2426 switch (blockType)
2427 {
2428 case PROGRESSIVE_WBT_SYNC:
2429 return "PROGRESSIVE_WBT_SYNC";
2430
2431 case PROGRESSIVE_WBT_FRAME_BEGIN:
2432 return "PROGRESSIVE_WBT_FRAME_BEGIN";
2433
2434 case PROGRESSIVE_WBT_FRAME_END:
2435 return "PROGRESSIVE_WBT_FRAME_END";
2436
2437 case PROGRESSIVE_WBT_CONTEXT:
2438 return "PROGRESSIVE_WBT_CONTEXT";
2439
2440 case PROGRESSIVE_WBT_REGION:
2441 return "PROGRESSIVE_WBT_REGION";
2442
2443 case PROGRESSIVE_WBT_TILE_SIMPLE:
2444 return "PROGRESSIVE_WBT_TILE_SIMPLE";
2445
2446 case PROGRESSIVE_WBT_TILE_FIRST:
2447 return "PROGRESSIVE_WBT_TILE_FIRST";
2448
2449 case PROGRESSIVE_WBT_TILE_UPGRADE:
2450 return "PROGRESSIVE_WBT_TILE_UPGRADE";
2451
2452 default:
2453 return "PROGRESSIVE_WBT_UNKNOWN";
2454 }
2455}
2456
2457BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT context,
2458 wStream* WINPR_RESTRICT s,
2459 const RFX_MESSAGE* WINPR_RESTRICT msg)
2460{
2461 WINPR_ASSERT(s);
2462 WINPR_ASSERT(msg);
2463 WINPR_ASSERT(context);
2464
2465 if (context->mode != RLGR1)
2466 {
2467 WLog_ERR(TAG, "error, RLGR1 mode is required!");
2468 return FALSE;
2469 }
2470
2471 if (!rfx_write_progressive_wb_sync(context, s))
2472 return FALSE;
2473
2474 if (!rfx_write_progressive_wb_context(context, s))
2475 return FALSE;
2476
2477 if (!rfx_write_progressive_frame_begin(context, s, msg))
2478 return FALSE;
2479
2480 if (!rfx_write_progressive_region(context, s, msg))
2481 return FALSE;
2482
2483 if (!rfx_write_progressive_frame_end(context, s))
2484 return FALSE;
2485
2486 return TRUE;
2487}
Definition rfx.h:44
Definition rfx.h:52
This struct contains function pointer to initialize/free objects.
Definition collections.h:57