package androidx.media3.extractor.ts; import androidx.media3.common.util.ParsableByteArray; import androidx.media3.common.util.TimestampAdjuster; import androidx.media3.common.util.Util; import androidx.media3.extractor.BinarySearchSeeker; import androidx.media3.extractor.ExtractorInput; import java.io.IOException; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public final class PsBinarySearchSeeker extends BinarySearchSeeker { private static final int MINIMUM_SEARCH_RANGE_BYTES = 1000; private static final long SEEK_TOLERANCE_US = 100000; private static final int TIMESTAMP_SEARCH_BYTES = 20000; public PsBinarySearchSeeker(TimestampAdjuster timestampAdjuster, long j, long j2) { super(new BinarySearchSeeker.DefaultSeekTimestampConverter(), new PsScrSeeker(timestampAdjuster), j, 0L, j + 1, 0L, j2, 188L, 1000); } /* loaded from: classes2.dex */ private static final class PsScrSeeker implements BinarySearchSeeker.TimestampSeeker { private final ParsableByteArray packetBuffer; private final TimestampAdjuster scrTimestampAdjuster; private PsScrSeeker(TimestampAdjuster timestampAdjuster) { this.scrTimestampAdjuster = timestampAdjuster; this.packetBuffer = new ParsableByteArray(); } @Override // androidx.media3.extractor.BinarySearchSeeker.TimestampSeeker public BinarySearchSeeker.TimestampSearchResult searchForTimestamp(ExtractorInput extractorInput, long j) throws IOException { long position = extractorInput.getPosition(); int min = (int) Math.min(20000L, extractorInput.getLength() - position); this.packetBuffer.reset(min); extractorInput.peekFully(this.packetBuffer.getData(), 0, min); return searchForScrValueInBuffer(this.packetBuffer, j, position); } @Override // androidx.media3.extractor.BinarySearchSeeker.TimestampSeeker public void onSeekFinished() { this.packetBuffer.reset(Util.EMPTY_BYTE_ARRAY); } private BinarySearchSeeker.TimestampSearchResult searchForScrValueInBuffer(ParsableByteArray parsableByteArray, long j, long j2) { int i = -1; int i2 = -1; long j3 = -9223372036854775807L; while (parsableByteArray.bytesLeft() >= 4) { if (PsBinarySearchSeeker.peekIntAtPosition(parsableByteArray.getData(), parsableByteArray.getPosition()) != 442) { parsableByteArray.skipBytes(1); } else { parsableByteArray.skipBytes(4); long readScrValueFromPack = PsDurationReader.readScrValueFromPack(parsableByteArray); if (readScrValueFromPack != -9223372036854775807L) { long adjustTsTimestamp = this.scrTimestampAdjuster.adjustTsTimestamp(readScrValueFromPack); if (adjustTsTimestamp > j) { if (j3 == -9223372036854775807L) { return BinarySearchSeeker.TimestampSearchResult.overestimatedResult(adjustTsTimestamp, j2); } return BinarySearchSeeker.TimestampSearchResult.targetFoundResult(j2 + i2); } if (PsBinarySearchSeeker.SEEK_TOLERANCE_US + adjustTsTimestamp > j) { return BinarySearchSeeker.TimestampSearchResult.targetFoundResult(j2 + parsableByteArray.getPosition()); } i2 = parsableByteArray.getPosition(); j3 = adjustTsTimestamp; } skipToEndOfCurrentPack(parsableByteArray); i = parsableByteArray.getPosition(); } } if (j3 != -9223372036854775807L) { return BinarySearchSeeker.TimestampSearchResult.underestimatedResult(j3, j2 + i); } return BinarySearchSeeker.TimestampSearchResult.NO_TIMESTAMP_IN_RANGE_RESULT; } private static void skipToEndOfCurrentPack(ParsableByteArray parsableByteArray) { int peekIntAtPosition; int limit = parsableByteArray.limit(); if (parsableByteArray.bytesLeft() < 10) { parsableByteArray.setPosition(limit); return; } parsableByteArray.skipBytes(9); int readUnsignedByte = parsableByteArray.readUnsignedByte() & 7; if (parsableByteArray.bytesLeft() < readUnsignedByte) { parsableByteArray.setPosition(limit); return; } parsableByteArray.skipBytes(readUnsignedByte); if (parsableByteArray.bytesLeft() >= 4) { if (PsBinarySearchSeeker.peekIntAtPosition(parsableByteArray.getData(), parsableByteArray.getPosition()) == 443) { parsableByteArray.skipBytes(4); int readUnsignedShort = parsableByteArray.readUnsignedShort(); if (parsableByteArray.bytesLeft() < readUnsignedShort) { parsableByteArray.setPosition(limit); return; } parsableByteArray.skipBytes(readUnsignedShort); } while (parsableByteArray.bytesLeft() >= 4 && (peekIntAtPosition = PsBinarySearchSeeker.peekIntAtPosition(parsableByteArray.getData(), parsableByteArray.getPosition())) != 442 && peekIntAtPosition != 441 && (peekIntAtPosition >>> 8) == 1) { parsableByteArray.skipBytes(4); if (parsableByteArray.bytesLeft() < 2) { parsableByteArray.setPosition(limit); return; } parsableByteArray.setPosition(Math.min(parsableByteArray.limit(), parsableByteArray.getPosition() + parsableByteArray.readUnsignedShort())); } return; } parsableByteArray.setPosition(limit); } } /* JADX INFO: Access modifiers changed from: private */ public static int peekIntAtPosition(byte[] bArr, int i) { return (bArr[i + 3] & 255) | ((bArr[i] & 255) << 24) | ((bArr[i + 1] & 255) << 16) | ((bArr[i + 2] & 255) << 8); } }