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(video): unpin the Ref2VA continuity anchor's end frame

Lincoln Stein729a2d55e05a
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.json
index 9faafcbd3a..087f97adbf 100644
--- a/invokeai/frontend/webv2/public/locales/en.json
+++ b/invokeai/frontend/webv2/public/locales/en.json
@@ -3569,7 +3569,7 @@
"referenceDetailMax": "Max detail (2048px, slower)",
"referenceExtendCapFull": "Initial video not set",
"referenceExtendCapFullDescription": "All 3 video reference slots are in use, so there is no room for the continuity reference this clip needs. Remove a video reference, then drop the clip again.",
- "referenceExtendHelp": "The generated video is appended to this clip at its End Frame. A linked video reference samples up to the last ~5 seconds before that cutpoint for continuity — never more than the generated clip's own length, since the model discards the overrun at the seam. Its trim re-derives whenever the cutpoints or the frame count change.",
+ "referenceExtendHelp": "The generated video is appended to this clip at its End Frame. A linked video reference samples up to the last ~5 seconds before that cutpoint for continuity — never more than the generated clip's own length, since the model discards the overrun at the end of the window. That trim is only a default: it re-derives whenever the cutpoints or the frame count change, until you move the reference's own Start Frame or Sample Length, after which it is left alone. Clear and re-set this clip to get the default back.",
"referenceImageCapRace": "All {{max}} image reference slots filled while this one was loading, so it was not added.",
"referenceImageCost": "{{width}}×{{height}} · {{rows}} rows per step",
"referenceVideoCapRace": "All {{max}} video or audio reference slots filled while this one was loading, so it was not added.",
diff --git a/invokeai/frontend/webv2/src/features/video/core/graph.ts b/invokeai/frontend/webv2/src/features/video/core/graph.ts
index 8a228edb86..bbc01ee552 100644
--- a/invokeai/frontend/webv2/src/features/video/core/graph.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/graph.ts
@@ -127,7 +127,9 @@ const addReferenceNode = (
* `tail - 1` frames before the cutpoint — so it rides the same negative anchor
* and the window keeps its length whatever the real count turns out to be.
*
- * Two cases stay absolute. A cutpoint far enough from the end leaves both
+ * Three cases stay absolute. A window the user moved off the derived default
+ * (`trimOverridden`) is a hand-picked trim like any other, so it takes the
+ * hand-picked treatment. A cutpoint far enough from the end leaves both
* bounds on the estimate already. And a start within `TAIL_INDEX_SLOP` of the
* clip's own beginning stays absolute because the relative form resolves to
* `startFrame + (real - estimate)`, which goes NEGATIVE once the estimate
@@ -149,7 +151,12 @@ const addReferenceNode = (
const toReferenceStartIndex = (reference: Extract<VideoReferenceItem, { kind: 'video' }>, endIndex: number): number => {
const { clip } = reference;
- if (reference.fromSourceVideo !== true || endIndex >= 0 || clip.startFrame <= TAIL_INDEX_SLOP) {
+ if (
+ reference.fromSourceVideo !== true ||
+ reference.trimOverridden === true ||
+ endIndex >= 0 ||
+ clip.startFrame <= TAIL_INDEX_SLOP
+ ) {
return toTailAwareIndex(clip.startFrame, clip.numFrames);
}
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 64448d2c23..2eb743d10c 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
@@ -628,6 +628,50 @@ describe('reference-extend linkage', () => {
expect(retrimmed[0]).toBe(added[0]);
});
+ it('leaves an overridden anchor window alone across cutpoint and frame-count changes', () => {
+ // The fade-to-black case: the cutpoint keeps the fade in the Initial Video
+ // that gets concatenated, while the reference samples earlier material.
+ // Ref2VA has no frame-exact seam, so that choice is the user's to make.
+ const linked = applyReferenceExtendSourceVideo([IMAGE_REFERENCE], source24, 3, FRAMES);
+ const overridden = linked.map((entry, index) =>
+ index === 1 && entry.kind === 'video'
+ ? {
+ ...entry,
+ clip: resizeReferenceSampleWindow(slideReferenceSampleWindow(entry.clip, 100), 120),
+ trimOverridden: true,
+ }
+ : entry
+ );
+
+ expect(overridden[1]).toMatchObject({ clip: { endFrame: 219, startFrame: 100 } });
+
+ // Moving the cutpoint on the SAME clip no longer drags the window with it.
+ const retrimmed = applyReferenceExtendSourceVideo(overridden, { ...source24, endFrame: 300 }, 3, FRAMES);
+
+ expect(retrimmed[1]).toMatchObject({
+ clip: { endFrame: 219, startFrame: 100, video_name: 'long.mp4' },
+ fromSourceVideo: true,
+ trimOverridden: true,
+ });
+ // Nor does a frame-count change re-budget it (identity-preserving).
+ expect(applyReferenceExtendNumFrames(retrimmed, 124)).toBe(retrimmed);
+ });
+
+ it('drops an overridden anchor window when the initial video changes clip', () => {
+ // The bounds index frames of a clip that is no longer there.
+ const linked = applyReferenceExtendSourceVideo([], source24, 3, FRAMES);
+ const overridden = linked.map((entry) =>
+ entry.kind === 'video'
+ ? { ...entry, clip: slideReferenceSampleWindow(entry.clip, 100), trimOverridden: true }
+ : entry
+ );
+ const other = { ...source24, endFrame: 200, numFrames: 202, video_name: 'other.mp4' };
+ const reset = applyReferenceExtendSourceVideo(overridden, other, 3, FRAMES);
+
+ expect(reset[0]).toMatchObject({ clip: { endFrame: 200, startFrame: 60, video_name: 'other.mp4' } });
+ expect(reset[0]).not.toHaveProperty('trimOverridden');
+ });
+
it('clearing the initial video removes only the linked reference (identity-preserving when none)', () => {
const list = applyReferenceExtendSourceVideo([VIDEO_REFERENCE, IMAGE_REFERENCE], source24, 3, FRAMES);
@@ -926,76 +970,66 @@ describe('reference sample window', () => {
});
describe('slideReferenceSampleWindow', () => {
- it('slides an ordinary window at constant length', () => {
- const next = slideReferenceSampleWindow(clip(0, 199), 50, false);
+ it('slides a window at constant length', () => {
+ const next = slideReferenceSampleWindow(clip(0, 199), 50);
expect([next.startFrame, next.endFrame]).toEqual([50, 249]);
});
it('keeps the length while the clip can still supply it', () => {
- const next = slideReferenceSampleWindow(clip(0, 199), 100, false);
+ const next = slideReferenceSampleWindow(clip(0, 199), 100);
expect([next.startFrame, next.endFrame]).toEqual([100, 299]);
});
it('pins the length to the frames left instead of blocking the start', () => {
// The start reaches the frame the user picked; the sample is what gives way.
- const shortened = slideReferenceSampleWindow(clip(0, 199), 250, false);
+ const shortened = slideReferenceSampleWindow(clip(0, 199), 250);
expect([shortened.startFrame, shortened.endFrame]).toEqual([250, 299]);
- const lastFrame = slideReferenceSampleWindow(clip(0, 199), 299, false);
+ const lastFrame = slideReferenceSampleWindow(clip(0, 199), 299);
expect([lastFrame.startFrame, lastFrame.endFrame]).toEqual([299, 299]);
});
- it('keeps the extend anchor end pinned to the cutpoint', () => {
- // The anchor's seam continuity depends on frames adjacent to its end frame.
- const next = slideReferenceSampleWindow(clip(180, 298), 200, true);
- expect([next.startFrame, next.endFrame]).toEqual([200, 298]);
- const backAndForth = slideReferenceSampleWindow(slideReferenceSampleWindow(next, 250, true), 200, true);
- expect([backAndForth.startFrame, backAndForth.endFrame]).toEqual([200, 298]);
- });
-
- it('clamps the anchor start to its pinned end', () => {
- const next = slideReferenceSampleWindow(clip(180, 298), 500, true);
- expect([next.startFrame, next.endFrame]).toEqual([298, 298]);
+ it('moves an extend-anchor window off the cutpoint like any other', () => {
+ // Ref2VA has no frame-exact seam to hold, so the anchor's end is the
+ // user's to move -- a clip that fades to black at the cutpoint wants the
+ // fade concatenated but not conditioned on.
+ const next = slideReferenceSampleWindow(clip(180, 298), 100);
+ expect([next.startFrame, next.endFrame]).toEqual([100, 218]);
});
it('self-heals a corrupt persisted trim into bounds', () => {
// end < start and end beyond the clip must both come back as a valid window.
- const inverted = slideReferenceSampleWindow(clip(10, 5, 20), 0, false);
+ const inverted = slideReferenceSampleWindow(clip(10, 5, 20), 0);
expect(inverted.startFrame).toBeGreaterThanOrEqual(0);
expect(inverted.endFrame).toBeGreaterThanOrEqual(inverted.startFrame);
expect(inverted.endFrame).toBeLessThanOrEqual(19);
- const oversized = slideReferenceSampleWindow(clip(0, 999, 20), 5, false);
+ const oversized = slideReferenceSampleWindow(clip(0, 999, 20), 5);
expect([oversized.startFrame, oversized.endFrame]).toEqual([5, 19]);
});
it('handles a single-frame clip', () => {
- const next = slideReferenceSampleWindow(clip(0, 0, 1), 5, false);
+ const next = slideReferenceSampleWindow(clip(0, 0, 1), 5);
expect([next.startFrame, next.endFrame]).toEqual([0, 0]);
});
});
describe('resizeReferenceSampleWindow', () => {
- it('grows an ordinary window forward from its start', () => {
- const next = resizeReferenceSampleWindow(clip(50, 60), 100, false);
+ it('grows a window forward from its start', () => {
+ const next = resizeReferenceSampleWindow(clip(50, 60), 100);
expect([next.startFrame, next.endFrame]).toEqual([50, 149]);
});
it('clamps the length to the clip end', () => {
- const next = resizeReferenceSampleWindow(clip(250, 260), 100, false);
+ const next = resizeReferenceSampleWindow(clip(250, 260), 100);
expect([next.startFrame, next.endFrame]).toEqual([250, 299]);
});
- it('grows the extend anchor backward from its pinned end', () => {
- const next = resizeReferenceSampleWindow(clip(280, 298), 100, true);
- expect([next.startFrame, next.endFrame]).toEqual([199, 298]);
- });
-
- it('clamps the anchor lead-in at the clip start', () => {
- const next = resizeReferenceSampleWindow(clip(280, 298), 1000, true);
- expect([next.startFrame, next.endFrame]).toEqual([0, 298]);
+ it('shortens an extend-anchor window away from the cutpoint', () => {
+ const next = resizeReferenceSampleWindow(clip(180, 298), 20);
+ expect([next.startFrame, next.endFrame]).toEqual([180, 199]);
});
it('never produces a window shorter than one frame', () => {
- const next = resizeReferenceSampleWindow(clip(50, 199), -5, false);
+ const next = resizeReferenceSampleWindow(clip(50, 199), -5);
expect([next.startFrame, next.endFrame]).toEqual([50, 50]);
});
});
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.ts
index 15c3e7006b..4efccbe7ee 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts
@@ -92,32 +92,21 @@ export const DEFAULT_REFERENCE_SAMPLE_FRAMES = 200;
/**
* Move a reference clip's sample window to a new start frame.
*
- * The two controls are independent: an ordinary reference's start frame reaches every
- * frame of the clip, and the sample length is what gives way — it keeps its value while
- * there is clip left to fill it and is pinned to the remaining frames past that, so
- * `start + length` never runs beyond the last frame. (The length slider's ceiling in the
- * panel is the same `numFrames - startFrame`, so the control tracks what the window can
- * actually hold.) The reference-extend anchor (`pinEnd`) instead keeps its end frame
- * pinned to the Initial Video cutpoint (seam continuity depends on the frames adjacent to
- * it; see deriveReferenceExtendClip), so moving its start only adjusts the lead-in length.
+ * The two controls are independent: the start frame reaches every frame of the clip, and
+ * the sample length is what gives way — it keeps its value while there is clip left to
+ * fill it and is pinned to the remaining frames past that, so `start + length` never runs
+ * beyond the last frame. (The length slider's ceiling in the panel is the same
+ * `numFrames - startFrame`, so the control tracks what the window can actually hold.)
+ *
+ * Every video reference trims this way, the reference-extend anchor included: Ref2VA has
+ * no frame-exact seam to protect (see deriveReferenceExtendClip), so the anchor's window
+ * is a default the user may move off the cutpoint like any other.
*
* Self-healing by construction: the returned window always satisfies
* 0 <= start <= end <= numFrames - 1, even from a corrupt persisted trim.
*/
-export const slideReferenceSampleWindow = (
- clip: VideoSourceClip,
- rawStart: number,
- pinEnd: boolean
-): VideoSourceClip => {
+export const slideReferenceSampleWindow = (clip: VideoSourceClip, rawStart: number): VideoSourceClip => {
const maxFrame = Math.max(0, clip.numFrames - 1);
-
- if (pinEnd) {
- const endFrame = Math.min(Math.max(0, clip.endFrame), maxFrame);
- const startFrame = Math.min(Math.max(0, Math.round(rawStart)), endFrame);
-
- return { ...clip, endFrame, startFrame };
- }
-
const sampleFrames = Math.min(Math.max(1, clip.endFrame - clip.startFrame + 1), maxFrame + 1);
const startFrame = Math.min(Math.max(0, Math.round(rawStart)), maxFrame);
@@ -127,25 +116,12 @@ export const slideReferenceSampleWindow = (
/**
* Resize a reference clip's sample window to a new length in frames.
*
- * Ordinary references grow from the start frame (the end moves, clamped to the clip); the
- * reference-extend anchor (`pinEnd`) grows backward from its pinned end (the start moves),
- * since its end must stay on the Initial Video cutpoint. Same self-healing bounds as
- * slideReferenceSampleWindow.
+ * The window grows forward from its start frame, with the end clamped to the clip. Same
+ * self-healing bounds as slideReferenceSampleWindow.
*/
-export const resizeReferenceSampleWindow = (
- clip: VideoSourceClip,
- rawSampleFrames: number,
- pinEnd: boolean
-): VideoSourceClip => {
+export const resizeReferenceSampleWindow = (clip: VideoSourceClip, rawSampleFrames: number): VideoSourceClip => {
const maxFrame = Math.max(0, clip.numFrames - 1);
const sampleFrames = Math.min(Math.max(1, Math.round(rawSampleFrames)), maxFrame + 1);
-
- if (pinEnd) {
- const endFrame = Math.min(Math.max(0, clip.endFrame), maxFrame);
-
- return { ...clip, endFrame, startFrame: Math.max(0, endFrame - (sampleFrames - 1)) };
- }
-
const startFrame = Math.min(Math.max(0, clip.startFrame), maxFrame);
return { ...clip, endFrame: Math.min(startFrame + sampleFrames - 1, maxFrame), startFrame };
@@ -589,12 +565,19 @@ export const MIN_VIDEO_TRIM_FRAMES = 2;
export const VIDEO_REFERENCE_EXTEND_TAIL_FRAMES = 141;
/**
- * The tail reference's default trim: the frames right before the cutpoint,
+ * The tail reference's DEFAULT trim: the frames right before the cutpoint,
* sized so the backend keeps ALL of them.
*
+ * A default, not a constraint. Ref2VA conditions on reference CONTENT — there
+ * is no frame-exact seam here the way FL2VA extend has one — so which frames
+ * the anchor samples is an editorial choice, and the cutpoint is only the
+ * likeliest one. A clip that fades to black at the cutpoint wants the fade
+ * concatenated and NOT conditioned on, so the user can move the window off the
+ * cutpoint (`trimOverridden`) and the panel then leaves it alone.
+ *
* Three backend rules bound the window, and every one of them discards from
- * the END — the frames adjacent to the cutpoint, the only ones continuity
- * depends on. `normalize_reference_video_frames` resamples onto H3's fixed
+ * the END of it — where this default puts the frames nearest the cutpoint.
+ * `normalize_reference_video_frames` resamples onto H3's fixed
* 24 fps and truncates to the GENERATED frame count keeping the FRONT
* (`frames[:num_frames]`); `encode_reference_video` then snaps what survives
* DOWN to the `17n + 5` grid the video VAE encodes without padding.
@@ -707,10 +690,10 @@ const tailSourceFrames = (budget: number, fps: number): number => {
* `numFrames`, so the values a keystroke or a drag passes through leave no
* trace.
*
- * The cost is that a hand-tuned linked trim resets on a frame-count change as
- * well as on a cutpoint change — the same bargain the section's help text
- * already describes, and the backend leaves no alternative: a window that does
- * not fit the budget loses its seam end.
+ * A window the user has moved by hand (`trimOverridden`) is exempt. The budget
+ * still binds it — the backend discards the overrun from the window's end —
+ * but that is equally true of every ordinary video reference, and silently
+ * rewriting a deliberate editorial choice is the worse failure of the two.
*/
export const applyReferenceExtendNumFrames = (
references: VideoReferenceItem[],
@@ -718,7 +701,7 @@ export const applyReferenceExtendNumFrames = (
): VideoReferenceItem[] => {
let changed = false;
const next = references.map((entry) => {
- if (entry.kind !== 'video' || entry.fromSourceVideo !== true) {
+ if (entry.kind !== 'video' || entry.fromSourceVideo !== true || entry.trimOverridden === true) {
return entry;
}
const startFrame = referenceExtendStartFrame(entry.clip, numFrames);
@@ -795,9 +778,11 @@ export const canPlaceReferenceExtendAnchor = (
*
* - clearing the Initial Video removes its linked reference;
* - setting or re-trimming it re-derives the linked reference's default trim
- * (the tail window ending on the cutpoint) — a manually tuned trim
- * therefore holds only until the next cutpoint change, which the section's
- * help text says;
+ * (the tail window ending on the cutpoint), UNLESS the user has moved that
+ * window by hand and the clip is the same one — an override is a deliberate
+ * editorial choice about which frames condition the generation, and a
+ * cutpoint move is not a reason to discard it. Switching to a different clip
+ * does discard it: the bounds index frames that are no longer there;
* - with no linked entry yet, an existing video reference for the same clip
* is adopted (recall restores the pair without the linkage flag; adopting
* avoids a duplicate), else a new one is APPENDED — unless the video cap is
@@ -842,9 +827,27 @@ export const applyReferenceExtendSourceVideo = (
if (linkedIndex >= 0) {
return pinReferenceExtendAnchor(
- references.map((entry, index) =>
- index === linkedIndex && entry.kind === 'video' ? { ...linked, conditioning: entry.conditioning } : entry
- )
+ references.map((entry, index) => {
+ if (index !== linkedIndex || entry.kind !== 'video') {
+ return entry;
+ }
+ if (entry.trimOverridden !== true || entry.clip.video_name !== sourceVideo.video_name) {
+ return { ...linked, conditioning: entry.conditioning };
+ }
+
+ return {
+ ...linked,
+ // Re-probed dimensions and frame rate come from the source record;
+ // the trim is the user's. Sliding the window to its own start re-
+ // clamps it against a frame count that may have moved with the probe.
+ clip: slideReferenceSampleWindow(
+ { ...sourceVideo, endFrame: entry.clip.endFrame, startFrame: entry.clip.startFrame },
+ entry.clip.startFrame
+ ),
+ conditioning: entry.conditioning,
+ trimOverridden: true,
+ };
+ })
);
}
diff --git a/invokeai/frontend/webv2/src/features/video/core/types.ts b/invokeai/frontend/webv2/src/features/video/core/types.ts
index 6758cae4b6..e030ecf04a 100644
--- a/invokeai/frontend/webv2/src/features/video/core/types.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/types.ts
@@ -54,6 +54,18 @@ export type VideoReferenceItem =
* removable — and the flag is panel state only, never in metadata.
*/
fromSourceVideo?: boolean;
+ /**
+ * True once the user has moved the anchor's OWN trim controls. The
+ * derived window is a default, not a constraint: Ref2VA conditions on
+ * reference content with no frame-exact seam to protect, so where the
+ * sample ends is an editorial choice (a clip that fades to black wants
+ * the fade concatenated but not conditioned on). From then on the panel
+ * stops re-deriving the window from the cutpoint and the frame count,
+ * and the entry is trimmed like any other reference. Clearing and
+ * re-setting the Initial Video drops the override. Panel state only,
+ * never in metadata; meaningless without `fromSourceVideo`.
+ */
+ trimOverridden?: boolean;
}
| { kind: 'image'; image: ImageWithDims; detail: VideoReferenceImageDetail };
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
index 3f0f03b12a..8ad7d23eca 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
@@ -154,14 +154,20 @@ const ReferenceCard = memo(function ReferenceCard({
// The trim is presented as a sliding sample window — start frame plus length — because
// what the user is choosing is "how much" (every reference frame costs denoise VRAM) and
// "from where". Storage stays startFrame/endFrame (the request contract); the window
- // math (constant-length slide that stops at the clip's end, and the extend anchor's
- // pinned-to-the-cutpoint end) lives in core/settings.
+ // math (a start that reaches every frame, with the length pinned to what the clip can
+ // still supply) lives in core/settings.
+ //
+ // Touching either control on the extend anchor marks its window overridden, which stops
+ // the panel re-deriving it from the Initial Video's cutpoint and frame count. The
+ // derived window is only a default: Ref2VA has no frame-exact seam to protect, so where
+ // the anchor samples is the user's editorial call.
const handleStartFrame = useCallback(
(rawStart: number) => {
if (reference.kind === 'video') {
onUpdate(index, {
...reference,
- clip: slideReferenceSampleWindow(reference.clip, rawStart, reference.fromSourceVideo === true),
+ clip: slideReferenceSampleWindow(reference.clip, rawStart),
+ ...(reference.fromSourceVideo === true ? { trimOverridden: true } : {}),
});
}
},
@@ -172,7 +178,8 @@ const ReferenceCard = memo(function ReferenceCard({
if (reference.kind === 'video') {
onUpdate(index, {
...reference,
- clip: resizeReferenceSampleWindow(reference.clip, rawSampleFrames, reference.fromSourceVideo === true),
+ clip: resizeReferenceSampleWindow(reference.clip, rawSampleFrames),
+ ...(reference.fromSourceVideo === true ? { trimOverridden: true } : {}),
});
}
},
@@ -282,13 +289,9 @@ const ReferenceCard = memo(function ReferenceCard({
<SliderNumberField
ariaLabel={t('widgets.video.sampleLength')}
disabled={disabled}
- // The anchor grows backward from its pinned end, so its ceiling is the
- // available lead-in; ordinary windows grow forward from their start.
- max={
- reference.fromSourceVideo === true
- ? Math.max(1, reference.clip.endFrame + 1)
- : Math.max(1, reference.clip.numFrames - reference.clip.startFrame)
- }
+ // The window grows forward from its start, so the ceiling is what the
+ // clip has left from there — it falls as the start frame climbs.
+ max={Math.max(1, reference.clip.numFrames - reference.clip.startFrame)}
min={1}
showStepper
step={1}