Media (Intel® oneAPI Video Processing Library, Intel Media SDK)
Access community support with transcoding, decoding, and encoding in applications using media tools from Intel. This includes Intel® oneAPI Video Processing Library and Intel® Media SDK.
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

Frame management in decoders

I am currently working on decoders, and I am a bit puzzled by frame management.
I have a decoding loop which looks like this :
[cpp]    sts = MFX_ERR_MORE_SURFACE;

    while ((sts == MFX_ERR_MORE_SURFACE) || (sts == MFX_ERR_MORE_DATA))
        if (bNeedFrame == 0)
            // find an available frame
            // now, pElement->mfxFrame is a fresh buffer

        pFrameOut = NULL;
        memset(&sSyncPoint, 0, sizeof(sSyncPoint));

        sts =
                                            &pFrameOut, &sSyncPoint);

        if (sts == MFX_ERR_MORE_DATA)
            // refill bitstream
        else if (sts == MFX_ERR_MORE_SURFACE)
            bNeedFrame = 0;
        else if (sts == MFX_ERR_NONE)
            if (pFrameOut == &pElement->mfxFrame)
                bNeedFrame = 0;

        if ((sts == MFX_ERR_NONE) && (sSyncPoint != NULL))
            sts =
                MFXVideoCORE_SyncOperation(hMfxSession, sSyncPoint,

        if ((sts == MFX_ERR_NONE) && (pFrameOut != NULL))
            // use decoded frame
This piece of software is used for both h264 and mpeg2 standards. I want to produce exactly one frame at a time in this part of the code. I do not know how to handle my bNeedFrame flag. I thought two options were possible :
  • Either I reset it before entering the loop, making the loop looking for a available buffer. However, if I do that, the h264 decoder consumes more buffers that allocated. This way, the decoder uses more than the number of buffers suggested in MFXVideoDECODE_QueryIOSurf function.
  • Or I let the decoder inform me that he needs fresh buffers : entering the loop, I use the old working buffer and wait for the decoder to produce a MFX_ERR_MORE_SURFACE status. This does not work either, in mpeg2 the decoder seems to overwrite old surfaces with new ones without querying for new surfaces.
I found a workaround as shown in my code : forcing the reset of the bNeedFlag when the decoder outputed frame is the same as the input one (this cases occurs for me only using mpeg2 standard). It seems to work in both mpeg2 and h264 standards, but I still do not know what should be the clean and probably more straightforward way to handle this.
Thanks for your help.
0 Kudos
1 Reply

Unfortunately I'm not able to fully understand the intended behavior of your code and the issues you are facing.

My suggestion is to take a look at the sample "sample_decode" that is part of the SDK. By exploring the behavior of that sample when feeding it a valid stream you may be able figure out how to design your decoder in the way you desire.

And since you seem to have some issues with the number of allocated buffers please take a special look at the buffer allocation part of that sample.