package N0;

import A0.h;
import B0.AbstractC0020g;
import B0.F;
import B0.I;
import C3.G;
import C3.W;
import L0.c0;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;
import d3.C;
import d3.D;
import d3.R0;
import d5.AbstractC0844a;
import e4.C0878i;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Objects;
import q1.C1444a;
import q1.C1446c;
import q1.C1449f;
import q1.C1453j;
import q1.InterfaceC1448e;
import q1.InterfaceC1456m;
import r1.C1487c;
import r1.C1490f;
import u0.C1579o;
import u0.E;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class e extends AbstractC0020g implements Handler.Callback {
    public final D H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public final h f4377I;

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

    /* JADX INFO: renamed from: K, reason: collision with root package name */
    public final K4.c f4379K;

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

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

    /* JADX INFO: renamed from: N, reason: collision with root package name */
    public InterfaceC1448e f4382N;

    /* JADX INFO: renamed from: O, reason: collision with root package name */
    public C1453j f4383O;

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    public C1446c f4384P;

    /* JADX INFO: renamed from: Q, reason: collision with root package name */
    public C1446c f4385Q;

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

    /* JADX INFO: renamed from: S, reason: collision with root package name */
    public final Handler f4387S;

    /* JADX INFO: renamed from: T, reason: collision with root package name */
    public final F f4388T;

    /* JADX INFO: renamed from: U, reason: collision with root package name */
    public final R0 f4389U;

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

    /* JADX INFO: renamed from: W, reason: collision with root package name */
    public boolean f4391W;
    public C1579o X;

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

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

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public IOException f4394a0;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public e(F f3, Looper looper) {
        super(3);
        K4.c cVar = d.f4376d;
        this.f4388T = f3;
        this.f4387S = looper == null ? null : new Handler(looper, this);
        this.f4379K = cVar;
        this.H = new D(12);
        this.f4377I = new h(1);
        this.f4389U = new R0(7, false);
        this.f4393Z = -9223372036854775807L;
        this.f4392Y = -9223372036854775807L;
    }

    public final void A() {
        AbstractC1709a.i("Legacy decoding is disabled, can't handle " + this.X.f17358n + " samples (expected application/x-media3-cues).", Objects.equals(this.X.f17358n, "application/cea-608") || Objects.equals(this.X.f17358n, "application/x-mp4-cea-608") || Objects.equals(this.X.f17358n, "application/cea-708"));
    }

    public final long B() {
        if (this.f4386R == -1) {
            return Long.MAX_VALUE;
        }
        this.f4384P.getClass();
        if (this.f4386R >= this.f4384P.k()) {
            return Long.MAX_VALUE;
        }
        return this.f4384P.i(this.f4386R);
    }

    public final long C(long j8) {
        AbstractC1709a.j(j8 != -9223372036854775807L);
        return j8 - this.f750A;
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x004d  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void D() {
        InterfaceC1448e bVar;
        int i;
        this.f4380L = true;
        C1579o c1579o = this.X;
        c1579o.getClass();
        K4.c cVar = this.f4379K;
        cVar.getClass();
        String str = c1579o.f17358n;
        if (str != null) {
            i = c1579o.f17340I;
            switch (str) {
                case "application/x-mp4-cea-608":
                case "application/cea-608":
                    bVar = new C1487c(i, str);
                    break;
                case "application/cea-708":
                    bVar = new C1490f(i, c1579o.f17361q);
                    break;
                default:
                    C c3 = (C) cVar.f3604r;
                    if (!c3.l(c1579o)) {
                        throw new IllegalArgumentException(AbstractC0844a.x("Attempted to create decoder for unsupported MIME type: ", str));
                    }
                    InterfaceC1456m interfaceC1456mG = c3.g(c1579o);
                    interfaceC1456mG.getClass().getSimpleName().concat("Decoder");
                    bVar = new H0.b(interfaceC1456mG);
                    break;
                    break;
            }
        }
        this.f4382N = bVar;
        bVar.f(this.f751B);
    }

    public final void E(w0.c cVar) {
        G g8 = cVar.f18214a;
        F f3 = this.f4388T;
        f3.f523q.f532C.e(27, new A1.G(6, g8));
        I i = f3.f523q;
        i.q0 = cVar;
        i.f532C.e(27, new A1.G(3, cVar));
    }

    public final void F() {
        this.f4383O = null;
        this.f4386R = -1;
        C1446c c1446c = this.f4384P;
        if (c1446c != null) {
            c1446c.g();
            this.f4384P = null;
        }
        C1446c c1446c2 = this.f4385Q;
        if (c1446c2 != null) {
            c1446c2.g();
            this.f4385Q = null;
        }
    }

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        if (message.what != 1) {
            throw new IllegalStateException();
        }
        E((w0.c) message.obj);
        return true;
    }

    @Override // B0.AbstractC0020g
    public final String i() {
        return "TextRenderer";
    }

    @Override // B0.AbstractC0020g
    public final boolean k() {
        return this.f4391W;
    }

    @Override // B0.AbstractC0020g
    public final boolean l() {
        if (this.X != null) {
            if (this.f4394a0 == null) {
                try {
                    c0 c0Var = this.f764y;
                    c0Var.getClass();
                    c0Var.l();
                } catch (IOException e8) {
                    this.f4394a0 = e8;
                }
            }
            if (this.f4394a0 != null) {
                C1579o c1579o = this.X;
                c1579o.getClass();
                if (Objects.equals(c1579o.f17358n, "application/x-media3-cues")) {
                    a aVar = this.f4378J;
                    aVar.getClass();
                    return aVar.d(this.f4392Y) != Long.MIN_VALUE;
                }
                if (!this.f4391W) {
                    if (this.f4390V) {
                        C1446c c1446c = this.f4384P;
                        long j8 = this.f4392Y;
                        if (c1446c == null || c1446c.i(c1446c.k() - 1) <= j8) {
                            C1446c c1446c2 = this.f4385Q;
                            long j9 = this.f4392Y;
                            if ((c1446c2 == null || c1446c2.i(c1446c2.k() - 1) <= j9) && this.f4383O != null) {
                            }
                        }
                    }
                }
                return false;
            }
        }
        return true;
    }

    @Override // B0.AbstractC0020g
    public final void m() {
        this.X = null;
        this.f4393Z = -9223372036854775807L;
        W w4 = W.f1225u;
        C(this.f4392Y);
        w0.c cVar = new w0.c(w4);
        Handler handler = this.f4387S;
        if (handler != null) {
            handler.obtainMessage(1, cVar).sendToTarget();
        } else {
            E(cVar);
        }
        this.f4392Y = -9223372036854775807L;
        if (this.f4382N != null) {
            F();
            InterfaceC1448e interfaceC1448e = this.f4382N;
            interfaceC1448e.getClass();
            interfaceC1448e.a();
            this.f4382N = null;
            this.f4381M = 0;
        }
    }

    @Override // B0.AbstractC0020g
    public final void o(long j8, boolean z8) {
        this.f4392Y = j8;
        a aVar = this.f4378J;
        if (aVar != null) {
            aVar.clear();
        }
        W w4 = W.f1225u;
        C(this.f4392Y);
        w0.c cVar = new w0.c(w4);
        Handler handler = this.f4387S;
        if (handler != null) {
            handler.obtainMessage(1, cVar).sendToTarget();
        } else {
            E(cVar);
        }
        this.f4390V = false;
        this.f4391W = false;
        this.f4393Z = -9223372036854775807L;
        C1579o c1579o = this.X;
        if (c1579o == null || Objects.equals(c1579o.f17358n, "application/x-media3-cues")) {
            return;
        }
        if (this.f4381M == 0) {
            F();
            InterfaceC1448e interfaceC1448e = this.f4382N;
            interfaceC1448e.getClass();
            interfaceC1448e.flush();
            interfaceC1448e.f(this.f751B);
            return;
        }
        F();
        InterfaceC1448e interfaceC1448e2 = this.f4382N;
        interfaceC1448e2.getClass();
        interfaceC1448e2.a();
        this.f4382N = null;
        this.f4381M = 0;
        D();
    }

    @Override // B0.AbstractC0020g
    public final void t(C1579o[] c1579oArr, long j8, long j9, L0.C c3) {
        C1579o c1579o = c1579oArr[0];
        this.X = c1579o;
        if (Objects.equals(c1579o.f17358n, "application/x-media3-cues")) {
            this.f4378J = this.X.f17341J == 1 ? new b() : new c(0);
            return;
        }
        A();
        if (this.f4382N != null) {
            this.f4381M = 1;
        } else {
            D();
        }
    }

    @Override // B0.AbstractC0020g
    public final void v(long j8, long j9) {
        boolean z8;
        R0 r02;
        boolean z9;
        long jI;
        if (this.D) {
            long j10 = this.f4393Z;
            if (j10 != -9223372036854775807L && j8 >= j10) {
                F();
                this.f4391W = true;
            }
        }
        if (this.f4391W) {
            return;
        }
        C1579o c1579o = this.X;
        c1579o.getClass();
        boolean zEquals = Objects.equals(c1579o.f17358n, "application/x-media3-cues");
        Handler handler = this.f4387S;
        boolean zC = false;
        zC = false;
        zC = false;
        R0 r03 = this.f4389U;
        if (zEquals) {
            this.f4378J.getClass();
            if (!this.f4390V) {
                h hVar = this.f4377I;
                if (u(r03, hVar, 0) == -4) {
                    if (hVar.d(4)) {
                        this.f4390V = true;
                    } else {
                        hVar.j();
                        ByteBuffer byteBuffer = hVar.f18u;
                        byteBuffer.getClass();
                        long j11 = hVar.f20w;
                        byte[] bArrArray = byteBuffer.array();
                        int iArrayOffset = byteBuffer.arrayOffset();
                        int iLimit = byteBuffer.limit();
                        this.H.getClass();
                        Parcel parcelObtain = Parcel.obtain();
                        parcelObtain.unmarshall(bArrArray, iArrayOffset, iLimit);
                        parcelObtain.setDataPosition(0);
                        Bundle bundle = parcelObtain.readBundle(Bundle.class.getClassLoader());
                        parcelObtain.recycle();
                        ArrayList parcelableArrayList = bundle.getParcelableArrayList("c");
                        parcelableArrayList.getClass();
                        C0878i c0878i = new C0878i(12);
                        C3.D dJ = G.j();
                        for (int i = 0; i < parcelableArrayList.size(); i++) {
                            Bundle bundle2 = (Bundle) parcelableArrayList.get(i);
                            bundle2.getClass();
                            dJ.a(c0878i.apply(bundle2));
                        }
                        C1444a c1444a = new C1444a(j11, bundle.getLong("d"), dJ.g());
                        hVar.e();
                        zC = this.f4378J.c(c1444a, j8);
                    }
                }
            }
            long jD = this.f4378J.d(this.f4392Y);
            if (jD == Long.MIN_VALUE && this.f4390V && !zC) {
                this.f4391W = true;
            }
            if (jD != Long.MIN_VALUE && jD <= j8) {
                zC = true;
            }
            if (zC) {
                G gA = this.f4378J.a(j8);
                long jB = this.f4378J.b(j8);
                C(jB);
                w0.c cVar = new w0.c(gA);
                if (handler != null) {
                    handler.obtainMessage(1, cVar).sendToTarget();
                } else {
                    E(cVar);
                }
                this.f4378J.e(jB);
            }
            this.f4392Y = j8;
            return;
        }
        A();
        this.f4392Y = j8;
        if (this.f4385Q == null) {
            InterfaceC1448e interfaceC1448e = this.f4382N;
            interfaceC1448e.getClass();
            interfaceC1448e.b(j8);
            try {
                InterfaceC1448e interfaceC1448e2 = this.f4382N;
                interfaceC1448e2.getClass();
                this.f4385Q = (C1446c) interfaceC1448e2.c();
            } catch (C1449f e8) {
                AbstractC1709a.p("TextRenderer", "Subtitle decoding failed. streamFormat=" + this.X, e8);
                W w4 = W.f1225u;
                C(this.f4392Y);
                w0.c cVar2 = new w0.c(w4);
                if (handler != null) {
                    handler.obtainMessage(1, cVar2).sendToTarget();
                } else {
                    E(cVar2);
                }
                F();
                InterfaceC1448e interfaceC1448e3 = this.f4382N;
                interfaceC1448e3.getClass();
                interfaceC1448e3.a();
                this.f4382N = null;
                this.f4381M = 0;
                D();
                return;
            }
        }
        if (this.f763x != 2) {
            return;
        }
        if (this.f4384P != null) {
            long jB2 = B();
            z8 = false;
            while (jB2 <= j8) {
                this.f4386R++;
                jB2 = B();
                z8 = true;
            }
        } else {
            z8 = false;
        }
        C1446c c1446c = this.f4385Q;
        if (c1446c == null) {
            r02 = r03;
            z9 = z8;
        } else if (c1446c.d(4)) {
            if (!z8 && B() == Long.MAX_VALUE) {
                if (this.f4381M == 2) {
                    F();
                    InterfaceC1448e interfaceC1448e4 = this.f4382N;
                    interfaceC1448e4.getClass();
                    interfaceC1448e4.a();
                    this.f4382N = null;
                    this.f4381M = 0;
                    D();
                } else {
                    F();
                    this.f4391W = true;
                }
            }
            r02 = r03;
            z9 = z8;
        } else {
            r02 = r03;
            z9 = z8;
            if (c1446c.f23s <= j8) {
                C1446c c1446c2 = this.f4384P;
                if (c1446c2 != null) {
                    c1446c2.g();
                }
                this.f4386R = c1446c.b(j8);
                this.f4384P = c1446c;
                this.f4385Q = null;
                z9 = true;
            }
        }
        if (z9) {
            this.f4384P.getClass();
            int iB = this.f4384P.b(j8);
            if (iB == 0 || this.f4384P.k() == 0) {
                jI = this.f4384P.f23s;
            } else if (iB == -1) {
                C1446c c1446c3 = this.f4384P;
                jI = c1446c3.i(c1446c3.k() - 1);
            } else {
                jI = this.f4384P.i(iB - 1);
            }
            C(jI);
            w0.c cVar3 = new w0.c(this.f4384P.f(j8));
            if (handler != null) {
                handler.obtainMessage(1, cVar3).sendToTarget();
            } else {
                E(cVar3);
            }
        }
        if (this.f4381M == 2) {
            return;
        }
        while (!this.f4390V) {
            try {
                C1453j c1453j = this.f4383O;
                if (c1453j == null) {
                    InterfaceC1448e interfaceC1448e5 = this.f4382N;
                    interfaceC1448e5.getClass();
                    c1453j = (C1453j) interfaceC1448e5.d();
                    if (c1453j == null) {
                        return;
                    } else {
                        this.f4383O = c1453j;
                    }
                }
                if (this.f4381M == 1) {
                    c1453j.f6r = 4;
                    InterfaceC1448e interfaceC1448e6 = this.f4382N;
                    interfaceC1448e6.getClass();
                    interfaceC1448e6.e(c1453j);
                    this.f4383O = null;
                    this.f4381M = 2;
                    return;
                }
                int iU = u(r02, c1453j, 0);
                if (iU == -4) {
                    if (c1453j.d(4)) {
                        this.f4390V = true;
                        this.f4380L = false;
                    } else {
                        C1579o c1579o2 = (C1579o) r02.f11693s;
                        if (c1579o2 == null) {
                            return;
                        }
                        c1453j.f16219z = c1579o2.f17363s;
                        c1453j.j();
                        this.f4380L &= !c1453j.d(1);
                    }
                    if (!this.f4380L) {
                        InterfaceC1448e interfaceC1448e7 = this.f4382N;
                        interfaceC1448e7.getClass();
                        interfaceC1448e7.e(c1453j);
                        this.f4383O = null;
                    }
                } else if (iU == -3) {
                    return;
                }
            } catch (C1449f e9) {
                AbstractC1709a.p("TextRenderer", "Subtitle decoding failed. streamFormat=" + this.X, e9);
                W w6 = W.f1225u;
                C(this.f4392Y);
                w0.c cVar4 = new w0.c(w6);
                if (handler != null) {
                    handler.obtainMessage(1, cVar4).sendToTarget();
                } else {
                    E(cVar4);
                }
                F();
                InterfaceC1448e interfaceC1448e8 = this.f4382N;
                interfaceC1448e8.getClass();
                interfaceC1448e8.a();
                this.f4382N = null;
                this.f4381M = 0;
                D();
                return;
            }
        }
    }

    @Override // B0.AbstractC0020g
    public final int y(C1579o c1579o) {
        if (!Objects.equals(c1579o.f17358n, "application/x-media3-cues")) {
            K4.c cVar = this.f4379K;
            cVar.getClass();
            if (!((C) cVar.f3604r).l(c1579o)) {
                String str = c1579o.f17358n;
                if (!Objects.equals(str, "application/cea-608") && !Objects.equals(str, "application/x-mp4-cea-608") && !Objects.equals(str, "application/cea-708")) {
                    return E.k(str) ? I1.a.e(1, 0, 0, 0) : I1.a.e(0, 0, 0, 0);
                }
            }
        }
        return I1.a.e(c1579o.f17344M == 0 ? 4 : 2, 0, 0, 0);
    }
}
