Files
FreeRDP/libfreerdp/codec/nsc.c

512 lines
12 KiB
C
Raw Normal View History

2011-10-03 00:56:33 +05:30
/**
2012-10-08 23:02:04 -04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* NSCodec Codec
*
* Copyright 2011 Samsung, Author Jiten Pathy
* Copyright 2012 Vic Lee
* Copyright 2016 Armin Novak <armin.novak@thincast.com>
* Copyright 2016 Thincast Technologies GmbH
*
* 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.
*/
2022-02-16 11:20:38 +01:00
#include <freerdp/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/crt.h>
#include <freerdp/codec/nsc.h>
#include <freerdp/codec/color.h>
2011-10-03 00:56:33 +05:30
#include "nsc_types.h"
2012-03-16 12:04:38 +08:00
#include "nsc_encode.h"
#include "sse/nsc_sse2.h"
#include "neon/nsc_neon.h"
#include <freerdp/log.h>
static BOOL nsc_decode(NSC_CONTEXT* WINPR_RESTRICT context)
2011-10-03 00:56:33 +05:30
{
UINT16 rw = 0;
BYTE shift = 0;
BYTE* bmpdata = NULL;
size_t pos = 0;
if (!context)
return FALSE;
rw = ROUND_UP_TO(context->width, 8);
shift = context->ColorLossLevel - 1; /* colorloss recovery + YCoCg shift */
bmpdata = context->BitmapData;
if (!bmpdata)
return FALSE;
for (size_t y = 0; y < context->height; y++)
2011-10-03 00:56:33 +05:30
{
const BYTE* yplane = NULL;
const BYTE* coplane = NULL;
const BYTE* cgplane = NULL;
2016-11-22 12:12:08 +01:00
const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width; /* A */
2016-04-11 14:14:17 +02:00
2014-09-23 20:00:26 -04:00
if (context->ChromaSubsamplingLevel)
2011-10-03 00:56:33 +05:30
{
2019-11-06 15:24:51 +01:00
yplane = context->priv->PlaneBuffers[0] + y * rw; /* Y */
coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
2011-10-03 00:56:33 +05:30
}
else
2011-10-03 00:56:33 +05:30
{
2019-11-06 15:24:51 +01:00
yplane = context->priv->PlaneBuffers[0] + y * context->width; /* Y */
coplane = context->priv->PlaneBuffers[1] + y * context->width; /* Co */
cgplane = context->priv->PlaneBuffers[2] + y * context->width; /* Cg */
2011-10-03 00:56:33 +05:30
}
for (UINT32 x = 0; x < context->width; x++)
2011-10-03 00:56:33 +05:30
{
2019-11-06 15:24:51 +01:00
INT16 y_val = (INT16)*yplane;
INT16 co_val = (INT16)(INT8)(((INT16)*coplane) << shift);
INT16 cg_val = (INT16)(INT8)(((INT16)*cgplane) << shift);
2016-04-11 14:14:17 +02:00
INT16 r_val = y_val + co_val - cg_val;
INT16 g_val = y_val + cg_val;
INT16 b_val = y_val - co_val - cg_val;
if (pos + 4 > context->BitmapDataLength)
return FALSE;
pos += 4;
*bmpdata++ = MINMAX(b_val, 0, 0xFF);
*bmpdata++ = MINMAX(g_val, 0, 0xFF);
*bmpdata++ = MINMAX(r_val, 0, 0xFF);
*bmpdata++ = *aplane;
yplane++;
2014-09-23 20:00:26 -04:00
coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
aplane++;
2011-10-03 00:56:33 +05:30
}
}
return TRUE;
2011-10-03 00:56:33 +05:30
}
2024-04-26 09:11:12 +02:00
static BOOL nsc_rle_decode(const BYTE* WINPR_RESTRICT in, size_t inSize, BYTE* WINPR_RESTRICT out,
UINT32 outSize, UINT32 originalSize)
2011-10-03 00:56:33 +05:30
{
UINT32 left = originalSize;
while (left > 4)
2011-10-03 00:56:33 +05:30
{
if (inSize < 1)
return FALSE;
inSize--;
const BYTE value = *in++;
UINT32 len = 0;
if (left == 5)
2011-10-03 00:56:33 +05:30
{
if (outSize < 1)
return FALSE;
outSize--;
*out++ = value;
left--;
2011-10-03 00:56:33 +05:30
}
else if (inSize < 1)
return FALSE;
else if (value == *in)
2011-10-03 00:56:33 +05:30
{
inSize--;
in++;
if (inSize < 1)
return FALSE;
else if (*in < 0xFF)
2011-10-03 00:56:33 +05:30
{
inSize--;
2019-11-06 15:24:51 +01:00
len = (UINT32)*in++;
len += 2;
2011-10-03 00:56:33 +05:30
}
else
{
if (inSize < 5)
return FALSE;
inSize -= 5;
in++;
len = ((UINT32)(*in++));
len |= ((UINT32)(*in++)) << 8U;
len |= ((UINT32)(*in++)) << 16U;
len |= ((UINT32)(*in++)) << 24U;
2011-10-03 00:56:33 +05:30
}
if ((outSize < len) || (left < len))
return FALSE;
outSize -= len;
FillMemory(out, len, value);
2012-03-07 11:42:37 +08:00
out += len;
left -= len;
2011-10-03 00:56:33 +05:30
}
else
{
if (outSize < 1)
return FALSE;
outSize--;
*out++ = value;
left--;
2011-10-03 00:56:33 +05:30
}
}
if ((outSize < 4) || (left < 4))
return FALSE;
if (inSize < 4)
return FALSE;
memcpy(out, in, 4);
return TRUE;
2011-10-03 00:56:33 +05:30
}
static BOOL nsc_rle_decompress_data(NSC_CONTEXT* WINPR_RESTRICT context)
2011-10-03 00:56:33 +05:30
{
if (!context)
return FALSE;
const BYTE* rle = context->Planes;
size_t rleSize = context->PlanesSize;
WINPR_ASSERT(rle);
for (size_t i = 0; i < 4; i++)
2011-10-03 00:56:33 +05:30
{
const UINT32 originalSize = context->OrgByteCount[i];
const UINT32 planeSize = context->PlaneByteCount[i];
if (rleSize < planeSize)
return FALSE;
if (planeSize == 0)
{
if (context->priv->PlaneBuffersLength < originalSize)
return FALSE;
FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
}
else if (planeSize < originalSize)
{
if (!nsc_rle_decode(rle, rleSize, context->priv->PlaneBuffers[i],
2019-11-06 15:24:51 +01:00
context->priv->PlaneBuffersLength, originalSize))
return FALSE;
}
2011-10-03 00:56:33 +05:30
else
{
if (context->priv->PlaneBuffersLength < originalSize)
return FALSE;
if (rleSize < originalSize)
return FALSE;
CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
}
rle += planeSize;
rleSize -= planeSize;
2011-10-03 00:56:33 +05:30
}
return TRUE;
2011-10-03 00:56:33 +05:30
}
static BOOL nsc_stream_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
2011-10-03 00:56:33 +05:30
{
WINPR_ASSERT(context);
WINPR_ASSERT(context->priv);
if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 20))
return FALSE;
size_t total = 0;
for (size_t i = 0; i < 4; i++)
{
2014-09-23 20:00:26 -04:00
Stream_Read_UINT32(s, context->PlaneByteCount[i]);
total += context->PlaneByteCount[i];
}
2019-11-06 15:24:51 +01:00
Stream_Read_UINT8(s, context->ColorLossLevel); /* ColorLossLevel (1 byte) */
if ((context->ColorLossLevel < 1) || (context->ColorLossLevel > 7))
{
WLog_Print(context->priv->log, WLOG_ERROR,
"ColorLossLevel=%" PRIu8 " out of range, must be [1,7] inclusive",
context->ColorLossLevel);
return FALSE;
}
2019-11-06 15:24:51 +01:00
Stream_Read_UINT8(s, context->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
Stream_Seek(s, 2); /* Reserved (2 bytes) */
2014-09-23 20:00:26 -04:00
context->Planes = Stream_Pointer(s);
context->PlanesSize = total;
return Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, total);
2011-10-03 00:56:33 +05:30
}
static BOOL nsc_context_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
2011-10-03 00:56:33 +05:30
{
if (!nsc_stream_initialize(context, s))
return FALSE;
const size_t blength = 4ull * context->width * context->height;
if (!context->BitmapData || (blength > context->BitmapDataLength))
{
void* tmp = winpr_aligned_recalloc(context->BitmapData, blength + 16, sizeof(BYTE), 32);
if (!tmp)
return FALSE;
context->BitmapData = tmp;
context->BitmapDataLength = blength;
}
const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
/* The maximum length a decoded plane can reach in all cases */
const size_t plength = 1ull * tempWidth * tempHeight;
2024-10-14 15:50:38 +02:00
if (plength > UINT32_MAX)
return FALSE;
if (plength > context->priv->PlaneBuffersLength)
{
for (size_t i = 0; i < 4; i++)
{
void* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], plength,
2023-03-24 12:15:06 +01:00
sizeof(BYTE), 32);
if (!tmp)
return FALSE;
context->priv->PlaneBuffers[i] = tmp;
}
2024-10-14 15:50:38 +02:00
context->priv->PlaneBuffersLength = (UINT32)plength;
}
for (size_t i = 0; i < 4; i++)
context->OrgByteCount[i] = context->width * context->height;
2014-09-23 20:00:26 -04:00
if (context->ChromaSubsamplingLevel)
2011-10-03 00:56:33 +05:30
{
context->OrgByteCount[0] = tempWidth * context->height;
context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
context->OrgByteCount[2] = context->OrgByteCount[1];
2011-10-03 00:56:33 +05:30
}
return TRUE;
2011-10-03 00:56:33 +05:30
}
static void nsc_profiler_print(NSC_CONTEXT_PRIV* WINPR_RESTRICT priv)
2022-06-23 07:57:38 +02:00
{
WINPR_UNUSED(priv);
PROFILER_PRINT_HEADER
PROFILER_PRINT(priv->prof_nsc_rle_decompress_data)
PROFILER_PRINT(priv->prof_nsc_decode)
PROFILER_PRINT(priv->prof_nsc_rle_compress_data)
PROFILER_PRINT(priv->prof_nsc_encode)
PROFILER_PRINT_FOOTER
2012-03-06 22:42:57 +08:00
}
BOOL nsc_context_reset(NSC_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
{
if (!context)
return FALSE;
2019-10-04 10:13:43 +02:00
if ((width > UINT16_MAX) || (height > UINT16_MAX))
return FALSE;
context->width = (UINT16)width;
context->height = (UINT16)height;
return TRUE;
}
NSC_CONTEXT* nsc_context_new(void)
{
2023-03-24 12:15:06 +01:00
NSC_CONTEXT* context = (NSC_CONTEXT*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT), 32);
if (!context)
return NULL;
2023-03-24 12:15:06 +01:00
context->priv = (NSC_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT_PRIV), 32);
if (!context->priv)
goto error;
context->priv->log = WLog_Get("com.freerdp.codec.nsc");
WLog_OpenAppender(context->priv->log);
context->BitmapData = NULL;
context->decode = nsc_decode;
context->encode = nsc_encode;
2019-11-06 15:24:51 +01:00
PROFILER_CREATE(context->priv->prof_nsc_rle_decompress_data, "nsc_rle_decompress_data")
PROFILER_CREATE(context->priv->prof_nsc_decode, "nsc_decode")
2019-11-06 15:24:51 +01:00
PROFILER_CREATE(context->priv->prof_nsc_rle_compress_data, "nsc_rle_compress_data")
PROFILER_CREATE(context->priv->prof_nsc_encode, "nsc_encode")
/* Default encoding parameters */
2014-09-23 20:00:26 -04:00
context->ColorLossLevel = 3;
context->ChromaSubsamplingLevel = 1;
/* init optimized methods */
nsc_init_sse2(context);
nsc_init_neon(context);
return context;
error:
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
nsc_context_free(context);
WINPR_PRAGMA_DIAG_POP
return NULL;
}
void nsc_context_free(NSC_CONTEXT* context)
2011-10-03 00:56:33 +05:30
{
if (!context)
return;
if (context->priv)
{
2023-03-24 12:15:06 +01:00
for (size_t i = 0; i < 5; i++)
winpr_aligned_free(context->priv->PlaneBuffers[i]);
nsc_profiler_print(context->priv);
PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
PROFILER_FREE(context->priv->prof_nsc_decode)
PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
PROFILER_FREE(context->priv->prof_nsc_encode)
2023-03-24 12:15:06 +01:00
winpr_aligned_free(context->priv);
}
2023-03-24 12:15:06 +01:00
winpr_aligned_free(context->BitmapData);
winpr_aligned_free(context);
}
#if defined(WITH_FREERDP_DEPRECATED)
BOOL nsc_context_set_pixel_format(NSC_CONTEXT* context, UINT32 pixel_format)
2019-10-04 10:13:43 +02:00
{
return nsc_context_set_parameters(context, NSC_COLOR_FORMAT, pixel_format);
}
#endif
2019-10-04 10:13:43 +02:00
BOOL nsc_context_set_parameters(NSC_CONTEXT* WINPR_RESTRICT context, NSC_PARAMETER what,
UINT32 value)
2012-03-15 16:55:29 +08:00
{
2017-01-10 10:23:49 +01:00
if (!context)
return FALSE;
2019-11-06 15:24:51 +01:00
switch (what)
2019-10-04 10:13:43 +02:00
{
2019-11-06 15:24:51 +01:00
case NSC_COLOR_LOSS_LEVEL:
context->ColorLossLevel = value;
break;
case NSC_ALLOW_SUBSAMPLING:
context->ChromaSubsamplingLevel = value;
break;
case NSC_DYNAMIC_COLOR_FIDELITY:
context->DynamicColorFidelity = value != 0;
break;
case NSC_COLOR_FORMAT:
context->format = value;
break;
default:
return FALSE;
2019-10-04 10:13:43 +02:00
}
return TRUE;
}
BOOL nsc_process_message(NSC_CONTEXT* WINPR_RESTRICT context, UINT16 bpp, UINT32 width,
UINT32 height, const BYTE* data, UINT32 length,
2024-04-26 09:11:12 +02:00
BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStride,
UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 flip)
{
wStream* s = NULL;
2022-04-27 21:02:18 +02:00
wStream sbuffer = { 0 };
BOOL ret = 0;
2019-10-04 10:13:43 +02:00
if (!context || !data || !pDstData)
return FALSE;
2022-04-27 21:02:18 +02:00
s = Stream_StaticConstInit(&sbuffer, data, length);
if (!s)
return FALSE;
2016-04-11 14:14:17 +02:00
if (nDstStride == 0)
nDstStride = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
2016-04-11 14:14:17 +02:00
switch (bpp)
2012-03-15 16:55:29 +08:00
{
case 32:
context->format = PIXEL_FORMAT_BGRA32;
2012-03-15 16:55:29 +08:00
break;
case 24:
context->format = PIXEL_FORMAT_BGR24;
2012-03-15 16:55:29 +08:00
break;
case 16:
context->format = PIXEL_FORMAT_BGR16;
2012-03-15 16:55:29 +08:00
break;
case 8:
context->format = PIXEL_FORMAT_RGB8;
2012-03-15 16:55:29 +08:00
break;
case 4:
context->format = PIXEL_FORMAT_A4;
2012-03-15 16:55:29 +08:00
break;
2012-03-15 16:55:29 +08:00
default:
return FALSE;
2012-03-15 16:55:29 +08:00
}
context->width = width;
context->height = height;
ret = nsc_context_initialize(context, s);
2011-10-03 00:56:33 +05:30
if (!ret)
return FALSE;
2011-10-03 00:56:33 +05:30
/* RLE decode */
{
BOOL rc = 0;
PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
rc = nsc_rle_decompress_data(context);
PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
if (!rc)
return FALSE;
}
/* Colorloss recover, Chroma supersample and AYCoCg to ARGB Conversion in one step */
{
BOOL rc = 0;
PROFILER_ENTER(context->priv->prof_nsc_decode)
rc = context->decode(context);
PROFILER_EXIT(context->priv->prof_nsc_decode)
if (!rc)
return FALSE;
}
2016-04-11 14:14:17 +02:00
if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStride, nXDst, nYDst, width, height,
context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0, 0, NULL,
flip))
2016-04-11 14:14:17 +02:00
return FALSE;
return TRUE;
2011-10-03 00:56:33 +05:30
}