Immediately decode bitstream produced by encoder

Hello, I’m wondering if it is possible to immediately decode a bitstream produced by an encoder and write the output to a file or directly display it. I tried:

encStatus = pEncFn.nvEncLockBitstream(encoder, &lockParams);
if (encStatus == NV_ENC_SUCCESS) {
    bufferSize = lockParams.bitstreamSizeInBytes;

    nFrameReturned = decoder.Decode((uint8_t *)lockParams.bitstreamBufferPtr, bufferSize);

    for (int i = 0; i < nFrameReturned; i++) {
        pFrame = decoder.GetFrame();

        fpOut.write(reinterpret_cast<char *>(pFrame), decoder.GetFrameSize());
    }
}
encStatus = pEncFn.nvEncUnlockBitstream(encoder, outputBuffer);

and I did get some data wrote to my output file, but when I played it, it only showed green screen.

I think I need to demux the bitstream first, but the sample code only demonstrates how to demux a media file using the ffmpeg demuxer.