将 YV12 转换为 NV21 (YUV YCrCb 4:2:0)
Convert YV12 to NV21 (YUV YCrCb 4:2:0)
如何转换:
YV12(FOOURCC代码:0x32315659)
至
NV21(FOURCC代码:0x3132564E)
(YCrCb 4:2:0 平面)
这些都是Android视频处理的常用格式,但没有直接在两者之间进行在线转换的示例。您可以通过 RGB,但我认为那样效率太低了。
最好在 C# 或 Java 中,但可以从其他任何地方转换代码...
输入是一个byte[],已知宽高
我一直在尝试遵循 Wikipedia Article 但无法正常运行。
对于赏金:一个函数获取 byte[] 并以其他格式输出 byte[]。
这是我的看法。这仍然未经测试,但它是这样的:
YV12
8 位 Y 平面后跟 8 位 2x2 子采样 V 和 U 平面。因此,单个框架将有一个全尺寸的 Y 平面,然后是 1/4 尺寸的 V 和 U 平面。
NV21
8 位 Y 平面后跟具有 2x2 子采样的交错 V/U 平面。因此,单个帧将有一个全尺寸的 Y 平面,然后是一个 8 位块中的 V 和 U。
下面是代码
public static byte[] YV12toNV21(final byte[] input,
final byte[] output, final int width, final int height) {
final int size = width * height;
final int quarter = size / 4;
final int vPosition = size; // This is where V starts
final int uPosition = size + quarter; // This is where U starts
System.arraycopy(input, 0, output, 0, size); // Y is same
for (int i = 0; i < quarter; i++) {
output[size + i*2 ] = input[vPosition + i]; // For NV21, V first
output[size + i*2 + 1] = input[uPosition + i]; // For Nv21, U second
}
return output;
}
如何转换:
YV12(FOOURCC代码:0x32315659)
至
NV21(FOURCC代码:0x3132564E)
(YCrCb 4:2:0 平面)
这些都是Android视频处理的常用格式,但没有直接在两者之间进行在线转换的示例。您可以通过 RGB,但我认为那样效率太低了。
最好在 C# 或 Java 中,但可以从其他任何地方转换代码...
输入是一个byte[],已知宽高
我一直在尝试遵循 Wikipedia Article 但无法正常运行。
对于赏金:一个函数获取 byte[] 并以其他格式输出 byte[]。
这是我的看法。这仍然未经测试,但它是这样的:
YV12 8 位 Y 平面后跟 8 位 2x2 子采样 V 和 U 平面。因此,单个框架将有一个全尺寸的 Y 平面,然后是 1/4 尺寸的 V 和 U 平面。
NV21 8 位 Y 平面后跟具有 2x2 子采样的交错 V/U 平面。因此,单个帧将有一个全尺寸的 Y 平面,然后是一个 8 位块中的 V 和 U。
下面是代码
public static byte[] YV12toNV21(final byte[] input,
final byte[] output, final int width, final int height) {
final int size = width * height;
final int quarter = size / 4;
final int vPosition = size; // This is where V starts
final int uPosition = size + quarter; // This is where U starts
System.arraycopy(input, 0, output, 0, size); // Y is same
for (int i = 0; i < quarter; i++) {
output[size + i*2 ] = input[vPosition + i]; // For NV21, V first
output[size + i*2 + 1] = input[uPosition + i]; // For Nv21, U second
}
return output;
}