Coverage Report

Created: 2026-04-12 06:59

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