Search code examples
androidandroid-mediacodec

android - MediaCodec decoder 1080p output yuv format


Android MediaCodec can be used to decode output yuv420 format. I guess it should be NV12 format, but when I try it on Nexus6 7.1.1 device. The result is very stranger:

  1. For 720p video, it works fine, the output yuv can be playbacked by ffplay with below command:

    ffplay -v info -f rawvideo -pixel_format yuv420p -video_size 1280x720 out.yuv

    And the out.yuv file size is exactly width*height*3/2.

  2. For 1080p video, the output yuv color not correct at top. And the out.yuv file size is 3133440, but width*height*3/2 should be 3110400, so another 23040 bytes is there. Sounds like the top layer color not correct.

enter image description here

When try to decode it to surface, it looks good. so I guess issue come from 1080p YUV package only.


Solution

  • For 1080p, it's very likely that the height of the actual decoded picture is 1920x1088, not 1920x1080. (1920x1088 adds up to the size of 3133440.) The bottommost 8 pixels are decoded and returned, but you're not supposed to show them.

    In this case, the MediaFormat.KEY_HEIGHT field in the output MediaFormat probably is 1088, while the crop-top field is 0 and the crop-bottom is 1079. See https://android.googlesource.com/platform/cts/+/jb-mr2-release/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java#976 for an example on how these fields are read.

    As beetlej suggested, if you use the Image class received from the MediaCodec.getOutputImage method (available since Android 5.0), these details will be taken care of.