25#include <freerdp/config.h>
31#include <winpr/assert.h>
32#include <winpr/cast.h>
34#include <winpr/print.h>
35#include <winpr/sysinfo.h>
36#include <winpr/bitstream.h>
37#include <winpr/intrin.h>
39#include "rfx_bitstream.h"
52#define GetMinBits(_val, _nbits) \
68static inline uint32_t UpdateParam(uint32_t* param, int32_t deltaP)
73 const uint32_t udeltaP = WINPR_ASSERTING_INT_CAST(uint32_t, -deltaP);
80 *param += WINPR_ASSERTING_INT_CAST(uint32_t, deltaP);
84 return (*param) >> LSGR;
87static BOOL g_LZCNT = FALSE;
89static INIT_ONCE rfx_rlgr_init_once = INIT_ONCE_STATIC_INIT;
91static BOOL CALLBACK rfx_rlgr_init(
PINIT_ONCE once, PVOID param, PVOID* context)
95 WINPR_UNUSED(context);
97 g_LZCNT = IsProcessorFeaturePresentEx(PF_EX_LZCNT);
101static inline UINT32 lzcnt_s(UINT32 x)
113 WINPR_ASSERT(n >= 16);
120 WINPR_ASSERT(n >= 8);
127 WINPR_ASSERT(n >= 4);
134 WINPR_ASSERT(n >= 2);
141 WINPR_ASSERT(n >= 2);
145 WINPR_ASSERT(n >= x);
152int rfx_rlgr_decode(RLGR_MODE mode,
const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
153 INT16* WINPR_RESTRICT pDstData, UINT32 rDstSize)
170 INT16* pOutput =
nullptr;
173 const SSIZE_T DstSize = rDstSize;
175 if (!InitOnceExecuteOnce(&rfx_rlgr_init_once, rfx_rlgr_init,
nullptr,
nullptr))
184 if ((mode != RLGR1) && (mode != RLGR3))
187 if (!pSrcData || !SrcSize)
190 if (!pDstData || !DstSize)
197 BitStream_Attach(bs, pSrcData, SrcSize);
200 while ((BitStream_GetRemainingLength(bs) > 0) && ((pOutput - pDstData) < DstSize))
210 cnt = lzcnt_s(bs->accumulator);
212 size_t nbits = BitStream_GetRemainingLength(bs);
215 cnt = WINPR_ASSERTING_INT_CAST(uint32_t, nbits);
217 vk = WINPR_ASSERTING_INT_CAST(uint32_t, cnt);
219 while ((cnt == 32) && (BitStream_GetRemainingLength(bs) > 0))
221 BitStream_Shift32(bs);
223 cnt = lzcnt_s(bs->accumulator);
225 nbits = BitStream_GetRemainingLength(bs);
230 WINPR_ASSERT(cnt + vk <= UINT32_MAX);
231 vk += WINPR_ASSERTING_INT_CAST(uint32_t, cnt);
234 BitStream_Shift(bs, (vk % 32));
236 if (BitStream_GetRemainingLength(bs) < 1)
239 BitStream_Shift(bs, 1);
243 const UINT32 add = (1 << k);
258 if (BitStream_GetRemainingLength(bs) < k)
261 bs->mask = ((1 << k) - 1);
262 run += ((bs->accumulator >> (32 - k)) & bs->mask);
263 BitStream_Shift(bs, k);
267 if (BitStream_GetRemainingLength(bs) < 1)
270 sign = (bs->accumulator & 0x80000000) ? 1 : 0;
271 BitStream_Shift(bs, 1);
275 cnt = lzcnt_s(~(bs->accumulator));
277 nbits = BitStream_GetRemainingLength(bs);
282 vk = WINPR_ASSERTING_INT_CAST(uint32_t, cnt);
284 while ((cnt == 32) && (BitStream_GetRemainingLength(bs) > 0))
286 BitStream_Shift32(bs);
288 cnt = lzcnt_s(~(bs->accumulator));
290 nbits = BitStream_GetRemainingLength(bs);
295 WINPR_ASSERT(cnt + vk <= UINT32_MAX);
296 vk += WINPR_ASSERTING_INT_CAST(uint32_t, cnt);
299 BitStream_Shift(bs, (vk % 32));
301 if (BitStream_GetRemainingLength(bs) < 1)
304 BitStream_Shift(bs, 1);
308 if (BitStream_GetRemainingLength(bs) < kr)
311 bs->mask = ((1 << kr) - 1);
313 code = (UINT16)((bs->accumulator >> (32 - kr)) & bs->mask);
316 BitStream_Shift(bs, kr);
357 mag = WINPR_ASSERTING_INT_CAST(int16_t, (code + 1)) * -1;
359 mag = WINPR_ASSERTING_INT_CAST(int16_t, code + 1);
363 offset = WINPR_ASSERTING_INT_CAST(
size_t, (pOutput)-pDstData);
366 if ((offset + size) > rDstSize)
367 size = WINPR_ASSERTING_INT_CAST(
size_t, DstSize) - offset;
371 ZeroMemory(pOutput, size *
sizeof(INT16));
375 if ((pOutput - pDstData) < DstSize)
387 cnt = lzcnt_s(~(bs->accumulator));
389 size_t nbits = BitStream_GetRemainingLength(bs);
394 vk = WINPR_ASSERTING_INT_CAST(uint32_t, cnt);
396 while ((cnt == 32) && (BitStream_GetRemainingLength(bs) > 0))
398 BitStream_Shift32(bs);
400 cnt = lzcnt_s(~(bs->accumulator));
402 nbits = BitStream_GetRemainingLength(bs);
407 WINPR_ASSERT(cnt + vk <= UINT32_MAX);
408 vk += WINPR_ASSERTING_INT_CAST(uint32_t, cnt);
411 BitStream_Shift(bs, (vk % 32));
413 if (BitStream_GetRemainingLength(bs) < 1)
416 BitStream_Shift(bs, 1);
420 if (BitStream_GetRemainingLength(bs) < kr)
423 bs->mask = ((1 << kr) - 1);
425 code = (UINT16)((bs->accumulator >> (32 - kr)) & bs->mask);
428 BitStream_Shift(bs, kr);
443 kr = (krp >> LSGR) & UINT32_MAX;
489 mag = WINPR_ASSERTING_INT_CAST(INT16, (code + 1) >> 1) * -1;
491 mag = WINPR_ASSERTING_INT_CAST(INT16, code >> 1);
494 if ((pOutput - pDstData) < DstSize)
500 else if (mode == RLGR3)
506 mag = WINPR_ASSERTING_INT_CAST(int16_t, code);
507 nIdx = 32 - lzcnt_s(WINPR_ASSERTING_INT_CAST(uint32_t, mag));
510 if (BitStream_GetRemainingLength(bs) < nIdx)
513 bs->mask = ((1 << nIdx) - 1);
515 val1 = ((bs->accumulator >> (32 - nIdx)) & bs->mask);
518 BitStream_Shift(bs, nIdx);
533 else if (!val1 && !val2)
546 mag = WINPR_ASSERTING_INT_CAST(int16_t, (val1 + 1) >> 1) * -1;
548 mag = WINPR_ASSERTING_INT_CAST(int16_t, val1 >> 1);
550 if ((pOutput - pDstData) < DstSize)
557 mag = WINPR_ASSERTING_INT_CAST(int16_t, (val2 + 1) >> 1) * -1;
559 mag = WINPR_ASSERTING_INT_CAST(int16_t, val2 >> 1);
561 if ((pOutput - pDstData) < DstSize)
563 *pOutput = WINPR_ASSERTING_INT_CAST(int16_t, mag);
570 offset = WINPR_ASSERTING_INT_CAST(
size_t, (pOutput - pDstData));
572 if (offset < rDstSize)
574 size = WINPR_ASSERTING_INT_CAST(
size_t, DstSize) - offset;
575 ZeroMemory(pOutput, size * 2);
579 offset = WINPR_ASSERTING_INT_CAST(
size_t, (pOutput - pDstData));
581 if ((DstSize < 0) || (offset != (
size_t)DstSize))
588#define GetNextInput(_n) \
603#define OutputBits(numBits, bitPattern) rfx_bitstream_put_bits(bs, bitPattern, numBits)
606static inline void OutputBit(
RFX_BITSTREAM* bs, uint32_t count, UINT8 bit)
608 UINT16 _b = ((bit) ? 0xFFFF : 0);
609 const uint32_t rem = count % 16;
610 for (uint32_t x = 0; x < count - rem; x += 16)
611 rfx_bitstream_put_bits(bs, _b, 16);
614 rfx_bitstream_put_bits(bs, _b, rem);
619static inline UINT32 Get2MagSign(INT32 input)
622 return WINPR_ASSERTING_INT_CAST(UINT32, 2 * input);
623 return WINPR_ASSERTING_INT_CAST(UINT32, -2 * input - 1);
627#define CodeGR(krp, val) rfx_rlgr_code_gr(bs, krp, val)
629static void rfx_rlgr_code_gr(
RFX_BITSTREAM* bs, uint32_t* krp, UINT32 val)
631 uint32_t kr = *krp >> LSGR;
635 const uint32_t vk = val >> kr;
636 OutputBit(bs, vk, 1);
642 OutputBits(kr, val & ((1 << kr) - 1));
648 (void)UpdateParam(krp, -2);
652 (void)UpdateParam(krp, WINPR_CXX_COMPAT_CAST(int32_t, vk));
656int rfx_rlgr_encode(RLGR_MODE mode,
const INT16* WINPR_RESTRICT data, UINT32 data_size,
657 BYTE* WINPR_RESTRICT buffer, UINT32 buffer_size)
667 rfx_bitstream_attach(bs, buffer, buffer_size);
675 while (data_size > 0)
681 uint32_t numZeros = 0;
690 while (input == 0 && data_size > 0)
698 while (numZeros >= runmax)
702 k = UpdateParam(&kp, UP_GR);
710 OutputBits(k, numZeros);
717 (UINT32)(input < 0 ? -input : input);
718 sign = (input < 0 ? 1 : 0);
720 OutputBit(bs, 1, sign);
721 CodeGR(&krp, mag ? mag - 1 : 0);
723 k = UpdateParam(&kp, -DN_GR);
737 twoMs = Get2MagSign(input);
745 k = UpdateParam(&kp, -DQ_GR);
749 k = UpdateParam(&kp, UQ_GR);
765 twoMs1 = Get2MagSign(input);
767 twoMs2 = Get2MagSign(input);
768 sum2Ms = twoMs1 + twoMs2;
770 CodeGR(&krp, sum2Ms);
773 GetMinBits(sum2Ms, nIdx);
774 OutputBits(nIdx, twoMs1);
778 if (twoMs1 && twoMs2)
780 k = UpdateParam(&kp, -2 * DQ_GR);
782 else if (!twoMs1 && !twoMs2)
784 k = UpdateParam(&kp, 2 * UQ_GR);
790 rfx_bitstream_flush(bs);
791 uint32_t processed_size = rfx_bitstream_get_processed_bytes(bs);
792 winpr_aligned_free(bs);
794 return WINPR_ASSERTING_INT_CAST(
int, processed_size);