title: List available capture formats
body: <p>New to V4L, I decided to start using the video4linux2 library in order to capture a frame from my camera in C (I am using the uvcvideo module with a Ricoh Co. camera). I followed several guides and tutorials, and managed to get a running program. My question is mainly about this usual code line :</p>
  
  <pre><code>struct v4l2_format format = {0};
  format.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
  // ...
  </code></pre>
  
  <p>This is where I set the actual video format to use when capturing. As you can see, in this sample, I'm using MJPEG (<a href="http://lxr.free-electrons.com/source/include/uapi/linux/videodev2.h#L390" rel="noreferrer">http://lxr.free-electrons.com/source/include/uapi/linux/videodev2.h#L390</a>). Even though this might be a great format and all, my application will probably require simple RGB formatting, pixel per pixel I guess. For this reason, I tried using RGB format constants such as <code>V4L2_PIX_FMT_RGB24</code>. Now for some reason... v4l2 doesn't like it. I'm guessing this is hardware-related, but I'd like to avoid MJPEG manipulations as much as possible. For testing purposes, I tried using other constants and formats, but no matter what I did, v4l2 kept changing the <code>pixelformat</code> field's value :</p>
  
  <pre><code>xioctl(fd, VIDIOC_S_FMT, &amp;format); // This call succeeds with errno != EINTR.
  if(format.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24){
      // My program always enters this block when not using MJPEG.
      fprintf(stderr, "Format wasn't accepted by v4l2.");
      exit(4);
  }
  </code></pre>
  
  <p>Now my question is : is there a way I could get a list of accepted video formats (and I mean, accepted by my camera/v4l2), from which I could pick something else than MJPEG ? If you think I have to stick with MJPEG, would you recommend me any library allowing me to manipulate it, and eventually, to draw back the capture in a GUI frame ?</p>
  
  <h2>Barbarian test code</h2>
  
  <p>I used the following trick to test all available formats on my hardware. First, some shell script to get a list of all formats...</p>
  
  <pre><code>grep 'V4L2_PIX_FMT' /usr/include/linux/videodev2.h | grep define | tr '\t' ' ' | cut -d' ' -f2 | sed 's/$/,/g'
  </code></pre>
  
  <p>... the output of which is used in this C program :</p>
  
  <pre><code>int formats[] = {/* result of above command */};
  int i = 0;
  struct v4l2_format format = {0};
  
  for(i = 0; i &lt; /* number of lines in previous command output */; i++){
      format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      format.fmt.pix.width = 320;
      format.fmt.pix.height = 240;
      format.fmt.pix.pixelformat = formats[i];
      format.fmt.pix.field = V4L2_FIELD_NONE;
      if(xioctl(fd, VIDIOC_S_FMT, &amp;format) != -1 &amp;&amp; format.fmt.pix.pixelformat == formats[i])
          fprintf(stderr, "Accepted : %d\n", i);
  }
  </code></pre>
  
  <p>This test reveals that only <code>V4L2_PIX_FMT_YUYV</code> and <code>V4L2_PIX_FMT_MJPEG</code> are functional. Any way I could improve this, or is it hardware-related ?</p>