package androidx.media3.extractor;

import androidx.media3.common.C;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.SeekMap;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
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;

    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;

        public BinarySearchSeekMap(SeekTimestampConverter seekTimestampConverter, long j4, long j6, long j7, long j8, long j9, long j10) {
            this.seekTimestampConverter = seekTimestampConverter;
            this.durationUs = j4;
            this.floorTimePosition = j6;
            this.ceilingTimePosition = j7;
            this.floorBytePosition = j8;
            this.ceilingBytePosition = j9;
            this.approxBytesPerFrame = j10;
        }

        @Override // androidx.media3.extractor.SeekMap
        public long getDurationUs() {
            return this.durationUs;
        }

        @Override // androidx.media3.extractor.SeekMap
        public SeekMap.SeekPoints getSeekPoints(long j4) {
            return new SeekMap.SeekPoints(new SeekPoint(j4, SeekOperationParams.calculateNextSearchBytePosition(this.seekTimestampConverter.timeUsToTargetTime(j4), this.floorTimePosition, this.ceilingTimePosition, this.floorBytePosition, this.ceilingBytePosition, this.approxBytesPerFrame)));
        }

        @Override // androidx.media3.extractor.SeekMap
        public boolean isSeekable() {
            return true;
        }

        public long timeUsToTargetTime(long j4) {
            return this.seekTimestampConverter.timeUsToTargetTime(j4);
        }
    }

    public static final class DefaultSeekTimestampConverter implements SeekTimestampConverter {
        @Override // androidx.media3.extractor.BinarySearchSeeker.SeekTimestampConverter
        public long timeUsToTargetTime(long j4) {
            return j4;
        }
    }

    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;

        public SeekOperationParams(long j4, long j6, long j7, long j8, long j9, long j10, long j11) {
            this.seekTimeUs = j4;
            this.targetTimePosition = j6;
            this.floorTimePosition = j7;
            this.ceilingTimePosition = j8;
            this.floorBytePosition = j9;
            this.ceilingBytePosition = j10;
            this.approxBytesPerFrame = j11;
            this.nextSearchBytePosition = calculateNextSearchBytePosition(j6, j7, j8, j9, j10, j11);
        }

        public static long calculateNextSearchBytePosition(long j4, long j6, long j7, long j8, long j9, long j10) {
            if (j8 + 1 >= j9 || j6 + 1 >= j7) {
                return j8;
            }
            long j11 = (long) ((j4 - j6) * ((j9 - j8) / (j7 - j6)));
            return Util.constrainValue(((j11 + j8) - j10) - (j11 / 20), j8, j9 - 1);
        }

        /* 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;
        }

        private void updateNextSearchBytePosition() {
            this.nextSearchBytePosition = calculateNextSearchBytePosition(this.targetTimePosition, this.floorTimePosition, this.ceilingTimePosition, this.floorBytePosition, this.ceilingBytePosition, this.approxBytesPerFrame);
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void updateSeekCeiling(long j4, long j6) {
            this.ceilingTimePosition = j4;
            this.ceilingBytePosition = j6;
            updateNextSearchBytePosition();
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void updateSeekFloor(long j4, long j6) {
            this.floorTimePosition = j4;
            this.floorBytePosition = j6;
            updateNextSearchBytePosition();
        }
    }

    public interface SeekTimestampConverter {
        long timeUsToTargetTime(long j4);
    }

    public static final class TimestampSearchResult {
        public static final TimestampSearchResult NO_TIMESTAMP_IN_RANGE_RESULT = new TimestampSearchResult(-3, C.TIME_UNSET, -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 i6, long j4, long j6) {
            this.type = i6;
            this.timestampToUpdate = j4;
            this.bytePositionToUpdate = j6;
        }

        public static TimestampSearchResult overestimatedResult(long j4, long j6) {
            return new TimestampSearchResult(-1, j4, j6);
        }

        public static TimestampSearchResult targetFoundResult(long j4) {
            return new TimestampSearchResult(0, C.TIME_UNSET, j4);
        }

        public static TimestampSearchResult underestimatedResult(long j4, long j6) {
            return new TimestampSearchResult(-2, j4, j6);
        }
    }

    public interface TimestampSeeker {
        default void onSeekFinished() {
        }

        TimestampSearchResult searchForTimestamp(ExtractorInput extractorInput, long j4);
    }

    public BinarySearchSeeker(SeekTimestampConverter seekTimestampConverter, TimestampSeeker timestampSeeker, long j4, long j6, long j7, long j8, long j9, long j10, int i6) {
        this.timestampSeeker = timestampSeeker;
        this.minimumSearchRange = i6;
        this.seekMap = new BinarySearchSeekMap(seekTimestampConverter, j4, j6, j7, j8, j9, j10);
    }

    public SeekOperationParams createSeekParamsForTargetTimeUs(long j4) {
        return new SeekOperationParams(j4, this.seekMap.timeUsToTargetTime(j4), this.seekMap.floorTimePosition, this.seekMap.ceilingTimePosition, this.seekMap.floorBytePosition, this.seekMap.ceilingBytePosition, this.seekMap.approxBytesPerFrame);
    }

    public final SeekMap getSeekMap() {
        return this.seekMap;
    }

    public int handlePendingSeek(ExtractorInput extractorInput, PositionHolder positionHolder) {
        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 timestampSearchResultSearchForTimestamp = this.timestampSeeker.searchForTimestamp(extractorInput, seekOperationParams.getTargetTimePosition());
            int i6 = timestampSearchResultSearchForTimestamp.type;
            if (i6 == -3) {
                markSeekOperationFinished(false, nextSearchBytePosition);
                return seekToPosition(extractorInput, nextSearchBytePosition, positionHolder);
            }
            if (i6 == -2) {
                seekOperationParams.updateSeekFloor(timestampSearchResultSearchForTimestamp.timestampToUpdate, timestampSearchResultSearchForTimestamp.bytePositionToUpdate);
            } else {
                if (i6 != -1) {
                    if (i6 != 0) {
                        throw new IllegalStateException("Invalid case");
                    }
                    skipInputUntilPosition(extractorInput, timestampSearchResultSearchForTimestamp.bytePositionToUpdate);
                    markSeekOperationFinished(true, timestampSearchResultSearchForTimestamp.bytePositionToUpdate);
                    return seekToPosition(extractorInput, timestampSearchResultSearchForTimestamp.bytePositionToUpdate, positionHolder);
                }
                seekOperationParams.updateSeekCeiling(timestampSearchResultSearchForTimestamp.timestampToUpdate, timestampSearchResultSearchForTimestamp.bytePositionToUpdate);
            }
        }
    }

    public final boolean isSeeking() {
        return this.seekOperationParams != null;
    }

    public final void markSeekOperationFinished(boolean z6, long j4) {
        this.seekOperationParams = null;
        this.timestampSeeker.onSeekFinished();
        onSeekOperationFinished(z6, j4);
    }

    public void onSeekOperationFinished(boolean z6, long j4) {
    }

    public final int seekToPosition(ExtractorInput extractorInput, long j4, PositionHolder positionHolder) {
        if (j4 == extractorInput.getPosition()) {
            return 0;
        }
        positionHolder.position = j4;
        return 1;
    }

    public final void setSeekTargetUs(long j4) {
        SeekOperationParams seekOperationParams = this.seekOperationParams;
        if (seekOperationParams == null || seekOperationParams.getSeekTimeUs() != j4) {
            this.seekOperationParams = createSeekParamsForTargetTimeUs(j4);
        }
    }

    public final boolean skipInputUntilPosition(ExtractorInput extractorInput, long j4) {
        long position = j4 - extractorInput.getPosition();
        if (position < 0 || position > MAX_SKIP_BYTES) {
            return false;
        }
        extractorInput.skipFully((int) position);
        return true;
    }
}
