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