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

import android.graphics.Bitmap;
import android.graphics.Rect;
import androidx.annotation.Nullable;
import java.util.Arrays;

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

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

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

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

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

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

    @Nullable
    private Rect zzg;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final int[] f14478a = new int[4];

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

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

    public static int c(int i10, int i11) {
        return (i10 & 16777215) | ((i11 * 17) << 24);
    }

    public final void a(p01 p01Var) {
        int[] iArr = this.f14481d;
        if (iArr == null || !this.f14479b) {
            return;
        }
        p01Var.E(p01Var.J() - 2);
        int iJ = p01Var.J();
        while (p01Var.f15226b < iJ && p01Var.y() > 0) {
            int I = p01Var.I();
            int[] iArr2 = this.f14478a;
            switch (I) {
                case 0:
                case 1:
                case 2:
                    break;
                case 3:
                    if (p01Var.y() < 2) {
                        return;
                    }
                    int I2 = p01Var.I();
                    int I3 = p01Var.I();
                    int i10 = I2 >> 4;
                    if (i10 >= iArr.length) {
                        i10 = 0;
                    }
                    iArr2[3] = iArr[i10];
                    int i11 = I2 & 15;
                    if (i11 >= iArr.length) {
                        i11 = 0;
                    }
                    iArr2[2] = iArr[i11];
                    int i12 = I3 >> 4;
                    if (i12 >= iArr.length) {
                        i12 = 0;
                    }
                    iArr2[1] = iArr[i12];
                    int i13 = I3 & 15;
                    if (i13 >= iArr.length) {
                        i13 = 0;
                    }
                    iArr2[0] = iArr[i13];
                    this.f14480c = true;
                    break;
                case 4:
                    if (p01Var.y() < 2 || !this.f14480c) {
                        return;
                    }
                    int I4 = p01Var.I();
                    int I5 = p01Var.I();
                    iArr2[3] = c(iArr2[3], I4 >> 4);
                    iArr2[2] = c(iArr2[2], I4 & 15);
                    iArr2[1] = c(iArr2[1], I5 >> 4);
                    iArr2[0] = c(iArr2[0], I5 & 15);
                    break;
                case 5:
                    if (p01Var.y() < 6) {
                        return;
                    }
                    int I6 = p01Var.I();
                    int I7 = p01Var.I();
                    int i14 = I7 >> 4;
                    int I8 = ((I7 & 15) << 8) | p01Var.I();
                    int I9 = p01Var.I();
                    int I10 = p01Var.I();
                    this.zzg = new Rect((I6 << 4) | i14, (I9 << 4) | (I10 >> 4), I8 + 1, (((I10 & 15) << 8) | p01Var.I()) + 1);
                    break;
                case 6:
                    if (p01Var.y() < 4) {
                        return;
                    }
                    this.f14484g = p01Var.J();
                    this.f14485h = p01Var.J();
                    break;
                default:
                    return;
            }
        }
    }

    public final void b() {
        this.f14480c = false;
        this.zzg = null;
        this.f14484g = -1;
        this.f14485h = -1;
    }

    public final void d(androidx.media3.extractor.b1 b1Var, boolean z10, Rect rect, int[] iArr) {
        int i10;
        int i11;
        int i12 = !z10 ? 1 : 0;
        int iWidth = rect.width();
        int i13 = i12 * iWidth;
        int iHeight = rect.height();
        while (true) {
            int i14 = 0;
            do {
                int iP = 0;
                for (int i15 = 1; iP < i15 && i15 <= 64; i15 <<= 2) {
                    if (b1Var.h() < 4) {
                        i10 = -1;
                        i11 = 0;
                        break;
                    }
                    iP = (iP << 4) | b1Var.p(4);
                }
                i10 = iP & 3;
                i11 = iP < 4 ? iWidth : iP >> 2;
                int iMin = Math.min(i11, iWidth - i14);
                if (iMin > 0) {
                    int i16 = i13 + iMin;
                    Arrays.fill(iArr, i13, i16, this.f14478a[i10]);
                    i14 += iMin;
                    i13 = i16;
                }
            } while (i14 < iWidth);
            i12 += 2;
            if (i12 >= iHeight) {
                return;
            }
            i13 = i12 * iWidth;
            b1Var.s();
        }
    }

    @Nullable
    public final ad0 zzc(p01 p01Var) {
        Rect rect;
        if (this.f14481d == null || !this.f14479b || !this.f14480c || (rect = this.zzg) == null || this.f14484g == -1 || this.f14485h == -1 || rect.width() < 2 || this.zzg.height() < 2) {
            return null;
        }
        Rect rect2 = this.zzg;
        int[] iArr = new int[rect2.height() * rect2.width()];
        androidx.media3.extractor.b1 b1Var = new androidx.media3.extractor.b1();
        p01Var.C(this.f14484g);
        b1Var.e(p01Var);
        d(b1Var, true, rect2, iArr);
        p01Var.C(this.f14485h);
        b1Var.e(p01Var);
        d(b1Var, false, rect2, iArr);
        Bitmap bitmapCreateBitmap = Bitmap.createBitmap(iArr, rect2.width(), rect2.height(), Bitmap.Config.ARGB_8888);
        gc0 gc0Var = new gc0();
        gc0Var.b(bitmapCreateBitmap);
        gc0Var.f11708d = rect2.left / this.f14482e;
        gc0Var.f11709e = 0;
        gc0Var.f11705a = rect2.top / this.f14483f;
        gc0Var.f11706b = 0;
        gc0Var.f11707c = 0;
        gc0Var.f11712h = rect2.width() / this.f14482e;
        gc0Var.f11713i = rect2.height() / this.f14483f;
        return gc0Var.c();
    }
}
