import type { CraftFinding, CraftFrame } from "./checkTypes.js"; import { filmFinding } from "./craftFinding.js"; /** Seeks to `time` and says whether its rendered frame shows only the bare stage. */ export type StageProbe = (time: number) => Promise; const BLANK_SPAN = { minSec: 0.5, /** Craft grid times are rounded to the millisecond. */ toleranceSec: 1e-3, /** One 8 fps step outside each edge, where a fade would still be in progress. */ edgeProbeSec: 0.125, /** Content this opaque just outside a stretch was cut away, not faded to the stage. */ cutPresence: 0.95, } as const; interface Stretch { start: number; end: number; times: number[]; } /** Stretches of 0.5 s or more between two hard cuts where every sample renders only the bare stage. */ export async function detectBlankSpans( frames: CraftFrame[], frameAt: (time: number) => Promise, stageIsEmpty: StageProbe, ): Promise { const findings: CraftFinding[] = []; for (const stretch of interiorUnpaintedStretches(frames)) { if (await isBlankBetweenCuts(stretch, frameAt, stageIsEmpty)) { findings.push(blankSpanFinding(stretch)); } } return findings; } // A run that reaches the first or last grid sample has content on at most one side, so it is never a stretch. function interiorUnpaintedStretches(frames: CraftFrame[]): Stretch[] { const stretches: Stretch[] = []; let run: CraftFrame[] = []; let reachesStart = true; for (const frame of frames) { if (!frame.painted) { run.push(frame); continue; } const first = run[0]; const last = run.at(-1); if (first && last && !reachesStart) { stretches.push({ start: first.time, end: last.time, times: run.map((sample) => sample.time), }); } run = []; reachesStart = false; } return stretches.filter( (stretch) => stretch.end - stretch.start + BLANK_SPAN.toleranceSec >= BLANK_SPAN.minSec, ); } async function isBlankBetweenCuts( stretch: Stretch, frameAt: (time: number) => Promise, stageIsEmpty: StageProbe, ): Promise { const before = await frameAt(stretch.start - BLANK_SPAN.edgeProbeSec); if (before.presence < BLANK_SPAN.cutPresence) return false; const after = await frameAt(stretch.end + BLANK_SPAN.edgeProbeSec); if (after.presence < BLANK_SPAN.cutPresence) return false; const verdicts: boolean[] = []; for (const time of stretch.times) verdicts.push(await stageIsEmpty(time)); return verdicts.every(Boolean); } function blankSpanFinding(stretch: Stretch): CraftFinding { const { start, end } = stretch; return filmFinding( "blank_span", start, `Nothing is visible from ${start.toFixed(2)} s to ${end.toFixed(2)} s, between two hard cuts: check the data-start and data-duration of what should show here and its entrance and exit tweens, or mark a deliberate blank with data-layout-allow-blank.`, { timeRange: [start, end], seconds: Math.round((end - start) * 1000) / 1000, floorSeconds: BLANK_SPAN.minSec, }, ); }