title: Two-thirds of screen is green when encode with MediaCodec
body: <p>I am working on a project that needs to process a video using OpenGL on Android. I decided to use MediaCodec and I managed to get it works with the help from <a href="https://android.googlesource.com/platform/cts/+/jb-mr2-release/tests/tests/media/src/android/media/cts/ExtractDecodeEditEncodeMuxTest.java" rel="nofollow noreferrer">ExtractDecodeEditEncodeMuxTest</a>. The result is quite good, I have it receives a video, extracts the tracks, decodes the videotrack, edits with OpenGL, and encodes to a video file.</p>
  
  <p>The problem is that the result video can be play well on Android, but when it comes to iOS, two-thirds of the screen is green.</p>
  
  <p>I tried to solve with the suggestions from <a href="https://stackoverflow.com/questions/30405018/android-video-decoder-not-drawing-to-gles-surface-on-lollipop-only">here</a>, <a href="https://stackoverflow.com/questions/15215636/re-encoding-h-264-content-with-a-different-bit-rate-using-android-mediacodec">here</a>, and <a href="https://stackoverflow.com/questions/25356314/received-wrong-frame-data-with-extractmpegframestest-in-android">here</a>, experiment different formats for the encoder, but the problem is still the same.</p>
  
  <p>Could someone suggest me the reasons that can cause this problem and how to fix it?</p>
  
  <p><a href="https://i.stack.imgur.com/Qe7RZ.png" rel="nofollow noreferrer">This is the video when it's played on iOS</a></p>
  
  <p>This is the configuration for the encoder</p>
  
  <pre><code>MediaCodec mediaCodec = MediaCodec.createEncoderByType("video/avc");
  MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 540, 960);
  mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 2000000);
  mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
  mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatSurface);
  mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
  mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  </code></pre>
  
  <p><strong>Update</strong>
  I wonder if i had any mistake with the video orientation, because the working partial of the output video has the same ratio as the desired output resolution, but in horizontal orientation. The input is vertical recorded, so does the desired output.
  Here is the code of the decoder configuration:</p>
  
  <pre><code>inputFormat.setInteger(MediaFormat.KEY_WIDTH, 540);
  inputFormat.setInteger(MediaFormat.KEY_HEIGHT, 960);
  inputFormat.setInteger("rotation-degrees", 90);
  
  String mime = inputFormat.getString(MediaFormat.KEY_MIME);
  MediaCodec decoder = MediaCodec.createDecoderByType(mime);
  decoder.configure(inputFormat, surface, null, 0);
  </code></pre>
  
  <p><strong>Update Dec 25:</strong> I've tried different resolutions and orientations when configuring both encoder and decoder to check if the video's orientation is the problem or not, but the output video just got rotated, the green problem is still there.
  I also tried "video/mp4v-es" for the encoder, the result video is viewable on Mac, but the iPhone cannot even play it.</p>