package H0;

import A0.i;
import A0.j;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import n4.u0;
import q1.C1446c;
import q1.C1449f;
import q1.C1453j;
import q1.InterfaceC1447d;
import q1.InterfaceC1448e;
import q1.InterfaceC1456m;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class b implements InterfaceC1448e, A0.e {

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

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final A0.h[] f3035e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final i[] f3036f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f3038h;
    public A0.h i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public A0.f f3039j;

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

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

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final /* synthetic */ int f3043n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final Object f3044o;

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public b(InterfaceC1456m interfaceC1456m) {
        this(new C1453j[2], new C1446c[2]);
        this.f3043n = 1;
        int i = this.f3037g;
        A0.h[] hVarArr = this.f3035e;
        AbstractC1709a.j(i == hVarArr.length);
        for (A0.h hVar : hVarArr) {
            hVar.h(1024);
        }
        this.f3044o = interfaceC1456m;
    }

    @Override // A0.e
    public final void a() {
        synchronized (this.f3032b) {
            this.f3041l = true;
            this.f3032b.notify();
        }
        try {
            this.f3031a.join();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
        }
    }

    @Override // A0.e
    public final Object d() {
        A0.h hVar;
        synchronized (this.f3032b) {
            try {
                A0.f fVar = this.f3039j;
                if (fVar != null) {
                    throw fVar;
                }
                AbstractC1709a.j(this.i == null);
                int i = this.f3037g;
                if (i == 0) {
                    hVar = null;
                } else {
                    A0.h[] hVarArr = this.f3035e;
                    int i5 = i - 1;
                    this.f3037g = i5;
                    hVar = hVarArr[i5];
                }
                this.i = hVar;
            } catch (Throwable th) {
                throw th;
            }
        }
        return hVar;
    }

    @Override // A0.e
    public final void f(long j8) {
        synchronized (this.f3032b) {
            try {
                AbstractC1709a.j(this.f3037g == this.f3035e.length || this.f3040k);
                this.f3042m = j8;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // A0.e
    public final void flush() {
        synchronized (this.f3032b) {
            try {
                this.f3040k = true;
                A0.h hVar = this.i;
                if (hVar != null) {
                    hVar.e();
                    int i = this.f3037g;
                    this.f3037g = i + 1;
                    this.f3035e[i] = hVar;
                    this.i = null;
                }
                while (!this.f3033c.isEmpty()) {
                    A0.h hVar2 = (A0.h) this.f3033c.removeFirst();
                    hVar2.e();
                    int i5 = this.f3037g;
                    this.f3037g = i5 + 1;
                    this.f3035e[i5] = hVar2;
                }
                while (!this.f3034d.isEmpty()) {
                    ((i) this.f3034d.removeFirst()).g();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final A0.f g(Throwable th) {
        switch (this.f3043n) {
            case u0.f15495q /* 0 */:
                return new d("Unexpected decode error", th);
            default:
                return new C1449f("Unexpected decode error", th);
        }
    }

    public final A0.f h(A0.h hVar, i iVar, boolean z8) {
        switch (this.f3043n) {
            case u0.f15495q /* 0 */:
                a aVar = (a) iVar;
                try {
                    ByteBuffer byteBuffer = hVar.f18u;
                    byteBuffer.getClass();
                    AbstractC1709a.j(byteBuffer.hasArray());
                    AbstractC1709a.d(byteBuffer.arrayOffset() == 0);
                    C0.c cVar = (C0.c) this.f3044o;
                    byte[] bArrArray = byteBuffer.array();
                    int iRemaining = byteBuffer.remaining();
                    cVar.getClass();
                    aVar.f3029u = C0.c.a(iRemaining, bArrArray);
                    aVar.f23s = hVar.f20w;
                    return null;
                } catch (d e8) {
                    return e8;
                }
            default:
                C1453j c1453j = (C1453j) hVar;
                C1446c c1446c = (C1446c) iVar;
                try {
                    ByteBuffer byteBuffer2 = c1453j.f18u;
                    byteBuffer2.getClass();
                    byte[] bArrArray2 = byteBuffer2.array();
                    int iLimit = byteBuffer2.limit();
                    InterfaceC1456m interfaceC1456m = (InterfaceC1456m) this.f3044o;
                    if (z8) {
                        interfaceC1456m.reset();
                    }
                    InterfaceC1447d interfaceC1447dD = interfaceC1456m.d(bArrArray2, 0, iLimit);
                    long j8 = c1453j.f20w;
                    long j9 = c1453j.f16219z;
                    c1446c.f23s = j8;
                    c1446c.f16202u = interfaceC1447dD;
                    if (j9 != Long.MAX_VALUE) {
                        j8 = j9;
                    }
                    c1446c.f16203v = j8;
                    c1446c.f24t = false;
                    return null;
                } catch (C1449f e9) {
                    return e9;
                }
        }
    }

    public final boolean i() {
        A0.f fVarG;
        synchronized (this.f3032b) {
            while (!this.f3041l) {
                try {
                    if (!this.f3033c.isEmpty() && this.f3038h > 0) {
                        break;
                    }
                    this.f3032b.wait();
                } finally {
                }
            }
            if (this.f3041l) {
                return false;
            }
            A0.h hVar = (A0.h) this.f3033c.removeFirst();
            i[] iVarArr = this.f3036f;
            int i = this.f3038h - 1;
            this.f3038h = i;
            i iVar = iVarArr[i];
            boolean z8 = this.f3040k;
            this.f3040k = false;
            if (hVar.d(4)) {
                iVar.a(4);
            } else {
                iVar.f23s = hVar.f20w;
                if (hVar.d(134217728)) {
                    iVar.a(134217728);
                }
                if (!k(hVar.f20w)) {
                    iVar.f24t = true;
                }
                try {
                    fVarG = h(hVar, iVar, z8);
                } catch (OutOfMemoryError e8) {
                    fVarG = g(e8);
                } catch (RuntimeException e9) {
                    fVarG = g(e9);
                }
                if (fVarG != null) {
                    synchronized (this.f3032b) {
                        this.f3039j = fVarG;
                    }
                    return false;
                }
            }
            synchronized (this.f3032b) {
                try {
                    if (this.f3040k || iVar.f24t) {
                        iVar.g();
                    } else {
                        this.f3034d.addLast(iVar);
                    }
                    hVar.e();
                    int i5 = this.f3037g;
                    this.f3037g = i5 + 1;
                    this.f3035e[i5] = hVar;
                } finally {
                }
            }
            return true;
        }
    }

    @Override // A0.e
    /* JADX INFO: renamed from: j, reason: merged with bridge method [inline-methods] */
    public final i c() {
        synchronized (this.f3032b) {
            try {
                A0.f fVar = this.f3039j;
                if (fVar != null) {
                    throw fVar;
                }
                if (this.f3034d.isEmpty()) {
                    return null;
                }
                return (i) this.f3034d.removeFirst();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean k(long j8) {
        boolean z8;
        synchronized (this.f3032b) {
            long j9 = this.f3042m;
            z8 = j9 == -9223372036854775807L || j8 >= j9;
        }
        return z8;
    }

    @Override // A0.e
    /* JADX INFO: renamed from: l, reason: merged with bridge method [inline-methods] */
    public final void e(A0.h hVar) {
        synchronized (this.f3032b) {
            try {
                A0.f fVar = this.f3039j;
                if (fVar != null) {
                    throw fVar;
                }
                AbstractC1709a.d(hVar == this.i);
                this.f3033c.addLast(hVar);
                if (!this.f3033c.isEmpty() && this.f3038h > 0) {
                    this.f3032b.notify();
                }
                this.i = null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void m(i iVar) {
        synchronized (this.f3032b) {
            iVar.e();
            int i = this.f3038h;
            this.f3038h = i + 1;
            this.f3036f[i] = iVar;
            if (!this.f3033c.isEmpty() && this.f3038h > 0) {
                this.f3032b.notify();
            }
        }
    }

    public b(A0.h[] hVarArr, i[] iVarArr) {
        i aVar;
        A0.h hVar;
        this.f3032b = new Object();
        this.f3042m = -9223372036854775807L;
        this.f3033c = new ArrayDeque();
        this.f3034d = new ArrayDeque();
        this.f3035e = hVarArr;
        this.f3037g = hVarArr.length;
        for (int i = 0; i < this.f3037g; i++) {
            A0.h[] hVarArr2 = this.f3035e;
            switch (this.f3043n) {
                case u0.f15495q /* 0 */:
                    hVar = new A0.h(1);
                    break;
                default:
                    hVar = new C1453j(1);
                    break;
            }
            hVarArr2[i] = hVar;
        }
        this.f3036f = iVarArr;
        this.f3038h = iVarArr.length;
        for (int i5 = 0; i5 < this.f3038h; i5++) {
            i[] iVarArr2 = this.f3036f;
            switch (this.f3043n) {
                case u0.f15495q /* 0 */:
                    aVar = new a(this);
                    break;
                default:
                    aVar = new C1446c(this);
                    break;
            }
            iVarArr2[i5] = aVar;
        }
        j jVar = new j(this);
        this.f3031a = jVar;
        jVar.start();
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public b(C0.c cVar) {
        this(new A0.h[1], new a[1]);
        this.f3043n = 0;
        this.f3044o = cVar;
    }

    @Override // q1.InterfaceC1448e
    public void b(long j8) {
    }
}
