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