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;
87 h264->height = height;
89 for (
size_t x = 0; x < nPlanes; x++)
91 BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], h264->iStride[x], pheight, 16);
93 winpr_aligned_recalloc(h264->pOldYUVData[x], h264->iStride[x], pheight, 16);
95 h264->pYUVData[x] = tmp1;
97 h264->pOldYUVData[x] = tmp2;
106BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
108 return yuv_ensure_buffer(h264, stride, width, height);
111static BOOL isRectValid(UINT32 width, UINT32 height,
const RECTANGLE_16* rect)
114 if (rect->left > width)
116 if (rect->right > width)
118 if (rect->left >= rect->right)
120 if (rect->top > height)
122 if (rect->bottom > height)
124 if (rect->top >= rect->bottom)
129static BOOL areRectsValid(UINT32 width, UINT32 height,
const RECTANGLE_16* rects, UINT32 count)
131 WINPR_ASSERT(rects || (count == 0));
132 for (
size_t x = 0; x < count; x++)
135 if (!isRectValid(width, height, rect))
141INT32 avc420_decompress(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
142 DWORD DstFormat, UINT32 nDstStep, WINPR_ATTR_UNUSED UINT32 nDstWidth,
143 WINPR_ATTR_UNUSED UINT32 nDstHeight,
const RECTANGLE_16* regionRects,
144 UINT32 numRegionRects)
147 const BYTE* pYUVData[3];
149 if (!h264 || h264->Compressor)
152 if (!areRectsValid(nDstWidth, nDstHeight, regionRects, numRegionRects))
155 status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
163 pYUVData[0] = h264->pYUVData[0];
164 pYUVData[1] = h264->pYUVData[1];
165 pYUVData[2] = h264->pYUVData[2];
166 if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->height, DstFormat,
167 pDstData, nDstStep, regionRects, numRegionRects))
173static BOOL allocate_h264_metablock(UINT32 QP,
RECTANGLE_16* rectangles,
177 if (!meta || (QP > UINT8_MAX))
183 meta->regionRects = rectangles;
187 if (count > UINT32_MAX)
192 if (!meta->quantQualityVals || !meta->regionRects)
194 meta->numRegionRects = (UINT32)count;
195 for (
size_t x = 0; x < count; x++)
202 cur->qualityVal = 100 - (QP & 0x3F);
207static inline BOOL diff_tile(
const RECTANGLE_16* regionRect, BYTE* pYUVData[3],
208 BYTE* pOldYUVData[3], UINT32
const iStride[3])
212 if (!regionRect || !pYUVData || !pOldYUVData || !iStride)
214 size = regionRect->right - regionRect->left;
215 if (regionRect->right > iStride[0])
217 if (regionRect->right / 2u > iStride[1])
219 if (regionRect->right / 2u > iStride[2])
222 for (
size_t y = regionRect->top; y < regionRect->bottom; y++)
224 const BYTE* cur0 = &pYUVData[0][y * iStride[0]];
225 const BYTE* cur1 = &pYUVData[1][y * iStride[1]];
226 const BYTE* cur2 = &pYUVData[2][y * iStride[2]];
227 const BYTE* old0 = &pOldYUVData[0][y * iStride[0]];
228 const BYTE* old1 = &pOldYUVData[1][y * iStride[1]];
229 const BYTE* old2 = &pOldYUVData[2][y * iStride[2]];
231 if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0)
233 if (memcmp(&cur1[regionRect->left / 2], &old1[regionRect->left / 2], size / 2) != 0)
235 if (memcmp(&cur2[regionRect->left / 2], &old2[regionRect->left / 2], size / 2) != 0)
241static BOOL detect_changes(BOOL firstFrameDone,
const UINT32 QP,
const RECTANGLE_16* regionRect,
242 BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32
const iStride[3],
250 if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta)
253 wc = (regionRect->right - regionRect->left) / 64 + 1;
254 hc = (regionRect->bottom - regionRect->top) / 64 + 1;
260 rectangles[0] = *regionRect;
265 for (
size_t y = regionRect->top; y < regionRect->bottom; y += 64)
267 for (
size_t x = regionRect->left; x < regionRect->right; x += 64)
270 rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x);
271 rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y);
273 (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right));
275 (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom));
276 if (diff_tile(&rect, pYUVData, pOldYUVData, iStride))
277 rectangles[count++] = rect;
281 if (!allocate_h264_metablock(QP, rectangles, meta, count))
286INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth,
287 UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3])
289 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
292 if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight))
295 for (
size_t x = 0; x < 3; x++)
297 YUVData[x] = h264->pYUVData[x];
298 stride[x] = h264->iStride[x];
304INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
306 if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress)
309 const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] };
311 return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
314INT32 avc420_compress(H264_CONTEXT* h264,
const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
315 UINT32 nSrcWidth, UINT32 nSrcHeight,
const RECTANGLE_16* regionRect,
319 BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT;
320 const BYTE* pcYUVData[3] = WINPR_C_ARRAY_INIT;
321 BYTE* pOldYUVData[3] = WINPR_C_ARRAY_INIT;
323 if (!h264 || !regionRect || !meta || !h264->Compressor)
326 if (!h264->subsystem->Compress)
329 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
332 if (h264->encodingBuffer)
334 for (
size_t x = 0; x < 3; x++)
336 pYUVData[x] = h264->pYUVData[x];
337 pOldYUVData[x] = h264->pOldYUVData[x];
342 for (
size_t x = 0; x < 3; x++)
344 pYUVData[x] = h264->pOldYUVData[x];
345 pOldYUVData[x] = h264->pYUVData[x];
348 h264->encodingBuffer = !h264->encodingBuffer;
350 if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData,
354 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, regionRect, pYUVData, pOldYUVData,
355 h264->iStride, meta))
358 if (meta->numRegionRects == 0)
364 for (
size_t x = 0; x < 3; x++)
365 pcYUVData[x] = pYUVData[x];
367 rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
369 h264->firstLumaFrameDone = TRUE;
373 free_h264_metablock(meta);
377INT32 avc444_compress(H264_CONTEXT* h264,
const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
378 UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version,
const RECTANGLE_16* region,
379 BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData,
384 BYTE* coded =
nullptr;
385 UINT32 codedSize = 0;
386 BYTE** pYUV444Data =
nullptr;
387 BYTE** pOldYUV444Data =
nullptr;
388 BYTE** pYUVData =
nullptr;
389 BYTE** pOldYUVData =
nullptr;
391 if (!h264 || !h264->Compressor)
394 if (!h264->subsystem->Compress)
397 if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
400 if (!avc444_ensure_buffer(h264, nSrcHeight))
403 if (h264->encodingBuffer)
405 pYUV444Data = h264->pOldYUV444Data;
406 pOldYUV444Data = h264->pYUV444Data;
407 pYUVData = h264->pOldYUVData;
408 pOldYUVData = h264->pYUVData;
412 pYUV444Data = h264->pYUV444Data;
413 pOldYUV444Data = h264->pOldYUV444Data;
414 pYUVData = h264->pYUVData;
415 pOldYUVData = h264->pOldYUVData;
417 h264->encodingBuffer = !h264->encodingBuffer;
419 if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride,
420 pYUV444Data, pYUVData, region, 1))
423 if (!detect_changes(h264->firstLumaFrameDone, h264->QP, region, pYUV444Data, pOldYUV444Data,
424 h264->iStride, meta))
426 if (!detect_changes(h264->firstChromaFrameDone, h264->QP, region, pYUVData, pOldYUVData,
427 h264->iStride, auxMeta))
436 if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0))
438 else if (meta->numRegionRects > 0)
440 else if (auxMeta->numRegionRects > 0)
444 WLog_INFO(TAG,
"no changes detected for luma or chroma frame");
449 if ((*op == 0) || (*op == 1))
451 const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] };
453 if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0)
455 h264->firstLumaFrameDone = TRUE;
456 memcpy(h264->lumaData, coded, codedSize);
457 *ppDstData = h264->lumaData;
458 *pDstSize = codedSize;
461 if ((*op == 0) || (*op == 2))
463 const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] };
465 if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0)
467 h264->firstChromaFrameDone = TRUE;
468 *ppAuxDstData = coded;
469 *pAuxDstSize = codedSize;
476 free_h264_metablock(meta);
477 free_h264_metablock(auxMeta);
482static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight)
486 const UINT32* piMainStride = h264->iStride;
487 UINT32* piDstSize = h264->iYUV444Size;
488 UINT32* piDstStride = h264->iYUV444Stride;
489 BYTE** ppYUVDstData = h264->pYUV444Data;
490 BYTE** ppOldYUVDstData = h264->pOldYUV444Data;
492 nDstHeight = MAX(h264->height, nDstHeight);
493 const UINT32 pad = nDstHeight % 16;
494 UINT32 padDstHeight = nDstHeight;
497 padDstHeight += 16 - pad;
499 if ((piMainStride[0] != piDstStride[0]) ||
500 (piDstSize[0] != 1ull * piMainStride[0] * padDstHeight))
502 for (UINT32 x = 0; x < 3; x++)
504 piDstStride[x] = piMainStride[0];
505 piDstSize[x] = piDstStride[x] * padDstHeight;
507 if (piDstSize[x] == 0)
510 BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], piDstSize[x], 1, 16);
513 ppYUVDstData[x] = tmp1;
514 BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], piDstSize[x], 1, 16);
517 ppOldYUVDstData[x] = tmp2;
521 BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, piDstSize[0], 4, 16);
524 h264->lumaData = tmp;
528 for (UINT32 x = 0; x < 3; x++)
530 if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
532 WLog_Print(h264->log, WLOG_ERROR,
533 "YUV buffer not initialized! check your decoder settings");
546static BOOL avc444_process_rects(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize,
547 BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
548 WINPR_ATTR_UNUSED UINT32 nDstWidth, UINT32 nDstHeight,
549 const RECTANGLE_16* rects, UINT32 nrRects, avc444_frame_type type)
551 const BYTE* pYUVData[3];
552 BYTE* pYUVDstData[3];
553 UINT32* piDstStride = h264->iYUV444Stride;
554 BYTE** ppYUVDstData = h264->pYUV444Data;
555 const UINT32* piStride = h264->iStride;
557 if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0)
560 pYUVData[0] = h264->pYUVData[0];
561 pYUVData[1] = h264->pYUVData[1];
562 pYUVData[2] = h264->pYUVData[2];
563 if (!avc444_ensure_buffer(h264, nDstHeight))
566 pYUVDstData[0] = ppYUVDstData[0];
567 pYUVDstData[1] = ppYUVDstData[1];
568 pYUVDstData[2] = ppYUVDstData[2];
569 return (yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->height,
570 pYUVDstData, piDstStride, DstFormat, pDstData, nDstStep, rects,
574#if defined(AVC444_FRAME_STAT)
575static UINT64 op1 = 0;
576static double op1sum = 0;
577static UINT64 op2 = 0;
578static double op2sum = 0;
579static UINT64 op3 = 0;
580static double op3sum = 0;
581static double avg(UINT64* count,
double old,
double size)
583 double tmp = size + *count * old;
590INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op,
const RECTANGLE_16* regionRects,
591 UINT32 numRegionRects,
const BYTE* pSrcData, UINT32 SrcSize,
592 const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
593 const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat,
594 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId)
597 avc444_frame_type chroma =
598 (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
600 if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
603 if (!areRectsValid(nDstWidth, nDstHeight, regionRects, numRegionRects))
605 if (!areRectsValid(nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect))
612 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
613 nDstWidth, nDstHeight, regionRects, numRegionRects,
616 else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
617 nDstStep, nDstWidth, nDstHeight, auxRegionRects,
618 numAuxRegionRect, chroma))
626 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
627 nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
635 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
636 nDstWidth, nDstHeight, regionRects, numRegionRects,
648#if defined(AVC444_FRAME_STAT)
653 op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
657 op2sum = avg(&op2, op2sum, SrcSize);
661 op3sum = avg(&op3, op3sum, SrcSize);
668 WLog_Print(h264->log, WLOG_INFO,
669 "luma=%" PRIu64
" [avg=%lf] chroma=%" PRIu64
" [avg=%lf] combined=%" PRIu64
671 op1, op1sum, op2, op2sum, op3, op3sum);
676#define MAX_SUBSYSTEMS 10
677static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT;
678static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = WINPR_C_ARRAY_INIT;
680static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED
PINIT_ONCE once,
681 WINPR_ATTR_UNUSED PVOID param,
682 WINPR_ATTR_UNUSED PVOID* context)
686#ifdef WITH_MEDIACODEC
688 subSystems[i] = &g_Subsystem_mediacodec;
692#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
694 subSystems[i] = &g_Subsystem_MF;
700 subSystems[i] = &g_Subsystem_OpenH264;
704#ifdef WITH_VIDEO_FFMPEG
706 subSystems[i] = &g_Subsystem_libavcodec;
713static BOOL h264_context_init(H264_CONTEXT* h264)
718 h264->subsystem =
nullptr;
719 if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems,
nullptr,
nullptr))
722 for (
size_t i = 0; i < MAX_SUBSYSTEMS; i++)
724 const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
726 if (!subsystem || !subsystem->Init)
729 if (subsystem->Init(h264))
731 h264->subsystem = subsystem;
739BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
745 h264->height = height;
747 if (h264->subsystem && h264->subsystem->Uninit)
748 h264->subsystem->Uninit(h264);
749 if (!h264_context_init(h264))
752 return yuv_context_reset(h264->yuv, width, height);
755H264_CONTEXT* h264_context_new(BOOL Compressor)
757 H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1,
sizeof(H264_CONTEXT));
761 h264->log = WLog_Get(TAG);
766 h264->Compressor = Compressor;
770 h264->BitRate = 1000000;
771 h264->FrameRate = 30;
774 if (!h264_context_init(h264))
777 h264->yuv = yuv_context_new(Compressor, 0);
784 WINPR_PRAGMA_DIAG_PUSH
785 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
786 h264_context_free(h264);
787 WINPR_PRAGMA_DIAG_POP
791void h264_context_free(H264_CONTEXT* h264)
797 WINPR_ASSERT(h264->subsystem->Uninit);
798 h264->subsystem->Uninit(h264);
801 for (
size_t x = 0; x < 3; x++)
803 if (h264->Compressor)
805 winpr_aligned_free(h264->pYUVData[x]);
806 winpr_aligned_free(h264->pOldYUVData[x]);
808 winpr_aligned_free(h264->pYUV444Data[x]);
809 winpr_aligned_free(h264->pOldYUV444Data[x]);
811 winpr_aligned_free(h264->lumaData);
813 yuv_context_free(h264->yuv);
823 free(meta->quantQualityVals);
824 free(meta->regionRects);
828BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value)
833 case H264_CONTEXT_OPTION_BITRATE:
834 h264->BitRate = value;
836 case H264_CONTEXT_OPTION_FRAMERATE:
837 h264->FrameRate = value;
839 case H264_CONTEXT_OPTION_RATECONTROL:
843 case H264_RATECONTROL_VBR:
844 h264->RateControlMode = H264_RATECONTROL_VBR;
846 case H264_RATECONTROL_CQP:
847 h264->RateControlMode = H264_RATECONTROL_CQP;
850 WLog_Print(h264->log, WLOG_WARN,
851 "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
"]",
857 case H264_CONTEXT_OPTION_QP:
860 case H264_CONTEXT_OPTION_USAGETYPE:
861 h264->UsageType = value;
863 case H264_CONTEXT_OPTION_HW_ACCEL:
864 h264->hwAccel = (value);
867 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",
873UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
878 case H264_CONTEXT_OPTION_BITRATE:
879 return h264->BitRate;
880 case H264_CONTEXT_OPTION_FRAMERATE:
881 return h264->FrameRate;
882 case H264_CONTEXT_OPTION_RATECONTROL:
883 return h264->RateControlMode;
884 case H264_CONTEXT_OPTION_QP:
886 case H264_CONTEXT_OPTION_USAGETYPE:
887 return h264->UsageType;
888 case H264_CONTEXT_OPTION_HW_ACCEL:
889 return h264->hwAccel;
891 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",