title: Convert V4L2_PIX_FMT_YUYV(YUV 4:2:2) to V4L2_PIX_FMT_YVU420(YUV 4:2:0)
body: <p>I have been trying to convert YUYV raw file to YUV420 raw file in C++. The format are documented as in <a href="https://www.linuxtv.org/downloads/v4l-dvb-apis-old/yuv-formats.html" rel="nofollow noreferrer" title="YUV formats">YUV Formats Page</a>. My output file shows a greenish look after conversion. I adopted the source code from <a href="https://www.experts-exchange.com/questions/27638324/speed-up-yuv422-to-yuv420-software-conversion.html" rel="nofollow noreferrer">Experts Exchange</a>, to convert from YUYV instead of UYVY. Here is my source code:</p>
  
  <pre><code>void conv422to420(uint8_t* src, uint8_t* dst)
  {
    uint8_t* pyFrame = dst;
    uint8_t* puFrame = pyFrame + width_*height_; // Cb
    uint8_t* pvFrame = puFrame + width_*height_/4; // Cr
  
    int uvOffset = width_ * 2 * sizeof(uint8_t);
  
    int i,j;
  
    for(i=0; i&lt;height_-1; i++)
    {
      for(j=0; j&lt;width_; j+=2)
      {
          auto evenRow = ((i&amp;1) == 0);
          *pyFrame++ = *src++;
          ++src;
          uint16_t calc;
          if (evenRow)
          {
              calc = *src;
              calc += *(src + uvOffset);
              calc /= 2;
              *pvFrame++ = (uint8_t) calc;
          }
          *pyFrame++ = *src++;
          ++src;
          if (evenRow)
          {
              calc = *src;
              calc += *(src + uvOffset);
              calc /= 2;
              *puFrame++ = (uint8_t) calc;
          }
      }
    }
  }
  </code></pre>
  
  <p>In general I took the average of the two U and two V values in two adjacent rows in YUYV to be the values in YUV420. I am just not sure if the "averaging it" is the right way to do this. Because I used </p>
  
  <blockquote>
    <p>ffmpeg -y -r 25.0 -f rawvideo -s 1280x720 -pix_fmt yuyv422 -i yuyv422.yuv -pix_fmt yuv420p -f rawvideo -r 25.0 -s 1280x720 -v debug yuv420p.yuv</p>
  </blockquote>
  
  <p>to generate the YUV420 raw file successfully and the diff for my own output file is drastically different from the one ffmpeg generated in U and Y channels.</p>
  
  <p>So my question is, are there any open source solution for such conversion？ And what is wrong with my solution?</p>