2 * FreeRDP: A Remote Desktop Protocol Implementation
5 * Copyright 2011 Samsung, Author Jiten Pathy
6 * Copyright 2012 Vic Lee
7 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
8 * Copyright 2016 Thincast Technologies GmbH
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
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"
42 #define NSC_INIT_SIMD(_nsc_context) \
48 static BOOL nsc_decode(NSC_CONTEXT* context)
60 rw = ROUND_UP_TO(context->width, 8);
61 shift = context->ColorLossLevel - 1; /* colorloss recovery + YCoCg shift */
62 bmpdata = context->BitmapData;
67 for (y = 0; y < context->height; y++)
72 const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width; /* A */
74 if (context->ChromaSubsamplingLevel)
76 yplane = context->priv->PlaneBuffers[0] + y * rw; /* Y */
77 coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
78 cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
82 yplane = context->priv->PlaneBuffers[0] + y * context->width; /* Y */
83 coplane = context->priv->PlaneBuffers[1] + y * context->width; /* Co */
84 cgplane = context->priv->PlaneBuffers[2] + y * context->width; /* Cg */
87 for (x = 0; x < context->width; x++)
89 INT16 y_val = (INT16)*yplane;
90 INT16 co_val = (INT16)(INT8)(*coplane << shift);
91 INT16 cg_val = (INT16)(INT8)(*cgplane << shift);
92 INT16 r_val = y_val + co_val - cg_val;
93 INT16 g_val = y_val + cg_val;
94 INT16 b_val = y_val - co_val - cg_val;
96 if (pos + 4 > context->BitmapDataLength)
100 *bmpdata++ = MINMAX(b_val, 0, 0xFF);
101 *bmpdata++ = MINMAX(g_val, 0, 0xFF);
102 *bmpdata++ = MINMAX(r_val, 0, 0xFF);
103 *bmpdata++ = *aplane;
105 coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
106 cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
114 static BOOL nsc_rle_decode(BYTE* in, BYTE* out, UINT32 outSize, UINT32 originalSize)
116 UINT32 left = originalSize;
120 const BYTE value = *in++;
132 else if (value == *in)
144 len = ((UINT32)(*in++));
145 len |= ((UINT32)(*in++)) << 8U;
146 len |= ((UINT32)(*in++)) << 16U;
147 len |= ((UINT32)(*in++)) << 24U;
154 FillMemory(out, len, value);
169 if ((outSize < 4) || (left < 4))
176 static BOOL nsc_rle_decompress_data(NSC_CONTEXT* context)
186 rle = context->Planes;
188 for (i = 0; i < 4; i++)
190 originalSize = context->OrgByteCount[i];
191 planeSize = context->PlaneByteCount[i];
195 if (context->priv->PlaneBuffersLength < originalSize)
198 FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
200 else if (planeSize < originalSize)
202 if (!nsc_rle_decode(rle, context->priv->PlaneBuffers[i],
203 context->priv->PlaneBuffersLength, originalSize))
208 if (context->priv->PlaneBuffersLength < originalSize)
211 CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
220 static BOOL nsc_stream_initialize(NSC_CONTEXT* context, wStream* s)
224 if (Stream_GetRemainingLength(s) < 20)
227 for (i = 0; i < 4; i++)
228 Stream_Read_UINT32(s, context->PlaneByteCount[i]);
230 Stream_Read_UINT8(s, context->ColorLossLevel); /* ColorLossLevel (1 byte) */
231 Stream_Read_UINT8(s, context->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
232 Stream_Seek(s, 2); /* Reserved (2 bytes) */
233 context->Planes = Stream_Pointer(s);
237 static BOOL nsc_context_initialize(NSC_CONTEXT* context, wStream* s)
244 if (!nsc_stream_initialize(context, s))
247 length = context->width * context->height * 4;
249 if (!context->BitmapData)
251 context->BitmapData = calloc(1, length + 16);
253 if (!context->BitmapData)
256 context->BitmapDataLength = length;
258 else if (length > context->BitmapDataLength)
261 tmp = realloc(context->BitmapData, length + 16);
266 context->BitmapData = tmp;
267 context->BitmapDataLength = length;
270 tempWidth = ROUND_UP_TO(context->width, 8);
271 tempHeight = ROUND_UP_TO(context->height, 2);
272 /* The maximum length a decoded plane can reach in all cases */
273 length = tempWidth * tempHeight;
275 if (length > context->priv->PlaneBuffersLength)
277 for (i = 0; i < 4; i++)
279 void* tmp = (BYTE*)realloc(context->priv->PlaneBuffers[i], length);
284 context->priv->PlaneBuffers[i] = tmp;
287 context->priv->PlaneBuffersLength = length;
290 for (i = 0; i < 4; i++)
292 context->OrgByteCount[i] = context->width * context->height;
295 if (context->ChromaSubsamplingLevel)
297 context->OrgByteCount[0] = tempWidth * context->height;
298 context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
299 context->OrgByteCount[2] = context->OrgByteCount[1];
305 static void nsc_profiler_print(NSC_CONTEXT_PRIV* priv){
306 PROFILER_PRINT_HEADER PROFILER_PRINT(priv->prof_nsc_rle_decompress_data)
307 PROFILER_PRINT(priv->prof_nsc_decode) PROFILER_PRINT(priv->prof_nsc_rle_compress_data)
308 PROFILER_PRINT(priv->prof_nsc_encode) PROFILER_PRINT_FOOTER
311 BOOL nsc_context_reset(NSC_CONTEXT* context, UINT32 width, UINT32 height)
316 if ((width > UINT16_MAX) || (height > UINT16_MAX))
319 context->width = (UINT16)width;
320 context->height = (UINT16)height;
324 NSC_CONTEXT* nsc_context_new(void)
326 NSC_CONTEXT* context;
327 context = (NSC_CONTEXT*)calloc(1, sizeof(NSC_CONTEXT));
332 context->priv = (NSC_CONTEXT_PRIV*)calloc(1, sizeof(NSC_CONTEXT_PRIV));
337 context->priv->log = WLog_Get("com.freerdp.codec.nsc");
338 WLog_OpenAppender(context->priv->log);
339 context->BitmapData = NULL;
340 context->decode = nsc_decode;
341 context->encode = nsc_encode;
343 PROFILER_CREATE(context->priv->prof_nsc_rle_decompress_data, "nsc_rle_decompress_data")
344 PROFILER_CREATE(context->priv->prof_nsc_decode, "nsc_decode")
345 PROFILER_CREATE(context->priv->prof_nsc_rle_compress_data, "nsc_rle_compress_data")
346 PROFILER_CREATE(context->priv->prof_nsc_encode, "nsc_encode")
347 /* Default encoding parameters */
348 context->ColorLossLevel = 3;
349 context->ChromaSubsamplingLevel = 1;
350 /* init optimized methods */
351 NSC_INIT_SIMD(context);
354 nsc_context_free(context);
358 void nsc_context_free(NSC_CONTEXT* context)
367 for (i = 0; i < 5; i++)
368 free(context->priv->PlaneBuffers[i]);
370 nsc_profiler_print(context->priv);
371 PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
372 PROFILER_FREE(context->priv->prof_nsc_decode)
373 PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
374 PROFILER_FREE(context->priv->prof_nsc_encode)
378 free(context->BitmapData);
382 BOOL nsc_context_set_pixel_format(NSC_CONTEXT* context, UINT32 pixel_format)
384 return nsc_context_set_parameters(context, NSC_COLOR_FORMAT, pixel_format);
387 BOOL nsc_context_set_parameters(NSC_CONTEXT* context, NSC_PARAMETER what, UINT32 value)
394 case NSC_COLOR_LOSS_LEVEL:
395 context->ColorLossLevel = value;
397 case NSC_ALLOW_SUBSAMPLING:
398 context->ChromaSubsamplingLevel = value;
400 case NSC_DYNAMIC_COLOR_FIDELITY:
401 context->DynamicColorFidelity = value != 0;
403 case NSC_COLOR_FORMAT:
404 context->format = value;
412 BOOL nsc_process_message(NSC_CONTEXT* context, UINT16 bpp, UINT32 width, UINT32 height,
413 const BYTE* data, UINT32 length, BYTE* pDstData, UINT32 DstFormat,
414 UINT32 nDstStride, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
415 UINT32 nHeight, UINT32 flip)
419 if (!context || !data || !pDstData)
422 s = Stream_New((BYTE*)data, length);
428 nDstStride = nWidth * GetBytesPerPixel(DstFormat);
433 context->format = PIXEL_FORMAT_BGRA32;
437 context->format = PIXEL_FORMAT_BGR24;
441 context->format = PIXEL_FORMAT_BGR16;
445 context->format = PIXEL_FORMAT_RGB8;
449 context->format = PIXEL_FORMAT_A4;
453 Stream_Free(s, TRUE);
457 context->width = width;
458 context->height = height;
459 ret = nsc_context_initialize(context, s);
460 Stream_Free(s, FALSE);
468 PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
469 rc = nsc_rle_decompress_data(context);
470 PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
475 /* Colorloss recover, Chroma supersample and AYCoCg to ARGB Conversion in one step */
478 PROFILER_ENTER(context->priv->prof_nsc_decode)
479 rc = context->decode(context);
480 PROFILER_EXIT(context->priv->prof_nsc_decode)
486 if (!freerdp_image_copy(pDstData, DstFormat, nDstStride, nXDst, nYDst, width, height,
487 context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0, 0, NULL, flip))