2017-06-14 17:04:54 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* H.264 Bitmap Compression
|
|
|
|
*
|
|
|
|
* Copyright 2014 Mike McDonald <Mike.McDonald@software.dell.com>
|
|
|
|
* Copyright 2015 Vic Lee <llyzs.vic@gmail.com>
|
|
|
|
* Copyright 2014 Armin Novak <armin.novak@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2019-01-15 11:49:48 +03:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-06-20 11:01:50 +03:00
|
|
|
#include <freerdp/log.h>
|
2017-06-14 17:04:54 +03:00
|
|
|
#include <freerdp/codec/h264.h>
|
2019-01-11 13:40:31 +03:00
|
|
|
#include <winpr/library.h>
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
#include "wels/codec_def.h"
|
|
|
|
#include "wels/codec_api.h"
|
|
|
|
#include "wels/codec_ver.h"
|
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
#include "h264.h"
|
|
|
|
|
2019-01-11 13:40:31 +03:00
|
|
|
typedef void (*pWelsGetCodecVersionEx)(OpenH264Version* pVersion);
|
|
|
|
|
|
|
|
typedef long (*pWelsCreateDecoder)(ISVCDecoder** ppDecoder);
|
|
|
|
typedef void (*pWelsDestroyDecoder)(ISVCDecoder* pDecoder);
|
|
|
|
|
|
|
|
typedef int (*pWelsCreateSVCEncoder)(ISVCEncoder** ppEncoder);
|
|
|
|
typedef void (*pWelsDestroySVCEncoder)(ISVCEncoder* pEncoder);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
struct _H264_CONTEXT_OPENH264
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_OPENH264_LOADING)
|
2019-01-11 13:40:31 +03:00
|
|
|
HMODULE lib;
|
2019-01-15 11:49:48 +03:00
|
|
|
OpenH264Version version;
|
2019-01-11 13:40:31 +03:00
|
|
|
#endif
|
|
|
|
pWelsGetCodecVersionEx WelsGetCodecVersionEx;
|
|
|
|
pWelsCreateDecoder WelsCreateDecoder;
|
|
|
|
pWelsDestroyDecoder WelsDestroyDecoder;
|
|
|
|
pWelsCreateSVCEncoder WelsCreateSVCEncoder;
|
|
|
|
pWelsDestroySVCEncoder WelsDestroySVCEncoder;
|
2017-06-14 17:04:54 +03:00
|
|
|
ISVCDecoder* pDecoder;
|
|
|
|
ISVCEncoder* pEncoder;
|
|
|
|
SEncParamExt EncParamExt;
|
|
|
|
};
|
|
|
|
typedef struct _H264_CONTEXT_OPENH264 H264_CONTEXT_OPENH264;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_OPENH264_LOADING)
|
|
|
|
static const char* openh264_library_names[] = {
|
2019-01-11 13:40:31 +03:00
|
|
|
#if defined(_WIN32)
|
|
|
|
"openh264.dll"
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
"libopenh264.dylib"
|
|
|
|
#else
|
|
|
|
"libopenh264.so"
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static void openh264_trace_callback(H264_CONTEXT* h264, int level, const char* message)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2018-01-17 17:35:11 +03:00
|
|
|
if (h264)
|
|
|
|
WLog_Print(h264->log, WLOG_TRACE, "%d - %s", level, message);
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static int openh264_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
|
|
|
DECODING_STATE state;
|
|
|
|
SBufferInfo sBufferInfo;
|
|
|
|
SSysMEMBuffer* pSystemBuffer;
|
2019-11-06 17:24:51 +03:00
|
|
|
H264_CONTEXT_OPENH264* sys = (H264_CONTEXT_OPENH264*)h264->pSystemData;
|
2017-06-14 17:04:54 +03:00
|
|
|
UINT32* iStride = h264->iStride;
|
|
|
|
BYTE** pYUVData = h264->pYUVData;
|
|
|
|
|
|
|
|
if (!sys->pDecoder)
|
|
|
|
return -2001;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decompress the image. The RDP host only seems to send I420 format.
|
|
|
|
*/
|
|
|
|
pYUVData[0] = NULL;
|
|
|
|
pYUVData[1] = NULL;
|
|
|
|
pYUVData[2] = NULL;
|
|
|
|
ZeroMemory(&sBufferInfo, sizeof(sBufferInfo));
|
2019-11-06 17:24:51 +03:00
|
|
|
state =
|
|
|
|
(*sys->pDecoder)->DecodeFrame2(sys->pDecoder, pSrcData, SrcSize, pYUVData, &sBufferInfo);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (sBufferInfo.iBufferStatus != 1)
|
|
|
|
{
|
|
|
|
if (state == dsNoParamSets)
|
|
|
|
{
|
|
|
|
/* this happens on the first frame due to missing parameter sets */
|
2019-11-06 17:24:51 +03:00
|
|
|
state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData, &sBufferInfo);
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
else if (state == dsErrorFree)
|
|
|
|
{
|
|
|
|
/* call DecodeFrame2 again to decode without delay */
|
2019-11-06 17:24:51 +03:00
|
|
|
state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData, &sBufferInfo);
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_WARN, "DecodeFrame2 state: 0x%04X iBufferStatus: %d", state,
|
|
|
|
sBufferInfo.iBufferStatus);
|
2017-06-14 17:04:54 +03:00
|
|
|
return -2002;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pSystemBuffer = &sBufferInfo.UsrData.sSystemBuffer;
|
|
|
|
iStride[0] = pSystemBuffer->iStride[0];
|
|
|
|
iStride[1] = pSystemBuffer->iStride[1];
|
|
|
|
iStride[2] = pSystemBuffer->iStride[1];
|
|
|
|
|
|
|
|
if (sBufferInfo.iBufferStatus != 1)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_WARN, "DecodeFrame2 iBufferStatus: %d",
|
|
|
|
sBufferInfo.iBufferStatus);
|
2017-06-14 17:04:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state != dsErrorFree)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_WARN, "DecodeFrame2 state: 0x%02X", state);
|
2017-06-14 17:04:54 +03:00
|
|
|
return -2003;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_INFO,
|
|
|
|
"h264_decompress: state=%u, pYUVData=[%p,%p,%p], bufferStatus=%d, width=%d, height=%d, format=%d, stride=[%d,%d]",
|
|
|
|
state, (void*) pYUVData[0], (void*) pYUVData[1], (void*) pYUVData[2], sBufferInfo.iBufferStatus,
|
|
|
|
pSystemBuffer->iWidth, pSystemBuffer->iHeight, pSystemBuffer->iFormat,
|
|
|
|
pSystemBuffer->iStride[0], pSystemBuffer->iStride[1]);
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (pSystemBuffer->iFormat != videoFormatI420)
|
|
|
|
return -2004;
|
|
|
|
|
|
|
|
if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
|
|
|
|
return -2005;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:08:11 +03:00
|
|
|
static int openh264_compress(H264_CONTEXT* h264, const BYTE** pYUVData, const UINT32* iStride,
|
|
|
|
BYTE** ppDstData, UINT32* pDstSize)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int status;
|
|
|
|
SFrameBSInfo info;
|
|
|
|
SSourcePicture pic;
|
|
|
|
SBitrateInfo bitrate;
|
|
|
|
H264_CONTEXT_OPENH264* sys;
|
2019-11-06 17:24:51 +03:00
|
|
|
sys = &((H264_CONTEXT_OPENH264*)h264->pSystemData)[0];
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (!sys->pEncoder)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
|
|
|
|
return -1;
|
|
|
|
|
2019-02-21 13:36:50 +03:00
|
|
|
if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((h264->FrameRate > INT_MAX) || (h264->NumberOfThreads > INT_MAX) ||
|
2019-11-06 17:24:51 +03:00
|
|
|
(h264->BitRate > INT_MAX) || (h264->QP > INT_MAX))
|
2019-02-21 13:36:50 +03:00
|
|
|
return -1;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((sys->EncParamExt.iPicWidth != (int)h264->width) ||
|
|
|
|
(sys->EncParamExt.iPicHeight != (int)h264->height))
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
|
|
|
status = (*sys->pEncoder)->GetDefaultParams(sys->pEncoder, &sys->EncParamExt);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to get OpenH264 default parameters (status=%d)", status);
|
2017-06-14 17:04:54 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->EncParamExt.iUsageType = SCREEN_CONTENT_REAL_TIME;
|
2019-02-21 13:36:50 +03:00
|
|
|
sys->EncParamExt.iPicWidth = (int)h264->width;
|
|
|
|
sys->EncParamExt.iPicHeight = (int)h264->height;
|
|
|
|
sys->EncParamExt.fMaxFrameRate = (int)h264->FrameRate;
|
2017-06-14 17:04:54 +03:00
|
|
|
sys->EncParamExt.iMaxBitrate = UNSPECIFIED_BIT_RATE;
|
|
|
|
sys->EncParamExt.bEnableDenoise = 0;
|
|
|
|
sys->EncParamExt.bEnableLongTermReference = 0;
|
|
|
|
sys->EncParamExt.bEnableFrameSkip = 0;
|
|
|
|
sys->EncParamExt.iSpatialLayerNum = 1;
|
2019-02-21 13:36:50 +03:00
|
|
|
sys->EncParamExt.iMultipleThreadIdc = (int)h264->NumberOfThreads;
|
2017-06-14 17:04:54 +03:00
|
|
|
sys->EncParamExt.sSpatialLayers[0].fFrameRate = h264->FrameRate;
|
|
|
|
sys->EncParamExt.sSpatialLayers[0].iVideoWidth = sys->EncParamExt.iPicWidth;
|
|
|
|
sys->EncParamExt.sSpatialLayers[0].iVideoHeight = sys->EncParamExt.iPicHeight;
|
2019-11-06 17:24:51 +03:00
|
|
|
sys->EncParamExt.sSpatialLayers[0].iMaxSpatialBitrate = sys->EncParamExt.iMaxBitrate;
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
switch (h264->RateControlMode)
|
|
|
|
{
|
|
|
|
case H264_RATECONTROL_VBR:
|
|
|
|
sys->EncParamExt.iRCMode = RC_BITRATE_MODE;
|
2019-02-21 13:36:50 +03:00
|
|
|
sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
|
2017-06-14 17:04:54 +03:00
|
|
|
sys->EncParamExt.sSpatialLayers[0].iSpatialBitrate =
|
|
|
|
sys->EncParamExt.iTargetBitrate;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H264_RATECONTROL_CQP:
|
|
|
|
sys->EncParamExt.iRCMode = RC_OFF_MODE;
|
2019-02-21 13:36:50 +03:00
|
|
|
sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
|
2017-06-14 17:04:54 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sys->EncParamExt.iMultipleThreadIdc > 1)
|
|
|
|
{
|
|
|
|
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
|
|
|
|
sys->EncParamExt.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE;
|
|
|
|
#else
|
|
|
|
sys->EncParamExt.sSpatialLayers[0].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
status = (*sys->pEncoder)->InitializeExt(sys->pEncoder, &sys->EncParamExt);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to initialize OpenH264 encoder (status=%d)",
|
|
|
|
status);
|
2017-06-14 17:04:54 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
|
|
|
(*sys->pEncoder)
|
|
|
|
->GetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sys->EncParamExt);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to get initial OpenH264 encoder parameters (status=%d)", status);
|
2017-06-14 17:04:54 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (h264->RateControlMode)
|
|
|
|
{
|
|
|
|
case H264_RATECONTROL_VBR:
|
2019-02-21 13:36:50 +03:00
|
|
|
if (sys->EncParamExt.iTargetBitrate != (int)h264->BitRate)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2019-02-21 13:36:50 +03:00
|
|
|
sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
|
2017-06-14 17:04:54 +03:00
|
|
|
bitrate.iLayer = SPATIAL_LAYER_ALL;
|
2019-02-21 13:36:50 +03:00
|
|
|
bitrate.iBitrate = (int)h264->BitRate;
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (*sys->pEncoder)
|
|
|
|
->SetOption(sys->pEncoder, ENCODER_OPTION_BITRATE, &bitrate);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to set encoder bitrate (status=%d)", status);
|
2017-06-14 17:04:54 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 13:36:50 +03:00
|
|
|
if (sys->EncParamExt.fMaxFrameRate != (int)h264->FrameRate)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2019-02-21 13:36:50 +03:00
|
|
|
sys->EncParamExt.fMaxFrameRate = (int)h264->FrameRate;
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (*sys->pEncoder)
|
|
|
|
->SetOption(sys->pEncoder, ENCODER_OPTION_FRAME_RATE,
|
|
|
|
&sys->EncParamExt.fMaxFrameRate);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to set encoder framerate (status=%d)", status);
|
2017-06-14 17:04:54 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H264_RATECONTROL_CQP:
|
2019-02-21 13:36:50 +03:00
|
|
|
if (sys->EncParamExt.sSpatialLayers[0].iDLayerQp != (int)h264->QP)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2019-02-21 13:36:50 +03:00
|
|
|
sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (*sys->pEncoder)
|
|
|
|
->SetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT,
|
|
|
|
&sys->EncParamExt);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to set encoder parameters (status=%d)", status);
|
2017-06-14 17:04:54 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(SFrameBSInfo));
|
|
|
|
memset(&pic, 0, sizeof(SSourcePicture));
|
2018-04-05 11:48:45 +03:00
|
|
|
pic.iPicWidth = (int)h264->width;
|
|
|
|
pic.iPicHeight = (int)h264->height;
|
2017-06-14 17:04:54 +03:00
|
|
|
pic.iColorFormat = videoFormatI420;
|
2018-04-05 11:48:45 +03:00
|
|
|
pic.iStride[0] = (int)iStride[0];
|
|
|
|
pic.iStride[1] = (int)iStride[1];
|
|
|
|
pic.iStride[2] = (int)iStride[2];
|
|
|
|
pic.pData[0] = (unsigned char*)pYUVData[0];
|
|
|
|
pic.pData[1] = (unsigned char*)pYUVData[1];
|
|
|
|
pic.pData[2] = (unsigned char*)pYUVData[2];
|
2017-06-14 17:04:54 +03:00
|
|
|
status = (*sys->pEncoder)->EncodeFrame(sys->pEncoder, &pic, &info);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to encode frame (status=%d)", status);
|
2017-06-14 17:04:54 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ppDstData = info.sLayerInfo[0].pBsBuf;
|
|
|
|
*pDstSize = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < info.iLayerNum; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < info.sLayerInfo[i].iNalCount; j++)
|
|
|
|
{
|
|
|
|
*pDstSize += info.sLayerInfo[i].pNalLengthInByte[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void openh264_uninit(H264_CONTEXT* h264)
|
|
|
|
{
|
|
|
|
UINT32 x;
|
2019-11-06 17:24:51 +03:00
|
|
|
H264_CONTEXT_OPENH264* sysContexts = (H264_CONTEXT_OPENH264*)h264->pSystemData;
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (sysContexts)
|
|
|
|
{
|
|
|
|
for (x = 0; x < h264->numSystemData; x++)
|
|
|
|
{
|
|
|
|
H264_CONTEXT_OPENH264* sys = &sysContexts[x];
|
|
|
|
|
|
|
|
if (sys->pDecoder)
|
|
|
|
{
|
|
|
|
(*sys->pDecoder)->Uninitialize(sys->pDecoder);
|
2019-01-11 13:40:31 +03:00
|
|
|
sysContexts->WelsDestroyDecoder(sys->pDecoder);
|
2017-06-14 17:04:54 +03:00
|
|
|
sys->pDecoder = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sys->pEncoder)
|
|
|
|
{
|
|
|
|
(*sys->pEncoder)->Uninitialize(sys->pEncoder);
|
2019-01-11 13:40:31 +03:00
|
|
|
sysContexts->WelsDestroySVCEncoder(sys->pEncoder);
|
2017-06-14 17:04:54 +03:00
|
|
|
sys->pEncoder = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_OPENH264_LOADING)
|
2019-10-04 11:23:43 +03:00
|
|
|
if (sysContexts->lib)
|
|
|
|
FreeLibrary(sysContexts->lib);
|
2019-01-11 13:40:31 +03:00
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
free(h264->pSystemData);
|
|
|
|
h264->pSystemData = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_OPENH264_LOADING)
|
2019-01-11 13:40:31 +03:00
|
|
|
static BOOL openh264_load_functionpointers(H264_CONTEXT* h264, const char* name)
|
|
|
|
{
|
|
|
|
H264_CONTEXT_OPENH264* sysContexts;
|
|
|
|
|
|
|
|
if (!h264)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sysContexts = h264->pSystemData;
|
|
|
|
|
|
|
|
if (!sysContexts)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-01-24 13:03:36 +03:00
|
|
|
sysContexts->lib = LoadLibraryA(name);
|
2019-01-11 13:40:31 +03:00
|
|
|
|
|
|
|
if (!sysContexts->lib)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
sysContexts->WelsGetCodecVersionEx =
|
|
|
|
(pWelsGetCodecVersionEx)GetProcAddress(sysContexts->lib, "WelsGetCodecVersionEx");
|
|
|
|
sysContexts->WelsCreateDecoder =
|
|
|
|
(pWelsCreateDecoder)GetProcAddress(sysContexts->lib, "WelsCreateDecoder");
|
|
|
|
sysContexts->WelsDestroyDecoder =
|
|
|
|
(pWelsDestroyDecoder)GetProcAddress(sysContexts->lib, "WelsDestroyDecoder");
|
|
|
|
sysContexts->WelsCreateSVCEncoder =
|
|
|
|
(pWelsCreateSVCEncoder)GetProcAddress(sysContexts->lib, "WelsCreateSVCEncoder");
|
|
|
|
sysContexts->WelsDestroySVCEncoder =
|
|
|
|
(pWelsDestroySVCEncoder)GetProcAddress(sysContexts->lib, "WelsDestroySVCEncoder");
|
2019-01-11 13:40:31 +03:00
|
|
|
|
|
|
|
if (!sysContexts->WelsCreateDecoder || !sysContexts->WelsDestroyDecoder ||
|
|
|
|
!sysContexts->WelsCreateSVCEncoder || !sysContexts->WelsDestroySVCEncoder ||
|
|
|
|
!sysContexts->WelsGetCodecVersionEx)
|
|
|
|
{
|
|
|
|
FreeLibrary(sysContexts->lib);
|
|
|
|
sysContexts->lib = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-01-15 11:49:48 +03:00
|
|
|
sysContexts->WelsGetCodecVersionEx(&sysContexts->version);
|
|
|
|
WLog_Print(h264->log, WLOG_INFO, "loaded %s %d.%d.%d", name, sysContexts->version.uMajor,
|
2019-11-06 17:24:51 +03:00
|
|
|
sysContexts->version.uMinor, sysContexts->version.uRevision);
|
2019-01-15 11:49:48 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((sysContexts->version.uMajor < 1) ||
|
|
|
|
((sysContexts->version.uMajor == 1) && (sysContexts->version.uMinor < 6)))
|
2019-01-15 11:49:48 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(
|
|
|
|
h264->log, WLOG_ERROR,
|
|
|
|
"OpenH264 %s %d.%d.%d is too old, need at least version 1.6.0 for dynamic loading",
|
|
|
|
name, sysContexts->version.uMajor, sysContexts->version.uMinor,
|
|
|
|
sysContexts->version.uRevision);
|
2019-01-15 11:49:48 +03:00
|
|
|
FreeLibrary(sysContexts->lib);
|
|
|
|
sysContexts->lib = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-01-11 13:40:31 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
static BOOL openh264_init(H264_CONTEXT* h264)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_OPENH264_LOADING)
|
2019-01-11 13:40:31 +03:00
|
|
|
BOOL success = FALSE;
|
|
|
|
size_t i;
|
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
UINT32 x;
|
|
|
|
long status;
|
|
|
|
SDecodingParam sDecParam;
|
|
|
|
H264_CONTEXT_OPENH264* sysContexts;
|
|
|
|
static int traceLevel = WELS_LOG_DEBUG;
|
|
|
|
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
|
|
|
|
static EVideoFormatType videoFormat = videoFormatI420;
|
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
static WelsTraceCallback traceCallback = (WelsTraceCallback)openh264_trace_callback;
|
2017-06-14 17:04:54 +03:00
|
|
|
h264->numSystemData = 1;
|
2019-11-06 17:24:51 +03:00
|
|
|
sysContexts =
|
|
|
|
(H264_CONTEXT_OPENH264*)calloc(h264->numSystemData, sizeof(H264_CONTEXT_OPENH264));
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (!sysContexts)
|
|
|
|
goto EXCEPTION;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
h264->pSystemData = (void*)sysContexts;
|
|
|
|
#if defined(WITH_OPENH264_LOADING)
|
2019-01-11 13:40:31 +03:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAYSIZE(openh264_library_names); i++)
|
|
|
|
{
|
|
|
|
const char* current = openh264_library_names[i];
|
|
|
|
success = openh264_load_functionpointers(h264, current);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
goto EXCEPTION;
|
|
|
|
|
|
|
|
#else
|
|
|
|
sysContexts->WelsGetCodecVersionEx = WelsGetCodecVersionEx;
|
|
|
|
sysContexts->WelsCreateDecoder = WelsCreateDecoder;
|
|
|
|
sysContexts->WelsDestroyDecoder = WelsDestroyDecoder;
|
|
|
|
sysContexts->WelsCreateSVCEncoder = WelsCreateSVCEncoder;
|
|
|
|
sysContexts->WelsDestroySVCEncoder = WelsDestroySVCEncoder;
|
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
for (x = 0; x < h264->numSystemData; x++)
|
|
|
|
{
|
|
|
|
H264_CONTEXT_OPENH264* sys = &sysContexts[x];
|
|
|
|
|
|
|
|
if (h264->Compressor)
|
|
|
|
{
|
2019-01-11 13:40:31 +03:00
|
|
|
sysContexts->WelsCreateSVCEncoder(&sys->pEncoder);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (!sys->pEncoder)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to create OpenH264 encoder");
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-11 13:40:31 +03:00
|
|
|
sysContexts->WelsCreateDecoder(&sys->pDecoder);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (!sys->pDecoder)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to create OpenH264 decoder");
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(&sDecParam, sizeof(sDecParam));
|
|
|
|
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
|
2019-11-06 17:24:51 +03:00
|
|
|
sDecParam.eOutputColorFormat = videoFormatI420;
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
|
|
|
sDecParam.eEcActiveIdc = ERROR_CON_FRAME_COPY;
|
|
|
|
sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
|
|
|
|
status = (*sys->pDecoder)->Initialize(sys->pDecoder, &sDecParam);
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to initialize OpenH264 decoder (status=%ld)", status);
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
|
|
|
(*sys->pDecoder)->SetOption(sys->pDecoder, DECODER_OPTION_DATAFORMAT, &videoFormat);
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to set data format option on OpenH264 decoder (status=%ld)",
|
|
|
|
status);
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2018-01-09 19:22:56 +03:00
|
|
|
if (WLog_GetLogLevel(h264->log) == WLOG_TRACE)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (*sys->pDecoder)
|
|
|
|
->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_LEVEL, &traceLevel);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Failed to set trace level option on OpenH264 decoder (status=%ld)",
|
|
|
|
status);
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
|
|
|
(*sys->pDecoder)
|
|
|
|
->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, &h264);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
2019-11-06 17:24:51 +03:00
|
|
|
"Failed to set trace callback context option on OpenH264 decoder "
|
|
|
|
"(status=%ld)",
|
2018-01-09 19:22:56 +03:00
|
|
|
status);
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
|
|
|
(*sys->pDecoder)
|
|
|
|
->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK, &traceCallback);
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(
|
|
|
|
h264->log, WLOG_ERROR,
|
|
|
|
"Failed to set trace callback option on OpenH264 decoder (status=%ld)",
|
|
|
|
status);
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
EXCEPTION:
|
|
|
|
openh264_uninit(h264);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264 = { "OpenH264", openh264_init, openh264_uninit,
|
|
|
|
openh264_decompress, openh264_compress };
|