package defpackage;

import androidx.media3.common.audio.AudioProcessor;
import androidx.media3.common.audio.BaseAudioProcessor;
import androidx.media3.container.NalUnitUtil;
import java.nio.ByteBuffer;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@qx6
@Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b\u0007\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lkx;", "Landroidx/media3/common/audio/BaseAudioProcessor;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
public final class kx extends BaseAudioProcessor {
    public volatile long a;
    public byte[] b;
    public int c;
    public int d;
    public int e;
    public int f;
    public int g;
    public int h;
    public int i;

    public final void a() {
        if (this.i == 0 || this.h == 0) {
            return;
        }
        long j = this.a;
        if (j <= 0) {
            if (j < 0) {
                this.g = ((int) ((((long) this.i) * (-j)) / ((long) 1000))) * this.h;
                this.b = new byte[0];
                this.f = 0;
                this.c = 0;
                this.d = 0;
                this.e = 0;
                return;
            }
            this.g = 0;
            this.b = new byte[0];
            this.f = 0;
            this.c = 0;
            this.d = 0;
            this.e = 0;
            return;
        }
        int i = ((int) ((((long) this.i) * j) / ((long) 1000))) * this.h;
        this.g = 0;
        int i2 = this.f;
        if (i != i2) {
            byte[] bArr = new byte[i];
            int i3 = this.e;
            if (i3 <= 0 || i2 <= 0) {
                this.c = 0;
                this.d = 0;
                this.e = 0;
            } else {
                int iMin = Math.min(i3, i);
                int i4 = this.e >= this.f ? this.d : 0;
                for (int i5 = 0; i5 < iMin; i5++) {
                    bArr[i5] = this.b[(((this.e - iMin) + i4) + i5) % this.f];
                }
                this.c = iMin % i;
                this.d = 0;
                this.e = iMin;
            }
            this.b = bArr;
            this.f = i;
        }
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor
    public final AudioProcessor.AudioFormat onConfigure(AudioProcessor.AudioFormat audioFormat) {
        cr4.g(audioFormat, "inputAudioFormat");
        this.i = audioFormat.sampleRate;
        this.h = audioFormat.bytesPerFrame;
        a();
        return audioFormat;
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor
    public final void onFlush() {
        super.onFlush();
        this.c = 0;
        this.d = 0;
        this.e = 0;
        a();
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor
    public final void onReset() {
        super.onReset();
        this.b = new byte[0];
        this.f = 0;
        this.c = 0;
        this.d = 0;
        this.e = 0;
        this.g = 0;
        this.i = 0;
        this.h = 0;
    }

    @Override // androidx.media3.common.audio.AudioProcessor
    public final void queueInput(ByteBuffer byteBuffer) {
        cr4.g(byteBuffer, "inputBuffer");
        int iRemaining = byteBuffer.remaining();
        if (iRemaining == 0) {
            return;
        }
        long j = this.a;
        if (j == 0) {
            ByteBuffer byteBufferReplaceOutputBuffer = replaceOutputBuffer(iRemaining);
            cr4.f(byteBufferReplaceOutputBuffer, "replaceOutputBuffer(...)");
            byteBufferReplaceOutputBuffer.put(byteBuffer);
            byteBufferReplaceOutputBuffer.flip();
            return;
        }
        if (j <= 0) {
            int i = this.g;
            if (i <= 0) {
                ByteBuffer byteBufferReplaceOutputBuffer2 = replaceOutputBuffer(iRemaining);
                cr4.f(byteBufferReplaceOutputBuffer2, "replaceOutputBuffer(...)");
                byteBufferReplaceOutputBuffer2.put(byteBuffer);
                byteBufferReplaceOutputBuffer2.flip();
                return;
            }
            int iMin = Math.min(i, iRemaining);
            this.g -= iMin;
            int i2 = iRemaining - iMin;
            if (i2 <= 0) {
                byteBuffer.position(byteBuffer.limit());
                ByteBuffer byteBufferReplaceOutputBuffer3 = replaceOutputBuffer(0);
                cr4.f(byteBufferReplaceOutputBuffer3, "replaceOutputBuffer(...)");
                byteBufferReplaceOutputBuffer3.flip();
                return;
            }
            byteBuffer.position(byteBuffer.position() + iMin);
            ByteBuffer byteBufferReplaceOutputBuffer4 = replaceOutputBuffer(i2);
            cr4.f(byteBufferReplaceOutputBuffer4, "replaceOutputBuffer(...)");
            byteBufferReplaceOutputBuffer4.put(byteBuffer);
            byteBufferReplaceOutputBuffer4.flip();
            return;
        }
        if (this.f == 0) {
            ByteBuffer byteBufferReplaceOutputBuffer5 = replaceOutputBuffer(iRemaining);
            cr4.f(byteBufferReplaceOutputBuffer5, "replaceOutputBuffer(...)");
            byteBufferReplaceOutputBuffer5.put(byteBuffer);
            byteBufferReplaceOutputBuffer5.flip();
            return;
        }
        ByteBuffer byteBufferReplaceOutputBuffer6 = replaceOutputBuffer(iRemaining);
        cr4.f(byteBufferReplaceOutputBuffer6, "replaceOutputBuffer(...)");
        byte[] bArr = new byte[iRemaining];
        byteBuffer.get(bArr);
        int i3 = this.e;
        if (i3 < this.f) {
            int iMin2 = Math.min(iRemaining, i3);
            int i4 = iRemaining - iMin2;
            for (int i5 = 0; i5 < i4; i5++) {
                byteBufferReplaceOutputBuffer6.put((byte) 0);
            }
            for (int i6 = 0; i6 < iMin2; i6++) {
                byteBufferReplaceOutputBuffer6.put(this.b[(this.d + i6) % this.f]);
            }
            if (iMin2 > 0) {
                this.d = (this.d + iMin2) % this.f;
            }
        } else {
            for (int i7 = 0; i7 < iRemaining; i7++) {
                byteBufferReplaceOutputBuffer6.put(this.b[(this.d + i7) % this.f]);
            }
            this.d = (this.d + iRemaining) % this.f;
        }
        for (int i8 = 0; i8 < iRemaining; i8++) {
            byte[] bArr2 = this.b;
            int i9 = this.c;
            bArr2[i9] = bArr[i8];
            this.c = (i9 + 1) % this.f;
        }
        this.e = Math.min(this.e + iRemaining, this.f);
        byteBufferReplaceOutputBuffer6.flip();
    }
}
