Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit f70fb8a92dddb9e230da61c5a9b54d1023de2332
parent 9f09ed1a5d6f7a32f7f9fb1272035f36bc60bc90
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Sat, 14 Nov 2026 06:16:39 -0500

compact-saved-videos: a good copy of fast-switching content passes, and the page says what verification cannot promise

A stream switching between a screen share (0.995) and a webcam (0.96) every
20 s fills each 60 s half-window half and half, so its floor flipped with the
majority -- and, split evenly, its median sat between the two and its floor
fell to about 0.77. A good copy failed with "below its neighbourhood's floor".

When the file keeps coming back to a lower-scoring kind of picture (an Otsu
split of the scores, each side at least a tenth of the frames, the lower
side's median below the upper side's own floor), a half-window holding a
fifth or more of its frames at that level takes its floor from those frames
alone: the frames in the same state. A second's dip inside a steady stretch
is under a fifth and keeps the stretch's floor. A plain clamp to a file-wide
lower floor was not enough: it would have let a 1 s dip to 0.95 inside the
share pass in any file that has a webcam stretch somewhere.

Also: a frame in the file's first or last half-window lacked a block on one
side and was judged by the other alone, so a webcam closing the last minute
was held to the share's floor (alternation every 60 s failed). That side is
now the first or last block.

The reviewer's cases: alternation every 20, 60 and 90 s pass; the 20/30/45 %
lower stretches pass; 1 s at 0.95 inside the share fails; 1 s at 0.92 inside
the webcam fails, alternating or not; the defect table fails as before. The
pointer records the lower state's floor and share.

OPERATING.md now says, beside the drop setting, what a passing verification
does not guarantee: a long evenly softer stretch of about 90 s or more; a
change to under about 0.5 % of the frame for a few seconds; a slight defect
within about 40 s of a change into a lower-scoring picture; and, in a file
that keeps returning to one, a stretch of about 35 s or more, or a dip while
the two kinds switch quickly, that stays above that kind's floor.

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
MOPERATING.md | 18++++++++++++++++--
Mcommon/controller/compactSavedVideos.ts | 1+
Mcommon/lib/compactMedia-server.ts | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mcommon/lib/savedVideo.ts | 7+++++++
Mcommon/lib/savedVideoCompaction.test.ts | 44++++++++++++++++++++++++++++++++++++++++++--
Meditor/CHANGELOG.md | 6++++--
6 files changed, 133 insertions(+), 14 deletions(-)

diff --git a/OPERATING.md b/OPERATING.md @@ -1201,7 +1201,8 @@ pnpm ops compaction-markers --json '{"clear":{"slug":"example","id":"<videoId>"} the copy. Each frame is scored whole: - no frame below `wholeFileSsimMin` (0.9); - a mean of at least `wholeFileSsimMean` (0.97); - - no frame below the file's own mean − max(`wholeFileSigmaK` (8) × its spread, `wholeFileSigmaMargin` (0.03)). + - no frame below the relative floor of the frames around it: their median − max(`wholeFileSigmaK` (8) × their + robust spread, `wholeFileSigmaMargin` (0.03)), described below. Each frame is also split into an 8×8 grid of tiles, each scored alone: a tile below `wholeFileTileMin` (0.7) for more than `wholeFileTileRunFrames` (2) frames in a row fails, so a small region changed for 3 frames at 30 @@ -1211,7 +1212,10 @@ pnpm ops compaction-markers --json '{"clear":{"slug":"example","id":"<videoId>"} cannot see a hue shift or chroma-only noise. The relative floor is LOCAL: each frame is judged against the `wholeFileWindowSeconds` (120) around it, the lower of the floors of the half-window before and the half-window after (median − max(8 × 1.4826 × the median absolute deviation, 0.03)). So a stream that alternates a webcam - and a screen share is judged stretch by stretch, and a few bad frames cannot widen their own floor. A degraded + and a screen share is judged stretch by stretch, and a few bad frames cannot widen their own floor. When the + file keeps coming back to a lower-scoring kind of picture (at least a tenth of its frames, scoring below the + other kind's floor; the pointer's `lowerState`), a half-window that is a fifth or more that kind takes its floor + from those frames alone. So a stream switching between a share and a webcam every 20 s passes. A degraded stretch longer than the window looks steady to that floor. It is caught instead by the absolute per-frame floor, the tile grid, the mean floor, and the VMAF excerpts, laid at least one every window across the file. At 640×360 the comparison costs about 5–6× a plain decode of both files; its per-frame cost is fixed (about 4 ms at 256 px), so @@ -1227,6 +1231,16 @@ pnpm ops compaction-markers --json '{"clear":{"slug":"example","id":"<videoId>"} - **The swap.** The re-encode is fsynced, then swapped in one locked pointer write. The write happens only while the pointer still names the encoded file, with the same bytes, dir, storedAt and sha256. After the swap nothing is undone: a failure after it is reported as one, with the original kept. +- **What verification does not guarantee.** It catches every planted defect we have tried, but a copy can pass and + still be slightly worse than its source in these places: + - a long, evenly softer stretch of about 90 s or more (it becomes its own neighbourhood), unless it falls below the + absolute or mean floor, the tile floor or a VMAF excerpt; + - a change to less than about 0.5% of the frame for a few seconds; + - a slight defect within about 40 s of a scene change into a lower-scoring kind of picture; + - in a file that keeps returning to a lower-scoring kind of picture, a stretch of about 35 s or more, or any dip + while the two kinds switch quickly, that stays above that kind's floor. + + A verified copy is a close copy, not an identical one. That is why `dropOriginal` stays `"keep"`. - **The original.** `dropOriginal` is `"keep"` by default: every original stays until a person drops it. Under `"after-verify"` a separate step, after the swap, reads the re-encode back from disk (fsynced, dropped from the page cache, size, sha256, container, length). It then drops the original only when all of these hold: diff --git a/common/controller/compactSavedVideos.ts b/common/controller/compactSavedVideos.ts @@ -1088,6 +1088,7 @@ async function verify(o: { windowSeconds: s.wholeFileWindowSeconds, worstLocalMargin: round(v.worstLocalMargin, 4), worstLocalAt: round(v.worstLocalAt / outFps, 3), + ...(v.lowerState ? { lowerState: { floor: round(v.lowerState.floor, 4), share: round(v.lowerState.share, 3) } } : {}), relativeFloor: round(v.relativeFloor, 4), chroma: { mean: round(v.chroma.mean, 4), diff --git a/common/lib/compactMedia-server.ts b/common/lib/compactMedia-server.ts @@ -889,6 +889,8 @@ export type WholeFileVerdict = { worstLocalMargin: number; worstLocalAt: number; windowFrames: number; + // The file's recurring lower kind of picture, when it has one (review F-a). + lowerState: LowerState | null; worstTile: TileSummary | null; chroma: { mean: number; min: number; minAt: number; plane: "U" | "V" }; }; @@ -914,18 +916,33 @@ function floorOf(f: Float32Array, k: number, margin: number): { median: number; // after the change fit their right-hand side — while a short defect inside a // steady stretch has the steady stretch on both sides and falls below. A file // shorter than the window is one neighbourhood: the whole file. -export function localFloors(f: Float32Array, windowFrames: number, k: number, margin: number): Float32Array { +// +// FAST SWITCHING (review F-a): a half-window that is half screen share and half +// webcam has a floor that flips with whichever is the majority (and, split +// evenly, a median between the two and a floor far too low). So when the file +// has a RECURRING LOWER KIND of picture (`lowerState`), a neighbourhood holding +// at least LOWER_STATE_BLOCK_SHARE of its frames at that kind's level takes its +// floor from THOSE frames alone: the frames in the same state. A second's dip +// inside a steady stretch is under that share and keeps the stretch's floor. +export function localFloors(f: Float32Array, windowFrames: number, k: number, margin: number, lower?: LowerState | null): Float32Array { const out = new Float32Array(f.length); const half = Math.max(8, Math.floor(windowFrames / 2)); + const lowCount = new Uint32Array(f.length + 1); + for (let i = 0; i < f.length; i += 1) lowCount[i + 1] = lowCount[i] + (lower && f[i] < lower.threshold ? 1 : 0); + const blockFloor = (s: number, e: number) => { + const block = f.subarray(s, e); + if (!lower || lowCount[e] - lowCount[s] < LOWER_STATE_BLOCK_SHARE * (e - s)) return floorOf(block, k, margin).floor; + return floorOf(block.filter((x) => x < lower.threshold), k, margin).floor; + }; if (f.length <= windowFrames) { - out.fill(floorOf(f, k, margin).floor); + out.fill(blockFloor(0, f.length)); return out; } const stride = Math.max(1, Math.floor(windowFrames / 8)); const starts: number[] = []; for (let s = 0; s + half <= f.length; s += stride) starts.push(s); if (starts[starts.length - 1] !== f.length - half) starts.push(f.length - half); - const floors = starts.map((s) => floorOf(f.subarray(s, s + half), k, margin).floor); + const floors = starts.map((s) => blockFloor(s, s + half)); for (let i = 0; i < f.length; i += 1) { // Left: the last block ending at or before i; right: the first starting after i. let lo = 0; @@ -948,13 +965,50 @@ export function localFloors(f: Float32Array, windowFrames: number, k: number, ma hi = mid - 1; } else lo = mid + 1; } - const a = left >= 0 ? floors[left] : Infinity; - const b = right >= 0 ? floors[right] : Infinity; - out[i] = Math.min(a, b) === Infinity ? floors[0] : Math.min(a, b); + // At the file's ends one side is missing: the first frames have no block + // ending before them, the last none starting after them. That side is then + // the first (or last) block, which holds the frame — not nothing, or a + // webcam closing the file's last minute is judged by the share before it. + const a = floors[left >= 0 ? left : 0]; + const b = floors[right >= 0 ? right : starts.length - 1]; + out[i] = Math.min(a, b); } return out; } +// A RECURRING LOWER KIND OF PICTURE: the file's frames split in two where the +// two groups are most distinct (Otsu's split on the scores), each group at +// least LOWER_STATE_FILE_SHARE of the file, and the lower group's median below +// the upper group's own floor (else the upper floor already admits it). Its +// floor is the lower group's median − max(k · its robust spread, margin). +// None for a file of one kind of picture, or one whose lower scores are a +// defect shorter than a tenth of it. +export type LowerState = { threshold: number; floor: number; share: number }; +export const LOWER_STATE_FILE_SHARE = 0.1; +export const LOWER_STATE_BLOCK_SHARE = 0.2; +export function lowerState(f: Float32Array, k: number, margin: number): LowerState | null { + const n = f.length; + const lo = Math.ceil(LOWER_STATE_FILE_SHARE * n); + if (n < 32 || lo >= n - lo) return null; + const s = Float64Array.from(f).sort(); + const sum = new Float64Array(n + 1); + for (let i = 0; i < n; i += 1) sum[i + 1] = sum[i] + s[i]; + let best = -1; + let at = lo; + for (let j = lo; j <= n - lo; j += 1) { + const d = sum[j] / j - (sum[n] - sum[j]) / (n - j); + const between = j * (n - j) * d * d; + if (between > best) { + best = between; + at = j; + } + } + const low = Float32Array.from(s.subarray(0, at)); + const upper = floorOf(Float32Array.from(s.subarray(at)), k, margin); + if (medianOf(low) >= upper.floor) return null; + return { threshold: (s[at - 1] + s[at]) / 2, floor: floorOf(low, k, margin).floor, share: at / n }; +} + export function robustSpread(f: Float32Array): { median: number; sd: number } { const median = medianOf(f); const dev = new Float64Array(f.length); @@ -993,7 +1047,8 @@ export function judgeWholeFile( const { median, sd } = robustSpread(f); const relativeFloor = median - Math.max(t.sigmaK * sd, t.sigmaMinMargin); const windowFrames = Math.max(16, Math.round(t.windowFrames ?? f.length)); - const local = localFloors(f, windowFrames, t.sigmaK, t.sigmaMinMargin); + const lower = lowerState(f, t.sigmaK, t.sigmaMinMargin); + const local = localFloors(f, windowFrames, t.sigmaK, t.sigmaMinMargin, lower); let worstLocalMargin = Infinity; let worstLocalAt = 0; for (let i = 0; i < f.length; i += 1) { @@ -1026,7 +1081,7 @@ export function judgeWholeFile( } const chroma = { mean: u.length ? cSum / u.length : 0, min: cMin, minAt: cMinAt, plane: cPlane }; const at = (i: number) => `${(i / t.fps).toFixed(2)} s`; - const base = { mean, median, sd, min, minAt, relativeFloor, worstLocalMargin, worstLocalAt, windowFrames, worstTile, chroma }; + const base = { mean, median, sd, min, minAt, relativeFloor, worstLocalMargin, worstLocalAt, windowFrames, lowerState: lower, worstTile, chroma }; if (f.length !== t.expectedFrames || u.length !== t.expectedFrames || v.length !== t.expectedFrames) { return { ...base, ok: false, why: `the comparison saw ${f.length} of the copy's ${t.expectedFrames} frames (colour: ${u.length}): it ends early or skips` }; } diff --git a/common/lib/savedVideo.ts b/common/lib/savedVideo.ts @@ -172,6 +172,10 @@ export type SavedVideoCompactedChecks = { windowSeconds?: number; worstLocalMargin?: number; worstLocalAt?: number; + // The file's recurring lower kind of picture, when it has one (review + // F-a): its floor and its share of the frames. A neighbourhood holding at + // least a fifth of its frames at that level took its floor from them alone. + lowerState?: { floor: number; share: number }; // THE COLOUR (review D1): SSIM on the U and V planes, every frame. chroma?: { mean: number; min: number; worstAt: number; plane: string; minFloor: number; meanFloor: number }; }; @@ -502,6 +506,9 @@ function parseCompacted(raw: unknown): SavedVideoCompacted | null { ...(typeof w.windowSeconds === "number" ? { windowSeconds: w.windowSeconds } : {}), ...(typeof w.worstLocalMargin === "number" ? { worstLocalMargin: w.worstLocalMargin } : {}), ...(typeof w.worstLocalAt === "number" ? { worstLocalAt: w.worstLocalAt } : {}), + ...(w.lowerState && typeof w.lowerState === "object" && typeof obj(w.lowerState).floor === "number" && typeof obj(w.lowerState).share === "number" + ? { lowerState: { floor: obj(w.lowerState).floor as number, share: obj(w.lowerState).share as number } } + : {}), ...(w.chroma && typeof w.chroma === "object" ? (() => { const c = obj(w.chroma); diff --git a/common/lib/savedVideoCompaction.test.ts b/common/lib/savedVideoCompaction.test.ts @@ -226,7 +226,47 @@ test("E1: a good copy with two kinds of picture passes; a short defect in either } // Five-minute stretches alternating all hour long. assert.equal(judgeWholeFile({ frames: make(0, true), tiles: [], chromaU: ones, chromaV: ones }, t).ok, true); - // The whole-file floor of the round before rejected the 20% case. + // The whole-file floor of the round before rejected the 20% case; judged as + // one neighbourhood it now passes too, on the webcam's own floor (F-a). const whole = judgeWholeFile({ frames: make(0.2), tiles: [], chromaU: ones, chromaV: ones }, { ...t, windowFrames: n }); - assert.equal(whole.ok, false); + assert.equal(whole.ok, true, whole.why); + assert.ok(whole.lowerState && Math.abs(whole.lowerState.share - 0.2) < 0.01, JSON.stringify(whole.lowerState)); +}); + +// F-a: fast switching. A share (0.995) and a webcam (0.96) every 20 s fill each +// half-window half and half, and its floor flipped with the majority. The file's +// recurring lower kind of picture lends its floor to a neighbourhood holding a +// fifth of its frames at that level; a second's dip in a steady stretch does not. +test("F-a: a good copy switching every 20, 60 or 90 s passes; a short dip still fails", async () => { + const { judgeWholeFile } = await import("./compactMedia-server"); + const fps = 30; + const n = 3600 * fps; + const jitter = (i: number) => (((i * 7919) % 11) - 5) * 0.0004; + const t = { minFrame: 0.9, meanFloor: 0.97, sigmaK: 8, sigmaMinMargin: 0.03, tileRunFrames: 2, expectedFrames: n, fps, chromaMinFrame: 0.9, chromaMeanFloor: 0.97, windowFrames: 120 * fps }; + const ones = new Float32Array(n).fill(1); + const judge = (f: Float32Array) => judgeWholeFile({ frames: f, tiles: [], chromaU: ones, chromaV: ones }, t); + const alternate = (period: number) => Float32Array.from({ length: n }, (_, i) => (Math.floor(i / fps / period) % 2 ? 0.96 : 0.995) + jitter(i)); + const dip = (f: Float32Array, at: number, score: number) => (f.fill(score, at * fps, (at + 1) * fps), f); + // 60 s: a webcam closing the file's last minute (it has no block after it). + for (const period of [20, 60, 90]) { + const v = judge(alternate(period)); + assert.equal(v.ok, true, `${period} s: ${v.why}`); + assert.ok(v.lowerState && Math.abs(v.lowerState.share - 0.5) < 0.02, JSON.stringify(v.lowerState)); + // A second at 0.92 inside a webcam stretch is below the webcam's own floor. + const w = judge(dip(alternate(period), period + 2, 0.92)); + assert.equal(w.ok, false, `${period} s dip`); + assert.match(w.why!, /below its neighbourhood's floor/); + } + // A screen share with one 12-minute webcam stretch: a second at 0.95 inside the + // share keeps the share's floor (the neighbourhood is not a fifth webcam). + const once = Float32Array.from({ length: n }, (_, i) => (i >= 1000 * fps && i < 1720 * fps ? 0.96 : 0.995) + jitter(i)); + assert.equal(judge(once).ok, true); + const d = judge(dip(once, 300, 0.95)); + assert.equal(d.ok, false); + assert.match(d.why!, /below its neighbourhood's floor 0\.96/); + // A file of one kind of picture has no lower state: a 20 s stretch at 0.95 fails. + const plain = Float32Array.from({ length: n }, (_, i) => 0.995 + jitter(i)); + assert.equal(judge(plain).lowerState, null); + plain.fill(0.95, 1000 * fps, 1020 * fps); + assert.equal(judge(plain).ok, false); }); diff --git a/editor/CHANGELOG.md b/editor/CHANGELOG.md @@ -35,8 +35,10 @@ - The whole-file comparison scores the colour (U and V planes) every frame too: a hue shift or chroma-only noise fails it, and "after-verify" needs both the luma and the colour record. Its relative floor uses the median and a robust spread over each frame's own two-minute neighbourhood (`wholeFileWindowSeconds`), so a stream alternating - two kinds of picture passes; a VMAF excerpt is laid at least every window. A negative plane score is read as - one. Clearing a marker never removes one written after the check. + two kinds of picture passes; a VMAF excerpt is laid at least every window. A file that keeps coming back to a + lower-scoring kind of picture (`lowerState` in the pointer) judges a neighbourhood that is a fifth or more that + kind by those frames alone, so switching every 20 s passes too; a frame closing the file is judged by the last + window. A negative plane score is read as one. Clearing a marker never removes one written after the check. - **ROLLBACK: do not roll the editor back past this release while `pnpm ops get kept-originals` lists anything.** An older editor does not know `"keptAs": "original"` / `"former-original"`, would treat them as ordinary alternates, and could prune or drop the only original.