22#include <freerdp/config.h>
28#include <winpr/assert.h>
29#include <winpr/cast.h>
32#include <freerdp/codec/nsc.h>
33#include <freerdp/codec/color.h>
36#include "nsc_encode.h"
48 BYTE* PlaneBuffers[5];
49 UINT32 OrgByteCount[4];
51 UINT32 LumaPlaneByteCount;
52 UINT32 OrangeChromaPlaneByteCount;
53 UINT32 GreenChromaPlaneByteCount;
54 UINT32 AlphaPlaneByteCount;
56 UINT8 ChromaSubsamplingLevel;
59static BOOL nsc_write_message(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
60 const NSC_MESSAGE* WINPR_RESTRICT message);
62static BOOL nsc_context_initialize_encode(NSC_CONTEXT* WINPR_RESTRICT context)
64 const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
65 const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
67 const UINT32 length = tempWidth * tempHeight + 16;
69 if (length > context->priv->PlaneBuffersLength)
71 for (
int i = 0; i < 5; i++)
73 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], length,
78 nsc_context_planebuffers_free(context->priv);
82 context->priv->PlaneBuffers[i] = tmp;
85 context->priv->PlaneBuffersLength = length;
88 if (context->ChromaSubsamplingLevel)
90 context->OrgByteCount[0] = tempWidth * context->height;
91 context->OrgByteCount[1] = tempWidth * tempHeight / 4;
92 context->OrgByteCount[2] = tempWidth * tempHeight / 4;
93 context->OrgByteCount[3] = context->width * context->height;
97 context->OrgByteCount[0] = context->width * context->height;
98 context->OrgByteCount[1] = context->width * context->height;
99 context->OrgByteCount[2] = context->width * context->height;
100 context->OrgByteCount[3] = context->width * context->height;
106static BOOL nsc_encode_argb_to_aycocg(NSC_CONTEXT* WINPR_RESTRICT context,
107 const BYTE* WINPR_RESTRICT data, UINT32 scanline)
110 const BYTE* src =
nullptr;
111 BYTE* yplane =
nullptr;
112 BYTE* coplane =
nullptr;
113 BYTE* cgplane =
nullptr;
114 BYTE* aplane =
nullptr;
120 UINT16 tempWidth = ROUND_UP_TO(context->width, 8);
121 const UINT16 rw = (context->ChromaSubsamplingLevel ? tempWidth : context->width);
122 const BYTE ccl = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel);
124 switch (context->format)
126 case PIXEL_FORMAT_A4:
127 case PIXEL_FORMAT_RGB8:
128 if (!context->palette)
135 for (; y < context->height; y++)
137 src = data + (context->height - 1 - y) * scanline;
138 yplane = context->priv->PlaneBuffers[0] + y * rw;
139 coplane = context->priv->PlaneBuffers[1] + y * rw;
140 cgplane = context->priv->PlaneBuffers[2] + y * rw;
141 aplane = context->priv->PlaneBuffers[3] + y * context->width;
144 for (; x < context->width; x++)
146 switch (context->format)
148 case PIXEL_FORMAT_BGRX32:
156 case PIXEL_FORMAT_BGRA32:
163 case PIXEL_FORMAT_RGBX32:
171 case PIXEL_FORMAT_RGBA32:
178 case PIXEL_FORMAT_BGR24:
185 case PIXEL_FORMAT_RGB24:
192 case PIXEL_FORMAT_BGR16:
193 b_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
194 g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
195 r_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
200 case PIXEL_FORMAT_RGB16:
201 r_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
202 g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
203 b_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
208 case PIXEL_FORMAT_A4:
212 shift = (7 - (x % 8));
213 idx = ((*src) >> shift) & 1;
214 idx |= (((*(src + 1)) >> shift) & 1) << 1;
215 idx |= (((*(src + 2)) >> shift) & 1) << 2;
216 idx |= (((*(src + 3)) >> shift) & 1) << 3;
218 r_val = (INT16)context->palette[idx];
219 g_val = (INT16)context->palette[idx + 1];
220 b_val = (INT16)context->palette[idx + 2];
229 case PIXEL_FORMAT_RGB8:
231 int idx = (*src) * 3;
232 r_val = (INT16)context->palette[idx];
233 g_val = (INT16)context->palette[idx + 1];
234 b_val = (INT16)context->palette[idx + 2];
242 r_val = g_val = b_val = a_val = 0;
246 *yplane++ = (BYTE)((r_val >> 2) + (g_val >> 1) + (b_val >> 2));
248 *coplane++ = (BYTE)((r_val - b_val) >> ccl);
249 *cgplane++ = (BYTE)((-(r_val >> 1) + g_val - (b_val >> 1)) >> ccl);
253 if (context->ChromaSubsamplingLevel && (x % 2) == 1)
255 *yplane = *(yplane - 1);
256 *coplane = *(coplane - 1);
257 *cgplane = *(cgplane - 1);
261 if (context->ChromaSubsamplingLevel && (y % 2) == 1)
263 yplane = context->priv->PlaneBuffers[0] + y * rw;
264 coplane = context->priv->PlaneBuffers[1] + y * rw;
265 cgplane = context->priv->PlaneBuffers[2] + y * rw;
266 CopyMemory(yplane, yplane - rw, rw);
267 CopyMemory(coplane, coplane - rw, rw);
268 CopyMemory(cgplane, cgplane - rw, rw);
274static BOOL nsc_encode_subsampling(NSC_CONTEXT* WINPR_RESTRICT context)
276 UINT32 tempWidth = 0;
277 UINT32 tempHeight = 0;
282 tempWidth = ROUND_UP_TO(context->width, 8);
283 tempHeight = ROUND_UP_TO(context->height, 2);
288 if (tempWidth > context->priv->PlaneBuffersLength / tempHeight)
291 for (
size_t y = 0; y < tempHeight >> 1; y++)
293 BYTE* co_dst = context->priv->PlaneBuffers[1] + y * (tempWidth >> 1);
294 BYTE* cg_dst = context->priv->PlaneBuffers[2] + y * (tempWidth >> 1);
295 const INT8* co_src0 = (INT8*)context->priv->PlaneBuffers[1] + (y << 1) * tempWidth;
296 const INT8* co_src1 = co_src0 + tempWidth;
297 const INT8* cg_src0 = (INT8*)context->priv->PlaneBuffers[2] + (y << 1) * tempWidth;
298 const INT8* cg_src1 = cg_src0 + tempWidth;
300 for (UINT32 x = 0; x < tempWidth >> 1; x++)
302 *co_dst++ = (BYTE)(((INT16)*co_src0 + (INT16) * (co_src0 + 1) + (INT16)*co_src1 +
303 (INT16) * (co_src1 + 1)) >>
305 *cg_dst++ = (BYTE)(((INT16)*cg_src0 + (INT16) * (cg_src0 + 1) + (INT16)*cg_src1 +
306 (INT16) * (cg_src1 + 1)) >>
318BOOL nsc_encode(NSC_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT bmpdata,
321 if (!context || !bmpdata || (rowstride == 0))
324 if (!nsc_encode_argb_to_aycocg(context, bmpdata, rowstride))
327 if (context->ChromaSubsamplingLevel)
329 if (!nsc_encode_subsampling(context))
336static UINT32 nsc_rle_encode(
const BYTE* WINPR_RESTRICT in, BYTE* WINPR_RESTRICT out,
340 UINT32 runlength = 1;
341 UINT32 planeSize = 0;
348 while (left > 4 && planeSize < originalSize - 4)
350 if (left > 5 && *in == *(in + 1))
354 else if (runlength == 1)
359 else if (runlength < 256)
363 WINPR_ASSERT(runlength >= 2);
364 *out++ = WINPR_ASSERTING_INT_CAST(BYTE, runlength - 2);
373 *out++ = (runlength & 0x000000FF);
374 *out++ = (runlength & 0x0000FF00) >> 8;
375 *out++ = (runlength & 0x00FF0000) >> 16;
376 *out++ = (runlength & 0xFF000000) >> 24;
385 if (planeSize < originalSize - 4)
386 CopyMemory(out, in, 4);
392static BOOL nsc_plane_is_opaque(
const BYTE* WINPR_RESTRICT plane, UINT32 size)
394 for (UINT32 x = 0; x < size; x++)
396 if (plane[x] != 0xFF)
402static void nsc_rle_compress_data(NSC_CONTEXT* WINPR_RESTRICT context)
404 UINT32 planeSize = 0;
405 UINT32 originalSize = 0;
407 for (UINT16 i = 0; i < 4; i++)
409 originalSize = context->OrgByteCount[i];
412 if ((originalSize == 0) ||
413 ((i == 3) && nsc_plane_is_opaque(context->priv->PlaneBuffers[i], originalSize)))
419 planeSize = nsc_rle_encode(context->priv->PlaneBuffers[i],
420 context->priv->PlaneBuffers[4], originalSize);
422 if (planeSize < originalSize)
423 CopyMemory(context->priv->PlaneBuffers[i], context->priv->PlaneBuffers[4],
426 planeSize = originalSize;
429 context->PlaneByteCount[i] = planeSize;
433BOOL nsc_write_message(WINPR_ATTR_UNUSED NSC_CONTEXT* WINPR_RESTRICT context,
434 wStream* WINPR_RESTRICT s,
const NSC_MESSAGE* WINPR_RESTRICT message)
436 UINT32 totalPlaneByteCount = 0;
437 totalPlaneByteCount = message->LumaPlaneByteCount + message->OrangeChromaPlaneByteCount +
438 message->GreenChromaPlaneByteCount + message->AlphaPlaneByteCount;
440 if (!Stream_EnsureRemainingCapacity(s, 20ull + totalPlaneByteCount))
443 Stream_Write_UINT32(s, message->LumaPlaneByteCount);
445 s, message->OrangeChromaPlaneByteCount);
447 s, message->GreenChromaPlaneByteCount);
448 Stream_Write_UINT32(s, message->AlphaPlaneByteCount);
449 Stream_Write_UINT8(s, message->ColorLossLevel);
450 Stream_Write_UINT8(s, message->ChromaSubsamplingLevel);
451 Stream_Write_UINT16(s, 0);
453 if (message->LumaPlaneByteCount)
454 Stream_Write(s, message->PlaneBuffers[0], message->LumaPlaneByteCount);
456 if (message->OrangeChromaPlaneByteCount)
457 Stream_Write(s, message->PlaneBuffers[1],
458 message->OrangeChromaPlaneByteCount);
460 if (message->GreenChromaPlaneByteCount)
461 Stream_Write(s, message->PlaneBuffers[2],
462 message->GreenChromaPlaneByteCount);
464 if (message->AlphaPlaneByteCount)
465 Stream_Write(s, message->PlaneBuffers[3], message->AlphaPlaneByteCount);
470BOOL nsc_compose_message(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
471 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
475 NSC_MESSAGE message = WINPR_C_ARRAY_INIT;
477 if (!context || !s || !data)
481 scanline = width * FreeRDPGetBytesPerPixel(context->format);
483 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
484 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
486 if (!nsc_context_initialize_encode(context))
490 PROFILER_ENTER(context->priv->prof_nsc_encode)
491 rc = context->encode(context, data, scanline);
492 PROFILER_EXIT(context->priv->prof_nsc_encode)
497 PROFILER_ENTER(context->priv->prof_nsc_rle_compress_data)
498 nsc_rle_compress_data(context);
499 PROFILER_EXIT(context->priv->prof_nsc_rle_compress_data)
500 message.PlaneBuffers[0] = context->priv->PlaneBuffers[0];
501 message.PlaneBuffers[1] = context->priv->PlaneBuffers[1];
502 message.PlaneBuffers[2] = context->priv->PlaneBuffers[2];
503 message.PlaneBuffers[3] = context->priv->PlaneBuffers[3];
504 message.LumaPlaneByteCount = context->PlaneByteCount[0];
505 message.OrangeChromaPlaneByteCount = context->PlaneByteCount[1];
506 message.GreenChromaPlaneByteCount = context->PlaneByteCount[2];
507 message.AlphaPlaneByteCount = context->PlaneByteCount[3];
509 message.ColorLossLevel = WINPR_ASSERTING_INT_CAST(BYTE, context->ColorLossLevel);
510 message.ChromaSubsamplingLevel =
511 WINPR_ASSERTING_INT_CAST(BYTE, context->ChromaSubsamplingLevel);
512 return nsc_write_message(context, s, &message);
515#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
516BOOL nsc_decompose_message(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
517 BYTE* WINPR_RESTRICT bmpdata, UINT32 x, UINT32 y, UINT32 width,
518 UINT32 height, UINT32 rowstride, UINT32 format, UINT32 flip)
520 size_t size = Stream_GetRemainingLength(s);
522 if (size > UINT32_MAX)
525 if (!nsc_process_message(context, (UINT16)FreeRDPGetBitsPerPixel(context->format), width,
526 height, Stream_Pointer(s), (UINT32)size, bmpdata, format, rowstride, x,
527 y, width, height, flip))
529 Stream_Seek(s, size);