package com.google.android.gms.internal.ads;

import android.util.SparseArray;
import androidx.annotation.Nullable;
import java.io.EOFException;
import java.util.Objects;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* JADX INFO: loaded from: classes2.dex */
public final class jq2 implements u3 {

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

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

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

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

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public boolean f12988r;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public boolean f12992v;

    @Nullable
    private final dn2 zzd;

    @Nullable
    private final an2 zze;

    @Nullable
    private iq2 zzf;

    @Nullable
    private zn2 zzg;

    @Nullable
    private ym2 zzh;

    @Nullable
    private zn2 zzz;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final eq2 f12972b = new eq2();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f12974d = 1000;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long[] f12975e = new long[1000];

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long[] f12976f = new long[1000];

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public long[] f12979i = new long[1000];

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int[] f12978h = new int[1000];

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int[] f12977g = new int[1000];

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public t3[] f12980j = new t3[1000];

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final bp f12973c = new bp(0);

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public long f12985o = Long.MIN_VALUE;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public long f12986p = Long.MIN_VALUE;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public long f12987q = Long.MIN_VALUE;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public boolean f12990t = true;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public boolean f12989s = true;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public boolean f12991u = true;

    public jq2(n nVar, @Nullable dn2 dn2Var, @Nullable an2 an2Var) {
        this.zzd = dn2Var;
        this.zze = an2Var;
        this.f12971a = new dq2(nVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:26:0x0099 A[Catch: all -> 0x0022, TRY_LEAVE, TryCatch #0 {all -> 0x0022, blocks: (B:3:0x0001, B:5:0x0007, B:9:0x001e, B:12:0x0025, B:16:0x002d, B:21:0x007e, B:23:0x0083, B:24:0x0090, B:26:0x0099, B:19:0x0064), top: B:33:0x0001 }] */
    /* JADX WARN: Removed duplicated region for block: B:29:0x0104 A[DONT_GENERATE] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final synchronized void zzG(long j10, int i10, long j11, int i11, @Nullable t3 t3Var) {
        int i12;
        int i13;
        try {
            int i14 = this.f12981k;
            if (i14 > 0) {
                int iJ = j(i14 - 1);
                ih1.x(this.f12976f[iJ] + ((long) this.f12977g[iJ]) <= j11);
            }
            this.f12988r = (536870912 & i10) != 0;
            this.f12987q = Math.max(this.f12987q, j10);
            int iJ2 = j(this.f12981k);
            this.f12979i[iJ2] = j10;
            this.f12976f[iJ2] = j11;
            this.f12977g[iJ2] = i11;
            this.f12978h[iJ2] = i10;
            this.f12980j[iJ2] = t3Var;
            this.f12975e[iJ2] = 0;
            bp bpVar = this.f12973c;
            if (((SparseArray) bpVar.f9618b).size() != 0) {
                if (!((gq2) ((SparseArray) bpVar.f9618b).valueAt(r10.size() - 1)).f11844a.equals(this.zzz)) {
                }
                i12 = this.f12981k + 1;
                this.f12981k = i12;
                i13 = this.f12974d;
                if (i12 != i13) {
                    int i15 = i13 + 1000;
                    long[] jArr = new long[i15];
                    long[] jArr2 = new long[i15];
                    long[] jArr3 = new long[i15];
                    int[] iArr = new int[i15];
                    int[] iArr2 = new int[i15];
                    t3[] t3VarArr = new t3[i15];
                    int i16 = this.f12983m;
                    int i17 = i13 - i16;
                    System.arraycopy(this.f12976f, i16, jArr2, 0, i17);
                    System.arraycopy(this.f12979i, this.f12983m, jArr3, 0, i17);
                    System.arraycopy(this.f12978h, this.f12983m, iArr, 0, i17);
                    System.arraycopy(this.f12977g, this.f12983m, iArr2, 0, i17);
                    System.arraycopy(this.f12980j, this.f12983m, t3VarArr, 0, i17);
                    System.arraycopy(this.f12975e, this.f12983m, jArr, 0, i17);
                    int i18 = this.f12983m;
                    System.arraycopy(this.f12976f, 0, jArr2, i17, i18);
                    System.arraycopy(this.f12979i, 0, jArr3, i17, i18);
                    System.arraycopy(this.f12978h, 0, iArr, i17, i18);
                    System.arraycopy(this.f12977g, 0, iArr2, i17, i18);
                    System.arraycopy(this.f12980j, 0, t3VarArr, i17, i18);
                    System.arraycopy(this.f12975e, 0, jArr, i17, i18);
                    this.f12976f = jArr2;
                    this.f12979i = jArr3;
                    this.f12978h = iArr;
                    this.f12977g = iArr2;
                    this.f12980j = t3VarArr;
                    this.f12975e = jArr;
                    this.f12983m = 0;
                    this.f12974d = i15;
                    return;
                }
                return;
            }
            zn2 zn2Var = this.zzz;
            zn2Var.getClass();
            bpVar.e(this.f12982l + this.f12981k, new gq2(zn2Var));
            i12 = this.f12981k + 1;
            this.f12981k = i12;
            i13 = this.f12974d;
            if (i12 != i13) {
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // com.google.android.gms.internal.ads.u3
    public final void b(p01 p01Var, int i10, int i11) {
        while (true) {
            dq2 dq2Var = this.f12971a;
            if (i10 <= 0) {
                dq2Var.getClass();
                return;
            }
            int iB = dq2Var.b(i10);
            cq2 cq2Var = dq2Var.f10486e;
            l lVar = cq2Var.zzc;
            byte[] bArr = lVar.f13441a;
            long j10 = dq2Var.f10487f - cq2Var.f10054a;
            lVar.getClass();
            p01Var.F((int) j10, iB, bArr);
            i10 -= iB;
            long j11 = dq2Var.f10487f + ((long) iB);
            dq2Var.f10487f = j11;
            cq2 cq2Var2 = dq2Var.f10486e;
            if (j11 == cq2Var2.f10055b) {
                dq2Var.f10486e = cq2Var2.zzd;
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.u3
    public final int c(eh2 eh2Var, int i10, boolean z10) throws EOFException {
        dq2 dq2Var = this.f12971a;
        int iB = dq2Var.b(i10);
        cq2 cq2Var = dq2Var.f10486e;
        l lVar = cq2Var.zzc;
        byte[] bArr = lVar.f13441a;
        long j10 = dq2Var.f10487f - cq2Var.f10054a;
        lVar.getClass();
        int iD = eh2Var.d(bArr, (int) j10, iB);
        if (iD == -1) {
            if (z10) {
                return -1;
            }
            throw new EOFException();
        }
        long j11 = dq2Var.f10487f + ((long) iD);
        dq2Var.f10487f = j11;
        cq2 cq2Var2 = dq2Var.f10486e;
        if (j11 != cq2Var2.f10055b) {
            return iD;
        }
        dq2Var.f10486e = cq2Var2.zzd;
        return iD;
    }

    @Override // com.google.android.gms.internal.ads.u3
    public final void d(zn2 zn2Var) {
        boolean z10;
        synchronized (this) {
            z10 = false;
            try {
                this.f12990t = false;
                if (!Objects.equals(zn2Var, this.zzz)) {
                    bp bpVar = this.f12973c;
                    if (((SparseArray) bpVar.f9618b).size() != 0) {
                        if (((gq2) ((SparseArray) bpVar.f9618b).valueAt(r2.size() - 1)).f11844a.equals(zn2Var)) {
                            this.zzz = ((gq2) ((SparseArray) bpVar.f9618b).valueAt(r5.size() - 1)).f11844a;
                        }
                        boolean z11 = this.f12991u;
                        zn2 zn2Var2 = this.zzz;
                        this.f12991u = z11 & hc.zzd(zn2Var2.zzo, zn2Var2.zzk);
                        this.f12992v = false;
                        z10 = true;
                    }
                    this.zzz = zn2Var;
                    boolean z112 = this.f12991u;
                    zn2 zn2Var22 = this.zzz;
                    this.f12991u = z112 & hc.zzd(zn2Var22.zzo, zn2Var22.zzk);
                    this.f12992v = false;
                    z10 = true;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        iq2 iq2Var = this.zzf;
        if (iq2Var == null || !z10) {
            return;
        }
        yp2 yp2Var = (yp2) iq2Var;
        yp2Var.R.post(yp2Var.f18955m);
    }

    public final void f() {
        if (this.zzh != null) {
            this.zzh = null;
            this.zzg = null;
        }
    }

    public final void g(zn2 zn2Var, di2 di2Var) {
        zn2 zn2Var2 = this.zzg;
        uk2 uk2Var = zn2Var2 == null ? null : zn2Var2.zzs;
        this.zzg = zn2Var;
        uk2 uk2Var2 = zn2Var.zzs;
        cn2 cn2Var = (cn2) this.zzd;
        cn2Var.getClass();
        int i10 = zn2Var.zzs != null ? 1 : 0;
        sm2 sm2Var = new sm2(zn2Var);
        sm2Var.f16503z = i10;
        di2Var.zzb = new zn2(sm2Var);
        di2Var.zza = this.zzh;
        if (zn2Var2 == null || !Objects.equals(uk2Var, uk2Var2)) {
            ym2 ym2VarZza = cn2Var.zza(this.zze, zn2Var);
            this.zzh = ym2VarZza;
            di2Var.zza = ym2VarZza;
        }
    }

    public final int h(int i10, int i11, long j10, boolean z10) {
        int i12 = -1;
        for (int i13 = 0; i13 < i11; i13++) {
            long j11 = this.f12979i[i10];
            if (j11 > j10) {
                break;
            }
            if (!z10 || (this.f12978h[i10] & 1) != 0) {
                i12 = i13;
                if (j11 == j10) {
                    break;
                }
            }
            i10++;
            if (i10 == this.f12974d) {
                i10 = 0;
            }
        }
        return i12;
    }

    public final long i(int i10) {
        long j10 = this.f12986p;
        long jMax = Long.MIN_VALUE;
        int i11 = 0;
        if (i10 != 0) {
            int iJ = j(i10 - 1);
            for (int i12 = 0; i12 < i10; i12++) {
                jMax = Math.max(jMax, this.f12979i[iJ]);
                if ((this.f12978h[iJ] & 1) != 0) {
                    break;
                }
                iJ--;
                if (iJ == -1) {
                    iJ = this.f12974d - 1;
                }
            }
        }
        this.f12986p = Math.max(j10, jMax);
        this.f12981k -= i10;
        int i13 = this.f12982l + i10;
        this.f12982l = i13;
        int i14 = this.f12983m + i10;
        this.f12983m = i14;
        int i15 = this.f12974d;
        if (i14 >= i15) {
            this.f12983m = i14 - i15;
        }
        int i16 = this.f12984n - i10;
        this.f12984n = i16;
        if (i16 < 0) {
            this.f12984n = 0;
        }
        bp bpVar = this.f12973c;
        while (true) {
            SparseArray sparseArray = (SparseArray) bpVar.f9618b;
            if (i11 >= sparseArray.size() - 1) {
                break;
            }
            int i17 = i11 + 1;
            if (i13 < sparseArray.keyAt(i17)) {
                break;
            }
            ((co0) bpVar.f9619c).mo2zza(sparseArray.valueAt(i11));
            sparseArray.removeAt(i11);
            int i18 = bpVar.f9617a;
            if (i18 > 0) {
                bpVar.f9617a = i18 - 1;
            }
            i11 = i17;
        }
        if (this.f12981k != 0) {
            return this.f12976f[this.f12983m];
        }
        int i19 = this.f12983m;
        if (i19 == 0) {
            i19 = this.f12974d;
        }
        int i20 = i19 - 1;
        return this.f12976f[i20] + ((long) this.f12977g[i20]);
    }

    public final int j(int i10) {
        int i11 = this.f12983m + i10;
        int i12 = this.f12974d;
        return i11 < i12 ? i11 : i11 - i12;
    }

    public final void k(boolean z10) {
        SparseArray sparseArray;
        dq2 dq2Var = this.f12971a;
        cq2 cq2Var = dq2Var.f10484c;
        l lVar = cq2Var.zzc;
        n nVar = dq2Var.f10482a;
        if (lVar != null) {
            nVar.zzc(cq2Var);
            cq2Var.zzc = null;
            cq2Var.zzd = null;
        }
        cq2 cq2Var2 = dq2Var.f10484c;
        int i10 = 0;
        ih1.b1(cq2Var2.zzc == null);
        cq2Var2.f10054a = 0L;
        cq2Var2.f10055b = IjkMediaMeta.AV_CH_TOP_BACK_CENTER;
        cq2 cq2Var3 = dq2Var.f10484c;
        dq2Var.f10485d = cq2Var3;
        dq2Var.f10486e = cq2Var3;
        dq2Var.f10487f = 0L;
        nVar.zzd();
        this.f12981k = 0;
        this.f12982l = 0;
        this.f12983m = 0;
        this.f12984n = 0;
        this.f12989s = true;
        this.f12985o = Long.MIN_VALUE;
        this.f12986p = Long.MIN_VALUE;
        this.f12987q = Long.MIN_VALUE;
        this.f12988r = false;
        bp bpVar = this.f12973c;
        while (true) {
            sparseArray = (SparseArray) bpVar.f9618b;
            if (i10 >= sparseArray.size()) {
                break;
            }
            ((co0) bpVar.f9619c).mo2zza(sparseArray.valueAt(i10));
            i10++;
        }
        bpVar.f9617a = -1;
        sparseArray.clear();
        if (z10) {
            this.zzz = null;
            this.f12990t = true;
            this.f12991u = true;
        }
    }

    public final void l() throws zztg {
        ym2 ym2Var = this.zzh;
        if (ym2Var != null) {
            throw ((en2) ym2Var).zza();
        }
    }

    public final synchronized boolean m(boolean z10) {
        int i10 = this.f12984n;
        boolean z11 = false;
        if (i10 != this.f12981k) {
            if (((gq2) this.f12973c.b(this.f12982l + i10)).f11844a != this.zzg) {
                return true;
            }
            int iJ = j(this.f12984n);
            if (this.zzh != null) {
                int i11 = this.f12978h[iJ] & 1073741824;
            } else {
                z11 = true;
            }
            return z11;
        }
        if (z10 || this.f12988r) {
            z11 = true;
        } else {
            zn2 zn2Var = this.zzz;
            if (zn2Var != null) {
                if (zn2Var == this.zzg) {
                    return false;
                }
                z11 = true;
            }
        }
        return z11;
    }

    public final int n(di2 di2Var, fe2 fe2Var, int i10, boolean z10) {
        int i11;
        boolean z11 = (i10 & 2) != 0;
        eq2 eq2Var = this.f12972b;
        synchronized (this) {
            try {
                fe2Var.f11189d = false;
                int i12 = this.f12984n;
                i11 = -3;
                if (i12 != this.f12981k) {
                    zn2 zn2Var = ((gq2) this.f12973c.b(this.f12982l + i12)).f11844a;
                    if (!z11 && zn2Var == this.zzg) {
                        int iJ = j(this.f12984n);
                        if (this.zzh != null) {
                            int i13 = this.f12978h[iJ];
                            fe2Var.f11189d = true;
                        } else {
                            int i14 = this.f12978h[iJ];
                            fe2Var.f32097b = i14;
                            if (this.f12984n == this.f12981k - 1 && (z10 || this.f12988r)) {
                                fe2Var.f32097b = 536870912 | i14;
                            }
                            fe2Var.f11190e = this.f12979i[iJ];
                            eq2Var.f10900a = this.f12977g[iJ];
                            eq2Var.f10901b = this.f12976f[iJ];
                            eq2Var.zzc = this.f12980j[iJ];
                            i11 = -4;
                        }
                    }
                    g(zn2Var, di2Var);
                    i11 = -5;
                } else {
                    if (!z10 && !this.f12988r) {
                        zn2 zn2Var2 = this.zzz;
                        if (zn2Var2 == null || (!z11 && zn2Var2 == this.zzg)) {
                        }
                        g(zn2Var2, di2Var);
                        i11 = -5;
                    }
                    fe2Var.f32097b = 4;
                    fe2Var.f11190e = Long.MIN_VALUE;
                    i11 = -4;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        if (i11 != -4) {
            return i11;
        }
        if (fe2Var.g(4)) {
            return -4;
        }
        int i15 = i10 & 1;
        if ((i10 & 4) == 0) {
            if (i15 != 0) {
                dq2 dq2Var = this.f12971a;
                dq2.c(dq2Var.f10485d, fe2Var, eq2Var, dq2Var.f10483b);
                return -4;
            }
            dq2 dq2Var2 = this.f12971a;
            dq2Var2.f10485d = dq2.c(dq2Var2.f10485d, fe2Var, eq2Var, dq2Var2.f10483b);
        } else if (i15 != 0) {
            return -4;
        }
        this.f12984n++;
        return -4;
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x0032  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x0050 A[Catch: all -> 0x0047, TryCatch #1 {all -> 0x0047, blocks: (B:3:0x0001, B:6:0x000b, B:7:0x000c, B:12:0x001c, B:14:0x0024, B:18:0x002d, B:22:0x0035, B:24:0x003d, B:27:0x0044, B:37:0x005a, B:34:0x0050, B:43:0x0066, B:44:0x0067, B:5:0x0003), top: B:49:0x0001, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:36:0x0059  */
    /* JADX WARN: Removed duplicated region for block: B:37:0x005a A[Catch: all -> 0x0047, TRY_LEAVE, TryCatch #1 {all -> 0x0047, blocks: (B:3:0x0001, B:6:0x000b, B:7:0x000c, B:12:0x001c, B:14:0x0024, B:18:0x002d, B:22:0x0035, B:24:0x003d, B:27:0x0044, B:37:0x005a, B:34:0x0050, B:43:0x0066, B:44:0x0067, B:5:0x0003), top: B:49:0x0001, inners: #0 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized boolean o(long j10, boolean z10) {
        try {
            synchronized (this) {
                this.f12984n = 0;
                dq2 dq2Var = this.f12971a;
                dq2Var.f10485d = dq2Var.f10484c;
            }
        } catch (Throwable th) {
            throw th;
        }
        int iJ = j(0);
        int i10 = this.f12984n;
        int iH = this.f12981k;
        if ((i10 != iH) && j10 >= this.f12979i[iJ]) {
            if (j10 <= this.f12987q) {
                if (this.f12991u) {
                    iH = h(iJ, iH, j10, true);
                } else {
                    int i11 = 0;
                    while (true) {
                        if (i11 < iH) {
                            if (this.f12979i[iJ] >= j10) {
                                iH = i11;
                                break;
                            }
                            iJ++;
                            if (iJ == this.f12974d) {
                                iJ = 0;
                            }
                            i11++;
                        } else if (!z10) {
                            iH = -1;
                        }
                    }
                }
                if (iH == -1) {
                    this.f12985o = j10;
                    this.f12984n += iH;
                    return true;
                }
            } else if (z10) {
                z10 = true;
                if (this.f12991u) {
                }
                if (iH == -1) {
                }
            }
        }
        return false;
    }

    public final void p() {
        long jI;
        dq2 dq2Var = this.f12971a;
        synchronized (this) {
            int i10 = this.f12981k;
            jI = i10 == 0 ? -1L : i(i10);
        }
        dq2Var.a(jI);
    }

    @Override // com.google.android.gms.internal.ads.u3
    public final void zze(long j10, int i10, int i11, int i12, @Nullable t3 t3Var) {
        if (this.f12989s) {
            if ((i10 & 1) == 0) {
                return;
            } else {
                this.f12989s = false;
            }
        }
        if (this.f12991u) {
            if (j10 < this.f12985o) {
                return;
            }
            if ((i10 & 1) == 0) {
                if (!this.f12992v) {
                    vt0.c("Overriding unexpected non-sync sample for format: ".concat(String.valueOf(this.zzz)));
                    this.f12992v = true;
                }
                i10 |= 1;
            }
        }
        int i13 = i10;
        zzG(j10, i13, (this.f12971a.f10487f - ((long) i11)) - ((long) i12), i11, t3Var);
    }

    @Nullable
    public final synchronized zn2 zzn() {
        if (this.f12990t) {
            return null;
        }
        return this.zzz;
    }

    public final void zzy(@Nullable iq2 iq2Var) {
        this.zzf = iq2Var;
    }
}
