package M;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private List f1403b;

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

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

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

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

    public i(InputStream inputStream, boolean z2) {
        this.f1403b = new ArrayList();
        j jVar = new j(inputStream, z2);
        this.f1402a = jVar.b();
        this.f1403b = jVar.g();
        this.f1404c = jVar.e();
    }

    private void b(m mVar) throws Exception {
        f fVarD = d(mVar);
        this.f1405d = fVarD;
        a aVarC = c(mVar, fVarD.d());
        this.f1406e = aVarC;
        this.f1407f = aVarC.f(this.f1405d.c(), 80, this.f1405d.f());
        this.f1406e.g(this.f1405d);
    }

    private a c(m mVar, int i2) throws Exception {
        String str;
        String str2;
        int i3;
        String str3 = "\n";
        String str4 = "PGSParserLog";
        int iD = mVar.d();
        int iC = mVar.c();
        int i4 = 0;
        long jA = ((e) mVar.p().b().get(0)).a();
        if (iD > mVar.f() || iC > mVar.b()) {
            throw new Exception("Subpicture too large: " + iD + "x" + iC + " at offset " + o.b(jA, 8));
        }
        a aVar = new a(iD, iC, (byte) i2);
        int i5 = 16;
        try {
            try {
                int iA = mVar.p().a();
                byte[] bArr = new byte[iA];
                int iB = 0;
                for (e eVar : mVar.p().b()) {
                    try {
                        b0.o.d(str4, "parseImg begin w=" + iD + ", h=" + iC + ", bsize=" + mVar.p().a() + ", fragSize=" + eVar.b() + ", Ofs=" + jA + str3, i5);
                        int i6 = 0;
                        while (i6 < eVar.b()) {
                            try {
                                try {
                                    str = str3;
                                    str2 = str4;
                                    try {
                                        bArr[iB + i6] = (byte) this.f1402a.b(eVar.a() + ((long) i6));
                                        i6++;
                                        str3 = str;
                                        str4 = str2;
                                    } catch (ArrayIndexOutOfBoundsException unused) {
                                        i4 = iB;
                                        b0.o.d(str2, "Problems during RLE decoding of picture OBJ at offset " + o.b(jA + ((long) i4), 8) + str, 16);
                                        return aVar;
                                    }
                                } catch (ArrayIndexOutOfBoundsException unused2) {
                                    str = str3;
                                    str2 = str4;
                                    i4 = iB;
                                    b0.o.d(str2, "Problems during RLE decoding of picture OBJ at offset " + o.b(jA + ((long) i4), 8) + str, 16);
                                    return aVar;
                                }
                            } catch (Exception e2) {
                                e = e2;
                                throw new Exception(e.getMessage());
                            }
                        }
                        String str5 = str3;
                        String str6 = str4;
                        iB += eVar.b();
                        str3 = str5;
                        str4 = str6;
                        i5 = 16;
                    } catch (ArrayIndexOutOfBoundsException unused3) {
                    }
                }
                str = str3;
                str2 = str4;
                int i7 = 0;
                int i8 = 0;
                int i9 = 0;
                do {
                    i4 = i7 + 1;
                    try {
                        int i10 = bArr[i7] & 255;
                        if (i10 == 0) {
                            int i11 = i7 + 2;
                            try {
                                byte b2 = bArr[i4];
                                int i12 = b2 & 255;
                                if (i12 == 0) {
                                    i8 = (i8 / iD) * iD;
                                    if (i9 < iD) {
                                        i8 += iD;
                                    }
                                    i7 = i11;
                                    i9 = 0;
                                } else {
                                    int i13 = b2 & 192;
                                    if (i13 == 64) {
                                        i7 += 3;
                                        try {
                                            i3 = ((i12 - 64) << 8) + (bArr[i11] & 255);
                                            int i14 = 0;
                                            while (i14 < i3) {
                                                aVar.e()[i8] = 0;
                                                i14++;
                                                i8++;
                                            }
                                        } catch (ArrayIndexOutOfBoundsException unused4) {
                                            i4 = i7;
                                            b0.o.d(str2, "Problems during RLE decoding of picture OBJ at offset " + o.b(jA + ((long) i4), 8) + str, 16);
                                            return aVar;
                                        }
                                    } else if (i13 == 128) {
                                        i3 = i12 - 128;
                                        i7 += 3;
                                        int i15 = bArr[i11] & 255;
                                        int i16 = 0;
                                        while (i16 < i3) {
                                            int i17 = i8 + 1;
                                            aVar.e()[i8] = (byte) i15;
                                            i16++;
                                            i8 = i17;
                                        }
                                    } else if (i13 != 0) {
                                        int i18 = i7 + 3;
                                        i3 = ((i12 - 192) << 8) + (bArr[i11] & 255);
                                        i7 += 4;
                                        int i19 = bArr[i18] & 255;
                                        int i20 = 0;
                                        while (i20 < i3) {
                                            int i21 = i8 + 1;
                                            aVar.e()[i8] = (byte) i19;
                                            i20++;
                                            i8 = i21;
                                        }
                                    } else {
                                        int i22 = 0;
                                        while (i22 < i12) {
                                            int i23 = i8 + 1;
                                            aVar.e()[i8] = 0;
                                            i22++;
                                            i8 = i23;
                                        }
                                        i9 += i12;
                                        i7 = i11;
                                    }
                                    i9 += i3;
                                }
                            } catch (ArrayIndexOutOfBoundsException unused5) {
                                i4 = i11;
                            }
                        } else {
                            int i24 = i8 + 1;
                            aVar.e()[i8] = (byte) i10;
                            i9++;
                            i7 = i4;
                            i8 = i24;
                        }
                    } catch (ArrayIndexOutOfBoundsException unused6) {
                    }
                } while (i7 < iA);
                return aVar;
            } catch (ArrayIndexOutOfBoundsException unused7) {
                str = "\n";
                str2 = "PGSParserLog";
            }
        } catch (Exception e3) {
            e = e3;
        }
    }

    private f d(m mVar) throws Exception {
        List<g> list = (List) mVar.t().get(mVar.p().d());
        if (list == null) {
            throw new Exception("Palette ID out of bounds.");
        }
        f fVar = new f(256, false);
        try {
            boolean z2 = false;
            for (g gVar : list) {
                int iA = gVar.a();
                for (int i2 = 0; i2 < gVar.b(); i2++) {
                    int iB = this.f1402a.b(iA);
                    int iB2 = this.f1402a.b(iA + 1);
                    int iB3 = this.f1402a.b(iA + 2);
                    int iB4 = this.f1402a.b(iA + 3);
                    int iB5 = this.f1402a.b(iA + 4);
                    if (iB5 >= fVar.b(iB)) {
                        if (iB5 < 14) {
                            iB3 = 128;
                            iB2 = 16;
                            iB4 = 128;
                        }
                        fVar.g(iB, iB5);
                    } else {
                        z2 = true;
                    }
                    fVar.h(iB, iB2, iB4, iB3);
                    iA += 5;
                }
            }
            if (z2) {
                b0.o.d("PGSParserLog", "fade out detected -> patched palette\n", 16);
            }
            return fVar;
        } catch (Exception e2) {
            throw new Exception(e2.getMessage());
        }
    }

    public void a(int i2) throws Exception {
        if (i2 < this.f1403b.size()) {
            b((m) this.f1403b.get(i2));
            return;
        }
        throw new Exception("Index " + i2 + " out of bounds\n");
    }

    public a e() {
        return this.f1406e;
    }

    public float f(int i2) {
        return ((m) this.f1403b.get(i2)).f() != 0 ? r2.p().f() / r2.f() : r2.p().f() / 1920.0f;
    }

    public final void finalize() {
    }

    public float g(int i2) {
        return ((m) this.f1403b.get(i2)).b() != 0 ? r2.p().g() / r2.b() : r2.p().g() / 1080.0f;
    }

    public List h() {
        return this.f1403b;
    }

    public i(byte[] bArr, boolean z2) {
        this.f1403b = new ArrayList();
        j jVar = new j(bArr, z2);
        this.f1402a = jVar.b();
        this.f1403b = jVar.g();
        this.f1404c = jVar.e();
    }
}
