23 #include <freerdp/config.h>
29 #include <winpr/assert.h>
30 #include <winpr/cast.h>
31 #include <winpr/crt.h>
33 #include <freerdp/codec/nsc.h>
34 #include <freerdp/codec/color.h>
36 #include "nsc_types.h"
37 #include "nsc_encode.h"
39 #include "sse/nsc_sse2.h"
40 #include "neon/nsc_neon.h"
42 #include <freerdp/log.h>
44 static BOOL nsc_decode(NSC_CONTEXT* WINPR_RESTRICT context)
51 const UINT16 rw = ROUND_UP_TO(context->width, 8);
52 WINPR_ASSERT(context->ColorLossLevel >= 1);
53 const BYTE shift = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel -
55 BYTE* bmpdata = context->BitmapData;
60 for (
size_t y = 0; y < context->height; y++)
62 const BYTE* yplane = NULL;
63 const BYTE* coplane = NULL;
64 const BYTE* cgplane = NULL;
65 const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width;
67 if (context->ChromaSubsamplingLevel)
69 yplane = context->priv->PlaneBuffers[0] + y * rw;
70 coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1);
71 cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1);
75 yplane = context->priv->PlaneBuffers[0] + y * context->width;
76 coplane = context->priv->PlaneBuffers[1] + y * context->width;
77 cgplane = context->priv->PlaneBuffers[2] + y * context->width;
80 for (UINT32 x = 0; x < context->width; x++)
82 INT16 y_val = (INT16)*yplane;
83 INT16 co_val = (INT16)(INT8)(((INT16)*coplane) << shift);
84 INT16 cg_val = (INT16)(INT8)(((INT16)*cgplane) << shift);
85 INT16 r_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + co_val - cg_val);
86 INT16 g_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + cg_val);
87 INT16 b_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val - co_val - cg_val);
89 if (pos + 4 > context->BitmapDataLength)
93 *bmpdata++ = MINMAX(b_val, 0, 0xFF);
94 *bmpdata++ = MINMAX(g_val, 0, 0xFF);
95 *bmpdata++ = MINMAX(r_val, 0, 0xFF);
98 coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
99 cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
107 static BOOL nsc_rle_decode(
const BYTE* WINPR_RESTRICT in,
size_t inSize, BYTE* WINPR_RESTRICT out,
108 UINT32 outSize, UINT32 originalSize)
110 UINT32 left = originalSize;
118 const BYTE value = *in++;
132 else if (value == *in)
151 len = ((UINT32)(*in++));
152 len |= ((UINT32)(*in++)) << 8U;
153 len |= ((UINT32)(*in++)) << 16U;
154 len |= ((UINT32)(*in++)) << 24U;
157 if ((outSize < len) || (left < len))
161 FillMemory(out, len, value);
176 if ((outSize < 4) || (left < 4))
185 static BOOL nsc_rle_decompress_data(NSC_CONTEXT* WINPR_RESTRICT context)
190 const BYTE* rle = context->Planes;
191 size_t rleSize = context->PlanesSize;
194 for (
size_t i = 0; i < 4; i++)
196 const UINT32 originalSize = context->OrgByteCount[i];
197 const UINT32 planeSize = context->PlaneByteCount[i];
199 if (rleSize < planeSize)
204 if (context->priv->PlaneBuffersLength < originalSize)
207 FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
209 else if (planeSize < originalSize)
211 if (!nsc_rle_decode(rle, rleSize, context->priv->PlaneBuffers[i],
212 context->priv->PlaneBuffersLength, originalSize))
217 if (context->priv->PlaneBuffersLength < originalSize)
220 if (rleSize < originalSize)
223 CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
227 rleSize -= planeSize;
233 static BOOL nsc_stream_initialize(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s)
235 WINPR_ASSERT(context);
236 WINPR_ASSERT(context->priv);
237 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 20))
241 for (
size_t i = 0; i < 4; i++)
243 Stream_Read_UINT32(s, context->PlaneByteCount[i]);
244 total += context->PlaneByteCount[i];
247 Stream_Read_UINT8(s, context->ColorLossLevel);
248 if ((context->ColorLossLevel < 1) || (context->ColorLossLevel > 7))
250 WLog_Print(context->priv->log, WLOG_ERROR,
251 "ColorLossLevel=%" PRIu8
" out of range, must be [1,7] inclusive",
252 context->ColorLossLevel);
255 Stream_Read_UINT8(s, context->ChromaSubsamplingLevel);
257 context->Planes = Stream_Pointer(s);
258 context->PlanesSize = total;
259 return Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, total);
262 static BOOL nsc_context_initialize(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s)
264 if (!nsc_stream_initialize(context, s))
267 const size_t blength = 4ull * context->width * context->height;
269 if (!context->BitmapData || (blength > context->BitmapDataLength))
271 void* tmp = winpr_aligned_recalloc(context->BitmapData, blength + 16,
sizeof(BYTE), 32);
276 context->BitmapData = tmp;
277 context->BitmapDataLength = blength;
280 const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
281 const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
283 const size_t plength = 1ull * tempWidth * tempHeight;
284 if (plength > UINT32_MAX)
287 if (plength > context->priv->PlaneBuffersLength)
289 for (
size_t i = 0; i < 4; i++)
291 void* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], plength,
297 context->priv->PlaneBuffers[i] = tmp;
300 context->priv->PlaneBuffersLength = (UINT32)plength;
303 for (
size_t i = 0; i < 4; i++)
304 context->OrgByteCount[i] = context->width * context->height;
306 if (context->ChromaSubsamplingLevel)
308 context->OrgByteCount[0] = tempWidth * context->height;
309 context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
310 context->OrgByteCount[2] = context->OrgByteCount[1];
320 PROFILER_PRINT_HEADER
321 PROFILER_PRINT(priv->prof_nsc_rle_decompress_data)
322 PROFILER_PRINT(priv->prof_nsc_decode)
323 PROFILER_PRINT(priv->prof_nsc_rle_compress_data)
324 PROFILER_PRINT(priv->prof_nsc_encode)
325 PROFILER_PRINT_FOOTER
328 BOOL nsc_context_reset(NSC_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
333 if ((width > UINT16_MAX) || (height > UINT16_MAX))
336 context->width = (UINT16)width;
337 context->height = (UINT16)height;
341 NSC_CONTEXT* nsc_context_new(
void)
343 NSC_CONTEXT* context = (NSC_CONTEXT*)winpr_aligned_calloc(1,
sizeof(NSC_CONTEXT), 32);
353 context->priv->log = WLog_Get(
"com.freerdp.codec.nsc");
354 WLog_OpenAppender(context->priv->log);
355 context->BitmapData = NULL;
356 context->decode = nsc_decode;
357 context->encode = nsc_encode;
359 PROFILER_CREATE(context->priv->prof_nsc_rle_decompress_data,
"nsc_rle_decompress_data")
360 PROFILER_CREATE(context->priv->prof_nsc_decode, "nsc_decode")
361 PROFILER_CREATE(context->priv->prof_nsc_rle_compress_data, "nsc_rle_compress_data")
362 PROFILER_CREATE(context->priv->prof_nsc_encode, "nsc_encode")
364 context->ColorLossLevel = 3;
365 context->ChromaSubsamplingLevel = 1;
367 nsc_init_sse2(context);
368 nsc_init_neon(context);
371 WINPR_PRAGMA_DIAG_PUSH
372 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
373 nsc_context_free(context);
374 WINPR_PRAGMA_DIAG_POP
378 void nsc_context_free(NSC_CONTEXT* context)
385 for (
size_t i = 0; i < 5; i++)
386 winpr_aligned_free(context->priv->PlaneBuffers[i]);
388 nsc_profiler_print(context->priv);
389 PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
390 PROFILER_FREE(context->priv->prof_nsc_decode)
391 PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
392 PROFILER_FREE(context->priv->prof_nsc_encode)
393 winpr_aligned_free(context->priv);
396 winpr_aligned_free(context->BitmapData);
397 winpr_aligned_free(context);
400 #if defined(WITH_FREERDP_DEPRECATED)
401 BOOL nsc_context_set_pixel_format(NSC_CONTEXT* context, UINT32 pixel_format)
403 return nsc_context_set_parameters(context, NSC_COLOR_FORMAT, pixel_format);
407 BOOL nsc_context_set_parameters(NSC_CONTEXT* WINPR_RESTRICT context, NSC_PARAMETER what,
415 case NSC_COLOR_LOSS_LEVEL:
416 context->ColorLossLevel = value;
418 case NSC_ALLOW_SUBSAMPLING:
419 context->ChromaSubsamplingLevel = value;
421 case NSC_DYNAMIC_COLOR_FIDELITY:
422 context->DynamicColorFidelity = value != 0;
424 case NSC_COLOR_FORMAT:
425 context->format = value;
433 BOOL nsc_process_message(NSC_CONTEXT* WINPR_RESTRICT context, UINT16 bpp, UINT32 width,
434 UINT32 height,
const BYTE* data, UINT32 length,
435 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStride,
436 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 flip)
441 if (!context || !data || !pDstData)
444 s = Stream_StaticConstInit(&sbuffer, data, length);
450 nDstStride = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
455 context->format = PIXEL_FORMAT_BGRA32;
459 context->format = PIXEL_FORMAT_BGR24;
463 context->format = PIXEL_FORMAT_BGR16;
467 context->format = PIXEL_FORMAT_RGB8;
471 context->format = PIXEL_FORMAT_A4;
478 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
479 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
480 ret = nsc_context_initialize(context, s);
488 PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
489 rc = nsc_rle_decompress_data(context);
490 PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
498 PROFILER_ENTER(context->priv->prof_nsc_decode)
499 rc = context->decode(context);
500 PROFILER_EXIT(context->priv->prof_nsc_decode)
506 if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStride, nXDst, nYDst, width, height,
507 context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0, 0, NULL,