Frame management in decoders

Frame management in decoders

Hi,I am currently working on decoders, and I am a bit puzzled by frame management.I have a decoding loop which looks like this :

    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 =
            MFXVideoDECODE_DecodeFrameAsync(hMfxSession,
                                            &sBitstream,
                                            &pElement->mfxFrame,
                                            &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,
                                           MSDK_DEC_WAIT_INTERVAL);

        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.

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi,

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.

Regards,Petter

Leave a Comment

Please sign in to add a comment. Not a member? Join today