IMFSourceReader hardware accelerated

1

I'm trying to follow this in order to accelerate video decoding with an IMFSourceReader.

When I don't use it, everything is OK. When I use it, it doesn't work. Here are the steps:

Creation of a DirectX 11 device

HRESULT CreateDirectXDevice(IDXGIAdapter1* g, ID3D11Device** device, ID3D11DeviceContext** context)
{
HRESULT hr = S_OK;

// Driver types supported
D3D_DRIVER_TYPE DriverTypes[] =
{
    D3D_DRIVER_TYPE_HARDWARE,
    D3D_DRIVER_TYPE_WARP,
    D3D_DRIVER_TYPE_REFERENCE,
};
UINT NumDriverTypes = ARRAYSIZE(DriverTypes);

// Feature levels supported
D3D_FEATURE_LEVEL FeatureLevels[] =
{
    D3D_FEATURE_LEVEL_11_0,
    D3D_FEATURE_LEVEL_10_1,
    D3D_FEATURE_LEVEL_10_0,
    D3D_FEATURE_LEVEL_9_3,
    D3D_FEATURE_LEVEL_9_2,
    D3D_FEATURE_LEVEL_9_1
};
UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);

D3D_FEATURE_LEVEL FeatureLevel;

// Create device
for (UINT DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
{
    hr = D3D11CreateDevice(g, DriverTypes[DriverTypeIndex], nullptr, D3D11_CREATE_DEVICE_VIDEO_SUPPORT, FeatureLevels, NumFeatureLevels,
        D3D11_SDK_VERSION, device, &FeatureLevel, context);
    if (SUCCEEDED(hr))
    {
        // Device creation success, no need to loop anymore
        break;
    }
}
return hr;
}

Then, create the source reader:

HRESULT CreateSourceReader(const wchar_t* file, IMFSourceReader** r,bool Hw,bool D11)
{
    CComPtr<IMFAttributes> attrs;
    MFCreateAttributes(&attrs, 0);
    if (Hw)
        attrs->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, true);
    if (D11)
    {
            CComPtr<IMFDXGIDeviceManager > rr = 0;
            MFCreateDXGIDeviceManager(&dm9rt, &rr);
            rr->ResetDevice(GenericDirect3DDevice,dm9rt);
            attrs->SetUnknown(MF_SOURCE_READER_D3D_MANAGER,rr);
    }
    auto hr = MFCreateSourceReaderFromURL(f, attrs, r);
    return hr;
}

After that, the source reader does not work. When I attempt to resize the sample, the IMFTransform resizer crashes on SetInput(). When I attempt to pass the sample to a sink writer, it fails at random, sometimes returning DXGI_ERROR_DRIVER_INTERNAL_ERROR/GRAPHICS DEVICE REMOVED (REASON 0X887A0020).

What am I doing wrong?

Thanks a lot.

winapi
video
direct3d
ms-media-foundation
dxgi
asked on Stack Overflow Jan 15, 2020 by Michael Chourdakis • edited Jan 15, 2020 by Roman R.

1 Answer

1

First of all, you are interested in creating D3D device with D3D11_CREATE_DEVICE_DEBUG flag, so that you could see hints related to failures in debug output.

Second, I am guessing that the root cause of this particular problem is that you did not enable multithreaded protection using ID3D11Multithread interface, see this for example.

D3DDevMT->SetMultithreadProtected(TRUE);

It is mandatory since Media Foundation is heavilty multihtreaded by its nature and running unprotected you quickly hit a corruption.

answered on Stack Overflow Jan 15, 2020 by Roman R. • edited Jan 15, 2020 by Roman R.

User contributions licensed under CC BY-SA 3.0