package androidx.media3.exoplayer.audio;

import androidx.media3.common.audio.AudioProcessor;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class SilenceSkippingAudioProcessor extends androidx.media3.common.audio.BaseAudioProcessor {
    private static final int AVOID_TRUNCATION_FACTOR = 1000;
    public static final long DEFAULT_MAX_SILENCE_TO_KEEP_DURATION_US = 2000000;
    public static final long DEFAULT_MINIMUM_SILENCE_DURATION_US = 100000;
    public static final int DEFAULT_MIN_VOLUME_TO_KEEP_PERCENTAGE = 10;

    @Deprecated
    public static final long DEFAULT_PADDING_SILENCE_US = 20000;
    public static final float DEFAULT_SILENCE_RETENTION_RATIO = 0.2f;
    public static final short DEFAULT_SILENCE_THRESHOLD_LEVEL = 1024;
    private static final int DO_NOT_CHANGE_VOLUME = 3;
    private static final int FADE_IN = 2;
    private static final int FADE_OUT = 0;
    private static final int MUTE = 1;
    private static final int STATE_NOISY = 0;
    private static final int STATE_SHORTENING_SILENCE = 1;
    private int bytesPerFrame;
    private byte[] contiguousOutputBuffer;
    private boolean enabled;
    private final long maxSilenceToKeepDurationUs;
    private byte[] maybeSilenceBuffer;
    private int maybeSilenceBufferContentsSize;
    private int maybeSilenceBufferStartIndex;
    private final int minVolumeToKeepPercentageWhenMuting;
    private final long minimumSilenceDurationUs;
    private int outputSilenceFramesSinceNoise;
    private final float silenceRetentionRatio;
    private final short silenceThresholdLevel;
    private long skippedFrames;
    private int state;

    public SilenceSkippingAudioProcessor() {
        this(DEFAULT_MINIMUM_SILENCE_DURATION_US, 0.2f, DEFAULT_MAX_SILENCE_TO_KEEP_DURATION_US, 10, DEFAULT_SILENCE_THRESHOLD_LEVEL);
    }

    private int alignToBytePerFrameBoundary(int i6) {
        int i7 = this.bytesPerFrame;
        return (i6 / i7) * i7;
    }

    private int calculateFadeInPercentage(int i6, int i7) {
        int i8 = this.minVolumeToKeepPercentageWhenMuting;
        return ((((i6 * 1000) * (100 - i8)) / i7) / 1000) + i8;
    }

    private int calculateFadeOutPercentage(int i6, int i7) {
        return ((((i6 * 1000) / i7) * (this.minVolumeToKeepPercentageWhenMuting - 100)) / 1000) + 100;
    }

    private int calculateShortenedSilenceLength(int i6) {
        int iDurationUsToFrames = ((durationUsToFrames(this.maxSilenceToKeepDurationUs) - this.outputSilenceFramesSinceNoise) * this.bytesPerFrame) - (this.maybeSilenceBuffer.length / 2);
        Assertions.checkState(iDurationUsToFrames >= 0);
        return alignToBytePerFrameBoundary(Math.min((i6 * this.silenceRetentionRatio) + 0.5f, iDurationUsToFrames));
    }

    private int durationUsToFrames(long j4) {
        return (int) ((j4 * ((long) this.inputAudioFormat.sampleRate)) / 1000000);
    }

    private int findNoiseLimit(ByteBuffer byteBuffer) {
        for (int iLimit = byteBuffer.limit() - 1; iLimit >= byteBuffer.position(); iLimit -= 2) {
            if (isNoise(byteBuffer.get(iLimit), byteBuffer.get(iLimit - 1))) {
                int i6 = this.bytesPerFrame;
                return ((iLimit / i6) * i6) + i6;
            }
        }
        return byteBuffer.position();
    }

    private int findNoisePosition(ByteBuffer byteBuffer) {
        for (int iPosition = byteBuffer.position() + 1; iPosition < byteBuffer.limit(); iPosition += 2) {
            if (isNoise(byteBuffer.get(iPosition), byteBuffer.get(iPosition - 1))) {
                int i6 = this.bytesPerFrame;
                return (iPosition / i6) * i6;
            }
        }
        return byteBuffer.limit();
    }

    private boolean isNoise(byte b5, byte b6) {
        return Math.abs(twoByteSampleToInt(b5, b6)) > this.silenceThresholdLevel;
    }

    private void modifyVolume(byte[] bArr, int i6, int i7) {
        if (i7 == 3) {
            return;
        }
        for (int i8 = 0; i8 < i6; i8 += 2) {
            sampleIntToTwoBigEndianBytes(bArr, i8, (twoByteSampleToInt(bArr[i8 + 1], bArr[i8]) * (i7 == 0 ? calculateFadeOutPercentage(i8, i6 - 1) : i7 == 2 ? calculateFadeInPercentage(i8, i6 - 1) : this.minVolumeToKeepPercentageWhenMuting)) / 100);
        }
    }

    private void output(ByteBuffer byteBuffer) {
        replaceOutputBuffer(byteBuffer.remaining()).put(byteBuffer).flip();
    }

    private void outputRange(byte[] bArr, int i6, int i7) {
        Assertions.checkArgument(i6 % this.bytesPerFrame == 0, "byteOutput size is not aligned to frame size " + i6);
        modifyVolume(bArr, i6, i7);
        replaceOutputBuffer(i6).put(bArr, 0, i6).flip();
    }

    private void outputShortenedSilenceBuffer(boolean z6) {
        int length;
        int iCalculateShortenedSilenceLength;
        int i6 = this.maybeSilenceBufferContentsSize;
        byte[] bArr = this.maybeSilenceBuffer;
        if (i6 == bArr.length || z6) {
            if (this.outputSilenceFramesSinceNoise == 0) {
                if (z6) {
                    outputSilence(i6, 3);
                    length = i6;
                } else {
                    Assertions.checkState(i6 >= bArr.length / 2);
                    length = this.maybeSilenceBuffer.length / 2;
                    outputSilence(length, 0);
                }
                iCalculateShortenedSilenceLength = length;
            } else if (z6) {
                int length2 = i6 - (bArr.length / 2);
                int length3 = (bArr.length / 2) + length2;
                int iCalculateShortenedSilenceLength2 = calculateShortenedSilenceLength(length2) + (this.maybeSilenceBuffer.length / 2);
                outputSilence(iCalculateShortenedSilenceLength2, 2);
                iCalculateShortenedSilenceLength = iCalculateShortenedSilenceLength2;
                length = length3;
            } else {
                length = i6 - (bArr.length / 2);
                iCalculateShortenedSilenceLength = calculateShortenedSilenceLength(length);
                outputSilence(iCalculateShortenedSilenceLength, 1);
            }
            Assertions.checkState(length % this.bytesPerFrame == 0, "bytesConsumed is not aligned to frame size: %s" + length);
            Assertions.checkState(i6 >= iCalculateShortenedSilenceLength);
            this.maybeSilenceBufferContentsSize -= length;
            int i7 = this.maybeSilenceBufferStartIndex + length;
            this.maybeSilenceBufferStartIndex = i7;
            this.maybeSilenceBufferStartIndex = i7 % this.maybeSilenceBuffer.length;
            int i8 = this.outputSilenceFramesSinceNoise;
            int i9 = this.bytesPerFrame;
            this.outputSilenceFramesSinceNoise = (iCalculateShortenedSilenceLength / i9) + i8;
            this.skippedFrames += (long) ((length - iCalculateShortenedSilenceLength) / i9);
        }
    }

    private void outputSilence(int i6, int i7) {
        if (i6 == 0) {
            return;
        }
        Assertions.checkArgument(this.maybeSilenceBufferContentsSize >= i6);
        if (i7 == 2) {
            int i8 = this.maybeSilenceBufferStartIndex;
            int i9 = this.maybeSilenceBufferContentsSize;
            int i10 = i8 + i9;
            byte[] bArr = this.maybeSilenceBuffer;
            if (i10 <= bArr.length) {
                System.arraycopy(bArr, (i8 + i9) - i6, this.contiguousOutputBuffer, 0, i6);
            } else {
                int length = i9 - (bArr.length - i8);
                if (length >= i6) {
                    System.arraycopy(bArr, length - i6, this.contiguousOutputBuffer, 0, i6);
                } else {
                    int i11 = i6 - length;
                    System.arraycopy(bArr, bArr.length - i11, this.contiguousOutputBuffer, 0, i11);
                    System.arraycopy(this.maybeSilenceBuffer, 0, this.contiguousOutputBuffer, i11, length);
                }
            }
        } else {
            int i12 = this.maybeSilenceBufferStartIndex;
            int i13 = i12 + i6;
            byte[] bArr2 = this.maybeSilenceBuffer;
            if (i13 <= bArr2.length) {
                System.arraycopy(bArr2, i12, this.contiguousOutputBuffer, 0, i6);
            } else {
                int length2 = bArr2.length - i12;
                System.arraycopy(bArr2, i12, this.contiguousOutputBuffer, 0, length2);
                System.arraycopy(this.maybeSilenceBuffer, 0, this.contiguousOutputBuffer, length2, i6 - length2);
            }
        }
        Assertions.checkArgument(i6 % this.bytesPerFrame == 0, "sizeToOutput is not aligned to frame size: " + i6);
        Assertions.checkState(this.maybeSilenceBufferStartIndex < this.maybeSilenceBuffer.length);
        outputRange(this.contiguousOutputBuffer, i6, i7);
    }

    private void processNoisy(ByteBuffer byteBuffer) {
        int iLimit = byteBuffer.limit();
        byteBuffer.limit(Math.min(iLimit, byteBuffer.position() + this.maybeSilenceBuffer.length));
        int iFindNoiseLimit = findNoiseLimit(byteBuffer);
        if (iFindNoiseLimit == byteBuffer.position()) {
            this.state = 1;
        } else {
            byteBuffer.limit(Math.min(iFindNoiseLimit, byteBuffer.capacity()));
            output(byteBuffer);
        }
        byteBuffer.limit(iLimit);
    }

    private static void sampleIntToTwoBigEndianBytes(byte[] bArr, int i6, int i7) {
        if (i7 >= 32767) {
            bArr[i6] = -1;
            bArr[i6 + 1] = 127;
        } else if (i7 <= -32768) {
            bArr[i6] = 0;
            bArr[i6 + 1] = -128;
        } else {
            bArr[i6] = (byte) (i7 & 255);
            bArr[i6 + 1] = (byte) (i7 >> 8);
        }
    }

    private void shortenSilenceSilenceUntilNoise(ByteBuffer byteBuffer) {
        int length;
        int i6;
        Assertions.checkState(this.maybeSilenceBufferStartIndex < this.maybeSilenceBuffer.length);
        int iLimit = byteBuffer.limit();
        int iFindNoisePosition = findNoisePosition(byteBuffer);
        int iPosition = iFindNoisePosition - byteBuffer.position();
        int i7 = this.maybeSilenceBufferStartIndex;
        int i8 = this.maybeSilenceBufferContentsSize;
        int i9 = i7 + i8;
        byte[] bArr = this.maybeSilenceBuffer;
        if (i9 < bArr.length) {
            length = bArr.length - (i8 + i7);
            i6 = i7 + i8;
        } else {
            int length2 = i8 - (bArr.length - i7);
            length = i7 - length2;
            i6 = length2;
        }
        boolean z6 = iFindNoisePosition < iLimit;
        int iMin = Math.min(iPosition, length);
        byteBuffer.limit(byteBuffer.position() + iMin);
        byteBuffer.get(this.maybeSilenceBuffer, i6, iMin);
        int i10 = this.maybeSilenceBufferContentsSize + iMin;
        this.maybeSilenceBufferContentsSize = i10;
        Assertions.checkState(i10 <= this.maybeSilenceBuffer.length);
        boolean z7 = z6 && iPosition < length;
        outputShortenedSilenceBuffer(z7);
        if (z7) {
            this.state = 0;
            this.outputSilenceFramesSinceNoise = 0;
        }
        byteBuffer.limit(iLimit);
    }

    private static int twoByteSampleToInt(byte b5, byte b6) {
        return (b5 << 8) | (b6 & 255);
    }

    public long getSkippedFrames() {
        return this.skippedFrames;
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor, androidx.media3.common.audio.AudioProcessor
    public boolean isActive() {
        return super.isActive() && this.enabled;
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor
    public AudioProcessor.AudioFormat onConfigure(AudioProcessor.AudioFormat audioFormat) throws AudioProcessor.UnhandledAudioFormatException {
        if (audioFormat.encoding == 2) {
            return audioFormat.sampleRate == -1 ? AudioProcessor.AudioFormat.NOT_SET : audioFormat;
        }
        throw new AudioProcessor.UnhandledAudioFormatException(audioFormat);
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor
    public void onFlush() {
        if (isActive()) {
            this.bytesPerFrame = this.inputAudioFormat.channelCount * 2;
            int iAlignToBytePerFrameBoundary = alignToBytePerFrameBoundary(durationUsToFrames(this.minimumSilenceDurationUs) / 2) * 2;
            if (this.maybeSilenceBuffer.length != iAlignToBytePerFrameBoundary) {
                this.maybeSilenceBuffer = new byte[iAlignToBytePerFrameBoundary];
                this.contiguousOutputBuffer = new byte[iAlignToBytePerFrameBoundary];
            }
        }
        this.state = 0;
        this.skippedFrames = 0L;
        this.outputSilenceFramesSinceNoise = 0;
        this.maybeSilenceBufferStartIndex = 0;
        this.maybeSilenceBufferContentsSize = 0;
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor
    public void onQueueEndOfStream() {
        if (this.maybeSilenceBufferContentsSize > 0) {
            outputShortenedSilenceBuffer(true);
            this.outputSilenceFramesSinceNoise = 0;
        }
    }

    @Override // androidx.media3.common.audio.BaseAudioProcessor
    public void onReset() {
        this.enabled = false;
        byte[] bArr = Util.EMPTY_BYTE_ARRAY;
        this.maybeSilenceBuffer = bArr;
        this.contiguousOutputBuffer = bArr;
    }

    @Override // androidx.media3.common.audio.AudioProcessor
    public void queueInput(ByteBuffer byteBuffer) {
        while (byteBuffer.hasRemaining() && !hasPendingOutput()) {
            int i6 = this.state;
            if (i6 == 0) {
                processNoisy(byteBuffer);
            } else {
                if (i6 != 1) {
                    throw new IllegalStateException();
                }
                shortenSilenceSilenceUntilNoise(byteBuffer);
            }
        }
    }

    public void setEnabled(boolean z6) {
        this.enabled = z6;
    }

    @Deprecated
    public SilenceSkippingAudioProcessor(long j4, long j6, short s6) {
        this(j4, j6 / j4, j4, 0, s6);
    }

    private int alignToBytePerFrameBoundary(float f6) {
        return alignToBytePerFrameBoundary((int) f6);
    }

    public SilenceSkippingAudioProcessor(long j4, float f6, long j6, int i6, short s6) {
        boolean z6 = false;
        this.outputSilenceFramesSinceNoise = 0;
        this.maybeSilenceBufferStartIndex = 0;
        this.maybeSilenceBufferContentsSize = 0;
        if (f6 >= 0.0f && f6 <= 1.0f) {
            z6 = true;
        }
        Assertions.checkArgument(z6);
        this.minimumSilenceDurationUs = j4;
        this.silenceRetentionRatio = f6;
        this.maxSilenceToKeepDurationUs = j6;
        this.minVolumeToKeepPercentageWhenMuting = i6;
        this.silenceThresholdLevel = s6;
        byte[] bArr = Util.EMPTY_BYTE_ARRAY;
        this.maybeSilenceBuffer = bArr;
        this.contiguousOutputBuffer = bArr;
    }
}
