23 #include <freerdp/config.h>
29 #include <winpr/crt.h>
31 #include <freerdp/codec/nsc.h>
32 #include <freerdp/codec/color.h>
34 #include "nsc_types.h"
35 #include "nsc_encode.h"
37 #include "sse/nsc_sse2.h"
38 #include "neon/nsc_neon.h"
40 #include <freerdp/log.h>
42 static BOOL nsc_decode(NSC_CONTEXT* WINPR_RESTRICT context)
52 rw = ROUND_UP_TO(context->width, 8);
53 shift = context->ColorLossLevel - 1;
54 bmpdata = context->BitmapData;
59 for (
size_t y = 0; y < context->height; y++)
61 const BYTE* yplane = NULL;
62 const BYTE* coplane = NULL;
63 const BYTE* cgplane = NULL;
64 const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width;
66 if (context->ChromaSubsamplingLevel)
68 yplane = context->priv->PlaneBuffers[0] + y * rw;
69 coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1);
70 cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1);
74 yplane = context->priv->PlaneBuffers[0] + y * context->width;
75 coplane = context->priv->PlaneBuffers[1] + y * context->width;
76 cgplane = context->priv->PlaneBuffers[2] + y * context->width;
79 for (UINT32 x = 0; x < context->width; x++)
81 INT16 y_val = (INT16)*yplane;
82 INT16 co_val = (INT16)(INT8)(((INT16)*coplane) << shift);
83 INT16 cg_val = (INT16)(INT8)(((INT16)*cgplane) << shift);
84 INT16 r_val = y_val + co_val - cg_val;
85 INT16 g_val = y_val + cg_val;
86 INT16 b_val = y_val - co_val - cg_val;
88 if (pos + 4 > context->BitmapDataLength)
92 *bmpdata++ = MINMAX(b_val, 0, 0xFF);
93 *bmpdata++ = MINMAX(g_val, 0, 0xFF);
94 *bmpdata++ = MINMAX(r_val, 0, 0xFF);
97 coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
98 cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
106 static BOOL nsc_rle_decode(
const BYTE* WINPR_RESTRICT in,
size_t inSize, BYTE* WINPR_RESTRICT out,
107 UINT32 outSize, UINT32 originalSize)
109 UINT32 left = originalSize;
117 const BYTE value = *in++;
131 else if (value == *in)
150 len = ((UINT32)(*in++));
151 len |= ((UINT32)(*in++)) << 8U;
152 len |= ((UINT32)(*in++)) << 16U;
153 len |= ((UINT32)(*in++)) << 24U;
156 if ((outSize < len) || (left < len))
160 FillMemory(out, len, value);
175 if ((outSize < 4) || (left < 4))
184 static BOOL nsc_rle_decompress_data(NSC_CONTEXT* WINPR_RESTRICT context)
189 const BYTE* rle = context->Planes;
190 size_t rleSize = context->PlanesSize;
193 for (
size_t i = 0; i < 4; i++)
195 const UINT32 originalSize = context->OrgByteCount[i];
196 const UINT32 planeSize = context->PlaneByteCount[i];
198 if (rleSize < planeSize)
203 if (context->priv->PlaneBuffersLength < originalSize)
206 FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
208 else if (planeSize < originalSize)
210 if (!nsc_rle_decode(rle, rleSize, context->priv->PlaneBuffers[i],
211 context->priv->PlaneBuffersLength, originalSize))
216 if (context->priv->PlaneBuffersLength < originalSize)
219 if (rleSize < originalSize)
222 CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
226 rleSize -= planeSize;
232 static BOOL nsc_stream_initialize(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s)
234 WINPR_ASSERT(context);
235 WINPR_ASSERT(context->priv);
236 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 20))
240 for (
size_t i = 0; i < 4; i++)
242 Stream_Read_UINT32(s, context->PlaneByteCount[i]);
243 total += context->PlaneByteCount[i];
246 Stream_Read_UINT8(s, context->ColorLossLevel);
247 if ((context->ColorLossLevel < 1) || (context->ColorLossLevel > 7))
249 WLog_Print(context->priv->log, WLOG_ERROR,
250 "ColorLossLevel=%" PRIu8
" out of range, must be [1,7] inclusive",
251 context->ColorLossLevel);
254 Stream_Read_UINT8(s, context->ChromaSubsamplingLevel);
256 context->Planes = Stream_Pointer(s);
257 context->PlanesSize = total;
258 return Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, total);
261 static BOOL nsc_context_initialize(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s)
263 if (!nsc_stream_initialize(context, s))
266 const size_t blength = 4ull * context->width * context->height;
268 if (!context->BitmapData || (blength > context->BitmapDataLength))
270 void* tmp = winpr_aligned_recalloc(context->BitmapData, blength + 16,
sizeof(BYTE), 32);
275 context->BitmapData = tmp;
276 context->BitmapDataLength = blength;
279 const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
280 const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
282 const size_t plength = 1ull * tempWidth * tempHeight;
283 if (plength > UINT32_MAX)
286 if (plength > context->priv->PlaneBuffersLength)
288 for (
size_t i = 0; i < 4; i++)
290 void* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], plength,
296 context->priv->PlaneBuffers[i] = tmp;
299 context->priv->PlaneBuffersLength = (UINT32)plength;
302 for (
size_t i = 0; i < 4; i++)
303 context->OrgByteCount[i] = context->width * context->height;
305 if (context->ChromaSubsamplingLevel)
307 context->OrgByteCount[0] = tempWidth * context->height;
308 context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
309 context->OrgByteCount[2] = context->OrgByteCount[1];
319 PROFILER_PRINT_HEADER
320 PROFILER_PRINT(priv->prof_nsc_rle_decompress_data)
321 PROFILER_PRINT(priv->prof_nsc_decode)
322 PROFILER_PRINT(priv->prof_nsc_rle_compress_data)
323 PROFILER_PRINT(priv->prof_nsc_encode)
324 PROFILER_PRINT_FOOTER
327 BOOL nsc_context_reset(NSC_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
332 if ((width > UINT16_MAX) || (height > UINT16_MAX))
335 context->width = (UINT16)width;
336 context->height = (UINT16)height;
340 NSC_CONTEXT* nsc_context_new(
void)
342 NSC_CONTEXT* context = (NSC_CONTEXT*)winpr_aligned_calloc(1,
sizeof(NSC_CONTEXT), 32);
352 context->priv->log = WLog_Get(
"com.freerdp.codec.nsc");
353 WLog_OpenAppender(context->priv->log);
354 context->BitmapData = NULL;
355 context->decode = nsc_decode;
356 context->encode = nsc_encode;
358 PROFILER_CREATE(context->priv->prof_nsc_rle_decompress_data,
"nsc_rle_decompress_data")
359 PROFILER_CREATE(context->priv->prof_nsc_decode, "nsc_decode")
360 PROFILER_CREATE(context->priv->prof_nsc_rle_compress_data, "nsc_rle_compress_data")
361 PROFILER_CREATE(context->priv->prof_nsc_encode, "nsc_encode")
363 context->ColorLossLevel = 3;
364 context->ChromaSubsamplingLevel = 1;
366 nsc_init_sse2(context);
367 nsc_init_neon(context);
370 WINPR_PRAGMA_DIAG_PUSH
371 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
372 nsc_context_free(context);
373 WINPR_PRAGMA_DIAG_POP
377 void nsc_context_free(NSC_CONTEXT* context)
384 for (
size_t i = 0; i < 5; i++)
385 winpr_aligned_free(context->priv->PlaneBuffers[i]);
387 nsc_profiler_print(context->priv);
388 PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
389 PROFILER_FREE(context->priv->prof_nsc_decode)
390 PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
391 PROFILER_FREE(context->priv->prof_nsc_encode)
392 winpr_aligned_free(context->priv);
395 winpr_aligned_free(context->BitmapData);
396 winpr_aligned_free(context);
399 #if defined(WITH_FREERDP_DEPRECATED)
400 BOOL nsc_context_set_pixel_format(NSC_CONTEXT* context, UINT32 pixel_format)
402 return nsc_context_set_parameters(context, NSC_COLOR_FORMAT, pixel_format);
406 BOOL nsc_context_set_parameters(NSC_CONTEXT* WINPR_RESTRICT context, NSC_PARAMETER what,
414 case NSC_COLOR_LOSS_LEVEL:
415 context->ColorLossLevel = value;
417 case NSC_ALLOW_SUBSAMPLING:
418 context->ChromaSubsamplingLevel = value;
420 case NSC_DYNAMIC_COLOR_FIDELITY:
421 context->DynamicColorFidelity = value != 0;
423 case NSC_COLOR_FORMAT:
424 context->format = value;
432 BOOL nsc_process_message(NSC_CONTEXT* WINPR_RESTRICT context, UINT16 bpp, UINT32 width,
433 UINT32 height,
const BYTE* data, UINT32 length,
434 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStride,
435 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 flip)
440 if (!context || !data || !pDstData)
443 s = Stream_StaticConstInit(&sbuffer, data, length);
449 nDstStride = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
454 context->format = PIXEL_FORMAT_BGRA32;
458 context->format = PIXEL_FORMAT_BGR24;
462 context->format = PIXEL_FORMAT_BGR16;
466 context->format = PIXEL_FORMAT_RGB8;
470 context->format = PIXEL_FORMAT_A4;
477 context->width = width;
478 context->height = height;
479 ret = nsc_context_initialize(context, s);
487 PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
488 rc = nsc_rle_decompress_data(context);
489 PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
497 PROFILER_ENTER(context->priv->prof_nsc_decode)
498 rc = context->decode(context);
499 PROFILER_EXIT(context->priv->prof_nsc_decode)
505 if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStride, nXDst, nYDst, width, height,
506 context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0, 0, NULL,