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