title: Optimize YUY2 to I420 conversion
body: <p>I am writing a video conference application with x264 as encoder. I receive YUY2 raw frames from the webcam and then convert them into I420, as requested by x264. The problem is - I wrote conversion algoritm by myself and it is not so fast (up 20% of the whole CPU time, data provided by profiler). How can I make it faster? Here is my code:</p>
  
  <pre><code>int YUY2ToI420(BYTE *in, BYTE *out){
  
      long pixels = _width * _height;
      long macropixels = pixels / 2; // macropixel count
      // new size will be w * h * 3/2 -&gt; 12 bits per pixel 4:2:0
  
      long mpx_per_row = info.biWidth / 2;
  
      // for each macropixel
      for (int i = 0, ci = 0; i &lt; macropixels; i++){ // ci is chroma index
          // get macropixel address, order is Y0 U0 Y1 V0
          BYTE *mpAddress = in + i * 4;
  
          // copy luma data
          out[i * 2] = mpAddress[0];
          out[i * 2 + 1] = mpAddress[2];
  
          // copy chroma data - we skip odd rows because of 4:2:0 sampling
          long row_number = i / mpx_per_row;
          if (row_number % 2 == 0) {
              out[pixels + ci] = mpAddress[1]; // shift by Y vector
              out[pixels + pixels / 4 + ci] = mpAddress[3]; // shift by Y and U vector
              ci++;
          }
      }
  
      return pixels * 12 / 8; // I420
  }
  </code></pre>