package s1;

import androidx.annotation.CallSuper;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.util.Assertions;
import androidx.media3.decoder.DecoderException;
import java.util.ArrayDeque;

/* JADX INFO: loaded from: classes.dex */
public abstract class i implements e {
    private int availableInputBufferCount;
    private final f[] availableInputBuffers;
    private int availableOutputBufferCount;
    private final g[] availableOutputBuffers;
    private final Thread decodeThread;

    @Nullable
    private f dequeuedInputBuffer;

    @Nullable
    private DecoderException exception;
    private boolean flushed;
    private final Object lock = new Object();
    private long outputStartTimeUs = -9223372036854775807L;
    private final ArrayDeque<f> queuedInputBuffers = new ArrayDeque<>();
    private final ArrayDeque<g> queuedOutputBuffers = new ArrayDeque<>();
    private boolean released;
    private int skippedOutputBufferCount;

    public i(f[] fVarArr, g[] gVarArr) {
        this.availableInputBuffers = fVarArr;
        this.availableInputBufferCount = fVarArr.length;
        for (int i10 = 0; i10 < this.availableInputBufferCount; i10++) {
            this.availableInputBuffers[i10] = createInputBuffer();
        }
        this.availableOutputBuffers = gVarArr;
        this.availableOutputBufferCount = gVarArr.length;
        for (int i11 = 0; i11 < this.availableOutputBufferCount; i11++) {
            this.availableOutputBuffers[i11] = createOutputBuffer();
        }
        h hVar = new h(this);
        this.decodeThread = hVar;
        hVar.start();
    }

    public static void access$000(i iVar) {
        iVar.getClass();
        do {
            try {
            } catch (InterruptedException e10) {
                throw new IllegalStateException(e10);
            }
        } while (iVar.a());
    }

    public final boolean a() {
        DecoderException decoderExceptionCreateUnexpectedDecodeException;
        synchronized (this.lock) {
            while (!this.released && (this.queuedInputBuffers.isEmpty() || this.availableOutputBufferCount <= 0)) {
                try {
                    this.lock.wait();
                } finally {
                }
            }
            if (this.released) {
                return false;
            }
            f fVarRemoveFirst = this.queuedInputBuffers.removeFirst();
            g[] gVarArr = this.availableOutputBuffers;
            int i10 = this.availableOutputBufferCount - 1;
            this.availableOutputBufferCount = i10;
            g gVar = gVarArr[i10];
            boolean z10 = this.flushed;
            this.flushed = false;
            if (fVarRemoveFirst.isEndOfStream()) {
                gVar.addFlag(4);
            } else {
                gVar.timeUs = fVarRemoveFirst.timeUs;
                if (fVarRemoveFirst.isFirstSample()) {
                    gVar.addFlag(C.BUFFER_FLAG_FIRST_SAMPLE);
                }
                if (!isAtLeastOutputStartTimeUs(fVarRemoveFirst.timeUs)) {
                    gVar.shouldBeSkipped = true;
                }
                try {
                    decoderExceptionCreateUnexpectedDecodeException = decode(fVarRemoveFirst, gVar, z10);
                } catch (OutOfMemoryError e10) {
                    decoderExceptionCreateUnexpectedDecodeException = createUnexpectedDecodeException(e10);
                } catch (RuntimeException e11) {
                    decoderExceptionCreateUnexpectedDecodeException = createUnexpectedDecodeException(e11);
                }
                if (decoderExceptionCreateUnexpectedDecodeException != null) {
                    synchronized (this.lock) {
                        this.exception = decoderExceptionCreateUnexpectedDecodeException;
                    }
                    return false;
                }
            }
            synchronized (this.lock) {
                try {
                    if (this.flushed) {
                        gVar.release();
                    } else if (gVar.shouldBeSkipped) {
                        this.skippedOutputBufferCount++;
                        gVar.release();
                    } else {
                        gVar.skippedOutputBufferCount = this.skippedOutputBufferCount;
                        this.skippedOutputBufferCount = 0;
                        this.queuedOutputBuffers.addLast(gVar);
                    }
                    fVarRemoveFirst.clear();
                    f[] fVarArr = this.availableInputBuffers;
                    int i11 = this.availableInputBufferCount;
                    this.availableInputBufferCount = i11 + 1;
                    fVarArr[i11] = fVarRemoveFirst;
                } finally {
                }
            }
            return true;
        }
    }

    public abstract f createInputBuffer();

    public abstract g createOutputBuffer();

    public abstract DecoderException createUnexpectedDecodeException(Throwable th);

    @Nullable
    public abstract DecoderException decode(f fVar, g gVar, boolean z10);

    @Override // s1.e
    public final void flush() {
        synchronized (this.lock) {
            try {
                this.flushed = true;
                this.skippedOutputBufferCount = 0;
                f fVar = this.dequeuedInputBuffer;
                if (fVar != null) {
                    fVar.clear();
                    f[] fVarArr = this.availableInputBuffers;
                    int i10 = this.availableInputBufferCount;
                    this.availableInputBufferCount = i10 + 1;
                    fVarArr[i10] = fVar;
                    this.dequeuedInputBuffer = null;
                }
                while (!this.queuedInputBuffers.isEmpty()) {
                    f fVarRemoveFirst = this.queuedInputBuffers.removeFirst();
                    fVarRemoveFirst.clear();
                    f[] fVarArr2 = this.availableInputBuffers;
                    int i11 = this.availableInputBufferCount;
                    this.availableInputBufferCount = i11 + 1;
                    fVarArr2[i11] = fVarRemoveFirst;
                }
                while (!this.queuedOutputBuffers.isEmpty()) {
                    this.queuedOutputBuffers.removeFirst().release();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean isAtLeastOutputStartTimeUs(long j10) {
        boolean z10;
        synchronized (this.lock) {
            long j11 = this.outputStartTimeUs;
            z10 = j11 == -9223372036854775807L || j10 >= j11;
        }
        return z10;
    }

    @Override // s1.e
    @CallSuper
    public void release() {
        synchronized (this.lock) {
            this.released = true;
            this.lock.notify();
        }
        try {
            this.decodeThread.join();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
        }
    }

    @CallSuper
    public void releaseOutputBuffer(g gVar) {
        synchronized (this.lock) {
            gVar.clear();
            g[] gVarArr = this.availableOutputBuffers;
            int i10 = this.availableOutputBufferCount;
            this.availableOutputBufferCount = i10 + 1;
            gVarArr[i10] = gVar;
            if (!this.queuedInputBuffers.isEmpty() && this.availableOutputBufferCount > 0) {
                this.lock.notify();
            }
        }
    }

    public final void setInitialInputBufferSize(int i10) {
        Assertions.checkState(this.availableInputBufferCount == this.availableInputBuffers.length);
        for (f fVar : this.availableInputBuffers) {
            fVar.ensureSpaceForWrite(i10);
        }
    }

    @Override // s1.e
    public final void setOutputStartTimeUs(long j10) {
        synchronized (this.lock) {
            try {
                Assertions.checkState(this.availableInputBufferCount == this.availableInputBuffers.length || this.flushed);
                this.outputStartTimeUs = j10;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // s1.e
    @Nullable
    public final f dequeueInputBuffer() throws DecoderException {
        f fVar;
        synchronized (this.lock) {
            try {
                DecoderException decoderException = this.exception;
                if (decoderException != null) {
                    throw decoderException;
                }
                Assertions.checkState(this.dequeuedInputBuffer == null);
                int i10 = this.availableInputBufferCount;
                if (i10 == 0) {
                    fVar = null;
                } else {
                    f[] fVarArr = this.availableInputBuffers;
                    int i11 = i10 - 1;
                    this.availableInputBufferCount = i11;
                    fVar = fVarArr[i11];
                }
                this.dequeuedInputBuffer = fVar;
            } catch (Throwable th) {
                throw th;
            }
        }
        return fVar;
    }

    @Override // s1.e, androidx.media3.exoplayer.image.ImageDecoder
    @Nullable
    public final g dequeueOutputBuffer() throws DecoderException {
        synchronized (this.lock) {
            try {
                DecoderException decoderException = this.exception;
                if (decoderException != null) {
                    throw decoderException;
                }
                if (this.queuedOutputBuffers.isEmpty()) {
                    return null;
                }
                return this.queuedOutputBuffers.removeFirst();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // s1.e
    public final void queueInputBuffer(f fVar) throws DecoderException {
        synchronized (this.lock) {
            try {
                DecoderException decoderException = this.exception;
                if (decoderException != null) {
                    throw decoderException;
                }
                Assertions.checkArgument(fVar == this.dequeuedInputBuffer);
                this.queuedInputBuffers.addLast(fVar);
                if (!this.queuedInputBuffers.isEmpty() && this.availableOutputBufferCount > 0) {
                    this.lock.notify();
                }
                this.dequeuedInputBuffer = null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
