Coverage Report

Created: 2026-09-14 06:31

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