Play MKVs whose subtitles are zlib-compressed

Ported from release-0.108 (0.108.93).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-07 15:17:18 +00:00
co-authored by Claude Opus 5.5
parent efaa84e683
commit d9a8b15e04
2 changed files with 63 additions and 7 deletions
@@ -905,7 +905,17 @@ public class MatroskaExtractor implements Extractor {
throw ParserException.createForMalformedContainer(
"CodecId is missing in TrackEntry element", /* cause= */ null);
} else {
if (isCodecSupported(currentTrack.codecId)) {
boolean zlibReadable =
!currentTrack.zlibCompressed
|| CODEC_ID_SUBRIP.equals(currentTrack.codecId)
|| CODEC_ID_ASS.equals(currentTrack.codecId)
|| CODEC_ID_SSA.equals(currentTrack.codecId)
|| CODEC_ID_VTT.equals(currentTrack.codecId);
if (!zlibReadable && (currentTrack.type == 1 || currentTrack.type == TRACK_TYPE_AUDIO)) {
throw ParserException.createForMalformedContainer(
"ContentCompAlgo 0 not supported for " + currentTrack.codecId, /* cause= */ null);
}
if (isCodecSupported(currentTrack.codecId) && zlibReadable) {
currentTrack.initializeOutput(extractorOutput, currentTrack.number);
tracks.put(currentTrack.number, currentTrack);
}
@@ -1023,7 +1033,12 @@ public class MatroskaExtractor implements Extractor {
}
break;
case ID_CONTENT_COMPRESSION_ALGORITHM:
// This extractor only supports header stripping.
// Header stripping, and zlib (Orbit, 7 November 2026: subtitle tracks in some MKVs are zlib-compressed, and the
// whole file used to fail to play). zlib is read for text subtitles; any other zlib track is left out below.
if (value == 0) {
getCurrentTrack(id).zlibCompressed = true;
break;
}
if (value != 3) {
throw ParserException.createForMalformedContainer(
"ContentCompAlgo " + value + " not supported", /* cause= */ null);
@@ -1562,13 +1577,13 @@ public class MatroskaExtractor implements Extractor {
private int writeSampleData(ExtractorInput input, Track track, int size, boolean isBlockGroup)
throws IOException {
if (CODEC_ID_SUBRIP.equals(track.codecId)) {
writeSubtitleSampleData(input, SUBRIP_PREFIX, size);
writeSubtitleSampleData(input, SUBRIP_PREFIX, size, track.zlibCompressed);
return finishWriteSampleData();
} else if (CODEC_ID_ASS.equals(track.codecId) || CODEC_ID_SSA.equals(track.codecId)) {
writeSubtitleSampleData(input, SSA_PREFIX, size);
writeSubtitleSampleData(input, SSA_PREFIX, size, track.zlibCompressed);
return finishWriteSampleData();
} else if (CODEC_ID_VTT.equals(track.codecId)) {
writeSubtitleSampleData(input, VTT_PREFIX, size);
writeSubtitleSampleData(input, VTT_PREFIX, size, track.zlibCompressed);
return finishWriteSampleData();
}
@@ -1773,8 +1788,17 @@ public class MatroskaExtractor implements Extractor {
sampleStrippedBytes.reset(/* limit= */ 0);
}
private void writeSubtitleSampleData(ExtractorInput input, byte[] samplePrefix, int size)
throws IOException {
private void writeSubtitleSampleData(
ExtractorInput input, byte[] samplePrefix, int size, boolean zlib) throws IOException {
if (zlib) {
byte[] packed = new byte[size];
input.readFully(packed, 0, size);
byte[] text = inflate(packed);
subtitleSample.reset(Arrays.copyOf(samplePrefix, samplePrefix.length + text.length));
System.arraycopy(text, 0, subtitleSample.getData(), samplePrefix.length, text.length);
subtitleSample.setPosition(0);
return;
}
int sizeWithPrefix = samplePrefix.length + size;
if (subtitleSample.capacity() < sizeWithPrefix) {
// Initialize subripSample to contain the required prefix and have space to hold a subtitle
@@ -1790,6 +1814,26 @@ public class MatroskaExtractor implements Extractor {
// the correct end timecode, which we might not have yet.
}
/** A zlib-compressed subtitle, unpacked; at most 1 MB, and malformed data reads as an empty line. */
public static byte[] inflate(byte[] packed) {
java.util.zip.Inflater inflater = new java.util.zip.Inflater();
try {
inflater.setInput(packed);
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(packed.length * 3);
byte[] chunk = new byte[4096];
while (!inflater.finished() && out.size() < (1 << 20)) {
int n = inflater.inflate(chunk);
if (n == 0 && (inflater.needsInput() || inflater.needsDictionary())) break;
out.write(chunk, 0, n);
}
return out.toByteArray();
} catch (java.util.zip.DataFormatException e) {
return new byte[0];
} finally {
inflater.end();
}
}
/**
* Overwrites the end timecode in {@code subtitleData} with the correctly formatted time derived
* from {@code durationUs}.
@@ -2099,6 +2143,8 @@ public class MatroskaExtractor implements Extractor {
public int maxBlockAdditionId;
private int blockAddIdType;
public boolean hasContentEncryption;
/** Its blocks are zlib-compressed (ContentCompAlgo 0). */
public boolean zlibCompressed;
public byte [] sampleStrippedBytes;
public TrackOutput.CryptoData cryptoData;
public byte [] codecPrivate;
@@ -26,4 +26,14 @@ class OrbitMatroskaTest {
assertTrue(ours[0] === media3[0])
assertTrue(ours[1] is tv.orbit.classic.mkv.MatroskaExtractor)
}
// Paradox, 7 November 2026: an MKV whose subtitle track is zlib-compressed failed to play at all. Its subtitles are now
// unpacked; anything that isn't zlib reads as an empty line rather than stopping the programme.
@Test fun zlibSubtitlesAreUnpacked() {
val line = "0,0,Default,,0,0,0,,Previously on Twin Peaks".toByteArray()
val deflater = java.util.zip.Deflater(); deflater.setInput(line); deflater.finish()
val packed = ByteArray(256).let { buf -> buf.copyOf(deflater.deflate(buf)) }; deflater.end()
assertTrue(line.contentEquals(tv.orbit.classic.mkv.MatroskaExtractor.inflate(packed)))
assertTrue(tv.orbit.classic.mkv.MatroskaExtractor.inflate(byteArrayOf(1, 2, 3, 4)).isEmpty())
}
}