package v0;

import java.util.Arrays;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class i {

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

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

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final int f18034g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int f18035h;
    public final short[] i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public short[] f18036j;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public short[] f18038l;

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public short[] f18040n;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public double f18049w;

    public i(int i, int i5, float f3, float f7, int i8) {
        this.f18028a = i;
        this.f18029b = i5;
        this.f18030c = f3;
        this.f18031d = f7;
        this.f18032e = i / i8;
        this.f18033f = i / 400;
        int i9 = i / 65;
        this.f18034g = i9;
        int i10 = i9 * 2;
        this.f18035h = i10;
        this.i = new short[i10];
        this.f18036j = new short[i10 * i5];
        this.f18038l = new short[i10 * i5];
        this.f18040n = new short[i10 * i5];
    }

    public static void e(int i, int i5, short[] sArr, int i8, short[] sArr2, int i9, short[] sArr3, int i10) {
        for (int i11 = 0; i11 < i5; i11++) {
            int i12 = (i8 * i5) + i11;
            int i13 = (i10 * i5) + i11;
            int i14 = (i9 * i5) + i11;
            for (int i15 = 0; i15 < i; i15++) {
                sArr[i12] = (short) (((sArr3[i13] * i15) + ((i - i15) * sArr2[i14])) / i);
                i12 += i5;
                i14 += i5;
                i13 += i5;
            }
        }
    }

    public final void a(short[] sArr, int i, int i5) {
        short[] sArrC = c(this.f18038l, this.f18039m, i5);
        this.f18038l = sArrC;
        int i8 = this.f18029b;
        System.arraycopy(sArr, i * i8, sArrC, this.f18039m * i8, i8 * i5);
        this.f18039m += i5;
    }

    public final void b(short[] sArr, int i, int i5) {
        int i8 = this.f18035h / i5;
        int i9 = this.f18029b;
        int i10 = i5 * i9;
        int i11 = i * i9;
        for (int i12 = 0; i12 < i8; i12++) {
            int i13 = 0;
            for (int i14 = 0; i14 < i10; i14++) {
                i13 += sArr[(i12 * i10) + i11 + i14];
            }
            this.i[i12] = (short) (i13 / i10);
        }
    }

    public final short[] c(short[] sArr, int i, int i5) {
        int length = sArr.length;
        int i8 = this.f18029b;
        int i9 = length / i8;
        return i + i5 <= i9 ? sArr : Arrays.copyOf(sArr, (((i9 * 3) / 2) + i5) * i8);
    }

    public final int d(short[] sArr, int i, int i5, int i8) {
        int i9 = i * this.f18029b;
        int i10 = 255;
        int i11 = 1;
        int i12 = 0;
        int i13 = 0;
        while (i5 <= i8) {
            int iAbs = 0;
            for (int i14 = 0; i14 < i5; i14++) {
                iAbs += Math.abs(sArr[i9 + i14] - sArr[(i9 + i5) + i14]);
            }
            if (iAbs * i12 < i11 * i5) {
                i12 = i5;
                i11 = iAbs;
            }
            if (iAbs * i10 > i13 * i5) {
                i10 = i5;
                i13 = iAbs;
            }
            i5++;
        }
        this.f18047u = i11 / i12;
        this.f18048v = i13 / i10;
        return i12;
    }

    public final void f() {
        float f3;
        double d8;
        int iD;
        int i;
        int i5;
        int iRound;
        int i8;
        int i9;
        int i10;
        long j8;
        long j9;
        int i11 = this.f18039m;
        float f7 = this.f18030c;
        float f8 = this.f18031d;
        double d9 = f7 / f8;
        float f9 = this.f18032e * f8;
        int i12 = this.f18028a;
        int i13 = this.f18029b;
        int i14 = 0;
        int i15 = 1;
        if (d9 > 1.0000100135803223d || d9 < 0.9999899864196777d) {
            int i16 = this.f18037k;
            int i17 = this.f18035h;
            if (i16 >= i17) {
                int i18 = 0;
                while (true) {
                    int i19 = this.f18044r;
                    if (i19 > 0) {
                        int iMin = Math.min(i17, i19);
                        a(this.f18036j, i18, iMin);
                        this.f18044r -= iMin;
                        i18 += iMin;
                        f3 = f9;
                        d8 = d9;
                    } else {
                        short[] sArr = this.f18036j;
                        int i20 = i12 > 4000 ? i12 / 4000 : i15;
                        int i21 = this.f18034g;
                        int i22 = this.f18033f;
                        if (i13 == i15 && i20 == i15) {
                            iD = d(sArr, i18, i22, i21);
                            f3 = f9;
                            d8 = d9;
                        } else {
                            b(sArr, i18, i20);
                            f3 = f9;
                            d8 = d9;
                            short[] sArr2 = this.i;
                            int iD2 = d(sArr2, i14, i22 / i20, i21 / i20);
                            if (i20 != 1) {
                                int i23 = iD2 * i20;
                                int i24 = i20 * 4;
                                int i25 = i23 - i24;
                                int i26 = i23 + i24;
                                if (i25 >= i22) {
                                    i22 = i25;
                                }
                                if (i26 <= i21) {
                                    i21 = i26;
                                }
                                if (i13 == 1) {
                                    iD = d(sArr, i18, i22, i21);
                                } else {
                                    b(sArr, i18, 1);
                                    iD = d(sArr2, i14, i22, i21);
                                }
                            } else {
                                iD = iD2;
                            }
                        }
                        int i27 = this.f18047u;
                        int i28 = this.f18048v;
                        if (i27 == 0 || (i = this.f18045s) == 0 || i28 > i27 * 3 || i27 * 2 <= this.f18046t * 3) {
                            i = iD;
                        }
                        this.f18046t = i27;
                        this.f18045s = iD;
                        if (d8 > 1.0d) {
                            short[] sArr3 = this.f18036j;
                            if (d8 >= 2.0d) {
                                double d10 = (((double) i) / (d8 - 1.0d)) + this.f18049w;
                                iRound = (int) Math.round(d10);
                                this.f18049w = d10 - ((double) iRound);
                            } else {
                                double d11 = (((2.0d - d8) * ((double) i)) / (d8 - 1.0d)) + this.f18049w;
                                int iRound2 = (int) Math.round(d11);
                                this.f18044r = iRound2;
                                this.f18049w = d11 - ((double) iRound2);
                                iRound = i;
                            }
                            short[] sArrC = c(this.f18038l, this.f18039m, iRound);
                            this.f18038l = sArrC;
                            int i29 = i18 + i;
                            int i30 = i18;
                            int i31 = iRound;
                            e(i31, this.f18029b, sArrC, this.f18039m, sArr3, i30, sArr3, i29);
                            this.f18039m += i31;
                            i18 = i + i31 + i30;
                        } else {
                            int i32 = i18;
                            short[] sArr4 = this.f18036j;
                            if (d8 < 0.5d) {
                                double d12 = ((((double) i) * d8) / (1.0d - d8)) + this.f18049w;
                                int iRound3 = (int) Math.round(d12);
                                this.f18049w = d12 - ((double) iRound3);
                                i5 = iRound3;
                            } else {
                                double d13 = ((((d8 * 2.0d) - 1.0d) * ((double) i)) / (1.0d - d8)) + this.f18049w;
                                int iRound4 = (int) Math.round(d13);
                                this.f18044r = iRound4;
                                this.f18049w = d13 - ((double) iRound4);
                                i5 = i;
                            }
                            int i33 = i + i5;
                            short[] sArrC2 = c(this.f18038l, this.f18039m, i33);
                            this.f18038l = sArrC2;
                            System.arraycopy(sArr4, i32 * i13, sArrC2, this.f18039m * i13, i * i13);
                            e(i5, this.f18029b, this.f18038l, this.f18039m + i, sArr4, i32 + i, sArr4, i32);
                            this.f18039m += i33;
                            i18 = i32 + i5;
                        }
                    }
                    if (i18 + i17 > i16) {
                        break;
                    }
                    i14 = 0;
                    i15 = 1;
                    f9 = f3;
                    d9 = d8;
                }
                int i34 = this.f18037k - i18;
                short[] sArr5 = this.f18036j;
                System.arraycopy(sArr5, i18 * i13, sArr5, 0, i34 * i13);
                this.f18037k = i34;
            }
            if (f3 != 1.0f || this.f18039m == i11) {
            }
            long j10 = (long) (i12 / f3);
            long j11 = i12;
            while (j10 != 0 && j11 != 0 && j10 % 2 == 0 && j11 % 2 == 0) {
                j10 /= 2;
                j11 /= 2;
            }
            int i35 = this.f18039m - i11;
            short[] sArrC3 = c(this.f18040n, this.f18041o, i35);
            this.f18040n = sArrC3;
            System.arraycopy(this.f18038l, i11 * i13, sArrC3, this.f18041o * i13, i35 * i13);
            this.f18039m = i11;
            this.f18041o += i35;
            int i36 = 0;
            while (true) {
                i8 = this.f18041o;
                i9 = i8 - 1;
                if (i36 >= i9) {
                    break;
                }
                while (true) {
                    i10 = this.f18042p + 1;
                    j8 = i10;
                    long j12 = j8 * j10;
                    j9 = this.f18043q;
                    if (j12 <= j9 * j11) {
                        break;
                    }
                    this.f18038l = c(this.f18038l, this.f18039m, 1);
                    int i37 = 0;
                    while (i37 < i13) {
                        short[] sArr6 = this.f18038l;
                        int i38 = (this.f18039m * i13) + i37;
                        short[] sArr7 = this.f18040n;
                        int i39 = (i36 * i13) + i37;
                        short s8 = sArr7[i39];
                        short s9 = sArr7[i39 + i13];
                        long j13 = ((long) this.f18043q) * j11;
                        int i40 = this.f18042p;
                        long j14 = j10;
                        int i41 = i36;
                        long j15 = ((long) (i40 + 1)) * j14;
                        long j16 = j15 - j13;
                        long j17 = j15 - (((long) i40) * j14);
                        sArr6[i38] = (short) ((((j17 - j16) * ((long) s9)) + (((long) s8) * j16)) / j17);
                        i37++;
                        i36 = i41;
                        j10 = j14;
                    }
                    this.f18043q++;
                    this.f18039m++;
                    i36 = i36;
                    j10 = j10;
                }
                long j18 = j10;
                int i42 = i36;
                this.f18042p = i10;
                if (j8 == j11) {
                    this.f18042p = 0;
                    AbstractC1709a.j(j9 == j18);
                    this.f18043q = 0;
                }
                i36 = i42 + 1;
                j10 = j18;
            }
            if (i9 == 0) {
                return;
            }
            short[] sArr8 = this.f18040n;
            System.arraycopy(sArr8, i9 * i13, sArr8, 0, (i8 - i9) * i13);
            this.f18041o -= i9;
            return;
        }
        a(this.f18036j, 0, this.f18037k);
        this.f18037k = 0;
        f3 = f9;
        if (f3 != 1.0f) {
        }
    }
}
