Skip to content

Instantly share code, notes, and snippets.

@Pikaurd
Last active July 4, 2018 09:57
Show Gist options
  • Save Pikaurd/cfa52356e30f851ca2fdc29947bf836d to your computer and use it in GitHub Desktop.
Save Pikaurd/cfa52356e30f851ca2fdc29947bf836d to your computer and use it in GitHub Desktop.
Resize nv12 raw data
public static byte[] nv12ScaleDown(byte[] bytes, int width, int height) {
int newWidth = width >> 1;
int newHeight = height >> 1;
byte []result = new byte[bytes.length >> 2];
final int sourceOffset = width * height;
final int destOffset = newWidth * newHeight;
for (int h = 0; h < newHeight; h++) {
for (int w = 0; w < newWidth; w++) {
// manipulate y
byte sourceY = bytes[(w << 1) + h * (width << 1)];
result[w + h * newWidth] = sourceY;
// manipulate uv
if ((w & 0x1) == 0 && (h & 0x1) == 0) {
int indexSourceU = (w << 1) + h * width + sourceOffset;
int indexSourceV = indexSourceU + 1;
int indexDestU = w + (h >> 1) * newWidth + destOffset;
result[indexDestU] = bytes[indexSourceU];
result[indexDestU + 1] = bytes[indexSourceV];
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment