package M;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public class j {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private c f1408a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private List f1409b = new ArrayList();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private int f1410c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private boolean f1411d;

    static /* synthetic */ class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        static final /* synthetic */ int[] f1412a;

        static {
            int[] iArr = new int[h.values().length];
            f1412a = iArr;
            try {
                iArr[h.EPOCH_CONTINUE.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f1412a[h.ACQU_POINT.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                f1412a[h.NORMAL.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    private static class b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        int f1413a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        int f1414b;

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

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        int f1416d;

        private b() {
        }
    }

    public j(InputStream inputStream, boolean z2) throws Exception {
        this.f1411d = z2;
        try {
            this.f1408a = new c(inputStream);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        h();
    }

    private void a() {
        this.f1410c = 0;
        Iterator it = this.f1409b.iterator();
        while (it.hasNext()) {
            if (((m) it.next()).i()) {
                this.f1410c++;
            }
        }
        b0.o.c("PGSParserLog", 16, "\nDetected ", Integer.valueOf(this.f1410c), " forced captions.\n");
    }

    private int c(b bVar) {
        return this.f1408a.e(bVar.f1416d + 5);
    }

    private h d(b bVar) throws Exception {
        b0.o.c("PGSParserLog", 16, "segment.offset: ", Integer.valueOf(bVar.f1416d), "buffer: ", this.f1408a.toString());
        int iB = this.f1408a.b(bVar.f1416d + 7);
        for (h hVar : h.values()) {
            if (iB == hVar.getType()) {
                return hVar;
            }
        }
        return h.INVALID;
    }

    private boolean f(b bVar) {
        return this.f1408a.b((long) (bVar.f1416d + 8)) == 128;
    }

    /* JADX WARN: Removed duplicated region for block: B:198:0x061b A[Catch: Exception -> 0x062d, TryCatch #3 {Exception -> 0x062d, blocks: (B:196:0x0617, B:198:0x061b, B:200:0x0620, B:199:0x061e), top: B:219:0x0617 }] */
    /* JADX WARN: Removed duplicated region for block: B:199:0x061e A[Catch: Exception -> 0x062d, TryCatch #3 {Exception -> 0x062d, blocks: (B:196:0x0617, B:198:0x061b, B:200:0x0620, B:199:0x061e), top: B:219:0x0617 }] */
    /* JADX WARN: Removed duplicated region for block: B:208:0x063d  */
    /* JADX WARN: Removed duplicated region for block: B:209:0x064d  */
    /* JADX WARN: Removed duplicated region for block: B:229:0x05e3 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Unreachable blocks removed: 2, instructions: 3 */
    /*  JADX ERROR: NullPointerException in pass: LoopRegionVisitor
        java.lang.NullPointerException: Cannot invoke "jadx.core.dex.instructions.args.SSAVar.use(jadx.core.dex.instructions.args.RegisterArg)" because "ssaVar" is null
        	at jadx.core.dex.nodes.InsnNode.rebindArgs(InsnNode.java:506)
        	at jadx.core.dex.nodes.InsnNode.rebindArgs(InsnNode.java:509)
        */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private void h() throws java.lang.Exception {
        /*
            Method dump skipped, instruction units count: 1636
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: M.j.h():void");
    }

    private boolean i(b bVar, m mVar, StringBuffer stringBuffer) throws Exception {
        d dVarP;
        boolean z2;
        int i2 = bVar.f1416d;
        int iE = this.f1408a.e(i2);
        int iB = this.f1408a.b(i2 + 1);
        int iB2 = this.f1408a.b(i2 + 3);
        boolean z3 = (iB2 & 128) == 128;
        boolean z4 = (iB2 & 64) == 64;
        if (mVar.s() != iE) {
            Iterator it = this.f1409b.iterator();
            while (true) {
                if (!it.hasNext()) {
                    dVarP = null;
                    z2 = false;
                    break;
                }
                m mVar2 = (m) it.next();
                if (mVar2.s() == iE) {
                    dVarP = mVar2.q(iE);
                    ((List) mVar2.t().get(dVarP.d())).add((g) ((List) mVar.t().get(mVar.p().d())).get(0));
                    if (iE > 0) {
                        mVar2.y(0);
                    }
                    z2 = true;
                }
            }
            if (!z2) {
                dVarP = new d();
                mVar.r().add(dVarP);
            }
            b0.o.d("PGSParserLog", "add new image, Id=" + iE + ", find=" + z2, 16);
        } else {
            dVarP = mVar.p();
            b0.o.d("PGSParserLog", "get image objectId=" + iE, 16);
        }
        if (!dVarP.b().isEmpty() && !z3) {
            int i3 = i2 + 4;
            boolean z5 = z4;
            e eVar = new e(i3, bVar.f1414b - (i3 - bVar.f1416d));
            dVarP.b().add(eVar);
            dVarP.h(dVarP.a() + eVar.b());
            stringBuffer.append("ID: ");
            stringBuffer.append(iE);
            stringBuffer.append(", update: ");
            stringBuffer.append(iB);
            stringBuffer.append(", seq: ");
            stringBuffer.append(HttpUrl.FRAGMENT_ENCODE_SET);
            stringBuffer.append(z5 ? "/" : HttpUrl.FRAGMENT_ENCODE_SET);
            stringBuffer.append(z5 ? "last" : HttpUrl.FRAGMENT_ENCODE_SET);
            return false;
        }
        boolean z6 = z4;
        int iE2 = this.f1408a.e(i2 + 7);
        int iE3 = this.f1408a.e(i2 + 9);
        if (iE2 > mVar.f() || iE3 > mVar.b()) {
            b0.o.c("PGSParserLog", 16, "Invalid image size - ignored\n");
            return false;
        }
        int i4 = i2 + 11;
        e eVar2 = new e(i4, bVar.f1414b - (i4 - bVar.f1416d));
        dVarP.b().add(eVar2);
        dVarP.h(eVar2.b());
        dVarP.i(iE3);
        dVarP.k(iE2);
        stringBuffer.append("ID: ");
        stringBuffer.append(iE);
        stringBuffer.append(", Order: ");
        stringBuffer.append(iB2);
        stringBuffer.append(", update: ");
        stringBuffer.append(iB);
        stringBuffer.append(", seq: ");
        stringBuffer.append(z3 ? "first" : HttpUrl.FRAGMENT_ENCODE_SET);
        stringBuffer.append((z3 && z6) ? "/" : HttpUrl.FRAGMENT_ENCODE_SET);
        stringBuffer.append(z6 ? "last" : HttpUrl.FRAGMENT_ENCODE_SET);
        stringBuffer.append(", offset: ");
        stringBuffer.append(eVar2.a());
        stringBuffer.append(", size: ");
        stringBuffer.append(eVar2.b());
        return true;
    }

    private void j(b bVar, m mVar, StringBuffer stringBuffer) throws Exception {
        d dVarQ;
        int i2;
        int i3 = bVar.f1416d;
        if (bVar.f1414b >= 4) {
            long j2 = i3;
            mVar.o(this.f1408a.e(j2));
            long j3 = i3 + 2;
            mVar.m(this.f1408a.e(j3));
            int iB = this.f1408a.b(i3 + 4);
            int iE = this.f1408a.e(i3 + 5);
            int iB2 = this.f1408a.b(i3 + 9);
            int iB3 = this.f1408a.b(i3 + 10);
            if (bVar.f1414b - 11 < iB3 * 8) {
                b0.o.d("PGSParserLog", "ERROR: Not enough data to import all CompositionObjects.\n", 16);
            }
            if (iB3 > 0) {
                int iE2 = this.f1408a.e(i3 + 11);
                stringBuffer.append("paletteId: ");
                stringBuffer.append(iB2);
                stringBuffer.append(", objectId: ");
                stringBuffer.append(iE2);
                if (mVar.q(iE2) == null) {
                    dVarQ = new d();
                    mVar.r().add(dVarQ);
                } else {
                    dVarQ = mVar.q(iE2);
                }
                dVarQ.j(iB2);
                mVar.y(iE2);
                int i4 = 19;
                if (bVar.f1414b >= 19) {
                    mVar.z(iB);
                    d dVar = dVarQ;
                    int iB4 = this.f1408a.b(i3 + 14);
                    mVar.j(iE);
                    boolean z2 = (iB4 & 64) == 64;
                    mVar.l(z2);
                    long j4 = i3 + 15;
                    dVar.l(this.f1408a.e(j4));
                    i2 = iB2;
                    long j5 = i3 + 17;
                    dVar.m(this.f1408a.e(j5));
                    stringBuffer.append(", XOffset: ");
                    stringBuffer.append(this.f1408a.e(j4));
                    stringBuffer.append(", YOffset: ");
                    stringBuffer.append(this.f1408a.e(j5));
                    stringBuffer.append(", objectCount: ");
                    stringBuffer.append(iB3);
                    if (z2) {
                        i4 = 27;
                    }
                } else {
                    i2 = iB2;
                    i4 = 0;
                }
                if (iB3 == 2) {
                    int iE3 = this.f1408a.e(i3 + i4);
                    d dVar2 = new d();
                    m mVar2 = new m();
                    mVar2.o(this.f1408a.e(j2));
                    mVar2.m(this.f1408a.e(j3));
                    mVar2.r().add(dVar2);
                    dVar2.j(i2);
                    mVar2.l((this.f1408a.b((long) ((i4 + 3) + i3)) & 64) == 64);
                    int iE4 = this.f1408a.e(i4 + 4 + i3);
                    dVar2.l(iE4);
                    int iE5 = this.f1408a.e(i3 + i4 + 6);
                    dVar2.m(iE5);
                    stringBuffer.append(",cs2 objectId: ");
                    stringBuffer.append(iE3);
                    stringBuffer.append(", X2Offset: ");
                    stringBuffer.append(iE4);
                    stringBuffer.append(", Y2Offset: ");
                    stringBuffer.append(iE5);
                    mVar2.y(iE3);
                    this.f1409b.add(mVar2);
                }
            }
        }
    }

    private int k(b bVar, m mVar, StringBuffer stringBuffer) throws Exception {
        int i2 = bVar.f1416d;
        long j2 = i2;
        int iB = this.f1408a.b(j2);
        int iB2 = this.f1408a.b(i2 + 1);
        if (iB > 7) {
            stringBuffer.append("Illegal palette id at offset ");
            stringBuffer.append(o.b(j2, 8));
            return -1;
        }
        g gVar = new g(i2 + 2, (bVar.f1414b - 2) / 5);
        ((List) mVar.t().get(iB)).add(gVar);
        stringBuffer.append("ID: ");
        stringBuffer.append(iB);
        stringBuffer.append(", update: ");
        stringBuffer.append(iB2);
        stringBuffer.append(", ");
        stringBuffer.append(gVar.b());
        stringBuffer.append(" entries");
        return gVar.b();
    }

    private void l(b bVar, m mVar) {
        int i2 = bVar.f1416d;
        if (bVar.f1414b >= 10) {
            mVar.C(this.f1408a.e(i2 + 2));
            mVar.D(this.f1408a.e(i2 + 4));
            mVar.B(this.f1408a.e(i2 + 6));
            mVar.A(this.f1408a.e(i2 + 8));
        }
    }

    private b m(int i2) throws Exception {
        b bVar = new b();
        if (this.f1411d) {
            bVar.f1413a = this.f1408a.b(i2);
            bVar.f1414b = this.f1408a.e(i2 + 1);
            bVar.f1416d = i2 + 3;
            return bVar;
        }
        long j2 = i2;
        if (this.f1408a.e(j2) == 20551) {
            bVar.f1415c = this.f1408a.c(i2 + 2);
            bVar.f1413a = this.f1408a.b(i2 + 10);
            bVar.f1414b = this.f1408a.e(i2 + 11);
            bVar.f1416d = i2 + 13;
            return bVar;
        }
        throw new Exception("PG missing at index " + o.b(j2, 8) + "\n");
    }

    private void n(int i2, int i3) {
        if (this.f1409b.size() > 0) {
            if (i2 == 0 || i3 == 0) {
                b0.o.c("PGSParserLog", 16, "Missing PDS/ODS: last epoch is discarded\n");
                this.f1409b.remove(r2.size() - 1);
            }
        }
    }

    public c b() {
        return this.f1408a;
    }

    public int e() {
        return this.f1410c;
    }

    public final void finalize() {
    }

    public List g() {
        return this.f1409b;
    }

    public j(byte[] bArr, boolean z2) throws Exception {
        this.f1411d = z2;
        try {
            this.f1408a = new c(bArr);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        h();
    }
}
