package F0;

import A1.C0000a;
import A1.C0002c;
import A1.C0003d;
import A1.C0005f;
import A1.J;
import C3.E;
import C3.G;
import C3.W;
import android.net.Uri;
import android.text.TextUtils;
import d3.C;
import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import n1.C1256h;
import q1.InterfaceC1454k;
import u0.C1575k;
import u0.C1578n;
import u0.C1579o;
import u0.D;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class j extends M0.b {

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    public static final AtomicInteger f2295L = new AtomicInteger();

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public final boolean f2296A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public final boolean f2297B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public b f2298C;
    public q D;

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

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

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public volatile boolean f2301G;
    public boolean H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public W f2302I;

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

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

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

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Uri f2307m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final boolean f2308n;

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

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final z0.h f2310p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final z0.k f2311q;

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final boolean f2313s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final boolean f2314t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final x0.t f2315u;

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final List f2317w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final C1575k f2318x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public final h1.h f2319y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public final x0.o f2320z;

    public j(c cVar, z0.h hVar, z0.k kVar, C1579o c1579o, boolean z8, z0.h hVar2, z0.k kVar2, boolean z9, Uri uri, List list, int i, Object obj, long j8, long j9, long j10, int i5, boolean z10, int i8, boolean z11, boolean z12, x0.t tVar, C1575k c1575k, b bVar, h1.h hVar3, x0.o oVar, boolean z13, C0.l lVar) {
        super(hVar, kVar, c1579o, i, obj, j8, j9, j10);
        this.f2296A = z8;
        this.f2309o = i5;
        this.f2304K = z10;
        this.f2306l = i8;
        this.f2311q = kVar2;
        this.f2310p = hVar2;
        this.f2300F = kVar2 != null;
        this.f2297B = z9;
        this.f2307m = uri;
        this.f2313s = z12;
        this.f2315u = tVar;
        this.f2314t = z11;
        this.f2316v = cVar;
        this.f2317w = list;
        this.f2318x = c1575k;
        this.f2312r = bVar;
        this.f2319y = hVar3;
        this.f2320z = oVar;
        this.f2308n = z13;
        E e8 = G.f1197r;
        this.f2302I = W.f1225u;
        this.f2305k = f2295L.getAndIncrement();
    }

    public static byte[] d(String str) {
        if (s7.d.F(str).startsWith("0x")) {
            str = str.substring(2);
        }
        byte[] byteArray = new BigInteger(str, 16).toByteArray();
        byte[] bArr = new byte[16];
        int length = byteArray.length > 16 ? byteArray.length - 16 : 0;
        System.arraycopy(byteArray, length, bArr, (16 - byteArray.length) + length, byteArray.length - length);
        return bArr;
    }

    @Override // P0.j
    public final void a() {
        b bVar;
        this.D.getClass();
        if (this.f2298C == null && (bVar = this.f2312r) != null) {
            T0.m mVarC = bVar.f2257a.c();
            if ((mVarC instanceof J) || (mVarC instanceof C1256h)) {
                this.f2298C = this.f2312r;
                this.f2300F = false;
            }
        }
        if (this.f2300F) {
            z0.h hVar = this.f2310p;
            hVar.getClass();
            z0.k kVar = this.f2311q;
            kVar.getClass();
            c(hVar, kVar, this.f2297B, false);
            this.f2299E = 0;
            this.f2300F = false;
        }
        if (this.f2301G) {
            return;
        }
        if (!this.f2314t) {
            c(this.i, this.f4141b, this.f2296A, true);
        }
        this.H = !this.f2301G;
    }

    @Override // P0.j
    public final void b() {
        this.f2301G = true;
    }

    /* JADX WARN: Removed duplicated region for block: B:21:0x004a A[Catch: all -> 0x0050, TRY_LEAVE, TryCatch #0 {all -> 0x0050, blocks: (B:19:0x0044, B:21:0x004a, B:34:0x0067, B:35:0x006b, B:45:0x0089, B:46:0x0091, B:40:0x007f, B:25:0x0052, B:27:0x0056, B:37:0x0070, B:39:0x0078, B:44:0x0088), top: B:49:0x0044, inners: #1, #2 }] */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0056 A[Catch: all -> 0x0063, EOFException -> 0x0065, TRY_LEAVE, TryCatch #1 {all -> 0x0063, blocks: (B:25:0x0052, B:27:0x0056, B:37:0x0070, B:39:0x0078, B:44:0x0088), top: B:50:0x0052, outer: #0 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c(z0.h hVar, z0.k kVar, boolean z8, boolean z9) {
        z0.k kVar2;
        long j8;
        long j9;
        boolean z10 = false;
        try {
            if (!z8) {
                long j10 = this.f2299E;
                long j11 = kVar.f19143f;
                long j12 = j11 != -1 ? j11 - j10 : -1L;
                if (j10 != 0 || j11 != j12) {
                    kVar2 = new z0.k(kVar.f19138a, kVar.f19139b, kVar.f19140c, kVar.f19141d, kVar.f19142e + j10, j12, kVar.f19144g);
                }
                T0.j jVarF = f(hVar, kVar2, z9);
                if (z10) {
                    jVarF.b(this.f2299E);
                }
                while (!this.f2301G) {
                    try {
                        try {
                            if (this.f2298C.f2257a.e(jVarF, b.f2256f) != 0) {
                                break;
                            }
                        } catch (Throwable th) {
                            this.f2299E = (int) (jVarF.f6428t - kVar.f19142e);
                            throw th;
                        }
                    } catch (EOFException e8) {
                        if ((this.f4143d.f17351f & 16384) == 0) {
                            throw e8;
                        }
                        this.f2298C.f2257a.i(0L, 0L);
                        j8 = jVarF.f6428t;
                        j9 = kVar.f19142e;
                    }
                }
                j8 = jVarF.f6428t;
                j9 = kVar.f19142e;
                this.f2299E = (int) (j8 - j9);
                return;
            }
            if (this.f2299E != 0) {
                z10 = true;
            }
            T0.j jVarF2 = f(hVar, kVar2, z9);
            if (z10) {
            }
            while (!this.f2301G) {
            }
            j8 = jVarF2.f6428t;
            j9 = kVar.f19142e;
            this.f2299E = (int) (j8 - j9);
            return;
        } finally {
            W3.b.k(hVar);
        }
        kVar2 = kVar;
    }

    public final int e(int i) {
        AbstractC1709a.j(!this.f2308n);
        W w4 = this.f2302I;
        if (i >= w4.f1227t) {
            return 0;
        }
        return ((Integer) w4.get(i)).intValue();
    }

    /* JADX WARN: Removed duplicated region for block: B:126:0x02a0  */
    /* JADX WARN: Removed duplicated region for block: B:127:0x02a2  */
    /* JADX WARN: Removed duplicated region for block: B:129:0x02a5  */
    /* JADX WARN: Removed duplicated region for block: B:130:0x02aa  */
    /* JADX WARN: Removed duplicated region for block: B:133:0x02b0  */
    /* JADX WARN: Removed duplicated region for block: B:134:0x02b3  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final T0.j f(z0.h hVar, z0.k kVar, boolean z8) throws IOException {
        int i;
        long j8;
        long jN;
        long j9;
        b bVar;
        ArrayList arrayList;
        T0.m c0000a;
        boolean zF;
        C c3;
        boolean z9;
        boolean z10;
        InterfaceC1454k interfaceC1454k;
        List listSingletonList;
        int i5;
        T0.m dVar;
        long jG = hVar.g(kVar);
        long j10 = this.f4146g;
        x0.t tVar = this.f2315u;
        if (z8) {
            try {
                tVar.h(j10, this.f2313s);
            } catch (InterruptedException unused) {
                throw new InterruptedIOException();
            } catch (TimeoutException e8) {
                throw new IOException(e8);
            }
        }
        T0.j jVar = new T0.j(hVar, kVar.f19142e, jG);
        if (this.f2298C == null) {
            x0.o oVar = this.f2320z;
            jVar.f6430v = 0;
            try {
                oVar.C(10);
                jVar.f(oVar.f18539a, 0, 10, false);
            } catch (EOFException unused2) {
                j8 = -9223372036854775807L;
            }
            if (oVar.w() != 4801587) {
                jN = -9223372036854775807L;
                j8 = -9223372036854775807L;
            } else {
                oVar.G(3);
                int iS = oVar.s();
                int i8 = iS + 10;
                byte[] bArr = oVar.f18539a;
                j8 = -9223372036854775807L;
                if (i8 > bArr.length) {
                    oVar.C(i8);
                    System.arraycopy(bArr, 0, oVar.f18539a, 0, 10);
                }
                jVar.f(oVar.f18539a, 10, iS, false);
                D dC = this.f2319y.C(iS, oVar.f18539a);
                if (dC == null) {
                    jN = j8;
                } else {
                    for (u0.C c8 : dC.f17150a) {
                        if (c8 instanceof h1.m) {
                            h1.m mVar = (h1.m) c8;
                            if ("com.apple.streaming.transportStreamTimestamp".equals(mVar.f13348b)) {
                                System.arraycopy(mVar.f13349c, 0, oVar.f18539a, 0, 8);
                                oVar.F(0);
                                oVar.E(8);
                                jN = oVar.n() & 8589934591L;
                                break;
                            }
                        }
                    }
                    jN = j8;
                }
            }
            jVar.f6430v = 0;
            b bVar2 = this.f2312r;
            if (bVar2 == null) {
                Map mapM = hVar.m();
                c cVar = this.f2316v;
                cVar.getClass();
                C1579o c1579o = this.f4143d;
                int iX = W2.g.x(c1579o.f17358n);
                List list = (List) mapM.get("Content-Type");
                int iX2 = W2.g.x((list == null || list.isEmpty()) ? null : (String) list.get(0));
                int iY = W2.g.y(kVar.f19138a);
                ArrayList arrayList2 = new ArrayList(7);
                c.a(iX, arrayList2);
                c.a(iX2, arrayList2);
                c.a(iY, arrayList2);
                int[] iArr = c.f2262c;
                int i9 = 0;
                for (int i10 = 7; i9 < i10; i10 = 7) {
                    c.a(iArr[i9], arrayList2);
                    i9++;
                }
                jVar.f6430v = 0;
                int i11 = 0;
                T0.m mVar2 = null;
                while (true) {
                    int size = arrayList2.size();
                    x0.t tVar2 = this.f2315u;
                    if (i11 >= size) {
                        j9 = j10;
                        i = 0;
                        mVar2.getClass();
                        bVar = new b(mVar2, c1579o, tVar2, cVar.f2263a, cVar.f2264b);
                        break;
                    }
                    int iIntValue = ((Integer) arrayList2.get(i11)).intValue();
                    int i12 = i11;
                    if (iIntValue == 0) {
                        j9 = j10;
                        arrayList = arrayList2;
                        c0000a = new C0000a();
                    } else if (iIntValue == 1) {
                        j9 = j10;
                        arrayList = arrayList2;
                        c0000a = new C0002c();
                    } else if (iIntValue == 2) {
                        j9 = j10;
                        arrayList = arrayList2;
                        c0000a = new C0003d();
                    } else if (iIntValue != 7) {
                        d3.D d8 = InterfaceC1454k.f16220p;
                        List list2 = this.f2317w;
                        if (iIntValue == 8) {
                            j9 = j10;
                            arrayList = arrayList2;
                            C c9 = cVar.f2263a;
                            boolean z11 = cVar.f2264b;
                            D d9 = c1579o.f17356l;
                            if (d9 == null) {
                                c3 = c9;
                                z9 = z11;
                            } else {
                                c3 = c9;
                                int i13 = 0;
                                while (true) {
                                    u0.C[] cArr = d9.f17150a;
                                    z9 = z11;
                                    if (i13 >= cArr.length) {
                                        break;
                                    }
                                    u0.C c10 = cArr[i13];
                                    if (c10 instanceof s) {
                                        z10 = !((s) c10).f2422c.isEmpty();
                                        break;
                                    }
                                    i13++;
                                    z11 = z9;
                                }
                                int i14 = !z10 ? 4 : 0;
                                if (z9) {
                                    i14 |= 32;
                                    interfaceC1454k = d8;
                                } else {
                                    interfaceC1454k = c3;
                                }
                                c0000a = new C1256h(interfaceC1454k, i14, tVar2, list2 == null ? list2 : W.f1225u);
                            }
                            z10 = false;
                            if (!z10) {
                            }
                            if (z9) {
                            }
                            c0000a = new C1256h(interfaceC1454k, i14, tVar2, list2 == null ? list2 : W.f1225u);
                        } else if (iIntValue == 11) {
                            j9 = j10;
                            arrayList = arrayList2;
                            C c11 = cVar.f2263a;
                            boolean z12 = cVar.f2264b;
                            if (list2 != null) {
                                i5 = 48;
                                listSingletonList = list2;
                            } else {
                                C1578n c1578n = new C1578n();
                                c1578n.f17320m = u0.E.m("application/cea-608");
                                listSingletonList = Collections.singletonList(new C1579o(c1578n));
                                i5 = 16;
                            }
                            String str = c1579o.f17355k;
                            if (!TextUtils.isEmpty(str)) {
                                if (u0.E.b(str, "audio/mp4a-latm") == null) {
                                    i5 |= 2;
                                }
                                if (u0.E.b(str, "video/avc") == null) {
                                    i5 |= 4;
                                }
                            }
                            J j11 = new J(2, !z12 ? 1 : 0, !z12 ? d8 : c11, tVar2, new C0005f(i5, listSingletonList));
                            tVar2 = tVar2;
                            c0000a = j11;
                        } else if (iIntValue != 13) {
                            j9 = j10;
                            arrayList = arrayList2;
                            c0000a = null;
                        } else {
                            j9 = j10;
                            arrayList = arrayList2;
                            c0000a = new u(c1579o.f17349d, tVar2, cVar.f2263a, cVar.f2264b);
                        }
                    } else {
                        j9 = j10;
                        arrayList = arrayList2;
                        c0000a = new m1.d(0L);
                    }
                    c0000a.getClass();
                    try {
                        zF = c0000a.f(jVar);
                        i = 0;
                        jVar.f6430v = 0;
                    } catch (EOFException unused3) {
                        i = 0;
                        jVar.f6430v = 0;
                        zF = false;
                    } catch (Throwable th) {
                        jVar.f6430v = 0;
                        throw th;
                    }
                    if (zF) {
                        bVar = new b(c0000a, c1579o, tVar2, cVar.f2263a, cVar.f2264b);
                        break;
                    }
                    C1579o c1579o2 = c1579o;
                    if (mVar2 == null && (iIntValue == iX || iIntValue == iX2 || iIntValue == iY || iIntValue == 11)) {
                        mVar2 = c0000a;
                    }
                    i11 = i12 + 1;
                    c1579o = c1579o2;
                    arrayList2 = arrayList;
                    j10 = j9;
                }
            } else {
                T0.m mVar3 = bVar2.f2257a;
                T0.m mVarC = mVar3.c();
                AbstractC1709a.j(!((mVarC instanceof J) || (mVarC instanceof C1256h)));
                AbstractC1709a.i("Can't recreate wrapped extractors. Outer type: " + mVar3.getClass(), mVar3.c() == mVar3);
                if (mVar3 instanceof u) {
                    dVar = new u(bVar2.f2258b.f17349d, bVar2.f2259c, bVar2.f2260d, bVar2.f2261e);
                } else if (mVar3 instanceof C0003d) {
                    dVar = new C0003d();
                } else if (mVar3 instanceof C0000a) {
                    dVar = new C0000a();
                } else if (mVar3 instanceof C0002c) {
                    dVar = new C0002c();
                } else {
                    if (!(mVar3 instanceof m1.d)) {
                        throw new IllegalStateException("Unexpected extractor type for recreation: ".concat(mVar3.getClass().getSimpleName()));
                    }
                    dVar = new m1.d();
                }
                bVar = new b(dVar, bVar2.f2258b, bVar2.f2259c, bVar2.f2260d, bVar2.f2261e);
                j9 = j10;
                i = 0;
            }
            b bVar3 = bVar;
            this.f2298C = bVar3;
            T0.m mVarC2 = bVar3.f2257a.c();
            if ((((mVarC2 instanceof C0003d) || (mVarC2 instanceof C0000a) || (mVarC2 instanceof C0002c) || (mVarC2 instanceof m1.d)) ? 1 : i) != 0) {
                q qVar = this.D;
                long jB = jN != j8 ? tVar.b(jN) : j9;
                if (qVar.f2401l0 != jB) {
                    qVar.f2401l0 = jB;
                    p[] pVarArr = qVar.f2376L;
                    int length = pVarArr.length;
                    for (int i15 = i; i15 < length; i15++) {
                        p pVar = pVarArr[i15];
                        if (pVar.f3767F != jB) {
                            pVar.f3767F = jB;
                            pVar.f3793z = true;
                        }
                    }
                }
            } else {
                q qVar2 = this.D;
                if (qVar2.f2401l0 != 0) {
                    qVar2.f2401l0 = 0L;
                    p[] pVarArr2 = qVar2.f2376L;
                    int length2 = pVarArr2.length;
                    for (int i16 = i; i16 < length2; i16++) {
                        p pVar2 = pVarArr2[i16];
                        if (pVar2.f3767F != 0) {
                            pVar2.f3767F = 0L;
                            pVar2.f3793z = true;
                        }
                    }
                }
            }
            this.D.f2378N.clear();
            this.f2298C.f2257a.l(this.D);
        } else {
            i = 0;
        }
        q qVar3 = this.D;
        C1575k c1575k = qVar3.f2402m0;
        C1575k c1575k2 = this.f2318x;
        if (!Objects.equals(c1575k, c1575k2)) {
            qVar3.f2402m0 = c1575k2;
            while (true) {
                p[] pVarArr3 = qVar3.f2376L;
                if (i >= pVarArr3.length) {
                    break;
                }
                if (qVar3.f2394e0[i]) {
                    p pVar3 = pVarArr3[i];
                    pVar3.f2365I = c1575k2;
                    pVar3.f3793z = true;
                }
                i++;
            }
        }
        return jVar;
    }
}
