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