During repeated runs of NVDEC, the first frame(s) output are not always identical.

We have an nvdec based decoder we’ve been testing out. One of these tests was to simply setup a decoder, play a video, tear down the decoder, and start over.

Most of the runs are without error, but about once every 5 runs, the first frame to be displayed is damaged or complete garbage. CRC checks confirm we are feeding identical data into the decoder on each run, but not always getting the same results out.

Is it possible the decoder is failing to initialize all the memory it depends on when first starting up?

It’s doubtful that the decoder is causing this. When you say “tear down the decoder” what does that mean exactly? There are a few things you need to do to properly reset:

Send end of stream.
Destroy and recreate the parser.
Flush the display queue.

Here is my own reset function that works reliably:

int decoder_reset(void)
    CUresult result;
    int i;
    if (Session.cuParser != NULL)
        pkt.flags = CUVID_PKT_ENDOFSTREAM;
        pkt.payload_size = 0;
        pkt.payload = NULL;
        pkt.timestamp = 0;  // not using timestamps
        cuvidParseVideoData(Session.cuParser, &pkt);
        Session.cuParser = NULL;
    // Create video parser
    memset(&parserInitParams, 0, sizeof(parserInitParams));
    parserInitParams.CodecType = Session.cuCodec;
    if (use_D3D)
        parserInitParams.ulMaxNumDecodeSurfaces = 16;
        parserInitParams.ulMaxNumDecodeSurfaces = MAX_FRM_CNT;
    parserInitParams.ulErrorThreshold = 100;
    parserInitParams.ulMaxDisplayDelay = 4;
    parserInitParams.ulClockRate = 1000000000;
    parserInitParams.pUserData = &Session;
    parserInitParams.pfnSequenceCallback = HandleVideoSequence;
    parserInitParams.pfnDecodePicture = HandlePictureDecode;
    parserInitParams.pfnDisplayPicture = HandlePictureDisplay;
    if (result != CUDA_SUCCESS)
        return -1;
    // Flush display queue
    for (i=0; i<DISPLAY_DELAY; i++)
        Session.DisplayQueue[i].picture_index = -1;
    Session.display_pos = 0;

    return 0;

Bumping my previous post as the forum listing does not show it as the last post so maybe it was not notified to the original poster.

Sorry for the long delay in replies. For some reason the forum kept losing my validation.

In any case, we found and fixed our issue a few weeks ago. It turns out there was a bug in our context management and so we sometimes called the decoder using a different context than we used to initialize it.