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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(webv2): never make an audio-only reference the reference-extend anchor
Lincoln Stein1e5e066c7337
diff --git a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts b/invokeai/frontend/webv2/src/features/video/core/settings.test.tsindex 70b35d9194..324fbd7d67 100644--- a/invokeai/frontend/webv2/src/features/video/core/settings.test.ts+++ b/invokeai/frontend/webv2/src/features/video/core/settings.test.ts@@ -6,6 +6,7 @@ import { MINIMAX_H3_NUM_FRAMES_CHOICES } from './dimensions';import {resizeReferenceSampleWindow,slideReferenceSampleWindow,+ anchorReferenceConditioning,applyReferenceExtendSourceVideo,applyReferenceExtendNumFrames,canPlaceReferenceExtendAnchor,@@ -504,6 +505,124 @@ 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('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 --git a/invokeai/frontend/webv2/src/features/video/core/settings.ts b/invokeai/frontend/webv2/src/features/video/core/settings.tsindex 67ff8bd225..60525a7c70 100644--- a/invokeai/frontend/webv2/src/features/video/core/settings.ts+++ b/invokeai/frontend/webv2/src/features/video/core/settings.ts@@ -232,7 +232,7 @@ const sanitizeVideoReferences = (value: unknown, sourceVideoName?: string): Videfor (let index = valid.length - 1; index >= 0; index -= 1) {const entry = valid[index]!;- if (entry.kind === 'video' && entry.clip.video_name === sourceVideoName) {+ if (entry.kind === 'video' && entry.clip.video_name === sourceVideoName && canAnchorReferenceExtend(entry)) {valid = valid.map((candidate, candidateIndex) =>candidateIndex === index && candidate.kind === 'video' ? { ...candidate, fromSourceVideo: true } : candidate);@@ -553,6 +553,42 @@ export const getDefaultReferenceConditioning = (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.+ */+export const anchorReferenceConditioning = (conditioning: VideoReferenceConditioning): VideoReferenceConditioning =>+ conditioning === 'audio' ? 'video_audio' : conditioning;+/*** The detail a newly added image reference starts on.*@@ -835,12 +871,25 @@ export const applyReferenceExtendSourceVideo = (const linkedIndex =flaggedIndex >= 0? flaggedIndex- : references.findIndex((entry) => entry.kind === 'video' && entry.clip.video_name === sourceVideo.video_name);+ : references.findIndex(+ (entry) =>+ entry.kind === 'video' &&+ entry.clip.video_name === sourceVideo.video_name &&+ // An audio-only reference to this clip is the user's soundtrack, not a+ // continuity anchor: adopting it would replace their window with the tail and+ // still leave the seam with no visuals. Fall through and append instead.+ canAnchorReferenceExtend(entry)+ );if (linkedIndex >= 0) {return pinReferenceExtendAnchor(references.map((entry, index) =>- index === linkedIndex && entry.kind === 'video' ? { ...linked, conditioning: entry.conditioning } : entry+ 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));}diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsxindex 3f0f03b12a..e9a30b7923 100644--- a/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoReferenceListField.tsx@@ -102,6 +102,8 @@ const getSingleGalleryDragItem = (data: unknown): { kind: 'image' | 'video'; nam};type ReferenceCollections = {+ /** The anchor's options: 'Audio only' is absent, see `anchorReferenceConditioning`. */+ anchorConditioning: ReturnType<typeof createListCollection<{ label: string; value: string }>>;conditioning: ReturnType<typeof createListCollection<{ label: string; value: string }>>;detail: ReturnType<typeof createListCollection<{ label: string; value: string }>>;};@@ -136,6 +138,15 @@ const ReferenceCard = memo(function ReferenceCard({() => [reference.kind === 'video' ? reference.conditioning : reference.detail],[reference]);+ const selectCollection = useMemo(() => {+ if (reference.kind !== 'video') {+ return collections.detail;+ }++ // The anchor is not offered 'Audio only': it is the reference the extension continues+ // FROM, and an audio-only one contributes no visual rows to continue from.+ return reference.fromSourceVideo === true ? collections.anchorConditioning : collections.conditioning;+ }, [collections, reference]);const handleSelect = useCallback((details: { value: string[] }) => {const value = details.value[0];@@ -222,7 +233,7 @@ const ReferenceCard = memo(function ReferenceCard({) : null}</HStack><Select- collection={reference.kind === 'video' ? collections.conditioning : collections.detail}+ collection={selectCollection}disabled={disabled}size="xs"value={selectValue}@@ -381,17 +392,19 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({!isInert && activeDragItem !== null && (activeDragItem.kind === 'video' ? canAddVideo : canAddImage);const { isOver, setNodeRef } = useDroppable({ disabled: !acceptsActiveDrag, id: DROP_ID });- const conditioningCollection = useMemo(- () =>- createListCollection({- items: [- { label: t('widgets.video.referenceConditioningVideoAudio'), value: 'video_audio' },- { label: t('widgets.video.referenceConditioningVideo'), value: 'video' },- { label: t('widgets.video.referenceConditioningAudio'), value: 'audio' },- ],- }),+ const conditioningItems = useMemo(+ () => [+ { label: t('widgets.video.referenceConditioningVideoAudio'), value: 'video_audio' },+ { label: t('widgets.video.referenceConditioningVideo'), value: 'video' },+ { label: t('widgets.video.referenceConditioningAudio'), value: 'audio' },+ ],[t]);+ const conditioningCollection = useMemo(() => createListCollection({ items: conditioningItems }), [conditioningItems]);+ const anchorConditioningCollection = useMemo(+ () => createListCollection({ items: conditioningItems.filter((item) => item.value !== 'audio') }),+ [conditioningItems]+ );const detailCollection = useMemo(() =>createListCollection({@@ -403,8 +416,12 @@ export const VideoReferenceListField = memo(function VideoReferenceListField({[t]);const collections = useMemo(- () => ({ conditioning: conditioningCollection, detail: detailCollection }),- [conditioningCollection, detailCollection]+ () => ({+ anchorConditioning: anchorConditioningCollection,+ conditioning: conditioningCollection,+ detail: detailCollection,+ }),+ [anchorConditioningCollection, conditioningCollection, detailCollection]);const handlePickImage = useCallback(() => imageInputRef.current?.click(), []);const handlePickVideo = useCallback(() => videoInputRef.current?.click(), []);