Skip to content

Write HTML. Render video. Built for agents.

fix(engine): skip motion-blur dedup when the shutter window moves (#4025)

miga-heygen9117b3647104
diff --git a/packages/engine/src/services/frameCapture-motionBlur.test.ts b/packages/engine/src/services/frameCapture-motionBlur.test.ts
index e010429b9a..e04c45d789 100644
--- a/packages/engine/src/services/frameCapture-motionBlur.test.ts
+++ b/packages/engine/src/services/frameCapture-motionBlur.test.ts
@@ -172,10 +172,15 @@ describe("sub-frame accumulation reaches the page with distinct sample times", (
});
describe("accumulation composes with static-frame dedup", () => {
+ // 720 degrees is the widest shutter After Effects offers, and the AD5 reference export
+ // uses it, so it is the case a reader is most likely to check the frame ranges against.
+ const wideShutter = resolveMotionBlurPlan({ shutterAngle: 720, shutterPhase: -360 }) ?? undefined;
+
it("pays nothing on a static frame instead of capturing K samples of the same instant", async () => {
const anchor = solidPng(42);
const session = makeSession({
- staticFrames: new Set([11]),
+ // The whole window either side of frame 11 is static, which is what reuse claims.
+ staticFrames: new Set([9, 10, 11, 12]),
lastFrameBuffer: anchor,
lastFrameAbsoluteIndex: 10,
});
@@ -199,6 +204,40 @@ describe("accumulation composes with static-frame dedup", () => {
expect(vi.mocked(pageScreenshotCapture)).toHaveBeenCalledTimes(16);
});
+
+ // The defect this locks: dedup asked only whether THIS frame matched its predecessor.
+ // The shutter window reads either side of the frame instant, so a still frame sitting
+ // next to a moving one reused a buffer that carries none of that motion. Frame 11 is
+ // static and 12 is not, and at a 720 degree shutter frame 11's window runs to 11.94, so
+ // it reaches motion the reused buffer cannot contain. Guard from #4013 by Dante-dan.
+ it("captures rather than reuses when the shutter window reaches a moving frame", async () => {
+ const session = makeSession({
+ motionBlur: wideShutter,
+ staticFrames: new Set([9, 10, 11]),
+ lastFrameBuffer: solidPng(42),
+ lastFrameAbsoluteIndex: 10,
+ });
+
+ await captureFrameToBuffer(session, 11, 11 / 30);
+
+ expect(vi.mocked(pageScreenshotCapture)).toHaveBeenCalledTimes(16);
+ expect(session.staticDedupCount).toBeUndefined();
+ });
+
+ it("still reuses at 720 degrees when the whole window is static", async () => {
+ const anchor = solidPng(42);
+ const session = makeSession({
+ motionBlur: wideShutter,
+ staticFrames: new Set([9, 10, 11, 12]),
+ lastFrameBuffer: anchor,
+ lastFrameAbsoluteIndex: 10,
+ });
+
+ const result = await captureFrameToBuffer(session, 11, 11 / 30);
+
+ expect(result.buffer).toBe(anchor);
+ expect(vi.mocked(pageScreenshotCapture)).not.toHaveBeenCalled();
+ });
});
describe("every path that marks a session ready also resolves the plan", () => {
diff --git a/packages/engine/src/services/frameCapture.ts b/packages/engine/src/services/frameCapture.ts
index d2ac5cacda..273cf20c2b 100644
--- a/packages/engine/src/services/frameCapture.ts
+++ b/packages/engine/src/services/frameCapture.ts
@@ -24,6 +24,7 @@ import { encodePng } from "../utils/alphaBlit.js";
import {
MotionBlurAccumulator,
motionBlurSampleTimes,
+ motionBlurWindowIsStatic,
resolveMotionBlurPlan,
type MotionBlurPlan,
} from "./motionBlur.js";
@@ -3832,7 +3833,9 @@ async function captureFrameCore(
if (
session.staticFrames?.has(absFrameIndex) &&
session.lastFrameBuffer &&
- session.lastFrameAbsoluteIndex === absFrameIndex - 1
+ session.lastFrameAbsoluteIndex === absFrameIndex - 1 &&
+ (!session.motionBlur ||
+ motionBlurWindowIsStatic(session.motionBlur, absFrameIndex, session.staticFrames))
) {
session.staticDedupCount = (session.staticDedupCount ?? 0) + 1;
session.lastFrameAbsoluteIndex = absFrameIndex;
diff --git a/packages/engine/src/services/motionBlur.ts b/packages/engine/src/services/motionBlur.ts
index d38dbe8f91..8de0b58ef3 100644
--- a/packages/engine/src/services/motionBlur.ts
+++ b/packages/engine/src/services/motionBlur.ts
@@ -108,6 +108,38 @@ export function motionBlurSampleTimes(
return plan.sampleTickOffsets.map((offset) => Math.max(0, frameTicks + offset) / grid);
}
+/**
+ * Is every frame the shutter window reads from known static, so this frame can reuse its
+ * predecessor's buffer instead of paying K captures?
+ *
+ * The static-frame dedup asks whether a frame is byte-identical to the one before it. With
+ * blur on that is not enough: the window reads content from either side of the frame
+ * instant, so a still frame next to a moving one still has to be captured. At a shutter
+ * angle above 360 the window spans whole neighbouring frames and the reuse would drop a
+ * frame of motion outright.
+ *
+ * The range covers the previous frame's window too, because reuse claims this frame's
+ * blurred output equals that one's. Contract taken from #4013 by Dante-dan, which carried
+ * this guard before we did.
+ */
+export function motionBlurWindowIsStatic(
+ plan: MotionBlurPlan,
+ frameIndex: number,
+ staticFrames: ReadonlySet<number>,
+): boolean {
+ const first = Math.floor(
+ frameIndex - 1 + Math.min(...plan.sampleTickOffsets) / plan.subFrameDivisions,
+ );
+ // A sample landing inside [F, F+1) reads content the frame set only pins down at both
+ // ends, so the frame after the last one touched has to be static as well.
+ const last =
+ Math.floor(frameIndex + Math.max(...plan.sampleTickOffsets) / plan.subFrameDivisions) + 1;
+ for (let frame = first; frame <= last; frame++) {
+ if (!staticFrames.has(frame)) return false;
+ }
+ return true;
+}
+
const SRGB_TO_LINEAR = (() => {
const lut = new Float64Array(256);
for (let i = 0; i < 256; i++) lut[i] = srgbByteToLinear(i);