问题
How can you convert:
YV12 (FOOURCC code: 0x32315659)
to
NV21 (FOURCC code: 0x3132564E)
(YCrCb 4:2:0 Planar)
These are both common formats for Android video handling, but there is no example online converting directly between the two. You can go through RGB but I assume that will be too inefficient.
Ideally in C# or Java, but can convert code from whatever else...
The input is a byte[], and the width and height are known.
I have been trying to follow the Wikipedia Article but cannot get it to function cleanly.
For the bounty: a function taking the byte[] and outputting a byte[] in the other format.
回答1:
This is my take on it. This is still untested but here it goes:
YV12 8 bit Y plane followed by 8 bit 2x2 subsampled V and U planes. So a single frame will have a full size Y plane followed 1/4th size V and U planes.
NV21 8-bit Y plane followed by an interleaved V/U plane with 2x2 subsampling. So a single frame will have a full size Y plane followed V and U in a 8 bit by bit blocks.
So here goes the code
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;
}
来源:https://stackoverflow.com/questions/42000543/convert-yv12-to-nv21-yuv-ycrcb-420