package androidx.media3.extractor.mp3;

import android.util.Pair;
import androidx.media3.common.C;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.SeekMap;
import androidx.media3.extractor.SeekPoint;
import androidx.media3.extractor.metadata.id3.MlltFrame;

/* JADX INFO: loaded from: classes.dex */
final class MlltSeeker implements Seeker {
    private final long durationUs;
    private final long[] referencePositions;
    private final long[] referenceTimesMs;

    private MlltSeeker(long[] jArr, long[] jArr2, long j4) {
        this.referencePositions = jArr;
        this.referenceTimesMs = jArr2;
        this.durationUs = j4 == C.TIME_UNSET ? Util.msToUs(jArr2[jArr2.length - 1]) : j4;
    }

    public static MlltSeeker create(long j4, MlltFrame mlltFrame, long j6) {
        int length = mlltFrame.bytesDeviations.length;
        int i6 = length + 1;
        long[] jArr = new long[i6];
        long[] jArr2 = new long[i6];
        jArr[0] = j4;
        long j7 = 0;
        jArr2[0] = 0;
        for (int i7 = 1; i7 <= length; i7++) {
            int i8 = i7 - 1;
            j4 += (long) (mlltFrame.bytesBetweenReference + mlltFrame.bytesDeviations[i8]);
            j7 += (long) (mlltFrame.millisecondsBetweenReference + mlltFrame.millisecondsDeviations[i8]);
            jArr[i7] = j4;
            jArr2[i7] = j7;
        }
        return new MlltSeeker(jArr, jArr2, j6);
    }

    private static Pair<Long, Long> linearlyInterpolate(long j4, long[] jArr, long[] jArr2) {
        int iBinarySearchFloor = Util.binarySearchFloor(jArr, j4, true, true);
        long j6 = jArr[iBinarySearchFloor];
        long j7 = jArr2[iBinarySearchFloor];
        int i6 = iBinarySearchFloor + 1;
        if (i6 == jArr.length) {
            return Pair.create(Long.valueOf(j6), Long.valueOf(j7));
        }
        return Pair.create(Long.valueOf(j4), Long.valueOf(((long) ((jArr[i6] == j6 ? 0.0d : (j4 - j6) / (r6 - j6)) * (jArr2[i6] - j7))) + j7));
    }

    @Override // androidx.media3.extractor.mp3.Seeker
    public int getAverageBitrate() {
        return C.RATE_UNSET_INT;
    }

    @Override // androidx.media3.extractor.mp3.Seeker
    public long getDataEndPosition() {
        return -1L;
    }

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

    @Override // androidx.media3.extractor.SeekMap
    public SeekMap.SeekPoints getSeekPoints(long j4) {
        Pair<Long, Long> pairLinearlyInterpolate = linearlyInterpolate(Util.usToMs(Util.constrainValue(j4, 0L, this.durationUs)), this.referenceTimesMs, this.referencePositions);
        return new SeekMap.SeekPoints(new SeekPoint(Util.msToUs(((Long) pairLinearlyInterpolate.first).longValue()), ((Long) pairLinearlyInterpolate.second).longValue()));
    }

    @Override // androidx.media3.extractor.mp3.Seeker
    public long getTimeUs(long j4) {
        return Util.msToUs(((Long) linearlyInterpolate(j4, this.referencePositions, this.referenceTimesMs).second).longValue());
    }

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