package H0;

import B0.AbstractC0020g;
import B0.C0030q;
import L0.C;
import android.graphics.Bitmap;
import android.os.Trace;
import androidx.media3.exoplayer.image.ImageOutput;
import d3.R0;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import u0.C1579o;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class h extends AbstractC0020g {
    public final K4.c H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public final A0.h f3052I;

    /* JADX INFO: renamed from: J, reason: collision with root package name */
    public final ArrayDeque f3053J;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: R, reason: collision with root package name */
    public C1579o f3061R;

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

    /* JADX INFO: renamed from: T, reason: collision with root package name */
    public A0.h f3063T;

    /* JADX INFO: renamed from: U, reason: collision with root package name */
    public ImageOutput f3064U;

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

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

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

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

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

    public h(K4.c cVar) {
        super(4);
        this.H = cVar;
        this.f3064U = ImageOutput.f8881a;
        this.f3052I = new A0.h(0);
        this.f3056M = f.f3046c;
        this.f3053J = new ArrayDeque();
        this.f3058O = -9223372036854775807L;
        this.f3057N = -9223372036854775807L;
        this.f3059P = 0;
        this.f3060Q = 1;
    }

    /* JADX WARN: Removed duplicated region for block: B:45:0x008a  */
    /* JADX WARN: Removed duplicated region for block: B:51:0x00d9  */
    /* JADX WARN: Removed duplicated region for block: B:52:0x00db  */
    /* JADX WARN: Removed duplicated region for block: B:55:0x00e0  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x00ef  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x00f8  */
    /* JADX WARN: Removed duplicated region for block: B:69:0x0106  */
    /* JADX WARN: Removed duplicated region for block: B:79:0x0148  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean A(long j8) throws C0030q {
        g gVar;
        Bitmap bitmap;
        long j9;
        boolean z8;
        int i;
        boolean z9;
        Bitmap bitmapCreateBitmap;
        Bitmap bitmap2 = this.f3065V;
        if ((bitmap2 == null || this.X != null) && (this.f3060Q != 0 || this.f763x == 2)) {
            ArrayDeque arrayDeque = this.f3053J;
            if (bitmap2 == null) {
                AbstractC1709a.k(this.f3062S);
                a aVar = (a) this.f3062S.c();
                if (aVar != null) {
                    if (!aVar.d(4)) {
                        AbstractC1709a.l(aVar.f3029u, "Non-EOS buffer came back from the decoder without bitmap.");
                        this.f3065V = aVar.f3029u;
                        aVar.g();
                        if (this.f3066W && this.f3065V != null && this.X != null) {
                            AbstractC1709a.k(this.f3061R);
                            C1579o c1579o = this.f3061R;
                            int i5 = c1579o.f17342K;
                            int i8 = c1579o.f17343L;
                            boolean z10 = ((i5 != 1 && i8 == 1) || i5 == -1 || i8 == -1) ? false : true;
                            gVar = this.X;
                            if (((Bitmap) gVar.f3051c) == null) {
                                if (z10) {
                                    AbstractC1709a.k(this.f3065V);
                                    int width = this.f3065V.getWidth();
                                    C1579o c1579o2 = this.f3061R;
                                    AbstractC1709a.k(c1579o2);
                                    int i9 = width / c1579o2.f17342K;
                                    int height = this.f3065V.getHeight();
                                    C1579o c1579o3 = this.f3061R;
                                    AbstractC1709a.k(c1579o3);
                                    int i10 = height / c1579o3.f17343L;
                                    int i11 = this.f3061R.f17342K;
                                    int i12 = gVar.f3049a;
                                    bitmapCreateBitmap = Bitmap.createBitmap(this.f3065V, (i12 % i11) * i9, (i12 / i11) * i10, i9, i10);
                                } else {
                                    bitmapCreateBitmap = this.f3065V;
                                    AbstractC1709a.k(bitmapCreateBitmap);
                                }
                                gVar.f3051c = bitmapCreateBitmap;
                            }
                            bitmap = (Bitmap) this.X.f3051c;
                            AbstractC1709a.k(bitmap);
                            j9 = this.X.f3050b;
                            long j10 = j9 - j8;
                            z8 = this.f763x == 2;
                            i = this.f3060Q;
                            if (i != 0) {
                                if (i == 1) {
                                    z8 = true;
                                } else {
                                    if (i != 3) {
                                        throw new IllegalStateException();
                                    }
                                    z8 = false;
                                }
                            }
                            if (z8 || j10 < 30000) {
                                this.f3064U.onImageAvailable(j9 - this.f3056M.f3048b, bitmap);
                                z9 = true;
                            } else {
                                z9 = false;
                            }
                            if (z9) {
                                g gVar2 = this.X;
                                AbstractC1709a.k(gVar2);
                                long j11 = gVar2.f3050b;
                                this.f3057N = j11;
                                while (!arrayDeque.isEmpty() && j11 >= ((f) arrayDeque.peek()).f3047a) {
                                    this.f3056M = (f) arrayDeque.removeFirst();
                                }
                                this.f3060Q = 3;
                                if (z10) {
                                    g gVar3 = this.X;
                                    AbstractC1709a.k(gVar3);
                                    C1579o c1579o4 = this.f3061R;
                                    AbstractC1709a.k(c1579o4);
                                    C1579o c1579o5 = this.f3061R;
                                    AbstractC1709a.k(c1579o5);
                                    if (gVar3.f3049a == (c1579o4.f17343L * c1579o5.f17342K) - 1) {
                                        this.f3065V = null;
                                    }
                                }
                                this.X = this.f3067Y;
                                this.f3067Y = null;
                                return true;
                            }
                        }
                    } else {
                        if (this.f3059P == 3) {
                            D();
                            AbstractC1709a.k(this.f3061R);
                            C();
                            return false;
                        }
                        aVar.g();
                        if (arrayDeque.isEmpty()) {
                            this.f3055L = true;
                            return false;
                        }
                    }
                }
            } else if (this.f3066W) {
                AbstractC1709a.k(this.f3061R);
                C1579o c1579o6 = this.f3061R;
                int i52 = c1579o6.f17342K;
                int i82 = c1579o6.f17343L;
                if (i52 != 1) {
                    gVar = this.X;
                    if (((Bitmap) gVar.f3051c) == null) {
                    }
                    bitmap = (Bitmap) this.X.f3051c;
                    AbstractC1709a.k(bitmap);
                    j9 = this.X.f3050b;
                    long j102 = j9 - j8;
                    if (this.f763x == 2) {
                    }
                    i = this.f3060Q;
                    if (i != 0) {
                    }
                    if (z8) {
                        this.f3064U.onImageAvailable(j9 - this.f3056M.f3048b, bitmap);
                        z9 = true;
                        if (z9) {
                        }
                    }
                } else {
                    gVar = this.X;
                    if (((Bitmap) gVar.f3051c) == null) {
                    }
                    bitmap = (Bitmap) this.X.f3051c;
                    AbstractC1709a.k(bitmap);
                    j9 = this.X.f3050b;
                    long j1022 = j9 - j8;
                    if (this.f763x == 2) {
                    }
                    i = this.f3060Q;
                    if (i != 0) {
                    }
                    if (z8) {
                    }
                }
            }
        }
        return false;
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x002f  */
    /* JADX WARN: Removed duplicated region for block: B:38:0x0082  */
    /* JADX WARN: Removed duplicated region for block: B:80:0x0108  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean B(long j8) {
        boolean z8;
        C1579o c1579o;
        int i;
        if (!this.f3066W || this.X == null) {
            R0 r02 = this.f758s;
            r02.H();
            b bVar = this.f3062S;
            if (bVar != null && this.f3059P != 3 && !this.f3054K) {
                if (this.f3063T == null) {
                    A0.h hVar = (A0.h) bVar.d();
                    this.f3063T = hVar;
                    if (hVar != null) {
                        if (this.f3059P == 2) {
                            AbstractC1709a.k(this.f3063T);
                            this.f3063T.f6r = 4;
                            b bVar2 = this.f3062S;
                            AbstractC1709a.k(bVar2);
                            bVar2.e(this.f3063T);
                            this.f3063T = null;
                            this.f3059P = 3;
                            return false;
                        }
                        int iU = u(r02, this.f3063T, 0);
                        if (iU == -5) {
                            C1579o c1579o2 = (C1579o) r02.f11693s;
                            AbstractC1709a.k(c1579o2);
                            this.f3061R = c1579o2;
                            this.f3069a0 = true;
                            this.f3059P = 2;
                            return true;
                        }
                        if (iU == -4) {
                            this.f3063T.j();
                            ByteBuffer byteBuffer = this.f3063T.f18u;
                            if (byteBuffer == null || byteBuffer.remaining() <= 0) {
                                A0.h hVar2 = this.f3063T;
                                AbstractC1709a.k(hVar2);
                                z8 = hVar2.d(4);
                            }
                            if (z8) {
                                b bVar3 = this.f3062S;
                                AbstractC1709a.k(bVar3);
                                A0.h hVar3 = this.f3063T;
                                AbstractC1709a.k(hVar3);
                                bVar3.e(hVar3);
                                this.f3068Z = 0;
                            }
                            A0.h hVar4 = this.f3063T;
                            AbstractC1709a.k(hVar4);
                            if (hVar4.d(4)) {
                                this.f3066W = true;
                            } else {
                                int i5 = this.f3068Z;
                                long j9 = hVar4.f20w;
                                g gVar = new g();
                                gVar.f3049a = i5;
                                gVar.f3050b = j9;
                                this.f3067Y = gVar;
                                this.f3068Z = i5 + 1;
                                if (this.f3066W) {
                                    this.X = this.f3067Y;
                                    this.f3067Y = null;
                                } else {
                                    boolean z9 = j9 - 30000 <= j8 && j8 <= 30000 + j9;
                                    g gVar2 = this.X;
                                    boolean z10 = gVar2 != null && gVar2.f3050b <= j8 && j8 < j9;
                                    C1579o c1579o3 = this.f3061R;
                                    AbstractC1709a.k(c1579o3);
                                    this.f3066W = z9 || z10 || (c1579o3.f17342K == -1 || (i = (c1579o = this.f3061R).f17343L) == -1 || i5 == (i * c1579o.f17342K) - 1);
                                    if (!z10 || z9) {
                                    }
                                }
                            }
                            A0.h hVar5 = this.f3063T;
                            AbstractC1709a.k(hVar5);
                            if (hVar5.d(4)) {
                                this.f3054K = true;
                                this.f3063T = null;
                                return false;
                            }
                            long j10 = this.f3058O;
                            A0.h hVar6 = this.f3063T;
                            AbstractC1709a.k(hVar6);
                            this.f3058O = Math.max(j10, hVar6.f20w);
                            if (z8) {
                                this.f3063T = null;
                            } else {
                                A0.h hVar7 = this.f3063T;
                                AbstractC1709a.k(hVar7);
                                hVar7.e();
                            }
                            return !this.f3066W;
                        }
                        if (iU != -3) {
                            throw new IllegalStateException();
                        }
                    }
                }
            }
        }
        return false;
    }

    public final void C() throws C0030q {
        if (this.f3069a0) {
            C1579o c1579o = this.f3061R;
            c1579o.getClass();
            K4.c cVar = this.H;
            int iL = cVar.L(c1579o);
            if (iL != I1.a.e(4, 0, 0, 0) && iL != I1.a.e(3, 0, 0, 0)) {
                throw f(new d("Provided decoder factory can't create decoder for format."), this.f3061R, false, 4005);
            }
            b bVar = this.f3062S;
            if (bVar != null) {
                bVar.a();
            }
            this.f3062S = new b((C0.c) cVar.f3604r);
            this.f3069a0 = false;
        }
    }

    public final void D() {
        this.f3063T = null;
        this.f3059P = 0;
        this.f3058O = -9223372036854775807L;
        b bVar = this.f3062S;
        if (bVar != null) {
            bVar.a();
            this.f3062S = null;
        }
    }

    @Override // B0.AbstractC0020g, B0.l0
    public final void d(int i, Object obj) {
        if (i != 15) {
            return;
        }
        ImageOutput imageOutput = obj instanceof ImageOutput ? (ImageOutput) obj : null;
        if (imageOutput == null) {
            imageOutput = ImageOutput.f8881a;
        }
        this.f3064U = imageOutput;
    }

    @Override // B0.AbstractC0020g
    public final String i() {
        return "ImageRenderer";
    }

    @Override // B0.AbstractC0020g
    public final boolean k() {
        return this.f3055L;
    }

    @Override // B0.AbstractC0020g
    public final boolean l() {
        int i = this.f3060Q;
        if (i != 3) {
            return i == 0 && this.f3066W;
        }
        return true;
    }

    @Override // B0.AbstractC0020g
    public final void m() {
        this.f3061R = null;
        this.f3056M = f.f3046c;
        this.f3053J.clear();
        D();
        this.f3064U.a();
    }

    @Override // B0.AbstractC0020g
    public final void n(boolean z8, boolean z9) {
        this.f3060Q = z9 ? 1 : 0;
    }

    @Override // B0.AbstractC0020g
    public final void o(long j8, boolean z8) {
        this.f3060Q = Math.min(this.f3060Q, 1);
        this.f3055L = false;
        this.f3054K = false;
        this.f3065V = null;
        this.X = null;
        this.f3067Y = null;
        this.f3066W = false;
        this.f3063T = null;
        b bVar = this.f3062S;
        if (bVar != null) {
            bVar.flush();
        }
        this.f3053J.clear();
    }

    @Override // B0.AbstractC0020g
    public final void p() {
        D();
    }

    @Override // B0.AbstractC0020g
    public final void q() {
        D();
        this.f3060Q = Math.min(this.f3060Q, 1);
    }

    /* JADX WARN: Code restructure failed: missing block: B:11:0x0023, code lost:
    
        if (r2 >= r6) goto L15;
     */
    @Override // B0.AbstractC0020g
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void t(C1579o[] c1579oArr, long j8, long j9, C c3) {
        if (this.f3056M.f3048b != -9223372036854775807L) {
            ArrayDeque arrayDeque = this.f3053J;
            if (arrayDeque.isEmpty()) {
                long j10 = this.f3058O;
                if (j10 != -9223372036854775807L) {
                    long j11 = this.f3057N;
                    if (j11 != -9223372036854775807L) {
                    }
                }
            }
            arrayDeque.add(new f(this.f3058O, j9));
            return;
        }
        this.f3056M = new f(-9223372036854775807L, j9);
    }

    @Override // B0.AbstractC0020g
    public final void v(long j8, long j9) throws C0030q {
        if (this.f3055L) {
            return;
        }
        if (this.f3061R == null) {
            R0 r02 = this.f758s;
            r02.H();
            A0.h hVar = this.f3052I;
            hVar.e();
            int iU = u(r02, hVar, 2);
            if (iU != -5) {
                if (iU == -4) {
                    AbstractC1709a.j(hVar.d(4));
                    this.f3054K = true;
                    this.f3055L = true;
                    return;
                }
                return;
            }
            C1579o c1579o = (C1579o) r02.f11693s;
            AbstractC1709a.k(c1579o);
            this.f3061R = c1579o;
            this.f3069a0 = true;
        }
        if (this.f3062S == null) {
            C();
        }
        try {
            Trace.beginSection("drainAndFeedDecoder");
            while (A(j8)) {
            }
            while (B(j8)) {
            }
            Trace.endSection();
        } catch (d e8) {
            throw f(e8, null, false, 4003);
        }
    }

    @Override // B0.AbstractC0020g
    public final int y(C1579o c1579o) {
        return this.H.L(c1579o);
    }
}
