package A1;

import T0.AbstractC0302b;
import T0.C0301a;
import java.math.RoundingMode;
import java.util.concurrent.atomic.AtomicInteger;
import n4.u0;
import u0.C1578n;
import u0.C1579o;
import x0.AbstractC1709a;

/* JADX INFO: renamed from: A1.g, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0006g implements InterfaceC0008i {

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public String f162f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public T0.F f163g;
    public int i;

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f164h = 0;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public long f172q = -9223372036854775807L;

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public int f170o = -1;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public int f171p = -1;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final String f161e = "video/mp2t";

    public C0006g(int i, int i5, String str) {
        this.f157a = new x0.o(new byte[i5]);
        this.f159c = str;
        this.f160d = i;
    }

    @Override // A1.InterfaceC0008i
    public final void a() {
        this.f164h = 0;
        this.i = 0;
        this.f165j = 0;
        this.f172q = -9223372036854775807L;
        this.f158b.set(0);
    }

    public final boolean b(x0.o oVar, byte[] bArr, int i) {
        int iMin = Math.min(oVar.a(), i - this.i);
        oVar.e(bArr, this.i, iMin);
        int i5 = this.i + iMin;
        this.i = i5;
        return i5 == i;
    }

    public final void c(C0301a c0301a) {
        int i;
        int i5 = c0301a.f6364b;
        if (i5 == -2147483647 || (i = c0301a.f6365c) == -1) {
            return;
        }
        C1579o c1579o = this.f167l;
        String str = c0301a.f6363a;
        if (c1579o != null && i == c1579o.D && i5 == c1579o.f17337E && str.equals(c1579o.f17358n)) {
            return;
        }
        C1579o c1579o2 = this.f167l;
        C1578n c1578n = c1579o2 == null ? new C1578n() : c1579o2.a();
        c1578n.f17309a = this.f162f;
        c1578n.f17319l = u0.E.m(this.f161e);
        c1578n.f17320m = u0.E.m(str);
        c1578n.f17301C = i;
        c1578n.D = i5;
        c1578n.f17312d = this.f159c;
        c1578n.f17314f = this.f160d;
        C1579o c1579o3 = new C1579o(c1578n);
        this.f167l = c1579o3;
        this.f163g.e(c1579o3);
    }

    @Override // A1.InterfaceC0008i
    public final void e(x0.o oVar) throws u0.F {
        int i;
        boolean z8;
        int i5;
        byte b8;
        int i8;
        byte b9;
        int i9;
        byte b10;
        int i10;
        int i11;
        int i12;
        int i13;
        int i14;
        int i15;
        long jS;
        int i16;
        int i17;
        long jS2;
        int i18;
        int i19;
        int i20;
        int i21;
        int i22 = 4;
        AbstractC1709a.k(this.f163g);
        while (oVar.a() > 0) {
            int i23 = this.f164h;
            int i24 = 8;
            x0.o oVar2 = this.f157a;
            switch (i23) {
                case u0.f15495q /* 0 */:
                    while (oVar.a() > 0) {
                        int i25 = this.f165j << 8;
                        this.f165j = i25;
                        int iT = i25 | oVar.t();
                        this.f165j = iT;
                        int i26 = (iT == 2147385345 || iT == -25230976 || iT == 536864768 || iT == -14745368) ? 1 : (iT == 1683496997 || iT == 622876772) ? 2 : (iT == 1078008818 || iT == -233094848) ? 3 : (iT == 1908687592 || iT == -398277519) ? 4 : 0;
                        this.f169n = i26;
                        if (i26 != 0) {
                            byte[] bArr = oVar2.f18539a;
                            bArr[0] = (byte) ((iT >> 24) & 255);
                            bArr[1] = (byte) ((iT >> 16) & 255);
                            bArr[2] = (byte) ((iT >> 8) & 255);
                            bArr[3] = (byte) (iT & 255);
                            this.i = 4;
                            this.f165j = 0;
                            if (i26 == 3 || i26 == 4) {
                                this.f164h = 4;
                            } else if (i26 == 1) {
                                this.f164h = 1;
                            } else {
                                this.f164h = 2;
                            }
                            i22 = 4;
                            break;
                        }
                    }
                    i22 = 4;
                    break;
                case 1:
                    if (b(oVar, oVar2.f18539a, 18)) {
                        byte[] bArr2 = oVar2.f18539a;
                        if (this.f167l == null) {
                            String str = this.f162f;
                            T0.H hI = AbstractC0302b.i(bArr2);
                            hI.t(60);
                            int i27 = AbstractC0302b.f6374j[hI.i(6)];
                            int i28 = AbstractC0302b.f6375k[hI.i(4)];
                            int i29 = hI.i(5);
                            int i30 = i29 >= 29 ? -1 : (AbstractC0302b.f6376l[i29] * 1000) / 2;
                            hI.t(10);
                            int i31 = i27 + (hI.i(2) > 0 ? 1 : 0);
                            C1578n c1578n = new C1578n();
                            c1578n.f17309a = str;
                            c1578n.f17319l = u0.E.m(this.f161e);
                            c1578n.f17320m = u0.E.m("audio/vnd.dts");
                            c1578n.f17316h = i30;
                            c1578n.f17301C = i31;
                            c1578n.D = i28;
                            c1578n.f17324q = null;
                            c1578n.f17312d = this.f159c;
                            c1578n.f17314f = this.f160d;
                            C1579o c1579o = new C1579o(c1578n);
                            this.f167l = c1579o;
                            this.f163g.e(c1579o);
                        }
                        byte b11 = bArr2[0];
                        if (b11 != -2) {
                            if (b11 == -1) {
                                i9 = ((bArr2[7] & 3) << 12) | ((bArr2[6] & 255) << 4);
                                b10 = bArr2[9];
                            } else if (b11 != 31) {
                                i = (((bArr2[5] & 3) << 12) | ((bArr2[6] & 255) << 4) | ((bArr2[7] & 240) >> 4)) + 1;
                                z8 = false;
                            } else {
                                i9 = ((bArr2[6] & 3) << 12) | ((bArr2[7] & 255) << 4);
                                b10 = bArr2[8];
                            }
                            i = (i9 | ((b10 & 60) >> 2)) + 1;
                            z8 = true;
                        } else {
                            i = (((bArr2[4] & 3) << 12) | ((bArr2[7] & 255) << 4) | ((bArr2[6] & 240) >> 4)) + 1;
                            z8 = false;
                        }
                        if (z8) {
                            i = (i * 16) / 14;
                        }
                        this.f168m = i;
                        if (b11 != -2) {
                            if (b11 == -1) {
                                i5 = (bArr2[4] & 7) << 4;
                                b9 = bArr2[7];
                            } else if (b11 != 31) {
                                i5 = (bArr2[4] & 1) << 6;
                                b8 = bArr2[5];
                            } else {
                                i5 = (bArr2[5] & 7) << 4;
                                b9 = bArr2[6];
                            }
                            i8 = b9 & 60;
                            this.f166k = W3.b.h(x0.v.Q((((i8 >> 2) | i5) + 1) * 32, this.f167l.f17337E));
                            oVar2.F(0);
                            this.f163g.c(18, oVar2);
                            this.f164h = 6;
                        } else {
                            i5 = (bArr2[5] & 1) << 6;
                            b8 = bArr2[4];
                        }
                        i8 = b8 & 252;
                        this.f166k = W3.b.h(x0.v.Q((((i8 >> 2) | i5) + 1) * 32, this.f167l.f17337E));
                        oVar2.F(0);
                        this.f163g.c(18, oVar2);
                        this.f164h = 6;
                    }
                    i22 = 4;
                    break;
                case 2:
                    if (b(oVar, oVar2.f18539a, 7)) {
                        T0.H hI2 = AbstractC0302b.i(oVar2.f18539a);
                        hI2.t(42);
                        this.f170o = hI2.i(hI2.h() ? 12 : 8) + 1;
                        this.f164h = 3;
                    }
                    i22 = 4;
                    break;
                case 3:
                    int i32 = i22;
                    if (b(oVar, oVar2.f18539a, this.f170o)) {
                        T0.H hI3 = AbstractC0302b.i(oVar2.f18539a);
                        hI3.t(40);
                        int i33 = hI3.i(2);
                        if (hI3.h()) {
                            i10 = 20;
                            i11 = 12;
                        } else {
                            i10 = 16;
                            i11 = 8;
                        }
                        hI3.t(i11);
                        int i34 = hI3.i(i10) + 1;
                        boolean zH = hI3.h();
                        if (zH) {
                            i12 = hI3.i(2);
                            i13 = (hI3.i(3) + 1) * 512;
                            if (hI3.h()) {
                                hI3.t(36);
                            }
                            int i35 = hI3.i(3) + 1;
                            int i36 = hI3.i(3) + 1;
                            if (i35 != 1 || i36 != 1) {
                                throw u0.F.c("Multiple audio presentations or assets not supported");
                            }
                            int i37 = i33 + 1;
                            int i38 = hI3.i(i37);
                            int i39 = 0;
                            while (i39 < i37) {
                                if (((i38 >> i39) & 1) == 1) {
                                    hI3.t(i24);
                                }
                                i39++;
                                i24 = 8;
                            }
                            if (hI3.h()) {
                                hI3.t(2);
                                int i40 = (hI3.i(2) + 1) << 2;
                                int i41 = hI3.i(2) + 1;
                                for (int i42 = 0; i42 < i41; i42++) {
                                    hI3.t(i40);
                                }
                            }
                        } else {
                            i12 = -1;
                            i13 = 0;
                        }
                        hI3.t(i10);
                        hI3.t(12);
                        if (zH) {
                            if (hI3.h()) {
                                hI3.t(i32);
                            }
                            if (hI3.h()) {
                                hI3.t(24);
                            }
                            if (hI3.h()) {
                                hI3.u(hI3.i(10) + 1);
                            }
                            hI3.t(5);
                            int i43 = AbstractC0302b.f6377m[hI3.i(4)];
                            i14 = hI3.i(8) + 1;
                            i15 = i43;
                        } else {
                            i14 = -1;
                            i15 = -2147483647;
                        }
                        if (zH) {
                            if (i12 == 0) {
                                i16 = 32000;
                            } else if (i12 == 1) {
                                i16 = 44100;
                            } else {
                                if (i12 != 2) {
                                    throw u0.F.a(null, "Unsupported reference clock code in DTS HD header: " + i12);
                                }
                                i16 = 48000;
                            }
                            int i44 = x0.v.f18553a;
                            jS = x0.v.S(i13, 1000000L, i16, RoundingMode.DOWN);
                        } else {
                            jS = -9223372036854775807L;
                        }
                        c(new C0301a("audio/vnd.dts.hd;profile=lbr", i14, i15, i34, jS));
                        this.f168m = i34;
                        this.f166k = jS == -9223372036854775807L ? 0L : jS;
                        oVar2.F(0);
                        this.f163g.c(this.f170o, oVar2);
                        this.f164h = 6;
                    }
                    i22 = 4;
                    break;
                case 4:
                    i17 = i22;
                    if (b(oVar, oVar2.f18539a, 6)) {
                        T0.H hI4 = AbstractC0302b.i(oVar2.f18539a);
                        hI4.t(32);
                        int iQ = AbstractC0302b.q(hI4, AbstractC0302b.f6382r) + 1;
                        this.f171p = iQ;
                        int i45 = this.i;
                        if (i45 > iQ) {
                            int i46 = i45 - iQ;
                            this.i = i45 - i46;
                            oVar.F(oVar.f18540b - i46);
                        }
                        this.f164h = 5;
                    }
                    i22 = i17;
                    break;
                case 5:
                    if (b(oVar, oVar2.f18539a, this.f171p)) {
                        byte[] bArr3 = oVar2.f18539a;
                        AtomicInteger atomicInteger = this.f158b;
                        i17 = i22;
                        T0.H hI5 = AbstractC0302b.i(bArr3);
                        int i47 = hI5.i(32) == 1078008818 ? 1 : 0;
                        int iQ2 = AbstractC0302b.q(hI5, AbstractC0302b.f6378n);
                        int i48 = iQ2 + 1;
                        if (i47 == 0) {
                            jS2 = -9223372036854775807L;
                            i18 = -2147483647;
                        } else {
                            if (!hI5.h()) {
                                throw u0.F.c("Only supports full channel mask-based audio presentation");
                            }
                            int i49 = iQ2 - 1;
                            int i50 = ((bArr3[i49] << 8) & 65535) | (bArr3[iQ2] & 255);
                            int i51 = x0.v.f18553a;
                            int i52 = 65535;
                            for (int i53 = 0; i53 < i49; i53++) {
                                byte b12 = bArr3[i53];
                                int[] iArr = x0.v.f18562k;
                                int i54 = (iArr[(((b12 & 255) >> 4) ^ ((i52 >> 12) & 255)) & 255] ^ ((i52 << 4) & 65535)) & 65535;
                                i52 = (((i54 << 4) & 65535) ^ iArr[((b12 & 15) ^ ((i54 >> 12) & 255)) & 255]) & 65535;
                            }
                            if (i50 != i52) {
                                throw u0.F.a(null, "CRC check failed");
                            }
                            int i55 = hI5.i(2);
                            if (i55 != 0) {
                                if (i55 == 1) {
                                    i20 = 480;
                                } else {
                                    if (i55 != 2) {
                                        throw u0.F.a(null, "Unsupported base duration index in DTS UHD header: " + i55);
                                    }
                                    i20 = 384;
                                }
                                i19 = 3;
                            } else {
                                i19 = 3;
                                i20 = 512;
                            }
                            int i56 = (hI5.i(i19) + 1) * i20;
                            int i57 = hI5.i(2);
                            if (i57 == 0) {
                                i21 = 32000;
                            } else if (i57 == 1) {
                                i21 = 44100;
                            } else {
                                if (i57 != 2) {
                                    throw u0.F.a(null, "Unsupported clock rate index in DTS UHD header: " + i57);
                                }
                                i21 = 48000;
                            }
                            if (hI5.h()) {
                                hI5.t(36);
                            }
                            i18 = i21 * (1 << hI5.i(2));
                            jS2 = x0.v.S(i56, 1000000L, i21, RoundingMode.DOWN);
                        }
                        int iQ3 = 0;
                        for (int i58 = 0; i58 < i47; i58++) {
                            iQ3 += AbstractC0302b.q(hI5, AbstractC0302b.f6379o);
                        }
                        if (i47 != 0) {
                            atomicInteger.set(AbstractC0302b.q(hI5, AbstractC0302b.f6380p));
                        }
                        int iQ4 = iQ3 + (atomicInteger.get() != 0 ? AbstractC0302b.q(hI5, AbstractC0302b.f6381q) : 0) + i48;
                        C0301a c0301a = new C0301a("audio/vnd.dts.uhd;profile=p2", 2, i18, iQ4, jS2);
                        if (this.f169n == 3) {
                            c(c0301a);
                        }
                        this.f168m = iQ4;
                        this.f166k = jS2 == -9223372036854775807L ? 0L : jS2;
                        oVar2.F(0);
                        this.f163g.c(this.f171p, oVar2);
                        this.f164h = 6;
                        i22 = i17;
                    } else {
                        continue;
                    }
                    break;
                case Z.g.STRING_SET_FIELD_NUMBER /* 6 */:
                    int iMin = Math.min(oVar.a(), this.f168m - this.i);
                    this.f163g.c(iMin, oVar);
                    int i59 = this.i + iMin;
                    this.i = i59;
                    if (i59 == this.f168m) {
                        AbstractC1709a.j(this.f172q != -9223372036854775807L);
                        this.f163g.a(this.f172q, this.f169n == i22 ? 0 : 1, this.f168m, 0, null);
                        this.f172q += this.f166k;
                        this.f164h = 0;
                    }
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
    }

    @Override // A1.InterfaceC0008i
    public final void g(T0.o oVar, M m8) {
        m8.a();
        m8.b();
        this.f162f = m8.f103e;
        m8.b();
        this.f163g = oVar.j(m8.f102d, 1);
    }

    @Override // A1.InterfaceC0008i
    public final void h(long j8, int i) {
        this.f172q = j8;
    }

    @Override // A1.InterfaceC0008i
    public final void f(boolean z8) {
    }
}
