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