FreeRDP
Loading...
Searching...
No Matches
nsc_encode.c
1
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
38typedef 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
59static BOOL nsc_write_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
60 const NSC_MESSAGE* WINPR_RESTRICT message);
61
62static BOOL nsc_context_initialize_encode(NSC_CONTEXT* WINPR_RESTRICT context)
63{
64 const UINT32 tempWidth = ROUND_UP_TO(context->width, 8);
65 const UINT32 tempHeight = ROUND_UP_TO(context->height, 2);
66 /* The maximum length a decoded plane can reach in all cases */
67 const UINT32 length = tempWidth * tempHeight + 16;
68
69 if (length > context->priv->PlaneBuffersLength)
70 {
71 for (int i = 0; i < 5; i++)
72 {
73 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(context->priv->PlaneBuffers[i], length,
74 sizeof(BYTE), 32);
75
76 if (!tmp)
77 {
78 nsc_context_planebuffers_free(context->priv);
79 return FALSE;
80 }
81
82 context->priv->PlaneBuffers[i] = tmp;
83 }
84
85 context->priv->PlaneBuffersLength = length;
86 }
87
88 if (context->ChromaSubsamplingLevel)
89 {
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;
94 }
95 else
96 {
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;
101 }
102
103 return TRUE;
104}
105
106static BOOL nsc_encode_argb_to_aycocg(NSC_CONTEXT* WINPR_RESTRICT context,
107 const BYTE* WINPR_RESTRICT data, UINT32 scanline)
108{
109 size_t y = 0;
110 const BYTE* src = nullptr;
111 BYTE* yplane = nullptr;
112 BYTE* coplane = nullptr;
113 BYTE* cgplane = nullptr;
114 BYTE* aplane = nullptr;
115 INT16 r_val = 0;
116 INT16 g_val = 0;
117 INT16 b_val = 0;
118 BYTE a_val = 0;
119
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);
123
124 switch (context->format)
125 {
126 case PIXEL_FORMAT_A4:
127 case PIXEL_FORMAT_RGB8:
128 if (!context->palette)
129 return FALSE;
130 break;
131 default:
132 break;
133 }
134
135 for (; y < context->height; y++)
136 {
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;
142
143 UINT16 x = 0;
144 for (; x < context->width; x++)
145 {
146 switch (context->format)
147 {
148 case PIXEL_FORMAT_BGRX32:
149 b_val = *src++;
150 g_val = *src++;
151 r_val = *src++;
152 src++;
153 a_val = 0xFF;
154 break;
155
156 case PIXEL_FORMAT_BGRA32:
157 b_val = *src++;
158 g_val = *src++;
159 r_val = *src++;
160 a_val = *src++;
161 break;
162
163 case PIXEL_FORMAT_RGBX32:
164 r_val = *src++;
165 g_val = *src++;
166 b_val = *src++;
167 src++;
168 a_val = 0xFF;
169 break;
170
171 case PIXEL_FORMAT_RGBA32:
172 r_val = *src++;
173 g_val = *src++;
174 b_val = *src++;
175 a_val = *src++;
176 break;
177
178 case PIXEL_FORMAT_BGR24:
179 b_val = *src++;
180 g_val = *src++;
181 r_val = *src++;
182 a_val = 0xFF;
183 break;
184
185 case PIXEL_FORMAT_RGB24:
186 r_val = *src++;
187 g_val = *src++;
188 b_val = *src++;
189 a_val = 0xFF;
190 break;
191
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));
196 a_val = 0xFF;
197 src += 2;
198 break;
199
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));
204 a_val = 0xFF;
205 src += 2;
206 break;
207
208 case PIXEL_FORMAT_A4:
209 {
210 int shift = 0;
211 BYTE idx = 0;
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;
217 idx *= 3;
218 r_val = (INT16)context->palette[idx];
219 g_val = (INT16)context->palette[idx + 1];
220 b_val = (INT16)context->palette[idx + 2];
221
222 if (shift == 0)
223 src += 4;
224 }
225
226 a_val = 0xFF;
227 break;
228
229 case PIXEL_FORMAT_RGB8:
230 {
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];
235 src++;
236 }
237
238 a_val = 0xFF;
239 break;
240
241 default:
242 r_val = g_val = b_val = a_val = 0;
243 break;
244 }
245
246 *yplane++ = (BYTE)((r_val >> 2) + (g_val >> 1) + (b_val >> 2));
247 /* Perform color loss reduction here */
248 *coplane++ = (BYTE)((r_val - b_val) >> ccl);
249 *cgplane++ = (BYTE)((-(r_val >> 1) + g_val - (b_val >> 1)) >> ccl);
250 *aplane++ = a_val;
251 }
252
253 if (context->ChromaSubsamplingLevel && (x % 2) == 1)
254 {
255 *yplane = *(yplane - 1);
256 *coplane = *(coplane - 1);
257 *cgplane = *(cgplane - 1);
258 }
259 }
260
261 if (context->ChromaSubsamplingLevel && (y % 2) == 1)
262 {
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);
269 }
270
271 return TRUE;
272}
273
274static BOOL nsc_encode_subsampling(NSC_CONTEXT* WINPR_RESTRICT context)
275{
276 UINT32 tempWidth = 0;
277 UINT32 tempHeight = 0;
278
279 if (!context)
280 return FALSE;
281
282 tempWidth = ROUND_UP_TO(context->width, 8);
283 tempHeight = ROUND_UP_TO(context->height, 2);
284
285 if (tempHeight == 0)
286 return FALSE;
287
288 if (tempWidth > context->priv->PlaneBuffersLength / tempHeight)
289 return FALSE;
290
291 for (size_t y = 0; y < tempHeight >> 1; y++)
292 {
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;
299
300 for (UINT32 x = 0; x < tempWidth >> 1; x++)
301 {
302 *co_dst++ = (BYTE)(((INT16)*co_src0 + (INT16) * (co_src0 + 1) + (INT16)*co_src1 +
303 (INT16) * (co_src1 + 1)) >>
304 2);
305 *cg_dst++ = (BYTE)(((INT16)*cg_src0 + (INT16) * (cg_src0 + 1) + (INT16)*cg_src1 +
306 (INT16) * (cg_src1 + 1)) >>
307 2);
308 co_src0 += 2;
309 co_src1 += 2;
310 cg_src0 += 2;
311 cg_src1 += 2;
312 }
313 }
314
315 return TRUE;
316}
317
318BOOL nsc_encode(NSC_CONTEXT* WINPR_RESTRICT context, const BYTE* WINPR_RESTRICT bmpdata,
319 UINT32 rowstride)
320{
321 if (!context || !bmpdata || (rowstride == 0))
322 return FALSE;
323
324 if (!nsc_encode_argb_to_aycocg(context, bmpdata, rowstride))
325 return FALSE;
326
327 if (context->ChromaSubsamplingLevel)
328 {
329 if (!nsc_encode_subsampling(context))
330 return FALSE;
331 }
332
333 return TRUE;
334}
335
336static UINT32 nsc_rle_encode(const BYTE* WINPR_RESTRICT in, BYTE* WINPR_RESTRICT out,
337 UINT32 originalSize)
338{
339 UINT32 left = 0;
340 UINT32 runlength = 1;
341 UINT32 planeSize = 0;
342 left = originalSize;
343
348 while (left > 4 && planeSize < originalSize - 4)
349 {
350 if (left > 5 && *in == *(in + 1))
351 {
352 runlength++;
353 }
354 else if (runlength == 1)
355 {
356 *out++ = *in;
357 planeSize++;
358 }
359 else if (runlength < 256)
360 {
361 *out++ = *in;
362 *out++ = *in;
363 WINPR_ASSERT(runlength >= 2);
364 *out++ = WINPR_ASSERTING_INT_CAST(BYTE, runlength - 2);
365 runlength = 1;
366 planeSize += 3;
367 }
368 else
369 {
370 *out++ = *in;
371 *out++ = *in;
372 *out++ = 0xFF;
373 *out++ = (runlength & 0x000000FF);
374 *out++ = (runlength & 0x0000FF00) >> 8;
375 *out++ = (runlength & 0x00FF0000) >> 16;
376 *out++ = (runlength & 0xFF000000) >> 24;
377 runlength = 1;
378 planeSize += 7;
379 }
380
381 in++;
382 left--;
383 }
384
385 if (planeSize < originalSize - 4)
386 CopyMemory(out, in, 4);
387
388 planeSize += 4;
389 return planeSize;
390}
391
392static BOOL nsc_plane_is_opaque(const BYTE* WINPR_RESTRICT plane, UINT32 size)
393{
394 for (UINT32 x = 0; x < size; x++)
395 {
396 if (plane[x] != 0xFF)
397 return FALSE;
398 }
399 return TRUE;
400}
401
402static void nsc_rle_compress_data(NSC_CONTEXT* WINPR_RESTRICT context)
403{
404 UINT32 planeSize = 0;
405 UINT32 originalSize = 0;
406
407 for (UINT16 i = 0; i < 4; i++)
408 {
409 originalSize = context->OrgByteCount[i];
410
411 /* an empty alpha plane is decoded as fully opaque */
412 if ((originalSize == 0) ||
413 ((i == 3) && nsc_plane_is_opaque(context->priv->PlaneBuffers[i], originalSize)))
414 {
415 planeSize = 0;
416 }
417 else
418 {
419 planeSize = nsc_rle_encode(context->priv->PlaneBuffers[i],
420 context->priv->PlaneBuffers[4], originalSize);
421
422 if (planeSize < originalSize)
423 CopyMemory(context->priv->PlaneBuffers[i], context->priv->PlaneBuffers[4],
424 planeSize);
425 else
426 planeSize = originalSize;
427 }
428
429 context->PlaneByteCount[i] = planeSize;
430 }
431}
432
433BOOL nsc_write_message(WINPR_ATTR_UNUSED NSC_CONTEXT* WINPR_RESTRICT context,
434 wStream* WINPR_RESTRICT s, const NSC_MESSAGE* WINPR_RESTRICT message)
435{
436 UINT32 totalPlaneByteCount = 0;
437 totalPlaneByteCount = message->LumaPlaneByteCount + message->OrangeChromaPlaneByteCount +
438 message->GreenChromaPlaneByteCount + message->AlphaPlaneByteCount;
439
440 if (!Stream_EnsureRemainingCapacity(s, 20ull + totalPlaneByteCount))
441 return FALSE;
442
443 Stream_Write_UINT32(s, message->LumaPlaneByteCount); /* LumaPlaneByteCount (4 bytes) */
444 Stream_Write_UINT32(
445 s, message->OrangeChromaPlaneByteCount); /* OrangeChromaPlaneByteCount (4 bytes) */
446 Stream_Write_UINT32(
447 s, message->GreenChromaPlaneByteCount); /* GreenChromaPlaneByteCount (4 bytes) */
448 Stream_Write_UINT32(s, message->AlphaPlaneByteCount); /* AlphaPlaneByteCount (4 bytes) */
449 Stream_Write_UINT8(s, message->ColorLossLevel); /* ColorLossLevel (1 byte) */
450 Stream_Write_UINT8(s, message->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
451 Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
452
453 if (message->LumaPlaneByteCount)
454 Stream_Write(s, message->PlaneBuffers[0], message->LumaPlaneByteCount); /* LumaPlane */
455
456 if (message->OrangeChromaPlaneByteCount)
457 Stream_Write(s, message->PlaneBuffers[1],
458 message->OrangeChromaPlaneByteCount); /* OrangeChromaPlane */
459
460 if (message->GreenChromaPlaneByteCount)
461 Stream_Write(s, message->PlaneBuffers[2],
462 message->GreenChromaPlaneByteCount); /* GreenChromaPlane */
463
464 if (message->AlphaPlaneByteCount)
465 Stream_Write(s, message->PlaneBuffers[3], message->AlphaPlaneByteCount); /* AlphaPlane */
466
467 return TRUE;
468}
469
470BOOL nsc_compose_message(NSC_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
471 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
472 UINT32 scanline)
473{
474 BOOL rc = 0;
475 NSC_MESSAGE message = WINPR_C_ARRAY_INIT;
476
477 if (!context || !s || !data)
478 return FALSE;
479
480 if (scanline == 0)
481 scanline = width * FreeRDPGetBytesPerPixel(context->format);
482
483 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
484 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
485
486 if (!nsc_context_initialize_encode(context))
487 return FALSE;
488
489 /* ARGB to AYCoCg conversion, chroma subsampling and colorloss reduction */
490 PROFILER_ENTER(context->priv->prof_nsc_encode)
491 rc = context->encode(context, data, scanline);
492 PROFILER_EXIT(context->priv->prof_nsc_encode)
493 if (!rc)
494 return FALSE;
495
496 /* RLE 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];
508
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);
513}
514
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)
519{
520 size_t size = Stream_GetRemainingLength(s);
521
522 if (size > UINT32_MAX)
523 return FALSE;
524
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))
528 return FALSE;
529 Stream_Seek(s, size);
530 return TRUE;
531}
532#endif