package com.google.android.gms.internal.ads;

import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes2.dex */
public final class vm2 extends y80 {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public int f17707i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f17708j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f17709k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public long f17710l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f17711m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public byte[] f17712n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public int f17713o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public int f17714p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public byte[] f17715q;

    @Override // com.google.android.gms.internal.ads.i80
    public final void b(ByteBuffer byteBuffer) {
        int iLimit;
        int i10;
        int iPosition;
        while (byteBuffer.hasRemaining() && !this.f18753g.hasRemaining()) {
            if (this.f17709k != 0) {
                ih1.b1(this.f17713o < this.f17712n.length);
                int iLimit2 = byteBuffer.limit();
                int iPosition2 = byteBuffer.position() + 1;
                while (true) {
                    if (iPosition2 >= byteBuffer.limit()) {
                        iLimit = byteBuffer.limit();
                        break;
                    }
                    if (Math.abs((byteBuffer.get(iPosition2) << 8) | (byteBuffer.get(iPosition2 - 1) & 255)) > 1024) {
                        int i11 = this.f17707i;
                        iLimit = (iPosition2 / i11) * i11;
                        break;
                    }
                    iPosition2 += 2;
                }
                int iPosition3 = iLimit - byteBuffer.position();
                int i12 = this.f17713o;
                int i13 = this.f17714p;
                int i14 = i12 + i13;
                int length = this.f17712n.length;
                if (i14 < length) {
                    i10 = length - i14;
                } else {
                    i14 = i13 - (length - i12);
                    i10 = i12 - i14;
                }
                int iMin = Math.min(iPosition3, i10);
                byteBuffer.limit(byteBuffer.position() + iMin);
                byteBuffer.get(this.f17712n, i14, iMin);
                int i15 = this.f17714p + iMin;
                this.f17714p = i15;
                ih1.b1(i15 <= this.f17712n.length);
                boolean z10 = iLimit < iLimit2 && iPosition3 < i10;
                i(z10);
                if (z10) {
                    this.f17709k = 0;
                    this.f17711m = 0;
                }
                byteBuffer.limit(iLimit2);
            } else {
                int iLimit3 = byteBuffer.limit();
                byteBuffer.limit(Math.min(iLimit3, byteBuffer.position() + this.f17712n.length));
                int iLimit4 = byteBuffer.limit() - 1;
                while (true) {
                    if (iLimit4 < byteBuffer.position()) {
                        iPosition = byteBuffer.position();
                        break;
                    }
                    if (Math.abs((byteBuffer.get(iLimit4) << 8) | (byteBuffer.get(iLimit4 - 1) & 255)) > 1024) {
                        int i16 = this.f17707i;
                        iPosition = ((iLimit4 / i16) * i16) + i16;
                        break;
                    }
                    iLimit4 -= 2;
                }
                if (iPosition == byteBuffer.position()) {
                    this.f17709k = 1;
                } else {
                    byteBuffer.limit(Math.min(iPosition, byteBuffer.capacity()));
                    d(byteBuffer.remaining()).put(byteBuffer).flip();
                }
                byteBuffer.limit(iLimit3);
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.y80
    public final s60 e(s60 s60Var) throws zzcn {
        if (s60Var.f16309c == 2) {
            return s60Var.f16307a == -1 ? s60.f16306e : s60Var;
        }
        throw new zzcn("Unhandled input format:", s60Var);
    }

    @Override // com.google.android.gms.internal.ads.y80
    public final void f() {
        if (this.f17714p > 0) {
            i(true);
            this.f17711m = 0;
        }
    }

    @Override // com.google.android.gms.internal.ads.y80
    public final void g() {
        if (zzc()) {
            s60 s60Var = this.f18748b;
            int i10 = s60Var.f16308b;
            int i11 = i10 + i10;
            this.f17707i = i11;
            int i12 = ((((int) ((SilenceSkippingAudioProcessor.DEFAULT_MINIMUM_SILENCE_DURATION_US * ((long) s60Var.f16307a)) / 1000000)) / 2) / i11) * i11;
            int i13 = i12 + i12;
            if (this.f17712n.length != i13) {
                this.f17712n = new byte[i13];
                this.f17715q = new byte[i13];
            }
        }
        this.f17709k = 0;
        this.f17710l = 0L;
        this.f17711m = 0;
        this.f17713o = 0;
        this.f17714p = 0;
    }

    @Override // com.google.android.gms.internal.ads.y80
    public final void h() {
        this.f17708j = false;
        byte[] bArr = bb1.f9475b;
        this.f17712n = bArr;
        this.f17715q = bArr;
    }

    public final void i(boolean z10) {
        int i10;
        int length;
        int i11 = this.f17714p;
        int length2 = this.f17712n.length;
        if (i11 != length2) {
            if (!z10) {
                return;
            } else {
                z10 = true;
            }
        }
        if (this.f17711m == 0) {
            if (z10) {
                k(i11, 3);
                length = i11;
            } else {
                ih1.b1(i11 >= (length2 >> 1));
                length = this.f17712n.length >> 1;
                k(length, 0);
            }
            i10 = length;
        } else {
            int i12 = length2 >> 1;
            int i13 = i11 - i12;
            if (z10) {
                int iJ = j(i13) + (this.f17712n.length >> 1);
                k(iJ, 2);
                int i14 = i12 + i13;
                i10 = iJ;
                length = i14;
            } else {
                int iJ2 = j(i13);
                k(iJ2, 1);
                i10 = iJ2;
                length = i13;
            }
        }
        if (length % this.f17707i != 0) {
            throw new IllegalStateException(ih1.B0("bytesConsumed is not aligned to frame size: %s", Integer.valueOf(length)));
        }
        ih1.b1(i11 >= i10);
        this.f17714p -= length;
        int i15 = this.f17713o + length;
        this.f17713o = i15;
        this.f17713o = i15 % this.f17712n.length;
        int i16 = this.f17711m;
        int i17 = this.f17707i;
        this.f17711m = (i10 / i17) + i16;
        this.f17710l += (long) ((length - i10) / i17);
    }

    public final int j(int i10) {
        int length = ((((int) ((SilenceSkippingAudioProcessor.DEFAULT_MAX_SILENCE_TO_KEEP_DURATION_US * ((long) this.f18748b.f16307a)) / 1000000)) - this.f17711m) * this.f17707i) - (this.f17712n.length >> 1);
        ih1.b1(length >= 0);
        int iMin = (int) Math.min((i10 * 0.2f) + 0.5f, length);
        int i11 = this.f17707i;
        return (iMin / i11) * i11;
    }

    public final void k(int i10, int i11) {
        int i12;
        if (i10 == 0) {
            return;
        }
        ih1.x(this.f17714p >= i10);
        if (i11 == 2) {
            int i13 = this.f17713o;
            int i14 = this.f17714p;
            int i15 = i13 + i14;
            byte[] bArr = this.f17712n;
            int length = bArr.length;
            if (i15 <= length) {
                System.arraycopy(bArr, i15 - i10, this.f17715q, 0, i10);
            } else {
                int i16 = i14 - (length - i13);
                if (i16 >= i10) {
                    System.arraycopy(bArr, i16 - i10, this.f17715q, 0, i10);
                } else {
                    int i17 = i10 - i16;
                    System.arraycopy(bArr, length - i17, this.f17715q, 0, i17);
                    System.arraycopy(this.f17712n, 0, this.f17715q, i17, i16);
                }
            }
        } else {
            int i18 = this.f17713o;
            int i19 = i18 + i10;
            byte[] bArr2 = this.f17712n;
            int length2 = bArr2.length;
            if (i19 <= length2) {
                System.arraycopy(bArr2, i18, this.f17715q, 0, i10);
            } else {
                int i20 = length2 - i18;
                System.arraycopy(bArr2, i18, this.f17715q, 0, i20);
                System.arraycopy(this.f17712n, 0, this.f17715q, i20, i10 - i20);
            }
        }
        ih1.C0(i10, "sizeToOutput is not aligned to frame size: %s", i10 % this.f17707i == 0);
        ih1.b1(this.f17713o < this.f17712n.length);
        byte[] bArr3 = this.f17715q;
        ih1.C0(i10, "byteOutput size is not aligned to frame size %s", i10 % this.f17707i == 0);
        if (i11 != 3) {
            for (int i21 = 0; i21 < i10; i21 += 2) {
                int i22 = i21 + 1;
                int i23 = (bArr3[i22] << 8) | (bArr3[i21] & 255);
                if (i11 == 0) {
                    i12 = ((((i21 * 1000) / (i10 - 1)) * (-90)) / 1000) + 100;
                } else {
                    i12 = 10;
                    if (i11 == 2) {
                        i12 = 10 + (((90000 * i21) / (i10 - 1)) / 1000);
                    }
                }
                int i24 = (i23 * i12) / 100;
                if (i24 >= 32767) {
                    bArr3[i21] = -1;
                    bArr3[i22] = 127;
                } else if (i24 <= -32768) {
                    bArr3[i21] = 0;
                    bArr3[i22] = -128;
                } else {
                    bArr3[i21] = (byte) (i24 & 255);
                    bArr3[i22] = (byte) (i24 >> 8);
                }
            }
        }
        d(i10).put(bArr3, 0, i10).flip();
    }

    @Override // com.google.android.gms.internal.ads.y80, com.google.android.gms.internal.ads.i80
    public final boolean zzc() {
        return super.zzc() && this.f17708j;
    }
}
