package x2;

import android.util.Pair;
import androidx.annotation.Nullable;
import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.z0;
import java.util.Arrays;
import java.util.Collections;

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public static final double[] f34512o = {23.976023976023978d, 24.0d, 25.0d, 29.97002997002997d, 30.0d, 50.0d, 59.94005994005994d, 60.0d};

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final boolean[] f34516d = new boolean[4];

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final k f34517e;

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

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

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

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

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

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

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

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

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

    @Nullable
    private final z userData;

    @Nullable
    private final ParsableByteArray userDataParsable;

    @Nullable
    private final q0 userDataReader;

    public l(@Nullable q0 q0Var, String str) {
        this.userDataReader = q0Var;
        this.f34515c = str;
        k kVar = new k();
        kVar.f34491d = new byte[128];
        this.f34517e = kVar;
        if (q0Var != null) {
            this.userData = new z(178);
            this.userDataParsable = new ParsableByteArray();
        } else {
            this.userData = null;
            this.userDataParsable = null;
        }
        this.f34522j = -9223372036854775807L;
        this.f34524l = -9223372036854775807L;
    }

    @Override // x2.j
    public final void a() {
        q1.v.a(this.f34516d);
        k kVar = this.f34517e;
        kVar.f34488a = false;
        kVar.f34489b = 0;
        kVar.f34490c = 0;
        z zVar = this.userData;
        if (zVar != null) {
            zVar.c();
        }
        this.f34518f = 0L;
        this.f34519g = false;
        this.f34522j = -9223372036854775807L;
        this.f34524l = -9223372036854775807L;
    }

    /* JADX WARN: Removed duplicated region for block: B:44:0x011f  */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    @Override // x2.j
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c(ParsableByteArray parsableByteArray) {
        k kVar;
        boolean z10;
        boolean z11;
        int i10;
        float f10;
        int i11;
        float f11;
        int i12;
        long j10;
        Assertions.checkStateNotNull(this.f34514b);
        int position = parsableByteArray.getPosition();
        int iLimit = parsableByteArray.limit();
        byte[] data = parsableByteArray.getData();
        this.f34518f += (long) parsableByteArray.bytesLeft();
        this.f34514b.sampleData(parsableByteArray, parsableByteArray.bytesLeft());
        while (true) {
            int iB = q1.v.b(data, position, iLimit, this.f34516d);
            kVar = this.f34517e;
            if (iB == iLimit) {
                break;
            }
            int i13 = iB + 3;
            int i14 = parsableByteArray.getData()[i13] & 255;
            int i15 = iB - position;
            if (!this.f34520h) {
                if (i15 > 0) {
                    kVar.a(position, iB, data);
                }
                int i16 = i15 < 0 ? -i15 : 0;
                if (kVar.f34488a) {
                    int i17 = kVar.f34489b - i16;
                    kVar.f34489b = i17;
                    if (kVar.f34490c == 0 && i14 == 181) {
                        kVar.f34490c = i17;
                    } else {
                        kVar.f34488a = false;
                        String str = (String) Assertions.checkNotNull(this.f34513a);
                        byte[] bArrCopyOf = Arrays.copyOf(kVar.f34491d, kVar.f34489b);
                        int i18 = bArrCopyOf[4] & 255;
                        byte b10 = bArrCopyOf[5];
                        int i19 = (i18 << 4) | ((b10 & 255) >> 4);
                        int i20 = (bArrCopyOf[6] & 255) | ((b10 & 15) << 8);
                        int i21 = (bArrCopyOf[7] & 240) >> 4;
                        if (i21 == 2) {
                            f10 = i20 * 4;
                            i11 = i19 * 3;
                        } else if (i21 == 3) {
                            f10 = i20 * 16;
                            i11 = i19 * 9;
                        } else if (i21 != 4) {
                            f11 = 1.0f;
                            Format formatBuild = new Format.Builder().setId(str).setContainerMimeType(this.f34515c).setSampleMimeType(MimeTypes.VIDEO_MPEG2).setWidth(i19).setHeight(i20).setPixelWidthHeightRatio(f11).setInitializationData(Collections.singletonList(bArrCopyOf)).build();
                            i12 = (bArrCopyOf[7] & 15) - 1;
                            if (i12 >= 0 || i12 >= 8) {
                                j10 = 0;
                            } else {
                                double d10 = f34512o[i12];
                                byte b11 = bArrCopyOf[kVar.f34490c + 9];
                                int i22 = (b11 & 96) >> 5;
                                int i23 = b11 & 31;
                                if (i22 != i23) {
                                    d10 *= (((double) i22) + 1.0d) / ((double) (i23 + 1));
                                }
                                j10 = (long) (1000000.0d / d10);
                            }
                            Pair pairCreate = Pair.create(formatBuild, Long.valueOf(j10));
                            this.f34514b.format((Format) pairCreate.first);
                            this.f34521i = ((Long) pairCreate.second).longValue();
                            this.f34520h = true;
                        } else {
                            f10 = i20 * 121;
                            i11 = i19 * 100;
                        }
                        f11 = f10 / i11;
                        Format formatBuild2 = new Format.Builder().setId(str).setContainerMimeType(this.f34515c).setSampleMimeType(MimeTypes.VIDEO_MPEG2).setWidth(i19).setHeight(i20).setPixelWidthHeightRatio(f11).setInitializationData(Collections.singletonList(bArrCopyOf)).build();
                        i12 = (bArrCopyOf[7] & 15) - 1;
                        if (i12 >= 0) {
                            j10 = 0;
                            Pair pairCreate2 = Pair.create(formatBuild2, Long.valueOf(j10));
                            this.f34514b.format((Format) pairCreate2.first);
                            this.f34521i = ((Long) pairCreate2.second).longValue();
                            this.f34520h = true;
                        }
                    }
                } else if (i14 == 179) {
                    kVar.f34488a = true;
                }
                kVar.a(0, 3, k.f34487e);
            }
            z zVar = this.userData;
            if (zVar != null) {
                if (i15 > 0) {
                    zVar.a(position, iB, data);
                    i10 = 0;
                } else {
                    i10 = -i15;
                }
                if (this.userData.b(i10)) {
                    z zVar2 = this.userData;
                    ((ParsableByteArray) Util.castNonNull(this.userDataParsable)).reset(this.userData.f34708d, q1.v.i(zVar2.f34709e, zVar2.f34708d));
                    ((q0) Util.castNonNull(this.userDataReader)).a(this.userDataParsable, this.f34524l);
                }
                if (i14 == 178 && parsableByteArray.getData()[iB + 2] == 1) {
                    this.userData.d(i14);
                }
            }
            if (i14 == 0 || i14 == 179) {
                int i24 = iLimit - iB;
                if (this.f34526n && this.f34520h) {
                    long j11 = this.f34524l;
                    if (j11 != -9223372036854775807L) {
                        this.f34514b.sampleMetadata(j11, this.f34525m ? 1 : 0, ((int) (this.f34518f - this.f34523k)) - i24, i24, null);
                    }
                }
                if (!this.f34519g || this.f34526n) {
                    this.f34523k = this.f34518f - ((long) i24);
                    long j12 = this.f34522j;
                    if (j12 == -9223372036854775807L) {
                        long j13 = this.f34524l;
                        j12 = j13 != -9223372036854775807L ? j13 + this.f34521i : -9223372036854775807L;
                    }
                    this.f34524l = j12;
                    z10 = false;
                    this.f34525m = false;
                    this.f34522j = -9223372036854775807L;
                    z11 = true;
                    this.f34519g = true;
                } else {
                    z10 = false;
                    z11 = true;
                }
                this.f34526n = i14 == 0 ? z11 : z10;
            } else if (i14 == 184) {
                this.f34525m = true;
            }
            position = i13;
        }
        if (!this.f34520h) {
            kVar.a(position, iLimit, data);
        }
        z zVar3 = this.userData;
        if (zVar3 != null) {
            zVar3.a(position, iLimit, data);
        }
    }

    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    @Override // x2.j
    public final void d(boolean z10) {
        Assertions.checkStateNotNull(this.f34514b);
        if (z10) {
            boolean z11 = this.f34525m;
            this.f34514b.sampleMetadata(this.f34524l, z11 ? 1 : 0, (int) (this.f34518f - this.f34523k), 0, null);
        }
    }

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

    @Override // x2.j
    public final void f(androidx.media3.extractor.b0 b0Var, o0 o0Var) {
        o0Var.a();
        o0Var.b();
        this.f34513a = o0Var.f34561e;
        o0Var.b();
        this.f34514b = b0Var.track(o0Var.f34560d, 2);
        q0 q0Var = this.userDataReader;
        if (q0Var != null) {
            q0Var.b(b0Var, o0Var);
        }
    }
}
