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#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
34#include <winpr/registry.h>
37#include <freerdp/log.h>
38#include <freerdp/settings.h>
39#include <freerdp/codec/rfx.h>
40#include <freerdp/constants.h>
41#include <freerdp/primitives.h>
42#include <freerdp/codec/region.h>
43#include <freerdp/build-config.h>
44#include <freerdp/utils/helpers.h>
45#include <winpr/config-readers.h>
47#include "rfx_constants.h"
49#include "rfx_decode.h"
50#include "rfx_encode.h"
51#include "rfx_quantization.h"
54#include "../core/utils.h"
56#include "sse/rfx_sse2.h"
57#include "neon/rfx_neon.h"
59#define TAG FREERDP_TAG("codec")
61#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
62#define RFX_KEY "Software\\%s\\RemoteFX"
76static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
78static inline BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
80 const RFX_TILE* WINPR_RESTRICT tile);
81static BOOL rfx_message_clear(RFX_CONTEXT* WINPR_RESTRICT context,
82 RFX_MESSAGE* WINPR_RESTRICT message);
84static inline void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context)
86 if (!context || !context->priv)
88 PROFILER_CREATE(context->priv->prof_rfx_decode_rgb,
"rfx_decode_rgb")
89 PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component")
90 PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode")
91 PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode")
92 PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode")
93 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode")
94 PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB")
95 PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb")
96 PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component")
97 PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode")
98 PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode")
99 PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode")
100 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode")
101 PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr")
102 PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb")
105static inline
void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context)
107 if (!context || !context->priv)
109 PROFILER_FREE(context->priv->prof_rfx_decode_rgb)
110 PROFILER_FREE(context->priv->prof_rfx_decode_component)
111 PROFILER_FREE(context->priv->prof_rfx_rlgr_decode)
112 PROFILER_FREE(context->priv->prof_rfx_differential_decode)
113 PROFILER_FREE(context->priv->prof_rfx_quantization_decode)
114 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode)
115 PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb)
116 PROFILER_FREE(context->priv->prof_rfx_encode_rgb)
117 PROFILER_FREE(context->priv->prof_rfx_encode_component)
118 PROFILER_FREE(context->priv->prof_rfx_rlgr_encode)
119 PROFILER_FREE(context->priv->prof_rfx_differential_encode)
120 PROFILER_FREE(context->priv->prof_rfx_quantization_encode)
121 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode)
122 PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr)
123 PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb)
126static inline
void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context)
128 if (!context || !context->priv)
131 PROFILER_PRINT_HEADER
132 PROFILER_PRINT(context->priv->prof_rfx_decode_rgb)
133 PROFILER_PRINT(context->priv->prof_rfx_decode_component)
134 PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode)
135 PROFILER_PRINT(context->priv->prof_rfx_differential_decode)
136 PROFILER_PRINT(context->priv->prof_rfx_quantization_decode)
137 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode)
138 PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb)
139 PROFILER_PRINT(context->priv->prof_rfx_encode_rgb)
140 PROFILER_PRINT(context->priv->prof_rfx_encode_component)
141 PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode)
142 PROFILER_PRINT(context->priv->prof_rfx_differential_encode)
143 PROFILER_PRINT(context->priv->prof_rfx_quantization_encode)
144 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode)
145 PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr)
146 PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb)
147 PROFILER_PRINT_FOOTER
150static inline
void rfx_tile_init(
void* obj)
158 tile->YData =
nullptr;
160 tile->CbData =
nullptr;
162 tile->CrData =
nullptr;
166static inline void* rfx_decoder_tile_new(
const void* val)
168 const size_t size = 4ULL * 64ULL * 64ULL;
175 if (!(tile->data = (BYTE*)winpr_aligned_calloc(1, size, 16)))
177 winpr_aligned_free(tile);
180 memset(tile->data, 0xff, size);
181 tile->allocated = TRUE;
185static inline void rfx_decoder_tile_free(
void* obj)
192 winpr_aligned_free(tile->data);
194 winpr_aligned_free(tile);
198static inline void* rfx_encoder_tile_new(
const void* val)
201 return winpr_aligned_calloc(1,
sizeof(
RFX_TILE), 32);
204static inline void rfx_encoder_tile_free(
void* obj)
206 winpr_aligned_free(obj);
209RFX_CONTEXT* rfx_context_new(BOOL encoder)
211 return rfx_context_new_ex(encoder, 0);
214RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags)
216 RFX_CONTEXT_PRIV* priv =
nullptr;
217 RFX_CONTEXT* 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 priv->TilePool = ObjectPool_New(TRUE);
236 wObject* pool = ObjectPool_Object(priv->TilePool);
239 if (context->encoder)
264 priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16);
266 if (!priv->BufferPool)
269 priv->UseThreads = FALSE;
271 const char config[] =
"remotefx.json";
272 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
273 priv->UseThreads = TRUE;
275#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
277 char* key = freerdp_getApplicatonDetailsRegKey(RFX_KEY);
282 RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
285 if (status == ERROR_SUCCESS)
287 WLog_WARN(TAG,
"HKLM.reg is deprecated since 3.33.0. ATTENTION: Use %s instead!",
291 DWORD dwSize =
sizeof(dwValue);
293 if (RegQueryValueEx(hKey, _T(
"UseThreads"),
nullptr, &dwType, (BYTE*)&dwValue,
294 &dwSize) == ERROR_SUCCESS)
295 priv->UseThreads = dwValue ? 1 : 0;
303 WINPR_JSON* json = freerdp_GetJSONConfigFile(TRUE, config);
306 winpr_config_apply_bool(config, json,
"UseThreads", &priv->UseThreads);
311 if (priv->UseThreads)
316 if (!primitives_get())
321 rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32);
323 rfx_profiler_create(context);
325 context->quantization_decode = rfx_quantization_decode;
326 context->quantization_encode = rfx_quantization_encode;
327 context->dwt_2d_decode = rfx_dwt_2d_decode;
328 context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode;
329 context->dwt_2d_encode = rfx_dwt_2d_encode;
330 context->rlgr_decode = rfx_rlgr_decode;
331 context->rlgr_encode = rfx_rlgr_encode;
332 rfx_init_sse2(context);
333 rfx_init_neon(context);
334 context->state = RFX_STATE_SEND_HEADERS;
335 context->expectedDataBlockType = WBT_FRAME_BEGIN;
338 WINPR_PRAGMA_DIAG_PUSH
339 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
340 rfx_context_free(context);
341 WINPR_PRAGMA_DIAG_POP
345void rfx_context_free(RFX_CONTEXT* context)
347 RFX_CONTEXT_PRIV* priv =
nullptr;
352 WINPR_ASSERT(
nullptr != context);
354 priv = context->priv;
355 WINPR_ASSERT(
nullptr != priv);
356 WINPR_ASSERT(
nullptr != priv->TilePool);
357 WINPR_ASSERT(
nullptr != priv->BufferPool);
360 rfx_message_clear(context, &context->currentMessage);
361 winpr_aligned_free(context->quants);
362 rfx_profiler_print(context);
363 rfx_profiler_free(context);
367 ObjectPool_Free(priv->TilePool);
368 if (priv->UseThreads)
370 winpr_aligned_free((
void*)priv->workObjects);
371 winpr_aligned_free(priv->tileWorkParams);
375 "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!");
379 BufferPool_Free(priv->BufferPool);
380 winpr_aligned_free(priv);
382 winpr_aligned_free(context);
385static inline RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
387 WINPR_ASSERT(message);
388 WINPR_ASSERT(message->tiles);
389 WINPR_ASSERT(index < message->numTiles);
390 return message->tiles[index];
393static inline const RFX_RECT* rfx_message_get_rect_const(
const RFX_MESSAGE* WINPR_RESTRICT message,
396 WINPR_ASSERT(message);
397 WINPR_ASSERT(message->rects);
398 WINPR_ASSERT(index < message->numRects);
399 return &message->rects[index];
402static inline RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
404 WINPR_ASSERT(message);
405 WINPR_ASSERT(message->rects);
406 WINPR_ASSERT(index < message->numRects);
407 return &message->rects[index];
410void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format)
412 WINPR_ASSERT(context);
413 context->pixel_format = pixel_format;
414 const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format);
415 context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp);
418UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context)
420 WINPR_ASSERT(context);
421 return context->pixel_format;
424void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context,
425 const BYTE* WINPR_RESTRICT palette)
427 WINPR_ASSERT(context);
428 context->palette = palette;
431const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context)
433 WINPR_ASSERT(context);
434 return context->palette;
437BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
442 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
443 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
444 context->state = RFX_STATE_SEND_HEADERS;
445 context->expectedDataBlockType = WBT_FRAME_BEGIN;
446 context->frameIdx = 0;
450static inline BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context,
455 WINPR_ASSERT(context);
456 WINPR_ASSERT(context->priv);
457 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC;
460 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
463 Stream_Read_UINT32(s, magic);
464 if (magic != WF_MAGIC)
466 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid magic number 0x%08" PRIX32
"", magic);
470 Stream_Read_UINT16(s, context->version);
471 if (context->version != WF_VERSION_1_0)
473 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid version number 0x%08" PRIX32
"",
478 WLog_Print(context->priv->log, WLOG_DEBUG,
"version 0x%08" PRIX32
"", context->version);
479 context->decodedHeaderBlocks |= RFX_DECODED_SYNC;
483static inline BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context,
488 WINPR_ASSERT(context);
489 WINPR_ASSERT(context->priv);
490 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS;
492 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
495 Stream_Read_UINT8(s, numCodecs);
496 Stream_Read_UINT8(s, context->codec_id);
498 s, context->codec_version);
502 WLog_Print(context->priv->log, WLOG_ERROR,
"numCodes is 0x%02" PRIX8
" (must be 0x01)",
507 if (context->codec_id != 0x01)
509 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec id (0x%02" PRIX32
")",
514 if (context->codec_version != WF_VERSION_1_0)
516 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec version (0x%08" PRIX32
")",
517 context->codec_version);
521 WLog_Print(context->priv->log, WLOG_DEBUG,
"id %" PRIu32
" version 0x%" PRIX32
".",
522 context->codec_id, context->codec_version);
523 context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS;
527static inline BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context,
531 BYTE numChannels = 0;
533 WINPR_ASSERT(context);
534 WINPR_ASSERT(context->priv);
535 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS;
537 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1))
540 Stream_Read_UINT8(s, numChannels);
547 WLog_Print(context->priv->log, WLOG_ERROR,
"no channels announced");
551 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull))
555 Stream_Read_UINT8(s, channelId);
557 if (channelId != 0x00)
559 WLog_Print(context->priv->log, WLOG_ERROR,
"channelId:0x%02" PRIX8
", expected:0x00",
564 Stream_Read_UINT16(s, context->width);
565 Stream_Read_UINT16(s, context->height);
567 if (!context->width || !context->height)
569 WLog_Print(context->priv->log, WLOG_ERROR,
570 "invalid channel with/height: %" PRIu16
"x%" PRIu16
"", context->width,
576 Stream_Seek(s, 5ULL * (numChannels - 1));
577 WLog_Print(context->priv->log, WLOG_DEBUG,
578 "numChannels %" PRIu8
" id %" PRIu8
", %" PRIu16
"x%" PRIu16
".", numChannels,
579 channelId, context->width, context->height);
580 context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS;
584static inline BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
589 UINT16 properties = 0;
591 WINPR_ASSERT(context);
592 WINPR_ASSERT(context->priv);
593 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT;
595 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5))
598 Stream_Read_UINT8(s, ctxId);
599 Stream_Read_UINT16(s, tileSize);
600 Stream_Read_UINT16(s, properties);
601 WLog_Print(context->priv->log, WLOG_DEBUG,
602 "ctxId %" PRIu8
" tileSize %" PRIu16
" properties 0x%04" PRIX16
".", ctxId, tileSize,
604 context->properties = properties;
605 context->flags = (properties & 0x0007);
607 if (context->flags == CODEC_MODE)
609 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in image mode.");
613 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in video mode.");
616 switch ((properties & 0x1E00) >> 9)
618 case CLW_ENTROPY_RLGR1:
619 context->mode = RLGR1;
620 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR1.");
623 case CLW_ENTROPY_RLGR3:
624 context->mode = RLGR3;
625 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR3.");
629 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown RLGR algorithm.");
633 context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT;
637static inline BOOL rfx_process_message_frame_begin(
638 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
639 wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
642 UINT16 numRegions = 0;
644 WINPR_ASSERT(context);
645 WINPR_ASSERT(context->priv);
646 WINPR_ASSERT(message);
647 WINPR_ASSERT(pExpectedBlockType);
649 if (*pExpectedBlockType != WBT_FRAME_BEGIN)
651 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_FRAME_BEGIN");
655 *pExpectedBlockType = WBT_REGION;
657 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
662 Stream_Read_UINT16(s, numRegions);
663 WLog_Print(context->priv->log, WLOG_DEBUG,
664 "RFX_FRAME_BEGIN: frameIdx: %" PRIu32
" numRegions: %" PRIu16
"", frameIdx,
669static inline BOOL rfx_process_message_frame_end(
670 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
671 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
673 WINPR_ASSERT(context);
674 WINPR_ASSERT(context->priv);
675 WINPR_ASSERT(message);
677 WINPR_ASSERT(pExpectedBlockType);
679 if (*pExpectedBlockType != WBT_FRAME_END)
681 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected, wants WBT_FRAME_END");
685 *pExpectedBlockType = WBT_FRAME_BEGIN;
686 WLog_Print(context->priv->log, WLOG_DEBUG,
"RFX_FRAME_END");
690static inline BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message)
692 WINPR_ASSERT(message);
695 winpr_aligned_recalloc(message->rects, message->numRects,
sizeof(
RFX_RECT), 32);
698 message->rects = tmpRects;
702static inline BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
703 RFX_MESSAGE* WINPR_RESTRICT message,
705 UINT16* WINPR_RESTRICT pExpectedBlockType)
707 UINT16 regionType = 0;
708 UINT16 numTileSets = 0;
710 WINPR_ASSERT(context);
711 WINPR_ASSERT(context->priv);
712 WINPR_ASSERT(message);
713 WINPR_ASSERT(pExpectedBlockType);
715 if (*pExpectedBlockType != WBT_REGION)
717 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_REGION");
721 *pExpectedBlockType = WBT_EXTENSION;
723 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3))
726 Stream_Seek_UINT8(s);
727 Stream_Read_UINT16(s, message->numRects);
729 if (message->numRects < 1)
737 message->numRects = 1;
738 if (!rfx_resize_rects(message))
741 message->rects->x = 0;
742 message->rects->y = 0;
743 message->rects->width = context->width;
744 message->rects->height = context->height;
748 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull))
751 if (!rfx_resize_rects(message))
755 for (UINT16 i = 0; i < message->numRects; i++)
757 RFX_RECT* rect = rfx_message_get_rect(message, i);
759 Stream_Read_UINT16(s, rect->x);
760 Stream_Read_UINT16(s, rect->y);
761 Stream_Read_UINT16(s, rect->width);
762 Stream_Read_UINT16(s, rect->height);
763 WLog_Print(context->priv->log, WLOG_DEBUG,
764 "rect %" PRIu16
" (x,y=%" PRIu16
",%" PRIu16
" w,h=%" PRIu16
" %" PRIu16
").", i,
765 rect->x, rect->y, rect->width, rect->height);
768 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
771 Stream_Read_UINT16(s, regionType);
772 Stream_Read_UINT16(s, numTileSets);
774 if (regionType != CBT_REGION)
776 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid region type 0x%04" PRIX16
"",
781 if (numTileSets != 0x0001)
783 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid number of tilesets (%" PRIu16
")",
794 RFX_CONTEXT* context;
795} RFX_TILE_PROCESS_WORK_PARAM;
797static inline void CALLBACK
798rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
799 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
801 RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
803 WINPR_ASSERT(param->context);
804 WINPR_ASSERT(param->context->priv);
806 if (!rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4))
807 WLog_Print(param->context->priv->log, WLOG_ERROR,
"rfx_decode_rgb failed");
810static inline BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count,
813 WINPR_ASSERT(message);
816 (
RFX_TILE**)winpr_aligned_recalloc((
void*)message->tiles, count,
sizeof(
RFX_TILE*), 32);
817 if (!tmpTiles && (count != 0))
820 message->tiles = tmpTiles;
822 message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count);
825 WINPR_ASSERT(message->numTiles <= count);
827 message->allocatedTiles = count;
832static inline BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
833 RFX_MESSAGE* WINPR_RESTRICT message,
835 UINT16* WINPR_RESTRICT pExpectedBlockType)
838 size_t close_cnt = 0;
841 UINT32* quants =
nullptr;
845 UINT32 blockType = 0;
846 UINT32 tilesDataSize = 0;
847 PTP_WORK* work_objects =
nullptr;
848 RFX_TILE_PROCESS_WORK_PARAM* params =
nullptr;
849 void* pmem =
nullptr;
851 WINPR_ASSERT(context);
852 WINPR_ASSERT(context->priv);
853 WINPR_ASSERT(message);
854 WINPR_ASSERT(pExpectedBlockType);
856 if (*pExpectedBlockType != WBT_EXTENSION)
858 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants a tileset");
862 *pExpectedBlockType = WBT_FRAME_END;
864 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14))
867 Stream_Read_UINT16(s, subtype);
868 if (subtype != CBT_TILESET)
870 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid subtype, expected CBT_TILESET.");
874 Stream_Seek_UINT16(s);
875 Stream_Seek_UINT16(s);
876 Stream_Read_UINT8(s, context->numQuant);
877 Stream_Seek_UINT8(s);
879 if (context->numQuant < 1)
881 WLog_Print(context->priv->log, WLOG_ERROR,
"no quantization value.");
885 Stream_Read_UINT16(s, numTiles);
892 Stream_Read_UINT32(s, tilesDataSize);
894 if (!(pmem = winpr_aligned_recalloc(context->quants, context->numQuant,
895 NR_QUANT_VALUES *
sizeof(UINT32), 32)))
898 quants = context->quants = (UINT32*)pmem;
901 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull))
904 for (
size_t i = 0; i < context->numQuant; i++)
907 Stream_Read_UINT8(s, quant);
908 *quants++ = (quant & 0x0F);
909 *quants++ = (quant >> 4);
910 Stream_Read_UINT8(s, quant);
911 *quants++ = (quant & 0x0F);
912 *quants++ = (quant >> 4);
913 Stream_Read_UINT8(s, quant);
914 *quants++ = (quant & 0x0F);
915 *quants++ = (quant >> 4);
916 Stream_Read_UINT8(s, quant);
917 *quants++ = (quant & 0x0F);
918 *quants++ = (quant >> 4);
919 Stream_Read_UINT8(s, quant);
920 *quants++ = (quant & 0x0F);
921 *quants++ = (quant >> 4);
923 context->priv->log, WLOG_DEBUG,
924 "quant %" PRIuz
" (%" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
925 " %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
").",
926 i, context->quants[i * NR_QUANT_VALUES], context->quants[i * NR_QUANT_VALUES + 1],
927 context->quants[i * NR_QUANT_VALUES + 2], context->quants[i * NR_QUANT_VALUES + 3],
928 context->quants[i * NR_QUANT_VALUES + 4], context->quants[i * NR_QUANT_VALUES + 5],
929 context->quants[i * NR_QUANT_VALUES + 6], context->quants[i * NR_QUANT_VALUES + 7],
930 context->quants[i * NR_QUANT_VALUES + 8], context->quants[i * NR_QUANT_VALUES + 9]);
933 for (
size_t i = 0; i < message->numTiles; i++)
935 ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
936 message->tiles[i] =
nullptr;
939 if (!rfx_allocate_tiles(message, numTiles, FALSE))
942 if (context->priv->UseThreads)
944 work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles,
sizeof(PTP_WORK), 32);
945 params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc(
946 nullptr, message->numTiles,
sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32);
950 winpr_aligned_free(params);
956 winpr_aligned_free((
void*)work_objects);
965 if (Stream_GetRemainingLength(s) >= tilesDataSize)
968 for (
size_t i = 0; i < message->numTiles; i++)
973 if (!(tile = (
RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
975 WLog_Print(context->priv->log, WLOG_ERROR,
976 "RfxMessageTileSet failed to get tile from object pool");
981 message->tiles[i] = tile;
984 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
986 WLog_Print(context->priv->log, WLOG_ERROR,
987 "RfxMessageTileSet packet too small to read tile %" PRIuz
"/%" PRIu16
"",
988 i, message->numTiles);
993 sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
996 Stream_Read_UINT32(sub, blockLen);
998 if (!Stream_SafeSeek(s, blockLen))
1003 if ((blockLen < 6 + 13) ||
1004 (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6)))
1006 WLog_Print(context->priv->log, WLOG_ERROR,
1007 "RfxMessageTileSet not enough bytes to read tile %" PRIuz
"/%" PRIu16
1008 " with blocklen=%" PRIu32
"",
1009 i, message->numTiles, blockLen);
1014 if (blockType != CBT_TILE)
1016 WLog_Print(context->priv->log, WLOG_ERROR,
1017 "unknown block type 0x%" PRIX32
", expected CBT_TILE (0xCAC3).",
1023 Stream_Read_UINT8(sub, tile->quantIdxY);
1024 Stream_Read_UINT8(sub, tile->quantIdxCb);
1025 Stream_Read_UINT8(sub, tile->quantIdxCr);
1026 if (tile->quantIdxY >= context->numQuant)
1028 WLog_Print(context->priv->log, WLOG_ERROR,
1029 "quantIdxY %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxY,
1034 if (tile->quantIdxCb >= context->numQuant)
1036 WLog_Print(context->priv->log, WLOG_ERROR,
1037 "quantIdxCb %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCb,
1042 if (tile->quantIdxCr >= context->numQuant)
1044 WLog_Print(context->priv->log, WLOG_ERROR,
1045 "quantIdxCr %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCr,
1051 Stream_Read_UINT16(sub, tile->xIdx);
1052 Stream_Read_UINT16(sub, tile->yIdx);
1053 Stream_Read_UINT16(sub, tile->YLen);
1054 Stream_Read_UINT16(sub, tile->CbLen);
1055 Stream_Read_UINT16(sub, tile->CrLen);
1056 tile->YData = Stream_PointerAs(sub, BYTE);
1057 if (!Stream_SafeSeek(sub, tile->YLen))
1062 tile->CbData = Stream_PointerAs(sub, BYTE);
1063 if (!Stream_SafeSeek(sub, tile->CbLen))
1068 tile->CrData = Stream_PointerAs(sub, BYTE);
1069 if (!Stream_SafeSeek(sub, tile->CrLen))
1074 tile->x = tile->xIdx * 64;
1075 tile->y = tile->yIdx * 64;
1077 if (context->priv->UseThreads)
1085 params[i].context = context;
1086 params[i].tile = message->tiles[i];
1088 if (!(work_objects[i] = CreateThreadpoolWork(rfx_process_message_tile_work_callback,
1089 (
void*)¶ms[i],
nullptr)))
1091 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThreadpoolWork failed.");
1096 SubmitThreadpoolWork(work_objects[i]);
1101 if (!rfx_decode_rgb(context, tile, tile->data, 64 * 4))
1110 if (context->priv->UseThreads)
1112 for (
size_t i = 0; i < close_cnt; i++)
1114 WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
1115 CloseThreadpoolWork(work_objects[i]);
1119 winpr_aligned_free((
void*)work_objects);
1120 winpr_aligned_free(params);
1122 for (
size_t i = 0; i < message->numTiles; i++)
1124 if (!(tile = message->tiles[i]))
1127 tile->YLen = tile->CbLen = tile->CrLen = 0;
1128 tile->YData = tile->CbData = tile->CrData =
nullptr;
1135static BOOL rect_is_valid(
const RFX_RECT* rect, UINT32 left, UINT32 top,
1138 WINPR_ASSERT(clippingRect);
1142 WLog_ERR(TAG,
"rect=NULL");
1145 if (left > UINT16_MAX - 1u * rect->x)
1147 WLog_ERR(TAG,
"left(%" PRIu32
") + rect->x(%d) > %d", left, rect->x, UINT16_MAX);
1150 if (top > UINT16_MAX - 1u * rect->y)
1152 WLog_ERR(TAG,
"top(%" PRIu32
") + rect->y(%d) > %d", top, rect->y, UINT16_MAX);
1155 if (clippingRect->left > UINT16_MAX - rect->width)
1157 WLog_ERR(TAG,
"clippingRect->left(%" PRIu32
") + rect->width(%d) > %d", clippingRect->left,
1158 rect->width, UINT16_MAX);
1161 if (clippingRect->top > UINT16_MAX - rect->height)
1163 WLog_ERR(TAG,
"clippingRect->top(%" PRIu32
") + rect->height(%d) > %d", clippingRect->top,
1164 rect->height, UINT16_MAX);
1170BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT data,
1171 UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst,
1172 UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight,
1173 REGION16* WINPR_RESTRICT invalidRegion)
1175 REGION16 updateRegion = WINPR_C_ARRAY_INIT;
1176 wStream inStream = WINPR_C_ARRAY_INIT;
1179 if (!context || !data || !length)
1182 WINPR_ASSERT(context->priv);
1183 RFX_MESSAGE* message = &context->currentMessage;
1185 wStream* s = Stream_StaticConstInit(&inStream, data, length);
1187 while (ok && Stream_GetRemainingLength(s) > 6)
1189 wStream subStreamBuffer = WINPR_C_ARRAY_INIT;
1190 size_t extraBlockLen = 0;
1191 UINT32 blockLen = 0;
1192 UINT32 blockType = 0;
1195 Stream_Read_UINT16(s, blockType);
1196 Stream_Read_UINT32(s, blockLen);
1197 WLog_Print(context->priv->log, WLOG_DEBUG,
"blockType 0x%" PRIX32
" blockLen %" PRIu32
"",
1198 blockType, blockLen);
1202 WLog_Print(context->priv->log, WLOG_ERROR,
"blockLen too small(%" PRIu32
")", blockLen);
1206 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6))
1209 if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS)
1211 WLog_Print(context->priv->log, WLOG_ERROR,
"incomplete header blocks processing");
1215 if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
1219 UINT8 channelId = 0;
1221 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1225 Stream_Read_UINT8(s, codecId);
1226 Stream_Read_UINT8(s, channelId);
1228 if (codecId != 0x01)
1230 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codecId 0x%02" PRIX8
"",
1235 if (blockType == WBT_CONTEXT)
1238 if (channelId != 0xFF)
1240 WLog_Print(context->priv->log, WLOG_ERROR,
1241 "invalid channelId 0x%02" PRIX8
" for blockType 0x%08" PRIX32
"",
1242 channelId, blockType);
1249 if (channelId != 0x00)
1251 WLog_Print(context->priv->log, WLOG_ERROR,
1252 "invalid channelId 0x%02" PRIX8
" for blockType WBT_CONTEXT",
1259 const size_t blockLenNoHeader = blockLen - 6;
1260 if (blockLenNoHeader < extraBlockLen)
1262 WLog_Print(context->priv->log, WLOG_ERROR,
1263 "blockLen too small(%" PRIu32
"), must be >= 6 + %" PRIuz, blockLen,
1268 const size_t subStreamLen = blockLenNoHeader - extraBlockLen;
1269 wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen);
1270 Stream_Seek(s, subStreamLen);
1279 ok = rfx_process_message_sync(context, subStream);
1283 ok = rfx_process_message_context(context, subStream);
1286 case WBT_CODEC_VERSIONS:
1287 ok = rfx_process_message_codec_versions(context, subStream);
1291 ok = rfx_process_message_channels(context, subStream);
1301 case WBT_FRAME_BEGIN:
1302 ok = rfx_process_message_frame_begin(context, message, subStream,
1303 &context->expectedDataBlockType);
1307 ok = rfx_process_message_region(context, message, subStream,
1308 &context->expectedDataBlockType);
1312 ok = rfx_process_message_tileset(context, message, subStream,
1313 &context->expectedDataBlockType);
1317 ok = rfx_process_message_frame_end(context, message, subStream,
1318 &context->expectedDataBlockType);
1322 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown blockType 0x%" PRIX32
"",
1330 UINT32 nbUpdateRects = 0;
1331 REGION16 clippingRects = WINPR_C_ARRAY_INIT;
1333 const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format);
1334 const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat);
1335 region16_init(&clippingRects);
1337 WINPR_ASSERT(dstWidth <= UINT16_MAX);
1338 WINPR_ASSERT(dstHeight <= UINT16_MAX);
1339 for (UINT32 i = 0; i < message->numRects; i++)
1342 const RFX_RECT* rect = &(message->rects[i]);
1344 if (!rect_is_valid(rect, left, top, &clippingRect))
1346 region16_uninit(&updateRegion);
1347 region16_uninit(&clippingRects);
1351 clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth));
1352 clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight));
1354 const UINT32 rw = 1UL * clippingRect.left + rect->width;
1355 const UINT32 rh = 1UL * clippingRect.top + rect->height;
1356 const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth));
1357 const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight));
1358 clippingRect.right = right;
1359 clippingRect.bottom = bottom;
1360 if (!region16_union_rect(&clippingRects, &clippingRects, &clippingRect))
1362 region16_uninit(&updateRegion);
1363 region16_uninit(&clippingRects);
1368 for (UINT32 i = 0; i < message->numTiles; i++)
1371 const RFX_TILE* tile = rfx_message_get_tile(message, i);
1373 WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1374 WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1376 updateRect.left = (UINT16)left + tile->x;
1377 updateRect.top = (UINT16)top + tile->y;
1378 updateRect.right = updateRect.left + 64;
1379 updateRect.bottom = updateRect.top + 64;
1380 region16_init(&updateRegion);
1381 if (!region16_intersect_rect(&updateRegion, &clippingRects, &updateRect))
1383 region16_uninit(&updateRegion);
1384 region16_uninit(&clippingRects);
1387 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1389 for (UINT32 j = 0; j < nbUpdateRects; j++)
1392 const UINT32 stride = 64 * formatSize;
1393 const UINT32 nXDst = cur->left;
1394 const UINT32 nYDst = cur->top;
1395 const UINT32 nXSrc = nXDst - updateRect.left;
1396 const UINT32 nYSrc = nYDst - updateRect.top;
1397 const UINT32 nWidth = cur->right - cur->left;
1398 const UINT32 nHeight = cur->bottom - cur->top;
1400 if (!freerdp_image_copy_no_overlap(
1401 dst, dstFormat, dstStride, nXDst, nYDst, nWidth, nHeight, tile->data,
1402 context->pixel_format, stride, nXSrc, nYSrc,
nullptr, FREERDP_FLIP_NONE))
1404 region16_uninit(&updateRegion);
1405 region16_uninit(&clippingRects);
1406 WLog_Print(context->priv->log, WLOG_ERROR,
1407 "nbUpdateRectx[%" PRIu32
" (%" PRIu32
")] freerdp_image_copy failed",
1414 if (!region16_union_rect(invalidRegion, invalidRegion, cur))
1416 region16_uninit(&updateRegion);
1417 region16_uninit(&clippingRects);
1423 region16_uninit(&updateRegion);
1426 region16_uninit(&clippingRects);
1431 rfx_message_clear(context, message);
1432 context->currentMessage.freeArray = TRUE;
1435 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1439const UINT32* rfx_message_get_quants(
const RFX_MESSAGE* WINPR_RESTRICT message,
1440 UINT16* WINPR_RESTRICT numQuantVals)
1442 WINPR_ASSERT(message);
1444 *numQuantVals = message->numQuant;
1445 return message->quantVals;
1448const RFX_TILE** rfx_message_get_tiles(
const RFX_MESSAGE* WINPR_RESTRICT message,
1449 UINT16* WINPR_RESTRICT numTiles)
1451 WINPR_ASSERT(message);
1453 *numTiles = message->numTiles;
1460 cnv.pp = message->tiles;
1464UINT16 rfx_message_get_tile_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1466 WINPR_ASSERT(message);
1467 return message->numTiles;
1470const RFX_RECT* rfx_message_get_rects(
const RFX_MESSAGE* WINPR_RESTRICT message,
1471 UINT16* WINPR_RESTRICT numRects)
1473 WINPR_ASSERT(message);
1475 *numRects = message->numRects;
1476 return message->rects;
1479UINT16 rfx_message_get_rect_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1481 WINPR_ASSERT(message);
1482 return message->numRects;
1485static BOOL rfx_message_clear(RFX_CONTEXT* WINPR_RESTRICT context,
1486 RFX_MESSAGE* WINPR_RESTRICT message)
1491 winpr_aligned_free(message->rects);
1495 for (
size_t i = 0; i < message->numTiles; i++)
1497 RFX_TILE* tile = message->tiles[i];
1501 if (tile->YCbCrData)
1503 BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1504 tile->YCbCrData =
nullptr;
1507 ObjectPool_Return(context->priv->TilePool, (
void*)tile);
1510 rfx_allocate_tiles(message, 0, FALSE);
1513 const BOOL freeArray = message->freeArray;
1514 const RFX_MESSAGE empty = WINPR_C_ARRAY_INIT;
1519void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1521 if (!rfx_message_clear(context, message))
1522 winpr_aligned_free(message);
1525static inline void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1527 UINT16 properties = 0;
1529 WINPR_ASSERT(context);
1532 properties |= (context->flags << 1);
1533 properties |= (COL_CONV_ICT << 4);
1534 properties |= (CLW_XFORM_DWT_53_A << 6);
1535 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10);
1536 properties |= (SCALAR_QUANTIZATION << 14);
1537 context->properties = properties;
1541rfx_write_message_sync(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1542 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s)
1544 WINPR_ASSERT(context);
1546 Stream_Write_UINT16(s, WBT_SYNC);
1547 Stream_Write_UINT32(s, 12);
1548 Stream_Write_UINT32(s, WF_MAGIC);
1549 Stream_Write_UINT16(s, WF_VERSION_1_0);
1553rfx_write_message_codec_versions(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1556 WINPR_ASSERT(context);
1558 Stream_Write_UINT16(s, WBT_CODEC_VERSIONS);
1559 Stream_Write_UINT32(s, 10);
1560 Stream_Write_UINT8(s, 1);
1561 Stream_Write_UINT8(s, 1);
1562 Stream_Write_UINT16(s, WF_VERSION_1_0);
1565static inline void rfx_write_message_channels(
const RFX_CONTEXT* WINPR_RESTRICT context,
1568 WINPR_ASSERT(context);
1570 Stream_Write_UINT16(s, WBT_CHANNELS);
1571 Stream_Write_UINT32(s, 12);
1572 Stream_Write_UINT8(s, 1);
1573 Stream_Write_UINT8(s, 0);
1574 Stream_Write_UINT16(s, context->width);
1575 Stream_Write_UINT16(s, context->height);
1578static inline void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1581 UINT16 properties = 0;
1582 WINPR_ASSERT(context);
1584 Stream_Write_UINT16(s, WBT_CONTEXT);
1585 Stream_Write_UINT32(s, 13);
1586 Stream_Write_UINT8(s, 1);
1587 Stream_Write_UINT8(s, 0xFF);
1588 Stream_Write_UINT8(s, 0);
1589 Stream_Write_UINT16(s, CT_TILE_64x64);
1591 properties = context->flags;
1592 properties |= (COL_CONV_ICT << 3);
1593 properties |= (CLW_XFORM_DWT_53_A << 5);
1594 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9);
1595 properties |= (SCALAR_QUANTIZATION << 13);
1596 Stream_Write_UINT16(s, properties);
1597 rfx_update_context_properties(context);
1600static inline BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1603 WINPR_ASSERT(context);
1604 if (!Stream_EnsureRemainingCapacity(s, 12ull + 10ull + 12ull + 13ull))
1607 rfx_write_message_sync(context, s);
1608 rfx_write_message_context(context, s);
1609 rfx_write_message_codec_versions(context, s);
1610 rfx_write_message_channels(context, s);
1614static inline size_t rfx_tile_length(
const RFX_TILE* WINPR_RESTRICT tile)
1617 return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1620static inline BOOL rfx_write_tile(
wStream* WINPR_RESTRICT s,
const RFX_TILE* WINPR_RESTRICT tile)
1622 const size_t blockLen = rfx_tile_length(tile);
1623 if (blockLen > UINT32_MAX)
1625 if (!Stream_EnsureRemainingCapacity(s, blockLen))
1628 Stream_Write_UINT16(s, CBT_TILE);
1629 Stream_Write_UINT32(s, (UINT32)blockLen);
1630 Stream_Write_UINT8(s, tile->quantIdxY);
1631 Stream_Write_UINT8(s, tile->quantIdxCb);
1632 Stream_Write_UINT8(s, tile->quantIdxCr);
1633 Stream_Write_UINT16(s, tile->xIdx);
1634 Stream_Write_UINT16(s, tile->yIdx);
1635 Stream_Write_UINT16(s, tile->YLen);
1636 Stream_Write_UINT16(s, tile->CbLen);
1637 Stream_Write_UINT16(s, tile->CrLen);
1638 Stream_Write(s, tile->YData, tile->YLen);
1639 Stream_Write(s, tile->CbData, tile->CbLen);
1640 Stream_Write(s, tile->CrData, tile->CrLen);
1644struct S_RFX_TILE_COMPOSE_WORK_PARAM
1647 RFX_CONTEXT* context;
1650static inline void CALLBACK
1651rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
1652 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
1654 RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1655 WINPR_ASSERT(param);
1656 WINPR_ASSERT(param->context);
1657 WINPR_ASSERT(param->context->priv);
1659 if (!rfx_encode_rgb(param->context, param->tile))
1660 WLog_Print(param->context->priv->log, WLOG_ERROR,
"rfx_encode_rgb failed");
1663static inline BOOL computeRegion(
const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1664 REGION16* WINPR_RESTRICT region,
size_t width,
size_t height)
1666 const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1667 WINPR_ASSERTING_INT_CAST(UINT16, height) };
1669 WINPR_ASSERT(rects);
1670 for (
size_t i = 0; i < numRects; i++)
1674 rect16.left = rect->x;
1675 rect16.top = rect->y;
1676 rect16.right = rect->x + rect->width;
1677 rect16.bottom = rect->y + rect->height;
1679 if (!region16_union_rect(region, region, &rect16))
1683 return region16_intersect_rect(region, region, &mainRect);
1686#define TILE_NO(v) ((v) / 64)
1688static inline BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context,
size_t nbTiles)
1690 WINPR_ASSERT(context);
1692 RFX_CONTEXT_PRIV* priv = context->priv;
1695 void* pmem =
nullptr;
1697 if (!context->priv->UseThreads)
1700 if (!(pmem = winpr_aligned_recalloc((
void*)priv->workObjects, nbTiles,
sizeof(PTP_WORK), 32)))
1703 priv->workObjects = (PTP_WORK*)pmem;
1705 if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1706 sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1709 priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1713static inline BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count)
1715 WINPR_ASSERT(message);
1717 if (message->numTiles + count <= message->allocatedTiles)
1720 const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1721 return rfx_allocate_tiles(message, alloc, TRUE);
1724RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1725 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1726 const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h,
size_t s)
1728 const UINT32 width = w;
1729 const UINT32 height = h;
1730 const UINT32 scanline = (UINT32)s;
1731 RFX_MESSAGE* message =
nullptr;
1732 PTP_WORK* workObject =
nullptr;
1733 RFX_TILE_COMPOSE_WORK_PARAM* workParam =
nullptr;
1734 BOOL success = FALSE;
1735 REGION16 rectsRegion = WINPR_C_ARRAY_INIT;
1736 REGION16 tilesRegion = WINPR_C_ARRAY_INIT;
1741 WINPR_ASSERT(rects);
1742 WINPR_ASSERT(numRects > 0);
1743 WINPR_ASSERT(w > 0);
1744 WINPR_ASSERT(h > 0);
1745 WINPR_ASSERT(s > 0);
1747 if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1,
sizeof(RFX_MESSAGE), 32)))
1750 region16_init(&tilesRegion);
1751 region16_init(&rectsRegion);
1753 if (context->state == RFX_STATE_SEND_HEADERS)
1754 rfx_update_context_properties(context);
1756 message->frameIdx = context->frameIdx++;
1758 if (!context->numQuant)
1760 WINPR_ASSERT(context->quants ==
nullptr);
1761 WINPR_ASSERT(NR_QUANT_VALUES == ARRAYSIZE(rfx_default_quantization_values));
1762 if (!(context->quants =
1763 (UINT32*)winpr_aligned_calloc(1,
sizeof(rfx_default_quantization_values), 32)))
1764 goto skip_encoding_loop;
1766 CopyMemory(context->quants, &rfx_default_quantization_values,
1767 sizeof(rfx_default_quantization_values));
1768 context->numQuant = 1;
1769 context->quantIdxY = 0;
1770 context->quantIdxCb = 0;
1771 context->quantIdxCr = 0;
1774 message->numQuant = context->numQuant;
1775 message->quantVals = context->quants;
1778 const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1779 if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1780 goto skip_encoding_loop;
1783 const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1784 WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1785 WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1786 const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1787 const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1788 const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1790 if (!rfx_ensure_tiles(message, maxNbTiles))
1791 goto skip_encoding_loop;
1793 if (!setupWorkers(context, maxNbTiles))
1794 goto skip_encoding_loop;
1797 if (context->priv->UseThreads)
1799 workObject = context->priv->workObjects;
1800 workParam = context->priv->tileWorkParams;
1804 UINT32 regionNbRects = 0;
1805 regionRect = region16_rects(&rectsRegion, ®ionNbRects);
1807 if (!(message->rects = winpr_aligned_calloc(regionNbRects,
sizeof(
RFX_RECT), 32)))
1808 goto skip_encoding_loop;
1810 message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1812 for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1814 RFX_RECT* rfxRect = &message->rects[i];
1815 UINT32 startTileX = regionRect->left / 64;
1816 UINT32 endTileX = (regionRect->right - 1) / 64;
1817 UINT32 startTileY = regionRect->top / 64;
1818 UINT32 endTileY = (regionRect->bottom - 1) / 64;
1819 rfxRect->x = regionRect->left;
1820 rfxRect->y = regionRect->top;
1821 rfxRect->width = (regionRect->right - regionRect->left);
1822 rfxRect->height = (regionRect->bottom - regionRect->top);
1824 for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1825 yIdx++, gridRelY += 64)
1827 UINT32 tileHeight = 64;
1829 if ((yIdx == endTileY) && (gridRelY + 64 > height))
1830 tileHeight = height - gridRelY;
1832 currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1833 currentTileRect.bottom =
1834 WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1836 for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1837 xIdx++, gridRelX += 64)
1844 UINT32 tileWidth = 64;
1846 if ((xIdx == endTileX) && (gridRelX + 64 > width))
1848 tileWidth = (width - gridRelX);
1851 currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1852 currentTileRect.right =
1853 WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1856 if (region16_intersects_rect(&tilesRegion, ¤tTileRect))
1861 goto skip_encoding_loop;
1863 tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1864 tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1865 tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1866 tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1867 tile->scanline = scanline;
1869 tile->width = tileWidth;
1870 tile->height = tileHeight;
1871 const UINT32 ax = gridRelX;
1872 const UINT32 ay = gridRelY;
1874 if (tile->data && tile->allocated)
1876 winpr_aligned_free(tile->data);
1877 tile->allocated = FALSE;
1881 cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1882 tile->data = cnv.pv;
1883 tile->quantIdxY = context->quantIdxY;
1884 tile->quantIdxCb = context->quantIdxCb;
1885 tile->quantIdxCr = context->quantIdxCr;
1886 tile->YLen = tile->CbLen = tile->CrLen = 0;
1888 if (!(tile->YCbCrData =
1889 (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1890 goto skip_encoding_loop;
1892 tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1893 tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1894 tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1896 if (!rfx_ensure_tiles(message, 1))
1897 goto skip_encoding_loop;
1898 message->tiles[message->numTiles++] = tile;
1900 if (context->priv->UseThreads)
1902 workParam->context = context;
1903 workParam->tile = tile;
1906 CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1907 (
void*)workParam,
nullptr)))
1909 goto skip_encoding_loop;
1912 SubmitThreadpoolWork(*workObject);
1918 if (!rfx_encode_rgb(context, tile))
1919 goto skip_encoding_loop;
1922 if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect))
1923 goto skip_encoding_loop;
1936 message->tilesDataSize = 0;
1937 workObject = context->priv->workObjects;
1939 for (UINT32 i = 0; i < message->numTiles; i++)
1941 if (context->priv->UseThreads)
1945 WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1946 CloseThreadpoolWork(*workObject);
1952 const RFX_TILE* tile = message->tiles[i];
1953 const size_t tlen = rfx_tile_length(tile);
1954 message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen);
1957 region16_uninit(&tilesRegion);
1958 region16_uninit(&rectsRegion);
1963 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1965 rfx_message_free(context, message);
1966 region16_uninit(&tilesRegion);
1967 region16_uninit(&rectsRegion);
1971static inline BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1972 const RFX_MESSAGE* WINPR_RESTRICT src)
1977 WINPR_ASSERT(dst->rects ==
nullptr);
1978 WINPR_ASSERT(dst->numRects == 0);
1980 if (src->numRects == 0)
1983 dst->rects = winpr_aligned_calloc(src->numRects,
sizeof(
RFX_RECT), 32);
1986 dst->numRects = src->numRects;
1987 for (
size_t x = 0; x < src->numRects; x++)
1989 dst->rects[x] = src->rects[x];
1994static inline BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1995 const RFX_MESSAGE* WINPR_RESTRICT src)
2000 WINPR_ASSERT(dst->quantVals ==
nullptr);
2001 WINPR_ASSERT(dst->numQuant == 0);
2003 if (src->numQuant == 0)
2007 dst->quantVals = src->quantVals;
2008 dst->numQuant = src->numQuant;
2013static inline RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
2014 RFX_MESSAGE* WINPR_RESTRICT message,
2015 size_t* WINPR_RESTRICT numMessages,
size_t maxDataSize)
2017 WINPR_ASSERT(context);
2018 WINPR_ASSERT(message);
2019 WINPR_ASSERT(numMessages);
2021 if (maxDataSize <= 1024)
2024 maxDataSize -= 1024;
2025 *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
2027 RFX_MESSAGE* messages =
2028 (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages),
sizeof(RFX_MESSAGE), 32);
2033 for (UINT16 i = 0; i < message->numTiles; i++)
2035 RFX_TILE* tile = message->tiles[i];
2036 RFX_MESSAGE* msg = &messages[j];
2041 const size_t tileDataSize = rfx_tile_length(tile);
2043 if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
2046 if (msg->numTiles == 0)
2048 msg->frameIdx = message->frameIdx + j;
2049 if (!rfx_clone_quants(msg, message))
2051 if (!rfx_clone_rects(msg, message))
2053 msg->freeArray = TRUE;
2054 if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
2058 msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize);
2060 WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
2061 msg->tiles[msg->numTiles++] = message->tiles[i];
2062 message->tiles[i] =
nullptr;
2065 *numMessages = j + 1ULL;
2066 context->frameIdx += j;
2067 message->numTiles = 0;
2071 for (
size_t i = 0; i < j; i++)
2072 rfx_allocate_tiles(&messages[i], 0, FALSE);
2074 winpr_aligned_free(messages);
2078const RFX_MESSAGE* rfx_message_list_get(
const RFX_MESSAGE_LIST* WINPR_RESTRICT messages,
size_t idx)
2080 WINPR_ASSERT(messages);
2081 if (idx >= messages->count)
2083 WINPR_ASSERT(messages->list);
2084 return &messages->list[idx];
2087void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
2091 for (
size_t x = 0; x < messages->count; x++)
2092 rfx_message_clear(messages->context, &messages->list[x]);
2096static inline RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
2097 RFX_MESSAGE* WINPR_RESTRICT messages,
2100 WINPR_ASSERT(context);
2101 RFX_MESSAGE_LIST* msg = calloc(1,
sizeof(RFX_MESSAGE_LIST));
2104 msg->context = context;
2106 msg->list = messages;
2110RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
2111 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2112 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2113 UINT32 scanline,
size_t* WINPR_RESTRICT numMessages,
2116 WINPR_ASSERT(context);
2117 WINPR_ASSERT(numMessages);
2119 RFX_MESSAGE* message =
2120 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2124 RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2125 rfx_message_free(context, message);
2129 return rfx_message_list_new(context, list, *numMessages);
2132static inline BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2134 const RFX_MESSAGE* WINPR_RESTRICT message)
2136 WINPR_ASSERT(context);
2137 WINPR_ASSERT(message);
2139 const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2141 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2144 Stream_Write_UINT16(s, WBT_EXTENSION);
2145 Stream_Write_UINT32(s, blockLen);
2146 Stream_Write_UINT8(s, 1);
2147 Stream_Write_UINT8(s, 0);
2148 Stream_Write_UINT16(s, CBT_TILESET);
2149 Stream_Write_UINT16(s, 0);
2150 Stream_Write_UINT16(s, context->properties);
2152 s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant));
2153 Stream_Write_UINT8(s, 0x40);
2154 Stream_Write_UINT16(s, message->numTiles);
2155 Stream_Write_UINT32(s, message->tilesDataSize);
2157 UINT32* quantVals = message->quantVals;
2158 for (
size_t i = 0; i < message->numQuant * 5ul; i++)
2160 WINPR_ASSERT(quantVals);
2161 Stream_Write_UINT8(s,
2162 WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2166 for (
size_t i = 0; i < message->numTiles; i++)
2168 RFX_TILE* tile = message->tiles[i];
2172 if (!rfx_write_tile(s, tile))
2176#ifdef WITH_DEBUG_RFX
2177 WLog_Print(context->priv->log, WLOG_DEBUG,
2178 "numQuant: %" PRIu16
" numTiles: %" PRIu16
" tilesDataSize: %" PRIu32
"",
2179 message->numQuant, message->numTiles, message->tilesDataSize);
2185rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2186 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT message)
2188 WINPR_ASSERT(context);
2189 WINPR_ASSERT(message);
2191 if (!Stream_EnsureRemainingCapacity(s, 14))
2194 Stream_Write_UINT16(s, WBT_FRAME_BEGIN);
2195 Stream_Write_UINT32(s, 14);
2196 Stream_Write_UINT8(s, 1);
2197 Stream_Write_UINT8(s, 0);
2198 Stream_Write_UINT32(s, message->frameIdx);
2199 Stream_Write_UINT16(s, 1);
2203static inline BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2205 const RFX_MESSAGE* WINPR_RESTRICT message)
2207 WINPR_ASSERT(context);
2208 WINPR_ASSERT(message);
2210 const size_t blockLen = 15 + (message->numRects * 8);
2211 if (blockLen > UINT32_MAX)
2214 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2217 Stream_Write_UINT16(s, WBT_REGION);
2218 Stream_Write_UINT32(s, (UINT32)blockLen);
2219 Stream_Write_UINT8(s, 1);
2220 Stream_Write_UINT8(s, 0);
2221 Stream_Write_UINT8(s, 1);
2222 Stream_Write_UINT16(s, message->numRects);
2224 for (UINT16 i = 0; i < message->numRects; i++)
2226 const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2230 Stream_Write_UINT16(s, rect->x);
2231 Stream_Write_UINT16(s, rect->y);
2232 Stream_Write_UINT16(s, rect->width);
2233 Stream_Write_UINT16(s, rect->height);
2236 Stream_Write_UINT16(s, CBT_REGION);
2237 Stream_Write_UINT16(s, 1);
2242rfx_write_message_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2244 WINPR_ATTR_UNUSED
const RFX_MESSAGE* WINPR_RESTRICT message)
2246 WINPR_ASSERT(context);
2247 WINPR_ASSERT(message);
2249 if (!Stream_EnsureRemainingCapacity(s, 8))
2252 Stream_Write_UINT16(s, WBT_FRAME_END);
2253 Stream_Write_UINT32(s, 8);
2254 Stream_Write_UINT8(s, 1);
2255 Stream_Write_UINT8(s, 0);
2259BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2260 const RFX_MESSAGE* WINPR_RESTRICT message)
2262 WINPR_ASSERT(context);
2263 WINPR_ASSERT(message);
2265 if (context->state == RFX_STATE_SEND_HEADERS)
2267 if (!rfx_compose_message_header(context, s))
2270 context->state = RFX_STATE_SEND_FRAME_DATA;
2273 if (!rfx_write_message_frame_begin(context, s, message) ||
2274 !rfx_write_message_region(context, s, message) ||
2275 !rfx_write_message_tileset(context, s, message) ||
2276 !rfx_write_message_frame_end(context, s, message))
2284BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2285 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2286 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2289 WINPR_ASSERT(context);
2290 RFX_MESSAGE* message =
2291 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2295 const BOOL ret = rfx_write_message(context, s, message);
2296 rfx_message_free(context, message);
2300BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2302 WINPR_ASSERT(context);
2303 context->mode = mode;
2307RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2309 WINPR_ASSERT(context);
2310 return context->mode;
2313UINT32 rfx_context_get_frame_idx(
const RFX_CONTEXT* WINPR_RESTRICT context)
2315 WINPR_ASSERT(context);
2316 return context->frameIdx;
2319UINT32 rfx_message_get_frame_idx(
const RFX_MESSAGE* WINPR_RESTRICT message)
2321 WINPR_ASSERT(message);
2322 return message->frameIdx;
2325static inline BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2328 const UINT32 blockLen = 12;
2332 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2335 Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC);
2336 Stream_Write_UINT32(s, blockLen);
2337 Stream_Write_UINT32(s, 0xCACCACCA);
2338 Stream_Write_UINT16(s, 0x0100);
2343rfx_write_progressive_wb_context(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2346 const UINT32 blockLen = 10;
2350 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2353 Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT);
2354 Stream_Write_UINT32(s, blockLen);
2355 Stream_Write_UINT8(s, 0);
2356 Stream_Write_UINT16(s, 64);
2357 Stream_Write_UINT8(s, 0);
2361static inline BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2363 const RFX_MESSAGE* WINPR_RESTRICT msg)
2366 UINT32 blockLen = 18;
2367 UINT32 tilesDataSize = 0;
2368 const size_t start = Stream_GetPosition(s);
2374 blockLen += msg->numRects * 8;
2375 blockLen += msg->numQuant * 5;
2376 tilesDataSize = msg->numTiles * 22UL;
2377 for (UINT16 i = 0; i < msg->numTiles; i++)
2379 const RFX_TILE* tile = msg->tiles[i];
2381 tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2383 blockLen += tilesDataSize;
2385 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2388 Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION);
2389 Stream_Write_UINT32(s, blockLen);
2390 Stream_Write_UINT8(s, 64);
2391 Stream_Write_UINT16(s, msg->numRects);
2392 WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2393 Stream_Write_UINT8(s, (UINT8)msg->numQuant);
2394 Stream_Write_UINT8(s, 0);
2395 Stream_Write_UINT8(s, 0);
2396 Stream_Write_UINT16(s, msg->numTiles);
2397 Stream_Write_UINT32(s, tilesDataSize);
2399 for (UINT16 i = 0; i < msg->numRects; i++)
2402 const RFX_RECT* r = &msg->rects[i];
2403 Stream_Write_UINT16(s, r->x);
2404 Stream_Write_UINT16(s, r->y);
2405 Stream_Write_UINT16(s, r->width);
2406 Stream_Write_UINT16(s, r->height);
2417 for (UINT16 i = 0; i < msg->numQuant; i++)
2419 const UINT32* qv = &msg->quantVals[10ULL * i];
2421 Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4)));
2422 Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4)));
2423 Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4)));
2424 Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4)));
2425 Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4)));
2428 for (UINT16 i = 0; i < msg->numTiles; i++)
2430 const RFX_TILE* tile = msg->tiles[i];
2431 if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2435 const size_t end = Stream_GetPosition(s);
2436 const size_t used = end - start;
2437 return (used == blockLen);
2441rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2442 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT msg)
2444 const UINT32 blockLen = 12;
2449 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2452 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN);
2453 Stream_Write_UINT32(s, blockLen);
2454 Stream_Write_UINT32(s, msg->frameIdx);
2455 Stream_Write_UINT16(s, 1);
2461rfx_write_progressive_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2464 const UINT32 blockLen = 6;
2468 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2471 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END);
2472 Stream_Write_UINT32(s, blockLen);
2478rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2481 UINT32 blockLen = 0;
2486 blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2487 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2490 Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE);
2491 Stream_Write_UINT32(s, blockLen);
2492 Stream_Write_UINT8(s, tile->quantIdxY);
2493 Stream_Write_UINT8(s, tile->quantIdxCb);
2494 Stream_Write_UINT8(s, tile->quantIdxCr);
2495 Stream_Write_UINT16(s, tile->xIdx);
2496 Stream_Write_UINT16(s, tile->yIdx);
2497 Stream_Write_UINT8(s, 0);
2498 Stream_Write_UINT16(s, tile->YLen);
2499 Stream_Write_UINT16(s, tile->CbLen);
2500 Stream_Write_UINT16(s, tile->CrLen);
2501 Stream_Write_UINT16(s, 0);
2502 Stream_Write(s, tile->YData, tile->YLen);
2503 Stream_Write(s, tile->CbData, tile->CbLen);
2504 Stream_Write(s, tile->CrData, tile->CrLen);
2509const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2513 case PROGRESSIVE_WBT_SYNC:
2514 return "PROGRESSIVE_WBT_SYNC";
2516 case PROGRESSIVE_WBT_FRAME_BEGIN:
2517 return "PROGRESSIVE_WBT_FRAME_BEGIN";
2519 case PROGRESSIVE_WBT_FRAME_END:
2520 return "PROGRESSIVE_WBT_FRAME_END";
2522 case PROGRESSIVE_WBT_CONTEXT:
2523 return "PROGRESSIVE_WBT_CONTEXT";
2525 case PROGRESSIVE_WBT_REGION:
2526 return "PROGRESSIVE_WBT_REGION";
2528 case PROGRESSIVE_WBT_TILE_SIMPLE:
2529 return "PROGRESSIVE_WBT_TILE_SIMPLE";
2531 case PROGRESSIVE_WBT_TILE_FIRST:
2532 return "PROGRESSIVE_WBT_TILE_FIRST";
2534 case PROGRESSIVE_WBT_TILE_UPGRADE:
2535 return "PROGRESSIVE_WBT_TILE_UPGRADE";
2538 return "PROGRESSIVE_WBT_UNKNOWN";
2542BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
2544 const RFX_MESSAGE* WINPR_RESTRICT msg)
2550 if (rfx->mode != RLGR1)
2552 WLog_ERR(TAG,
"error, RLGR1 mode is required!");
2556 if (!rfx_write_progressive_wb_sync(rfx, s))
2559 if (!rfx_write_progressive_wb_context(rfx, s))
2562 if (!rfx_write_progressive_frame_begin(rfx, s, msg))
2565 if (!rfx_write_progressive_region(rfx, s, msg))
2568 if (!rfx_write_progressive_frame_end(rfx, s))
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew
OBJECT_INIT_FN fnObjectInit