package androidx.media3.extractor; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import androidx.media3.extractor.SeekMap; import java.io.IOException; /* loaded from: classes2.dex */ public abstract class BinarySearchSeeker { private static final long MAX_SKIP_BYTES = 262144; private final int minimumSearchRange; protected final BinarySearchSeekMap seekMap; protected SeekOperationParams seekOperationParams; protected final TimestampSeeker timestampSeeker; /* loaded from: classes2.dex */ public static final class DefaultSeekTimestampConverter implements SeekTimestampConverter { @Override // androidx.media3.extractor.BinarySearchSeeker.SeekTimestampConverter public long timeUsToTargetTime(long j) { return j; } } /* JADX INFO: Access modifiers changed from: protected */ /* loaded from: classes2.dex */ public interface SeekTimestampConverter { long timeUsToTargetTime(long j); } /* JADX INFO: Access modifiers changed from: protected */ /* loaded from: classes2.dex */ public interface TimestampSeeker { default void onSeekFinished() { } TimestampSearchResult searchForTimestamp(ExtractorInput extractorInput, long j) throws IOException; } public final SeekMap getSeekMap() { return this.seekMap; } public final boolean isSeeking() { return this.seekOperationParams != null; } protected void onSeekOperationFinished(boolean z, long j) { } /* JADX INFO: Access modifiers changed from: protected */ public BinarySearchSeeker(SeekTimestampConverter seekTimestampConverter, TimestampSeeker timestampSeeker, long j, long j2, long j3, long j4, long j5, long j6, int i) { this.timestampSeeker = timestampSeeker; this.minimumSearchRange = i; this.seekMap = new BinarySearchSeekMap(seekTimestampConverter, j, j2, j3, j4, j5, j6); } public final void setSeekTargetUs(long j) { SeekOperationParams seekOperationParams = this.seekOperationParams; if (seekOperationParams == null || seekOperationParams.getSeekTimeUs() != j) { this.seekOperationParams = createSeekParamsForTargetTimeUs(j); } } public int handlePendingSeek(ExtractorInput extractorInput, PositionHolder positionHolder) throws IOException { while (true) { SeekOperationParams seekOperationParams = (SeekOperationParams) Assertions.checkStateNotNull(this.seekOperationParams); long floorBytePosition = seekOperationParams.getFloorBytePosition(); long ceilingBytePosition = seekOperationParams.getCeilingBytePosition(); long nextSearchBytePosition = seekOperationParams.getNextSearchBytePosition(); if (ceilingBytePosition - floorBytePosition <= this.minimumSearchRange) { markSeekOperationFinished(false, floorBytePosition); return seekToPosition(extractorInput, floorBytePosition, positionHolder); } if (!skipInputUntilPosition(extractorInput, nextSearchBytePosition)) { return seekToPosition(extractorInput, nextSearchBytePosition, positionHolder); } extractorInput.resetPeekPosition(); TimestampSearchResult searchForTimestamp = this.timestampSeeker.searchForTimestamp(extractorInput, seekOperationParams.getTargetTimePosition()); int i = searchForTimestamp.type; if (i == -3) { markSeekOperationFinished(false, nextSearchBytePosition); return seekToPosition(extractorInput, nextSearchBytePosition, positionHolder); } if (i == -2) { seekOperationParams.updateSeekFloor(searchForTimestamp.timestampToUpdate, searchForTimestamp.bytePositionToUpdate); } else { if (i != -1) { if (i != 0) { throw new IllegalStateException("Invalid case"); } skipInputUntilPosition(extractorInput, searchForTimestamp.bytePositionToUpdate); markSeekOperationFinished(true, searchForTimestamp.bytePositionToUpdate); return seekToPosition(extractorInput, searchForTimestamp.bytePositionToUpdate, positionHolder); } seekOperationParams.updateSeekCeiling(searchForTimestamp.timestampToUpdate, searchForTimestamp.bytePositionToUpdate); } } } protected SeekOperationParams createSeekParamsForTargetTimeUs(long j) { return new SeekOperationParams(j, this.seekMap.timeUsToTargetTime(j), this.seekMap.floorTimePosition, this.seekMap.ceilingTimePosition, this.seekMap.floorBytePosition, this.seekMap.ceilingBytePosition, this.seekMap.approxBytesPerFrame); } protected final void markSeekOperationFinished(boolean z, long j) { this.seekOperationParams = null; this.timestampSeeker.onSeekFinished(); onSeekOperationFinished(z, j); } protected final boolean skipInputUntilPosition(ExtractorInput extractorInput, long j) throws IOException { long position = j - extractorInput.getPosition(); if (position < 0 || position > MAX_SKIP_BYTES) { return false; } extractorInput.skipFully((int) position); return true; } protected final int seekToPosition(ExtractorInput extractorInput, long j, PositionHolder positionHolder) { if (j == extractorInput.getPosition()) { return 0; } positionHolder.position = j; return 1; } /* JADX INFO: Access modifiers changed from: protected */ /* loaded from: classes2.dex */ public static class SeekOperationParams { private final long approxBytesPerFrame; private long ceilingBytePosition; private long ceilingTimePosition; private long floorBytePosition; private long floorTimePosition; private long nextSearchBytePosition; private final long seekTimeUs; private final long targetTimePosition; /* JADX INFO: Access modifiers changed from: private */ public long getCeilingBytePosition() { return this.ceilingBytePosition; } /* JADX INFO: Access modifiers changed from: private */ public long getFloorBytePosition() { return this.floorBytePosition; } /* JADX INFO: Access modifiers changed from: private */ public long getNextSearchBytePosition() { return this.nextSearchBytePosition; } /* JADX INFO: Access modifiers changed from: private */ public long getSeekTimeUs() { return this.seekTimeUs; } /* JADX INFO: Access modifiers changed from: private */ public long getTargetTimePosition() { return this.targetTimePosition; } protected static long calculateNextSearchBytePosition(long j, long j2, long j3, long j4, long j5, long j6) { if (j4 + 1 >= j5 || j2 + 1 >= j3) { return j4; } long j7 = ((float) (j - j2)) * (((float) (j5 - j4)) / ((float) (j3 - j2))); return Util.constrainValue(((j7 + j4) - j6) - (j7 / 20), j4, j5 - 1); } protected SeekOperationParams(long j, long j2, long j3, long j4, long j5, long j6, long j7) { this.seekTimeUs = j; this.targetTimePosition = j2; this.floorTimePosition = j3; this.ceilingTimePosition = j4; this.floorBytePosition = j5; this.ceilingBytePosition = j6; this.approxBytesPerFrame = j7; this.nextSearchBytePosition = calculateNextSearchBytePosition(j2, j3, j4, j5, j6, j7); } /* JADX INFO: Access modifiers changed from: private */ public void updateSeekFloor(long j, long j2) { this.floorTimePosition = j; this.floorBytePosition = j2; updateNextSearchBytePosition(); } /* JADX INFO: Access modifiers changed from: private */ public void updateSeekCeiling(long j, long j2) { this.ceilingTimePosition = j; this.ceilingBytePosition = j2; updateNextSearchBytePosition(); } private void updateNextSearchBytePosition() { this.nextSearchBytePosition = calculateNextSearchBytePosition(this.targetTimePosition, this.floorTimePosition, this.ceilingTimePosition, this.floorBytePosition, this.ceilingBytePosition, this.approxBytesPerFrame); } } /* loaded from: classes2.dex */ public static final class TimestampSearchResult { public static final TimestampSearchResult NO_TIMESTAMP_IN_RANGE_RESULT = new TimestampSearchResult(-3, -9223372036854775807L, -1); public static final int TYPE_NO_TIMESTAMP = -3; public static final int TYPE_POSITION_OVERESTIMATED = -1; public static final int TYPE_POSITION_UNDERESTIMATED = -2; public static final int TYPE_TARGET_TIMESTAMP_FOUND = 0; private final long bytePositionToUpdate; private final long timestampToUpdate; private final int type; private TimestampSearchResult(int i, long j, long j2) { this.type = i; this.timestampToUpdate = j; this.bytePositionToUpdate = j2; } public static TimestampSearchResult overestimatedResult(long j, long j2) { return new TimestampSearchResult(-1, j, j2); } public static TimestampSearchResult underestimatedResult(long j, long j2) { return new TimestampSearchResult(-2, j, j2); } public static TimestampSearchResult targetFoundResult(long j) { return new TimestampSearchResult(0, -9223372036854775807L, j); } } /* loaded from: classes2.dex */ public static class BinarySearchSeekMap implements SeekMap { private final long approxBytesPerFrame; private final long ceilingBytePosition; private final long ceilingTimePosition; private final long durationUs; private final long floorBytePosition; private final long floorTimePosition; private final SeekTimestampConverter seekTimestampConverter; @Override // androidx.media3.extractor.SeekMap public long getDurationUs() { return this.durationUs; } @Override // androidx.media3.extractor.SeekMap public boolean isSeekable() { return true; } public BinarySearchSeekMap(SeekTimestampConverter seekTimestampConverter, long j, long j2, long j3, long j4, long j5, long j6) { this.seekTimestampConverter = seekTimestampConverter; this.durationUs = j; this.floorTimePosition = j2; this.ceilingTimePosition = j3; this.floorBytePosition = j4; this.ceilingBytePosition = j5; this.approxBytesPerFrame = j6; } @Override // androidx.media3.extractor.SeekMap public SeekMap.SeekPoints getSeekPoints(long j) { return new SeekMap.SeekPoints(new SeekPoint(j, SeekOperationParams.calculateNextSearchBytePosition(this.seekTimestampConverter.timeUsToTargetTime(j), this.floorTimePosition, this.ceilingTimePosition, this.floorBytePosition, this.ceilingBytePosition, this.approxBytesPerFrame))); } public long timeUsToTargetTime(long j) { return this.seekTimestampConverter.timeUsToTargetTime(j); } } }