Coverage Report

Created: 2025-10-10 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
350
{
46
350
  size_t pos = 0;
47
48
350
  if (!context)
49
0
    return FALSE;
50
51
350
  const UINT16 rw = ROUND_UP_TO(context->width, 8);
52
350
  WINPR_ASSERT(context->ColorLossLevel >= 1);
53
700
  const BYTE shift = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel -
54
700
                                                        1); /* colorloss recovery + YCoCg shift */
55
700
  BYTE* bmpdata = context->BitmapData;
56
57
700
  if (!bmpdata)
58
0
    return FALSE;
59
60
2.19k
  for (size_t y = 0; y < context->height; y++)
61
1.84k
  {
62
1.84k
    const BYTE* yplane = NULL;
63
1.84k
    const BYTE* coplane = NULL;
64
1.84k
    const BYTE* cgplane = NULL;
65
1.84k
    const BYTE* aplane = context->priv->PlaneBuffers[3] + y * context->width; /* A */
66
67
1.84k
    if (context->ChromaSubsamplingLevel)
68
1.16k
    {
69
1.16k
      yplane = context->priv->PlaneBuffers[0] + y * rw;                /* Y */
70
1.16k
      coplane = context->priv->PlaneBuffers[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
71
1.16k
      cgplane = context->priv->PlaneBuffers[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
72
1.16k
    }
73
678
    else
74
678
    {
75
678
      yplane = context->priv->PlaneBuffers[0] + y * context->width;  /* Y */
76
678
      coplane = context->priv->PlaneBuffers[1] + y * context->width; /* Co */
77
678
      cgplane = context->priv->PlaneBuffers[2] + y * context->width; /* Cg */
78
678
    }
79
80
34.8k
    for (UINT32 x = 0; x < context->width; x++)
81
33.0k
    {
82
33.0k
      INT16 y_val = (INT16)*yplane;
83
33.0k
      INT16 co_val = (INT16)(INT8)(((INT16)*coplane) << shift);
84
33.0k
      INT16 cg_val = (INT16)(INT8)(((INT16)*cgplane) << shift);
85
66.0k
      INT16 r_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + co_val - cg_val);
86
66.0k
      INT16 g_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val + cg_val);
87
66.0k
      INT16 b_val = WINPR_ASSERTING_INT_CAST(int16_t, y_val - co_val - cg_val);
88
89
66.0k
      if (pos + 4 > context->BitmapDataLength)
90
0
        return FALSE;
91
92
33.0k
      pos += 4;
93
33.0k
      *bmpdata++ = MINMAX(b_val, 0, 0xFF);
94
33.0k
      *bmpdata++ = MINMAX(g_val, 0, 0xFF);
95
33.0k
      *bmpdata++ = MINMAX(r_val, 0, 0xFF);
96
33.0k
      *bmpdata++ = *aplane;
97
33.0k
      yplane++;
98
33.0k
      coplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
99
33.0k
      cgplane += (context->ChromaSubsamplingLevel ? x % 2 : 1);
100
33.0k
      aplane++;
101
33.0k
    }
102
1.84k
  }
103
104
350
  return TRUE;
105
350
}
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
392
{
110
392
  UINT32 left = originalSize;
111
112
3.75k
  while (left > 4)
113
3.53k
  {
114
3.53k
    if (inSize < 1)
115
15
      return FALSE;
116
3.51k
    inSize--;
117
118
3.51k
    const BYTE value = *in++;
119
3.51k
    UINT32 len = 0;
120
121
3.51k
    if (left == 5)
122
154
    {
123
154
      if (outSize < 1)
124
0
        return FALSE;
125
126
154
      outSize--;
127
154
      *out++ = value;
128
154
      left--;
129
154
    }
130
3.36k
    else if (inSize < 1)
131
40
      return FALSE;
132
3.32k
    else if (value == *in)
133
1.27k
    {
134
1.27k
      inSize--;
135
1.27k
      in++;
136
137
1.27k
      if (inSize < 1)
138
6
        return FALSE;
139
1.27k
      else if (*in < 0xFF)
140
1.11k
      {
141
1.11k
        inSize--;
142
1.11k
        len = (UINT32)*in++;
143
1.11k
        len += 2;
144
1.11k
      }
145
160
      else
146
160
      {
147
160
        if (inSize < 5)
148
12
          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.25k
      if ((outSize < len) || (left < len))
158
98
        return FALSE;
159
160
1.16k
      outSize -= len;
161
1.16k
      FillMemory(out, len, value);
162
1.16k
      out += len;
163
1.16k
      left -= len;
164
1.16k
    }
165
2.04k
    else
166
2.04k
    {
167
2.04k
      if (outSize < 1)
168
0
        return FALSE;
169
170
2.04k
      outSize--;
171
2.04k
      *out++ = value;
172
2.04k
      left--;
173
2.04k
    }
174
3.51k
  }
175
176
221
  if ((outSize < 4) || (left < 4))
177
21
    return FALSE;
178
179
200
  if (inSize < 4)
180
18
    return FALSE;
181
182
  memcpy(out, in, 4);
182
182
  return TRUE;
183
200
}
184
185
static BOOL nsc_rle_decompress_data(NSC_CONTEXT* WINPR_RESTRICT context)
186
560
{
187
560
  if (!context)
188
0
    return FALSE;
189
190
560
  const BYTE* rle = context->Planes;
191
560
  size_t rleSize = context->PlanesSize;
192
560
  WINPR_ASSERT(rle);
193
194
2.08k
  for (size_t i = 0; i < 4; i++)
195
1.73k
  {
196
1.73k
    const UINT32 originalSize = context->OrgByteCount[i];
197
1.73k
    const UINT32 planeSize = context->PlaneByteCount[i];
198
199
1.73k
    if (rleSize < planeSize)
200
0
      return FALSE;
201
202
1.73k
    if (planeSize == 0)
203
1.06k
    {
204
1.06k
      if (context->priv->PlaneBuffersLength < originalSize)
205
0
        return FALSE;
206
207
1.06k
      FillMemory(context->priv->PlaneBuffers[i], originalSize, 0xFF);
208
1.06k
    }
209
671
    else if (planeSize < originalSize)
210
392
    {
211
392
      if (!nsc_rle_decode(rle, rleSize, context->priv->PlaneBuffers[i],
212
392
                          context->priv->PlaneBuffersLength, originalSize))
213
210
        return FALSE;
214
392
    }
215
279
    else
216
279
    {
217
279
      if (context->priv->PlaneBuffersLength < originalSize)
218
0
        return FALSE;
219
220
279
      if (rleSize < originalSize)
221
0
        return FALSE;
222
223
279
      CopyMemory(context->priv->PlaneBuffers[i], rle, originalSize);
224
279
    }
225
226
1.52k
    rle += planeSize;
227
1.52k
    rleSize -= planeSize;
228
1.52k
  }
229
230
350
  return TRUE;
231
560
}
232
233
static BOOL nsc_stream_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
234
718
{
235
718
  WINPR_ASSERT(context);
236
718
  WINPR_ASSERT(context->priv);
237
718
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 20))
238
75
    return FALSE;
239
240
643
  size_t total = 0;
241
3.21k
  for (size_t i = 0; i < 4; i++)
242
2.57k
  {
243
2.57k
    Stream_Read_UINT32(s, context->PlaneByteCount[i]);
244
2.57k
    total += context->PlaneByteCount[i];
245
2.57k
  }
246
247
643
  Stream_Read_UINT8(s, context->ColorLossLevel);         /* ColorLossLevel (1 byte) */
248
643
  if ((context->ColorLossLevel < 1) || (context->ColorLossLevel > 7))
249
48
  {
250
48
    WLog_Print(context->priv->log, WLOG_ERROR,
251
48
               "ColorLossLevel=%" PRIu8 " out of range, must be [1,7] inclusive",
252
48
               context->ColorLossLevel);
253
48
    return FALSE;
254
48
  }
255
595
  Stream_Read_UINT8(s, context->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
256
595
  Stream_Seek(s, 2);                                     /* Reserved (2 bytes) */
257
595
  context->Planes = Stream_Pointer(s);
258
595
  context->PlanesSize = total;
259
595
  return Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, total);
260
643
}
261
262
static BOOL nsc_context_initialize(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s)
263
718
{
264
718
  if (!nsc_stream_initialize(context, s))
265
158
    return FALSE;
266
267
560
  const size_t blength = 4ull * context->width * context->height;
268
269
560
  if (!context->BitmapData || (blength > context->BitmapDataLength))
270
544
  {
271
544
    void* tmp = winpr_aligned_recalloc(context->BitmapData, blength + 16, sizeof(BYTE), 32);
272
273
544
    if (!tmp)
274
0
      return FALSE;
275
276
544
    context->BitmapData = tmp;
277
544
    context->BitmapDataLength = blength;
278
544
  }
279
280
560
  const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
281
560
  const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
282
  /* The maximum length a decoded plane can reach in all cases */
283
560
  const size_t plength = 1ull * tempWidth * tempHeight;
284
560
  if (plength > UINT32_MAX)
285
0
    return FALSE;
286
287
560
  if (plength > context->priv->PlaneBuffersLength)
288
489
  {
289
2.44k
    for (size_t i = 0; i < 4; i++)
290
1.95k
    {
291
1.95k
      void* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], plength,
292
1.95k
                                                sizeof(BYTE), 32);
293
294
1.95k
      if (!tmp)
295
0
        return FALSE;
296
297
1.95k
      context->priv->PlaneBuffers[i] = tmp;
298
1.95k
    }
299
300
489
    context->priv->PlaneBuffersLength = (UINT32)plength;
301
489
  }
302
303
2.80k
  for (size_t i = 0; i < 4; i++)
304
2.24k
    context->OrgByteCount[i] = context->width * context->height;
305
306
560
  if (context->ChromaSubsamplingLevel)
307
292
  {
308
292
    context->OrgByteCount[0] = tempWidth * context->height;
309
292
    context->OrgByteCount[1] = (tempWidth >> 1) * (tempHeight >> 1);
310
292
    context->OrgByteCount[2] = context->OrgByteCount[1];
311
292
  }
312
313
560
  return TRUE;
314
560
}
315
316
static void nsc_profiler_print(NSC_CONTEXT_PRIV* WINPR_RESTRICT priv)
317
17.2k
{
318
17.2k
  WINPR_UNUSED(priv);
319
320
17.2k
  PROFILER_PRINT_HEADER
321
17.2k
  PROFILER_PRINT(priv->prof_nsc_rle_decompress_data)
322
17.2k
  PROFILER_PRINT(priv->prof_nsc_decode)
323
17.2k
  PROFILER_PRINT(priv->prof_nsc_rle_compress_data)
324
17.2k
  PROFILER_PRINT(priv->prof_nsc_encode)
325
17.2k
  PROFILER_PRINT_FOOTER
326
17.2k
}
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.2k
{
343
17.2k
  NSC_CONTEXT* context = (NSC_CONTEXT*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT), 32);
344
345
17.2k
  if (!context)
346
0
    return NULL;
347
348
17.2k
  context->priv = (NSC_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(NSC_CONTEXT_PRIV), 32);
349
350
17.2k
  if (!context->priv)
351
0
    goto error;
352
353
17.2k
  context->priv->log = WLog_Get("com.freerdp.codec.nsc");
354
17.2k
  WLog_OpenAppender(context->priv->log);
355
17.2k
  context->BitmapData = NULL;
356
17.2k
  context->decode = nsc_decode;
357
17.2k
  context->encode = nsc_encode;
358
359
17.2k
  PROFILER_CREATE(context->priv->prof_nsc_rle_decompress_data, "nsc_rle_decompress_data")
360
17.2k
  PROFILER_CREATE(context->priv->prof_nsc_decode, "nsc_decode")
361
17.2k
  PROFILER_CREATE(context->priv->prof_nsc_rle_compress_data, "nsc_rle_compress_data")
362
17.2k
  PROFILER_CREATE(context->priv->prof_nsc_encode, "nsc_encode")
363
  /* Default encoding parameters */
364
17.2k
  context->ColorLossLevel = 3;
365
17.2k
  context->ChromaSubsamplingLevel = 1;
366
  /* init optimized methods */
367
17.2k
  nsc_init_sse2(context);
368
17.2k
  nsc_init_neon(context);
369
17.2k
  return context;
370
0
error:
371
0
  WINPR_PRAGMA_DIAG_PUSH
372
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
373
0
  nsc_context_free(context);
374
0
  WINPR_PRAGMA_DIAG_POP
375
0
  return NULL;
376
17.2k
}
377
378
void nsc_context_free(NSC_CONTEXT* context)
379
17.2k
{
380
17.2k
  if (!context)
381
0
    return;
382
383
17.2k
  if (context->priv)
384
17.2k
  {
385
103k
    for (size_t i = 0; i < 5; i++)
386
86.1k
      winpr_aligned_free(context->priv->PlaneBuffers[i]);
387
388
17.2k
    nsc_profiler_print(context->priv);
389
17.2k
    PROFILER_FREE(context->priv->prof_nsc_rle_decompress_data)
390
17.2k
    PROFILER_FREE(context->priv->prof_nsc_decode)
391
17.2k
    PROFILER_FREE(context->priv->prof_nsc_rle_compress_data)
392
17.2k
    PROFILER_FREE(context->priv->prof_nsc_encode)
393
17.2k
    winpr_aligned_free(context->priv);
394
17.2k
  }
395
396
17.2k
  winpr_aligned_free(context->BitmapData);
397
17.2k
  winpr_aligned_free(context);
398
17.2k
}
399
400
#if defined(WITH_FREERDP_DEPRECATED)
401
BOOL nsc_context_set_pixel_format(NSC_CONTEXT* context, UINT32 pixel_format)
402
{
403
  return nsc_context_set_parameters(context, NSC_COLOR_FORMAT, pixel_format);
404
}
405
#endif
406
407
BOOL nsc_context_set_parameters(NSC_CONTEXT* WINPR_RESTRICT context, NSC_PARAMETER what,
408
                                UINT32 value)
409
34.4k
{
410
34.4k
  if (!context)
411
0
    return FALSE;
412
413
34.4k
  switch (what)
414
34.4k
  {
415
0
    case NSC_COLOR_LOSS_LEVEL:
416
0
      context->ColorLossLevel = value;
417
0
      break;
418
0
    case NSC_ALLOW_SUBSAMPLING:
419
0
      context->ChromaSubsamplingLevel = value;
420
0
      break;
421
0
    case NSC_DYNAMIC_COLOR_FIDELITY:
422
0
      context->DynamicColorFidelity = value != 0;
423
0
      break;
424
34.4k
    case NSC_COLOR_FORMAT:
425
34.4k
      context->format = value;
426
34.4k
      break;
427
0
    default:
428
0
      return FALSE;
429
34.4k
  }
430
34.4k
  return TRUE;
431
34.4k
}
432
433
BOOL nsc_process_message(NSC_CONTEXT* WINPR_RESTRICT context, UINT16 bpp, UINT32 width,
434
                         UINT32 height, const BYTE* data, UINT32 length,
435
                         BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStride,
436
                         UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
437
                         WINPR_ATTR_UNUSED UINT32 nHeight, UINT32 flip)
438
718
{
439
718
  wStream* s = NULL;
440
718
  wStream sbuffer = { 0 };
441
718
  BOOL ret = 0;
442
718
  if (!context || !data || !pDstData)
443
0
    return FALSE;
444
445
718
  s = Stream_StaticConstInit(&sbuffer, data, length);
446
447
718
  if (!s)
448
0
    return FALSE;
449
450
718
  if (nDstStride == 0)
451
718
    nDstStride = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
452
453
718
  switch (bpp)
454
718
  {
455
718
    case 32:
456
718
      context->format = PIXEL_FORMAT_BGRA32;
457
718
      break;
458
459
0
    case 24:
460
0
      context->format = PIXEL_FORMAT_BGR24;
461
0
      break;
462
463
0
    case 16:
464
0
      context->format = PIXEL_FORMAT_BGR16;
465
0
      break;
466
467
0
    case 8:
468
0
      context->format = PIXEL_FORMAT_RGB8;
469
0
      break;
470
471
0
    case 4:
472
0
      context->format = PIXEL_FORMAT_A4;
473
0
      break;
474
475
0
    default:
476
0
      return FALSE;
477
718
  }
478
479
1.43k
  context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
480
1.43k
  context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
481
1.43k
  ret = nsc_context_initialize(context, s);
482
483
1.43k
  if (!ret)
484
158
    return FALSE;
485
486
  /* RLE decode */
487
560
  {
488
560
    BOOL rc = 0;
489
560
    PROFILER_ENTER(context->priv->prof_nsc_rle_decompress_data)
490
560
    rc = nsc_rle_decompress_data(context);
491
560
    PROFILER_EXIT(context->priv->prof_nsc_rle_decompress_data)
492
493
560
    if (!rc)
494
210
      return FALSE;
495
560
  }
496
  /* Colorloss recover, Chroma supersample and AYCoCg to ARGB Conversion in one step */
497
350
  {
498
350
    BOOL rc = 0;
499
350
    PROFILER_ENTER(context->priv->prof_nsc_decode)
500
350
    rc = context->decode(context);
501
350
    PROFILER_EXIT(context->priv->prof_nsc_decode)
502
503
350
    if (!rc)
504
0
      return FALSE;
505
350
  }
506
507
350
  if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStride, nXDst, nYDst, width, height,
508
350
                                     context->BitmapData, PIXEL_FORMAT_BGRA32, 0, 0, 0, NULL,
509
350
                                     flip))
510
0
    return FALSE;
511
512
350
  return TRUE;
513
350
}