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, 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)
45 const BYTE* src = NULL;
52 uint32_t x_exceed = 64 - width;
53 uint32_t y_exceed = 64 - height;
55 for (uint32_t y = 0; y < height; y++)
57 src = rgb_data + 1ULL * y * rowstride;
61 case PIXEL_FORMAT_BGRX32:
62 case PIXEL_FORMAT_BGRA32:
63 for (uint32_t x = 0; x < width; x++)
65 *b_buf++ = (INT16)(*src++);
66 *g_buf++ = (INT16)(*src++);
67 *r_buf++ = (INT16)(*src++);
73 case PIXEL_FORMAT_XBGR32:
74 case PIXEL_FORMAT_ABGR32:
75 for (
size_t x = 0; x < width; x++)
78 *b_buf++ = (INT16)(*src++);
79 *g_buf++ = (INT16)(*src++);
80 *r_buf++ = (INT16)(*src++);
85 case PIXEL_FORMAT_RGBX32:
86 case PIXEL_FORMAT_RGBA32:
87 for (
size_t x = 0; x < width; x++)
89 *r_buf++ = (INT16)(*src++);
90 *g_buf++ = (INT16)(*src++);
91 *b_buf++ = (INT16)(*src++);
97 case PIXEL_FORMAT_XRGB32:
98 case PIXEL_FORMAT_ARGB32:
99 for (
size_t x = 0; x < width; x++)
102 *r_buf++ = (INT16)(*src++);
103 *g_buf++ = (INT16)(*src++);
104 *b_buf++ = (INT16)(*src++);
109 case PIXEL_FORMAT_BGR24:
110 for (
size_t x = 0; x < width; x++)
112 *b_buf++ = (INT16)(*src++);
113 *g_buf++ = (INT16)(*src++);
114 *r_buf++ = (INT16)(*src++);
119 case PIXEL_FORMAT_RGB24:
120 for (
size_t x = 0; x < width; x++)
122 *r_buf++ = (INT16)(*src++);
123 *g_buf++ = (INT16)(*src++);
124 *b_buf++ = (INT16)(*src++);
129 case PIXEL_FORMAT_BGR16:
130 for (
size_t x = 0; x < width; x++)
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));
140 case PIXEL_FORMAT_RGB16:
141 for (
size_t x = 0; x < width; x++)
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));
151 case PIXEL_FORMAT_RGB8:
155 for (
size_t x = 0; x < width; x++)
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;
164 *r_buf++ = (INT16)palette[idx];
165 *g_buf++ = (INT16)palette[idx + 1];
166 *b_buf++ = (INT16)palette[idx + 2];
174 case PIXEL_FORMAT_A4:
178 for (
size_t x = 0; x < width; x++)
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];
201 for (
size_t x = 0; x < x_exceed; x++)
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));
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)
237 INT16* dwt_buffer = BufferPool_Take(context->priv->BufferPool, -1);
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);
254 *size = WINPR_ASSERTING_INT_CAST(uint32_t, rc);
257 void rfx_encode_rgb(RFX_CONTEXT* WINPR_RESTRICT context,
RFX_TILE* WINPR_RESTRICT tile)
264 BYTE* pBuffer = NULL;
269 UINT32* YQuant = NULL;
270 UINT32* CbQuant = NULL;
271 UINT32* CrQuant = NULL;
275 if (!(pBuffer = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
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]));
283 pSrcDst[1] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 1ULL) + 16ULL]));
284 pSrcDst[2] = (INT16*)((&pBuffer[((8192ULL + 32ULL) * 2ULL) + 16ULL]));
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],
290 PROFILER_EXIT(context->priv->prof_rfx_encode_format_rgb)
291 PROFILER_ENTER(context->priv->prof_rfx_rgb_to_ycbcr)
294 prims->RGBToYCbCr_16s16s_P3P3(cnv.cpv, 64 * sizeof(INT16), pSrcDst, 64 * sizeof(INT16),
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);