Skip to content

fix(media): decode inline video without plaintext temp files - #105

Merged
Gajesh2007 merged 2 commits into
mainfrom
fix/darkbloom-memory-inline-video-20260812
Aug 13, 2026
Merged

Gajesh2007 merged 2 commits into
mainfrom
fix/darkbloom-memory-inline-video-20260812

Conversation

@Gajesh2007

Copy link
Copy Markdown
Member

Changes

  • Add MemoryBackedVideoAsset, an owned AVFoundation resource loader that serves bounded byte ranges directly from immutable Data.
  • Add UserInput.Video.memoryBacked so the asset owner survives metadata probing and frame sampling.
  • Decode both MP4 and QuickTime data URIs without creating a file or network URL.
  • Validate ISO-BMFF/QuickTime container identity, reject external asset references, preserve cancellation, and cover range/lifetime/container edge cases.
flowchart LR
  subgraph Before
    A1[data:video URI] --> B1[write vlm-UUID.mp4]
    B1 --> C1[AVURLAsset file URL]
    C1 --> D1[metadata and frame sampling]
    D1 --> E1[deferred file removal]
  end
  subgraph After
    A2[data:video MP4 or QuickTime URI] --> B2[MemoryBackedVideoAsset]
    B2 --> C2[bounded AVAssetResourceLoader ranges]
    C2 --> D2[metadata and frame sampling]
    D2 --> E2[owner retained; no plaintext file]
  end
Loading
flowchart LR
  subgraph Before_Code
    F1[UserInput.Video.url] --> G1[AVAsset URL]
    G1 --> H1[MediaProcessing frame APIs]
  end
  subgraph After_Code
    F2[MemoryBackedVideoAsset] --> G2[UserInput.Video.memoryBacked]
    G2 --> H2[MediaProcessing.metadata]
    G2 --> I2[MediaProcessing frame APIs]
    H2 --> J2[retained asset owner]
    I2 --> J2
  end
Loading

Verification

  • swift-format lint --strict --configuration .swift-format Libraries/MLXLMCommon/MemoryBackedVideoAsset.swift Libraries/MLXLMCommon/UserInput.swift Libraries/MLXVLM/MediaProcessing.swift Tests/MLXLMTests/MediaProcessingTests.swift
  • swift build --build-tests
  • swift test --skip-build --filter MLXLMTests.MediaProcesingTests — 13 tests passed after installing the source-matched mlx.metallib in the test bundle.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files and retained formatting changes only in this PR's files; all four changed files pass strict swift-format lint
  • I have updated the necessary documentation (API comments describe ownership, cancellation, and the memory-only transport)
  • I have added tests that prove the lifetime, random-access, container-validation, cancellation, and no-temp-file contracts

@Gajesh2007 Gajesh2007 added the bug Something isn't working label Aug 12, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a12498dcb3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +401 to +402
for image in images {
try Task.checkCancellation()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check cancellation when no images were produced

When cancellation causes AVAssetImageGenerator.images(for:) to terminate before yielding its first frame, images is empty and this loop executes zero times, so none of its cancellation checks run. The caller can consequently return an empty ProcessedFrames successfully from a cancelled operation; perform an unconditional cancellation check before or after the loop.

Useful? React with 👍 / 👎.

@Gajesh2007
Gajesh2007 force-pushed the fix/darkbloom-memory-inline-video-20260812 branch from a12498d to 99bdf73 Compare August 13, 2026 06:45

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 99bdf73617

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

domain: "MediaProcessing", code: -1,
userInfo: [NSLocalizedDescriptionKey: "Failed to load the asset's duration"])
}
try Task.checkCancellation()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve cancellation errors from duration loading

When a memory-backed decode is cancelled while the preceding asset.load(.duration) is suspended, that load can throw for cancellation, but try? converts the error to nil and the guard throws the generic MediaProcessing NSError; this cancellation check is therefore never reached. Callers awaiting the new .memoryBacked path cannot reliably detect cancellation, so preserve the load error or check cancellation inside the guard's failure path.

Useful? React with 👍 / 👎.

Comment on lines +85 to +87
guard prefix[4] == 0x66, prefix[5] == 0x74, prefix[6] == 0x79, prefix[7] == 0x70
else {
throw MemoryBackedVideoAssetError.invalidContainer

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Accept QuickTime files without a leading ftyp atom

Valid QuickTime movie files do not require an ftyp atom, and their first atom may instead be moov, mdat, or another QuickTime atom. This unconditional leading-ftyp check therefore rejects a supported class of .mov/video/quicktime inputs before AVFoundation can inspect them, despite the initializer's stated QuickTime support; recognize the legacy QuickTime layout or narrow the API contract to only QuickTime files with a leading ftyp.

Useful? React with 👍 / 👎.

@Gajesh2007
Gajesh2007 merged commit cc37ccd into main Aug 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant