Coverage Report

Created: 2026-05-30 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/nsc.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * NSCodec Codec
4
 *
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
9
 *
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
13
 *
14
 *   http://www.apache.org/licenses/LICENSE-2.0
15
 *
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.
21
 */
22
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
44
static BOOL nsc_decode(NSC_CONTEXT* WINPR_RESTRICT context)
45
890
{
46
890
  size_t pos = 0;
47
48
890
  if (!context)
49
0
    return FALSE;
50
51
890
  const UINT16 rw = ROUND_UP_TO(context->width, 8);
52
890
  WINPR_ASSERT(context->ColorLossLevel >= 1);
53
890
  const BYTE shift = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel -
54
890
                                                        1); /* colorloss recovery + YCoCg shift */
55
890
  BYTE* bmpdata = context->BitmapData;
56
57
890
  if (!bmpdata)
58
0
    return FALSE;
59
60
17.8k
  for (size_t y = 0; y < context->height; y++)
61
16.9k
  {
62
16.9k
    const BYTE* yplane = nullptr;
63
16.9k
    const BYTE* coplane = nullptr;
64
16.9k
    const BYTE* cgplane = nullptr;
65
16.9k
    const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width; /* A */
66
67
16.9k
    if (context->ChromaSubsamplingLevel)
68
12.4k
    {
69
12.4k
      yplane = context->priv->PlaneBuffers[0] + y * rw;                /* Y */
70
12.4k
      coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
71
12.4k
      cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
72
12.4k
    }
73
4.48k
    else
74
4.48k
    {
75
4.48k
      yplane = context->priv->PlaneBuffers[0] + y * context->width;  /* Y */
76
4.48k
      coplane = context->priv->PlaneBuffers[1] + y * context->width; /* Co */
77
4.48k
      cgplane = context->priv->PlaneBuffers[2] + y * context->width; /* Cg */
78
4.48k
    }
79
80
692k
    for (UINT32 x = 0; x < context->width; x++)
81
675k
    {
82
675k
      INT16 y_val = (INT16)*yplane;
83
675k
      INT16 co_val = (INT16)(INT8)(((INT16)*coplane) << shift);
84
675k
      INT16 cg_val = (INT16)(INT8)(((INT16)*cgplane) << shift);
85
675k
      INT16 r_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + co_val - cg_val);
86
675k
      INT16 g_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + cg_val);
87
675k
      INT16 b_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val - co_val - cg_val);
88
89
675k
      if (pos + 4 > context->BitmapDataLength)
90
0
        return FALSE;
91
92
675k
      pos += 4;
93
675k
      *bmpdata++ = MINMAX(b_val, 0, 0xFF);
94
675k
      *bmpdata++ = MINMAX(g_val, 0, 0xFF);
95
675k
      *bmpdata++ = MINMAX(r_val, 0, 0xFF);
96
675k
      *bmpdata++ = *aplane;
97
675k
      yplane++;
98
675k
      coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
99
675k
      cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
100
675k
      aplane++;
101
675k
    }
102
16.9k
  }
103
104
890
  return TRUE;
105
890
}
106
107
static BOOL nsc_rle_decode(const BYTE* WINPR_RESTRICT in, size_t inSize, BYTE* WINPR_RESTRICT out,
108
                           UINT32 outSize, UINT32 originalSize)
109
1.58k
{
110
1.58k
  UINT32 left = originalSize;
111
112
44.3k
  while (left > 4)
113
43.7k
  {
114
43.7k
    if (inSize < 1)
115
107
      return FALSE;
116
43.6k
    inSize--;
117
118
43.6k
    const BYTE value = *in++;
119
43.6k
    UINT32 len = 0;
120
121
43.6k
    if (left == 5)
122
399
    {
123
399
      if (outSize < 1)
124
0
        return FALSE;
125
126
399
      outSize--;
127
399
      *out++ = value;
128
399
      left--;
129
399
    }
130
43.2k
    else if (inSize < 1)
131
292
      return FALSE;
132
42.9k
    else if (value == *in)
133
21.3k
    {
134
21.3k
      inSize--;
135
21.3k
      in++;
136
137
21.3k
      if (inSize < 1)
138
44
        return FALSE;
139
21.3k
      else if (*in < 0xFF)
140
20.4k
      {
141
20.4k
        inSize--;
142
20.4k
        len = (UINT32)*in++;
143
20.4k
        len += 2;
144
20.4k
      }
145
923
      else
146
923
      {
147
923
        if (inSize < 5)
148
31
          return FALSE;
149
892
        inSize -= 5;
150
892
        in++;
151
892
        len = ((UINT32)(*in++));
152
892
        len |= ((UINT32)(*in++)) << 8U;
153
892
        len |= ((UINT32)(*in++)) << 16U;
154
892
        len |= ((UINT32)(*in++)) << 24U;
155
892
      }
156
157
21.3k
      if ((outSize < len) || (left < len))
158
487
        return FALSE;
159
160
20.8k
      outSize -= len;
161
20.8k
      FillMemory(out, len, value);
162
20.8k
      out += len;
163
20.8k
      left -= len;
164
20.8k
    }
165
21.5k
    else
166
21.5k
    {
167
21.5k
      if (outSize < 1)
168
0
        return FALSE;
169
170
21.5k
      outSize--;
171
21.5k
      *out++ = value;
172
21.5k
      left--;
173
21.5k
    }
174
43.6k
  }
175
176
626
  if ((outSize < 4) || (left < 4))
177
50
    return FALSE;
178
179
576
  if (inSize < 4)
180
44
    return FALSE;
181
532
  memcpy(out, in, 4);
182
532
  return TRUE;
183
576
}
184
185
static BOOL nsc_rle_decompress_data(NSC_CONTEXT* WINPR_RESTRICT context)
186
1.94k
{
187
1.94k
  if (!context)
188
0
    return FALSE;
189
190
1.94k
  const BYTE* rle = context->Planes;
191
1.94k
  size_t rleSize = context->PlanesSize;
192
1.94k
  WINPR_ASSERT(rle);
193
194
6.50k
  for (size_t i = 0; i < 4; i++)
195
5.61k
  {
196
5.61k
    const UINT32 originalSize = context->OrgByteCount[i];
197
5.61k
    const UINT32 planeSize = context->PlaneByteCount[i];
198
199
5.61k
    if (rleSize < planeSize)
200
0
      return FALSE;
201
202
5.61k
    if (planeSize == 0)
203
3.18k
    {
204
3.18k
      if (context->priv->PlaneBuffersLength < originalSize)
205
0
        return FALSE;
206
207
3.18k
      FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
208
3.18k
    }
209
2.42k
    else if (planeSize < originalSize)
210
1.58k
    {
211
1.58k
      if (!nsc_rle_decode(rle, rleSize, context->priv->PlaneBuffers[i],
212
1.58k
                          context->priv->PlaneBuffersLength, originalSize))
213
1.05k
        return FALSE;
214
1.58k
    }
215
841
    else
216
841
    {
217
841
      if (context->priv->PlaneBuffersLength < originalSize)
218
0
        return FALSE;
219
220
841
      if (rleSize < originalSize)
221
0
        return FALSE;
222
223
841
      CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
224
841
    }
225
226
4.56k
    rle += planeSize;
227
4.56k
    rleSize -= planeSize;
228
4.56k
  }
229
230
890
  return TRUE;
231
1.94k
}
232
233
static BOOL nsc_stream_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
234
2.51k
{
235
2.51k
  WINPR_ASSERT(context);
236
2.51k
  WINPR_ASSERT(context->priv);
237
2.51k
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 20))
238
77
    return FALSE;
239
240
2.44k
  size_t total = 0;
241
12.2k
  for (size_t i = 0; i < 4; i++)
242
9.76k
  {
243
9.76k
    Stream_Read_UINT32(s, context->PlaneByteCount[i]);
244
9.76k
    total += context->PlaneByteCount[i];
245
9.76k
  }
246
247
2.44k
  Stream_Read_UINT8(s, context->ColorLossLevel);         /* ColorLossLevel (1 byte) */
248
2.44k
  if ((context->ColorLossLevel < 1) || (context->ColorLossLevel > 7))
249
157
  {
250
157
    WLog_Print(context->priv->log, WLOG_ERROR,
251
157
               "ColorLossLevel=%" PRIu8 " out of range, must be [1,7] inclusive",
252
157
               context->ColorLossLevel);
253
157
    return FALSE;
254
157
  }
255
2.28k
  Stream_Read_UINT8(s, context->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
256
2.28k
  Stream_Seek(s, 2);                                     /* Reserved (2 bytes) */
257
2.28k
  context->Planes = Stream_Pointer(s);
258
2.28k
  context->PlanesSize = total;
259
2.28k
  return Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, total);
260
2.44k
}
261
262
static BOOL nsc_context_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
263
2.51k
{
264
2.51k
  if (!nsc_stream_initialize(context, s))
265
573
    return FALSE;
266
267
1.94k
  const size_t blength = 4ull * context->width * context->height;
268
269
1.94k
  if (!context->BitmapData || (blength > context->BitmapDataLength))
270
1.92k
  {
271
1.92k
    void* tmp = winpr_aligned_recalloc(context->BitmapData, blength + 16, sizeof(BYTE), 32);
272
273
1.92k
    if (!tmp)
274
0
      return FALSE;
275
276
1.92k
    context->BitmapData = tmp;
277
1.92k
    context->BitmapDataLength = blength;
278
1.92k
  }
279
280
1.94k
  const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
281
1.94k
  const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
282
  /* The maximum length a decoded plane can reach in all cases */
283
1.94k
  const size_t plength = 1ull * tempWidth * tempHeight;
284
1.94k
  if (plength > UINT32_MAX)
285
0
    return FALSE;
286
287
1.94k
  if (plength > context->priv->PlaneBuffersLength)
288
1.86k
  {
289
9.32k
    for (size_t i = 0; i < 4; i++)
290
7.46k
    {
291
7.46k
      void* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], plength,
292
7.46k
                                                sizeof(BYTE), 32);
293
294
7.46k
      if (!tmp)
295
0
        return FALSE;
296
297
7.46k
      context->priv->PlaneBuffers[i] = tmp;
298
7.46k
    }
299
300
1.86k
    context->priv->PlaneBuffersLength = (UINT32)plength;
301
1.86k
  }
302
303
9.72k
  for (size_t i = 0; i < 4; i++)
304
7.78k
    context->OrgByteCount[i] = context->width * context->height;
305
306
1.94k
  if (context->ChromaSubsamplingLevel)
307
1.15k
  {
308
1.15k
    context->OrgByteCount[0] = tempWidth * context->height;
309
1.15k
    context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
310
1.15k
    context->OrgByteCount[2] = context->OrgByteCount[1];
311
1.15k
  }
312
313
1.94k
  return TRUE;
314
1.94k
}
315
316
static void nsc_profiler_print(NSC_CONTEXT_PRIV* WINPR_RESTRICT priv)
317
19.4k
{
318
19.4k
  WINPR_UNUSED(priv);
319
320
19.4k
  PROFILER_PRINT_HEADER
321
19.4k
  PROFILER_PRINT(priv->prof_nsc_rle_decompress_data)
322
19.4k
  PROFILER_PRINT(priv->prof_nsc_decode)
323
19.4k
  PROFILER_PRINT(priv->prof_nsc_rle_compress_data)
324
19.4k
  PROFILER_PRINT(priv->prof_nsc_encode)
325
19.4k
  PROFILER_PRINT_FOOTER
326
19.4k
}
327
328
BOOL nsc_context_reset(NSC_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
329
0
{
330
0
  if (!context)
331
0
    return FALSE;
332
333
0
  if ((width > UINT16_MAX) || (height > UINT16_MAX))
334
0
    return FALSE;
335
336
0
  context->width = (UINT16)width;
337
0
  context->height = (UINT16)height;
338
0
  return TRUE;
339
0
}
340
341
NSC_CONTEXT* nsc_context_new(void)
342
19.4k
{
343
19.4k
  NSC_CONTEXT* context = (NSC_CONTEXT*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT), 32);
344
345
19.4k
  if (!context)
346
0
    return nullptr;
347
348
19.4k
  context->priv = (NSC_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT_PRIV), 32);
349
350
19.4k
  if (!context->priv)
351
0
    goto error;
352
353
19.4k
  context->priv->log = WLog_Get("com.freerdp.codec.nsc");
354
19.4k
  context->BitmapData = nullptr;
355
19.4k
  context->decode = nsc_decode;
356
19.4k
  context->encode = nsc_encode;
357
358
19.4k
  PROFILER_CREATE(context->priv->prof_nsc_rle_decompress_data, "nsc_rle_decompress_data")
359
19.4k
  PROFILER_CREATE(context->priv->prof_nsc_decode, "nsc_decode")
360
19.4k
  PROFILER_CREATE(context->priv->prof_nsc_rle_compress_data, "nsc_rle_compress_data")
361
19.4k
  PROFILER_CREATE(context->priv->prof_nsc_encode, "nsc_encode")
362
  /* Default encoding parameters */
363
19.4k
  context->ColorLossLevel = 3;
364
19.4k
  context->ChromaSubsamplingLevel = 1;
365
  /* init optimized methods */
366
19.4k
  nsc_init_sse2(context);
367
19.4k
  nsc_init_neon(context);
368
19.4k
  return context;
369
0
error:
370
0
  WINPR_PRAGMA_DIAG_PUSH
371
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
372
0
  nsc_context_free(context);
373
0
  WINPR_PRAGMA_DIAG_POP
374
0
  return nullptr;
375
19.4k
}
376
377
void nsc_context_free(NSC_CONTEXT* context)
378
19.4k
{
379
19.4k
  if (!context)
380
0
    return;
381
382
19.4k
  if (context->priv)
383
19.4k
  {
384
116k
    for (size_t i = 0; i < 5; i++)
385
97.1k
      winpr_aligned_free(context->priv->PlaneBuffers[i]);
386
387
19.4k
    nsc_profiler_print(context->priv);
388
19.4k
    PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
389
19.4k
    PROFILER_FREE(context->priv->prof_nsc_decode)
390
19.4k
    PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
391
19.4k
    PROFILER_FREE(context->priv->prof_nsc_encode)
392
19.4k
    winpr_aligned_free(context->priv);
393
19.4k
  }
394
395
19.4k
  winpr_aligned_free(context->BitmapData);
396
19.4k
  winpr_aligned_free(context);
397
19.4k
}
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
35.3k
{
409
35.3k
  if (!context)
410
0
    return FALSE;
411
412
35.3k
  switch (what)
413
35.3k
  {
414
0
    case NSC_COLOR_LOSS_LEVEL:
415
0
      context->ColorLossLevel = value;
416
0
      break;
417
0
    case NSC_ALLOW_SUBSAMPLING:
418
0
      context->ChromaSubsamplingLevel = value;
419
0
      break;
420
0
    case NSC_DYNAMIC_COLOR_FIDELITY:
421
0
      context->DynamicColorFidelity = value != 0;
422
0
      break;
423
35.3k
    case NSC_COLOR_FORMAT:
424
35.3k
      context->format = value;
425
35.3k
      break;
426
0
    default:
427
0
      return FALSE;
428
35.3k
  }
429
35.3k
  return TRUE;
430
35.3k
}
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
2.51k
{
437
2.51k
  WINPR_ASSERT(context);
438
2.51k
  WINPR_ASSERT(context->priv);
439
440
2.51k
  wStream sbuffer = WINPR_C_ARRAY_INIT;
441
2.51k
  BOOL ret = 0;
442
2.51k
  if (!data || !pDstData)
443
0
  {
444
0
    WLog_Print(context->priv->log, WLOG_ERROR, "Invalid argument: data=%p, pDstData=%p",
445
0
               (const void*)data, (void*)pDstData);
446
0
    return FALSE;
447
0
  }
448
449
2.51k
  if (nXDst > nWidth)
450
0
  {
451
0
    WLog_Print(context->priv->log, WLOG_ERROR, "nXDst %" PRIu32 " > nWidth %" PRIu32, nXDst,
452
0
               nWidth);
453
0
    return FALSE;
454
0
  }
455
2.51k
  if (nYDst > nHeight)
456
0
  {
457
0
    WLog_Print(context->priv->log, WLOG_ERROR, "nYDst %" PRIu32 " > nHeight %" PRIu32, nYDst,
458
0
               nHeight);
459
0
    return FALSE;
460
0
  }
461
462
2.51k
  wStream* s = Stream_StaticConstInit(&sbuffer, data, length);
463
2.51k
  if (!s)
464
0
    return FALSE;
465
466
2.51k
  const UINT32 minStride = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
467
2.51k
  if (nDstStride == 0)
468
757
    nDstStride = minStride;
469
2.51k
  if (nDstStride < minStride)
470
0
  {
471
0
    WLog_Print(context->priv->log, WLOG_ERROR,
472
0
               "nDstStride %" PRIu32 " < minimum stride %" PRIu32, nDstStride, minStride);
473
0
    return FALSE;
474
0
  }
475
476
2.51k
  switch (bpp)
477
2.51k
  {
478
1.34k
    case 32:
479
1.34k
      context->format = PIXEL_FORMAT_BGRA32;
480
1.34k
      break;
481
482
587
    case 24:
483
587
      context->format = PIXEL_FORMAT_BGR24;
484
587
      break;
485
486
587
    case 16:
487
587
      context->format = PIXEL_FORMAT_BGR16;
488
587
      break;
489
490
0
    case 8:
491
0
      context->format = PIXEL_FORMAT_RGB8;
492
0
      break;
493
494
0
    case 4:
495
0
      context->format = PIXEL_FORMAT_A4;
496
0
      break;
497
498
0
    default:
499
0
      return FALSE;
500
2.51k
  }
501
502
2.51k
  context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
503
2.51k
  context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
504
2.51k
  ret = nsc_context_initialize(context, s);
505
506
2.51k
  if (!ret)
507
573
    return FALSE;
508
509
  /* RLE decode */
510
1.94k
  {
511
1.94k
    BOOL rc = 0;
512
1.94k
    PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
513
1.94k
    rc = nsc_rle_decompress_data(context);
514
1.94k
    PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
515
516
1.94k
    if (!rc)
517
1.05k
      return FALSE;
518
1.94k
  }
519
  /* Colorloss recover, Chroma supersample and AYCoCg to ARGB Conversion in one step */
520
890
  {
521
890
    BOOL rc = 0;
522
890
    PROFILER_ENTER(context->priv->prof_nsc_decode)
523
890
    rc = context->decode(context);
524
890
    PROFILER_EXIT(context->priv->prof_nsc_decode)
525
526
890
    if (!rc)
527
0
      return FALSE;
528
890
  }
529
530
890
  uint32_t cwidth = width;
531
890
  if (1ull * nXDst + width > nWidth)
532
0
    cwidth = nWidth - nXDst;
533
534
890
  uint32_t cheight = height;
535
890
  if (1ull * nYDst + height > nHeight)
536
0
    cheight = nHeight - nYDst;
537
538
890
  return (freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStride, nXDst, nYDst, cwidth,
539
890
                                        cheight, context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0,
540
890
                                        0, nullptr, flip));
541
890
}