Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/cli/src/measure/cuts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,43 @@ describe("findCuts on moving pictures", () => {
expect(classifiedCuts(frames).map((c) => c.kind)).toEqual(["fast"]);
});

/** A dark window `w` wide on grey, centred, with a lighter title bar. */
const window = (w: number) => {
const f = new Uint8Array(SIG_W * SIG_H).fill(235);
const h = w * 0.8;
for (let y = 0; y < SIG_H; y++)
for (let x = 0; x < SIG_W; x++) {
const top = (SIG_H - 1) / 2 - h / 2;
if (Math.abs(x - (SIG_W - 1) / 2) <= w / 2 && Math.abs(y - (SIG_H - 1) / 2) <= h / 2)
f[y * SIG_W + x] = y - top < 2 ? 60 : 25;
}
return f;
};

it("calls a zoom that speeds up past a cut's level a fast move", () => {
// stripe 0.2 s: the app window grows 3, 9, 12, 5 grey levels a frame.
const widths = [20, 20, 20, 20, 20.5, 22, 25, 30, 32, 33.5, 34.5, 35, 35.5];
const frames = [...widths, ...Array<number>(13).fill(36)].map(window);
expect(classifiedCuts(frames).map((c) => [c.frame, c.kind])).toEqual([[7, "fast"]]);
expect(findCuts(frames)).toEqual([]);
});

it("keeps a punch-in out of a still picture as a cut", () => {
const frames = [...Array<number>(13).fill(20), ...Array<number>(13).fill(28)].map(window);
expect(findCuts(frames).map((c) => c.frame)).toEqual([13]);
});

// hf-review: a picture still on one side and moving on the other is an edit, not a camera move.
it("keeps a punch-in out of a still picture as a cut when the next shot moves", () => {
const frames = [...Array<number>(13).fill(20), 25, ...Array<number>(16).fill(27)].map(window);
expect(findCuts(frames).map((c) => c.frame)).toEqual([13]);
});

it("keeps a punch-out into a still picture as a cut after a moving shot", () => {
const frames = [...Array<number>(16).fill(27), 25, ...Array<number>(13).fill(20)].map(window);
expect(findCuts(frames).map((c) => c.frame)).toEqual([17]);
});

it("calls a light flash a flash and the cut after it a cut", () => {
const frames = [...still(100, 20), ...still(250, 2), ...still(100, 20), ...still(30, 20)];
expect(classifiedCuts(frames).map((c) => [c.frame, c.kind])).toEqual([
Expand Down
39 changes: 38 additions & 1 deletion packages/cli/src/measure/cuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const MAX_CUT_FRAMES = 3;
const FLASH_FRAMES = 4;
const RETURNED = 0.4;
const SAME_TONES = 0.8;
/** A neighbour that changes this share of the peak: the picture was already moving. */
const IN_MOTION = 0.3;

export interface Cut {
frame: number;
Expand Down Expand Up @@ -95,6 +97,40 @@ const sharedTones = (a: Uint8Array, b: Uint8Array) => {
return tones(a).reduce((sum, share, bin) => sum + Math.min(share, after[bin]!), 0);
};

/** `f` scaled by `s` about the centre and shifted by dx,dy, edges held. */
function moved(f: Uint8Array, s: number, dx: number, dy: number): Uint8Array {
const out = new Uint8Array(f.length);
const cx = (SIG_W - 1) / 2;
const cy = (SIG_H - 1) / 2;
for (let y = 0; y < SIG_H; y++) {
const sy = Math.min(SIG_H - 1, Math.max(0, Math.round((y - cy - dy) / s + cy)));
for (let x = 0; x < SIG_W; x++) {
const sx = Math.min(SIG_W - 1, Math.max(0, Math.round((x - cx - dx) / s + cx)));
out[y * SIG_W + x] = f[sy * SIG_W + sx]!;
}
}
return out;
}

/** How far `after` stays from `before` zoomed (0.8–1.25x) and shifted, at the closest of a small search. */
function movedChange(before: Uint8Array, after: Uint8Array): number {
let least = meanAbsDiff(before, after);
for (let s = 0.8; s <= 1.25; s += 0.025)
for (let dx = -6; dx <= 6; dx++)
for (let dy = -4; dy <= 4; dy++)
least = Math.min(least, meanAbsDiff(moved(before, s, dx, dy), after));
return least;
}

/**
* A camera move peaking past a cut's level: the frames on both sides move too, and the frame before, zoomed or
* shifted, nearly gives the one after. A punch-in or punch-out also zooms, but out of or into a still picture.
*/
function isMove(frames: Uint8Array[], diffs: number[], c: Cut, end: number): boolean {
const inMotion = Math.min(diffs[c.frame - 1] ?? 0, diffs[end] ?? 0) >= IN_MOTION * c.change;
return inMotion && movedChange(frames[c.frame - 1]!, frames[end - 1]!) < RETURNED * c.change;
}

/** Every change that reads as a cut, each marked a flash or a fast move when it is not a new shot. */
export function classifiedCuts(frames: Uint8Array[]): Cut[] {
const diffs = frameChanges(frames);
Expand All @@ -113,7 +149,8 @@ export function classifiedCuts(frames: Uint8Array[]): Cut[] {
const returns = Array.from({ length: FLASH_FRAMES }, (_, k) => k + 1).some(
(k) => back(c.frame - 1, end - 1 + k) || back(c.frame - k, end),
);
return returns ? { ...c, kind: "flash" as const } : c;
if (returns) return { ...c, kind: "flash" as const };
return isMove(frames, diffs, c, end) ? { ...c, kind: "fast" as const } : c;
});
}

Expand Down
Loading