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>
36#define TAG FREERDP_TAG("codec")
38static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight);
40static BOOL yuv_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
43 UINT32 pheight = height;
52 stride += 16 - stride % 16;
54 if (pheight % 16 != 0)
55 pheight += 16 - pheight % 16;
57 const size_t nPlanes = h264->hwAccel ? 2 : 3;
59 for (
size_t x = 0; x < nPlanes; x++)
61 if (!h264->pYUVData[x] || !h264->pOldYUVData[x])
70 if (isNull || (width != h264->width) || (height != h264->height) ||
71 (stride != h264->iStride[0]))
75 h264->iStride[0] = stride;
76 h264->iStride[1] = stride;
81 h264->iStride[0] = stride;
82 h264->iStride[1] = (stride + 1) / 2;
83 h264->iStride[2] = (stride + 1) / 2;
86 for (
size_t x = 0; x < nPlanes; x++)
88 BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], h264->iStride[x], pheight, 16);
90 winpr_aligned_recalloc(h264->pOldYUVData[x], h264->iStride[x], pheight, 16);
92 h264->pYUVData[x] = tmp1;
94 h264->pOldYUVData[x] = tmp2;
99 h264->height = height;
105BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
107 return yuv_ensure_buffer(h264, stride, width, height);
110static BOOL isRectValid(UINT32 width, UINT32 height,
const RECTANGLE_16* rect)
113 if (rect->left > width)
115 if (rect->right > width)
117 if (rect->left >= rect->right)
119 if (rect->top > height)
121 if (rect->bottom > height)
123 if (rect->top >= rect->bottom)
128static BOOL areRectsValid(UINT32 width, UINT32 height,
const RECTANGLE_16* rects, UINT32 count)
130 WINPR_ASSERT(rects || (count == 0));
131 for (
size_t x = 0; x < count; x++)
134 if (!isRectValid(width, height, rect))
140INT32 avc420_decompress(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
141 DWORD DstFormat, UINT32 nDstStep, WINPR_ATTR_UNUSED UINT32 nDstWidth,
142 WINPR_ATTR_UNUSED UINT32 nDstHeight,
const RECTANGLE_16* regionRects,
143 UINT32 numRegionRects)
146 const BYTE* pYUVData[3];
148 if (!h264 || h264->Compressor)
151 if (!areRectsValid(nDstWidth, nDstHeight, regionRects, numRegionRects))
154 status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
162 pYUVData[0] = h264->pYUVData[0];
163 pYUVData[1] = h264->pYUVData[1];
164 pYUVData[2] = h264->pYUVData[2];
165 if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->height, DstFormat,
166 pDstData, nDstStep, regionRects, numRegionRects))
172static BOOL allocate_h264_metablock(UINT32 QP,
RECTANGLE_16* rectangles,
176 if (!meta || (QP > UINT8_MAX))
182 meta->regionRects = rectangles;
186 if (count > UINT32_MAX)
191 if (!meta->quantQualityVals || !meta->regionRects)
193 meta->numRegionRects = (UINT32)count;
194 for (
size_t x = 0; x < count; x++)
201 cur->qualityVal = 100 - (QP & 0x3F);
206static inline BOOL diff_tile(
const RECTANGLE_16* regionRect, BYTE* pYUVData[3],
207 BYTE* pOldYUVData[3], UINT32
const iStride[3])
211 if (!regionRect || !pYUVData || !pOldYUVData || !iStride)
213 size = regionRect->right - regionRect->left;
214 if (regionRect->right > iStride[0])
216 if (regionRect->right / 2u > iStride[1])
218 if (regionRect->right / 2u > iStride[2])
221 for (
size_t y = regionRect->top; y < regionRect->bottom; y++)
223 const BYTE* cur0 = &pYUVData[0][y * iStride[0]];
224 const BYTE* cur1 = &pYUVData[1][y * iStride[1]];
225 const BYTE* cur2 = &pYUVData[2][y * iStride[2]];
226 const BYTE* old0 = &pOldYUVData[0][y * iStride[0]];
227 const BYTE* old1 = &pOldYUVData[1][y * iStride[1]];
228 const BYTE* old2 = &pOldYUVData[2][y * iStride[2]];
230 if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0)
232 if (memcmp(&cur1[regionRect->left / 2], &old1[regionRect->left / 2], size / 2) != 0)
234 if (memcmp(&cur2[regionRect->left / 2], &old2[regionRect->left / 2], size / 2) != 0)
240static BOOL detect_changes(BOOL firstFrameDone,
const UINT32 QP,
const RECTANGLE_16* regionRect,
241 BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32
const iStride[3],
249 if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta)
252 wc = (regionRect->right - regionRect->left) / 64 + 1;
253 hc = (regionRect->bottom - regionRect->top) / 64 + 1;
259 rectangles[0] = *regionRect;
264 for (
size_t y = regionRect->top; y < regionRect->bottom; y += 64)
266 for (
size_t x = regionRect->left; x < regionRect->right; x += 64)
269 rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x);
270 rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y);
272 (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right));
274 (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom));
275 if (diff_tile(&rect, pYUVData, pOldYUVData, iStride))
276 rectangles[count++] = rect;
280 if (!allocate_h264_metablock(QP, rectangles, meta, count))
285INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth,
286 UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3])
288 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
291 if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight))
294 for (
size_t x = 0; x < 3; x++)
296 YUVData[x] = h264->pYUVData[x];
297 stride[x] = h264->iStride[x];
303INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
305 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
308 const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] };
310 return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
313INT32 avc420_compress(H264_CONTEXT* h264,
const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
314 UINT32 nSrcWidth, UINT32 nSrcHeight,
const RECTANGLE_16* regionRect,
318 BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT;
319 const BYTE* pcYUVData[3] = WINPR_C_ARRAY_INIT;
320 BYTE* pOldYUVData[3] = WINPR_C_ARRAY_INIT;
322 if (!h264 || !regionRect || !meta || !h264->Compressor)
325 if (!h264->subsystem->Compress)
328 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
331 if (h264->encodingBuffer)
333 for (
size_t x = 0; x < 3; x++)
335 pYUVData[x] = h264->pYUVData[x];
336 pOldYUVData[x] = h264->pOldYUVData[x];
341 for (
size_t x = 0; x < 3; x++)
343 pYUVData[x] = h264->pOldYUVData[x];
344 pOldYUVData[x] = h264->pYUVData[x];
347 h264->encodingBuffer = !h264->encodingBuffer;
349 if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData,
353 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, regionRect, pYUVData, pOldYUVData,
354 h264->iStride, meta))
357 if (meta->numRegionRects == 0)
363 for (
size_t x = 0; x < 3; x++)
364 pcYUVData[x] = pYUVData[x];
366 rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
368 h264->firstLumaFrameDone = TRUE;
372 free_h264_metablock(meta);
376INT32 avc444_compress(H264_CONTEXT* h264,
const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
377 UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version,
const RECTANGLE_16* region,
378 BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData,
383 BYTE* coded =
nullptr;
384 UINT32 codedSize = 0;
385 BYTE** pYUV444Data =
nullptr;
386 BYTE** pOldYUV444Data =
nullptr;
387 BYTE** pYUVData =
nullptr;
388 BYTE** pOldYUVData =
nullptr;
390 if (!h264 || !h264->Compressor)
393 if (!h264->subsystem->Compress)
396 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
399 if (!avc444_ensure_buffer(h264, nSrcHeight))
402 if (h264->encodingBuffer)
404 pYUV444Data = h264->pOldYUV444Data;
405 pOldYUV444Data = h264->pYUV444Data;
406 pYUVData = h264->pOldYUVData;
407 pOldYUVData = h264->pYUVData;
411 pYUV444Data = h264->pYUV444Data;
412 pOldYUV444Data = h264->pOldYUV444Data;
413 pYUVData = h264->pYUVData;
414 pOldYUVData = h264->pOldYUVData;
416 h264->encodingBuffer = !h264->encodingBuffer;
418 if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride,
419 pYUV444Data, pYUVData, region, 1))
422 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, region, pYUV444Data, pOldYUV444Data,
423 h264->iStride, meta))
425 if (!detect_changes(h264->firstChromaFrameDone, h264->QP, region, pYUVData, pOldYUVData,
426 h264->iStride, auxMeta))
435 if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0))
437 else if (meta->numRegionRects > 0)
439 else if (auxMeta->numRegionRects > 0)
443 WLog_Print(h264->log, WLOG_TRACE,
"no changes detected for luma or chroma frame");
448 if ((*op == 0) || (*op == 1))
450 const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] };
452 if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0)
454 h264->firstLumaFrameDone = TRUE;
455 memcpy(h264->lumaData, coded, codedSize);
456 *ppDstData = h264->lumaData;
457 *pDstSize = codedSize;
460 if ((*op == 0) || (*op == 2))
462 const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] };
464 if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0)
466 h264->firstChromaFrameDone = TRUE;
467 *ppAuxDstData = coded;
468 *pAuxDstSize = codedSize;
475 free_h264_metablock(meta);
476 free_h264_metablock(auxMeta);
481static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight)
485 const UINT32* piMainStride = h264->iStride;
486 UINT32* piDstSize = h264->iYUV444Size;
487 UINT32* piDstStride = h264->iYUV444Stride;
488 BYTE** ppYUVDstData = h264->pYUV444Data;
489 BYTE** ppOldYUVDstData = h264->pOldYUV444Data;
491 nDstHeight = MAX(h264->height, nDstHeight);
492 const UINT32 pad = nDstHeight % 16;
493 UINT32 padDstHeight = nDstHeight;
496 padDstHeight += 16 - pad;
498 if ((piMainStride[0] == 0) || (padDstHeight == 0))
501 const uint64_t dstsize = 1ull * piMainStride[0] * padDstHeight;
502 if (dstsize > UINT32_MAX)
505 if ((piMainStride[0] != piDstStride[0]) || (piDstSize[0] != dstsize))
507 for (UINT32 x = 0; x < 3; x++)
509 piDstStride[x] = piMainStride[0];
511 const uint64_t dstride = 1ull * piDstStride[x] * padDstHeight;
512 if (dstride > UINT32_MAX)
515 piDstSize[x] = WINPR_ASSERTING_INT_CAST(UINT32, dstride);
516 if (piDstSize[x] == 0)
519 BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], piDstSize[x], 1, 16);
522 ppYUVDstData[x] = tmp1;
523 BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], piDstSize[x], 1, 16);
526 ppOldYUVDstData[x] = tmp2;
530 BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, piDstSize[0], 4, 16);
533 h264->lumaData = tmp;
537 for (UINT32 x = 0; x < 3; x++)
539 if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
541 WLog_Print(h264->log, WLOG_ERROR,
542 "YUV buffer not initialized! check your decoder settings");
555static BOOL avc444_process_rects(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize,
556 BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
557 WINPR_ATTR_UNUSED UINT32 nDstWidth, UINT32 nDstHeight,
558 const RECTANGLE_16* rects, UINT32 nrRects, avc444_frame_type type)
560 const BYTE* pYUVData[3];
561 BYTE* pYUVDstData[3];
562 UINT32* piDstStride = h264->iYUV444Stride;
563 BYTE** ppYUVDstData = h264->pYUV444Data;
564 const UINT32* piStride = h264->iStride;
566 if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0)
569 pYUVData[0] = h264->pYUVData[0];
570 pYUVData[1] = h264->pYUVData[1];
571 pYUVData[2] = h264->pYUVData[2];
572 if (!avc444_ensure_buffer(h264, nDstHeight))
575 pYUVDstData[0] = ppYUVDstData[0];
576 pYUVDstData[1] = ppYUVDstData[1];
577 pYUVDstData[2] = ppYUVDstData[2];
578 return (yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->height,
579 pYUVDstData, piDstStride, DstFormat, pDstData, nDstStep, rects,
583#if defined(AVC444_FRAME_STAT)
584static UINT64 op1 = 0;
585static double op1sum = 0;
586static UINT64 op2 = 0;
587static double op2sum = 0;
588static UINT64 op3 = 0;
589static double op3sum = 0;
590static double avg(UINT64* count,
double old,
double size)
592 double tmp = size + *count * old;
599INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op,
const RECTANGLE_16* regionRects,
600 UINT32 numRegionRects,
const BYTE* pSrcData, UINT32 SrcSize,
601 const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
602 const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat,
603 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId)
606 avc444_frame_type chroma =
607 (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
609 if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
612 if (!areRectsValid(nDstWidth, nDstHeight, regionRects, numRegionRects))
614 if (!areRectsValid(nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect))
621 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
622 nDstWidth, nDstHeight, regionRects, numRegionRects,
625 else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
626 nDstStep, nDstWidth, nDstHeight, auxRegionRects,
627 numAuxRegionRect, chroma))
635 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
636 nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
644 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
645 nDstWidth, nDstHeight, regionRects, numRegionRects,
657#if defined(AVC444_FRAME_STAT)
662 op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
666 op2sum = avg(&op2, op2sum, SrcSize);
670 op3sum = avg(&op3, op3sum, SrcSize);
677 WLog_Print(h264->log, WLOG_INFO,
678 "luma=%" PRIu64
" [avg=%lf] chroma=%" PRIu64
" [avg=%lf] combined=%" PRIu64
680 op1, op1sum, op2, op2sum, op3, op3sum);
685#define MAX_SUBSYSTEMS 10
686static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT;
687static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = WINPR_C_ARRAY_INIT;
689static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED
PINIT_ONCE once,
690 WINPR_ATTR_UNUSED PVOID param,
691 WINPR_ATTR_UNUSED PVOID* context)
695#ifdef WITH_MEDIACODEC
697 subSystems[i] = &g_Subsystem_mediacodec;
701#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
703 subSystems[i] = &g_Subsystem_MF;
709 subSystems[i] = &g_Subsystem_OpenH264;
713#ifdef WITH_VIDEO_FFMPEG
715 subSystems[i] = &g_Subsystem_libavcodec;
722static BOOL h264_context_init(H264_CONTEXT* h264)
727 h264->subsystem =
nullptr;
728 if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems,
nullptr,
nullptr))
731 for (
size_t i = 0; i < MAX_SUBSYSTEMS; i++)
733 const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
735 if (!subsystem || !subsystem->Init)
738 if (subsystem->Init(h264))
740 h264->subsystem = subsystem;
748BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
754 h264->height = height;
756 if (h264->subsystem && h264->subsystem->Uninit)
757 h264->subsystem->Uninit(h264);
758 if (!h264_context_init(h264))
761 return yuv_context_reset(h264->yuv, width, height);
764H264_CONTEXT* h264_context_new(BOOL Compressor)
766 H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1,
sizeof(H264_CONTEXT));
770 h264->log = WLog_Get(TAG);
775 h264->Compressor = Compressor;
779 h264->BitRate = 1000000;
780 h264->FrameRate = 30;
783 if (!h264_context_init(h264))
786 h264->yuv = yuv_context_new(Compressor, 0);
793 WINPR_PRAGMA_DIAG_PUSH
794 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
795 h264_context_free(h264);
796 WINPR_PRAGMA_DIAG_POP
800void h264_context_free(H264_CONTEXT* h264)
806 WINPR_ASSERT(h264->subsystem->Uninit);
807 h264->subsystem->Uninit(h264);
810 for (
size_t x = 0; x < 3; x++)
812 if (h264->Compressor)
814 winpr_aligned_free(h264->pYUVData[x]);
815 winpr_aligned_free(h264->pOldYUVData[x]);
817 winpr_aligned_free(h264->pYUV444Data[x]);
818 winpr_aligned_free(h264->pOldYUV444Data[x]);
820 winpr_aligned_free(h264->lumaData);
822 yuv_context_free(h264->yuv);
832 free(meta->quantQualityVals);
833 free(meta->regionRects);
837BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value)
842 case H264_CONTEXT_OPTION_BITRATE:
843 h264->BitRate = value;
845 case H264_CONTEXT_OPTION_FRAMERATE:
846 h264->FrameRate = value;
848 case H264_CONTEXT_OPTION_RATECONTROL:
852 case H264_RATECONTROL_VBR:
853 h264->RateControlMode = H264_RATECONTROL_VBR;
855 case H264_RATECONTROL_CQP:
856 h264->RateControlMode = H264_RATECONTROL_CQP;
859 WLog_Print(h264->log, WLOG_WARN,
860 "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
"]",
866 case H264_CONTEXT_OPTION_QP:
869 case H264_CONTEXT_OPTION_USAGETYPE:
870 h264->UsageType = value;
872 case H264_CONTEXT_OPTION_HW_ACCEL:
873 h264->hwAccel = (value);
876 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",
882UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
887 case H264_CONTEXT_OPTION_BITRATE:
888 return h264->BitRate;
889 case H264_CONTEXT_OPTION_FRAMERATE:
890 return h264->FrameRate;
891 case H264_CONTEXT_OPTION_RATECONTROL:
892 return h264->RateControlMode;
893 case H264_CONTEXT_OPTION_QP:
895 case H264_CONTEXT_OPTION_USAGETYPE:
896 return h264->UsageType;
897 case H264_CONTEXT_OPTION_HW_ACCEL:
898 return h264->hwAccel;
900 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",