MFXVideoDECODE_Init 失败 MFX_ERR_MEMORY_ALLOC

MFXVideoDECODE_Init fail with MFX_ERR_MEMORY_ALLOC

我正在尝试对 h.264 视频使用 intel-media-sdk 解码器。这是我初始化解码器的代码:

mfxStatus decoder::initDecoder(HWND window, mfxBitstream *Header) {
    mfxStatus sts = MFX_ERR_NONE;
    mfxVersion ver = { { 0, 1 } };
    mfxVideoParam mfxVideoParams;
    mfxFrameAllocator mfxAllocator;
    mfxFrameAllocResponse mfxResponse;

    sts = m_mfxSession.Init(MFX_IMPL_AUTO_ANY, &ver); //sts = MFX_ERR_NONE

    if (sts == MFX_ERR_NONE) {
        sts = m_mfxSession.SetHandle(MFX_HANDLE_DIRECT3D_DEVICE_MANAGER9, 
            m_renderer.initD3d(GetIntelDeviceAdapterNum(), window)); //sts = MFX_ERR_NONE
        if (sts == MFX_ERR_NONE) {
            mfxAllocator.pthis = m_mfxSession;
            sts = m_mfxSession.SetFrameAllocator(&mfxAllocator); //sts = MFX_ERR_NONE
            if (sts == MFX_ERR_NONE) {
                MFXVideoDECODE mfxDEC(m_mfxSession);
                m_mfxVideoDecode = mfxDEC;
                memset(&mfxVideoParams, 0, sizeof(mfxVideoParams));
                mfxVideoParams.mfx.CodecId = MFX_CODEC_AVC;
                mfxVideoParams.IOPattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;

                sts = m_mfxVideoDecode.DecodeHeader(Header, &mfxVideoParams); //sts = MFX_ERR_NONE
                if (sts == MFX_ERR_NONE) {
                    memset(&m_mfxRequest, 0, sizeof(m_mfxRequest));
                    sts = m_mfxVideoDecode.QueryIOSurf(&mfxVideoParams, &m_mfxRequest); //sts = MFX_ERR_NONE
                    if (sts == MFX_ERR_NONE) {
                        sts = m_renderer.allocSurfaces(mfxAllocator.pthis, &m_mfxRequest, &mfxResponse);
                        if (sts == MFX_ERR_NONE) {
                            m_pmfxSurfaces = new mfxFrameSurface1 *[m_mfxRequest.NumFrameSuggested];
                            for (int i = 0; i < m_mfxRequest.NumFrameSuggested; i++) {
                                m_pmfxSurfaces[i] = new mfxFrameSurface1;
                                memset(m_pmfxSurfaces[i], 0, sizeof(mfxFrameSurface1));
                                memcpy(&(m_pmfxSurfaces[i]->Info), &(mfxVideoParams.mfx.FrameInfo), sizeof(mfxFrameInfo));
                                // MID (memory id) represents one video NV12 surface
                                m_pmfxSurfaces[i]->Data.MemId = mfxResponse.mids[i];
                            };
                            sts = m_mfxVideoDecode.Init(&mfxVideoParams); //sts = MFX_ERR_MEMORY_ALLOC
                        }
                    }
                }
            }
        }
    }

    return sts;
}

所以你可以看到 MFXVideoDECODE::Init(mfxVideoParam*)(内部调用 MFXVideoDECODE_Init)returns MFX_ERR_MEMORY_ALLOC 奇怪的是 this document 它说此函数没有此 return 值。

这里是一些关于mfxVideoParams的调试信息:

AllocId = 0, AsyncDepth = 0, IOPattern = 16, mfx.CodecId = 541283905, mfx.CodecProfile = 77, mfx.CodecLevel = 30, vpp.In.FourCC = 842094158, vpp.In.Width = 864, vpp.In.Height = 480, vpp.In.CropW = 854, vpp.In.CropH = 480, vpp.In.BufferSize = 31458144, vpp.In.AspectRatioW = 1, vpp.In.AspectRatioH = 1, vpp.In.PicStruct = 1, vpp.In.ChromaFormat = 1

这里是头文件中的一些成员数据定义,在这里使用:

MFXVideoSession m_mfxSession;
MFXVideoDECODE m_mfxVideoDecode;
mfxFrameAllocRequest m_mfxRequest;
mfxFrameSurface1** m_pmfxSurfaces;

下面是一些可能与此问题相关的关于我当前工作设备的信息:

最后,为了重现完全相同的情况,我从 this site 下载了名为 big_buck_bunny_1080p_h264.mov 的视频,然后使用 ffmpeg 将其解压缩为 h264 并在我的程序中使用它。

您需要使用适当的函数初始化 mfxFrameAlocator 回调函数(Alloc、Free、GetHDL 等)。 例如:

//static member
mfxStatus decoder::gethdl(mfxHDL pthis, mfxMemId mid, mfxHDL* handle)
{
    pthis; // To avoid warning for this unused parameter

    if (handle == 0) return MFX_ERR_INVALID_HANDLE;

    *handle = mid;
    return MFX_ERR_NONE;
}



mfxStatus decoder::initDecoder(HWND window, mfxBitstream *Header) {

    //blah blah
    mfxAllocator.pthis = m_mfxSession;
    mfxAllocator.GetHDL = gethdl;
    //define for these too
    //mfxAllocator.Alloc = alloc;
    //mfxAllocator.Free = free;
    //mfxAllocator.Lock = lock;
    //mfxAllocator.Unlock = unlock;

    //rest of your code
}