NVJPEG - nvBuffer to cv:Mat

I was trying to write own jpeg decoder. I could not complete the conversion from nvbuffer to cvmat while decoding. I am sharing the mat2nvbuf function with you when I was encoding. Could you please help me how to do the reverse of this process?

int mat2nvbuf(const char *inpBuf, unsigned inpBufLen, NvBuffer &buffer)
{
uint32_t i, j;
char *data;



for (i = 0; i < buffer.n_planes; i++)
{
NvBuffer::NvBufferPlane &plane = buffer.planes[i];
std::streamsize bytes_to_read = plane.fmt.bytesperpixel * plane.fmt.width;



data = (char *)plane.data;
plane.bytesused = 0;
for (j = 0; j < plane.fmt.height; j++)
{
unsigned numRead = min((unsigned)bytes_to_read, (unsigned)inpBufLen);



memcpy(data, inpBuf, numRead);



if (numRead < bytes_to_read)
{
std::cout << std::to_string(bytes_to_read) << " vs " << std::to_string(inpBufLen) << std::endl;
return -1;
}



inpBuf += numRead;
inpBufLen -= numRead;



data += plane.fmt.stride;
}
plane.bytesused = plane.fmt.stride * plane.fmt.height;
}
return 0;
}


Thank you for helping before you handle it.