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

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.view.Choreographer;
import android.view.Surface;
import androidx.annotation.Nullable;
import com.google.firebase.messaging.Constants$ScionAnalytics$MessageType;

/* JADX INFO: loaded from: classes2.dex */
public final class i1 {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Context f12302b;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @Nullable
    private f1 zzc;

    @Nullable
    private Surface zze;

    public i1(Context context) {
        this.f12302b = context;
        l0 l0Var = new l0();
        l0Var.f13442a = new k0();
        l0Var.f13443b = new k0();
        l0Var.f13445d = -9223372036854775807L;
        this.f12301a = l0Var;
        this.f12304d = -1.0f;
        this.f12307g = 1.0f;
        this.f12308h = 0;
    }

    public final void a() {
        this.f12303c = true;
        d();
        DisplayManager displayManager = (DisplayManager) this.f12302b.getSystemService(Constants$ScionAnalytics$MessageType.DISPLAY_NOTIFICATION);
        f1 h1Var = null;
        if (displayManager != null) {
            try {
                Choreographer choreographer = Choreographer.getInstance();
                h1Var = Build.VERSION.SDK_INT >= 33 ? new h1(choreographer, displayManager) : new g1(choreographer, displayManager);
            } catch (RuntimeException e10) {
                vt0.zzd("VideoFrameReleaseHelper", "Vsync sampling disabled due to platform error", e10);
            }
        }
        this.zzc = h1Var;
        if (h1Var != null) {
            h1Var.a();
        }
        f(false);
    }

    public final void b() {
        this.f12303c = false;
        f1 f1Var = this.zzc;
        if (f1Var != null) {
            f1Var.b();
        }
        g();
    }

    /* JADX WARN: Removed duplicated region for block: B:47:0x00c9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long c(long j10, long j11) {
        long j12;
        long j13;
        long j14;
        float f10;
        long j15;
        long j16;
        long j17 = 0;
        if (this.f12315o != -1) {
            l0 l0Var = this.f12301a;
            if (l0Var.f13442a.b()) {
                if (l0Var.f13442a.b()) {
                    k0 k0Var = l0Var.f13442a;
                    long j18 = k0Var.f13054e;
                    j16 = j18 == 0 ? 0L : k0Var.f13055f / j18;
                } else {
                    j16 = -9223372036854775807L;
                }
                j15 = (this.f12311k - this.f12315o) * j16;
                f10 = this.f12307g;
            } else {
                long j19 = j11 - this.f12317q;
                f10 = this.f12307g;
                j15 = j19 * 1000;
            }
            j12 = this.f12316p + ((long) (j15 / f10));
            if (Math.abs(j10 - j12) > 20000000) {
                d();
                j12 = j10;
            }
        } else {
            j12 = j10;
        }
        this.f12312l = this.f12311k;
        this.f12313m = j12;
        this.f12314n = j11;
        f1 f1Var = this.zzc;
        if (f1Var != null) {
            long j20 = f1Var.f10983c;
            long j21 = this.zzc.f10984d;
            if (j20 != -9223372036854775807L && j21 != -9223372036854775807L) {
                long j22 = (((j12 - j20) / j21) * j21) + j20;
                if (j12 <= j22) {
                    j13 = j22 - j21;
                } else {
                    j13 = j22;
                    j22 += j21;
                }
                long j23 = j21 / 2;
                long j24 = j22 - j12;
                long j25 = j12 - j13;
                long jAbs = Math.abs(j24 - j25);
                if (jAbs < j23) {
                    long j26 = j21 / 4;
                    j14 = j13;
                    if (jAbs < j26) {
                        long j27 = this.f12309i;
                        if (j27 != 0) {
                            this.f12310j = j27;
                            j17 = j27;
                        } else {
                            j17 = j24 < j25 ? -j26 : j26;
                        }
                    }
                    if (j24 + j17 >= j25) {
                        j22 = j14;
                    }
                    return j22 - ((j21 * 80) / 100);
                }
                j14 = j13;
                j17 = this.f12309i;
                this.f12310j = j17;
                if (j24 + j17 >= j25) {
                }
                return j22 - ((j21 * 80) / 100);
            }
        }
        return j12;
    }

    public final void d() {
        this.f12311k = 0L;
        this.f12315o = -1L;
        this.f12312l = -1L;
        this.f12309i = 0L;
        this.f12310j = 0L;
    }

    public final void e() {
        float f10;
        if (Build.VERSION.SDK_INT < 30 || this.zze == null) {
            return;
        }
        l0 l0Var = this.f12301a;
        if (!l0Var.f13442a.b()) {
            f10 = this.f12304d;
        } else if (l0Var.f13442a.b()) {
            f10 = (float) (1.0E9d / (l0Var.f13442a.f13054e != 0 ? r2.f13055f / r4 : 0L));
        } else {
            f10 = -1.0f;
        }
        float f11 = this.f12305e;
        if (f10 != f11) {
            if (f10 != -1.0f && f11 != -1.0f) {
                float f12 = 1.0f;
                if (l0Var.f13442a.b()) {
                    if ((l0Var.f13442a.b() ? l0Var.f13442a.f13055f : -9223372036854775807L) >= 5000000000L) {
                        f12 = 0.1f;
                    }
                }
                if (Math.abs(f10 - this.f12305e) < f12) {
                    return;
                }
            } else if (f10 == -1.0f && l0Var.f13446e < 30) {
                return;
            }
            this.f12305e = f10;
            f(false);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x0028  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void f(boolean z10) {
        Surface surface;
        float f10;
        if (Build.VERSION.SDK_INT < 30 || (surface = this.zze) == null || this.f12308h == Integer.MIN_VALUE || !surface.isValid()) {
            return;
        }
        if (this.f12303c) {
            float f11 = this.f12305e;
            f10 = f11 != -1.0f ? f11 * this.f12307g : 0.0f;
        }
        if (z10 || this.f12306f != f10) {
            this.f12306f = f10;
            try {
                this.zze.setFrameRate(f10, f10 == 0.0f ? 0 : 1);
            } catch (IllegalStateException e10) {
                vt0.zzf("VideoFrameReleaseHelper", "Failed to call Surface.setFrameRate", e10);
            }
        }
    }

    public final void g() {
        Surface surface;
        if (Build.VERSION.SDK_INT < 30 || (surface = this.zze) == null || this.f12308h == Integer.MIN_VALUE || this.f12306f == 0.0f || !surface.isValid()) {
            return;
        }
        this.f12306f = 0.0f;
        try {
            this.zze.setFrameRate(0.0f, 0);
        } catch (IllegalStateException e10) {
            vt0.zzf("VideoFrameReleaseHelper", "Failed to call Surface.setFrameRate", e10);
        }
    }

    public final void zzc(@Nullable Surface surface) {
        if (this.zze == surface) {
            return;
        }
        g();
        this.zze = surface;
        f(true);
    }
}
