Coverage Report

Created: 2024-09-08 06:18

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