// Zoom at points (lane clip-kit, slice F): the rules, the sharp and the soft limits, the eased expression. import { test } from "node:test"; import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { renderZoomsErrors, resolveZooms, restEnlargement, sharpLimit, validateZooms, zoomEnd, zoomFilters, zoomsErrors } from "./clip-zoom.mjs"; import { clipGeometry, desktopGeometry, pictureRegion } from "./vertical.mjs"; const clip = (zooms, o = {}) => ({ type: "clip", id: "c1", start: 10, end: 40, zooms, ...o }); test("the rules: a list of 1 to 8, inside the window, none overlapping; a box or a scale and focus", () => { assert.deepEqual(zoomsErrors([{ at: 12 }, { at: 20, scale: 1.5, focus: [0.7, 0.3], hold: 2, ramp: 0.3 }, { at: 30, box: [0.5, 0, 0.3, 0.4] }], "z", clip()), []); const no = (z, re) => assert.match(zoomsErrors(z, "z", clip()).join("\n"), re, JSON.stringify(z)); no([], /a list of 1 to 8/); no(Array.from({ length: 9 }, (_, i) => ({ at: 11 + i * 3 })), /a list of 1 to 8/); no([{ at: 39.5 }], /runs outside the clip's window/); // The writer checks the cut; a build the window (a cut tightened since leaves it out, said). const cut = clip(undefined, { cutStart: 15, cutEnd: 30 }); assert.match(zoomsErrors([{ at: 29 }], "z", cut, { cut: true }).join(), /z\[0\] \(29–31\.3 s\) runs outside the clip's cut \(15–30\)/); assert.match(zoomsErrors([{ at: 12 }], "z", cut, { cut: true }).join(), /runs outside the clip's cut/); assert.deepEqual(zoomsErrors([{ at: 29 }], "z", cut), [], "a build: inside its window"); no([{ at: 12, scale: 5 }], /scale is 1\.05–3/); no([{ at: 12, focus: [2, 0] }], /focus is \[x, y\]/); no([{ at: 12, box: [0.9, 0, 0.3, 0.3] }], /box is \[x, y, w, h\]/); no([{ at: 12, box: [0.1, 0, 0.3, 0.3], scale: 2 }], /a box says its own scale/); no([{ at: 12 }, { at: 13 }], /starts before the one at 12 s is out again \(14\.3 s\)/); no([{ at: 12, wobble: 1 }], /wobble is not a key/); assert.equal(zoomEnd({ at: 12 }), 14.3); assert.deepEqual(renderZoomsErrors({ soften: 1.3 }), []); assert.match(renderZoomsErrors({ soften: 2 }).join(), /soften is 1–1\.3/); assert.match(validateZooms({ render: {}, timeline: [{ type: "post", id: "p", zooms: [{ at: 1 }] }] }).join(), /zooms are a clip's/); }); test("sharp: a layout's rest enlargement, the sharp limit -- a framed short has room, a desktop clip and a full short none", () => { const W = 1080, H = 1920; const framed = pictureRegion(clipGeometry("framed", {}, W, H), 1920, 1080); assert.equal(Math.round(framed.rest * 1000) / 1000, 0.563); assert.equal(Math.round(sharpLimit(framed.rest) * 100) / 100, 1.78); const full = pictureRegion(clipGeometry("full", {}, W, H), 1920, 1080); assert.ok(full.rest > 1.7 && sharpLimit(full.rest) === 1, "a full short is already past its pixels"); const desk = pictureRegion(desktopGeometry({}, 1920, 1080), 1920, 1080); assert.equal(desk.rest, 1); // 720 windows: a framed short's room is 1.18. assert.equal(Math.round(sharpLimit(pictureRegion(clipGeometry("framed", {}, W, H), 1280, 720).rest) * 100) / 100, 1.19); assert.equal(restEnlargement({ w: 100, h: 100 }, { w: 200, h: 150 }), 1.5); }); test("resolved: refused past the limit by name; a soft limit lets it through with a note; a box's scale and focus", () => { const desk = pictureRegion(desktopGeometry({}, 1920, 1080), 1920, 1080); const base = { from: 10, srcW: 1920, srcH: 1080, region: desk.region, rest: desk.rest, seconds: 30 }; const strict = resolveZooms(clip([{ at: 12, scale: 1.2 }]), base); assert.equal(strict.zooms.length, 0); assert.match(strict.errors[0], /c1: the zoom at 12 s \(×1\.2\) plays its source at 1\.2× its pixels; the limit here is ×1 .*soft limit/); const soft = resolveZooms(clip([{ at: 12, scale: 1.2 }]), { ...base, soften: 1.3 }); assert.deepEqual(soft.errors, []); assert.match(soft.notes[0], /plays softer than its source: 1\.2× its pixels/); assert.equal(soft.zooms[0].at, 2, "on the segment's clock"); assert.match(resolveZooms(clip([{ at: 12, scale: 1.4 }]), { ...base, soften: 1.3 }).errors[0], /limit here is ×1\.3 .*sharper source \(render\.tallWindows\)$/); // soften is NOT a cap on the total past the source's pixels: the reference is the coarser of those and the layout's // resting picture. A full 9:16 crop of a 1080 window already plays at 1.78×: soften 1.3 lets it go 1.3 times that. const full = pictureRegion(clipGeometry("full", {}, 1080, 1920), 1920, 1080); assert.ok(full.rest > 1.7, `a full crop rests at ${full.rest}`); assert.match(resolveZooms(clip([{ at: 12, scale: 1.2 }]), { ...base, region: full.region, rest: full.rest }).errors[0], /the limit here is ×1 /, "strict: no zoom"); const fsoft = resolveZooms(clip([{ at: 12, scale: 1.3 }]), { ...base, region: full.region, rest: full.rest, soften: 1.3 }); assert.deepEqual(fsoft.errors, []); assert.match(fsoft.notes[0], /plays softer than its source: 2\.3\d?× its pixels/); assert.match(resolveZooms(clip([{ at: 12, scale: 1.35 }]), { ...base, region: full.region, rest: full.rest, soften: 1.3 }).errors[0], /limit here is ×1\.3 /); // A framed short (rests at 0.56): soften 1.3 lets the picture play 1.3× its source's pixels -- scale up to 2.31. const fr0 = pictureRegion(clipGeometry("framed", {}, 1080, 1920), 1920, 1080); assert.deepEqual(resolveZooms(clip([{ at: 12, scale: 2.3 }]), { ...base, region: fr0.region, rest: fr0.rest, soften: 1.3 }).errors, []); assert.equal(resolveZooms(clip([{ at: 12, scale: 2.4 }]), { ...base, region: fr0.region, rest: fr0.rest, soften: 1.3 }).errors.length, 1); // A framed short: 1.5 is sharp, no note. const fr = pictureRegion(clipGeometry("framed", {}, 1080, 1920), 1920, 1080); const sharp = resolveZooms(clip([{ at: 12, scale: 1.5 }]), { ...base, region: fr.region, rest: fr.rest }); assert.deepEqual([sharp.errors, sharp.notes, sharp.zooms[0].focus], [[], [], [0.5, 0.5]]); // A box: its centre the focus, the scale that fills the region with it. const box = resolveZooms(clip([{ at: 12, box: [0.4, 0.4, 0.6, 0.6] }]), { ...base, region: fr.region, rest: fr.rest }); assert.deepEqual([Math.round(box.zooms[0].scale * 1000) / 1000, box.zooms[0].focus.map((n) => Math.round(n * 100) / 100)], [1.667, [0.7, 0.7]]); // A box too small for the source's pixels: refused like any zoom past its limit. assert.match(resolveZooms(clip([{ at: 12, box: [0.5, 0.5, 0.5, 0.5] }]), { ...base, region: fr.region, rest: fr.rest }).errors[0], /×2\) plays its source at 1\.13× its pixels/); // Outside what plays (the cut later than the window): left out, said. const out = resolveZooms(clip([{ at: 12 }]), { ...base, from: 13, region: fr.region, rest: fr.rest }); assert.deepEqual([out.zooms.length, /outside what plays/.test(out.notes[0])], [0, true]); }); test("the filter: frames at the cut's rate, a sub-pixel perspective eased by smoothstep about each push's focus, on only over the pushes; none without zooms", () => { assert.deepEqual(zoomFilters([], { srcW: 1920, srcH: 1080, fps: 30 }), []); const [fps, zp] = zoomFilters([{ at: 2, ramp: 0.4, hold: 1.5, scale: 1.5, focus: [0.75, 0.3] }], { srcW: 1920, srcH: 1080, fps: 30 }); assert.equal(fps, "fps=30"); assert.match(zp, /^perspective=x0='if\(between\(\(\(in-1\)\/30\),2\.0000,4\.3000\),0\.7500,0\.5\)\*W\*\(1-1\/\(1\+if\(between\(\(\(in-1\)\/30\),2\.0000,4\.3000\),0\.5000\*if\(lt\(\(\(in-1\)\/30\),2\.4000\),/); assert.match(zp, /\*\(3-2\*/, "smoothstep"); assert.match(zp, /:interpolation=cubic:sense=source:eval=frame:enable='between\(n\/30,2\.0000,4\.3000\)'$/); assert.doesNotMatch(zp, /zoompan/, "zoompan rounds its crop to whole pixels: a slow ease wobbles"); }); const haveFfmpeg = spawnSync("ffmpeg", ["-version"]).status === 0; test("a slow push (1 s eases, 2 s held: 4 s) moves the picture monotonically -- no back-step in its offset; untouched outside it", { skip: !haveFfmpeg && "needs ffmpeg" }, () => { // A black picture with a white right quarter: the edge's place on screen, to a fraction of a pixel, every frame. const W = 640; const H = 360; const FPS = 30; const z = { at: 0.5, ramp: 1, hold: 2, scale: 1.06, focus: [0.5, 0.5] }; const chain = zoomFilters([z], { srcW: W, srcH: H, fps: FPS }).join(","); const r = spawnSync("ffmpeg", ["-v", "error", "-f", "lavfi", "-i", `color=c=black:s=${W}x${H}:r=${FPS}:d=5,drawbox=x=480:y=0:w=160:h=${H}:c=white:t=fill,format=yuv420p`, "-vf", chain, "-f", "rawvideo", "-pix_fmt", "gray", "-"], { maxBuffer: 64 << 20 }); assert.equal(r.status, 0, String(r.stderr)); const n = r.stdout.length / (W * H); assert.equal(n, 5 * FPS); const edge = []; for (let i = 0; i < n; i += 1) { let sum = 0; for (let y = 150; y < 210; y += 1) for (let x = 0; x < W; x += 1) sum += r.stdout[i * W * H + y * W + x]; edge.push(W - sum / 255 / 60); } const t = (i) => i / FPS; const steps = []; for (let i = 1; i < n; i += 1) { const d = edge[i] - edge[i - 1]; if (t(i) > z.at && t(i) <= z.at + z.ramp && d < -0.02) steps.push(`in at ${t(i).toFixed(2)} s: ${d.toFixed(2)} px`); if (t(i) > z.at + z.ramp + z.hold && t(i) <= zoomEnd(z) && d > 0.02) steps.push(`out at ${t(i).toFixed(2)} s: +${d.toFixed(2)} px`); } assert.deepEqual(steps, [], "a back-step in the ease"); // On time, frame by frame: the edge where the eased zoom puts it at that frame's second -- 160·(Z − 1) px past 480 // (focus in the middle). One frame early or late is off by tenths of a pixel in the ramps. const ease = (u) => u * u * (3 - 2 * u); const want = (s) => { if (s < z.at || s > zoomEnd(z)) return 480; const u = s < z.at + z.ramp ? ease((s - z.at) / z.ramp) : s < z.at + z.ramp + z.hold ? 1 : ease((zoomEnd(z) - s) / z.ramp); return 480 + 160 * (z.scale - 1) * u; }; const off = edge.map((e, i) => Math.abs(e - want(t(i)))); assert.ok(Math.max(...off) < 0.06, `frame ${off.indexOf(Math.max(...off))} is ${Math.max(...off).toFixed(3)} px off its second`); // The first pushed frame (at 0.5 s, the ease at 0) is still the source; the next has moved; the last eased frame // (one before 4.5 s) is a hair out, the one at 4.5 s back home. const first = Math.round(z.at * FPS); const last = Math.round(zoomEnd(z) * FPS); assert.ok(Math.abs(edge[first] - 480) < 0.01, `the first pushed frame ${edge[first]}`); assert.ok(edge[first + 1] - 480 > 0.015, `the second pushed frame ${edge[first + 1]}`); assert.ok(edge[last - 1] - 480 > 0.015, `the last eased frame ${edge[last - 1]}`); assert.ok(Math.abs(edge[last] - 480) < 0.01, `the push over ${edge[last]}`); // Pushed in about the centre: the edge at 480 lands at (480 − 0.5·640·(1 − 1/1.06))·1.06 = 489.6 while held. const held = edge[Math.round((z.at + z.ramp + 1) * FPS)]; assert.ok(Math.abs(held - 489.6) < 0.3, `held at ${held}`); // Outside the push the frame is the source's, untouched. assert.equal(edge[3], 480); assert.equal(edge[n - 3], 480); });