package s1;

import androidx.annotation.Nullable;
import androidx.media3.common.Format;
import androidx.media3.common.MediaLibraryInfo;
import java.nio.ByteBuffer;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;

/* JADX INFO: loaded from: classes.dex */
public class f extends a {
    public static final int BUFFER_REPLACEMENT_MODE_DIRECT = 2;
    public static final int BUFFER_REPLACEMENT_MODE_DISABLED = 0;
    public static final int BUFFER_REPLACEMENT_MODE_NORMAL = 1;
    private final int bufferReplacementMode;

    @Nullable
    public ByteBuffer data;

    @Nullable
    public Format format;

    @Nullable
    public ByteBuffer supplementalData;
    public long timeUs;
    public boolean waitingForKeys;
    public final d cryptoInfo = new d();
    private final int paddingSize = 0;

    static {
        MediaLibraryInfo.registerModule("media3.decoder");
    }

    public f(int i10) {
        this.bufferReplacementMode = i10;
    }

    public static f newNoDataInstance() {
        return new f(0);
    }

    @Override // s1.a
    public void clear() {
        super.clear();
        ByteBuffer byteBuffer = this.data;
        if (byteBuffer != null) {
            byteBuffer.clear();
        }
        ByteBuffer byteBuffer2 = this.supplementalData;
        if (byteBuffer2 != null) {
            byteBuffer2.clear();
        }
        this.waitingForKeys = false;
    }

    public final ByteBuffer e(final int i10) {
        int i11 = this.bufferReplacementMode;
        if (i11 == 1) {
            return ByteBuffer.allocate(i10);
        }
        if (i11 == 2) {
            return ByteBuffer.allocateDirect(i10);
        }
        ByteBuffer byteBuffer = this.data;
        final int iCapacity = byteBuffer == null ? 0 : byteBuffer.capacity();
        throw new IllegalStateException(iCapacity, i10) { // from class: androidx.media3.decoder.DecoderInputBuffer$InsufficientCapacityException
            public final int currentCapacity;
            public final int requiredCapacity;

            {
                super("Buffer too small (" + iCapacity + " < " + i10 + ")");
                this.currentCapacity = iCapacity;
                this.requiredCapacity = i10;
            }
        };
    }

    @EnsuresNonNull({"data"})
    public void ensureSpaceForWrite(int i10) {
        int i11 = i10 + this.paddingSize;
        ByteBuffer byteBuffer = this.data;
        if (byteBuffer == null) {
            this.data = e(i11);
            return;
        }
        int iCapacity = byteBuffer.capacity();
        int iPosition = byteBuffer.position();
        int i12 = i11 + iPosition;
        if (iCapacity >= i12) {
            this.data = byteBuffer;
            return;
        }
        ByteBuffer byteBufferE = e(i12);
        byteBufferE.order(byteBuffer.order());
        if (iPosition > 0) {
            byteBuffer.flip();
            byteBufferE.put(byteBuffer);
        }
        this.data = byteBufferE;
    }

    public final void flip() {
        ByteBuffer byteBuffer = this.data;
        if (byteBuffer != null) {
            byteBuffer.flip();
        }
        ByteBuffer byteBuffer2 = this.supplementalData;
        if (byteBuffer2 != null) {
            byteBuffer2.flip();
        }
    }

    public final boolean isEncrypted() {
        return getFlag(1073741824);
    }

    @EnsuresNonNull({"supplementalData"})
    public void resetSupplementalData(int i10) {
        ByteBuffer byteBuffer = this.supplementalData;
        if (byteBuffer == null || byteBuffer.capacity() < i10) {
            this.supplementalData = ByteBuffer.allocate(i10);
        } else {
            this.supplementalData.clear();
        }
    }
}
