【发布时间】:2017-04-12 17:57:23
【问题描述】:
我正在尝试使用 MediaCodec 和相机 (onPreviewFrame) 对 .h264 视频进行编码。我无法将颜色空间从 YV12(来自相机)转换为 COLOR_FormatYUV420SemiPlanar(编码器需要)。
编辑:我注意到这可能是 MediaCodec 上的一个错误,因为以下代码适用于其他设备:
public static byte[] YV12toYUV420PackedSemiPlanar(final byte[] input, final byte[] output, final int width, final int height) {
/*
* COLOR_TI_FormatYUV420PackedSemiPlanar is NV12
* We convert by putting the corresponding U and V bytes together (interleaved).
*/
final int frameSize = width * height;
final int qFrameSize = frameSize / 4;
System.arraycopy(input, 0, output, 0, frameSize); // Y
for (int i = 0; i < qFrameSize; i++) {
output[frameSize + i * 2] = input[frameSize + i + qFrameSize]; // Cb (U)
output[frameSize + i * 2 + 1] = input[frameSize + i]; // Cr (V)
}
return output;
}
这是我得到的结果(好像颜色位有一些偏移):
编辑 2:帧大小为 1280x720,设备为 Samsung s5(SM-G900V),带有运行 Android Lollipop 5.0 (API 21) 的 OMX.qcom.video.encoder.avc。
注意:我知道 COLOR_FormatSurface,但我需要在 API 16 上完成这项工作。
【问题讨论】:
标签: android video-capture android-mediacodec color-space