22 #include <freerdp/config.h>
28 #include <winpr/crt.h>
30 #include <freerdp/codec/nsc.h>
31 #include <freerdp/codec/color.h>
33 #include "nsc_types.h"
34 #include "nsc_encode.h"
46 BYTE* PlaneBuffers[5];
47 UINT32 OrgByteCount[4];
49 UINT32 LumaPlaneByteCount;
50 UINT32 OrangeChromaPlaneByteCount;
51 UINT32 GreenChromaPlaneByteCount;
52 UINT32 AlphaPlaneByteCount;
54 UINT8 ChromaSubsamplingLevel;
57 static BOOL nsc_write_message(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
58 const NSC_MESSAGE* WINPR_RESTRICT message);
60 static BOOL nsc_context_initialize_encode(NSC_CONTEXT* WINPR_RESTRICT context)
64 UINT32 tempHeight = 0;
65 tempWidth = ROUND_UP_TO(context->width, 8);
66 tempHeight = ROUND_UP_TO(context->height, 2);
68 length = tempWidth * tempHeight + 16;
70 if (length > context->priv->PlaneBuffersLength)
72 for (
int i = 0; i < 5; i++)
74 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], length,
80 context->priv->PlaneBuffers[i] = tmp;
83 context->priv->PlaneBuffersLength = length;
86 if (context->ChromaSubsamplingLevel)
88 context->OrgByteCount[0] = tempWidth * context->height;
89 context->OrgByteCount[1] = tempWidth * tempHeight / 4;
90 context->OrgByteCount[2] = tempWidth * tempHeight / 4;
91 context->OrgByteCount[3] = context->width * context->height;
95 context->OrgByteCount[0] = context->width * context->height;
96 context->OrgByteCount[1] = context->width * context->height;
97 context->OrgByteCount[2] = context->width * context->height;
98 context->OrgByteCount[3] = context->width * context->height;
104 if (length > context->priv->PlaneBuffersLength)
106 for (
int i = 0; i < 5; i++)
107 winpr_aligned_free(context->priv->PlaneBuffers[i]);
113 static BOOL nsc_encode_argb_to_aycocg(NSC_CONTEXT* WINPR_RESTRICT context,
114 const BYTE* WINPR_RESTRICT data, UINT32 scanline)
119 const BYTE* src = NULL;
121 BYTE* coplane = NULL;
122 BYTE* cgplane = NULL;
128 UINT32 tempWidth = 0;
130 tempWidth = ROUND_UP_TO(context->width, 8);
131 rw = (context->ChromaSubsamplingLevel ? tempWidth : context->width);
132 ccl = context->ColorLossLevel;
134 for (; y < context->height; y++)
136 src = data + (context->height - 1 - y) * scanline;
137 yplane = context->priv->PlaneBuffers[0] + y * rw;
138 coplane = context->priv->PlaneBuffers[1] + y * rw;
139 cgplane = context->priv->PlaneBuffers[2] + y * rw;
140 aplane = context->priv->PlaneBuffers[3] + y * context->width;
143 for (; x < context->width; x++)
145 switch (context->format)
147 case PIXEL_FORMAT_BGRX32:
155 case PIXEL_FORMAT_BGRA32:
162 case PIXEL_FORMAT_RGBX32:
170 case PIXEL_FORMAT_RGBA32:
177 case PIXEL_FORMAT_BGR24:
184 case PIXEL_FORMAT_RGB24:
191 case PIXEL_FORMAT_BGR16:
192 b_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
193 g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
194 r_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
199 case PIXEL_FORMAT_RGB16:
200 r_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
201 g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
202 b_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
207 case PIXEL_FORMAT_A4:
211 shift = (7 - (x % 8));
212 idx = ((*src) >> shift) & 1;
213 idx |= (((*(src + 1)) >> shift) & 1) << 1;
214 idx |= (((*(src + 2)) >> shift) & 1) << 2;
215 idx |= (((*(src + 3)) >> shift) & 1) << 3;
217 r_val = (INT16)context->palette[idx];
218 g_val = (INT16)context->palette[idx + 1];
219 b_val = (INT16)context->palette[idx + 2];
228 case PIXEL_FORMAT_RGB8:
230 int idx = (*src) * 3;
231 r_val = (INT16)context->palette[idx];
232 g_val = (INT16)context->palette[idx + 1];
233 b_val = (INT16)context->palette[idx + 2];
241 r_val = g_val = b_val = a_val = 0;
245 *yplane++ = (BYTE)((r_val >> 2) + (g_val >> 1) + (b_val >> 2));
247 *coplane++ = (BYTE)((r_val - b_val) >> ccl);
248 *cgplane++ = (BYTE)((-(r_val >> 1) + g_val - (b_val >> 1)) >> ccl);
252 if (context->ChromaSubsamplingLevel && (x % 2) == 1)
254 *yplane = *(yplane - 1);
255 *coplane = *(coplane - 1);
256 *cgplane = *(cgplane - 1);
260 if (context->ChromaSubsamplingLevel && (y % 2) == 1)
262 yplane = context->priv->PlaneBuffers[0] + y * rw;
263 coplane = context->priv->PlaneBuffers[1] + y * rw;
264 cgplane = context->priv->PlaneBuffers[2] + y * rw;
265 CopyMemory(yplane, yplane - rw, rw);
266 CopyMemory(coplane, coplane - rw, rw);
267 CopyMemory(cgplane, cgplane - rw, rw);
273 static BOOL nsc_encode_subsampling(NSC_CONTEXT* WINPR_RESTRICT context)
275 UINT32 tempWidth = 0;
276 UINT32 tempHeight = 0;
281 tempWidth = ROUND_UP_TO(context->width, 8);
282 tempHeight = ROUND_UP_TO(context->height, 2);
287 if (tempWidth > context->priv->PlaneBuffersLength / tempHeight)
290 for (
size_t y = 0; y < tempHeight >> 1; y++)
292 BYTE* co_dst = context->priv->PlaneBuffers[1] + y * (tempWidth >> 1);
293 BYTE* cg_dst = context->priv->PlaneBuffers[2] + y * (tempWidth >> 1);
294 const INT8* co_src0 = (INT8*)context->priv->PlaneBuffers[1] + (y << 1) * tempWidth;
295 const INT8* co_src1 = co_src0 + tempWidth;
296 const INT8* cg_src0 = (INT8*)context->priv->PlaneBuffers[2] + (y << 1) * tempWidth;
297 const INT8* cg_src1 = cg_src0 + tempWidth;
299 for (UINT32 x = 0; x < tempWidth >> 1; x++)
301 *co_dst++ = (BYTE)(((INT16)*co_src0 + (INT16) * (co_src0 + 1) + (INT16)*co_src1 +
302 (INT16) * (co_src1 + 1)) >>
304 *cg_dst++ = (BYTE)(((INT16)*cg_src0 + (INT16) * (cg_src0 + 1) + (INT16)*cg_src1 +
305 (INT16) * (cg_src1 + 1)) >>
317 BOOL nsc_encode(NSC_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT bmpdata,
320 if (!context || !bmpdata || (rowstride == 0))
323 if (!nsc_encode_argb_to_aycocg(context, bmpdata, rowstride))
326 if (context->ChromaSubsamplingLevel)
328 if (!nsc_encode_subsampling(context))
335 static UINT32 nsc_rle_encode(
const BYTE* WINPR_RESTRICT in, BYTE* WINPR_RESTRICT out,
339 UINT32 runlength = 1;
340 UINT32 planeSize = 0;
347 while (left > 4 && planeSize < originalSize - 4)
349 if (left > 5 && *in == *(in + 1))
353 else if (runlength == 1)
358 else if (runlength < 256)
362 *out++ = runlength - 2;
371 *out++ = (runlength & 0x000000FF);
372 *out++ = (runlength & 0x0000FF00) >> 8;
373 *out++ = (runlength & 0x00FF0000) >> 16;
374 *out++ = (runlength & 0xFF000000) >> 24;
383 if (planeSize < originalSize - 4)
384 CopyMemory(out, in, 4);
390 static void nsc_rle_compress_data(NSC_CONTEXT* WINPR_RESTRICT context)
392 UINT32 planeSize = 0;
393 UINT32 originalSize = 0;
395 for (UINT16 i = 0; i < 4; i++)
397 originalSize = context->OrgByteCount[i];
399 if (originalSize == 0)
405 planeSize = nsc_rle_encode(context->priv->PlaneBuffers[i],
406 context->priv->PlaneBuffers[4], originalSize);
408 if (planeSize < originalSize)
409 CopyMemory(context->priv->PlaneBuffers[i], context->priv->PlaneBuffers[4],
412 planeSize = originalSize;
415 context->PlaneByteCount[i] = planeSize;
419 static UINT32 nsc_compute_byte_count(NSC_CONTEXT* WINPR_RESTRICT context,
420 UINT32* WINPR_RESTRICT ByteCount, UINT32 width, UINT32 height)
422 UINT32 tempWidth = 0;
423 UINT32 tempHeight = 0;
424 UINT32 maxPlaneSize = 0;
425 tempWidth = ROUND_UP_TO(width, 8);
426 tempHeight = ROUND_UP_TO(height, 2);
427 maxPlaneSize = tempWidth * tempHeight + 16;
429 if (context->ChromaSubsamplingLevel)
431 ByteCount[0] = tempWidth * height;
432 ByteCount[1] = tempWidth * tempHeight / 4;
433 ByteCount[2] = tempWidth * tempHeight / 4;
434 ByteCount[3] = width * height;
438 ByteCount[0] = width * height;
439 ByteCount[1] = width * height;
440 ByteCount[2] = width * height;
441 ByteCount[3] = width * height;
447 BOOL nsc_write_message(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
448 const NSC_MESSAGE* WINPR_RESTRICT message)
450 UINT32 totalPlaneByteCount = 0;
451 totalPlaneByteCount = message->LumaPlaneByteCount + message->OrangeChromaPlaneByteCount +
452 message->GreenChromaPlaneByteCount + message->AlphaPlaneByteCount;
454 if (!Stream_EnsureRemainingCapacity(s, 20 + totalPlaneByteCount))
457 Stream_Write_UINT32(s, message->LumaPlaneByteCount);
459 s, message->OrangeChromaPlaneByteCount);
461 s, message->GreenChromaPlaneByteCount);
462 Stream_Write_UINT32(s, message->AlphaPlaneByteCount);
463 Stream_Write_UINT8(s, message->ColorLossLevel);
464 Stream_Write_UINT8(s, message->ChromaSubsamplingLevel);
465 Stream_Write_UINT16(s, 0);
467 if (message->LumaPlaneByteCount)
468 Stream_Write(s, message->PlaneBuffers[0], message->LumaPlaneByteCount);
470 if (message->OrangeChromaPlaneByteCount)
471 Stream_Write(s, message->PlaneBuffers[1],
472 message->OrangeChromaPlaneByteCount);
474 if (message->GreenChromaPlaneByteCount)
475 Stream_Write(s, message->PlaneBuffers[2],
476 message->GreenChromaPlaneByteCount);
478 if (message->AlphaPlaneByteCount)
479 Stream_Write(s, message->PlaneBuffers[3], message->AlphaPlaneByteCount);
484 BOOL nsc_compose_message(NSC_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
485 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
489 NSC_MESSAGE message = { 0 };
491 if (!context || !s || !data)
494 context->width = width;
495 context->height = height;
497 if (!nsc_context_initialize_encode(context))
501 PROFILER_ENTER(context->priv->prof_nsc_encode)
502 rc = context->encode(context, data, scanline);
503 PROFILER_EXIT(context->priv->prof_nsc_encode)
508 PROFILER_ENTER(context->priv->prof_nsc_rle_compress_data)
509 nsc_rle_compress_data(context);
510 PROFILER_EXIT(context->priv->prof_nsc_rle_compress_data)
511 message.PlaneBuffers[0] = context->priv->PlaneBuffers[0];
512 message.PlaneBuffers[1] = context->priv->PlaneBuffers[1];
513 message.PlaneBuffers[2] = context->priv->PlaneBuffers[2];
514 message.PlaneBuffers[3] = context->priv->PlaneBuffers[3];
515 message.LumaPlaneByteCount = context->PlaneByteCount[0];
516 message.OrangeChromaPlaneByteCount = context->PlaneByteCount[1];
517 message.GreenChromaPlaneByteCount = context->PlaneByteCount[2];
518 message.AlphaPlaneByteCount = context->PlaneByteCount[3];
519 message.ColorLossLevel = context->ColorLossLevel;
520 message.ChromaSubsamplingLevel = context->ChromaSubsamplingLevel;
521 return nsc_write_message(context, s, &message);
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)
528 size_t size = Stream_GetRemainingLength(s);
530 if (size > UINT32_MAX)
533 if (!nsc_process_message(context, (UINT16)FreeRDPGetBitsPerPixel(context->format), width,
534 height, Stream_Pointer(s), (UINT32)size, bmpdata, format, rowstride, x,
535 y, width, height, flip))
537 Stream_Seek(s, size);