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