package androidx.media3.extractor.ogg; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import androidx.media3.extractor.ExtractorInput; import androidx.media3.extractor.ExtractorUtil; import androidx.media3.extractor.SeekMap; import androidx.media3.extractor.SeekPoint; import java.io.EOFException; import java.io.IOException; import java.math.BigInteger; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public final class DefaultOggSeeker implements OggSeeker { private static final int DEFAULT_OFFSET = 30000; private static final int MATCH_BYTE_RANGE = 100000; private static final int MATCH_RANGE = 72000; private static final int STATE_IDLE = 4; private static final int STATE_READ_LAST_PAGE = 1; private static final int STATE_SEEK = 2; private static final int STATE_SEEK_TO_END = 0; private static final int STATE_SKIP = 3; private long end; private long endGranule; private final OggPageHeader pageHeader; private final long payloadEndPosition; private final long payloadStartPosition; private long positionBeforeSeekToEnd; private long start; private long startGranule; private int state; private final StreamReader streamReader; private long targetGranule; private long totalGranules; public DefaultOggSeeker(StreamReader streamReader, long j, long j2, long j3, long j4, boolean z) { Assertions.checkArgument(j >= 0 && j2 > j); this.streamReader = streamReader; this.payloadStartPosition = j; this.payloadEndPosition = j2; if (j3 == j2 - j || z) { this.totalGranules = j4; this.state = 4; } else { this.state = 0; } this.pageHeader = new OggPageHeader(); } @Override // androidx.media3.extractor.ogg.OggSeeker public long read(ExtractorInput extractorInput) throws IOException { int i = this.state; if (i == 0) { long position = extractorInput.getPosition(); this.positionBeforeSeekToEnd = position; this.state = 1; long j = this.payloadEndPosition - 65307; if (j > position) { return j; } } else if (i != 1) { if (i == 2) { long nextSeekPosition = getNextSeekPosition(extractorInput); if (nextSeekPosition != -1) { return nextSeekPosition; } this.state = 3; } else if (i != 3) { if (i == 4) { return -1L; } throw new IllegalStateException(); } skipToPageOfTargetGranule(extractorInput); this.state = 4; return -(this.startGranule + 2); } this.totalGranules = readGranuleOfLastPage(extractorInput); this.state = 4; return this.positionBeforeSeekToEnd; } @Override // androidx.media3.extractor.ogg.OggSeeker public OggSeekMap createSeekMap() { if (this.totalGranules != 0) { return new OggSeekMap(); } return null; } @Override // androidx.media3.extractor.ogg.OggSeeker public void startSeek(long j) { this.targetGranule = Util.constrainValue(j, 0L, this.totalGranules - 1); this.state = 2; this.start = this.payloadStartPosition; this.end = this.payloadEndPosition; this.startGranule = 0L; this.endGranule = this.totalGranules; } private long getNextSeekPosition(ExtractorInput extractorInput) throws IOException { if (this.start == this.end) { return -1L; } long position = extractorInput.getPosition(); if (!this.pageHeader.skipToNextPage(extractorInput, this.end)) { long j = this.start; if (j != position) { return j; } throw new IOException("No ogg page can be found."); } this.pageHeader.populate(extractorInput, false); extractorInput.resetPeekPosition(); long j2 = this.targetGranule - this.pageHeader.granulePosition; int i = this.pageHeader.headerSize + this.pageHeader.bodySize; if (0 <= j2 && j2 < 72000) { return -1L; } if (j2 < 0) { this.end = position; this.endGranule = this.pageHeader.granulePosition; } else { this.start = extractorInput.getPosition() + i; this.startGranule = this.pageHeader.granulePosition; } long j3 = this.end; long j4 = this.start; if (j3 - j4 < 100000) { this.end = j4; return j4; } long j5 = i; long j6 = j2 <= 0 ? 2L : 1L; long position2 = extractorInput.getPosition(); long j7 = this.end; long j8 = this.start; return Util.constrainValue((position2 - (j5 * j6)) + ((j2 * (j7 - j8)) / (this.endGranule - this.startGranule)), j8, j7 - 1); } private void skipToPageOfTargetGranule(ExtractorInput extractorInput) throws IOException { while (true) { this.pageHeader.skipToNextPage(extractorInput); this.pageHeader.populate(extractorInput, false); if (this.pageHeader.granulePosition <= this.targetGranule) { extractorInput.skipFully(this.pageHeader.headerSize + this.pageHeader.bodySize); this.start = extractorInput.getPosition(); this.startGranule = this.pageHeader.granulePosition; } else { extractorInput.resetPeekPosition(); return; } } } long readGranuleOfLastPage(ExtractorInput extractorInput) throws IOException { this.pageHeader.reset(); if (!this.pageHeader.skipToNextPage(extractorInput)) { throw new EOFException(); } this.pageHeader.populate(extractorInput, false); extractorInput.skipFully(this.pageHeader.headerSize + this.pageHeader.bodySize); long j = this.pageHeader.granulePosition; while ((this.pageHeader.type & 4) != 4 && this.pageHeader.skipToNextPage(extractorInput) && extractorInput.getPosition() < this.payloadEndPosition && this.pageHeader.populate(extractorInput, true) && ExtractorUtil.skipFullyQuietly(extractorInput, this.pageHeader.headerSize + this.pageHeader.bodySize)) { j = this.pageHeader.granulePosition; } return j; } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public final class OggSeekMap implements SeekMap { @Override // androidx.media3.extractor.SeekMap public boolean isSeekable() { return true; } private OggSeekMap() { } @Override // androidx.media3.extractor.SeekMap public SeekMap.SeekPoints getSeekPoints(long j) { return new SeekMap.SeekPoints(new SeekPoint(j, Util.constrainValue((DefaultOggSeeker.this.payloadStartPosition + BigInteger.valueOf(DefaultOggSeeker.this.streamReader.convertTimeToGranule(j)).multiply(BigInteger.valueOf(DefaultOggSeeker.this.payloadEndPosition - DefaultOggSeeker.this.payloadStartPosition)).divide(BigInteger.valueOf(DefaultOggSeeker.this.totalGranules)).longValue()) - 30000, DefaultOggSeeker.this.payloadStartPosition, DefaultOggSeeker.this.payloadEndPosition - 1))); } @Override // androidx.media3.extractor.SeekMap public long getDurationUs() { return DefaultOggSeeker.this.streamReader.convertGranuleToTime(DefaultOggSeeker.this.totalGranules); } } }