Search code examples
ms-media-foundation

Why MediaFoundation's H.264 decoder fails with "CopyDecodedFrame failed"?


I'm writing an application which uses MediaFoundation's H.264 decoder. I've based my code on this example.

Everything seems to be working as long as I follow the example and allocate a new IMFMediaBuffer for each decoded frame. If I attempt to reuse the same buffer for a subsequent frame, the decoding fails inside IMFTransform::ProcessOutput with an exception "CopyDecodedFrame failed".

Neither the function "CopyDecodedFrame" nor the error message is publicly documented anywhere. The decoder doesn't seem to be holding any references to the application's buffers (as evidenced by the AddRef/Release return value). Keeping the "used" buffers in a pool and reusing them after several frames doesn't help either.


Solution

  • I've discovered that the decoder works properly only when the submitted buffers are "empty" (currentLength = 0). Calling buffer->SetCurrentLength(0) allows the buffer to be safely reused.

    This requirement doesn't seem to be documented anywhere though.