Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ public static ImmutableList<Sample> mediaSamples() {
Sample.forFile("sample_with_htc_rotation_track_name.mkv"),
Sample.withBitmapSubtitles("sample_with_pgs_subtitles.mkv"),
Sample.forFile("sample_with_ssa_subtitles.mkv"),
Sample.forFile("sample_with_zlib_ass_subtitles.mkv"),
Sample.forFile("sample_with_null_terminated_ssa_subtitles.mkv"),
Sample.forFile("sample_with_overlapping_ssa_subtitles.mkv"),
Sample.forFile("sample_with_srt.mkv"),
Sample.forFile("sample_with_null_terminated_srt.mkv"),
Sample.forFile("sample_with_zlib_null_terminated_srt.mkv"),
Sample.forFile("sample_with_overlapping_srt.mkv"),
Sample.forFile("sample_with_vtt_subtitles.mkv"),
Sample.forFile("sample_with_null_terminated_vtt_subtitles.mkv"),
Sample.forFile("sample_with_zlib_null_terminated_vtt_subtitles.mkv"),
Sample.withBitmapSubtitles("sample_with_vobsub.mkv"),
Sample.forFile("sample_last_cluster.mkv"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.zip.Inflater;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;
Expand Down Expand Up @@ -469,6 +470,7 @@ public static ExtractorsFactory newFactory(SubtitleParser.Factory subtitleParser
private final ParsableByteArray seekEntryIdBytes;
private final ParsableByteArray sampleStrippedBytes;
private final ParsableByteArray subtitleSample;
private final ParsableByteArray inflatedSubtitleSample;
private final ParsableByteArray encryptionInitializationVector;
private final ParsableByteArray encryptionSubsampleData;
private final ParsableByteArray supplementalData;
Expand All @@ -482,6 +484,9 @@ public static ExtractorsFactory newFactory(SubtitleParser.Factory subtitleParser
private boolean isWebm;
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.


// The chapter corresponding to the current EditionEntry element, or null.
@Nullable private ChapterEntry currentChapter;

Expand Down Expand Up @@ -602,6 +607,7 @@ public MatroskaExtractor(SubtitleParser.Factory subtitleParserFactory, @Flags in
nalLength = new ParsableByteArray(4);
sampleStrippedBytes = new ParsableByteArray();
subtitleSample = new ParsableByteArray();
inflatedSubtitleSample = new ParsableByteArray();
encryptionInitializationVector = new ParsableByteArray(ENCRYPTION_IV_SIZE);
encryptionSubsampleData = new ParsableByteArray();
supplementalData = new ParsableByteArray();
Expand Down Expand Up @@ -869,6 +875,9 @@ protected void startMasterElement(int id, long contentPosition, long contentSize
case ID_CONTENT_ENCODING:
// 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.

break;
case ID_CONTENT_ENCRYPTION:
getCurrentTrack(id).hasContentEncryption = true;
break;
Expand Down Expand Up @@ -1269,8 +1278,8 @@ protected void integerElement(int id, long value) throws ParserException {
}
break;
case ID_CONTENT_COMPRESSION_ALGORITHM:
// 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.

throw ParserException.createForMalformedContainer(
"ContentCompAlgo " + value + " not supported", /* cause= */ null);
}
Expand Down Expand Up @@ -1853,13 +1862,13 @@ private void readScratch(ExtractorInput input, int requiredLength) throws IOExce
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.hasContentCompression);
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.hasContentCompression);
return finishWriteSampleData();
} else if (CODEC_ID_VTT.equals(track.codecId)) {
writeSubtitleSampleData(input, VTT_PREFIX, size);
writeSubtitleSampleData(input, VTT_PREFIX, size, track.hasContentCompression);
return finishWriteSampleData();
}

Expand Down Expand Up @@ -2072,7 +2081,8 @@ private void resetWriteSampleData() {
sampleStrippedBytes.reset(/* limit= */ 0);
}

private void writeSubtitleSampleData(ExtractorInput input, byte[] samplePrefix, int size)
private void writeSubtitleSampleData(
ExtractorInput input, byte[] samplePrefix, int size, boolean hasContentCompression)
throws IOException {
int sizeWithPrefix = samplePrefix.length + size;
if (subtitleSample.capacity() < sizeWithPrefix) {
Expand All @@ -2083,6 +2093,25 @@ private void writeSubtitleSampleData(ExtractorInput input, byte[] samplePrefix,
System.arraycopy(samplePrefix, 0, subtitleSample.getData(), 0, samplePrefix.length);
}
input.readFully(subtitleSample.getData(), samplePrefix.length, size);

if (hasContentCompression) {
if (inflater == null) {
inflater = new Inflater();
}
subtitleSample.setPosition(samplePrefix.length);
subtitleSample.setLimit(sizeWithPrefix);
if (Util.maybeInflate(subtitleSample, inflatedSubtitleSample, inflater)) {
sizeWithPrefix = samplePrefix.length + inflatedSubtitleSample.limit();
subtitleSample.ensureCapacity(sizeWithPrefix);
System.arraycopy(
inflatedSubtitleSample.getData(),
0,
subtitleSample.getData(),
samplePrefix.length,
inflatedSubtitleSample.limit());
}
}

subtitleSample.setPosition(0);
subtitleSample.setLimit(sizeWithPrefix);
// Defer writing the data to the track output. We need to modify the sample data by setting
Expand Down Expand Up @@ -2417,6 +2446,7 @@ protected static final class Track {
public int defaultSampleDurationNs;
public int maxBlockAdditionId;
private int blockAddIdType;
public boolean hasContentCompression;
public boolean hasContentEncryption;
public byte @MonotonicNonNull [] sampleStrippedBytes;
public TrackOutput.@MonotonicNonNull CryptoData cryptoData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ public void mkvSample_withNullTerminatedSubripSubtitles() throws Exception {
simulationConfig);
}

@Test
// TODO: b/507050745 - Suppressed due to failure with subtitlesParsedDuringExtraction=true on SDK
// >= 32.
@Config(maxSdk = 31)
public void mkvSample_withZlibNullTerminatedSubripSubtitles() throws Exception {
ExtractorAsserts.assertBehavior(
getExtractorFactory(subtitlesParsedDuringExtraction),
"media/mkv/sample_with_zlib_null_terminated_srt.mkv",
getAssertionConfigWithPrefix(
"media/mkv/sample_with_zlib_null_terminated_srt.mkv", subtitlesParsedDuringExtraction),
simulationConfig);
}

@Test
// TODO: b/507050745 - Suppressed due to failure with subtitlesParsedDuringExtraction=true on SDK
// >= 32.
Expand Down Expand Up @@ -143,6 +156,19 @@ public void mkvSample_withAssSubtitles() throws Exception {
simulationConfig);
}

@Test
// TODO: b/507050745 - Suppressed due to failure with subtitlesParsedDuringExtraction=true on SDK
// >= 32.
@Config(maxSdk = 31)
public void mkvSample_withZlibAssSubtitles() throws Exception {
ExtractorAsserts.assertBehavior(
getExtractorFactory(subtitlesParsedDuringExtraction),
"media/mkv/sample_with_zlib_ass_subtitles.mkv",
getAssertionConfigWithPrefix(
"media/mkv/sample_with_zlib_ass_subtitles.mkv", subtitlesParsedDuringExtraction),
simulationConfig);
}

// https://github.com/google/ExoPlayer/pull/8265
@Test
// TODO: b/507050745 - Suppressed due to failure with subtitlesParsedDuringExtraction=true on SDK
Expand Down Expand Up @@ -198,6 +224,20 @@ public void mkvSample_withNullTerminatedVttSubtitles() throws Exception {
simulationConfig);
}

@Test
// TODO: b/507050745 - Suppressed due to failure with subtitlesParsedDuringExtraction=true on SDK
// >= 32.
@Config(maxSdk = 31)
public void mkvSample_withZlibNullTerminatedVttSubtitles() throws Exception {
ExtractorAsserts.assertBehavior(
getExtractorFactory(subtitlesParsedDuringExtraction),
"media/mkv/sample_with_zlib_null_terminated_vtt_subtitles.mkv",
getAssertionConfigWithPrefix(
"media/mkv/sample_with_zlib_null_terminated_vtt_subtitles.mkv",
subtitlesParsedDuringExtraction),
simulationConfig);
}

@Test
public void mkvSample_withVorbisAudio() throws Exception {
ExtractorAsserts.assertBehavior(
Expand Down
Loading