// Where the person's head is, read off their own cut-out, so an outline's rings and its words never part. import { exec } from "./clip.js"; import { type JsonObject, isObject, objectOf, round } from "./json.js"; import { Refused } from "./refused.js"; const STEP = 0.1; const COLUMNS = 90; const SOLID = 128; /** A row with fewer person pixels than this is matte noise, not the top of the head. */ const ROW_MIN = 2; const ROW_SHARE = 0.03; /** How far below the top, in frame heights, the head's width is read: the hair and forehead, above the ears. */ const BAND = 0.08; /** Share of the steps the head must be found in for a track. */ const ENOUGH = 0.6; /** One frame's crown: [centre x, top, half width] as frame fractions, or null where no one is in it. */ export function crownIn(alpha: Uint8Array, columns: number, rows: number): number[] | null { const solid = (x: number, y: number) => (alpha[y * columns + x] ?? 0) >= SOLID; const least = Math.max(ROW_MIN, Math.ceil(columns * ROW_SHARE)); let top = -1; for (let y = 0; y < rows && top < 0; y++) { let n = 0; for (let x = 0; x < columns; x++) if (solid(x, y)) n++; if (n >= least) top = y; } if (top < 0) return null; let left = columns; let right = -1; let sum = 0; let count = 0; for (let y = top; y < Math.min(rows, top + Math.max(1, Math.round(BAND * rows))); y++) for (let x = 0; x < columns; x++) if (solid(x, y)) { left = Math.min(left, x); right = Math.max(right, x); sum += x; count++; } return [ round((sum / count + 0.5) / columns, 4), round(top / rows, 4), round((right - left + 1) / columns / 2, 4), ]; } /** Every STEP s of the matte: [t, centre x, top, half width]; null when the head is found too seldom. */ async function crownTrack( matte: string, seconds: number, aspect: number, ): Promise { const rows = Math.max(1, Math.round(COLUMNS * aspect)); const { stdout } = await exec("ffmpeg", [ ...["-v", "error", "-c:v", "libvpx-vp9", "-i", matte], ...[ "-t", String(seconds), "-vf", `fps=${1 / STEP},format=yuva420p,extractplanes=a,scale=${COLUMNS}:${rows}`, ], ...["-f", "rawvideo", "-"], ]); const size = COLUMNS * rows; const out: number[][] = []; for (let k = 0; (k + 1) * size <= stdout.length; k++) { const found = crownIn(stdout.subarray(k * size, (k + 1) * size), COLUMNS, rows); if (found) out.push([round(k * STEP, 3), ...found]); } const steps = Math.max(1, Math.round(seconds / STEP)); return out.length >= Math.max(2, ENOUGH * steps) ? out : null; } /** The values the effect says follow its matte, by name and shape. */ function matteFollowed(meta: JsonObject): [string, string][] { return Object.entries(objectOf(meta.follows)).flatMap(([name, rule]) => isObject(rule) && rule.of === "matte" ? [[name, String(rule.as ?? "")] as [string, string]] : [], ); } /** Matte-followed values measured from `matte`; a stand-in matte waits, and a real one with no head refuses the effect. */ export async function matteFilled( meta: JsonObject, params: JsonObject, matte: string, real: boolean, seconds: number, aspect: number, ): Promise<[JsonObject, string[]]> { const named = matteFollowed(meta); if (!named.length) return [params, []]; if (!real) return [ params, [`${named.map(([n]) => n).join(", ")}: set by code from the person's cut-out once it comes`], ]; const out = { ...params }; const notes: string[] = []; for (const [name, shape] of named) { let failed = ""; const made = shape === "crown" ? await crownTrack(matte, seconds, aspect).catch((err: unknown) => { failed = err instanceof Error ? err.message : String(err); return null; }) : null; if (made) { out[name] = made; notes.push( `${name} follows the top of the person's head in their cut-out (${made.length} moments); code sets it`, ); } else throw new Refused( failed ? `${name} could not be read: the cut-out could not be decoded (${failed.slice(0, 200)})` : `${name} could not be read: the cut-out shows no head often enough in these seconds`, ); } return [out, notes]; }