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.8",
"libopenh264.so.2.6.0",
"libopenh264.so.7",
69 "libopenh264.so.2.5.0",
"libopenh264.so.2.4.1",
"libopenh264.so.2.4.0",
70 "libopenh264.so.2.3.1",
"libopenh264.so.2.3.0",
"libopenh264.so",
76static void openh264_trace_callback(
void* ctx,
int level,
const char* message)
78 H264_CONTEXT* h264 = ctx;
80 WLog_Print(h264->log, WLOG_TRACE,
"%d - %s", level, message);
83static int openh264_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
84 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
86 DECODING_STATE state = dsInvalidArgument;
87 SBufferInfo sBufferInfo = WINPR_C_ARRAY_INIT;
88 SSysMEMBuffer* pSystemBuffer =
nullptr;
89 H264_CONTEXT_OPENH264* sys =
nullptr;
90 UINT32* iStride =
nullptr;
91 BYTE** pYUVData =
nullptr;
94 WINPR_ASSERT(pSrcData || (SrcSize == 0));
96 sys = (H264_CONTEXT_OPENH264*)h264->pSystemData;
99 iStride = h264->iStride;
100 WINPR_ASSERT(iStride);
102 pYUVData = h264->pYUVData;
103 WINPR_ASSERT(pYUVData);
109 if (SrcSize > INT32_MAX)
111 WLog_Print(h264->log, WLOG_WARN,
"SrcSize %" PRIu32
" exceeds INT32_MAX", SrcSize);
118 pYUVData[0] =
nullptr;
119 pYUVData[1] =
nullptr;
120 pYUVData[2] =
nullptr;
122 WINPR_ASSERT(sys->pDecoder);
123 state = (*sys->pDecoder)
124 ->DecodeFrame2(sys->pDecoder, pSrcData, WINPR_ASSERTING_INT_CAST(
int, SrcSize),
125 pYUVData, &sBufferInfo);
127 if (sBufferInfo.iBufferStatus != 1)
129 if (state == dsNoParamSets)
133 (*sys->pDecoder)->DecodeFrame2(sys->pDecoder,
nullptr, 0, pYUVData, &sBufferInfo);
135 else if (state == dsErrorFree)
139 (*sys->pDecoder)->DecodeFrame2(sys->pDecoder,
nullptr, 0, pYUVData, &sBufferInfo);
143 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%04X iBufferStatus: %d", state,
144 sBufferInfo.iBufferStatus);
149 if (state != dsErrorFree)
151 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
155#if OPENH264_MAJOR >= 2
156 state = (*sys->pDecoder)->FlushFrame(sys->pDecoder, pYUVData, &sBufferInfo);
157 if (state != dsErrorFree)
159 WLog_Print(h264->log, WLOG_WARN,
"FlushFrame state: 0x%02X", state);
164 pSystemBuffer = &sBufferInfo.UsrData.sSystemBuffer;
165 iStride[0] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[0]);
166 iStride[1] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
167 iStride[2] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
169 if (sBufferInfo.iBufferStatus != 1)
171 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 iBufferStatus: %d",
172 sBufferInfo.iBufferStatus);
176 if (state != dsErrorFree)
178 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
182 if (pSystemBuffer->iFormat != videoFormatI420)
185 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
188 if ((pSystemBuffer->iWidth <= 0) || (pSystemBuffer->iHeight <= 0))
191 h264->YUVWidth = WINPR_ASSERTING_INT_CAST(UINT32, pSystemBuffer->iWidth);
192 h264->YUVHeight = WINPR_ASSERTING_INT_CAST(UINT32, pSystemBuffer->iHeight);
197static int openh264_compress(H264_CONTEXT* WINPR_RESTRICT h264,
198 const BYTE** WINPR_RESTRICT pYUVData,
199 const UINT32* WINPR_RESTRICT iStride, BYTE** WINPR_RESTRICT ppDstData,
200 UINT32* WINPR_RESTRICT pDstSize)
203 SFrameBSInfo info = WINPR_C_ARRAY_INIT;
204 SSourcePicture pic = WINPR_C_ARRAY_INIT;
206 H264_CONTEXT_OPENH264* sys =
nullptr;
209 WINPR_ASSERT(pYUVData);
210 WINPR_ASSERT(iStride);
211 WINPR_ASSERT(ppDstData);
212 WINPR_ASSERT(pDstSize);
214 sys = &((H264_CONTEXT_OPENH264*)h264->pSystemData)[0];
220 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
223 if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
226 if ((h264->FrameRate > INT_MAX) || (h264->NumberOfThreads > INT_MAX) ||
227 (h264->BitRate > INT_MAX) || (h264->QP > INT_MAX))
230 WINPR_ASSERT(sys->pEncoder);
231 if ((sys->EncParamExt.iPicWidth != (
int)h264->width) ||
232 (sys->EncParamExt.iPicHeight != (
int)h264->height))
234 WINPR_ASSERT((*sys->pEncoder)->GetDefaultParams);
235 status = (*sys->pEncoder)->GetDefaultParams(sys->pEncoder, &sys->EncParamExt);
237 if (status != cmResultSuccess)
239 WLog_Print(h264->log, WLOG_ERROR,
240 "Failed to get OpenH264 default parameters (status=%d)", status);
241 return status > 0 ? -status : status;
244 EUsageType usageType = SCREEN_CONTENT_REAL_TIME;
246 switch (h264->UsageType)
248 case H264_CAMERA_VIDEO_NON_REAL_TIME:
249 usageType = CAMERA_VIDEO_NON_REAL_TIME;
251 case H264_CAMERA_VIDEO_REAL_TIME:
252 usageType = CAMERA_VIDEO_REAL_TIME;
254 case H264_SCREEN_CONTENT_NON_REAL_TIME:
255 usageType = SCREEN_CONTENT_NON_REAL_TIME;
257 case H264_SCREEN_CONTENT_REAL_TIME:
262 sys->EncParamExt.iUsageType = usageType;
263 sys->EncParamExt.iPicWidth = WINPR_ASSERTING_INT_CAST(
int, h264->width);
264 sys->EncParamExt.iPicHeight = WINPR_ASSERTING_INT_CAST(
int, h264->height);
265 sys->EncParamExt.fMaxFrameRate = WINPR_ASSERTING_INT_CAST(
short, h264->FrameRate);
266 sys->EncParamExt.iMaxBitrate = UNSPECIFIED_BIT_RATE;
267 sys->EncParamExt.bEnableDenoise = 0;
268 sys->EncParamExt.bEnableLongTermReference = 0;
269 sys->EncParamExt.iSpatialLayerNum = 1;
270 sys->EncParamExt.iMultipleThreadIdc =
271 WINPR_ASSERTING_INT_CAST(
unsigned short, h264->NumberOfThreads);
272 sys->EncParamExt.sSpatialLayers[0].fFrameRate =
273 WINPR_ASSERTING_INT_CAST(
short, h264->FrameRate);
274 sys->EncParamExt.sSpatialLayers[0].iVideoWidth = sys->EncParamExt.iPicWidth;
275 sys->EncParamExt.sSpatialLayers[0].iVideoHeight = sys->EncParamExt.iPicHeight;
276 sys->EncParamExt.sSpatialLayers[0].iMaxSpatialBitrate = sys->EncParamExt.iMaxBitrate;
278 switch (h264->RateControlMode)
280 case H264_RATECONTROL_VBR:
281 sys->EncParamExt.iRCMode = RC_BITRATE_MODE;
282 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
283 sys->EncParamExt.sSpatialLayers[0].iSpatialBitrate =
284 sys->EncParamExt.iTargetBitrate;
285 sys->EncParamExt.bEnableFrameSkip = 1;
288 case H264_RATECONTROL_CQP:
289 sys->EncParamExt.iRCMode = RC_OFF_MODE;
290 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
291 sys->EncParamExt.bEnableFrameSkip = 0;
297 if (sys->EncParamExt.iMultipleThreadIdc > 1)
299#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
300 sys->EncParamExt.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE;
302 sys->EncParamExt.sSpatialLayers[0].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
306 WINPR_ASSERT((*sys->pEncoder)->InitializeExt);
307 status = (*sys->pEncoder)->InitializeExt(sys->pEncoder, &sys->EncParamExt);
309 if (status != cmResultSuccess)
311 WLog_Print(h264->log, WLOG_ERROR,
"Failed to initialize OpenH264 encoder (status=%d)",
313 return status > 0 ? -status : status;
316 WINPR_ASSERT((*sys->pEncoder)->GetOption);
319 ->GetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sys->EncParamExt);
321 if (status != cmResultSuccess)
323 WLog_Print(h264->log, WLOG_ERROR,
324 "Failed to get initial OpenH264 encoder parameters (status=%d)", status);
325 return status > 0 ? -status : status;
330 switch (h264->RateControlMode)
332 case H264_RATECONTROL_VBR:
333 if (sys->EncParamExt.iTargetBitrate != (
int)h264->BitRate)
335 SBitrateInfo bitrate = WINPR_C_ARRAY_INIT;
337 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
338 bitrate.iLayer = SPATIAL_LAYER_ALL;
339 bitrate.iBitrate = (int)h264->BitRate;
341 WINPR_ASSERT((*sys->pEncoder)->SetOption);
342 status = (*sys->pEncoder)
343 ->SetOption(sys->pEncoder, ENCODER_OPTION_BITRATE, &bitrate);
345 if (status != cmResultSuccess)
347 WLog_Print(h264->log, WLOG_ERROR,
348 "Failed to set encoder bitrate (status=%d)", status);
349 return status > 0 ? -status : status;
353 if ((uint32_t)sys->EncParamExt.fMaxFrameRate != h264->FrameRate)
355 sys->EncParamExt.fMaxFrameRate =
356 WINPR_ASSERTING_INT_CAST(
float, h264->FrameRate);
358 WINPR_ASSERT((*sys->pEncoder)->SetOption);
359 status = (*sys->pEncoder)
360 ->SetOption(sys->pEncoder, ENCODER_OPTION_FRAME_RATE,
361 &sys->EncParamExt.fMaxFrameRate);
363 if (status != cmResultSuccess)
365 WLog_Print(h264->log, WLOG_ERROR,
366 "Failed to set encoder framerate (status=%d)", status);
367 return status > 0 ? -status : status;
373 case H264_RATECONTROL_CQP:
374 if (sys->EncParamExt.sSpatialLayers[0].iDLayerQp != (
int)h264->QP)
376 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
378 WINPR_ASSERT((*sys->pEncoder)->SetOption);
379 status = (*sys->pEncoder)
380 ->SetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT,
383 if (status != cmResultSuccess)
385 WLog_Print(h264->log, WLOG_ERROR,
386 "Failed to set encoder parameters (status=%d)", status);
387 return status > 0 ? -status : status;
397 pic.iPicWidth = (int)h264->width;
398 pic.iPicHeight = (int)h264->height;
399 pic.iColorFormat = videoFormatI420;
400 pic.iStride[0] = (int)iStride[0];
401 pic.iStride[1] = (int)iStride[1];
402 pic.iStride[2] = (int)iStride[2];
403 pic.pData[0] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[0], BYTE*);
404 pic.pData[1] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[1], BYTE*);
405 pic.pData[2] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[2], BYTE*);
407 WINPR_ASSERT((*sys->pEncoder)->EncodeFrame);
408 status = (*sys->pEncoder)->EncodeFrame(sys->pEncoder, &pic, &info);
410 if (status != cmResultSuccess)
412 WLog_Print(h264->log, WLOG_ERROR,
"Failed to encode frame (status=%d)", status);
413 return status > 0 ? -status : status;
416 *ppDstData = info.sLayerInfo[0].pBsBuf;
419 for (
int i = 0; i < info.iLayerNum; i++)
421 for (
int j = 0; j < info.sLayerInfo[i].iNalCount; j++)
423 const int val = info.sLayerInfo[i].pNalLengthInByte[j];
424 *pDstSize += WINPR_ASSERTING_INT_CAST(uint32_t, val);
431static void openh264_uninit(H264_CONTEXT* h264)
433 H264_CONTEXT_OPENH264* sysContexts =
nullptr;
437 sysContexts = (H264_CONTEXT_OPENH264*)h264->pSystemData;
441 for (UINT32 x = 0; x < h264->numSystemData; x++)
443 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
447 (*sys->pDecoder)->Uninitialize(sys->pDecoder);
448 sysContexts->WelsDestroyDecoder(sys->pDecoder);
449 sys->pDecoder =
nullptr;
454 (*sys->pEncoder)->Uninitialize(sys->pEncoder);
455 sysContexts->WelsDestroySVCEncoder(sys->pEncoder);
456 sys->pEncoder =
nullptr;
460#if defined(WITH_OPENH264_LOADING)
461 if (sysContexts->lib)
462 FreeLibrary(sysContexts->lib);
464 free(h264->pSystemData);
465 h264->pSystemData =
nullptr;
469#if defined(WITH_OPENH264_LOADING)
470static BOOL openh264_load_functionpointers(H264_CONTEXT* h264,
const char* name)
472 H264_CONTEXT_OPENH264* sysContexts;
479 sysContexts = h264->pSystemData;
484 sysContexts->lib = LoadLibraryA(name);
486 if (!sysContexts->lib)
489 sysContexts->WelsGetCodecVersionEx =
490 GetProcAddressAs(sysContexts->lib,
"WelsGetCodecVersionEx", pWelsGetCodecVersionEx);
491 sysContexts->WelsCreateDecoder =
492 GetProcAddressAs(sysContexts->lib,
"WelsCreateDecoder", pWelsCreateDecoder);
493 sysContexts->WelsDestroyDecoder =
494 GetProcAddressAs(sysContexts->lib,
"WelsDestroyDecoder", pWelsDestroyDecoder);
495 sysContexts->WelsCreateSVCEncoder =
496 GetProcAddressAs(sysContexts->lib,
"WelsCreateSVCEncoder", pWelsCreateSVCEncoder);
497 sysContexts->WelsDestroySVCEncoder =
498 GetProcAddressAs(sysContexts->lib,
"WelsDestroySVCEncoder", pWelsDestroySVCEncoder);
500 if (!sysContexts->WelsCreateDecoder || !sysContexts->WelsDestroyDecoder ||
501 !sysContexts->WelsCreateSVCEncoder || !sysContexts->WelsDestroySVCEncoder ||
502 !sysContexts->WelsGetCodecVersionEx)
504 FreeLibrary(sysContexts->lib);
505 sysContexts->lib =
nullptr;
509 sysContexts->WelsGetCodecVersionEx(&sysContexts->version);
510 WLog_Print(h264->log, WLOG_DEBUG,
"loaded %s %u.%u.%u", name, sysContexts->version.uMajor,
511 sysContexts->version.uMinor, sysContexts->version.uRevision);
513 if ((sysContexts->version.uMajor < 1) ||
514 ((sysContexts->version.uMajor == 1) && (sysContexts->version.uMinor < 6)))
517 h264->log, WLOG_ERROR,
518 "OpenH264 %s %u.%u.%u is too old, need at least version 1.6.0 for dynamic loading",
519 name, sysContexts->version.uMajor, sysContexts->version.uMinor,
520 sysContexts->version.uRevision);
521 FreeLibrary(sysContexts->lib);
522 sysContexts->lib =
nullptr;
530static BOOL openh264_init(H264_CONTEXT* h264)
532#if defined(WITH_OPENH264_LOADING)
533 BOOL success = FALSE;
536 H264_CONTEXT_OPENH264* sysContexts =
nullptr;
537 static int traceLevel = WELS_LOG_DEBUG;
538#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
539 static EVideoFormatType videoFormat = videoFormatI420;
541 static WelsTraceCallback traceCallback = openh264_trace_callback;
545 h264->numSystemData = 1;
547 (H264_CONTEXT_OPENH264*)calloc(h264->numSystemData,
sizeof(H264_CONTEXT_OPENH264));
552 h264->pSystemData = (
void*)sysContexts;
553#if defined(WITH_OPENH264_LOADING)
555 for (
size_t i = 0; i < ARRAYSIZE(openh264_library_names); i++)
557 const char* current = openh264_library_names[i];
558 success = openh264_load_functionpointers(h264, current);
568 sysContexts->WelsGetCodecVersionEx = WelsGetCodecVersionEx;
569 sysContexts->WelsCreateDecoder = WelsCreateDecoder;
570 sysContexts->WelsDestroyDecoder = WelsDestroyDecoder;
571 sysContexts->WelsCreateSVCEncoder = WelsCreateSVCEncoder;
572 sysContexts->WelsDestroySVCEncoder = WelsDestroySVCEncoder;
575 for (UINT32 x = 0; x < h264->numSystemData; x++)
577 SDecodingParam sDecParam = WINPR_C_ARRAY_INIT;
578 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
580 if (h264->Compressor)
582#if defined(WITH_OPENH264_LOADING)
583 if (sysContexts->version.uMajor != OPENH264_MAJOR ||
584 sysContexts->version.uMinor != OPENH264_MINOR)
586 WLog_Print(h264->log, WLOG_WARN,
587 "OpenH264 encoder ABI mismatch: runtime %d.%d.%d vs compiled %d.%d.%d",
588 sysContexts->version.uMajor, sysContexts->version.uMinor,
589 sysContexts->version.uRevision, OPENH264_MAJOR, OPENH264_MINOR,
594 const int rc = sysContexts->WelsCreateSVCEncoder(&sys->pEncoder);
597 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 encoder: %d", rc);
602 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 encoder");
608 const long rc = sysContexts->WelsCreateDecoder(&sys->pDecoder);
611 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 decoder: %ld", rc);
616 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 decoder");
620#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
621 sDecParam.eOutputColorFormat = videoFormatI420;
623 sDecParam.eEcActiveIdc = ERROR_CON_FRAME_COPY;
624 sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
625 status = (*sys->pDecoder)->Initialize(sys->pDecoder, &sDecParam);
629 WLog_Print(h264->log, WLOG_ERROR,
630 "Failed to initialize OpenH264 decoder (status=%ld)", status);
634#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
636 (*sys->pDecoder)->SetOption(sys->pDecoder, DECODER_OPTION_DATAFORMAT, &videoFormat);
641 WLog_Print(h264->log, WLOG_ERROR,
642 "Failed to set data format option on OpenH264 decoder (status=%ld)",
647 if (WLog_GetLogLevel(h264->log) == WLOG_TRACE)
649 status = (*sys->pDecoder)
650 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_LEVEL, &traceLevel);
654 WLog_Print(h264->log, WLOG_ERROR,
655 "Failed to set trace level option on OpenH264 decoder (status=%ld)",
660 status = (*sys->pDecoder)
661 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
666 WLog_Print(h264->log, WLOG_ERROR,
667 "Failed to set trace callback context option on OpenH264 decoder "
673 status = (*sys->pDecoder)
674 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK,
675 (
void*)&traceCallback);
680 h264->log, WLOG_ERROR,
681 "Failed to set trace callback option on OpenH264 decoder (status=%ld)",
689 h264->hwAccel = FALSE;
692 openh264_uninit(h264);
696const H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264 = {
"OpenH264", openh264_init, openh264_uninit,
697 openh264_decompress, openh264_compress };