/src/FreeRDP/libfreerdp/codec/rfx.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * RemoteFX Codec Library |
4 | | * |
5 | | * Copyright 2011 Vic Lee |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 Norbert Federa <norbert.federa@thincast.com> |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include <winpr/assert.h> |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | |
29 | | #include <winpr/crt.h> |
30 | | #include <winpr/tchar.h> |
31 | | #include <winpr/sysinfo.h> |
32 | | #include <winpr/registry.h> |
33 | | |
34 | | #include <freerdp/log.h> |
35 | | #include <freerdp/settings.h> |
36 | | #include <freerdp/codec/rfx.h> |
37 | | #include <freerdp/constants.h> |
38 | | #include <freerdp/primitives.h> |
39 | | #include <freerdp/codec/region.h> |
40 | | #include <freerdp/build-config.h> |
41 | | |
42 | | #include "rfx_constants.h" |
43 | | #include "rfx_types.h" |
44 | | #include "rfx_decode.h" |
45 | | #include "rfx_encode.h" |
46 | | #include "rfx_quantization.h" |
47 | | #include "rfx_dwt.h" |
48 | | #include "rfx_rlgr.h" |
49 | | |
50 | | #include "rfx_sse2.h" |
51 | | #include "rfx_neon.h" |
52 | | |
53 | | #define TAG FREERDP_TAG("codec") |
54 | | |
55 | | #ifndef RFX_INIT_SIMD |
56 | | #define RFX_INIT_SIMD(_rfx_context) \ |
57 | 11.4k | do \ |
58 | 11.4k | { \ |
59 | 11.4k | } while (0) |
60 | | #endif |
61 | | |
62 | 11.4k | #define RFX_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\RemoteFX" |
63 | | |
64 | | /** |
65 | | * The quantization values control the compression rate and quality. The value |
66 | | * range is between 6 and 15. The higher value, the higher compression rate |
67 | | * and lower quality. |
68 | | * |
69 | | * This is the default values being use by the MS RDP server, and we will also |
70 | | * use it as our default values for the encoder. It can be overrided by setting |
71 | | * the context->num_quants and context->quants member. |
72 | | * |
73 | | * The order of the values are: |
74 | | * LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1 |
75 | | */ |
76 | | static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 }; |
77 | | |
78 | | static INLINE BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* rfx, wStream* s, |
79 | | const RFX_TILE* tile); |
80 | | |
81 | | static void rfx_profiler_create(RFX_CONTEXT* context) |
82 | 11.4k | { |
83 | 11.4k | if (!context || !context->priv) |
84 | 0 | return; |
85 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_decode_rgb, "rfx_decode_rgb") |
86 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component") |
87 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode") |
88 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode") |
89 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode") |
90 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode") |
91 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB") |
92 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb") |
93 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component") |
94 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode") |
95 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode") |
96 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode") |
97 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode") |
98 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr") |
99 | 11.4k | PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb") |
100 | 11.4k | } |
101 | | |
102 | | static void rfx_profiler_free(RFX_CONTEXT* context) |
103 | 11.4k | { |
104 | 11.4k | if (!context || !context->priv) |
105 | 0 | return; |
106 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_decode_rgb) |
107 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_decode_component) |
108 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_rlgr_decode) |
109 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_differential_decode) |
110 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_quantization_decode) |
111 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode) |
112 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb) |
113 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_encode_rgb) |
114 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_encode_component) |
115 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_rlgr_encode) |
116 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_differential_encode) |
117 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_quantization_encode) |
118 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode) |
119 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr) |
120 | 11.4k | PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb) |
121 | 11.4k | } |
122 | | |
123 | | static void rfx_profiler_print(RFX_CONTEXT* context) |
124 | 11.4k | { |
125 | 11.4k | if (!context || !context->priv) |
126 | 0 | return; |
127 | | |
128 | 11.4k | PROFILER_PRINT_HEADER |
129 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_decode_rgb) |
130 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_decode_component) |
131 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode) |
132 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_differential_decode) |
133 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_quantization_decode) |
134 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode) |
135 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb) |
136 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_encode_rgb) |
137 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_encode_component) |
138 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode) |
139 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_differential_encode) |
140 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_quantization_encode) |
141 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode) |
142 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr) |
143 | 11.4k | PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb) |
144 | 11.4k | PROFILER_PRINT_FOOTER |
145 | 11.4k | } |
146 | | |
147 | | static void rfx_tile_init(void* obj) |
148 | 0 | { |
149 | 0 | RFX_TILE* tile = (RFX_TILE*)obj; |
150 | 0 | if (tile) |
151 | 0 | { |
152 | 0 | tile->x = 0; |
153 | 0 | tile->y = 0; |
154 | 0 | tile->YLen = 0; |
155 | 0 | tile->YData = NULL; |
156 | 0 | tile->CbLen = 0; |
157 | 0 | tile->CbData = NULL; |
158 | 0 | tile->CrLen = 0; |
159 | 0 | tile->CrData = NULL; |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | | static void* rfx_decoder_tile_new(const void* val) |
164 | 0 | { |
165 | 0 | const size_t size = 4 * 64 * 64; |
166 | 0 | RFX_TILE* tile = NULL; |
167 | 0 | WINPR_UNUSED(val); |
168 | |
|
169 | 0 | if (!(tile = (RFX_TILE*)winpr_aligned_calloc(1, sizeof(RFX_TILE), 32))) |
170 | 0 | return NULL; |
171 | | |
172 | 0 | if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16))) |
173 | 0 | { |
174 | 0 | winpr_aligned_free(tile); |
175 | 0 | return NULL; |
176 | 0 | } |
177 | 0 | memset(tile->data, 0xff, size); |
178 | 0 | tile->allocated = TRUE; |
179 | 0 | return tile; |
180 | 0 | } |
181 | | |
182 | | static void rfx_decoder_tile_free(void* obj) |
183 | 0 | { |
184 | 0 | RFX_TILE* tile = (RFX_TILE*)obj; |
185 | |
|
186 | 0 | if (tile) |
187 | 0 | { |
188 | 0 | if (tile->allocated) |
189 | 0 | winpr_aligned_free(tile->data); |
190 | |
|
191 | 0 | winpr_aligned_free(tile); |
192 | 0 | } |
193 | 0 | } |
194 | | |
195 | | static void* rfx_encoder_tile_new(const void* val) |
196 | 0 | { |
197 | 0 | WINPR_UNUSED(val); |
198 | 0 | return winpr_aligned_calloc(1, sizeof(RFX_TILE), 32); |
199 | 0 | } |
200 | | |
201 | | static void rfx_encoder_tile_free(void* obj) |
202 | 0 | { |
203 | 0 | winpr_aligned_free(obj); |
204 | 0 | } |
205 | | |
206 | | RFX_CONTEXT* rfx_context_new(BOOL encoder) |
207 | 5.71k | { |
208 | 5.71k | return rfx_context_new_ex(encoder, 0); |
209 | 5.71k | } |
210 | | |
211 | | RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags) |
212 | 11.4k | { |
213 | 11.4k | HKEY hKey = NULL; |
214 | 11.4k | LONG status = 0; |
215 | 11.4k | DWORD dwType = 0; |
216 | 11.4k | DWORD dwSize = 0; |
217 | 11.4k | DWORD dwValue = 0; |
218 | 11.4k | SYSTEM_INFO sysinfo; |
219 | 11.4k | RFX_CONTEXT* context = NULL; |
220 | 11.4k | wObject* pool = NULL; |
221 | 11.4k | RFX_CONTEXT_PRIV* priv = NULL; |
222 | 11.4k | context = (RFX_CONTEXT*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT), 32); |
223 | | |
224 | 11.4k | if (!context) |
225 | 0 | return NULL; |
226 | | |
227 | 11.4k | context->encoder = encoder; |
228 | 11.4k | context->currentMessage.freeArray = TRUE; |
229 | 11.4k | context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT_PRIV), 32); |
230 | | |
231 | 11.4k | if (!priv) |
232 | 0 | goto fail; |
233 | | |
234 | 11.4k | priv->log = WLog_Get("com.freerdp.codec.rfx"); |
235 | 11.4k | WLog_OpenAppender(priv->log); |
236 | 11.4k | priv->TilePool = ObjectPool_New(TRUE); |
237 | | |
238 | 11.4k | if (!priv->TilePool) |
239 | 0 | goto fail; |
240 | | |
241 | 11.4k | pool = ObjectPool_Object(priv->TilePool); |
242 | 11.4k | pool->fnObjectInit = rfx_tile_init; |
243 | | |
244 | 11.4k | if (context->encoder) |
245 | 0 | { |
246 | 0 | pool->fnObjectNew = rfx_encoder_tile_new; |
247 | 0 | pool->fnObjectFree = rfx_encoder_tile_free; |
248 | 0 | } |
249 | 11.4k | else |
250 | 11.4k | { |
251 | 11.4k | pool->fnObjectNew = rfx_decoder_tile_new; |
252 | 11.4k | pool->fnObjectFree = rfx_decoder_tile_free; |
253 | 11.4k | } |
254 | | |
255 | | /* |
256 | | * align buffers to 16 byte boundary (needed for SSE/NEON instructions) |
257 | | * |
258 | | * y_r_buffer, cb_g_buffer, cr_b_buffer: 64 * 64 * sizeof(INT16) = 8192 (0x2000) |
259 | | * dwt_buffer: 32 * 32 * 2 * 2 * sizeof(INT16) = 8192, maximum sub-band width is 32 |
260 | | * |
261 | | * Additionally we add 32 bytes (16 in front and 16 at the back of the buffer) |
262 | | * in order to allow optimized functions (SEE, NEON) to read from positions |
263 | | * that are actually in front/beyond the buffer. Offset calculations are |
264 | | * performed at the BufferPool_Take function calls in rfx_encode/decode.c. |
265 | | * |
266 | | * We then multiply by 3 to use a single, partioned buffer for all 3 channels. |
267 | | */ |
268 | 11.4k | priv->BufferPool = BufferPool_New(TRUE, (8192 + 32) * 3, 16); |
269 | | |
270 | 11.4k | if (!priv->BufferPool) |
271 | 0 | goto fail; |
272 | | |
273 | 11.4k | if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS)) |
274 | 11.4k | { |
275 | 11.4k | priv->UseThreads = TRUE; |
276 | | |
277 | 11.4k | GetNativeSystemInfo(&sysinfo); |
278 | 11.4k | priv->MinThreadCount = sysinfo.dwNumberOfProcessors; |
279 | 11.4k | priv->MaxThreadCount = 0; |
280 | 11.4k | status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey); |
281 | | |
282 | 11.4k | if (status == ERROR_SUCCESS) |
283 | 0 | { |
284 | 0 | dwSize = sizeof(dwValue); |
285 | |
|
286 | 0 | if (RegQueryValueEx(hKey, _T("UseThreads"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) == |
287 | 0 | ERROR_SUCCESS) |
288 | 0 | priv->UseThreads = dwValue ? 1 : 0; |
289 | |
|
290 | 0 | if (RegQueryValueEx(hKey, _T("MinThreadCount"), NULL, &dwType, (BYTE*)&dwValue, |
291 | 0 | &dwSize) == ERROR_SUCCESS) |
292 | 0 | priv->MinThreadCount = dwValue; |
293 | |
|
294 | 0 | if (RegQueryValueEx(hKey, _T("MaxThreadCount"), NULL, &dwType, (BYTE*)&dwValue, |
295 | 0 | &dwSize) == ERROR_SUCCESS) |
296 | 0 | priv->MaxThreadCount = dwValue; |
297 | |
|
298 | 0 | RegCloseKey(hKey); |
299 | 0 | } |
300 | 11.4k | } |
301 | 0 | else |
302 | 0 | { |
303 | 0 | priv->UseThreads = FALSE; |
304 | 0 | } |
305 | | |
306 | 11.4k | if (priv->UseThreads) |
307 | 11.4k | { |
308 | | /* Call primitives_get here in order to avoid race conditions when using primitives_get */ |
309 | | /* from multiple threads. This call will initialize all function pointers correctly */ |
310 | | /* before any decoding threads are started */ |
311 | 11.4k | primitives_get(); |
312 | 11.4k | priv->ThreadPool = CreateThreadpool(NULL); |
313 | | |
314 | 11.4k | if (!priv->ThreadPool) |
315 | 0 | goto fail; |
316 | | |
317 | 11.4k | InitializeThreadpoolEnvironment(&priv->ThreadPoolEnv); |
318 | 11.4k | SetThreadpoolCallbackPool(&priv->ThreadPoolEnv, priv->ThreadPool); |
319 | | |
320 | 11.4k | if (priv->MinThreadCount) |
321 | 11.4k | if (!SetThreadpoolThreadMinimum(priv->ThreadPool, priv->MinThreadCount)) |
322 | 0 | goto fail; |
323 | | |
324 | 11.4k | if (priv->MaxThreadCount) |
325 | 0 | SetThreadpoolThreadMaximum(priv->ThreadPool, priv->MaxThreadCount); |
326 | 11.4k | } |
327 | | |
328 | | /* initialize the default pixel format */ |
329 | 11.4k | rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32); |
330 | | /* create profilers for default decoding routines */ |
331 | 11.4k | rfx_profiler_create(context); |
332 | | /* set up default routines */ |
333 | 11.4k | context->quantization_decode = rfx_quantization_decode; |
334 | 11.4k | context->quantization_encode = rfx_quantization_encode; |
335 | 11.4k | context->dwt_2d_decode = rfx_dwt_2d_decode; |
336 | 11.4k | context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode; |
337 | 11.4k | context->dwt_2d_encode = rfx_dwt_2d_encode; |
338 | 11.4k | context->rlgr_decode = rfx_rlgr_decode; |
339 | 11.4k | context->rlgr_encode = rfx_rlgr_encode; |
340 | 11.4k | RFX_INIT_SIMD(context); |
341 | 11.4k | context->state = RFX_STATE_SEND_HEADERS; |
342 | 11.4k | context->expectedDataBlockType = WBT_FRAME_BEGIN; |
343 | 11.4k | return context; |
344 | 0 | fail: |
345 | 0 | WINPR_PRAGMA_DIAG_PUSH |
346 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
347 | 0 | rfx_context_free(context); |
348 | 0 | WINPR_PRAGMA_DIAG_POP |
349 | 0 | return NULL; |
350 | 11.4k | } |
351 | | |
352 | | void rfx_context_free(RFX_CONTEXT* context) |
353 | 11.4k | { |
354 | 11.4k | RFX_CONTEXT_PRIV* priv = NULL; |
355 | | |
356 | 11.4k | if (!context) |
357 | 0 | return; |
358 | | |
359 | 11.4k | WINPR_ASSERT(NULL != context); |
360 | | |
361 | 11.4k | priv = context->priv; |
362 | 11.4k | WINPR_ASSERT(NULL != priv); |
363 | 11.4k | WINPR_ASSERT(NULL != priv->TilePool); |
364 | 11.4k | WINPR_ASSERT(NULL != priv->BufferPool); |
365 | | |
366 | | /* coverity[address_free] */ |
367 | 11.4k | rfx_message_free(context, &context->currentMessage); |
368 | 11.4k | winpr_aligned_free(context->quants); |
369 | 11.4k | rfx_profiler_print(context); |
370 | 11.4k | rfx_profiler_free(context); |
371 | | |
372 | 11.4k | if (priv) |
373 | 11.4k | { |
374 | 11.4k | ObjectPool_Free(priv->TilePool); |
375 | 11.4k | if (priv->UseThreads) |
376 | 11.4k | { |
377 | 11.4k | if (priv->ThreadPool) |
378 | 11.4k | CloseThreadpool(priv->ThreadPool); |
379 | 11.4k | DestroyThreadpoolEnvironment(&priv->ThreadPoolEnv); |
380 | 11.4k | winpr_aligned_free(priv->workObjects); |
381 | 11.4k | winpr_aligned_free(priv->tileWorkParams); |
382 | | #ifdef WITH_PROFILER |
383 | | WLog_VRB( |
384 | | TAG, |
385 | | "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!"); |
386 | | #endif |
387 | 11.4k | } |
388 | | |
389 | 11.4k | BufferPool_Free(priv->BufferPool); |
390 | 11.4k | winpr_aligned_free(priv); |
391 | 11.4k | } |
392 | 11.4k | winpr_aligned_free(context); |
393 | 11.4k | } |
394 | | |
395 | | static RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* message, UINT32 index) |
396 | 0 | { |
397 | 0 | WINPR_ASSERT(message); |
398 | 0 | WINPR_ASSERT(message->tiles); |
399 | 0 | WINPR_ASSERT(index < message->numTiles); |
400 | 0 | return message->tiles[index]; |
401 | 0 | } |
402 | | |
403 | | static const RFX_RECT* rfx_message_get_rect_const(const RFX_MESSAGE* message, UINT32 index) |
404 | 0 | { |
405 | 0 | WINPR_ASSERT(message); |
406 | 0 | WINPR_ASSERT(message->rects); |
407 | 0 | WINPR_ASSERT(index < message->numRects); |
408 | 0 | return &message->rects[index]; |
409 | 0 | } |
410 | | |
411 | | static RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* message, UINT32 index) |
412 | 0 | { |
413 | 0 | WINPR_ASSERT(message); |
414 | 0 | WINPR_ASSERT(message->rects); |
415 | 0 | WINPR_ASSERT(index < message->numRects); |
416 | 0 | return &message->rects[index]; |
417 | 0 | } |
418 | | |
419 | | void rfx_context_set_pixel_format(RFX_CONTEXT* context, UINT32 pixel_format) |
420 | 11.4k | { |
421 | 11.4k | WINPR_ASSERT(context); |
422 | 11.4k | context->pixel_format = pixel_format; |
423 | 11.4k | context->bits_per_pixel = FreeRDPGetBitsPerPixel(pixel_format); |
424 | 11.4k | } |
425 | | |
426 | | UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* context) |
427 | 0 | { |
428 | 0 | WINPR_ASSERT(context); |
429 | 0 | return context->pixel_format; |
430 | 0 | } |
431 | | |
432 | | void rfx_context_set_palette(RFX_CONTEXT* context, const BYTE* palette) |
433 | 0 | { |
434 | 0 | WINPR_ASSERT(context); |
435 | 0 | context->palette = palette; |
436 | 0 | } |
437 | | |
438 | | const BYTE* rfx_context_get_palette(RFX_CONTEXT* context) |
439 | 0 | { |
440 | 0 | WINPR_ASSERT(context); |
441 | 0 | return context->palette; |
442 | 0 | } |
443 | | |
444 | | BOOL rfx_context_reset(RFX_CONTEXT* context, UINT32 width, UINT32 height) |
445 | 0 | { |
446 | 0 | if (!context) |
447 | 0 | return FALSE; |
448 | | |
449 | 0 | context->width = width; |
450 | 0 | context->height = height; |
451 | 0 | context->state = RFX_STATE_SEND_HEADERS; |
452 | 0 | context->expectedDataBlockType = WBT_FRAME_BEGIN; |
453 | 0 | context->frameIdx = 0; |
454 | 0 | return TRUE; |
455 | 0 | } |
456 | | |
457 | | static BOOL rfx_process_message_sync(RFX_CONTEXT* context, wStream* s) |
458 | 245 | { |
459 | 245 | UINT32 magic = 0; |
460 | | |
461 | 245 | WINPR_ASSERT(context); |
462 | 245 | WINPR_ASSERT(context->priv); |
463 | 245 | context->decodedHeaderBlocks &= ~RFX_DECODED_SYNC; |
464 | | |
465 | | /* RFX_SYNC */ |
466 | 245 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
467 | 5 | return FALSE; |
468 | | |
469 | 240 | Stream_Read_UINT32(s, magic); /* magic (4 bytes), 0xCACCACCA */ |
470 | 240 | if (magic != WF_MAGIC) |
471 | 66 | { |
472 | 66 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid magic number 0x%08" PRIX32 "", magic); |
473 | 66 | return FALSE; |
474 | 66 | } |
475 | | |
476 | 174 | Stream_Read_UINT16(s, context->version); /* version (2 bytes), WF_VERSION_1_0 (0x0100) */ |
477 | 174 | if (context->version != WF_VERSION_1_0) |
478 | 30 | { |
479 | 30 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid version number 0x%08" PRIX32 "", |
480 | 30 | context->version); |
481 | 30 | return FALSE; |
482 | 30 | } |
483 | | |
484 | 144 | WLog_Print(context->priv->log, WLOG_DEBUG, "version 0x%08" PRIX32 "", context->version); |
485 | 144 | context->decodedHeaderBlocks |= RFX_DECODED_SYNC; |
486 | 144 | return TRUE; |
487 | 174 | } |
488 | | |
489 | | static BOOL rfx_process_message_codec_versions(RFX_CONTEXT* context, wStream* s) |
490 | 605 | { |
491 | 605 | BYTE numCodecs = 0; |
492 | | |
493 | 605 | WINPR_ASSERT(context); |
494 | 605 | WINPR_ASSERT(context->priv); |
495 | 605 | context->decodedHeaderBlocks &= ~RFX_DECODED_VERSIONS; |
496 | | |
497 | 605 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4)) |
498 | 2 | return FALSE; |
499 | | |
500 | 603 | Stream_Read_UINT8(s, numCodecs); /* numCodecs (1 byte), must be set to 0x01 */ |
501 | 603 | Stream_Read_UINT8(s, context->codec_id); /* codecId (1 byte), must be set to 0x01 */ |
502 | 603 | Stream_Read_UINT16( |
503 | 603 | s, context->codec_version); /* version (2 bytes), must be set to WF_VERSION_1_0 (0x0100) */ |
504 | | |
505 | 603 | if (numCodecs != 1) |
506 | 298 | { |
507 | 298 | WLog_Print(context->priv->log, WLOG_ERROR, "numCodes is 0x%02" PRIX8 " (must be 0x01)", |
508 | 298 | numCodecs); |
509 | 298 | return FALSE; |
510 | 298 | } |
511 | | |
512 | 305 | if (context->codec_id != 0x01) |
513 | 8 | { |
514 | 8 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec id (0x%02" PRIX32 ")", |
515 | 8 | context->codec_id); |
516 | 8 | return FALSE; |
517 | 8 | } |
518 | | |
519 | 297 | if (context->codec_version != WF_VERSION_1_0) |
520 | 11 | { |
521 | 11 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec version (0x%08" PRIX32 ")", |
522 | 11 | context->codec_version); |
523 | 11 | return FALSE; |
524 | 11 | } |
525 | | |
526 | 286 | WLog_Print(context->priv->log, WLOG_DEBUG, "id %" PRIu32 " version 0x%" PRIX32 ".", |
527 | 286 | context->codec_id, context->codec_version); |
528 | 286 | context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS; |
529 | 286 | return TRUE; |
530 | 297 | } |
531 | | |
532 | | static BOOL rfx_process_message_channels(RFX_CONTEXT* context, wStream* s) |
533 | 171 | { |
534 | 171 | BYTE channelId = 0; |
535 | 171 | BYTE numChannels = 0; |
536 | | |
537 | 171 | WINPR_ASSERT(context); |
538 | 171 | WINPR_ASSERT(context->priv); |
539 | 171 | context->decodedHeaderBlocks &= ~RFX_DECODED_CHANNELS; |
540 | | |
541 | 171 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1)) |
542 | 38 | return FALSE; |
543 | | |
544 | 133 | Stream_Read_UINT8(s, numChannels); /* numChannels (1 byte), must bet set to 0x01 */ |
545 | | |
546 | | /* In RDVH sessions, numChannels will represent the number of virtual monitors |
547 | | * configured and does not always be set to 0x01 as [MS-RDPRFX] said. |
548 | | */ |
549 | 133 | if (numChannels < 1) |
550 | 5 | { |
551 | 5 | WLog_Print(context->priv->log, WLOG_ERROR, "no channels announced"); |
552 | 5 | return FALSE; |
553 | 5 | } |
554 | | |
555 | 128 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull)) |
556 | 9 | return FALSE; |
557 | | |
558 | | /* RFX_CHANNELT */ |
559 | 119 | Stream_Read_UINT8(s, channelId); /* channelId (1 byte), must be set to 0x00 */ |
560 | | |
561 | 119 | if (channelId != 0x00) |
562 | 12 | { |
563 | 12 | WLog_Print(context->priv->log, WLOG_ERROR, "channelId:0x%02" PRIX8 ", expected:0x00", |
564 | 12 | channelId); |
565 | 12 | return FALSE; |
566 | 12 | } |
567 | | |
568 | 107 | Stream_Read_UINT16(s, context->width); /* width (2 bytes) */ |
569 | 107 | Stream_Read_UINT16(s, context->height); /* height (2 bytes) */ |
570 | | |
571 | 107 | if (!context->width || !context->height) |
572 | 3 | { |
573 | 3 | WLog_Print(context->priv->log, WLOG_ERROR, |
574 | 3 | "invalid channel with/height: %" PRIu16 "x%" PRIu16 "", context->width, |
575 | 3 | context->height); |
576 | 3 | return FALSE; |
577 | 3 | } |
578 | | |
579 | | /* Now, only the first monitor can be used, therefore the other channels will be ignored. */ |
580 | 104 | Stream_Seek(s, 5 * (numChannels - 1)); |
581 | 104 | WLog_Print(context->priv->log, WLOG_DEBUG, |
582 | 104 | "numChannels %" PRIu8 " id %" PRIu8 ", %" PRIu16 "x%" PRIu16 ".", numChannels, |
583 | 104 | channelId, context->width, context->height); |
584 | 104 | context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS; |
585 | 104 | return TRUE; |
586 | 107 | } |
587 | | |
588 | | static BOOL rfx_process_message_context(RFX_CONTEXT* context, wStream* s) |
589 | 206 | { |
590 | 206 | BYTE ctxId = 0; |
591 | 206 | UINT16 tileSize = 0; |
592 | 206 | UINT16 properties = 0; |
593 | | |
594 | 206 | WINPR_ASSERT(context); |
595 | 206 | WINPR_ASSERT(context->priv); |
596 | 206 | context->decodedHeaderBlocks &= ~RFX_DECODED_CONTEXT; |
597 | | |
598 | 206 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5)) |
599 | 3 | return FALSE; |
600 | | |
601 | 203 | Stream_Read_UINT8(s, ctxId); /* ctxId (1 byte), must be set to 0x00 */ |
602 | 203 | Stream_Read_UINT16(s, tileSize); /* tileSize (2 bytes), must be set to CT_TILE_64x64 (0x0040) */ |
603 | 203 | Stream_Read_UINT16(s, properties); /* properties (2 bytes) */ |
604 | 203 | WLog_Print(context->priv->log, WLOG_DEBUG, |
605 | 203 | "ctxId %" PRIu8 " tileSize %" PRIu16 " properties 0x%04" PRIX16 ".", ctxId, tileSize, |
606 | 203 | properties); |
607 | 203 | context->properties = properties; |
608 | 203 | context->flags = (properties & 0x0007); |
609 | | |
610 | 203 | if (context->flags == CODEC_MODE) |
611 | 36 | { |
612 | 36 | WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in image mode."); |
613 | 36 | } |
614 | 167 | else |
615 | 167 | { |
616 | 167 | WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in video mode."); |
617 | 167 | } |
618 | | |
619 | 203 | switch ((properties & 0x1E00) >> 9) |
620 | 203 | { |
621 | 97 | case CLW_ENTROPY_RLGR1: |
622 | 97 | context->mode = RLGR1; |
623 | 97 | WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR1."); |
624 | 97 | break; |
625 | | |
626 | 94 | case CLW_ENTROPY_RLGR3: |
627 | 94 | context->mode = RLGR3; |
628 | 94 | WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR3."); |
629 | 94 | break; |
630 | | |
631 | 12 | default: |
632 | 12 | WLog_Print(context->priv->log, WLOG_ERROR, "unknown RLGR algorithm."); |
633 | 12 | return FALSE; |
634 | 203 | } |
635 | | |
636 | 191 | context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT; |
637 | 191 | return TRUE; |
638 | 203 | } |
639 | | |
640 | | static BOOL rfx_process_message_frame_begin(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s, |
641 | | UINT16* pExpectedBlockType) |
642 | 0 | { |
643 | 0 | UINT32 frameIdx = 0; |
644 | 0 | UINT16 numRegions = 0; |
645 | |
|
646 | 0 | WINPR_ASSERT(context); |
647 | 0 | WINPR_ASSERT(context->priv); |
648 | 0 | WINPR_ASSERT(message); |
649 | 0 | WINPR_ASSERT(pExpectedBlockType); |
650 | | |
651 | 0 | if (*pExpectedBlockType != WBT_FRAME_BEGIN) |
652 | 0 | { |
653 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_FRAME_BEGIN"); |
654 | 0 | return FALSE; |
655 | 0 | } |
656 | | |
657 | 0 | *pExpectedBlockType = WBT_REGION; |
658 | |
|
659 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
660 | 0 | return FALSE; |
661 | | |
662 | 0 | Stream_Read_UINT32( |
663 | 0 | s, frameIdx); /* frameIdx (4 bytes), if codec is in video mode, must be ignored */ |
664 | 0 | Stream_Read_UINT16(s, numRegions); /* numRegions (2 bytes) */ |
665 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
666 | 0 | "RFX_FRAME_BEGIN: frameIdx: %" PRIu32 " numRegions: %" PRIu16 "", frameIdx, |
667 | 0 | numRegions); |
668 | 0 | return TRUE; |
669 | 0 | } |
670 | | |
671 | | static BOOL rfx_process_message_frame_end(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s, |
672 | | UINT16* pExpectedBlockType) |
673 | 0 | { |
674 | 0 | WINPR_ASSERT(context); |
675 | 0 | WINPR_ASSERT(context->priv); |
676 | 0 | WINPR_ASSERT(message); |
677 | 0 | WINPR_ASSERT(s); |
678 | 0 | WINPR_ASSERT(pExpectedBlockType); |
679 | | |
680 | 0 | if (*pExpectedBlockType != WBT_FRAME_END) |
681 | 0 | { |
682 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected, wants WBT_FRAME_END"); |
683 | 0 | return FALSE; |
684 | 0 | } |
685 | | |
686 | 0 | *pExpectedBlockType = WBT_FRAME_BEGIN; |
687 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "RFX_FRAME_END"); |
688 | 0 | return TRUE; |
689 | 0 | } |
690 | | |
691 | | static BOOL rfx_resize_rects(RFX_MESSAGE* message) |
692 | 0 | { |
693 | 0 | WINPR_ASSERT(message); |
694 | | |
695 | 0 | RFX_RECT* tmpRects = |
696 | 0 | winpr_aligned_recalloc(message->rects, message->numRects, sizeof(RFX_RECT), 32); |
697 | 0 | if (!tmpRects) |
698 | 0 | return FALSE; |
699 | 0 | message->rects = tmpRects; |
700 | 0 | return TRUE; |
701 | 0 | } |
702 | | |
703 | | static BOOL rfx_process_message_region(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s, |
704 | | UINT16* pExpectedBlockType) |
705 | 0 | { |
706 | 0 | UINT16 regionType = 0; |
707 | 0 | UINT16 numTileSets = 0; |
708 | |
|
709 | 0 | WINPR_ASSERT(context); |
710 | 0 | WINPR_ASSERT(context->priv); |
711 | 0 | WINPR_ASSERT(message); |
712 | 0 | WINPR_ASSERT(pExpectedBlockType); |
713 | | |
714 | 0 | if (*pExpectedBlockType != WBT_REGION) |
715 | 0 | { |
716 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_REGION"); |
717 | 0 | return FALSE; |
718 | 0 | } |
719 | | |
720 | 0 | *pExpectedBlockType = WBT_EXTENSION; |
721 | |
|
722 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3)) |
723 | 0 | return FALSE; |
724 | | |
725 | 0 | Stream_Seek_UINT8(s); /* regionFlags (1 byte) */ |
726 | 0 | Stream_Read_UINT16(s, message->numRects); /* numRects (2 bytes) */ |
727 | |
|
728 | 0 | if (message->numRects < 1) |
729 | 0 | { |
730 | | /* |
731 | | If numRects is zero the decoder must generate a rectangle with |
732 | | coordinates (0, 0, width, height). |
733 | | See [MS-RDPRFX] (revision >= 17.0) 2.2.2.3.3 TS_RFX_REGION |
734 | | https://msdn.microsoft.com/en-us/library/ff635233.aspx |
735 | | */ |
736 | 0 | message->numRects = 1; |
737 | 0 | if (!rfx_resize_rects(message)) |
738 | 0 | return FALSE; |
739 | | |
740 | 0 | message->rects->x = 0; |
741 | 0 | message->rects->y = 0; |
742 | 0 | message->rects->width = context->width; |
743 | 0 | message->rects->height = context->height; |
744 | 0 | return TRUE; |
745 | 0 | } |
746 | | |
747 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull)) |
748 | 0 | return FALSE; |
749 | | |
750 | 0 | if (!rfx_resize_rects(message)) |
751 | 0 | return FALSE; |
752 | | |
753 | | /* rects */ |
754 | 0 | for (UINT16 i = 0; i < message->numRects; i++) |
755 | 0 | { |
756 | 0 | RFX_RECT* rect = rfx_message_get_rect(message, i); |
757 | | /* RFX_RECT */ |
758 | 0 | Stream_Read_UINT16(s, rect->x); /* x (2 bytes) */ |
759 | 0 | Stream_Read_UINT16(s, rect->y); /* y (2 bytes) */ |
760 | 0 | Stream_Read_UINT16(s, rect->width); /* width (2 bytes) */ |
761 | 0 | Stream_Read_UINT16(s, rect->height); /* height (2 bytes) */ |
762 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
763 | 0 | "rect %" PRIu16 " (x,y=%" PRIu16 ",%" PRIu16 " w,h=%" PRIu16 " %" PRIu16 ").", i, |
764 | 0 | rect->x, rect->y, rect->width, rect->height); |
765 | 0 | } |
766 | |
|
767 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4)) |
768 | 0 | return FALSE; |
769 | | |
770 | 0 | Stream_Read_UINT16(s, regionType); /*regionType (2 bytes): MUST be set to CBT_REGION (0xCAC1)*/ |
771 | 0 | Stream_Read_UINT16(s, numTileSets); /*numTilesets (2 bytes): MUST be set to 0x0001.*/ |
772 | |
|
773 | 0 | if (regionType != CBT_REGION) |
774 | 0 | { |
775 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid region type 0x%04" PRIX16 "", |
776 | 0 | regionType); |
777 | 0 | return TRUE; |
778 | 0 | } |
779 | | |
780 | 0 | if (numTileSets != 0x0001) |
781 | 0 | { |
782 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid number of tilesets (%" PRIu16 ")", |
783 | 0 | numTileSets); |
784 | 0 | return FALSE; |
785 | 0 | } |
786 | | |
787 | 0 | return TRUE; |
788 | 0 | } |
789 | | |
790 | | typedef struct |
791 | | { |
792 | | RFX_TILE* tile; |
793 | | RFX_CONTEXT* context; |
794 | | } RFX_TILE_PROCESS_WORK_PARAM; |
795 | | |
796 | | static void CALLBACK rfx_process_message_tile_work_callback(PTP_CALLBACK_INSTANCE instance, |
797 | | void* context, PTP_WORK work) |
798 | 0 | { |
799 | 0 | RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context; |
800 | 0 | WINPR_ASSERT(param); |
801 | 0 | rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4); |
802 | 0 | } |
803 | | |
804 | | static BOOL rfx_allocate_tiles(RFX_MESSAGE* message, size_t count, BOOL allocOnly) |
805 | 0 | { |
806 | 0 | WINPR_ASSERT(message); |
807 | | |
808 | 0 | RFX_TILE** tmpTiles = winpr_aligned_recalloc(message->tiles, count, sizeof(RFX_TILE*), 32); |
809 | 0 | if (!tmpTiles && (count != 0)) |
810 | 0 | return FALSE; |
811 | | |
812 | 0 | message->tiles = tmpTiles; |
813 | 0 | if (!allocOnly) |
814 | 0 | message->numTiles = count; |
815 | 0 | else |
816 | 0 | { |
817 | 0 | WINPR_ASSERT(message->numTiles <= count); |
818 | 0 | } |
819 | 0 | message->allocatedTiles = count; |
820 | |
|
821 | 0 | return TRUE; |
822 | 0 | } |
823 | | static BOOL rfx_process_message_tileset(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s, |
824 | | UINT16* pExpectedBlockType) |
825 | 0 | { |
826 | 0 | BOOL rc = 0; |
827 | 0 | size_t close_cnt = 0; |
828 | 0 | BYTE quant = 0; |
829 | 0 | RFX_TILE* tile = NULL; |
830 | 0 | UINT32* quants = NULL; |
831 | 0 | UINT16 subtype = 0; |
832 | 0 | UINT16 numTiles = 0; |
833 | 0 | UINT32 blockLen = 0; |
834 | 0 | UINT32 blockType = 0; |
835 | 0 | UINT32 tilesDataSize = 0; |
836 | 0 | PTP_WORK* work_objects = NULL; |
837 | 0 | RFX_TILE_PROCESS_WORK_PARAM* params = NULL; |
838 | 0 | void* pmem = NULL; |
839 | |
|
840 | 0 | WINPR_ASSERT(context); |
841 | 0 | WINPR_ASSERT(context->priv); |
842 | 0 | WINPR_ASSERT(message); |
843 | 0 | WINPR_ASSERT(pExpectedBlockType); |
844 | | |
845 | 0 | if (*pExpectedBlockType != WBT_EXTENSION) |
846 | 0 | { |
847 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants a tileset"); |
848 | 0 | return FALSE; |
849 | 0 | } |
850 | | |
851 | 0 | *pExpectedBlockType = WBT_FRAME_END; |
852 | |
|
853 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14)) |
854 | 0 | return FALSE; |
855 | | |
856 | 0 | Stream_Read_UINT16(s, subtype); /* subtype (2 bytes) must be set to CBT_TILESET (0xCAC2) */ |
857 | 0 | if (subtype != CBT_TILESET) |
858 | 0 | { |
859 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid subtype, expected CBT_TILESET."); |
860 | 0 | return FALSE; |
861 | 0 | } |
862 | | |
863 | 0 | Stream_Seek_UINT16(s); /* idx (2 bytes), must be set to 0x0000 */ |
864 | 0 | Stream_Seek_UINT16(s); /* properties (2 bytes) */ |
865 | 0 | Stream_Read_UINT8(s, context->numQuant); /* numQuant (1 byte) */ |
866 | 0 | Stream_Seek_UINT8(s); /* tileSize (1 byte), must be set to 0x40 */ |
867 | |
|
868 | 0 | if (context->numQuant < 1) |
869 | 0 | { |
870 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "no quantization value."); |
871 | 0 | return FALSE; |
872 | 0 | } |
873 | | |
874 | 0 | Stream_Read_UINT16(s, numTiles); /* numTiles (2 bytes) */ |
875 | 0 | if (numTiles < 1) |
876 | 0 | { |
877 | | /* Windows Server 2012 (not R2) can send empty tile sets */ |
878 | 0 | return TRUE; |
879 | 0 | } |
880 | | |
881 | 0 | Stream_Read_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */ |
882 | |
|
883 | 0 | if (!(pmem = |
884 | 0 | winpr_aligned_recalloc(context->quants, context->numQuant, 10 * sizeof(UINT32), 32))) |
885 | 0 | return FALSE; |
886 | | |
887 | 0 | quants = context->quants = (UINT32*)pmem; |
888 | | |
889 | | /* quantVals */ |
890 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull)) |
891 | 0 | return FALSE; |
892 | | |
893 | 0 | for (size_t i = 0; i < context->numQuant; i++) |
894 | 0 | { |
895 | | /* RFX_CODEC_QUANT */ |
896 | 0 | Stream_Read_UINT8(s, quant); |
897 | 0 | *quants++ = (quant & 0x0F); |
898 | 0 | *quants++ = (quant >> 4); |
899 | 0 | Stream_Read_UINT8(s, quant); |
900 | 0 | *quants++ = (quant & 0x0F); |
901 | 0 | *quants++ = (quant >> 4); |
902 | 0 | Stream_Read_UINT8(s, quant); |
903 | 0 | *quants++ = (quant & 0x0F); |
904 | 0 | *quants++ = (quant >> 4); |
905 | 0 | Stream_Read_UINT8(s, quant); |
906 | 0 | *quants++ = (quant & 0x0F); |
907 | 0 | *quants++ = (quant >> 4); |
908 | 0 | Stream_Read_UINT8(s, quant); |
909 | 0 | *quants++ = (quant & 0x0F); |
910 | 0 | *quants++ = (quant >> 4); |
911 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
912 | 0 | "quant %d (%" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 |
913 | 0 | " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 ").", |
914 | 0 | i, context->quants[i * 10], context->quants[i * 10 + 1], |
915 | 0 | context->quants[i * 10 + 2], context->quants[i * 10 + 3], |
916 | 0 | context->quants[i * 10 + 4], context->quants[i * 10 + 5], |
917 | 0 | context->quants[i * 10 + 6], context->quants[i * 10 + 7], |
918 | 0 | context->quants[i * 10 + 8], context->quants[i * 10 + 9]); |
919 | 0 | } |
920 | |
|
921 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
922 | 0 | { |
923 | 0 | ObjectPool_Return(context->priv->TilePool, message->tiles[i]); |
924 | 0 | message->tiles[i] = NULL; |
925 | 0 | } |
926 | |
|
927 | 0 | if (!rfx_allocate_tiles(message, numTiles, FALSE)) |
928 | 0 | return FALSE; |
929 | | |
930 | 0 | if (context->priv->UseThreads) |
931 | 0 | { |
932 | 0 | work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles, sizeof(PTP_WORK), 32); |
933 | 0 | params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc( |
934 | 0 | NULL, message->numTiles, sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32); |
935 | |
|
936 | 0 | if (!work_objects) |
937 | 0 | { |
938 | 0 | winpr_aligned_free(params); |
939 | 0 | return FALSE; |
940 | 0 | } |
941 | | |
942 | 0 | if (!params) |
943 | 0 | { |
944 | 0 | winpr_aligned_free(work_objects); |
945 | 0 | return FALSE; |
946 | 0 | } |
947 | 0 | } |
948 | | |
949 | | /* tiles */ |
950 | 0 | close_cnt = 0; |
951 | 0 | rc = FALSE; |
952 | |
|
953 | 0 | if (Stream_GetRemainingLength(s) >= tilesDataSize) |
954 | 0 | { |
955 | 0 | rc = TRUE; |
956 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
957 | 0 | { |
958 | 0 | wStream subBuffer; |
959 | 0 | wStream* sub = NULL; |
960 | |
|
961 | 0 | if (!(tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool))) |
962 | 0 | { |
963 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
964 | 0 | "RfxMessageTileSet failed to get tile from object pool"); |
965 | 0 | rc = FALSE; |
966 | 0 | break; |
967 | 0 | } |
968 | | |
969 | 0 | message->tiles[i] = tile; |
970 | | |
971 | | /* RFX_TILE */ |
972 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
973 | 0 | { |
974 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
975 | 0 | "RfxMessageTileSet packet too small to read tile %d/%" PRIu16 "", i, |
976 | 0 | message->numTiles); |
977 | 0 | rc = FALSE; |
978 | 0 | break; |
979 | 0 | } |
980 | | |
981 | 0 | sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s)); |
982 | 0 | Stream_Read_UINT16( |
983 | 0 | sub, blockType); /* blockType (2 bytes), must be set to CBT_TILE (0xCAC3) */ |
984 | 0 | Stream_Read_UINT32(sub, blockLen); /* blockLen (4 bytes) */ |
985 | |
|
986 | 0 | if (!Stream_SafeSeek(s, blockLen)) |
987 | 0 | { |
988 | 0 | rc = FALSE; |
989 | 0 | break; |
990 | 0 | } |
991 | 0 | if ((blockLen < 6 + 13) || |
992 | 0 | (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6))) |
993 | 0 | { |
994 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
995 | 0 | "RfxMessageTileSet not enough bytes to read tile %d/%" PRIu16 |
996 | 0 | " with blocklen=%" PRIu32 "", |
997 | 0 | i, message->numTiles, blockLen); |
998 | 0 | rc = FALSE; |
999 | 0 | break; |
1000 | 0 | } |
1001 | | |
1002 | 0 | if (blockType != CBT_TILE) |
1003 | 0 | { |
1004 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1005 | 0 | "unknown block type 0x%" PRIX32 ", expected CBT_TILE (0xCAC3).", |
1006 | 0 | blockType); |
1007 | 0 | rc = FALSE; |
1008 | 0 | break; |
1009 | 0 | } |
1010 | | |
1011 | 0 | Stream_Read_UINT8(sub, tile->quantIdxY); /* quantIdxY (1 byte) */ |
1012 | 0 | Stream_Read_UINT8(sub, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
1013 | 0 | Stream_Read_UINT8(sub, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
1014 | 0 | if (tile->quantIdxY >= context->numQuant) |
1015 | 0 | { |
1016 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1017 | 0 | "quantIdxY %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxY, |
1018 | 0 | context->numQuant); |
1019 | 0 | rc = FALSE; |
1020 | 0 | break; |
1021 | 0 | } |
1022 | 0 | if (tile->quantIdxCb >= context->numQuant) |
1023 | 0 | { |
1024 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1025 | 0 | "quantIdxCb %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCb, |
1026 | 0 | context->numQuant); |
1027 | 0 | rc = FALSE; |
1028 | 0 | break; |
1029 | 0 | } |
1030 | 0 | if (tile->quantIdxCr >= context->numQuant) |
1031 | 0 | { |
1032 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1033 | 0 | "quantIdxCr %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCr, |
1034 | 0 | context->numQuant); |
1035 | 0 | rc = FALSE; |
1036 | 0 | break; |
1037 | 0 | } |
1038 | | |
1039 | 0 | Stream_Read_UINT16(sub, tile->xIdx); /* xIdx (2 bytes) */ |
1040 | 0 | Stream_Read_UINT16(sub, tile->yIdx); /* yIdx (2 bytes) */ |
1041 | 0 | Stream_Read_UINT16(sub, tile->YLen); /* YLen (2 bytes) */ |
1042 | 0 | Stream_Read_UINT16(sub, tile->CbLen); /* CbLen (2 bytes) */ |
1043 | 0 | Stream_Read_UINT16(sub, tile->CrLen); /* CrLen (2 bytes) */ |
1044 | 0 | Stream_GetPointer(sub, tile->YData); |
1045 | 0 | if (!Stream_SafeSeek(sub, tile->YLen)) |
1046 | 0 | { |
1047 | 0 | rc = FALSE; |
1048 | 0 | break; |
1049 | 0 | } |
1050 | 0 | Stream_GetPointer(sub, tile->CbData); |
1051 | 0 | if (!Stream_SafeSeek(sub, tile->CbLen)) |
1052 | 0 | { |
1053 | 0 | rc = FALSE; |
1054 | 0 | break; |
1055 | 0 | } |
1056 | 0 | Stream_GetPointer(sub, tile->CrData); |
1057 | 0 | if (!Stream_SafeSeek(sub, tile->CrLen)) |
1058 | 0 | { |
1059 | 0 | rc = FALSE; |
1060 | 0 | break; |
1061 | 0 | } |
1062 | 0 | tile->x = tile->xIdx * 64; |
1063 | 0 | tile->y = tile->yIdx * 64; |
1064 | |
|
1065 | 0 | if (context->priv->UseThreads) |
1066 | 0 | { |
1067 | 0 | if (!params) |
1068 | 0 | { |
1069 | 0 | rc = FALSE; |
1070 | 0 | break; |
1071 | 0 | } |
1072 | | |
1073 | 0 | params[i].context = context; |
1074 | 0 | params[i].tile = message->tiles[i]; |
1075 | |
|
1076 | 0 | if (!(work_objects[i] = |
1077 | 0 | CreateThreadpoolWork(rfx_process_message_tile_work_callback, |
1078 | 0 | (void*)¶ms[i], &context->priv->ThreadPoolEnv))) |
1079 | 0 | { |
1080 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "CreateThreadpoolWork failed."); |
1081 | 0 | rc = FALSE; |
1082 | 0 | break; |
1083 | 0 | } |
1084 | | |
1085 | 0 | SubmitThreadpoolWork(work_objects[i]); |
1086 | 0 | close_cnt = i + 1; |
1087 | 0 | } |
1088 | 0 | else |
1089 | 0 | { |
1090 | 0 | rfx_decode_rgb(context, tile, tile->data, 64 * 4); |
1091 | 0 | } |
1092 | 0 | } |
1093 | 0 | } |
1094 | |
|
1095 | 0 | if (context->priv->UseThreads) |
1096 | 0 | { |
1097 | 0 | for (size_t i = 0; i < close_cnt; i++) |
1098 | 0 | { |
1099 | 0 | WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE); |
1100 | 0 | CloseThreadpoolWork(work_objects[i]); |
1101 | 0 | } |
1102 | 0 | } |
1103 | |
|
1104 | 0 | winpr_aligned_free(work_objects); |
1105 | 0 | winpr_aligned_free(params); |
1106 | |
|
1107 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
1108 | 0 | { |
1109 | 0 | if (!(tile = message->tiles[i])) |
1110 | 0 | continue; |
1111 | | |
1112 | 0 | tile->YLen = tile->CbLen = tile->CrLen = 0; |
1113 | 0 | tile->YData = tile->CbData = tile->CrData = NULL; |
1114 | 0 | } |
1115 | |
|
1116 | 0 | return rc; |
1117 | 0 | } |
1118 | | |
1119 | | BOOL rfx_process_message(RFX_CONTEXT* context, const BYTE* data, UINT32 length, UINT32 left, |
1120 | | UINT32 top, BYTE* dst, UINT32 dstFormat, UINT32 dstStride, |
1121 | | UINT32 dstHeight, REGION16* invalidRegion) |
1122 | 6.63k | { |
1123 | 6.63k | REGION16 updateRegion = { 0 }; |
1124 | 6.63k | wStream inStream = { 0 }; |
1125 | 6.63k | BOOL ok = TRUE; |
1126 | | |
1127 | 6.63k | if (!context || !data || !length) |
1128 | 0 | return FALSE; |
1129 | | |
1130 | 6.63k | WINPR_ASSERT(context->priv); |
1131 | 6.63k | RFX_MESSAGE* message = &context->currentMessage; |
1132 | | |
1133 | 6.63k | wStream* s = Stream_StaticConstInit(&inStream, data, length); |
1134 | | |
1135 | 7.86k | while (ok && Stream_GetRemainingLength(s) > 6) |
1136 | 5.52k | { |
1137 | 5.52k | wStream subStreamBuffer = { 0 }; |
1138 | 5.52k | size_t extraBlockLen = 0; |
1139 | 5.52k | UINT32 blockLen = 0; |
1140 | 5.52k | UINT32 blockType = 0; |
1141 | | |
1142 | | /* RFX_BLOCKT */ |
1143 | 5.52k | Stream_Read_UINT16(s, blockType); /* blockType (2 bytes) */ |
1144 | 5.52k | Stream_Read_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
1145 | 5.52k | WLog_Print(context->priv->log, WLOG_DEBUG, "blockType 0x%" PRIX32 " blockLen %" PRIu32 "", |
1146 | 5.52k | blockType, blockLen); |
1147 | | |
1148 | 5.52k | if (blockLen < 6) |
1149 | 1.39k | { |
1150 | 1.39k | WLog_Print(context->priv->log, WLOG_ERROR, "blockLen too small(%" PRIu32 ")", blockLen); |
1151 | 1.39k | return FALSE; |
1152 | 1.39k | } |
1153 | | |
1154 | 4.12k | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6)) |
1155 | 2.36k | return FALSE; |
1156 | | |
1157 | 1.76k | if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS) |
1158 | 163 | { |
1159 | 163 | WLog_Print(context->priv->log, WLOG_ERROR, "incomplete header blocks processing"); |
1160 | 163 | return FALSE; |
1161 | 163 | } |
1162 | | |
1163 | 1.60k | if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION) |
1164 | 305 | { |
1165 | | /* RFX_CODEC_CHANNELT */ |
1166 | 305 | UINT8 codecId = 0; |
1167 | 305 | UINT8 channelId = 0; |
1168 | | |
1169 | 305 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2)) |
1170 | 2 | return FALSE; |
1171 | | |
1172 | 303 | extraBlockLen = 2; |
1173 | 303 | Stream_Read_UINT8(s, codecId); /* codecId (1 byte) must be set to 0x01 */ |
1174 | 303 | Stream_Read_UINT8(s, channelId); /* channelId (1 byte) 0xFF or 0x00, see below */ |
1175 | | |
1176 | 303 | if (codecId != 0x01) |
1177 | 55 | { |
1178 | 55 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codecId 0x%02" PRIX8 "", |
1179 | 55 | codecId); |
1180 | 55 | return FALSE; |
1181 | 55 | } |
1182 | | |
1183 | 248 | if (blockType == WBT_CONTEXT) |
1184 | 248 | { |
1185 | | /* If the blockType is set to WBT_CONTEXT, then channelId MUST be set to 0xFF.*/ |
1186 | 248 | if (channelId != 0xFF) |
1187 | 39 | { |
1188 | 39 | WLog_Print(context->priv->log, WLOG_ERROR, |
1189 | 39 | "invalid channelId 0x%02" PRIX8 " for blockType 0x%08" PRIX32 "", |
1190 | 39 | channelId, blockType); |
1191 | 39 | return FALSE; |
1192 | 39 | } |
1193 | 248 | } |
1194 | 0 | else |
1195 | 0 | { |
1196 | | /* For all other values of blockType, channelId MUST be set to 0x00. */ |
1197 | 0 | if (channelId != 0x00) |
1198 | 0 | { |
1199 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1200 | 0 | "invalid channelId 0x%02" PRIX8 " for blockType WBT_CONTEXT", |
1201 | 0 | channelId); |
1202 | 0 | return FALSE; |
1203 | 0 | } |
1204 | 0 | } |
1205 | 248 | } |
1206 | | |
1207 | 1.50k | const size_t blockLenNoHeader = blockLen - 6; |
1208 | 1.50k | if (blockLenNoHeader < extraBlockLen) |
1209 | 3 | { |
1210 | 3 | WLog_Print(context->priv->log, WLOG_ERROR, |
1211 | 3 | "blockLen too small(%" PRIu32 "), must be >= 6 + %" PRIu16, blockLen, |
1212 | 3 | extraBlockLen); |
1213 | 3 | return FALSE; |
1214 | 3 | } |
1215 | | |
1216 | 1.50k | const size_t subStreamLen = blockLenNoHeader - extraBlockLen; |
1217 | 1.50k | wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen); |
1218 | 1.50k | Stream_Seek(s, subStreamLen); |
1219 | | |
1220 | 1.50k | switch (blockType) |
1221 | 1.50k | { |
1222 | | /* Header messages: |
1223 | | * The stream MUST start with the header messages and any of these headers can appear |
1224 | | * in the stream at a later stage. The header messages can be repeated. |
1225 | | */ |
1226 | 245 | case WBT_SYNC: |
1227 | 245 | ok = rfx_process_message_sync(context, subStream); |
1228 | 245 | break; |
1229 | | |
1230 | 206 | case WBT_CONTEXT: |
1231 | 206 | ok = rfx_process_message_context(context, subStream); |
1232 | 206 | break; |
1233 | | |
1234 | 605 | case WBT_CODEC_VERSIONS: |
1235 | 605 | ok = rfx_process_message_codec_versions(context, subStream); |
1236 | 605 | break; |
1237 | | |
1238 | 171 | case WBT_CHANNELS: |
1239 | 171 | ok = rfx_process_message_channels(context, subStream); |
1240 | 171 | break; |
1241 | | |
1242 | | /* Data messages: |
1243 | | * The data associated with each encoded frame or image is always bracketed by the |
1244 | | * TS_RFX_FRAME_BEGIN (section 2.2.2.3.1) and TS_RFX_FRAME_END (section 2.2.2.3.2) |
1245 | | * messages. There MUST only be one TS_RFX_REGION (section 2.2.2.3.3) message per |
1246 | | * frame and one TS_RFX_TILESET (section 2.2.2.3.4) message per TS_RFX_REGION. |
1247 | | */ |
1248 | | |
1249 | 0 | case WBT_FRAME_BEGIN: |
1250 | 0 | ok = rfx_process_message_frame_begin(context, message, subStream, |
1251 | 0 | &context->expectedDataBlockType); |
1252 | 0 | break; |
1253 | | |
1254 | 0 | case WBT_REGION: |
1255 | 0 | ok = rfx_process_message_region(context, message, subStream, |
1256 | 0 | &context->expectedDataBlockType); |
1257 | 0 | break; |
1258 | | |
1259 | 0 | case WBT_EXTENSION: |
1260 | 0 | ok = rfx_process_message_tileset(context, message, subStream, |
1261 | 0 | &context->expectedDataBlockType); |
1262 | 0 | break; |
1263 | | |
1264 | 0 | case WBT_FRAME_END: |
1265 | 0 | ok = rfx_process_message_frame_end(context, message, subStream, |
1266 | 0 | &context->expectedDataBlockType); |
1267 | 0 | break; |
1268 | | |
1269 | 278 | default: |
1270 | 278 | WLog_Print(context->priv->log, WLOG_ERROR, "unknown blockType 0x%" PRIX32 "", |
1271 | 278 | blockType); |
1272 | 278 | return FALSE; |
1273 | 1.50k | } |
1274 | 1.50k | } |
1275 | | |
1276 | 2.34k | if (ok) |
1277 | 1.83k | { |
1278 | 1.83k | UINT32 nbUpdateRects = 0; |
1279 | 1.83k | REGION16 clippingRects = { 0 }; |
1280 | 1.83k | const RECTANGLE_16* updateRects = NULL; |
1281 | 1.83k | const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format); |
1282 | 1.83k | const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat); |
1283 | 1.83k | region16_init(&clippingRects); |
1284 | | |
1285 | 1.83k | WINPR_ASSERT(dstWidth <= UINT16_MAX); |
1286 | 1.83k | WINPR_ASSERT(dstHeight <= UINT16_MAX); |
1287 | 1.83k | for (UINT32 i = 0; i < message->numRects; i++) |
1288 | 0 | { |
1289 | 0 | RECTANGLE_16 clippingRect = { 0 }; |
1290 | 0 | const RFX_RECT* rect = &(message->rects[i]); |
1291 | |
|
1292 | 0 | WINPR_ASSERT(left + rect->x <= UINT16_MAX); |
1293 | 0 | WINPR_ASSERT(top + rect->y <= UINT16_MAX); |
1294 | 0 | WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX); |
1295 | 0 | WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX); |
1296 | | |
1297 | 0 | clippingRect.left = (UINT16)MIN(left + rect->x, dstWidth); |
1298 | 0 | clippingRect.top = (UINT16)MIN(top + rect->y, dstHeight); |
1299 | 0 | clippingRect.right = (UINT16)MIN(clippingRect.left + rect->width, dstWidth); |
1300 | 0 | clippingRect.bottom = (UINT16)MIN(clippingRect.top + rect->height, dstHeight); |
1301 | 0 | region16_union_rect(&clippingRects, &clippingRects, &clippingRect); |
1302 | 0 | } |
1303 | | |
1304 | 1.83k | for (UINT32 i = 0; i < message->numTiles; i++) |
1305 | 0 | { |
1306 | 0 | RECTANGLE_16 updateRect = { 0 }; |
1307 | 0 | const RFX_TILE* tile = rfx_message_get_tile(message, i); |
1308 | |
|
1309 | 0 | WINPR_ASSERT(left + tile->x <= UINT16_MAX); |
1310 | 0 | WINPR_ASSERT(top + tile->y <= UINT16_MAX); |
1311 | | |
1312 | 0 | updateRect.left = (UINT16)left + tile->x; |
1313 | 0 | updateRect.top = (UINT16)top + tile->y; |
1314 | 0 | updateRect.right = updateRect.left + 64; |
1315 | 0 | updateRect.bottom = updateRect.top + 64; |
1316 | 0 | region16_init(&updateRegion); |
1317 | 0 | region16_intersect_rect(&updateRegion, &clippingRects, &updateRect); |
1318 | 0 | updateRects = region16_rects(&updateRegion, &nbUpdateRects); |
1319 | |
|
1320 | 0 | for (UINT32 j = 0; j < nbUpdateRects; j++) |
1321 | 0 | { |
1322 | 0 | const UINT32 stride = 64 * formatSize; |
1323 | 0 | const UINT32 nXDst = updateRects[j].left; |
1324 | 0 | const UINT32 nYDst = updateRects[j].top; |
1325 | 0 | const UINT32 nXSrc = nXDst - updateRect.left; |
1326 | 0 | const UINT32 nYSrc = nYDst - updateRect.top; |
1327 | 0 | const UINT32 nWidth = updateRects[j].right - updateRects[j].left; |
1328 | 0 | const UINT32 nHeight = updateRects[j].bottom - updateRects[j].top; |
1329 | |
|
1330 | 0 | if (!freerdp_image_copy(dst, dstFormat, dstStride, nXDst, nYDst, nWidth, nHeight, |
1331 | 0 | tile->data, context->pixel_format, stride, nXSrc, nYSrc, |
1332 | 0 | NULL, FREERDP_FLIP_NONE)) |
1333 | 0 | { |
1334 | 0 | region16_uninit(&updateRegion); |
1335 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1336 | 0 | "nbUpdateRectx[%" PRIu32 " (%" PRIu32 ")] freerdp_image_copy failed", |
1337 | 0 | j, nbUpdateRects); |
1338 | 0 | return FALSE; |
1339 | 0 | } |
1340 | | |
1341 | 0 | if (invalidRegion) |
1342 | 0 | region16_union_rect(invalidRegion, invalidRegion, &updateRects[j]); |
1343 | 0 | } |
1344 | | |
1345 | 0 | region16_uninit(&updateRegion); |
1346 | 0 | } |
1347 | | |
1348 | 1.83k | region16_uninit(&clippingRects); |
1349 | 1.83k | return TRUE; |
1350 | 1.83k | } |
1351 | 502 | else |
1352 | 502 | { |
1353 | 502 | rfx_message_free(context, message); |
1354 | 502 | context->currentMessage.freeArray = TRUE; |
1355 | 502 | } |
1356 | | |
1357 | 502 | WLog_Print(context->priv->log, WLOG_ERROR, "failed"); |
1358 | 502 | return FALSE; |
1359 | 2.34k | } |
1360 | | |
1361 | | const UINT32* rfx_message_get_quants(const RFX_MESSAGE* message, UINT16* numQuantVals) |
1362 | 0 | { |
1363 | 0 | WINPR_ASSERT(message); |
1364 | 0 | if (numQuantVals) |
1365 | 0 | *numQuantVals = message->numQuant; |
1366 | 0 | return message->quantVals; |
1367 | 0 | } |
1368 | | |
1369 | | const RFX_TILE** rfx_message_get_tiles(const RFX_MESSAGE* message, UINT16* numTiles) |
1370 | 0 | { |
1371 | 0 | WINPR_ASSERT(message); |
1372 | 0 | if (numTiles) |
1373 | 0 | *numTiles = message->numTiles; |
1374 | 0 | return (const RFX_TILE**)message->tiles; |
1375 | 0 | } |
1376 | | |
1377 | | UINT16 rfx_message_get_tile_count(const RFX_MESSAGE* message) |
1378 | 0 | { |
1379 | 0 | WINPR_ASSERT(message); |
1380 | 0 | return message->numTiles; |
1381 | 0 | } |
1382 | | |
1383 | | const RFX_RECT* rfx_message_get_rects(const RFX_MESSAGE* message, UINT16* numRects) |
1384 | 0 | { |
1385 | 0 | WINPR_ASSERT(message); |
1386 | 0 | if (numRects) |
1387 | 0 | *numRects = message->numRects; |
1388 | 0 | return message->rects; |
1389 | 0 | } |
1390 | | |
1391 | | UINT16 rfx_message_get_rect_count(const RFX_MESSAGE* message) |
1392 | 0 | { |
1393 | 0 | WINPR_ASSERT(message); |
1394 | 0 | return message->numRects; |
1395 | 0 | } |
1396 | | |
1397 | | void rfx_message_free(RFX_CONTEXT* context, RFX_MESSAGE* message) |
1398 | 11.9k | { |
1399 | 11.9k | if (!message) |
1400 | 0 | return; |
1401 | | |
1402 | 11.9k | winpr_aligned_free(message->rects); |
1403 | | |
1404 | 11.9k | if (message->tiles) |
1405 | 0 | { |
1406 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
1407 | 0 | { |
1408 | 0 | RFX_TILE* tile = message->tiles[i]; |
1409 | 0 | if (!tile) |
1410 | 0 | continue; |
1411 | | |
1412 | 0 | if (tile->YCbCrData) |
1413 | 0 | { |
1414 | 0 | BufferPool_Return(context->priv->BufferPool, tile->YCbCrData); |
1415 | 0 | tile->YCbCrData = NULL; |
1416 | 0 | } |
1417 | |
|
1418 | 0 | ObjectPool_Return(context->priv->TilePool, (void*)tile); |
1419 | 0 | } |
1420 | |
|
1421 | 0 | rfx_allocate_tiles(message, 0, FALSE); |
1422 | 0 | } |
1423 | | |
1424 | 11.9k | const BOOL freeArray = message->freeArray; |
1425 | 11.9k | const RFX_MESSAGE empty = { 0 }; |
1426 | 11.9k | *message = empty; |
1427 | | |
1428 | 11.9k | if (!freeArray) |
1429 | 0 | winpr_aligned_free(message); |
1430 | 11.9k | } |
1431 | | |
1432 | | static void rfx_update_context_properties(RFX_CONTEXT* context) |
1433 | 0 | { |
1434 | 0 | UINT16 properties = 0; |
1435 | |
|
1436 | 0 | WINPR_ASSERT(context); |
1437 | | /* properties in tilesets: note that this has different format from the one in TS_RFX_CONTEXT */ |
1438 | 0 | properties = 1; /* lt */ |
1439 | 0 | properties |= (context->flags << 1); /* flags */ |
1440 | 0 | properties |= (COL_CONV_ICT << 4); /* cct */ |
1441 | 0 | properties |= (CLW_XFORM_DWT_53_A << 6); /* xft */ |
1442 | 0 | properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10); /* et */ |
1443 | 0 | properties |= (SCALAR_QUANTIZATION << 14); /* qt */ |
1444 | 0 | context->properties = properties; |
1445 | 0 | } |
1446 | | |
1447 | | static void rfx_write_message_sync(const RFX_CONTEXT* context, wStream* s) |
1448 | 0 | { |
1449 | 0 | WINPR_ASSERT(context); |
1450 | | |
1451 | 0 | Stream_Write_UINT16(s, WBT_SYNC); /* BlockT.blockType (2 bytes) */ |
1452 | 0 | Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */ |
1453 | 0 | Stream_Write_UINT32(s, WF_MAGIC); /* magic (4 bytes) */ |
1454 | 0 | Stream_Write_UINT16(s, WF_VERSION_1_0); /* version (2 bytes) */ |
1455 | 0 | } |
1456 | | |
1457 | | static void rfx_write_message_codec_versions(const RFX_CONTEXT* context, wStream* s) |
1458 | 0 | { |
1459 | 0 | WINPR_ASSERT(context); |
1460 | | |
1461 | 0 | Stream_Write_UINT16(s, WBT_CODEC_VERSIONS); /* BlockT.blockType (2 bytes) */ |
1462 | 0 | Stream_Write_UINT32(s, 10); /* BlockT.blockLen (4 bytes) */ |
1463 | 0 | Stream_Write_UINT8(s, 1); /* numCodecs (1 byte) */ |
1464 | 0 | Stream_Write_UINT8(s, 1); /* codecs.codecId (1 byte) */ |
1465 | 0 | Stream_Write_UINT16(s, WF_VERSION_1_0); /* codecs.version (2 bytes) */ |
1466 | 0 | } |
1467 | | |
1468 | | static void rfx_write_message_channels(const RFX_CONTEXT* context, wStream* s) |
1469 | 0 | { |
1470 | 0 | WINPR_ASSERT(context); |
1471 | | |
1472 | 0 | Stream_Write_UINT16(s, WBT_CHANNELS); /* BlockT.blockType (2 bytes) */ |
1473 | 0 | Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */ |
1474 | 0 | Stream_Write_UINT8(s, 1); /* numChannels (1 byte) */ |
1475 | 0 | Stream_Write_UINT8(s, 0); /* Channel.channelId (1 byte) */ |
1476 | 0 | Stream_Write_UINT16(s, context->width); /* Channel.width (2 bytes) */ |
1477 | 0 | Stream_Write_UINT16(s, context->height); /* Channel.height (2 bytes) */ |
1478 | 0 | } |
1479 | | |
1480 | | static void rfx_write_message_context(RFX_CONTEXT* context, wStream* s) |
1481 | 0 | { |
1482 | 0 | UINT16 properties = 0; |
1483 | 0 | WINPR_ASSERT(context); |
1484 | | |
1485 | 0 | Stream_Write_UINT16(s, WBT_CONTEXT); /* CodecChannelT.blockType (2 bytes) */ |
1486 | 0 | Stream_Write_UINT32(s, 13); /* CodecChannelT.blockLen (4 bytes) */ |
1487 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
1488 | 0 | Stream_Write_UINT8(s, 0xFF); /* CodecChannelT.channelId (1 byte) */ |
1489 | 0 | Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */ |
1490 | 0 | Stream_Write_UINT16(s, CT_TILE_64x64); /* tileSize (2 bytes) */ |
1491 | | /* properties */ |
1492 | 0 | properties = context->flags; /* flags */ |
1493 | 0 | properties |= (COL_CONV_ICT << 3); /* cct */ |
1494 | 0 | properties |= (CLW_XFORM_DWT_53_A << 5); /* xft */ |
1495 | 0 | properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9); /* et */ |
1496 | 0 | properties |= (SCALAR_QUANTIZATION << 13); /* qt */ |
1497 | 0 | Stream_Write_UINT16(s, properties); /* properties (2 bytes) */ |
1498 | 0 | rfx_update_context_properties(context); |
1499 | 0 | } |
1500 | | |
1501 | | static BOOL rfx_compose_message_header(RFX_CONTEXT* context, wStream* s) |
1502 | 0 | { |
1503 | 0 | WINPR_ASSERT(context); |
1504 | 0 | if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13)) |
1505 | 0 | return FALSE; |
1506 | | |
1507 | 0 | rfx_write_message_sync(context, s); |
1508 | 0 | rfx_write_message_context(context, s); |
1509 | 0 | rfx_write_message_codec_versions(context, s); |
1510 | 0 | rfx_write_message_channels(context, s); |
1511 | 0 | return TRUE; |
1512 | 0 | } |
1513 | | |
1514 | | static size_t rfx_tile_length(const RFX_TILE* tile) |
1515 | 0 | { |
1516 | 0 | WINPR_ASSERT(tile); |
1517 | 0 | return 19ull + tile->YLen + tile->CbLen + tile->CrLen; |
1518 | 0 | } |
1519 | | |
1520 | | static BOOL rfx_write_tile(wStream* s, const RFX_TILE* tile) |
1521 | 0 | { |
1522 | 0 | const size_t blockLen = rfx_tile_length(tile); |
1523 | |
|
1524 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
1525 | 0 | return FALSE; |
1526 | | |
1527 | 0 | Stream_Write_UINT16(s, CBT_TILE); /* BlockT.blockType (2 bytes) */ |
1528 | 0 | Stream_Write_UINT32(s, blockLen); /* BlockT.blockLen (4 bytes) */ |
1529 | 0 | Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */ |
1530 | 0 | Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
1531 | 0 | Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
1532 | 0 | Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */ |
1533 | 0 | Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */ |
1534 | 0 | Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */ |
1535 | 0 | Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */ |
1536 | 0 | Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */ |
1537 | 0 | Stream_Write(s, tile->YData, tile->YLen); /* YData */ |
1538 | 0 | Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */ |
1539 | 0 | Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */ |
1540 | 0 | return TRUE; |
1541 | 0 | } |
1542 | | |
1543 | | struct S_RFX_TILE_COMPOSE_WORK_PARAM |
1544 | | { |
1545 | | RFX_TILE* tile; |
1546 | | RFX_CONTEXT* context; |
1547 | | }; |
1548 | | |
1549 | | static void CALLBACK rfx_compose_message_tile_work_callback(PTP_CALLBACK_INSTANCE instance, |
1550 | | void* context, PTP_WORK work) |
1551 | 0 | { |
1552 | 0 | RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context; |
1553 | 0 | WINPR_ASSERT(param); |
1554 | 0 | rfx_encode_rgb(param->context, param->tile); |
1555 | 0 | } |
1556 | | |
1557 | | static BOOL computeRegion(const RFX_RECT* rects, size_t numRects, REGION16* region, size_t width, |
1558 | | size_t height) |
1559 | 0 | { |
1560 | 0 | const RECTANGLE_16 mainRect = { 0, 0, width, height }; |
1561 | |
|
1562 | 0 | WINPR_ASSERT(rects); |
1563 | 0 | for (size_t i = 0; i < numRects; i++) |
1564 | 0 | { |
1565 | 0 | const RFX_RECT* rect = &rects[i]; |
1566 | 0 | RECTANGLE_16 rect16 = { 0 }; |
1567 | 0 | rect16.left = rect->x; |
1568 | 0 | rect16.top = rect->y; |
1569 | 0 | rect16.right = rect->x + rect->width; |
1570 | 0 | rect16.bottom = rect->y + rect->height; |
1571 | |
|
1572 | 0 | if (!region16_union_rect(region, region, &rect16)) |
1573 | 0 | return FALSE; |
1574 | 0 | } |
1575 | | |
1576 | 0 | return region16_intersect_rect(region, region, &mainRect); |
1577 | 0 | } |
1578 | | |
1579 | 0 | #define TILE_NO(v) ((v) / 64) |
1580 | | |
1581 | | static BOOL setupWorkers(RFX_CONTEXT* context, size_t nbTiles) |
1582 | 0 | { |
1583 | 0 | WINPR_ASSERT(context); |
1584 | | |
1585 | 0 | RFX_CONTEXT_PRIV* priv = context->priv; |
1586 | 0 | WINPR_ASSERT(priv); |
1587 | | |
1588 | 0 | void* pmem = NULL; |
1589 | |
|
1590 | 0 | if (!context->priv->UseThreads) |
1591 | 0 | return TRUE; |
1592 | | |
1593 | 0 | if (!(pmem = winpr_aligned_recalloc(priv->workObjects, nbTiles, sizeof(PTP_WORK), 32))) |
1594 | 0 | return FALSE; |
1595 | | |
1596 | 0 | priv->workObjects = (PTP_WORK*)pmem; |
1597 | |
|
1598 | 0 | if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles, |
1599 | 0 | sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32))) |
1600 | 0 | return FALSE; |
1601 | | |
1602 | 0 | priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem; |
1603 | 0 | return TRUE; |
1604 | 0 | } |
1605 | | |
1606 | | static BOOL rfx_ensure_tiles(RFX_MESSAGE* message, size_t count) |
1607 | 0 | { |
1608 | 0 | WINPR_ASSERT(message); |
1609 | | |
1610 | 0 | if (message->numTiles + count <= message->allocatedTiles) |
1611 | 0 | return TRUE; |
1612 | | |
1613 | 0 | const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count); |
1614 | 0 | return rfx_allocate_tiles(message, alloc, TRUE); |
1615 | 0 | } |
1616 | | |
1617 | | RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* context, const RFX_RECT* rects, size_t numRects, |
1618 | | const BYTE* data, UINT32 w, UINT32 h, size_t s) |
1619 | 0 | { |
1620 | 0 | const UINT32 width = (UINT32)w; |
1621 | 0 | const UINT32 height = (UINT32)h; |
1622 | 0 | const UINT32 scanline = (UINT32)s; |
1623 | 0 | RFX_MESSAGE* message = NULL; |
1624 | 0 | PTP_WORK* workObject = NULL; |
1625 | 0 | RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL; |
1626 | 0 | BOOL success = FALSE; |
1627 | 0 | REGION16 rectsRegion = { 0 }; |
1628 | 0 | REGION16 tilesRegion = { 0 }; |
1629 | 0 | RECTANGLE_16 currentTileRect = { 0 }; |
1630 | 0 | const RECTANGLE_16* regionRect = NULL; |
1631 | |
|
1632 | 0 | WINPR_ASSERT(data); |
1633 | 0 | WINPR_ASSERT(rects); |
1634 | 0 | WINPR_ASSERT(numRects > 0); |
1635 | 0 | WINPR_ASSERT(w > 0); |
1636 | 0 | WINPR_ASSERT(h > 0); |
1637 | 0 | WINPR_ASSERT(s > 0); |
1638 | | |
1639 | 0 | if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1, sizeof(RFX_MESSAGE), 32))) |
1640 | 0 | return NULL; |
1641 | | |
1642 | 0 | region16_init(&tilesRegion); |
1643 | 0 | region16_init(&rectsRegion); |
1644 | |
|
1645 | 0 | if (context->state == RFX_STATE_SEND_HEADERS) |
1646 | 0 | rfx_update_context_properties(context); |
1647 | |
|
1648 | 0 | message->frameIdx = context->frameIdx++; |
1649 | |
|
1650 | 0 | if (!context->numQuant) |
1651 | 0 | { |
1652 | 0 | WINPR_ASSERT(context->quants == NULL); |
1653 | 0 | if (!(context->quants = |
1654 | 0 | (UINT32*)winpr_aligned_malloc(sizeof(rfx_default_quantization_values), 32))) |
1655 | 0 | goto skip_encoding_loop; |
1656 | | |
1657 | 0 | CopyMemory(context->quants, &rfx_default_quantization_values, |
1658 | 0 | sizeof(rfx_default_quantization_values)); |
1659 | 0 | context->numQuant = 1; |
1660 | 0 | context->quantIdxY = 0; |
1661 | 0 | context->quantIdxCb = 0; |
1662 | 0 | context->quantIdxCr = 0; |
1663 | 0 | } |
1664 | | |
1665 | 0 | message->numQuant = context->numQuant; |
1666 | 0 | message->quantVals = context->quants; |
1667 | 0 | const UINT32 bytesPerPixel = (context->bits_per_pixel / 8); |
1668 | |
|
1669 | 0 | if (!computeRegion(rects, numRects, &rectsRegion, width, height)) |
1670 | 0 | goto skip_encoding_loop; |
1671 | | |
1672 | 0 | const RECTANGLE_16* extents = region16_extents(&rectsRegion); |
1673 | 0 | WINPR_ASSERT((INT32)extents->right - extents->left > 0); |
1674 | 0 | WINPR_ASSERT((INT32)extents->bottom - extents->top > 0); |
1675 | 0 | const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left); |
1676 | 0 | const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top); |
1677 | 0 | const UINT32 maxNbTiles = maxTilesX * maxTilesY; |
1678 | |
|
1679 | 0 | if (!rfx_ensure_tiles(message, maxNbTiles)) |
1680 | 0 | goto skip_encoding_loop; |
1681 | | |
1682 | 0 | if (!setupWorkers(context, maxNbTiles)) |
1683 | 0 | goto skip_encoding_loop; |
1684 | | |
1685 | 0 | if (context->priv->UseThreads) |
1686 | 0 | { |
1687 | 0 | workObject = context->priv->workObjects; |
1688 | 0 | workParam = context->priv->tileWorkParams; |
1689 | 0 | } |
1690 | |
|
1691 | 0 | UINT32 regionNbRects = 0; |
1692 | 0 | regionRect = region16_rects(&rectsRegion, ®ionNbRects); |
1693 | |
|
1694 | 0 | if (!(message->rects = winpr_aligned_calloc(regionNbRects, sizeof(RFX_RECT), 32))) |
1695 | 0 | goto skip_encoding_loop; |
1696 | | |
1697 | 0 | message->numRects = regionNbRects; |
1698 | |
|
1699 | 0 | for (UINT32 i = 0; i < regionNbRects; i++, regionRect++) |
1700 | 0 | { |
1701 | 0 | RFX_RECT* rfxRect = &message->rects[i]; |
1702 | 0 | UINT32 startTileX = regionRect->left / 64; |
1703 | 0 | UINT32 endTileX = (regionRect->right - 1) / 64; |
1704 | 0 | UINT32 startTileY = regionRect->top / 64; |
1705 | 0 | UINT32 endTileY = (regionRect->bottom - 1) / 64; |
1706 | 0 | rfxRect->x = regionRect->left; |
1707 | 0 | rfxRect->y = regionRect->top; |
1708 | 0 | rfxRect->width = (regionRect->right - regionRect->left); |
1709 | 0 | rfxRect->height = (regionRect->bottom - regionRect->top); |
1710 | |
|
1711 | 0 | for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY; |
1712 | 0 | yIdx++, gridRelY += 64) |
1713 | 0 | { |
1714 | 0 | UINT32 tileHeight = 64; |
1715 | |
|
1716 | 0 | if ((yIdx == endTileY) && (gridRelY + 64 > height)) |
1717 | 0 | tileHeight = height - gridRelY; |
1718 | |
|
1719 | 0 | currentTileRect.top = gridRelY; |
1720 | 0 | currentTileRect.bottom = gridRelY + tileHeight; |
1721 | |
|
1722 | 0 | for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX; |
1723 | 0 | xIdx++, gridRelX += 64) |
1724 | 0 | { |
1725 | 0 | union |
1726 | 0 | { |
1727 | 0 | const BYTE* cpv; |
1728 | 0 | BYTE* pv; |
1729 | 0 | } cnv; |
1730 | 0 | int tileWidth = 64; |
1731 | |
|
1732 | 0 | if ((xIdx == endTileX) && (gridRelX + 64 > width)) |
1733 | 0 | tileWidth = width - gridRelX; |
1734 | |
|
1735 | 0 | currentTileRect.left = gridRelX; |
1736 | 0 | currentTileRect.right = gridRelX + tileWidth; |
1737 | | |
1738 | | /* checks if this tile is already treated */ |
1739 | 0 | if (region16_intersects_rect(&tilesRegion, ¤tTileRect)) |
1740 | 0 | continue; |
1741 | | |
1742 | 0 | RFX_TILE* tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool); |
1743 | 0 | if (!tile) |
1744 | 0 | goto skip_encoding_loop; |
1745 | | |
1746 | 0 | tile->xIdx = xIdx; |
1747 | 0 | tile->yIdx = yIdx; |
1748 | 0 | tile->x = gridRelX; |
1749 | 0 | tile->y = gridRelY; |
1750 | 0 | tile->scanline = scanline; |
1751 | 0 | tile->width = tileWidth; |
1752 | 0 | tile->height = tileHeight; |
1753 | 0 | const UINT32 ax = gridRelX; |
1754 | 0 | const UINT32 ay = gridRelY; |
1755 | |
|
1756 | 0 | if (tile->data && tile->allocated) |
1757 | 0 | { |
1758 | 0 | winpr_aligned_free(tile->data); |
1759 | 0 | tile->allocated = FALSE; |
1760 | 0 | } |
1761 | | |
1762 | | /* Cast away const */ |
1763 | 0 | cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)]; |
1764 | 0 | tile->data = cnv.pv; |
1765 | 0 | tile->quantIdxY = context->quantIdxY; |
1766 | 0 | tile->quantIdxCb = context->quantIdxCb; |
1767 | 0 | tile->quantIdxCr = context->quantIdxCr; |
1768 | 0 | tile->YLen = tile->CbLen = tile->CrLen = 0; |
1769 | |
|
1770 | 0 | if (!(tile->YCbCrData = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1))) |
1771 | 0 | goto skip_encoding_loop; |
1772 | | |
1773 | 0 | tile->YData = (BYTE*)&(tile->YCbCrData[((8192 + 32) * 0) + 16]); |
1774 | 0 | tile->CbData = (BYTE*)&(tile->YCbCrData[((8192 + 32) * 1) + 16]); |
1775 | 0 | tile->CrData = (BYTE*)&(tile->YCbCrData[((8192 + 32) * 2) + 16]); |
1776 | |
|
1777 | 0 | if (!rfx_ensure_tiles(message, 1)) |
1778 | 0 | goto skip_encoding_loop; |
1779 | 0 | message->tiles[message->numTiles++] = tile; |
1780 | |
|
1781 | 0 | if (context->priv->UseThreads) |
1782 | 0 | { |
1783 | 0 | workParam->context = context; |
1784 | 0 | workParam->tile = tile; |
1785 | |
|
1786 | 0 | if (!(*workObject = CreateThreadpoolWork(rfx_compose_message_tile_work_callback, |
1787 | 0 | (void*)workParam, |
1788 | 0 | &context->priv->ThreadPoolEnv))) |
1789 | 0 | { |
1790 | 0 | goto skip_encoding_loop; |
1791 | 0 | } |
1792 | | |
1793 | 0 | SubmitThreadpoolWork(*workObject); |
1794 | 0 | workObject++; |
1795 | 0 | workParam++; |
1796 | 0 | } |
1797 | 0 | else |
1798 | 0 | { |
1799 | 0 | rfx_encode_rgb(context, tile); |
1800 | 0 | } |
1801 | | |
1802 | 0 | if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect)) |
1803 | 0 | goto skip_encoding_loop; |
1804 | 0 | } /* xIdx */ |
1805 | 0 | } /* yIdx */ |
1806 | 0 | } /* rects */ |
1807 | | |
1808 | 0 | success = TRUE; |
1809 | 0 | skip_encoding_loop: |
1810 | | |
1811 | | /* when using threads ensure all computations are done */ |
1812 | 0 | if (success) |
1813 | 0 | { |
1814 | 0 | message->tilesDataSize = 0; |
1815 | 0 | workObject = context->priv->workObjects; |
1816 | |
|
1817 | 0 | for (UINT32 i = 0; i < message->numTiles; i++) |
1818 | 0 | { |
1819 | 0 | if (context->priv->UseThreads) |
1820 | 0 | { |
1821 | 0 | if (*workObject) |
1822 | 0 | { |
1823 | 0 | WaitForThreadpoolWorkCallbacks(*workObject, FALSE); |
1824 | 0 | CloseThreadpoolWork(*workObject); |
1825 | 0 | } |
1826 | |
|
1827 | 0 | workObject++; |
1828 | 0 | } |
1829 | |
|
1830 | 0 | const RFX_TILE* tile = message->tiles[i]; |
1831 | 0 | message->tilesDataSize += rfx_tile_length(tile); |
1832 | 0 | } |
1833 | |
|
1834 | 0 | region16_uninit(&tilesRegion); |
1835 | 0 | region16_uninit(&rectsRegion); |
1836 | |
|
1837 | 0 | return message; |
1838 | 0 | } |
1839 | | |
1840 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "failed"); |
1841 | |
|
1842 | 0 | rfx_message_free(context, message); |
1843 | 0 | return NULL; |
1844 | 0 | } |
1845 | | |
1846 | | static BOOL rfx_clone_rects(RFX_MESSAGE* dst, const RFX_MESSAGE* src) |
1847 | 0 | { |
1848 | 0 | WINPR_ASSERT(dst); |
1849 | 0 | WINPR_ASSERT(src); |
1850 | | |
1851 | 0 | WINPR_ASSERT(dst->rects == NULL); |
1852 | 0 | WINPR_ASSERT(dst->numRects == 0); |
1853 | | |
1854 | 0 | if (src->numRects == 0) |
1855 | 0 | return TRUE; |
1856 | | |
1857 | 0 | dst->rects = winpr_aligned_calloc(src->numRects, sizeof(RECTANGLE_16), 32); |
1858 | 0 | if (!dst->rects) |
1859 | 0 | return FALSE; |
1860 | 0 | dst->numRects = src->numRects; |
1861 | 0 | for (size_t x = 0; x < src->numRects; x++) |
1862 | 0 | { |
1863 | 0 | dst->rects[x] = src->rects[x]; |
1864 | 0 | } |
1865 | 0 | return TRUE; |
1866 | 0 | } |
1867 | | |
1868 | | static BOOL rfx_clone_quants(RFX_MESSAGE* dst, const RFX_MESSAGE* src) |
1869 | 0 | { |
1870 | 0 | WINPR_ASSERT(dst); |
1871 | 0 | WINPR_ASSERT(src); |
1872 | | |
1873 | 0 | WINPR_ASSERT(dst->quantVals == NULL); |
1874 | 0 | WINPR_ASSERT(dst->numQuant == 0); |
1875 | | |
1876 | 0 | if (src->numQuant == 0) |
1877 | 0 | return TRUE; |
1878 | | |
1879 | | /* quantVals are part of context */ |
1880 | 0 | dst->quantVals = src->quantVals; |
1881 | 0 | dst->numQuant = src->numQuant; |
1882 | |
|
1883 | 0 | return TRUE; |
1884 | 0 | } |
1885 | | |
1886 | | static RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* context, RFX_MESSAGE* message, |
1887 | | size_t* numMessages, size_t maxDataSize) |
1888 | 0 | { |
1889 | 0 | WINPR_ASSERT(context); |
1890 | 0 | WINPR_ASSERT(message); |
1891 | 0 | WINPR_ASSERT(numMessages); |
1892 | | |
1893 | 0 | maxDataSize -= 1024; /* reserve enough space for headers */ |
1894 | 0 | *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull; |
1895 | |
|
1896 | 0 | RFX_MESSAGE* messages = |
1897 | 0 | (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages), sizeof(RFX_MESSAGE), 32); |
1898 | 0 | if (!messages) |
1899 | 0 | return NULL; |
1900 | | |
1901 | 0 | size_t j = 0; |
1902 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
1903 | 0 | { |
1904 | 0 | RFX_TILE* tile = message->tiles[i]; |
1905 | 0 | RFX_MESSAGE* msg = &messages[j]; |
1906 | |
|
1907 | 0 | WINPR_ASSERT(tile); |
1908 | 0 | WINPR_ASSERT(msg); |
1909 | | |
1910 | 0 | const size_t tileDataSize = rfx_tile_length(tile); |
1911 | |
|
1912 | 0 | if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize)) |
1913 | 0 | j++; |
1914 | |
|
1915 | 0 | if (msg->numTiles == 0) |
1916 | 0 | { |
1917 | 0 | msg->frameIdx = message->frameIdx + j; |
1918 | 0 | if (!rfx_clone_quants(msg, message)) |
1919 | 0 | goto free_messages; |
1920 | 0 | if (!rfx_clone_rects(msg, message)) |
1921 | 0 | goto free_messages; |
1922 | 0 | msg->freeArray = TRUE; |
1923 | 0 | if (!rfx_allocate_tiles(msg, message->numTiles, TRUE)) |
1924 | 0 | goto free_messages; |
1925 | 0 | } |
1926 | | |
1927 | 0 | msg->tilesDataSize += tileDataSize; |
1928 | |
|
1929 | 0 | WINPR_ASSERT(msg->numTiles < msg->allocatedTiles); |
1930 | 0 | msg->tiles[msg->numTiles++] = message->tiles[i]; |
1931 | 0 | message->tiles[i] = NULL; |
1932 | 0 | } |
1933 | | |
1934 | 0 | *numMessages = j + 1; |
1935 | 0 | context->frameIdx += j; |
1936 | 0 | message->numTiles = 0; |
1937 | 0 | return messages; |
1938 | 0 | free_messages: |
1939 | |
|
1940 | 0 | for (size_t i = 0; i < j; i++) |
1941 | 0 | rfx_allocate_tiles(&messages[i], 0, FALSE); |
1942 | |
|
1943 | 0 | winpr_aligned_free(messages); |
1944 | 0 | return NULL; |
1945 | 0 | } |
1946 | | |
1947 | | const RFX_MESSAGE* rfx_message_list_get(const RFX_MESSAGE_LIST* messages, size_t idx) |
1948 | 0 | { |
1949 | 0 | WINPR_ASSERT(messages); |
1950 | 0 | if (idx >= messages->count) |
1951 | 0 | return NULL; |
1952 | 0 | WINPR_ASSERT(messages->list); |
1953 | 0 | return &messages->list[idx]; |
1954 | 0 | } |
1955 | | |
1956 | | void rfx_message_list_free(RFX_MESSAGE_LIST* messages) |
1957 | 0 | { |
1958 | 0 | if (!messages) |
1959 | 0 | return; |
1960 | 0 | for (size_t x = 0; x < messages->count; x++) |
1961 | 0 | rfx_message_free(messages->context, &messages->list[x]); |
1962 | 0 | free(messages); |
1963 | 0 | } |
1964 | | |
1965 | | static RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* context, RFX_MESSAGE* messages, |
1966 | | size_t count) |
1967 | 0 | { |
1968 | 0 | WINPR_ASSERT(context); |
1969 | 0 | RFX_MESSAGE_LIST* msg = calloc(1, sizeof(RFX_MESSAGE_LIST)); |
1970 | 0 | WINPR_ASSERT(msg); |
1971 | | |
1972 | 0 | msg->context = context; |
1973 | 0 | msg->count = count; |
1974 | 0 | msg->list = messages; |
1975 | 0 | return msg; |
1976 | 0 | } |
1977 | | |
1978 | | RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* context, const RFX_RECT* rects, size_t numRects, |
1979 | | const BYTE* data, UINT32 width, UINT32 height, |
1980 | | UINT32 scanline, size_t* numMessages, size_t maxDataSize) |
1981 | 0 | { |
1982 | 0 | WINPR_ASSERT(context); |
1983 | 0 | WINPR_ASSERT(numMessages); |
1984 | | |
1985 | 0 | RFX_MESSAGE* message = |
1986 | 0 | rfx_encode_message(context, rects, numRects, data, width, height, scanline); |
1987 | 0 | if (!message) |
1988 | 0 | return NULL; |
1989 | | |
1990 | 0 | RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize); |
1991 | 0 | rfx_message_free(context, message); |
1992 | 0 | if (!list) |
1993 | 0 | return NULL; |
1994 | | |
1995 | 0 | return rfx_message_list_new(context, list, *numMessages); |
1996 | 0 | } |
1997 | | |
1998 | | static BOOL rfx_write_message_tileset(RFX_CONTEXT* context, wStream* s, const RFX_MESSAGE* message) |
1999 | 0 | { |
2000 | 0 | WINPR_ASSERT(context); |
2001 | 0 | WINPR_ASSERT(message); |
2002 | | |
2003 | 0 | const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize; |
2004 | |
|
2005 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2006 | 0 | return FALSE; |
2007 | | |
2008 | 0 | Stream_Write_UINT16(s, WBT_EXTENSION); /* CodecChannelT.blockType (2 bytes) */ |
2009 | 0 | Stream_Write_UINT32(s, blockLen); /* set CodecChannelT.blockLen (4 bytes) */ |
2010 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
2011 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */ |
2012 | 0 | Stream_Write_UINT16(s, CBT_TILESET); /* subtype (2 bytes) */ |
2013 | 0 | Stream_Write_UINT16(s, 0); /* idx (2 bytes) */ |
2014 | 0 | Stream_Write_UINT16(s, context->properties); /* properties (2 bytes) */ |
2015 | 0 | Stream_Write_UINT8(s, message->numQuant); /* numQuant (1 byte) */ |
2016 | 0 | Stream_Write_UINT8(s, 0x40); /* tileSize (1 byte) */ |
2017 | 0 | Stream_Write_UINT16(s, message->numTiles); /* numTiles (2 bytes) */ |
2018 | 0 | Stream_Write_UINT32(s, message->tilesDataSize); /* tilesDataSize (4 bytes) */ |
2019 | |
|
2020 | 0 | UINT32* quantVals = message->quantVals; |
2021 | 0 | for (size_t i = 0; i < message->numQuant * 5ul; i++) |
2022 | 0 | { |
2023 | 0 | WINPR_ASSERT(quantVals); |
2024 | 0 | Stream_Write_UINT8(s, quantVals[0] + (quantVals[1] << 4)); |
2025 | 0 | quantVals += 2; |
2026 | 0 | } |
2027 | | |
2028 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
2029 | 0 | { |
2030 | 0 | RFX_TILE* tile = message->tiles[i]; |
2031 | 0 | if (!tile) |
2032 | 0 | return FALSE; |
2033 | | |
2034 | 0 | if (!rfx_write_tile(s, tile)) |
2035 | 0 | return FALSE; |
2036 | 0 | } |
2037 | | |
2038 | | #ifdef WITH_DEBUG_RFX |
2039 | | WLog_Print(context->priv->log, WLOG_DEBUG, |
2040 | | "numQuant: %" PRIu16 " numTiles: %" PRIu16 " tilesDataSize: %" PRIu32 "", |
2041 | | message->numQuant, message->numTiles, message->tilesDataSize); |
2042 | | #endif |
2043 | 0 | return TRUE; |
2044 | 0 | } |
2045 | | |
2046 | | static BOOL rfx_write_message_frame_begin(RFX_CONTEXT* context, wStream* s, |
2047 | | const RFX_MESSAGE* message) |
2048 | 0 | { |
2049 | 0 | WINPR_ASSERT(context); |
2050 | 0 | WINPR_ASSERT(message); |
2051 | | |
2052 | 0 | if (!Stream_EnsureRemainingCapacity(s, 14)) |
2053 | 0 | return FALSE; |
2054 | | |
2055 | 0 | Stream_Write_UINT16(s, WBT_FRAME_BEGIN); /* CodecChannelT.blockType */ |
2056 | 0 | Stream_Write_UINT32(s, 14); /* CodecChannelT.blockLen */ |
2057 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */ |
2058 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */ |
2059 | 0 | Stream_Write_UINT32(s, message->frameIdx); /* frameIdx */ |
2060 | 0 | Stream_Write_UINT16(s, 1); /* numRegions */ |
2061 | 0 | return TRUE; |
2062 | 0 | } |
2063 | | |
2064 | | static BOOL rfx_write_message_region(RFX_CONTEXT* context, wStream* s, const RFX_MESSAGE* message) |
2065 | 0 | { |
2066 | 0 | WINPR_ASSERT(context); |
2067 | 0 | WINPR_ASSERT(message); |
2068 | | |
2069 | 0 | const size_t blockLen = 15 + (message->numRects * 8); |
2070 | |
|
2071 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2072 | 0 | return FALSE; |
2073 | | |
2074 | 0 | Stream_Write_UINT16(s, WBT_REGION); /* CodecChannelT.blockType (2 bytes) */ |
2075 | 0 | Stream_Write_UINT32(s, blockLen); /* set CodecChannelT.blockLen (4 bytes) */ |
2076 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
2077 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */ |
2078 | 0 | Stream_Write_UINT8(s, 1); /* regionFlags (1 byte) */ |
2079 | 0 | Stream_Write_UINT16(s, message->numRects); /* numRects (2 bytes) */ |
2080 | |
|
2081 | 0 | for (size_t i = 0; i < message->numRects; i++) |
2082 | 0 | { |
2083 | 0 | const RFX_RECT* rect = rfx_message_get_rect_const(message, i); |
2084 | 0 | WINPR_ASSERT(rect); |
2085 | | |
2086 | | /* Clipping rectangles are relative to destLeft, destTop */ |
2087 | 0 | Stream_Write_UINT16(s, rect->x); /* x (2 bytes) */ |
2088 | 0 | Stream_Write_UINT16(s, rect->y); /* y (2 bytes) */ |
2089 | 0 | Stream_Write_UINT16(s, rect->width); /* width (2 bytes) */ |
2090 | 0 | Stream_Write_UINT16(s, rect->height); /* height (2 bytes) */ |
2091 | 0 | } |
2092 | | |
2093 | 0 | Stream_Write_UINT16(s, CBT_REGION); /* regionType (2 bytes) */ |
2094 | 0 | Stream_Write_UINT16(s, 1); /* numTilesets (2 bytes) */ |
2095 | 0 | return TRUE; |
2096 | 0 | } |
2097 | | |
2098 | | static BOOL rfx_write_message_frame_end(RFX_CONTEXT* context, wStream* s, |
2099 | | const RFX_MESSAGE* message) |
2100 | 0 | { |
2101 | 0 | WINPR_ASSERT(context); |
2102 | 0 | WINPR_ASSERT(message); |
2103 | | |
2104 | 0 | if (!Stream_EnsureRemainingCapacity(s, 8)) |
2105 | 0 | return FALSE; |
2106 | | |
2107 | 0 | Stream_Write_UINT16(s, WBT_FRAME_END); /* CodecChannelT.blockType */ |
2108 | 0 | Stream_Write_UINT32(s, 8); /* CodecChannelT.blockLen */ |
2109 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */ |
2110 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */ |
2111 | 0 | return TRUE; |
2112 | 0 | } |
2113 | | |
2114 | | BOOL rfx_write_message(RFX_CONTEXT* context, wStream* s, const RFX_MESSAGE* message) |
2115 | 0 | { |
2116 | 0 | WINPR_ASSERT(context); |
2117 | 0 | WINPR_ASSERT(message); |
2118 | | |
2119 | 0 | if (context->state == RFX_STATE_SEND_HEADERS) |
2120 | 0 | { |
2121 | 0 | if (!rfx_compose_message_header(context, s)) |
2122 | 0 | return FALSE; |
2123 | | |
2124 | 0 | context->state = RFX_STATE_SEND_FRAME_DATA; |
2125 | 0 | } |
2126 | | |
2127 | 0 | if (!rfx_write_message_frame_begin(context, s, message) || |
2128 | 0 | !rfx_write_message_region(context, s, message) || |
2129 | 0 | !rfx_write_message_tileset(context, s, message) || |
2130 | 0 | !rfx_write_message_frame_end(context, s, message)) |
2131 | 0 | { |
2132 | 0 | return FALSE; |
2133 | 0 | } |
2134 | | |
2135 | 0 | return TRUE; |
2136 | 0 | } |
2137 | | |
2138 | | BOOL rfx_compose_message(RFX_CONTEXT* context, wStream* s, const RFX_RECT* rects, size_t numRects, |
2139 | | const BYTE* data, UINT32 width, UINT32 height, UINT32 scanline) |
2140 | 0 | { |
2141 | 0 | WINPR_ASSERT(context); |
2142 | 0 | RFX_MESSAGE* message = |
2143 | 0 | rfx_encode_message(context, rects, numRects, data, width, height, scanline); |
2144 | 0 | if (!message) |
2145 | 0 | return FALSE; |
2146 | | |
2147 | 0 | const BOOL ret = rfx_write_message(context, s, message); |
2148 | 0 | rfx_message_free(context, message); |
2149 | 0 | return ret; |
2150 | 0 | } |
2151 | | |
2152 | | BOOL rfx_context_set_mode(RFX_CONTEXT* context, RLGR_MODE mode) |
2153 | 0 | { |
2154 | 0 | WINPR_ASSERT(context); |
2155 | 0 | context->mode = mode; |
2156 | 0 | return TRUE; |
2157 | 0 | } |
2158 | | |
2159 | | RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* context) |
2160 | 0 | { |
2161 | 0 | WINPR_ASSERT(context); |
2162 | 0 | return context->mode; |
2163 | 0 | } |
2164 | | |
2165 | | UINT32 rfx_context_get_frame_idx(const RFX_CONTEXT* context) |
2166 | 0 | { |
2167 | 0 | WINPR_ASSERT(context); |
2168 | 0 | return context->frameIdx; |
2169 | 0 | } |
2170 | | |
2171 | | UINT32 rfx_message_get_frame_idx(const RFX_MESSAGE* message) |
2172 | 0 | { |
2173 | 0 | WINPR_ASSERT(message); |
2174 | 0 | return message->frameIdx; |
2175 | 0 | } |
2176 | | |
2177 | | static INLINE BOOL rfx_write_progressive_wb_sync(RFX_CONTEXT* rfx, wStream* s) |
2178 | 0 | { |
2179 | 0 | const UINT32 blockLen = 12; |
2180 | 0 | WINPR_ASSERT(rfx); |
2181 | 0 | WINPR_ASSERT(s); |
2182 | | |
2183 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2184 | 0 | return FALSE; |
2185 | | |
2186 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC); /* blockType (2 bytes) */ |
2187 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2188 | 0 | Stream_Write_UINT32(s, 0xCACCACCA); /* magic (4 bytes) */ |
2189 | 0 | Stream_Write_UINT16(s, 0x0100); /* version (2 bytes) */ |
2190 | 0 | return TRUE; |
2191 | 0 | } |
2192 | | |
2193 | | static INLINE BOOL rfx_write_progressive_wb_context(RFX_CONTEXT* rfx, wStream* s) |
2194 | 0 | { |
2195 | 0 | const UINT32 blockLen = 10; |
2196 | 0 | WINPR_ASSERT(rfx); |
2197 | 0 | WINPR_ASSERT(s); |
2198 | | |
2199 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2200 | 0 | return FALSE; |
2201 | | |
2202 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT); /* blockType (2 bytes) */ |
2203 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2204 | 0 | Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */ |
2205 | 0 | Stream_Write_UINT16(s, 64); /* tileSize (2 bytes) */ |
2206 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2207 | 0 | return TRUE; |
2208 | 0 | } |
2209 | | |
2210 | | static INLINE BOOL rfx_write_progressive_region(RFX_CONTEXT* rfx, wStream* s, |
2211 | | const RFX_MESSAGE* msg) |
2212 | 0 | { |
2213 | | /* RFX_REGION */ |
2214 | 0 | UINT32 blockLen = 18; |
2215 | 0 | UINT32 tilesDataSize = 0; |
2216 | 0 | const size_t start = Stream_GetPosition(s); |
2217 | |
|
2218 | 0 | WINPR_ASSERT(rfx); |
2219 | 0 | WINPR_ASSERT(s); |
2220 | 0 | WINPR_ASSERT(msg); |
2221 | | |
2222 | 0 | blockLen += msg->numRects * 8; |
2223 | 0 | blockLen += msg->numQuant * 5; |
2224 | 0 | tilesDataSize = msg->numTiles * 22UL; |
2225 | 0 | for (UINT16 i = 0; i < msg->numTiles; i++) |
2226 | 0 | { |
2227 | 0 | const RFX_TILE* tile = msg->tiles[i]; |
2228 | 0 | WINPR_ASSERT(tile); |
2229 | 0 | tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen; |
2230 | 0 | } |
2231 | 0 | blockLen += tilesDataSize; |
2232 | |
|
2233 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2234 | 0 | return FALSE; |
2235 | | |
2236 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION); /* blockType (2 bytes) */ |
2237 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2238 | 0 | Stream_Write_UINT8(s, 64); /* tileSize (1 byte) */ |
2239 | 0 | Stream_Write_UINT16(s, msg->numRects); /* numRects (2 bytes) */ |
2240 | 0 | WINPR_ASSERT(msg->numQuant <= UINT8_MAX); |
2241 | 0 | Stream_Write_UINT8(s, (UINT8)msg->numQuant); /* numQuant (1 byte) */ |
2242 | 0 | Stream_Write_UINT8(s, 0); /* numProgQuant (1 byte) */ |
2243 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2244 | 0 | Stream_Write_UINT16(s, msg->numTiles); /* numTiles (2 bytes) */ |
2245 | 0 | Stream_Write_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */ |
2246 | |
|
2247 | 0 | for (UINT16 i = 0; i < msg->numRects; i++) |
2248 | 0 | { |
2249 | | /* TS_RFX_RECT */ |
2250 | 0 | const RFX_RECT* r = &msg->rects[i]; |
2251 | 0 | Stream_Write_UINT16(s, r->x); /* x (2 bytes) */ |
2252 | 0 | Stream_Write_UINT16(s, r->y); /* y (2 bytes) */ |
2253 | 0 | Stream_Write_UINT16(s, r->width); /* width (2 bytes) */ |
2254 | 0 | Stream_Write_UINT16(s, r->height); /* height (2 bytes) */ |
2255 | 0 | } |
2256 | | |
2257 | | /** |
2258 | | * Note: The RFX_COMPONENT_CODEC_QUANT structure differs from the |
2259 | | * TS_RFX_CODEC_QUANT ([MS-RDPRFX] section 2.2.2.1.5) structure with respect |
2260 | | * to the order of the bands. |
2261 | | * 0 1 2 3 4 5 6 7 8 9 |
2262 | | * RDPRFX: LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1 |
2263 | | * RDPEGFX: LL3, HL3, LH3, HH3, HL2, LH2, HH2, HL1, LH1, HH1 |
2264 | | */ |
2265 | 0 | for (UINT16 i = 0; i < msg->numQuant; i++) |
2266 | 0 | { |
2267 | 0 | const UINT32* qv = &msg->quantVals[i * 10]; |
2268 | | /* RFX_COMPONENT_CODEC_QUANT */ |
2269 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4))); /* LL3 (4-bit), HL3 (4-bit) */ |
2270 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4))); /* LH3 (4-bit), HH3 (4-bit) */ |
2271 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4))); /* HL2 (4-bit), LH2 (4-bit) */ |
2272 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4))); /* HH2 (4-bit), HL1 (4-bit) */ |
2273 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4))); /* LH1 (4-bit), HH1 (4-bit) */ |
2274 | 0 | } |
2275 | |
|
2276 | 0 | for (UINT16 i = 0; i < msg->numTiles; i++) |
2277 | 0 | { |
2278 | 0 | const RFX_TILE* tile = msg->tiles[i]; |
2279 | 0 | if (!rfx_write_progressive_tile_simple(rfx, s, tile)) |
2280 | 0 | return FALSE; |
2281 | 0 | } |
2282 | | |
2283 | 0 | const size_t end = Stream_GetPosition(s); |
2284 | 0 | const size_t used = end - start; |
2285 | 0 | return (used == blockLen); |
2286 | 0 | } |
2287 | | |
2288 | | static INLINE BOOL rfx_write_progressive_frame_begin(RFX_CONTEXT* rfx, wStream* s, |
2289 | | const RFX_MESSAGE* msg) |
2290 | 0 | { |
2291 | 0 | const UINT32 blockLen = 12; |
2292 | 0 | WINPR_ASSERT(rfx); |
2293 | 0 | WINPR_ASSERT(s); |
2294 | 0 | WINPR_ASSERT(msg); |
2295 | | |
2296 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2297 | 0 | return FALSE; |
2298 | | |
2299 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN); /* blockType (2 bytes) */ |
2300 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2301 | 0 | Stream_Write_UINT32(s, msg->frameIdx); /* frameIndex (4 bytes) */ |
2302 | 0 | Stream_Write_UINT16(s, 1); /* regionCount (2 bytes) */ |
2303 | |
|
2304 | 0 | return TRUE; |
2305 | 0 | } |
2306 | | |
2307 | | static INLINE BOOL rfx_write_progressive_frame_end(RFX_CONTEXT* rfx, wStream* s) |
2308 | 0 | { |
2309 | 0 | const UINT32 blockLen = 6; |
2310 | 0 | WINPR_ASSERT(rfx); |
2311 | 0 | WINPR_ASSERT(s); |
2312 | | |
2313 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2314 | 0 | return FALSE; |
2315 | | |
2316 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END); /* blockType (2 bytes) */ |
2317 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2318 | |
|
2319 | 0 | return TRUE; |
2320 | 0 | } |
2321 | | |
2322 | | static INLINE BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* rfx, wStream* s, |
2323 | | const RFX_TILE* tile) |
2324 | 0 | { |
2325 | 0 | UINT32 blockLen = 0; |
2326 | 0 | WINPR_ASSERT(rfx); |
2327 | 0 | WINPR_ASSERT(s); |
2328 | 0 | WINPR_ASSERT(tile); |
2329 | | |
2330 | 0 | blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen; |
2331 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2332 | 0 | return FALSE; |
2333 | | |
2334 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE); /* blockType (2 bytes) */ |
2335 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2336 | 0 | Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */ |
2337 | 0 | Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
2338 | 0 | Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
2339 | 0 | Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */ |
2340 | 0 | Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */ |
2341 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2342 | 0 | Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */ |
2343 | 0 | Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */ |
2344 | 0 | Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */ |
2345 | 0 | Stream_Write_UINT16(s, 0); /* tailLen (2 bytes) */ |
2346 | 0 | Stream_Write(s, tile->YData, tile->YLen); /* YData */ |
2347 | 0 | Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */ |
2348 | 0 | Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */ |
2349 | |
|
2350 | 0 | return TRUE; |
2351 | 0 | } |
2352 | | |
2353 | | const char* rfx_get_progressive_block_type_string(UINT16 blockType) |
2354 | 43 | { |
2355 | 43 | switch (blockType) |
2356 | 43 | { |
2357 | 1 | case PROGRESSIVE_WBT_SYNC: |
2358 | 1 | return "PROGRESSIVE_WBT_SYNC"; |
2359 | | |
2360 | 0 | case PROGRESSIVE_WBT_FRAME_BEGIN: |
2361 | 0 | return "PROGRESSIVE_WBT_FRAME_BEGIN"; |
2362 | | |
2363 | 1 | case PROGRESSIVE_WBT_FRAME_END: |
2364 | 1 | return "PROGRESSIVE_WBT_FRAME_END"; |
2365 | | |
2366 | 1 | case PROGRESSIVE_WBT_CONTEXT: |
2367 | 1 | return "PROGRESSIVE_WBT_CONTEXT"; |
2368 | | |
2369 | 0 | case PROGRESSIVE_WBT_REGION: |
2370 | 0 | return "PROGRESSIVE_WBT_REGION"; |
2371 | | |
2372 | 0 | case PROGRESSIVE_WBT_TILE_SIMPLE: |
2373 | 0 | return "PROGRESSIVE_WBT_TILE_SIMPLE"; |
2374 | | |
2375 | 0 | case PROGRESSIVE_WBT_TILE_FIRST: |
2376 | 0 | return "PROGRESSIVE_WBT_TILE_FIRST"; |
2377 | | |
2378 | 0 | case PROGRESSIVE_WBT_TILE_UPGRADE: |
2379 | 0 | return "PROGRESSIVE_WBT_TILE_UPGRADE"; |
2380 | | |
2381 | 40 | default: |
2382 | 40 | return "PROGRESSIVE_WBT_UNKNOWN"; |
2383 | 43 | } |
2384 | 43 | } |
2385 | | |
2386 | | BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* context, wStream* s, const RFX_MESSAGE* msg) |
2387 | 0 | { |
2388 | 0 | WINPR_ASSERT(s); |
2389 | 0 | WINPR_ASSERT(msg); |
2390 | 0 | WINPR_ASSERT(context); |
2391 | | |
2392 | 0 | if (context->mode != RLGR1) |
2393 | 0 | { |
2394 | 0 | WLog_ERR(TAG, "error, RLGR1 mode is required!"); |
2395 | 0 | return FALSE; |
2396 | 0 | } |
2397 | | |
2398 | 0 | if (!rfx_write_progressive_wb_sync(context, s)) |
2399 | 0 | return FALSE; |
2400 | | |
2401 | 0 | if (!rfx_write_progressive_wb_context(context, s)) |
2402 | 0 | return FALSE; |
2403 | | |
2404 | 0 | if (!rfx_write_progressive_frame_begin(context, s, msg)) |
2405 | 0 | return FALSE; |
2406 | | |
2407 | 0 | if (!rfx_write_progressive_region(context, s, msg)) |
2408 | 0 | return FALSE; |
2409 | | |
2410 | 0 | if (!rfx_write_progressive_frame_end(context, s)) |
2411 | 0 | return FALSE; |
2412 | | |
2413 | 0 | return TRUE; |
2414 | 0 | } |