Friday, January 20, 2017

NVidia Jetson TX1 raw bayer frames via v4l2 via /dev/video0

The NVidia Jetson TX1 dev kit comes with a 5MP camera and a bewildering array of different libraries for accessing it.  One of the routes is using v4l2 through the /dev/video0 device.  This route only offers raw bayer data, rather than the more usual YUV-style formats.  Also it only supports V4L2_MEMORY_MMAP, not V4L2_MEMORY_USERPTR or read()ing from /dev/video0.

If you 'sudo apt-get install v4l-utils' you can use this to capture a frame from the camera:

v4l2-ctl --stream-mmap --stream-to=foo.raw --stream-count=1

foo.raw is 10077696 bytes, 2 bytes for each of the 2592x1944 pixels.  If you want to write your own code to grab frames like this, this capture example gets you most of the way there, but you need to request the V4L2_PIX_FMT_SRGGB10 (raw bayer) format instead of the default.  So change init_device to be like this:

 static void init_device(void)  
 {  
  struct v4l2_format format = {0};  
  format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  format.fmt.pix.width = 2592;  
  format.fmt.pix.height = 1944;  
  format.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB10;  
  format.fmt.pix.field = V4L2_FIELD_NONE;  
  int retval = xioctl(fd, VIDIOC_S_FMT, &format);  
  if (retval == -1) { perror("Setting format\n"); exit(3); }  
   
  if (io != IO_METHOD_MMAP) {  
   printf("Sorry, Jetson TX1 v4l2 only supports mmap\n");  
   exit(4);  
  }  
   
  init_mmap();  
 }  


No comments: