FreeRDP
rfx_encode.c
1 
21 #include <freerdp/config.h>
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <winpr/crt.h>
28 #include <winpr/collections.h>
29 
30 #include <freerdp/primitives.h>
31 
32 #include "rfx_types.h"
33 #include "rfx_rlgr.h"
34 #include "rfx_differential.h"
35 #include "rfx_quantization.h"
36 #include "rfx_dwt.h"
37 
38 #include "rfx_encode.h"
39 
40 static void rfx_encode_format_rgb(const BYTE* WINPR_RESTRICT rgb_data, uint32_t width,
41  uint32_t height, uint32_t rowstride, UINT32 pixel_format,
42  const BYTE* WINPR_RESTRICT palette, INT16* WINPR_RESTRICT r_buf,
43  INT16* WINPR_RESTRICT g_buf, INT16* WINPR_RESTRICT b_buf)
44 {
45  const BYTE* src = NULL;
46  INT16 r = 0;
47  INT16 g = 0;
48  INT16 b = 0;
49  INT16* r_last = NULL;
50  INT16* g_last = NULL;
51  INT16* b_last = NULL;
52  uint32_t x_exceed = 64 - width;
53  uint32_t y_exceed = 64 - height;
54 
55  for (uint32_t y = 0; y < height; y++)
56  {
57  src = rgb_data + 1ULL * y * rowstride;
58 
59  switch (pixel_format)
60  {
61  case PIXEL_FORMAT_BGRX32:
62  case PIXEL_FORMAT_BGRA32:
63  for (uint32_t x = 0; x < width; x++)
64  {
65  *b_buf++ = (INT16)(*src++);
66  *g_buf++ = (INT16)(*src++);
67  *r_buf++ = (INT16)(*src++);
68  src++;
69  }
70 
71  break;
72 
73  case PIXEL_FORMAT_XBGR32:
74  case PIXEL_FORMAT_ABGR32:
75  for (size_t x = 0; x < width; x++)
76  {
77  src++;
78  *b_buf++ = (INT16)(*src++);
79  *g_buf++ = (INT16)(*src++);
80  *r_buf++ = (INT16)(*src++);
81  }
82 
83  break;
84 
85  case PIXEL_FORMAT_RGBX32:
86  case PIXEL_FORMAT_RGBA32:
87  for (size_t x = 0; x < width; x++)
88  {
89  *r_buf++ = (INT16)(*src++);
90  *g_buf++ = (INT16)(*src++);
91  *b_buf++ = (INT16)(*src++);
92  src++;
93  }
94 
95  break;
96 
97  case PIXEL_FORMAT_XRGB32:
98  case PIXEL_FORMAT_ARGB32:
99  for (size_t x = 0; x < width; x++)
100  {
101  src++;
102  *r_buf++ = (INT16)(*src++);
103  *g_buf++ = (INT16)(*src++);
104  *b_buf++ = (INT16)(*src++);
105  }
106 
107  break;
108 
109  case PIXEL_FORMAT_BGR24:
110  for (size_t x = 0; x < width; x++)
111  {
112  *b_buf++ = (INT16)(*src++);
113  *g_buf++ = (INT16)(*src++);
114  *r_buf++ = (INT16)(*src++);
115  }
116 
117  break;
118 
119  case PIXEL_FORMAT_RGB24:
120  for (size_t x = 0; x < width; x++)
121  {
122  *r_buf++ = (INT16)(*src++);
123  *g_buf++ = (INT16)(*src++);
124  *b_buf++ = (INT16)(*src++);
125  }
126 
127  break;
128 
129  case PIXEL_FORMAT_BGR16:
130  for (size_t x = 0; x < width; x++)
131  {
132  *b_buf++ = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
133  *g_buf++ = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
134  *r_buf++ = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
135  src += 2;
136  }
137 
138  break;
139 
140  case PIXEL_FORMAT_RGB16:
141  for (size_t x = 0; x < width; x++)
142  {
143  *r_buf++ = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
144  *g_buf++ = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
145  *b_buf++ = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
146  src += 2;
147  }
148 
149  break;
150 
151  case PIXEL_FORMAT_RGB8:
152  if (!palette)
153  break;
154 
155  for (size_t x = 0; x < width; x++)
156  {
157  BYTE idx = 0;
158  const size_t shift = (7 - (x % 8));
159  idx = ((*src) >> shift) & 1;
160  idx |= (((*(src + 1)) >> shift) & 1) << 1;
161  idx |= (((*(src + 2)) >> shift) & 1) << 2;
162  idx |= (((*(src + 3)) >> shift) & 1) << 3;
163  idx *= 3;
164  *r_buf++ = (INT16)palette[idx];
165  *g_buf++ = (INT16)palette[idx + 1];
166  *b_buf++ = (INT16)palette[idx + 2];
167 
168  if (shift == 0)
169  src += 4;
170  }
171 
172  break;
173 
174  case PIXEL_FORMAT_A4:
175  if (!palette)
176  break;
177 
178  for (size_t x = 0; x < width; x++)
179  {
180  int idx = (*src) * 3;
181  *r_buf++ = (INT16)palette[idx];
182  *g_buf++ = (INT16)palette[idx + 1];
183  *b_buf++ = (INT16)palette[idx + 2];
184  src++;
185  }
186 
187  break;
188 
189  default:
190  break;
191  }
192 
193  /* Fill the horizontal region outside of 64x64 tile size with the right-most pixel for best
194  * quality */
195  if (x_exceed > 0)
196  {
197  r = *(r_buf - 1);
198  g = *(g_buf - 1);
199  b = *(b_buf - 1);
200 
201  for (size_t x = 0; x < x_exceed; x++)
202  {
203  *r_buf++ = r;
204  *g_buf++ = g;
205  *b_buf++ = b;
206  }
207  }
208  }
209 
210  /* Fill the vertical region outside of 64x64 tile size with the last line. */
211  if (y_exceed > 0)
212  {
213  r_last = r_buf - 64;
214  g_last = g_buf - 64;
215  b_last = b_buf - 64;
216 
217  while (y_exceed > 0)
218  {
219  CopyMemory(r_buf, r_last, 64 * sizeof(INT16));
220  CopyMemory(g_buf, g_last, 64 * sizeof(INT16));
221  CopyMemory(b_buf, b_last, 64 * sizeof(INT16));
222  r_buf += 64;
223  g_buf += 64;
224  b_buf += 64;
225  y_exceed--;
226  }
227  }
228 }
229 
230 /* rfx_encode_rgb_to_ycbcr code now resides in the primitives library. */
231 
232 static void rfx_encode_component(RFX_CONTEXT* WINPR_RESTRICT context,
233  const UINT32* WINPR_RESTRICT quantization_values,
234  INT16* WINPR_RESTRICT data, BYTE* WINPR_RESTRICT buffer,
235  uint32_t buffer_size, uint32_t* WINPR_RESTRICT size)
236 {
237  INT16* dwt_buffer = BufferPool_Take(context->priv->BufferPool, -1); /* dwt_buffer */
238  PROFILER_ENTER(context->priv->prof_rfx_encode_component)
239  PROFILER_ENTER(context->priv->prof_rfx_dwt_2d_encode)
240  context->dwt_2d_encode(data, dwt_buffer);
241  PROFILER_EXIT(context->priv->prof_rfx_dwt_2d_encode)
242  PROFILER_ENTER(context->priv->prof_rfx_quantization_encode)
243  context->quantization_encode(data, quantization_values);
244  PROFILER_EXIT(context->priv->prof_rfx_quantization_encode)
245  PROFILER_ENTER(context->priv->prof_rfx_differential_encode)
246  rfx_differential_encode(data + 4032, 64);
247  PROFILER_EXIT(context->priv->prof_rfx_differential_encode)
248  PROFILER_ENTER(context->priv->prof_rfx_rlgr_encode)
249  const int rc = context->rlgr_encode(context->mode, data, 4096, buffer, buffer_size);
250  PROFILER_EXIT(context->priv->prof_rfx_rlgr_encode)
251  PROFILER_EXIT(context->priv->prof_rfx_encode_component)
252  BufferPool_Return(context->priv->BufferPool, dwt_buffer);
253 
254  *size = WINPR_ASSERTING_INT_CAST(uint32_t, rc);
255 }
256 
257 void rfx_encode_rgb(RFX_CONTEXT* WINPR_RESTRICT context, RFX_TILE* WINPR_RESTRICT tile)
258 {
259  union
260  {
261  const INT16** cpv;
262  INT16** pv;
263  } cnv;
264  BYTE* pBuffer = NULL;
265  INT16* pSrcDst[3];
266  uint32_t YLen = 0;
267  uint32_t CbLen = 0;
268  uint32_t CrLen = 0;
269  UINT32* YQuant = NULL;
270  UINT32* CbQuant = NULL;
271  UINT32* CrQuant = NULL;
272  primitives_t* prims = primitives_get();
273  static const prim_size_t roi_64x64 = { 64, 64 };
274 
275  if (!(pBuffer = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
276  return;
277 
278  YLen = CbLen = CrLen = 0;
279  YQuant = context->quants + (10ULL * tile->quantIdxY);
280  CbQuant = context->quants + (10ULL * tile->quantIdxCb);
281  CrQuant = context->quants + (10ULL * tile->quantIdxCr);
282  pSrcDst[0] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 0ULL) + 16ULL])); /* y_r_buffer */
283  pSrcDst[1] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 1ULL) + 16ULL])); /* cb_g_buffer */
284  pSrcDst[2] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 2ULL) + 16ULL])); /* cr_b_buffer */
285  PROFILER_ENTER(context->priv->prof_rfx_encode_rgb)
286  PROFILER_ENTER(context->priv->prof_rfx_encode_format_rgb)
287  rfx_encode_format_rgb(tile->data, tile->width, tile->height, tile->scanline,
288  context->pixel_format, context->palette, pSrcDst[0], pSrcDst[1],
289  pSrcDst[2]);
290  PROFILER_EXIT(context->priv->prof_rfx_encode_format_rgb)
291  PROFILER_ENTER(context->priv->prof_rfx_rgb_to_ycbcr)
292 
293  cnv.pv = pSrcDst;
294  prims->RGBToYCbCr_16s16s_P3P3(cnv.cpv, 64 * sizeof(INT16), pSrcDst, 64 * sizeof(INT16),
295  &roi_64x64);
296  PROFILER_EXIT(context->priv->prof_rfx_rgb_to_ycbcr)
301  ZeroMemory(tile->YData, 4096);
302  ZeroMemory(tile->CbData, 4096);
303  ZeroMemory(tile->CrData, 4096);
304  rfx_encode_component(context, YQuant, pSrcDst[0], tile->YData, 4096, &YLen);
305  rfx_encode_component(context, CbQuant, pSrcDst[1], tile->CbData, 4096, &CbLen);
306  rfx_encode_component(context, CrQuant, pSrcDst[2], tile->CrData, 4096, &CrLen);
307  tile->YLen = WINPR_ASSERTING_INT_CAST(UINT16, YLen);
308  tile->CbLen = WINPR_ASSERTING_INT_CAST(UINT16, CbLen);
309  tile->CrLen = WINPR_ASSERTING_INT_CAST(UINT16, CrLen);
310  PROFILER_EXIT(context->priv->prof_rfx_encode_rgb)
311  BufferPool_Return(context->priv->BufferPool, pBuffer);
312 }
Definition: rfx.h:52