package y4;

import android.graphics.Bitmap;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Iterator;

/* JADX INFO: loaded from: classes.dex */
public final class e implements a {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final l5.b f36830c;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public byte[] f36832e;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public byte[] f36835h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public byte[] f36836i;

    @Nullable
    private Boolean isFirstFrameTransparent;

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final int[] f36829b = new int[256];

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public Bitmap.Config f36847t = Bitmap.Config.ARGB_8888;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public c f36840m = new c();

    public e(l5.b bVar, c cVar, ByteBuffer byteBuffer, int i10) {
        this.f36830c = bVar;
        c(cVar, byteBuffer, i10);
    }

    public final void a() {
        this.f36840m = null;
        byte[] bArr = this.f36837j;
        l5.b bVar = this.f36830c;
        if (bArr != null) {
            bVar.c(bArr);
        }
        int[] iArr = this.f36838k;
        if (iArr != null) {
            bVar.d(iArr);
        }
        Bitmap bitmap = this.f36841n;
        if (bitmap != null) {
            bVar.f30124a.a(bitmap);
        }
        this.f36841n = null;
        this.f36831d = null;
        this.isFirstFrameTransparent = null;
        byte[] bArr2 = this.f36832e;
        if (bArr2 != null) {
            bVar.c(bArr2);
        }
    }

    public final Bitmap b() {
        Boolean bool = this.isFirstFrameTransparent;
        Bitmap bitmapB = this.f36830c.f30124a.b(this.f36846s, this.f36845r, (bool == null || bool.booleanValue()) ? Bitmap.Config.ARGB_8888 : this.f36847t);
        bitmapB.setHasAlpha(true);
        return bitmapB;
    }

    public final synchronized void c(c cVar, ByteBuffer byteBuffer, int i10) {
        try {
            if (i10 <= 0) {
                throw new IllegalArgumentException("Sample size must be >=0, not: " + i10);
            }
            int iHighestOneBit = Integer.highestOneBit(i10);
            this.f36843p = 0;
            this.f36840m = cVar;
            this.f36839l = -1;
            ByteBuffer byteBufferAsReadOnlyBuffer = byteBuffer.asReadOnlyBuffer();
            this.f36831d = byteBufferAsReadOnlyBuffer;
            byteBufferAsReadOnlyBuffer.position(0);
            this.f36831d.order(ByteOrder.LITTLE_ENDIAN);
            this.f36842o = false;
            Iterator it = cVar.f36817e.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                } else if (((b) it.next()).f36808g == 3) {
                    this.f36842o = true;
                    break;
                }
            }
            this.f36844q = iHighestOneBit;
            int i11 = cVar.f36818f;
            this.f36846s = i11 / iHighestOneBit;
            int i12 = cVar.f36819g;
            this.f36845r = i12 / iHighestOneBit;
            this.f36837j = this.f36830c.a(i11 * i12);
            this.f36838k = this.f36830c.b(this.f36846s * this.f36845r);
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void d(Bitmap.Config config) {
        Bitmap.Config config2;
        Bitmap.Config config3 = Bitmap.Config.ARGB_8888;
        if (config == config3 || config == (config2 = Bitmap.Config.RGB_565)) {
            this.f36847t = config;
            return;
        }
        throw new IllegalArgumentException("Unsupported format: " + config + ", must be one of " + config3 + " or " + config2);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:26:0x0045  */
    /* JADX WARN: Type inference failed for: r5v25 */
    /* JADX WARN: Type inference failed for: r5v26 */
    /* JADX WARN: Type inference failed for: r5v27 */
    /* JADX WARN: Type inference failed for: r5v32, types: [short] */
    /* JADX WARN: Type inference failed for: r5v34 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Bitmap e(b bVar, b bVar2) {
        int[] iArr;
        byte b10;
        int i10;
        int i11;
        int i12;
        int i13;
        int i14;
        int i15;
        int i16;
        int i17;
        int i18;
        int i19;
        int i20;
        int i21;
        int i22;
        short s10;
        int i23;
        Bitmap bitmap;
        int[] iArr2 = this.f36838k;
        l5.b bVar3 = this.f36830c;
        byte b11 = 0;
        if (bVar2 == null) {
            Bitmap bitmap2 = this.f36841n;
            if (bitmap2 != null) {
                bVar3.f30124a.a(bitmap2);
            }
            this.f36841n = null;
            Arrays.fill(iArr2, 0);
        }
        if (bVar2 != null && bVar2.f36808g == 3 && this.f36841n == null) {
            Arrays.fill(iArr2, 0);
        }
        if (bVar2 != null && (i23 = bVar2.f36808g) > 0) {
            if (i23 == 2) {
                if (!bVar.f36807f) {
                    c cVar = this.f36840m;
                    int i24 = cVar.f36823k;
                    if (bVar.f36812k != null && cVar.f36822j == bVar.f36809h) {
                        i24 = 0;
                    }
                    int i25 = bVar2.f36805d;
                    int i26 = this.f36844q;
                    int i27 = i25 / i26;
                    int i28 = bVar2.f36803b / i26;
                    int i29 = bVar2.f36804c / i26;
                    int i30 = bVar2.f36802a / i26;
                    int i31 = this.f36846s;
                    int i32 = (i28 * i31) + i30;
                    int i33 = (i27 * i31) + i32;
                    while (i32 < i33) {
                        int i34 = i32 + i29;
                        for (int i35 = i32; i35 < i34; i35++) {
                            iArr2[i35] = i24;
                        }
                        i32 += this.f36846s;
                    }
                }
            } else if (i23 == 3 && (bitmap = this.f36841n) != null) {
                int i36 = this.f36846s;
                bitmap.getPixels(iArr2, 0, i36, 0, 0, i36, this.f36845r);
            }
        }
        this.f36831d.position(bVar.f36811j);
        int i37 = bVar.f36804c * bVar.f36805d;
        byte[] bArr = this.f36837j;
        if (bArr == null || bArr.length < i37) {
            this.f36837j = bVar3.a(i37);
        }
        byte[] bArr2 = this.f36837j;
        if (this.f36834g == null) {
            this.f36834g = new short[4096];
        }
        short[] sArr = this.f36834g;
        if (this.f36835h == null) {
            this.f36835h = new byte[4096];
        }
        byte[] bArr3 = this.f36835h;
        if (this.f36836i == null) {
            this.f36836i = new byte[4097];
        }
        byte[] bArr4 = this.f36836i;
        int i38 = this.f36831d.get() & 255;
        int i39 = 1 << i38;
        int i40 = i39 + 1;
        int i41 = i39 + 2;
        int i42 = i38 + 1;
        int i43 = (1 << i42) - 1;
        for (int i44 = 0; i44 < i39; i44++) {
            sArr[i44] = 0;
            bArr3[i44] = (byte) i44;
        }
        byte[] bArr5 = this.f36832e;
        int i45 = i42;
        int i46 = 0;
        int i47 = 0;
        int i48 = 0;
        int i49 = 0;
        int i50 = 0;
        int i51 = 0;
        int i52 = 0;
        int i53 = 0;
        int i54 = i41;
        int i55 = i43;
        int i56 = -1;
        while (true) {
            if (i46 >= i37) {
                iArr = iArr2;
                b10 = b11;
                i10 = i51;
                break;
            }
            if (i47 == 0) {
                int i57 = this.f36831d.get() & 255;
                if (i57 <= 0) {
                    i21 = i42;
                    i22 = i46;
                } else {
                    ByteBuffer byteBuffer = this.f36831d;
                    i21 = i42;
                    i22 = i46;
                    byteBuffer.get(this.f36832e, 0, Math.min(i57, byteBuffer.remaining()));
                }
                if (i57 <= 0) {
                    this.f36843p = 3;
                    iArr = iArr2;
                    i10 = i51;
                    b10 = 0;
                    break;
                }
                i47 = i57;
                i48 = 0;
            } else {
                i21 = i42;
                i22 = i46;
            }
            i50 += (bArr5[i48] & 255) << i49;
            i48++;
            i47--;
            int i58 = i56;
            int i59 = i49 + 8;
            int i60 = i54;
            int i61 = i45;
            i46 = i22;
            byte[] bArr6 = bArr5;
            int i62 = i52;
            while (true) {
                if (i59 < i61) {
                    i52 = i62;
                    i45 = i61;
                    i49 = i59;
                    i54 = i60;
                    bArr5 = bArr6;
                    i42 = i21;
                    b11 = 0;
                    i56 = i58;
                    break;
                }
                int[] iArr3 = iArr2;
                int i63 = i50 & i55;
                i50 >>= i61;
                i59 -= i61;
                if (i63 == i39) {
                    i60 = i41;
                    i55 = i43;
                    iArr2 = iArr3;
                    i61 = i21;
                    i58 = -1;
                } else {
                    if (i63 == i40) {
                        i52 = i62;
                        i45 = i61;
                        i54 = i60;
                        bArr5 = bArr6;
                        iArr2 = iArr3;
                        i42 = i21;
                        i56 = i58;
                        i49 = i59;
                        b11 = 0;
                        break;
                    }
                    if (i58 == -1) {
                        bArr2[i51] = bArr3[i63];
                        i51++;
                        i46++;
                        i62 = i63;
                        i58 = i62;
                        iArr2 = iArr3;
                        i59 = i59;
                    } else {
                        if (i63 >= i60) {
                            bArr4[i53] = (byte) i62;
                            i53++;
                            s10 = i58;
                        } else {
                            s10 = i63;
                        }
                        while (s10 >= i39) {
                            bArr4[i53] = bArr3[s10];
                            i53++;
                            s10 = sArr[s10];
                        }
                        int i64 = bArr3[s10] & 255;
                        byte b12 = (byte) i64;
                        bArr2[i51] = b12;
                        while (true) {
                            i51++;
                            i46++;
                            if (i53 <= 0) {
                                break;
                            }
                            i53--;
                            bArr2[i51] = bArr4[i53];
                        }
                        if (i60 < 4096) {
                            sArr[i60] = (short) i58;
                            bArr3[i60] = b12;
                            i60++;
                            if ((i60 & i55) == 0 && i60 < 4096) {
                                i61++;
                                i55 += i60;
                            }
                        }
                        i58 = i63;
                        iArr2 = iArr3;
                        i59 = i59;
                        i62 = i64;
                    }
                }
            }
        }
        Arrays.fill(bArr2, i10, i37, b10);
        if (bVar.f36806e || this.f36844q != 1) {
            int[] iArr4 = this.f36838k;
            int i65 = bVar.f36805d;
            int i66 = this.f36844q;
            int i67 = i65 / i66;
            int i68 = bVar.f36803b / i66;
            int i69 = bVar.f36804c / i66;
            int i70 = bVar.f36802a / i66;
            boolean z10 = this.f36839l == 0;
            int i71 = this.f36846s;
            int i72 = this.f36845r;
            byte[] bArr7 = this.f36837j;
            int[] iArr5 = this.f36828a;
            Boolean bool = this.isFirstFrameTransparent;
            int i73 = 8;
            int i74 = 0;
            int i75 = 0;
            int i76 = 1;
            while (i74 < i67) {
                Boolean bool2 = bool;
                if (bVar.f36806e) {
                    if (i75 >= i67) {
                        int i77 = i76 + 1;
                        i11 = i67;
                        if (i77 == 2) {
                            i75 = 4;
                        } else if (i77 == 3) {
                            i73 = 4;
                            i76 = i77;
                            i75 = 2;
                        } else if (i77 == 4) {
                            i76 = i77;
                            i75 = 1;
                            i73 = 2;
                        }
                        i76 = i77;
                    } else {
                        i11 = i67;
                    }
                    i12 = i75 + i73;
                } else {
                    i11 = i67;
                    i12 = i75;
                    i75 = i74;
                }
                int i78 = i75 + i68;
                boolean z11 = i66 == 1;
                if (i78 < i72) {
                    int i79 = i78 * i71;
                    int i80 = i79 + i70;
                    i13 = i12;
                    int i81 = i80 + i69;
                    int i82 = i79 + i71;
                    if (i82 < i81) {
                        i81 = i82;
                    }
                    i14 = i68;
                    int i83 = i74 * i66 * bVar.f36804c;
                    if (z11) {
                        bool = bool2;
                        int i84 = i80;
                        while (i84 < i81) {
                            int i85 = i69;
                            int i86 = iArr5[bArr7[i83] & 255];
                            if (i86 != 0) {
                                iArr4[i84] = i86;
                            } else if (z10 && bool == null) {
                                bool = Boolean.TRUE;
                            }
                            i83 += i66;
                            i84++;
                            i69 = i85;
                        }
                        i15 = i69;
                    } else {
                        i15 = i69;
                        int i87 = ((i81 - i80) * i66) + i83;
                        bool = bool2;
                        int i88 = i80;
                        while (i88 < i81) {
                            int i89 = i81;
                            int i90 = bVar.f36804c;
                            int i91 = i70;
                            int i92 = i71;
                            int i93 = i83;
                            int i94 = 0;
                            int i95 = 0;
                            int i96 = 0;
                            int i97 = 0;
                            int i98 = 0;
                            while (true) {
                                if (i93 >= this.f36844q + i83) {
                                    i19 = i72;
                                    break;
                                }
                                byte[] bArr8 = this.f36837j;
                                i19 = i72;
                                if (i93 >= bArr8.length || i93 >= i87) {
                                    break;
                                }
                                int i99 = this.f36828a[bArr8[i93] & 255];
                                if (i99 != 0) {
                                    i94 += (i99 >> 24) & 255;
                                    i95 += (i99 >> 16) & 255;
                                    i96 += (i99 >> 8) & 255;
                                    i97 += i99 & 255;
                                    i98++;
                                }
                                i93++;
                                i72 = i19;
                            }
                            int i100 = i90 + i83;
                            for (int i101 = i100; i101 < this.f36844q + i100; i101++) {
                                byte[] bArr9 = this.f36837j;
                                if (i101 >= bArr9.length || i101 >= i87) {
                                    break;
                                }
                                int i102 = this.f36828a[bArr9[i101] & 255];
                                if (i102 != 0) {
                                    i94 += (i102 >> 24) & 255;
                                    i95 += (i102 >> 16) & 255;
                                    i96 += (i102 >> 8) & 255;
                                    i97 += i102 & 255;
                                    i98++;
                                }
                            }
                            int i103 = i98 == 0 ? 0 : ((i94 / i98) << 24) | ((i95 / i98) << 16) | ((i96 / i98) << 8) | (i97 / i98);
                            if (i103 != 0) {
                                iArr4[i88] = i103;
                            } else if (z10 && bool == null) {
                                bool = Boolean.TRUE;
                            }
                            i83 += i66;
                            i88++;
                            i81 = i89;
                            i70 = i91;
                            i71 = i92;
                            i72 = i19;
                        }
                    }
                    i16 = i70;
                    i17 = i71;
                    i18 = i72;
                } else {
                    i13 = i12;
                    i14 = i68;
                    i15 = i69;
                    i16 = i70;
                    i17 = i71;
                    i18 = i72;
                    bool = bool2;
                }
                i74++;
                i67 = i11;
                i75 = i13;
                i68 = i14;
                i69 = i15;
                i70 = i16;
                i71 = i17;
                i72 = i18;
            }
            Boolean bool3 = bool;
            if (this.isFirstFrameTransparent == null) {
                this.isFirstFrameTransparent = Boolean.valueOf(bool3 == null ? false : bool3.booleanValue());
            }
        } else {
            int[] iArr6 = this.f36838k;
            int i104 = bVar.f36805d;
            int i105 = bVar.f36803b;
            int i106 = bVar.f36804c;
            int i107 = bVar.f36802a;
            byte b13 = this.f36839l == 0 ? (byte) 1 : b10;
            int i108 = this.f36846s;
            byte[] bArr10 = this.f36837j;
            int[] iArr7 = this.f36828a;
            byte b14 = -1;
            for (int i109 = b10; i109 < i104; i109++) {
                int i110 = (i109 + i105) * i108;
                int i111 = i110 + i107;
                int i112 = i111 + i106;
                int i113 = i110 + i108;
                if (i113 < i112) {
                    i112 = i113;
                }
                int i114 = bVar.f36804c * i109;
                while (i111 < i112) {
                    int i115 = i104;
                    byte b15 = bArr10[i114];
                    int i116 = i105;
                    int i117 = b15 & 255;
                    if (i117 != b14) {
                        int i118 = iArr7[i117];
                        if (i118 != 0) {
                            iArr6[i111] = i118;
                        } else {
                            b14 = b15;
                        }
                    }
                    i114++;
                    i111++;
                    i104 = i115;
                    i105 = i116;
                }
            }
            Boolean bool4 = this.isFirstFrameTransparent;
            this.isFirstFrameTransparent = Boolean.valueOf((bool4 != null && bool4.booleanValue()) || !(this.isFirstFrameTransparent != null || b13 == 0 || b14 == -1));
        }
        if (this.f36842o && ((i20 = bVar.f36808g) == 0 || i20 == 1)) {
            if (this.f36841n == null) {
                this.f36841n = b();
            }
            Bitmap bitmap3 = this.f36841n;
            int i119 = this.f36846s;
            bitmap3.setPixels(iArr, 0, i119, 0, 0, i119, this.f36845r);
        }
        Bitmap bitmapB = b();
        int i120 = this.f36846s;
        bitmapB.setPixels(iArr, 0, i120, 0, 0, i120, this.f36845r);
        return bitmapB;
    }

    @Override // y4.a
    @Nullable
    public synchronized Bitmap getNextFrame() {
        try {
            if (this.f36840m.f36815c <= 0 || this.f36839l < 0) {
                if (Log.isLoggable("e", 3)) {
                    int i10 = this.f36840m.f36815c;
                }
                this.f36843p = 1;
            }
            int i11 = this.f36843p;
            if (i11 != 1 && i11 != 2) {
                this.f36843p = 0;
                if (this.f36832e == null) {
                    this.f36832e = this.f36830c.a(255);
                }
                b bVar = (b) this.f36840m.f36817e.get(this.f36839l);
                int i12 = this.f36839l - 1;
                b bVar2 = i12 >= 0 ? (b) this.f36840m.f36817e.get(i12) : null;
                int[] iArr = bVar.f36812k;
                if (iArr == null) {
                    iArr = this.f36840m.f36813a;
                }
                this.f36828a = iArr;
                if (iArr == null) {
                    this.f36843p = 1;
                    return null;
                }
                if (bVar.f36807f) {
                    System.arraycopy(iArr, 0, this.f36829b, 0, iArr.length);
                    int[] iArr2 = this.f36829b;
                    this.f36828a = iArr2;
                    iArr2[bVar.f36809h] = 0;
                    if (bVar.f36808g == 2 && this.f36839l == 0) {
                        this.isFirstFrameTransparent = Boolean.TRUE;
                    }
                }
                return e(bVar, bVar2);
            }
            return null;
        } finally {
        }
    }

    @Override // y4.a
    public int read(@Nullable InputStream inputStream, int i10) {
        if (inputStream != null) {
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(i10 > 0 ? i10 + 4096 : 16384);
                byte[] bArr = new byte[16384];
                while (true) {
                    int i11 = inputStream.read(bArr, 0, 16384);
                    if (i11 == -1) {
                        break;
                    }
                    byteArrayOutputStream.write(bArr, 0, i11);
                }
                byteArrayOutputStream.flush();
                read(byteArrayOutputStream.toByteArray());
            } catch (IOException unused) {
            }
        } else {
            this.f36843p = 2;
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException unused2) {
            }
        }
        return this.f36843p;
    }

    @Override // y4.a
    public synchronized int read(@Nullable byte[] bArr) {
        if (this.f36833f == null) {
            this.f36833f = new d();
        }
        c cVarB = this.f36833f.setData(bArr).b();
        this.f36840m = cVarB;
        if (bArr != null) {
            synchronized (this) {
                ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArr);
                synchronized (this) {
                    c(cVarB, byteBufferWrap, 1);
                }
            }
        }
        return this.f36843p;
    }
}
