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_encode.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * NSCodec Encoder
4
 *
5
 * Copyright 2012 Vic Lee
6
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2016 Thincast Technologies GmbH
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include <winpr/assert.h>
29
#include <winpr/cast.h>
30
#include <winpr/crt.h>
31
32
#include <freerdp/codec/nsc.h>
33
#include <freerdp/codec/color.h>
34
35
#include "nsc_types.h"
36
#include "nsc_encode.h"
37
38
typedef struct
39
{
40
  UINT32 x;
41
  UINT32 y;
42
  UINT32 width;
43
  UINT32 height;
44
  const BYTE* data;
45
  UINT32 scanline;
46
  BYTE* PlaneBuffer;
47
  UINT32 MaxPlaneSize;
48
  BYTE* PlaneBuffers[5];
49
  UINT32 OrgByteCount[4];
50
51
  UINT32 LumaPlaneByteCount;
52
  UINT32 OrangeChromaPlaneByteCount;
53
  UINT32 GreenChromaPlaneByteCount;
54
  UINT32 AlphaPlaneByteCount;
55
  UINT8 ColorLossLevel;
56
  UINT8 ChromaSubsamplingLevel;
57
} NSC_MESSAGE;
58
59
static BOOL nsc_write_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
60
                              const NSC_MESSAGE* WINPR_RESTRICT message);
61
62
static BOOL nsc_context_initialize_encode(NSC_CONTEXT* WINPR_RESTRICT context)
63
0
{
64
0
  const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
65
0
  const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
66
  /* The maximum length a decoded plane can reach in all cases */
67
0
  const UINT32 length = tempWidth * tempHeight + 16;
68
69
0
  if (length > context->priv->PlaneBuffersLength)
70
0
  {
71
0
    for (int i = 0; i < 5; i++)
72
0
    {
73
0
      BYTE* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], length,
74
0
                                                sizeof(BYTE), 32);
75
76
0
      if (!tmp)
77
0
      {
78
0
        nsc_context_planebuffers_free(context->priv);
79
0
        return FALSE;
80
0
      }
81
82
0
      context->priv->PlaneBuffers[i] = tmp;
83
0
    }
84
85
0
    context->priv->PlaneBuffersLength = length;
86
0
  }
87
88
0
  if (context->ChromaSubsamplingLevel)
89
0
  {
90
0
    context->OrgByteCount[0] = tempWidth * context->height;
91
0
    context->OrgByteCount[1] = tempWidth * tempHeight / 4;
92
0
    context->OrgByteCount[2] = tempWidth * tempHeight / 4;
93
0
    context->OrgByteCount[3] = context->width * context->height;
94
0
  }
95
0
  else
96
0
  {
97
0
    context->OrgByteCount[0] = context->width * context->height;
98
0
    context->OrgByteCount[1] = context->width * context->height;
99
0
    context->OrgByteCount[2] = context->width * context->height;
100
0
    context->OrgByteCount[3] = context->width * context->height;
101
0
  }
102
103
0
  return TRUE;
104
0
}
105
106
static BOOL nsc_encode_argb_to_aycocg(NSC_CONTEXT* WINPR_RESTRICT context,
107
                                      const BYTE* WINPR_RESTRICT data, UINT32 scanline)
108
0
{
109
0
  size_t y = 0;
110
0
  const BYTE* src = nullptr;
111
0
  BYTE* yplane = nullptr;
112
0
  BYTE* coplane = nullptr;
113
0
  BYTE* cgplane = nullptr;
114
0
  BYTE* aplane = nullptr;
115
0
  INT16 r_val = 0;
116
0
  INT16 g_val = 0;
117
0
  INT16 b_val = 0;
118
0
  BYTE a_val = 0;
119
120
0
  UINT16 tempWidth = ROUND_UP_TO(context->width, 8);
121
0
  const UINT16 rw = (context->ChromaSubsamplingLevel ? tempWidth : context->width);
122
0
  const BYTE ccl = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel);
123
124
0
  for (; y < context->height; y++)
125
0
  {
126
0
    src = data + (context->height - 1 - y) * scanline;
127
0
    yplane = context->priv->PlaneBuffers[0] + y * rw;
128
0
    coplane = context->priv->PlaneBuffers[1] + y * rw;
129
0
    cgplane = context->priv->PlaneBuffers[2] + y * rw;
130
0
    aplane = context->priv->PlaneBuffers[3] + y * context->width;
131
132
0
    UINT16 x = 0;
133
0
    for (; x < context->width; x++)
134
0
    {
135
0
      switch (context->format)
136
0
      {
137
0
        case PIXEL_FORMAT_BGRX32:
138
0
          b_val = *src++;
139
0
          g_val = *src++;
140
0
          r_val = *src++;
141
0
          src++;
142
0
          a_val = 0xFF;
143
0
          break;
144
145
0
        case PIXEL_FORMAT_BGRA32:
146
0
          b_val = *src++;
147
0
          g_val = *src++;
148
0
          r_val = *src++;
149
0
          a_val = *src++;
150
0
          break;
151
152
0
        case PIXEL_FORMAT_RGBX32:
153
0
          r_val = *src++;
154
0
          g_val = *src++;
155
0
          b_val = *src++;
156
0
          src++;
157
0
          a_val = 0xFF;
158
0
          break;
159
160
0
        case PIXEL_FORMAT_RGBA32:
161
0
          r_val = *src++;
162
0
          g_val = *src++;
163
0
          b_val = *src++;
164
0
          a_val = *src++;
165
0
          break;
166
167
0
        case PIXEL_FORMAT_BGR24:
168
0
          b_val = *src++;
169
0
          g_val = *src++;
170
0
          r_val = *src++;
171
0
          a_val = 0xFF;
172
0
          break;
173
174
0
        case PIXEL_FORMAT_RGB24:
175
0
          r_val = *src++;
176
0
          g_val = *src++;
177
0
          b_val = *src++;
178
0
          a_val = 0xFF;
179
0
          break;
180
181
0
        case PIXEL_FORMAT_BGR16:
182
0
          b_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
183
0
          g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
184
0
          r_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
185
0
          a_val = 0xFF;
186
0
          src += 2;
187
0
          break;
188
189
0
        case PIXEL_FORMAT_RGB16:
190
0
          r_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
191
0
          g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
192
0
          b_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
193
0
          a_val = 0xFF;
194
0
          src += 2;
195
0
          break;
196
197
0
        case PIXEL_FORMAT_A4:
198
0
        {
199
0
          int shift = 0;
200
0
          BYTE idx = 0;
201
0
          shift = (7 - (x % 8));
202
0
          idx = ((*src) >> shift) & 1;
203
0
          idx |= (((*(src + 1)) >> shift) & 1) << 1;
204
0
          idx |= (((*(src + 2)) >> shift) & 1) << 2;
205
0
          idx |= (((*(src + 3)) >> shift) & 1) << 3;
206
0
          idx *= 3;
207
0
          r_val = (INT16)context->palette[idx];
208
0
          g_val = (INT16)context->palette[idx + 1];
209
0
          b_val = (INT16)context->palette[idx + 2];
210
211
0
          if (shift == 0)
212
0
            src += 4;
213
0
        }
214
215
0
          a_val = 0xFF;
216
0
          break;
217
218
0
        case PIXEL_FORMAT_RGB8:
219
0
        {
220
0
          int idx = (*src) * 3;
221
0
          r_val = (INT16)context->palette[idx];
222
0
          g_val = (INT16)context->palette[idx + 1];
223
0
          b_val = (INT16)context->palette[idx + 2];
224
0
          src++;
225
0
        }
226
227
0
          a_val = 0xFF;
228
0
          break;
229
230
0
        default:
231
0
          r_val = g_val = b_val = a_val = 0;
232
0
          break;
233
0
      }
234
235
0
      *yplane++ = (BYTE)((r_val >> 2) + (g_val >> 1) + (b_val >> 2));
236
      /* Perform color loss reduction here */
237
0
      *coplane++ = (BYTE)((r_val - b_val) >> ccl);
238
0
      *cgplane++ = (BYTE)((-(r_val >> 1) + g_val - (b_val >> 1)) >> ccl);
239
0
      *aplane++ = a_val;
240
0
    }
241
242
0
    if (context->ChromaSubsamplingLevel && (x % 2) == 1)
243
0
    {
244
0
      *yplane = *(yplane - 1);
245
0
      *coplane = *(coplane - 1);
246
0
      *cgplane = *(cgplane - 1);
247
0
    }
248
0
  }
249
250
0
  if (context->ChromaSubsamplingLevel && (y % 2) == 1)
251
0
  {
252
0
    yplane = context->priv->PlaneBuffers[0] + y * rw;
253
0
    coplane = context->priv->PlaneBuffers[1] + y * rw;
254
0
    cgplane = context->priv->PlaneBuffers[2] + y * rw;
255
0
    CopyMemory(yplane, yplane - rw, rw);
256
0
    CopyMemory(coplane, coplane - rw, rw);
257
0
    CopyMemory(cgplane, cgplane - rw, rw);
258
0
  }
259
260
0
  return TRUE;
261
0
}
262
263
static BOOL nsc_encode_subsampling(NSC_CONTEXT* WINPR_RESTRICT context)
264
0
{
265
0
  UINT32 tempWidth = 0;
266
0
  UINT32 tempHeight = 0;
267
268
0
  if (!context)
269
0
    return FALSE;
270
271
0
  tempWidth = ROUND_UP_TO(context->width, 8);
272
0
  tempHeight = ROUND_UP_TO(context->height, 2);
273
274
0
  if (tempHeight == 0)
275
0
    return FALSE;
276
277
0
  if (tempWidth > context->priv->PlaneBuffersLength / tempHeight)
278
0
    return FALSE;
279
280
0
  for (size_t y = 0; y < tempHeight >> 1; y++)
281
0
  {
282
0
    BYTE* co_dst = context->priv->PlaneBuffers[1] + y * (tempWidth >> 1);
283
0
    BYTE* cg_dst = context->priv->PlaneBuffers[2] + y * (tempWidth >> 1);
284
0
    const INT8* co_src0 = (INT8*)context->priv->PlaneBuffers[1] + (y << 1) * tempWidth;
285
0
    const INT8* co_src1 = co_src0 + tempWidth;
286
0
    const INT8* cg_src0 = (INT8*)context->priv->PlaneBuffers[2] + (y << 1) * tempWidth;
287
0
    const INT8* cg_src1 = cg_src0 + tempWidth;
288
289
0
    for (UINT32 x = 0; x < tempWidth >> 1; x++)
290
0
    {
291
0
      *co_dst++ = (BYTE)(((INT16)*co_src0 + (INT16) * (co_src0 + 1) + (INT16)*co_src1 +
292
0
                          (INT16) * (co_src1 + 1)) >>
293
0
                         2);
294
0
      *cg_dst++ = (BYTE)(((INT16)*cg_src0 + (INT16) * (cg_src0 + 1) + (INT16)*cg_src1 +
295
0
                          (INT16) * (cg_src1 + 1)) >>
296
0
                         2);
297
0
      co_src0 += 2;
298
0
      co_src1 += 2;
299
0
      cg_src0 += 2;
300
0
      cg_src1 += 2;
301
0
    }
302
0
  }
303
304
0
  return TRUE;
305
0
}
306
307
BOOL nsc_encode(NSC_CONTEXT* WINPR_RESTRICT context, const BYTE* WINPR_RESTRICT bmpdata,
308
                UINT32 rowstride)
309
0
{
310
0
  if (!context || !bmpdata || (rowstride == 0))
311
0
    return FALSE;
312
313
0
  if (!nsc_encode_argb_to_aycocg(context, bmpdata, rowstride))
314
0
    return FALSE;
315
316
0
  if (context->ChromaSubsamplingLevel)
317
0
  {
318
0
    if (!nsc_encode_subsampling(context))
319
0
      return FALSE;
320
0
  }
321
322
0
  return TRUE;
323
0
}
324
325
static UINT32 nsc_rle_encode(const BYTE* WINPR_RESTRICT in, BYTE* WINPR_RESTRICT out,
326
                             UINT32 originalSize)
327
0
{
328
0
  UINT32 left = 0;
329
0
  UINT32 runlength = 1;
330
0
  UINT32 planeSize = 0;
331
0
  left = originalSize;
332
333
  /**
334
   * We quit the loop if the running compressed size is larger than the original.
335
   * In such cases data will be sent uncompressed.
336
   */
337
0
  while (left > 4 && planeSize < originalSize - 4)
338
0
  {
339
0
    if (left > 5 && *in == *(in + 1))
340
0
    {
341
0
      runlength++;
342
0
    }
343
0
    else if (runlength == 1)
344
0
    {
345
0
      *out++ = *in;
346
0
      planeSize++;
347
0
    }
348
0
    else if (runlength < 256)
349
0
    {
350
0
      *out++ = *in;
351
0
      *out++ = *in;
352
0
      WINPR_ASSERT(runlength >= 2);
353
0
      *out++ = WINPR_ASSERTING_INT_CAST(BYTE, runlength - 2);
354
0
      runlength = 1;
355
0
      planeSize += 3;
356
0
    }
357
0
    else
358
0
    {
359
0
      *out++ = *in;
360
0
      *out++ = *in;
361
0
      *out++ = 0xFF;
362
0
      *out++ = (runlength & 0x000000FF);
363
0
      *out++ = (runlength & 0x0000FF00) >> 8;
364
0
      *out++ = (runlength & 0x00FF0000) >> 16;
365
0
      *out++ = (runlength & 0xFF000000) >> 24;
366
0
      runlength = 1;
367
0
      planeSize += 7;
368
0
    }
369
370
0
    in++;
371
0
    left--;
372
0
  }
373
374
0
  if (planeSize < originalSize - 4)
375
0
    CopyMemory(out, in, 4);
376
377
0
  planeSize += 4;
378
0
  return planeSize;
379
0
}
380
381
static void nsc_rle_compress_data(NSC_CONTEXT* WINPR_RESTRICT context)
382
0
{
383
0
  UINT32 planeSize = 0;
384
0
  UINT32 originalSize = 0;
385
386
0
  for (UINT16 i = 0; i < 4; i++)
387
0
  {
388
0
    originalSize = context->OrgByteCount[i];
389
390
0
    if (originalSize == 0)
391
0
    {
392
0
      planeSize = 0;
393
0
    }
394
0
    else
395
0
    {
396
0
      planeSize = nsc_rle_encode(context->priv->PlaneBuffers[i],
397
0
                                 context->priv->PlaneBuffers[4], originalSize);
398
399
0
      if (planeSize < originalSize)
400
0
        CopyMemory(context->priv->PlaneBuffers[i], context->priv->PlaneBuffers[4],
401
0
                   planeSize);
402
0
      else
403
0
        planeSize = originalSize;
404
0
    }
405
406
0
    context->PlaneByteCount[i] = planeSize;
407
0
  }
408
0
}
409
410
BOOL nsc_write_message(WINPR_ATTR_UNUSED NSC_CONTEXT* WINPR_RESTRICT context,
411
                       wStream* WINPR_RESTRICT s, const NSC_MESSAGE* WINPR_RESTRICT message)
412
0
{
413
0
  UINT32 totalPlaneByteCount = 0;
414
0
  totalPlaneByteCount = message->LumaPlaneByteCount + message->OrangeChromaPlaneByteCount +
415
0
                        message->GreenChromaPlaneByteCount + message->AlphaPlaneByteCount;
416
417
0
  if (!Stream_EnsureRemainingCapacity(s, 20 + totalPlaneByteCount))
418
0
    return FALSE;
419
420
0
  Stream_Write_UINT32(s, message->LumaPlaneByteCount); /* LumaPlaneByteCount (4 bytes) */
421
0
  Stream_Write_UINT32(
422
0
      s, message->OrangeChromaPlaneByteCount); /* OrangeChromaPlaneByteCount (4 bytes) */
423
0
  Stream_Write_UINT32(
424
0
      s, message->GreenChromaPlaneByteCount);           /* GreenChromaPlaneByteCount (4 bytes) */
425
0
  Stream_Write_UINT32(s, message->AlphaPlaneByteCount); /* AlphaPlaneByteCount (4 bytes) */
426
0
  Stream_Write_UINT8(s, message->ColorLossLevel);       /* ColorLossLevel (1 byte) */
427
0
  Stream_Write_UINT8(s, message->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
428
0
  Stream_Write_UINT16(s, 0);                              /* Reserved (2 bytes) */
429
430
0
  if (message->LumaPlaneByteCount)
431
0
    Stream_Write(s, message->PlaneBuffers[0], message->LumaPlaneByteCount); /* LumaPlane */
432
433
0
  if (message->OrangeChromaPlaneByteCount)
434
0
    Stream_Write(s, message->PlaneBuffers[1],
435
0
                 message->OrangeChromaPlaneByteCount); /* OrangeChromaPlane */
436
437
0
  if (message->GreenChromaPlaneByteCount)
438
0
    Stream_Write(s, message->PlaneBuffers[2],
439
0
                 message->GreenChromaPlaneByteCount); /* GreenChromaPlane */
440
441
0
  if (message->AlphaPlaneByteCount)
442
0
    Stream_Write(s, message->PlaneBuffers[3], message->AlphaPlaneByteCount); /* AlphaPlane */
443
444
0
  return TRUE;
445
0
}
446
447
BOOL nsc_compose_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
448
                         const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
449
                         UINT32 scanline)
450
0
{
451
0
  BOOL rc = 0;
452
0
  NSC_MESSAGE message = WINPR_C_ARRAY_INIT;
453
454
0
  if (!context || !s || !data)
455
0
    return FALSE;
456
457
0
  if (scanline == 0)
458
0
    scanline = width * FreeRDPGetBytesPerPixel(context->format);
459
460
0
  context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
461
0
  context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
462
463
0
  if (!nsc_context_initialize_encode(context))
464
0
    return FALSE;
465
466
  /* ARGB to AYCoCg conversion, chroma subsampling and colorloss reduction */
467
0
  PROFILER_ENTER(context->priv->prof_nsc_encode)
468
0
  rc = context->encode(context, data, scanline);
469
0
  PROFILER_EXIT(context->priv->prof_nsc_encode)
470
0
  if (!rc)
471
0
    return FALSE;
472
473
  /* RLE encode */
474
0
  PROFILER_ENTER(context->priv->prof_nsc_rle_compress_data)
475
0
  nsc_rle_compress_data(context);
476
0
  PROFILER_EXIT(context->priv->prof_nsc_rle_compress_data)
477
0
  message.PlaneBuffers[0] = context->priv->PlaneBuffers[0];
478
0
  message.PlaneBuffers[1] = context->priv->PlaneBuffers[1];
479
0
  message.PlaneBuffers[2] = context->priv->PlaneBuffers[2];
480
0
  message.PlaneBuffers[3] = context->priv->PlaneBuffers[3];
481
0
  message.LumaPlaneByteCount = context->PlaneByteCount[0];
482
0
  message.OrangeChromaPlaneByteCount = context->PlaneByteCount[1];
483
0
  message.GreenChromaPlaneByteCount = context->PlaneByteCount[2];
484
0
  message.AlphaPlaneByteCount = context->PlaneByteCount[3];
485
486
0
  message.ColorLossLevel = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel);
487
0
  message.ChromaSubsamplingLevel =
488
0
      WINPR_ASSERTING_INT_CAST(BYTE, context->ChromaSubsamplingLevel);
489
0
  return nsc_write_message(context, s, &message);
490
0
}
491
492
#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
493
BOOL nsc_decompose_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
494
                           BYTE* WINPR_RESTRICT bmpdata, UINT32 x, UINT32 y, UINT32 width,
495
                           UINT32 height, UINT32 rowstride, UINT32 format, UINT32 flip)
496
0
{
497
0
  size_t size = Stream_GetRemainingLength(s);
498
499
0
  if (size > UINT32_MAX)
500
0
    return FALSE;
501
502
0
  if (!nsc_process_message(context, (UINT16)FreeRDPGetBitsPerPixel(context->format), width,
503
0
                           height, Stream_Pointer(s), (UINT32)size, bmpdata, format, rowstride, x,
504
0
                           y, width, height, flip))
505
0
    return FALSE;
506
0
  Stream_Seek(s, size);
507
0
  return TRUE;
508
0
}
509
#endif