I'm writing a screen capture application that uses the UWP Screen Capture API.
As a result, I get a callback each frame with a ID3D11Texture2D
with the target screen or application's image, which I want to use as input to MediaFoundation's SinkWriter to create an h264 in mp4 container file.
There are two issues I am facing:
- The texture is not CPU readable and attempts to map it fail
- The texture has padding (image stride > pixel width * pixel format size), I assume
To solve them, my approach has been to:
- Use
ID3D11DeviceContext::CopyResource
to copy the original texture into a new one created withD3D11_USAGE_STAGING
andD3D11_CPU_ACCESS_READ
flags - Since that texture too has padding, create a
IMFMediaBuffer
buffer wrapping it withMFCreateDXGISurfaceBuffer
cast it toIMF2DBuffer
and useIMF2DBuffer::ContiguousCopyTo
to another IMFMediaBuffer that I create withMFCreateMemoryBuffer
So I'm basically copying around each and every frame two times, once on the GPU and once on the CPU, which works, but seems way inefficient.
What is a better way of doing this? Is MediaFoundation smart enough to deal with input frames that have padding?