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