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