gpt4 book ai didi

c++ - 无法在HEVC解码器IMFTransform上设置输出类型

转载 作者:行者123 更新时间:2023-12-02 10:02:22 34 4
gpt4 key购买 nike

我已经编写了此程序来基于https://docs.microsoft.com/en-us/windows/win32/medfound/supporting-direct3d-11-video-decoding-in-media-foundation设置HEVC解码器。一切正常,直到我调用result = decoder->SetOutputType(0, media_type, 0);为止,这将返回错误MF_E_ATTRIBUTENOTFOUND。我不确定出什么问题,SetOutputType文档中未描述此错误,我只找到了使用MF进行HEVC解码的几个示例,但都没有一个这样的错误描述。

// WindowsProject5.cpp : Defines the entry point for the application.
//

#include <iostream>
#include <initguid.h>
#include <mfapi.h>
#include <mftransform.h>
#include <combaseapi.h>
#include <d3d11.h>
#include <optional>
#include <Mferror.h>

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

auto result = CoInitialize(NULL);
if (result != S_OK) {
std::cout << "CoInitialize failed" << std::endl;
std::terminate();
}

MSG msg;

MFT_REGISTER_TYPE_INFO inputInfo{ MFMediaType_Video , MFVideoFormat_HEVC };
MFT_REGISTER_TYPE_INFO outputInfo{ MFMediaType_Video, MFVideoFormat_NV12 };
IMFActivate** activates;
unsigned int numActivates = 255;

result = MFTEnumEx(MFT_CATEGORY_VIDEO_DECODER, MFT_ENUM_FLAG_SYNCMFT, &inputInfo, nullptr, &activates, &numActivates);
if (result != S_OK) {
std::cout << "MFTEnum failed" << std::endl;
std::terminate();
}

std::cout << numActivates << std::endl;

if (!numActivates) {
std::cout << "No HEVC decoders found" << std::endl;
std::terminate();
}

IMFTransform* decoder;
result = activates[0]->ActivateObject(IID_PPV_ARGS(&decoder));
if (result != S_OK) {
std::cout << "ActivateObject failed" << std::endl;
std::terminate();
}

IMFAttributes* attributes;
result = decoder->GetAttributes(&attributes);
if (result != S_OK) {
std::cout << "GetAttributes failed" << std::endl;
std::terminate();
}

auto aware = 123456u;
result = attributes->GetUINT32(MF_SA_D3D11_AWARE, &aware);
if (result != S_OK) {
std::cout << "GetAttributes failed" << std::endl;
std::terminate();
}

std::cout << "MF_SA_D3D11_AWARE = " << aware << std::endl;

if (!aware) {
std::cout << "HEVC decoder is not DirectX aware" << std::endl;
std::terminate();
}

unsigned int resetToken;
IMFDXGIDeviceManager* deviceManager;

result = MFCreateDXGIDeviceManager(&resetToken, &deviceManager);
if (result != S_OK) {
std::cout << "MFCreateDXGIDeviceManager failed" << std::endl;
std::terminate();
}

result = decoder->ProcessMessage(MFT_MESSAGE_SET_D3D_MANAGER, reinterpret_cast<ULONG_PTR>(deviceManager));
if (result != S_OK) {
std::cout << "ProcessMessage(MFT_MESSAGE_SET_D3D_MANAGER) failed" << std::endl;
std::terminate();
}

ID3D11Device* device;
D3D_FEATURE_LEVEL featureLevel;
ID3D11DeviceContext* deviceContext;
result = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, D3D11_CREATE_DEVICE_VIDEO_SUPPORT, nullptr, 0, D3D11_SDK_VERSION, &device, &featureLevel, &deviceContext);
if (result != S_OK) {
std::cout << "D3D11CreateDevice failed" << std::endl;
std::terminate();
}

result = deviceManager->ResetDevice(device, resetToken);
if (result != S_OK) {
std::cout << "ResetDevice failed" << std::endl;
std::terminate();
}

HANDLE deviceHandle;
result = deviceManager->OpenDeviceHandle(&deviceHandle);
if (result != S_OK) {
std::cout << "OpenDeviceHandle failed" << std::endl;
std::terminate();
}

ID3D11VideoDevice* videoDevice;
result = deviceManager->GetVideoService(deviceHandle, IID_PPV_ARGS(&videoDevice));
if (result != S_OK) {
std::cout << "GetVideoService failed" << std::endl;
std::terminate();
}

ID3D11VideoContext* videoContext;
result = deviceContext->QueryInterface(IID_PPV_ARGS(&videoContext));
if (result != S_OK) {
std::cout << "QueryInterface(videoContext) failed" << std::endl;
std::terminate();
}


ID3D10Multithread* multithreaded;
result = device->QueryInterface(IID_PPV_ARGS(&multithreaded));
if (result != S_OK) {
std::cout << "QueryInterface(multithreaded) failed" << std::endl;
std::terminate();
}

multithreaded->SetMultithreadProtected(true);

auto profileCount = videoDevice->GetVideoDecoderProfileCount();
std::optional<GUID> selectedProfile;
for (decltype(profileCount) i = 0; i < profileCount; ++i) {
GUID profile;
result = videoDevice->GetVideoDecoderProfile(i, &profile);
if (result != S_OK) {
std::cout << "GetVideoDecoderProfile(" << i << ") failed" << std::endl;
std::terminate();
}
if (profile == D3D11_DECODER_PROFILE_HEVC_VLD_MAIN) {
selectedProfile = profile;
std::cout << "D3D11_DECODER_PROFILE_HEVC_VLD_MAIN found" << std::endl;
}
else if (profile == D3D11_DECODER_PROFILE_HEVC_VLD_MAIN10) {
std::cout << "D3D11_DECODER_PROFILE_HEVC_VLD_MAIN10 found" << std::endl;
}
}

if (!selectedProfile.has_value()) {
std::cout << "No HEVC decoder profile found" << std::endl;
std::terminate();
}

BOOL supported;
result = videoDevice->CheckVideoDecoderFormat(&selectedProfile.value(), DXGI_FORMAT_NV12, &supported);
if (result != S_OK) {
std::cout << "CheckVideoDecoderFormat failed" << std::endl;
std::terminate();
}

if (!supported) {
std::cout << "Decoder format not supported" << std::endl;
std::terminate();
}

IMFMediaType* media_type;
result = MFCreateMediaType(&media_type);
if (result != S_OK) {
std::cout << "MFCreateMediaType failed" << std::endl;
std::terminate();
}
result = media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
if (result != S_OK) {
std::cout << "SetGUID(MF_MT_MAJOR_TYPE) failed" << std::endl;
std::terminate();
}
result = media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_HEVC);
if (result != S_OK) {
std::cout << "SetGUID(MF_MT_SUBTYPE) failed" << std::endl;
std::terminate();
}

result = decoder->SetInputType(0, media_type, 0); // No flags
if (result != S_OK) {
std::cout << "SetInputType failed" << std::endl;
std::terminate();
}

for (uint32_t i = 0;
SUCCEEDED(decoder->GetOutputAvailableType(0, i, &media_type)); ++i) {
GUID out_subtype = { 0 };
result = media_type->GetGUID(MF_MT_SUBTYPE, &out_subtype);
if (result != S_OK) {
std::cout << "GetGUID failed" << std::endl;
std::terminate();
}

if (out_subtype == MFVideoFormat_NV12) {
result = decoder->SetOutputType(0, media_type, 0); // No flags
if (result != S_OK) {
std::cout << "SetOutputType failed" << std::endl;
std::terminate();
}
return true;
}
}

return 0;
}

最佳答案

即使MSDN在H.265 / HEVC Video Decoder中未提及其他输入媒体类型属性,您也需要对其进行设置。有关所需的属性,请参见H.264 decoder article:MF_MT_FRAME_SIZE和 friend 。

您也可以使用MFTrace SDK工具在遇到故障之前检查哪些属性被查询并报告为丢失。

  // Have this line added:
MFSetAttributeSize(media_type, MF_MT_FRAME_SIZE, 1280, 720);

result = decoder->SetInputType(0, media_type, 0); // No flags

关于c++ - 无法在HEVC解码器IMFTransform上设置输出类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61996152/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com