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.

Merge origin/main into fix/video-reference-independent-sample-window

Lincoln Stein971e84272057
diff --cc invokeai/frontend/webv2/src/features/video/core/settings.test.ts
index 27b44e841b,cba342ffa2..0543ba668b
--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts
@@@ -4,10 -4,10 +4,12 @@@ import type { VideoReferenceItem, Video
import { MINIMAX_H3_NUM_FRAMES_CHOICES } from './dimensions';
import {
+ clampReferenceSampleFrames,
+ DEFAULT_REFERENCE_SAMPLE_FRAMES,
+ referenceSampleFrames,
resizeReferenceSampleWindow,
slideReferenceSampleWindow,
+ anchorReferenceConditioning,
applyReferenceExtendSourceVideo,
applyReferenceExtendNumFrames,
canPlaceReferenceExtendAnchor,
@@@ -506,6 -590,124 +592,144 @@@ describe('references', () =>
});
});
+ describe('anchorReferenceConditioning', () => {
+ it('converts an audio-only choice, which cannot carry a seam', () => {
+ expect(anchorReferenceConditioning('audio')).toBe('video_audio');
+ });
+
+ it("leaves the user's own visual answers alone", () => {
+ expect(anchorReferenceConditioning('video')).toBe('video');
+ expect(anchorReferenceConditioning('video_audio')).toBe('video_audio');
+ });
+ });
+
+ describe('reference-extend anchor: audio-only references', () => {
+ const longSource = { ...SOURCE_VIDEO, endFrame: 400, numFrames: 402, video_name: 'long.mp4' };
+ const source24 = { ...longSource, fps: 24 };
+ const FRAMES = 141;
+ // The user's own soundtrack reference to the clip they are extending: whole clip, audio.
+ const userAudio = {
+ clip: { ...SOURCE_VIDEO, endFrame: 401, numFrames: 402, startFrame: 0, video_name: 'long.mp4' },
+ conditioning: 'audio',
+ kind: 'video',
+ } as const;
+
+ it('appends a real anchor beside an audio-only reference rather than consuming it', () => {
+ // Upload a soundtrack as a reference (it defaults to 'audio'), then extend that same
+ // clip. Adopting the audio entry would replace the user's window with the tail AND
+ // leave the seam with no visuals at all, since an 'audio' reference contributes no
+ // visual rows. Both references have a job; both survive.
+ const linked = applyReferenceExtendSourceVideo([userAudio], source24, 3, FRAMES);
+
+ expect(linked).toHaveLength(2);
+ expect(linked[0]).toBe(userAudio);
+ expect(linked[1]).toMatchObject({
+ clip: { endFrame: 400, startFrame: 260 },
+ conditioning: 'video_audio',
+ fromSourceVideo: true,
+ });
+ });
+
+ it('adopts a same-clip reference that CAN carry the seam, in preference to the audio one', () => {
+ const userVideo = {
+ ...userAudio,
+ clip: { ...userAudio.clip, endFrame: 172, startFrame: 100 },
+ conditioning: 'video',
+ } as const;
+ const linked = applyReferenceExtendSourceVideo([userAudio, userVideo], source24, 3, FRAMES);
+
+ // The audio entry is untouched, by identity; the video one becomes the anchor and
+ // keeps its deliberate 'video' choice.
+ expect(linked).toHaveLength(2);
+ expect(linked.find((entry) => entry === userAudio)).toBe(userAudio);
+ expect(linked[linked.length - 1]).toMatchObject({
+ clip: { endFrame: 400, startFrame: 260 },
+ conditioning: 'video',
+ fromSourceVideo: true,
+ });
+ });
+
+ it('converts an already-flagged audio anchor, re-deriving its window in the same pass', () => {
+ // A record written before this rule can carry one. It is healed WHOLE: the conditioning
+ // becomes usable and the window becomes the tail, which is why the conversion lives
+ // here and not in normalization.
+ const stale = {
+ ...userAudio,
+ clip: { ...userAudio.clip, endFrame: 72, startFrame: 0 },
+ fromSourceVideo: true,
+ } as const;
+ const linked = applyReferenceExtendSourceVideo([stale], source24, 3, FRAMES);
+
+ expect(linked[0]).toMatchObject({
+ clip: { endFrame: 400, startFrame: 260 },
+ conditioning: 'video_audio',
+ fromSourceVideo: true,
+ });
+ });
+
++ it('converts an overridden audio anchor without disturbing the window the user picked', () => {
++ // Where the two rules meet. The ROLE still needs visual rows, so the conditioning is
++ // promoted — but the window is one the user chose FOR this anchor, not the arbitrary
++ // one a mis-flagged entry carries, so a cutpoint change leaves it intact.
++ const overridden = {
++ ...userAudio,
++ clip: { ...userAudio.clip, endFrame: 219, startFrame: 100 },
++ fromSourceVideo: true,
++ trimOverridden: true,
++ } as const;
++ const linked = applyReferenceExtendSourceVideo([overridden], { ...source24, endFrame: 300 }, 3, FRAMES);
++
++ expect(linked[0]).toMatchObject({
++ clip: { endFrame: 219, startFrame: 100 },
++ conditioning: 'video_audio',
++ fromSourceVideo: true,
++ trimOverridden: true,
++ });
++ });
++
+ it('normalization never flags an audio-only reference as the anchor', () => {
+ // The recall re-derive picks the anchor by clip name. Landing on an audio-only entry
+ // would flag a reference whose window is NOT the tail -- once its conditioning implied
+ // visuals, the generation would continue from the opening of the clip. It is left as
+ // the plain reference it is, and no anchor is claimed.
+ const normalized = normalizeVideoSettings(
+ createSettings({ references: [IMAGE_REFERENCE, userAudio], sourceVideo: source24 })
+ );
+
+ expect(normalized?.references).toHaveLength(2);
+ expect(normalized?.references.some((entry) => entry.kind === 'video' && entry.fromSourceVideo === true)).toBe(
+ false
+ );
+ expect(normalized?.references.find((entry) => entry.kind === 'video')).toMatchObject({
+ clip: { endFrame: 401, startFrame: 0 },
+ conditioning: 'audio',
+ });
+ });
+
+ it('still re-derives the flag onto a same-clip reference that can carry the seam', () => {
+ // The recall re-derive itself is intact -- this is the case it exists for.
+ const recalled = { ...userAudio, conditioning: 'video_audio' as const };
+ const normalized = normalizeVideoSettings(
+ createSettings({ references: [IMAGE_REFERENCE, recalled], sourceVideo: source24 })
+ );
+
+ expect(normalized?.references[1]).toMatchObject({ conditioning: 'video_audio', fromSourceVideo: true });
+ });
+
+ it('leaves an audio-only reference beside a real anchor completely alone', () => {
+ // The rule is about the anchor's ROLE, not about audio references. With a flagged
+ // anchor present the re-derive does not run at all, and the user's soundtrack keeps
+ // its conditioning, its window and its object identity across normalization.
+ const realAnchor = { ...userAudio, conditioning: 'video_audio' as const, fromSourceVideo: true } as const;
+ const normalized = normalizeVideoSettings(
+ createSettings({ references: [userAudio, realAnchor], sourceVideo: source24 })
+ );
+
+ expect(normalized?.references[0]).toMatchObject({ conditioning: 'audio', clip: { endFrame: 401, startFrame: 0 } });
+ expect(normalized?.references[0]).not.toHaveProperty('fromSourceVideo', true);
+ });
+ });
+
describe('reference-extend linkage', () => {
// H3 generates at a fixed 24 fps; SOURCE_VIDEO runs at 16, so the two rates
// are exercised separately.
diff --cc invokeai/frontend/webv2/src/features/video/core/settings.ts
index fdac44c6cb,a6608e4b56..b5a2cbdf16
--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts
@@@ -566,6 -553,72 +566,73 @@@ export const getDefaultReferenceConditi
metadata: Record<string, unknown> | null | undefined
): VideoReferenceConditioning => (metadata?.media_origin === 'audio_upload' ? 'audio' : 'video_audio');
+ /**
+ * Whether a video reference can serve as the reference-extend ANCHOR.
+ *
+ * The anchor carries the tail of the Initial Video across the seam, and that is what the
+ * generated frames continue from. An 'audio' reference contributes soundtrack rows and no
+ * visual rows at all — the backend decodes a reference's frames only when its packed kind
+ * is `video` (`reference_kind`) — so it has nothing to offer the seam. Nor does it fail
+ * loudly: the all-audio validation reason needs EVERY reference to be audio-only, so one
+ * image reference alongside is enough to let a silently discontinuous seam queue.
+ *
+ * So an audio-only reference is never CHOSEN as the anchor. Adoption and the recall
+ * re-derive both skip it, and the anchor is appended fresh instead — which keeps the
+ * soundtrack the user actually asked for rather than overwriting it with a role it cannot
+ * fill. Only an entry already claiming the flag is converted, by
+ * {@link anchorReferenceConditioning}.
+ */
+ const canAnchorReferenceExtend = (entry: VideoReferenceItem): boolean =>
+ entry.kind === 'video' && entry.conditioning !== 'audio';
+
+ /**
+ * The conditioning an entry keeps once it IS the reference-extend anchor.
+ *
+ * Only reached for an entry already carrying the flag: a record written before
+ * {@link canAnchorReferenceExtend} existed, or a hand-edited one. Promoting keeps the
+ * soundtrack the choice asked for and restores the visuals the role requires; 'video' and
+ * 'video_audio' are the user's own answer and pass through.
+ *
- * Deliberately applied only where the anchor's WINDOW is re-derived alongside it
- * (`applyReferenceExtendSourceVideo`). Promoting in normalization instead would turn an
- * entry that was merely inert into one emitting visual rows from whatever window it
- * happens to hold — for a mis-flagged entry, the opening of the clip rather than the tail,
- * which is a worse seam than no anchor at all.
++ * Deliberately applied only in `applyReferenceExtendSourceVideo`, where the window is
++ * either re-derived alongside it or is one the user picked for THIS anchor
++ * (`trimOverridden`). Promoting in normalization instead would turn an entry that was
++ * merely inert into one emitting visual rows from whatever window it happens to hold —
++ * for a mis-flagged entry, the opening of the clip rather than the tail, which is a worse
++ * seam than no anchor at all.
+ */
+ export const anchorReferenceConditioning = (conditioning: VideoReferenceConditioning): VideoReferenceConditioning =>
+ conditioning === 'audio' ? 'video_audio' : conditioning;
+
+ /**
+ * The sample window a newly added video reference starts on.
+ *
+ * Footage starts on {@link DEFAULT_REFERENCE_SAMPLE_FRAMES} from the clip's head, because
+ * every reference frame is VAE-encoded into rows the denoiser re-attends at every step.
+ *
+ * An AUDIO-ONLY reference starts on the whole clip. It pays none of that cost: the backend
+ * decodes a reference's frames only when its packed kind is `video` (`reference_kind`), so
+ * an 'audio' reference contributes soundtrack rows and no visual rows at all — and those
+ * rows are bounded by the GENERATED duration, not by how long the window is. What the
+ * window decides is which audio the model hears: the track is sliced to the window and only
+ * THEN truncated to the generated duration, so a window shorter than the generation cuts
+ * the soundtrack off early. At 8s that is every frame count above 200 — over half of the
+ * ones the panel offers.
+ *
+ * Only the starting value — the card's sample-length control still trims either kind, and
+ * changing a card's conditioning afterwards leaves the window the user can see alone.
+ */
+ export const getDefaultReferenceClip = (
+ clip: VideoSourceClip,
+ conditioning: VideoReferenceConditioning
+ ): VideoSourceClip => ({
+ ...clip,
+ endFrame:
+ conditioning === 'audio'
+ ? Math.max(0, clip.numFrames - 1)
+ : Math.max(0, Math.min(DEFAULT_REFERENCE_SAMPLE_FRAMES, clip.numFrames) - 1),
+ startFrame: 0,
+ });
+
/**
* The detail a newly added image reference starts on.
*
@@@ -862,35 -913,14 +937,40 @@@ export const applyReferenceExtendSource
if (linkedIndex >= 0) {
return pinReferenceExtendAnchor(
- references.map((entry, index) =>
- index === linkedIndex && entry.kind === 'video'
- ? // Safe to convert here, and only here: `linked` re-derives the window in the
- // same breath, so a flagged audio-only entry from an older record is healed
- // whole rather than left pointing at the wrong part of the clip.
- { ...linked, conditioning: anchorReferenceConditioning(entry.conditioning) }
- : entry
- )
+ references.map((entry, index) => {
+ if (index !== linkedIndex || entry.kind !== 'video') {
+ return entry;
+ }
++ // Safe to convert an audio-only entry here: it is the flagged anchor, or one just
++ // adopted into the role, and the role needs visual rows. See
++ // `anchorReferenceConditioning`.
++ const conditioning = anchorReferenceConditioning(entry.conditioning);
++
+ // `fromSourceVideo` is required, not implied by `linkedIndex`: the adopt-by-name
- // fallback below reaches UNFLAGGED entries (recall restores the pair without the
++ // fallback above reaches UNFLAGGED entries (recall restores the pair without the
+ // flag), and those have never been an anchor, so they get the derived default.
+ if (
+ entry.fromSourceVideo !== true ||
+ entry.trimOverridden !== true ||
+ entry.clip.video_name !== sourceVideo.video_name
+ ) {
- return { ...linked, conditioning: entry.conditioning };
++ return { ...linked, 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,
++ conditioning,
+ sampleFrames: entry.sampleFrames,
+ trimOverridden: true,
+ };
+ })
);
}
diff --cc invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
index 184169c658,4d2337dbd4..dd95adc890
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx
@@@ -15,12 -15,10 +15,12 @@@ import { GalleryPickerPopover } from '@
import { galleryImageUrls, galleryVideoUrls, isGalleryItemDragData } from '@features/gallery/utility';
import { resolveMiniMaxH3ReferenceImage } from '@features/video/core/dimensions';
import {
+ clampReferenceSampleFrames,
createVideoSourceClip,
- DEFAULT_REFERENCE_SAMPLE_FRAMES,
+ getDefaultReferenceClip,
getDefaultReferenceConditioning,
getDefaultReferenceImageDetail,
+ referenceSampleFrames,
resizeReferenceSampleWindow,
slideReferenceSampleWindow,
} from '@features/video/core/settings';
diff --cc tests/app/invocations/test_minimax_h3_reference_nodes.py
index 011ee57c69,92a467f04c..99c2ea8e1b
--- a/tests/app/invocations/test_minimax_h3_reference_nodes.py
+++ b/tests/app/invocations/test_minimax_h3_reference_nodes.py
@@@ -5,8 -5,9 +5,9 @@@ prompt-vs-conditioning signature cross-
guard. The numerics live in tests/backend/minimax_h3/.
"""
-from unittest.mock import MagicMock
+from unittest.mock import MagicMock, patch
+ import numpy as np
import pytest
import torch
@@@ -20,7 -21,7 +21,8 @@@ from invokeai.app.invocations.fields im
)
from invokeai.app.invocations.minimax_h3_denoise import MiniMaxH3DenoiseInvocation
from invokeai.app.invocations.minimax_h3_reference import (
+ _ResolvedVideoRange,
+ load_reference_audio,
normalize_reference_list,
reference_has_audio,
reference_kind,