Coverage Report

Created: 2026-08-31 06:25

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