Using Media Foundation to encode Direct X surfaces

I am trying to use the MediaFoundation API to encode videos, but I am having trouble clicking samples on SinkWriter.

I get frames for encoding through the desktop duplication API. As a result, I have ID3D11Texture2D with a desktop image in it.

I am trying to create an IMFVideoSample containing this surface and then click this sample video on SinkWriter.

I tried about it differently:

  • I called MFCreateVideoSampleFromSurface(texture, &pSample) , where the texture is ID3D11Texture2D, populated with SampleTime and SampleDuration, and then passed the created pattern to SinkWriter.
    SinkWriter returned E_INVALIDARG.

  • I tried to create a sample by passing nullptr as the first argument and creating the buffer itself using MFCreateDXGISurfaceBuffer and then passing the resulting buffer to Sample.
    That didn't work either.

  • I read the MediaFoundation documentation and could not find detailed information on how to create a sample from DirectX texture.

I did not succeed in trying.
Has anyone out there used this API before and can think of things I should check, or how I can debug this?

+5
source share
1 answer

First of all, you must learn to use the mftrace tool . Most likely, he will immediately tell you about the problem.

But I guess the following problems are possible.

  • Perhaps some other attributes are required besides SampleTime / SampleDuration.

  • Perhaps SinkWriter needs a texture that it can read on the processor. To fix this, when a frame is available, create an intermediate texture of the same format + size, call CopyResource to copy the desktop to the intermediate texture, and then transfer this intermediate texture to MF.

  • Even if you use a hardware encoder, so the CPU never tries to read texture data, I do not consider it a good idea to directly transfer the texture of the desktop to MF.

When you set the D3D texture for the pattern, the data is not copied anywhere, the pattern just saves the texture.

MF works asynchronously, it can buffer several samples in its topological nodes if they want.

DD gives you data synchronously, you can only access text between calls to AcquireNextFrame and ReleaseFrame.

+1
source

All Articles