FreeRDP
Loading...
Searching...
No Matches
nsc.c
1
23#include <freerdp/config.h>
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <winpr/assert.h>
30#include <winpr/cast.h>
31#include <winpr/crt.h>
32
33#include <freerdp/codec/nsc.h>
34#include <freerdp/codec/color.h>
35
36#include "nsc_types.h"
37#include "nsc_encode.h"
38
39#include "sse/nsc_sse2.h"
40#include "neon/nsc_neon.h"
41
42#include <freerdp/log.h>
43
44static BOOL nsc_decode(NSC_CONTEXT* WINPR_RESTRICT context)
45{
46 size_t pos = 0;
47
48 if (!context)
49 return FALSE;
50
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 -
54 1); /* colorloss recovery + YCoCg shift */
55 BYTE* bmpdata = context->BitmapData;
56
57 if (!bmpdata)
58 return FALSE;
59
60 for (size_t y = 0; y < context->height; y++)
61 {
62 const BYTE* yplane = nullptr;
63 const BYTE* coplane = nullptr;
64 const BYTE* cgplane = nullptr;
65 const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width; /* A */
66
67 if (context->ChromaSubsamplingLevel)
68 {
69 yplane = context->priv->PlaneBuffers[0] + y * rw; /* Y */
70 coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
71 cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
72 }
73 else
74 {
75 yplane = context->priv->PlaneBuffers[0] + y * context->width; /* Y */
76 coplane = context->priv->PlaneBuffers[1] + y * context->width; /* Co */
77 cgplane = context->priv->PlaneBuffers[2] + y * context->width; /* Cg */
78 }
79
80 for (UINT32 x = 0; x < context->width; x++)
81 {
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);
88
89 if (pos + 4 > context->BitmapDataLength)
90 return FALSE;
91
92 pos += 4;
93 *bmpdata++ = MINMAX(b_val, 0, 0xFF);
94 *bmpdata++ = MINMAX(g_val, 0, 0xFF);
95 *bmpdata++ = MINMAX(r_val, 0, 0xFF);
96 *bmpdata++ = *aplane;
97 yplane++;
98 coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
99 cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
100 aplane++;
101 }
102 }
103
104 return TRUE;
105}
106
107static BOOL nsc_rle_decode(const BYTE* WINPR_RESTRICT in, size_t inSize, BYTE* WINPR_RESTRICT out,
108 UINT32 outSize, UINT32 originalSize)
109{
110 UINT32 left = originalSize;
111
112 while (left > 4)
113 {
114 if (inSize < 1)
115 return FALSE;
116 inSize--;
117
118 const BYTE value = *in++;
119 UINT32 len = 0;
120
121 if (left == 5)
122 {
123 if (outSize < 1)
124 return FALSE;
125
126 outSize--;
127 *out++ = value;
128 left--;
129 }
130 else if (inSize < 1)
131 return FALSE;
132 else if (value == *in)
133 {
134 inSize--;
135 in++;
136
137 if (inSize < 1)
138 return FALSE;
139 else if (*in < 0xFF)
140 {
141 inSize--;
142 len = (UINT32)*in++;
143 len += 2;
144 }
145 else
146 {
147 if (inSize < 5)
148 return FALSE;
149 inSize -= 5;
150 in++;
151 len = ((UINT32)(*in++));
152 len |= ((UINT32)(*in++)) << 8U;
153 len |= ((UINT32)(*in++)) << 16U;
154 len |= ((UINT32)(*in++)) << 24U;
155 }
156
157 if ((outSize < len) || (left < len))
158 return FALSE;
159
160 outSize -= len;
161 FillMemory(out, len, value);
162 out += len;
163 left -= len;
164 }
165 else
166 {
167 if (outSize < 1)
168 return FALSE;
169
170 outSize--;
171 *out++ = value;
172 left--;
173 }
174 }
175
176 if ((outSize < 4) || (left < 4))
177 return FALSE;
178
179 if (inSize < 4)
180 return FALSE;
181 memcpy(out, in, 4);
182 return TRUE;
183}
184
185static BOOL nsc_rle_decompress_data(NSC_CONTEXT* WINPR_RESTRICT context)
186{
187 if (!context)
188 return FALSE;
189
190 const BYTE* rle = context->Planes;
191 size_t rleSize = context->PlanesSize;
192 WINPR_ASSERT(rle);
193
194 for (size_t i = 0; i < 4; i++)
195 {
196 const UINT32 originalSize = context->OrgByteCount[i];
197 const UINT32 planeSize = context->PlaneByteCount[i];
198
199 if (rleSize < planeSize)
200 return FALSE;
201
202 if (planeSize == 0)
203 {
204 if (context->priv->PlaneBuffersLength < originalSize)
205 return FALSE;
206
207 FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
208 }
209 else if (planeSize < originalSize)
210 {
211 if (!nsc_rle_decode(rle, rleSize, context->priv->PlaneBuffers[i],
212 context->priv->PlaneBuffersLength, originalSize))
213 return FALSE;
214 }
215 else
216 {
217 if (context->priv->PlaneBuffersLength < originalSize)
218 return FALSE;
219
220 if (rleSize < originalSize)
221 return FALSE;
222
223 CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
224 }
225
226 rle += planeSize;
227 rleSize -= planeSize;
228 }
229
230 return TRUE;
231}
232
233static BOOL nsc_stream_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
234{
235 WINPR_ASSERT(context);
236 WINPR_ASSERT(context->priv);
237 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 20))
238 return FALSE;
239
240 size_t total = 0;
241 for (size_t i = 0; i < 4; i++)
242 {
243 Stream_Read_UINT32(s, context->PlaneByteCount[i]);
244 total += context->PlaneByteCount[i];
245 }
246
247 Stream_Read_UINT8(s, context->ColorLossLevel); /* ColorLossLevel (1 byte) */
248 if ((context->ColorLossLevel < 1) || (context->ColorLossLevel > 7))
249 {
250 WLog_Print(context->priv->log, WLOG_ERROR,
251 "ColorLossLevel=%" PRIu8 " out of range, must be [1,7] inclusive",
252 context->ColorLossLevel);
253 return FALSE;
254 }
255 Stream_Read_UINT8(s, context->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
256 Stream_Seek(s, 2); /* Reserved (2 bytes) */
257 context->Planes = Stream_Pointer(s);
258 context->PlanesSize = total;
259 return Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, total);
260}
261
262static BOOL nsc_context_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
263{
264 if (!nsc_stream_initialize(context, s))
265 return FALSE;
266
267 const size_t blength = 4ull * context->width * context->height;
268
269 if (!context->BitmapData || (blength > context->BitmapDataLength))
270 {
271 void* tmp = winpr_aligned_recalloc(context->BitmapData, blength + 16, sizeof(BYTE), 32);
272
273 if (!tmp)
274 return FALSE;
275
276 context->BitmapData = tmp;
277 context->BitmapDataLength = blength;
278 }
279
280 const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
281 const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
282 /* The maximum length a decoded plane can reach in all cases */
283 const size_t plength = 1ull * tempWidth * tempHeight;
284 if (plength > UINT32_MAX)
285 return FALSE;
286
287 if (plength > context->priv->PlaneBuffersLength)
288 {
289 for (size_t i = 0; i < 4; i++)
290 {
291 void* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], plength,
292 sizeof(BYTE), 32);
293
294 if (!tmp)
295 return FALSE;
296
297 context->priv->PlaneBuffers[i] = tmp;
298 }
299
300 context->priv->PlaneBuffersLength = (UINT32)plength;
301 }
302
303 for (size_t i = 0; i < 4; i++)
304 context->OrgByteCount[i] = context->width * context->height;
305
306 if (context->ChromaSubsamplingLevel)
307 {
308 context->OrgByteCount[0] = tempWidth * context->height;
309 context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
310 context->OrgByteCount[2] = context->OrgByteCount[1];
311 }
312
313 return TRUE;
314}
315
316static void nsc_profiler_print(NSC_CONTEXT_PRIV* WINPR_RESTRICT priv)
317{
318 WINPR_UNUSED(priv);
319
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
326}
327
328BOOL nsc_context_reset(NSC_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
329{
330 if (!context)
331 return FALSE;
332
333 if ((width > UINT16_MAX) || (height > UINT16_MAX))
334 return FALSE;
335
336 context->width = (UINT16)width;
337 context->height = (UINT16)height;
338 return TRUE;
339}
340
341NSC_CONTEXT* nsc_context_new(void)
342{
343 NSC_CONTEXT* context = (NSC_CONTEXT*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT), 32);
344
345 if (!context)
346 return nullptr;
347
348 context->priv = (NSC_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT_PRIV), 32);
349
350 if (!context->priv)
351 goto error;
352
353 context->priv->log = WLog_Get("com.freerdp.codec.nsc");
354 context->BitmapData = nullptr;
355 context->decode = nsc_decode;
356 context->encode = nsc_encode;
357
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")
362 /* Default encoding parameters */
363 context->ColorLossLevel = 3;
364 context->ChromaSubsamplingLevel = 1;
365 /* init optimized methods */
366 nsc_init_sse2(context);
367 nsc_init_neon(context);
368 return context;
369error:
370 WINPR_PRAGMA_DIAG_PUSH
371 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
372 nsc_context_free(context);
373 WINPR_PRAGMA_DIAG_POP
374 return nullptr;
375}
376
377void nsc_context_planebuffers_free(NSC_CONTEXT_PRIV* priv)
378{
379 if (!priv)
380 return;
381
382 for (size_t i = 0; i < ARRAYSIZE(priv->PlaneBuffers); i++)
383 {
384 BYTE* cur = priv->PlaneBuffers[i];
385 priv->PlaneBuffers[i] = nullptr;
386 winpr_aligned_free(cur);
387 }
388
389 priv->PlaneBuffersLength = 0;
390}
391
392void nsc_context_free(NSC_CONTEXT* context)
393{
394 if (!context)
395 return;
396
397 if (context->priv)
398 {
399 nsc_context_planebuffers_free(context->priv);
400
401 nsc_profiler_print(context->priv);
402 PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
403 PROFILER_FREE(context->priv->prof_nsc_decode)
404 PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
405 PROFILER_FREE(context->priv->prof_nsc_encode)
406 winpr_aligned_free(context->priv);
407 }
408
409 winpr_aligned_free(context->BitmapData);
410 winpr_aligned_free(context);
411}
412
413#if defined(WITH_FREERDP_DEPRECATED)
414BOOL nsc_context_set_pixel_format(NSC_CONTEXT* context, UINT32 pixel_format)
415{
416 return nsc_context_set_parameters(context, NSC_COLOR_FORMAT, pixel_format);
417}
418#endif
419
420BOOL nsc_context_set_parameters(NSC_CONTEXT* WINPR_RESTRICT context, NSC_PARAMETER what,
421 UINT32 value)
422{
423 if (!context)
424 return FALSE;
425
426 switch (what)
427 {
428 case NSC_COLOR_LOSS_LEVEL:
429 context->ColorLossLevel = value;
430 break;
431 case NSC_ALLOW_SUBSAMPLING:
432 context->ChromaSubsamplingLevel = value;
433 break;
434 case NSC_DYNAMIC_COLOR_FIDELITY:
435 context->DynamicColorFidelity = value != 0;
436 break;
437 case NSC_COLOR_FORMAT:
438 context->format = value;
439 break;
440 default:
441 return FALSE;
442 }
443 return TRUE;
444}
445
446BOOL nsc_process_message(NSC_CONTEXT* WINPR_RESTRICT context, UINT16 bpp, UINT32 width,
447 UINT32 height, const BYTE* data, UINT32 length,
448 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStride,
449 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 flip)
450{
451 if (!context)
452 return FALSE;
453
454 WINPR_ASSERT(context->priv);
455
456 wStream sbuffer = WINPR_C_ARRAY_INIT;
457 BOOL ret = 0;
458 if (!data || !pDstData)
459 {
460 WLog_Print(context->priv->log, WLOG_ERROR, "Invalid argument: data=%p, pDstData=%p",
461 (const void*)data, (void*)pDstData);
462 return FALSE;
463 }
464
465 if (nXDst > nWidth)
466 {
467 WLog_Print(context->priv->log, WLOG_ERROR, "nXDst %" PRIu32 " > nWidth %" PRIu32, nXDst,
468 nWidth);
469 return FALSE;
470 }
471 if (nYDst > nHeight)
472 {
473 WLog_Print(context->priv->log, WLOG_ERROR, "nYDst %" PRIu32 " > nHeight %" PRIu32, nYDst,
474 nHeight);
475 return FALSE;
476 }
477
478 wStream* s = Stream_StaticConstInit(&sbuffer, data, length);
479 if (!s)
480 return FALSE;
481
482 const UINT32 minStride = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
483 if (nDstStride == 0)
484 nDstStride = minStride;
485 if (nDstStride < minStride)
486 {
487 WLog_Print(context->priv->log, WLOG_ERROR,
488 "nDstStride %" PRIu32 " < minimum stride %" PRIu32, nDstStride, minStride);
489 return FALSE;
490 }
491
492 switch (bpp)
493 {
494 case 32:
495 context->format = PIXEL_FORMAT_BGRA32;
496 break;
497
498 case 24:
499 context->format = PIXEL_FORMAT_BGR24;
500 break;
501
502 case 16:
503 context->format = PIXEL_FORMAT_BGR16;
504 break;
505
506 case 8:
507 context->format = PIXEL_FORMAT_RGB8;
508 break;
509
510 case 4:
511 context->format = PIXEL_FORMAT_A4;
512 break;
513
514 default:
515 return FALSE;
516 }
517
518 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
519 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
520 ret = nsc_context_initialize(context, s);
521
522 if (!ret)
523 return FALSE;
524
525 /* RLE decode */
526 {
527 BOOL rc = 0;
528 PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
529 rc = nsc_rle_decompress_data(context);
530 PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
531
532 if (!rc)
533 return FALSE;
534 }
535 /* Colorloss recover, Chroma supersample and AYCoCg to ARGB Conversion in one step */
536 {
537 BOOL rc = 0;
538 PROFILER_ENTER(context->priv->prof_nsc_decode)
539 rc = context->decode(context);
540 PROFILER_EXIT(context->priv->prof_nsc_decode)
541
542 if (!rc)
543 return FALSE;
544 }
545
546 uint32_t cwidth = width;
547 if (1ull * nXDst + width > nWidth)
548 cwidth = nWidth - nXDst;
549
550 uint32_t cheight = height;
551 if (1ull * nYDst + height > nHeight)
552 cheight = nHeight - nYDst;
553
554 return (freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStride, nXDst, nYDst, cwidth,
555 cheight, context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0,
556 0, nullptr, flip));
557}