22#include <freerdp/config.h>
24#include <winpr/assert.h>
25#include <winpr/cast.h>
27#include <winpr/print.h>
28#include <winpr/bitstream.h>
30#include <freerdp/primitives.h>
31#include <freerdp/codec/color.h>
32#include <freerdp/codec/progressive.h>
33#include <freerdp/codec/region.h>
34#include <freerdp/log.h>
36#include "rfx_differential.h"
37#include "rfx_quantization.h"
40#include "rfx_constants.h"
42#include "progressive.h"
44#define TAG FREERDP_TAG("codec.progressive")
57} RFX_PROGRESSIVE_UPGRADE_STATE;
60progressive_component_codec_quant_read(
wStream* WINPR_RESTRICT s,
64 Stream_Read_UINT8(s, b);
65 quantVal->LL3 = b & 0x0F;
66 quantVal->HL3 = b >> 4;
67 Stream_Read_UINT8(s, b);
68 quantVal->LH3 = b & 0x0F;
69 quantVal->HH3 = b >> 4;
70 Stream_Read_UINT8(s, b);
71 quantVal->HL2 = b & 0x0F;
72 quantVal->LH2 = b >> 4;
73 Stream_Read_UINT8(s, b);
74 quantVal->HH2 = b & 0x0F;
75 quantVal->HL1 = b >> 4;
76 Stream_Read_UINT8(s, b);
77 quantVal->LH1 = b & 0x0F;
78 quantVal->HH1 = b >> 4;
85 dst->HL1 = q1->HL1 + q2->HL1;
86 dst->LH1 = q1->LH1 + q2->LH1;
87 dst->HH1 = q1->HH1 + q2->HH1;
88 dst->HL2 = q1->HL2 + q2->HL2;
89 dst->LH2 = q1->LH2 + q2->LH2;
90 dst->HH2 = q1->HH2 + q2->HH2;
91 dst->HL3 = q1->HL3 + q2->HL3;
92 dst->LH3 = q1->LH3 + q2->LH3;
93 dst->HH3 = q1->HH3 + q2->HH3;
94 dst->LL3 = q1->LL3 + q2->LL3;
147 if (q1->HL1 < q2->HL1)
149 dst->HL1 = q1->HL1 - q2->HL1;
151 if (q1->LH1 < q2->LH1)
153 dst->LH1 = q1->LH1 - q2->LH1;
155 if (q1->HH1 < q2->HH1)
157 dst->HH1 = q1->HH1 - q2->HH1;
159 if (q1->HL2 < q2->HL2)
161 dst->HL2 = q1->HL2 - q2->HL2;
163 if (q1->LH2 < q2->LH2)
165 dst->LH2 = q1->LH2 - q2->LH2;
167 if (q1->HH2 < q2->HH2)
169 dst->HH2 = q1->HH2 - q2->HH2;
171 if (q1->HL3 < q2->HL3)
173 dst->HL3 = q1->HL3 - q2->HL3;
175 if (q1->LH3 < q2->LH3)
177 dst->LH3 = q1->LH3 - q2->LH3;
179 if (q1->HH3 < q2->HH3)
181 dst->HH3 = q1->HH3 - q2->HH3;
183 if (q1->LL3 < q2->LL3)
185 dst->LL3 = q1->LL3 - q2->LL3;
266 if (q1->HL1 != q2->HL1)
269 if (q1->LH1 != q2->LH1)
272 if (q1->HH1 != q2->HH1)
275 if (q1->HL2 != q2->HL2)
278 if (q1->LH2 != q2->LH2)
281 if (q1->HH2 != q2->HH2)
284 if (q1->HL3 != q2->HL3)
287 if (q1->LH3 != q2->LH3)
290 if (q1->HH3 != q2->HH3)
293 if (q1->LL3 != q2->LL3)
299static inline BOOL progressive_set_surface_data(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
304 key = ((ULONG_PTR)surfaceId) + 1;
307 return HashTable_Insert(progressive->SurfaceContexts, (
void*)key, pData);
309 HashTable_Remove(progressive->SurfaceContexts, (
void*)key);
314progressive_get_surface_data(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, UINT16 surfaceId)
316 void* key = (
void*)(((ULONG_PTR)surfaceId) + 1);
321 return HashTable_GetItemValue(progressive->SurfaceContexts, key);
328 winpr_aligned_free(tile->sign);
329 winpr_aligned_free(tile->current);
330 winpr_aligned_free(tile->data);
331 winpr_aligned_free(tile);
335static void progressive_surface_context_free(
void* ptr)
344 for (
size_t index = 0; index < surface->tilesSize; index++)
347 progressive_tile_free(tile);
351 winpr_aligned_free((
void*)surface->tiles);
352 winpr_aligned_free(surface->updatedTileIndices);
353 winpr_aligned_free(surface);
364 tile->stride = 4 * tile->width;
367 const size_t dataLen = 1ull * tile->stride * tile->height;
368 tile->data = (BYTE*)winpr_aligned_calloc(tile->stride, tile->height, 16);
371 memset(tile->data, 0xFF, dataLen);
375 const size_t signLen = (8192ULL + 32ULL);
376 tile->sign = (BYTE*)winpr_aligned_calloc(3, signLen, 16);
383 const size_t currentLen = (8192ULL + 32ULL);
384 tile->current = (BYTE*)winpr_aligned_calloc(3, currentLen, 16);
392 progressive_tile_free(tile);
401 WINPR_ASSERT(surface);
402 WINPR_ASSERT(surface->gridSize > 0);
408 const size_t limit = 8ull * surface->gridWidth * surface->gridHeight;
411 WLog_ERR(TAG,
"tile cache limit %" PRIuz
" reached, aborting", limit);
415 UINT32 gridSize = surface->gridSize;
419 while (gridSize < min)
421 if (gridSize > UINT32_MAX - 1024)
431 surface->tilesSize = gridSize;
434 for (
size_t x = oldIndex; x < surface->tilesSize; x++)
436 surface->tiles[x] = progressive_tile_new();
437 if (!surface->tiles[x])
441 tmp = winpr_aligned_recalloc(surface->updatedTileIndices, gridSize,
sizeof(UINT32), 32);
445 surface->gridSize = gridSize;
446 surface->updatedTileIndices = tmp;
460 surface->id = surfaceId;
461 surface->width = width;
462 surface->height = height;
463 surface->gridWidth = (width + (64 - width % 64)) / 64;
464 surface->gridHeight = (height + (64 - height % 64)) / 64;
465 surface->gridSize = surface->gridWidth * surface->gridHeight;
467 if (!progressive_allocate_tile_cache(surface, surface->gridSize))
469 progressive_surface_context_free(surface);
478 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
484 if (!surface || !tile)
487 if ((tile->xIdx >= surface->gridWidth) || (tile->yIdx >= surface->gridHeight))
489 WLog_ERR(TAG,
"Invalid tile index %" PRIu16
"x%" PRIu16
" for grid %" PRIu32
"x%" PRIu32,
490 tile->xIdx, tile->yIdx, surface->gridWidth, surface->gridHeight);
494 zIdx = (tile->yIdx * surface->gridWidth) + tile->xIdx;
496 if (zIdx >= surface->tilesSize)
498 WLog_ERR(TAG,
"Invalid zIndex %" PRIuz, zIdx);
502 t = surface->tiles[zIdx];
504 t->blockType = tile->blockType;
505 t->blockLen = tile->blockLen;
506 t->quantIdxY = tile->quantIdxY;
507 t->quantIdxCb = tile->quantIdxCb;
508 t->quantIdxCr = tile->quantIdxCr;
509 t->xIdx = tile->xIdx;
510 t->yIdx = tile->yIdx;
511 t->flags = tile->flags;
512 t->quality = tile->quality;
513 t->x = tile->xIdx * t->width;
514 t->y = tile->yIdx * t->height;
518 t->ySrlLen = tile->ySrlLen;
519 t->yRawLen = tile->yRawLen;
520 t->cbSrlLen = tile->cbSrlLen;
521 t->cbRawLen = tile->cbRawLen;
522 t->crSrlLen = tile->crSrlLen;
523 t->crRawLen = tile->crRawLen;
524 t->ySrlData = tile->ySrlData;
525 t->yRawData = tile->yRawData;
526 t->cbSrlData = tile->cbSrlData;
527 t->cbRawData = tile->cbRawData;
528 t->crSrlData = tile->crSrlData;
529 t->crRawData = tile->crRawData;
533 t->yLen = tile->yLen;
534 t->cbLen = tile->cbLen;
535 t->crLen = tile->crLen;
536 t->tailLen = tile->tailLen;
537 t->yData = tile->yData;
538 t->cbData = tile->cbData;
539 t->crData = tile->crData;
540 t->tailData = tile->tailData;
543 if (region->usedTiles >= region->numTiles)
545 WLog_ERR(TAG,
"Invalid tile count, only expected %" PRIu16
", got %" PRIu16,
546 region->numTiles, region->usedTiles);
550 region->tiles[region->usedTiles++] = t;
553 if (surface->numUpdatedTiles >= surface->gridSize)
555 if (!progressive_allocate_tile_cache(surface, surface->numUpdatedTiles + 1))
559 surface->updatedTileIndices[surface->numUpdatedTiles++] = (UINT32)zIdx;
566INT32 progressive_create_surface_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
567 UINT16 surfaceId, UINT32 width, UINT32 height)
573 surface = progressive_surface_context_new(surfaceId, width, height);
578 if (!progressive_set_surface_data(progressive, surfaceId, (
void*)surface))
580 progressive_surface_context_free(surface);
588int progressive_delete_surface_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
591 progressive_set_surface_data(progressive, surfaceId,
nullptr);
614static int16_t clampi16(
int val)
623static inline void progressive_rfx_idwt_x(
const INT16* WINPR_RESTRICT pLowBand,
size_t nLowStep,
624 const INT16* WINPR_RESTRICT pHighBand,
size_t nHighStep,
625 INT16* WINPR_RESTRICT pDstBand,
size_t nDstStep,
626 size_t nLowCount,
size_t nHighCount,
size_t nDstCount)
631 for (
size_t i = 0; i < nDstCount; i++)
633 const INT16* pL = pLowBand;
634 const INT16* pH = pHighBand;
635 INT16* pX = pDstBand;
638 INT16 X0 = clampi16((int32_t)L0 - H0);
639 INT16 X2 = clampi16((int32_t)L0 - H0);
641 for (
size_t j = 0; j < (nHighCount - 1); j++)
647 X2 = clampi16((int32_t)L0 - ((H0 + H1) / 2));
648 X1 = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
656 if (nLowCount <= (nHighCount + 1))
658 if (nLowCount <= nHighCount)
661 pX[1] = clampi16((int32_t)X2 + (2 * H0));
667 X0 = clampi16((int32_t)L0 - H0);
669 pX[1] = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
677 X0 = clampi16((int32_t)L0 - (H0 / 2));
679 pX[1] = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
683 pX[3] = clampi16((int32_t)(X0 + L0) / 2);
686 pLowBand += nLowStep;
687 pHighBand += nHighStep;
688 pDstBand += nDstStep;
692static inline void progressive_rfx_idwt_y(
const INT16* WINPR_RESTRICT pLowBand,
size_t nLowStep,
693 const INT16* WINPR_RESTRICT pHighBand,
size_t nHighStep,
694 INT16* WINPR_RESTRICT pDstBand,
size_t nDstStep,
695 size_t nLowCount,
size_t nHighCount,
size_t nDstCount)
697 for (
size_t i = 0; i < nDstCount; i++)
701 const INT16* pL = pLowBand;
702 const INT16* pH = pHighBand;
703 INT16* pX = pDstBand;
708 int16_t X0 = clampi16((int32_t)L0 - H0);
709 int16_t X2 = clampi16((int32_t)L0 - H0);
711 for (
size_t j = 0; j < (nHighCount - 1); j++)
717 X2 = clampi16((int32_t)L0 - ((H0 + H1) / 2));
718 X1 = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
727 if (nLowCount <= (nHighCount + 1))
729 if (nLowCount <= nHighCount)
733 *pX = clampi16((int32_t)X2 + (2 * H0));
738 X0 = clampi16((int32_t)L0 - H0);
741 *pX = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
750 X0 = clampi16((int32_t)L0 - (H0 / 2));
753 *pX = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
758 *pX = clampi16((int32_t)(X0 + L0) / 2);
767static inline size_t progressive_rfx_get_band_l_count(
size_t level)
769 return (64 >> level) + 1;
772static inline size_t progressive_rfx_get_band_h_count(
size_t level)
775 return (64 >> 1) - 1;
777 return (64 + (1u << (level - 1))) >> level;
780static inline void progressive_rfx_dwt_2d_decode_block(INT16* WINPR_RESTRICT buffer,
781 INT16* WINPR_RESTRICT temp,
size_t level)
783 size_t nDstStepX = 0;
784 size_t nDstStepY = 0;
785 const INT16* WINPR_RESTRICT HL =
nullptr;
786 const INT16* WINPR_RESTRICT LH =
nullptr;
787 const INT16* WINPR_RESTRICT HH =
nullptr;
788 INT16* WINPR_RESTRICT LL =
nullptr;
789 INT16* WINPR_RESTRICT L =
nullptr;
790 INT16* WINPR_RESTRICT H =
nullptr;
791 INT16* WINPR_RESTRICT LLx =
nullptr;
793 const size_t nBandL = progressive_rfx_get_band_l_count(level);
794 const size_t nBandH = progressive_rfx_get_band_h_count(level);
797 HL = &buffer[offset];
798 offset += (nBandH * nBandL);
799 LH = &buffer[offset];
800 offset += (nBandL * nBandH);
801 HH = &buffer[offset];
802 offset += (nBandH * nBandH);
803 LL = &buffer[offset];
804 nDstStepX = (nBandL + nBandH);
805 nDstStepY = (nBandL + nBandH);
808 offset += (nBandL * nDstStepX);
813 progressive_rfx_idwt_x(LL, nBandL, HL, nBandH, L, nDstStepX, nBandL, nBandH, nBandL);
816 progressive_rfx_idwt_x(LH, nBandL, HH, nBandH, H, nDstStepX, nBandL, nBandH, nBandH);
819 progressive_rfx_idwt_y(L, nDstStepX, H, nDstStepX, LLx, nDstStepY, nBandL, nBandH,
823void rfx_dwt_2d_extrapolate_decode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer)
825 WINPR_ASSERT(buffer);
826 WINPR_ASSERT(dwt_buffer);
827 progressive_rfx_dwt_2d_decode_block(&buffer[3807], dwt_buffer, 3);
828 progressive_rfx_dwt_2d_decode_block(&buffer[3007], dwt_buffer, 2);
829 progressive_rfx_dwt_2d_decode_block(&buffer[0], dwt_buffer, 1);
832static inline int progressive_rfx_dwt_2d_decode(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
833 INT16* WINPR_RESTRICT buffer,
834 INT16* WINPR_RESTRICT current, BOOL coeffDiff,
835 BOOL extrapolate, BOOL reverse)
839 if (!progressive || !buffer || !current)
842 const uint32_t belements = 4096;
843 const uint32_t bsize = belements *
sizeof(INT16);
845 memcpy(buffer, current, bsize);
847 memcpy(current, buffer, bsize);
850 const pstatus_t rc = prims->
add_16s_inplace(buffer, current, belements);
851 if (rc != PRIMITIVES_SUCCESS)
855 DECLSPEC_ALIGN(16) INT16 temp[64 * 64];
859 progressive->rfx_context->dwt_2d_decode(buffer, temp);
863 WINPR_ASSERT(progressive->rfx_context->dwt_2d_extrapolate_decode);
864 progressive->rfx_context->dwt_2d_extrapolate_decode(buffer, temp);
869static inline BOOL progressive_rfx_decode_block(
const primitives_t* prims,
870 INT16* WINPR_RESTRICT buffer, UINT32 length,
880progressive_rfx_decode_component(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
882 const BYTE* WINPR_RESTRICT data, UINT32 length,
883 INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT current,
884 INT16* WINPR_RESTRICT sign, BOOL coeffDiff,
885 WINPR_ATTR_UNUSED BOOL subbandDiff, BOOL extrapolate)
890 status = progressive->rfx_context->rlgr_decode(RLGR1, data, length, buffer, 4096);
895 CopyMemory(sign, buffer, 4096ULL * 2ULL);
898 if (!rfx_differential_decode(buffer + 4032, 64))
900 if (!progressive_rfx_decode_block(prims, &buffer[0], 1024, shift->HL1))
902 if (!progressive_rfx_decode_block(prims, &buffer[1024], 1024, shift->LH1))
904 if (!progressive_rfx_decode_block(prims, &buffer[2048], 1024, shift->HH1))
906 if (!progressive_rfx_decode_block(prims, &buffer[3072], 256, shift->HL2))
908 if (!progressive_rfx_decode_block(prims, &buffer[3328], 256, shift->LH2))
910 if (!progressive_rfx_decode_block(prims, &buffer[3584], 256, shift->HH2))
912 if (!progressive_rfx_decode_block(prims, &buffer[3840], 64, shift->HL3))
914 if (!progressive_rfx_decode_block(prims, &buffer[3904], 64, shift->LH3))
916 if (!progressive_rfx_decode_block(prims, &buffer[3968], 64, shift->HH3))
918 if (!progressive_rfx_decode_block(prims, &buffer[4032], 64, shift->LL3))
923 if (!progressive_rfx_decode_block(prims, &buffer[0], 1023, shift->HL1))
925 if (!progressive_rfx_decode_block(prims, &buffer[1023], 1023, shift->LH1))
927 if (!progressive_rfx_decode_block(prims, &buffer[2046], 961, shift->HH1))
929 if (!progressive_rfx_decode_block(prims, &buffer[3007], 272, shift->HL2))
931 if (!progressive_rfx_decode_block(prims, &buffer[3279], 272, shift->LH2))
933 if (!progressive_rfx_decode_block(prims, &buffer[3551], 256, shift->HH2))
935 if (!progressive_rfx_decode_block(prims, &buffer[3807], 72, shift->HL3))
937 if (!progressive_rfx_decode_block(prims, &buffer[3879], 72, shift->LH3))
939 if (!progressive_rfx_decode_block(prims, &buffer[3951], 64, shift->HH3))
941 if (!rfx_differential_decode(&buffer[4015], 81))
943 if (!progressive_rfx_decode_block(prims, &buffer[4015], 81, shift->LL3))
946 return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
951progressive_decompress_tile_first(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
953 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
959 BOOL extrapolate = 0;
960 DECLSPEC_ALIGN(16) BYTE pBuffer[(8192 + 32) * 3];
978 diff = tile->flags & RFX_TILE_DIFFERENCE;
979 sub = context->flags & RFX_SUBBAND_DIFFING;
980 extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
982#if defined(WITH_DEBUG_CODECS)
983 WLog_Print(progressive->log, WLOG_DEBUG,
984 "ProgressiveTile%s: quantIdx Y: %" PRIu8
" Cb: %" PRIu8
" Cr: %" PRIu8
985 " xIdx: %" PRIu16
" yIdx: %" PRIu16
" flags: 0x%02" PRIX8
" quality: %" PRIu8
986 " yLen: %" PRIu16
" cbLen: %" PRIu16
" crLen: %" PRIu16
" tailLen: %" PRIu16
"",
987 (tile->blockType == PROGRESSIVE_WBT_TILE_FIRST) ?
"First" :
"Simple",
988 tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx, tile->yIdx,
989 tile->flags, tile->quality, tile->yLen, tile->cbLen, tile->crLen, tile->tailLen);
992 if (tile->quantIdxY >= region->numQuant)
994 WLog_ERR(TAG,
"quantIdxY %" PRIu8
" > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
998 quantY = &(region->quantVals[tile->quantIdxY]);
1000 if (tile->quantIdxCb >= region->numQuant)
1002 WLog_ERR(TAG,
"quantIdxCb %" PRIu8
" > numQuant %" PRIu8, tile->quantIdxCb,
1007 quantCb = &(region->quantVals[tile->quantIdxCb]);
1009 if (tile->quantIdxCr >= region->numQuant)
1011 WLog_ERR(TAG,
"quantIdxCr %" PRIu8
" > numQuant %" PRIu8, tile->quantIdxCr,
1016 quantCr = &(region->quantVals[tile->quantIdxCr]);
1018 if (tile->quality == 0xFF)
1020 quantProgVal = &(progressive->quantProgValFull);
1024 if (tile->quality >= region->numProgQuant)
1026 WLog_ERR(TAG,
"quality %" PRIu8
" > numProgQuant %" PRIu8, tile->quality,
1027 region->numProgQuant);
1031 quantProgVal = &(region->quantProgVals[tile->quality]);
1034 quantProgY = &(quantProgVal->yQuantValues);
1035 quantProgCb = &(quantProgVal->cbQuantValues);
1036 quantProgCr = &(quantProgVal->crQuantValues);
1038 tile->yQuant = *quantY;
1039 tile->cbQuant = *quantCb;
1040 tile->crQuant = *quantCr;
1041 tile->yProgQuant = *quantProgY;
1042 tile->cbProgQuant = *quantProgCb;
1043 tile->crProgQuant = *quantProgCr;
1045 progressive_rfx_quant_add(quantY, quantProgY, &(tile->yBitPos));
1046 progressive_rfx_quant_add(quantCb, quantProgCb, &(tile->cbBitPos));
1047 progressive_rfx_quant_add(quantCr, quantProgCr, &(tile->crBitPos));
1048 progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
1049 if (!progressive_rfx_quant_lsub(&shiftY, 1))
1051 progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
1052 if (!progressive_rfx_quant_lsub(&shiftCb, 1))
1054 progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
1055 if (!progressive_rfx_quant_lsub(&shiftCr, 1))
1059 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 0) + 16]));
1061 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 1) + 16]));
1063 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 2) + 16]));
1066 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 0) + 16]));
1068 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 1) + 16]));
1070 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 2) + 16]));
1073 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 0) + 16]));
1075 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 1) + 16]));
1077 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 2) + 16]));
1079 rc = progressive_rfx_decode_component(progressive, &shiftY, tile->yData, tile->yLen, pSrcDst[0],
1080 pCurrent[0], pSign[0], diff, sub, extrapolate);
1083 rc = progressive_rfx_decode_component(progressive, &shiftCb, tile->cbData, tile->cbLen,
1084 pSrcDst[1], pCurrent[1], pSign[1], diff, sub,
1088 rc = progressive_rfx_decode_component(progressive, &shiftCr, tile->crData, tile->crLen,
1089 pSrcDst[2], pCurrent[2], pSign[2], diff, sub,
1095 const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**,
const INT16**);
1096 rc = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride,
1097 progressive->format, &roi_64x64);
1103static inline INT16 progressive_rfx_srl_read(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
1106 WINPR_ASSERT(state);
1117 const UINT32 k = state->kp / 8;
1122 const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1123 BitStream_Shift(bs, 1);
1128 state->nz = (1 << k);
1145 bs->mask = ((1u << k) - 1);
1147 WINPR_ASSERTING_INT_CAST(int16_t, ((bs->accumulator >> (32u - k)) & bs->mask));
1148 BitStream_Shift(bs, k);
1162 const UINT32 sign = (bs->accumulator & 0x80000000) ? 1 : 0;
1163 BitStream_Shift(bs, 1);
1171 return sign ? -1 : 1;
1174 const UINT32 max = (1u << numBits) - 1;
1181 if (bs->position >= bs->length)
1187 const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1188 BitStream_Shift(bs, 1);
1196 if (mag > INT16_MAX)
1198 return (INT16)(sign ? -1 * (int)mag : (INT16)mag);
1202progressive_rfx_upgrade_state_finish(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state)
1213 pad = (raw->position % 8) ? (8 - (raw->position % 8)) : 0;
1216 BitStream_Shift(raw, pad);
1218 pad = (srl->position % 8) ? (8 - (srl->position % 8)) : 0;
1221 BitStream_Shift(srl, pad);
1223 if (BitStream_GetRemainingLength(srl) == 8)
1224 BitStream_Shift(srl, 8);
1229static inline int16_t rawShift(
wBitStream* raw, UINT32 numBits)
1232 WINPR_ASSERT(numBits > 0);
1234 raw->mask = ((1u << numBits) - 1);
1235 const int16_t input = (int16_t)((raw->accumulator >> (32 - numBits)) & raw->mask);
1236 BitStream_Shift(raw, numBits);
1241static inline BOOL progressive_rfx_upgrade_coefficient(INT16* WINPR_RESTRICT coefficient,
1242 int32_t input, UINT32 shift)
1246 const int64_t val = *coefficient + input * (1LL << shift);
1247 if ((val > INT16_MAX) || (val < INT16_MIN))
1249 WLog_ERR(TAG,
"upgrade: %d <= %" PRId64
" <= %d", INT16_MIN, val, INT16_MAX);
1252 *coefficient = WINPR_ASSERTING_INT_CAST(INT16, val);
1256static inline int progressive_rfx_upgrade_block(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
1257 INT16* WINPR_RESTRICT buffer,
1258 INT16* WINPR_RESTRICT sign, UINT32 length,
1259 UINT32 shift, WINPR_ATTR_UNUSED UINT32 bitPos,
1270 for (UINT32 index = 0; index < length; index++)
1272 input = rawShift(raw, numBits);
1274 if (!progressive_rfx_upgrade_coefficient(&buffer[index], input, shift))
1281 for (UINT32 index = 0; index < length; index++)
1283 if (sign[index] > 0)
1286 input = rawShift(raw, numBits);
1288 else if (sign[index] < 0)
1291 input = rawShift(raw, numBits);
1297 input = progressive_rfx_srl_read(state, numBits);
1298 sign[index] = WINPR_ASSERTING_INT_CAST(int16_t, input);
1301 if (!progressive_rfx_upgrade_coefficient(&buffer[index], input, shift))
1308static inline int progressive_rfx_upgrade_component(
1309 PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1313 INT16* WINPR_RESTRICT current, INT16* WINPR_RESTRICT sign,
const BYTE* WINPR_RESTRICT srlData,
1314 UINT32 srlLen,
const BYTE* WINPR_RESTRICT rawData, UINT32 rawLen, BOOL coeffDiff,
1315 WINPR_ATTR_UNUSED BOOL subbandDiff, BOOL extrapolate)
1320 RFX_PROGRESSIVE_UPGRADE_STATE state = WINPR_C_ARRAY_INIT;
1326 BitStream_Attach(state.srl, srlData, srlLen);
1327 BitStream_Fetch(state.srl);
1328 BitStream_Attach(state.raw, rawData, rawLen);
1329 BitStream_Fetch(state.raw);
1332 rc = progressive_rfx_upgrade_block(&state, ¤t[0], &sign[0], 1023, shift->HL1, bitPos->HL1,
1336 rc = progressive_rfx_upgrade_block(&state, ¤t[1023], &sign[1023], 1023, shift->LH1,
1337 bitPos->LH1, numBits->LH1);
1340 rc = progressive_rfx_upgrade_block(&state, ¤t[2046], &sign[2046], 961, shift->HH1,
1341 bitPos->HH1, numBits->HH1);
1344 rc = progressive_rfx_upgrade_block(&state, ¤t[3007], &sign[3007], 272, shift->HL2,
1345 bitPos->HL2, numBits->HL2);
1348 rc = progressive_rfx_upgrade_block(&state, ¤t[3279], &sign[3279], 272, shift->LH2,
1349 bitPos->LH2, numBits->LH2);
1352 rc = progressive_rfx_upgrade_block(&state, ¤t[3551], &sign[3551], 256, shift->HH2,
1353 bitPos->HH2, numBits->HH2);
1356 rc = progressive_rfx_upgrade_block(&state, ¤t[3807], &sign[3807], 72, shift->HL3,
1357 bitPos->HL3, numBits->HL3);
1360 rc = progressive_rfx_upgrade_block(&state, ¤t[3879], &sign[3879], 72, shift->LH3,
1361 bitPos->LH3, numBits->LH3);
1364 rc = progressive_rfx_upgrade_block(&state, ¤t[3951], &sign[3951], 64, shift->HH3,
1365 bitPos->HH3, numBits->HH3);
1369 state.nonLL = FALSE;
1370 rc = progressive_rfx_upgrade_block(&state, ¤t[4015], &sign[4015], 81, shift->LL3,
1371 bitPos->LL3, numBits->LL3);
1374 rc = progressive_rfx_upgrade_state_finish(&state);
1377 return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
1382progressive_decompress_tile_upgrade(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1384 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1390 BOOL extrapolate = 0;
1391 DECLSPEC_ALIGN(16) BYTE pBuffer[(8192 + 32) * 3];
1392 INT16* pSign[3] = WINPR_C_ARRAY_INIT;
1393 INT16* pSrcDst[3] = WINPR_C_ARRAY_INIT;
1394 INT16* pCurrent[3] = WINPR_C_ARRAY_INIT;
1414 coeffDiff = tile->flags & RFX_TILE_DIFFERENCE;
1415 sub = context->flags & RFX_SUBBAND_DIFFING;
1416 extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
1420#if defined(WITH_DEBUG_CODECS)
1421 WLog_Print(progressive->log, WLOG_DEBUG,
1422 "ProgressiveTileUpgrade: pass: %" PRIu16
" quantIdx Y: %" PRIu8
" Cb: %" PRIu8
1423 " Cr: %" PRIu8
" xIdx: %" PRIu16
" yIdx: %" PRIu16
" quality: %" PRIu8
1424 " ySrlLen: %" PRIu16
" yRawLen: %" PRIu16
" cbSrlLen: %" PRIu16
" cbRawLen: %" PRIu16
1425 " crSrlLen: %" PRIu16
" crRawLen: %" PRIu16
"",
1426 tile->pass, tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx,
1427 tile->yIdx, tile->quality, tile->ySrlLen, tile->yRawLen, tile->cbSrlLen,
1428 tile->cbRawLen, tile->crSrlLen, tile->crRawLen);
1431 if (tile->quantIdxY >= region->numQuant)
1433 WLog_ERR(TAG,
"quantIdxY %" PRIu8
" > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
1437 quantY = &(region->quantVals[tile->quantIdxY]);
1439 if (tile->quantIdxCb >= region->numQuant)
1441 WLog_ERR(TAG,
"quantIdxCb %" PRIu8
" > numQuant %" PRIu8, tile->quantIdxCb,
1446 quantCb = &(region->quantVals[tile->quantIdxCb]);
1448 if (tile->quantIdxCr >= region->numQuant)
1450 WLog_ERR(TAG,
"quantIdxCr %" PRIu8
" > numQuant %" PRIu8, tile->quantIdxCr,
1455 quantCr = &(region->quantVals[tile->quantIdxCr]);
1457 if (tile->quality == 0xFF)
1459 quantProg = &(progressive->quantProgValFull);
1463 if (tile->quality >= region->numProgQuant)
1465 WLog_ERR(TAG,
"quality %" PRIu8
" > numProgQuant %" PRIu8, tile->quality,
1466 region->numProgQuant);
1470 quantProg = &(region->quantProgVals[tile->quality]);
1473 quantProgY = &(quantProg->yQuantValues);
1474 quantProgCb = &(quantProg->cbQuantValues);
1475 quantProgCr = &(quantProg->crQuantValues);
1477 if (!progressive_rfx_quant_cmp_equal(quantY, &(tile->yQuant)))
1478 WLog_Print(progressive->log, WLOG_WARN,
"non-progressive quantY has changed!");
1480 if (!progressive_rfx_quant_cmp_equal(quantCb, &(tile->cbQuant)))
1481 WLog_Print(progressive->log, WLOG_WARN,
"non-progressive quantCb has changed!");
1483 if (!progressive_rfx_quant_cmp_equal(quantCr, &(tile->crQuant)))
1484 WLog_Print(progressive->log, WLOG_WARN,
"non-progressive quantCr has changed!");
1486 if (!(context->flags & RFX_SUBBAND_DIFFING))
1487 WLog_WARN(TAG,
"PROGRESSIVE_BLOCK_CONTEXT::flags & RFX_SUBBAND_DIFFING not set");
1489 progressive_rfx_quant_add(quantY, quantProgY, &yBitPos);
1490 progressive_rfx_quant_add(quantCb, quantProgCb, &cbBitPos);
1491 progressive_rfx_quant_add(quantCr, quantProgCr, &crBitPos);
1492 if (!progressive_rfx_quant_sub(&(tile->yBitPos), &yBitPos, &yNumBits))
1494 if (!progressive_rfx_quant_sub(&(tile->cbBitPos), &cbBitPos, &cbNumBits))
1496 if (!progressive_rfx_quant_sub(&(tile->crBitPos), &crBitPos, &crNumBits))
1498 progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
1499 if (!progressive_rfx_quant_lsub(&shiftY, 1))
1501 progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
1502 if (!progressive_rfx_quant_lsub(&shiftCb, 1))
1504 progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
1505 if (!progressive_rfx_quant_lsub(&shiftCr, 1))
1508 tile->yBitPos = yBitPos;
1509 tile->cbBitPos = cbBitPos;
1510 tile->crBitPos = crBitPos;
1511 tile->yQuant = *quantY;
1512 tile->cbQuant = *quantCb;
1513 tile->crQuant = *quantCr;
1514 tile->yProgQuant = *quantProgY;
1515 tile->cbProgQuant = *quantProgCb;
1516 tile->crProgQuant = *quantProgCr;
1519 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 0) + 16]));
1521 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 1) + 16]));
1523 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 2) + 16]));
1526 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 0) + 16]));
1528 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 1) + 16]));
1530 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 2) + 16]));
1533 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 0) + 16]));
1535 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 1) + 16]));
1537 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 2) + 16]));
1539 status = progressive_rfx_upgrade_component(progressive, &shiftY, quantProgY, &yNumBits,
1540 pSrcDst[0], pCurrent[0], pSign[0], tile->ySrlData,
1541 tile->ySrlLen, tile->yRawData, tile->yRawLen,
1542 coeffDiff, sub, extrapolate);
1547 status = progressive_rfx_upgrade_component(progressive, &shiftCb, quantProgCb, &cbNumBits,
1548 pSrcDst[1], pCurrent[1], pSign[1], tile->cbSrlData,
1549 tile->cbSrlLen, tile->cbRawData, tile->cbRawLen,
1550 coeffDiff, sub, extrapolate);
1555 status = progressive_rfx_upgrade_component(progressive, &shiftCr, quantProgCr, &crNumBits,
1556 pSrcDst[2], pCurrent[2], pSign[2], tile->crSrlData,
1557 tile->crSrlLen, tile->crRawData, tile->crRawLen,
1558 coeffDiff, sub, extrapolate);
1564 const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**,
const INT16**);
1565 status = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride,
1566 progressive->format, &roi_64x64);
1572static inline BOOL progressive_tile_read_upgrade(
1573 PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
wStream* WINPR_RESTRICT s, UINT16 blockType,
1575 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1579 const size_t expect = 20;
1581 if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1584 tile.blockType = blockType;
1585 tile.blockLen = blockLen;
1588 Stream_Read_UINT8(s, tile.quantIdxY);
1589 Stream_Read_UINT8(s, tile.quantIdxCb);
1590 Stream_Read_UINT8(s, tile.quantIdxCr);
1591 Stream_Read_UINT16(s, tile.xIdx);
1592 Stream_Read_UINT16(s, tile.yIdx);
1593 Stream_Read_UINT8(s, tile.quality);
1594 Stream_Read_UINT16(s, tile.ySrlLen);
1595 Stream_Read_UINT16(s, tile.yRawLen);
1596 Stream_Read_UINT16(s, tile.cbSrlLen);
1597 Stream_Read_UINT16(s, tile.cbRawLen);
1598 Stream_Read_UINT16(s, tile.crSrlLen);
1599 Stream_Read_UINT16(s, tile.crRawLen);
1601 tile.ySrlData = Stream_Pointer(s);
1602 if (!Stream_SafeSeek(s, tile.ySrlLen))
1604 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes", tile.ySrlLen);
1608 tile.yRawData = Stream_Pointer(s);
1609 if (!Stream_SafeSeek(s, tile.yRawLen))
1611 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes", tile.yRawLen);
1615 tile.cbSrlData = Stream_Pointer(s);
1616 if (!Stream_SafeSeek(s, tile.cbSrlLen))
1618 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes",
1623 tile.cbRawData = Stream_Pointer(s);
1624 if (!Stream_SafeSeek(s, tile.cbRawLen))
1626 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes",
1631 tile.crSrlData = Stream_Pointer(s);
1632 if (!Stream_SafeSeek(s, tile.crSrlLen))
1634 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes",
1639 tile.crRawData = Stream_Pointer(s);
1640 if (!Stream_SafeSeek(s, tile.crRawLen))
1642 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes",
1647 return progressive_surface_tile_replace(surface, region, &tile, TRUE);
1651progressive_tile_read(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, BOOL simple,
1652 wStream* WINPR_RESTRICT s, UINT16 blockType, UINT32 blockLen,
1654 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1658 size_t expect = simple ? 16 : 17;
1660 if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1663 tile.blockType = blockType;
1664 tile.blockLen = blockLen;
1666 Stream_Read_UINT8(s, tile.quantIdxY);
1667 Stream_Read_UINT8(s, tile.quantIdxCb);
1668 Stream_Read_UINT8(s, tile.quantIdxCr);
1669 Stream_Read_UINT16(s, tile.xIdx);
1670 Stream_Read_UINT16(s, tile.yIdx);
1671 Stream_Read_UINT8(s, tile.flags);
1674 Stream_Read_UINT8(s, tile.quality);
1676 tile.quality = 0xFF;
1677 Stream_Read_UINT16(s, tile.yLen);
1678 Stream_Read_UINT16(s, tile.cbLen);
1679 Stream_Read_UINT16(s, tile.crLen);
1680 Stream_Read_UINT16(s, tile.tailLen);
1682 tile.yData = Stream_Pointer(s);
1683 if (!Stream_SafeSeek(s, tile.yLen))
1685 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes", tile.yLen);
1689 tile.cbData = Stream_Pointer(s);
1690 if (!Stream_SafeSeek(s, tile.cbLen))
1692 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes", tile.cbLen);
1696 tile.crData = Stream_Pointer(s);
1697 if (!Stream_SafeSeek(s, tile.crLen))
1699 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes", tile.crLen);
1703 tile.tailData = Stream_Pointer(s);
1704 if (!Stream_SafeSeek(s, tile.tailLen))
1706 WLog_Print(progressive->log, WLOG_ERROR,
" Failed to seek %" PRIu32
" bytes", tile.tailLen);
1710 return progressive_surface_tile_replace(surface, region, &tile, FALSE);
1713static void CALLBACK progressive_process_tiles_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
1714 void* context, PTP_WORK work)
1718 WINPR_UNUSED(instance);
1721 switch (param->tile->blockType)
1723 case PROGRESSIVE_WBT_TILE_SIMPLE:
1724 case PROGRESSIVE_WBT_TILE_FIRST:
1725 progressive_decompress_tile_first(param->progressive, param->tile, param->region,
1729 case PROGRESSIVE_WBT_TILE_UPGRADE:
1730 progressive_decompress_tile_upgrade(param->progressive, param->tile, param->region,
1734 WLog_Print(param->progressive->log, WLOG_ERROR,
"Invalid block type %04" PRIx16
" (%s)",
1735 param->tile->blockType,
1736 rfx_get_progressive_block_type_string(param->tile->blockType));
1741static inline SSIZE_T
1742progressive_process_tiles(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1744 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1750 const size_t start = Stream_GetPosition(s);
1751 UINT16 blockType = 0;
1752 UINT32 blockLen = 0;
1754 UINT16 close_cnt = 0;
1756 WINPR_ASSERT(progressive);
1757 WINPR_ASSERT(region);
1759 if (!Stream_CheckAndLogRequiredLength(TAG, s, region->tileDataSize))
1762 while ((Stream_GetRemainingLength(s) >= 6) &&
1763 (region->tileDataSize > (Stream_GetPosition(s) - start)))
1765 const size_t pos = Stream_GetPosition(s);
1767 Stream_Read_UINT16(s, blockType);
1768 Stream_Read_UINT32(s, blockLen);
1770#if defined(WITH_DEBUG_CODECS)
1771 WLog_Print(progressive->log, WLOG_DEBUG,
"%s",
1772 rfx_get_progressive_block_type_string(blockType));
1777 WLog_Print(progressive->log, WLOG_ERROR,
"Expected >= %" PRIu32
" remaining %" PRIu32,
1781 if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
1786 case PROGRESSIVE_WBT_TILE_SIMPLE:
1787 if (!progressive_tile_read(progressive, TRUE, s, blockType, blockLen, surface,
1792 case PROGRESSIVE_WBT_TILE_FIRST:
1793 if (!progressive_tile_read(progressive, FALSE, s, blockType, blockLen, surface,
1798 case PROGRESSIVE_WBT_TILE_UPGRADE:
1799 if (!progressive_tile_read_upgrade(progressive, s, blockType, blockLen, surface,
1804 WLog_ERR(TAG,
"Invalid block type %04" PRIx16
" (%s)", blockType,
1805 rfx_get_progressive_block_type_string(blockType));
1809 size_t rem = Stream_GetPosition(s);
1810 if ((rem - pos) != blockLen)
1812 WLog_Print(progressive->log, WLOG_ERROR,
1813 "Actual block read %" PRIuz
" but expected %" PRIu32, rem - pos, blockLen);
1819 end = Stream_GetPosition(s);
1820 if ((end - start) != region->tileDataSize)
1822 WLog_Print(progressive->log, WLOG_ERROR,
1823 "Actual total blocks read %" PRIuz
" but expected %" PRIu32, end - start,
1824 region->tileDataSize);
1828 if (count != region->numTiles)
1830 WLog_Print(progressive->log, WLOG_WARN,
1831 "numTiles inconsistency: actual: %" PRIu32
", expected: %" PRIu16
"\n", count,
1836 for (UINT32 idx = 0; idx < region->numTiles; idx++)
1840 param->progressive = progressive;
1841 param->region = region;
1842 param->context = context;
1845 if (progressive->rfx_context->priv->UseThreads)
1847 progressive->work_objects[idx] = CreateThreadpoolWork(
1848 progressive_process_tiles_tile_work_callback, (
void*)param,
nullptr);
1849 if (!progressive->work_objects[idx])
1851 WLog_Print(progressive->log, WLOG_ERROR,
1852 "Failed to create ThreadpoolWork for tile %" PRIu32, idx);
1857 SubmitThreadpoolWork(progressive->work_objects[idx]);
1859 close_cnt = WINPR_ASSERTING_INT_CAST(UINT16, idx + 1);
1863 progressive_process_tiles_tile_work_callback(
nullptr, param,
nullptr);
1868 WLog_Print(progressive->log, WLOG_ERROR,
"Failed to decompress %s at %" PRIu16,
1869 rfx_get_progressive_block_type_string(tile->blockType), idx);
1874 if (progressive->rfx_context->priv->UseThreads)
1876 for (UINT32 idx = 0; idx < close_cnt; idx++)
1878 WaitForThreadpoolWorkCallbacks(progressive->work_objects[idx], FALSE);
1879 CloseThreadpoolWork(progressive->work_objects[idx]);
1888 return (SSIZE_T)(end - start);
1891static inline SSIZE_T progressive_wb_sync(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1892 wStream* WINPR_RESTRICT s, UINT16 blockType,
1895 const UINT32 magic = 0xCACCACCA;
1896 const UINT16 version = 0x0100;
1899 sync.blockType = blockType;
1900 sync.blockLen = blockLen;
1902 if (sync.blockLen != 12)
1904 WLog_Print(progressive->log, WLOG_ERROR,
1905 "PROGRESSIVE_BLOCK_SYNC::blockLen = 0x%08" PRIx32
" != 0x%08" PRIx32,
1906 sync.blockLen, 12u);
1910 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1913#if defined(WITH_DEBUG_CODECS)
1914 WLog_Print(progressive->log, WLOG_DEBUG,
"ProgressiveSync");
1917 Stream_Read_UINT32(s, sync.magic);
1918 Stream_Read_UINT16(s, sync.version);
1920 if (sync.magic != magic)
1922 WLog_Print(progressive->log, WLOG_ERROR,
1923 "PROGRESSIVE_BLOCK_SYNC::magic = 0x%08" PRIx32
" != 0x%08" PRIx32, sync.magic,
1928 if (sync.version != 0x0100)
1930 WLog_Print(progressive->log, WLOG_ERROR,
1931 "PROGRESSIVE_BLOCK_SYNC::version = 0x%04" PRIx16
" != 0x%04" PRIu16,
1932 sync.version, version);
1936 if ((progressive->state & FLAG_WBT_SYNC) != 0)
1937 WLog_WARN(TAG,
"Duplicate PROGRESSIVE_BLOCK_SYNC, ignoring");
1939 progressive->state |= FLAG_WBT_SYNC;
1943static inline SSIZE_T progressive_wb_frame_begin(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1944 wStream* WINPR_RESTRICT s, UINT16 blockType,
1949 frameBegin.blockType = blockType;
1950 frameBegin.blockLen = blockLen;
1952 if (frameBegin.blockLen != 12)
1954 WLog_Print(progressive->log, WLOG_ERROR,
1955 " RFX_PROGRESSIVE_FRAME_BEGIN::blockLen = 0x%08" PRIx32
" != 0x%08" PRIx32,
1956 frameBegin.blockLen, 12u);
1960 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1963 Stream_Read_UINT32(s, frameBegin.frameIndex);
1964 Stream_Read_UINT16(s, frameBegin.regionCount);
1966#if defined(WITH_DEBUG_CODECS)
1967 WLog_Print(progressive->log, WLOG_DEBUG,
1968 "ProgressiveFrameBegin: frameIndex: %" PRIu32
" regionCount: %" PRIu16
"",
1969 frameBegin.frameIndex, frameBegin.regionCount);
1978 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
1980 WLog_ERR(TAG,
"Duplicate RFX_PROGRESSIVE_FRAME_BEGIN in stream, this is not allowed!");
1984 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1986 WLog_ERR(TAG,
"RFX_PROGRESSIVE_FRAME_BEGIN after RFX_PROGRESSIVE_FRAME_END in stream, this "
1991 progressive->state |= FLAG_WBT_FRAME_BEGIN;
1995static inline SSIZE_T progressive_wb_frame_end(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1996 wStream* WINPR_RESTRICT s, UINT16 blockType,
2001 frameEnd.blockType = blockType;
2002 frameEnd.blockLen = blockLen;
2004 if (frameEnd.blockLen != 6)
2006 WLog_Print(progressive->log, WLOG_ERROR,
2007 " RFX_PROGRESSIVE_FRAME_END::blockLen = 0x%08" PRIx32
" != 0x%08" PRIx32,
2008 frameEnd.blockLen, 6u);
2012 if (Stream_GetRemainingLength(s) != 0)
2014 WLog_Print(progressive->log, WLOG_ERROR,
2015 "ProgressiveFrameEnd short %" PRIuz
", expected %u",
2016 Stream_GetRemainingLength(s), 0U);
2020#if defined(WITH_DEBUG_CODECS)
2021 WLog_Print(progressive->log, WLOG_DEBUG,
"ProgressiveFrameEnd");
2024 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
2025 WLog_WARN(TAG,
"RFX_PROGRESSIVE_FRAME_END before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
2026 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2027 WLog_WARN(TAG,
"Duplicate RFX_PROGRESSIVE_FRAME_END, ignoring");
2029 progressive->state |= FLAG_WBT_FRAME_END;
2033static inline SSIZE_T progressive_wb_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2034 wStream* WINPR_RESTRICT s, UINT16 blockType,
2038 context->blockType = blockType;
2039 context->blockLen = blockLen;
2041 if (context->blockLen != 10)
2043 WLog_Print(progressive->log, WLOG_ERROR,
2044 "RFX_PROGRESSIVE_CONTEXT::blockLen = 0x%08" PRIx32
" != 0x%08x",
2045 context->blockLen, 10u);
2049 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2052 Stream_Read_UINT8(s, context->ctxId);
2053 Stream_Read_UINT16(s, context->tileSize);
2054 Stream_Read_UINT8(s, context->flags);
2056 if (context->ctxId != 0x00)
2057 WLog_WARN(TAG,
"RFX_PROGRESSIVE_CONTEXT::ctxId != 0x00: %" PRIu8, context->ctxId);
2059 if (context->tileSize != 64)
2061 WLog_ERR(TAG,
"RFX_PROGRESSIVE_CONTEXT::tileSize != 0x40: %" PRIu16, context->tileSize);
2065 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
2066 WLog_WARN(TAG,
"RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_BEGIN");
2067 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2068 WLog_WARN(TAG,
"RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_END");
2069 if ((progressive->state & FLAG_WBT_CONTEXT) != 0)
2070 WLog_WARN(TAG,
"Duplicate RFX_PROGRESSIVE_CONTEXT received, ignoring.");
2072#if defined(WITH_DEBUG_CODECS)
2073 WLog_Print(progressive->log, WLOG_DEBUG,
"ProgressiveContext: flags: 0x%02" PRIX8
"",
2077 progressive->state |= FLAG_WBT_CONTEXT;
2081static inline SSIZE_T
2082progressive_wb_read_region_header(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2083 wStream* WINPR_RESTRICT s, UINT16 blockType, UINT32 blockLen,
2084 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2086 region->usedTiles = 0;
2088 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2091 region->blockType = blockType;
2092 region->blockLen = blockLen;
2093 Stream_Read_UINT8(s, region->tileSize);
2094 Stream_Read_UINT16(s, region->numRects);
2095 Stream_Read_UINT8(s, region->numQuant);
2096 Stream_Read_UINT8(s, region->numProgQuant);
2097 Stream_Read_UINT8(s, region->flags);
2098 Stream_Read_UINT16(s, region->numTiles);
2099 Stream_Read_UINT32(s, region->tileDataSize);
2101 if (region->tileSize != 64)
2103 WLog_Print(progressive->log, WLOG_ERROR,
2104 "ProgressiveRegion tile size %" PRIu8
", expected %u", region->tileSize, 64U);
2108 if (region->numRects < 1)
2110 WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveRegion missing rect count %" PRIu16,
2115 if (region->numQuant > 7)
2117 WLog_Print(progressive->log, WLOG_ERROR,
2118 "ProgressiveRegion quant count too high %" PRIu8
", expected < %u",
2119 region->numQuant, 7U);
2123 const SSIZE_T rc = WINPR_ASSERTING_INT_CAST(SSIZE_T, Stream_GetRemainingLength(s));
2124 const SSIZE_T expect = region->numRects * 8ll + region->numQuant * 5ll +
2125 region->numProgQuant * 16ll + region->tileDataSize;
2129 if (len / 8LL < region->numRects)
2131 WLog_Print(progressive->log, WLOG_ERROR,
2132 "ProgressiveRegion data short for region->rects");
2135 len -= region->numRects * 8LL;
2137 if (len / 5LL < region->numQuant)
2139 WLog_Print(progressive->log, WLOG_ERROR,
2140 "ProgressiveRegion data short for region->cQuant");
2143 len -= region->numQuant * 5LL;
2145 if (len / 16LL < region->numProgQuant)
2147 WLog_Print(progressive->log, WLOG_ERROR,
2148 "ProgressiveRegion data short for region->cProgQuant");
2151 len -= region->numProgQuant * 16LL;
2153 if (len < region->tileDataSize * 1ll)
2155 WLog_Print(progressive->log, WLOG_ERROR,
2156 "ProgressiveRegion data short for region->tiles");
2159 len -= region->tileDataSize;
2162 WLog_Print(progressive->log, WLOG_WARN,
2163 "Unused bytes detected, %" PRIdz
" bytes not processed", len);
2169static inline SSIZE_T progressive_wb_skip_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2170 wStream* WINPR_RESTRICT s, UINT16 blockType,
2173 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2176 progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2180 if (!Stream_SafeSeek(s, WINPR_ASSERTING_INT_CAST(
size_t, rc)))
2186static inline SSIZE_T progressive_wb_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2187 wStream* WINPR_RESTRICT s, UINT16 blockType,
2190 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2195 UINT16 boxRight = 0;
2196 UINT16 boxBottom = 0;
2199 UINT16 idxRight = 0;
2200 UINT16 idxBottom = 0;
2203 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
2205 WLog_WARN(TAG,
"RFX_PROGRESSIVE_REGION before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
2206 return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2208 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2210 WLog_WARN(TAG,
"RFX_PROGRESSIVE_REGION after RFX_PROGRESSIVE_FRAME_END, ignoring");
2211 return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2214 progressive->state |= FLAG_WBT_REGION;
2216 rc = progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2220 for (UINT16 index = 0; index < region->numRects; index++)
2222 RFX_RECT* rect = &(region->rects[index]);
2223 Stream_Read_UINT16(s, rect->x);
2224 Stream_Read_UINT16(s, rect->y);
2225 Stream_Read_UINT16(s, rect->width);
2226 Stream_Read_UINT16(s, rect->height);
2229 for (BYTE index = 0; index < region->numQuant; index++)
2232 progressive_component_codec_quant_read(s, quantVal);
2234 if (!progressive_rfx_quant_lcmp_greater_equal(quantVal, 6))
2236 WLog_Print(progressive->log, WLOG_ERROR,
2237 "ProgressiveRegion region->cQuant[%" PRIu32
"] < 6", index);
2241 if (!progressive_rfx_quant_lcmp_less_equal(quantVal, 15))
2243 WLog_Print(progressive->log, WLOG_ERROR,
2244 "ProgressiveRegion region->cQuant[%" PRIu32
"] > 15", index);
2249 for (BYTE index = 0; index < region->numProgQuant; index++)
2253 Stream_Read_UINT8(s, quantProgVal->quality);
2255 progressive_component_codec_quant_read(s, &(quantProgVal->yQuantValues));
2256 progressive_component_codec_quant_read(s, &(quantProgVal->cbQuantValues));
2257 progressive_component_codec_quant_read(s, &(quantProgVal->crQuantValues));
2260#if defined(WITH_DEBUG_CODECS)
2261 WLog_Print(progressive->log, WLOG_DEBUG,
2262 "ProgressiveRegion: numRects: %" PRIu16
" numTiles: %" PRIu16
2263 " tileDataSize: %" PRIu32
" flags: 0x%02" PRIX8
" numQuant: %" PRIu8
2264 " numProgQuant: %" PRIu8
"",
2265 region->numRects, region->numTiles, region->tileDataSize, region->flags,
2266 region->numQuant, region->numProgQuant);
2269 boxLeft = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridWidth);
2270 boxTop = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridHeight);
2274 for (UINT16 index = 0; index < region->numRects; index++)
2276 RFX_RECT* rect = &(region->rects[index]);
2277 idxLeft = rect->x / 64;
2278 idxTop = rect->y / 64;
2279 idxRight = (rect->x + rect->width + 63) / 64;
2280 idxBottom = (rect->y + rect->height + 63) / 64;
2282 if (idxLeft < boxLeft)
2285 if (idxTop < boxTop)
2288 if (idxRight > boxRight)
2289 boxRight = idxRight;
2291 if (idxBottom > boxBottom)
2292 boxBottom = idxBottom;
2294#if defined(WITH_DEBUG_CODECS)
2295 WLog_Print(progressive->log, WLOG_DEBUG,
2296 "rect[%" PRIu16
"]: x: %" PRIu16
" y: %" PRIu16
" w: %" PRIu16
" h: %" PRIu16
"",
2297 index, rect->x, rect->y, rect->width, rect->height);
2301 const SSIZE_T res = progressive_process_tiles(progressive, s, region, surface, context);
2307static inline SSIZE_T progressive_parse_block(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2310 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2312 UINT16 blockType = 0;
2313 UINT32 blockLen = 0;
2315 wStream sub = WINPR_C_ARRAY_INIT;
2317 WINPR_ASSERT(progressive);
2319 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
2322 Stream_Read_UINT16(s, blockType);
2323 Stream_Read_UINT32(s, blockLen);
2327 WLog_WARN(TAG,
"Invalid blockLen %" PRIu32
", expected >= 6", blockLen);
2330 if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
2332 Stream_StaticConstInit(&sub, Stream_Pointer(s), blockLen - 6);
2333 Stream_Seek(s, blockLen - 6);
2337 case PROGRESSIVE_WBT_SYNC:
2338 rc = progressive_wb_sync(progressive, &sub, blockType, blockLen);
2341 case PROGRESSIVE_WBT_FRAME_BEGIN:
2342 rc = progressive_wb_frame_begin(progressive, &sub, blockType, blockLen);
2345 case PROGRESSIVE_WBT_FRAME_END:
2346 rc = progressive_wb_frame_end(progressive, &sub, blockType, blockLen);
2349 case PROGRESSIVE_WBT_CONTEXT:
2350 rc = progressive_wb_context(progressive, &sub, blockType, blockLen);
2353 case PROGRESSIVE_WBT_REGION:
2354 rc = progressive_wb_region(progressive, &sub, blockType, blockLen, surface, region);
2358 WLog_Print(progressive->log, WLOG_ERROR,
"Invalid block type %04" PRIx16, blockType);
2365 if (Stream_GetRemainingLength(&sub) > 0)
2367 WLog_Print(progressive->log, WLOG_ERROR,
2368 "block len %" PRIu32
" does not match read data %" PRIuz, blockLen,
2369 blockLen - Stream_GetRemainingLength(&sub));
2377static inline BOOL update_tiles(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2379 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2380 UINT32 nXDst, UINT32 nYDst,
2381 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
2382 REGION16* WINPR_RESTRICT invalidRegion)
2386 if ((nXDst > UINT16_MAX) || (nYDst > UINT16_MAX))
2389 REGION16 clippingRects = WINPR_C_ARRAY_INIT;
2390 region16_init(&clippingRects);
2391 for (UINT32 i = 0; i < region->numRects; i++)
2394 const RFX_RECT* rect = &(region->rects[i]);
2396 const UINT32 cx = nXDst + rect->x;
2397 const UINT32 cy = nYDst + rect->y;
2398 if ((cx > UINT16_MAX) || (cy > UINT16_MAX))
2401 clippingRect.left = (UINT16)cx;
2402 clippingRect.top = (UINT16)cy;
2404 const UINT32 cr = 1u * clippingRect.left + rect->width;
2405 const UINT32 cb = 1u * clippingRect.top + rect->height;
2406 if ((cr > UINT16_MAX) || (cb > UINT16_MAX))
2409 clippingRect.right = (UINT16)cr;
2410 clippingRect.bottom = (UINT16)cb;
2411 if (!region16_union_rect(&clippingRects, &clippingRects, &clippingRect))
2415 for (UINT32 i = 0; i < surface->numUpdatedTiles; i++)
2417 UINT32 nbUpdateRects = 0;
2421 WINPR_ASSERT(surface->updatedTileIndices);
2422 const UINT32 index = surface->updatedTileIndices[i];
2424 WINPR_ASSERT(index < surface->tilesSize);
2428 const UINT32 dl = nXDst + tile->x;
2429 if (dl > UINT16_MAX - 64)
2431 updateRect.left = WINPR_ASSERTING_INT_CAST(UINT16, dl);
2433 const UINT32 dt = nYDst + tile->y;
2434 if (dt > UINT16_MAX - 64)
2436 updateRect.top = WINPR_ASSERTING_INT_CAST(UINT16, dt);
2437 updateRect.right = updateRect.left + 64;
2438 updateRect.bottom = updateRect.top + 64;
2440 REGION16 updateRegion = WINPR_C_ARRAY_INIT;
2441 region16_init(&updateRegion);
2442 BOOL success = FALSE;
2443 if (!region16_intersect_rect(&updateRegion, &clippingRects, &updateRect))
2446 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
2448 for (UINT32 j = 0; j < nbUpdateRects; j++)
2451 if (rect->left < updateRect.left)
2453 const UINT32 nXSrc = rect->left - updateRect.left;
2454 const UINT32 nYSrc = rect->top - updateRect.top;
2455 const UINT32 width = rect->right - rect->left;
2456 const UINT32 height = rect->bottom - rect->top;
2458 if (rect->left + width > surface->width)
2460 if (rect->top + height > surface->height)
2462 const BOOL res = freerdp_image_copy_no_overlap(
2463 pDstData, DstFormat, nDstStep, rect->left, rect->top, width, height, tile->data,
2464 progressive->format, tile->stride, nXSrc, nYSrc,
nullptr, FREERDP_KEEP_DST_ALPHA);
2471 const RECTANGLE_16* damage = region16_extents(&updateRegion);
2472 if (!region16_union_rect(invalidRegion, invalidRegion, damage))
2476 tile->dirty = FALSE;
2480 region16_uninit(&updateRegion);
2488 region16_uninit(&clippingRects);
2492INT32 progressive_decompress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2493 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
2494 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2495 UINT32 nXDst, UINT32 nYDst,
REGION16* WINPR_RESTRICT invalidRegion,
2496 UINT16 surfaceId, UINT32 frameId)
2500 WINPR_ASSERT(progressive);
2505 WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveRegion no surface for %" PRIu16,
2510 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2511 WINPR_ASSERT(region);
2513 if (surface->frameId != frameId)
2515 surface->frameId = frameId;
2516 surface->numUpdatedTiles = 0;
2519 wStream ss = WINPR_C_ARRAY_INIT;
2520 wStream* s = Stream_StaticConstInit(&ss, pSrcData, SrcSize);
2525 case PIXEL_FORMAT_RGBA32:
2526 case PIXEL_FORMAT_RGBX32:
2527 case PIXEL_FORMAT_BGRA32:
2528 case PIXEL_FORMAT_BGRX32:
2529 progressive->format = DstFormat;
2532 progressive->format = PIXEL_FORMAT_XRGB32;
2536 const size_t start = Stream_GetPosition(s);
2537 progressive->state = 0;
2538 while (Stream_GetRemainingLength(s) > 0)
2540 if (progressive_parse_block(progressive, s, surface, region) < 0)
2545 const size_t end = Stream_GetPosition(s);
2546 if ((end - start) != SrcSize)
2548 WLog_Print(progressive->log, WLOG_ERROR,
2549 "total block len %" PRIuz
" does not match read data %" PRIu32, end - start,
2556 if (!update_tiles(progressive, surface, pDstData, DstFormat, nDstStep, nXDst, nYDst, region,
2565BOOL progressive_rfx_write_message_progressive_simple(
2566 PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
wStream* WINPR_RESTRICT s,
2567 const RFX_MESSAGE* WINPR_RESTRICT msg)
2569 RFX_CONTEXT* context =
nullptr;
2571 WINPR_ASSERT(progressive);
2574 context = progressive->rfx_context;
2575 return rfx_write_message_progressive_simple(context, s, msg);
2578int progressive_compress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2579 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 SrcFormat,
2580 UINT32 Width, UINT32 Height, UINT32 ScanLine,
2581 const REGION16* WINPR_RESTRICT invalidRegion,
2582 BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
2587 UINT32 numRects = 0;
2589 RFX_MESSAGE* message =
nullptr;
2591 if (!progressive || !pSrcData || !ppDstData || !pDstSize)
2600 case PIXEL_FORMAT_ABGR32:
2601 case PIXEL_FORMAT_ARGB32:
2602 case PIXEL_FORMAT_XBGR32:
2603 case PIXEL_FORMAT_XRGB32:
2604 case PIXEL_FORMAT_BGRA32:
2605 case PIXEL_FORMAT_BGRX32:
2606 case PIXEL_FORMAT_RGBA32:
2607 case PIXEL_FORMAT_RGBX32:
2608 ScanLine = Width * 4;
2615 if (SrcSize < Height * ScanLine)
2620 numRects = (Width + 63) / 64;
2621 numRects *= (Height + 63) / 64;
2625 const int nr = region16_n_rects(invalidRegion);
2626 numRects = WINPR_ASSERTING_INT_CAST(uint32_t, nr);
2632 if (!Stream_EnsureRemainingCapacity(progressive->rects, numRects *
sizeof(
RFX_RECT)))
2634 rects = Stream_BufferAs(progressive->rects,
RFX_RECT);
2637 const RECTANGLE_16* region_rects = region16_rects(invalidRegion,
nullptr);
2638 for (UINT32 idx = 0; idx < numRects; idx++)
2645 rect->width = r->right - r->left;
2646 rect->height = r->bottom - r->top;
2654 for (UINT32 i = 0; i < numRects; i++)
2660 WINPR_ASSERT(Width >= x);
2661 WINPR_ASSERT(Height >= y);
2662 r->width = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Width - x));
2663 r->height = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Height - y));
2665 if (x + 64UL >= Width)
2673 WINPR_ASSERT(r->x % 64 == 0);
2674 WINPR_ASSERT(r->y % 64 == 0);
2675 WINPR_ASSERT(r->width <= 64);
2676 WINPR_ASSERT(r->height <= 64);
2679 s = progressive->buffer;
2680 Stream_ResetPosition(s);
2682 progressive->rfx_context->mode = RLGR1;
2684 progressive->rfx_context->width = WINPR_ASSERTING_INT_CAST(UINT16, Width);
2685 progressive->rfx_context->height = WINPR_ASSERTING_INT_CAST(UINT16, Height);
2686 rfx_context_set_pixel_format(progressive->rfx_context, SrcFormat);
2687 message = rfx_encode_message(progressive->rfx_context, rects, numRects, pSrcData, Width, Height,
2691 WLog_ERR(TAG,
"failed to encode rfx message");
2695 rc = progressive_rfx_write_message_progressive_simple(progressive, s, message);
2696 rfx_message_free(progressive->rfx_context, message);
2701 const size_t pos = Stream_GetPosition(s);
2702 WINPR_ASSERT(pos <= UINT32_MAX);
2703 *pDstSize = (UINT32)pos;
2705 *ppDstData = Stream_Buffer(s);
2711BOOL progressive_context_reset(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive)
2716 RFX_CONTEXT* rfx = progressive->rfx_context;
2718 return rfx_context_reset(rfx, rfx->width, rfx->height);
2721PROGRESSIVE_CONTEXT* progressive_context_new(BOOL Compressor)
2723 return progressive_context_new_ex(Compressor, 0);
2726PROGRESSIVE_CONTEXT* progressive_context_new_ex(BOOL Compressor, UINT32 ThreadingFlags)
2728 PROGRESSIVE_CONTEXT* progressive =
2729 (PROGRESSIVE_CONTEXT*)winpr_aligned_calloc(1,
sizeof(PROGRESSIVE_CONTEXT), 32);
2734 progressive->Compressor = Compressor;
2735 progressive->quantProgValFull.quality = 100;
2736 progressive->log = WLog_Get(TAG);
2737 if (!progressive->log)
2739 progressive->rfx_context = rfx_context_new_ex(Compressor, ThreadingFlags);
2740 if (!progressive->rfx_context)
2742 progressive->buffer = Stream_New(
nullptr, 1024);
2743 if (!progressive->buffer)
2745 progressive->rects = Stream_New(
nullptr, 1024);
2746 if (!progressive->rects)
2748 progressive->SurfaceContexts = HashTable_New(TRUE);
2749 if (!progressive->SurfaceContexts)
2753 wObject* obj = HashTable_ValueObject(progressive->SurfaceContexts);
2755 obj->fnObjectFree = progressive_surface_context_free;
2759 WINPR_PRAGMA_DIAG_PUSH
2760 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2761 progressive_context_free(progressive);
2762 WINPR_PRAGMA_DIAG_POP
2766void progressive_context_free(PROGRESSIVE_CONTEXT* progressive)
2771 Stream_Free(progressive->buffer, TRUE);
2772 Stream_Free(progressive->rects, TRUE);
2773 rfx_context_free(progressive->rfx_context);
2775 HashTable_Free(progressive->SurfaceContexts);
2777 winpr_aligned_free(progressive);
WINPR_ATTR_NODISCARD fn_lShiftC_16s_inplace_t lShiftC_16s_inplace
WINPR_ATTR_NODISCARD fn_add_16s_inplace_t add_16s_inplace
Do vecotor addition, store result in both input buffers pSrcDst1 = pSrcDst2 = pSrcDst1 + pSrcDst2.
This struct contains function pointer to initialize/free objects.