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