package D0;

import java.nio.ByteBuffer;
import v0.AbstractC1669h;
import v0.C1666e;
import v0.C1667f;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class J extends AbstractC1669h {

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

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

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

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

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

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public int f1522r = 0;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public int f1524t = 0;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public int f1525u = 0;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final long f1516l = 100000;
    public final float i = 0.2f;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final long f1517m = 2000000;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final int f1515k = 10;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final short f1514j = 1024;

    public J() {
        byte[] bArr = x0.v.f18555c;
        this.f1523s = bArr;
        this.f1526v = bArr;
    }

    @Override // v0.AbstractC1669h, v0.InterfaceC1668g
    public final boolean a() {
        return super.a() && this.f1519o;
    }

    @Override // v0.InterfaceC1668g
    public final void e(ByteBuffer byteBuffer) {
        int iLimit;
        int iPosition;
        while (byteBuffer.hasRemaining() && !this.f18026g.hasRemaining()) {
            int i = this.f1520p;
            short s8 = this.f1514j;
            if (i == 0) {
                int iLimit2 = byteBuffer.limit();
                byteBuffer.limit(Math.min(iLimit2, byteBuffer.position() + this.f1523s.length));
                int iLimit3 = byteBuffer.limit() - 1;
                while (true) {
                    if (iLimit3 < byteBuffer.position()) {
                        iPosition = byteBuffer.position();
                        break;
                    }
                    if (Math.abs((byteBuffer.get(iLimit3) << 8) | (byteBuffer.get(iLimit3 - 1) & 255)) > s8) {
                        int i5 = this.f1518n;
                        iPosition = ((iLimit3 / i5) * i5) + i5;
                        break;
                    }
                    iLimit3 -= 2;
                }
                if (iPosition == byteBuffer.position()) {
                    this.f1520p = 1;
                } else {
                    byteBuffer.limit(Math.min(iPosition, byteBuffer.capacity()));
                    k(byteBuffer.remaining()).put(byteBuffer).flip();
                }
                byteBuffer.limit(iLimit2);
            } else {
                if (i != 1) {
                    throw new IllegalStateException();
                }
                AbstractC1709a.j(this.f1524t < this.f1523s.length);
                int iLimit4 = 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)) > s8) {
                        int i8 = this.f1518n;
                        iLimit = (iPosition2 / i8) * i8;
                        break;
                    }
                    iPosition2 += 2;
                }
                int iPosition3 = iLimit - byteBuffer.position();
                int length = this.f1524t;
                int i9 = this.f1525u;
                int length2 = length + i9;
                byte[] bArr = this.f1523s;
                if (length2 < bArr.length) {
                    length = bArr.length;
                } else {
                    length2 = i9 - (bArr.length - length);
                }
                int i10 = length - length2;
                boolean z8 = iLimit < iLimit4;
                int iMin = Math.min(iPosition3, i10);
                byteBuffer.limit(byteBuffer.position() + iMin);
                byteBuffer.get(this.f1523s, length2, iMin);
                int i11 = this.f1525u + iMin;
                this.f1525u = i11;
                AbstractC1709a.j(i11 <= this.f1523s.length);
                boolean z9 = z8 && iPosition3 < i10;
                m(z9);
                if (z9) {
                    this.f1520p = 0;
                    this.f1522r = 0;
                }
                byteBuffer.limit(iLimit4);
            }
        }
    }

    @Override // v0.AbstractC1669h
    public final C1666e g(C1666e c1666e) throws C1667f {
        if (c1666e.f18018c == 2) {
            return c1666e.f18016a == -1 ? C1666e.f18015e : c1666e;
        }
        throw new C1667f(c1666e);
    }

    @Override // v0.AbstractC1669h
    public final void h() {
        if (a()) {
            C1666e c1666e = this.f18021b;
            int i = c1666e.f18017b * 2;
            this.f1518n = i;
            int i5 = ((((int) ((this.f1516l * ((long) c1666e.f18016a)) / 1000000)) / 2) / i) * i * 2;
            if (this.f1523s.length != i5) {
                this.f1523s = new byte[i5];
                this.f1526v = new byte[i5];
            }
        }
        this.f1520p = 0;
        this.f1521q = 0L;
        this.f1522r = 0;
        this.f1524t = 0;
        this.f1525u = 0;
    }

    @Override // v0.AbstractC1669h
    public final void i() {
        if (this.f1525u > 0) {
            m(true);
            this.f1522r = 0;
        }
    }

    @Override // v0.AbstractC1669h
    public final void j() {
        this.f1519o = false;
        byte[] bArr = x0.v.f18555c;
        this.f1523s = bArr;
        this.f1526v = bArr;
    }

    public final int l(int i) {
        int length = ((((int) ((this.f1517m * ((long) this.f18021b.f18016a)) / 1000000)) - this.f1522r) * this.f1518n) - (this.f1523s.length / 2);
        AbstractC1709a.j(length >= 0);
        int iMin = (int) Math.min((i * this.i) + 0.5f, length);
        int i5 = this.f1518n;
        return (iMin / i5) * i5;
    }

    public final void m(boolean z8) {
        int length;
        int iL;
        int i = this.f1525u;
        byte[] bArr = this.f1523s;
        if (i == bArr.length || z8) {
            if (this.f1522r == 0) {
                if (z8) {
                    n(i, 3);
                    length = i;
                } else {
                    AbstractC1709a.j(i >= bArr.length / 2);
                    length = this.f1523s.length / 2;
                    n(length, 0);
                }
                iL = length;
            } else if (z8) {
                int length2 = i - (bArr.length / 2);
                int length3 = (bArr.length / 2) + length2;
                int iL2 = l(length2) + (this.f1523s.length / 2);
                n(iL2, 2);
                iL = iL2;
                length = length3;
            } else {
                length = i - (bArr.length / 2);
                iL = l(length);
                n(iL, 1);
            }
            AbstractC1709a.i("bytesConsumed is not aligned to frame size: %s" + length, length % this.f1518n == 0);
            AbstractC1709a.j(i >= iL);
            this.f1525u -= length;
            int i5 = this.f1524t + length;
            this.f1524t = i5;
            this.f1524t = i5 % this.f1523s.length;
            int i8 = this.f1522r;
            int i9 = this.f1518n;
            this.f1522r = (iL / i9) + i8;
            this.f1521q += (long) ((length - iL) / i9);
        }
    }

    public final void n(int i, int i5) {
        if (i == 0) {
            return;
        }
        AbstractC1709a.d(this.f1525u >= i);
        if (i5 == 2) {
            int i8 = this.f1524t;
            int i9 = this.f1525u;
            int i10 = i8 + i9;
            byte[] bArr = this.f1523s;
            if (i10 <= bArr.length) {
                System.arraycopy(bArr, i10 - i, this.f1526v, 0, i);
            } else {
                int length = i9 - (bArr.length - i8);
                if (length >= i) {
                    System.arraycopy(bArr, length - i, this.f1526v, 0, i);
                } else {
                    int i11 = i - length;
                    System.arraycopy(bArr, bArr.length - i11, this.f1526v, 0, i11);
                    System.arraycopy(this.f1523s, 0, this.f1526v, i11, length);
                }
            }
        } else {
            int i12 = this.f1524t;
            int i13 = i12 + i;
            byte[] bArr2 = this.f1523s;
            if (i13 <= bArr2.length) {
                System.arraycopy(bArr2, i12, this.f1526v, 0, i);
            } else {
                int length2 = bArr2.length - i12;
                System.arraycopy(bArr2, i12, this.f1526v, 0, length2);
                System.arraycopy(this.f1523s, 0, this.f1526v, length2, i - length2);
            }
        }
        AbstractC1709a.c("sizeToOutput is not aligned to frame size: " + i, i % this.f1518n == 0);
        AbstractC1709a.j(this.f1524t < this.f1523s.length);
        byte[] bArr3 = this.f1526v;
        AbstractC1709a.c("byteOutput size is not aligned to frame size " + i, i % this.f1518n == 0);
        if (i5 != 3) {
            for (int i14 = 0; i14 < i; i14 += 2) {
                int i15 = i14 + 1;
                int i16 = (bArr3[i15] << 8) | (bArr3[i14] & 255);
                int i17 = this.f1515k;
                if (i5 == 0) {
                    i17 = ((((i14 * 1000) / (i - 1)) * (i17 - 100)) / 1000) + 100;
                } else if (i5 == 2) {
                    i17 += (((i14 * 1000) * (100 - i17)) / (i - 1)) / 1000;
                }
                int i18 = (i16 * i17) / 100;
                if (i18 >= 32767) {
                    bArr3[i14] = -1;
                    bArr3[i15] = 127;
                } else if (i18 <= -32768) {
                    bArr3[i14] = 0;
                    bArr3[i15] = -128;
                } else {
                    bArr3[i14] = (byte) (i18 & 255);
                    bArr3[i15] = (byte) (i18 >> 8);
                }
            }
        }
        k(i).put(bArr3, 0, i).flip();
    }
}
