22#include <freerdp/config.h>
28#include <winpr/assert.h>
29#include <winpr/cast.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"
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"
70static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
72static INLINE BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
74 const RFX_TILE* WINPR_RESTRICT tile);
76static 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")
97static 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)
118static 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
142static INLINE
void rfx_tile_init(
void* obj)
158static 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;
177static INLINE
void rfx_decoder_tile_free(
void* obj)
184 winpr_aligned_free(tile->data);
186 winpr_aligned_free(tile);
190static INLINE
void* rfx_encoder_tile_new(
const void* val)
193 return winpr_aligned_calloc(1,
sizeof(
RFX_TILE), 32);
196static INLINE
void rfx_encoder_tile_free(
void* obj)
198 winpr_aligned_free(obj);
201RFX_CONTEXT* rfx_context_new(BOOL encoder)
203 return rfx_context_new_ex(encoder, 0);
206RFX_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
348void 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);
391static 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];
399static 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];
408static 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];
416void 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);
424UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context)
426 WINPR_ASSERT(context);
427 return context->pixel_format;
430void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context,
431 const BYTE* WINPR_RESTRICT palette)
433 WINPR_ASSERT(context);
434 context->palette = palette;
437const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context)
439 WINPR_ASSERT(context);
440 return context->palette;
443BOOL 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;
456static 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;
489static 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;
533static 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;
590static 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;
643static INLINE BOOL rfx_process_message_frame_begin(
644 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
645 wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
648 UINT16 numRegions = 0;
650 WINPR_ASSERT(context);
651 WINPR_ASSERT(context->priv);
652 WINPR_ASSERT(message);
653 WINPR_ASSERT(pExpectedBlockType);
655 if (*pExpectedBlockType != WBT_FRAME_BEGIN)
657 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_FRAME_BEGIN");
661 *pExpectedBlockType = WBT_REGION;
663 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
668 Stream_Read_UINT16(s, numRegions);
669 WLog_Print(context->priv->log, WLOG_DEBUG,
670 "RFX_FRAME_BEGIN: frameIdx: %" PRIu32
" numRegions: %" PRIu16
"", frameIdx,
675static INLINE BOOL rfx_process_message_frame_end(
676 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
677 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
679 WINPR_ASSERT(context);
680 WINPR_ASSERT(context->priv);
681 WINPR_ASSERT(message);
683 WINPR_ASSERT(pExpectedBlockType);
685 if (*pExpectedBlockType != WBT_FRAME_END)
687 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected, wants WBT_FRAME_END");
691 *pExpectedBlockType = WBT_FRAME_BEGIN;
692 WLog_Print(context->priv->log, WLOG_DEBUG,
"RFX_FRAME_END");
696static INLINE BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message)
698 WINPR_ASSERT(message);
701 winpr_aligned_recalloc(message->rects, message->numRects,
sizeof(
RFX_RECT), 32);
704 message->rects = tmpRects;
708static INLINE BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
709 RFX_MESSAGE* WINPR_RESTRICT message,
711 UINT16* WINPR_RESTRICT pExpectedBlockType)
713 UINT16 regionType = 0;
714 UINT16 numTileSets = 0;
716 WINPR_ASSERT(context);
717 WINPR_ASSERT(context->priv);
718 WINPR_ASSERT(message);
719 WINPR_ASSERT(pExpectedBlockType);
721 if (*pExpectedBlockType != WBT_REGION)
723 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_REGION");
727 *pExpectedBlockType = WBT_EXTENSION;
729 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3))
732 Stream_Seek_UINT8(s);
733 Stream_Read_UINT16(s, message->numRects);
735 if (message->numRects < 1)
743 message->numRects = 1;
744 if (!rfx_resize_rects(message))
747 message->rects->x = 0;
748 message->rects->y = 0;
749 message->rects->width = context->width;
750 message->rects->height = context->height;
754 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull))
757 if (!rfx_resize_rects(message))
761 for (UINT16 i = 0; i < message->numRects; i++)
763 RFX_RECT* rect = rfx_message_get_rect(message, i);
765 Stream_Read_UINT16(s, rect->x);
766 Stream_Read_UINT16(s, rect->y);
767 Stream_Read_UINT16(s, rect->width);
768 Stream_Read_UINT16(s, rect->height);
769 WLog_Print(context->priv->log, WLOG_DEBUG,
770 "rect %" PRIu16
" (x,y=%" PRIu16
",%" PRIu16
" w,h=%" PRIu16
" %" PRIu16
").", i,
771 rect->x, rect->y, rect->width, rect->height);
774 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
777 Stream_Read_UINT16(s, regionType);
778 Stream_Read_UINT16(s, numTileSets);
780 if (regionType != CBT_REGION)
782 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid region type 0x%04" PRIX16
"",
787 if (numTileSets != 0x0001)
789 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid number of tilesets (%" PRIu16
")",
800 RFX_CONTEXT* context;
801} RFX_TILE_PROCESS_WORK_PARAM;
803static INLINE
void CALLBACK
804rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
805 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
807 RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
809 rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4);
812static INLINE BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count,
815 WINPR_ASSERT(message);
818 (
RFX_TILE**)winpr_aligned_recalloc((
void*)message->tiles, count,
sizeof(
RFX_TILE*), 32);
819 if (!tmpTiles && (count != 0))
822 message->tiles = tmpTiles;
824 message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count);
827 WINPR_ASSERT(message->numTiles <= count);
829 message->allocatedTiles = count;
834static INLINE BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
835 RFX_MESSAGE* WINPR_RESTRICT message,
837 UINT16* WINPR_RESTRICT pExpectedBlockType)
840 size_t close_cnt = 0;
843 UINT32* quants = NULL;
847 UINT32 blockType = 0;
848 UINT32 tilesDataSize = 0;
849 PTP_WORK* work_objects = NULL;
850 RFX_TILE_PROCESS_WORK_PARAM* params = NULL;
853 WINPR_ASSERT(context);
854 WINPR_ASSERT(context->priv);
855 WINPR_ASSERT(message);
856 WINPR_ASSERT(pExpectedBlockType);
858 if (*pExpectedBlockType != WBT_EXTENSION)
860 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants a tileset");
864 *pExpectedBlockType = WBT_FRAME_END;
866 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14))
869 Stream_Read_UINT16(s, subtype);
870 if (subtype != CBT_TILESET)
872 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid subtype, expected CBT_TILESET.");
876 Stream_Seek_UINT16(s);
877 Stream_Seek_UINT16(s);
878 Stream_Read_UINT8(s, context->numQuant);
879 Stream_Seek_UINT8(s);
881 if (context->numQuant < 1)
883 WLog_Print(context->priv->log, WLOG_ERROR,
"no quantization value.");
887 Stream_Read_UINT16(s, numTiles);
894 Stream_Read_UINT32(s, tilesDataSize);
897 winpr_aligned_recalloc(context->quants, context->numQuant, 10 *
sizeof(UINT32), 32)))
900 quants = context->quants = (UINT32*)pmem;
903 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull))
906 for (
size_t i = 0; i < context->numQuant; i++)
909 Stream_Read_UINT8(s, quant);
910 *quants++ = (quant & 0x0F);
911 *quants++ = (quant >> 4);
912 Stream_Read_UINT8(s, quant);
913 *quants++ = (quant & 0x0F);
914 *quants++ = (quant >> 4);
915 Stream_Read_UINT8(s, quant);
916 *quants++ = (quant & 0x0F);
917 *quants++ = (quant >> 4);
918 Stream_Read_UINT8(s, quant);
919 *quants++ = (quant & 0x0F);
920 *quants++ = (quant >> 4);
921 Stream_Read_UINT8(s, quant);
922 *quants++ = (quant & 0x0F);
923 *quants++ = (quant >> 4);
924 WLog_Print(context->priv->log, WLOG_DEBUG,
925 "quant %d (%" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
926 " %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
").",
927 i, context->quants[i * 10], context->quants[i * 10 + 1],
928 context->quants[i * 10 + 2], context->quants[i * 10 + 3],
929 context->quants[i * 10 + 4], context->quants[i * 10 + 5],
930 context->quants[i * 10 + 6], context->quants[i * 10 + 7],
931 context->quants[i * 10 + 8], context->quants[i * 10 + 9]);
934 for (
size_t i = 0; i < message->numTiles; i++)
936 ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
937 message->tiles[i] = NULL;
940 if (!rfx_allocate_tiles(message, numTiles, FALSE))
943 if (context->priv->UseThreads)
945 work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles,
sizeof(PTP_WORK), 32);
946 params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc(
947 NULL, message->numTiles,
sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32);
951 winpr_aligned_free(params);
957 winpr_aligned_free((
void*)work_objects);
966 if (Stream_GetRemainingLength(s) >= tilesDataSize)
969 for (
size_t i = 0; i < message->numTiles; i++)
974 if (!(tile = (
RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
976 WLog_Print(context->priv->log, WLOG_ERROR,
977 "RfxMessageTileSet failed to get tile from object pool");
982 message->tiles[i] = tile;
985 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
987 WLog_Print(context->priv->log, WLOG_ERROR,
988 "RfxMessageTileSet packet too small to read tile %d/%" PRIu16
"", i,
994 sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
997 Stream_Read_UINT32(sub, blockLen);
999 if (!Stream_SafeSeek(s, blockLen))
1004 if ((blockLen < 6 + 13) ||
1005 (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6)))
1007 WLog_Print(context->priv->log, WLOG_ERROR,
1008 "RfxMessageTileSet not enough bytes to read tile %d/%" PRIu16
1009 " with blocklen=%" PRIu32
"",
1010 i, message->numTiles, blockLen);
1015 if (blockType != CBT_TILE)
1017 WLog_Print(context->priv->log, WLOG_ERROR,
1018 "unknown block type 0x%" PRIX32
", expected CBT_TILE (0xCAC3).",
1024 Stream_Read_UINT8(sub, tile->quantIdxY);
1025 Stream_Read_UINT8(sub, tile->quantIdxCb);
1026 Stream_Read_UINT8(sub, tile->quantIdxCr);
1027 if (tile->quantIdxY >= context->numQuant)
1029 WLog_Print(context->priv->log, WLOG_ERROR,
1030 "quantIdxY %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxY,
1035 if (tile->quantIdxCb >= context->numQuant)
1037 WLog_Print(context->priv->log, WLOG_ERROR,
1038 "quantIdxCb %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCb,
1043 if (tile->quantIdxCr >= context->numQuant)
1045 WLog_Print(context->priv->log, WLOG_ERROR,
1046 "quantIdxCr %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCr,
1052 Stream_Read_UINT16(sub, tile->xIdx);
1053 Stream_Read_UINT16(sub, tile->yIdx);
1054 Stream_Read_UINT16(sub, tile->YLen);
1055 Stream_Read_UINT16(sub, tile->CbLen);
1056 Stream_Read_UINT16(sub, tile->CrLen);
1057 Stream_GetPointer(sub, tile->YData);
1058 if (!Stream_SafeSeek(sub, tile->YLen))
1063 Stream_GetPointer(sub, tile->CbData);
1064 if (!Stream_SafeSeek(sub, tile->CbLen))
1069 Stream_GetPointer(sub, tile->CrData);
1070 if (!Stream_SafeSeek(sub, tile->CrLen))
1075 tile->x = tile->xIdx * 64;
1076 tile->y = tile->yIdx * 64;
1078 if (context->priv->UseThreads)
1086 params[i].context = context;
1087 params[i].tile = message->tiles[i];
1089 if (!(work_objects[i] =
1090 CreateThreadpoolWork(rfx_process_message_tile_work_callback,
1091 (
void*)¶ms[i], &context->priv->ThreadPoolEnv)))
1093 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThreadpoolWork failed.");
1098 SubmitThreadpoolWork(work_objects[i]);
1103 rfx_decode_rgb(context, tile, tile->data, 64 * 4);
1108 if (context->priv->UseThreads)
1110 for (
size_t i = 0; i < close_cnt; i++)
1112 WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
1113 CloseThreadpoolWork(work_objects[i]);
1117 winpr_aligned_free((
void*)work_objects);
1118 winpr_aligned_free(params);
1120 for (
size_t i = 0; i < message->numTiles; i++)
1122 if (!(tile = message->tiles[i]))
1125 tile->YLen = tile->CbLen = tile->CrLen = 0;
1126 tile->YData = tile->CbData = tile->CrData = NULL;
1132BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT data,
1133 UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst,
1134 UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight,
1135 REGION16* WINPR_RESTRICT invalidRegion)
1141 if (!context || !data || !length)
1144 WINPR_ASSERT(context->priv);
1145 RFX_MESSAGE* message = &context->currentMessage;
1147 wStream* s = Stream_StaticConstInit(&inStream, data, length);
1149 while (ok && Stream_GetRemainingLength(s) > 6)
1151 wStream subStreamBuffer = { 0 };
1152 size_t extraBlockLen = 0;
1153 UINT32 blockLen = 0;
1154 UINT32 blockType = 0;
1157 Stream_Read_UINT16(s, blockType);
1158 Stream_Read_UINT32(s, blockLen);
1159 WLog_Print(context->priv->log, WLOG_DEBUG,
"blockType 0x%" PRIX32
" blockLen %" PRIu32
"",
1160 blockType, blockLen);
1164 WLog_Print(context->priv->log, WLOG_ERROR,
"blockLen too small(%" PRIu32
")", blockLen);
1168 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6))
1171 if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS)
1173 WLog_Print(context->priv->log, WLOG_ERROR,
"incomplete header blocks processing");
1177 if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
1181 UINT8 channelId = 0;
1183 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1187 Stream_Read_UINT8(s, codecId);
1188 Stream_Read_UINT8(s, channelId);
1190 if (codecId != 0x01)
1192 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codecId 0x%02" PRIX8
"",
1197 if (blockType == WBT_CONTEXT)
1200 if (channelId != 0xFF)
1202 WLog_Print(context->priv->log, WLOG_ERROR,
1203 "invalid channelId 0x%02" PRIX8
" for blockType 0x%08" PRIX32
"",
1204 channelId, blockType);
1211 if (channelId != 0x00)
1213 WLog_Print(context->priv->log, WLOG_ERROR,
1214 "invalid channelId 0x%02" PRIX8
" for blockType WBT_CONTEXT",
1221 const size_t blockLenNoHeader = blockLen - 6;
1222 if (blockLenNoHeader < extraBlockLen)
1224 WLog_Print(context->priv->log, WLOG_ERROR,
1225 "blockLen too small(%" PRIu32
"), must be >= 6 + %" PRIu16, blockLen,
1230 const size_t subStreamLen = blockLenNoHeader - extraBlockLen;
1231 wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen);
1232 Stream_Seek(s, subStreamLen);
1241 ok = rfx_process_message_sync(context, subStream);
1245 ok = rfx_process_message_context(context, subStream);
1248 case WBT_CODEC_VERSIONS:
1249 ok = rfx_process_message_codec_versions(context, subStream);
1253 ok = rfx_process_message_channels(context, subStream);
1263 case WBT_FRAME_BEGIN:
1264 ok = rfx_process_message_frame_begin(context, message, subStream,
1265 &context->expectedDataBlockType);
1269 ok = rfx_process_message_region(context, message, subStream,
1270 &context->expectedDataBlockType);
1274 ok = rfx_process_message_tileset(context, message, subStream,
1275 &context->expectedDataBlockType);
1279 ok = rfx_process_message_frame_end(context, message, subStream,
1280 &context->expectedDataBlockType);
1284 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown blockType 0x%" PRIX32
"",
1292 UINT32 nbUpdateRects = 0;
1295 const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format);
1296 const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat);
1297 region16_init(&clippingRects);
1299 WINPR_ASSERT(dstWidth <= UINT16_MAX);
1300 WINPR_ASSERT(dstHeight <= UINT16_MAX);
1301 for (UINT32 i = 0; i < message->numRects; i++)
1304 const RFX_RECT* rect = &(message->rects[i]);
1306 WINPR_ASSERT(left + rect->x <= UINT16_MAX);
1307 WINPR_ASSERT(top + rect->y <= UINT16_MAX);
1308 WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX);
1309 WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX);
1311 const UINT32 rw = 1UL * clippingRect.left + rect->width;
1312 const UINT32 rh = 1UL * clippingRect.top + rect->width;
1313 const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth));
1314 const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight));
1315 clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth));
1316 clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight));
1317 clippingRect.right = right;
1318 clippingRect.bottom = bottom;
1319 region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
1322 for (UINT32 i = 0; i < message->numTiles; i++)
1325 const RFX_TILE* tile = rfx_message_get_tile(message, i);
1327 WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1328 WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1330 updateRect.left = (UINT16)left + tile->x;
1331 updateRect.top = (UINT16)top + tile->y;
1332 updateRect.right = updateRect.left + 64;
1333 updateRect.bottom = updateRect.top + 64;
1334 region16_init(&updateRegion);
1335 region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
1336 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1338 for (UINT32 j = 0; j < nbUpdateRects; j++)
1340 const UINT32 stride = 64 * formatSize;
1341 const UINT32 nXDst = updateRects[j].left;
1342 const UINT32 nYDst = updateRects[j].top;
1343 const UINT32 nXSrc = nXDst - updateRect.left;
1344 const UINT32 nYSrc = nYDst - updateRect.top;
1345 const UINT32 nWidth = updateRects[j].right - updateRects[j].left;
1346 const UINT32 nHeight = updateRects[j].bottom - updateRects[j].top;
1348 if (!freerdp_image_copy_no_overlap(dst, dstFormat, dstStride, nXDst, nYDst, nWidth,
1349 nHeight, tile->data, context->pixel_format,
1350 stride, nXSrc, nYSrc, NULL, FREERDP_FLIP_NONE))
1352 region16_uninit(&updateRegion);
1353 region16_uninit(&clippingRects);
1354 WLog_Print(context->priv->log, WLOG_ERROR,
1355 "nbUpdateRectx[%" PRIu32
" (%" PRIu32
")] freerdp_image_copy failed",
1361 region16_union_rect(invalidRegion, invalidRegion, &updateRects[j]);
1364 region16_uninit(&updateRegion);
1367 region16_uninit(&clippingRects);
1372 rfx_message_free(context, message);
1373 context->currentMessage.freeArray = TRUE;
1376 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1380const UINT32* rfx_message_get_quants(
const RFX_MESSAGE* WINPR_RESTRICT message,
1381 UINT16* WINPR_RESTRICT numQuantVals)
1383 WINPR_ASSERT(message);
1385 *numQuantVals = message->numQuant;
1386 return message->quantVals;
1389const RFX_TILE** rfx_message_get_tiles(
const RFX_MESSAGE* WINPR_RESTRICT message,
1390 UINT16* WINPR_RESTRICT numTiles)
1392 WINPR_ASSERT(message);
1394 *numTiles = message->numTiles;
1401 cnv.pp = message->tiles;
1405UINT16 rfx_message_get_tile_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1407 WINPR_ASSERT(message);
1408 return message->numTiles;
1411const RFX_RECT* rfx_message_get_rects(
const RFX_MESSAGE* WINPR_RESTRICT message,
1412 UINT16* WINPR_RESTRICT numRects)
1414 WINPR_ASSERT(message);
1416 *numRects = message->numRects;
1417 return message->rects;
1420UINT16 rfx_message_get_rect_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1422 WINPR_ASSERT(message);
1423 return message->numRects;
1426void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1431 winpr_aligned_free(message->rects);
1435 for (
size_t i = 0; i < message->numTiles; i++)
1437 RFX_TILE* tile = message->tiles[i];
1441 if (tile->YCbCrData)
1443 BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1444 tile->YCbCrData = NULL;
1447 ObjectPool_Return(context->priv->TilePool, (
void*)tile);
1450 rfx_allocate_tiles(message, 0, FALSE);
1453 const BOOL freeArray = message->freeArray;
1454 const RFX_MESSAGE empty = { 0 };
1458 winpr_aligned_free(message);
1461static INLINE
void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1463 UINT16 properties = 0;
1465 WINPR_ASSERT(context);
1468 properties |= (context->flags << 1);
1469 properties |= (COL_CONV_ICT << 4);
1470 properties |= (CLW_XFORM_DWT_53_A << 6);
1471 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10);
1472 properties |= (SCALAR_QUANTIZATION << 14);
1473 context->properties = properties;
1477rfx_write_message_sync(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1478 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s)
1480 WINPR_ASSERT(context);
1482 Stream_Write_UINT16(s, WBT_SYNC);
1483 Stream_Write_UINT32(s, 12);
1484 Stream_Write_UINT32(s, WF_MAGIC);
1485 Stream_Write_UINT16(s, WF_VERSION_1_0);
1489rfx_write_message_codec_versions(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1492 WINPR_ASSERT(context);
1494 Stream_Write_UINT16(s, WBT_CODEC_VERSIONS);
1495 Stream_Write_UINT32(s, 10);
1496 Stream_Write_UINT8(s, 1);
1497 Stream_Write_UINT8(s, 1);
1498 Stream_Write_UINT16(s, WF_VERSION_1_0);
1501static INLINE
void rfx_write_message_channels(
const RFX_CONTEXT* WINPR_RESTRICT context,
1504 WINPR_ASSERT(context);
1506 Stream_Write_UINT16(s, WBT_CHANNELS);
1507 Stream_Write_UINT32(s, 12);
1508 Stream_Write_UINT8(s, 1);
1509 Stream_Write_UINT8(s, 0);
1510 Stream_Write_UINT16(s, context->width);
1511 Stream_Write_UINT16(s, context->height);
1514static INLINE
void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1517 UINT16 properties = 0;
1518 WINPR_ASSERT(context);
1520 Stream_Write_UINT16(s, WBT_CONTEXT);
1521 Stream_Write_UINT32(s, 13);
1522 Stream_Write_UINT8(s, 1);
1523 Stream_Write_UINT8(s, 0xFF);
1524 Stream_Write_UINT8(s, 0);
1525 Stream_Write_UINT16(s, CT_TILE_64x64);
1527 properties = context->flags;
1528 properties |= (COL_CONV_ICT << 3);
1529 properties |= (CLW_XFORM_DWT_53_A << 5);
1530 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9);
1531 properties |= (SCALAR_QUANTIZATION << 13);
1532 Stream_Write_UINT16(s, properties);
1533 rfx_update_context_properties(context);
1536static INLINE BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1539 WINPR_ASSERT(context);
1540 if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
1543 rfx_write_message_sync(context, s);
1544 rfx_write_message_context(context, s);
1545 rfx_write_message_codec_versions(context, s);
1546 rfx_write_message_channels(context, s);
1550static INLINE
size_t rfx_tile_length(
const RFX_TILE* WINPR_RESTRICT tile)
1553 return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1556static INLINE BOOL rfx_write_tile(
wStream* WINPR_RESTRICT s,
const RFX_TILE* WINPR_RESTRICT tile)
1558 const size_t blockLen = rfx_tile_length(tile);
1559 if (blockLen > UINT32_MAX)
1561 if (!Stream_EnsureRemainingCapacity(s, blockLen))
1564 Stream_Write_UINT16(s, CBT_TILE);
1565 Stream_Write_UINT32(s, (UINT32)blockLen);
1566 Stream_Write_UINT8(s, tile->quantIdxY);
1567 Stream_Write_UINT8(s, tile->quantIdxCb);
1568 Stream_Write_UINT8(s, tile->quantIdxCr);
1569 Stream_Write_UINT16(s, tile->xIdx);
1570 Stream_Write_UINT16(s, tile->yIdx);
1571 Stream_Write_UINT16(s, tile->YLen);
1572 Stream_Write_UINT16(s, tile->CbLen);
1573 Stream_Write_UINT16(s, tile->CrLen);
1574 Stream_Write(s, tile->YData, tile->YLen);
1575 Stream_Write(s, tile->CbData, tile->CbLen);
1576 Stream_Write(s, tile->CrData, tile->CrLen);
1580struct S_RFX_TILE_COMPOSE_WORK_PARAM
1583 RFX_CONTEXT* context;
1586static INLINE
void CALLBACK
1587rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
1588 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
1590 RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1591 WINPR_ASSERT(param);
1592 rfx_encode_rgb(param->context, param->tile);
1595static INLINE BOOL computeRegion(
const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1596 REGION16* WINPR_RESTRICT region,
size_t width,
size_t height)
1598 const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1599 WINPR_ASSERTING_INT_CAST(UINT16, height) };
1601 WINPR_ASSERT(rects);
1602 for (
size_t i = 0; i < numRects; i++)
1606 rect16.left = rect->x;
1607 rect16.top = rect->y;
1608 rect16.right = rect->x + rect->width;
1609 rect16.bottom = rect->y + rect->height;
1611 if (!region16_union_rect(region, region, &rect16))
1615 return region16_intersect_rect(region, region, &mainRect);
1618#define TILE_NO(v) ((v) / 64)
1620static INLINE BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context,
size_t nbTiles)
1622 WINPR_ASSERT(context);
1624 RFX_CONTEXT_PRIV* priv = context->priv;
1629 if (!context->priv->UseThreads)
1632 if (!(pmem = winpr_aligned_recalloc((
void*)priv->workObjects, nbTiles,
sizeof(PTP_WORK), 32)))
1635 priv->workObjects = (PTP_WORK*)pmem;
1637 if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1638 sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1641 priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1645static INLINE BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count)
1647 WINPR_ASSERT(message);
1649 if (message->numTiles + count <= message->allocatedTiles)
1652 const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1653 return rfx_allocate_tiles(message, alloc, TRUE);
1656RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1657 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1658 const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h,
size_t s)
1660 const UINT32 width = w;
1661 const UINT32 height = h;
1662 const UINT32 scanline = (UINT32)s;
1663 RFX_MESSAGE* message = NULL;
1664 PTP_WORK* workObject = NULL;
1665 RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL;
1666 BOOL success = FALSE;
1673 WINPR_ASSERT(rects);
1674 WINPR_ASSERT(numRects > 0);
1675 WINPR_ASSERT(w > 0);
1676 WINPR_ASSERT(h > 0);
1677 WINPR_ASSERT(s > 0);
1679 if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1,
sizeof(RFX_MESSAGE), 32)))
1682 region16_init(&tilesRegion);
1683 region16_init(&rectsRegion);
1685 if (context->state == RFX_STATE_SEND_HEADERS)
1686 rfx_update_context_properties(context);
1688 message->frameIdx = context->frameIdx++;
1690 if (!context->numQuant)
1692 WINPR_ASSERT(context->quants == NULL);
1693 if (!(context->quants =
1694 (UINT32*)winpr_aligned_malloc(
sizeof(rfx_default_quantization_values), 32)))
1695 goto skip_encoding_loop;
1697 CopyMemory(context->quants, &rfx_default_quantization_values,
1698 sizeof(rfx_default_quantization_values));
1699 context->numQuant = 1;
1700 context->quantIdxY = 0;
1701 context->quantIdxCb = 0;
1702 context->quantIdxCr = 0;
1705 message->numQuant = context->numQuant;
1706 message->quantVals = context->quants;
1707 const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1709 if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1710 goto skip_encoding_loop;
1712 const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1713 WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1714 WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1715 const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1716 const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1717 const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1719 if (!rfx_ensure_tiles(message, maxNbTiles))
1720 goto skip_encoding_loop;
1722 if (!setupWorkers(context, maxNbTiles))
1723 goto skip_encoding_loop;
1725 if (context->priv->UseThreads)
1727 workObject = context->priv->workObjects;
1728 workParam = context->priv->tileWorkParams;
1731 UINT32 regionNbRects = 0;
1732 regionRect = region16_rects(&rectsRegion, ®ionNbRects);
1734 if (!(message->rects = winpr_aligned_calloc(regionNbRects,
sizeof(
RFX_RECT), 32)))
1735 goto skip_encoding_loop;
1737 message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1739 for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1741 RFX_RECT* rfxRect = &message->rects[i];
1742 UINT32 startTileX = regionRect->left / 64;
1743 UINT32 endTileX = (regionRect->right - 1) / 64;
1744 UINT32 startTileY = regionRect->top / 64;
1745 UINT32 endTileY = (regionRect->bottom - 1) / 64;
1746 rfxRect->x = regionRect->left;
1747 rfxRect->y = regionRect->top;
1748 rfxRect->width = (regionRect->right - regionRect->left);
1749 rfxRect->height = (regionRect->bottom - regionRect->top);
1751 for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1752 yIdx++, gridRelY += 64)
1754 UINT32 tileHeight = 64;
1756 if ((yIdx == endTileY) && (gridRelY + 64 > height))
1757 tileHeight = height - gridRelY;
1759 currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1760 currentTileRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1762 for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1763 xIdx++, gridRelX += 64)
1770 UINT32 tileWidth = 64;
1772 if ((xIdx == endTileX) && (gridRelX + 64 > width))
1774 tileWidth = (width - gridRelX);
1777 currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1778 currentTileRect.right = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1781 if (region16_intersects_rect(&tilesRegion, ¤tTileRect))
1786 goto skip_encoding_loop;
1788 tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1789 tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1790 tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1791 tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1792 tile->scanline = scanline;
1794 tile->width = tileWidth;
1795 tile->height = tileHeight;
1796 const UINT32 ax = gridRelX;
1797 const UINT32 ay = gridRelY;
1799 if (tile->data && tile->allocated)
1801 winpr_aligned_free(tile->data);
1802 tile->allocated = FALSE;
1806 cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1807 tile->data = cnv.pv;
1808 tile->quantIdxY = context->quantIdxY;
1809 tile->quantIdxCb = context->quantIdxCb;
1810 tile->quantIdxCr = context->quantIdxCr;
1811 tile->YLen = tile->CbLen = tile->CrLen = 0;
1813 if (!(tile->YCbCrData = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1814 goto skip_encoding_loop;
1816 tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1817 tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1818 tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1820 if (!rfx_ensure_tiles(message, 1))
1821 goto skip_encoding_loop;
1822 message->tiles[message->numTiles++] = tile;
1824 if (context->priv->UseThreads)
1826 workParam->context = context;
1827 workParam->tile = tile;
1829 if (!(*workObject = CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1831 &context->priv->ThreadPoolEnv)))
1833 goto skip_encoding_loop;
1836 SubmitThreadpoolWork(*workObject);
1842 rfx_encode_rgb(context, tile);
1845 if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect))
1846 goto skip_encoding_loop;
1857 message->tilesDataSize = 0;
1858 workObject = context->priv->workObjects;
1860 for (UINT32 i = 0; i < message->numTiles; i++)
1862 if (context->priv->UseThreads)
1866 WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1867 CloseThreadpoolWork(*workObject);
1873 const RFX_TILE* tile = message->tiles[i];
1874 const size_t tlen = rfx_tile_length(tile);
1875 message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen);
1878 region16_uninit(&tilesRegion);
1879 region16_uninit(&rectsRegion);
1884 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1886 rfx_message_free(context, message);
1887 region16_uninit(&tilesRegion);
1888 region16_uninit(&rectsRegion);
1892static INLINE BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1893 const RFX_MESSAGE* WINPR_RESTRICT src)
1898 WINPR_ASSERT(dst->rects == NULL);
1899 WINPR_ASSERT(dst->numRects == 0);
1901 if (src->numRects == 0)
1904 dst->rects = winpr_aligned_calloc(src->numRects,
sizeof(
RECTANGLE_16), 32);
1907 dst->numRects = src->numRects;
1908 for (
size_t x = 0; x < src->numRects; x++)
1910 dst->rects[x] = src->rects[x];
1915static INLINE BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1916 const RFX_MESSAGE* WINPR_RESTRICT src)
1921 WINPR_ASSERT(dst->quantVals == NULL);
1922 WINPR_ASSERT(dst->numQuant == 0);
1924 if (src->numQuant == 0)
1928 dst->quantVals = src->quantVals;
1929 dst->numQuant = src->numQuant;
1934static INLINE RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
1935 RFX_MESSAGE* WINPR_RESTRICT message,
1936 size_t* WINPR_RESTRICT numMessages,
size_t maxDataSize)
1938 WINPR_ASSERT(context);
1939 WINPR_ASSERT(message);
1940 WINPR_ASSERT(numMessages);
1942 maxDataSize -= 1024;
1943 *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
1945 RFX_MESSAGE* messages =
1946 (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages),
sizeof(RFX_MESSAGE), 32);
1951 for (UINT16 i = 0; i < message->numTiles; i++)
1953 RFX_TILE* tile = message->tiles[i];
1954 RFX_MESSAGE* msg = &messages[j];
1959 const size_t tileDataSize = rfx_tile_length(tile);
1961 if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
1964 if (msg->numTiles == 0)
1966 msg->frameIdx = message->frameIdx + j;
1967 if (!rfx_clone_quants(msg, message))
1969 if (!rfx_clone_rects(msg, message))
1971 msg->freeArray = TRUE;
1972 if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
1976 msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize);
1978 WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
1979 msg->tiles[msg->numTiles++] = message->tiles[i];
1980 message->tiles[i] = NULL;
1983 *numMessages = j + 1ULL;
1984 context->frameIdx += j;
1985 message->numTiles = 0;
1989 for (
size_t i = 0; i < j; i++)
1990 rfx_allocate_tiles(&messages[i], 0, FALSE);
1992 winpr_aligned_free(messages);
1996const RFX_MESSAGE* rfx_message_list_get(
const RFX_MESSAGE_LIST* WINPR_RESTRICT messages,
size_t idx)
1998 WINPR_ASSERT(messages);
1999 if (idx >= messages->count)
2001 WINPR_ASSERT(messages->list);
2002 return &messages->list[idx];
2005void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
2009 for (
size_t x = 0; x < messages->count; x++)
2010 rfx_message_free(messages->context, &messages->list[x]);
2014static INLINE RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
2015 RFX_MESSAGE* WINPR_RESTRICT messages,
2018 WINPR_ASSERT(context);
2019 RFX_MESSAGE_LIST* msg = calloc(1,
sizeof(RFX_MESSAGE_LIST));
2022 msg->context = context;
2024 msg->list = messages;
2028RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
2029 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2030 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2031 UINT32 scanline,
size_t* WINPR_RESTRICT numMessages,
2034 WINPR_ASSERT(context);
2035 WINPR_ASSERT(numMessages);
2037 RFX_MESSAGE* message =
2038 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2042 RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2043 rfx_message_free(context, message);
2047 return rfx_message_list_new(context, list, *numMessages);
2050static INLINE BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2052 const RFX_MESSAGE* WINPR_RESTRICT message)
2054 WINPR_ASSERT(context);
2055 WINPR_ASSERT(message);
2057 const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2059 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2062 Stream_Write_UINT16(s, WBT_EXTENSION);
2063 Stream_Write_UINT32(s, blockLen);
2064 Stream_Write_UINT8(s, 1);
2065 Stream_Write_UINT8(s, 0);
2066 Stream_Write_UINT16(s, CBT_TILESET);
2067 Stream_Write_UINT16(s, 0);
2068 Stream_Write_UINT16(s, context->properties);
2070 s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant));
2071 Stream_Write_UINT8(s, 0x40);
2072 Stream_Write_UINT16(s, message->numTiles);
2073 Stream_Write_UINT32(s, message->tilesDataSize);
2075 UINT32* quantVals = message->quantVals;
2076 for (
size_t i = 0; i < message->numQuant * 5ul; i++)
2078 WINPR_ASSERT(quantVals);
2079 Stream_Write_UINT8(s,
2080 WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2084 for (
size_t i = 0; i < message->numTiles; i++)
2086 RFX_TILE* tile = message->tiles[i];
2090 if (!rfx_write_tile(s, tile))
2094#ifdef WITH_DEBUG_RFX
2095 WLog_Print(context->priv->log, WLOG_DEBUG,
2096 "numQuant: %" PRIu16
" numTiles: %" PRIu16
" tilesDataSize: %" PRIu32
"",
2097 message->numQuant, message->numTiles, message->tilesDataSize);
2103rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2104 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT message)
2106 WINPR_ASSERT(context);
2107 WINPR_ASSERT(message);
2109 if (!Stream_EnsureRemainingCapacity(s, 14))
2112 Stream_Write_UINT16(s, WBT_FRAME_BEGIN);
2113 Stream_Write_UINT32(s, 14);
2114 Stream_Write_UINT8(s, 1);
2115 Stream_Write_UINT8(s, 0);
2116 Stream_Write_UINT32(s, message->frameIdx);
2117 Stream_Write_UINT16(s, 1);
2121static INLINE BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2123 const RFX_MESSAGE* WINPR_RESTRICT message)
2125 WINPR_ASSERT(context);
2126 WINPR_ASSERT(message);
2128 const size_t blockLen = 15 + (message->numRects * 8);
2129 if (blockLen > UINT32_MAX)
2132 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2135 Stream_Write_UINT16(s, WBT_REGION);
2136 Stream_Write_UINT32(s, (UINT32)blockLen);
2137 Stream_Write_UINT8(s, 1);
2138 Stream_Write_UINT8(s, 0);
2139 Stream_Write_UINT8(s, 1);
2140 Stream_Write_UINT16(s, message->numRects);
2142 for (UINT16 i = 0; i < message->numRects; i++)
2144 const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2148 Stream_Write_UINT16(s, rect->x);
2149 Stream_Write_UINT16(s, rect->y);
2150 Stream_Write_UINT16(s, rect->width);
2151 Stream_Write_UINT16(s, rect->height);
2154 Stream_Write_UINT16(s, CBT_REGION);
2155 Stream_Write_UINT16(s, 1);
2159static INLINE BOOL rfx_write_message_frame_end(
2160 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2161 WINPR_ATTR_UNUSED
const RFX_MESSAGE* WINPR_RESTRICT message)
2163 WINPR_ASSERT(context);
2164 WINPR_ASSERT(message);
2166 if (!Stream_EnsureRemainingCapacity(s, 8))
2169 Stream_Write_UINT16(s, WBT_FRAME_END);
2170 Stream_Write_UINT32(s, 8);
2171 Stream_Write_UINT8(s, 1);
2172 Stream_Write_UINT8(s, 0);
2176BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2177 const RFX_MESSAGE* WINPR_RESTRICT message)
2179 WINPR_ASSERT(context);
2180 WINPR_ASSERT(message);
2182 if (context->state == RFX_STATE_SEND_HEADERS)
2184 if (!rfx_compose_message_header(context, s))
2187 context->state = RFX_STATE_SEND_FRAME_DATA;
2190 if (!rfx_write_message_frame_begin(context, s, message) ||
2191 !rfx_write_message_region(context, s, message) ||
2192 !rfx_write_message_tileset(context, s, message) ||
2193 !rfx_write_message_frame_end(context, s, message))
2201BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2202 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2203 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2206 WINPR_ASSERT(context);
2207 RFX_MESSAGE* message =
2208 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2212 const BOOL ret = rfx_write_message(context, s, message);
2213 rfx_message_free(context, message);
2217BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2219 WINPR_ASSERT(context);
2220 context->mode = mode;
2224RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2226 WINPR_ASSERT(context);
2227 return context->mode;
2230UINT32 rfx_context_get_frame_idx(
const RFX_CONTEXT* WINPR_RESTRICT context)
2232 WINPR_ASSERT(context);
2233 return context->frameIdx;
2236UINT32 rfx_message_get_frame_idx(
const RFX_MESSAGE* WINPR_RESTRICT message)
2238 WINPR_ASSERT(message);
2239 return message->frameIdx;
2242static INLINE BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2245 const UINT32 blockLen = 12;
2249 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2252 Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC);
2253 Stream_Write_UINT32(s, blockLen);
2254 Stream_Write_UINT32(s, 0xCACCACCA);
2255 Stream_Write_UINT16(s, 0x0100);
2259static INLINE BOOL rfx_write_progressive_wb_context(
2260 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
wStream* WINPR_RESTRICT s)
2262 const UINT32 blockLen = 10;
2266 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2269 Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT);
2270 Stream_Write_UINT32(s, blockLen);
2271 Stream_Write_UINT8(s, 0);
2272 Stream_Write_UINT16(s, 64);
2273 Stream_Write_UINT8(s, 0);
2277static INLINE BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2279 const RFX_MESSAGE* WINPR_RESTRICT msg)
2282 UINT32 blockLen = 18;
2283 UINT32 tilesDataSize = 0;
2284 const size_t start = Stream_GetPosition(s);
2290 blockLen += msg->numRects * 8;
2291 blockLen += msg->numQuant * 5;
2292 tilesDataSize = msg->numTiles * 22UL;
2293 for (UINT16 i = 0; i < msg->numTiles; i++)
2295 const RFX_TILE* tile = msg->tiles[i];
2297 tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2299 blockLen += tilesDataSize;
2301 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2304 Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION);
2305 Stream_Write_UINT32(s, blockLen);
2306 Stream_Write_UINT8(s, 64);
2307 Stream_Write_UINT16(s, msg->numRects);
2308 WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2309 Stream_Write_UINT8(s, (UINT8)msg->numQuant);
2310 Stream_Write_UINT8(s, 0);
2311 Stream_Write_UINT8(s, 0);
2312 Stream_Write_UINT16(s, msg->numTiles);
2313 Stream_Write_UINT32(s, tilesDataSize);
2315 for (UINT16 i = 0; i < msg->numRects; i++)
2318 const RFX_RECT* r = &msg->rects[i];
2319 Stream_Write_UINT16(s, r->x);
2320 Stream_Write_UINT16(s, r->y);
2321 Stream_Write_UINT16(s, r->width);
2322 Stream_Write_UINT16(s, r->height);
2333 for (UINT16 i = 0; i < msg->numQuant; i++)
2335 const UINT32* qv = &msg->quantVals[10ULL * i];
2337 Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4)));
2338 Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4)));
2339 Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4)));
2340 Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4)));
2341 Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4)));
2344 for (UINT16 i = 0; i < msg->numTiles; i++)
2346 const RFX_TILE* tile = msg->tiles[i];
2347 if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2351 const size_t end = Stream_GetPosition(s);
2352 const size_t used = end - start;
2353 return (used == blockLen);
2357rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2358 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT msg)
2360 const UINT32 blockLen = 12;
2365 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2368 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN);
2369 Stream_Write_UINT32(s, blockLen);
2370 Stream_Write_UINT32(s, msg->frameIdx);
2371 Stream_Write_UINT16(s, 1);
2376static INLINE BOOL rfx_write_progressive_frame_end(
2377 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
wStream* WINPR_RESTRICT s)
2379 const UINT32 blockLen = 6;
2383 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2386 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END);
2387 Stream_Write_UINT32(s, blockLen);
2393rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2396 UINT32 blockLen = 0;
2401 blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2402 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2405 Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE);
2406 Stream_Write_UINT32(s, blockLen);
2407 Stream_Write_UINT8(s, tile->quantIdxY);
2408 Stream_Write_UINT8(s, tile->quantIdxCb);
2409 Stream_Write_UINT8(s, tile->quantIdxCr);
2410 Stream_Write_UINT16(s, tile->xIdx);
2411 Stream_Write_UINT16(s, tile->yIdx);
2412 Stream_Write_UINT8(s, 0);
2413 Stream_Write_UINT16(s, tile->YLen);
2414 Stream_Write_UINT16(s, tile->CbLen);
2415 Stream_Write_UINT16(s, tile->CrLen);
2416 Stream_Write_UINT16(s, 0);
2417 Stream_Write(s, tile->YData, tile->YLen);
2418 Stream_Write(s, tile->CbData, tile->CbLen);
2419 Stream_Write(s, tile->CrData, tile->CrLen);
2424const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2428 case PROGRESSIVE_WBT_SYNC:
2429 return "PROGRESSIVE_WBT_SYNC";
2431 case PROGRESSIVE_WBT_FRAME_BEGIN:
2432 return "PROGRESSIVE_WBT_FRAME_BEGIN";
2434 case PROGRESSIVE_WBT_FRAME_END:
2435 return "PROGRESSIVE_WBT_FRAME_END";
2437 case PROGRESSIVE_WBT_CONTEXT:
2438 return "PROGRESSIVE_WBT_CONTEXT";
2440 case PROGRESSIVE_WBT_REGION:
2441 return "PROGRESSIVE_WBT_REGION";
2443 case PROGRESSIVE_WBT_TILE_SIMPLE:
2444 return "PROGRESSIVE_WBT_TILE_SIMPLE";
2446 case PROGRESSIVE_WBT_TILE_FIRST:
2447 return "PROGRESSIVE_WBT_TILE_FIRST";
2449 case PROGRESSIVE_WBT_TILE_UPGRADE:
2450 return "PROGRESSIVE_WBT_TILE_UPGRADE";
2453 return "PROGRESSIVE_WBT_UNKNOWN";
2457BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT context,
2459 const RFX_MESSAGE* WINPR_RESTRICT msg)
2463 WINPR_ASSERT(context);
2465 if (context->mode != RLGR1)
2467 WLog_ERR(TAG,
"error, RLGR1 mode is required!");
2471 if (!rfx_write_progressive_wb_sync(context, s))
2474 if (!rfx_write_progressive_wb_context(context, s))
2477 if (!rfx_write_progressive_frame_begin(context, s, msg))
2480 if (!rfx_write_progressive_region(context, s, msg))
2483 if (!rfx_write_progressive_frame_end(context, s))
This struct contains function pointer to initialize/free objects.