Question

I'm trying to create an IMFSample from a CImage so that I that I can encode a video from a series of images using a SinkWriter. Microsoft has a sample of how to do this with a DWORD array (instead of a CIMage) here: http://msdn.microsoft.com/en-us/library/windows/desktop/ff819477(v=vs.85).aspx . Doing this involves using MFCopyImage(), but I am getting an ACCESS VIOLATION when calling this function, so I may be passing in the wrong parameters. I am new to C++.

Working from their sample, this is what I've got:

CImage *img = LoadImage();

IMFSample *pSample = NULL;
IMFMediaBuffer *pBuffer = NULL;

const LONG cbWidth = img->GetBPP() / 8 * img->GetWidth();
const DWORD cbBuffer = cbWidth * img->GetHeight();

BYTE *pData = NULL;
HRESULT hr = MFCreateMemoryBuffer(cbBuffer, &pBuffer);

if (SUCCEEDED(hr))
{
    hr = pBuffer->Lock(&pData, NULL, NULL);
}

if (SUCCEEDED(hr))
{
    hr = MFCopyImage(
        pData,                     
        cbWidth,                    
        (BYTE*)img->GetBits(),   
        cbWidth,                   
        cbWidth,                    
        img->GetHeight()
        ); // ACCESS VIOLATION HERE!
}
if (pBuffer)
{
    pBuffer->Unlock();
}

Anyone know why this gets me an ACCESS VIOLATION ? Am I passing in the right values, especially for the stride? Based on this documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/bb970554(v=vs.85).aspx , it was not clear to me what exactly this parameter is supposed to be.

Update Dec 11: Changed code sample with corrections provided by @cha and @roman-r. Still getting the same error.

Was it helpful?

Solution

Access violation is caused by wrong arguments. The API just hits memory beyond either source or destination buffers.

You are already aware of GetBPP issue, which is not small but severe (you should have your code snippet updated respectively).

Another issue is the last MFCopyImage argument. It's height, not width. Since width is typically smaller you are likely to have a buffer overrun.

Additionally you are assuming minimal stride. Effectively you might have a larger one. In this case you would see a skew effect on the copied image and not access violation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top