21 #include <freerdp/config.h>
27 #include <winpr/crt.h>
28 #include <winpr/collections.h>
30 #include <freerdp/primitives.h>
32 #include "rfx_types.h"
34 #include "rfx_differential.h"
35 #include "rfx_quantization.h"
38 #include "rfx_encode.h"
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)
47 const BYTE* src = NULL;
54 x_exceed = 64 - width;
55 y_exceed = 64 - height;
57 for (
int y = 0; y < height; y++)
59 src = rgb_data + 1ULL * y * rowstride;
63 case PIXEL_FORMAT_BGRX32:
64 case PIXEL_FORMAT_BGRA32:
65 for (
int x = 0; x < width; x++)
67 *b_buf++ = (INT16)(*src++);
68 *g_buf++ = (INT16)(*src++);
69 *r_buf++ = (INT16)(*src++);
75 case PIXEL_FORMAT_XBGR32:
76 case PIXEL_FORMAT_ABGR32:
77 for (
int x = 0; x < width; x++)
80 *b_buf++ = (INT16)(*src++);
81 *g_buf++ = (INT16)(*src++);
82 *r_buf++ = (INT16)(*src++);
87 case PIXEL_FORMAT_RGBX32:
88 case PIXEL_FORMAT_RGBA32:
89 for (
int x = 0; x < width; x++)
91 *r_buf++ = (INT16)(*src++);
92 *g_buf++ = (INT16)(*src++);
93 *b_buf++ = (INT16)(*src++);
99 case PIXEL_FORMAT_XRGB32:
100 case PIXEL_FORMAT_ARGB32:
101 for (
int x = 0; x < width; x++)
104 *r_buf++ = (INT16)(*src++);
105 *g_buf++ = (INT16)(*src++);
106 *b_buf++ = (INT16)(*src++);
111 case PIXEL_FORMAT_BGR24:
112 for (
int x = 0; x < width; x++)
114 *b_buf++ = (INT16)(*src++);
115 *g_buf++ = (INT16)(*src++);
116 *r_buf++ = (INT16)(*src++);
121 case PIXEL_FORMAT_RGB24:
122 for (
int x = 0; x < width; x++)
124 *r_buf++ = (INT16)(*src++);
125 *g_buf++ = (INT16)(*src++);
126 *b_buf++ = (INT16)(*src++);
131 case PIXEL_FORMAT_BGR16:
132 for (
int x = 0; x < width; x++)
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));
142 case PIXEL_FORMAT_RGB16:
143 for (
int x = 0; x < width; x++)
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));
153 case PIXEL_FORMAT_RGB8:
157 for (
int x = 0; x < width; x++)
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;
167 *r_buf++ = (INT16)palette[idx];
168 *g_buf++ = (INT16)palette[idx + 1];
169 *b_buf++ = (INT16)palette[idx + 2];
177 case PIXEL_FORMAT_A4:
181 for (
int x = 0; x < width; x++)
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];
204 for (
int x = 0; x < x_exceed; x++)
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));
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)
240 INT16* dwt_buffer = NULL;
241 dwt_buffer = BufferPool_Take(context->priv->BufferPool, -1);
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);
259 void rfx_encode_rgb(RFX_CONTEXT* WINPR_RESTRICT context,
RFX_TILE* WINPR_RESTRICT tile)
266 BYTE* pBuffer = NULL;
271 UINT32* YQuant = NULL;
272 UINT32* CbQuant = NULL;
273 UINT32* CrQuant = NULL;
277 if (!(pBuffer = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
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]));
285 pSrcDst[1] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 1ULL) + 16ULL]));
286 pSrcDst[2] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 2ULL) + 16ULL]));
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],
292 PROFILER_EXIT(context->priv->prof_rfx_encode_format_rgb)
293 PROFILER_ENTER(context->priv->prof_rfx_rgb_to_ycbcr)
296 prims->RGBToYCbCr_16s16s_P3P3(cnv.cpv, 64 * sizeof(INT16), pSrcDst, 64 * sizeof(INT16),
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);