package m2;

import androidx.annotation.Nullable;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor;
import androidx.media3.extractor.z;
import java.io.EOFException;
import java.io.IOException;

/* JADX INFO: loaded from: classes.dex */
public final class b implements h {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final g f30291a;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f30295e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f30296f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public long f30297g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public long f30298h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public long f30299i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public long f30300j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public long f30301k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public long f30302l;

    public b(k kVar, long j10, long j11, long j12, long j13, boolean z10) {
        Assertions.checkArgument(j10 >= 0 && j11 > j10);
        this.f30294d = kVar;
        this.f30292b = j10;
        this.f30293c = j11;
        if (j12 == j11 - j10 || z10) {
            this.f30296f = j13;
            this.f30295e = 4;
        } else {
            this.f30295e = 0;
        }
        this.f30291a = new g();
    }

    @Override // m2.h
    public final void a(long j10) {
        this.f30298h = Util.constrainValue(j10, 0L, this.f30296f - 1);
        this.f30295e = 2;
        this.f30299i = this.f30292b;
        this.f30300j = this.f30293c;
        this.f30301k = 0L;
        this.f30302l = this.f30296f;
    }

    /* JADX WARN: Removed duplicated region for block: B:43:0x00cd A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:44:0x00ce  */
    @Override // m2.h
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long read(z zVar) throws IOException {
        g gVar;
        g gVar2;
        long jConstrainValue;
        long j10;
        int i10 = this.f30295e;
        long j11 = this.f30293c;
        g gVar3 = this.f30291a;
        if (i10 == 0) {
            gVar = gVar3;
            long position = zVar.getPosition();
            this.f30297g = position;
            this.f30295e = 1;
            long j12 = j11 - 65307;
            if (j12 > position) {
                return j12;
            }
        } else if (i10 != 1) {
            if (i10 != 2) {
                if (i10 != 3) {
                    if (i10 == 4) {
                        return -1L;
                    }
                    throw new IllegalStateException();
                }
                gVar2 = gVar3;
                j10 = -1;
            } else if (this.f30299i == this.f30300j) {
                gVar2 = gVar3;
                j10 = -1;
                jConstrainValue = -1;
                if (jConstrainValue != j10) {
                    return jConstrainValue;
                }
                this.f30295e = 3;
            } else {
                long position2 = zVar.getPosition();
                if (gVar3.b(zVar, this.f30300j)) {
                    gVar3.a(zVar, false);
                    zVar.j();
                    long j13 = this.f30298h;
                    long j14 = gVar3.f30316b;
                    long j15 = j13 - j14;
                    int i11 = gVar3.f30318d + gVar3.f30319e;
                    if (0 > j15 || j15 >= 72000) {
                        if (j15 < 0) {
                            this.f30300j = position2;
                            this.f30302l = j14;
                        } else {
                            this.f30299i = zVar.getPosition() + ((long) i11);
                            this.f30301k = gVar3.f30316b;
                        }
                        long j16 = this.f30300j;
                        long j17 = this.f30299i;
                        if (j16 - j17 < SilenceSkippingAudioProcessor.DEFAULT_MINIMUM_SILENCE_DURATION_US) {
                            this.f30300j = j17;
                            gVar2 = gVar3;
                            jConstrainValue = j17;
                        } else {
                            long position3 = zVar.getPosition() - (((long) i11) * (j15 <= 0 ? 2L : 1L));
                            long j18 = this.f30300j;
                            long j19 = this.f30299i;
                            gVar2 = gVar3;
                            jConstrainValue = Util.constrainValue((((j18 - j19) * j15) / (this.f30302l - this.f30301k)) + position3, j19, j18 - 1);
                        }
                    }
                    gVar2 = gVar3;
                    j10 = -1;
                    jConstrainValue = -1;
                    if (jConstrainValue != j10) {
                    }
                } else {
                    long j20 = this.f30299i;
                    if (j20 == position2) {
                        throw new IOException("No ogg page can be found.");
                    }
                    jConstrainValue = j20;
                    gVar2 = gVar3;
                }
                j10 = -1;
                if (jConstrainValue != j10) {
                }
            }
            g gVar4 = gVar2;
            while (true) {
                gVar4.b(zVar, j10);
                gVar4.a(zVar, false);
                if (gVar4.f30316b > this.f30298h) {
                    zVar.j();
                    this.f30295e = 4;
                    return -(this.f30301k + 2);
                }
                zVar.k(gVar4.f30318d + gVar4.f30319e);
                this.f30299i = zVar.getPosition();
                this.f30301k = gVar4.f30316b;
                j10 = -1;
            }
        } else {
            gVar = gVar3;
        }
        gVar.f30315a = 0;
        gVar.f30316b = 0L;
        gVar.f30317c = 0;
        gVar.f30318d = 0;
        gVar.f30319e = 0;
        if (!gVar.b(zVar, -1L)) {
            throw new EOFException();
        }
        gVar.a(zVar, false);
        zVar.k(gVar.f30318d + gVar.f30319e);
        long j21 = gVar.f30316b;
        while ((gVar.f30315a & 4) != 4 && gVar.b(zVar, -1L) && zVar.getPosition() < j11 && gVar.a(zVar, true)) {
            try {
                zVar.k(gVar.f30318d + gVar.f30319e);
                j21 = gVar.f30316b;
            } catch (EOFException unused) {
            }
        }
        this.f30296f = j21;
        this.f30295e = 4;
        return this.f30297g;
    }

    @Override // m2.h
    @Nullable
    public a createSeekMap() {
        if (this.f30296f != 0) {
            return new a(this);
        }
        return null;
    }
}
