Play MKVs whose subtitles are zlib-compressed

Orbit's Matroska reader stopped the whole programme on ContentCompAlgo 0 (seen on Paradox,
7 November 2026). Text subtitles in zlib are now unpacked; other zlib tracks that aren't
sound or picture are left out instead of failing the file. 0.108.93.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-07 15:16:33 +00:00
co-authored by Claude Opus 5.5
parent a43c064738
commit 2ef38b7f70
5 changed files with 68 additions and 12 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ android {
applicationId = "tv.orbit.classic"
minSdk = 26
targetSdk = 36
versionCode = 252
versionName = "0.108.92-remember-who"
versionCode = 253
versionName = "0.108.93-zlib-subtitles"
}
buildFeatures { compose = true }
// The finished build for the TVs (Paul, 6 November 2026: the test build stuttered on the box). Not debuggable, so
@@ -62,9 +62,9 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
}
/** The sign-in headers for this kind of server. */
internal fun authHeaders(token: String?): Map<String, String> = when (kind) {
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.92\"") +
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.93\"") +
(token?.let { mapOf("X-Emby-Token" to it) } ?: emptyMap())
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.92\"" +
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.93\"" +
(token?.let { ", Token=\"$it\"" } ?: ""))
}
/** One picture from the provider, or nothing. Same fixed origin and no redirects, and failures are never shown or logged. */
@@ -39,7 +39,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.108.92")
next.setRequestProperty("User-Agent", "OrbitTV/0.108.93")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {
@@ -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())
}
}