Skip to content

More efficient mp4 seek logic#1878

Open
JBRS307 wants to merge 2 commits intomasterfrom
@jbrs/efficient-seek
Open

More efficient mp4 seek logic#1878
JBRS307 wants to merge 2 commits intomasterfrom
@jbrs/efficient-seek

Conversation

@JBRS307
Copy link
Copy Markdown
Collaborator

@JBRS307 JBRS307 commented Apr 3, 2026

Closes #1858

@JBRS307 JBRS307 force-pushed the @jbrs/efficient-seek branch from 02e1bea to 9acb24f Compare April 3, 2026 10:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the MP4 seek logic by replacing an inefficient O(n) sample-by-sample iteration with an O(m) algorithm that uses the STTS (Sample-to-Time) and STSS (Sync Sample) MP4 box structures. The old implementation read every sample individually to find the seek position, while the new one calculates the position using metadata boxes and binary search, eliminating unnecessary I/O operations.

Changes:

  • Refactors find_seek_start_sample to use STTS box entries for efficient sample position calculation instead of reading each sample
  • Uses partition_point binary search on STSS entries to find the nearest sync sample before the seek point
  • Changes function signature from &mut self to &self, eliminating unnecessary mutability requirement
  • Removes the TODO: improve performance comment along with the inefficient implementation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

let mut present_from_index = None;

// Finds the first sample after the provided seek point.
for entry in &stts.entries {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This does not work if there is only one entry in stts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More efficient handling of mp4 seek.

3 participants