Repository navigation
feat: Honor and save the Matroska track enabled flag in place - #140
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #140 +/- ##
==========================================
- Coverage 93.04% 92.97% -0.07%
==========================================
Files 288 293 +5
Lines 57634 58521 +887
Branches 5510 5632 +122
==========================================
+ Hits 53624 54410 +786
- Misses 2334 2418 +84
- Partials 1676 1693 +17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
8 tasks done
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
FlagEnabled (0xB9) defaults to 1 and a stored 0 marks the track as not usable (RFC 9559 5.1.4.1.4). The main audio and video track is now the first enabled default track of that type, then the first enabled one. When every track of a type is disabled, the same order runs over the disabled tracks, since chapter codecs or control tracks can still make them usable. A missing or empty element is enabled. MatroskaFile.Tracks lists each known-size TrackEntry with its number, UID, type, codec ID, default flag, and enabled flag.
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the nearest Void that is not past a Cluster, after Tracks first, so the clusters stay where they are. A Void that would be left with one octet is skipped. Without a usable Void every later element moves. SeekHead and Cues positions follow each piece to where it landed, and the CRC-32 of every changed master is recomputed. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails.
The integration tests read TAGLIBSHARP2_TEST_MATROSKA, check that an untouched save keeps the Tracks and Cluster payloads, and toggle the first audio track and back.
An in-place FlagEnabled edit moves no element, so it no longer asks for the strict SeekHead and Cues walk. A SeekHead the writer cannot read only fails the save when an element it may name has moved.
A missing FlagEnabled now takes its room from the Void right after Tracks, so no element changes its segment position and the SeekHead, Cues, and clusters keep their bytes. Any other insert fails and writes nothing. Moving later elements is left to the shared Matroska shift, so MatroskaRewrite is back to its main behavior apart from being partial.
…ck list Adding FlagEnabled (minver 2) makes the EBML header's DocTypeVersion at least 2 (RFC 9559 section 7): a lower stored value is raised in place, and an empty or missing one, which means 1, is written, growing the header and its CRC. The header is outside the Segment, so no Segment Position moves. MatroskaFile.Tracks keeps at most 4096 entries. RFC 9559 sets no maxOccurs on TrackEntry, and a 10 MB file of empty entries allocated 433 MiB; every entry still ranks for the main audio and video track. FlagDefault and TagDefault read through IsFlagSet, like FlagEnabled. The track fixtures use an EBML header with DocTypeVersion 4, as muxers write it.
MatroskaFile.Tracks is documented as the first 4096 known-size TrackEntry elements, and the new MatroskaFile.TracksTruncated is true when a file had more, so a caller can tell a cut list from a full one. Tests now pin the EBML header size widening when an added DocTypeVersion takes 123 header octets to 127 (40 7F, +5), and the shrink when a wide empty DocTypeVersion is replaced (-6).
decriptor
force-pushed
the
feat/matroska-track-enabled
branch
from
November 21, 2026 19:49
1af3193 to
dd09959
Compare
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 2 (its #1 and #3), applied to this stack: MatroskaFile.Tracks is documented as the first 4096 known-size TrackEntry elements, MatroskaFile.TracksTruncated says when a file had more, and tests pin the EBML header size widening (+5, 40 7F) and the wide empty DocTypeVersion shrink (-6).
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 2 (its #1 and #3), applied to this stack: MatroskaFile.Tracks is documented as the first 4096 known-size TrackEntry elements, MatroskaFile.TracksTruncated says when a file had more, and tests pin the EBML header size widening (+5, 40 7F) and the wide empty DocTypeVersion shrink (-6).
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 2 (its #1 and #3), applied to this stack: MatroskaFile.Tracks is documented as the first 4096 known-size TrackEntry elements, MatroskaFile.TracksTruncated says when a file had more, and tests pin the EBML header size widening (+5, 40 7F) and the wide empty DocTypeVersion shrink (-6).
Every other public bool in the library has an Is, Has or Uses prefix. The property is unreleased, so the rename breaks no one.
DocTypeVersion has maxOccurs 1, but a file can store it twice. Every copy below 2 is now raised, and an empty copy is written as 2, as every FlagEnabled copy is written, so a reader that takes the first copy no longer sees 1. The header CRC is recomputed only when a copy changed. The same change as #148 round 2.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Every other public bool in the library has an Is, Has or Uses prefix. The property is unreleased, so the rename breaks no one. The same change as #140 round 3.
A header with two empty DocTypeVersion copies must end with both at 2. Without this row, replacing only the first empty copy stayed green.
Owner
Author
|
Review: 0 findings at c8da9a8. All findings from four review rounds are fixed, and the last round found nothing new. What was checked at this head:
|
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 2 (its #1 and #3), applied to this stack: MatroskaFile.Tracks is documented as the first 4096 known-size TrackEntry elements, MatroskaFile.TracksTruncated says when a file had more, and tests pin the EBML header size widening (+5, 40 7F) and the wide empty DocTypeVersion shrink (-6).
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Every other public bool in the library has an Is, Has or Uses prefix. The property is unreleased, so the rename breaks no one. The same change as #140 round 3.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
The same change as #140 round 2 (its #1 and #3), applied to this stack: MatroskaFile.Tracks is documented as the first 4096 known-size TrackEntry elements, MatroskaFile.TracksTruncated says when a file had more, and tests pin the EBML header size widening (+5, 40 7F) and the wide empty DocTypeVersion shrink (-6).
decriptor
added a commit
that referenced
this pull request
Nov 21, 2026
Every other public bool in the library has an Is, Has or Uses prefix. The property is unreleased, so the rename breaks no one. The same change as #140 round 3.
decriptor
added a commit
that referenced
this pull request
Nov 22, 2026
Setting MatroskaTrack.IsEnabled writes FlagEnabled back on save. A stored 1 to 8 octet element is rewritten in place. An empty or missing one becomes a 3-octet element, and the TrackEntry and Tracks sizes grow to hold it, keeping their width when the new size fits. The growth comes out of the Void right after Tracks, so no element moves; without that room the save fails and nothing is written. A stored DocTypeVersion of 1 is raised to 2 when an element is added, since FlagEnabled has minver 2. A track the caller never set keeps its bytes. The track at each ordinal must still carry the number and UID it was read with, or the save fails. This is the same change as #140.
decriptor
added a commit
that referenced
this pull request
Nov 22, 2026
The same change as #140 round 1 (its F2 and F4), applied to this stack: MatroskaFile.Tracks keeps at most 4096 entries while every entry still ranks for the main track, and FlagDefault and TagDefault share IsFlagSet.
decriptor
added a commit
that referenced
this pull request
Nov 22, 2026
The same change as #140 round 2 (its #1 and #3), applied to this stack: MatroskaFile.Tracks is documented as the first 4096 known-size TrackEntry elements, MatroskaFile.TracksTruncated says when a file had more, and tests pin the EBML header size widening (+5, 40 7F) and the wide empty DocTypeVersion shrink (-6).
decriptor
added a commit
that referenced
this pull request
Nov 22, 2026
Every other public bool in the library has an Is, Has or Uses prefix. The property is unreleased, so the rename breaks no one. The same change as #140 round 3.
This was referenced Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Matroska now reads and writes
TrackEntry\FlagEnabled(0xB9).Selection. The main audio and video track is the first enabled default track of that type, then the first enabled one. A track that stores
FlagEnabled0 can no longer win while an enabled track of its type exists. A missing or empty element counts as enabled (default 1).API.
MatroskaFile.Tracks(IReadOnlyList<MatroskaTrack>) lists the first 4096 known-sizeTrackEntryelements in file order (MatroskaFile.IsTrackListTruncatedis true when a file has more; every entry still ranks for the main track) withTrackNumber,TrackUid,TrackType(newMatroskaTrackType, RFC 9559 Table 2),CodecId,IsDefault, and a settableIsEnabled. No per-track public API existed before; this is the smallest one that can carry a settable flag, shaped likeFlacCueSheet.Tracks/FlacCueSheetTrack.Saving (no element ever moves). Only a track whose
IsEnabledthe caller set is touched, and only when the stored value differs.TrackEntryandTracksCRC-32s are recomputed.B9 81 0v. TheTrackEntryandTrackssizes grow, keeping their octet width when the new size fits. The extra octets come out of theVoiddirectly afterTracks, which shrinks to nothing or to at least 2 octets. No element changes its segment position, so the SeekHead, Cues, and clusters keep their bytes.FlagEnabledhas minver 2, and RFC 9559 §7 requires DocTypeVersion to be at least the highest element version. So when an element is added, a stored DocTypeVersion below 2 is raised in place, and an empty or missing one (which means 1) is written as 2. The EBML header grows by 1 octet (an empty element widened), 4 (a missing one added), or 5 (added, with the header size widening from 1 to 2 octets), or shrinks when an empty element was stored with a wide size (10 octets → 4: −6). Its CRC-32 is recomputed. The header is outside the Segment, so no Segment Position moves (round 1, F1).TrackNumberandTrackUIDit was read with, or the save fails. This guardsSaveToFile(path, otherData).Render) go through oneTryRender.MatroskaRewrite.csis unchanged apart frompartial.Motivation and Context
Issue #117: a disabled track could become the file's audio or video track, and the flag was neither read nor writable.
Closes #117
Spec references
Design decisions
MediaTypesstays consistent withTrackType. Documented onMatroskaTrack.IsEnabledis sticky. Disable, save, re-enable, save writes 1 again. Saving the same value twice gives identical bytes.ChapterFlagEnabled(0x4598) is left to the chapter editor, as the issue says.Out of scope (follow-up stacked PR)
Inserts that need elements to move. These are files without a Void right after Tracks: 11 of the 13 real files below, including all mkvmerge-era samples and ffmpeg output, where ffmpeg puts its Void after the SeekHead. Making room there means moving elements and patching the SeekHead, Cues, and
Cluster\Position. That work is being done once for Tags, Chapters, Attachments and Tracks onfix/matroska-seekhead-fallback. A second PR stacked on that branch will route this insert through it. Until then those saves fail cleanly instead of writing a partial fix.Existing main behavior, not changed here. An unchanged save already re-renders Tags. On test1/test6 it moves Tags to the segment end and drops their SeekHead entry. Every comparison below is against a
mainbuild's unchanged save of the same input.Type of Change
Checklist
dotnet test)Testing
Gates
dotnet build -c Release: 0 warnings, 0 errors (netstandard2.0, netstandard2.1, net8.0, net10.0)dotnet test -c Release(rebased on main85ce8d2): net8.0 4528 passed, 13 skipped; net10.0 4528 passed, 13 skipped. The skips are env-gated integration tests.dotnet format --verify-no-changes: cleanUnit tests (
MatroskaTrackEnabledTests12,MatroskaTrackEnabledSaveTests34)Integration (
MatroskaIntegrationTests, env varTAGLIBSHARP2_TEST_MATROSKA), run locally against all 13 files below.Real files. Black-box tools: ffprobe/ffmpeg 8 and mediainfo 26.10. Neither reports FlagEnabled at all, so
enzyme0.5.2 (MIT, PyPI, used only as an independent reader) confirms the flag. A local EBML walker checks every CRC-32, every SeekHead entry, and every CueClusterPosition target.Insert (track #2 disabled):
posidenticalposidentical; mediainfo identical apart from mtimeMain audio after disabling: Turning Lime AAC 48000; test5 AAC 44100 (track #10, the enabled non-default track).
In place (re-enabling files that store the flag):
None of these files stores a FlagEnabled as shipped, and this PR refuses to add one to them (see "Refused" below). The inputs for this table are the disabled outputs of an earlier revision of this branch,
3bc9171, which still moved later elements to make room. With this head, the same inputs come from the stacked #148. From #140 alone, the reproducible in-place check is: insert the flag into Turning Lime or test5, which have a Void after Tracks, then toggle it back. The result differs by 1 byte, the clusters are identical, and the CRC, seek and cue checks pass, as the reviewer reproduced.Refused (no Void after Tracks): test1-4, test6-8, and ffmpeg multi.mkv / multi.webm / audio.mka / live.mkv. Each save returns the error and writes nothing.
Generated with ffmpeg from lavfi sources: H.264 + 2 AAC with
-disposition:a:1 default, VP9 + 2 Opus WebM, 2 FLAC mka, and-live 1mkv.Additional Notes
Implemented from RFC 9559 and RFC 8794 only. No media files are committed; tests build their inputs in code.
Round 1
Commits
aa2f4f2and9efd129(after the rebase onto main; they weref3f11beand1af3193):F1, DocTypeVersion: a missing or empty DocTypeVersion is now written as 2 when the flag is added (above). Tests:
Save_InsertedFlag_AddsAMissingDocTypeVersion(+4 octets, header CRC valid, no Segment Position moved),Save_InsertedFlag_ReplacesAnEmptyDocTypeVersion(+1), andSave_InPlaceFlag_LeavesAMissingDocTypeVersion. The first two fail at07724ed. The track fixtures now use an EBML header with DocTypeVersion 4, as muxers write it.F2, track cap:
MatroskaFile.Trackskeeps at most 4096 entries. Every entry still ranks for the main track. On the reviewer'smany5m.mkv(5,000,000 empty entries), measured with the reviewer'smem.cs:manytags.mkvis unchanged (the Tags follow-up). Test:Read_ManyTrackEntries_ExposesABoundedListButRanksThemAll, which fails at8f5a6a6(was07724edbefore the rebase) with 4597 tracks instead of 4096.F3, PR body: the provenance of the in-place rows is stated above.
F4,
IsFlagSet: FlagDefault and TagDefault now read throughIsFlagSet. There is no behaviour change; the existing default-flag tests cover it.Before/after,
8f5a6a6(was07724ed) vs9efd129:MediaFiledetection over the 573-file corpus: identical output.dtv_absent.mkvanddtv_empty.mkv. Those now carry DocTypeVersion 2, and enzyme, ffprobe and the decode check still read them cleanly.Patch coverage on this PR's source lines vs main is 93%.
Round 2
Rebased onto main
85ce8d2. CHANGELOG now has this PR's### Addedblock above the single### Fixedlist, and the #117 line is added to main's Fixed entries.TracksXML doc and this body now say "the first 4096". The new publicMatroskaFile.IsTrackListTruncatedtells a caller when a file had more (public API snapshot updated). Tests:Read_ManyTrackEntries_ExposesABoundedListButRanksThemAllasserts it is true, andRead_ExactlyTheTrackLimit_IsNotTruncatedasserts it is false at exactly 4096 and on a normal file.Save_InsertedFlag_WidensTheEbmlHeaderSize. A 123-octet header without DocTypeVersion grows by 5 and its size reads40 7F. The reviewer's mutant (EncodeSizeat a fixed width) turns it red.Save_InsertedFlag_ReplacesAWideEmptyDocTypeVersionpins the −6 case.Round 3
Commits
72cac91andd3f98af(headd3f98af):MatroskaFile.TracksTruncatedis nowIsTrackListTruncated, matching the library's other public bools (Is/Has/Uses). It is unreleased, so the rename breaks no caller. The code, tests, PublicApi snapshot, CHANGELOG and this body are updated.Save_InsertedFlag_RaisesEveryDocTypeVersionCopy(cases (1, 4) → (2, 4), (1, empty) → (2, 2), and, from round 4 inc8da9a8, (empty, empty) → (2, 2), which turns red if only the first empty copy is replaced). The same code fails it on feat: Move later Matroska elements when a track flag needs room #148 atf6b9a29.