videodirect3dms-media-foundationdxgidesktop-duplication

E_INVALIDARG when I encode the NV12 texture?


I am converting RGB to NV12 and encode it to H264.


Solution

  • Your code is not too far from being able to work, but there are a few problems.

    First, I could not reproduce the claimed E_INVALIDARG with the code as it is.

    Second, you hardcode H.264/AVC encoder to encode 1920x1080@30 video. You don't check the effectively selected input media type, in particular, assuming that encoder sorts things out from partial media type. You would normally not want to let it work this way and specify the media types explicitly or at least make sure that they get resolved as you expect it.

    Third, most important, your texture, desc you feed into ProcessInput has resolution of display - not the resolution configured with the encoder - you can't expect it to work like this; then you attach hardcoded value with SetCurrentLength (don't do this - you need some thing like this from the body of the Q), then feed this into encoder with hardcoded resolution.

    Fourth, note that you are using software video encoder, and hence you don't really have an advantage of using video memory backed input. It is not the problem here, just a side observation.


    I updated your code to make it work. It is far from being perfect, but it does what you wanted it to do.

    https://github.com/roman380/MediaFoundationDesktopToNv12Avc/blob/main/sandbox.cpp

    It creates "C:\Temp\Output.h264" with raw H.264/AVC content, you can play it with something like Media Player Classic.