Dma buffer to opencv mat (after jetpack 5.1.1)

On Jetpack 5.1.1, I was able to get capture data from dma buffer into an opencv mat like this:

void* pdata = NULL;
NvBufferMemMap(dmabuf_fd, 0, NvBufferMem_Read, &pdata);
NvBufferMemSyncForCpu(dmabuf_fd, 0, &pdata);
cv::Mat rgba = cv::Mat(m_size.height(), m_size.width(), CV_8UC4, pdata);
cv::cvtColor(bgra, out, cv::COLOR_RGBA2RGB);
NvBufferMemUnMap(dmabuf_fd, 0, &pdata);

where i used this colorFormat for my NvBuffer:

input_params.colorFormat = NVBUF_COLOR_FORMAT_RGBx;

I now flashed with Jetpack 5.1.3 and can see that nvbuf_utils is deprecated and gone, meaning I can’t do NvBufferMemMap, NvBufferMemSyncForCpu and NvBufferMemUnMap anymore. What can I do instead? I can see something about migrating to NvBufSurfaceMap but i would like a complete example to make sure i understand what is the right way now.

In genera, what is the best approach for getting dma buffer into an opencv mat? Is it best/fastest to let jetson multimedia api convert from YUV to RGB like i did before? My RGB frames in the end gets cropped up into a batch of images, resized and converted to NCHW before calling tensorrt on the batch.

Hi,
We support jetson_multimedia_api and gstreamer. Please make sure you have checked the document:
https://docs.nvidia.com/jetson/archives/r36.3/DeveloperGuide/SD/Multimedia.html
https://docs.nvidia.com/jetson/archives/r36.3/ApiReference/index.html
There are some examples in

Q: Is there any example of running RTSP streaming?
Q: Is there an example for running UDP streaming?
Q: I have a USB camera. How can I launch it on AGX Orin?

For further issues, please share a method to replicate the issue through gstreamer command, or either sample. We will set up developer kit and check.

Thanks!

this answer got here within 5 seconds of posting. it must be generated by a LLM bot. i already asked claude about solutions without any success. hopefully there is a human somewhere who can help me

Hi,
NvBuffer APIs are deprecated on Jetpack 5. Please use NvBufSurface APIs:

Jetson Linux 35.4.1 | NVIDIA Developer
nvbuf_utils to NvUtils Migration Guide

If anyone stumbles on this thread, I fixed it with this:

NvBufSurface *nvbuf_surf = 0;
int ret = NvBufSurfaceFromFd(dmabuf_fd, (void**)(&nvbuf_surf));
if(ret != 0) {
    printf("NvBufSurfaceFromFd return value error\n");
}
NvBufSurfaceMap(nvbuf_surf, -1, 0, NVBUF_MAP_READ);
NvBufSurfaceSyncForCpu(nvbuf_surf, -1, 0);
cv::Mat rgba = cv::Mat(m_size.height(), m_size.width(), CV_8UC4, nvbuf_surf->surfaceList->mappedAddr.addr[0]);
cv::cvtColor(rgba, out, cv::COLOR_RGBA2RGB);
NvBufSurfaceUnMap(nvbuf_surf, -1, 0);
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.