Use Util.maybeInflate()

This commit is contained in:
Sven Wischnowsky 2025-01-03 20:55:59 +01:00 committed by Ian Baker
parent 3cd0e1ad3d
commit f52d98eafd

View File

@ -49,8 +49,6 @@ public final class PgsParser implements SubtitleParser {
private static final int SECTION_TYPE_IDENTIFIER = 0x16; private static final int SECTION_TYPE_IDENTIFIER = 0x16;
private static final int SECTION_TYPE_END = 0x80; private static final int SECTION_TYPE_END = 0x80;
private static final byte INFLATE_HEADER = 0x78;
private final ParsableByteArray buffer; private final ParsableByteArray buffer;
private final ParsableByteArray inflatedBuffer; private final ParsableByteArray inflatedBuffer;
private final CueBuilder cueBuilder; private final CueBuilder cueBuilder;
@ -76,7 +74,12 @@ public final class PgsParser implements SubtitleParser {
Consumer<CuesWithTiming> output) { Consumer<CuesWithTiming> output) {
buffer.reset(data, /* limit= */ offset + length); buffer.reset(data, /* limit= */ offset + length);
buffer.setPosition(offset); buffer.setPosition(offset);
maybeInflateData(buffer); if (inflater == null) {
inflater = new Inflater();
}
if (Util.maybeInflate(buffer, inflatedBuffer, inflater)) {
buffer.reset(inflatedBuffer.getData(), inflatedBuffer.limit());
}
cueBuilder.reset(); cueBuilder.reset();
ArrayList<Cue> cues = new ArrayList<>(); ArrayList<Cue> cues = new ArrayList<>();
while (buffer.bytesLeft() >= 3) { while (buffer.bytesLeft() >= 3) {
@ -89,17 +92,6 @@ public final class PgsParser implements SubtitleParser {
new CuesWithTiming(cues, /* startTimeUs= */ C.TIME_UNSET, /* durationUs= */ C.TIME_UNSET)); new CuesWithTiming(cues, /* startTimeUs= */ C.TIME_UNSET, /* durationUs= */ C.TIME_UNSET));
} }
private void maybeInflateData(ParsableByteArray buffer) {
if (buffer.bytesLeft() > 0 && buffer.peekUnsignedByte() == INFLATE_HEADER) {
if (inflater == null) {
inflater = new Inflater();
}
if (Util.inflate(buffer, inflatedBuffer, inflater)) {
buffer.reset(inflatedBuffer.getData(), inflatedBuffer.limit());
} // else assume data is not compressed.
}
}
@Nullable @Nullable
private static Cue readNextSection(ParsableByteArray buffer, CueBuilder cueBuilder) { private static Cue readNextSection(ParsableByteArray buffer, CueBuilder cueBuilder) {
int limit = buffer.limit(); int limit = buffer.limit();