Repository navigation
Add support for Matroska zlib compressed subtitle tracks #3211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
||
| // The chapter corresponding to the current EditionEntry element, or null. | ||
| @Nullable private ChapterEntry currentChapter; | ||
|
|
||
|
|
@@ -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(); | ||
|
|
@@ -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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably also the case for |
||
| break; | ||
| case ID_CONTENT_ENCRYPTION: | ||
| getCurrentTrack(id).hasContentEncryption = true; | ||
| break; | ||
|
|
@@ -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)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In EBML/Matroska,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't validate it in |
||
| throw ParserException.createForMalformedContainer( | ||
| "ContentCompAlgo " + value + " not supported", /* cause= */ null); | ||
| } | ||
|
|
@@ -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(); | ||
| } | ||
|
|
||
|
|
@@ -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) { | ||
|
|
@@ -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 | ||
|
|
@@ -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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inflaterholds native zlib resources (z_stream), butrelease()is still a no-op here. Callingif (inflater != null) { inflater.end(); inflater = null; }inrelease()avoids leaking native memory until GC finalization.There was a problem hiding this comment.
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
Inflateris 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.