Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): budget the tail window on short clips, and land it on the grid exactly

Lincoln Stein6a04f56729e2
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
index e7f6b013a4..a0fe1b558a 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
@@ -449,10 +449,12 @@ describe('reference-extend linkage', () => {
it('derives the tail trim: the window ending at the cutpoint, clamped at 0', () => {
expect(deriveReferenceExtendClip(source24, FRAMES)).toMatchObject({ endFrame: 400, startFrame: 260 });
- // Shorter than the tail window: sample from the clip's own start.
+ // Shorter than the tail window: fall back to the largest ON-GRID budget the
+ // clip supports (80 frames -> 73) so the window still ENDS on the cutpoint.
+ // Starting at 0 kept the same 73 frames but stopped 7 short of the seam.
expect(deriveReferenceExtendClip({ ...SOURCE_VIDEO, fps: 24 }, FRAMES)).toMatchObject({
endFrame: 79,
- startFrame: 0,
+ startFrame: 7,
});
});
@@ -486,6 +488,41 @@ describe('reference-extend linkage', () => {
}
});
+ it('a clip SHORTER than the window still ends its tail on the cutpoint', () => {
+ // The clamped branch used to take the whole clip, whose length is
+ // arbitrary: off the 17n+5 grid, so the snap-down cut the difference from
+ // the END. Nothing about it needs an inexact estimate — it was the common
+ // case for any source under ~5.9s at 24 fps, and the sweep above could not
+ // see it because it only runs a 402-frame clip.
+ const resample = (n: number, fps: number) => Math.floor((n * 24) / fps + 0.5);
+ const snapDown = (n: number) => Math.max(1, Math.floor((n - 5) / 17)) * 17 + 5;
+
+ for (const fps of [12, 16, 23.976, 24, 25, 30]) {
+ for (const total of [40, 60, 80, 100, 120, 141, 200]) {
+ for (const numFrames of [90, 124, 141, 345]) {
+ const source = { ...longSource, endFrame: total - 1, fps, numFrames: total };
+ const clip = deriveReferenceExtendClip(source, numFrames);
+ const window = clip.endFrame - clip.startFrame + 1;
+ const kept = snapDown(Math.min(resample(window, fps), numFrames));
+
+ const discarded = Math.min(resample(window, fps), numFrames) - kept;
+
+ // At most a single frame is trimmed off the seam end -- the source's
+ // own frame boundaries sometimes cannot land on the grid exactly (12
+ // fps cannot reach an odd 141 at all). The old clamp shed up to 16.
+ expect({ discarded: discarded <= 1, fps, numFrames, total }).toEqual({
+ discarded: true,
+ fps,
+ numFrames,
+ total,
+ });
+ // And the window always still ends where it was asked to.
+ expect(clip.endFrame).toBe(source.endFrame);
+ }
+ }
+ }
+ });
+
it('converts the window into the source clip fps', () => {
// 141 frames of 24 fps material is 94 frames of a 16 fps clip — the same
// 5.875s of wall time, which is what the tail window actually means.
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.ts
index 86dea91b02..e2dbab72b8 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts
@@ -444,19 +444,58 @@ export const deriveReferenceExtendClip = (sourceVideo: VideoSourceClip, numFrame
startFrame: referenceExtendStartFrame(sourceVideo, numFrames),
});
+/** `snap_reference_num_frames`: down to the `17n + 5` grid the video VAE encodes whole. */
+const snapReferenceFrames = (frames: number): number => Math.max(1, Math.floor((frames - 5) / 17)) * 17 + 5;
+
+/** Source frames -> frames after the backend's 24 fps resample (ffmpeg's fps-filter count). */
+const resampledFrameCount = (sourceFrames: number, fps: number): number =>
+ Math.floor((sourceFrames * MINIMAX_H3_FPS) / fps + 0.5);
+
/** The tail window's start index — see `deriveReferenceExtendClip`. */
const referenceExtendStartFrame = (clip: VideoSourceClip, numFrames: number): number => {
- const budget = Number.isFinite(numFrames)
- ? Math.min(VIDEO_REFERENCE_EXTEND_TAIL_FRAMES, Math.trunc(numFrames))
- : VIDEO_REFERENCE_EXTEND_TAIL_FRAMES;
// A clip whose probe recorded no usable rate: the backend resamples at the
// rate it probes, so 24 — a no-op conversion — is the only safe assumption.
// A zero or negative rate would otherwise collapse the window to the 2-frame
// floor, which is below the 13 frames text conditioning needs.
const fps = Number.isFinite(clip.fps) && clip.fps > 0 ? clip.fps : MINIMAX_H3_FPS;
- const tail = Math.max(MIN_VIDEO_TRIM_FRAMES, Math.ceil((budget * fps) / MINIMAX_H3_FPS));
+ const requested = Number.isFinite(numFrames)
+ ? Math.min(VIDEO_REFERENCE_EXTEND_TAIL_FRAMES, Math.trunc(numFrames))
+ : VIDEO_REFERENCE_EXTEND_TAIL_FRAMES;
+ // A clip shorter than the window cannot supply the whole budget, and simply
+ // taking what is there leaves the window OFF the 17n+5 grid — so the
+ // snap-down cuts the difference from the END, the frames at the cutpoint.
+ // (24 fps, a 120-frame clip, numFrames 345: a [0,118] window keeps 107 of
+ // its 119 frames and stops half a second short of the seam.) Falling back to
+ // the largest on-grid budget the clip DOES support keeps the same frames and
+ // lands them on the seam. Both bounds are already on the grid in the normal
+ // case, so this is an identity there.
+ const budget = Math.min(
+ requested,
+ snapReferenceFrames(Math.min(resampledFrameCount(clip.endFrame + 1, fps), requested))
+ );
+
+ return Math.max(0, clip.endFrame - (tailSourceFrames(budget, fps) - 1));
+};
+
+/**
+ * The fewest source frames that still resample to at least `budget` frames.
+ *
+ * `ceil(budget * fps / 24)` is the closed form and it is not exact: the
+ * resample is a step function, so the rounded-up count can overshoot the
+ * budget by a frame or two, and every overshot frame is discarded from the END
+ * — the seam. Walking back down to the smallest count that still reaches the
+ * budget lands on it exactly wherever the source's frame boundaries allow, and
+ * within a frame where they do not (12 fps cannot hit an odd 141 at all). The
+ * loop runs `24 / fps` times at most.
+ */
+const tailSourceFrames = (budget: number, fps: number): number => {
+ let tail = Math.max(MIN_VIDEO_TRIM_FRAMES, Math.ceil((budget * fps) / MINIMAX_H3_FPS));
+
+ while (tail > MIN_VIDEO_TRIM_FRAMES && resampledFrameCount(tail - 1, fps) >= budget) {
+ tail -= 1;
+ }
- return Math.max(0, clip.endFrame - (tail - 1));
+ return tail;
};
/**