Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// Pixels for the guest's camera (kwcam, camera.ts): the three raw formats// it offers, conversions into them, and a synthetic test pattern; and the// fourcc of the compressed one (H.264, which the source encodes:// camera-chunks.ts, camera-h264.ts). Universal.//// - I420 (V4L2_PIX_FMT_YUV420, "YU12"; kwcam's default): planar 4:2:0, a Y// plane (width bytes a row), then Cb and Cr planes at half the width and// height (width / 2 bytes a row): ffmpeg's yuv420p as is.// - NV12: 4:2:0, a Y plane (width bytes a row), then one plane of Cb Cr// pairs at half the height (width bytes a row).// - YUYV: packed 4:2:2, Y0 Cb Y1 Cr per two pixels, width * 2 bytes a row.// - All are BT.601 with limited range (Y 16-235, Cb and Cr 16-240), as// the kernel reports (V4L2_YCBCR_ENC_601, V4L2_QUANTIZATION_LIM_RANGE),// what webcams give and what Chromium and the VideoFrame planes of a// camera track use. RGBA input is converted with the BT.601 integer// formulas; chroma is the average of the pixels it covers.// - Sources of another size are scaled by the caller (js/web/camera.ts// draws them into a canvas of the stream's size); these work on frames// of the stream's size, except the 4:2:0 ones, which take each plane's// offset and stride (VideoFrame.copyTo()'s layout).
/** The raw formats: the host writes the frame. */export type CameraPixelFormat = "I420" | "NV12" | "YUYV";/** The compressed formats: the source encodes, the guest copies the stream (kwcam offers each only where the source makes it). */export type CameraCodec = "H264";/** A stream's format. */export type CameraStreamFormat = CameraPixelFormat | CameraCodec;
/** The compressed formats there are, in the kernel's bit order (wasm_driver_camera_init()'s KWCAM_INIT_* bits). */export const CAMERA_CODECS: readonly CameraCodec[] = ["H264"];
/** V4L2 fourccs (little-endian, as the kernel passes them). */export const CAMERA_FOURCC_YUYV = 0x56595559;export const CAMERA_FOURCC_NV12 = 0x3231564e;/** V4L2_PIX_FMT_YUV420 ("YU12"): I420 */export const CAMERA_FOURCC_I420 = 0x32315559;export const CAMERA_FOURCC_H264 = 0x34363248;
export function cameraFourcc(f: CameraStreamFormat): number { return f === "NV12" ? CAMERA_FOURCC_NV12 : f === "I420" ? CAMERA_FOURCC_I420 : f === "H264" ? CAMERA_FOURCC_H264 : CAMERA_FOURCC_YUYV;}
export function cameraPixelFormat(fourcc: number): CameraStreamFormat | null { return fourcc === CAMERA_FOURCC_YUYV ? "YUYV" : fourcc === CAMERA_FOURCC_NV12 ? "NV12" : fourcc === CAMERA_FOURCC_I420 ? "I420" : fourcc === CAMERA_FOURCC_H264 ? "H264" : null;}
/** A 4:2:0 format (its frame is width * height * 3 / 2 bytes: Y, then chroma at half the height). */export function camera420(f: CameraStreamFormat): f is "I420" | "NV12" { return f === "I420" || f === "NV12";}
/** A compressed format (its buffers hold chunks of the stream, not frames). */export function cameraCompressed(f: CameraStreamFormat): f is CameraCodec { return f === "H264";}
/** Bytes of one frame; for a compressed format the buffer's size, the largest chunk (kwcam's sizeimage: the frame's size as NV12). */export function cameraFrameBytes(f: CameraStreamFormat, width: number, height: number): number { return f === "YUYV" ? width * height * 2 : width * height * 3 / 2;}
/** One plane of a 4:2:0 frame: where it starts and how many bytes a row. */export interface PlaneLayout { offset: number; stride: number;}
const yOf = (r: number, g: number, b: number) => ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;const uOf = (r: number, g: number, b: number) => ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;const vOf = (r: number, g: number, b: number) => ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
/** RGBA (width * 4 bytes a row) to YUYV. */export function rgbaToYuyv(src: ArrayLike<number>, width: number, height: number, out: Uint8Array): void { let o = 0; for (let y = 0; y < height; y++) { let i = y * width * 4; for (let x = 0; x < width; x += 2, i += 8) { const r0 = src[i]!, g0 = src[i + 1]!, b0 = src[i + 2]!; const r1 = src[i + 4]!, g1 = src[i + 5]!, b1 = src[i + 6]!; const r = (r0 + r1) >> 1, g = (g0 + g1) >> 1, b = (b0 + b1) >> 1; out[o] = yOf(r0, g0, b0); out[o + 1] = uOf(r, g, b); out[o + 2] = yOf(r1, g1, b1); out[o + 3] = vOf(r, g, b); o += 4; } }}
/** RGBA (width * 4 bytes a row) to NV12. */export function rgbaToNv12(src: ArrayLike<number>, width: number, height: number, out: Uint8Array): void { rgbaTo420(src, width, height, out, false);}
/** RGBA (width * 4 bytes a row) to I420. */export function rgbaToI420(src: ArrayLike<number>, width: number, height: number, out: Uint8Array): void { rgbaTo420(src, width, height, out, true);}
function rgbaTo420(src: ArrayLike<number>, width: number, height: number, out: Uint8Array, planar: boolean): void { const w4 = width * 4, n = width * height; for (let y = 0; y < height; y++) { let i = y * w4, o = y * width; for (let x = 0; x < width; x++, i += 4) out[o++] = yOf(src[i]!, src[i + 1]!, src[i + 2]!); } let o = n, u = n, v = n + (n >> 2); for (let y = 0; y < height; y += 2) { let i = y * w4; for (let x = 0; x < width; x += 2, i += 8) { const j = i + w4; const r = (src[i]! + src[i + 4]! + src[j]! + src[j + 4]! + 2) >> 2; const g = (src[i + 1]! + src[i + 5]! + src[j + 1]! + src[j + 5]! + 2) >> 2; const b = (src[i + 2]! + src[i + 6]! + src[j + 2]! + src[j + 6]! + 2) >> 2; if (planar) { out[u++] = uOf(r, g, b); out[v++] = vOf(r, g, b); } else { out[o++] = uOf(r, g, b); out[o++] = vOf(r, g, b); } } }}
/** RGBA to the stream's raw format. */export function rgbaTo(format: CameraPixelFormat, src: ArrayLike<number>, width: number, height: number, out: Uint8Array): void { if (format === "YUYV") rgbaToYuyv(src, width, height, out); else rgbaTo420(src, width, height, out, format === "I420");}
/** The layout of a frame of `format` (I420 or NV12) as kwcam's buffer holds it, for VideoFrame.copyTo() straight into it. */export function camera420Layout(format: "I420" | "NV12", width: number, height: number): PlaneLayout[] { const n = width * height; return format === "NV12" ? [{ offset: 0, stride: width }, { offset: n, stride: width }] : [{ offset: 0, stride: width }, { offset: n, stride: width >> 1 }, { offset: n + (n >> 2), stride: width >> 1 }];}
/** * A 4:2:0 frame of the stream's size, planar ("I420": Y, U, V planes) or * semi-planar ("NV12": Y and UV planes), with the planes' layout, into * the stream's format. */export function yuv420To(src: Uint8Array, layout: readonly PlaneLayout[], chroma: "I420" | "NV12", width: number, height: number, format: CameraPixelFormat, out: Uint8Array): void { const yp = layout[0]!, up = layout[1]!, vp = chroma === "I420" ? layout[2]! : layout[1]!; const step = chroma === "I420" ? 1 : 2, vOff = chroma === "I420" ? 0 : 1; if (format === "I420") { for (let y = 0; y < height; y++) out.set(src.subarray(yp.offset + y * yp.stride, yp.offset + y * yp.stride + width), y * width); const n = width * height, cw = width >> 1, ch = height >> 1; let uo = n, vo = n + (n >> 2); for (let y = 0; y < ch; y++) { if (chroma === "I420") { out.set(src.subarray(up.offset + y * up.stride, up.offset + y * up.stride + cw), uo); out.set(src.subarray(vp.offset + y * vp.stride, vp.offset + y * vp.stride + cw), vo); uo += cw; vo += cw; continue; } // (NV12: the CbCr pairs split) let c = up.offset + y * up.stride; for (let x = 0; x < cw; x++, c += 2) { out[uo++] = src[c]!; out[vo++] = src[c + 1]!; } } return; } if (format === "NV12") { for (let y = 0; y < height; y++) out.set(src.subarray(yp.offset + y * yp.stride, yp.offset + y * yp.stride + width), y * width); let o = width * height; const cw = width >> 1; for (let y = 0; y < height >> 1; y++) { if (chroma === "NV12") { out.set(src.subarray(up.offset + y * up.stride, up.offset + y * up.stride + width), o); o += width; continue; } let u = up.offset + y * up.stride, v = vp.offset + y * vp.stride; for (let x = 0; x < cw; x++) { out[o++] = src[u++]!; out[o++] = src[v++]!; } } return; } let o = 0; for (let y = 0; y < height; y++) { let yi = yp.offset + y * yp.stride; let u = up.offset + (y >> 1) * up.stride, v = vp.offset + (y >> 1) * vp.stride + vOff; for (let x = 0; x < width; x += 2) { out[o] = src[yi]!; out[o + 1] = src[u]!; out[o + 2] = src[yi + 1]!; out[o + 3] = src[v]!; o += 4; yi += 2; u += step; v += step; } }}
/** A black frame (Y 16, Cb and Cr 128), as the kernel fills one. */export function blackFrame(format: CameraPixelFormat, width: number, height: number, out: Uint8Array): void { if (format !== "YUYV") { out.fill(16, 0, width * height); out.fill(128, width * height, width * height * 3 / 2); return; } for (let i = 0; i < width * height * 2; i += 4) { out[i] = 16; out[i + 1] = 128; out[i + 2] = 16; out[i + 3] = 128; }}
// 75% colour bars (white, yellow, cyan, green, magenta, red, blue, black) in BT.601 limited range.const BARS: readonly (readonly [number, number, number])[] = [ [180, 128, 128], [162, 44, 142], [131, 156, 44], [112, 72, 58], [84, 184, 198], [65, 100, 212], [35, 212, 114], [16, 128, 128],];// Digits 0-9, 3x5, one row of three bits per number (MSB left).const DIGITS = [ [7, 5, 5, 5, 7], [2, 6, 2, 2, 7], [7, 1, 7, 4, 7], [7, 1, 7, 1, 7], [5, 5, 7, 1, 1], [7, 4, 7, 1, 7], [7, 4, 7, 5, 7], [7, 1, 1, 1, 1], [7, 5, 7, 5, 7], [7, 5, 7, 1, 7],];
/** * The synthetic camera's frame `n` (the CLI's --camera, tests): colour * bars that scroll left by 4 pixels a frame, and `n` in digits, black on * white, in the top left corner. The same `n` always gives the same bytes. */export function testPattern(format: CameraPixelFormat, width: number, height: number, n: number, out: Uint8Array): void { const barW = Math.max(2, Math.floor(width / 8)) & ~1; const shift = (n * 4) % width; const bar = (x: number) => BARS[Math.floor(((x + shift) % width) / barW) % 8]!; // The counter: up to 8 digits, scale s, a white box with a margin of s. const s = Math.max(2, Math.floor(height / 48)); const text = String(Math.floor(n) % 1e8); const boxW = (text.length * 4 + 1) * s, boxH = 7 * s; const inBox = (x: number, y: number) => x < boxW && y < boxH; const ink = (x: number, y: number): boolean => { const cx = Math.floor(x / s) - 1, cy = Math.floor(y / s) - 1; if (cx < 0 || cy < 0 || cy >= 5) return false; const d = Math.floor(cx / 4), col = cx % 4; if (d >= text.length || col === 3) return false; return ((DIGITS[text.charCodeAt(d) - 48]![cy]! >> (2 - col)) & 1) === 1; }; const lumaAt = (x: number, y: number) => (inBox(x, y) ? (ink(x, y) ? 16 : 235) : bar(x)[0]); if (format !== "YUYV") { for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) out[y * width + x] = lumaAt(x, y); const n = width * height; let o = n, u = n, v = n + (n >> 2); for (let y = 0; y < height; y += 2) { for (let x = 0; x < width; x += 2) { const c = inBox(x, y) ? null : bar(x); if (format === "I420") { out[u++] = c ? c[1] : 128; out[v++] = c ? c[2] : 128; } else { out[o++] = c ? c[1] : 128; out[o++] = c ? c[2] : 128; } } } return; } let o = 0; for (let y = 0; y < height; y++) { for (let x = 0; x < width; x += 2) { const c = inBox(x, y) ? null : bar(x); out[o] = lumaAt(x, y); out[o + 1] = c ? c[1] : 128; out[o + 2] = lumaAt(x + 1, y); out[o + 3] = c ? c[2] : 128; o += 4; } }}