User7723337
User7723337

Reputation: 12018

Rendering "IMFMediaBuffer" using DirectX 11 APIs

I am following details as given in below link for creating DirectX 11 Device and using it for rendering:
https://msdn.microsoft.com/en-us/library/windows/desktop/dn643747(v=vs.85).aspx

This example demonstrates rendering cube in the video window.
But I want to render Video Buffer using this example.

I have IMFMediaBuffer which is having video frame read from a file.
I want to decode this buffer and then display it using DirectX 11 APIs.
I can decode this buffer using decoder, but not sure how to render it.

Upvotes: 0

Views: 1620

Answers (1)

VuVirt
VuVirt

Reputation: 1917

Obtain the IMFDXGIBuffer interface from your IMFMediaBuffer via QueryInterface: https://msdn.microsoft.com/en-us/library/windows/desktop/hh447901(v=vs.85).aspx. Use GetResource to access the ID3D11Texture2D wrapped by this media buffer. Store the sub resource index using GetSubresourceIndex. Use ID3D11DeviceContext::CopySubresourceRegion (https://msdn.microsoft.com/en-us/library/windows/desktop/ff476394(v=vs.85).aspx), by specifying the stored sub resource index as SrcSubresource parameter, to copy the obtained ID3D11Texture2D as pSrcResource parameter, to your device's swapchain (set in pDstResource). Call Present(1) of your swapchain.

CopySubresourceRegion(pSwapChain, 0, 0, 0, 0, pTexture2D, index, NULL);

Note: You should set the same D3D11 device via IMFDXGIDeviceManager to the decoder for this to work.

More details:

ComPtr<IMFMediaBuffer> buffer;
CHK(sample->GetBufferByIndex(0, &buffer));

// Get the MF DXGI buffer
ComPtr<IMFDXGIBuffer> dxgiBuffer;
if (FAILED(buffer.As(&dxgiBuffer)))
{
    ComPtr<ID3D11Texture2D> texture;
    unsigned int subresource;
    CHK(dxgiBuffer->GetResource(IID_PPV_ARGS(&texture)));
    CHK(dxgiBuffer->GetSubresourceIndex(&subresource));

    ComPtr<ID3D11Device> device;
    ComPtr<ID3D11DeviceContext> context;
    texture->GetDevice(&device);
    device->GetImmediateContext(&context);

    context->CopySubresourceRegion(m_pSwapChain, 0, 0, 0, 0, texture.Get(), subresource, nullptr);
}

Upvotes: 4

Related Questions