Skip to content

Instantly share code, notes, and snippets.

@dukess
Created March 15, 2021 14:01
Show Gist options
  • Save dukess/fcd83c0311567c044934a835a52914ce to your computer and use it in GitHub Desktop.
Save dukess/fcd83c0311567c044934a835a52914ce to your computer and use it in GitHub Desktop.
Convert YUV NV21 to I420
/** Convert NV21 (YYYYYYYYY:VUVU) to I420 (YYYYYYYY:UU:VV)
Convert I420 (YYYYYYYY:UU:VV) to NV21 (YYYYYYYYY:VUVU) can be get here: https://gist.github.com/pnemonic78/2d8411ed6b0bd6c71b651800bc97eb8e
*/
public byte[] NV21toI420(final byte[] input, byte[] output, final int width, final int height) {
if (output == null) {
output = new byte[input.length];
}
final int size = width * height;
final int quarter = size / 4;
final int v0 = size + quarter;
System.arraycopy(input, 0, output, 0, size); // Y is same
for (int u = size, v = v0, o = size; u < v0; u++, v++, o += 2) {
output[v] = input[o]; // For NV21, V first
output[u] = input[o + 1]; // For NV21, U second
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment