22 #include <freerdp/config.h>
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>
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>
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"
51 #include "sse/rfx_sse2.h"
52 #include "neon/rfx_neon.h"
54 #define TAG FREERDP_TAG("codec")
56 #define RFX_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\RemoteFX"
70 static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
72 static INLINE BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
74 const RFX_TILE* WINPR_RESTRICT tile);
76 static INLINE
void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context)
78 if (!context || !context->priv)
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")
97 static INLINE
void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context)
99 if (!context || !context->priv)
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)
118 static INLINE
void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context)
120 if (!context || !context->priv)
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
142 static INLINE
void rfx_tile_init(
void* obj)
158 static INLINE
void* rfx_decoder_tile_new(
const void* val)
160 const size_t size = 4ULL * 64ULL * 64ULL;
167 if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16)))
169 winpr_aligned_free(tile);
172 memset(tile->data, 0xff, size);
173 tile->allocated = TRUE;
177 static INLINE
void rfx_decoder_tile_free(
void* obj)
184 winpr_aligned_free(tile->data);
186 winpr_aligned_free(tile);
190 static INLINE
void* rfx_encoder_tile_new(
const void* val)
193 return winpr_aligned_calloc(1,
sizeof(
RFX_TILE), 32);
196 static INLINE
void rfx_encoder_tile_free(
void* obj)
198 winpr_aligned_free(obj);
201 RFX_CONTEXT* rfx_context_new(BOOL encoder)
203 return rfx_context_new_ex(encoder, 0);
206 RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags)
214 RFX_CONTEXT* context = NULL;
216 RFX_CONTEXT_PRIV* priv = NULL;
217 context = (RFX_CONTEXT*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT), 32);
222 context->encoder = encoder;
223 context->currentMessage.freeArray = TRUE;
224 context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT_PRIV), 32);
229 priv->log = WLog_Get(
"com.freerdp.codec.rfx");
230 WLog_OpenAppender(priv->log);
231 priv->TilePool = ObjectPool_New(TRUE);
236 pool = ObjectPool_Object(priv->TilePool);
237 pool->fnObjectInit = rfx_tile_init;
239 if (context->encoder)
241 pool->fnObjectNew = rfx_encoder_tile_new;
242 pool->fnObjectFree = rfx_encoder_tile_free;
246 pool->fnObjectNew = rfx_decoder_tile_new;
247 pool->fnObjectFree = rfx_decoder_tile_free;
263 priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16);
265 if (!priv->BufferPool)
268 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
270 priv->UseThreads = TRUE;
272 GetNativeSystemInfo(&sysinfo);
273 priv->MinThreadCount = sysinfo.dwNumberOfProcessors;
274 priv->MaxThreadCount = 0;
275 status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
277 if (status == ERROR_SUCCESS)
279 dwSize =
sizeof(dwValue);
281 if (RegQueryValueEx(hKey, _T(
"UseThreads"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
283 priv->UseThreads = dwValue ? 1 : 0;
285 if (RegQueryValueEx(hKey, _T(
"MinThreadCount"), NULL, &dwType, (BYTE*)&dwValue,
286 &dwSize) == ERROR_SUCCESS)
287 priv->MinThreadCount = dwValue;
289 if (RegQueryValueEx(hKey, _T(
"MaxThreadCount"), NULL, &dwType, (BYTE*)&dwValue,
290 &dwSize) == ERROR_SUCCESS)
291 priv->MaxThreadCount = dwValue;
298 priv->UseThreads = FALSE;
301 if (priv->UseThreads)
307 priv->ThreadPool = CreateThreadpool(NULL);
309 if (!priv->ThreadPool)
312 InitializeThreadpoolEnvironment(&priv->ThreadPoolEnv);
313 SetThreadpoolCallbackPool(&priv->ThreadPoolEnv, priv->ThreadPool);
315 if (priv->MinThreadCount)
316 if (!SetThreadpoolThreadMinimum(priv->ThreadPool, priv->MinThreadCount))
319 if (priv->MaxThreadCount)
320 SetThreadpoolThreadMaximum(priv->ThreadPool, priv->MaxThreadCount);
324 rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32);
326 rfx_profiler_create(context);
328 context->quantization_decode = rfx_quantization_decode;
329 context->quantization_encode = rfx_quantization_encode;
330 context->dwt_2d_decode = rfx_dwt_2d_decode;
331 context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode;
332 context->dwt_2d_encode = rfx_dwt_2d_encode;
333 context->rlgr_decode = rfx_rlgr_decode;
334 context->rlgr_encode = rfx_rlgr_encode;
335 rfx_init_sse2(context);
336 rfx_init_neon(context);
337 context->state = RFX_STATE_SEND_HEADERS;
338 context->expectedDataBlockType = WBT_FRAME_BEGIN;
341 WINPR_PRAGMA_DIAG_PUSH
342 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
343 rfx_context_free(context);
344 WINPR_PRAGMA_DIAG_POP
348 void rfx_context_free(RFX_CONTEXT* context)
350 RFX_CONTEXT_PRIV* priv = NULL;
355 WINPR_ASSERT(NULL != context);
357 priv = context->priv;
358 WINPR_ASSERT(NULL != priv);
359 WINPR_ASSERT(NULL != priv->TilePool);
360 WINPR_ASSERT(NULL != priv->BufferPool);
363 rfx_message_free(context, &context->currentMessage);
364 winpr_aligned_free(context->quants);
365 rfx_profiler_print(context);
366 rfx_profiler_free(context);
370 ObjectPool_Free(priv->TilePool);
371 if (priv->UseThreads)
373 if (priv->ThreadPool)
374 CloseThreadpool(priv->ThreadPool);
375 DestroyThreadpoolEnvironment(&priv->ThreadPoolEnv);
376 winpr_aligned_free((
void*)priv->workObjects);
377 winpr_aligned_free(priv->tileWorkParams);
381 "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!");
385 BufferPool_Free(priv->BufferPool);
386 winpr_aligned_free(priv);
388 winpr_aligned_free(context);
391 static INLINE
RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
393 WINPR_ASSERT(message);
394 WINPR_ASSERT(message->tiles);
395 WINPR_ASSERT(index < message->numTiles);
396 return message->tiles[index];
399 static INLINE
const RFX_RECT* rfx_message_get_rect_const(
const RFX_MESSAGE* WINPR_RESTRICT message,
402 WINPR_ASSERT(message);
403 WINPR_ASSERT(message->rects);
404 WINPR_ASSERT(index < message->numRects);
405 return &message->rects[index];
408 static INLINE
RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
410 WINPR_ASSERT(message);
411 WINPR_ASSERT(message->rects);
412 WINPR_ASSERT(index < message->numRects);
413 return &message->rects[index];
416 void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format)
418 WINPR_ASSERT(context);
419 context->pixel_format = pixel_format;
420 const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format);
421 context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp);
424 UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context)
426 WINPR_ASSERT(context);
427 return context->pixel_format;
430 void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context,
431 const BYTE* WINPR_RESTRICT palette)
433 WINPR_ASSERT(context);
434 context->palette = palette;
437 const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context)
439 WINPR_ASSERT(context);
440 return context->palette;
443 BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
448 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
449 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
450 context->state = RFX_STATE_SEND_HEADERS;
451 context->expectedDataBlockType = WBT_FRAME_BEGIN;
452 context->frameIdx = 0;
456 static INLINE BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context,
461 WINPR_ASSERT(context);
462 WINPR_ASSERT(context->priv);
463 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC;
466 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
469 Stream_Read_UINT32(s, magic);
470 if (magic != WF_MAGIC)
472 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid magic number 0x%08" PRIX32
"", magic);
476 Stream_Read_UINT16(s, context->version);
477 if (context->version != WF_VERSION_1_0)
479 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid version number 0x%08" PRIX32
"",
484 WLog_Print(context->priv->log, WLOG_DEBUG,
"version 0x%08" PRIX32
"", context->version);
485 context->decodedHeaderBlocks |= RFX_DECODED_SYNC;
489 static INLINE BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context,
494 WINPR_ASSERT(context);
495 WINPR_ASSERT(context->priv);
496 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS;
498 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
501 Stream_Read_UINT8(s, numCodecs);
502 Stream_Read_UINT8(s, context->codec_id);
504 s, context->codec_version);
508 WLog_Print(context->priv->log, WLOG_ERROR,
"numCodes is 0x%02" PRIX8
" (must be 0x01)",
513 if (context->codec_id != 0x01)
515 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec id (0x%02" PRIX32
")",
520 if (context->codec_version != WF_VERSION_1_0)
522 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec version (0x%08" PRIX32
")",
523 context->codec_version);
527 WLog_Print(context->priv->log, WLOG_DEBUG,
"id %" PRIu32
" version 0x%" PRIX32
".",
528 context->codec_id, context->codec_version);
529 context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS;
533 static INLINE BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context,
537 BYTE numChannels = 0;
539 WINPR_ASSERT(context);
540 WINPR_ASSERT(context->priv);
541 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS;
543 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1))
546 Stream_Read_UINT8(s, numChannels);
553 WLog_Print(context->priv->log, WLOG_ERROR,
"no channels announced");
557 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull))
561 Stream_Read_UINT8(s, channelId);
563 if (channelId != 0x00)
565 WLog_Print(context->priv->log, WLOG_ERROR,
"channelId:0x%02" PRIX8
", expected:0x00",
570 Stream_Read_UINT16(s, context->width);
571 Stream_Read_UINT16(s, context->height);
573 if (!context->width || !context->height)
575 WLog_Print(context->priv->log, WLOG_ERROR,
576 "invalid channel with/height: %" PRIu16
"x%" PRIu16
"", context->width,
582 Stream_Seek(s, 5ULL * (numChannels - 1));
583 WLog_Print(context->priv->log, WLOG_DEBUG,
584 "numChannels %" PRIu8
" id %" PRIu8
", %" PRIu16
"x%" PRIu16
".", numChannels,
585 channelId, context->width, context->height);
586 context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS;
590 static INLINE BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
595 UINT16 properties = 0;
597 WINPR_ASSERT(context);
598 WINPR_ASSERT(context->priv);
599 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT;
601 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5))
604 Stream_Read_UINT8(s, ctxId);
605 Stream_Read_UINT16(s, tileSize);
606 Stream_Read_UINT16(s, properties);
607 WLog_Print(context->priv->log, WLOG_DEBUG,
608 "ctxId %" PRIu8
" tileSize %" PRIu16
" properties 0x%04" PRIX16
".", ctxId, tileSize,
610 context->properties = properties;
611 context->flags = (properties & 0x0007);
613 if (context->flags == CODEC_MODE)
615 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in image mode.");
619 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in video mode.");
622 switch ((properties & 0x1E00) >> 9)
624 case CLW_ENTROPY_RLGR1:
625 context->mode = RLGR1;
626 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR1.");
629 case CLW_ENTROPY_RLGR3:
630 context->mode = RLGR3;
631 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR3.");
635 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown RLGR algorithm.");
639 context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT;
643 static INLINE BOOL rfx_process_message_frame_begin(RFX_CONTEXT* WINPR_RESTRICT context,
644 RFX_MESSAGE* WINPR_RESTRICT message,
646 UINT16* WINPR_RESTRICT pExpectedBlockType)
649 UINT16 numRegions = 0;
651 WINPR_ASSERT(context);
652 WINPR_ASSERT(context->priv);
653 WINPR_ASSERT(message);
654 WINPR_ASSERT(pExpectedBlockType);
656 if (*pExpectedBlockType != WBT_FRAME_BEGIN)
658 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_FRAME_BEGIN");
662 *pExpectedBlockType = WBT_REGION;
664 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
669 Stream_Read_UINT16(s, numRegions);
670 WLog_Print(context->priv->log, WLOG_DEBUG,
671 "RFX_FRAME_BEGIN: frameIdx: %" PRIu32
" numRegions: %" PRIu16
"", frameIdx,
676 static INLINE BOOL rfx_process_message_frame_end(RFX_CONTEXT* WINPR_RESTRICT context,
677 RFX_MESSAGE* WINPR_RESTRICT message,
679 UINT16* WINPR_RESTRICT pExpectedBlockType)
681 WINPR_ASSERT(context);
682 WINPR_ASSERT(context->priv);
683 WINPR_ASSERT(message);
685 WINPR_ASSERT(pExpectedBlockType);
687 if (*pExpectedBlockType != WBT_FRAME_END)
689 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected, wants WBT_FRAME_END");
693 *pExpectedBlockType = WBT_FRAME_BEGIN;
694 WLog_Print(context->priv->log, WLOG_DEBUG,
"RFX_FRAME_END");
698 static INLINE BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message)
700 WINPR_ASSERT(message);
703 winpr_aligned_recalloc(message->rects, message->numRects,
sizeof(
RFX_RECT), 32);
706 message->rects = tmpRects;
710 static INLINE BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
711 RFX_MESSAGE* WINPR_RESTRICT message,
713 UINT16* WINPR_RESTRICT pExpectedBlockType)
715 UINT16 regionType = 0;
716 UINT16 numTileSets = 0;
718 WINPR_ASSERT(context);
719 WINPR_ASSERT(context->priv);
720 WINPR_ASSERT(message);
721 WINPR_ASSERT(pExpectedBlockType);
723 if (*pExpectedBlockType != WBT_REGION)
725 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_REGION");
729 *pExpectedBlockType = WBT_EXTENSION;
731 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3))
734 Stream_Seek_UINT8(s);
735 Stream_Read_UINT16(s, message->numRects);
737 if (message->numRects < 1)
745 message->numRects = 1;
746 if (!rfx_resize_rects(message))
749 message->rects->x = 0;
750 message->rects->y = 0;
751 message->rects->width = context->width;
752 message->rects->height = context->height;
756 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull))
759 if (!rfx_resize_rects(message))
763 for (UINT16 i = 0; i < message->numRects; i++)
765 RFX_RECT* rect = rfx_message_get_rect(message, i);
767 Stream_Read_UINT16(s, rect->x);
768 Stream_Read_UINT16(s, rect->y);
769 Stream_Read_UINT16(s, rect->width);
770 Stream_Read_UINT16(s, rect->height);
771 WLog_Print(context->priv->log, WLOG_DEBUG,
772 "rect %" PRIu16
" (x,y=%" PRIu16
",%" PRIu16
" w,h=%" PRIu16
" %" PRIu16
").", i,
773 rect->x, rect->y, rect->width, rect->height);
776 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
779 Stream_Read_UINT16(s, regionType);
780 Stream_Read_UINT16(s, numTileSets);
782 if (regionType != CBT_REGION)
784 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid region type 0x%04" PRIX16
"",
789 if (numTileSets != 0x0001)
791 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid number of tilesets (%" PRIu16
")",
802 RFX_CONTEXT* context;
803 } RFX_TILE_PROCESS_WORK_PARAM;
805 static INLINE
void CALLBACK rfx_process_message_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
806 void* context, PTP_WORK work)
808 RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
810 rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4);
813 static INLINE BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count,
816 WINPR_ASSERT(message);
819 (
RFX_TILE**)winpr_aligned_recalloc((
void*)message->tiles, count,
sizeof(
RFX_TILE*), 32);
820 if (!tmpTiles && (count != 0))
823 message->tiles = tmpTiles;
825 message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count);
828 WINPR_ASSERT(message->numTiles <= count);
830 message->allocatedTiles = count;
835 static INLINE BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
836 RFX_MESSAGE* WINPR_RESTRICT message,
838 UINT16* WINPR_RESTRICT pExpectedBlockType)
841 size_t close_cnt = 0;
844 UINT32* quants = NULL;
848 UINT32 blockType = 0;
849 UINT32 tilesDataSize = 0;
850 PTP_WORK* work_objects = NULL;
851 RFX_TILE_PROCESS_WORK_PARAM* params = NULL;
854 WINPR_ASSERT(context);
855 WINPR_ASSERT(context->priv);
856 WINPR_ASSERT(message);
857 WINPR_ASSERT(pExpectedBlockType);
859 if (*pExpectedBlockType != WBT_EXTENSION)
861 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants a tileset");
865 *pExpectedBlockType = WBT_FRAME_END;
867 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14))
870 Stream_Read_UINT16(s, subtype);
871 if (subtype != CBT_TILESET)
873 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid subtype, expected CBT_TILESET.");
877 Stream_Seek_UINT16(s);
878 Stream_Seek_UINT16(s);
879 Stream_Read_UINT8(s, context->numQuant);
880 Stream_Seek_UINT8(s);
882 if (context->numQuant < 1)
884 WLog_Print(context->priv->log, WLOG_ERROR,
"no quantization value.");
888 Stream_Read_UINT16(s, numTiles);
895 Stream_Read_UINT32(s, tilesDataSize);
898 winpr_aligned_recalloc(context->quants, context->numQuant, 10 *
sizeof(UINT32), 32)))
901 quants = context->quants = (UINT32*)pmem;
904 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull))
907 for (
size_t i = 0; i < context->numQuant; i++)
910 Stream_Read_UINT8(s, quant);
911 *quants++ = (quant & 0x0F);
912 *quants++ = (quant >> 4);
913 Stream_Read_UINT8(s, quant);
914 *quants++ = (quant & 0x0F);
915 *quants++ = (quant >> 4);
916 Stream_Read_UINT8(s, quant);
917 *quants++ = (quant & 0x0F);
918 *quants++ = (quant >> 4);
919 Stream_Read_UINT8(s, quant);
920 *quants++ = (quant & 0x0F);
921 *quants++ = (quant >> 4);
922 Stream_Read_UINT8(s, quant);
923 *quants++ = (quant & 0x0F);
924 *quants++ = (quant >> 4);
925 WLog_Print(context->priv->log, WLOG_DEBUG,
926 "quant %d (%" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
927 " %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
").",
928 i, context->quants[i * 10], context->quants[i * 10 + 1],
929 context->quants[i * 10 + 2], context->quants[i * 10 + 3],
930 context->quants[i * 10 + 4], context->quants[i * 10 + 5],
931 context->quants[i * 10 + 6], context->quants[i * 10 + 7],
932 context->quants[i * 10 + 8], context->quants[i * 10 + 9]);
935 for (
size_t i = 0; i < message->numTiles; i++)
937 ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
938 message->tiles[i] = NULL;
941 if (!rfx_allocate_tiles(message, numTiles, FALSE))
944 if (context->priv->UseThreads)
946 work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles,
sizeof(PTP_WORK), 32);
947 params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc(
948 NULL, message->numTiles,
sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32);
952 winpr_aligned_free(params);
958 winpr_aligned_free((
void*)work_objects);
967 if (Stream_GetRemainingLength(s) >= tilesDataSize)
970 for (
size_t i = 0; i < message->numTiles; i++)
975 if (!(tile = (
RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
977 WLog_Print(context->priv->log, WLOG_ERROR,
978 "RfxMessageTileSet failed to get tile from object pool");
983 message->tiles[i] = tile;
986 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
988 WLog_Print(context->priv->log, WLOG_ERROR,
989 "RfxMessageTileSet packet too small to read tile %d/%" PRIu16
"", i,
995 sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
998 Stream_Read_UINT32(sub, blockLen);
1000 if (!Stream_SafeSeek(s, blockLen))
1005 if ((blockLen < 6 + 13) ||
1006 (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6)))
1008 WLog_Print(context->priv->log, WLOG_ERROR,
1009 "RfxMessageTileSet not enough bytes to read tile %d/%" PRIu16
1010 " with blocklen=%" PRIu32
"",
1011 i, message->numTiles, blockLen);
1016 if (blockType != CBT_TILE)
1018 WLog_Print(context->priv->log, WLOG_ERROR,
1019 "unknown block type 0x%" PRIX32
", expected CBT_TILE (0xCAC3).",
1025 Stream_Read_UINT8(sub, tile->quantIdxY);
1026 Stream_Read_UINT8(sub, tile->quantIdxCb);
1027 Stream_Read_UINT8(sub, tile->quantIdxCr);
1028 if (tile->quantIdxY >= context->numQuant)
1030 WLog_Print(context->priv->log, WLOG_ERROR,
1031 "quantIdxY %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxY,
1036 if (tile->quantIdxCb >= context->numQuant)
1038 WLog_Print(context->priv->log, WLOG_ERROR,
1039 "quantIdxCb %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCb,
1044 if (tile->quantIdxCr >= context->numQuant)
1046 WLog_Print(context->priv->log, WLOG_ERROR,
1047 "quantIdxCr %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCr,
1053 Stream_Read_UINT16(sub, tile->xIdx);
1054 Stream_Read_UINT16(sub, tile->yIdx);
1055 Stream_Read_UINT16(sub, tile->YLen);
1056 Stream_Read_UINT16(sub, tile->CbLen);
1057 Stream_Read_UINT16(sub, tile->CrLen);
1058 Stream_GetPointer(sub, tile->YData);
1059 if (!Stream_SafeSeek(sub, tile->YLen))
1064 Stream_GetPointer(sub, tile->CbData);
1065 if (!Stream_SafeSeek(sub, tile->CbLen))
1070 Stream_GetPointer(sub, tile->CrData);
1071 if (!Stream_SafeSeek(sub, tile->CrLen))
1076 tile->x = tile->xIdx * 64;
1077 tile->y = tile->yIdx * 64;
1079 if (context->priv->UseThreads)
1087 params[i].context = context;
1088 params[i].tile = message->tiles[i];
1090 if (!(work_objects[i] =
1091 CreateThreadpoolWork(rfx_process_message_tile_work_callback,
1092 (
void*)¶ms[i], &context->priv->ThreadPoolEnv)))
1094 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThreadpoolWork failed.");
1099 SubmitThreadpoolWork(work_objects[i]);
1104 rfx_decode_rgb(context, tile, tile->data, 64 * 4);
1109 if (context->priv->UseThreads)
1111 for (
size_t i = 0; i < close_cnt; i++)
1113 WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
1114 CloseThreadpoolWork(work_objects[i]);
1118 winpr_aligned_free((
void*)work_objects);
1119 winpr_aligned_free(params);
1121 for (
size_t i = 0; i < message->numTiles; i++)
1123 if (!(tile = message->tiles[i]))
1126 tile->YLen = tile->CbLen = tile->CrLen = 0;
1127 tile->YData = tile->CbData = tile->CrData = NULL;
1133 BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT data,
1134 UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst,
1135 UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight,
1136 REGION16* WINPR_RESTRICT invalidRegion)
1142 if (!context || !data || !length)
1145 WINPR_ASSERT(context->priv);
1146 RFX_MESSAGE* message = &context->currentMessage;
1148 wStream* s = Stream_StaticConstInit(&inStream, data, length);
1150 while (ok && Stream_GetRemainingLength(s) > 6)
1152 wStream subStreamBuffer = { 0 };
1153 size_t extraBlockLen = 0;
1154 UINT32 blockLen = 0;
1155 UINT32 blockType = 0;
1158 Stream_Read_UINT16(s, blockType);
1159 Stream_Read_UINT32(s, blockLen);
1160 WLog_Print(context->priv->log, WLOG_DEBUG,
"blockType 0x%" PRIX32
" blockLen %" PRIu32
"",
1161 blockType, blockLen);
1165 WLog_Print(context->priv->log, WLOG_ERROR,
"blockLen too small(%" PRIu32
")", blockLen);
1169 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6))
1172 if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS)
1174 WLog_Print(context->priv->log, WLOG_ERROR,
"incomplete header blocks processing");
1178 if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
1182 UINT8 channelId = 0;
1184 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1188 Stream_Read_UINT8(s, codecId);
1189 Stream_Read_UINT8(s, channelId);
1191 if (codecId != 0x01)
1193 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codecId 0x%02" PRIX8
"",
1198 if (blockType == WBT_CONTEXT)
1201 if (channelId != 0xFF)
1203 WLog_Print(context->priv->log, WLOG_ERROR,
1204 "invalid channelId 0x%02" PRIX8
" for blockType 0x%08" PRIX32
"",
1205 channelId, blockType);
1212 if (channelId != 0x00)
1214 WLog_Print(context->priv->log, WLOG_ERROR,
1215 "invalid channelId 0x%02" PRIX8
" for blockType WBT_CONTEXT",
1222 const size_t blockLenNoHeader = blockLen - 6;
1223 if (blockLenNoHeader < extraBlockLen)
1225 WLog_Print(context->priv->log, WLOG_ERROR,
1226 "blockLen too small(%" PRIu32
"), must be >= 6 + %" PRIu16, blockLen,
1231 const size_t subStreamLen = blockLenNoHeader - extraBlockLen;
1232 wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen);
1233 Stream_Seek(s, subStreamLen);
1242 ok = rfx_process_message_sync(context, subStream);
1246 ok = rfx_process_message_context(context, subStream);
1249 case WBT_CODEC_VERSIONS:
1250 ok = rfx_process_message_codec_versions(context, subStream);
1254 ok = rfx_process_message_channels(context, subStream);
1264 case WBT_FRAME_BEGIN:
1265 ok = rfx_process_message_frame_begin(context, message, subStream,
1266 &context->expectedDataBlockType);
1270 ok = rfx_process_message_region(context, message, subStream,
1271 &context->expectedDataBlockType);
1275 ok = rfx_process_message_tileset(context, message, subStream,
1276 &context->expectedDataBlockType);
1280 ok = rfx_process_message_frame_end(context, message, subStream,
1281 &context->expectedDataBlockType);
1285 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown blockType 0x%" PRIX32
"",
1293 UINT32 nbUpdateRects = 0;
1296 const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format);
1297 const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat);
1298 region16_init(&clippingRects);
1300 WINPR_ASSERT(dstWidth <= UINT16_MAX);
1301 WINPR_ASSERT(dstHeight <= UINT16_MAX);
1302 for (UINT32 i = 0; i < message->numRects; i++)
1305 const RFX_RECT* rect = &(message->rects[i]);
1307 WINPR_ASSERT(left + rect->x <= UINT16_MAX);
1308 WINPR_ASSERT(top + rect->y <= UINT16_MAX);
1309 WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX);
1310 WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX);
1312 clippingRect.left = (UINT16)MIN(left + rect->x, dstWidth);
1313 clippingRect.top = (UINT16)MIN(top + rect->y, dstHeight);
1314 clippingRect.right = (UINT16)MIN(clippingRect.left + rect->width, dstWidth);
1315 clippingRect.bottom = (UINT16)MIN(clippingRect.top + rect->height, dstHeight);
1316 region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
1319 for (UINT32 i = 0; i < message->numTiles; i++)
1322 const RFX_TILE* tile = rfx_message_get_tile(message, i);
1324 WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1325 WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1327 updateRect.left = (UINT16)left + tile->x;
1328 updateRect.top = (UINT16)top + tile->y;
1329 updateRect.right = updateRect.left + 64;
1330 updateRect.bottom = updateRect.top + 64;
1331 region16_init(&updateRegion);
1332 region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
1333 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1335 for (UINT32 j = 0; j < nbUpdateRects; j++)
1337 const UINT32 stride = 64 * formatSize;
1338 const UINT32 nXDst = updateRects[j].left;
1339 const UINT32 nYDst = updateRects[j].top;
1340 const UINT32 nXSrc = nXDst - updateRect.left;
1341 const UINT32 nYSrc = nYDst - updateRect.top;
1342 const UINT32 nWidth = updateRects[j].right - updateRects[j].left;
1343 const UINT32 nHeight = updateRects[j].bottom - updateRects[j].top;
1345 if (!freerdp_image_copy_no_overlap(dst, dstFormat, dstStride, nXDst, nYDst, nWidth,
1346 nHeight, tile->data, context->pixel_format,
1347 stride, nXSrc, nYSrc, NULL, FREERDP_FLIP_NONE))
1349 region16_uninit(&updateRegion);
1350 WLog_Print(context->priv->log, WLOG_ERROR,
1351 "nbUpdateRectx[%" PRIu32
" (%" PRIu32
")] freerdp_image_copy failed",
1357 region16_union_rect(invalidRegion, invalidRegion, &updateRects[j]);
1360 region16_uninit(&updateRegion);
1363 region16_uninit(&clippingRects);
1368 rfx_message_free(context, message);
1369 context->currentMessage.freeArray = TRUE;
1372 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1376 const UINT32* rfx_message_get_quants(
const RFX_MESSAGE* WINPR_RESTRICT message,
1377 UINT16* WINPR_RESTRICT numQuantVals)
1379 WINPR_ASSERT(message);
1381 *numQuantVals = message->numQuant;
1382 return message->quantVals;
1385 const RFX_TILE** rfx_message_get_tiles(
const RFX_MESSAGE* WINPR_RESTRICT message,
1386 UINT16* WINPR_RESTRICT numTiles)
1388 WINPR_ASSERT(message);
1390 *numTiles = message->numTiles;
1397 cnv.pp = message->tiles;
1401 UINT16 rfx_message_get_tile_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1403 WINPR_ASSERT(message);
1404 return message->numTiles;
1407 const RFX_RECT* rfx_message_get_rects(
const RFX_MESSAGE* WINPR_RESTRICT message,
1408 UINT16* WINPR_RESTRICT numRects)
1410 WINPR_ASSERT(message);
1412 *numRects = message->numRects;
1413 return message->rects;
1416 UINT16 rfx_message_get_rect_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1418 WINPR_ASSERT(message);
1419 return message->numRects;
1422 void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1427 winpr_aligned_free(message->rects);
1431 for (
size_t i = 0; i < message->numTiles; i++)
1433 RFX_TILE* tile = message->tiles[i];
1437 if (tile->YCbCrData)
1439 BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1440 tile->YCbCrData = NULL;
1443 ObjectPool_Return(context->priv->TilePool, (
void*)tile);
1446 rfx_allocate_tiles(message, 0, FALSE);
1449 const BOOL freeArray = message->freeArray;
1450 const RFX_MESSAGE empty = { 0 };
1454 winpr_aligned_free(message);
1457 static INLINE
void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1459 UINT16 properties = 0;
1461 WINPR_ASSERT(context);
1464 properties |= (context->flags << 1);
1465 properties |= (COL_CONV_ICT << 4);
1466 properties |= (CLW_XFORM_DWT_53_A << 6);
1467 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10);
1468 properties |= (SCALAR_QUANTIZATION << 14);
1469 context->properties = properties;
1472 static INLINE
void rfx_write_message_sync(
const RFX_CONTEXT* WINPR_RESTRICT context,
1475 WINPR_ASSERT(context);
1477 Stream_Write_UINT16(s, WBT_SYNC);
1478 Stream_Write_UINT32(s, 12);
1479 Stream_Write_UINT32(s, WF_MAGIC);
1480 Stream_Write_UINT16(s, WF_VERSION_1_0);
1483 static INLINE
void rfx_write_message_codec_versions(
const RFX_CONTEXT* WINPR_RESTRICT context,
1486 WINPR_ASSERT(context);
1488 Stream_Write_UINT16(s, WBT_CODEC_VERSIONS);
1489 Stream_Write_UINT32(s, 10);
1490 Stream_Write_UINT8(s, 1);
1491 Stream_Write_UINT8(s, 1);
1492 Stream_Write_UINT16(s, WF_VERSION_1_0);
1495 static INLINE
void rfx_write_message_channels(
const RFX_CONTEXT* WINPR_RESTRICT context,
1498 WINPR_ASSERT(context);
1500 Stream_Write_UINT16(s, WBT_CHANNELS);
1501 Stream_Write_UINT32(s, 12);
1502 Stream_Write_UINT8(s, 1);
1503 Stream_Write_UINT8(s, 0);
1504 Stream_Write_UINT16(s, context->width);
1505 Stream_Write_UINT16(s, context->height);
1508 static INLINE
void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1511 UINT16 properties = 0;
1512 WINPR_ASSERT(context);
1514 Stream_Write_UINT16(s, WBT_CONTEXT);
1515 Stream_Write_UINT32(s, 13);
1516 Stream_Write_UINT8(s, 1);
1517 Stream_Write_UINT8(s, 0xFF);
1518 Stream_Write_UINT8(s, 0);
1519 Stream_Write_UINT16(s, CT_TILE_64x64);
1521 properties = context->flags;
1522 properties |= (COL_CONV_ICT << 3);
1523 properties |= (CLW_XFORM_DWT_53_A << 5);
1524 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9);
1525 properties |= (SCALAR_QUANTIZATION << 13);
1526 Stream_Write_UINT16(s, properties);
1527 rfx_update_context_properties(context);
1530 static INLINE BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1533 WINPR_ASSERT(context);
1534 if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
1537 rfx_write_message_sync(context, s);
1538 rfx_write_message_context(context, s);
1539 rfx_write_message_codec_versions(context, s);
1540 rfx_write_message_channels(context, s);
1544 static INLINE
size_t rfx_tile_length(
const RFX_TILE* WINPR_RESTRICT tile)
1547 return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1550 static INLINE BOOL rfx_write_tile(
wStream* WINPR_RESTRICT s,
const RFX_TILE* WINPR_RESTRICT tile)
1552 const size_t blockLen = rfx_tile_length(tile);
1553 if (blockLen > UINT32_MAX)
1555 if (!Stream_EnsureRemainingCapacity(s, blockLen))
1558 Stream_Write_UINT16(s, CBT_TILE);
1559 Stream_Write_UINT32(s, (UINT32)blockLen);
1560 Stream_Write_UINT8(s, tile->quantIdxY);
1561 Stream_Write_UINT8(s, tile->quantIdxCb);
1562 Stream_Write_UINT8(s, tile->quantIdxCr);
1563 Stream_Write_UINT16(s, tile->xIdx);
1564 Stream_Write_UINT16(s, tile->yIdx);
1565 Stream_Write_UINT16(s, tile->YLen);
1566 Stream_Write_UINT16(s, tile->CbLen);
1567 Stream_Write_UINT16(s, tile->CrLen);
1568 Stream_Write(s, tile->YData, tile->YLen);
1569 Stream_Write(s, tile->CbData, tile->CbLen);
1570 Stream_Write(s, tile->CrData, tile->CrLen);
1574 struct S_RFX_TILE_COMPOSE_WORK_PARAM
1577 RFX_CONTEXT* context;
1580 static INLINE
void CALLBACK rfx_compose_message_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
1581 void* context, PTP_WORK work)
1583 RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1584 WINPR_ASSERT(param);
1585 rfx_encode_rgb(param->context, param->tile);
1588 static INLINE BOOL computeRegion(
const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1589 REGION16* WINPR_RESTRICT region,
size_t width,
size_t height)
1591 const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1592 WINPR_ASSERTING_INT_CAST(UINT16, height) };
1594 WINPR_ASSERT(rects);
1595 for (
size_t i = 0; i < numRects; i++)
1599 rect16.left = rect->x;
1600 rect16.top = rect->y;
1601 rect16.right = rect->x + rect->width;
1602 rect16.bottom = rect->y + rect->height;
1604 if (!region16_union_rect(region, region, &rect16))
1608 return region16_intersect_rect(region, region, &mainRect);
1611 #define TILE_NO(v) ((v) / 64)
1613 static INLINE BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context,
size_t nbTiles)
1615 WINPR_ASSERT(context);
1617 RFX_CONTEXT_PRIV* priv = context->priv;
1622 if (!context->priv->UseThreads)
1625 if (!(pmem = winpr_aligned_recalloc((
void*)priv->workObjects, nbTiles,
sizeof(PTP_WORK), 32)))
1628 priv->workObjects = (PTP_WORK*)pmem;
1630 if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1631 sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1634 priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1638 static INLINE BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count)
1640 WINPR_ASSERT(message);
1642 if (message->numTiles + count <= message->allocatedTiles)
1645 const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1646 return rfx_allocate_tiles(message, alloc, TRUE);
1649 RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1650 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1651 const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h,
size_t s)
1653 const UINT32 width = w;
1654 const UINT32 height = h;
1655 const UINT32 scanline = (UINT32)s;
1656 RFX_MESSAGE* message = NULL;
1657 PTP_WORK* workObject = NULL;
1658 RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL;
1659 BOOL success = FALSE;
1666 WINPR_ASSERT(rects);
1667 WINPR_ASSERT(numRects > 0);
1668 WINPR_ASSERT(w > 0);
1669 WINPR_ASSERT(h > 0);
1670 WINPR_ASSERT(s > 0);
1672 if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1,
sizeof(RFX_MESSAGE), 32)))
1675 region16_init(&tilesRegion);
1676 region16_init(&rectsRegion);
1678 if (context->state == RFX_STATE_SEND_HEADERS)
1679 rfx_update_context_properties(context);
1681 message->frameIdx = context->frameIdx++;
1683 if (!context->numQuant)
1685 WINPR_ASSERT(context->quants == NULL);
1686 if (!(context->quants =
1687 (UINT32*)winpr_aligned_malloc(
sizeof(rfx_default_quantization_values), 32)))
1688 goto skip_encoding_loop;
1690 CopyMemory(context->quants, &rfx_default_quantization_values,
1691 sizeof(rfx_default_quantization_values));
1692 context->numQuant = 1;
1693 context->quantIdxY = 0;
1694 context->quantIdxCb = 0;
1695 context->quantIdxCr = 0;
1698 message->numQuant = context->numQuant;
1699 message->quantVals = context->quants;
1700 const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1702 if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1703 goto skip_encoding_loop;
1705 const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1706 WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1707 WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1708 const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1709 const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1710 const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1712 if (!rfx_ensure_tiles(message, maxNbTiles))
1713 goto skip_encoding_loop;
1715 if (!setupWorkers(context, maxNbTiles))
1716 goto skip_encoding_loop;
1718 if (context->priv->UseThreads)
1720 workObject = context->priv->workObjects;
1721 workParam = context->priv->tileWorkParams;
1724 UINT32 regionNbRects = 0;
1725 regionRect = region16_rects(&rectsRegion, ®ionNbRects);
1727 if (!(message->rects = winpr_aligned_calloc(regionNbRects,
sizeof(
RFX_RECT), 32)))
1728 goto skip_encoding_loop;
1730 message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1732 for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1734 RFX_RECT* rfxRect = &message->rects[i];
1735 UINT32 startTileX = regionRect->left / 64;
1736 UINT32 endTileX = (regionRect->right - 1) / 64;
1737 UINT32 startTileY = regionRect->top / 64;
1738 UINT32 endTileY = (regionRect->bottom - 1) / 64;
1739 rfxRect->x = regionRect->left;
1740 rfxRect->y = regionRect->top;
1741 rfxRect->width = (regionRect->right - regionRect->left);
1742 rfxRect->height = (regionRect->bottom - regionRect->top);
1744 for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1745 yIdx++, gridRelY += 64)
1747 UINT32 tileHeight = 64;
1749 if ((yIdx == endTileY) && (gridRelY + 64 > height))
1750 tileHeight = height - gridRelY;
1752 currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1753 currentTileRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1755 for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1756 xIdx++, gridRelX += 64)
1763 UINT32 tileWidth = 64;
1765 if ((xIdx == endTileX) && (gridRelX + 64 > width))
1767 tileWidth = (width - gridRelX);
1770 currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1771 currentTileRect.right = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1774 if (region16_intersects_rect(&tilesRegion, ¤tTileRect))
1779 goto skip_encoding_loop;
1781 tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1782 tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1783 tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1784 tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1785 tile->scanline = scanline;
1787 tile->width = tileWidth;
1788 tile->height = tileHeight;
1789 const UINT32 ax = gridRelX;
1790 const UINT32 ay = gridRelY;
1792 if (tile->data && tile->allocated)
1794 winpr_aligned_free(tile->data);
1795 tile->allocated = FALSE;
1799 cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1800 tile->data = cnv.pv;
1801 tile->quantIdxY = context->quantIdxY;
1802 tile->quantIdxCb = context->quantIdxCb;
1803 tile->quantIdxCr = context->quantIdxCr;
1804 tile->YLen = tile->CbLen = tile->CrLen = 0;
1806 if (!(tile->YCbCrData = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1807 goto skip_encoding_loop;
1809 tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1810 tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1811 tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1813 if (!rfx_ensure_tiles(message, 1))
1814 goto skip_encoding_loop;
1815 message->tiles[message->numTiles++] = tile;
1817 if (context->priv->UseThreads)
1819 workParam->context = context;
1820 workParam->tile = tile;
1822 if (!(*workObject = CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1824 &context->priv->ThreadPoolEnv)))
1826 goto skip_encoding_loop;
1829 SubmitThreadpoolWork(*workObject);
1835 rfx_encode_rgb(context, tile);
1838 if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect))
1839 goto skip_encoding_loop;
1850 message->tilesDataSize = 0;
1851 workObject = context->priv->workObjects;
1853 for (UINT32 i = 0; i < message->numTiles; i++)
1855 if (context->priv->UseThreads)
1859 WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1860 CloseThreadpoolWork(*workObject);
1866 const RFX_TILE* tile = message->tiles[i];
1867 message->tilesDataSize += rfx_tile_length(tile);
1870 region16_uninit(&tilesRegion);
1871 region16_uninit(&rectsRegion);
1876 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1878 rfx_message_free(context, message);
1882 static INLINE BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1883 const RFX_MESSAGE* WINPR_RESTRICT src)
1888 WINPR_ASSERT(dst->rects == NULL);
1889 WINPR_ASSERT(dst->numRects == 0);
1891 if (src->numRects == 0)
1894 dst->rects = winpr_aligned_calloc(src->numRects,
sizeof(
RECTANGLE_16), 32);
1897 dst->numRects = src->numRects;
1898 for (
size_t x = 0; x < src->numRects; x++)
1900 dst->rects[x] = src->rects[x];
1905 static INLINE BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1906 const RFX_MESSAGE* WINPR_RESTRICT src)
1911 WINPR_ASSERT(dst->quantVals == NULL);
1912 WINPR_ASSERT(dst->numQuant == 0);
1914 if (src->numQuant == 0)
1918 dst->quantVals = src->quantVals;
1919 dst->numQuant = src->numQuant;
1924 static INLINE RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
1925 RFX_MESSAGE* WINPR_RESTRICT message,
1926 size_t* WINPR_RESTRICT numMessages,
size_t maxDataSize)
1928 WINPR_ASSERT(context);
1929 WINPR_ASSERT(message);
1930 WINPR_ASSERT(numMessages);
1932 maxDataSize -= 1024;
1933 *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
1935 RFX_MESSAGE* messages =
1936 (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages),
sizeof(RFX_MESSAGE), 32);
1941 for (UINT16 i = 0; i < message->numTiles; i++)
1943 RFX_TILE* tile = message->tiles[i];
1944 RFX_MESSAGE* msg = &messages[j];
1949 const size_t tileDataSize = rfx_tile_length(tile);
1951 if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
1954 if (msg->numTiles == 0)
1956 msg->frameIdx = message->frameIdx + j;
1957 if (!rfx_clone_quants(msg, message))
1959 if (!rfx_clone_rects(msg, message))
1961 msg->freeArray = TRUE;
1962 if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
1966 msg->tilesDataSize += tileDataSize;
1968 WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
1969 msg->tiles[msg->numTiles++] = message->tiles[i];
1970 message->tiles[i] = NULL;
1973 *numMessages = j + 1ULL;
1974 context->frameIdx += j;
1975 message->numTiles = 0;
1979 for (
size_t i = 0; i < j; i++)
1980 rfx_allocate_tiles(&messages[i], 0, FALSE);
1982 winpr_aligned_free(messages);
1986 const RFX_MESSAGE* rfx_message_list_get(
const RFX_MESSAGE_LIST* WINPR_RESTRICT messages,
size_t idx)
1988 WINPR_ASSERT(messages);
1989 if (idx >= messages->count)
1991 WINPR_ASSERT(messages->list);
1992 return &messages->list[idx];
1995 void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
1999 for (
size_t x = 0; x < messages->count; x++)
2000 rfx_message_free(messages->context, &messages->list[x]);
2004 static INLINE RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
2005 RFX_MESSAGE* WINPR_RESTRICT messages,
2008 WINPR_ASSERT(context);
2009 RFX_MESSAGE_LIST* msg = calloc(1,
sizeof(RFX_MESSAGE_LIST));
2012 msg->context = context;
2014 msg->list = messages;
2018 RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
2019 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2020 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2021 UINT32 scanline,
size_t* WINPR_RESTRICT numMessages,
2024 WINPR_ASSERT(context);
2025 WINPR_ASSERT(numMessages);
2027 RFX_MESSAGE* message =
2028 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2032 RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2033 rfx_message_free(context, message);
2037 return rfx_message_list_new(context, list, *numMessages);
2040 static INLINE BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2042 const RFX_MESSAGE* WINPR_RESTRICT message)
2044 WINPR_ASSERT(context);
2045 WINPR_ASSERT(message);
2047 const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2049 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2052 Stream_Write_UINT16(s, WBT_EXTENSION);
2053 Stream_Write_UINT32(s, blockLen);
2054 Stream_Write_UINT8(s, 1);
2055 Stream_Write_UINT8(s, 0);
2056 Stream_Write_UINT16(s, CBT_TILESET);
2057 Stream_Write_UINT16(s, 0);
2058 Stream_Write_UINT16(s, context->properties);
2060 s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant));
2061 Stream_Write_UINT8(s, 0x40);
2062 Stream_Write_UINT16(s, message->numTiles);
2063 Stream_Write_UINT32(s, message->tilesDataSize);
2065 UINT32* quantVals = message->quantVals;
2066 for (
size_t i = 0; i < message->numQuant * 5ul; i++)
2068 WINPR_ASSERT(quantVals);
2069 Stream_Write_UINT8(s,
2070 WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2074 for (
size_t i = 0; i < message->numTiles; i++)
2076 RFX_TILE* tile = message->tiles[i];
2080 if (!rfx_write_tile(s, tile))
2084 #ifdef WITH_DEBUG_RFX
2085 WLog_Print(context->priv->log, WLOG_DEBUG,
2086 "numQuant: %" PRIu16
" numTiles: %" PRIu16
" tilesDataSize: %" PRIu32
"",
2087 message->numQuant, message->numTiles, message->tilesDataSize);
2092 static INLINE BOOL rfx_write_message_frame_begin(RFX_CONTEXT* WINPR_RESTRICT context,
2094 const RFX_MESSAGE* WINPR_RESTRICT message)
2096 WINPR_ASSERT(context);
2097 WINPR_ASSERT(message);
2099 if (!Stream_EnsureRemainingCapacity(s, 14))
2102 Stream_Write_UINT16(s, WBT_FRAME_BEGIN);
2103 Stream_Write_UINT32(s, 14);
2104 Stream_Write_UINT8(s, 1);
2105 Stream_Write_UINT8(s, 0);
2106 Stream_Write_UINT32(s, message->frameIdx);
2107 Stream_Write_UINT16(s, 1);
2111 static INLINE BOOL rfx_write_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
2113 const RFX_MESSAGE* WINPR_RESTRICT message)
2115 WINPR_ASSERT(context);
2116 WINPR_ASSERT(message);
2118 const size_t blockLen = 15 + (message->numRects * 8);
2119 if (blockLen > UINT32_MAX)
2122 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2125 Stream_Write_UINT16(s, WBT_REGION);
2126 Stream_Write_UINT32(s, (UINT32)blockLen);
2127 Stream_Write_UINT8(s, 1);
2128 Stream_Write_UINT8(s, 0);
2129 Stream_Write_UINT8(s, 1);
2130 Stream_Write_UINT16(s, message->numRects);
2132 for (UINT16 i = 0; i < message->numRects; i++)
2134 const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2138 Stream_Write_UINT16(s, rect->x);
2139 Stream_Write_UINT16(s, rect->y);
2140 Stream_Write_UINT16(s, rect->width);
2141 Stream_Write_UINT16(s, rect->height);
2144 Stream_Write_UINT16(s, CBT_REGION);
2145 Stream_Write_UINT16(s, 1);
2149 static INLINE BOOL rfx_write_message_frame_end(RFX_CONTEXT* WINPR_RESTRICT context,
2151 const RFX_MESSAGE* WINPR_RESTRICT message)
2153 WINPR_ASSERT(context);
2154 WINPR_ASSERT(message);
2156 if (!Stream_EnsureRemainingCapacity(s, 8))
2159 Stream_Write_UINT16(s, WBT_FRAME_END);
2160 Stream_Write_UINT32(s, 8);
2161 Stream_Write_UINT8(s, 1);
2162 Stream_Write_UINT8(s, 0);
2166 BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2167 const RFX_MESSAGE* WINPR_RESTRICT message)
2169 WINPR_ASSERT(context);
2170 WINPR_ASSERT(message);
2172 if (context->state == RFX_STATE_SEND_HEADERS)
2174 if (!rfx_compose_message_header(context, s))
2177 context->state = RFX_STATE_SEND_FRAME_DATA;
2180 if (!rfx_write_message_frame_begin(context, s, message) ||
2181 !rfx_write_message_region(context, s, message) ||
2182 !rfx_write_message_tileset(context, s, message) ||
2183 !rfx_write_message_frame_end(context, s, message))
2191 BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2192 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2193 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2196 WINPR_ASSERT(context);
2197 RFX_MESSAGE* message =
2198 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2202 const BOOL ret = rfx_write_message(context, s, message);
2203 rfx_message_free(context, message);
2207 BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2209 WINPR_ASSERT(context);
2210 context->mode = mode;
2214 RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2216 WINPR_ASSERT(context);
2217 return context->mode;
2220 UINT32 rfx_context_get_frame_idx(
const RFX_CONTEXT* WINPR_RESTRICT context)
2222 WINPR_ASSERT(context);
2223 return context->frameIdx;
2226 UINT32 rfx_message_get_frame_idx(
const RFX_MESSAGE* WINPR_RESTRICT message)
2228 WINPR_ASSERT(message);
2229 return message->frameIdx;
2232 static INLINE BOOL rfx_write_progressive_wb_sync(RFX_CONTEXT* WINPR_RESTRICT rfx,
2235 const UINT32 blockLen = 12;
2239 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2242 Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC);
2243 Stream_Write_UINT32(s, blockLen);
2244 Stream_Write_UINT32(s, 0xCACCACCA);
2245 Stream_Write_UINT16(s, 0x0100);
2249 static INLINE BOOL rfx_write_progressive_wb_context(RFX_CONTEXT* WINPR_RESTRICT rfx,
2252 const UINT32 blockLen = 10;
2256 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2259 Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT);
2260 Stream_Write_UINT32(s, blockLen);
2261 Stream_Write_UINT8(s, 0);
2262 Stream_Write_UINT16(s, 64);
2263 Stream_Write_UINT8(s, 0);
2267 static INLINE BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2269 const RFX_MESSAGE* WINPR_RESTRICT msg)
2272 UINT32 blockLen = 18;
2273 UINT32 tilesDataSize = 0;
2274 const size_t start = Stream_GetPosition(s);
2280 blockLen += msg->numRects * 8;
2281 blockLen += msg->numQuant * 5;
2282 tilesDataSize = msg->numTiles * 22UL;
2283 for (UINT16 i = 0; i < msg->numTiles; i++)
2285 const RFX_TILE* tile = msg->tiles[i];
2287 tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2289 blockLen += tilesDataSize;
2291 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2294 Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION);
2295 Stream_Write_UINT32(s, blockLen);
2296 Stream_Write_UINT8(s, 64);
2297 Stream_Write_UINT16(s, msg->numRects);
2298 WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2299 Stream_Write_UINT8(s, (UINT8)msg->numQuant);
2300 Stream_Write_UINT8(s, 0);
2301 Stream_Write_UINT8(s, 0);
2302 Stream_Write_UINT16(s, msg->numTiles);
2303 Stream_Write_UINT32(s, tilesDataSize);
2305 for (UINT16 i = 0; i < msg->numRects; i++)
2308 const RFX_RECT* r = &msg->rects[i];
2309 Stream_Write_UINT16(s, r->x);
2310 Stream_Write_UINT16(s, r->y);
2311 Stream_Write_UINT16(s, r->width);
2312 Stream_Write_UINT16(s, r->height);
2323 for (UINT16 i = 0; i < msg->numQuant; i++)
2325 const UINT32* qv = &msg->quantVals[10ULL * i];
2327 Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4)));
2328 Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4)));
2329 Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4)));
2330 Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4)));
2331 Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4)));
2334 for (UINT16 i = 0; i < msg->numTiles; i++)
2336 const RFX_TILE* tile = msg->tiles[i];
2337 if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2341 const size_t end = Stream_GetPosition(s);
2342 const size_t used = end - start;
2343 return (used == blockLen);
2346 static INLINE BOOL rfx_write_progressive_frame_begin(RFX_CONTEXT* WINPR_RESTRICT rfx,
2348 const RFX_MESSAGE* WINPR_RESTRICT msg)
2350 const UINT32 blockLen = 12;
2355 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2358 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN);
2359 Stream_Write_UINT32(s, blockLen);
2360 Stream_Write_UINT32(s, msg->frameIdx);
2361 Stream_Write_UINT16(s, 1);
2366 static INLINE BOOL rfx_write_progressive_frame_end(RFX_CONTEXT* WINPR_RESTRICT rfx,
2369 const UINT32 blockLen = 6;
2373 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2376 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END);
2377 Stream_Write_UINT32(s, blockLen);
2382 static INLINE BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
2384 const RFX_TILE* WINPR_RESTRICT tile)
2386 UINT32 blockLen = 0;
2391 blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2392 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2395 Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE);
2396 Stream_Write_UINT32(s, blockLen);
2397 Stream_Write_UINT8(s, tile->quantIdxY);
2398 Stream_Write_UINT8(s, tile->quantIdxCb);
2399 Stream_Write_UINT8(s, tile->quantIdxCr);
2400 Stream_Write_UINT16(s, tile->xIdx);
2401 Stream_Write_UINT16(s, tile->yIdx);
2402 Stream_Write_UINT8(s, 0);
2403 Stream_Write_UINT16(s, tile->YLen);
2404 Stream_Write_UINT16(s, tile->CbLen);
2405 Stream_Write_UINT16(s, tile->CrLen);
2406 Stream_Write_UINT16(s, 0);
2407 Stream_Write(s, tile->YData, tile->YLen);
2408 Stream_Write(s, tile->CbData, tile->CbLen);
2409 Stream_Write(s, tile->CrData, tile->CrLen);
2414 const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2418 case PROGRESSIVE_WBT_SYNC:
2419 return "PROGRESSIVE_WBT_SYNC";
2421 case PROGRESSIVE_WBT_FRAME_BEGIN:
2422 return "PROGRESSIVE_WBT_FRAME_BEGIN";
2424 case PROGRESSIVE_WBT_FRAME_END:
2425 return "PROGRESSIVE_WBT_FRAME_END";
2427 case PROGRESSIVE_WBT_CONTEXT:
2428 return "PROGRESSIVE_WBT_CONTEXT";
2430 case PROGRESSIVE_WBT_REGION:
2431 return "PROGRESSIVE_WBT_REGION";
2433 case PROGRESSIVE_WBT_TILE_SIMPLE:
2434 return "PROGRESSIVE_WBT_TILE_SIMPLE";
2436 case PROGRESSIVE_WBT_TILE_FIRST:
2437 return "PROGRESSIVE_WBT_TILE_FIRST";
2439 case PROGRESSIVE_WBT_TILE_UPGRADE:
2440 return "PROGRESSIVE_WBT_TILE_UPGRADE";
2443 return "PROGRESSIVE_WBT_UNKNOWN";
2447 BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT context,
2449 const RFX_MESSAGE* WINPR_RESTRICT msg)
2453 WINPR_ASSERT(context);
2455 if (context->mode != RLGR1)
2457 WLog_ERR(TAG,
"error, RLGR1 mode is required!");
2461 if (!rfx_write_progressive_wb_sync(context, s))
2464 if (!rfx_write_progressive_wb_context(context, s))
2467 if (!rfx_write_progressive_frame_begin(context, s, msg))
2470 if (!rfx_write_progressive_region(context, s, msg))
2473 if (!rfx_write_progressive_frame_end(context, s))
This struct contains function pointer to initialize/free objects.