package br.carinha.rosto; import java.nio.ByteBuffer; /** Converts Camera2 planes to NV21, respecting padding and interleaved chroma. */ public final class YuvFrame { public static byte[] nv21(int w, int h, ByteBuffer[] planes, int[] rows, int[] pixels) { if (w <= 0 || h <= 0 || (w & 1) != 0 || (h & 1) != 0) throw new IllegalArgumentException("YUV requires positive even dimensions"); byte[] out = new byte[w * h * 3 / 2]; int at = 0; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) out[at++] = planes[0].get(planes[0].position() + y * rows[0] + x * pixels[0]); for (int y = 0; y < h / 2; y++) for (int x = 0; x < w / 2; x++) { out[at++] = planes[2].get(planes[2].position() + y * rows[2] + x * pixels[2]); out[at++] = planes[1].get(planes[1].position() + y * rows[1] + x * pixels[1]); } return out; } }