title: How to get stride and Y plane alignment values for MediaCodec encoder
body: <p>There's a few related questions and discussions on this subject:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/15739684/mediacodec-and-camera-color-space-incorrect">Mediacodec and camera, color space incorrect</a></li>
  <li><a href="https://stackoverflow.com/questions/17493169/getting-qualcomm-encoders-to-work-via-mediacodec-api">Getting QualComm encoders to work via MediaCodec API</a></li>
  <li><a href="https://groups.google.com/d/msg/android-platform/awaNwgb6EbY/a-YiIOwaL0QJ" rel="nofollow noreferrer">https://groups.google.com/d/msg/android-platform/awaNwgb6EbY/a-YiIOwaL0QJ</a></li>
  <li><a href="https://code.google.com/p/android/issues/detail?id=37769" rel="nofollow noreferrer">https://code.google.com/p/android/issues/detail?id=37769</a></li>
  </ul>
  
  <p>I am feeding camera preview frames (<code>NV21</code> converted to <code>NV12</code>) to <code>MediaCodec</code> encoder (<code>NV12</code> aka <code>COLOR_FormatYUV420SemiPlanar</code>). It looks like that on some devices with <code>QualComm</code> encoder which are running Android versions less than <code>4.3</code> I have to do some input frames processing in order to receive back frame with correct color.</p>
  
  <p>On <code>Sony Xperia ZR</code> running <code>Android 4.2.2</code> I have to add <code>Y</code> plane alignment in order to make it working on almost all resolutions. Code above adds <code>1024</code> bytes alignment for widths which can not be divided by <code>32</code> and <code>2048</code> bytes alignment for other resolutions. It makes <code>MediaCodec</code> to encode frames properly for all resolutions which can be divided by <code>16</code> (except <code>176x144</code> for which <code>UV</code> plane looks misaligned).</p>
  
  <pre><code>int getYPadding() {
      if (mediaCodecInfo.getName().contains("OMX.qcom") &amp;&amp; android.os.Build.VERSION.SDK_INT &lt; 18) {
          if ((getWidth() % 32) != 0) {
              return (getWidth()*getHeight()) % 1024;
          } else {
              return (getWidth()*getHeight()) % 2048;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>I've tried to test this alignment on <code>LG G2</code> which is running same <code>Android 4.2.2</code> and has <code>QualComm</code> encoder, and it looks like it does not working on it correctly. <code>UV</code> plane is misaligned (a green stripe at the bottom of the frame). I was not able to calculate the padding which will work for both phones.</p>
  
  <p>I also have access to <code>Sony Xperia Z1</code> running <code>Android 4.3</code> with <code>QualComm</code> chipset and it looks like it does not have such problems. Video on every resolution looks fine and <code>Y</code> plane does not needs to be aligned anyhow.</p>
  
  <p>I understand that it's hardware-related and might be complicated, but since I have to support users running Android prior to <code>4.3</code> I have a question. Is it possible to programmatically determine <code>Y</code> plane alignment and vertical / horizontal stride values which encoder is expecting for the given color format?</p>