package com.google.android.gms.internal.ads;

import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.MimeTypes;
import androidx.media3.exoplayer.RendererCapabilities;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes2.dex */
public final class ia implements ja {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final p01 f12428a;

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

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

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

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

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

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

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

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

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

    @Nullable
    private final String zzc;

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

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

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final String f12431d = MimeTypes.VIDEO_MP2T;

    public ia(@Nullable String str, int i10, int i11, String str2) {
        this.f12428a = new p01(new byte[i11]);
        this.zzc = str;
        this.f12430c = i10;
    }

    @Override // com.google.android.gms.internal.ads.ja
    public final void a(q2 q2Var, x2.o0 o0Var) {
        o0Var.d();
        o0Var.e();
        this.f12432e = o0Var.f34561e;
        o0Var.e();
        this.f12433f = q2Var.e(o0Var.f34560d, 1);
    }

    @Override // com.google.android.gms.internal.ads.ja
    public final void b(boolean z10) {
    }

    @Override // com.google.android.gms.internal.ads.ja
    public final void c(int i10, long j10) {
        this.f12443p = j10;
    }

    @Override // com.google.android.gms.internal.ads.ja
    public final void d(p01 p01Var) throws zzat {
        long jR;
        int i10;
        int i11;
        int i12;
        int i13;
        int iP;
        int i14;
        int iP2;
        int i15;
        long jR2;
        int i16;
        int i17;
        int i18;
        int i19;
        byte b10;
        int i20;
        this.f12433f.getClass();
        while (p01Var.y() > 0) {
            int i21 = this.f12434g;
            p01 p01Var2 = this.f12428a;
            if (i21 == 0) {
                while (true) {
                    if (p01Var.y() > 0) {
                        int i22 = this.f12436i << 8;
                        this.f12436i = i22;
                        int I = i22 | p01Var.I();
                        this.f12436i = I;
                        int iA = n2.a(I);
                        this.f12440m = iA;
                        if (iA != 0) {
                            byte[] bArr = p01Var2.f15225a;
                            int i23 = this.f12436i;
                            bArr[0] = (byte) ((i23 >> 24) & 255);
                            bArr[1] = (byte) ((i23 >> 16) & 255);
                            bArr[2] = (byte) ((i23 >> 8) & 255);
                            bArr[3] = (byte) (i23 & 255);
                            this.f12435h = 4;
                            this.f12436i = 0;
                            if (iA == 3 || iA == 4) {
                                this.f12434g = 4;
                            } else if (iA == 1) {
                                this.f12434g = 1;
                            } else {
                                this.f12434g = 2;
                            }
                        }
                    }
                }
            } else if (i21 != 1) {
                if (i21 != 2) {
                    if (i21 != 3) {
                        if (i21 != 4) {
                            if (i21 != 5) {
                                int iMin = Math.min(p01Var.y(), this.f12439l - this.f12435h);
                                this.f12433f.a(p01Var, iMin);
                                int i24 = this.f12435h + iMin;
                                this.f12435h = i24;
                                if (i24 == this.f12439l) {
                                    ih1.b1(this.f12443p != -9223372036854775807L);
                                    this.f12433f.zze(this.f12443p, this.f12440m == 4 ? 0 : 1, this.f12439l, 0, null);
                                    this.f12443p += this.f12437j;
                                    this.f12434g = 0;
                                }
                            } else if (e(p01Var, p01Var2.f15225a, this.f12442o)) {
                                AtomicInteger atomicInteger = this.f12429b;
                                byte[] bArr2 = p01Var2.f15225a;
                                androidx.media3.extractor.b1 b1VarD = n2.d(bArr2);
                                int iP3 = b1VarD.p(32);
                                int iC = n2.c(b1VarD, n2.f14408e);
                                int i25 = iC + 1;
                                char c6 = iP3 == 1078008818 ? (char) 1 : (char) 0;
                                if (c6 == 0) {
                                    jR = -9223372036854775807L;
                                    i10 = C.RATE_UNSET_INT;
                                } else {
                                    if (!b1VarD.o()) {
                                        throw zzat.zzc("Only supports full channel mask-based audio presentation");
                                    }
                                    int i26 = iC - 1;
                                    int i27 = bArr2[i26] << 8;
                                    int i28 = bArr2[iC] & 255;
                                    String str = bb1.f9474a;
                                    char c10 = 65535;
                                    for (int i29 = 0; i29 < i26; i29++) {
                                        byte b11 = bArr2[i29];
                                        int[] iArr = bb1.f9480g;
                                        char c11 = (char) (iArr[((c10 >> '\f') ^ ((b11 & 255) >> 4)) & 255] ^ ((char) (c10 << 4)));
                                        c10 = (char) (((char) (c11 << 4)) ^ iArr[((b11 & 15) ^ (c11 >> '\f')) & 255]);
                                    }
                                    if ((((char) i27) | i28) != c10) {
                                        throw zzat.zzb("CRC check failed", null);
                                    }
                                    int iP4 = b1VarD.p(2);
                                    if (iP4 == 0) {
                                        i11 = 512;
                                    } else if (iP4 == 1) {
                                        i11 = 480;
                                    } else {
                                        if (iP4 != 2) {
                                            StringBuilder sb = new StringBuilder(String.valueOf(iP4).length() + 51);
                                            sb.append("Unsupported base duration index in DTS UHD header: ");
                                            sb.append(iP4);
                                            throw zzat.zzb(sb.toString(), null);
                                        }
                                        i11 = RendererCapabilities.DECODER_SUPPORT_MASK;
                                    }
                                    int iP5 = b1VarD.p(3) + 1;
                                    int iP6 = b1VarD.p(2);
                                    if (iP6 == 0) {
                                        i12 = 32000;
                                    } else if (iP6 == 1) {
                                        i12 = 44100;
                                    } else {
                                        if (iP6 != 2) {
                                            StringBuilder sb2 = new StringBuilder(String.valueOf(iP6).length() + 48);
                                            sb2.append("Unsupported clock rate index in DTS UHD header: ");
                                            sb2.append(iP6);
                                            throw zzat.zzb(sb2.toString(), null);
                                        }
                                        i12 = 48000;
                                    }
                                    if (b1VarD.o()) {
                                        b1VarD.n(36);
                                    }
                                    int iP7 = i12 * (1 << b1VarD.p(2));
                                    jR = bb1.r(i11 * iP5, 1000000L, i12, RoundingMode.DOWN);
                                    i10 = iP7;
                                }
                                int iC2 = 0;
                                for (char c12 = 0; c12 < c6; c12 = 1) {
                                    iC2 += n2.c(b1VarD, n2.f14409f);
                                }
                                for (int i30 = 0; i30 <= 0; i30++) {
                                    if (c6 != 0) {
                                        atomicInteger.set(n2.c(b1VarD, n2.f14410g));
                                    }
                                    iC2 += atomicInteger.get() != 0 ? n2.c(b1VarD, n2.f14411h) : 0;
                                }
                                int i31 = i25 + iC2;
                                androidx.media3.extractor.w wVar = new androidx.media3.extractor.w(MimeTypes.AUDIO_DTS_X, 2, i10, i31, jR);
                                if (this.f12440m == 3) {
                                    f(wVar);
                                }
                                this.f12439l = i31;
                                if (jR == -9223372036854775807L) {
                                    jR = 0;
                                }
                                this.f12437j = jR;
                                p01Var2.C(0);
                                this.f12433f.a(p01Var2, this.f12442o);
                                this.f12434g = 6;
                            } else {
                                continue;
                            }
                        } else if (e(p01Var, p01Var2.f15225a, 6)) {
                            androidx.media3.extractor.b1 b1VarD2 = n2.d(p01Var2.f15225a);
                            b1VarD2.n(32);
                            int iC3 = n2.c(b1VarD2, n2.f14412i) + 1;
                            this.f12442o = iC3;
                            int i32 = this.f12435h;
                            if (i32 > iC3) {
                                int i33 = i32 - iC3;
                                this.f12435h = i32 - i33;
                                p01Var.C(p01Var.f15226b - i33);
                            }
                            this.f12434g = 5;
                        }
                    } else if (e(p01Var, p01Var2.f15225a, this.f12441n)) {
                        androidx.media3.extractor.b1 b1VarD3 = n2.d(p01Var2.f15225a);
                        b1VarD3.n(40);
                        int iP8 = b1VarD3.p(2);
                        boolean zO = b1VarD3.o();
                        int i34 = 1;
                        int i35 = true != zO ? 16 : 20;
                        b1VarD3.n(true != zO ? 8 : 12);
                        int iP9 = b1VarD3.p(i35) + 1;
                        boolean zO2 = b1VarD3.o();
                        if (zO2) {
                            iP = b1VarD3.p(2);
                            int iP10 = b1VarD3.p(3) + 1;
                            if (b1VarD3.o()) {
                                b1VarD3.n(36);
                            }
                            int iP11 = b1VarD3.p(3) + 1;
                            int iP12 = b1VarD3.p(3) + 1;
                            if (iP11 != 1 || iP12 != 1) {
                                throw zzat.zzc("Multiple audio presentations or assets not supported");
                            }
                            int i36 = iP8 + 1;
                            int iP13 = b1VarD3.p(i36);
                            int i37 = 0;
                            while (i37 < i36) {
                                if (((iP13 >> i37) & 1) == i34) {
                                    b1VarD3.n(8);
                                }
                                i37++;
                                i34 = 1;
                            }
                            i13 = 512 * iP10;
                            if (b1VarD3.o()) {
                                b1VarD3.n(2);
                                int iP14 = (b1VarD3.p(2) + 1) << 2;
                                int iP15 = b1VarD3.p(2) + 1;
                                for (int i38 = 0; i38 < iP15; i38++) {
                                    b1VarD3.n(iP14);
                                }
                            }
                        } else {
                            i13 = 0;
                            iP = -1;
                        }
                        b1VarD3.n(i35);
                        b1VarD3.n(12);
                        if (zO2) {
                            if (b1VarD3.o()) {
                                b1VarD3.n(4);
                            }
                            if (b1VarD3.o()) {
                                b1VarD3.n(24);
                            }
                            if (b1VarD3.o()) {
                                i14 = 1;
                                b1VarD3.u(b1VarD3.p(10) + 1);
                            } else {
                                i14 = 1;
                            }
                            b1VarD3.n(5);
                            i15 = n2.f14407d[b1VarD3.p(4)];
                            iP2 = b1VarD3.p(8) + 1;
                        } else {
                            i14 = 1;
                            iP2 = -1;
                            i15 = C.RATE_UNSET_INT;
                        }
                        if (zO2) {
                            if (iP == 0) {
                                i16 = 32000;
                            } else if (iP == i14) {
                                i16 = 44100;
                            } else {
                                if (iP != 2) {
                                    StringBuilder sb3 = new StringBuilder(String.valueOf(iP).length() + 51);
                                    sb3.append("Unsupported reference clock code in DTS HD header: ");
                                    sb3.append(iP);
                                    throw zzat.zzb(sb3.toString(), null);
                                }
                                i16 = 48000;
                            }
                            jR2 = bb1.r(i13, 1000000L, i16, RoundingMode.DOWN);
                        } else {
                            jR2 = -9223372036854775807L;
                        }
                        f(new androidx.media3.extractor.w(MimeTypes.AUDIO_DTS_EXPRESS, iP2, i15, iP9, jR2));
                        this.f12439l = iP9;
                        if (jR2 == -9223372036854775807L) {
                            jR2 = 0;
                        }
                        this.f12437j = jR2;
                        p01Var2.C(0);
                        this.f12433f.a(p01Var2, this.f12441n);
                        this.f12434g = 6;
                    } else {
                        continue;
                    }
                } else if (e(p01Var, p01Var2.f15225a, 7)) {
                    androidx.media3.extractor.b1 b1VarD4 = n2.d(p01Var2.f15225a);
                    b1VarD4.n(42);
                    this.f12441n = b1VarD4.p(true == b1VarD4.o() ? 12 : 8) + 1;
                    this.f12434g = 3;
                }
            } else if (e(p01Var, p01Var2.f15225a, 18)) {
                byte[] bArr3 = p01Var2.f15225a;
                if (this.f12438k == null) {
                    zn2 zn2VarZzb = n2.zzb(bArr3, this.f12432e, this.zzc, this.f12430c, this.f12431d, null);
                    this.f12438k = zn2VarZzb;
                    this.f12433f.d(zn2VarZzb);
                }
                this.f12439l = n2.b(bArr3);
                byte b12 = bArr3[0];
                if (b12 != -2) {
                    if (b12 == -1) {
                        i19 = (bArr3[4] & 7) << 4;
                        b10 = bArr3[7];
                    } else if (b12 != 31) {
                        i19 = (bArr3[4] & 1) << 6;
                        i20 = bArr3[5] & 252;
                        i18 = (i20 >> 2) | i19;
                        i17 = 1;
                    } else {
                        i19 = (bArr3[5] & 7) << 4;
                        b10 = bArr3[6];
                    }
                    i20 = b10 & 60;
                    i18 = (i20 >> 2) | i19;
                    i17 = 1;
                } else {
                    i17 = 1;
                    i18 = ((bArr3[4] & 252) >> 2) | ((bArr3[5] & 1) << 6);
                }
                this.f12437j = za2.b(bb1.q(this.f12438k.f19349v, (i18 + i17) * 32));
                p01Var2.C(0);
                this.f12433f.a(p01Var2, 18);
                this.f12434g = 6;
            }
        }
    }

    public final boolean e(p01 p01Var, byte[] bArr, int i10) {
        int iMin = Math.min(p01Var.y(), i10 - this.f12435h);
        p01Var.F(this.f12435h, iMin, bArr);
        int i11 = this.f12435h + iMin;
        this.f12435h = i11;
        return i11 == i10;
    }

    public final void f(androidx.media3.extractor.w wVar) {
        int i10;
        int i11 = wVar.f3488b;
        if (i11 == -2147483647 || (i10 = wVar.f3489c) == -1) {
            return;
        }
        zn2 zn2Var = this.f12438k;
        String str = wVar.f3487a;
        if (zn2Var != null && i10 == zn2Var.f19348u && i11 == zn2Var.f19349v && Objects.equals(str, zn2Var.zzo)) {
            return;
        }
        zn2 zn2Var2 = this.f12438k;
        sm2 sm2Var = zn2Var2 == null ? new sm2() : new sm2(zn2Var2);
        sm2Var.zza(this.f12432e);
        sm2Var.zzl(this.f12431d);
        sm2Var.zzm(str);
        sm2Var.f16496s = i10;
        sm2Var.f16497t = i11;
        sm2Var.zze(this.zzc);
        sm2Var.f16480c = this.f12430c;
        zn2 zn2Var3 = new zn2(sm2Var);
        this.f12438k = zn2Var3;
        this.f12433f.d(zn2Var3);
    }

    @Override // com.google.android.gms.internal.ads.ja
    public final void zza() {
        this.f12434g = 0;
        this.f12435h = 0;
        this.f12436i = 0;
        this.f12443p = -9223372036854775807L;
        this.f12429b.set(0);
    }
}
