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] != piDstStride[0]) ||
499 (piDstSize[0] != 1ull * piMainStride[0] * padDstHeight))
501 for (UINT32 x = 0; x < 3; x++)
503 piDstStride[x] = piMainStride[0];
504 piDstSize[x] = piDstStride[x] * padDstHeight;
506 if (piDstSize[x] == 0)
509 BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], piDstSize[x], 1, 16);
512 ppYUVDstData[x] = tmp1;
513 BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], piDstSize[x], 1, 16);
516 ppOldYUVDstData[x] = tmp2;
520 BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, piDstSize[0], 4, 16);
523 h264->lumaData = tmp;
527 for (UINT32 x = 0; x < 3; x++)
529 if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
531 WLog_Print(h264->log, WLOG_ERROR,
532 "YUV buffer not initialized! check your decoder settings");
545static BOOL avc444_process_rects(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize,
546 BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
547 WINPR_ATTR_UNUSED UINT32 nDstWidth, UINT32 nDstHeight,
548 const RECTANGLE_16* rects, UINT32 nrRects, avc444_frame_type type)
550 const BYTE* pYUVData[3];
551 BYTE* pYUVDstData[3];
552 UINT32* piDstStride = h264->iYUV444Stride;
553 BYTE** ppYUVDstData = h264->pYUV444Data;
554 const UINT32* piStride = h264->iStride;
556 if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0)
559 pYUVData[0] = h264->pYUVData[0];
560 pYUVData[1] = h264->pYUVData[1];
561 pYUVData[2] = h264->pYUVData[2];
562 if (!avc444_ensure_buffer(h264, nDstHeight))
565 pYUVDstData[0] = ppYUVDstData[0];
566 pYUVDstData[1] = ppYUVDstData[1];
567 pYUVDstData[2] = ppYUVDstData[2];
568 return (yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->height,
569 pYUVDstData, piDstStride, DstFormat, pDstData, nDstStep, rects,
573#if defined(AVC444_FRAME_STAT)
574static UINT64 op1 = 0;
575static double op1sum = 0;
576static UINT64 op2 = 0;
577static double op2sum = 0;
578static UINT64 op3 = 0;
579static double op3sum = 0;
580static double avg(UINT64* count,
double old,
double size)
582 double tmp = size + *count * old;
589INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op,
const RECTANGLE_16* regionRects,
590 UINT32 numRegionRects,
const BYTE* pSrcData, UINT32 SrcSize,
591 const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
592 const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat,
593 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId)
596 avc444_frame_type chroma =
597 (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
599 if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
602 if (!areRectsValid(nDstWidth, nDstHeight, regionRects, numRegionRects))
604 if (!areRectsValid(nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect))
611 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
612 nDstWidth, nDstHeight, regionRects, numRegionRects,
615 else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
616 nDstStep, nDstWidth, nDstHeight, auxRegionRects,
617 numAuxRegionRect, chroma))
625 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
626 nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
634 if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
635 nDstWidth, nDstHeight, regionRects, numRegionRects,
647#if defined(AVC444_FRAME_STAT)
652 op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
656 op2sum = avg(&op2, op2sum, SrcSize);
660 op3sum = avg(&op3, op3sum, SrcSize);
667 WLog_Print(h264->log, WLOG_INFO,
668 "luma=%" PRIu64
" [avg=%lf] chroma=%" PRIu64
" [avg=%lf] combined=%" PRIu64
670 op1, op1sum, op2, op2sum, op3, op3sum);
675#define MAX_SUBSYSTEMS 10
676static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT;
677static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = WINPR_C_ARRAY_INIT;
679static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED
PINIT_ONCE once,
680 WINPR_ATTR_UNUSED PVOID param,
681 WINPR_ATTR_UNUSED PVOID* context)
685#ifdef WITH_MEDIACODEC
687 subSystems[i] = &g_Subsystem_mediacodec;
691#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
693 subSystems[i] = &g_Subsystem_MF;
699 subSystems[i] = &g_Subsystem_OpenH264;
703#ifdef WITH_VIDEO_FFMPEG
705 subSystems[i] = &g_Subsystem_libavcodec;
712static BOOL h264_context_init(H264_CONTEXT* h264)
717 h264->subsystem =
nullptr;
718 if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems,
nullptr,
nullptr))
721 for (
size_t i = 0; i < MAX_SUBSYSTEMS; i++)
723 const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
725 if (!subsystem || !subsystem->Init)
728 if (subsystem->Init(h264))
730 h264->subsystem = subsystem;
738BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
744 h264->height = height;
746 if (h264->subsystem && h264->subsystem->Uninit)
747 h264->subsystem->Uninit(h264);
748 if (!h264_context_init(h264))
751 return yuv_context_reset(h264->yuv, width, height);
754H264_CONTEXT* h264_context_new(BOOL Compressor)
756 H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1,
sizeof(H264_CONTEXT));
760 h264->log = WLog_Get(TAG);
765 h264->Compressor = Compressor;
769 h264->BitRate = 1000000;
770 h264->FrameRate = 30;
773 if (!h264_context_init(h264))
776 h264->yuv = yuv_context_new(Compressor, 0);
783 WINPR_PRAGMA_DIAG_PUSH
784 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
785 h264_context_free(h264);
786 WINPR_PRAGMA_DIAG_POP
790void h264_context_free(H264_CONTEXT* h264)
796 WINPR_ASSERT(h264->subsystem->Uninit);
797 h264->subsystem->Uninit(h264);
800 for (
size_t x = 0; x < 3; x++)
802 if (h264->Compressor)
804 winpr_aligned_free(h264->pYUVData[x]);
805 winpr_aligned_free(h264->pOldYUVData[x]);
807 winpr_aligned_free(h264->pYUV444Data[x]);
808 winpr_aligned_free(h264->pOldYUV444Data[x]);
810 winpr_aligned_free(h264->lumaData);
812 yuv_context_free(h264->yuv);
822 free(meta->quantQualityVals);
823 free(meta->regionRects);
827BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value)
832 case H264_CONTEXT_OPTION_BITRATE:
833 h264->BitRate = value;
835 case H264_CONTEXT_OPTION_FRAMERATE:
836 h264->FrameRate = value;
838 case H264_CONTEXT_OPTION_RATECONTROL:
842 case H264_RATECONTROL_VBR:
843 h264->RateControlMode = H264_RATECONTROL_VBR;
845 case H264_RATECONTROL_CQP:
846 h264->RateControlMode = H264_RATECONTROL_CQP;
849 WLog_Print(h264->log, WLOG_WARN,
850 "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
"]",
856 case H264_CONTEXT_OPTION_QP:
859 case H264_CONTEXT_OPTION_USAGETYPE:
860 h264->UsageType = value;
862 case H264_CONTEXT_OPTION_HW_ACCEL:
863 h264->hwAccel = (value);
866 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",
872UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
877 case H264_CONTEXT_OPTION_BITRATE:
878 return h264->BitRate;
879 case H264_CONTEXT_OPTION_FRAMERATE:
880 return h264->FrameRate;
881 case H264_CONTEXT_OPTION_RATECONTROL:
882 return h264->RateControlMode;
883 case H264_CONTEXT_OPTION_QP:
885 case H264_CONTEXT_OPTION_USAGETYPE:
886 return h264->UsageType;
887 case H264_CONTEXT_OPTION_HW_ACCEL:
888 return h264->hwAccel;
890 WLog_Print(h264->log, WLOG_WARN,
"Unknown H264_CONTEXT_OPTION[0x%08" PRIx32
"]",