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 pWelsCreateDecoder WelsCreateDecoder;
53 pWelsDestroyDecoder WelsDestroyDecoder;
54 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 = { 0 };
87 SSysMEMBuffer* pSystemBuffer = NULL;
88 H264_CONTEXT_OPENH264* sys = NULL;
89 UINT32* iStride = NULL;
90 BYTE** pYUVData = NULL;
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);
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)
124 state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData, &sBufferInfo);
126 else if (state == dsErrorFree)
129 state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData, &sBufferInfo);
133 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%04X iBufferStatus: %d", state,
134 sBufferInfo.iBufferStatus);
139 if (state != dsErrorFree)
141 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
145#if OPENH264_MAJOR >= 2
146 state = (*sys->pDecoder)->FlushFrame(sys->pDecoder, pYUVData, &sBufferInfo);
147 if (state != dsErrorFree)
149 WLog_Print(h264->log, WLOG_WARN,
"FlushFrame state: 0x%02X", state);
154 pSystemBuffer = &sBufferInfo.UsrData.sSystemBuffer;
155 iStride[0] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[0]);
156 iStride[1] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
157 iStride[2] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
159 if (sBufferInfo.iBufferStatus != 1)
161 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 iBufferStatus: %d",
162 sBufferInfo.iBufferStatus);
166 if (state != dsErrorFree)
168 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
172 if (pSystemBuffer->iFormat != videoFormatI420)
175 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
181static int openh264_compress(H264_CONTEXT* WINPR_RESTRICT h264,
182 const BYTE** WINPR_RESTRICT pYUVData,
183 const UINT32* WINPR_RESTRICT iStride, BYTE** WINPR_RESTRICT ppDstData,
184 UINT32* WINPR_RESTRICT pDstSize)
187 SFrameBSInfo info = { 0 };
188 SSourcePicture pic = { 0 };
190 H264_CONTEXT_OPENH264* sys = NULL;
193 WINPR_ASSERT(pYUVData);
194 WINPR_ASSERT(iStride);
195 WINPR_ASSERT(ppDstData);
196 WINPR_ASSERT(pDstSize);
198 sys = &((H264_CONTEXT_OPENH264*)h264->pSystemData)[0];
204 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
207 if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
210 if ((h264->FrameRate > INT_MAX) || (h264->NumberOfThreads > INT_MAX) ||
211 (h264->BitRate > INT_MAX) || (h264->QP > INT_MAX))
214 WINPR_ASSERT(sys->pEncoder);
215 if ((sys->EncParamExt.iPicWidth != (
int)h264->width) ||
216 (sys->EncParamExt.iPicHeight != (
int)h264->height))
218 WINPR_ASSERT((*sys->pEncoder)->GetDefaultParams);
219 status = (*sys->pEncoder)->GetDefaultParams(sys->pEncoder, &sys->EncParamExt);
223 WLog_Print(h264->log, WLOG_ERROR,
224 "Failed to get OpenH264 default parameters (status=%d)", status);
228 EUsageType usageType = SCREEN_CONTENT_REAL_TIME;
230 switch (h264->UsageType)
232 case H264_CAMERA_VIDEO_NON_REAL_TIME:
233 usageType = CAMERA_VIDEO_NON_REAL_TIME;
235 case H264_CAMERA_VIDEO_REAL_TIME:
236 usageType = CAMERA_VIDEO_REAL_TIME;
238 case H264_SCREEN_CONTENT_NON_REAL_TIME:
239 usageType = SCREEN_CONTENT_NON_REAL_TIME;
241 case H264_SCREEN_CONTENT_REAL_TIME:
246 sys->EncParamExt.iUsageType = usageType;
247 sys->EncParamExt.iPicWidth = WINPR_ASSERTING_INT_CAST(
int, h264->width);
248 sys->EncParamExt.iPicHeight = WINPR_ASSERTING_INT_CAST(
int, h264->height);
249 sys->EncParamExt.fMaxFrameRate = WINPR_ASSERTING_INT_CAST(
short, h264->FrameRate);
250 sys->EncParamExt.iMaxBitrate = UNSPECIFIED_BIT_RATE;
251 sys->EncParamExt.bEnableDenoise = 0;
252 sys->EncParamExt.bEnableLongTermReference = 0;
253 sys->EncParamExt.iSpatialLayerNum = 1;
254 sys->EncParamExt.iMultipleThreadIdc =
255 WINPR_ASSERTING_INT_CAST(
unsigned short, h264->NumberOfThreads);
256 sys->EncParamExt.sSpatialLayers[0].fFrameRate =
257 WINPR_ASSERTING_INT_CAST(
short, h264->FrameRate);
258 sys->EncParamExt.sSpatialLayers[0].iVideoWidth = sys->EncParamExt.iPicWidth;
259 sys->EncParamExt.sSpatialLayers[0].iVideoHeight = sys->EncParamExt.iPicHeight;
260 sys->EncParamExt.sSpatialLayers[0].iMaxSpatialBitrate = sys->EncParamExt.iMaxBitrate;
262 switch (h264->RateControlMode)
264 case H264_RATECONTROL_VBR:
265 sys->EncParamExt.iRCMode = RC_BITRATE_MODE;
266 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
267 sys->EncParamExt.sSpatialLayers[0].iSpatialBitrate =
268 sys->EncParamExt.iTargetBitrate;
269 sys->EncParamExt.bEnableFrameSkip = 1;
272 case H264_RATECONTROL_CQP:
273 sys->EncParamExt.iRCMode = RC_OFF_MODE;
274 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
275 sys->EncParamExt.bEnableFrameSkip = 0;
281 if (sys->EncParamExt.iMultipleThreadIdc > 1)
283#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
284 sys->EncParamExt.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE;
286 sys->EncParamExt.sSpatialLayers[0].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
290 WINPR_ASSERT((*sys->pEncoder)->InitializeExt);
291 status = (*sys->pEncoder)->InitializeExt(sys->pEncoder, &sys->EncParamExt);
295 WLog_Print(h264->log, WLOG_ERROR,
"Failed to initialize OpenH264 encoder (status=%d)",
300 WINPR_ASSERT((*sys->pEncoder)->GetOption);
303 ->GetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sys->EncParamExt);
307 WLog_Print(h264->log, WLOG_ERROR,
308 "Failed to get initial OpenH264 encoder parameters (status=%d)", status);
314 switch (h264->RateControlMode)
316 case H264_RATECONTROL_VBR:
317 if (sys->EncParamExt.iTargetBitrate != (
int)h264->BitRate)
319 SBitrateInfo bitrate = { 0 };
321 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
322 bitrate.iLayer = SPATIAL_LAYER_ALL;
323 bitrate.iBitrate = (int)h264->BitRate;
325 WINPR_ASSERT((*sys->pEncoder)->SetOption);
326 status = (*sys->pEncoder)
327 ->SetOption(sys->pEncoder, ENCODER_OPTION_BITRATE, &bitrate);
331 WLog_Print(h264->log, WLOG_ERROR,
332 "Failed to set encoder bitrate (status=%d)", status);
337 if ((uint32_t)sys->EncParamExt.fMaxFrameRate != h264->FrameRate)
339 sys->EncParamExt.fMaxFrameRate = WINPR_ASSERTING_INT_CAST(
int, h264->FrameRate);
341 WINPR_ASSERT((*sys->pEncoder)->SetOption);
342 status = (*sys->pEncoder)
343 ->SetOption(sys->pEncoder, ENCODER_OPTION_FRAME_RATE,
344 &sys->EncParamExt.fMaxFrameRate);
348 WLog_Print(h264->log, WLOG_ERROR,
349 "Failed to set encoder framerate (status=%d)", status);
356 case H264_RATECONTROL_CQP:
357 if (sys->EncParamExt.sSpatialLayers[0].iDLayerQp != (
int)h264->QP)
359 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
361 WINPR_ASSERT((*sys->pEncoder)->SetOption);
362 status = (*sys->pEncoder)
363 ->SetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT,
368 WLog_Print(h264->log, WLOG_ERROR,
369 "Failed to set encoder parameters (status=%d)", status);
380 pic.iPicWidth = (int)h264->width;
381 pic.iPicHeight = (int)h264->height;
382 pic.iColorFormat = videoFormatI420;
383 pic.iStride[0] = (int)iStride[0];
384 pic.iStride[1] = (int)iStride[1];
385 pic.iStride[2] = (int)iStride[2];
386 pic.pData[0] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[0], BYTE*);
387 pic.pData[1] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[1], BYTE*);
388 pic.pData[2] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[2], BYTE*);
390 WINPR_ASSERT((*sys->pEncoder)->EncodeFrame);
391 status = (*sys->pEncoder)->EncodeFrame(sys->pEncoder, &pic, &info);
395 WLog_Print(h264->log, WLOG_ERROR,
"Failed to encode frame (status=%d)", status);
399 *ppDstData = info.sLayerInfo[0].pBsBuf;
402 for (
int i = 0; i < info.iLayerNum; i++)
404 for (
int j = 0; j < info.sLayerInfo[i].iNalCount; j++)
406 const int val = info.sLayerInfo[i].pNalLengthInByte[j];
407 *pDstSize += WINPR_ASSERTING_INT_CAST(uint32_t, val);
414static void openh264_uninit(H264_CONTEXT* h264)
416 H264_CONTEXT_OPENH264* sysContexts = NULL;
420 sysContexts = (H264_CONTEXT_OPENH264*)h264->pSystemData;
424 for (UINT32 x = 0; x < h264->numSystemData; x++)
426 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
430 (*sys->pDecoder)->Uninitialize(sys->pDecoder);
431 sysContexts->WelsDestroyDecoder(sys->pDecoder);
432 sys->pDecoder = NULL;
437 (*sys->pEncoder)->Uninitialize(sys->pEncoder);
438 sysContexts->WelsDestroySVCEncoder(sys->pEncoder);
439 sys->pEncoder = NULL;
443#if defined(WITH_OPENH264_LOADING)
444 if (sysContexts->lib)
445 FreeLibrary(sysContexts->lib);
447 free(h264->pSystemData);
448 h264->pSystemData = NULL;
452#if defined(WITH_OPENH264_LOADING)
453static BOOL openh264_load_functionpointers(H264_CONTEXT* h264,
const char* name)
455 H264_CONTEXT_OPENH264* sysContexts;
462 sysContexts = h264->pSystemData;
467 sysContexts->lib = LoadLibraryA(name);
469 if (!sysContexts->lib)
472 sysContexts->WelsGetCodecVersionEx =
473 GetProcAddressAs(sysContexts->lib,
"WelsGetCodecVersionEx", pWelsGetCodecVersionEx);
474 sysContexts->WelsCreateDecoder =
475 GetProcAddressAs(sysContexts->lib,
"WelsCreateDecoder", pWelsCreateDecoder);
476 sysContexts->WelsDestroyDecoder =
477 GetProcAddressAs(sysContexts->lib,
"WelsDestroyDecoder", pWelsDestroyDecoder);
478 sysContexts->WelsCreateSVCEncoder =
479 GetProcAddressAs(sysContexts->lib,
"WelsCreateSVCEncoder", pWelsCreateSVCEncoder);
480 sysContexts->WelsDestroySVCEncoder =
481 GetProcAddressAs(sysContexts->lib,
"WelsDestroySVCEncoder", pWelsDestroySVCEncoder);
483 if (!sysContexts->WelsCreateDecoder || !sysContexts->WelsDestroyDecoder ||
484 !sysContexts->WelsCreateSVCEncoder || !sysContexts->WelsDestroySVCEncoder ||
485 !sysContexts->WelsGetCodecVersionEx)
487 FreeLibrary(sysContexts->lib);
488 sysContexts->lib = NULL;
492 sysContexts->WelsGetCodecVersionEx(&sysContexts->version);
493 WLog_Print(h264->log, WLOG_INFO,
"loaded %s %d.%d.%d", name, sysContexts->version.uMajor,
494 sysContexts->version.uMinor, sysContexts->version.uRevision);
496 if ((sysContexts->version.uMajor < 1) ||
497 ((sysContexts->version.uMajor == 1) && (sysContexts->version.uMinor < 6)))
500 h264->log, WLOG_ERROR,
501 "OpenH264 %s %d.%d.%d is too old, need at least version 1.6.0 for dynamic loading",
502 name, sysContexts->version.uMajor, sysContexts->version.uMinor,
503 sysContexts->version.uRevision);
504 FreeLibrary(sysContexts->lib);
505 sysContexts->lib = NULL;
513static BOOL openh264_init(H264_CONTEXT* h264)
515#if defined(WITH_OPENH264_LOADING)
516 BOOL success = FALSE;
519 H264_CONTEXT_OPENH264* sysContexts = NULL;
520 static int traceLevel = WELS_LOG_DEBUG;
521#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
522 static EVideoFormatType videoFormat = videoFormatI420;
524 static WelsTraceCallback traceCallback = openh264_trace_callback;
528 h264->numSystemData = 1;
530 (H264_CONTEXT_OPENH264*)calloc(h264->numSystemData,
sizeof(H264_CONTEXT_OPENH264));
535 h264->pSystemData = (
void*)sysContexts;
536#if defined(WITH_OPENH264_LOADING)
538 for (
size_t i = 0; i < ARRAYSIZE(openh264_library_names); i++)
540 const char* current = openh264_library_names[i];
541 success = openh264_load_functionpointers(h264, current);
551 sysContexts->WelsGetCodecVersionEx = WelsGetCodecVersionEx;
552 sysContexts->WelsCreateDecoder = WelsCreateDecoder;
553 sysContexts->WelsDestroyDecoder = WelsDestroyDecoder;
554 sysContexts->WelsCreateSVCEncoder = WelsCreateSVCEncoder;
555 sysContexts->WelsDestroySVCEncoder = WelsDestroySVCEncoder;
558 for (UINT32 x = 0; x < h264->numSystemData; x++)
560 SDecodingParam sDecParam = { 0 };
561 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
563 if (h264->Compressor)
565 sysContexts->WelsCreateSVCEncoder(&sys->pEncoder);
569 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 encoder");
575 sysContexts->WelsCreateDecoder(&sys->pDecoder);
579 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 decoder");
583#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
584 sDecParam.eOutputColorFormat = videoFormatI420;
586 sDecParam.eEcActiveIdc = ERROR_CON_FRAME_COPY;
587 sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
588 status = (*sys->pDecoder)->Initialize(sys->pDecoder, &sDecParam);
592 WLog_Print(h264->log, WLOG_ERROR,
593 "Failed to initialize OpenH264 decoder (status=%ld)", status);
597#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
599 (*sys->pDecoder)->SetOption(sys->pDecoder, DECODER_OPTION_DATAFORMAT, &videoFormat);
604 WLog_Print(h264->log, WLOG_ERROR,
605 "Failed to set data format option on OpenH264 decoder (status=%ld)",
610 if (WLog_GetLogLevel(h264->log) == WLOG_TRACE)
612 status = (*sys->pDecoder)
613 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_LEVEL, &traceLevel);
617 WLog_Print(h264->log, WLOG_ERROR,
618 "Failed to set trace level option on OpenH264 decoder (status=%ld)",
623 status = (*sys->pDecoder)
624 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
629 WLog_Print(h264->log, WLOG_ERROR,
630 "Failed to set trace callback context option on OpenH264 decoder "
636 status = (*sys->pDecoder)
637 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK,
638 (
void*)&traceCallback);
643 h264->log, WLOG_ERROR,
644 "Failed to set trace callback option on OpenH264 decoder (status=%ld)",
652 h264->hwAccel = FALSE;
655 openh264_uninit(h264);
659const H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264 = {
"OpenH264", openh264_init, openh264_uninit,
660 openh264_decompress, openh264_compress };