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 reference sample window per adversarial review; extract and test the math

Lincoln Stein0560663c4783
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 19f0ac5771..ca57edd394 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
@@ -4,6 +4,8 @@ import type { VideoReferenceItem, VideoSettings } from './types';
import { MINIMAX_H3_NUM_FRAMES_CHOICES } from './dimensions';
import {
+ resizeReferenceSampleWindow,
+ slideReferenceSampleWindow,
applyReferenceExtendSourceVideo,
applyReferenceExtendNumFrames,
canPlaceReferenceExtendAnchor,
@@ -869,3 +871,85 @@ describe('reference-extend linkage', () => {
expect(applyReferenceExtendNumFrames(unlinked, 90)).toBe(unlinked);
});
});
+
+describe('reference sample window', () => {
+ const clip = (startFrame: number, endFrame: number, numFrames = 300) => ({
+ endFrame,
+ fps: 24,
+ height: 480,
+ numFrames,
+ startFrame,
+ video_name: 'clip.mp4',
+ width: 640,
+ });
+
+ describe('slideReferenceSampleWindow', () => {
+ it('slides an ordinary window at constant length', () => {
+ const next = slideReferenceSampleWindow(clip(0, 199), 50, false);
+ expect([next.startFrame, next.endFrame]).toEqual([50, 249]);
+ });
+
+ it('stops at the clip end instead of shrinking (no overshoot ratchet)', () => {
+ // Drag far past the wall, then back to 0: the length must survive the round trip.
+ const overshot = slideReferenceSampleWindow(clip(0, 199), 299, false);
+ expect([overshot.startFrame, overshot.endFrame]).toEqual([100, 299]);
+ const back = slideReferenceSampleWindow(overshot, 0, false);
+ expect([back.startFrame, back.endFrame]).toEqual([0, 199]);
+ });
+
+ 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('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);
+ 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);
+ expect([oversized.startFrame, oversized.endFrame]).toEqual([0, 19]);
+ });
+
+ it('handles a single-frame clip', () => {
+ const next = slideReferenceSampleWindow(clip(0, 0, 1), 5, false);
+ 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);
+ expect([next.startFrame, next.endFrame]).toEqual([50, 149]);
+ });
+
+ it('clamps the length to the clip end', () => {
+ const next = resizeReferenceSampleWindow(clip(250, 260), 100, false);
+ 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('never produces a window shorter than one frame', () => {
+ const next = resizeReferenceSampleWindow(clip(50, 199), -5, false);
+ 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 e3b0ddc9fc..2445a82563 100644
--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts
@@ -87,6 +87,65 @@ export const VIDEO_REFERENCE_MAX_IMAGES = 9;
*/
export const DEFAULT_REFERENCE_SAMPLE_FRAMES = 200;
+/**
+ * Move a reference clip's sample window to a new start frame.
+ *
+ * Ordinary references slide at CONSTANT length, stopping at the clip's end rather than
+ * shrinking — a transient overshoot during a drag must not ratchet the sample down. 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.
+ *
+ * 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 => {
+ 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 - (sampleFrames - 1));
+
+ return { ...clip, endFrame: startFrame + sampleFrames - 1, startFrame };
+};
+
+/**
+ * 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.
+ */
+export const resizeReferenceSampleWindow = (
+ clip: VideoSourceClip,
+ rawSampleFrames: number,
+ pinEnd: boolean
+): 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 };
+};
+
const VIDEO_REFERENCE_CONDITIONINGS = ['video_audio', 'video', 'audio'] as const;
const VIDEO_REFERENCE_IMAGE_DETAILS = ['max', 'match'] as const;
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
index b20dc47efc..32bcfd9ecc 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
@@ -10,7 +10,12 @@ import { Badge, Box, createListCollection, HStack, Image, Input, Spinner, Stack,
import { useDndContext, useDndMonitor, useDroppable } from '@dnd-kit/core';
import { galleryImages, galleryItems, galleryTransfers } from '@features/gallery';
import { galleryImageUrls, galleryVideoUrls, isGalleryItemDragData } from '@features/gallery/utility';
-import { createVideoSourceClip, DEFAULT_REFERENCE_SAMPLE_FRAMES } from '@features/video/core/settings';
+import {
+ createVideoSourceClip,
+ DEFAULT_REFERENCE_SAMPLE_FRAMES,
+ resizeReferenceSampleWindow,
+ slideReferenceSampleWindow,
+} from '@features/video/core/settings';
import {
assertAccountScopeCurrent,
captureAccountScope,
@@ -108,19 +113,15 @@ 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): length is
- // derived as end - start + 1, and moving the start slides the window at constant length
- // until it hits the clip's end, where it shrinks.
+ // "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.
const handleStartFrame = useCallback(
(rawStart: number) => {
if (reference.kind === 'video') {
- const maxFrame = Math.max(0, reference.clip.numFrames - 1);
- const sampleFrames = reference.clip.endFrame - reference.clip.startFrame + 1;
- const startFrame = Math.min(Math.max(0, rawStart), maxFrame);
-
onUpdate(index, {
...reference,
- clip: { ...reference.clip, endFrame: Math.min(startFrame + sampleFrames - 1, maxFrame), startFrame },
+ clip: slideReferenceSampleWindow(reference.clip, rawStart, reference.fromSourceVideo === true),
});
}
},
@@ -129,10 +130,10 @@ const ReferenceCard = memo(function ReferenceCard({
const handleSampleFrames = useCallback(
(rawSampleFrames: number) => {
if (reference.kind === 'video') {
- const maxFrame = Math.max(0, reference.clip.numFrames - 1);
- const endFrame = Math.min(reference.clip.startFrame + Math.max(1, rawSampleFrames) - 1, maxFrame);
-
- onUpdate(index, { ...reference, clip: { ...reference.clip, endFrame } });
+ onUpdate(index, {
+ ...reference,
+ clip: resizeReferenceSampleWindow(reference.clip, rawSampleFrames, reference.fromSourceVideo === true),
+ });
}
},
[index, onUpdate, reference]
@@ -208,7 +209,13 @@ const ReferenceCard = memo(function ReferenceCard({
<SliderNumberField
ariaLabel={t('widgets.video.sampleLength')}
disabled={disabled}
- max={Math.max(1, reference.clip.numFrames - reference.clip.startFrame)}
+ // 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)
+ }
min={1}
showStepper
step={1}