Coverage Report

Created: 2024-05-20 06:11

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