20 #include <winpr/winpr.h>
21 #include <freerdp/log.h>
22 #include <freerdp/codec/h264.h>
29 #include <wmcodecdsp.h>
30 #include <mftransform.h>
34 #define TAG FREERDP_TAG("codec")
36 static const GUID sCLSID_CMSH264DecoderMFT = {
37 0x62CE7E72, 0x4C71, 0x4d20, { 0xB1, 0x5D, 0x45, 0x28, 0x31, 0xA8, 0x7D, 0x9D }
39 static const GUID sIID_IMFTransform = {
40 0xbf94c121, 0x5b05, 0x4e6f, { 0x80, 0x00, 0xba, 0x59, 0x89, 0x61, 0x41, 0x4d }
42 static const GUID sMF_MT_MAJOR_TYPE = {
43 0x48eba18e, 0xf8c9, 0x4687, { 0xbf, 0x11, 0x0a, 0x74, 0xc9, 0xf9, 0x6a, 0x8f }
45 static const GUID sMF_MT_FRAME_SIZE = {
46 0x1652c33d, 0xd6b2, 0x4012, { 0xb8, 0x34, 0x72, 0x03, 0x08, 0x49, 0xa3, 0x7d }
48 static const GUID sMF_MT_DEFAULT_STRIDE = {
49 0x644b4e48, 0x1e02, 0x4516, { 0xb0, 0xeb, 0xc0, 0x1c, 0xa9, 0xd4, 0x9a, 0xc6 }
51 static const GUID sMF_MT_SUBTYPE = {
52 0xf7e34c9a, 0x42e8, 0x4714, { 0xb7, 0x4b, 0xcb, 0x29, 0xd7, 0x2c, 0x35, 0xe5 }
54 static const GUID sMFMediaType_Video = {
55 0x73646976, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }
57 static const GUID sMFVideoFormat_H264 = {
58 0x34363248, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
60 static const GUID sMFVideoFormat_IYUV = {
61 0x56555949, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
63 static const GUID sIID_ICodecAPI = {
64 0x901db4c7, 0x31ce, 0x41a2, { 0x85, 0xdc, 0x8f, 0xa0, 0xbf, 0x41, 0xb8, 0xda }
66 static const GUID sCODECAPI_AVLowLatencyMode = {
67 0x9c27891a, 0xed7a, 0x40e1, { 0x88, 0xe8, 0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee }
70 typedef HRESULT(__stdcall* pfnMFStartup)(ULONG Version, DWORD dwFlags);
71 typedef HRESULT(__stdcall* pfnMFShutdown)(void);
72 typedef HRESULT(__stdcall* pfnMFCreateSample)(IMFSample** ppIMFSample);
73 typedef HRESULT(__stdcall* pfnMFCreateMemoryBuffer)(DWORD cbMaxLength, IMFMediaBuffer** ppBuffer);
74 typedef HRESULT(__stdcall* pfnMFCreateMediaType)(IMFMediaType** ppMFType);
79 IMFTransform* transform;
80 IMFMediaType* inputType;
81 IMFMediaType* outputType;
85 IMFSample* outputSample;
86 IMFMediaBuffer* outputBuffer;
88 pfnMFStartup MFStartup;
89 pfnMFShutdown MFShutdown;
90 pfnMFCreateSample MFCreateSample;
91 pfnMFCreateMemoryBuffer MFCreateMemoryBuffer;
92 pfnMFCreateMediaType MFCreateMediaType;
95 static HRESULT mf_find_output_type(H264_CONTEXT_MF* sys,
const GUID* guid,
96 IMFMediaType** ppMediaType)
101 IMFMediaType* pMediaType = NULL;
105 hr = sys->transform->lpVtbl->GetOutputAvailableType(sys->transform, 0, idx, &pMediaType);
110 pMediaType->lpVtbl->GetGUID(pMediaType, &sMF_MT_SUBTYPE, &mediaGuid);
112 if (IsEqualGUID(&mediaGuid, guid))
114 *ppMediaType = pMediaType;
118 pMediaType->lpVtbl->Release(pMediaType);
125 static HRESULT mf_create_output_sample(H264_CONTEXT* h264, H264_CONTEXT_MF* sys)
128 MFT_OUTPUT_STREAM_INFO streamInfo;
130 if (sys->outputSample)
132 sys->outputSample->lpVtbl->Release(sys->outputSample);
133 sys->outputSample = NULL;
136 hr = sys->MFCreateSample(&sys->outputSample);
140 WLog_Print(h264->log, WLOG_ERROR,
"MFCreateSample failure: 0x%08" PRIX32
"", hr);
144 hr = sys->transform->lpVtbl->GetOutputStreamInfo(sys->transform, 0, &streamInfo);
148 WLog_Print(h264->log, WLOG_ERROR,
"GetOutputStreamInfo failure: 0x%08" PRIX32
"", hr);
152 hr = sys->MFCreateMemoryBuffer(streamInfo.cbSize, &sys->outputBuffer);
156 WLog_Print(h264->log, WLOG_ERROR,
"MFCreateMemoryBuffer failure: 0x%08" PRIX32
"", hr);
160 sys->outputSample->lpVtbl->AddBuffer(sys->outputSample, sys->outputBuffer);
164 WLog_Print(h264->log, WLOG_ERROR,
"AddBuffer failure: 0x%08" PRIX32
"", hr);
168 sys->outputBuffer->lpVtbl->Release(sys->outputBuffer);
173 static int mf_decompress(H264_CONTEXT* h264,
const BYTE* pSrcData, UINT32 SrcSize)
176 BYTE* pbBuffer = NULL;
177 DWORD cbMaxLength = 0;
178 DWORD cbCurrentLength = 0;
179 DWORD outputStatus = 0;
180 IMFSample* inputSample = NULL;
181 IMFMediaBuffer* inputBuffer = NULL;
182 IMFMediaBuffer* outputBuffer = NULL;
183 MFT_OUTPUT_DATA_BUFFER outputDataBuffer;
184 H264_CONTEXT_MF* sys = (H264_CONTEXT_MF*)h264->pSystemData;
185 UINT32* iStride = h264->iStride;
186 BYTE** pYUVData = h264->pYUVData;
187 hr = sys->MFCreateMemoryBuffer(SrcSize, &inputBuffer);
191 WLog_Print(h264->log, WLOG_ERROR,
"MFCreateMemoryBuffer failure: 0x%08" PRIX32
"", hr);
195 hr = inputBuffer->lpVtbl->Lock(inputBuffer, &pbBuffer, &cbMaxLength, &cbCurrentLength);
199 WLog_Print(h264->log, WLOG_ERROR,
"Lock failure: 0x%08" PRIX32
"", hr);
203 CopyMemory(pbBuffer, pSrcData, SrcSize);
204 hr = inputBuffer->lpVtbl->SetCurrentLength(inputBuffer, SrcSize);
208 WLog_Print(h264->log, WLOG_ERROR,
"SetCurrentLength failure: 0x%08" PRIX32
"", hr);
212 hr = inputBuffer->lpVtbl->Unlock(inputBuffer);
216 WLog_Print(h264->log, WLOG_ERROR,
"Unlock failure: 0x%08" PRIX32
"", hr);
220 hr = sys->MFCreateSample(&inputSample);
224 WLog_Print(h264->log, WLOG_ERROR,
"MFCreateSample failure: 0x%08" PRIX32
"", hr);
228 inputSample->lpVtbl->AddBuffer(inputSample, inputBuffer);
232 WLog_Print(h264->log, WLOG_ERROR,
"AddBuffer failure: 0x%08" PRIX32
"", hr);
236 inputBuffer->lpVtbl->Release(inputBuffer);
237 hr = sys->transform->lpVtbl->ProcessInput(sys->transform, 0, inputSample, 0);
241 WLog_Print(h264->log, WLOG_ERROR,
"ProcessInput failure: 0x%08" PRIX32
"", hr);
245 hr = mf_create_output_sample(h264, sys);
249 WLog_Print(h264->log, WLOG_ERROR,
"mf_create_output_sample failure: 0x%08" PRIX32
"", hr);
253 outputDataBuffer.dwStreamID = 0;
254 outputDataBuffer.dwStatus = 0;
255 outputDataBuffer.pEvents = NULL;
256 outputDataBuffer.pSample = sys->outputSample;
257 hr = sys->transform->lpVtbl->ProcessOutput(sys->transform, 0, 1, &outputDataBuffer,
260 if (hr == MF_E_TRANSFORM_STREAM_CHANGE)
263 UINT64 frameSize = 0;
267 sys->outputType->lpVtbl->Release(sys->outputType);
268 sys->outputType = NULL;
271 hr = mf_find_output_type(sys, &sMFVideoFormat_IYUV, &sys->outputType);
275 WLog_Print(h264->log, WLOG_ERROR,
"mf_find_output_type failure: 0x%08" PRIX32
"", hr);
279 hr = sys->transform->lpVtbl->SetOutputType(sys->transform, 0, sys->outputType, 0);
283 WLog_Print(h264->log, WLOG_ERROR,
"SetOutputType failure: 0x%08" PRIX32
"", hr);
287 hr = mf_create_output_sample(h264, sys);
291 WLog_Print(h264->log, WLOG_ERROR,
"mf_create_output_sample failure: 0x%08" PRIX32
"",
296 hr = sys->outputType->lpVtbl->GetUINT64(sys->outputType, &sMF_MT_FRAME_SIZE, &frameSize);
300 WLog_Print(h264->log, WLOG_ERROR,
301 "GetUINT64(MF_MT_FRAME_SIZE) failure: 0x%08" PRIX32
"", hr);
305 sys->frameWidth = (UINT32)(frameSize >> 32);
306 sys->frameHeight = (UINT32)frameSize;
307 hr = sys->outputType->lpVtbl->GetUINT32(sys->outputType, &sMF_MT_DEFAULT_STRIDE, &stride);
311 WLog_Print(h264->log, WLOG_ERROR,
312 "GetUINT32(MF_MT_DEFAULT_STRIDE) failure: 0x%08" PRIX32
"", hr);
316 if (!avc420_ensure_buffer(h264, stride, sys->frameWidth, sys->frameHeight))
319 else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT)
324 WLog_Print(h264->log, WLOG_ERROR,
"ProcessOutput failure: 0x%08" PRIX32
"", hr);
331 DWORD bufferCount = 0;
332 DWORD cbMaxLength = 0;
333 DWORD cbCurrentLength = 0;
334 hr = sys->outputSample->lpVtbl->GetBufferCount(sys->outputSample, &bufferCount);
338 WLog_Print(h264->log, WLOG_ERROR,
"GetBufferCount failure: 0x%08" PRIX32
"", hr);
342 hr = sys->outputSample->lpVtbl->GetBufferByIndex(sys->outputSample, 0, &outputBuffer);
346 WLog_Print(h264->log, WLOG_ERROR,
"GetBufferByIndex failure: 0x%08" PRIX32
"", hr);
350 hr = outputBuffer->lpVtbl->Lock(outputBuffer, &buffer, &cbMaxLength, &cbCurrentLength);
354 WLog_Print(h264->log, WLOG_ERROR,
"Lock failure: 0x%08" PRIX32
"", hr);
358 CopyMemory(pYUVData[0], &buffer[offset], iStride[0] * sys->frameHeight);
359 offset += iStride[0] * sys->frameHeight;
360 CopyMemory(pYUVData[1], &buffer[offset], iStride[1] * (sys->frameHeight / 2));
361 offset += iStride[1] * (sys->frameHeight / 2);
362 CopyMemory(pYUVData[2], &buffer[offset], iStride[2] * (sys->frameHeight / 2));
363 offset += iStride[2] * (sys->frameHeight / 2);
364 hr = outputBuffer->lpVtbl->Unlock(outputBuffer);
368 WLog_Print(h264->log, WLOG_ERROR,
"Unlock failure: 0x%08" PRIX32
"", hr);
372 outputBuffer->lpVtbl->Release(outputBuffer);
375 inputSample->lpVtbl->Release(inputSample);
378 (void)fprintf(stderr,
"mf_decompress error\n");
382 static int mf_compress(H264_CONTEXT* h264,
const BYTE** ppSrcYuv,
const UINT32* pStride,
383 BYTE** ppDstData, UINT32* pDstSize)
385 H264_CONTEXT_MF* sys = (H264_CONTEXT_MF*)h264->pSystemData;
389 static BOOL mf_plat_loaded(H264_CONTEXT_MF* sys)
391 return sys->MFStartup && sys->MFShutdown && sys->MFCreateSample && sys->MFCreateMemoryBuffer &&
392 sys->MFCreateMediaType;
395 static void mf_uninit(H264_CONTEXT* h264)
397 H264_CONTEXT_MF* sys = (H264_CONTEXT_MF*)h264->pSystemData;
403 sys->transform->lpVtbl->Release(sys->transform);
404 sys->transform = NULL;
409 sys->codecApi->lpVtbl->Release(sys->codecApi);
410 sys->codecApi = NULL;
415 sys->inputType->lpVtbl->Release(sys->inputType);
416 sys->inputType = NULL;
421 sys->outputType->lpVtbl->Release(sys->outputType);
422 sys->outputType = NULL;
425 if (sys->outputSample)
427 sys->outputSample->lpVtbl->Release(sys->outputSample);
428 sys->outputSample = NULL;
433 if (mf_plat_loaded(sys))
436 FreeLibrary(sys->mfplat);
439 if (mf_plat_loaded(sys))
443 for (
size_t x = 0; x <
sizeof(h264->pYUVData) /
sizeof(h264->pYUVData[0]); x++)
444 winpr_aligned_free(h264->pYUVData[x]);
446 memset(h264->pYUVData, 0,
sizeof(h264->pYUVData));
447 memset(h264->iStride, 0,
sizeof(h264->iStride));
450 h264->pSystemData = NULL;
454 static BOOL mf_init(H264_CONTEXT* h264)
457 H264_CONTEXT_MF* sys = (H264_CONTEXT_MF*)calloc(1,
sizeof(H264_CONTEXT_MF));
462 h264->pSystemData = (
void*)sys;
465 sys->mfplat = LoadLibraryA(
"mfplat.dll");
470 sys->MFStartup = GetProcAddressAs(sys->mfplat,
"MFStartup", pfnMFStartup);
471 sys->MFShutdown = GetProcAddressAs(sys->mfplat,
"MFShutdown", pfnMFShutdown);
472 sys->MFCreateSample = GetProcAddressAs(sys->mfplat,
"MFCreateSample", pfnMFCreateSample);
473 sys->MFCreateMemoryBuffer =
474 GetProcAddressAs(sys->mfplat,
"MFCreateMemoryBuffer", pfnMFCreateMemoryBuffer);
475 sys->MFCreateMediaType =
476 GetProcAddressAs(sys->mfplat,
"MFCreateMediaType", pfnMFCreateMediaType);
478 if (!mf_plat_loaded(sys))
481 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
483 if (h264->Compressor)
489 hr = sys->MFStartup(MF_VERSION, 0);
493 WLog_Print(h264->log, WLOG_ERROR,
"MFStartup failure: 0x%08" PRIX32
"", hr);
497 hr = CoCreateInstance(&sCLSID_CMSH264DecoderMFT, NULL, CLSCTX_INPROC_SERVER,
498 &sIID_IMFTransform, (
void**)&sys->transform);
502 WLog_Print(h264->log, WLOG_ERROR,
503 "CoCreateInstance(CLSID_CMSH264DecoderMFT) failure: 0x%08" PRIX32
"", hr);
507 hr = sys->transform->lpVtbl->QueryInterface(sys->transform, &sIID_ICodecAPI,
508 (
void**)&sys->codecApi);
512 WLog_Print(h264->log, WLOG_ERROR,
513 "QueryInterface(IID_ICodecAPI) failure: 0x%08" PRIX32
"", hr);
519 hr = sys->codecApi->lpVtbl->SetValue(sys->codecApi, &sCODECAPI_AVLowLatencyMode, &var);
523 WLog_Print(h264->log, WLOG_ERROR,
524 "SetValue(CODECAPI_AVLowLatencyMode) failure: 0x%08" PRIX32
"", hr);
528 hr = sys->MFCreateMediaType(&sys->inputType);
532 WLog_Print(h264->log, WLOG_ERROR,
"MFCreateMediaType failure: 0x%08" PRIX32
"", hr);
536 hr = sys->inputType->lpVtbl->SetGUID(sys->inputType, &sMF_MT_MAJOR_TYPE,
537 &sMFMediaType_Video);
541 WLog_Print(h264->log, WLOG_ERROR,
"SetGUID(MF_MT_MAJOR_TYPE) failure: 0x%08" PRIX32
"",
546 hr = sys->inputType->lpVtbl->SetGUID(sys->inputType, &sMF_MT_SUBTYPE, &sMFVideoFormat_H264);
550 WLog_Print(h264->log, WLOG_ERROR,
"SetGUID(MF_MT_SUBTYPE) failure: 0x%08" PRIX32
"",
555 hr = sys->transform->lpVtbl->SetInputType(sys->transform, 0, sys->inputType, 0);
559 WLog_Print(h264->log, WLOG_ERROR,
"SetInputType failure: 0x%08" PRIX32
"", hr);
563 hr = mf_find_output_type(sys, &sMFVideoFormat_IYUV, &sys->outputType);
567 WLog_Print(h264->log, WLOG_ERROR,
"mf_find_output_type failure: 0x%08" PRIX32
"", hr);
571 hr = sys->transform->lpVtbl->SetOutputType(sys->transform, 0, sys->outputType, 0);
575 WLog_Print(h264->log, WLOG_ERROR,
"SetOutputType failure: 0x%08" PRIX32
"", hr);
579 hr = mf_create_output_sample(h264, sys);
583 WLog_Print(h264->log, WLOG_ERROR,
"mf_create_output_sample failure: 0x%08" PRIX32
"",
591 WLog_Print(h264->log, WLOG_ERROR,
"mf_init failure");
596 const H264_CONTEXT_SUBSYSTEM g_Subsystem_MF = {
"MediaFoundation", mf_init, mf_uninit,
597 mf_decompress, mf_compress };