21#include <freerdp/config.h>
24#include <winpr/print.h>
25#include <winpr/library.h>
26#include <winpr/bitstream.h>
27#include <winpr/synch.h>
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>
38#define TAG FREERDP_TAG("codec")
41static BOOL yuv_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
44 UINT32 pheight = height;
51 const UINT32 pad = width % 16;
58 stride += 32 - stride % 16;
59 pheight += 32 - pheight % 16;
61 const size_t nPlanes = h264->hwAccel ? 2 : 3;
63 for (
size_t x = 0; x < nPlanes; x++)
65 if (!h264->pYUVData[x] || !h264->pOldYUVData[x])
74 if (isNull || (width != h264->width) || (height != h264->height) ||
75 (stride != h264->iStride[0]))
77 UINT32 iStride[3] = WINPR_C_ARRAY_INIT;
87 iStride[1] = (stride + 1) / 2;
88 iStride[2] = (stride + 1) / 2;
91 for (
size_t x = 0; x < nPlanes; x++)
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);
96 h264->pYUVData[x] = tmp1;
98 h264->pOldYUVData[x] = tmp2;
103 memcpy(h264->iStride, iStride,
sizeof(h264->iStride));
105 h264->height = height;
106 h264->firstLumaFrameDone = FALSE;
112BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
114 return yuv_ensure_buffer(h264, stride, width, height);
118static BOOL isRectValid(wLog* log,
size_t pos, UINT32 width, UINT32 height,
122 if ((rect->left > width) || (rect->right > width) || (rect->left >= rect->right) ||
123 (rect->top > height) || (rect->bottom > height) || (rect->top >= rect->bottom))
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);
136static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height,
const RECTANGLE_16* rects,
139 WINPR_ASSERT(rects || (count == 0));
140 for (
size_t x = 0; x < count; x++)
143 if (!isRectValid(log, x, width, height, rect))
150static int log_decompress(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize,
154 WINPR_ASSERT(h264->subsystem);
155 WINPR_ASSERT(h264->subsystem->Decompress);
157 const int status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
160 WLog_Print(h264->log, WLOG_WARN,
"H264 decompress failed with %d", status);
170 if (!areRectsValid(h264->log, h264->YUVWidth, h264->YUVHeight, rects, nrRects))
172 if (!areRectsValid(h264->log, h264->width, h264->height, rects, nrRects))
180static BOOL yuv444_planes_decode(H264_CONTEXT* h264, DWORD DstFormat, BYTE* pDstData,
182 UINT32 numRegionRects)
190 const size_t bpp = FreeRDPGetBytesPerPixel(DstFormat);
191 for (UINT32 x = 0; x < numRegionRects; 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];
201 if (prims->
I444ToRGB_8u(pSrc, h264->iStride, pDst, nDstStep, DstFormat, &roi) !=
208INT32 avc420_decompress(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
209 DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
212 return h26X_decompress(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
213 nDstHeight, regionRects, numRegionRects);
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)
222 const BYTE* pYUVData[3];
224 if (!h264 || h264->Compressor)
227 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects))
230 status = log_decompress(h264, pSrcData, SrcSize, regionRects, numRegionRects);
240 if (!yuv444_planes_decode(h264, DstFormat, pDstData, nDstStep, regionRects, numRegionRects))
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))
260 if (!meta || (QP > UINT8_MAX))
266 meta->regionRects = rectangles;
270 if (count > UINT32_MAX)
275 if (!meta->quantQualityVals || !meta->regionRects)
277 meta->numRegionRects = (UINT32)count;
278 for (
size_t x = 0; x < count; x++)
285 cur->qualityVal = 100 - (QP & 0x3F);
291static inline BOOL diff_tile(
const RECTANGLE_16* regionRect, BYTE* pYUVData[3],
292 BYTE* pOldYUVData[3], UINT32
const iStride[3])
296 if (!regionRect || !pYUVData || !pOldYUVData || !iStride)
298 size = regionRect->right - regionRect->left;
299 if (regionRect->right > iStride[0])
301 if (regionRect->right / 2u > iStride[1])
303 if (regionRect->right / 2u > iStride[2])
306 for (
size_t y = regionRect->top; y < regionRect->bottom; y++)
308 const BYTE* cur0 = &pYUVData[0][y * iStride[0]];
309 const BYTE* old0 = &pOldYUVData[0][y * iStride[0]];
311 if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0)
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++)
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]];
327 if (memcmp(&cur1[uvLeft], &old1[uvLeft], uvSize) != 0)
329 if (memcmp(&cur2[uvLeft], &old2[uvLeft], uvSize) != 0)
342static inline BOOL diff_tile_avc444v2_aux(
const RECTANGLE_16* tile, UINT32 width, BYTE* pYUVData[3],
343 BYTE* pOldYUVData[3], UINT32
const iStride[3])
345 if (!tile || !pYUVData || !pOldYUVData || !iStride || (tile->right <= tile->left))
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;
355 if ((h2 + l2 + n2 > iStride[0]) || (h4 + l4 + n4 > iStride[1]) || (h4 + l4 + n4 > iStride[2]))
358 for (
size_t y = tile->top; y < tile->bottom; y++)
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))
367 for (
size_t y = tile->top / 2u; y < (tile->bottom + 1u) / 2u; y++)
369 for (
size_t p = 1; p < 3; p++)
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))
381BOOL detect_changes(BOOL firstFrameDone, UINT32 QP,
const RECTANGLE_16* regionRect,
382 BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32
const iStride[3],
390 if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta)
393 wc = (regionRect->right - regionRect->left) / 64 + 1;
394 hc = (regionRect->bottom - regionRect->top) / 64 + 1;
400 rectangles[0] = *regionRect;
405 for (
size_t y = 0; y < regionRect->bottom - regionRect->top; y += 64)
407 for (
size_t x = 0; x < regionRect->right - regionRect->left; x += 64)
410 rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x);
411 rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y);
413 (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right));
415 (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom));
418 ? diff_tile_avc444v2_aux(&rect, v2AuxWidth, pYUVData, pOldYUVData, iStride)
419 : diff_tile(&rect, pYUVData, pOldYUVData, iStride);
421 rectangles[count++] = rect;
425 if (!allocate_h264_metablock(QP, rectangles, meta, count))
430INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth,
431 UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3])
433 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
436 if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight))
439 for (
size_t x = 0; x < 3; x++)
441 YUVData[x] = h264->pYUVData[x];
442 stride[x] = h264->iStride[x];
448INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
450 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
453 const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] };
455 return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
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) };
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])
482 WINPR_ASSERT(region);
483 if ((region->left == 0) && (region->top == 0) && (region->right >= width) &&
484 (region->bottom >= height))
487 for (
size_t x = 0; x < 3; x++)
489 if (dst[x] && src[x] && (size[x] > 0))
490 memcpy(dst[x], src[x], size[x]);
495static void h264_yuv420_plane_sizes(
const H264_CONTEXT* h264,
size_t size[3])
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;
502INT32 avc420_compress(H264_CONTEXT* h264,
const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
503 UINT32 nSrcWidth, UINT32 nSrcHeight,
const RECTANGLE_16* regionRect,
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;
511 if (!h264 || !regionRect || !meta || !h264->Compressor)
514 if (!h264->subsystem->Compress)
517 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
520 if (h264->encodingBuffer)
522 for (
size_t x = 0; x < 3; x++)
524 pYUVData[x] = h264->pYUVData[x];
525 pOldYUVData[x] = h264->pOldYUVData[x];
530 for (
size_t x = 0; x < 3; x++)
532 pYUVData[x] = h264->pOldYUVData[x];
533 pOldYUVData[x] = h264->pYUVData[x];
536 h264->encodingBuffer = !h264->encodingBuffer;
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);
543 const RECTANGLE_16 aligned = h264_align_region(regionRect, nSrcWidth, nSrcHeight);
544 if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData,
548 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData,
549 h264->iStride, 0, meta))
552 if (meta->numRegionRects == 0)
558 for (
size_t x = 0; x < 3; x++)
559 pcYUVData[x] = pYUVData[x];
561 rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
563 h264->firstLumaFrameDone = TRUE;
567 free_h264_metablock(meta);
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,
578 BYTE* coded =
nullptr;
579 UINT32 codedSize = 0;
580 BYTE** pYUV444Data =
nullptr;
581 BYTE** pOldYUV444Data =
nullptr;
582 BYTE** pYUVData =
nullptr;
583 BYTE** pOldYUVData =
nullptr;
585 if (!h264 || !h264->Compressor)
588 if (!h264->subsystem->Compress)
591 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
594 if (!avc444_ensure_buffer(h264, nSrcHeight))
597 if (h264->encodingBuffer)
599 pYUV444Data = h264->pOldYUV444Data;
600 pOldYUV444Data = h264->pYUV444Data;
601 pYUVData = h264->pOldYUVData;
602 pOldYUVData = h264->pYUVData;
606 pYUV444Data = h264->pYUV444Data;
607 pOldYUV444Data = h264->pOldYUV444Data;
608 pYUVData = h264->pYUVData;
609 pOldYUVData = h264->pOldYUVData;
611 h264->encodingBuffer = !h264->encodingBuffer;
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);
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))
627 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUV444Data, pOldYUV444Data,
628 h264->iStride, 0, meta))
631 if (!detect_changes(h264->firstChromaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData,
632 h264->iStride, (version == 2) ? h264->width : 0, auxMeta))
641 if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0))
643 else if (meta->numRegionRects > 0)
645 else if (auxMeta->numRegionRects > 0)
649 WLog_Print(h264->log, WLOG_TRACE,
"no changes detected for luma or chroma frame");
654 if ((*op == 0) || (*op == 1))
656 const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] };
658 if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0)
660 h264->firstLumaFrameDone = TRUE;
661 memcpy(h264->lumaData, coded, codedSize);
662 *ppDstData = h264->lumaData;
663 *pDstSize = codedSize;
666 if ((*op == 0) || (*op == 2))
668 const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] };
670 if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0)
672 h264->firstChromaFrameDone = TRUE;
673 *ppAuxDstData = coded;
674 *pAuxDstSize = codedSize;
681 free_h264_metablock(meta);
682 free_h264_metablock(auxMeta);
687BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight)
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;
697 nDstHeight = MAX(h264->height, nDstHeight);
698 const UINT32 pad = nDstHeight % 16;
699 UINT32 padDstHeight = nDstHeight;
702 padDstHeight += 16 - pad;
707 if ((piMainStride[0] == 0) || (padDstHeight == 0))
710 const uint64_t dstsize = 1ull * piMainStride[0] * padDstHeight;
711 if (dstsize > UINT32_MAX)
714 if ((piMainStride[0] != piDstStride[0]) || (piDstSize[0] != dstsize))
716 const UINT32 size = WINPR_ASSERTING_INT_CAST(UINT32, dstsize);
718 for (UINT32 x = 0; x < 3; x++)
720 BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], size, 1, 16);
723 ppYUVDstData[x] = tmp1;
724 BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], size, 1, 16);
727 ppOldYUVDstData[x] = tmp2;
731 BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, size, 4, 16);
734 h264->lumaData = tmp;
739 for (UINT32 x = 0; x < 3; x++)
741 piDstStride[x] = piMainStride[0];
746 for (UINT32 x = 0; x < 3; x++)
748 if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
750 WLog_Print(h264->log, WLOG_ERROR,
751 "YUV buffer not initialized! check your decoder settings");
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)
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;
776 const int status = log_decompress(h264, pSrcData, SrcSize, rects, nrRects);
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))
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,
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)
805 double tmp = size + *count * old;
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)
819 avc444_frame_type chroma =
820 (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
822 if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
825 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects))
827 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect))
834 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
835 nDstWidth, nDstHeight, regionRects, numRegionRects,
838 else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
839 nDstStep, nDstWidth, nDstHeight, auxRegionRects,
840 numAuxRegionRect, chroma))
848 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
849 nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
857 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
858 nDstWidth, nDstHeight, regionRects, numRegionRects,
870#if defined(AVC444_FRAME_STAT)
875 op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
879 op2sum = avg(&op2, op2sum, SrcSize);
883 op3sum = avg(&op3, op3sum, SrcSize);
890 WLog_Print(h264->log, WLOG_INFO,
891 "luma=%" PRIu64
" [avg=%lf] chroma=%" PRIu64
" [avg=%lf] combined=%" PRIu64
893 op1, op1sum, op2, op2sum, op3, op3sum);
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;
903static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED PINIT_ONCE once,
904 WINPR_ATTR_UNUSED PVOID param,
905 WINPR_ATTR_UNUSED PVOID* context)
909#ifdef WITH_MEDIACODEC
911 subSystems[i] = &g_Subsystem_mediacodec;
915#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
917 subSystems[i] = &g_Subsystem_MF;
921#ifdef WITH_APPLE_VIDEOTOOLBOX
923 subSystems[i] = &g_Subsystem_VideoToolbox;
929 subSystems[i] = &g_Subsystem_OpenH264;
933#ifdef WITH_VIDEO_FFMPEG
935 subSystems[i] = &g_Subsystem_libavcodec;
939#ifdef WITH_H264_WEBCODECS
941 subSystems[i] = &g_Subsystem_WebCodecs;
949static BOOL h264_context_init(H264_CONTEXT* h264)
954 h264->subsystem =
nullptr;
955 h264->yuv444 = FALSE;
956 if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems,
nullptr,
nullptr))
959 for (
size_t i = 0; i < MAX_SUBSYSTEMS; i++)
961 const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
963 if (!subsystem || !subsystem->Init)
966 if (subsystem->Init(h264))
968 h264->subsystem = subsystem;
976static void h264_buffers_free(H264_CONTEXT* h264)
979 for (
size_t x = 0; x < 3; x++)
981 if (h264->Compressor)
983 winpr_aligned_free(h264->pYUVData[x]);
984 winpr_aligned_free(h264->pOldYUVData[x]);
986 winpr_aligned_free(h264->pYUV444Data[x]);
987 winpr_aligned_free(h264->pOldYUV444Data[x]);
989 h264->pYUVData[x] =
nullptr;
990 h264->pOldYUVData[x] =
nullptr;
991 h264->pYUV444Data[x] =
nullptr;
992 h264->pOldYUV444Data[x] =
nullptr;
994 winpr_aligned_free(h264->lumaData);
995 h264->lumaData =
nullptr;
998BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
1003 if (width > UINT16_MAX)
1005 WLog_Print(h264->log, WLOG_ERROR,
"h264::width %" PRIu32
" > %d, aborting", width,
1009 if (height > UINT16_MAX)
1011 WLog_Print(h264->log, WLOG_ERROR,
"h264::height %" PRIu32
" > %d, aborting", height,
1016 if (h264->subsystem && h264->subsystem->Uninit)
1017 h264->subsystem->Uninit(h264);
1019 h264_buffers_free(h264);
1020 h264->width = width;
1021 h264->height = height;
1023 if (!h264_context_init(h264))
1026 return yuv_context_reset(h264->yuv, width, height);
1029H264_CONTEXT* h264_context_new(BOOL Compressor)
1031 return h264_context_new_ex(Compressor, FALSE, FALSE);
1034H264_CONTEXT* h264_context_new_ex(BOOL Compressor, BOOL hevc, BOOL hevc444)
1036 H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1,
sizeof(H264_CONTEXT));
1040 h264->log = WLog_Get(TAG);
1046 h264->hevc444 = hevc444;
1047 h264->Compressor = Compressor;
1051 h264->BitRate = 1000000;
1052 h264->FrameRate = 30;
1055 if (!h264_context_init(h264))
1058 h264->yuv = yuv_context_new(Compressor, 0);
1065 WINPR_PRAGMA_DIAG_PUSH
1066 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1067 h264_context_free(h264);
1068 WINPR_PRAGMA_DIAG_POP
1072void h264_context_free(H264_CONTEXT* h264)
1077 if (h264->subsystem)
1079 WINPR_ASSERT(h264->subsystem->Uninit);
1080 h264->subsystem->Uninit(h264);
1083 h264_buffers_free(h264);
1085 yuv_context_free(h264->yuv);
1094 free(meta->quantQualityVals);
1095 free(meta->regionRects);
1099BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value)
1104 case H264_CONTEXT_OPTION_BITRATE:
1105 h264->BitRate = value;
1107 case H264_CONTEXT_OPTION_FRAMERATE:
1108 h264->FrameRate = value;
1110 case H264_CONTEXT_OPTION_RATECONTROL:
1114 case H264_RATECONTROL_VBR:
1115 h264->RateControlMode = H264_RATECONTROL_VBR;
1117 case H264_RATECONTROL_CQP:
1118 h264->RateControlMode = H264_RATECONTROL_CQP;
1121 WLog_Print(h264->log, WLOG_WARN,
1122 "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
"]",
1128 case H264_CONTEXT_OPTION_QP:
1131 case H264_CONTEXT_OPTION_USAGETYPE:
1132 h264->UsageType = value;
1134 case H264_CONTEXT_OPTION_HW_ACCEL:
1135#ifdef WITH_APPLE_VIDEOTOOLBOX
1138 if (h264->subsystem == &g_Subsystem_VideoToolbox)
1141 if (h264->hwAccel == value)
1143 h264->hwAccel = (value);
1144 IFCALL(h264->subsystem->Uninit, h264);
1145 return IFCALLRESULT(TRUE, h264->subsystem->Init, h264);
1146 case H264_CONTEXT_OPTION_HEVC:
1148 const BOOL hevc = value != 0;
1149 if (h264->hevc == hevc)
1152 if (h264->subsystem)
1153 IFCALL(h264->subsystem->Uninit, h264);
1155 if (h264_context_init(h264))
1157 WLog_Print(h264->log, WLOG_WARN,
"no H264 subsystem supports %s",
1158 hevc ?
"HEVC" :
"H264");
1161 case H264_CONTEXT_OPTION_HEVC444:
1163 const BOOL hevc444 = value != 0;
1164 if (h264->hevc444 == hevc444)
1166 if (hevc444 && !h264->hevc)
1168 WLog_Print(h264->log, WLOG_WARN,
"HEVC 4:4:4 needs H264_CONTEXT_OPTION_HEVC");
1171 if (h264->subsystem)
1172 IFCALL(h264->subsystem->Uninit, h264);
1173 h264->hevc444 = hevc444;
1174 if (h264_context_init(h264))
1176 WLog_Print(h264->log, WLOG_WARN,
"no H264 subsystem supports HEVC %s",
1177 hevc444 ?
"4:4:4" :
"4:2:0");
1181 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",
1187UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
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:
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;
1209 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",
1215static BOOL codecSupportStatus[33][2];
1216static INIT_ONCE codecSupportStatusOnce = INIT_ONCE_STATIC_INIT;
1218static size_t codecFlagToIndex(FreeRDP_CodecFlags codec)
1220 for (
size_t index = 0; index < 32; index++)
1222 const size_t mask = 1ull << index;
1226 return ARRAYSIZE(codecSupportStatus) - 1;
1229static void setCodecSupportStatus(FreeRDP_CodecFlags codec, BOOL decoder, BOOL encoder)
1231 const size_t index = codecFlagToIndex(codec);
1232 codecSupportStatus[index][0] = decoder;
1233 codecSupportStatus[index][1] = encoder;
1236#if defined(WITH_GFX_AV1)
1238static BOOL test_av1(BOOL Compressor, UINT32 flags)
1240 FREERDP_AV1_CONTEXT* av1 = freerdp_av1_context_new(Compressor);
1244 if (flags == FREERDP_CODEC_AV1_I444)
1246 BOOL decoder = TRUE;
1248 decoder = freerdp_av1_context_set_option(av1, FREERDP_AV1_CONTEXT_OPTION_PROFILE, profile);
1249 freerdp_av1_context_free(av1);
1254#if defined(WITH_GFX_AV2)
1256static BOOL test_av2(BOOL Compressor, UINT32 flags)
1258 FREERDP_AV2_CONTEXT* av2 = freerdp_av2_context_new(Compressor);
1262 if (flags == FREERDP_CODEC_AV2_I444)
1264 BOOL decoder = TRUE;
1266 decoder = freerdp_av2_context_set_option(av2, FREERDP_AV2_CONTEXT_OPTION_PROFILE, profile);
1267 freerdp_av2_context_free(av2);
1272#if defined(WITH_GFX_AZURE)
1274static BOOL test_hevc(BOOL Compressor, UINT32 flags)
1276 FREERDP_HEVC_CONTEXT* hevc = freerdp_hevc_context_new(Compressor);
1281 if (flags == FREERDP_CODEC_HEVC_I444)
1283 const BOOL decoder =
1284 freerdp_hevc_context_set_option(hevc, FREERDP_HEVC_CONTEXT_OPTION_PROFILE, profile);
1285 freerdp_hevc_context_free(hevc);
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)
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);
1302#if defined(WITH_GFX_H264)
1304 H264_CONTEXT* h264 = h264_context_new(FALSE);
1305 const BOOL decoder = h264 !=
nullptr;
1306 h264_context_free(h264);
1308 H264_CONTEXT* h264enc = h264_context_new(TRUE);
1309 const BOOL encoder = h264enc !=
nullptr;
1310 h264_context_free(h264enc);
1312 setCodecSupportStatus(FREERDP_CODEC_AVC420, decoder, encoder);
1313 setCodecSupportStatus(FREERDP_CODEC_AVC444, decoder, encoder);
1316#if defined(WITH_GFX_AV1)
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));
1324#if defined(WITH_GFX_AV2)
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));
1332#if defined(WITH_GFX_AZURE)
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));
1344BOOL freerdp_codec_supported(WINPR_ATTR_UNUSED rdpContext* context, FreeRDP_CodecFlags codec,
1347 if (!InitOnceExecuteOnce(&codecSupportStatusOnce, codec_support_status_init,
nullptr,
nullptr))
1349 size_t index = encoder ? 1 : 0;
1350 return codecSupportStatus[codecFlagToIndex(codec)][index];
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 ...