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