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