Repository navigation
Handle secondary and recursive SeekHead elements in MatroskaExtractor (updated) - #3579
TharathielCB wants to merge 9 commits into
Conversation
When resolving secondary SeekHead elements, format emission was delayed until Cues were parsed. If the secondary SeekHead pointed directly to Cues or contained only Cluster entries, the extractor returned directly to the saved Cluster position without scheduling Cues or completing the no-Cues fallback first, causing samples to be emitted before track formats. This commit updates the index-resolution flow to: - Properly schedule Cues parsing or complete the no-Cues fallback (emitting track formats and unseekable SeekMap) before returning to sample reading. - Support multi-hop SeekHead traversal up to a bounded depth (16 hops). - Detect and prevent cycles using visited and pending position tracking. - Reset index traversal state when an explicit seek to position 0 occurs before the seek map is finalized. - Add regression test coverage in MatroskaExtractorNonParameterizedTest for single-hop, multi-hop, Cluster-only secondary SeekHead, cycles, and seek resets. Fixes androidx#1143
When resolving the index (via secondary SeekHead or Cues): - If a Cues reference points to a Cluster, encountering ID_CLUSTER during index resolution could cause maybePrepareSeekMap() to repeatedly schedule seeks to the same position. Now, encountering ID_CLUSTER while seekPositionAfterBuildingCues is set completes index resolution fallback (emitting unseekable SeekMap, track formats, and clearing index flags), and avoids redundant seeks if already at the saved Cluster position. - If EOF is reached while resolving the index (such as a Cues pointer referencing an element at or past the end of input), the extractor now executes terminal fallback by emitting an unseekable SeekMap and track formats, and restores position to the saved Cluster to resume media playback. - Add regression tests in MatroskaExtractorNonParameterizedTest covering both cases. Fixes androidx#1143
- Consume cuesContentPosition upon scheduling seek and record target position in visitedSeekHeadPositions to prevent repeated seeks and cyclic Cues references. - Reject Cues entries pointing to already-visited SeekHead positions. - Guard visitedSeekHeadPositions array bounds against MAX_SEEK_HEAD_HOPS. - Ensure seekMap and track formats are emitted if EOF is reached before seek map is sent. - Validate seekEntryPosition in integerElement against negative values. - Add regression tests in MatroskaExtractorNonParameterizedTest for Cues pointing to non-Cues SeekHead and mutual cyclic Cues references. Fixes androidx#1143
|
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. |
1c6c8dc to
db83b8d
Compare
|
Hi @sahuadarsh0 Thank you for your review! Both points are addressed in the latest commit:
When Claude looked over my changes he suggest to add a seperate test for that. So I added read_withTruncatedSecondarySeekHeadAtEndOfInput_seeksBackAndOutputsSamples. It uses a secondary SeekHead whose Seek element is truncated at the end of the input, and verifies that the extractor seeks back to the original position, outputs an unseekable SeekMap and continues extracting samples. And I also removed some explanatory comments I had added for myself in the previous revision. |
|
OK. Thank you. merge conflicts are closed aswell. We are good to go. |
|
Thanks for adding the shared seek budget and bounds checks. I rechecked The new truncation test ends between child elements, so Could an |
|
Thank you for your review @FongMi . An EOFException thrown while reading the optional index (secondary SeekHead or Cues) now uses the same fallback as reaching end of input: index resolution is abandoned, the EBML reader is reset, and extraction seeks back to the saved Cluster position. EOFs outside index resolution are still propagated unchanged. Both paths share a single helper. Added read_withSecondarySeekHeadTruncatedInsideSeekPosition_seeksBackAndOutputsSamples, which cuts the secondary SeekHead inside the SeekPosition payload and verifies that an unseekable SeekMap is output and samples resume from the first Cluster. One Point Claude hinted me to: |
|
See #3588 (comment) |
Based on PR: #3479 by https://github.com/rashidekbal
Adressed Issue: #1143
Original problem description:
Problem
Certain Matroska (.mkv) files, especially large WEB-DL and high-definition encodes created by mkvmerge, place their Cues element reference inside a secondary (chained/recursive) SeekHead rather than the primary SeekHead at the beginning of the file.
When reading these files, MatroskaExtractor only recorded ID_CUES and ID_TRACKS seek entries from the primary SeekHead. It ignored ID_SEEK_HEAD entries. Consequently, cuesContentPosition remained unset (C.INDEX_UNSET). When buildSeekMap() was invoked, MatroskaExtractor concluded that the file lacked cues and initialized an unseekable SeekMap.Unseekable(durationUs), rendering the media non-seekable despite valid cues existing in the container.
Solution
Detect Chained SeekHead: In MatroskaExtractor.endMasterElement(), check for seekEntryId == ID_SEEK_HEAD. When cuesContentPosition == C.INDEX_UNSET, record secondarySeekHeadPosition = seekEntryPosition.
Execute Secondary SeekHead Read: If cuesContentPosition has not been located but secondarySeekHeadPosition is known, initiate a seek via maybeSeekForSecondarySeekHead() to parse the secondary SeekHead and discover the true cuesContentPosition.
Defer SeekMap Fallback: Update buildSeekMap() and early format emission guards so that seeking is not prematurely disabled as unseekable before attempting to parse the secondary SeekHead.
Position Restoration: After reading the secondary SeekHead, restore the input position back to the previous track reading offset.
Testing
Verified with Matroska WEB-DL files (1080p / 4K) containing secondary SeekHead pointing to Cues.
Confirmed that cuesContentPosition is successfully resolved, Cues are parsed, seek map entries are built, and seeking functions accurately without regression.
Addition in this PR:
Changes:
Updated acc. to the review: #3479 (comment) by https://github.com/FongMi
Addressed both points:
Added tests for the budget at capacity (Cues chain through more SeekHeads than the budget), a Cues offset beyond the input length, and a Cues offset beyond the Segment end. Existing tests that append SeekHeads after the original Segment now update the Segment size accordingly. All 14 tests in MatroskaExtractorNonParameterizedTest and all tests in androidx.media3.extractor.mkv.* pass locally.
Thank you for the review!
Note:
Claude helped me out, here and there.