package A1;

import d5.AbstractC0844a;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class z implements N {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final T0.H f367b = new T0.H(10, new byte[10]);

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public x0.t f370e;

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

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

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

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

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

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

    public z(InterfaceC0008i interfaceC0008i) {
        this.f366a = interfaceC0008i;
    }

    @Override // A1.N
    public final void a() {
        this.f368c = 0;
        this.f369d = 0;
        this.f373h = false;
        this.f366a.a();
    }

    @Override // A1.N
    public final void b(x0.t tVar, T0.o oVar, M m8) {
        this.f370e = tVar;
        this.f366a.g(oVar, m8);
    }

    @Override // A1.N
    public final void c(int i, x0.o oVar) {
        AbstractC1709a.k(this.f370e);
        int i5 = i & 1;
        InterfaceC0008i interfaceC0008i = this.f366a;
        int i8 = -1;
        int i9 = 2;
        if (i5 != 0) {
            int i10 = this.f368c;
            if (i10 != 0 && i10 != 1) {
                if (i10 == 2) {
                    AbstractC1709a.B("PesReader", "Unexpected start indicator reading extended header");
                } else {
                    if (i10 != 3) {
                        throw new IllegalStateException();
                    }
                    if (this.f374j != -1) {
                        AbstractC1709a.B("PesReader", "Unexpected start indicator: expected " + this.f374j + " more bytes");
                    }
                    interfaceC0008i.f(oVar.f18541c == 0);
                }
            }
            this.f368c = 1;
            this.f369d = 0;
        }
        int i11 = i;
        while (oVar.a() > 0) {
            int i12 = this.f368c;
            if (i12 != 0) {
                T0.H h8 = this.f367b;
                if (i12 != 1) {
                    if (i12 == i9) {
                        if (d(oVar, h8.f6361d, Math.min(10, this.i)) && d(oVar, null, this.i)) {
                            h8.q(0);
                            this.f376l = -9223372036854775807L;
                            if (this.f371f) {
                                h8.t(4);
                                long jI = ((long) h8.i(3)) << 30;
                                h8.t(1);
                                long jI2 = ((long) (h8.i(15) << 15)) | jI;
                                h8.t(1);
                                long jI3 = jI2 | ((long) h8.i(15));
                                h8.t(1);
                                if (!this.f373h && this.f372g) {
                                    h8.t(4);
                                    long jI4 = ((long) h8.i(3)) << 30;
                                    h8.t(1);
                                    long jI5 = jI4 | ((long) (h8.i(15) << 15));
                                    h8.t(1);
                                    long jI6 = jI5 | ((long) h8.i(15));
                                    h8.t(1);
                                    this.f370e.b(jI6);
                                    this.f373h = true;
                                }
                                this.f376l = this.f370e.b(jI3);
                            }
                            i11 |= this.f375k ? 4 : 0;
                            interfaceC0008i.h(this.f376l, i11);
                            this.f368c = 3;
                            this.f369d = 0;
                        }
                    } else {
                        if (i12 != 3) {
                            throw new IllegalStateException();
                        }
                        int iA = oVar.a();
                        int i13 = this.f374j;
                        int i14 = i13 == i8 ? 0 : iA - i13;
                        if (i14 > 0) {
                            iA -= i14;
                            oVar.E(oVar.f18540b + iA);
                        }
                        interfaceC0008i.e(oVar);
                        int i15 = this.f374j;
                        if (i15 != i8) {
                            int i16 = i15 - iA;
                            this.f374j = i16;
                            if (i16 == 0) {
                                interfaceC0008i.f(false);
                                this.f368c = 1;
                                this.f369d = 0;
                            }
                        }
                    }
                } else if (d(oVar, h8.f6361d, 9)) {
                    this.f368c = e() ? 2 : 0;
                    this.f369d = 0;
                }
            } else {
                oVar.G(oVar.a());
            }
            i8 = -1;
            i9 = 2;
        }
    }

    public final boolean d(x0.o oVar, byte[] bArr, int i) {
        int iMin = Math.min(oVar.a(), i - this.f369d);
        if (iMin <= 0) {
            return true;
        }
        if (bArr == null) {
            oVar.G(iMin);
        } else {
            oVar.e(bArr, this.f369d, iMin);
        }
        int i5 = this.f369d + iMin;
        this.f369d = i5;
        return i5 == i;
    }

    public final boolean e() {
        T0.H h8 = this.f367b;
        h8.q(0);
        int i = h8.i(24);
        if (i != 1) {
            AbstractC0844a.s(i, "Unexpected start code prefix: ", "PesReader");
            this.f374j = -1;
            return false;
        }
        h8.t(8);
        int i5 = h8.i(16);
        h8.t(5);
        this.f375k = h8.h();
        h8.t(2);
        this.f371f = h8.h();
        this.f372g = h8.h();
        h8.t(6);
        int i8 = h8.i(8);
        this.i = i8;
        if (i5 == 0) {
            this.f374j = -1;
        } else {
            int i9 = (i5 - 3) - i8;
            this.f374j = i9;
            if (i9 < 0) {
                AbstractC1709a.B("PesReader", "Found negative packet payload size: " + this.f374j);
                this.f374j = -1;
            }
        }
        return true;
    }
}
