Skip to content

Matroska: follow linked SeekHead elements to find Cues - #3526

Open
brendongl wants to merge 1 commit into
androidx:mainfrom
brendongl:mkv-linked-seekhead
Open

brendongl wants to merge 1 commit into
androidx:mainfrom
brendongl:mkv-linked-seekhead

Conversation

@brendongl

Copy link
Copy Markdown

Fixes #1143.

Problem

Some MKV files have a first SeekHead whose only entry points to another SeekHead, and only a later SeekHead references Cues. As noted in #1143, mkvtoolnix writes this layout when it can't rewrite the header in place (e.g. after mkvpropedit). MatroskaExtractor only records Seek entries for Cues and Tracks, so it never finds the Cues, reports the stream as unseekable, and every seek fails or restarts from the start.

This is common in the wild. In one media library, 216 of 299 files from the WEB-DL group TURG use this layout. A typical file has: front SeekHead → SeekHead at end of file → SeekHead before the attachments → Cues. We reproduced the failure on Android TV with two different Media3-based players.

Change

  • Seek entries that point to a SeekHead are queued as linked SeekHeads. Already-visited positions and positions outside the segment are skipped, and at most 8 are followed.
  • When the first Cluster is reached and no Cues position is known, the extractor seeks to the next linked SeekHead. The decision is made when that SeekHead element ends, because a linked SeekHead can be the last element in the file. If Cues is now known it is read; otherwise the next link is followed or SeekMap.Unseekable is output. In every case the extractor then seeks back to the original position, as the existing Cues/Tracks seeks do.
  • Track formats are deferred while linked SeekHeads are pending, so they're output once: with the Cues-derived metadata, or on the unseekable fallback path.
  • The linked-SeekHead state is reset in seek(), so a restart from the beginning works.

Files without linked SeekHeads behave exactly as before.

Testing

  • New asset media/mkv/sample_with_linked_seek_heads.mkv. It is sample.mkv with the first SeekHead replaced by a single link (padded with a Void so all other offsets are unchanged) to a SeekHead at the end of the file. That one links to another SeekHead holding the original entries, matching the real-world layout above (verified with mkvinfo).
  • MatroskaExtractorTest#mkvSample_withLinkedSeekHeads passes in all simulation modes. Its dumps are identical to sample.mkv's, including isSeekable = true and the same seek points.
  • MatroskaExtractorNonParameterizedTest#read_cuesOnlyReferencedByLinkedSeekHead_seekMapIsSeekable checks that the seek map is seekable and matches sample.mkv's.
  • ./gradlew :lib-extractor:testDebugUnitTest: 5765 tests, 0 failures.

🤖 Generated with Claude Code

Some files have a first SeekHead that only links to another SeekHead
(e.g. at the end of the file), and only that one references the Cues
element. This layout is produced when the header can't be rewritten in
place, for example after editing with mkvpropedit. Previously only the
first SeekHead was parsed, so the Cues were never found and the file was
reported as unseekable.

Follow SeekHead entries that point to other SeekHead elements (up to a
fixed limit, skipping already visited positions) before deciding that
the Cues element is missing. Track formats are deferred while linked
SeekHeads are pending, so they're output once with the Cues-derived
metadata, or on the fallback path if no Cues are found.

Issue: androidx#1143

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@google-cla

google-cla Bot commented Nov 8, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@google-oss-bot

Copy link
Copy Markdown
Collaborator

Hey @brendongl. We need more information to resolve this issue but there hasn't been an update in 14 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@icbaker

icbaker commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

See #3588 (comment)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants