package o2;

import androidx.annotation.Nullable;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.text.SubtitleDecoderException;
import java.util.ArrayDeque;
import n2.l;

/* JADX INFO: loaded from: classes.dex */
public abstract class j implements n2.h {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ArrayDeque f31362a = new ArrayDeque();

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

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

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

    @Nullable
    private h dequeuedInputBuffer;

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

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

    public j() {
        int i10;
        int i11 = 0;
        while (true) {
            i10 = 10;
            if (i11 >= 10) {
                break;
            }
            this.f31362a.add(new h(1));
            i11++;
        }
        this.f31363b = new ArrayDeque();
        for (int i12 = 0; i12 < 2; i12++) {
            ArrayDeque arrayDeque = this.f31363b;
            androidx.core.app.e eVar = new androidx.core.app.e(this, i10);
            i iVar = new i();
            iVar.f31361b = eVar;
            arrayDeque.add(iVar);
        }
        this.f31364c = new ArrayDeque();
        this.f31367f = -9223372036854775807L;
    }

    public abstract k a();

    public abstract void b(h hVar);

    public abstract boolean c();

    @Override // s1.e
    public void flush() {
        ArrayDeque arrayDeque;
        this.f31366e = 0L;
        this.f31365d = 0L;
        while (true) {
            ArrayDeque arrayDeque2 = this.f31364c;
            boolean zIsEmpty = arrayDeque2.isEmpty();
            arrayDeque = this.f31362a;
            if (zIsEmpty) {
                break;
            }
            h hVar = (h) Util.castNonNull((h) arrayDeque2.poll());
            hVar.clear();
            arrayDeque.add(hVar);
        }
        h hVar2 = this.dequeuedInputBuffer;
        if (hVar2 != null) {
            hVar2.clear();
            arrayDeque.add(hVar2);
            this.dequeuedInputBuffer = null;
        }
    }

    @Nullable
    public final l getAvailableOutputBuffer() {
        return (l) this.f31363b.pollFirst();
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0034  */
    @Override // s1.e
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void queueInputBuffer(Object obj) {
        n2.k kVar = (n2.k) obj;
        Assertions.checkArgument(kVar == this.dequeuedInputBuffer);
        h hVar = (h) kVar;
        if (!hVar.isEndOfStream()) {
            long j10 = hVar.timeUs;
            if (j10 != Long.MIN_VALUE) {
                long j11 = this.f31367f;
                if (j11 == -9223372036854775807L || j10 >= j11) {
                    long j12 = this.f31366e;
                    this.f31366e = 1 + j12;
                    hVar.f31360b = j12;
                    this.f31364c.add(hVar);
                } else {
                    hVar.clear();
                    this.f31362a.add(hVar);
                }
            }
        }
        this.dequeuedInputBuffer = null;
    }

    @Override // s1.e
    public void release() {
    }

    @Override // s1.e
    public final void setOutputStartTimeUs(long j10) {
        this.f31367f = j10;
    }

    @Override // n2.h
    public final void setPositionUs(long j10) {
        this.f31365d = j10;
    }

    @Override // n2.h, s1.e
    @Nullable
    public n2.k dequeueInputBuffer() throws SubtitleDecoderException {
        Assertions.checkState(this.dequeuedInputBuffer == null);
        ArrayDeque arrayDeque = this.f31362a;
        if (arrayDeque.isEmpty()) {
            return null;
        }
        h hVar = (h) arrayDeque.pollFirst();
        this.dequeuedInputBuffer = hVar;
        return hVar;
    }

    /* JADX WARN: Code restructure failed: missing block: B:18:0x0083, code lost:
    
        return null;
     */
    @Override // n2.h, s1.e, androidx.media3.exoplayer.image.ImageDecoder
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public l dequeueOutputBuffer() throws SubtitleDecoderException {
        ArrayDeque arrayDeque = this.f31363b;
        if (arrayDeque.isEmpty()) {
            return null;
        }
        while (true) {
            ArrayDeque arrayDeque2 = this.f31364c;
            if (arrayDeque2.isEmpty() || ((h) Util.castNonNull((h) arrayDeque2.peek())).timeUs > this.f31365d) {
                break;
            }
            h hVar = (h) Util.castNonNull((h) arrayDeque2.poll());
            boolean zIsEndOfStream = hVar.isEndOfStream();
            ArrayDeque arrayDeque3 = this.f31362a;
            if (zIsEndOfStream) {
                l lVar = (l) Util.castNonNull((l) arrayDeque.pollFirst());
                lVar.addFlag(4);
                hVar.clear();
                arrayDeque3.add(hVar);
                return lVar;
            }
            b(hVar);
            if (c()) {
                k kVarA = a();
                l lVar2 = (l) Util.castNonNull((l) arrayDeque.pollFirst());
                lVar2.e(hVar.timeUs, kVarA, Long.MAX_VALUE);
                hVar.clear();
                arrayDeque3.add(hVar);
                return lVar2;
            }
            hVar.clear();
            arrayDeque3.add(hVar);
        }
    }
}
