Skip to content

Add support for Matroska zlib compressed subtitle tracks - #3211

Open
inorichi wants to merge 3 commits into
androidx:mainfrom
inorichi:feature/zlib_text_tracks_in_mkv
Open

inorichi wants to merge 3 commits into
androidx:mainfrom
inorichi:feature/zlib_text_tracks_in_mkv

Conversation

@inorichi

@inorichi inorichi commented May 8, 2026 •

Copy link
Copy Markdown

This PR solves #2361

The first commit implements the changes to support zlib compressed subtitles in SUBRIP, ASS, SSA and VTT.
PGS and VOBSUB already support zlib compressed subtitles (I verified this by creating a sample file with zlib compression) so no changes were needed.
The second commit adds the samples and new tests and dumps for those samples.

The implementation looks for the master element ID_CONTENT_COMPRESSION and when present, enables a boolean flag in the current track.

Later, when a subtitle block is processed, a new inflatedSubtitleSample is used to hold the uncompressed data, and follows a similar approach to PgsParser and VobsubParser by using the method Util.maybeInflate and finally copies the result to the real subtitleSample.

The method reset could not be used because the input already contains the codec prefix at the beginning, so I used System.arraycopy after the prefix. Let me know if you prefer another approach.

The samples were created from the existing ones with the closest tool that created them in the first place, so I've used both ffmpeg and mkvmerge. When running diffs on the dumps of the original mkv and derived mkv with zlib compression, the only changes are in the seekMap positions, so I don't think we can reuse other dumps as it's done for other tests, but I'm no expert on the seekMap so let me know if it's possible.

I've also mixed usages of the null terminated samples because zlib data may contain a null byte, so the decompression must be done before looking for a null byte.

The commands used were either of the two:

mkvmerge -o sample_output.mkv --compression 2:zlib sample_input.mkv
ffmpeg -i sample_input.mkv -map 0 -c copy -metadata:s:s:0 compression=zlib sample_output.mkv

@ethaldeman

Copy link
Copy Markdown

Any chance of this making it into 1.11.0? I am suffering downstream issues that this pull request would solve.

@inorichi
inorichi force-pushed the feature/zlib_text_tracks_in_mkv branch from cda36af to d658190 Compare August 28, 2026 10:41
@tzi4

tzi4 commented Sep 7, 2026

Copy link
Copy Markdown

Could this PR be reviewed for an upcoming release please? In my setup MKV files with explicit ContentCompAlgo=0 on PGS tracks cause playback failure in Jellyfin Android TV even with subtitles disabled which triggers software transcoding that overwhelms my Raspberry Pi server.

@moi15moi

Copy link
Copy Markdown

@rohitjoins Will you review this PR?

@hingbong

Copy link
Copy Markdown

Hi, any progress here

private boolean pendingEndTracks;

// Inflater for zlib compressed tracks
@Nullable private Inflater inflater;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Inflater holds native zlib resources (z_stream), but release() is still a no-op here. Calling if (inflater != null) { inflater.end(); inflater = null; } in release() avoids leaking native memory until GC finalization.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The other places in this project where Inflater is used does not manually handle finalization, so I see no reason for this to be an exception. The GC will properly cleanup any native resources.

// TODO: check and fail if more than one content encoding is present.
break;
case ID_CONTENT_COMPRESSION:
getCurrentTrack(id).hasContentCompression = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ID_CONTENT_COMPRESSION (0x5034) is the parent element for both zlib (ContentCompAlgo = 0, the Matroska default when omitted) and header stripping (ContentCompAlgo = 3). Setting hasContentCompression = true unconditionally here means header-stripped subtitle tracks will also enter the Util.maybeInflate branch in writeSubtitleSampleData (where ZLIB_INFLATE_HEADER == 0x78, ASCII 'x'). Clearing the zlib flag when ID_CONTENT_COMPRESSION_ALGORITHM is 3 (or tracking the algorithm explicitly) keeps header stripping and zlib separate.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is probably also the case for ID_CONTENT_ENCRYPTION. I prefer to follow the status of the current implementation. If you can demonstrate this is a problem with a valid MKV file, we can discuss it further.

// This extractor only supports header stripping.
if (value != 3) {
// This extractor only supports header stripping and zlib compressed text tracks.
if (value != 3 && (value != 0 || getCurrentTrack(id).type != C.TRACK_TYPE_TEXT)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In EBML/Matroska, TrackEntry children have no required wire order, so ContentEncodings can appear before TrackType. If ID_CONTENT_COMPRESSION_ALGORITHM (0) is parsed before ID_TRACK_TYPE, getCurrentTrack(id).type is still C.TRACK_TYPE_UNKNOWN and a valid zlib subtitle track will be rejected. Validating ContentCompAlgo == 0 against currentTrack.type == C.TRACK_TYPE_TEXT in endMasterElement(ID_TRACK_ENTRY) avoids order-dependent failures (and also catches omitted ContentCompAlgo, which defaults to 0, on non-text tracks).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I can't validate it in endMasterElement if the exception is already thrown. The PR would need more changes to not throw exceptions here. If you find an affected MKV and the androidx.media team wants to handle that case, then I can work on that.

@FongMi

FongMi commented Nov 27, 2026

Copy link
Copy Markdown

I reproduced two issues with zlib-compressed SubRip tracks using the patch from d658190 on main.

  1. Valid zlib streams starting with 0x18 are skipped. Util.maybeInflate() checks for 0x78, so my sample with a valid 512-byte window reaches the subtitle output still compressed. For a track declared as zlib, Util.inflate() can parse the header directly. RFC 1950 §2.2
  2. Failed inflation still outputs the compressed bytes. When inflation returns false, writeSubtitleSampleData() leaves the original payload in subtitleSample and the extractor emits it as subtitle text. I reproduced this with a truncated zlib stream. Please handle the inflation failure before emitting the sample.

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.

8 participants