Search code examples
c++decodeintelintel-media-sdk

MFXVideoDECODE_Init fail with MFX_ERR_MEMORY_ALLOC


I'm trying to use intel-media-sdk decoder for h.264 videos. Here is my code for initializing decoder :

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;
}

So as you can see MFXVideoDECODE::Init(mfxVideoParam*) (which internally calls MFXVideoDECODE_Init) returns MFX_ERR_MEMORY_ALLOC and the strange thing here is in this document it says this function does not have this return value.

Here is some debug information about 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

Here is Some member data definition in header which used here :

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

And Here is some information about my current working device that might relate to this problem:

  • Operating System : Windows 8.1
  • Processor : Intel(R) Core(TM) i5-3470 CPU @ 3.20GHZ
  • System type : 64-bit operating system, x64-based processor
  • Installed memory (RAM) : 8.00 GB

And finally to reproduce the exact same situation I downloaded video named big_buck_bunny_1080p_h264.mov from this site and then extracted it with ffmpeg to h264 and used it in my program.


Solution

  • You need to initialize mfxFrameAlocator callback functions (Alloc, Free, GetHDL, ...) with proper functions. for example :

    //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
    }