package androidx.media3.decoder.av1;

import android.view.Surface;
import androidx.media3.common.C;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.decoder.Decoder;
import androidx.media3.decoder.DecoderInputBuffer;
import androidx.media3.decoder.VideoDecoderOutputBuffer;
import defpackage.dj1;
import defpackage.if7;
import defpackage.ny3;
import defpackage.rq6;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class Dav1dDecoder implements Decoder<DecoderInputBuffer, VideoDecoderOutputBuffer, Dav1dDecoderException> {
    private static final int DAV1D_DECODE_ONLY = 2;
    private static final int DAV1D_EAGAIN = 3;
    private static final int DAV1D_ERROR = 0;
    private static final int DAV1D_OK = 1;

    @ny3
    private int availableInputBufferCount;

    @ny3
    private final DecoderInputBuffer[] availableInputBuffers;

    @ny3
    private int availableOutputBufferCount;

    @ny3
    private final VideoDecoderOutputBuffer[] availableOutputBuffers;
    private long dav1dDecoderContext;
    private final Thread decodeThread;

    @rq6
    @ny3
    private DecoderInputBuffer dequeuedInputBuffer;

    @rq6
    @ny3
    private Dav1dDecoderException exception;

    @ny3
    private boolean flushed;
    private final Object lock;
    private volatile int outputMode;

    @ny3
    private long outputStartTimeUs;

    @ny3
    private final ArrayDeque<DecoderInputBuffer> queuedInputBuffers;

    @ny3
    private final ArrayDeque<VideoDecoderOutputBuffer> queuedOutputBuffers;

    @ny3
    private boolean released;

    @ny3
    private int skippedOutputBufferCount;

    @rq6
    private Surface surface;

    public Dav1dDecoder(int i, int i2, int i3, final int i4, final int i5, final boolean z) throws Dav1dDecoderException {
        if (!Dav1dLibrary.isAvailable()) {
            throw new Dav1dDecoderException("Failed to load decoder native library.");
        }
        this.lock = new Object();
        this.outputStartTimeUs = C.TIME_UNSET;
        this.queuedInputBuffers = new ArrayDeque<>();
        this.queuedOutputBuffers = new ArrayDeque<>();
        this.availableInputBuffers = new DecoderInputBuffer[i];
        this.availableInputBufferCount = i;
        for (int i6 = 0; i6 < this.availableInputBufferCount; i6++) {
            this.availableInputBuffers[i6] = new DecoderInputBuffer(2);
            this.availableInputBuffers[i6].ensureSpaceForWrite(i3);
        }
        this.availableOutputBuffers = new VideoDecoderOutputBuffer[i2];
        this.availableOutputBufferCount = i2;
        for (int i7 = 0; i7 < this.availableOutputBufferCount; i7++) {
            this.availableOutputBuffers[i7] = new VideoDecoderOutputBuffer(new dj1(this, 17));
        }
        Thread thread = new Thread("ExoPlayer:Dav1dDecoder") { // from class: androidx.media3.decoder.av1.Dav1dDecoder.1
            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                Dav1dDecoder dav1dDecoder = Dav1dDecoder.this;
                dav1dDecoder.dav1dDecoderContext = dav1dDecoder.dav1dInit(i4, i5, z);
                Dav1dDecoder dav1dDecoder2 = Dav1dDecoder.this;
                if (dav1dDecoder2.dav1dCheckError(dav1dDecoder2.dav1dDecoderContext) != 0) {
                    Dav1dDecoder.this.run();
                    Dav1dDecoder dav1dDecoder3 = Dav1dDecoder.this;
                    dav1dDecoder3.releaseUnusedInputBuffers(dav1dDecoder3.dav1dDecoderContext, Dav1dDecoder.this);
                    Dav1dDecoder dav1dDecoder4 = Dav1dDecoder.this;
                    dav1dDecoder4.dav1dClose(dav1dDecoder4.dav1dDecoderContext);
                    return;
                }
                synchronized (Dav1dDecoder.this.lock) {
                    Dav1dDecoder dav1dDecoder5 = Dav1dDecoder.this;
                    StringBuilder sb = new StringBuilder("Failed to initialize decoder. Error: ");
                    Dav1dDecoder dav1dDecoder6 = Dav1dDecoder.this;
                    sb.append(dav1dDecoder6.dav1dGetErrorMessage(dav1dDecoder6.dav1dDecoderContext));
                    dav1dDecoder5.exception = new Dav1dDecoderException(sb.toString());
                }
                Dav1dDecoder dav1dDecoder7 = Dav1dDecoder.this;
                dav1dDecoder7.dav1dClose(dav1dDecoder7.dav1dDecoderContext);
            }
        };
        this.decodeThread = thread;
        thread.start();
        maybeThrowException();
    }

    @ny3
    private boolean canDecodeInputBuffer() {
        return !this.queuedInputBuffers.isEmpty();
    }

    @ny3
    private boolean canDecodeOutputBuffer() {
        return this.availableOutputBufferCount > 0;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public native int dav1dCheckError(long j);

    /* JADX INFO: Access modifiers changed from: private */
    public native void dav1dClose(long j);

    private native int dav1dDecode(long j, DecoderInputBuffer decoderInputBuffer, int i, int i2, boolean z, int i3, long j2, int i4);

    private native void dav1dFlush(long j);

    /* JADX INFO: Access modifiers changed from: private */
    public native String dav1dGetErrorMessage(long j);

    private native int dav1dGetFrame(long j, VideoDecoderOutputBuffer videoDecoderOutputBuffer);

    /* JADX INFO: Access modifiers changed from: private */
    public native long dav1dInit(int i, int i2, boolean z);

    private native void dav1dReleaseFrame(long j, VideoDecoderOutputBuffer videoDecoderOutputBuffer);

    private native int dav1dRenderFrame(long j, Surface surface, VideoDecoderOutputBuffer videoDecoderOutputBuffer);

    private boolean decode() throws InterruptedException {
        Dav1dDecoder dav1dDecoder;
        ByteBuffer byteBuffer;
        int iDav1dGetFrame;
        synchronized (this.lock) {
            try {
                if (this.flushed) {
                    flushInternal();
                }
                while (!this.released && ((!canDecodeInputBuffer() || !canDecodeOutputBuffer()) && !this.flushed)) {
                    this.lock.wait();
                }
                if (this.released) {
                    return false;
                }
                if (this.flushed) {
                    flushInternal();
                    return true;
                }
                DecoderInputBuffer decoderInputBufferRemoveFirst = this.queuedInputBuffers.removeFirst();
                VideoDecoderOutputBuffer[] videoDecoderOutputBufferArr = this.availableOutputBuffers;
                int i = this.availableOutputBufferCount - 1;
                this.availableOutputBufferCount = i;
                VideoDecoderOutputBuffer videoDecoderOutputBuffer = videoDecoderOutputBufferArr[i];
                if (decoderInputBufferRemoveFirst.isEndOfStream()) {
                    videoDecoderOutputBuffer.addFlag(4);
                    releaseInputBuffer(decoderInputBufferRemoveFirst);
                    synchronized (this.lock) {
                        try {
                            if (this.flushed) {
                                videoDecoderOutputBuffer.release();
                                flushInternal();
                            } else {
                                videoDecoderOutputBuffer.skippedOutputBufferCount = this.skippedOutputBufferCount;
                                this.skippedOutputBufferCount = 0;
                                this.queuedOutputBuffers.addLast(videoDecoderOutputBuffer);
                            }
                        } finally {
                        }
                    }
                    dav1dDecoder = this;
                } else {
                    boolean z = !isAtLeastOutputStartTimeUs(decoderInputBufferRemoveFirst.timeUs);
                    int i2 = decoderInputBufferRemoveFirst.isFirstSample() ? 134217728 : 0;
                    try {
                        byteBuffer = (ByteBuffer) Util.castNonNull(decoderInputBufferRemoveFirst.data);
                        dav1dDecoder = this;
                        try {
                        } catch (Dav1dDecoderException e) {
                            e = e;
                        } catch (OutOfMemoryError e2) {
                            e = e2;
                            e = createUnexpectedDecodeException(e);
                        } catch (RuntimeException e3) {
                            e = e3;
                            e = createUnexpectedDecodeException(e);
                        }
                    } catch (Dav1dDecoderException e4) {
                        e = e4;
                        dav1dDecoder = this;
                    } catch (OutOfMemoryError e5) {
                        e = e5;
                        dav1dDecoder = this;
                    } catch (RuntimeException e6) {
                        e = e6;
                        dav1dDecoder = this;
                    }
                    if (dav1dDecoder.dav1dDecode(this.dav1dDecoderContext, decoderInputBufferRemoveFirst, byteBuffer.position(), byteBuffer.remaining(), z, i2, decoderInputBufferRemoveFirst.timeUs, this.outputMode) == 0) {
                        throw new Dav1dDecoderException("dav1dDecode error: " + dav1dGetErrorMessage(dav1dDecoder.dav1dDecoderContext));
                    }
                    while (true) {
                        iDav1dGetFrame = dav1dGetFrame(dav1dDecoder.dav1dDecoderContext, videoDecoderOutputBuffer);
                        if (iDav1dGetFrame != 1 && iDav1dGetFrame != 2) {
                            break;
                        }
                        if (iDav1dGetFrame == 2) {
                            videoDecoderOutputBuffer.shouldBeSkipped = true;
                        }
                        synchronized (dav1dDecoder.lock) {
                            if (dav1dDecoder.flushed) {
                                videoDecoderOutputBuffer.release();
                                flushInternal();
                            } else {
                                if (!isAtLeastOutputStartTimeUs(videoDecoderOutputBuffer.timeUs) || videoDecoderOutputBuffer.shouldBeSkipped) {
                                    dav1dDecoder.skippedOutputBufferCount++;
                                    videoDecoderOutputBuffer.release();
                                } else {
                                    videoDecoderOutputBuffer.skippedOutputBufferCount = dav1dDecoder.skippedOutputBufferCount;
                                    dav1dDecoder.skippedOutputBufferCount = 0;
                                    dav1dDecoder.queuedOutputBuffers.addLast(videoDecoderOutputBuffer);
                                }
                                while (!dav1dDecoder.released && !canDecodeOutputBuffer() && !dav1dDecoder.flushed) {
                                    dav1dDecoder.lock.wait();
                                }
                                if (dav1dDecoder.released) {
                                    return false;
                                }
                                if (dav1dDecoder.flushed) {
                                    flushInternal();
                                    return true;
                                }
                                VideoDecoderOutputBuffer[] videoDecoderOutputBufferArr2 = dav1dDecoder.availableOutputBuffers;
                                int i3 = dav1dDecoder.availableOutputBufferCount - 1;
                                dav1dDecoder.availableOutputBufferCount = i3;
                                videoDecoderOutputBuffer = videoDecoderOutputBufferArr2[i3];
                            }
                        }
                        break;
                    }
                    if (iDav1dGetFrame == 0) {
                        throw new Dav1dDecoderException("dav1dGetFrame error: " + dav1dGetErrorMessage(dav1dDecoder.dav1dDecoderContext));
                    }
                    if (iDav1dGetFrame == 3) {
                        videoDecoderOutputBuffer.release();
                    }
                    e = null;
                    if (e != null) {
                        synchronized (dav1dDecoder.lock) {
                            dav1dDecoder.exception = e;
                        }
                        return false;
                    }
                }
                releaseUnusedInputBuffers(dav1dDecoder.dav1dDecoderContext, this);
                return true;
            } finally {
                th = th;
                while (true) {
                    try {
                    } catch (Throwable th) {
                        th = th;
                    }
                }
            }
        }
    }

    @ny3
    private void flushInternal() {
        this.skippedOutputBufferCount = 0;
        DecoderInputBuffer decoderInputBuffer = this.dequeuedInputBuffer;
        if (decoderInputBuffer != null) {
            releaseInputBufferInternal(decoderInputBuffer);
            this.dequeuedInputBuffer = null;
        }
        while (!this.queuedInputBuffers.isEmpty()) {
            releaseInputBufferInternal(this.queuedInputBuffers.removeFirst());
        }
        while (!this.queuedOutputBuffers.isEmpty()) {
            this.queuedOutputBuffers.removeFirst().release();
        }
        dav1dFlush(this.dav1dDecoderContext);
        this.flushed = false;
    }

    @ny3
    private void maybeNotifyDecodeLoop() {
        if (canDecodeInputBuffer() || canDecodeOutputBuffer()) {
            this.lock.notify();
        }
    }

    @ny3
    private void maybeThrowException() throws Dav1dDecoderException {
        Dav1dDecoderException dav1dDecoderException = this.exception;
        if (dav1dDecoderException != null) {
            throw dav1dDecoderException;
        }
    }

    private void releaseInputBuffer(DecoderInputBuffer decoderInputBuffer) {
        synchronized (this.lock) {
            releaseInputBufferInternal(decoderInputBuffer);
        }
    }

    @ny3
    private void releaseInputBufferInternal(DecoderInputBuffer decoderInputBuffer) {
        decoderInputBuffer.clear();
        DecoderInputBuffer[] decoderInputBufferArr = this.availableInputBuffers;
        int i = this.availableInputBufferCount;
        this.availableInputBufferCount = i + 1;
        decoderInputBufferArr[i] = decoderInputBuffer;
    }

    @ny3
    private void releaseOutputBufferInternal(VideoDecoderOutputBuffer videoDecoderOutputBuffer) {
        videoDecoderOutputBuffer.clear();
        VideoDecoderOutputBuffer[] videoDecoderOutputBufferArr = this.availableOutputBuffers;
        int i = this.availableOutputBufferCount;
        this.availableOutputBufferCount = i + 1;
        videoDecoderOutputBufferArr[i] = videoDecoderOutputBuffer;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public native void releaseUnusedInputBuffers(long j, Dav1dDecoder dav1dDecoder);

    /* JADX INFO: Access modifiers changed from: private */
    public void run() {
        do {
            try {
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        } while (decode());
    }

    public Dav1dDecoderException createUnexpectedDecodeException(Throwable th) {
        return new Dav1dDecoderException("Unexpected decode error", th);
    }

    @Override // androidx.media3.decoder.Decoder
    public final void flush() {
        synchronized (this.lock) {
            this.flushed = true;
            this.lock.notify();
        }
    }

    @Override // androidx.media3.decoder.Decoder
    public String getName() {
        return "libdav1d";
    }

    public final boolean isAtLeastOutputStartTimeUs(long j) {
        boolean z;
        synchronized (this.lock) {
            long j2 = this.outputStartTimeUs;
            z = j2 == C.TIME_UNSET || j >= j2;
        }
        return z;
    }

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

    public void releaseOutputBuffer(VideoDecoderOutputBuffer videoDecoderOutputBuffer) {
        synchronized (this.lock) {
            dav1dReleaseFrame(this.dav1dDecoderContext, videoDecoderOutputBuffer);
            releaseOutputBufferInternal(videoDecoderOutputBuffer);
            maybeNotifyDecodeLoop();
        }
    }

    public void renderToSurface(VideoDecoderOutputBuffer videoDecoderOutputBuffer, Surface surface) throws Dav1dDecoderException {
        if (this.outputMode != 1) {
            throw new Dav1dDecoderException("Unsupported Output Mode.");
        }
        if (dav1dRenderFrame(this.dav1dDecoderContext, surface, videoDecoderOutputBuffer) != 1) {
            throw new Dav1dDecoderException("Failed to render output buffer to surface.");
        }
    }

    public void setOutputMode(int i) {
        this.outputMode = i;
    }

    @Override // androidx.media3.decoder.Decoder
    public final void setOutputStartTimeUs(long j) {
        synchronized (this.lock) {
            this.outputStartTimeUs = j;
        }
    }

    /* JADX WARN: Can't rename method to resolve collision */
    @Override // androidx.media3.decoder.Decoder
    @rq6
    public final DecoderInputBuffer dequeueInputBuffer() throws Dav1dDecoderException {
        DecoderInputBuffer decoderInputBuffer;
        synchronized (this.lock) {
            try {
                maybeThrowException();
                if7.o(this.dequeuedInputBuffer == null || this.flushed);
                int i = this.availableInputBufferCount;
                if (i == 0 || this.flushed) {
                    decoderInputBuffer = null;
                } else {
                    DecoderInputBuffer[] decoderInputBufferArr = this.availableInputBuffers;
                    int i2 = i - 1;
                    this.availableInputBufferCount = i2;
                    decoderInputBuffer = decoderInputBufferArr[i2];
                }
                this.dequeuedInputBuffer = decoderInputBuffer;
            } catch (Throwable th) {
                throw th;
            }
        }
        return decoderInputBuffer;
    }

    /* JADX WARN: Can't rename method to resolve collision */
    @Override // androidx.media3.decoder.Decoder
    @rq6
    public final VideoDecoderOutputBuffer dequeueOutputBuffer() throws Dav1dDecoderException {
        synchronized (this.lock) {
            try {
                maybeThrowException();
                if (!this.queuedOutputBuffers.isEmpty() && !this.flushed) {
                    return this.queuedOutputBuffers.removeFirst();
                }
                return null;
            } finally {
            }
        }
    }

    @Override // androidx.media3.decoder.Decoder
    public final void queueInputBuffer(DecoderInputBuffer decoderInputBuffer) throws Dav1dDecoderException {
        synchronized (this.lock) {
            maybeThrowException();
            if7.e(decoderInputBuffer == this.dequeuedInputBuffer);
            this.queuedInputBuffers.addLast(decoderInputBuffer);
            maybeNotifyDecodeLoop();
            this.dequeuedInputBuffer = null;
        }
    }
}
