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"
50#include "../core/utils.h"
52#include "sse/rfx_sse2.h"
53#include "neon/rfx_neon.h"
55#define TAG FREERDP_TAG("codec")
57#define RFX_KEY "Software\\%s\\RemoteFX"
71static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
73static inline BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
75 const RFX_TILE* WINPR_RESTRICT tile);
76static BOOL rfx_message_clear(RFX_CONTEXT* WINPR_RESTRICT context,
77 RFX_MESSAGE* WINPR_RESTRICT message);
79static inline void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context)
81 if (!context || !context->priv)
83 PROFILER_CREATE(context->priv->prof_rfx_decode_rgb,
"rfx_decode_rgb")
84 PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component")
85 PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode")
86 PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode")
87 PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode")
88 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode")
89 PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB")
90 PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb")
91 PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component")
92 PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode")
93 PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode")
94 PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode")
95 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode")
96 PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr")
97 PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb")
100static inline
void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context)
102 if (!context || !context->priv)
104 PROFILER_FREE(context->priv->prof_rfx_decode_rgb)
105 PROFILER_FREE(context->priv->prof_rfx_decode_component)
106 PROFILER_FREE(context->priv->prof_rfx_rlgr_decode)
107 PROFILER_FREE(context->priv->prof_rfx_differential_decode)
108 PROFILER_FREE(context->priv->prof_rfx_quantization_decode)
109 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode)
110 PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb)
111 PROFILER_FREE(context->priv->prof_rfx_encode_rgb)
112 PROFILER_FREE(context->priv->prof_rfx_encode_component)
113 PROFILER_FREE(context->priv->prof_rfx_rlgr_encode)
114 PROFILER_FREE(context->priv->prof_rfx_differential_encode)
115 PROFILER_FREE(context->priv->prof_rfx_quantization_encode)
116 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode)
117 PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr)
118 PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb)
121static inline
void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context)
123 if (!context || !context->priv)
126 PROFILER_PRINT_HEADER
127 PROFILER_PRINT(context->priv->prof_rfx_decode_rgb)
128 PROFILER_PRINT(context->priv->prof_rfx_decode_component)
129 PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode)
130 PROFILER_PRINT(context->priv->prof_rfx_differential_decode)
131 PROFILER_PRINT(context->priv->prof_rfx_quantization_decode)
132 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode)
133 PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb)
134 PROFILER_PRINT(context->priv->prof_rfx_encode_rgb)
135 PROFILER_PRINT(context->priv->prof_rfx_encode_component)
136 PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode)
137 PROFILER_PRINT(context->priv->prof_rfx_differential_encode)
138 PROFILER_PRINT(context->priv->prof_rfx_quantization_encode)
139 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode)
140 PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr)
141 PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb)
142 PROFILER_PRINT_FOOTER
145static inline
void rfx_tile_init(
void* obj)
153 tile->YData =
nullptr;
155 tile->CbData =
nullptr;
157 tile->CrData =
nullptr;
161static inline void* rfx_decoder_tile_new(
const void* val)
163 const size_t size = 4ULL * 64ULL * 64ULL;
170 if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16)))
172 winpr_aligned_free(tile);
175 memset(tile->data, 0xff, size);
176 tile->allocated = TRUE;
180static inline void rfx_decoder_tile_free(
void* obj)
187 winpr_aligned_free(tile->data);
189 winpr_aligned_free(tile);
193static inline void* rfx_encoder_tile_new(
const void* val)
196 return winpr_aligned_calloc(1,
sizeof(
RFX_TILE), 32);
199static inline void rfx_encoder_tile_free(
void* obj)
201 winpr_aligned_free(obj);
204RFX_CONTEXT* rfx_context_new(BOOL encoder)
206 return rfx_context_new_ex(encoder, 0);
209RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags)
211 RFX_CONTEXT_PRIV* priv =
nullptr;
212 RFX_CONTEXT* context = (RFX_CONTEXT*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT), 32);
217 context->encoder = encoder;
218 context->currentMessage.freeArray = TRUE;
219 context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT_PRIV), 32);
224 priv->log = WLog_Get(
"com.freerdp.codec.rfx");
225 priv->TilePool = ObjectPool_New(TRUE);
231 wObject* pool = ObjectPool_Object(priv->TilePool);
234 if (context->encoder)
259 priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16);
261 if (!priv->BufferPool)
264 priv->UseThreads = FALSE;
265 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
266 priv->UseThreads = TRUE;
269 char* key = freerdp_getApplicatonDetailsRegKey(RFX_KEY);
274 RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
277 if (status == ERROR_SUCCESS)
281 DWORD dwSize =
sizeof(dwValue);
283 if (RegQueryValueEx(hKey, _T(
"UseThreads"),
nullptr, &dwType, (BYTE*)&dwValue,
284 &dwSize) == ERROR_SUCCESS)
285 priv->UseThreads = dwValue ? 1 : 0;
292 if (priv->UseThreads)
297 if (!primitives_get())
302 rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32);
304 rfx_profiler_create(context);
306 context->quantization_decode = rfx_quantization_decode;
307 context->quantization_encode = rfx_quantization_encode;
308 context->dwt_2d_decode = rfx_dwt_2d_decode;
309 context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode;
310 context->dwt_2d_encode = rfx_dwt_2d_encode;
311 context->rlgr_decode = rfx_rlgr_decode;
312 context->rlgr_encode = rfx_rlgr_encode;
313 rfx_init_sse2(context);
314 rfx_init_neon(context);
315 context->state = RFX_STATE_SEND_HEADERS;
316 context->expectedDataBlockType = WBT_FRAME_BEGIN;
319 WINPR_PRAGMA_DIAG_PUSH
320 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
321 rfx_context_free(context);
322 WINPR_PRAGMA_DIAG_POP
326void rfx_context_free(RFX_CONTEXT* context)
328 RFX_CONTEXT_PRIV* priv =
nullptr;
333 WINPR_ASSERT(
nullptr != context);
335 priv = context->priv;
336 WINPR_ASSERT(
nullptr != priv);
337 WINPR_ASSERT(
nullptr != priv->TilePool);
338 WINPR_ASSERT(
nullptr != priv->BufferPool);
341 rfx_message_clear(context, &context->currentMessage);
342 winpr_aligned_free(context->quants);
343 rfx_profiler_print(context);
344 rfx_profiler_free(context);
348 ObjectPool_Free(priv->TilePool);
349 if (priv->UseThreads)
351 winpr_aligned_free((
void*)priv->workObjects);
352 winpr_aligned_free(priv->tileWorkParams);
356 "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!");
360 BufferPool_Free(priv->BufferPool);
361 winpr_aligned_free(priv);
363 winpr_aligned_free(context);
366static inline RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
368 WINPR_ASSERT(message);
369 WINPR_ASSERT(message->tiles);
370 WINPR_ASSERT(index < message->numTiles);
371 return message->tiles[index];
374static inline const RFX_RECT* rfx_message_get_rect_const(
const RFX_MESSAGE* WINPR_RESTRICT message,
377 WINPR_ASSERT(message);
378 WINPR_ASSERT(message->rects);
379 WINPR_ASSERT(index < message->numRects);
380 return &message->rects[index];
383static inline RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
385 WINPR_ASSERT(message);
386 WINPR_ASSERT(message->rects);
387 WINPR_ASSERT(index < message->numRects);
388 return &message->rects[index];
391void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format)
393 WINPR_ASSERT(context);
394 context->pixel_format = pixel_format;
395 const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format);
396 context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp);
399UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context)
401 WINPR_ASSERT(context);
402 return context->pixel_format;
405void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context,
406 const BYTE* WINPR_RESTRICT palette)
408 WINPR_ASSERT(context);
409 context->palette = palette;
412const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context)
414 WINPR_ASSERT(context);
415 return context->palette;
418BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
423 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
424 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
425 context->state = RFX_STATE_SEND_HEADERS;
426 context->expectedDataBlockType = WBT_FRAME_BEGIN;
427 context->frameIdx = 0;
431static inline BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context,
436 WINPR_ASSERT(context);
437 WINPR_ASSERT(context->priv);
438 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC;
441 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
444 Stream_Read_UINT32(s, magic);
445 if (magic != WF_MAGIC)
447 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid magic number 0x%08" PRIX32
"", magic);
451 Stream_Read_UINT16(s, context->version);
452 if (context->version != WF_VERSION_1_0)
454 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid version number 0x%08" PRIX32
"",
459 WLog_Print(context->priv->log, WLOG_DEBUG,
"version 0x%08" PRIX32
"", context->version);
460 context->decodedHeaderBlocks |= RFX_DECODED_SYNC;
464static inline BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context,
469 WINPR_ASSERT(context);
470 WINPR_ASSERT(context->priv);
471 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS;
473 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
476 Stream_Read_UINT8(s, numCodecs);
477 Stream_Read_UINT8(s, context->codec_id);
479 s, context->codec_version);
483 WLog_Print(context->priv->log, WLOG_ERROR,
"numCodes is 0x%02" PRIX8
" (must be 0x01)",
488 if (context->codec_id != 0x01)
490 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec id (0x%02" PRIX32
")",
495 if (context->codec_version != WF_VERSION_1_0)
497 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec version (0x%08" PRIX32
")",
498 context->codec_version);
502 WLog_Print(context->priv->log, WLOG_DEBUG,
"id %" PRIu32
" version 0x%" PRIX32
".",
503 context->codec_id, context->codec_version);
504 context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS;
508static inline BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context,
512 BYTE numChannels = 0;
514 WINPR_ASSERT(context);
515 WINPR_ASSERT(context->priv);
516 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS;
518 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1))
521 Stream_Read_UINT8(s, numChannels);
528 WLog_Print(context->priv->log, WLOG_ERROR,
"no channels announced");
532 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull))
536 Stream_Read_UINT8(s, channelId);
538 if (channelId != 0x00)
540 WLog_Print(context->priv->log, WLOG_ERROR,
"channelId:0x%02" PRIX8
", expected:0x00",
545 Stream_Read_UINT16(s, context->width);
546 Stream_Read_UINT16(s, context->height);
548 if (!context->width || !context->height)
550 WLog_Print(context->priv->log, WLOG_ERROR,
551 "invalid channel with/height: %" PRIu16
"x%" PRIu16
"", context->width,
557 Stream_Seek(s, 5ULL * (numChannels - 1));
558 WLog_Print(context->priv->log, WLOG_DEBUG,
559 "numChannels %" PRIu8
" id %" PRIu8
", %" PRIu16
"x%" PRIu16
".", numChannels,
560 channelId, context->width, context->height);
561 context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS;
565static inline BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
570 UINT16 properties = 0;
572 WINPR_ASSERT(context);
573 WINPR_ASSERT(context->priv);
574 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT;
576 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5))
579 Stream_Read_UINT8(s, ctxId);
580 Stream_Read_UINT16(s, tileSize);
581 Stream_Read_UINT16(s, properties);
582 WLog_Print(context->priv->log, WLOG_DEBUG,
583 "ctxId %" PRIu8
" tileSize %" PRIu16
" properties 0x%04" PRIX16
".", ctxId, tileSize,
585 context->properties = properties;
586 context->flags = (properties & 0x0007);
588 if (context->flags == CODEC_MODE)
590 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in image mode.");
594 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in video mode.");
597 switch ((properties & 0x1E00) >> 9)
599 case CLW_ENTROPY_RLGR1:
600 context->mode = RLGR1;
601 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR1.");
604 case CLW_ENTROPY_RLGR3:
605 context->mode = RLGR3;
606 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR3.");
610 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown RLGR algorithm.");
614 context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT;
618static inline BOOL rfx_process_message_frame_begin(
619 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
620 wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
623 UINT16 numRegions = 0;
625 WINPR_ASSERT(context);
626 WINPR_ASSERT(context->priv);
627 WINPR_ASSERT(message);
628 WINPR_ASSERT(pExpectedBlockType);
630 if (*pExpectedBlockType != WBT_FRAME_BEGIN)
632 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_FRAME_BEGIN");
636 *pExpectedBlockType = WBT_REGION;
638 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
643 Stream_Read_UINT16(s, numRegions);
644 WLog_Print(context->priv->log, WLOG_DEBUG,
645 "RFX_FRAME_BEGIN: frameIdx: %" PRIu32
" numRegions: %" PRIu16
"", frameIdx,
650static inline BOOL rfx_process_message_frame_end(
651 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
652 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
654 WINPR_ASSERT(context);
655 WINPR_ASSERT(context->priv);
656 WINPR_ASSERT(message);
658 WINPR_ASSERT(pExpectedBlockType);
660 if (*pExpectedBlockType != WBT_FRAME_END)
662 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected, wants WBT_FRAME_END");
666 *pExpectedBlockType = WBT_FRAME_BEGIN;
667 WLog_Print(context->priv->log, WLOG_DEBUG,
"RFX_FRAME_END");
671static inline BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message)
673 WINPR_ASSERT(message);
676 winpr_aligned_recalloc(message->rects, message->numRects,
sizeof(
RFX_RECT), 32);
679 message->rects = tmpRects;
683static inline BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
684 RFX_MESSAGE* WINPR_RESTRICT message,
686 UINT16* WINPR_RESTRICT pExpectedBlockType)
688 UINT16 regionType = 0;
689 UINT16 numTileSets = 0;
691 WINPR_ASSERT(context);
692 WINPR_ASSERT(context->priv);
693 WINPR_ASSERT(message);
694 WINPR_ASSERT(pExpectedBlockType);
696 if (*pExpectedBlockType != WBT_REGION)
698 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_REGION");
702 *pExpectedBlockType = WBT_EXTENSION;
704 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3))
707 Stream_Seek_UINT8(s);
708 Stream_Read_UINT16(s, message->numRects);
710 if (message->numRects < 1)
718 message->numRects = 1;
719 if (!rfx_resize_rects(message))
722 message->rects->x = 0;
723 message->rects->y = 0;
724 message->rects->width = context->width;
725 message->rects->height = context->height;
729 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull))
732 if (!rfx_resize_rects(message))
736 for (UINT16 i = 0; i < message->numRects; i++)
738 RFX_RECT* rect = rfx_message_get_rect(message, i);
740 Stream_Read_UINT16(s, rect->x);
741 Stream_Read_UINT16(s, rect->y);
742 Stream_Read_UINT16(s, rect->width);
743 Stream_Read_UINT16(s, rect->height);
744 WLog_Print(context->priv->log, WLOG_DEBUG,
745 "rect %" PRIu16
" (x,y=%" PRIu16
",%" PRIu16
" w,h=%" PRIu16
" %" PRIu16
").", i,
746 rect->x, rect->y, rect->width, rect->height);
749 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
752 Stream_Read_UINT16(s, regionType);
753 Stream_Read_UINT16(s, numTileSets);
755 if (regionType != CBT_REGION)
757 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid region type 0x%04" PRIX16
"",
762 if (numTileSets != 0x0001)
764 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid number of tilesets (%" PRIu16
")",
775 RFX_CONTEXT* context;
776} RFX_TILE_PROCESS_WORK_PARAM;
778static inline void CALLBACK
779rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
780 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
782 RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
784 WINPR_ASSERT(param->context);
785 WINPR_ASSERT(param->context->priv);
787 if (!rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4))
788 WLog_Print(param->context->priv->log, WLOG_ERROR,
"rfx_decode_rgb failed");
791static inline BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count,
794 WINPR_ASSERT(message);
797 (
RFX_TILE**)winpr_aligned_recalloc((
void*)message->tiles, count,
sizeof(
RFX_TILE*), 32);
798 if (!tmpTiles && (count != 0))
801 message->tiles = tmpTiles;
803 message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count);
806 WINPR_ASSERT(message->numTiles <= count);
808 message->allocatedTiles = count;
813static inline BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
814 RFX_MESSAGE* WINPR_RESTRICT message,
816 UINT16* WINPR_RESTRICT pExpectedBlockType)
819 size_t close_cnt = 0;
822 UINT32* quants =
nullptr;
826 UINT32 blockType = 0;
827 UINT32 tilesDataSize = 0;
828 PTP_WORK* work_objects =
nullptr;
829 RFX_TILE_PROCESS_WORK_PARAM* params =
nullptr;
830 void* pmem =
nullptr;
832 WINPR_ASSERT(context);
833 WINPR_ASSERT(context->priv);
834 WINPR_ASSERT(message);
835 WINPR_ASSERT(pExpectedBlockType);
837 if (*pExpectedBlockType != WBT_EXTENSION)
839 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants a tileset");
843 *pExpectedBlockType = WBT_FRAME_END;
845 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14))
848 Stream_Read_UINT16(s, subtype);
849 if (subtype != CBT_TILESET)
851 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid subtype, expected CBT_TILESET.");
855 Stream_Seek_UINT16(s);
856 Stream_Seek_UINT16(s);
857 Stream_Read_UINT8(s, context->numQuant);
858 Stream_Seek_UINT8(s);
860 if (context->numQuant < 1)
862 WLog_Print(context->priv->log, WLOG_ERROR,
"no quantization value.");
866 Stream_Read_UINT16(s, numTiles);
873 Stream_Read_UINT32(s, tilesDataSize);
875 if (!(pmem = winpr_aligned_recalloc(context->quants, context->numQuant,
876 NR_QUANT_VALUES *
sizeof(UINT32), 32)))
879 quants = context->quants = (UINT32*)pmem;
882 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull))
885 for (
size_t i = 0; i < context->numQuant; i++)
888 Stream_Read_UINT8(s, quant);
889 *quants++ = (quant & 0x0F);
890 *quants++ = (quant >> 4);
891 Stream_Read_UINT8(s, quant);
892 *quants++ = (quant & 0x0F);
893 *quants++ = (quant >> 4);
894 Stream_Read_UINT8(s, quant);
895 *quants++ = (quant & 0x0F);
896 *quants++ = (quant >> 4);
897 Stream_Read_UINT8(s, quant);
898 *quants++ = (quant & 0x0F);
899 *quants++ = (quant >> 4);
900 Stream_Read_UINT8(s, quant);
901 *quants++ = (quant & 0x0F);
902 *quants++ = (quant >> 4);
904 context->priv->log, WLOG_DEBUG,
905 "quant %" PRIuz
" (%" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
906 " %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
").",
907 i, context->quants[i * NR_QUANT_VALUES], context->quants[i * NR_QUANT_VALUES + 1],
908 context->quants[i * NR_QUANT_VALUES + 2], context->quants[i * NR_QUANT_VALUES + 3],
909 context->quants[i * NR_QUANT_VALUES + 4], context->quants[i * NR_QUANT_VALUES + 5],
910 context->quants[i * NR_QUANT_VALUES + 6], context->quants[i * NR_QUANT_VALUES + 7],
911 context->quants[i * NR_QUANT_VALUES + 8], context->quants[i * NR_QUANT_VALUES + 9]);
914 for (
size_t i = 0; i < message->numTiles; i++)
916 ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
917 message->tiles[i] =
nullptr;
920 if (!rfx_allocate_tiles(message, numTiles, FALSE))
923 if (context->priv->UseThreads)
925 work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles,
sizeof(PTP_WORK), 32);
926 params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc(
927 nullptr, message->numTiles,
sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32);
931 winpr_aligned_free(params);
937 winpr_aligned_free((
void*)work_objects);
946 if (Stream_GetRemainingLength(s) >= tilesDataSize)
949 for (
size_t i = 0; i < message->numTiles; i++)
954 if (!(tile = (
RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
956 WLog_Print(context->priv->log, WLOG_ERROR,
957 "RfxMessageTileSet failed to get tile from object pool");
962 message->tiles[i] = tile;
965 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
967 WLog_Print(context->priv->log, WLOG_ERROR,
968 "RfxMessageTileSet packet too small to read tile %" PRIuz
"/%" PRIu16
"",
969 i, message->numTiles);
974 sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
977 Stream_Read_UINT32(sub, blockLen);
979 if (!Stream_SafeSeek(s, blockLen))
984 if ((blockLen < 6 + 13) ||
985 (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6)))
987 WLog_Print(context->priv->log, WLOG_ERROR,
988 "RfxMessageTileSet not enough bytes to read tile %" PRIuz
"/%" PRIu16
989 " with blocklen=%" PRIu32
"",
990 i, message->numTiles, blockLen);
995 if (blockType != CBT_TILE)
997 WLog_Print(context->priv->log, WLOG_ERROR,
998 "unknown block type 0x%" PRIX32
", expected CBT_TILE (0xCAC3).",
1004 Stream_Read_UINT8(sub, tile->quantIdxY);
1005 Stream_Read_UINT8(sub, tile->quantIdxCb);
1006 Stream_Read_UINT8(sub, tile->quantIdxCr);
1007 if (tile->quantIdxY >= context->numQuant)
1009 WLog_Print(context->priv->log, WLOG_ERROR,
1010 "quantIdxY %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxY,
1015 if (tile->quantIdxCb >= context->numQuant)
1017 WLog_Print(context->priv->log, WLOG_ERROR,
1018 "quantIdxCb %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCb,
1023 if (tile->quantIdxCr >= context->numQuant)
1025 WLog_Print(context->priv->log, WLOG_ERROR,
1026 "quantIdxCr %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCr,
1032 Stream_Read_UINT16(sub, tile->xIdx);
1033 Stream_Read_UINT16(sub, tile->yIdx);
1034 Stream_Read_UINT16(sub, tile->YLen);
1035 Stream_Read_UINT16(sub, tile->CbLen);
1036 Stream_Read_UINT16(sub, tile->CrLen);
1037 tile->YData = Stream_PointerAs(sub, BYTE);
1038 if (!Stream_SafeSeek(sub, tile->YLen))
1043 tile->CbData = Stream_PointerAs(sub, BYTE);
1044 if (!Stream_SafeSeek(sub, tile->CbLen))
1049 tile->CrData = Stream_PointerAs(sub, BYTE);
1050 if (!Stream_SafeSeek(sub, tile->CrLen))
1055 tile->x = tile->xIdx * 64;
1056 tile->y = tile->yIdx * 64;
1058 if (context->priv->UseThreads)
1066 params[i].context = context;
1067 params[i].tile = message->tiles[i];
1069 if (!(work_objects[i] = CreateThreadpoolWork(rfx_process_message_tile_work_callback,
1070 (
void*)¶ms[i],
nullptr)))
1072 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThreadpoolWork failed.");
1077 SubmitThreadpoolWork(work_objects[i]);
1082 if (!rfx_decode_rgb(context, tile, tile->data, 64 * 4))
1091 if (context->priv->UseThreads)
1093 for (
size_t i = 0; i < close_cnt; i++)
1095 WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
1096 CloseThreadpoolWork(work_objects[i]);
1100 winpr_aligned_free((
void*)work_objects);
1101 winpr_aligned_free(params);
1103 for (
size_t i = 0; i < message->numTiles; i++)
1105 if (!(tile = message->tiles[i]))
1108 tile->YLen = tile->CbLen = tile->CrLen = 0;
1109 tile->YData = tile->CbData = tile->CrData =
nullptr;
1115BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT data,
1116 UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst,
1117 UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight,
1118 REGION16* WINPR_RESTRICT invalidRegion)
1120 REGION16 updateRegion = WINPR_C_ARRAY_INIT;
1121 wStream inStream = WINPR_C_ARRAY_INIT;
1124 if (!context || !data || !length)
1127 WINPR_ASSERT(context->priv);
1128 RFX_MESSAGE* message = &context->currentMessage;
1130 wStream* s = Stream_StaticConstInit(&inStream, data, length);
1132 while (ok && Stream_GetRemainingLength(s) > 6)
1134 wStream subStreamBuffer = WINPR_C_ARRAY_INIT;
1135 size_t extraBlockLen = 0;
1136 UINT32 blockLen = 0;
1137 UINT32 blockType = 0;
1140 Stream_Read_UINT16(s, blockType);
1141 Stream_Read_UINT32(s, blockLen);
1142 WLog_Print(context->priv->log, WLOG_DEBUG,
"blockType 0x%" PRIX32
" blockLen %" PRIu32
"",
1143 blockType, blockLen);
1147 WLog_Print(context->priv->log, WLOG_ERROR,
"blockLen too small(%" PRIu32
")", blockLen);
1151 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6))
1154 if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS)
1156 WLog_Print(context->priv->log, WLOG_ERROR,
"incomplete header blocks processing");
1160 if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
1164 UINT8 channelId = 0;
1166 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1170 Stream_Read_UINT8(s, codecId);
1171 Stream_Read_UINT8(s, channelId);
1173 if (codecId != 0x01)
1175 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codecId 0x%02" PRIX8
"",
1180 if (blockType == WBT_CONTEXT)
1183 if (channelId != 0xFF)
1185 WLog_Print(context->priv->log, WLOG_ERROR,
1186 "invalid channelId 0x%02" PRIX8
" for blockType 0x%08" PRIX32
"",
1187 channelId, blockType);
1194 if (channelId != 0x00)
1196 WLog_Print(context->priv->log, WLOG_ERROR,
1197 "invalid channelId 0x%02" PRIX8
" for blockType WBT_CONTEXT",
1204 const size_t blockLenNoHeader = blockLen - 6;
1205 if (blockLenNoHeader < extraBlockLen)
1207 WLog_Print(context->priv->log, WLOG_ERROR,
1208 "blockLen too small(%" PRIu32
"), must be >= 6 + %" PRIuz, blockLen,
1213 const size_t subStreamLen = blockLenNoHeader - extraBlockLen;
1214 wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen);
1215 Stream_Seek(s, subStreamLen);
1224 ok = rfx_process_message_sync(context, subStream);
1228 ok = rfx_process_message_context(context, subStream);
1231 case WBT_CODEC_VERSIONS:
1232 ok = rfx_process_message_codec_versions(context, subStream);
1236 ok = rfx_process_message_channels(context, subStream);
1246 case WBT_FRAME_BEGIN:
1247 ok = rfx_process_message_frame_begin(context, message, subStream,
1248 &context->expectedDataBlockType);
1252 ok = rfx_process_message_region(context, message, subStream,
1253 &context->expectedDataBlockType);
1257 ok = rfx_process_message_tileset(context, message, subStream,
1258 &context->expectedDataBlockType);
1262 ok = rfx_process_message_frame_end(context, message, subStream,
1263 &context->expectedDataBlockType);
1267 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown blockType 0x%" PRIX32
"",
1275 UINT32 nbUpdateRects = 0;
1276 REGION16 clippingRects = WINPR_C_ARRAY_INIT;
1278 const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format);
1279 const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat);
1280 region16_init(&clippingRects);
1282 WINPR_ASSERT(dstWidth <= UINT16_MAX);
1283 WINPR_ASSERT(dstHeight <= UINT16_MAX);
1284 for (UINT32 i = 0; i < message->numRects; i++)
1287 const RFX_RECT* rect = &(message->rects[i]);
1289 WINPR_ASSERT(left + rect->x <= UINT16_MAX);
1290 WINPR_ASSERT(top + rect->y <= UINT16_MAX);
1291 WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX);
1292 WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX);
1294 clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth));
1295 clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight));
1297 const UINT32 rw = 1UL * clippingRect.left + rect->width;
1298 const UINT32 rh = 1UL * clippingRect.top + rect->height;
1299 const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth));
1300 const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight));
1301 clippingRect.right = right;
1302 clippingRect.bottom = bottom;
1303 if (!region16_union_rect(&clippingRects, &clippingRects, &clippingRect))
1305 region16_uninit(&updateRegion);
1306 region16_uninit(&clippingRects);
1311 for (UINT32 i = 0; i < message->numTiles; i++)
1314 const RFX_TILE* tile = rfx_message_get_tile(message, i);
1316 WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1317 WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1319 updateRect.left = (UINT16)left + tile->x;
1320 updateRect.top = (UINT16)top + tile->y;
1321 updateRect.right = updateRect.left + 64;
1322 updateRect.bottom = updateRect.top + 64;
1323 region16_init(&updateRegion);
1324 if (!region16_intersect_rect(&updateRegion, &clippingRects, &updateRect))
1326 region16_uninit(&updateRegion);
1327 region16_uninit(&clippingRects);
1330 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1332 for (UINT32 j = 0; j < nbUpdateRects; j++)
1335 const UINT32 stride = 64 * formatSize;
1336 const UINT32 nXDst = cur->left;
1337 const UINT32 nYDst = cur->top;
1338 const UINT32 nXSrc = nXDst - updateRect.left;
1339 const UINT32 nYSrc = nYDst - updateRect.top;
1340 const UINT32 nWidth = cur->right - cur->left;
1341 const UINT32 nHeight = cur->bottom - cur->top;
1343 if (!freerdp_image_copy_no_overlap(
1344 dst, dstFormat, dstStride, nXDst, nYDst, nWidth, nHeight, tile->data,
1345 context->pixel_format, stride, nXSrc, nYSrc,
nullptr, FREERDP_FLIP_NONE))
1347 region16_uninit(&updateRegion);
1348 region16_uninit(&clippingRects);
1349 WLog_Print(context->priv->log, WLOG_ERROR,
1350 "nbUpdateRectx[%" PRIu32
" (%" PRIu32
")] freerdp_image_copy failed",
1357 if (!region16_union_rect(invalidRegion, invalidRegion, cur))
1359 region16_uninit(&updateRegion);
1360 region16_uninit(&clippingRects);
1366 region16_uninit(&updateRegion);
1369 region16_uninit(&clippingRects);
1374 rfx_message_clear(context, message);
1375 context->currentMessage.freeArray = TRUE;
1378 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1382const UINT32* rfx_message_get_quants(
const RFX_MESSAGE* WINPR_RESTRICT message,
1383 UINT16* WINPR_RESTRICT numQuantVals)
1385 WINPR_ASSERT(message);
1387 *numQuantVals = message->numQuant;
1388 return message->quantVals;
1391const RFX_TILE** rfx_message_get_tiles(
const RFX_MESSAGE* WINPR_RESTRICT message,
1392 UINT16* WINPR_RESTRICT numTiles)
1394 WINPR_ASSERT(message);
1396 *numTiles = message->numTiles;
1403 cnv.pp = message->tiles;
1407UINT16 rfx_message_get_tile_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1409 WINPR_ASSERT(message);
1410 return message->numTiles;
1413const RFX_RECT* rfx_message_get_rects(
const RFX_MESSAGE* WINPR_RESTRICT message,
1414 UINT16* WINPR_RESTRICT numRects)
1416 WINPR_ASSERT(message);
1418 *numRects = message->numRects;
1419 return message->rects;
1422UINT16 rfx_message_get_rect_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1424 WINPR_ASSERT(message);
1425 return message->numRects;
1428static BOOL rfx_message_clear(RFX_CONTEXT* WINPR_RESTRICT context,
1429 RFX_MESSAGE* WINPR_RESTRICT message)
1434 winpr_aligned_free(message->rects);
1438 for (
size_t i = 0; i < message->numTiles; i++)
1440 RFX_TILE* tile = message->tiles[i];
1444 if (tile->YCbCrData)
1446 BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1447 tile->YCbCrData =
nullptr;
1450 ObjectPool_Return(context->priv->TilePool, (
void*)tile);
1453 rfx_allocate_tiles(message, 0, FALSE);
1456 const BOOL freeArray = message->freeArray;
1457 const RFX_MESSAGE empty = WINPR_C_ARRAY_INIT;
1462void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1464 if (!rfx_message_clear(context, message))
1465 winpr_aligned_free(message);
1468static inline void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1470 UINT16 properties = 0;
1472 WINPR_ASSERT(context);
1475 properties |= (context->flags << 1);
1476 properties |= (COL_CONV_ICT << 4);
1477 properties |= (CLW_XFORM_DWT_53_A << 6);
1478 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10);
1479 properties |= (SCALAR_QUANTIZATION << 14);
1480 context->properties = properties;
1484rfx_write_message_sync(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1485 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s)
1487 WINPR_ASSERT(context);
1489 Stream_Write_UINT16(s, WBT_SYNC);
1490 Stream_Write_UINT32(s, 12);
1491 Stream_Write_UINT32(s, WF_MAGIC);
1492 Stream_Write_UINT16(s, WF_VERSION_1_0);
1496rfx_write_message_codec_versions(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1499 WINPR_ASSERT(context);
1501 Stream_Write_UINT16(s, WBT_CODEC_VERSIONS);
1502 Stream_Write_UINT32(s, 10);
1503 Stream_Write_UINT8(s, 1);
1504 Stream_Write_UINT8(s, 1);
1505 Stream_Write_UINT16(s, WF_VERSION_1_0);
1508static inline void rfx_write_message_channels(
const RFX_CONTEXT* WINPR_RESTRICT context,
1511 WINPR_ASSERT(context);
1513 Stream_Write_UINT16(s, WBT_CHANNELS);
1514 Stream_Write_UINT32(s, 12);
1515 Stream_Write_UINT8(s, 1);
1516 Stream_Write_UINT8(s, 0);
1517 Stream_Write_UINT16(s, context->width);
1518 Stream_Write_UINT16(s, context->height);
1521static inline void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1524 UINT16 properties = 0;
1525 WINPR_ASSERT(context);
1527 Stream_Write_UINT16(s, WBT_CONTEXT);
1528 Stream_Write_UINT32(s, 13);
1529 Stream_Write_UINT8(s, 1);
1530 Stream_Write_UINT8(s, 0xFF);
1531 Stream_Write_UINT8(s, 0);
1532 Stream_Write_UINT16(s, CT_TILE_64x64);
1534 properties = context->flags;
1535 properties |= (COL_CONV_ICT << 3);
1536 properties |= (CLW_XFORM_DWT_53_A << 5);
1537 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9);
1538 properties |= (SCALAR_QUANTIZATION << 13);
1539 Stream_Write_UINT16(s, properties);
1540 rfx_update_context_properties(context);
1543static inline BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1546 WINPR_ASSERT(context);
1547 if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
1550 rfx_write_message_sync(context, s);
1551 rfx_write_message_context(context, s);
1552 rfx_write_message_codec_versions(context, s);
1553 rfx_write_message_channels(context, s);
1557static inline size_t rfx_tile_length(
const RFX_TILE* WINPR_RESTRICT tile)
1560 return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1563static inline BOOL rfx_write_tile(
wStream* WINPR_RESTRICT s,
const RFX_TILE* WINPR_RESTRICT tile)
1565 const size_t blockLen = rfx_tile_length(tile);
1566 if (blockLen > UINT32_MAX)
1568 if (!Stream_EnsureRemainingCapacity(s, blockLen))
1571 Stream_Write_UINT16(s, CBT_TILE);
1572 Stream_Write_UINT32(s, (UINT32)blockLen);
1573 Stream_Write_UINT8(s, tile->quantIdxY);
1574 Stream_Write_UINT8(s, tile->quantIdxCb);
1575 Stream_Write_UINT8(s, tile->quantIdxCr);
1576 Stream_Write_UINT16(s, tile->xIdx);
1577 Stream_Write_UINT16(s, tile->yIdx);
1578 Stream_Write_UINT16(s, tile->YLen);
1579 Stream_Write_UINT16(s, tile->CbLen);
1580 Stream_Write_UINT16(s, tile->CrLen);
1581 Stream_Write(s, tile->YData, tile->YLen);
1582 Stream_Write(s, tile->CbData, tile->CbLen);
1583 Stream_Write(s, tile->CrData, tile->CrLen);
1587struct S_RFX_TILE_COMPOSE_WORK_PARAM
1590 RFX_CONTEXT* context;
1593static inline void CALLBACK
1594rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
1595 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
1597 RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1598 WINPR_ASSERT(param);
1599 WINPR_ASSERT(param->context);
1600 WINPR_ASSERT(param->context->priv);
1602 if (!rfx_encode_rgb(param->context, param->tile))
1603 WLog_Print(param->context->priv->log, WLOG_ERROR,
"rfx_encode_rgb failed");
1606static inline BOOL computeRegion(
const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1607 REGION16* WINPR_RESTRICT region,
size_t width,
size_t height)
1609 const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1610 WINPR_ASSERTING_INT_CAST(UINT16, height) };
1612 WINPR_ASSERT(rects);
1613 for (
size_t i = 0; i < numRects; i++)
1617 rect16.left = rect->x;
1618 rect16.top = rect->y;
1619 rect16.right = rect->x + rect->width;
1620 rect16.bottom = rect->y + rect->height;
1622 if (!region16_union_rect(region, region, &rect16))
1626 return region16_intersect_rect(region, region, &mainRect);
1629#define TILE_NO(v) ((v) / 64)
1631static inline BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context,
size_t nbTiles)
1633 WINPR_ASSERT(context);
1635 RFX_CONTEXT_PRIV* priv = context->priv;
1638 void* pmem =
nullptr;
1640 if (!context->priv->UseThreads)
1643 if (!(pmem = winpr_aligned_recalloc((
void*)priv->workObjects, nbTiles,
sizeof(PTP_WORK), 32)))
1646 priv->workObjects = (PTP_WORK*)pmem;
1648 if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1649 sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1652 priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1656static inline BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count)
1658 WINPR_ASSERT(message);
1660 if (message->numTiles + count <= message->allocatedTiles)
1663 const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1664 return rfx_allocate_tiles(message, alloc, TRUE);
1667RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1668 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1669 const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h,
size_t s)
1671 const UINT32 width = w;
1672 const UINT32 height = h;
1673 const UINT32 scanline = (UINT32)s;
1674 RFX_MESSAGE* message =
nullptr;
1675 PTP_WORK* workObject =
nullptr;
1676 RFX_TILE_COMPOSE_WORK_PARAM* workParam =
nullptr;
1677 BOOL success = FALSE;
1678 REGION16 rectsRegion = WINPR_C_ARRAY_INIT;
1679 REGION16 tilesRegion = WINPR_C_ARRAY_INIT;
1684 WINPR_ASSERT(rects);
1685 WINPR_ASSERT(numRects > 0);
1686 WINPR_ASSERT(w > 0);
1687 WINPR_ASSERT(h > 0);
1688 WINPR_ASSERT(s > 0);
1690 if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1,
sizeof(RFX_MESSAGE), 32)))
1693 region16_init(&tilesRegion);
1694 region16_init(&rectsRegion);
1696 if (context->state == RFX_STATE_SEND_HEADERS)
1697 rfx_update_context_properties(context);
1699 message->frameIdx = context->frameIdx++;
1701 if (!context->numQuant)
1703 WINPR_ASSERT(context->quants ==
nullptr);
1704 WINPR_ASSERT(NR_QUANT_VALUES == ARRAYSIZE(rfx_default_quantization_values));
1705 if (!(context->quants =
1706 (UINT32*)winpr_aligned_malloc(
sizeof(rfx_default_quantization_values), 32)))
1707 goto skip_encoding_loop;
1709 CopyMemory(context->quants, &rfx_default_quantization_values,
1710 sizeof(rfx_default_quantization_values));
1711 context->numQuant = 1;
1712 context->quantIdxY = 0;
1713 context->quantIdxCb = 0;
1714 context->quantIdxCr = 0;
1717 message->numQuant = context->numQuant;
1718 message->quantVals = context->quants;
1721 const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1722 if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1723 goto skip_encoding_loop;
1726 const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1727 WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1728 WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1729 const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1730 const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1731 const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1733 if (!rfx_ensure_tiles(message, maxNbTiles))
1734 goto skip_encoding_loop;
1736 if (!setupWorkers(context, maxNbTiles))
1737 goto skip_encoding_loop;
1740 if (context->priv->UseThreads)
1742 workObject = context->priv->workObjects;
1743 workParam = context->priv->tileWorkParams;
1747 UINT32 regionNbRects = 0;
1748 regionRect = region16_rects(&rectsRegion, ®ionNbRects);
1750 if (!(message->rects = winpr_aligned_calloc(regionNbRects,
sizeof(
RFX_RECT), 32)))
1751 goto skip_encoding_loop;
1753 message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1755 for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1757 RFX_RECT* rfxRect = &message->rects[i];
1758 UINT32 startTileX = regionRect->left / 64;
1759 UINT32 endTileX = (regionRect->right - 1) / 64;
1760 UINT32 startTileY = regionRect->top / 64;
1761 UINT32 endTileY = (regionRect->bottom - 1) / 64;
1762 rfxRect->x = regionRect->left;
1763 rfxRect->y = regionRect->top;
1764 rfxRect->width = (regionRect->right - regionRect->left);
1765 rfxRect->height = (regionRect->bottom - regionRect->top);
1767 for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1768 yIdx++, gridRelY += 64)
1770 UINT32 tileHeight = 64;
1772 if ((yIdx == endTileY) && (gridRelY + 64 > height))
1773 tileHeight = height - gridRelY;
1775 currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1776 currentTileRect.bottom =
1777 WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1779 for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1780 xIdx++, gridRelX += 64)
1787 UINT32 tileWidth = 64;
1789 if ((xIdx == endTileX) && (gridRelX + 64 > width))
1791 tileWidth = (width - gridRelX);
1794 currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1795 currentTileRect.right =
1796 WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1799 if (region16_intersects_rect(&tilesRegion, ¤tTileRect))
1804 goto skip_encoding_loop;
1806 tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1807 tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1808 tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1809 tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1810 tile->scanline = scanline;
1812 tile->width = tileWidth;
1813 tile->height = tileHeight;
1814 const UINT32 ax = gridRelX;
1815 const UINT32 ay = gridRelY;
1817 if (tile->data && tile->allocated)
1819 winpr_aligned_free(tile->data);
1820 tile->allocated = FALSE;
1824 cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1825 tile->data = cnv.pv;
1826 tile->quantIdxY = context->quantIdxY;
1827 tile->quantIdxCb = context->quantIdxCb;
1828 tile->quantIdxCr = context->quantIdxCr;
1829 tile->YLen = tile->CbLen = tile->CrLen = 0;
1831 if (!(tile->YCbCrData =
1832 (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1833 goto skip_encoding_loop;
1835 tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1836 tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1837 tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1839 if (!rfx_ensure_tiles(message, 1))
1840 goto skip_encoding_loop;
1841 message->tiles[message->numTiles++] = tile;
1843 if (context->priv->UseThreads)
1845 workParam->context = context;
1846 workParam->tile = tile;
1849 CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1850 (
void*)workParam,
nullptr)))
1852 goto skip_encoding_loop;
1855 SubmitThreadpoolWork(*workObject);
1861 if (!rfx_encode_rgb(context, tile))
1862 goto skip_encoding_loop;
1865 if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect))
1866 goto skip_encoding_loop;
1879 message->tilesDataSize = 0;
1880 workObject = context->priv->workObjects;
1882 for (UINT32 i = 0; i < message->numTiles; i++)
1884 if (context->priv->UseThreads)
1888 WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1889 CloseThreadpoolWork(*workObject);
1895 const RFX_TILE* tile = message->tiles[i];
1896 const size_t tlen = rfx_tile_length(tile);
1897 message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen);
1900 region16_uninit(&tilesRegion);
1901 region16_uninit(&rectsRegion);
1906 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1908 rfx_message_free(context, message);
1909 region16_uninit(&tilesRegion);
1910 region16_uninit(&rectsRegion);
1914static inline BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1915 const RFX_MESSAGE* WINPR_RESTRICT src)
1920 WINPR_ASSERT(dst->rects ==
nullptr);
1921 WINPR_ASSERT(dst->numRects == 0);
1923 if (src->numRects == 0)
1926 dst->rects = winpr_aligned_calloc(src->numRects,
sizeof(
RFX_RECT), 32);
1929 dst->numRects = src->numRects;
1930 for (
size_t x = 0; x < src->numRects; x++)
1932 dst->rects[x] = src->rects[x];
1937static inline BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1938 const RFX_MESSAGE* WINPR_RESTRICT src)
1943 WINPR_ASSERT(dst->quantVals ==
nullptr);
1944 WINPR_ASSERT(dst->numQuant == 0);
1946 if (src->numQuant == 0)
1950 dst->quantVals = src->quantVals;
1951 dst->numQuant = src->numQuant;
1956static inline RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
1957 RFX_MESSAGE* WINPR_RESTRICT message,
1958 size_t* WINPR_RESTRICT numMessages,
size_t maxDataSize)
1960 WINPR_ASSERT(context);
1961 WINPR_ASSERT(message);
1962 WINPR_ASSERT(numMessages);
1964 if (maxDataSize <= 1024)
1967 maxDataSize -= 1024;
1968 *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
1970 RFX_MESSAGE* messages =
1971 (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages),
sizeof(RFX_MESSAGE), 32);
1976 for (UINT16 i = 0; i < message->numTiles; i++)
1978 RFX_TILE* tile = message->tiles[i];
1979 RFX_MESSAGE* msg = &messages[j];
1984 const size_t tileDataSize = rfx_tile_length(tile);
1986 if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
1989 if (msg->numTiles == 0)
1991 msg->frameIdx = message->frameIdx + j;
1992 if (!rfx_clone_quants(msg, message))
1994 if (!rfx_clone_rects(msg, message))
1996 msg->freeArray = TRUE;
1997 if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
2001 msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize);
2003 WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
2004 msg->tiles[msg->numTiles++] = message->tiles[i];
2005 message->tiles[i] =
nullptr;
2008 *numMessages = j + 1ULL;
2009 context->frameIdx += j;
2010 message->numTiles = 0;
2014 for (
size_t i = 0; i < j; i++)
2015 rfx_allocate_tiles(&messages[i], 0, FALSE);
2017 winpr_aligned_free(messages);
2021const RFX_MESSAGE* rfx_message_list_get(
const RFX_MESSAGE_LIST* WINPR_RESTRICT messages,
size_t idx)
2023 WINPR_ASSERT(messages);
2024 if (idx >= messages->count)
2026 WINPR_ASSERT(messages->list);
2027 return &messages->list[idx];
2030void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
2034 for (
size_t x = 0; x < messages->count; x++)
2035 rfx_message_clear(messages->context, &messages->list[x]);
2039static inline RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
2040 RFX_MESSAGE* WINPR_RESTRICT messages,
2043 WINPR_ASSERT(context);
2044 RFX_MESSAGE_LIST* msg = calloc(1,
sizeof(RFX_MESSAGE_LIST));
2047 msg->context = context;
2049 msg->list = messages;
2053RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
2054 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2055 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2056 UINT32 scanline,
size_t* WINPR_RESTRICT numMessages,
2059 WINPR_ASSERT(context);
2060 WINPR_ASSERT(numMessages);
2062 RFX_MESSAGE* message =
2063 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2067 RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2068 rfx_message_free(context, message);
2072 return rfx_message_list_new(context, list, *numMessages);
2075static inline BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2077 const RFX_MESSAGE* WINPR_RESTRICT message)
2079 WINPR_ASSERT(context);
2080 WINPR_ASSERT(message);
2082 const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2084 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2087 Stream_Write_UINT16(s, WBT_EXTENSION);
2088 Stream_Write_UINT32(s, blockLen);
2089 Stream_Write_UINT8(s, 1);
2090 Stream_Write_UINT8(s, 0);
2091 Stream_Write_UINT16(s, CBT_TILESET);
2092 Stream_Write_UINT16(s, 0);
2093 Stream_Write_UINT16(s, context->properties);
2095 s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant));
2096 Stream_Write_UINT8(s, 0x40);
2097 Stream_Write_UINT16(s, message->numTiles);
2098 Stream_Write_UINT32(s, message->tilesDataSize);
2100 UINT32* quantVals = message->quantVals;
2101 for (
size_t i = 0; i < message->numQuant * 5ul; i++)
2103 WINPR_ASSERT(quantVals);
2104 Stream_Write_UINT8(s,
2105 WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2109 for (
size_t i = 0; i < message->numTiles; i++)
2111 RFX_TILE* tile = message->tiles[i];
2115 if (!rfx_write_tile(s, tile))
2119#ifdef WITH_DEBUG_RFX
2120 WLog_Print(context->priv->log, WLOG_DEBUG,
2121 "numQuant: %" PRIu16
" numTiles: %" PRIu16
" tilesDataSize: %" PRIu32
"",
2122 message->numQuant, message->numTiles, message->tilesDataSize);
2128rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2129 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT message)
2131 WINPR_ASSERT(context);
2132 WINPR_ASSERT(message);
2134 if (!Stream_EnsureRemainingCapacity(s, 14))
2137 Stream_Write_UINT16(s, WBT_FRAME_BEGIN);
2138 Stream_Write_UINT32(s, 14);
2139 Stream_Write_UINT8(s, 1);
2140 Stream_Write_UINT8(s, 0);
2141 Stream_Write_UINT32(s, message->frameIdx);
2142 Stream_Write_UINT16(s, 1);
2146static inline BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2148 const RFX_MESSAGE* WINPR_RESTRICT message)
2150 WINPR_ASSERT(context);
2151 WINPR_ASSERT(message);
2153 const size_t blockLen = 15 + (message->numRects * 8);
2154 if (blockLen > UINT32_MAX)
2157 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2160 Stream_Write_UINT16(s, WBT_REGION);
2161 Stream_Write_UINT32(s, (UINT32)blockLen);
2162 Stream_Write_UINT8(s, 1);
2163 Stream_Write_UINT8(s, 0);
2164 Stream_Write_UINT8(s, 1);
2165 Stream_Write_UINT16(s, message->numRects);
2167 for (UINT16 i = 0; i < message->numRects; i++)
2169 const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2173 Stream_Write_UINT16(s, rect->x);
2174 Stream_Write_UINT16(s, rect->y);
2175 Stream_Write_UINT16(s, rect->width);
2176 Stream_Write_UINT16(s, rect->height);
2179 Stream_Write_UINT16(s, CBT_REGION);
2180 Stream_Write_UINT16(s, 1);
2185rfx_write_message_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2187 WINPR_ATTR_UNUSED
const RFX_MESSAGE* WINPR_RESTRICT message)
2189 WINPR_ASSERT(context);
2190 WINPR_ASSERT(message);
2192 if (!Stream_EnsureRemainingCapacity(s, 8))
2195 Stream_Write_UINT16(s, WBT_FRAME_END);
2196 Stream_Write_UINT32(s, 8);
2197 Stream_Write_UINT8(s, 1);
2198 Stream_Write_UINT8(s, 0);
2202BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2203 const RFX_MESSAGE* WINPR_RESTRICT message)
2205 WINPR_ASSERT(context);
2206 WINPR_ASSERT(message);
2208 if (context->state == RFX_STATE_SEND_HEADERS)
2210 if (!rfx_compose_message_header(context, s))
2213 context->state = RFX_STATE_SEND_FRAME_DATA;
2216 if (!rfx_write_message_frame_begin(context, s, message) ||
2217 !rfx_write_message_region(context, s, message) ||
2218 !rfx_write_message_tileset(context, s, message) ||
2219 !rfx_write_message_frame_end(context, s, message))
2227BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2228 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2229 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2232 WINPR_ASSERT(context);
2233 RFX_MESSAGE* message =
2234 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2238 const BOOL ret = rfx_write_message(context, s, message);
2239 rfx_message_free(context, message);
2243BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2245 WINPR_ASSERT(context);
2246 context->mode = mode;
2250RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2252 WINPR_ASSERT(context);
2253 return context->mode;
2256UINT32 rfx_context_get_frame_idx(
const RFX_CONTEXT* WINPR_RESTRICT context)
2258 WINPR_ASSERT(context);
2259 return context->frameIdx;
2262UINT32 rfx_message_get_frame_idx(
const RFX_MESSAGE* WINPR_RESTRICT message)
2264 WINPR_ASSERT(message);
2265 return message->frameIdx;
2268static inline BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2271 const UINT32 blockLen = 12;
2275 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2278 Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC);
2279 Stream_Write_UINT32(s, blockLen);
2280 Stream_Write_UINT32(s, 0xCACCACCA);
2281 Stream_Write_UINT16(s, 0x0100);
2286rfx_write_progressive_wb_context(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2289 const UINT32 blockLen = 10;
2293 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2296 Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT);
2297 Stream_Write_UINT32(s, blockLen);
2298 Stream_Write_UINT8(s, 0);
2299 Stream_Write_UINT16(s, 64);
2300 Stream_Write_UINT8(s, 0);
2304static inline BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2306 const RFX_MESSAGE* WINPR_RESTRICT msg)
2309 UINT32 blockLen = 18;
2310 UINT32 tilesDataSize = 0;
2311 const size_t start = Stream_GetPosition(s);
2317 blockLen += msg->numRects * 8;
2318 blockLen += msg->numQuant * 5;
2319 tilesDataSize = msg->numTiles * 22UL;
2320 for (UINT16 i = 0; i < msg->numTiles; i++)
2322 const RFX_TILE* tile = msg->tiles[i];
2324 tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2326 blockLen += tilesDataSize;
2328 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2331 Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION);
2332 Stream_Write_UINT32(s, blockLen);
2333 Stream_Write_UINT8(s, 64);
2334 Stream_Write_UINT16(s, msg->numRects);
2335 WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2336 Stream_Write_UINT8(s, (UINT8)msg->numQuant);
2337 Stream_Write_UINT8(s, 0);
2338 Stream_Write_UINT8(s, 0);
2339 Stream_Write_UINT16(s, msg->numTiles);
2340 Stream_Write_UINT32(s, tilesDataSize);
2342 for (UINT16 i = 0; i < msg->numRects; i++)
2345 const RFX_RECT* r = &msg->rects[i];
2346 Stream_Write_UINT16(s, r->x);
2347 Stream_Write_UINT16(s, r->y);
2348 Stream_Write_UINT16(s, r->width);
2349 Stream_Write_UINT16(s, r->height);
2360 for (UINT16 i = 0; i < msg->numQuant; i++)
2362 const UINT32* qv = &msg->quantVals[10ULL * i];
2364 Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4)));
2365 Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4)));
2366 Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4)));
2367 Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4)));
2368 Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4)));
2371 for (UINT16 i = 0; i < msg->numTiles; i++)
2373 const RFX_TILE* tile = msg->tiles[i];
2374 if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2378 const size_t end = Stream_GetPosition(s);
2379 const size_t used = end - start;
2380 return (used == blockLen);
2384rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2385 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT msg)
2387 const UINT32 blockLen = 12;
2392 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2395 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN);
2396 Stream_Write_UINT32(s, blockLen);
2397 Stream_Write_UINT32(s, msg->frameIdx);
2398 Stream_Write_UINT16(s, 1);
2404rfx_write_progressive_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2407 const UINT32 blockLen = 6;
2411 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2414 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END);
2415 Stream_Write_UINT32(s, blockLen);
2421rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2424 UINT32 blockLen = 0;
2429 blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2430 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2433 Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE);
2434 Stream_Write_UINT32(s, blockLen);
2435 Stream_Write_UINT8(s, tile->quantIdxY);
2436 Stream_Write_UINT8(s, tile->quantIdxCb);
2437 Stream_Write_UINT8(s, tile->quantIdxCr);
2438 Stream_Write_UINT16(s, tile->xIdx);
2439 Stream_Write_UINT16(s, tile->yIdx);
2440 Stream_Write_UINT8(s, 0);
2441 Stream_Write_UINT16(s, tile->YLen);
2442 Stream_Write_UINT16(s, tile->CbLen);
2443 Stream_Write_UINT16(s, tile->CrLen);
2444 Stream_Write_UINT16(s, 0);
2445 Stream_Write(s, tile->YData, tile->YLen);
2446 Stream_Write(s, tile->CbData, tile->CbLen);
2447 Stream_Write(s, tile->CrData, tile->CrLen);
2452const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2456 case PROGRESSIVE_WBT_SYNC:
2457 return "PROGRESSIVE_WBT_SYNC";
2459 case PROGRESSIVE_WBT_FRAME_BEGIN:
2460 return "PROGRESSIVE_WBT_FRAME_BEGIN";
2462 case PROGRESSIVE_WBT_FRAME_END:
2463 return "PROGRESSIVE_WBT_FRAME_END";
2465 case PROGRESSIVE_WBT_CONTEXT:
2466 return "PROGRESSIVE_WBT_CONTEXT";
2468 case PROGRESSIVE_WBT_REGION:
2469 return "PROGRESSIVE_WBT_REGION";
2471 case PROGRESSIVE_WBT_TILE_SIMPLE:
2472 return "PROGRESSIVE_WBT_TILE_SIMPLE";
2474 case PROGRESSIVE_WBT_TILE_FIRST:
2475 return "PROGRESSIVE_WBT_TILE_FIRST";
2477 case PROGRESSIVE_WBT_TILE_UPGRADE:
2478 return "PROGRESSIVE_WBT_TILE_UPGRADE";
2481 return "PROGRESSIVE_WBT_UNKNOWN";
2485BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
2487 const RFX_MESSAGE* WINPR_RESTRICT msg)
2493 if (rfx->mode != RLGR1)
2495 WLog_ERR(TAG,
"error, RLGR1 mode is required!");
2499 if (!rfx_write_progressive_wb_sync(rfx, s))
2502 if (!rfx_write_progressive_wb_context(rfx, s))
2505 if (!rfx_write_progressive_frame_begin(rfx, s, msg))
2508 if (!rfx_write_progressive_region(rfx, s, msg))
2511 if (!rfx_write_progressive_frame_end(rfx, s))
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew
OBJECT_INIT_FN fnObjectInit