title: Capturing YUYV in c++ using v4l2
body: <p>I have a webcam connected to beaglebone via usb. I am coding in c++ and my goal is to capture raw UNCOMPRESSED picture from the webcam.
  Firstly i checked what formats are supported via command <code>v4l2-ctl --list-formats</code> and the result was:</p>
  <pre><code>        Index       : 0
          Type        : Video Capture
          Pixel Format: 'MJPG' (compressed)
          Name        : Motion-JPEG
  
          Index       : 1
          Type        : Video Capture
          Pixel Format: 'YUYV'
          Name        : YUYV 4:2:2
  </code></pre>
  <p>So from this I assume it has to be possible to get an uncompressed picture if i try to use YUYV format.</p>
  <p>Knowing this I started writing a program in c++. I successfully written a program to capture a compressed picture, but when trying to capture using format YUYV it doesnt work and i really need some help to get this done.</p>
  <p>Here is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/mman.h&gt;
  #include &lt;linux/videodev2.h&gt;
  #include &lt;libv4l2.h&gt;
  
  
  template &lt;typename typeXX&gt;
  void clear_memmory(typeXX* x) {
      memset(x, 0, sizeof(*x));
      }
      
  void xioctl(int cd, int request, void *arg){
      int response;
      do{
          //ensures we get the correct response.
          response = v4l2_ioctl(cd, request, arg);
          }
      while (response == -1 &amp;&amp; ((errno == EINTR) || (errno == EAGAIN)));
  
      if (response == -1) {
          fprintf(stderr, &quot;error %d, %s\n&quot;, errno, strerror(errno));
          exit(EXIT_FAILURE);
          }
      }
      
      struct LMSBBB_buffer{
      void*  start;
      size_t length;
      };
  
  int main(){
      
      const char* dev_name = &quot;/dev/video0&quot;;
      int width=1920;
      int height=1080;
      
      int fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
      
      struct v4l2_format format = {0};
      format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      format.fmt.pix.width = width;
      format.fmt.pix.height = height;
      format.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;//V4L2_PIX_FMT_YUYV //V4L2_PIX_FMT_RGB24
      format.fmt.pix.field = V4L2_FIELD_NONE; //V4L2_FIELD_NONE
      xioctl(fd, VIDIOC_S_FMT, &amp;format);
  
          
      printf(&quot;Device initialized.\n&quot;);
      
      
      ///request buffers  
      struct v4l2_requestbuffers req = {0};
      req.count = 2;
      req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      req.memory = V4L2_MEMORY_MMAP;
      xioctl(fd, VIDIOC_REQBUFS, &amp;req);
  
      printf(&quot;Buffers requested.\n&quot;);
      
  
      ///mapping buffers  
      struct v4l2_buffer buf;
      LMSBBB_buffer* buffers;
      unsigned int i;
      buffers = (LMSBBB_buffer*) calloc(req.count, sizeof(*buffers));
      for (i = 0; i &lt; req.count; i++) {
      clear_memmory(&amp;(buf));
  
      (buf).type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      (buf).memory      = V4L2_MEMORY_MMAP;
      (buf).index       = i;
  
      xioctl(fd, VIDIOC_QUERYBUF, &amp;buf);
  
      buffers[i].length = (buf).length;
      printf(&quot;A buff has a len of: %i\n&quot;,buffers[i].length);
      buffers[i].start = v4l2_mmap(NULL, (buf).length, PROT_READ | PROT_WRITE, MAP_SHARED,fd, (buf).m.offset);
      
      if (MAP_FAILED == buffers[i].start) {
          perror(&quot;Can not map the buffers.&quot;);
          exit(EXIT_FAILURE);
          }
      }
      printf(&quot;Buffers mapped.\n&quot;);    
      
      for (i = 0; i &lt; req.count; i++) {
          clear_memmory(&amp;(buf));
          (buf).type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
          (buf).memory = V4L2_MEMORY_MMAP;
          (buf).index = i;
          ioctl(fd,VIDIOC_QBUF, &amp;(buf));
          }
      enum v4l2_buf_type type;
      type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      ioctl(fd,VIDIOC_STREAMON, &amp;type);
      
      
      printf(&quot;buffers queued and streaming.\n&quot;);
      
      
      
      int pic_count=0;
      ///CAPTURE
      fd_set fds;
      struct timeval tv;
      int r;
      char out_name[256];
      FILE* fout;
          
  
          
      do {
          FD_ZERO(&amp;fds);
          FD_SET(fd, &amp;fds);
  
          // Timeout.
          tv.tv_sec = 2;
          tv.tv_usec = 0;
  
          r = select(fd + 1, &amp;fds, NULL, NULL, &amp;tv);
          } while ((r == -1 &amp;&amp; (errno = EINTR)));
      if (r == -1) {
          perror(&quot;select&quot;);
          exit(EXIT_FAILURE);
          }
  
      clear_memmory(&amp;(buf));
      (buf).type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      (buf).memory = V4L2_MEMORY_MMAP;
      xioctl(fd,VIDIOC_DQBUF, &amp;(buf));
      
      printf(&quot;Buff index: %i\n&quot;,(buf).index);
      sprintf(out_name, &quot;image%03d.ppm&quot;,pic_count);
      fout = fopen(out_name, &quot;w&quot;);
      if (!fout) {
          perror(&quot;Cannot open image&quot;);
          exit(EXIT_FAILURE);
          }
      fprintf(fout, &quot;P6\n%d %d 255\n&quot;,width, height);
      fwrite(buffers[(buf).index].start, (buf).bytesused, 1, fout);
      fclose(fout);
      pic_count++;
      
      clear_memmory(&amp;(buf));
      (buf).type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      (buf).memory = V4L2_MEMORY_MMAP;
      xioctl(fd,VIDIOC_DQBUF, &amp;(buf));
      printf(&quot;Buff index: %i\n&quot;,(buf).index);
      sprintf(out_name, &quot;image%03d.ppm&quot;,pic_count);
      fout = fopen(out_name, &quot;w&quot;);
      if (!fout) {
          perror(&quot;Cannot open image&quot;);
          exit(EXIT_FAILURE);
          }
      fprintf(fout, &quot;P6\n%d %d 255\n&quot;,width, height);
      fwrite(buffers[(buf).index].start, (buf).bytesused, 1, fout);
      fclose(fout);
      pic_count++;
      
      
      ///xioctl(fd,VIDIOC_QBUF, &amp;(buf));
      
      
      return 0;
      }
  </code></pre>
  <p>in line 50, i can choose the format between V4L2_PIX_FMT_YUYV and V4L2_PIX_FMT_RGB24.
  for V4L2_PIX_FMT_RGB24 i get the picture, but when using V4L2_PIX_FMT_YUYV I get this error:</p>
  <pre><code>libv4l2: error dequeuing buf: Resource temporarily unavailable
  libv4l2: error dequeuing buf: Resource temporarily unavailable
  libv4l2: error dequeuing buf: Resource temporarily unavailable
  libv4l2: error dequeuing buf: Resource temporarily unavailable
  libv4l2: error dequeuing buf: Resource temporarily unavailable
  </code></pre>
  <p>the error lines goes for ever until i end the program manually.</p>
  <p>Does anyone have an idea what to do? I spent over 2 weeks on this and i can't move anywhere from here. I would really appreciate any advice.</p>