FreeRDP/libfreerdp/codec/h264.c

1957 lines
49 KiB
C
Raw Normal View History

/**
* FreeRDP: A Remote Desktop Protocol Implementation
* H.264 Bitmap Compression
*
* Copyright 2014 Mike McDonald <Mike.McDonald@software.dell.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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <winpr/crt.h>
#include <winpr/print.h>
2016-03-29 23:03:15 +03:00
#include <winpr/library.h>
#include <winpr/bitstream.h>
#include <freerdp/primitives.h>
#include <freerdp/codec/h264.h>
#include <freerdp/log.h>
#define TAG FREERDP_TAG("codec")
2014-09-06 03:11:03 +04:00
/**
* Dummy subsystem
*/
static int dummy_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize)
{
2014-09-06 03:11:03 +04:00
return -1;
}
static int dummy_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
2017-02-14 18:34:02 +03:00
{
//H264_CONTEXT_X264* sys = (H264_CONTEXT_X264*) h264->pSystemData;
return -1;
}
2014-09-06 03:11:03 +04:00
static void dummy_uninit(H264_CONTEXT* h264)
{
}
2014-09-06 03:11:03 +04:00
static BOOL dummy_init(H264_CONTEXT* h264)
{
return TRUE;
}
2014-09-06 03:11:03 +04:00
static H264_CONTEXT_SUBSYSTEM g_Subsystem_dummy =
{
"dummy",
dummy_init,
dummy_uninit,
2017-02-14 18:34:02 +03:00
dummy_decompress,
dummy_compress
2014-09-06 03:11:03 +04:00
};
/**
* Media Foundation subsystem
*/
#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
#include <ks.h>
#include <codecapi.h>
#include <mfapi.h>
#include <mferror.h>
#include <wmcodecdsp.h>
#include <mftransform.h>
#undef DEFINE_GUID
#define INITGUID
#include <initguid.h>
2016-08-03 16:29:12 +03:00
DEFINE_GUID(CLSID_CMSH264DecoderMFT, 0x62CE7E72, 0x4C71, 0x4d20, 0xB1, 0x5D,
0x45, 0x28, 0x31, 0xA8, 0x7D, 0x9D);
DEFINE_GUID(CLSID_VideoProcessorMFT, 0x88753b26, 0x5b24, 0x49bd, 0xb2, 0xe7,
0x0c, 0x44, 0x5c, 0x78, 0xc9, 0x82);
DEFINE_GUID(IID_IMFTransform, 0xbf94c121, 0x5b05, 0x4e6f, 0x80, 0x00, 0xba,
0x59, 0x89, 0x61, 0x41, 0x4d);
DEFINE_GUID(MF_MT_MAJOR_TYPE, 0x48eba18e, 0xf8c9, 0x4687, 0xbf, 0x11, 0x0a,
0x74, 0xc9, 0xf9, 0x6a, 0x8f);
DEFINE_GUID(MF_MT_FRAME_SIZE, 0x1652c33d, 0xd6b2, 0x4012, 0xb8, 0x34, 0x72,
0x03, 0x08, 0x49, 0xa3, 0x7d);
DEFINE_GUID(MF_MT_DEFAULT_STRIDE, 0x644b4e48, 0x1e02, 0x4516, 0xb0, 0xeb, 0xc0,
0x1c, 0xa9, 0xd4, 0x9a, 0xc6);
DEFINE_GUID(MF_MT_SUBTYPE, 0xf7e34c9a, 0x42e8, 0x4714, 0xb7, 0x4b, 0xcb, 0x29,
0xd7, 0x2c, 0x35, 0xe5);
DEFINE_GUID(MF_XVP_DISABLE_FRC, 0x2c0afa19, 0x7a97, 0x4d5a, 0x9e, 0xe8, 0x16,
0xd4, 0xfc, 0x51, 0x8d, 0x8c);
DEFINE_GUID(MFMediaType_Video, 0x73646976, 0x0000, 0x0010, 0x80, 0x00, 0x00,
0xAA, 0x00, 0x38, 0x9B, 0x71);
DEFINE_GUID(MFVideoFormat_RGB32, 22, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA,
0x00, 0x38, 0x9B, 0x71);
DEFINE_GUID(MFVideoFormat_ARGB32, 21, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA,
0x00, 0x38, 0x9B, 0x71);
DEFINE_GUID(MFVideoFormat_H264, 0x34363248, 0x0000, 0x0010, 0x80, 0x00, 0x00,
0xaa, 0x00, 0x38, 0x9b, 0x71);
DEFINE_GUID(MFVideoFormat_IYUV, 0x56555949, 0x0000, 0x0010, 0x80, 0x00, 0x00,
0xaa, 0x00, 0x38, 0x9b, 0x71);
DEFINE_GUID(IID_ICodecAPI, 0x901db4c7, 0x31ce, 0x41a2, 0x85, 0xdc, 0x8f, 0xa0,
0xbf, 0x41, 0xb8, 0xda);
DEFINE_GUID(CODECAPI_AVLowLatencyMode, 0x9c27891a, 0xed7a, 0x40e1, 0x88, 0xe8,
0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee);
DEFINE_GUID(CODECAPI_AVDecVideoMaxCodedWidth, 0x5ae557b8, 0x77af, 0x41f5, 0x9f,
0xa6, 0x4d, 0xb2, 0xfe, 0x1d, 0x4b, 0xca);
DEFINE_GUID(CODECAPI_AVDecVideoMaxCodedHeight, 0x7262a16a, 0xd2dc, 0x4e75, 0x9b,
0xa8, 0x65, 0xc0, 0xc6, 0xd3, 0x2b, 0x13);
#ifndef __IMFDXGIDeviceManager_FWD_DEFINED__
#define __IMFDXGIDeviceManager_FWD_DEFINED__
typedef interface IMFDXGIDeviceManager IMFDXGIDeviceManager;
#endif /* __IMFDXGIDeviceManager_FWD_DEFINED__ */
#ifndef __IMFDXGIDeviceManager_INTERFACE_DEFINED__
#define __IMFDXGIDeviceManager_INTERFACE_DEFINED__
typedef struct IMFDXGIDeviceManagerVtbl
{
2016-08-03 16:29:12 +03:00
HRESULT(STDMETHODCALLTYPE* QueryInterface)(IMFDXGIDeviceManager* This,
REFIID riid, void** ppvObject);
ULONG(STDMETHODCALLTYPE* AddRef)(IMFDXGIDeviceManager* This);
ULONG(STDMETHODCALLTYPE* Release)(IMFDXGIDeviceManager* This);
HRESULT(STDMETHODCALLTYPE* CloseDeviceHandle)(IMFDXGIDeviceManager* This,
HANDLE hDevice);
HRESULT(STDMETHODCALLTYPE* GetVideoService)(IMFDXGIDeviceManager* This,
HANDLE hDevice, REFIID riid, void** ppService);
HRESULT(STDMETHODCALLTYPE* LockDevice)(IMFDXGIDeviceManager* This,
HANDLE hDevice, REFIID riid, void** ppUnkDevice, BOOL fBlock);
HRESULT(STDMETHODCALLTYPE* OpenDeviceHandle)(IMFDXGIDeviceManager* This,
HANDLE* phDevice);
HRESULT(STDMETHODCALLTYPE* ResetDevice)(IMFDXGIDeviceManager* This,
IUnknown* pUnkDevice, UINT resetToken);
HRESULT(STDMETHODCALLTYPE* TestDevice)(IMFDXGIDeviceManager* This,
HANDLE hDevice);
HRESULT(STDMETHODCALLTYPE* UnlockDevice)(IMFDXGIDeviceManager* This,
HANDLE hDevice, BOOL fSaveState);
}
IMFDXGIDeviceManagerVtbl;
interface IMFDXGIDeviceManager
{
CONST_VTBL struct IMFDXGIDeviceManagerVtbl* lpVtbl;
};
#endif /* __IMFDXGIDeviceManager_INTERFACE_DEFINED__ */
2016-08-03 16:29:12 +03:00
typedef HRESULT(__stdcall* pfnMFStartup)(ULONG Version, DWORD dwFlags);
typedef HRESULT(__stdcall* pfnMFShutdown)(void);
typedef HRESULT(__stdcall* pfnMFCreateSample)(IMFSample** ppIMFSample);
typedef HRESULT(__stdcall* pfnMFCreateMemoryBuffer)(DWORD cbMaxLength,
IMFMediaBuffer** ppBuffer);
typedef HRESULT(__stdcall* pfnMFCreateMediaType)(IMFMediaType** ppMFType);
typedef HRESULT(__stdcall* pfnMFCreateDXGIDeviceManager)(UINT* pResetToken,
IMFDXGIDeviceManager** ppDXVAManager);
struct _H264_CONTEXT_MF
{
ICodecAPI* codecApi;
IMFTransform* transform;
IMFMediaType* inputType;
IMFMediaType* outputType;
IMFSample* sample;
UINT32 frameWidth;
UINT32 frameHeight;
IMFSample* outputSample;
IMFMediaBuffer* outputBuffer;
HMODULE mfplat;
pfnMFStartup MFStartup;
pfnMFShutdown MFShutdown;
pfnMFCreateSample MFCreateSample;
pfnMFCreateMemoryBuffer MFCreateMemoryBuffer;
pfnMFCreateMediaType MFCreateMediaType;
pfnMFCreateDXGIDeviceManager MFCreateDXGIDeviceManager;
};
typedef struct _H264_CONTEXT_MF H264_CONTEXT_MF;
2016-08-03 16:29:12 +03:00
static HRESULT mf_find_output_type(H264_CONTEXT_MF* sys, const GUID* guid,
IMFMediaType** ppMediaType)
{
DWORD idx = 0;
GUID mediaGuid;
HRESULT hr = S_OK;
IMFMediaType* pMediaType = NULL;
while (1)
{
2016-08-03 16:29:12 +03:00
hr = sys->transform->lpVtbl->GetOutputAvailableType(sys->transform, 0, idx,
&pMediaType);
if (FAILED(hr))
break;
pMediaType->lpVtbl->GetGUID(pMediaType, &MF_MT_SUBTYPE, &mediaGuid);
if (IsEqualGUID(&mediaGuid, guid))
{
*ppMediaType = pMediaType;
return S_OK;
}
pMediaType->lpVtbl->Release(pMediaType);
idx++;
}
return hr;
}
static HRESULT mf_create_output_sample(H264_CONTEXT_MF* sys)
{
HRESULT hr = S_OK;
MFT_OUTPUT_STREAM_INFO streamInfo;
if (sys->outputSample)
{
sys->outputSample->lpVtbl->Release(sys->outputSample);
sys->outputSample = NULL;
}
hr = sys->MFCreateSample(&sys->outputSample);
if (FAILED(hr))
{
WLog_ERR(TAG, "MFCreateSample failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->transform->lpVtbl->GetOutputStreamInfo(sys->transform, 0,
&streamInfo);
if (FAILED(hr))
{
WLog_ERR(TAG, "GetOutputStreamInfo failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = sys->MFCreateMemoryBuffer(streamInfo.cbSize, &sys->outputBuffer);
if (FAILED(hr))
{
WLog_ERR(TAG, "MFCreateMemoryBuffer failure: 0x%08"PRIX32"", hr);
goto error;
}
sys->outputSample->lpVtbl->AddBuffer(sys->outputSample, sys->outputBuffer);
if (FAILED(hr))
{
WLog_ERR(TAG, "AddBuffer failure: 0x%08"PRIX32"", hr);
goto error;
}
sys->outputBuffer->lpVtbl->Release(sys->outputBuffer);
error:
return hr;
}
static int mf_decompress(H264_CONTEXT* h264, BYTE* pSrcData, UINT32 SrcSize)
{
HRESULT hr;
BYTE* pbBuffer = NULL;
DWORD cbMaxLength = 0;
DWORD cbCurrentLength = 0;
DWORD outputStatus = 0;
IMFSample* inputSample = NULL;
IMFMediaBuffer* inputBuffer = NULL;
IMFMediaBuffer* outputBuffer = NULL;
MFT_OUTPUT_DATA_BUFFER outputDataBuffer;
H264_CONTEXT_MF* sys = (H264_CONTEXT_MF*) h264->pSystemData;
INT32* iStride = h264->iStride;
BYTE** pYUVData = h264->pYUVData;
hr = sys->MFCreateMemoryBuffer(SrcSize, &inputBuffer);
if (FAILED(hr))
{
WLog_ERR(TAG, "MFCreateMemoryBuffer failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = inputBuffer->lpVtbl->Lock(inputBuffer, &pbBuffer, &cbMaxLength,
&cbCurrentLength);
if (FAILED(hr))
{
WLog_ERR(TAG, "Lock failure: 0x%08"PRIX32"", hr);
goto error;
}
CopyMemory(pbBuffer, pSrcData, SrcSize);
hr = inputBuffer->lpVtbl->SetCurrentLength(inputBuffer, SrcSize);
if (FAILED(hr))
{
WLog_ERR(TAG, "SetCurrentLength failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = inputBuffer->lpVtbl->Unlock(inputBuffer);
if (FAILED(hr))
{
WLog_ERR(TAG, "Unlock failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = sys->MFCreateSample(&inputSample);
if (FAILED(hr))
{
WLog_ERR(TAG, "MFCreateSample failure: 0x%08"PRIX32"", hr);
goto error;
}
inputSample->lpVtbl->AddBuffer(inputSample, inputBuffer);
if (FAILED(hr))
{
WLog_ERR(TAG, "AddBuffer failure: 0x%08"PRIX32"", hr);
goto error;
}
inputBuffer->lpVtbl->Release(inputBuffer);
hr = sys->transform->lpVtbl->ProcessInput(sys->transform, 0, inputSample, 0);
if (FAILED(hr))
{
WLog_ERR(TAG, "ProcessInput failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = mf_create_output_sample(sys);
if (FAILED(hr))
{
WLog_ERR(TAG, "mf_create_output_sample failure: 0x%08"PRIX32"", hr);
goto error;
}
outputDataBuffer.dwStreamID = 0;
outputDataBuffer.dwStatus = 0;
outputDataBuffer.pEvents = NULL;
outputDataBuffer.pSample = sys->outputSample;
2016-08-03 16:29:12 +03:00
hr = sys->transform->lpVtbl->ProcessOutput(sys->transform, 0, 1,
&outputDataBuffer, &outputStatus);
if (hr == MF_E_TRANSFORM_STREAM_CHANGE)
{
2016-03-02 17:16:49 +03:00
BYTE* pTmpYUVData;
int offset = 0;
UINT32 stride = 0;
UINT64 frameSize = 0;
IMFAttributes* attributes = NULL;
if (sys->outputType)
{
sys->outputType->lpVtbl->Release(sys->outputType);
sys->outputType = NULL;
}
hr = mf_find_output_type(sys, &MFVideoFormat_IYUV, &sys->outputType);
if (FAILED(hr))
{
WLog_ERR(TAG, "mf_find_output_type failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->transform->lpVtbl->SetOutputType(sys->transform, 0, sys->outputType,
0);
if (FAILED(hr))
{
WLog_ERR(TAG, "SetOutputType failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = mf_create_output_sample(sys);
if (FAILED(hr))
{
WLog_ERR(TAG, "mf_create_output_sample failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->outputType->lpVtbl->GetUINT64(sys->outputType, &MF_MT_FRAME_SIZE,
&frameSize);
if (FAILED(hr))
{
WLog_ERR(TAG, "GetUINT64(MF_MT_FRAME_SIZE) failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
sys->frameWidth = (UINT32)(frameSize >> 32);
sys->frameHeight = (UINT32) frameSize;
2016-08-03 16:29:12 +03:00
hr = sys->outputType->lpVtbl->GetUINT32(sys->outputType, &MF_MT_DEFAULT_STRIDE,
&stride);
if (FAILED(hr))
{
WLog_ERR(TAG, "GetUINT32(MF_MT_DEFAULT_STRIDE) failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-03-02 17:16:49 +03:00
iStride[0] = stride;
iStride[1] = stride / 2;
iStride[2] = stride / 2;
pTmpYUVData = (BYTE*) calloc(1, 2 * stride * sys->frameHeight);
pYUVData[0] = &pTmpYUVData[offset];
pTmpYUVData += iStride[0] * sys->frameHeight;
pYUVData[1] = &pTmpYUVData[offset];
pTmpYUVData += iStride[1] * (sys->frameHeight / 2);
pYUVData[2] = &pTmpYUVData[offset];
pTmpYUVData += iStride[2] * (sys->frameHeight / 2);
h264->width = sys->frameWidth;
h264->height = sys->frameHeight;
}
else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT)
{
}
else if (FAILED(hr))
{
WLog_ERR(TAG, "ProcessOutput failure: 0x%08"PRIX32"", hr);
goto error;
}
else
{
int offset = 0;
BYTE* buffer = NULL;
DWORD bufferCount = 0;
DWORD cbMaxLength = 0;
DWORD cbCurrentLength = 0;
hr = sys->outputSample->lpVtbl->GetBufferCount(sys->outputSample, &bufferCount);
if (FAILED(hr))
{
WLog_ERR(TAG, "GetBufferCount failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->outputSample->lpVtbl->GetBufferByIndex(sys->outputSample, 0,
&outputBuffer);
if (FAILED(hr))
{
WLog_ERR(TAG, "GetBufferByIndex failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = outputBuffer->lpVtbl->Lock(outputBuffer, &buffer, &cbMaxLength,
&cbCurrentLength);
if (FAILED(hr))
{
WLog_ERR(TAG, "Lock failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-03-02 17:16:49 +03:00
CopyMemory(pYUVData[0], &buffer[offset], iStride[0] * sys->frameHeight);
offset += iStride[0] * sys->frameHeight;
CopyMemory(pYUVData[1], &buffer[offset], iStride[1] * (sys->frameHeight / 2));
offset += iStride[1] * (sys->frameHeight / 2);
CopyMemory(pYUVData[2], &buffer[offset], iStride[2] * (sys->frameHeight / 2));
offset += iStride[2] * (sys->frameHeight / 2);
hr = outputBuffer->lpVtbl->Unlock(outputBuffer);
if (FAILED(hr))
{
WLog_ERR(TAG, "Unlock failure: 0x%08"PRIX32"", hr);
goto error;
}
outputBuffer->lpVtbl->Release(outputBuffer);
}
inputSample->lpVtbl->Release(inputSample);
return 1;
error:
fprintf(stderr, "mf_decompress error\n");
return -1;
}
static int mf_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
{
H264_CONTEXT_MF* sys = (H264_CONTEXT_MF*) h264->pSystemData;
return 1;
}
static void mf_uninit(H264_CONTEXT* h264)
{
2016-03-02 17:16:49 +03:00
UINT32 x;
H264_CONTEXT_MF* sys = (H264_CONTEXT_MF*) h264->pSystemData;
if (sys)
{
if (sys->transform)
{
sys->transform->lpVtbl->Release(sys->transform);
sys->transform = NULL;
}
if (sys->codecApi)
{
sys->codecApi->lpVtbl->Release(sys->codecApi);
sys->codecApi = NULL;
}
if (sys->inputType)
{
sys->inputType->lpVtbl->Release(sys->inputType);
sys->inputType = NULL;
}
if (sys->outputType)
{
sys->outputType->lpVtbl->Release(sys->outputType);
sys->outputType = NULL;
}
if (sys->outputSample)
{
sys->outputSample->lpVtbl->Release(sys->outputSample);
sys->outputSample = NULL;
}
if (sys->mfplat)
{
FreeLibrary(sys->mfplat);
sys->mfplat = NULL;
}
2016-08-03 16:29:12 +03:00
for (x = 0; x < sizeof(h264->pYUVData) / sizeof(h264->pYUVData[0]); x++)
free(h264->pYUVData[x]);
2016-03-02 17:16:49 +03:00
memset(h264->pYUVData, 0, sizeof(h264->pYUVData));
memset(h264->iStride, 0, sizeof(h264->iStride));
sys->MFShutdown();
CoUninitialize();
free(sys);
h264->pSystemData = NULL;
}
}
static BOOL mf_init(H264_CONTEXT* h264)
{
HRESULT hr;
H264_CONTEXT_MF* sys;
sys = (H264_CONTEXT_MF*) calloc(1, sizeof(H264_CONTEXT_MF));
if (!sys)
goto error;
h264->pSystemData = (void*) sys;
/* http://decklink-sdk-delphi.googlecode.com/svn/trunk/Blackmagic%20DeckLink%20SDK%209.7/Win/Samples/Streaming/StreamingPreview/DecoderMF.cpp */
sys->mfplat = LoadLibraryA("mfplat.dll");
if (!sys->mfplat)
goto error;
sys->MFStartup = (pfnMFStartup) GetProcAddress(sys->mfplat, "MFStartup");
sys->MFShutdown = (pfnMFShutdown) GetProcAddress(sys->mfplat, "MFShutdown");
2016-08-03 16:29:12 +03:00
sys->MFCreateSample = (pfnMFCreateSample) GetProcAddress(sys->mfplat,
"MFCreateSample");
sys->MFCreateMemoryBuffer = (pfnMFCreateMemoryBuffer) GetProcAddress(
sys->mfplat, "MFCreateMemoryBuffer");
sys->MFCreateMediaType = (pfnMFCreateMediaType) GetProcAddress(sys->mfplat,
"MFCreateMediaType");
sys->MFCreateDXGIDeviceManager = (pfnMFCreateDXGIDeviceManager) GetProcAddress(
sys->mfplat, "MFCreateDXGIDeviceManager");
if (!sys->MFStartup || !sys->MFShutdown || !sys->MFCreateSample
|| !sys->MFCreateMemoryBuffer ||
!sys->MFCreateMediaType || !sys->MFCreateDXGIDeviceManager)
goto error;
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (h264->Compressor)
{
}
else
{
VARIANT var = { 0 };
hr = sys->MFStartup(MF_VERSION, 0);
if (FAILED(hr))
{
WLog_ERR(TAG, "MFStartup failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = CoCreateInstance(&CLSID_CMSH264DecoderMFT, NULL, CLSCTX_INPROC_SERVER,
&IID_IMFTransform, (void**) &sys->transform);
if (FAILED(hr))
{
WLog_ERR(TAG, "CoCreateInstance(CLSID_CMSH264DecoderMFT) failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->transform->lpVtbl->QueryInterface(sys->transform, &IID_ICodecAPI,
(void**) &sys->codecApi);
if (FAILED(hr))
{
WLog_ERR(TAG, "QueryInterface(IID_ICodecAPI) failure: 0x%08"PRIX32"", hr);
goto error;
}
var.vt = VT_UI4;
var.ulVal = 1;
2016-08-03 16:29:12 +03:00
hr = sys->codecApi->lpVtbl->SetValue(sys->codecApi, &CODECAPI_AVLowLatencyMode,
&var);
if (FAILED(hr))
{
WLog_ERR(TAG, "SetValue(CODECAPI_AVLowLatencyMode) failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = sys->MFCreateMediaType(&sys->inputType);
if (FAILED(hr))
{
WLog_ERR(TAG, "MFCreateMediaType failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->inputType->lpVtbl->SetGUID(sys->inputType, &MF_MT_MAJOR_TYPE,
&MFMediaType_Video);
if (FAILED(hr))
{
WLog_ERR(TAG, "SetGUID(MF_MT_MAJOR_TYPE) failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->inputType->lpVtbl->SetGUID(sys->inputType, &MF_MT_SUBTYPE,
&MFVideoFormat_H264);
if (FAILED(hr))
{
WLog_ERR(TAG, "SetGUID(MF_MT_SUBTYPE) failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = sys->transform->lpVtbl->SetInputType(sys->transform, 0, sys->inputType, 0);
if (FAILED(hr))
{
WLog_ERR(TAG, "SetInputType failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = mf_find_output_type(sys, &MFVideoFormat_IYUV, &sys->outputType);
if (FAILED(hr))
{
WLog_ERR(TAG, "mf_find_output_type failure: 0x%08"PRIX32"", hr);
goto error;
}
2016-08-03 16:29:12 +03:00
hr = sys->transform->lpVtbl->SetOutputType(sys->transform, 0, sys->outputType,
0);
if (FAILED(hr))
{
WLog_ERR(TAG, "SetOutputType failure: 0x%08"PRIX32"", hr);
goto error;
}
hr = mf_create_output_sample(sys);
if (FAILED(hr))
{
WLog_ERR(TAG, "mf_create_output_sample failure: 0x%08"PRIX32"", hr);
goto error;
}
}
2016-08-03 16:29:12 +03:00
return TRUE;
error:
WLog_ERR(TAG, "mf_init failure");
mf_uninit(h264);
return FALSE;
}
static H264_CONTEXT_SUBSYSTEM g_Subsystem_MF =
{
"MediaFoundation",
mf_init,
mf_uninit,
mf_decompress,
mf_compress
};
#endif
2015-07-28 23:01:34 +03:00
/**
* x264 subsystem
*/
#ifdef WITH_X264
#define NAL_UNKNOWN X264_NAL_UNKNOWN
#define NAL_SLICE X264_NAL_SLICE
#define NAL_SLICE_DPA X264_NAL_SLICE_DPA
#define NAL_SLICE_DPB X264_NAL_SLICE_DPB
#define NAL_SLICE_DPC X264_NAL_SLICE_DPC
#define NAL_SLICE_IDR X264_NAL_SLICE_IDR
#define NAL_SEI X264_NAL_SEI
#define NAL_SPS X264_NAL_SPS
#define NAL_PPS X264_NAL_PPS
#define NAL_AUD X264_NAL_AUD
#define NAL_FILLER X264_NAL_FILLER
#define NAL_PRIORITY_DISPOSABLE X264_NAL_PRIORITY_DISPOSABLE
#define NAL_PRIORITY_LOW X264_NAL_PRIORITY_LOW
#define NAL_PRIORITY_HIGH X264_NAL_PRIORITY_HIGH
#define NAL_PRIORITY_HIGHEST X264_NAL_PRIORITY_HIGHEST
#include <stdint.h>
#include <x264.h>
struct _H264_CONTEXT_X264
{
void* dummy;
};
typedef struct _H264_CONTEXT_X264 H264_CONTEXT_X264;
static int x264_decompress(H264_CONTEXT* h264, BYTE* pSrcData, UINT32 SrcSize)
2015-07-28 23:01:34 +03:00
{
//H264_CONTEXT_X264* sys = (H264_CONTEXT_X264*) h264->pSystemData;
return -1;
2015-07-28 23:01:34 +03:00
}
static int x264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
2015-07-28 23:01:34 +03:00
{
//H264_CONTEXT_X264* sys = (H264_CONTEXT_X264*) h264->pSystemData;
return -1;
2015-07-28 23:01:34 +03:00
}
static void x264_uninit(H264_CONTEXT* h264)
{
H264_CONTEXT_X264* sys = (H264_CONTEXT_X264*) h264->pSystemData;
if (sys)
{
free(sys);
h264->pSystemData = NULL;
}
}
static BOOL x264_init(H264_CONTEXT* h264)
{
H264_CONTEXT_X264* sys;
2016-03-02 17:16:49 +03:00
h264->numSystemData = 1;
sys = (H264_CONTEXT_X264*) calloc(h264->numSystemData,
2016-08-03 16:29:12 +03:00
sizeof(H264_CONTEXT_X264));
2015-07-28 23:01:34 +03:00
if (!sys)
{
goto EXCEPTION;
}
h264->pSystemData = (void*) sys;
if (h264->Compressor)
{
}
else
{
}
return TRUE;
EXCEPTION:
x264_uninit(h264);
return FALSE;
}
static H264_CONTEXT_SUBSYSTEM g_Subsystem_x264 =
{
"x264",
x264_init,
x264_uninit,
x264_decompress,
x264_compress
};
#undef NAL_UNKNOWN
#undef NAL_SLICE
#undef NAL_SLICE_DPA
#undef NAL_SLICE_DPB
#undef NAL_SLICE_DPC
#undef NAL_SLICE_IDR
#undef NAL_SEI
#undef NAL_SPS
#undef NAL_PPS
#undef NAL_AUD
#undef NAL_FILLER
#undef NAL_PRIORITY_DISPOSABLE
#undef NAL_PRIORITY_LOW
#undef NAL_PRIORITY_HIGH
#undef NAL_PRIORITY_HIGHEST
#endif
2014-09-06 03:11:03 +04:00
/**
* OpenH264 subsystem
*/
#ifdef WITH_OPENH264
2014-09-06 03:11:03 +04:00
#include "wels/codec_def.h"
#include "wels/codec_api.h"
2016-07-04 13:33:52 +03:00
#include "wels/codec_ver.h"
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR < 3) || (OPENH264_MAJOR < 1)
#error "Unsupported OpenH264 version "OPENH264_MAJOR"."OPENH264_MINOR"."OPENH264_REVISION" detected!"
#elif (OPENH264_MAJOR > 1) || (OPENH264_MINOR > 6)
#warning "Untested OpenH264 version "OPENH264_MAJOR"."OPENH264_MINOR"."OPENH264_REVISION" detected!"
#endif
2014-09-06 03:11:03 +04:00
struct _H264_CONTEXT_OPENH264
{
ISVCDecoder* pDecoder;
2015-02-16 12:51:20 +03:00
ISVCEncoder* pEncoder;
SEncParamExt EncParamExt;
2014-09-06 03:11:03 +04:00
};
typedef struct _H264_CONTEXT_OPENH264 H264_CONTEXT_OPENH264;
static BOOL g_openh264_trace_enabled = FALSE;
2016-08-03 16:29:12 +03:00
static void openh264_trace_callback(H264_CONTEXT* h264, int level,
const char* message)
{
WLog_INFO(TAG, "%d - %s", level, message);
}
static int openh264_decompress(H264_CONTEXT* h264, const BYTE* pSrcData,
UINT32 SrcSize)
{
DECODING_STATE state;
SBufferInfo sBufferInfo;
SSysMEMBuffer* pSystemBuffer;
2014-09-06 03:11:03 +04:00
H264_CONTEXT_OPENH264* sys = (H264_CONTEXT_OPENH264*) h264->pSystemData;
UINT32* iStride = h264->iStride;
BYTE** pYUVData = h264->pYUVData;
2014-09-06 03:11:03 +04:00
if (!sys->pDecoder)
2015-07-28 23:01:34 +03:00
return -2001;
/*
* Decompress the image. The RDP host only seems to send I420 format.
*/
2016-03-02 17:16:49 +03:00
pYUVData[0] = NULL;
pYUVData[1] = NULL;
pYUVData[2] = NULL;
ZeroMemory(&sBufferInfo, sizeof(sBufferInfo));
2016-08-03 16:29:12 +03:00
state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, pSrcData, SrcSize,
pYUVData, &sBufferInfo);
2015-07-28 23:01:34 +03:00
if (sBufferInfo.iBufferStatus != 1)
{
if (state == dsNoParamSets)
{
/* this happens on the first frame due to missing parameter sets */
2016-08-03 16:29:12 +03:00
state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData,
&sBufferInfo);
2015-07-28 23:01:34 +03:00
}
else if (state == dsErrorFree)
{
/* call DecodeFrame2 again to decode without delay */
2016-08-03 16:29:12 +03:00
state = (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, NULL, 0, pYUVData,
&sBufferInfo);
2015-07-28 23:01:34 +03:00
}
else
{
WLog_WARN(TAG, "DecodeFrame2 state: 0x%04X iBufferStatus: %d", state,
2016-08-03 16:29:12 +03:00
sBufferInfo.iBufferStatus);
2015-07-28 23:01:34 +03:00
return -2002;
}
}
2016-03-02 17:16:49 +03:00
pSystemBuffer = &sBufferInfo.UsrData.sSystemBuffer;
iStride[0] = pSystemBuffer->iStride[0];
iStride[1] = pSystemBuffer->iStride[1];
iStride[2] = pSystemBuffer->iStride[1];
if (sBufferInfo.iBufferStatus != 1)
2015-07-28 23:01:34 +03:00
{
WLog_WARN(TAG, "DecodeFrame2 iBufferStatus: %d", sBufferInfo.iBufferStatus);
return 0;
}
if (state != dsErrorFree)
{
WLog_WARN(TAG, "DecodeFrame2 state: 0x%02X", state);
return -2003;
}
#if 0
2016-08-03 16:29:12 +03:00
WLog_INFO(TAG,
"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,
2016-08-03 16:29:12 +03:00
pSystemBuffer->iWidth, pSystemBuffer->iHeight, pSystemBuffer->iFormat,
pSystemBuffer->iStride[0], pSystemBuffer->iStride[1]);
#endif
if (pSystemBuffer->iFormat != videoFormatI420)
2015-07-28 23:01:34 +03:00
return -2004;
2016-03-02 17:16:49 +03:00
if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
2015-07-28 23:01:34 +03:00
return -2005;
return 1;
}
2016-08-03 16:29:12 +03:00
static int openh264_compress(H264_CONTEXT* h264, BYTE** ppDstData,
UINT32* pDstSize)
2015-02-16 12:51:20 +03:00
{
int i, j;
int status;
SFrameBSInfo info;
SSourcePicture pic;
SBitrateInfo bitrate;
2016-03-02 17:16:49 +03:00
H264_CONTEXT_OPENH264* sys;
BYTE** pYUVData = h264->pYUVData;
UINT32* iStride = h264->iStride;
2016-03-02 17:16:49 +03:00
sys = &((H264_CONTEXT_OPENH264*) h264->pSystemData)[0];
2015-02-16 12:51:20 +03:00
if (!sys->pEncoder)
return -1;
2016-03-02 17:16:49 +03:00
if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
2015-02-16 12:51:20 +03:00
return -1;
2016-08-03 16:29:12 +03:00
if ((sys->EncParamExt.iPicWidth != h264->width)
|| (sys->EncParamExt.iPicHeight != h264->height))
2015-02-16 12:51:20 +03:00
{
status = (*sys->pEncoder)->GetDefaultParams(sys->pEncoder, &sys->EncParamExt);
2015-02-16 12:51:20 +03:00
if (status < 0)
{
WLog_ERR(TAG, "Failed to get OpenH264 default parameters (status=%d)", status);
return status;
}
sys->EncParamExt.iUsageType = SCREEN_CONTENT_REAL_TIME;
sys->EncParamExt.iPicWidth = h264->width;
sys->EncParamExt.iPicHeight = h264->height;
sys->EncParamExt.fMaxFrameRate = h264->FrameRate;
sys->EncParamExt.iMaxBitrate = UNSPECIFIED_BIT_RATE;
sys->EncParamExt.bEnableDenoise = 0;
sys->EncParamExt.bEnableLongTermReference = 0;
sys->EncParamExt.bEnableFrameSkip = 0;
sys->EncParamExt.iSpatialLayerNum = 1;
sys->EncParamExt.iMultipleThreadIdc = h264->NumberOfThreads;
sys->EncParamExt.sSpatialLayers[0].fFrameRate = h264->FrameRate;
sys->EncParamExt.sSpatialLayers[0].iVideoWidth = sys->EncParamExt.iPicWidth;
sys->EncParamExt.sSpatialLayers[0].iVideoHeight = sys->EncParamExt.iPicHeight;
2016-08-03 16:29:12 +03:00
sys->EncParamExt.sSpatialLayers[0].iMaxSpatialBitrate =
sys->EncParamExt.iMaxBitrate;
2015-07-28 23:01:34 +03:00
switch (h264->RateControlMode)
{
2016-08-03 16:29:12 +03:00
case H264_RATECONTROL_VBR:
sys->EncParamExt.iRCMode = RC_BITRATE_MODE;
sys->EncParamExt.iTargetBitrate = h264->BitRate;
sys->EncParamExt.sSpatialLayers[0].iSpatialBitrate =
sys->EncParamExt.iTargetBitrate;
break;
2015-07-28 23:01:34 +03:00
2016-08-03 16:29:12 +03:00
case H264_RATECONTROL_CQP:
sys->EncParamExt.iRCMode = RC_OFF_MODE;
sys->EncParamExt.sSpatialLayers[0].iDLayerQp = h264->QP;
break;
}
if (sys->EncParamExt.iMultipleThreadIdc > 1)
{
2016-07-04 13:33:52 +03:00
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
sys->EncParamExt.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE;
2016-07-04 13:33:52 +03:00
#endif
}
status = (*sys->pEncoder)->InitializeExt(sys->pEncoder, &sys->EncParamExt);
2015-02-16 12:51:20 +03:00
if (status < 0)
{
WLog_ERR(TAG, "Failed to initialize OpenH264 encoder (status=%d)", status);
2015-02-16 12:51:20 +03:00
return status;
}
2016-08-03 16:29:12 +03:00
status = (*sys->pEncoder)->GetOption(sys->pEncoder,
ENCODER_OPTION_SVC_ENCODE_PARAM_EXT,
&sys->EncParamExt);
if (status < 0)
{
WLog_ERR(TAG, "Failed to get initial OpenH264 encoder parameters (status=%d)",
2016-08-03 16:29:12 +03:00
status);
return status;
}
2015-02-16 12:51:20 +03:00
}
else
2015-02-16 12:51:20 +03:00
{
switch (h264->RateControlMode)
2015-02-16 12:51:20 +03:00
{
2016-08-03 16:29:12 +03:00
case H264_RATECONTROL_VBR:
if (sys->EncParamExt.iTargetBitrate != h264->BitRate)
{
2016-08-03 16:29:12 +03:00
sys->EncParamExt.iTargetBitrate = h264->BitRate;
bitrate.iLayer = SPATIAL_LAYER_ALL;
bitrate.iBitrate = h264->BitRate;
status = (*sys->pEncoder)->SetOption(sys->pEncoder, ENCODER_OPTION_BITRATE,
&bitrate);
if (status < 0)
{
WLog_ERR(TAG, "Failed to set encoder bitrate (status=%d)", status);
2016-08-03 16:29:12 +03:00
return status;
}
}
2016-08-03 16:29:12 +03:00
if (sys->EncParamExt.fMaxFrameRate != h264->FrameRate)
2016-03-02 17:16:49 +03:00
{
2016-08-03 16:29:12 +03:00
sys->EncParamExt.fMaxFrameRate = h264->FrameRate;
status = (*sys->pEncoder)->SetOption(sys->pEncoder, ENCODER_OPTION_FRAME_RATE,
&sys->EncParamExt.fMaxFrameRate);
if (status < 0)
{
WLog_ERR(TAG, "Failed to set encoder framerate (status=%d)", status);
2016-08-03 16:29:12 +03:00
return status;
}
}
2016-08-03 16:29:12 +03:00
break;
2016-08-03 16:29:12 +03:00
case H264_RATECONTROL_CQP:
if (sys->EncParamExt.sSpatialLayers[0].iDLayerQp != h264->QP)
2016-03-02 17:16:49 +03:00
{
2016-08-03 16:29:12 +03:00
sys->EncParamExt.sSpatialLayers[0].iDLayerQp = h264->QP;
status = (*sys->pEncoder)->SetOption(sys->pEncoder,
ENCODER_OPTION_SVC_ENCODE_PARAM_EXT,
&sys->EncParamExt);
if (status < 0)
{
WLog_ERR(TAG, "Failed to set encoder parameters (status=%d)", status);
2016-08-03 16:29:12 +03:00
return status;
}
}
2016-08-03 16:29:12 +03:00
break;
2015-02-16 12:51:20 +03:00
}
}
memset(&info, 0, sizeof(SFrameBSInfo));
memset(&pic, 0, sizeof(SSourcePicture));
pic.iPicWidth = h264->width;
pic.iPicHeight = h264->height;
pic.iColorFormat = videoFormatI420;
2016-03-02 17:16:49 +03:00
pic.iStride[0] = iStride[0];
pic.iStride[1] = iStride[1];
pic.iStride[2] = iStride[2];
pic.pData[0] = pYUVData[0];
pic.pData[1] = pYUVData[1];
pic.pData[2] = pYUVData[2];
2015-02-16 12:51:20 +03:00
status = (*sys->pEncoder)->EncodeFrame(sys->pEncoder, &pic, &info);
if (status < 0)
{
WLog_ERR(TAG, "Failed to encode frame (status=%d)", status);
2015-02-16 12:51:20 +03:00
return status;
}
2015-07-28 23:01:34 +03:00
2015-02-16 12:51:20 +03:00
*ppDstData = info.sLayerInfo[0].pBsBuf;
*pDstSize = 0;
2015-07-28 23:01:34 +03:00
2015-02-16 12:51:20 +03:00
for (i = 0; i < info.iLayerNum; i++)
{
for (j = 0; j < info.sLayerInfo[i].iNalCount; j++)
{
*pDstSize += info.sLayerInfo[i].pNalLengthInByte[j];
}
}
return 1;
}
2014-09-06 03:11:03 +04:00
static void openh264_uninit(H264_CONTEXT* h264)
{
2016-03-02 17:16:49 +03:00
UINT32 x;
H264_CONTEXT_OPENH264* sysContexts = (H264_CONTEXT_OPENH264*) h264->pSystemData;
2014-09-06 03:11:03 +04:00
2016-03-02 17:16:49 +03:00
if (sysContexts)
{
2016-08-03 16:29:12 +03:00
for (x = 0; x < h264->numSystemData; x++)
2014-09-06 03:11:03 +04:00
{
2016-03-02 17:16:49 +03:00
H264_CONTEXT_OPENH264* sys = &sysContexts[x];
2014-09-06 03:11:03 +04:00
2016-03-02 17:16:49 +03:00
if (sys->pDecoder)
{
(*sys->pDecoder)->Uninitialize(sys->pDecoder);
WelsDestroyDecoder(sys->pDecoder);
sys->pDecoder = NULL;
}
2015-02-16 12:51:20 +03:00
2016-03-02 17:16:49 +03:00
if (sys->pEncoder)
{
(*sys->pEncoder)->Uninitialize(sys->pEncoder);
WelsDestroySVCEncoder(sys->pEncoder);
sys->pEncoder = NULL;
}
}
2016-08-03 16:29:12 +03:00
2016-03-02 17:16:49 +03:00
free(h264->pSystemData);
2014-09-06 03:11:03 +04:00
h264->pSystemData = NULL;
}
}
static BOOL openh264_init(H264_CONTEXT* h264)
{
2016-03-02 17:16:49 +03:00
UINT32 x;
2014-09-06 03:11:03 +04:00
long status;
SDecodingParam sDecParam;
2016-03-02 17:16:49 +03:00
H264_CONTEXT_OPENH264* sysContexts;
static int traceLevel = WELS_LOG_DEBUG;
2016-07-04 13:33:52 +03:00
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
2014-09-06 03:11:03 +04:00
static EVideoFormatType videoFormat = videoFormatI420;
2016-07-04 13:33:52 +03:00
#endif
2016-08-03 16:29:12 +03:00
static WelsTraceCallback traceCallback = (WelsTraceCallback)
openh264_trace_callback;
2016-03-02 17:16:49 +03:00
h264->numSystemData = 1;
sysContexts = (H264_CONTEXT_OPENH264*) calloc(h264->numSystemData,
2016-08-03 16:29:12 +03:00
sizeof(H264_CONTEXT_OPENH264));
2016-03-02 17:16:49 +03:00
if (!sysContexts)
2014-09-06 03:11:03 +04:00
goto EXCEPTION;
2016-03-02 17:16:49 +03:00
h264->pSystemData = (void*) sysContexts;
2016-08-03 16:29:12 +03:00
for (x = 0; x < h264->numSystemData; x++)
{
2016-03-02 17:16:49 +03:00
H264_CONTEXT_OPENH264* sys = &sysContexts[x];
2016-03-02 17:16:49 +03:00
if (h264->Compressor)
2015-02-16 12:51:20 +03:00
{
2016-03-02 17:16:49 +03:00
WelsCreateSVCEncoder(&sys->pEncoder);
2016-03-02 17:16:49 +03:00
if (!sys->pEncoder)
{
WLog_ERR(TAG, "Failed to create OpenH264 encoder");
goto EXCEPTION;
}
2015-02-16 12:51:20 +03:00
}
2016-03-02 17:16:49 +03:00
else
{
2016-03-02 17:16:49 +03:00
WelsCreateDecoder(&sys->pDecoder);
2016-03-02 17:16:49 +03:00
if (!sys->pDecoder)
{
WLog_ERR(TAG, "Failed to create OpenH264 decoder");
goto EXCEPTION;
}
2014-09-06 03:11:03 +04:00
2016-03-02 17:16:49 +03:00
ZeroMemory(&sDecParam, sizeof(sDecParam));
2016-07-04 13:33:52 +03:00
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
2016-03-02 17:16:49 +03:00
sDecParam.eOutputColorFormat = videoFormatI420;
2016-07-04 13:33:52 +03:00
#endif
2016-03-02 17:16:49 +03:00
sDecParam.eEcActiveIdc = ERROR_CON_FRAME_COPY;
sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
status = (*sys->pDecoder)->Initialize(sys->pDecoder, &sDecParam);
2015-02-16 12:51:20 +03:00
if (status != 0)
{
2016-03-02 17:16:49 +03:00
WLog_ERR(TAG, "Failed to initialize OpenH264 decoder (status=%ld)",
2016-08-03 16:29:12 +03:00
status);
2016-03-02 17:16:49 +03:00
goto EXCEPTION;
2015-02-16 12:51:20 +03:00
}
2016-08-03 16:29:12 +03:00
2016-07-04 13:33:52 +03:00
#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
2016-03-02 17:16:49 +03:00
status = (*sys->pDecoder)->SetOption(
2016-08-03 16:29:12 +03:00
sys->pDecoder, DECODER_OPTION_DATAFORMAT,
&videoFormat);
2016-07-04 13:33:52 +03:00
#endif
2016-08-03 16:29:12 +03:00
2015-02-16 12:51:20 +03:00
if (status != 0)
{
2016-08-03 16:29:12 +03:00
WLog_ERR(TAG,
"Failed to set data format option on OpenH264 decoder (status=%ld)",
status);
2016-03-02 17:16:49 +03:00
goto EXCEPTION;
2015-02-16 12:51:20 +03:00
}
2016-03-02 17:16:49 +03:00
if (g_openh264_trace_enabled)
2015-02-16 12:51:20 +03:00
{
2016-03-02 17:16:49 +03:00
status = (*sys->pDecoder)->SetOption(
2016-08-03 16:29:12 +03:00
sys->pDecoder, DECODER_OPTION_TRACE_LEVEL,
&traceLevel);
2016-03-02 17:16:49 +03:00
if (status != 0)
{
2016-08-03 16:29:12 +03:00
WLog_ERR(TAG,
"Failed to set trace level option on OpenH264 decoder (status=%ld)",
status);
2016-03-02 17:16:49 +03:00
goto EXCEPTION;
}
status = (*sys->pDecoder)->SetOption(
2016-08-03 16:29:12 +03:00
sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK,
&traceCallback);
2016-03-02 17:16:49 +03:00
if (status != 0)
{
2016-08-03 16:29:12 +03:00
WLog_ERR(TAG,
"Failed to set trace callback option on OpenH264 decoder (status=%ld)",
status);
2016-03-02 17:16:49 +03:00
goto EXCEPTION;
}
status = (*sys->pDecoder)->SetOption(
2016-08-03 16:29:12 +03:00
sys->pDecoder,
DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
&h264);
2016-03-02 17:16:49 +03:00
if (status != 0)
{
2016-08-03 16:29:12 +03:00
WLog_ERR(TAG,
"Failed to set trace callback context option on OpenH264 decoder (status=%ld)",
status);
2016-03-02 17:16:49 +03:00
goto EXCEPTION;
}
2015-02-16 12:51:20 +03:00
}
}
}
return TRUE;
EXCEPTION:
2014-09-06 03:11:03 +04:00
openh264_uninit(h264);
return FALSE;
}
2014-09-06 03:11:03 +04:00
static H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264 =
{
"OpenH264",
openh264_init,
openh264_uninit,
2015-02-16 12:51:20 +03:00
openh264_decompress,
openh264_compress
2014-09-06 03:11:03 +04:00
};
#endif
2014-09-06 03:11:03 +04:00
/**
* libavcodec subsystem
*/
2014-09-06 03:11:03 +04:00
#ifdef WITH_LIBAVCODEC
2014-09-06 03:11:03 +04:00
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
/* Fallback support for older libavcodec versions */
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
#define AV_CODEC_ID_H264 CODEC_ID_H264
#endif
2014-09-06 03:11:03 +04:00
struct _H264_CONTEXT_LIBAVCODEC
{
AVCodec* codec;
AVCodecContext* codecContext;
AVCodecParserContext* codecParser;
AVFrame* videoFrame;
};
typedef struct _H264_CONTEXT_LIBAVCODEC H264_CONTEXT_LIBAVCODEC;
static int libavcodec_decompress(H264_CONTEXT* h264, const BYTE* pSrcData,
UINT32 SrcSize)
{
int status;
2014-09-06 03:11:03 +04:00
int gotFrame = 0;
AVPacket packet;
H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*) h264->pSystemData;
BYTE** pYUVData = h264->pYUVData;
UINT32* iStride = h264->iStride;
av_init_packet(&packet);
packet.data = (BYTE*)pSrcData;
packet.size = SrcSize;
/* avcodec_decode_video2 is deprecated with libavcodec 57.48.101 */
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
status = avcodec_send_packet(sys->codecContext, &packet);
if (status < 0)
{
WLog_ERR(TAG, "Failed to decode video frame (status=%d)", status);
return -1;
}
do
{
status = avcodec_receive_frame(sys->codecContext, sys->videoFrame);
}
while (status == AVERROR(EAGAIN));
gotFrame = (status == 0);
#else
2016-08-03 16:29:12 +03:00
status = avcodec_decode_video2(sys->codecContext, sys->videoFrame, &gotFrame,
&packet);
#endif
if (status < 0)
{
WLog_ERR(TAG, "Failed to decode video frame (status=%d)", status);
return -1;
}
#if 0
2016-08-03 16:29:12 +03:00
WLog_INFO(TAG,
"libavcodec_decompress: frame decoded (status=%d, gotFrame=%d, width=%d, height=%d, Y=[%p,%d], U=[%p,%d], V=[%p,%d])",
status, gotFrame, sys->videoFrame->width, sys->videoFrame->height,
(void*) sys->videoFrame->data[0], sys->videoFrame->linesize[0],
(void*) sys->videoFrame->data[1], sys->videoFrame->linesize[1],
(void*) sys->videoFrame->data[2], sys->videoFrame->linesize[2]);
#endif
if (gotFrame)
{
2016-03-02 17:16:49 +03:00
pYUVData[0] = sys->videoFrame->data[0];
pYUVData[1] = sys->videoFrame->data[1];
pYUVData[2] = sys->videoFrame->data[2];
iStride[0] = sys->videoFrame->linesize[0];
iStride[1] = sys->videoFrame->linesize[1];
iStride[2] = sys->videoFrame->linesize[2];
h264->width = sys->videoFrame->width;
h264->height = sys->videoFrame->height;
}
else
return -2;
return 1;
}
static int libavcodec_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize)
2017-02-14 18:34:02 +03:00
{
return -1;
}
2014-09-06 03:11:03 +04:00
static void libavcodec_uninit(H264_CONTEXT* h264)
{
2014-09-06 03:11:03 +04:00
H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*) h264->pSystemData;
if (!sys)
return;
if (sys->videoFrame)
{
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
av_frame_free(&sys->videoFrame);
#else
2017-02-14 18:34:02 +03:00
av_free(sys->videoFrame);
#endif
}
2014-09-06 03:11:03 +04:00
if (sys->codecParser)
{
2014-09-06 03:11:03 +04:00
av_parser_close(sys->codecParser);
}
2014-09-06 03:11:03 +04:00
if (sys->codecContext)
{
2014-09-06 03:11:03 +04:00
avcodec_close(sys->codecContext);
2017-02-14 18:34:02 +03:00
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
avcodec_free_context(&sys->codecContext);
#else
2014-09-06 03:11:03 +04:00
av_free(sys->codecContext);
2017-02-14 18:34:02 +03:00
#endif
}
2014-09-06 03:11:03 +04:00
free(sys);
h264->pSystemData = NULL;
}
static BOOL libavcodec_init(H264_CONTEXT* h264)
{
2014-09-06 03:11:03 +04:00
H264_CONTEXT_LIBAVCODEC* sys;
sys = (H264_CONTEXT_LIBAVCODEC*) calloc(1, sizeof(H264_CONTEXT_LIBAVCODEC));
if (!sys)
{
goto EXCEPTION;
}
h264->pSystemData = (void*) sys;
avcodec_register_all();
sys->codec = avcodec_find_decoder(AV_CODEC_ID_H264);
2014-09-06 03:11:03 +04:00
if (!sys->codec)
{
WLog_ERR(TAG, "Failed to find libav H.264 codec");
goto EXCEPTION;
}
2014-09-06 03:11:03 +04:00
sys->codecContext = avcodec_alloc_context3(sys->codec);
if (!sys->codecContext)
{
WLog_ERR(TAG, "Failed to allocate libav codec context");
goto EXCEPTION;
}
2014-09-06 03:11:03 +04:00
if (sys->codec->capabilities & CODEC_CAP_TRUNCATED)
{
2014-09-06 03:11:03 +04:00
sys->codecContext->flags |= CODEC_FLAG_TRUNCATED;
}
2014-09-06 03:11:03 +04:00
if (avcodec_open2(sys->codecContext, sys->codec, NULL) < 0)
{
WLog_ERR(TAG, "Failed to open libav codec");
goto EXCEPTION;
}
sys->codecParser = av_parser_init(AV_CODEC_ID_H264);
2014-09-06 03:11:03 +04:00
if (!sys->codecParser)
{
WLog_ERR(TAG, "Failed to initialize libav parser");
goto EXCEPTION;
}
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
sys->videoFrame = av_frame_alloc();
#else
2014-09-06 03:11:03 +04:00
sys->videoFrame = avcodec_alloc_frame();
#endif
2014-09-06 03:11:03 +04:00
if (!sys->videoFrame)
{
WLog_ERR(TAG, "Failed to allocate libav frame");
goto EXCEPTION;
}
return TRUE;
EXCEPTION:
2014-09-06 03:11:03 +04:00
libavcodec_uninit(h264);
return FALSE;
}
2014-09-06 03:11:03 +04:00
static H264_CONTEXT_SUBSYSTEM g_Subsystem_libavcodec =
{
"libavcodec",
libavcodec_init,
libavcodec_uninit,
2017-02-14 18:34:02 +03:00
libavcodec_decompress,
libavcodec_compress
2014-09-06 03:11:03 +04:00
};
#endif
2016-03-02 17:16:49 +03:00
static BOOL check_rect(const H264_CONTEXT* h264, const RECTANGLE_16* rect,
2016-08-03 16:29:12 +03:00
UINT32 nDstWidth, UINT32 nDstHeight)
{
2016-03-02 17:16:49 +03:00
/* Check, if the output rectangle is valid in decoded h264 frame. */
if ((rect->right > h264->width) || (rect->left > h264->width))
return FALSE;
2016-08-03 16:29:12 +03:00
2016-03-02 17:16:49 +03:00
if ((rect->top > h264->height) || (rect->bottom > h264->height))
return FALSE;
/* Check, if the output rectangle is valid in destination buffer. */
if ((rect->right > nDstWidth) || (rect->left > nDstWidth))
return FALSE;
2016-08-03 16:29:12 +03:00
2016-03-02 17:16:49 +03:00
if ((rect->bottom > nDstHeight) || (rect->top > nDstHeight))
return FALSE;
return TRUE;
}
static BOOL avc_yuv_to_rgb(H264_CONTEXT* h264, const RECTANGLE_16* regionRects,
2016-08-03 16:29:12 +03:00
UINT32 numRegionRects, UINT32 nDstWidth,
UINT32 nDstHeight, UINT32 nDstStep, BYTE* pDstData,
DWORD DstFormat, BOOL use444)
2016-03-02 17:16:49 +03:00
{
UINT32 x;
BYTE* pDstPoint;
prim_size_t roi;
int width, height;
2016-03-02 17:16:49 +03:00
const BYTE* pYUVPoint[3];
2015-07-28 23:01:34 +03:00
primitives_t* prims = primitives_get();
2016-08-03 16:29:12 +03:00
for (x = 0; x < numRegionRects; x++)
2016-03-02 17:16:49 +03:00
{
const RECTANGLE_16* rect = &(regionRects[x]);
const UINT32* iStride;
BYTE** ppYUVData;
2016-03-02 17:16:49 +03:00
if (use444)
{
iStride = h264->iYUV444Stride;
ppYUVData = h264->pYUV444Data;
}
else
{
iStride = h264->iStride;
ppYUVData = h264->pYUVData;
2016-03-02 17:16:49 +03:00
}
2016-03-02 17:16:49 +03:00
if (!check_rect(h264, rect, nDstWidth, nDstHeight))
2016-08-10 11:29:59 +03:00
return FALSE;
width = rect->right - rect->left;
height = rect->bottom - rect->top;
pDstPoint = pDstData + rect->top * nDstStep + rect->left * 4;
2016-03-02 17:16:49 +03:00
pYUVPoint[0] = ppYUVData[0] + rect->top * iStride[0] + rect->left;
pYUVPoint[1] = ppYUVData[1];
pYUVPoint[2] = ppYUVData[2];
2016-08-03 16:29:12 +03:00
2016-03-02 17:16:49 +03:00
if (use444)
{
pYUVPoint[1] += rect->top * iStride[1] + rect->left;
pYUVPoint[2] += rect->top * iStride[2] + rect->left;
}
else
{
2016-08-03 16:29:12 +03:00
pYUVPoint[1] += rect->top / 2 * iStride[1] + rect->left / 2;
pYUVPoint[2] += rect->top / 2 * iStride[2] + rect->left / 2;
2016-03-02 17:16:49 +03:00
}
roi.width = width;
roi.height = height;
2016-03-02 17:16:49 +03:00
if (use444)
{
if (prims->YUV444ToRGB_8u_P3AC4R(
2016-08-03 16:29:12 +03:00
pYUVPoint, iStride, pDstPoint, nDstStep,
DstFormat, &roi) != PRIMITIVES_SUCCESS)
2016-03-02 17:16:49 +03:00
{
return FALSE;
}
}
else
{
if (prims->YUV420ToRGB_8u_P3AC4R(
2016-08-03 16:29:12 +03:00
pYUVPoint, iStride, pDstPoint, nDstStep,
DstFormat, &roi) != PRIMITIVES_SUCCESS)
2016-03-02 17:16:49 +03:00
return FALSE;
}
}
2016-03-02 17:16:49 +03:00
return TRUE;
}
INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize,
2016-08-03 16:29:12 +03:00
BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep,
UINT32 nDstWidth, UINT32 nDstHeight,
RECTANGLE_16* regionRects, UINT32 numRegionRects)
2016-03-02 17:16:49 +03:00
{
int status;
if (!h264)
return -1001;
status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
2016-03-02 17:16:49 +03:00
if (status == 0)
return 1;
if (status < 0)
return status;
if (!avc_yuv_to_rgb(h264, regionRects, numRegionRects, nDstWidth,
2016-08-03 16:29:12 +03:00
nDstHeight, nDstStep, pDstData, DstFormat, FALSE))
2016-03-02 17:16:49 +03:00
return -1002;
return 1;
}
INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat,
2016-08-03 16:29:12 +03:00
UINT32 nSrcStep, UINT32 nSrcWidth, UINT32 nSrcHeight,
BYTE** ppDstData, UINT32* pDstSize)
{
int status = -1;
2015-02-16 12:51:20 +03:00
prim_size_t roi;
int nWidth, nHeight;
2015-07-28 23:01:34 +03:00
primitives_t* prims = primitives_get();
2016-03-02 17:16:49 +03:00
UINT32* iStride;
BYTE** pYUVData;
2015-02-16 12:51:20 +03:00
if (!h264)
return -1;
2016-03-02 17:16:49 +03:00
2015-02-16 12:51:20 +03:00
if (!h264->subsystem->Compress)
return -1;
iStride = h264->iStride;
pYUVData = h264->pYUVData;
2015-02-16 12:51:20 +03:00
nWidth = (nSrcWidth + 1) & ~1;
nHeight = (nSrcHeight + 1) & ~1;
if (!(pYUVData[0] = (BYTE*) _aligned_malloc(nWidth * nHeight, 16)))
return -1;
2016-08-03 16:29:12 +03:00
2016-03-02 17:16:49 +03:00
iStride[0] = nWidth;
if (!(pYUVData[1] = (BYTE*) _aligned_malloc(nWidth * nHeight, 16)))
goto error_1;
2016-08-03 16:29:12 +03:00
2016-03-02 17:16:49 +03:00
iStride[1] = nWidth / 2;
if (!(pYUVData[2] = (BYTE*) _aligned_malloc(nWidth * nHeight, 16)))
goto error_2;
2016-08-03 16:29:12 +03:00
iStride[2] = nWidth / 2;
2015-02-16 12:51:20 +03:00
roi.width = nSrcWidth;
roi.height = nSrcHeight;
2016-08-03 16:29:12 +03:00
prims->RGBToYUV420_8u_P3AC4R(pSrcData, SrcFormat, nSrcStep, pYUVData, iStride,
&roi);
status = h264->subsystem->Compress(h264, ppDstData, pDstSize);
_aligned_free(pYUVData[2]);
2016-03-02 17:16:49 +03:00
pYUVData[2] = NULL;
error_2:
_aligned_free(pYUVData[1]);
2016-03-02 17:16:49 +03:00
pYUVData[1] = NULL;
error_1:
_aligned_free(pYUVData[0]);
2016-03-02 17:16:49 +03:00
pYUVData[0] = NULL;
return status;
}
INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat,
2016-08-03 16:29:12 +03:00
UINT32 nSrcStep, UINT32 nSrcWidth, UINT32 nSrcHeight,
BYTE* op, BYTE** ppDstData, UINT32* pDstSize,
BYTE** ppAuxDstData, UINT32* pAuxDstSize)
2016-03-02 17:16:49 +03:00
{
return -1;
}
static BOOL avc444_ensure_buffer(H264_CONTEXT* h264,
DWORD nDstHeight)
2016-03-02 17:16:49 +03:00
{
UINT32 x;
const UINT32* piMainStride = h264->iStride;
2016-03-02 17:16:49 +03:00
UINT32* piDstSize = h264->iYUV444Size;
UINT32* piDstStride = h264->iYUV444Stride;
BYTE** ppYUVDstData = h264->pYUV444Data;
UINT32 padDstHeight = nDstHeight + 16; /* Need alignment to 16x16 blocks */
if ((piMainStride[0] != piDstStride[0]) ||
(piDstSize[0] != piMainStride[0] * padDstHeight))
{
2016-08-03 16:29:12 +03:00
for (x = 0; x < 3; x++)
2016-03-02 17:16:49 +03:00
{
piDstStride[x] = piMainStride[0];
piDstSize[x] = piDstStride[x] * padDstHeight;
_aligned_free(ppYUVDstData[x]);
ppYUVDstData[x] = _aligned_malloc(piDstSize[x], 16);
2016-03-02 17:16:49 +03:00
if (!ppYUVDstData[x])
2016-03-02 17:16:49 +03:00
goto fail;
memset(ppYUVDstData[x], 0, piDstSize[x]);
}
}
2016-08-03 16:29:12 +03:00
for (x = 0; x < 3; x++)
2016-03-02 17:16:49 +03:00
{
if (!ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
{
WLog_ERR(TAG, "YUV buffer not initialized! check your decoder settings");
goto fail;
}
}
return TRUE;
fail:
_aligned_free(ppYUVDstData[0]);
_aligned_free(ppYUVDstData[1]);
_aligned_free(ppYUVDstData[2]);
2016-03-02 17:16:49 +03:00
ppYUVDstData[0] = NULL;
ppYUVDstData[1] = NULL;
ppYUVDstData[2] = NULL;
return FALSE;
}
static BOOL avc444_process_rects(H264_CONTEXT* h264, const BYTE* pSrcData,
UINT32 SrcSize, BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
UINT32 nDstWidth, UINT32 nDstHeight,
const RECTANGLE_16* rects, UINT32 nrRects,
BOOL main)
{
const primitives_t* prims = primitives_get();
UINT32 x;
const BYTE* pYUVPoint[3] = { NULL, NULL, NULL };
BYTE* pYUVDstPoint[3];
UINT32* piDstStride = h264->iYUV444Stride;
BYTE** ppYUVDstData = h264->pYUV444Data;
const UINT32* piStride = h264->iStride;
BYTE** ppYUVData = h264->pYUVData;
if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0)
return FALSE;
if (!avc444_ensure_buffer(h264, nDstHeight))
return FALSE;
for (x = 0; x < nrRects; x++)
{
const RECTANGLE_16* rect = &rects[x];
prim_size_t roi;
if (!check_rect(h264, rect, nDstWidth, nDstHeight))
continue;
pYUVPoint[0] = ppYUVData[0] + rect->top * piStride[0] +
rect->left;
pYUVPoint[1] = ppYUVData[1] + rect->top / 2 * piStride[1] +
rect->left / 2;
pYUVPoint[2] = ppYUVData[2] + rect->top / 2 * piStride[2] +
rect->left / 2;
pYUVDstPoint[0] = ppYUVDstData[0] + rect->top * piDstStride[0] +
rect->left;
pYUVDstPoint[1] = ppYUVDstData[1] + rect->top * piDstStride[1] +
rect->left;
pYUVDstPoint[2] = ppYUVDstData[2] + rect->top * piDstStride[2] +
rect->left;
roi.width = rect->right - rect->left + 1;
roi.height = rect->bottom - rect->top + 1;
if (main)
{
if (prims->YUV420CombineToYUV444(pYUVPoint, piStride,
NULL, NULL,
pYUVDstPoint, piDstStride,
&roi) != PRIMITIVES_SUCCESS)
return FALSE;
}
else
{
if (prims->YUV420CombineToYUV444(NULL, NULL,
pYUVPoint, piStride,
pYUVDstPoint, piDstStride,
&roi) != PRIMITIVES_SUCCESS)
return FALSE;
}
}
if (!avc_yuv_to_rgb(h264, rects, nrRects, nDstWidth,
nDstHeight, nDstStep, pDstData, DstFormat, TRUE))
return FALSE;
return TRUE;
}
2016-03-02 17:16:49 +03:00
#if defined(AVC444_FRAME_STAT)
static UINT64 op1 = 0;
static double op1sum = 0;
static UINT64 op2 = 0;
static double op2sum = 0;
static UINT64 op3 = 0;
static double op3sum = 0;
static double avg(UINT64* count, double old, double size)
{
double tmp = size + *count * old;
(*count)++;
tmp = tmp / *count;
return tmp;
}
#endif
INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op,
2016-08-03 16:29:12 +03:00
RECTANGLE_16* regionRects, UINT32 numRegionRects,
const BYTE* pSrcData, UINT32 SrcSize,
2016-08-03 16:29:12 +03:00
RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
const BYTE* pAuxSrcData, UINT32 AuxSrcSize,
2016-08-03 16:29:12 +03:00
BYTE* pDstData, DWORD DstFormat,
UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight)
2016-03-02 17:16:49 +03:00
{
INT32 status = -1;
if (!h264 || !regionRects ||
!pSrcData || !pDstData)
return -1001;
2016-08-03 16:29:12 +03:00
switch (op)
2016-03-02 17:16:49 +03:00
{
2016-08-03 16:29:12 +03:00
case 0: /* YUV420 in stream 1
2016-03-02 17:16:49 +03:00
* Chroma420 in stream 2 */
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
nDstHeight,
regionRects, numRegionRects, TRUE))
status = -1;
else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat, nDstStep,
nDstWidth, nDstHeight,
auxRegionRects, numAuxRegionRect, FALSE))
status = -1;
else
status = 0;
2016-08-03 16:29:12 +03:00
break;
case 2: /* Chroma420 in stream 1 */
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
nDstHeight,
regionRects, numRegionRects, FALSE))
status = -1;
else
status = 0;
2016-08-03 16:29:12 +03:00
break;
case 1: /* YUV420 in stream 1 */
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
nDstHeight,
regionRects, numRegionRects, TRUE))
status = -1;
else
status = 0;
2016-08-03 16:29:12 +03:00
break;
default: /* WTF? */
break;
2016-03-02 17:16:49 +03:00
}
#if defined(AVC444_FRAME_STAT)
2016-08-03 16:29:12 +03:00
switch (op)
2016-03-02 17:16:49 +03:00
{
2016-08-03 16:29:12 +03:00
case 0:
op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
break;
case 1:
op2sum = avg(&op2, op2sum, SrcSize);
break;
case 2:
op3sum = avg(&op3, op3sum, SrcSize);
break;
default:
break;
2016-03-02 17:16:49 +03:00
}
2016-08-03 16:29:12 +03:00
WLog_INFO(TAG,
"luma=%"PRIu64" [avg=%lf] chroma=%"PRIu64" [avg=%lf] combined=%"PRIu64" [avg=%lf]",
2016-08-03 16:29:12 +03:00
op1, op1sum, op2, op2sum, op3, op3sum);
2016-03-02 17:16:49 +03:00
#endif
2015-02-16 12:51:20 +03:00
return status;
}
2014-09-06 03:11:03 +04:00
BOOL h264_context_init(H264_CONTEXT* h264)
{
#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
2016-08-03 16:29:12 +03:00
if (g_Subsystem_MF.Init(h264))
{
h264->subsystem = &g_Subsystem_MF;
return TRUE;
}
2016-08-03 16:29:12 +03:00
#endif
2014-09-06 03:11:03 +04:00
#ifdef WITH_LIBAVCODEC
2016-08-03 16:29:12 +03:00
2014-09-06 03:11:03 +04:00
if (g_Subsystem_libavcodec.Init(h264))
{
h264->subsystem = &g_Subsystem_libavcodec;
return TRUE;
}
2016-08-03 16:29:12 +03:00
#endif
2014-09-06 03:11:03 +04:00
#ifdef WITH_OPENH264
2016-08-03 16:29:12 +03:00
2014-09-06 03:11:03 +04:00
if (g_Subsystem_OpenH264.Init(h264))
{
h264->subsystem = &g_Subsystem_OpenH264;
return TRUE;
}
2016-08-03 16:29:12 +03:00
#endif
2015-07-28 23:01:34 +03:00
#ifdef WITH_X264
2016-08-03 16:29:12 +03:00
2015-07-28 23:01:34 +03:00
if (g_Subsystem_x264.Init(h264))
{
h264->subsystem = &g_Subsystem_x264;
return TRUE;
}
2016-08-03 16:29:12 +03:00
#endif
h264->subsystem = &g_Subsystem_dummy;
return TRUE;
}
BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
{
if (!h264)
return FALSE;
h264->width = width;
h264->height = height;
return TRUE;
}
H264_CONTEXT* h264_context_new(BOOL Compressor)
{
H264_CONTEXT* h264;
h264 = (H264_CONTEXT*) calloc(1, sizeof(H264_CONTEXT));
if (h264)
{
h264->Compressor = Compressor;
if (Compressor)
{
/* Default compressor settings, may be changed by caller */
h264->BitRate = 1000000;
h264->FrameRate = 30;
}
2014-09-06 03:11:03 +04:00
if (!h264_context_init(h264))
{
free(h264);
return NULL;
}
}
return h264;
}
void h264_context_free(H264_CONTEXT* h264)
{
if (h264)
{
2014-09-06 03:11:03 +04:00
h264->subsystem->Uninit(h264);
_aligned_free(h264->pYUV444Data[0]);
_aligned_free(h264->pYUV444Data[1]);
_aligned_free(h264->pYUV444Data[2]);
free(h264);
}
}