21 #include <freerdp/config.h>
23 #include <winpr/winpr.h>
24 #include <winpr/library.h>
25 #include <winpr/assert.h>
27 #include <freerdp/log.h>
28 #include <freerdp/codec/h264.h>
30 #include <wels/codec_def.h>
31 #include <wels/codec_api.h>
32 #include <wels/codec_ver.h>
36 typedef void (*pWelsGetCodecVersionEx)(OpenH264Version* pVersion);
38 typedef long (*pWelsCreateDecoder)(ISVCDecoder** ppDecoder);
39 typedef void (*pWelsDestroyDecoder)(ISVCDecoder* pDecoder);
41 typedef int (*pWelsCreateSVCEncoder)(ISVCEncoder** ppEncoder);
42 typedef void (*pWelsDestroySVCEncoder)(ISVCEncoder* pEncoder);
46 #if defined(WITH_OPENH264_LOADING)
48 OpenH264Version version;
50 pWelsGetCodecVersionEx WelsGetCodecVersionEx;
51 pWelsCreateDecoder WelsCreateDecoder;
52 pWelsDestroyDecoder WelsDestroyDecoder;
53 pWelsCreateSVCEncoder WelsCreateSVCEncoder;
54 pWelsDestroySVCEncoder WelsDestroySVCEncoder;
55 ISVCDecoder* pDecoder;
56 ISVCEncoder* pEncoder;
57 SEncParamExt EncParamExt;
58 } H264_CONTEXT_OPENH264;
60 #if defined(WITH_OPENH264_LOADING)
61 static const char* openh264_library_names[] = {
64 #elif defined(__APPLE__)
72 static void openh264_trace_callback(H264_CONTEXT* WINPR_RESTRICT h264,
int level,
73 const char* WINPR_RESTRICT message)
76 WLog_Print(h264->log, WLOG_TRACE,
"%d - %s", level, message);
79 static int openh264_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
80 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
82 DECODING_STATE state = dsInvalidArgument;
83 SBufferInfo sBufferInfo = { 0 };
84 SSysMEMBuffer* pSystemBuffer = NULL;
85 H264_CONTEXT_OPENH264* sys = NULL;
86 UINT32* iStride = NULL;
87 BYTE** pYUVData = NULL;
90 WINPR_ASSERT(pSrcData || (SrcSize == 0));
92 sys = (H264_CONTEXT_OPENH264*)h264->pSystemData;
95 iStride = h264->iStride;
96 WINPR_ASSERT(iStride);
98 pYUVData = h264->pYUVData;
99 WINPR_ASSERT(pYUVData);
111 WINPR_ASSERT(sys->pDecoder);
113 (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, pSrcData, SrcSize, pYUVData, &sBufferInfo);
115 if (sBufferInfo.iBufferStatus != 1)
117 if (state == dsNoParamSets)
120 state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData, &sBufferInfo);
122 else if (state == dsErrorFree)
125 state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData, &sBufferInfo);
129 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%04X iBufferStatus: %d", state,
130 sBufferInfo.iBufferStatus);
135 if (state != dsErrorFree)
137 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
141 #if OPENH264_MAJOR >= 2
142 state = (*sys->pDecoder)->FlushFrame(sys->pDecoder, pYUVData, &sBufferInfo);
143 if (state != dsErrorFree)
145 WLog_Print(h264->log, WLOG_WARN,
"FlushFrame state: 0x%02X", state);
150 pSystemBuffer = &sBufferInfo.UsrData.sSystemBuffer;
151 iStride[0] = pSystemBuffer->iStride[0];
152 iStride[1] = pSystemBuffer->iStride[1];
153 iStride[2] = pSystemBuffer->iStride[1];
155 if (sBufferInfo.iBufferStatus != 1)
157 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 iBufferStatus: %d",
158 sBufferInfo.iBufferStatus);
162 if (state != dsErrorFree)
164 WLog_Print(h264->log, WLOG_WARN,
"DecodeFrame2 state: 0x%02X", state);
169 WLog_Print(h264->log, WLOG_INFO,
170 "h264_decompress: state=%u, pYUVData=[%p,%p,%p], bufferStatus=%d, width=%d, height=%d, format=%d, stride=[%d,%d]",
171 state, (
void*) pYUVData[0], (
void*) pYUVData[1], (
void*) pYUVData[2], sBufferInfo.iBufferStatus,
172 pSystemBuffer->iWidth, pSystemBuffer->iHeight, pSystemBuffer->iFormat,
173 pSystemBuffer->iStride[0], pSystemBuffer->iStride[1]);
176 if (pSystemBuffer->iFormat != videoFormatI420)
179 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
185 static int openh264_compress(H264_CONTEXT* WINPR_RESTRICT h264,
186 const BYTE** WINPR_RESTRICT pYUVData,
187 const UINT32* WINPR_RESTRICT iStride, BYTE** WINPR_RESTRICT ppDstData,
188 UINT32* WINPR_RESTRICT pDstSize)
191 SFrameBSInfo info = { 0 };
192 SSourcePicture pic = { 0 };
194 H264_CONTEXT_OPENH264* sys = NULL;
197 WINPR_ASSERT(pYUVData);
198 WINPR_ASSERT(iStride);
199 WINPR_ASSERT(ppDstData);
200 WINPR_ASSERT(pDstSize);
202 sys = &((H264_CONTEXT_OPENH264*)h264->pSystemData)[0];
208 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
211 if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
214 if ((h264->FrameRate > INT_MAX) || (h264->NumberOfThreads > INT_MAX) ||
215 (h264->BitRate > INT_MAX) || (h264->QP > INT_MAX))
218 WINPR_ASSERT(sys->pEncoder);
219 if ((sys->EncParamExt.iPicWidth != (
int)h264->width) ||
220 (sys->EncParamExt.iPicHeight != (
int)h264->height))
222 WINPR_ASSERT((*sys->pEncoder)->GetDefaultParams);
223 status = (*sys->pEncoder)->GetDefaultParams(sys->pEncoder, &sys->EncParamExt);
227 WLog_Print(h264->log, WLOG_ERROR,
228 "Failed to get OpenH264 default parameters (status=%d)", status);
232 EUsageType usageType = SCREEN_CONTENT_REAL_TIME;
234 switch (h264->UsageType)
236 case H264_CAMERA_VIDEO_NON_REAL_TIME:
237 usageType = CAMERA_VIDEO_NON_REAL_TIME;
239 case H264_CAMERA_VIDEO_REAL_TIME:
240 usageType = CAMERA_VIDEO_REAL_TIME;
242 case H264_SCREEN_CONTENT_NON_REAL_TIME:
243 usageType = SCREEN_CONTENT_NON_REAL_TIME;
245 case H264_SCREEN_CONTENT_REAL_TIME:
250 sys->EncParamExt.iUsageType = usageType;
251 sys->EncParamExt.iPicWidth = (int)h264->width;
252 sys->EncParamExt.iPicHeight = (
int)h264->height;
253 sys->EncParamExt.fMaxFrameRate = (int)h264->FrameRate;
254 sys->EncParamExt.iMaxBitrate = UNSPECIFIED_BIT_RATE;
255 sys->EncParamExt.bEnableDenoise = 0;
256 sys->EncParamExt.bEnableLongTermReference = 0;
257 sys->EncParamExt.iSpatialLayerNum = 1;
258 sys->EncParamExt.iMultipleThreadIdc = (
int)h264->NumberOfThreads;
259 sys->EncParamExt.sSpatialLayers[0].fFrameRate = h264->FrameRate;
260 sys->EncParamExt.sSpatialLayers[0].iVideoWidth = sys->EncParamExt.iPicWidth;
261 sys->EncParamExt.sSpatialLayers[0].iVideoHeight = sys->EncParamExt.iPicHeight;
262 sys->EncParamExt.sSpatialLayers[0].iMaxSpatialBitrate = sys->EncParamExt.iMaxBitrate;
264 switch (h264->RateControlMode)
266 case H264_RATECONTROL_VBR:
267 sys->EncParamExt.iRCMode = RC_BITRATE_MODE;
268 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
269 sys->EncParamExt.sSpatialLayers[0].iSpatialBitrate =
270 sys->EncParamExt.iTargetBitrate;
271 sys->EncParamExt.bEnableFrameSkip = 1;
274 case H264_RATECONTROL_CQP:
275 sys->EncParamExt.iRCMode = RC_OFF_MODE;
276 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (
int)h264->QP;
277 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 (sys->EncParamExt.fMaxFrameRate != (
int)h264->FrameRate)
339 sys->EncParamExt.fMaxFrameRate = (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);
378 pic.iPicWidth = (int)h264->width;
379 pic.iPicHeight = (
int)h264->height;
380 pic.iColorFormat = videoFormatI420;
381 pic.iStride[0] = (int)iStride[0];
382 pic.iStride[1] = (int)iStride[1];
383 pic.iStride[2] = (int)iStride[2];
384 pic.pData[0] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[0], BYTE*);
385 pic.pData[1] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[1], BYTE*);
386 pic.pData[2] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[2], BYTE*);
388 WINPR_ASSERT((*sys->pEncoder)->EncodeFrame);
389 status = (*sys->pEncoder)->EncodeFrame(sys->pEncoder, &pic, &info);
393 WLog_Print(h264->log, WLOG_ERROR,
"Failed to encode frame (status=%d)", status);
397 *ppDstData = info.sLayerInfo[0].pBsBuf;
400 for (
int i = 0; i < info.iLayerNum; i++)
402 for (
int j = 0; j < info.sLayerInfo[i].iNalCount; j++)
404 *pDstSize += info.sLayerInfo[i].pNalLengthInByte[j];
411 static void openh264_uninit(H264_CONTEXT* h264)
413 H264_CONTEXT_OPENH264* sysContexts = NULL;
417 sysContexts = (H264_CONTEXT_OPENH264*)h264->pSystemData;
421 for (UINT32 x = 0; x < h264->numSystemData; x++)
423 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
427 (*sys->pDecoder)->Uninitialize(sys->pDecoder);
428 sysContexts->WelsDestroyDecoder(sys->pDecoder);
429 sys->pDecoder = NULL;
434 (*sys->pEncoder)->Uninitialize(sys->pEncoder);
435 sysContexts->WelsDestroySVCEncoder(sys->pEncoder);
436 sys->pEncoder = NULL;
440 #if defined(WITH_OPENH264_LOADING)
441 if (sysContexts->lib)
442 FreeLibrary(sysContexts->lib);
444 free(h264->pSystemData);
445 h264->pSystemData = NULL;
449 #if defined(WITH_OPENH264_LOADING)
450 static BOOL openh264_load_functionpointers(H264_CONTEXT* h264,
const char* name)
452 H264_CONTEXT_OPENH264* sysContexts;
459 sysContexts = h264->pSystemData;
464 sysContexts->lib = LoadLibraryA(name);
466 if (!sysContexts->lib)
469 sysContexts->WelsGetCodecVersionEx =
470 GetProcAddressAs(sysContexts->lib,
"WelsGetCodecVersionEx", pWelsGetCodecVersionEx);
471 sysContexts->WelsCreateDecoder =
472 GetProcAddressAs(sysContexts->lib,
"WelsCreateDecoder", pWelsCreateDecoder);
473 sysContexts->WelsDestroyDecoder =
474 GetProcAddressAs(sysContexts->lib,
"WelsDestroyDecoder", pWelsDestroyDecoder);
475 sysContexts->WelsCreateSVCEncoder =
476 GetProcAddressAs(sysContexts->lib,
"WelsCreateSVCEncoder", pWelsCreateSVCEncoder);
477 sysContexts->WelsDestroySVCEncoder =
478 GetProcAddressAs(sysContexts->lib,
"WelsDestroySVCEncoder", pWelsDestroySVCEncoder);
480 if (!sysContexts->WelsCreateDecoder || !sysContexts->WelsDestroyDecoder ||
481 !sysContexts->WelsCreateSVCEncoder || !sysContexts->WelsDestroySVCEncoder ||
482 !sysContexts->WelsGetCodecVersionEx)
484 FreeLibrary(sysContexts->lib);
485 sysContexts->lib = NULL;
489 sysContexts->WelsGetCodecVersionEx(&sysContexts->version);
490 WLog_Print(h264->log, WLOG_INFO,
"loaded %s %d.%d.%d", name, sysContexts->version.uMajor,
491 sysContexts->version.uMinor, sysContexts->version.uRevision);
493 if ((sysContexts->version.uMajor < 1) ||
494 ((sysContexts->version.uMajor == 1) && (sysContexts->version.uMinor < 6)))
497 h264->log, WLOG_ERROR,
498 "OpenH264 %s %d.%d.%d is too old, need at least version 1.6.0 for dynamic loading",
499 name, sysContexts->version.uMajor, sysContexts->version.uMinor,
500 sysContexts->version.uRevision);
501 FreeLibrary(sysContexts->lib);
502 sysContexts->lib = NULL;
510 static BOOL openh264_init(H264_CONTEXT* h264)
512 #if defined(WITH_OPENH264_LOADING)
513 BOOL success = FALSE;
516 H264_CONTEXT_OPENH264* sysContexts = NULL;
517 static int traceLevel = WELS_LOG_DEBUG;
518 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
519 static EVideoFormatType videoFormat = videoFormatI420;
521 static WelsTraceCallback traceCallback = (WelsTraceCallback)openh264_trace_callback;
525 h264->numSystemData = 1;
527 (H264_CONTEXT_OPENH264*)calloc(h264->numSystemData,
sizeof(H264_CONTEXT_OPENH264));
532 h264->pSystemData = (
void*)sysContexts;
533 #if defined(WITH_OPENH264_LOADING)
535 for (
size_t i = 0; i < ARRAYSIZE(openh264_library_names); i++)
537 const char* current = openh264_library_names[i];
538 success = openh264_load_functionpointers(h264, current);
548 sysContexts->WelsGetCodecVersionEx = WelsGetCodecVersionEx;
549 sysContexts->WelsCreateDecoder = WelsCreateDecoder;
550 sysContexts->WelsDestroyDecoder = WelsDestroyDecoder;
551 sysContexts->WelsCreateSVCEncoder = WelsCreateSVCEncoder;
552 sysContexts->WelsDestroySVCEncoder = WelsDestroySVCEncoder;
555 for (UINT32 x = 0; x < h264->numSystemData; x++)
557 SDecodingParam sDecParam = { 0 };
558 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
560 if (h264->Compressor)
562 sysContexts->WelsCreateSVCEncoder(&sys->pEncoder);
566 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 encoder");
572 sysContexts->WelsCreateDecoder(&sys->pDecoder);
576 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create OpenH264 decoder");
580 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
581 sDecParam.eOutputColorFormat = videoFormatI420;
583 sDecParam.eEcActiveIdc = ERROR_CON_FRAME_COPY;
584 sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
585 status = (*sys->pDecoder)->Initialize(sys->pDecoder, &sDecParam);
589 WLog_Print(h264->log, WLOG_ERROR,
590 "Failed to initialize OpenH264 decoder (status=%ld)", status);
594 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
596 (*sys->pDecoder)->SetOption(sys->pDecoder, DECODER_OPTION_DATAFORMAT, &videoFormat);
601 WLog_Print(h264->log, WLOG_ERROR,
602 "Failed to set data format option on OpenH264 decoder (status=%ld)",
607 if (WLog_GetLogLevel(h264->log) == WLOG_TRACE)
609 status = (*sys->pDecoder)
610 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_LEVEL, &traceLevel);
614 WLog_Print(h264->log, WLOG_ERROR,
615 "Failed to set trace level option on OpenH264 decoder (status=%ld)",
622 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, &h264);
626 WLog_Print(h264->log, WLOG_ERROR,
627 "Failed to set trace callback context option on OpenH264 decoder "
635 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK, &traceCallback);
640 h264->log, WLOG_ERROR,
641 "Failed to set trace callback option on OpenH264 decoder (status=%ld)",
651 openh264_uninit(h264);
655 const H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264 = {
"OpenH264", openh264_init, openh264_uninit,
656 openh264_decompress, openh264_compress };