package x2;

import androidx.annotation.Nullable;
import androidx.media3.common.ColorInfo;
import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.CodecSpecificDataUtil;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.z0;
import java.util.Collections;

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

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public z0 f34629c;

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final boolean[] f34632f = new boolean[3];

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final z f34633g = new z(32);

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final z f34634h = new z(33);

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final z f34635i = new z(34);

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final z f34636j = new z(39);

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final z f34637k = new z(40);

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final ParsableByteArray f34640n = new ParsableByteArray();

    public t(h0 h0Var) {
        this.f34627a = h0Var;
    }

    private static Format parseMediaFormat(@Nullable String str, z zVar, z zVar2, z zVar3, String str2) {
        int i10 = zVar.f34709e;
        byte[] bArr = new byte[zVar2.f34709e + i10 + zVar3.f34709e];
        System.arraycopy(zVar.f34708d, 0, bArr, 0, i10);
        System.arraycopy(zVar2.f34708d, 0, bArr, zVar.f34709e, zVar2.f34709e);
        System.arraycopy(zVar3.f34708d, 0, bArr, zVar.f34709e + zVar2.f34709e, zVar3.f34709e);
        q1.p h265SpsNalUnit = q1.v.parseH265SpsNalUnit(zVar2.f34708d, 3, zVar2.f34709e, null);
        q1.k kVar = h265SpsNalUnit.profileTierLevel;
        return new Format.Builder().setId(str).setContainerMimeType(str2).setSampleMimeType(MimeTypes.VIDEO_H265).setCodecs(kVar != null ? CodecSpecificDataUtil.buildHevcCodecString(kVar.f32108a, kVar.f32109b, kVar.f32110c, kVar.f32111d, kVar.f32112e, kVar.f32113f) : null).setWidth(h265SpsNalUnit.f32127d).setHeight(h265SpsNalUnit.f32128e).setDecodedWidth(h265SpsNalUnit.f32129f).setDecodedHeight(h265SpsNalUnit.f32130g).setColorInfo(new ColorInfo.Builder().setColorSpace(h265SpsNalUnit.f32133j).setColorRange(h265SpsNalUnit.f32134k).setColorTransfer(h265SpsNalUnit.f32135l).setLumaBitdepth(h265SpsNalUnit.f32125b + 8).setChromaBitdepth(h265SpsNalUnit.f32126c + 8).build()).setPixelWidthHeightRatio(h265SpsNalUnit.f32131h).setMaxNumReorderSamples(h265SpsNalUnit.f32132i).setMaxSubLayers(h265SpsNalUnit.f32124a + 1).setInitializationData(Collections.singletonList(bArr)).build();
    }

    @Override // x2.j
    public final void a() {
        this.f34638l = 0L;
        this.f34639m = -9223372036854775807L;
        q1.v.a(this.f34632f);
        this.f34633g.c();
        this.f34634h.c();
        this.f34635i.c();
        this.f34636j.c();
        this.f34637k.c();
        this.f34627a.f34465c.b(0);
        s sVar = this.f34630d;
        if (sVar != null) {
            sVar.f34618e = false;
            sVar.f34619f = false;
            sVar.f34620g = false;
            sVar.f34621h = false;
            sVar.f34622i = false;
        }
    }

    public final void b(int i10, int i11, long j10, long j11) {
        s sVar = this.f34630d;
        boolean z10 = this.f34631e;
        if (sVar.f34622i && sVar.f34619f) {
            sVar.f34625l = sVar.f34615b;
            sVar.f34622i = false;
        } else if (sVar.f34620g || sVar.f34619f) {
            if (z10 && sVar.f34621h) {
                sVar.a(i10 + ((int) (j10 - sVar.f34614a)));
            }
            sVar.f34623j = sVar.f34614a;
            sVar.f34624k = sVar.f34617d;
            sVar.f34625l = sVar.f34615b;
            sVar.f34621h = true;
        }
        boolean z11 = this.f34631e;
        h0 h0Var = this.f34627a;
        if (!z11) {
            z zVar = this.f34633g;
            zVar.b(i11);
            z zVar2 = this.f34634h;
            zVar2.b(i11);
            z zVar3 = this.f34635i;
            zVar3.b(i11);
            if (zVar.f34707c && zVar2.f34707c && zVar3.f34707c) {
                Format mediaFormat = parseMediaFormat(this.f34628b, zVar, zVar2, zVar3, MimeTypes.VIDEO_MP2T);
                this.f34629c.format(mediaFormat);
                int i12 = mediaFormat.maxNumReorderSamples;
                if (i12 == -1) {
                    throw new IllegalStateException();
                }
                h0Var.f34465c.c(i12);
                this.f34631e = true;
            }
        }
        z zVar4 = this.f34636j;
        boolean zB = zVar4.b(i11);
        ParsableByteArray parsableByteArray = this.f34640n;
        if (zB) {
            parsableByteArray.reset(zVar4.f34708d, q1.v.i(zVar4.f34709e, zVar4.f34708d));
            parsableByteArray.skipBytes(5);
            h0Var.f34465c.a(parsableByteArray, j11);
        }
        z zVar5 = this.f34637k;
        if (zVar5.b(i11)) {
            parsableByteArray.reset(zVar5.f34708d, q1.v.i(zVar5.f34709e, zVar5.f34708d));
            parsableByteArray.skipBytes(5);
            h0Var.f34465c.a(parsableByteArray, j11);
        }
    }

    @Override // x2.j
    public final void c(ParsableByteArray parsableByteArray) {
        int i10;
        Assertions.checkStateNotNull(this.f34629c);
        Util.castNonNull(this.f34630d);
        while (parsableByteArray.bytesLeft() > 0) {
            int position = parsableByteArray.getPosition();
            int iLimit = parsableByteArray.limit();
            byte[] data = parsableByteArray.getData();
            this.f34638l += (long) parsableByteArray.bytesLeft();
            this.f34629c.sampleData(parsableByteArray, parsableByteArray.bytesLeft());
            while (position < iLimit) {
                int iB = q1.v.b(data, position, iLimit, this.f34632f);
                if (iB == iLimit) {
                    g(position, iLimit, data);
                    return;
                }
                int i11 = (data[iB + 3] & 126) >> 1;
                if (iB <= 0 || data[iB - 1] != 0) {
                    i10 = 3;
                } else {
                    iB--;
                    i10 = 4;
                }
                int i12 = iB;
                int i13 = i10;
                int i14 = i12 - position;
                if (i14 > 0) {
                    g(position, i12, data);
                }
                int i15 = iLimit - i12;
                long j10 = this.f34638l - ((long) i15);
                b(i15, i14 < 0 ? -i14 : 0, j10, this.f34639m);
                h(i15, i11, j10, this.f34639m);
                position = i12 + i13;
            }
        }
    }

    @Override // x2.j
    public final void d(boolean z10) {
        Assertions.checkStateNotNull(this.f34629c);
        Util.castNonNull(this.f34630d);
        if (z10) {
            this.f34627a.f34465c.b(0);
            b(0, 0, this.f34638l, this.f34639m);
            h(0, 48, this.f34638l, this.f34639m);
        }
    }

    @Override // x2.j
    public final void e(int i10, long j10) {
        this.f34639m = j10;
    }

    @Override // x2.j
    public final void f(androidx.media3.extractor.b0 b0Var, o0 o0Var) {
        o0Var.a();
        o0Var.b();
        this.f34628b = o0Var.f34561e;
        o0Var.b();
        z0 z0VarTrack = b0Var.track(o0Var.f34560d, 2);
        this.f34629c = z0VarTrack;
        this.f34630d = new s(z0VarTrack);
        this.f34627a.a(b0Var, o0Var);
    }

    public final void g(int i10, int i11, byte[] bArr) {
        s sVar = this.f34630d;
        if (sVar.f34618e) {
            int i12 = sVar.f34616c;
            int i13 = (i10 + 2) - i12;
            if (i13 < i11) {
                sVar.f34619f = (bArr[i13] & 128) != 0;
                sVar.f34618e = false;
            } else {
                sVar.f34616c = (i11 - i10) + i12;
            }
        }
        if (!this.f34631e) {
            this.f34633g.a(i10, i11, bArr);
            this.f34634h.a(i10, i11, bArr);
            this.f34635i.a(i10, i11, bArr);
        }
        this.f34636j.a(i10, i11, bArr);
        this.f34637k.a(i10, i11, bArr);
    }

    public final void h(int i10, int i11, long j10, long j11) {
        s sVar = this.f34630d;
        boolean z10 = this.f34631e;
        sVar.f34619f = false;
        sVar.f34620g = false;
        sVar.f34617d = j11;
        sVar.f34616c = 0;
        sVar.f34614a = j10;
        if (i11 >= 32 && i11 != 40) {
            if (sVar.f34621h && !sVar.f34622i) {
                if (z10) {
                    sVar.a(i10);
                }
                sVar.f34621h = false;
            }
            if ((32 <= i11 && i11 <= 35) || i11 == 39) {
                sVar.f34620g = !sVar.f34622i;
                sVar.f34622i = true;
            }
        }
        boolean z11 = i11 >= 16 && i11 <= 21;
        sVar.f34615b = z11;
        sVar.f34618e = z11 || i11 <= 9;
        if (!this.f34631e) {
            this.f34633g.d(i11);
            this.f34634h.d(i11);
            this.f34635i.d(i11);
        }
        this.f34636j.d(i11);
        this.f34637k.d(i11);
    }
}
