Jetson Orin V4L2_MEMORY_USERPTR capture fail

When I capture the 4096*3072 BGGR10 format image on jetson orin, I can capture pictures normally if I use V4L2_MEMORY_MMAP ;
but if I use V4L2_MEMORY_USERPTR, ioctl(fd, VIDIOC_QBUF) return -1. Here is my code section :

#ifdef MMAP_MODE
     //... V4L2_MEMORY_MMAP code
#else
 struct v4l2_requestbuffers req;
	memset(&req,0,sizeof(req));
	req.count = MMAP_BUF_NUM;  
	req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	req.memory = V4L2_MEMORY_USERPTR;
	if(-1 == ioctl(fd, VIDIOC_REQBUFS,&req))
	{
		printf("VIDIOC_REQBUFS fail");
		close(fd);
		return -1;
	}

	unsigned int page_size = getpagesize();
	unsigned int map_buf_size =  ceil((float)WIDTH * HEIGHT * 2 / page_size) * page_size;

	for (int n = 0; n < MMAP_BUF_NUM; ++n)
	{
		buffers[n].length = map_buf_size;
		buffers[n].start = aligned_alloc(map_buf_size, page_size);
		if (NULL == buffers[n].start)
		{
			printf("Align memory alloc failed.");
			return -1;
		} 
	}

	unsigned int i;
	for (i = 0; i < MMAP_BUF_NUM; ++i) 
	{
		struct v4l2_buffer buf;
		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf.memory = V4L2_MEMORY_USERPTR;
		buf.index = i;
		buf.m.userptr = (unsigned long)buffers[i].start;
		buf.length = buffers[i].length;
		if(-1 == ioctl(fd, VIDIOC_QBUF, &buf))
		{
			printf("VIDIOC_QBUF fail ");
			close(fd);
			return -1;
		}	
    }
#endif

Then i use v4l2-ctl to test, there is the same problem.

root@smartsens-desktop:~/test/build# v4l2-ctl -d /dev/video0 --stream-mmap
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.03 fps
<<<<<^C
root@smartsens-desktop:~/test/build# v4l2-ctl -d /dev/video0 --stream-user
                VIDIOC_QBUF returned -1 (Invalid argument)
root@smartsens-desktop:~/test/build# 

Is this a problem with a specific sdk version?

root@smartsens-desktop:~/test/build# cat /etc/nv_tegra_release 
# R35 (release), REVISION: 3.1, GCID: 32827747, BOARD: t186ref, EABI: aarch64, DATE: Sun Mar 19 15:19:21 UTC 2023

Is there a good solution?

Please get the v4l-utils source and apply the patch to try.

userptr.diff (9.7 KB)

https://www.linuxtv.org/wiki/index.php/V4l-utils

Thanks for your help. Referring to the patch file you provided, the function of memory allocation has been modified, and the problem has been solved.

------>>

posix_memalign(&buffers[n].start, page_size, map_buf_size);

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