21#include <freerdp/config.h>
23#include <winpr/winpr.h>
24#include <winpr/library.h>
25#include <winpr/assert.h>
26#include <winpr/cast.h>
28#include <freerdp/log.h>
29#include <freerdp/codec/h264.h>
31#include <wels/codec_def.h>
32#include <wels/codec_api.h>
33#include <wels/codec_ver.h>
37typedef void (*pWelsGetCodecVersionEx)(OpenH264Version* pVersion);
39typedef long (*pWelsCreateDecoder)(ISVCDecoder** ppDecoder);
40typedef void (*pWelsDestroyDecoder)(ISVCDecoder* pDecoder);
42typedef int (*pWelsCreateSVCEncoder)(ISVCEncoder** ppEncoder);
43typedef void (*pWelsDestroySVCEncoder)(ISVCEncoder* pEncoder);
47#if defined(WITH_OPENH264_LOADING)
49 OpenH264Version version;
51 pWelsGetCodecVersionEx WelsGetCodecVersionEx;
52 WINPR_ATTR_NODISCARD pWelsCreateDecoder WelsCreateDecoder;
53 pWelsDestroyDecoder WelsDestroyDecoder;
54 WINPR_ATTR_NODISCARD pWelsCreateSVCEncoder WelsCreateSVCEncoder;
55 pWelsDestroySVCEncoder WelsDestroySVCEncoder;
56 ISVCDecoder* pDecoder;
57 ISVCEncoder* pEncoder;
58 SEncParamExt EncParamExt;
59} H264_CONTEXT_OPENH264;
61#if defined(WITH_OPENH264_LOADING)
62static const char* openh264_library_names[] = {
65#elif defined(__APPLE__)
68 "libopenh264.so.7",
"libopenh264.so.2.5.0",
"libopenh264.so.2.4.1",
"libopenh264.so.2.4.0",
69 "libopenh264.so.2.3.1",
"libopenh264.so.2.3.0",
"libopenh264.so",
75static void openh264_trace_callback(
void* ctx,
int level,
const char* message)
77 H264_CONTEXT* h264 = ctx;
79 WLog_Print(h264->log, WLOG_TRACE,
"%d - %s", level, message);
82static int openh264_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
83 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
85 DECODING_STATE state = dsInvalidArgument;
86 SBufferInfo sBufferInfo = WINPR_C_ARRAY_INIT;
87 SSysMEMBuffer* pSystemBuffer =
nullptr;
88 H264_CONTEXT_OPENH264* sys =
nullptr;
89 UINT32* iStride =
nullptr;
90 BYTE** pYUVData =
nullptr;
93 WINPR_ASSERT(pSrcData || (SrcSize == 0));
95 sys = (H264_CONTEXT_OPENH264*)h264->pSystemData;
98 iStride = h264->iStride;
99 WINPR_ASSERT(iStride);
101 pYUVData = h264->pYUVData;
102 WINPR_ASSERT(pYUVData);
110 pYUVData[0] =
nullptr;
111 pYUVData[1] =
nullptr;
112 pYUVData[2] =
nullptr;
114 WINPR_ASSERT(sys->pDecoder);
115 state = (*sys->pDecoder)
116 ->DecodeFrame2(sys->pDecoder, pSrcData, WINPR_ASSERTING_INT_CAST(
int, SrcSize),
117 pYUVData, &sBufferInfo);
119 if (sBufferInfo.iBufferStatus != 1)
121 if (state == dsNoParamSets)
125 (*sys->pDecoder)->DecodeFrame2(sys->pDecoder,
nullptr, 0, pYUVData, &sBufferInfo);
127 else if (state == dsErrorFree)
131 (*sys->pDecoder)->DecodeFrame2(sys->pDecoder,
nullptr, 0, pYUVData, &sBufferInfo);
135 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%04X iBufferStatus: %d", state,
136 sBufferInfo.iBufferStatus);
141 if (state != dsErrorFree)
143 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
147#if OPENH264_MAJOR >= 2
148 state = (*sys->pDecoder)->FlushFrame(sys->pDecoder, pYUVData, &sBufferInfo);
149 if (state != dsErrorFree)
151 WLog_Print(h264->log, WLOG_WARN,
"FlushFrame state: 0x%02X", state);
156 pSystemBuffer = &sBufferInfo.UsrData.sSystemBuffer;
157 iStride[0] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[0]);
158 iStride[1] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
159 iStride[2] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
161 if (sBufferInfo.iBufferStatus != 1)
163 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 iBufferStatus: %d",
164 sBufferInfo.iBufferStatus);
168 if (state != dsErrorFree)
170 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
174 if (pSystemBuffer->iFormat != videoFormatI420)
177 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
180 if ((pSystemBuffer->iWidth <= 0) || (pSystemBuffer->iHeight <= 0))
183 h264->YUVWidth = WINPR_ASSERTING_INT_CAST(UINT32, pSystemBuffer->iWidth);
184 h264->YUVHeight = WINPR_ASSERTING_INT_CAST(UINT32, pSystemBuffer->iHeight);
189static int openh264_compress(H264_CONTEXT* WINPR_RESTRICT h264,
190 const BYTE** WINPR_RESTRICT pYUVData,
191 const UINT32* WINPR_RESTRICT iStride, BYTE** WINPR_RESTRICT ppDstData,
192 UINT32* WINPR_RESTRICT pDstSize)
195 SFrameBSInfo info = WINPR_C_ARRAY_INIT;
196 SSourcePicture pic = WINPR_C_ARRAY_INIT;
198 H264_CONTEXT_OPENH264* sys =
nullptr;
201 WINPR_ASSERT(pYUVData);
202 WINPR_ASSERT(iStride);
203 WINPR_ASSERT(ppDstData);
204 WINPR_ASSERT(pDstSize);
206 sys = &((H264_CONTEXT_OPENH264*)h264->pSystemData)[0];
212 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
215 if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
218 if ((h264->FrameRate > INT_MAX) || (h264->NumberOfThreads > INT_MAX) ||
219 (h264->BitRate > INT_MAX) || (h264->QP > INT_MAX))
222 WINPR_ASSERT(sys->pEncoder);
223 if ((sys->EncParamExt.iPicWidth != (
int)h264->width) ||
224 (sys->EncParamExt.iPicHeight != (
int)h264->height))
226 WINPR_ASSERT((*sys->pEncoder)->GetDefaultParams);
227 status = (*sys->pEncoder)->GetDefaultParams(sys->pEncoder, &sys->EncParamExt);
231 WLog_Print(h264->log, WLOG_ERROR,
232 "Failed to get OpenH264 default parameters (status=%d)", status);
236 EUsageType usageType = SCREEN_CONTENT_REAL_TIME;
238 switch (h264->UsageType)
240 case H264_CAMERA_VIDEO_NON_REAL_TIME:
241 usageType = CAMERA_VIDEO_NON_REAL_TIME;
243 case H264_CAMERA_VIDEO_REAL_TIME:
244 usageType = CAMERA_VIDEO_REAL_TIME;
246 case H264_SCREEN_CONTENT_NON_REAL_TIME:
247 usageType = SCREEN_CONTENT_NON_REAL_TIME;
249 case H264_SCREEN_CONTENT_REAL_TIME:
254 sys->EncParamExt.iUsageType = usageType;
255 sys->EncParamExt.iPicWidth = WINPR_ASSERTING_INT_CAST(
int, h264->width);
256 sys->EncParamExt.iPicHeight = WINPR_ASSERTING_INT_CAST(
int, h264->height);
257 sys->EncParamExt.fMaxFrameRate = WINPR_ASSERTING_INT_CAST(
short, h264->FrameRate);
258 sys->EncParamExt.iMaxBitrate = UNSPECIFIED_BIT_RATE;
259 sys->EncParamExt.bEnableDenoise = 0;
260 sys->EncParamExt.bEnableLongTermReference = 0;
261 sys->EncParamExt.iSpatialLayerNum = 1;
262 sys->EncParamExt.iMultipleThreadIdc =
263 WINPR_ASSERTING_INT_CAST(
unsigned short, h264->NumberOfThreads);
264 sys->EncParamExt.sSpatialLayers[0].fFrameRate =
265 WINPR_ASSERTING_INT_CAST(
short, h264->FrameRate);
266 sys->EncParamExt.sSpatialLayers[0].iVideoWidth = sys->EncParamExt.iPicWidth;
267 sys->EncParamExt.sSpatialLayers[0].iVideoHeight = sys->EncParamExt.iPicHeight;
268 sys->EncParamExt.sSpatialLayers[0].iMaxSpatialBitrate = sys->EncParamExt.iMaxBitrate;
270 switch (h264->RateControlMode)
272 case H264_RATECONTROL_VBR:
273 sys->EncParamExt.iRCMode = RC_BITRATE_MODE;
274 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
275 sys->EncParamExt.sSpatialLayers[0].iSpatialBitrate =
276 sys->EncParamExt.iTargetBitrate;
277 sys->EncParamExt.bEnableFrameSkip = 1;
280 case H264_RATECONTROL_CQP:
281 sys->EncParamExt.iRCMode = RC_OFF_MODE;
282 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
283 sys->EncParamExt.bEnableFrameSkip = 0;
289 if (sys->EncParamExt.iMultipleThreadIdc > 1)
291#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
292 sys->EncParamExt.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE;
294 sys->EncParamExt.sSpatialLayers[0].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
298 WINPR_ASSERT((*sys->pEncoder)->InitializeExt);
299 status = (*sys->pEncoder)->InitializeExt(sys->pEncoder, &sys->EncParamExt);
303 WLog_Print(h264->log, WLOG_ERROR,
"Failed to initialize OpenH264 encoder (status=%d)",
308 WINPR_ASSERT((*sys->pEncoder)->GetOption);
311 ->GetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sys->EncParamExt);
315 WLog_Print(h264->log, WLOG_ERROR,
316 "Failed to get initial OpenH264 encoder parameters (status=%d)", status);
322 switch (h264->RateControlMode)
324 case H264_RATECONTROL_VBR:
325 if (sys->EncParamExt.iTargetBitrate != (
int)h264->BitRate)
327 SBitrateInfo bitrate = WINPR_C_ARRAY_INIT;
329 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
330 bitrate.iLayer = SPATIAL_LAYER_ALL;
331 bitrate.iBitrate = (int)h264->BitRate;
333 WINPR_ASSERT((*sys->pEncoder)->SetOption);
334 status = (*sys->pEncoder)
335 ->SetOption(sys->pEncoder, ENCODER_OPTION_BITRATE, &bitrate);
339 WLog_Print(h264->log, WLOG_ERROR,
340 "Failed to set encoder bitrate (status=%d)", status);
345 if ((uint32_t)sys->EncParamExt.fMaxFrameRate != h264->FrameRate)
347 sys->EncParamExt.fMaxFrameRate =
348 WINPR_ASSERTING_INT_CAST(
float, h264->FrameRate);
350 WINPR_ASSERT((*sys->pEncoder)->SetOption);
351 status = (*sys->pEncoder)
352 ->SetOption(sys->pEncoder, ENCODER_OPTION_FRAME_RATE,
353 &sys->EncParamExt.fMaxFrameRate);
357 WLog_Print(h264->log, WLOG_ERROR,
358 "Failed to set encoder framerate (status=%d)", status);
365 case H264_RATECONTROL_CQP:
366 if (sys->EncParamExt.sSpatialLayers[0].iDLayerQp != (
int)h264->QP)
368 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
370 WINPR_ASSERT((*sys->pEncoder)->SetOption);
371 status = (*sys->pEncoder)
372 ->SetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT,
377 WLog_Print(h264->log, WLOG_ERROR,
378 "Failed to set encoder parameters (status=%d)", status);
389 pic.iPicWidth = (int)h264->width;
390 pic.iPicHeight = (int)h264->height;
391 pic.iColorFormat = videoFormatI420;
392 pic.iStride[0] = (int)iStride[0];
393 pic.iStride[1] = (int)iStride[1];
394 pic.iStride[2] = (int)iStride[2];
395 pic.pData[0] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[0], BYTE*);
396 pic.pData[1] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[1], BYTE*);
397 pic.pData[2] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[2], BYTE*);
399 WINPR_ASSERT((*sys->pEncoder)->EncodeFrame);
400 status = (*sys->pEncoder)->EncodeFrame(sys->pEncoder, &pic, &info);
404 WLog_Print(h264->log, WLOG_ERROR,
"Failed to encode frame (status=%d)", status);
408 *ppDstData = info.sLayerInfo[0].pBsBuf;
411 for (
int i = 0; i < info.iLayerNum; i++)
413 for (
int j = 0; j < info.sLayerInfo[i].iNalCount; j++)
415 const int val = info.sLayerInfo[i].pNalLengthInByte[j];
416 *pDstSize += WINPR_ASSERTING_INT_CAST(uint32_t, val);
423static void openh264_uninit(H264_CONTEXT* h264)
425 H264_CONTEXT_OPENH264* sysContexts =
nullptr;
429 sysContexts = (H264_CONTEXT_OPENH264*)h264->pSystemData;
433 for (UINT32 x = 0; x < h264->numSystemData; x++)
435 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
439 (*sys->pDecoder)->Uninitialize(sys->pDecoder);
440 sysContexts->WelsDestroyDecoder(sys->pDecoder);
441 sys->pDecoder =
nullptr;
446 (*sys->pEncoder)->Uninitialize(sys->pEncoder);
447 sysContexts->WelsDestroySVCEncoder(sys->pEncoder);
448 sys->pEncoder =
nullptr;
452#if defined(WITH_OPENH264_LOADING)
453 if (sysContexts->lib)
454 FreeLibrary(sysContexts->lib);
456 free(h264->pSystemData);
457 h264->pSystemData =
nullptr;
461#if defined(WITH_OPENH264_LOADING)
462static BOOL openh264_load_functionpointers(H264_CONTEXT* h264,
const char* name)
464 H264_CONTEXT_OPENH264* sysContexts;
471 sysContexts = h264->pSystemData;
476 sysContexts->lib = LoadLibraryA(name);
478 if (!sysContexts->lib)
481 sysContexts->WelsGetCodecVersionEx =
482 GetProcAddressAs(sysContexts->lib,
"WelsGetCodecVersionEx", pWelsGetCodecVersionEx);
483 sysContexts->WelsCreateDecoder =
484 GetProcAddressAs(sysContexts->lib,
"WelsCreateDecoder", pWelsCreateDecoder);
485 sysContexts->WelsDestroyDecoder =
486 GetProcAddressAs(sysContexts->lib,
"WelsDestroyDecoder", pWelsDestroyDecoder);
487 sysContexts->WelsCreateSVCEncoder =
488 GetProcAddressAs(sysContexts->lib,
"WelsCreateSVCEncoder", pWelsCreateSVCEncoder);
489 sysContexts->WelsDestroySVCEncoder =
490 GetProcAddressAs(sysContexts->lib,
"WelsDestroySVCEncoder", pWelsDestroySVCEncoder);
492 if (!sysContexts->WelsCreateDecoder || !sysContexts->WelsDestroyDecoder ||
493 !sysContexts->WelsCreateSVCEncoder || !sysContexts->WelsDestroySVCEncoder ||
494 !sysContexts->WelsGetCodecVersionEx)
496 FreeLibrary(sysContexts->lib);
497 sysContexts->lib =
nullptr;
501 sysContexts->WelsGetCodecVersionEx(&sysContexts->version);
502 WLog_Print(h264->log, WLOG_DEBUG,
"loaded %s %u.%u.%u", name, sysContexts->version.uMajor,
503 sysContexts->version.uMinor, sysContexts->version.uRevision);
505 if ((sysContexts->version.uMajor < 1) ||
506 ((sysContexts->version.uMajor == 1) && (sysContexts->version.uMinor < 6)))
509 h264->log, WLOG_ERROR,
510 "OpenH264 %s %u.%u.%u is too old, need at least version 1.6.0 for dynamic loading",
511 name, sysContexts->version.uMajor, sysContexts->version.uMinor,
512 sysContexts->version.uRevision);
513 FreeLibrary(sysContexts->lib);
514 sysContexts->lib =
nullptr;
522static BOOL openh264_init(H264_CONTEXT* h264)
524#if defined(WITH_OPENH264_LOADING)
525 BOOL success = FALSE;
528 H264_CONTEXT_OPENH264* sysContexts =
nullptr;
529 static int traceLevel = WELS_LOG_DEBUG;
530#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
531 static EVideoFormatType videoFormat = videoFormatI420;
533 static WelsTraceCallback traceCallback = openh264_trace_callback;
537 h264->numSystemData = 1;
539 (H264_CONTEXT_OPENH264*)calloc(h264->numSystemData,
sizeof(H264_CONTEXT_OPENH264));
544 h264->pSystemData = (
void*)sysContexts;
545#if defined(WITH_OPENH264_LOADING)
547 for (
size_t i = 0; i < ARRAYSIZE(openh264_library_names); i++)
549 const char* current = openh264_library_names[i];
550 success = openh264_load_functionpointers(h264, current);
560 sysContexts->WelsGetCodecVersionEx = WelsGetCodecVersionEx;
561 sysContexts->WelsCreateDecoder = WelsCreateDecoder;
562 sysContexts->WelsDestroyDecoder = WelsDestroyDecoder;
563 sysContexts->WelsCreateSVCEncoder = WelsCreateSVCEncoder;
564 sysContexts->WelsDestroySVCEncoder = WelsDestroySVCEncoder;
567 for (UINT32 x = 0; x < h264->numSystemData; x++)
569 SDecodingParam sDecParam = WINPR_C_ARRAY_INIT;
570 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
572 if (h264->Compressor)
574#if defined(WITH_OPENH264_LOADING)
575 if (sysContexts->version.uMajor != OPENH264_MAJOR ||
576 sysContexts->version.uMinor != OPENH264_MINOR)
578 WLog_Print(h264->log, WLOG_WARN,
579 "OpenH264 encoder ABI mismatch: runtime %d.%d.%d vs compiled %d.%d.%d",
580 sysContexts->version.uMajor, sysContexts->version.uMinor,
581 sysContexts->version.uRevision, OPENH264_MAJOR, OPENH264_MINOR,
586 const int rc = sysContexts->WelsCreateSVCEncoder(&sys->pEncoder);
589 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 encoder: %d", rc);
594 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 encoder");
600 const long rc = sysContexts->WelsCreateDecoder(&sys->pDecoder);
603 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 decoder: %ld", rc);
608 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 decoder");
612#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
613 sDecParam.eOutputColorFormat = videoFormatI420;
615 sDecParam.eEcActiveIdc = ERROR_CON_FRAME_COPY;
616 sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
617 status = (*sys->pDecoder)->Initialize(sys->pDecoder, &sDecParam);
621 WLog_Print(h264->log, WLOG_ERROR,
622 "Failed to initialize OpenH264 decoder (status=%ld)", status);
626#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
628 (*sys->pDecoder)->SetOption(sys->pDecoder, DECODER_OPTION_DATAFORMAT, &videoFormat);
633 WLog_Print(h264->log, WLOG_ERROR,
634 "Failed to set data format option on OpenH264 decoder (status=%ld)",
639 if (WLog_GetLogLevel(h264->log) == WLOG_TRACE)
641 status = (*sys->pDecoder)
642 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_LEVEL, &traceLevel);
646 WLog_Print(h264->log, WLOG_ERROR,
647 "Failed to set trace level option on OpenH264 decoder (status=%ld)",
652 status = (*sys->pDecoder)
653 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
658 WLog_Print(h264->log, WLOG_ERROR,
659 "Failed to set trace callback context option on OpenH264 decoder "
665 status = (*sys->pDecoder)
666 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK,
667 (
void*)&traceCallback);
672 h264->log, WLOG_ERROR,
673 "Failed to set trace callback option on OpenH264 decoder (status=%ld)",
681 h264->hwAccel = FALSE;
684 openh264_uninit(h264);
688const H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264 = {
"OpenH264", openh264_init, openh264_uninit,
689 openh264_decompress, openh264_compress };