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>
37#define TAG FREERDP_TAG("codec")
40static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight);
43static BOOL yuv_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
46 UINT32 pheight = height;
53 const UINT32 pad = width % 16;
60 stride += 32 - stride % 16;
61 pheight += 32 - pheight % 16;
63 const size_t nPlanes = h264->hwAccel ? 2 : 3;
65 for (
size_t x = 0; x < nPlanes; x++)
67 if (!h264->pYUVData[x] || !h264->pOldYUVData[x])
76 if (isNull || (width != h264->width) || (height != h264->height) ||
77 (stride != h264->iStride[0]))
79 UINT32 iStride[3] = WINPR_C_ARRAY_INIT;
89 iStride[1] = (stride + 1) / 2;
90 iStride[2] = (stride + 1) / 2;
93 for (
size_t x = 0; x < nPlanes; x++)
95 BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], iStride[x], pheight, 16);
96 BYTE* tmp2 = winpr_aligned_recalloc(h264->pOldYUVData[x], iStride[x], pheight, 16);
98 h264->pYUVData[x] = tmp1;
100 h264->pOldYUVData[x] = tmp2;
105 memcpy(h264->iStride, iStride,
sizeof(h264->iStride));
107 h264->height = height;
113BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
115 return yuv_ensure_buffer(h264, stride, width, height);
119static BOOL isRectValid(wLog* log,
size_t pos, UINT32 width, UINT32 height,
123 if ((rect->left > width) || (rect->right > width) || (rect->left >= rect->right) ||
124 (rect->top > height) || (rect->bottom > height) || (rect->top >= rect->bottom))
126 char buffer[64] = WINPR_C_ARRAY_INIT;
127 WLog_Print(log, WLOG_WARN,
128 "Rectangle %" PRIuz
" %s outside of bounding frame %" PRIu32
"x%" PRIu32, pos,
129 rectangle_to_string(rect, buffer,
sizeof(buffer)), width, height);
137static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height,
const RECTANGLE_16* rects,
140 WINPR_ASSERT(rects || (count == 0));
141 for (
size_t x = 0; x < count; x++)
144 if (!isRectValid(log, x, width, height, rect))
151static int log_decompress(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize,
155 WINPR_ASSERT(h264->subsystem);
156 WINPR_ASSERT(h264->subsystem->Decompress);
158 const int status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
161 WLog_Print(h264->log, WLOG_WARN,
"H264 decompress failed with %d", status);
171 if (!areRectsValid(h264->log, h264->YUVWidth, h264->YUVHeight, rects, nrRects))
173 if (!areRectsValid(h264->log, h264->width, h264->height, rects, nrRects))
178INT32 avc420_decompress(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
179 DWORD DstFormat, UINT32 nDstStep, WINPR_ATTR_UNUSED UINT32 nDstWidth,
180 WINPR_ATTR_UNUSED UINT32 nDstHeight,
const RECTANGLE_16* regionRects,
181 UINT32 numRegionRects)
184 const BYTE* pYUVData[3];
186 if (!h264 || h264->Compressor)
189 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects))
192 status = log_decompress(h264, pSrcData, SrcSize, regionRects, numRegionRects);
200 pYUVData[0] = h264->pYUVData[0];
201 pYUVData[1] = h264->pYUVData[1];
202 pYUVData[2] = h264->pYUVData[2];
203 if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->YUVHeight, DstFormat,
204 pDstData, nDstStep, regionRects, numRegionRects))
211static BOOL allocate_h264_metablock(UINT32 QP,
RECTANGLE_16* rectangles,
215 if (!meta || (QP > UINT8_MAX))
221 meta->regionRects = rectangles;
225 if (count > UINT32_MAX)
230 if (!meta->quantQualityVals || !meta->regionRects)
232 meta->numRegionRects = (UINT32)count;
233 for (
size_t x = 0; x < count; x++)
240 cur->qualityVal = 100 - (QP & 0x3F);
246static inline BOOL diff_tile(
const RECTANGLE_16* regionRect, BYTE* pYUVData[3],
247 BYTE* pOldYUVData[3], UINT32
const iStride[3])
251 if (!regionRect || !pYUVData || !pOldYUVData || !iStride)
253 size = regionRect->right - regionRect->left;
254 if (regionRect->right > iStride[0])
256 if (regionRect->right / 2u > iStride[1])
258 if (regionRect->right / 2u > iStride[2])
261 for (
size_t y = regionRect->top; y < regionRect->bottom; y++)
263 const BYTE* cur0 = &pYUVData[0][y * iStride[0]];
264 const BYTE* old0 = &pOldYUVData[0][y * iStride[0]];
266 if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0)
273 const size_t uvLeft = regionRect->left / 2u;
274 const size_t uvSize = (regionRect->right + 1u) / 2u - uvLeft;
275 for (
size_t y = regionRect->top / 2u; y < (regionRect->bottom + 1u) / 2u; y++)
277 const BYTE* cur1 = &pYUVData[1][y * iStride[1]];
278 const BYTE* cur2 = &pYUVData[2][y * iStride[2]];
279 const BYTE* old1 = &pOldYUVData[1][y * iStride[1]];
280 const BYTE* old2 = &pOldYUVData[2][y * iStride[2]];
282 if (memcmp(&cur1[uvLeft], &old1[uvLeft], uvSize) != 0)
284 if (memcmp(&cur2[uvLeft], &old2[uvLeft], uvSize) != 0)
297static inline BOOL diff_tile_avc444v2_aux(
const RECTANGLE_16* tile, UINT32 width, BYTE* pYUVData[3],
298 BYTE* pOldYUVData[3], UINT32
const iStride[3])
300 if (!tile || !pYUVData || !pOldYUVData || !iStride || (tile->right <= tile->left))
303 const size_t l2 = tile->left / 2u;
304 const size_t n2 = (tile->right + 1u) / 2u - l2;
305 const size_t h2 = width / 2u;
306 const size_t l4 = tile->left / 4u;
307 const size_t n4 = (tile->right + 3u) / 4u - l4;
308 const size_t h4 = width / 4u;
310 if ((h2 + l2 + n2 > iStride[0]) || (h4 + l4 + n4 > iStride[1]) || (h4 + l4 + n4 > iStride[2]))
313 for (
size_t y = tile->top; y < tile->bottom; y++)
315 const BYTE* cur = &pYUVData[0][y * iStride[0]];
316 const BYTE* old = &pOldYUVData[0][y * iStride[0]];
317 if ((memcmp(&cur[l2], &old[l2], n2) != 0) ||
318 (memcmp(&cur[h2 + l2], &old[h2 + l2], n2) != 0))
322 for (
size_t y = tile->top / 2u; y < (tile->bottom + 1u) / 2u; y++)
324 for (
size_t p = 1; p < 3; p++)
326 const BYTE* cur = &pYUVData[p][y * iStride[p]];
327 const BYTE* old = &pOldYUVData[p][y * iStride[p]];
328 if ((memcmp(&cur[l4], &old[l4], n4) != 0) ||
329 (memcmp(&cur[h4 + l4], &old[h4 + l4], n4) != 0))
337static BOOL detect_changes(BOOL firstFrameDone,
const UINT32 QP,
const RECTANGLE_16* regionRect,
338 BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32
const iStride[3],
346 if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta)
349 wc = (regionRect->right - regionRect->left) / 64 + 1;
350 hc = (regionRect->bottom - regionRect->top) / 64 + 1;
356 rectangles[0] = *regionRect;
361 for (
size_t y = 0; y < regionRect->bottom - regionRect->top; y += 64)
363 for (
size_t x = 0; x < regionRect->right - regionRect->left; x += 64)
366 rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x);
367 rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y);
369 (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right));
371 (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom));
374 ? diff_tile_avc444v2_aux(&rect, v2AuxWidth, pYUVData, pOldYUVData, iStride)
375 : diff_tile(&rect, pYUVData, pOldYUVData, iStride);
377 rectangles[count++] = rect;
381 if (!allocate_h264_metablock(QP, rectangles, meta, count))
386INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth,
387 UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3])
389 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
392 if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight))
395 for (
size_t x = 0; x < 3; x++)
397 YUVData[x] = h264->pYUVData[x];
398 stride[x] = h264->iStride[x];
404INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
406 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
409 const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] };
411 return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
423 const UINT32 right = MIN(width, ((UINT32)rect->right + 15u) & ~15u);
424 const UINT32 bottom = MIN(height, ((UINT32)rect->bottom + 15u) & ~15u);
425 const RECTANGLE_16 aligned = { (UINT16)(rect->left & ~15u), (UINT16)(rect->top & ~15u),
426 (UINT16)MAX(rect->right, right),
427 (UINT16)MAX(rect->bottom, bottom) };
435static void h264_keep_last_frame(
const RECTANGLE_16* region, UINT32 width, UINT32 height,
436 BYTE* dst[3], BYTE*
const src[3],
const size_t size[3])
438 WINPR_ASSERT(region);
439 if ((region->left == 0) && (region->top == 0) && (region->right >= width) &&
440 (region->bottom >= height))
443 for (
size_t x = 0; x < 3; x++)
445 if (dst[x] && src[x] && (size[x] > 0))
446 memcpy(dst[x], src[x], size[x]);
451static void h264_yuv420_plane_sizes(
const H264_CONTEXT* h264,
size_t size[3])
453 const size_t rows = h264->height + 32u - h264->height % 16u;
454 for (
size_t x = 0; x < 3; x++)
455 size[x] = 1ull * h264->iStride[x] * rows;
458INT32 avc420_compress(H264_CONTEXT* h264,
const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
459 UINT32 nSrcWidth, UINT32 nSrcHeight,
const RECTANGLE_16* regionRect,
463 BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT;
464 const BYTE* pcYUVData[3] = WINPR_C_ARRAY_INIT;
465 BYTE* pOldYUVData[3] = WINPR_C_ARRAY_INIT;
467 if (!h264 || !regionRect || !meta || !h264->Compressor)
470 if (!h264->subsystem->Compress)
473 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
476 if (h264->encodingBuffer)
478 for (
size_t x = 0; x < 3; x++)
480 pYUVData[x] = h264->pYUVData[x];
481 pOldYUVData[x] = h264->pOldYUVData[x];
486 for (
size_t x = 0; x < 3; x++)
488 pYUVData[x] = h264->pOldYUVData[x];
489 pOldYUVData[x] = h264->pYUVData[x];
492 h264->encodingBuffer = !h264->encodingBuffer;
494 size_t size420[3] = WINPR_C_ARRAY_INIT;
495 h264_yuv420_plane_sizes(h264, size420);
496 h264_keep_last_frame(regionRect, nSrcWidth, nSrcHeight, pYUVData, pOldYUVData, size420);
499 const RECTANGLE_16 aligned = h264_align_region(regionRect, nSrcWidth, nSrcHeight);
500 if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData,
504 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData,
505 h264->iStride, 0, meta))
508 if (meta->numRegionRects == 0)
514 for (
size_t x = 0; x < 3; x++)
515 pcYUVData[x] = pYUVData[x];
517 rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
519 h264->firstLumaFrameDone = TRUE;
523 free_h264_metablock(meta);
527INT32 avc444_compress(H264_CONTEXT* h264,
const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
528 UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version,
const RECTANGLE_16* region,
529 BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData,
534 BYTE* coded =
nullptr;
535 UINT32 codedSize = 0;
536 BYTE** pYUV444Data =
nullptr;
537 BYTE** pOldYUV444Data =
nullptr;
538 BYTE** pYUVData =
nullptr;
539 BYTE** pOldYUVData =
nullptr;
541 if (!h264 || !h264->Compressor)
544 if (!h264->subsystem->Compress)
547 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
550 if (!avc444_ensure_buffer(h264, nSrcHeight))
553 if (h264->encodingBuffer)
555 pYUV444Data = h264->pOldYUV444Data;
556 pOldYUV444Data = h264->pYUV444Data;
557 pYUVData = h264->pOldYUVData;
558 pOldYUVData = h264->pYUVData;
562 pYUV444Data = h264->pYUV444Data;
563 pOldYUV444Data = h264->pOldYUV444Data;
564 pYUVData = h264->pYUVData;
565 pOldYUVData = h264->pOldYUVData;
567 h264->encodingBuffer = !h264->encodingBuffer;
569 size_t size420[3] = WINPR_C_ARRAY_INIT;
570 size_t size444[3] = WINPR_C_ARRAY_INIT;
571 h264_yuv420_plane_sizes(h264, size420);
572 for (
size_t x = 0; x < 3; x++)
573 size444[x] = h264->iYUV444Size[x];
574 h264_keep_last_frame(region, nSrcWidth, nSrcHeight, pYUVData, pOldYUVData, size420);
575 h264_keep_last_frame(region, nSrcWidth, nSrcHeight, pYUV444Data, pOldYUV444Data, size444);
578 const RECTANGLE_16 aligned = h264_align_region(region, nSrcWidth, nSrcHeight);
579 if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride,
580 pYUV444Data, pYUVData, &aligned, 1))
583 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUV444Data, pOldYUV444Data,
584 h264->iStride, 0, meta))
587 if (!detect_changes(h264->firstChromaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData,
588 h264->iStride, (version == 2) ? h264->width : 0, auxMeta))
597 if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0))
599 else if (meta->numRegionRects > 0)
601 else if (auxMeta->numRegionRects > 0)
605 WLog_Print(h264->log, WLOG_TRACE,
"no changes detected for luma or chroma frame");
610 if ((*op == 0) || (*op == 1))
612 const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] };
614 if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0)
616 h264->firstLumaFrameDone = TRUE;
617 memcpy(h264->lumaData, coded, codedSize);
618 *ppDstData = h264->lumaData;
619 *pDstSize = codedSize;
622 if ((*op == 0) || (*op == 2))
624 const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] };
626 if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0)
628 h264->firstChromaFrameDone = TRUE;
629 *ppAuxDstData = coded;
630 *pAuxDstSize = codedSize;
637 free_h264_metablock(meta);
638 free_h264_metablock(auxMeta);
643BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight)
647 const UINT32* piMainStride = h264->iStride;
648 UINT32* piDstSize = h264->iYUV444Size;
649 UINT32* piDstStride = h264->iYUV444Stride;
650 BYTE** ppYUVDstData = h264->pYUV444Data;
651 BYTE** ppOldYUVDstData = h264->pOldYUV444Data;
653 nDstHeight = MAX(h264->height, nDstHeight);
654 const UINT32 pad = nDstHeight % 16;
655 UINT32 padDstHeight = nDstHeight;
658 padDstHeight += 16 - pad;
663 if ((piMainStride[0] == 0) || (padDstHeight == 0))
666 const uint64_t dstsize = 1ull * piMainStride[0] * padDstHeight;
667 if (dstsize > UINT32_MAX)
670 if ((piMainStride[0] != piDstStride[0]) || (piDstSize[0] != dstsize))
672 const UINT32 size = WINPR_ASSERTING_INT_CAST(UINT32, dstsize);
674 for (UINT32 x = 0; x < 3; x++)
676 BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], size, 1, 16);
679 ppYUVDstData[x] = tmp1;
680 BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], size, 1, 16);
683 ppOldYUVDstData[x] = tmp2;
687 BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, size, 4, 16);
690 h264->lumaData = tmp;
695 for (UINT32 x = 0; x < 3; x++)
697 piDstStride[x] = piMainStride[0];
702 for (UINT32 x = 0; x < 3; x++)
704 if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
706 WLog_Print(h264->log, WLOG_ERROR,
707 "YUV buffer not initialized! check your decoder settings");
721static BOOL avc444_process_rects(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize,
722 BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
723 WINPR_ATTR_UNUSED UINT32 nDstWidth, UINT32 nDstHeight,
724 const RECTANGLE_16* rects, UINT32 nrRects, avc444_frame_type type)
726 const BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT;
727 BYTE* pYUVDstData[3] = WINPR_C_ARRAY_INIT;
728 UINT32* piDstStride = h264->iYUV444Stride;
729 BYTE** ppYUVDstData = h264->pYUV444Data;
730 const UINT32* piStride = h264->iStride;
732 const int status = log_decompress(h264, pSrcData, SrcSize, rects, nrRects);
738 pYUVData[0] = h264->pYUVData[0];
739 pYUVData[1] = h264->pYUVData[1];
740 pYUVData[2] = h264->pYUVData[2];
741 if (!avc444_ensure_buffer(h264, nDstHeight))
744 pYUVDstData[0] = ppYUVDstData[0];
745 pYUVDstData[1] = ppYUVDstData[1];
746 pYUVDstData[2] = ppYUVDstData[2];
747 return (yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->YUVHeight,
748 pYUVDstData, piDstStride, DstFormat, pDstData, nDstStep, rects,
752#if defined(AVC444_FRAME_STAT)
753static UINT64 op1 = 0;
754static double op1sum = 0;
755static UINT64 op2 = 0;
756static double op2sum = 0;
757static UINT64 op3 = 0;
758static double op3sum = 0;
759static double avg(UINT64* count,
double old,
double size)
761 double tmp = size + *count * old;
768INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op,
const RECTANGLE_16* regionRects,
769 UINT32 numRegionRects,
const BYTE* pSrcData, UINT32 SrcSize,
770 const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
771 const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat,
772 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId)
775 avc444_frame_type chroma =
776 (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
778 if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
781 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects))
783 if (!areRectsValid(h264->log, nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect))
790 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
791 nDstWidth, nDstHeight, regionRects, numRegionRects,
794 else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
795 nDstStep, nDstWidth, nDstHeight, auxRegionRects,
796 numAuxRegionRect, chroma))
804 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
805 nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
813 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
814 nDstWidth, nDstHeight, regionRects, numRegionRects,
826#if defined(AVC444_FRAME_STAT)
831 op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
835 op2sum = avg(&op2, op2sum, SrcSize);
839 op3sum = avg(&op3, op3sum, SrcSize);
846 WLog_Print(h264->log, WLOG_INFO,
847 "luma=%" PRIu64
" [avg=%lf] chroma=%" PRIu64
" [avg=%lf] combined=%" PRIu64
849 op1, op1sum, op2, op2sum, op3, op3sum);
854#define MAX_SUBSYSTEMS 10
855static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT;
856static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = WINPR_C_ARRAY_INIT;
859static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED
PINIT_ONCE once,
860 WINPR_ATTR_UNUSED PVOID param,
861 WINPR_ATTR_UNUSED PVOID* context)
865#ifdef WITH_MEDIACODEC
867 subSystems[i] = &g_Subsystem_mediacodec;
871#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
873 subSystems[i] = &g_Subsystem_MF;
877#ifdef WITH_APPLE_VIDEOTOOLBOX
879 subSystems[i] = &g_Subsystem_VideoToolbox;
885 subSystems[i] = &g_Subsystem_OpenH264;
889#ifdef WITH_VIDEO_FFMPEG
891 subSystems[i] = &g_Subsystem_libavcodec;
895#ifdef WITH_H264_WEBCODECS
897 subSystems[i] = &g_Subsystem_WebCodecs;
905static BOOL h264_context_init(H264_CONTEXT* h264)
910 h264->subsystem =
nullptr;
911 if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems,
nullptr,
nullptr))
914 for (
size_t i = 0; i < MAX_SUBSYSTEMS; i++)
916 const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
918 if (!subsystem || !subsystem->Init)
921 if (subsystem->Init(h264))
923 h264->subsystem = subsystem;
931static void h264_buffers_free(H264_CONTEXT* h264)
934 for (
size_t x = 0; x < 3; x++)
936 if (h264->Compressor)
938 winpr_aligned_free(h264->pYUVData[x]);
939 winpr_aligned_free(h264->pOldYUVData[x]);
941 winpr_aligned_free(h264->pYUV444Data[x]);
942 winpr_aligned_free(h264->pOldYUV444Data[x]);
944 h264->pYUVData[x] =
nullptr;
945 h264->pOldYUVData[x] =
nullptr;
946 h264->pYUV444Data[x] =
nullptr;
947 h264->pOldYUV444Data[x] =
nullptr;
949 winpr_aligned_free(h264->lumaData);
950 h264->lumaData =
nullptr;
953BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
958 if (width > UINT16_MAX)
960 WLog_Print(h264->log, WLOG_ERROR,
"h264::width %" PRIu32
" > %d, aborting", width,
964 if (height > UINT16_MAX)
966 WLog_Print(h264->log, WLOG_ERROR,
"h264::height %" PRIu32
" > %d, aborting", height,
971 if (h264->subsystem && h264->subsystem->Uninit)
972 h264->subsystem->Uninit(h264);
974 h264_buffers_free(h264);
976 h264->height = height;
978 if (!h264_context_init(h264))
981 return yuv_context_reset(h264->yuv, width, height);
984H264_CONTEXT* h264_context_new(BOOL Compressor)
986 H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1,
sizeof(H264_CONTEXT));
990 h264->log = WLog_Get(TAG);
995 h264->Compressor = Compressor;
999 h264->BitRate = 1000000;
1000 h264->FrameRate = 30;
1003 if (!h264_context_init(h264))
1006 h264->yuv = yuv_context_new(Compressor, 0);
1013 WINPR_PRAGMA_DIAG_PUSH
1014 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1015 h264_context_free(h264);
1016 WINPR_PRAGMA_DIAG_POP
1020void h264_context_free(H264_CONTEXT* h264)
1025 if (h264->subsystem)
1027 WINPR_ASSERT(h264->subsystem->Uninit);
1028 h264->subsystem->Uninit(h264);
1031 h264_buffers_free(h264);
1033 yuv_context_free(h264->yuv);
1042 free(meta->quantQualityVals);
1043 free(meta->regionRects);
1047BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value)
1052 case H264_CONTEXT_OPTION_BITRATE:
1053 h264->BitRate = value;
1055 case H264_CONTEXT_OPTION_FRAMERATE:
1056 h264->FrameRate = value;
1058 case H264_CONTEXT_OPTION_RATECONTROL:
1062 case H264_RATECONTROL_VBR:
1063 h264->RateControlMode = H264_RATECONTROL_VBR;
1065 case H264_RATECONTROL_CQP:
1066 h264->RateControlMode = H264_RATECONTROL_CQP;
1069 WLog_Print(h264->log, WLOG_WARN,
1070 "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
"]",
1076 case H264_CONTEXT_OPTION_QP:
1079 case H264_CONTEXT_OPTION_USAGETYPE:
1080 h264->UsageType = value;
1082 case H264_CONTEXT_OPTION_HW_ACCEL:
1083#ifdef WITH_APPLE_VIDEOTOOLBOX
1086 if (h264->subsystem == &g_Subsystem_VideoToolbox)
1089 if (h264->hwAccel == value)
1091 h264->hwAccel = (value);
1092 IFCALL(h264->subsystem->Uninit, h264);
1093 return IFCALLRESULT(TRUE, h264->subsystem->Init, h264);
1095 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",
1101UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
1106 case H264_CONTEXT_OPTION_BITRATE:
1107 return h264->BitRate;
1108 case H264_CONTEXT_OPTION_FRAMERATE:
1109 return h264->FrameRate;
1110 case H264_CONTEXT_OPTION_RATECONTROL:
1111 return h264->RateControlMode;
1112 case H264_CONTEXT_OPTION_QP:
1114 case H264_CONTEXT_OPTION_USAGETYPE:
1115 return h264->UsageType;
1116 case H264_CONTEXT_OPTION_HW_ACCEL:
1117 return h264->hwAccel;
1119 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",