Memory Allocation and External Allocators

All SDK implementations delegate memory management to the application. The application must allocate sufficient memory for input and output parameters and buffers, and de-allocate it when SDK functions complete their operations. During execution, the SDK functions use callback functions to the application to manage memory for video frames through external allocator interface mfxFrameAllocator.

mfxBufferAllocator interface is deprecated.

If an application needs to control the allocation of video frames, it can use callback functions through the mfxFrameAllocator interface. If an application does not specify an allocator, an internal allocator is used. However, if an application uses video memory surfaces for input and output, it must specify the hardware acceleration device and an external frame allocator using mfxFrameAllocator.

The external frame allocator can allocate different frame types:

·        in system memory and

·        in video memory, as “decoder render targets” or “processor render targets.” See the section Working with hardware acceleration for additional details.

The external frame allocator responds only to frame allocation requests for the requested memory type and returns MFX_ERR_UNSUPPORTED for all others. The allocation request uses flags, part of memory type field, to indicate which SDK class initiates the request, so the external frame allocator can respond accordingly. Example 11 illustrates a simple external frame allocator.

Text Box: typedef struct { mfxU16 width, height; mfxU8 *base; } mid_struct; mfxStatus fa_alloc(mfxHDL pthis, mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) { if (!(requestàtype&MFX_MEMTYPE_SYSTEM_MEMORY)) return MFX_ERR_UNSUPPORTED; if (requestàInfoàFourCC!=MFX_FOURCC_NV12) return MFX_ERR_UNSUPPORTED; responseàNumFrameActual=requestàNumFrameMin; for (int i=0;i<requestàNumFrameMin;i++) { mid_struct *mmid=(mid_struct *)malloc(sizeof(mid_struct)); mmidàwidth=ALIGN32(requestàInfoàWidth); mmidàheight=ALIGN32(requestàInfoàHeight); mmidàbase=(mfxU8*)malloc(mmidàwidth*mmidàheight*3/2); responseàmids[i]=mmid; } return MFX_ERR_NONE; } mfxStatus fa_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr) { mid_struct *mmid=(mid_struct *)mid; ptràpitch=mmidàwidth; ptràY=mmidàbase; ptràU=ptràY+mmidàwidth*mmidàheight; ptràV=ptràU+1; return MFX_ERR_NONE; } mfxStatus fa_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr) { if (ptr) ptràY=ptràU=ptràV=ptràA=0; return MFX_ERR_NONE; } mfxStatus fa_gethdl(mfxHDL pthis, mfxMemId mid, mfxHDL *handle) { return MFX_ERR_UNSUPPORTED; } mfxStatus fa_free(mfxHDL pthis, mfxFrameAllocResponse *response) { for (int i=0;i<responseàNumFrameActual;i++) { mid_struct *mmid=(mid_struct *)responseàmids[i]; free(mmidàbase); free(mid); } return MFX_ERR_NONE; }

Example 11: Example Frame Allocator

For system memory, it is highly recommended to allocate memory for all planes of the same frame as a single buffer (using one single malloc call).

 

For more complete information about compiler optimizations, see our Optimization Notice.