Slice level read back of NVENC

I want to use NVENC to implement slice-based encoding. I set sliceMode=3, sliceModeData=10, donotwait=true, enableSubFrameWrite=1 according https://on-demand.gputechconf.com/gtc/2014/presentations/S4654-detailed-overview-nvenc-encoder-api.pdf
But in a read thread, I can’t read slice. Why this? Have a demo about slice-based encoding using NVENC to implement slice level read back? Thank you!

Hi.
Your settings used are enough to use slice based encoding. As you mentioned in another post, enableEncodeAsync should be set to 0.
I am not clean what you mean by "But in a read thread, I can’t read slice’.

It will be easier if you can share the reproduction along with the steps for anyone to help here.

Thanks.

But if enableEncodeAsync set to 0, when a picture is encoding, how can I read slice at the same time?
I want to read encoded slice when it is encoding. So I use a encoding thread to encode, and a read thread to read encoded slice. So it need to be async mode?

Do you mean below?
void NvEncoder::DoEncode(NV_ENC_INPUT_PTR inputBuffer, std::vector<std::vector<uint8_t>> &vPacket, NV_ENC_PIC_PARAMS *pPicParams)
{
NV_ENC_PIC_PARAMS picParams = {};
if (pPicParams)
{
picParams = *pPicParams;
}
picParams.version = NV_ENC_PIC_PARAMS_VER;
picParams.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
picParams.inputBuffer = inputBuffer;
picParams.bufferFmt = GetPixelFormat();
picParams.inputWidth = GetEncodeWidth();
picParams.inputHeight = GetEncodeHeight();
picParams.outputBitstream = m_vBitstreamOutputBuffer[m_iToSend % m_nEncoderBuffer];
NVENCSTATUS nvStatus = m_nvenc.nvEncEncodePicture(m_hEncoder, &picParams);

NV_ENC_LOCK_BITSTREAM lockBitstreamData = { NV_ENC_LOCK_BITSTREAM_VER };
lockBitstreamData.outputBitstream = picParams.outputBitstream;
lockBitstreamData.doNotWait = true;
NVENC_API_CALL(m_nvenc.nvEncLockBitstream(m_hEncoder, &lockBitstreamData));
int i = 0;
while (lockBitstreamData.hwEncodeStatus != 2) {
	uint8_t *pData = (uint8_t *)lockBitstreamData.bitstreamBufferPtr;
	if (vPacket.size() < i + 1)
	{
		vPacket.push_back(std::vector<uint8_t>());
	}
	vPacket[i].clear();
	vPacket[i].insert(vPacket[i].end(), &pData[0], &pData[lockBitstreamData.bitstreamSizeInBytes]);
}
NVENC_API_CALL(m_nvenc.nvEncUnlockBitstream(m_hEncoder, lockBitstreamData.outputBitstream));
if (m_vMappedInputBuffers[m_iToSend % m_nEncoderBuffer])
{
	NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[m_iToSend % m_nEncoderBuffer]));
	m_vMappedInputBuffers[m_iToSend % m_nEncoderBuffer] = nullptr;
}

if (m_bMotionEstimationOnly && m_vMappedRefBuffers[m_iToSend % m_nEncoderBuffer])
{
	NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[m_iToSend % m_nEncoderBuffer]));
	m_vMappedRefBuffers[m_iToSend % m_nEncoderBuffer] = nullptr;
}

}

This method is failed!

For examples, I set sliceMode = 3, sliceModeData =10. I want to read back encoded slice data (such as the first slice) as the encoder is encoding the second slice. Then how can I do it? I don’t know how to do it If enableEncodeAsync set to 0.

Do subFrameWrite =1 mean the encoder can output a slice to buffer after a slice is encoded? Slice level read back means I can read a slice once?

I think ,You can try this code and will understand:

while (true)
{
    NVENC_API_CALL(m_nvenc.nvEncLockBitstream(m_hEncoder, &lockBitstreamData));
    if (status == NVENCSTATUS::NV_ENC_SUCCESS)
    {
        if (lockBitstreamData.hwEncodeStatus == 2)
        {
            //do something, copy slice or ...
            printf("bitstreamSizeInBytes = %d, numSlices = %d\n", lockBitstreamData.bitstreamSizeInBytes, lockBitstreamData.numSlices);
            break;
        }
        else
        {
            //do something, copy slice or ...
            printf("bitstreamSizeInBytes = %d, numSlices = %d\n", lockBitstreamData.bitstreamSizeInBytes, lockBitstreamData.numSlices);
        }
        NVENC_API_CALL(m_nvenc.nvEncUnlockBitstream(m_hEncoder, lockBitstreamData.outputBitstream));
    }
    else
    {
        break;
    }
}