package k2;

import androidx.annotation.Nullable;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.q0;
import androidx.media3.extractor.s0;
import androidx.media3.extractor.u0;

/* JADX INFO: loaded from: classes.dex */
public final class g implements f {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final long[] f29045a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final long[] f29046b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final long f29047c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final long f29048d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final long f29049e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final int f29050f;

    public g(long[] jArr, long[] jArr2, long j10, long j11, long j12, int i10) {
        this.f29045a = jArr;
        this.f29046b = jArr2;
        this.f29047c = j10;
        this.f29048d = j11;
        this.f29049e = j12;
        this.f29050f = i10;
    }

    @Nullable
    public static g create(long j10, long j11, q0 q0Var, ParsableByteArray parsableByteArray) {
        long jMax;
        int unsignedByte;
        parsableByteArray.skipBytes(6);
        int i10 = parsableByteArray.readInt();
        long j12 = j11 + ((long) q0Var.f3451b);
        long j13 = ((long) i10) + j12;
        int i11 = parsableByteArray.readInt();
        if (i11 <= 0) {
            return null;
        }
        long jSampleCountToDurationUs = Util.sampleCountToDurationUs((((long) i11) * ((long) q0Var.f3455f)) - 1, q0Var.f3452c);
        int unsignedShort = parsableByteArray.readUnsignedShort();
        int unsignedShort2 = parsableByteArray.readUnsignedShort();
        int unsignedShort3 = parsableByteArray.readUnsignedShort();
        parsableByteArray.skipBytes(2);
        int i12 = unsignedShort2;
        long j14 = j11 + ((long) q0Var.f3451b);
        long[] jArr = new long[unsignedShort];
        long[] jArr2 = new long[unsignedShort];
        int i13 = 0;
        while (i13 < unsignedShort) {
            long j15 = j12;
            long j16 = jSampleCountToDurationUs;
            jArr[i13] = (((long) i13) * jSampleCountToDurationUs) / ((long) unsignedShort);
            jArr2[i13] = j14;
            if (unsignedShort3 == 1) {
                unsignedByte = parsableByteArray.readUnsignedByte();
            } else if (unsignedShort3 == 2) {
                unsignedByte = parsableByteArray.readUnsignedShort();
            } else if (unsignedShort3 == 3) {
                unsignedByte = parsableByteArray.readUnsignedInt24();
            } else {
                if (unsignedShort3 != 4) {
                    return null;
                }
                unsignedByte = parsableByteArray.readUnsignedIntToInt();
            }
            int i14 = i12;
            j14 += ((long) unsignedByte) * ((long) i14);
            i13++;
            jArr = jArr;
            jArr2 = jArr2;
            j12 = j15;
            i12 = i14;
            jSampleCountToDurationUs = j16;
        }
        long j17 = jSampleCountToDurationUs;
        long j18 = j12;
        long[] jArr3 = jArr2;
        long[] jArr4 = jArr;
        if (j10 != -1 && j10 != j13) {
            StringBuilder sbS = defpackage.e.s("VBRI data size mismatch: ", j10, ", ");
            sbS.append(j13);
            Log.w("VbriSeeker", sbS.toString());
        }
        if (j13 != j14) {
            StringBuilder sbS2 = defpackage.e.s("VBRI bytes and ToC mismatch (using max): ", j13, ", ");
            sbS2.append(j14);
            sbS2.append("\nSeeking will be inaccurate.");
            Log.w("VbriSeeker", sbS2.toString());
            jMax = Math.max(j13, j14);
        } else {
            jMax = j13;
        }
        return new g(jArr4, jArr3, j17, j18, jMax, q0Var.f3454e);
    }

    @Override // k2.f
    public final long a() {
        return this.f29049e;
    }

    @Override // k2.f
    public final long b() {
        return this.f29048d;
    }

    @Override // k2.f
    public final int c() {
        return this.f29050f;
    }

    @Override // androidx.media3.extractor.t0
    public final long getDurationUs() {
        return this.f29047c;
    }

    @Override // androidx.media3.extractor.t0
    public final s0 getSeekPoints(long j10) {
        long[] jArr = this.f29045a;
        int iBinarySearchFloor = Util.binarySearchFloor(jArr, j10, true, true);
        long j11 = jArr[iBinarySearchFloor];
        long[] jArr2 = this.f29046b;
        u0 u0Var = new u0(j11, jArr2[iBinarySearchFloor]);
        if (j11 >= j10 || iBinarySearchFloor == jArr.length - 1) {
            return new s0(u0Var, u0Var);
        }
        int i10 = iBinarySearchFloor + 1;
        return new s0(u0Var, new u0(jArr[i10], jArr2[i10]));
    }

    @Override // k2.f
    public final long getTimeUs(long j10) {
        return this.f29045a[Util.binarySearchFloor(this.f29046b, j10, true, true)];
    }

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