FreeRDP
Loading...
Searching...
No Matches
h264.c
1
20
21#include <freerdp/config.h>
22
23#include <winpr/crt.h>
24#include <winpr/print.h>
25#include <winpr/library.h>
26#include <winpr/bitstream.h>
27#include <winpr/synch.h>
28
29#include <freerdp/primitives.h>
30#include <freerdp/codec/h264.h>
31#include <freerdp/codec/yuv.h>
32#include <freerdp/log.h>
33#include <freerdp/codec/region.h>
34#include <freerdp/codecs.h>
35
36#include "h264.h"
37
38#define TAG FREERDP_TAG("codec")
39
40WINPR_ATTR_NODISCARD
41static BOOL yuv_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
42{
43 BOOL isNull = FALSE;
44 UINT32 pheight = height;
45
46 if (!h264)
47 return FALSE;
48
49 if (stride == 0)
50 {
51 const UINT32 pad = width % 16;
52 stride = width;
53 if (pad > 0)
54 width += 16 - pad;
55 }
56
57 /* Add padding lines. Allows relaxing bounds checks in decoder functions */
58 stride += 32 - stride % 16;
59 pheight += 32 - pheight % 16;
60
61 const size_t nPlanes = h264->hwAccel ? 2 : 3;
62
63 for (size_t x = 0; x < nPlanes; x++)
64 {
65 if (!h264->pYUVData[x] || !h264->pOldYUVData[x])
66 isNull = TRUE;
67 }
68
69 if (pheight == 0)
70 return FALSE;
71 if (stride == 0)
72 return FALSE;
73
74 if (isNull || (width != h264->width) || (height != h264->height) ||
75 (stride != h264->iStride[0]))
76 {
77 UINT32 iStride[3] = WINPR_C_ARRAY_INIT;
78 if (h264->hwAccel) /* NV12 */
79 {
80 iStride[0] = stride;
81 iStride[1] = stride;
82 iStride[2] = 0;
83 }
84 else /* I420 */
85 {
86 iStride[0] = stride;
87 iStride[1] = (stride + 1) / 2;
88 iStride[2] = (stride + 1) / 2;
89 }
90
91 for (size_t x = 0; x < nPlanes; x++)
92 {
93 BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], iStride[x], pheight, 16);
94 BYTE* tmp2 = winpr_aligned_recalloc(h264->pOldYUVData[x], iStride[x], pheight, 16);
95 if (tmp1)
96 h264->pYUVData[x] = tmp1;
97 if (tmp2)
98 h264->pOldYUVData[x] = tmp2;
99 if (!tmp1 || !tmp2)
100 return FALSE;
101 }
102 /* Only update the strides after the buffers were successfully allocated */
103 memcpy(h264->iStride, iStride, sizeof(h264->iStride));
104 h264->width = width;
105 h264->height = height;
106 h264->firstLumaFrameDone = FALSE;
107 }
108
109 return TRUE;
110}
111
112BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
113{
114 return yuv_ensure_buffer(h264, stride, width, height);
115}
116
117WINPR_ATTR_NODISCARD
118static BOOL isRectValid(wLog* log, size_t pos, UINT32 width, UINT32 height,
119 const RECTANGLE_16* rect)
120{
121 WINPR_ASSERT(rect);
122 if ((rect->left > width) || (rect->right > width) || (rect->left >= rect->right) ||
123 (rect->top > height) || (rect->bottom > height) || (rect->top >= rect->bottom))
124 {
125 char buffer[64] = WINPR_C_ARRAY_INIT;
126 WLog_Print(log, WLOG_WARN,
127 "Rectangle %" PRIuz " %s outside of bounding frame %" PRIu32 "x%" PRIu32, pos,
128 rectangle_to_string(rect, buffer, sizeof(buffer)), width, height);
129 return FALSE;
130 }
131
132 return TRUE;
133}
134
135WINPR_ATTR_NODISCARD
136static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height, const RECTANGLE_16* rects,
137 UINT32 count)
138{
139 WINPR_ASSERT(rects || (count == 0));
140 for (size_t x = 0; x < count; x++)
141 {
142 const RECTANGLE_16* rect = &rects[x];
143 if (!isRectValid(log, x, width, height, rect))
144 return FALSE;
145 }
146 return TRUE;
147}
148
149WINPR_ATTR_NODISCARD
150static int log_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize,
151 const RECTANGLE_16* rects, UINT32 nrRects)
152{
153 WINPR_ASSERT(h264);
154 WINPR_ASSERT(h264->subsystem);
155 WINPR_ASSERT(h264->subsystem->Decompress);
156
157 const int status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
158 if (status < 0)
159 {
160 WLog_Print(h264->log, WLOG_WARN, "H264 decompress failed with %d", status);
161 return status;
162 }
163
164 /* no picture decoded, nothing to validate */
165 if (status == 0)
166 return 0;
167
168 /* some server implementations (krdc) use H264 frames smaller than the surface sizes,
169 * validate the regions against this size as well */
170 if (!areRectsValid(h264->log, h264->YUVWidth, h264->YUVHeight, rects, nrRects))
171 return -1015;
172 if (!areRectsValid(h264->log, h264->width, h264->height, rects, nrRects))
173 return -1016;
174 return status;
175}
176
177/* A full resolution 4:4:4 picture (HEVC 4:4:4): every pixel has its own chroma sample, convert
178 * only the updated regions. The colour is BT.709 full range as for 4:2:0. */
179WINPR_ATTR_NODISCARD
180static BOOL yuv444_planes_decode(H264_CONTEXT* h264, DWORD DstFormat, BYTE* pDstData,
181 UINT32 nDstStep, const RECTANGLE_16* regionRects,
182 UINT32 numRegionRects)
183{
184 WINPR_ASSERT(h264);
185
186 primitives_t* prims = primitives_get();
187 if (!prims || !prims->I444ToRGB_8u)
188 return FALSE;
189
190 const size_t bpp = FreeRDPGetBytesPerPixel(DstFormat);
191 for (UINT32 x = 0; x < numRegionRects; x++)
192 {
193 const RECTANGLE_16* rect = &regionRects[x];
194 const prim_size_t roi = { .width = rect->right - rect->left,
195 .height = rect->bottom - rect->top };
196 const BYTE* pSrc[3] = WINPR_C_ARRAY_INIT;
197 for (size_t p = 0; p < 3; p++)
198 pSrc[p] = &h264->pYUVData[p][1ull * rect->top * h264->iStride[p] + rect->left];
199 BYTE* pDst = &pDstData[1ull * rect->top * nDstStep + rect->left * bpp];
200
201 if (prims->I444ToRGB_8u(pSrc, h264->iStride, pDst, nDstStep, DstFormat, &roi) !=
202 PRIMITIVES_SUCCESS)
203 return FALSE;
204 }
205 return TRUE;
206}
207
208INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
209 DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
210 const RECTANGLE_16* regionRects, UINT32 numRegionRects)
211{
212 return h26X_decompress(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
213 nDstHeight, regionRects, numRegionRects);
214}
215
216INT32 h26X_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
217 DWORD DstFormat, UINT32 nDstStep, WINPR_ATTR_UNUSED UINT32 nDstWidth,
218 WINPR_ATTR_UNUSED UINT32 nDstHeight, const RECTANGLE_16* regionRects,
219 UINT32 numRegionRects)
220{
221 int status = 0;
222 const BYTE* pYUVData[3];
223
224 if (!h264 || h264->Compressor)
225 return -1001;
226
227 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects))
228 return -1013;
229
230 status = log_decompress(h264, pSrcData, SrcSize, regionRects, numRegionRects);
231
232 if (status == 0)
233 return 1;
234
235 if (status < 0)
236 return status;
237
238 if (h264->yuv444)
239 {
240 if (!yuv444_planes_decode(h264, DstFormat, pDstData, nDstStep, regionRects, numRegionRects))
241 return -1002;
242 return 1;
243 }
244
245 pYUVData[0] = h264->pYUVData[0];
246 pYUVData[1] = h264->pYUVData[1];
247 pYUVData[2] = h264->pYUVData[2];
248 if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->YUVHeight, DstFormat,
249 pDstData, nDstStep, regionRects, numRegionRects))
250 return -1002;
251
252 return 1;
253}
254
255WINPR_ATTR_NODISCARD
256BOOL allocate_h264_metablock(UINT32 QP, RECTANGLE_16* rectangles, RDPGFX_H264_METABLOCK* meta,
257 size_t count)
258{
259 /* [MS-RDPEGFX] 2.2.4.4.2 RDPGFX_AVC420_QUANT_QUALITY */
260 if (!meta || (QP > UINT8_MAX))
261 {
262 free(rectangles);
263 return FALSE;
264 }
265
266 meta->regionRects = rectangles;
267 if (count == 0)
268 return TRUE;
269
270 if (count > UINT32_MAX)
271 return FALSE;
272
273 meta->quantQualityVals = calloc(count, sizeof(RDPGFX_H264_QUANT_QUALITY));
274
275 if (!meta->quantQualityVals || !meta->regionRects)
276 return FALSE;
277 meta->numRegionRects = (UINT32)count;
278 for (size_t x = 0; x < count; x++)
279 {
280 RDPGFX_H264_QUANT_QUALITY* cur = &meta->quantQualityVals[x];
281 cur->qp = (UINT8)QP;
282
283 /* qpVal bit 6 and 7 are flags, so mask them out here.
284 * qualityVal is [0-100] so 100 - qpVal [0-64] is always in range */
285 cur->qualityVal = 100 - (QP & 0x3F);
286 }
287 return TRUE;
288}
289
290WINPR_ATTR_NODISCARD
291static inline BOOL diff_tile(const RECTANGLE_16* regionRect, BYTE* pYUVData[3],
292 BYTE* pOldYUVData[3], UINT32 const iStride[3])
293{
294 size_t size = 0;
295
296 if (!regionRect || !pYUVData || !pOldYUVData || !iStride)
297 return FALSE;
298 size = regionRect->right - regionRect->left;
299 if (regionRect->right > iStride[0])
300 return FALSE;
301 if (regionRect->right / 2u > iStride[1])
302 return FALSE;
303 if (regionRect->right / 2u > iStride[2])
304 return FALSE;
305
306 for (size_t y = regionRect->top; y < regionRect->bottom; y++)
307 {
308 const BYTE* cur0 = &pYUVData[0][y * iStride[0]];
309 const BYTE* old0 = &pOldYUVData[0][y * iStride[0]];
310
311 if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0)
312 return TRUE;
313 }
314
315 /* the U and V planes have half the rows: the tile's chroma is on rows top / 2 to
316 * (bottom + 1) / 2. Row y of them is screen row 2 * y, so reading row y here compared
317 * another part of the screen (and past the planes' content below half height). */
318 const size_t uvLeft = regionRect->left / 2u;
319 const size_t uvSize = (regionRect->right + 1u) / 2u - uvLeft;
320 for (size_t y = regionRect->top / 2u; y < (regionRect->bottom + 1u) / 2u; y++)
321 {
322 const BYTE* cur1 = &pYUVData[1][y * iStride[1]];
323 const BYTE* cur2 = &pYUVData[2][y * iStride[2]];
324 const BYTE* old1 = &pOldYUVData[1][y * iStride[1]];
325 const BYTE* old2 = &pOldYUVData[2][y * iStride[2]];
326
327 if (memcmp(&cur1[uvLeft], &old1[uvLeft], uvSize) != 0)
328 return TRUE;
329 if (memcmp(&cur2[uvLeft], &old2[uvLeft], uvSize) != 0)
330 return TRUE;
331 }
332 return FALSE;
333}
334
335/* The AVC444v2 auxiliary view keeps the chroma detail of screen columns [left, right) in two
336 * halves (MS-RDPEGFX 3.3.8.3.3): in its Y plane at [left / 2, right / 2) and, width / 2 further,
337 * at [width / 2 + left / 2, width / 2 + right / 2) on the same rows; in its U and V planes at a
338 * quarter of those columns on half the rows. Comparing the tile's own columns there, as
339 * diff_tile does, looks at other pixels, so a tile whose chroma changed could go unreported
340 * and the client kept its old colours. */
341WINPR_ATTR_NODISCARD
342static inline BOOL diff_tile_avc444v2_aux(const RECTANGLE_16* tile, UINT32 width, BYTE* pYUVData[3],
343 BYTE* pOldYUVData[3], UINT32 const iStride[3])
344{
345 if (!tile || !pYUVData || !pOldYUVData || !iStride || (tile->right <= tile->left))
346 return FALSE;
347
348 const size_t l2 = tile->left / 2u;
349 const size_t n2 = (tile->right + 1u) / 2u - l2;
350 const size_t h2 = width / 2u;
351 const size_t l4 = tile->left / 4u;
352 const size_t n4 = (tile->right + 3u) / 4u - l4;
353 const size_t h4 = width / 4u;
354
355 if ((h2 + l2 + n2 > iStride[0]) || (h4 + l4 + n4 > iStride[1]) || (h4 + l4 + n4 > iStride[2]))
356 return TRUE; /* can't tell: send it */
357
358 for (size_t y = tile->top; y < tile->bottom; y++)
359 {
360 const BYTE* cur = &pYUVData[0][y * iStride[0]];
361 const BYTE* old = &pOldYUVData[0][y * iStride[0]];
362 if ((memcmp(&cur[l2], &old[l2], n2) != 0) ||
363 (memcmp(&cur[h2 + l2], &old[h2 + l2], n2) != 0))
364 return TRUE;
365 }
366
367 for (size_t y = tile->top / 2u; y < (tile->bottom + 1u) / 2u; y++)
368 {
369 for (size_t p = 1; p < 3; p++)
370 {
371 const BYTE* cur = &pYUVData[p][y * iStride[p]];
372 const BYTE* old = &pOldYUVData[p][y * iStride[p]];
373 if ((memcmp(&cur[l4], &old[l4], n4) != 0) ||
374 (memcmp(&cur[h4 + l4], &old[h4 + l4], n4) != 0))
375 return TRUE;
376 }
377 }
378 return FALSE;
379}
380
381BOOL detect_changes(BOOL firstFrameDone, UINT32 QP, const RECTANGLE_16* regionRect,
382 BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32 const iStride[3],
383 UINT32 v2AuxWidth, RDPGFX_H264_METABLOCK* meta)
384{
385 size_t count = 0;
386 size_t wc = 0;
387 size_t hc = 0;
388 RECTANGLE_16* rectangles = nullptr;
389
390 if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta)
391 return FALSE;
392
393 wc = (regionRect->right - regionRect->left) / 64 + 1;
394 hc = (regionRect->bottom - regionRect->top) / 64 + 1;
395 rectangles = calloc(wc * hc, sizeof(RECTANGLE_16));
396 if (!rectangles)
397 return FALSE;
398 if (!firstFrameDone)
399 {
400 rectangles[0] = *regionRect;
401 count = 1;
402 }
403 else
404 {
405 for (size_t y = 0; y < regionRect->bottom - regionRect->top; y += 64)
406 {
407 for (size_t x = 0; x < regionRect->right - regionRect->left; x += 64)
408 {
409 RECTANGLE_16 rect;
410 rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x);
411 rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y);
412 rect.right =
413 (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right));
414 rect.bottom =
415 (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom));
416 const BOOL changed =
417 v2AuxWidth
418 ? diff_tile_avc444v2_aux(&rect, v2AuxWidth, pYUVData, pOldYUVData, iStride)
419 : diff_tile(&rect, pYUVData, pOldYUVData, iStride);
420 if (changed)
421 rectangles[count++] = rect;
422 }
423 }
424 }
425 if (!allocate_h264_metablock(QP, rectangles, meta, count))
426 return FALSE;
427 return TRUE;
428}
429
430INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth,
431 UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3])
432{
433 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
434 return -1;
435
436 if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight))
437 return -1;
438
439 for (size_t x = 0; x < 3; x++)
440 {
441 YUVData[x] = h264->pYUVData[x];
442 stride[x] = h264->iStride[x];
443 }
444
445 return 0;
446}
447
448INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
449{
450 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
451 return -1;
452
453 const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] };
454
455 return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
456}
457
458/* The region to encode, rounded out to the 16 pixel macroblock grid inside the frame.
459 * Clients rebuild AVC444 colours relative to each region's corner: the odd chroma columns and
460 * rows, and every fourth sample in AVC444v2 (MS-RDPEGFX 3.3.8.3.2, 3.3.8.3.3), are counted from
461 * left and top. A region starting at an odd or unaligned column or row (the shadow server sends
462 * the bounding box of what changed, at any pixel) put those samples one pixel off. Encoders work
463 * on 16x16 macroblocks anyway, so the wider region costs no bits. */
464static RECTANGLE_16 h264_align_region(const RECTANGLE_16* rect, UINT32 width, UINT32 height)
465{
466 WINPR_ASSERT(rect);
467 const UINT32 right = MIN(width, ((UINT32)rect->right + 15u) & ~15u);
468 const UINT32 bottom = MIN(height, ((UINT32)rect->bottom + 15u) & ~15u);
469 const RECTANGLE_16 aligned = { (UINT16)(rect->left & ~15u), (UINT16)(rect->top & ~15u),
470 (UINT16)MAX(rect->right, right),
471 (UINT16)MAX(rect->bottom, bottom) };
472 return aligned;
473}
474
475/* The encoder alternates between two sets of planes: the one it converts into this frame held
476 * the frame before last, and only the region is converted. Outside the region the encoder then
477 * saw that older picture, a change every frame where nothing changed, which costs bits (and
478 * takes them from what did change). Start from the last frame, unless the region is all of it. */
479static void h264_keep_last_frame(const RECTANGLE_16* region, UINT32 width, UINT32 height,
480 BYTE* dst[3], BYTE* const src[3], const size_t size[3])
481{
482 WINPR_ASSERT(region);
483 if ((region->left == 0) && (region->top == 0) && (region->right >= width) &&
484 (region->bottom >= height))
485 return;
486
487 for (size_t x = 0; x < 3; x++)
488 {
489 if (dst[x] && src[x] && (size[x] > 0))
490 memcpy(dst[x], src[x], size[x]);
491 }
492}
493
494/* the size of the 4:2:0 planes, as yuv_ensure_buffer allocates them */
495static void h264_yuv420_plane_sizes(const H264_CONTEXT* h264, size_t size[3])
496{
497 const size_t rows = h264->height + 32u - h264->height % 16u;
498 for (size_t x = 0; x < 3; x++)
499 size[x] = 1ull * h264->iStride[x] * rows;
500}
501
502INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
503 UINT32 nSrcWidth, UINT32 nSrcHeight, const RECTANGLE_16* regionRect,
504 BYTE** ppDstData, UINT32* pDstSize, RDPGFX_H264_METABLOCK* meta)
505{
506 INT32 rc = -1;
507 BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT;
508 const BYTE* pcYUVData[3] = WINPR_C_ARRAY_INIT;
509 BYTE* pOldYUVData[3] = WINPR_C_ARRAY_INIT;
510
511 if (!h264 || !regionRect || !meta || !h264->Compressor)
512 return -1;
513
514 if (!h264->subsystem->Compress)
515 return -1;
516
517 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
518 return -1;
519
520 if (h264->encodingBuffer)
521 {
522 for (size_t x = 0; x < 3; x++)
523 {
524 pYUVData[x] = h264->pYUVData[x];
525 pOldYUVData[x] = h264->pOldYUVData[x];
526 }
527 }
528 else
529 {
530 for (size_t x = 0; x < 3; x++)
531 {
532 pYUVData[x] = h264->pOldYUVData[x];
533 pOldYUVData[x] = h264->pYUVData[x];
534 }
535 }
536 h264->encodingBuffer = !h264->encodingBuffer;
537 {
538 size_t size420[3] = WINPR_C_ARRAY_INIT;
539 h264_yuv420_plane_sizes(h264, size420);
540 h264_keep_last_frame(regionRect, nSrcWidth, nSrcHeight, pYUVData, pOldYUVData, size420);
541 }
542
543 const RECTANGLE_16 aligned = h264_align_region(regionRect, nSrcWidth, nSrcHeight);
544 if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData,
545 &aligned, 1))
546 goto fail;
547
548 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData,
549 h264->iStride, 0, meta))
550 goto fail;
551
552 if (meta->numRegionRects == 0)
553 {
554 rc = 0;
555 goto fail;
556 }
557
558 for (size_t x = 0; x < 3; x++)
559 pcYUVData[x] = pYUVData[x];
560
561 rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
562 if (rc >= 0)
563 h264->firstLumaFrameDone = TRUE;
564
565fail:
566 if (rc < 0)
567 free_h264_metablock(meta);
568 return rc;
569}
570
571INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
572 UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version, const RECTANGLE_16* region,
573 BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData,
574 UINT32* pAuxDstSize, RDPGFX_H264_METABLOCK* meta,
575 RDPGFX_H264_METABLOCK* auxMeta)
576{
577 int rc = -1;
578 BYTE* coded = nullptr;
579 UINT32 codedSize = 0;
580 BYTE** pYUV444Data = nullptr;
581 BYTE** pOldYUV444Data = nullptr;
582 BYTE** pYUVData = nullptr;
583 BYTE** pOldYUVData = nullptr;
584
585 if (!h264 || !h264->Compressor)
586 return -1;
587
588 if (!h264->subsystem->Compress)
589 return -1;
590
591 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
592 return -1;
593
594 if (!avc444_ensure_buffer(h264, nSrcHeight))
595 return -1;
596
597 if (h264->encodingBuffer)
598 {
599 pYUV444Data = h264->pOldYUV444Data;
600 pOldYUV444Data = h264->pYUV444Data;
601 pYUVData = h264->pOldYUVData;
602 pOldYUVData = h264->pYUVData;
603 }
604 else
605 {
606 pYUV444Data = h264->pYUV444Data;
607 pOldYUV444Data = h264->pOldYUV444Data;
608 pYUVData = h264->pYUVData;
609 pOldYUVData = h264->pOldYUVData;
610 }
611 h264->encodingBuffer = !h264->encodingBuffer;
612 {
613 size_t size420[3] = WINPR_C_ARRAY_INIT;
614 size_t size444[3] = WINPR_C_ARRAY_INIT;
615 h264_yuv420_plane_sizes(h264, size420);
616 for (size_t x = 0; x < 3; x++)
617 size444[x] = h264->iYUV444Size[x];
618 h264_keep_last_frame(region, nSrcWidth, nSrcHeight, pYUVData, pOldYUVData, size420);
619 h264_keep_last_frame(region, nSrcWidth, nSrcHeight, pYUV444Data, pOldYUV444Data, size444);
620 }
621
622 const RECTANGLE_16 aligned = h264_align_region(region, nSrcWidth, nSrcHeight);
623 if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride,
624 pYUV444Data, pYUVData, &aligned, 1))
625 goto fail;
626
627 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUV444Data, pOldYUV444Data,
628 h264->iStride, 0, meta))
629 goto fail;
630 /* in AVC444v2 the auxiliary view is laid out by the frame width, not by screen position */
631 if (!detect_changes(h264->firstChromaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData,
632 h264->iStride, (version == 2) ? h264->width : 0, auxMeta))
633 goto fail;
634
635 /* [MS-RDPEGFX] 2.2.4.5 RFX_AVC444_BITMAP_STREAM
636 * LC:
637 * 0 ... Luma & Chroma
638 * 1 ... Luma
639 * 2 ... Chroma
640 */
641 if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0))
642 *op = 0;
643 else if (meta->numRegionRects > 0)
644 *op = 1;
645 else if (auxMeta->numRegionRects > 0)
646 *op = 2;
647 else
648 {
649 WLog_Print(h264->log, WLOG_TRACE, "no changes detected for luma or chroma frame");
650 rc = 0;
651 goto fail;
652 }
653
654 if ((*op == 0) || (*op == 1))
655 {
656 const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] };
657
658 if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0)
659 goto fail;
660 h264->firstLumaFrameDone = TRUE;
661 memcpy(h264->lumaData, coded, codedSize);
662 *ppDstData = h264->lumaData;
663 *pDstSize = codedSize;
664 }
665
666 if ((*op == 0) || (*op == 2))
667 {
668 const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] };
669
670 if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0)
671 goto fail;
672 h264->firstChromaFrameDone = TRUE;
673 *ppAuxDstData = coded;
674 *pAuxDstSize = codedSize;
675 }
676
677 rc = 1;
678fail:
679 if (rc < 0)
680 {
681 free_h264_metablock(meta);
682 free_h264_metablock(auxMeta);
683 }
684 return rc;
685}
686
687BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight)
688{
689 WINPR_ASSERT(h264);
690
691 const UINT32* piMainStride = h264->iStride;
692 UINT32* piDstSize = h264->iYUV444Size;
693 UINT32* piDstStride = h264->iYUV444Stride;
694 BYTE** ppYUVDstData = h264->pYUV444Data;
695 BYTE** ppOldYUVDstData = h264->pOldYUV444Data;
696
697 nDstHeight = MAX(h264->height, nDstHeight);
698 const UINT32 pad = nDstHeight % 16;
699 UINT32 padDstHeight = nDstHeight; /* Need alignment to 16x16 blocks */
700
701 if (pad != 0)
702 padDstHeight += 16 - pad;
703
704 /* Add padding lines. Allows relaxing bounds checks in decoder functions */
705 padDstHeight += 16;
706
707 if ((piMainStride[0] == 0) || (padDstHeight == 0))
708 return FALSE;
709
710 const uint64_t dstsize = 1ull * piMainStride[0] * padDstHeight;
711 if (dstsize > UINT32_MAX)
712 return FALSE;
713
714 if ((piMainStride[0] != piDstStride[0]) || (piDstSize[0] != dstsize))
715 {
716 const UINT32 size = WINPR_ASSERTING_INT_CAST(UINT32, dstsize);
717
718 for (UINT32 x = 0; x < 3; x++)
719 {
720 BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], size, 1, 16);
721 if (!tmp1)
722 return FALSE;
723 ppYUVDstData[x] = tmp1;
724 BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], size, 1, 16);
725 if (!tmp2)
726 return FALSE;
727 ppOldYUVDstData[x] = tmp2;
728 }
729
730 {
731 BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, size, 4, 16);
732 if (!tmp)
733 goto fail;
734 h264->lumaData = tmp;
735 }
736
737 /* Only update stride and size after all buffers were successfully allocated,
738 * otherwise the next call would skip the reallocation and use too small planes */
739 for (UINT32 x = 0; x < 3; x++)
740 {
741 piDstStride[x] = piMainStride[0];
742 piDstSize[x] = size;
743 }
744 }
745
746 for (UINT32 x = 0; x < 3; x++)
747 {
748 if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
749 {
750 WLog_Print(h264->log, WLOG_ERROR,
751 "YUV buffer not initialized! check your decoder settings");
752 goto fail;
753 }
754 }
755
756 if (!h264->lumaData)
757 goto fail;
758
759 return TRUE;
760fail:
761 return FALSE;
762}
763
764WINPR_ATTR_NODISCARD
765static BOOL avc444_process_rects(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize,
766 BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
767 WINPR_ATTR_UNUSED UINT32 nDstWidth, UINT32 nDstHeight,
768 const RECTANGLE_16* rects, UINT32 nrRects, avc444_frame_type type)
769{
770 const BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT;
771 BYTE* pYUVDstData[3] = WINPR_C_ARRAY_INIT;
772 UINT32* piDstStride = h264->iYUV444Stride;
773 BYTE** ppYUVDstData = h264->pYUV444Data;
774 const UINT32* piStride = h264->iStride;
775
776 const int status = log_decompress(h264, pSrcData, SrcSize, rects, nrRects);
777 if (status < 0)
778 return FALSE;
779 if (status == 0)
780 return TRUE;
781
782 pYUVData[0] = h264->pYUVData[0];
783 pYUVData[1] = h264->pYUVData[1];
784 pYUVData[2] = h264->pYUVData[2];
785 if (!avc444_ensure_buffer(h264, nDstHeight))
786 return FALSE;
787
788 pYUVDstData[0] = ppYUVDstData[0];
789 pYUVDstData[1] = ppYUVDstData[1];
790 pYUVDstData[2] = ppYUVDstData[2];
791 return (yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->YUVHeight,
792 pYUVDstData, piDstStride, DstFormat, pDstData, nDstStep, rects,
793 nrRects));
794}
795
796#if defined(AVC444_FRAME_STAT)
797static UINT64 op1 = 0;
798static double op1sum = 0;
799static UINT64 op2 = 0;
800static double op2sum = 0;
801static UINT64 op3 = 0;
802static double op3sum = 0;
803static double avg(UINT64* count, double old, double size)
804{
805 double tmp = size + *count * old;
806 (*count)++;
807 tmp = tmp / *count;
808 return tmp;
809}
810#endif
811
812INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op, const RECTANGLE_16* regionRects,
813 UINT32 numRegionRects, const BYTE* pSrcData, UINT32 SrcSize,
814 const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
815 const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat,
816 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId)
817{
818 INT32 status = -1;
819 avc444_frame_type chroma =
820 (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
821
822 if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
823 return -1001;
824
825 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects))
826 return -1013;
827 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect))
828 return -1014;
829
830 switch (op)
831 {
832 case 0: /* YUV420 in stream 1
833 * Chroma420 in stream 2 */
834 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
835 nDstWidth, nDstHeight, regionRects, numRegionRects,
836 AVC444_LUMA))
837 status = -1;
838 else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
839 nDstStep, nDstWidth, nDstHeight, auxRegionRects,
840 numAuxRegionRect, chroma))
841 status = -1;
842 else
843 status = 0;
844
845 break;
846
847 case 2: /* Chroma420 in stream 1 */
848 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
849 nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
850 status = -1;
851 else
852 status = 0;
853
854 break;
855
856 case 1: /* YUV420 in stream 1 */
857 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
858 nDstWidth, nDstHeight, regionRects, numRegionRects,
859 AVC444_LUMA))
860 status = -1;
861 else
862 status = 0;
863
864 break;
865
866 default: /* WTF? */
867 break;
868 }
869
870#if defined(AVC444_FRAME_STAT)
871
872 switch (op)
873 {
874 case 0:
875 op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
876 break;
877
878 case 1:
879 op2sum = avg(&op2, op2sum, SrcSize);
880 break;
881
882 case 2:
883 op3sum = avg(&op3, op3sum, SrcSize);
884 break;
885
886 default:
887 break;
888 }
889
890 WLog_Print(h264->log, WLOG_INFO,
891 "luma=%" PRIu64 " [avg=%lf] chroma=%" PRIu64 " [avg=%lf] combined=%" PRIu64
892 " [avg=%lf]",
893 op1, op1sum, op2, op2sum, op3, op3sum);
894#endif
895 return status;
896}
897
898#define MAX_SUBSYSTEMS 10
899static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT;
900static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = WINPR_C_ARRAY_INIT;
901
902WINPR_ATTR_NODISCARD
903static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED PINIT_ONCE once,
904 WINPR_ATTR_UNUSED PVOID param,
905 WINPR_ATTR_UNUSED PVOID* context)
906{
907 int i = 0;
908
909#ifdef WITH_MEDIACODEC
910 {
911 subSystems[i] = &g_Subsystem_mediacodec;
912 i++;
913 }
914#endif
915#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
916 {
917 subSystems[i] = &g_Subsystem_MF;
918 i++;
919 }
920#endif
921#ifdef WITH_APPLE_VIDEOTOOLBOX
922 {
923 subSystems[i] = &g_Subsystem_VideoToolbox;
924 i++;
925 }
926#endif
927#ifdef WITH_OPENH264
928 {
929 subSystems[i] = &g_Subsystem_OpenH264;
930 i++;
931 }
932#endif
933#ifdef WITH_VIDEO_FFMPEG
934 {
935 subSystems[i] = &g_Subsystem_libavcodec;
936 i++;
937 }
938#endif
939#ifdef WITH_H264_WEBCODECS
940 {
941 subSystems[i] = &g_Subsystem_WebCodecs;
942 i++;
943 }
944#endif
945 return i > 0;
946}
947
948WINPR_ATTR_NODISCARD
949static BOOL h264_context_init(H264_CONTEXT* h264)
950{
951 if (!h264)
952 return FALSE;
953
954 h264->subsystem = nullptr;
955 h264->yuv444 = FALSE;
956 if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems, nullptr, nullptr))
957 return FALSE;
958
959 for (size_t i = 0; i < MAX_SUBSYSTEMS; i++)
960 {
961 const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
962
963 if (!subsystem || !subsystem->Init)
964 break;
965
966 if (subsystem->Init(h264))
967 {
968 h264->subsystem = subsystem;
969 return TRUE;
970 }
971 }
972
973 return FALSE;
974}
975
976static void h264_buffers_free(H264_CONTEXT* h264)
977{
978 WINPR_ASSERT(h264);
979 for (size_t x = 0; x < 3; x++)
980 {
981 if (h264->Compressor)
982 {
983 winpr_aligned_free(h264->pYUVData[x]);
984 winpr_aligned_free(h264->pOldYUVData[x]);
985 }
986 winpr_aligned_free(h264->pYUV444Data[x]);
987 winpr_aligned_free(h264->pOldYUV444Data[x]);
988
989 h264->pYUVData[x] = nullptr;
990 h264->pOldYUVData[x] = nullptr;
991 h264->pYUV444Data[x] = nullptr;
992 h264->pOldYUV444Data[x] = nullptr;
993 }
994 winpr_aligned_free(h264->lumaData);
995 h264->lumaData = nullptr;
996}
997
998BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
999{
1000 if (!h264)
1001 return FALSE;
1002
1003 if (width > UINT16_MAX)
1004 {
1005 WLog_Print(h264->log, WLOG_ERROR, "h264::width %" PRIu32 " > %d, aborting", width,
1006 UINT16_MAX);
1007 return FALSE;
1008 }
1009 if (height > UINT16_MAX)
1010 {
1011 WLog_Print(h264->log, WLOG_ERROR, "h264::height %" PRIu32 " > %d, aborting", height,
1012 UINT16_MAX);
1013 return FALSE;
1014 }
1015
1016 if (h264->subsystem && h264->subsystem->Uninit)
1017 h264->subsystem->Uninit(h264);
1018
1019 h264_buffers_free(h264);
1020 h264->width = width;
1021 h264->height = height;
1022
1023 if (!h264_context_init(h264))
1024 return FALSE;
1025
1026 return yuv_context_reset(h264->yuv, width, height);
1027}
1028
1029H264_CONTEXT* h264_context_new(BOOL Compressor)
1030{
1031 return h264_context_new_ex(Compressor, FALSE, FALSE);
1032}
1033
1034H264_CONTEXT* h264_context_new_ex(BOOL Compressor, BOOL hevc, BOOL hevc444)
1035{
1036 H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1, sizeof(H264_CONTEXT));
1037 if (!h264)
1038 return nullptr;
1039
1040 h264->log = WLog_Get(TAG);
1041
1042 if (!h264->log)
1043 goto fail;
1044
1045 h264->hevc = hevc;
1046 h264->hevc444 = hevc444;
1047 h264->Compressor = Compressor;
1048 if (Compressor)
1049 {
1050 /* Default compressor settings, may be changed by caller */
1051 h264->BitRate = 1000000;
1052 h264->FrameRate = 30;
1053 }
1054
1055 if (!h264_context_init(h264))
1056 goto fail;
1057
1058 h264->yuv = yuv_context_new(Compressor, 0);
1059 if (!h264->yuv)
1060 goto fail;
1061
1062 return h264;
1063
1064fail:
1065 WINPR_PRAGMA_DIAG_PUSH
1066 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1067 h264_context_free(h264);
1068 WINPR_PRAGMA_DIAG_POP
1069 return nullptr;
1070}
1071
1072void h264_context_free(H264_CONTEXT* h264)
1073{
1074 if (!h264)
1075 return;
1076
1077 if (h264->subsystem)
1078 {
1079 WINPR_ASSERT(h264->subsystem->Uninit);
1080 h264->subsystem->Uninit(h264);
1081 }
1082
1083 h264_buffers_free(h264);
1084
1085 yuv_context_free(h264->yuv);
1086 free(h264);
1087}
1088
1089void free_h264_metablock(RDPGFX_H264_METABLOCK* meta)
1090{
1091 RDPGFX_H264_METABLOCK m = WINPR_C_ARRAY_INIT;
1092 if (!meta)
1093 return;
1094 free(meta->quantQualityVals);
1095 free(meta->regionRects);
1096 *meta = m;
1097}
1098
1099BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value)
1100{
1101 WINPR_ASSERT(h264);
1102 switch (option)
1103 {
1104 case H264_CONTEXT_OPTION_BITRATE:
1105 h264->BitRate = value;
1106 return TRUE;
1107 case H264_CONTEXT_OPTION_FRAMERATE:
1108 h264->FrameRate = value;
1109 return TRUE;
1110 case H264_CONTEXT_OPTION_RATECONTROL:
1111 {
1112 switch (value)
1113 {
1114 case H264_RATECONTROL_VBR:
1115 h264->RateControlMode = H264_RATECONTROL_VBR;
1116 return TRUE;
1117 case H264_RATECONTROL_CQP:
1118 h264->RateControlMode = H264_RATECONTROL_CQP;
1119 return TRUE;
1120 default:
1121 WLog_Print(h264->log, WLOG_WARN,
1122 "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32 "]",
1123 value);
1124 return FALSE;
1125 }
1126 return TRUE;
1127 }
1128 case H264_CONTEXT_OPTION_QP:
1129 h264->QP = value;
1130 return TRUE;
1131 case H264_CONTEXT_OPTION_USAGETYPE:
1132 h264->UsageType = value;
1133 return TRUE;
1134 case H264_CONTEXT_OPTION_HW_ACCEL:
1135#ifdef WITH_APPLE_VIDEOTOOLBOX
1136 /* VideoToolbox follows Apple's decoder-selection policy;
1137 * ignore this option without reinitializing the decoder. */
1138 if (h264->subsystem == &g_Subsystem_VideoToolbox)
1139 return TRUE;
1140#endif
1141 if (h264->hwAccel == value)
1142 return TRUE;
1143 h264->hwAccel = (value);
1144 IFCALL(h264->subsystem->Uninit, h264);
1145 return IFCALLRESULT(TRUE, h264->subsystem->Init, h264);
1146 case H264_CONTEXT_OPTION_HEVC:
1147 {
1148 const BOOL hevc = value != 0;
1149 if (h264->hevc == hevc)
1150 return TRUE;
1151 /* the codec decides which subsystem can be used, select it again */
1152 if (h264->subsystem)
1153 IFCALL(h264->subsystem->Uninit, h264);
1154 h264->hevc = hevc;
1155 if (h264_context_init(h264))
1156 return TRUE;
1157 WLog_Print(h264->log, WLOG_WARN, "no H264 subsystem supports %s",
1158 hevc ? "HEVC" : "H264");
1159 return FALSE;
1160 }
1161 case H264_CONTEXT_OPTION_HEVC444:
1162 {
1163 const BOOL hevc444 = value != 0;
1164 if (h264->hevc444 == hevc444)
1165 return TRUE;
1166 if (hevc444 && !h264->hevc)
1167 {
1168 WLog_Print(h264->log, WLOG_WARN, "HEVC 4:4:4 needs H264_CONTEXT_OPTION_HEVC");
1169 return FALSE;
1170 }
1171 if (h264->subsystem)
1172 IFCALL(h264->subsystem->Uninit, h264);
1173 h264->hevc444 = hevc444;
1174 if (h264_context_init(h264))
1175 return TRUE;
1176 WLog_Print(h264->log, WLOG_WARN, "no H264 subsystem supports HEVC %s",
1177 hevc444 ? "4:4:4" : "4:2:0");
1178 return FALSE;
1179 }
1180 default:
1181 WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]",
1182 option);
1183 return FALSE;
1184 }
1185}
1186
1187UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
1188{
1189 WINPR_ASSERT(h264);
1190 switch (option)
1191 {
1192 case H264_CONTEXT_OPTION_BITRATE:
1193 return h264->BitRate;
1194 case H264_CONTEXT_OPTION_FRAMERATE:
1195 return h264->FrameRate;
1196 case H264_CONTEXT_OPTION_RATECONTROL:
1197 return h264->RateControlMode;
1198 case H264_CONTEXT_OPTION_QP:
1199 return h264->QP;
1200 case H264_CONTEXT_OPTION_USAGETYPE:
1201 return h264->UsageType;
1202 case H264_CONTEXT_OPTION_HW_ACCEL:
1203 return h264->hwAccel;
1204 case H264_CONTEXT_OPTION_HEVC:
1205 return h264->hevc ? 1 : 0;
1206 case H264_CONTEXT_OPTION_HEVC444:
1207 return h264->hevc444 ? 1 : 0;
1208 default:
1209 WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]",
1210 option);
1211 return 0;
1212 }
1213}
1214
1215static BOOL codecSupportStatus[33][2];
1216static INIT_ONCE codecSupportStatusOnce = INIT_ONCE_STATIC_INIT;
1217
1218static size_t codecFlagToIndex(FreeRDP_CodecFlags codec)
1219{
1220 for (size_t index = 0; index < 32; index++)
1221 {
1222 const size_t mask = 1ull << index;
1223 if (codec == mask)
1224 return index;
1225 }
1226 return ARRAYSIZE(codecSupportStatus) - 1;
1227}
1228
1229static void setCodecSupportStatus(FreeRDP_CodecFlags codec, BOOL decoder, BOOL encoder)
1230{
1231 const size_t index = codecFlagToIndex(codec);
1232 codecSupportStatus[index][0] = decoder;
1233 codecSupportStatus[index][1] = encoder;
1234}
1235
1236#if defined(WITH_GFX_AV1)
1237WINPR_ATTR_NODISCARD
1238static BOOL test_av1(BOOL Compressor, UINT32 flags)
1239{
1240 FREERDP_AV1_CONTEXT* av1 = freerdp_av1_context_new(Compressor);
1241 if (!av1)
1242 return FALSE;
1243 UINT32 profile = 0;
1244 if (flags == FREERDP_CODEC_AV1_I444)
1245 profile = 1;
1246 BOOL decoder = TRUE;
1247 if (Compressor)
1248 decoder = freerdp_av1_context_set_option(av1, FREERDP_AV1_CONTEXT_OPTION_PROFILE, profile);
1249 freerdp_av1_context_free(av1);
1250 return decoder;
1251}
1252#endif
1253
1254#if defined(WITH_GFX_AV2)
1255WINPR_ATTR_NODISCARD
1256static BOOL test_av2(BOOL Compressor, UINT32 flags)
1257{
1258 FREERDP_AV2_CONTEXT* av2 = freerdp_av2_context_new(Compressor);
1259 if (!av2)
1260 return FALSE;
1261 UINT32 profile = 0;
1262 if (flags == FREERDP_CODEC_AV2_I444)
1263 profile = 1;
1264 BOOL decoder = TRUE;
1265 if (Compressor)
1266 decoder = freerdp_av2_context_set_option(av2, FREERDP_AV2_CONTEXT_OPTION_PROFILE, profile);
1267 freerdp_av2_context_free(av2);
1268 return decoder;
1269}
1270#endif
1271
1272#if defined(WITH_GFX_AZURE)
1273WINPR_ATTR_NODISCARD
1274static BOOL test_hevc(BOOL Compressor, UINT32 flags)
1275{
1276 FREERDP_HEVC_CONTEXT* hevc = freerdp_hevc_context_new(Compressor);
1277 if (!hevc)
1278 return FALSE;
1279
1280 UINT32 profile = 0;
1281 if (flags == FREERDP_CODEC_HEVC_I444)
1282 profile = 1;
1283 const BOOL decoder =
1284 freerdp_hevc_context_set_option(hevc, FREERDP_HEVC_CONTEXT_OPTION_PROFILE, profile);
1285 freerdp_hevc_context_free(hevc);
1286 return decoder;
1287}
1288#endif
1289
1290static BOOL CALLBACK codec_support_status_init(WINPR_ATTR_UNUSED PINIT_ONCE once,
1291 WINPR_ATTR_UNUSED PVOID param,
1292 WINPR_ATTR_UNUSED PVOID* context)
1293{
1294 setCodecSupportStatus(FREERDP_CODEC_INTERLEAVED, TRUE, TRUE);
1295 setCodecSupportStatus(FREERDP_CODEC_PLANAR, TRUE, TRUE);
1296 setCodecSupportStatus(FREERDP_CODEC_NSCODEC, TRUE, TRUE);
1297 setCodecSupportStatus(FREERDP_CODEC_REMOTEFX, TRUE, TRUE);
1298 setCodecSupportStatus(FREERDP_CODEC_CLEARCODEC, TRUE, TRUE);
1299 setCodecSupportStatus(FREERDP_CODEC_ALPHACODEC, TRUE, TRUE);
1300 setCodecSupportStatus(FREERDP_CODEC_PROGRESSIVE, TRUE, TRUE);
1301
1302#if defined(WITH_GFX_H264)
1303 {
1304 H264_CONTEXT* h264 = h264_context_new(FALSE);
1305 const BOOL decoder = h264 != nullptr;
1306 h264_context_free(h264);
1307
1308 H264_CONTEXT* h264enc = h264_context_new(TRUE);
1309 const BOOL encoder = h264enc != nullptr;
1310 h264_context_free(h264enc);
1311
1312 setCodecSupportStatus(FREERDP_CODEC_AVC420, decoder, encoder);
1313 setCodecSupportStatus(FREERDP_CODEC_AVC444, decoder, encoder);
1314 }
1315#endif
1316#if defined(WITH_GFX_AV1)
1317 {
1318 setCodecSupportStatus(FREERDP_CODEC_AV1_I420, test_av1(FALSE, FREERDP_CODEC_AV1_I420),
1319 test_av1(TRUE, FREERDP_CODEC_AV1_I420));
1320 setCodecSupportStatus(FREERDP_CODEC_AV1_I444, test_av1(FALSE, FREERDP_CODEC_AV1_I444),
1321 test_av1(TRUE, FREERDP_CODEC_AV1_I444));
1322 }
1323#endif
1324#if defined(WITH_GFX_AV2)
1325 {
1326 setCodecSupportStatus(FREERDP_CODEC_AV2_I420, test_av2(FALSE, FREERDP_CODEC_AV2_I420),
1327 test_av2(TRUE, FREERDP_CODEC_AV2_I420));
1328 setCodecSupportStatus(FREERDP_CODEC_AV2_I444, test_av2(FALSE, FREERDP_CODEC_AV2_I444),
1329 test_av2(TRUE, FREERDP_CODEC_AV2_I444));
1330 }
1331#endif
1332#if defined(WITH_GFX_AZURE)
1333 {
1334 setCodecSupportStatus(FREERDP_CODEC_HEVC_I420, test_hevc(FALSE, FREERDP_CODEC_HEVC_I420),
1335 test_hevc(TRUE, FREERDP_CODEC_HEVC_I420));
1336 setCodecSupportStatus(FREERDP_CODEC_HEVC_I444, test_hevc(FALSE, FREERDP_CODEC_HEVC_I444),
1337 test_hevc(TRUE, FREERDP_CODEC_HEVC_I444));
1338 }
1339#endif
1340
1341 return TRUE;
1342}
1343
1344BOOL freerdp_codec_supported(WINPR_ATTR_UNUSED rdpContext* context, FreeRDP_CodecFlags codec,
1345 BOOL encoder)
1346{
1347 if (!InitOnceExecuteOnce(&codecSupportStatusOnce, codec_support_status_init, nullptr, nullptr))
1348 FALSE;
1349 size_t index = encoder ? 1 : 0;
1350 return codecSupportStatus[codecFlagToIndex(codec)][index];
1351}
WINPR_ATTR_NODISCARD fn_YUV444ToRGB_8u_P3AC4R_t I444ToRGB_8u
Convert a plain YUV444 picture (e.g. decoded AV1) to RGB. Unlike YUV444ToRGB_8u_P3AC4R this does not ...
Definition primitives.h:310