2011-08-10 11:13:39 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-10 11:13:39 +04:00
|
|
|
* RemoteFX Codec Library - RLGR
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This implementation of RLGR refers to
|
|
|
|
* [MS-RDPRFX] 3.1.8.1.7.3 RLGR1/RLGR3 Pseudocode
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-22 05:21:08 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-08-06 05:41:58 +04:00
|
|
|
#include <winpr/print.h>
|
2014-08-12 02:48:42 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2014-08-06 05:41:58 +04:00
|
|
|
#include <winpr/bitstream.h>
|
2015-05-26 17:41:29 +03:00
|
|
|
#include <winpr/intrin.h>
|
2012-11-22 05:21:08 +04:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
#include "rfx_bitstream.h"
|
|
|
|
|
|
|
|
#include "rfx_rlgr.h"
|
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
/* Constants used in RLGR1/RLGR3 algorithm */
|
|
|
|
#define KPMAX (80) /* max value for kp or krp */
|
|
|
|
#define LSGR (3) /* shift count to convert kp to k */
|
|
|
|
#define UP_GR (4) /* increase in kp after a zero run in RL mode */
|
|
|
|
#define DN_GR (6) /* decrease in kp after a nonzero symbol in RL mode */
|
|
|
|
#define UQ_GR (3) /* increase in kp after nonzero symbol in GR mode */
|
|
|
|
#define DQ_GR (3) /* decrease in kp after zero symbol in GR mode */
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* Returns the least number of bits required to represent a given value */
|
|
|
|
#define GetMinBits(_val, _nbits) \
|
|
|
|
{ \
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 _v = _val; \
|
2011-08-10 11:13:39 +04:00
|
|
|
_nbits = 0; \
|
|
|
|
while (_v) \
|
|
|
|
{ \
|
|
|
|
_v >>= 1; \
|
|
|
|
_nbits++; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the passed parameter and clamp it to the range [0, KPMAX]
|
|
|
|
* Return the value of parameter right-shifted by LSGR
|
|
|
|
*/
|
|
|
|
#define UpdateParam(_param, _deltaP, _k) \
|
|
|
|
{ \
|
|
|
|
_param += _deltaP; \
|
|
|
|
if (_param > KPMAX) \
|
|
|
|
_param = KPMAX; \
|
|
|
|
if (_param < 0) \
|
|
|
|
_param = 0; \
|
|
|
|
_k = (_param >> LSGR); \
|
|
|
|
}
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
static BOOL g_LZCNT = FALSE;
|
|
|
|
|
2017-03-27 10:00:27 +03:00
|
|
|
static INIT_ONCE rfx_rlgr_init_once = INIT_ONCE_STATIC_INIT;
|
|
|
|
|
|
|
|
static BOOL CALLBACK rfx_rlgr_init(PINIT_ONCE once, PVOID param, PVOID *context)
|
|
|
|
{
|
|
|
|
g_LZCNT = IsProcessorFeaturePresentEx(PF_EX_LZCNT);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
static INLINE UINT32 lzcnt_s(UINT32 x)
|
|
|
|
{
|
|
|
|
if (!x)
|
|
|
|
return 32;
|
|
|
|
|
|
|
|
if (!g_LZCNT)
|
|
|
|
{
|
|
|
|
UINT32 y;
|
|
|
|
int n = 32;
|
|
|
|
y = x >> 16; if (y != 0) { n = n - 16; x = y; }
|
|
|
|
y = x >> 8; if (y != 0) { n = n - 8; x = y; }
|
|
|
|
y = x >> 4; if (y != 0) { n = n - 4; x = y; }
|
|
|
|
y = x >> 2; if (y != 0) { n = n - 2; x = y; }
|
|
|
|
y = x >> 1; if (y != 0) return n - 2;
|
|
|
|
return n - x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __lzcnt(x);
|
|
|
|
}
|
|
|
|
|
2016-11-23 15:21:59 +03:00
|
|
|
int rfx_rlgr_decode(RLGR_MODE mode, const BYTE* pSrcData, UINT32 SrcSize, INT16* pDstData, UINT32 DstSize)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2011-11-03 06:26:00 +04:00
|
|
|
int vk;
|
2014-08-06 05:41:58 +04:00
|
|
|
int run;
|
|
|
|
int cnt;
|
|
|
|
int size;
|
|
|
|
int nbits;
|
2019-02-07 16:17:35 +03:00
|
|
|
size_t offset;
|
2014-08-06 05:41:58 +04:00
|
|
|
INT16 mag;
|
2019-02-07 16:17:35 +03:00
|
|
|
UINT32 k;
|
|
|
|
INT32 kp;
|
|
|
|
UINT32 kr;
|
|
|
|
INT32 krp;
|
2014-08-06 05:41:58 +04:00
|
|
|
UINT16 code;
|
|
|
|
UINT32 sign;
|
|
|
|
UINT32 nIdx;
|
|
|
|
UINT32 val1;
|
|
|
|
UINT32 val2;
|
|
|
|
INT16* pOutput;
|
|
|
|
wBitStream* bs;
|
|
|
|
wBitStream s_bs;
|
2012-11-22 05:21:08 +04:00
|
|
|
|
2017-03-27 10:00:27 +03:00
|
|
|
InitOnceExecuteOnce(&rfx_rlgr_init_once, rfx_rlgr_init, NULL, NULL);
|
2014-08-12 02:48:42 +04:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
k = 1;
|
|
|
|
kp = k << LSGR;
|
2014-08-06 05:41:58 +04:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
kr = 1;
|
|
|
|
krp = kr << LSGR;
|
|
|
|
|
2016-11-23 15:21:59 +03:00
|
|
|
if ((mode != RLGR1) && (mode != RLGR3))
|
|
|
|
mode = RLGR1;
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
if (!pSrcData || !SrcSize)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!pDstData || !DstSize)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pOutput = pDstData;
|
|
|
|
|
|
|
|
bs = &s_bs;
|
|
|
|
|
|
|
|
BitStream_Attach(bs, pSrcData, SrcSize);
|
|
|
|
BitStream_Fetch(bs);
|
|
|
|
|
|
|
|
while ((BitStream_GetRemainingLength(bs) > 0) && ((pOutput - pDstData) < DstSize))
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
|
|
|
if (k)
|
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
/* Run-Length (RL) Mode */
|
|
|
|
|
|
|
|
run = 0;
|
|
|
|
|
|
|
|
/* count number of leading 0s */
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
cnt = lzcnt_s(bs->accumulator);
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
nbits = BitStream_GetRemainingLength(bs);
|
|
|
|
|
|
|
|
if (cnt > nbits)
|
|
|
|
cnt = nbits;
|
|
|
|
|
|
|
|
vk = cnt;
|
|
|
|
|
|
|
|
while ((cnt == 32) && (BitStream_GetRemainingLength(bs) > 0))
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
BitStream_Shift32(bs);
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
cnt = lzcnt_s(bs->accumulator);
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
nbits = BitStream_GetRemainingLength(bs);
|
|
|
|
|
|
|
|
if (cnt > nbits)
|
|
|
|
cnt = nbits;
|
|
|
|
|
|
|
|
vk += cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitStream_Shift(bs, (vk % 32));
|
|
|
|
|
|
|
|
if (BitStream_GetRemainingLength(bs) < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
BitStream_Shift(bs, 1);
|
|
|
|
|
|
|
|
while (vk--)
|
|
|
|
{
|
|
|
|
run += (1 << k); /* add (1 << k) to run length */
|
|
|
|
|
|
|
|
/* update k, kp params */
|
|
|
|
|
|
|
|
kp += UP_GR;
|
|
|
|
|
|
|
|
if (kp > KPMAX)
|
|
|
|
kp = KPMAX;
|
|
|
|
|
|
|
|
k = kp >> LSGR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* next k bits contain run length remainder */
|
|
|
|
|
|
|
|
if (BitStream_GetRemainingLength(bs) < k)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bs->mask = ((1 << k) - 1);
|
|
|
|
run += ((bs->accumulator >> (32 - k)) & bs->mask);
|
|
|
|
BitStream_Shift(bs, k);
|
|
|
|
|
|
|
|
/* read sign bit */
|
|
|
|
|
|
|
|
if (BitStream_GetRemainingLength(bs) < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sign = (bs->accumulator & 0x80000000) ? 1 : 0;
|
|
|
|
BitStream_Shift(bs, 1);
|
|
|
|
|
|
|
|
/* count number of leading 1s */
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
cnt = lzcnt_s(~(bs->accumulator));
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
nbits = BitStream_GetRemainingLength(bs);
|
|
|
|
|
|
|
|
if (cnt > nbits)
|
|
|
|
cnt = nbits;
|
|
|
|
|
|
|
|
vk = cnt;
|
|
|
|
|
|
|
|
while ((cnt == 32) && (BitStream_GetRemainingLength(bs) > 0))
|
|
|
|
{
|
|
|
|
BitStream_Shift32(bs);
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
cnt = lzcnt_s(~(bs->accumulator));
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
nbits = BitStream_GetRemainingLength(bs);
|
|
|
|
|
|
|
|
if (cnt > nbits)
|
|
|
|
cnt = nbits;
|
|
|
|
|
|
|
|
vk += cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitStream_Shift(bs, (vk % 32));
|
|
|
|
|
|
|
|
if (BitStream_GetRemainingLength(bs) < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
BitStream_Shift(bs, 1);
|
|
|
|
|
|
|
|
/* next kr bits contain code remainder */
|
|
|
|
|
|
|
|
if (BitStream_GetRemainingLength(bs) < kr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bs->mask = ((1 << kr) - 1);
|
|
|
|
code = (UINT16) ((bs->accumulator >> (32 - kr)) & bs->mask);
|
|
|
|
BitStream_Shift(bs, kr);
|
|
|
|
|
|
|
|
/* add (vk << kr) to code */
|
|
|
|
|
|
|
|
code |= (vk << kr);
|
|
|
|
|
|
|
|
if (!vk)
|
|
|
|
{
|
|
|
|
/* update kr, krp params */
|
|
|
|
|
|
|
|
krp -= 2;
|
|
|
|
|
|
|
|
if (krp < 0)
|
|
|
|
krp = 0;
|
|
|
|
|
|
|
|
kr = krp >> LSGR;
|
|
|
|
}
|
|
|
|
else if (vk != 1)
|
|
|
|
{
|
|
|
|
/* update kr, krp params */
|
|
|
|
|
|
|
|
krp += vk;
|
|
|
|
|
|
|
|
if (krp > KPMAX)
|
|
|
|
krp = KPMAX;
|
|
|
|
|
|
|
|
kr = krp >> LSGR;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
/* update k, kp params */
|
|
|
|
|
|
|
|
kp -= DN_GR;
|
|
|
|
|
|
|
|
if (kp < 0)
|
|
|
|
kp = 0;
|
|
|
|
|
|
|
|
k = kp >> LSGR;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
/* compute magnitude from code */
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
if (sign)
|
|
|
|
mag = ((INT16) (code + 1)) * -1;
|
|
|
|
else
|
|
|
|
mag = (INT16) (code + 1);
|
|
|
|
|
|
|
|
/* write to output stream */
|
|
|
|
|
2019-02-07 16:17:35 +03:00
|
|
|
offset = (pOutput - pDstData);
|
2014-08-06 05:41:58 +04:00
|
|
|
size = run;
|
|
|
|
|
|
|
|
if ((offset + size) > DstSize)
|
|
|
|
size = DstSize - offset;
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
ZeroMemory(pOutput, size * sizeof(INT16));
|
|
|
|
pOutput += size;
|
|
|
|
}
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
if ((pOutput - pDstData) < DstSize)
|
|
|
|
{
|
|
|
|
*pOutput = mag;
|
|
|
|
pOutput++;
|
|
|
|
}
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
/* Golomb-Rice (GR) Mode */
|
|
|
|
|
|
|
|
/* count number of leading 1s */
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
cnt = lzcnt_s(~(bs->accumulator));
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
nbits = BitStream_GetRemainingLength(bs);
|
|
|
|
|
|
|
|
if (cnt > nbits)
|
|
|
|
cnt = nbits;
|
|
|
|
|
|
|
|
vk = cnt;
|
|
|
|
|
|
|
|
while ((cnt == 32) && (BitStream_GetRemainingLength(bs) > 0))
|
|
|
|
{
|
|
|
|
BitStream_Shift32(bs);
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
cnt = lzcnt_s(~(bs->accumulator));
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
nbits = BitStream_GetRemainingLength(bs);
|
|
|
|
|
|
|
|
if (cnt > nbits)
|
|
|
|
cnt = nbits;
|
|
|
|
|
|
|
|
vk += cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitStream_Shift(bs, (vk % 32));
|
|
|
|
|
|
|
|
if (BitStream_GetRemainingLength(bs) < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
BitStream_Shift(bs, 1);
|
|
|
|
|
|
|
|
/* next kr bits contain code remainder */
|
|
|
|
|
|
|
|
if (BitStream_GetRemainingLength(bs) < kr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bs->mask = ((1 << kr) - 1);
|
|
|
|
code = (UINT16) ((bs->accumulator >> (32 - kr)) & bs->mask);
|
|
|
|
BitStream_Shift(bs, kr);
|
|
|
|
|
|
|
|
/* add (vk << kr) to code */
|
|
|
|
|
|
|
|
code |= (vk << kr);
|
|
|
|
|
|
|
|
if (!vk)
|
|
|
|
{
|
|
|
|
/* update kr, krp params */
|
|
|
|
|
|
|
|
krp -= 2;
|
|
|
|
|
|
|
|
if (krp < 0)
|
|
|
|
krp = 0;
|
|
|
|
|
|
|
|
kr = krp >> LSGR;
|
|
|
|
}
|
|
|
|
else if (vk != 1)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
/* update kr, krp params */
|
|
|
|
|
|
|
|
krp += vk;
|
|
|
|
|
|
|
|
if (krp > KPMAX)
|
|
|
|
krp = KPMAX;
|
|
|
|
|
|
|
|
kr = krp >> LSGR;
|
|
|
|
}
|
|
|
|
|
2016-11-23 15:21:59 +03:00
|
|
|
if (mode == RLGR1) /* RLGR1 */
|
2014-08-06 05:41:58 +04:00
|
|
|
{
|
|
|
|
if (!code)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
/* update k, kp params */
|
|
|
|
|
|
|
|
kp += UQ_GR;
|
|
|
|
|
|
|
|
if (kp > KPMAX)
|
|
|
|
kp = KPMAX;
|
|
|
|
|
|
|
|
k = kp >> LSGR;
|
|
|
|
|
|
|
|
mag = 0;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
/* update k, kp params */
|
|
|
|
|
|
|
|
kp -= DQ_GR;
|
|
|
|
|
|
|
|
if (kp < 0)
|
|
|
|
kp = 0;
|
|
|
|
|
|
|
|
k = kp >> LSGR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* code = 2 * mag - sign
|
|
|
|
* sign + code = 2 * mag
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (code & 1)
|
|
|
|
mag = ((INT16) ((code + 1) >> 1)) * -1;
|
|
|
|
else
|
|
|
|
mag = (INT16) (code >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pOutput - pDstData) < DstSize)
|
|
|
|
{
|
|
|
|
*pOutput = mag;
|
|
|
|
pOutput++;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
2016-11-23 15:21:59 +03:00
|
|
|
else if (mode == RLGR3) /* RLGR3 */
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
nIdx = 0;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
if (code)
|
|
|
|
{
|
|
|
|
mag = (UINT32) code;
|
2014-08-12 02:48:42 +04:00
|
|
|
nIdx = 32 - lzcnt_s(mag);
|
2014-08-06 05:41:58 +04:00
|
|
|
}
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
if (BitStream_GetRemainingLength(bs) < nIdx)
|
|
|
|
break;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
bs->mask = ((1 << nIdx) - 1);
|
|
|
|
val1 = ((bs->accumulator >> (32 - nIdx)) & bs->mask);
|
|
|
|
BitStream_Shift(bs, nIdx);
|
|
|
|
|
|
|
|
val2 = code - val1;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
if (val1 && val2)
|
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
/* update k, kp params */
|
|
|
|
|
|
|
|
kp -= (2 * DQ_GR);
|
|
|
|
|
|
|
|
if (kp < 0)
|
|
|
|
kp = 0;
|
|
|
|
|
|
|
|
k = kp >> LSGR;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
else if (!val1 && !val2)
|
|
|
|
{
|
2014-08-06 05:41:58 +04:00
|
|
|
/* update k, kp params */
|
|
|
|
|
|
|
|
kp += (2 * UQ_GR);
|
|
|
|
|
|
|
|
if (kp > KPMAX)
|
|
|
|
kp = KPMAX;
|
|
|
|
|
|
|
|
k = kp >> LSGR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val1 & 1)
|
|
|
|
mag = ((INT16) ((val1 + 1) >> 1)) * -1;
|
|
|
|
else
|
|
|
|
mag = (INT16) (val1 >> 1);
|
|
|
|
|
|
|
|
if ((pOutput - pDstData) < DstSize)
|
|
|
|
{
|
|
|
|
*pOutput = mag;
|
|
|
|
pOutput++;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
if (val2 & 1)
|
|
|
|
mag = ((INT16) ((val2 + 1) >> 1)) * -1;
|
|
|
|
else
|
|
|
|
mag = (INT16) (val2 >> 1);
|
|
|
|
|
|
|
|
if ((pOutput - pDstData) < DstSize)
|
|
|
|
{
|
|
|
|
*pOutput = mag;
|
|
|
|
pOutput++;
|
|
|
|
}
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 16:17:35 +03:00
|
|
|
offset = (pOutput - pDstData);
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
if (offset < DstSize)
|
|
|
|
{
|
|
|
|
size = DstSize - offset;
|
|
|
|
ZeroMemory(pOutput, size * 2);
|
|
|
|
pOutput += size;
|
|
|
|
}
|
|
|
|
|
2019-02-07 16:17:35 +03:00
|
|
|
offset = (pOutput - pDstData);
|
2014-08-06 05:41:58 +04:00
|
|
|
|
|
|
|
if (offset != DstSize)
|
|
|
|
return -1;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2014-08-06 05:41:58 +04:00
|
|
|
return 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the next coefficient (a signed int) to encode, from the input stream */
|
|
|
|
#define GetNextInput(_n) \
|
|
|
|
{ \
|
|
|
|
if (data_size > 0) \
|
|
|
|
{ \
|
|
|
|
_n = *data++; \
|
|
|
|
data_size--; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
_n = 0; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit bitPattern to the output bitstream */
|
2011-09-14 19:47:47 +04:00
|
|
|
#define OutputBits(numBits, bitPattern) rfx_bitstream_put_bits(bs, bitPattern, numBits)
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* Emit a bit (0 or 1), count number of times, to the output bitstream */
|
|
|
|
#define OutputBit(count, bit) \
|
|
|
|
{ \
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 _b = (bit ? 0xFFFF : 0); \
|
2011-08-10 11:13:39 +04:00
|
|
|
int _c = (count); \
|
|
|
|
for (; _c > 0; _c -= 16) \
|
|
|
|
rfx_bitstream_put_bits(bs, _b, (_c > 16 ? 16 : _c)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Converts the input value to (2 * abs(input) - sign(input)), where sign(input) = (input < 0 ? 1 : 0) and returns it */
|
|
|
|
#define Get2MagSign(input) ((input) >= 0 ? 2 * (input) : -2 * (input) - 1)
|
|
|
|
|
|
|
|
/* Outputs the Golomb/Rice encoding of a non-negative integer */
|
|
|
|
#define CodeGR(krp, val) rfx_rlgr_code_gr(bs, krp, val)
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static void rfx_rlgr_code_gr(RFX_BITSTREAM* bs, int* krp, UINT32 val)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
|
|
|
int kr = *krp >> LSGR;
|
|
|
|
|
|
|
|
/* unary part of GR code */
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 vk = (val) >> kr;
|
2011-08-10 11:13:39 +04:00
|
|
|
OutputBit(vk, 1);
|
|
|
|
OutputBit(1, 0);
|
|
|
|
|
|
|
|
/* remainder part of GR code, if needed */
|
|
|
|
if (kr)
|
|
|
|
{
|
|
|
|
OutputBits(kr, val & ((1 << kr) - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update krp, only if it is not equal to 1 */
|
|
|
|
if (vk == 0)
|
|
|
|
{
|
|
|
|
UpdateParam(*krp, -2, kr);
|
|
|
|
}
|
|
|
|
else if (vk > 1)
|
|
|
|
{
|
|
|
|
UpdateParam(*krp, vk, kr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 15:21:59 +03:00
|
|
|
int rfx_rlgr_encode(RLGR_MODE mode, const INT16* data, UINT32 data_size, BYTE* buffer, UINT32 buffer_size)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
|
|
|
int k;
|
|
|
|
int kp;
|
|
|
|
int krp;
|
|
|
|
RFX_BITSTREAM* bs;
|
|
|
|
int processed_size;
|
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!(bs = (RFX_BITSTREAM*) calloc(1, sizeof(RFX_BITSTREAM))))
|
|
|
|
return 0;
|
2012-11-22 05:21:08 +04:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
rfx_bitstream_attach(bs, buffer, buffer_size);
|
|
|
|
|
|
|
|
/* initialize the parameters */
|
|
|
|
k = 1;
|
|
|
|
kp = 1 << LSGR;
|
|
|
|
krp = 1 << LSGR;
|
|
|
|
|
|
|
|
/* process all the input coefficients */
|
|
|
|
while (data_size > 0)
|
|
|
|
{
|
|
|
|
int input;
|
|
|
|
|
|
|
|
if (k)
|
|
|
|
{
|
|
|
|
int numZeros;
|
|
|
|
int runmax;
|
|
|
|
int mag;
|
|
|
|
int sign;
|
|
|
|
|
|
|
|
/* RUN-LENGTH MODE */
|
|
|
|
|
|
|
|
/* collect the run of zeros in the input stream */
|
|
|
|
numZeros = 0;
|
|
|
|
GetNextInput(input);
|
|
|
|
while (input == 0 && data_size > 0)
|
|
|
|
{
|
|
|
|
numZeros++;
|
|
|
|
GetNextInput(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
// emit output zeros
|
|
|
|
runmax = 1 << k;
|
|
|
|
while (numZeros >= runmax)
|
|
|
|
{
|
|
|
|
OutputBit(1, 0); /* output a zero bit */
|
|
|
|
numZeros -= runmax;
|
|
|
|
UpdateParam(kp, UP_GR, k); /* update kp, k */
|
|
|
|
runmax = 1 << k;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a 1 to terminate runs */
|
|
|
|
OutputBit(1, 1);
|
|
|
|
|
|
|
|
/* output the remaining run length using k bits */
|
|
|
|
OutputBits(k, numZeros);
|
|
|
|
|
2011-08-24 17:33:15 +04:00
|
|
|
/* note: when we reach here and the last byte being encoded is 0, we still
|
|
|
|
need to output the last two bits, otherwise mstsc will crash */
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2011-08-24 17:33:15 +04:00
|
|
|
/* encode the nonzero value using GR coding */
|
|
|
|
mag = (input < 0 ? -input : input); /* absolute value of input coefficient */
|
|
|
|
sign = (input < 0 ? 1 : 0); /* sign of input coefficient */
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2011-08-24 17:33:15 +04:00
|
|
|
OutputBit(1, sign); /* output the sign bit */
|
|
|
|
CodeGR(&krp, mag ? mag - 1 : 0); /* output GR code for (mag - 1) */
|
|
|
|
|
|
|
|
UpdateParam(kp, -DN_GR, k);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* GOLOMB-RICE MODE */
|
|
|
|
|
|
|
|
if (mode == RLGR1)
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 twoMs;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* RLGR1 variant */
|
|
|
|
|
|
|
|
/* convert input to (2*magnitude - sign), encode using GR code */
|
|
|
|
GetNextInput(input);
|
|
|
|
twoMs = Get2MagSign(input);
|
|
|
|
CodeGR(&krp, twoMs);
|
|
|
|
|
|
|
|
/* update k, kp */
|
2011-08-24 14:02:34 +04:00
|
|
|
/* NOTE: as of Aug 2011, the algorithm is still wrongly documented
|
|
|
|
and the update direction is reversed */
|
2011-08-10 11:13:39 +04:00
|
|
|
if (twoMs)
|
|
|
|
{
|
2011-08-24 14:02:34 +04:00
|
|
|
UpdateParam(kp, -DQ_GR, k);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-24 14:02:34 +04:00
|
|
|
UpdateParam(kp, UQ_GR, k);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* mode == RLGR3 */
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 twoMs1;
|
|
|
|
UINT32 twoMs2;
|
|
|
|
UINT32 sum2Ms;
|
|
|
|
UINT32 nIdx;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* RLGR3 variant */
|
|
|
|
|
|
|
|
/* convert the next two input values to (2*magnitude - sign) and */
|
|
|
|
/* encode their sum using GR code */
|
|
|
|
|
|
|
|
GetNextInput(input);
|
|
|
|
twoMs1 = Get2MagSign(input);
|
|
|
|
GetNextInput(input);
|
|
|
|
twoMs2 = Get2MagSign(input);
|
|
|
|
sum2Ms = twoMs1 + twoMs2;
|
|
|
|
|
|
|
|
CodeGR(&krp, sum2Ms);
|
|
|
|
|
|
|
|
/* encode binary representation of the first input (twoMs1). */
|
|
|
|
GetMinBits(sum2Ms, nIdx);
|
|
|
|
OutputBits(nIdx, twoMs1);
|
|
|
|
|
|
|
|
/* update k,kp for the two input values */
|
|
|
|
|
|
|
|
if (twoMs1 && twoMs2)
|
|
|
|
{
|
|
|
|
UpdateParam(kp, -2 * DQ_GR, k);
|
|
|
|
}
|
|
|
|
else if (!twoMs1 && !twoMs2)
|
|
|
|
{
|
|
|
|
UpdateParam(kp, 2 * UQ_GR, k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 11:59:56 +03:00
|
|
|
rfx_bitstream_flush(bs);
|
2011-08-10 11:13:39 +04:00
|
|
|
processed_size = rfx_bitstream_get_processed_bytes(bs);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(bs);
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
return processed_size;
|
|
|
|
}
|