package v;

import a0.C0146a;
import a0.InterfaceC0147b;
import android.media.MediaCodec;
import android.util.Log;
import b0.AbstractC0399d;
import b0.t;
import com.seewo.sdk.model.SDKKeyboardCode;
import java.io.EOFException;
import java.nio.ByteBuffer;
import java.util.concurrent.LinkedBlockingDeque;
import n.C0633A;
import n.C0639e;

/* JADX INFO: loaded from: classes.dex */
final class k {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final InterfaceC0147b f6633a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final int f6634b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final a f6635c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final LinkedBlockingDeque f6636d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final b f6637e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final t f6638f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private long f6639g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private long f6640h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private C0146a f6641i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f6642j;

    private static final class a {

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

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private int f6652j;

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

        /* JADX INFO: renamed from: l, reason: collision with root package name */
        private int f6654l;

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private int f6643a = 1000;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private long[] f6644b = new long[1000];

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private long[] f6647e = new long[1000];

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private long[] f6648f = new long[1000];

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private int[] f6646d = new int[1000];

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private int[] f6645c = new int[1000];

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private byte[][] f6649g = new byte[1000][];

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        private C0639e[] f6650h = new C0639e[1000];

        public synchronized void a() {
            this.f6652j = 0;
            this.f6653k = 0;
            this.f6654l = 0;
            this.f6651i = 0;
        }

        public synchronized void b(long j2, int i2, long j3, int i3, byte[] bArr) {
            try {
                long[] jArr = this.f6647e;
                int i4 = this.f6654l;
                jArr[i4] = j2;
                long[] jArr2 = this.f6644b;
                jArr2[i4] = j3;
                this.f6645c[i4] = i3;
                this.f6646d[i4] = i2;
                this.f6649g[i4] = bArr;
                int i5 = this.f6651i + 1;
                this.f6651i = i5;
                int i6 = this.f6643a;
                if (i5 == i6) {
                    int i7 = i6 + 1000;
                    long[] jArr3 = new long[i7];
                    long[] jArr4 = new long[i7];
                    long[] jArr5 = new long[i7];
                    int[] iArr = new int[i7];
                    int[] iArr2 = new int[i7];
                    byte[][] bArr2 = new byte[i7][];
                    int i8 = this.f6653k;
                    int i9 = i6 - i8;
                    System.arraycopy(jArr2, i8, jArr3, 0, i9);
                    System.arraycopy(this.f6647e, this.f6653k, jArr4, 0, i9);
                    System.arraycopy(this.f6648f, this.f6653k, jArr5, 0, i9);
                    System.arraycopy(this.f6646d, this.f6653k, iArr, 0, i9);
                    System.arraycopy(this.f6645c, this.f6653k, iArr2, 0, i9);
                    System.arraycopy(this.f6649g, this.f6653k, bArr2, 0, i9);
                    int i10 = this.f6653k;
                    System.arraycopy(this.f6644b, 0, jArr3, i9, i10);
                    System.arraycopy(this.f6647e, 0, jArr4, i9, i10);
                    System.arraycopy(this.f6648f, 0, jArr5, i9, i10);
                    System.arraycopy(this.f6646d, 0, iArr, i9, i10);
                    System.arraycopy(this.f6645c, 0, iArr2, i9, i10);
                    System.arraycopy(this.f6649g, 0, bArr2, i9, i10);
                    this.f6644b = jArr3;
                    this.f6647e = jArr4;
                    this.f6648f = jArr5;
                    this.f6646d = iArr;
                    this.f6645c = iArr2;
                    this.f6649g = bArr2;
                    this.f6653k = 0;
                    int i11 = this.f6643a;
                    this.f6654l = i11;
                    this.f6651i = i11;
                    this.f6643a = i7;
                } else {
                    int i12 = i4 + 1;
                    this.f6654l = i12;
                    if (i12 == i6) {
                        this.f6654l = 0;
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }

        public synchronized long c(int i2) {
            try {
                int iE = e() - i2;
                AbstractC0399d.a(iE >= 0 && iE <= this.f6651i);
                if (iE != 0) {
                    this.f6651i -= iE;
                    int i3 = this.f6654l;
                    int i4 = this.f6643a;
                    int i5 = ((i3 + i4) - iE) % i4;
                    this.f6654l = i5;
                    return this.f6644b[i5];
                }
                if (this.f6652j == 0) {
                    return 0L;
                }
                int i6 = this.f6654l;
                if (i6 == 0) {
                    i6 = this.f6643a;
                }
                int i7 = i6 - 1;
                return this.f6644b[i7] + ((long) this.f6645c[i7]);
            } catch (Throwable th) {
                throw th;
            }
        }

        public synchronized int d() {
            return this.f6652j;
        }

        public synchronized int e() {
            return this.f6652j + this.f6651i;
        }

        public synchronized long f() {
            int i2;
            int i3;
            try {
                i2 = this.f6651i - 1;
                this.f6651i = i2;
                i3 = this.f6653k;
                int i4 = i3 + 1;
                this.f6653k = i4;
                this.f6652j++;
                if (i4 == this.f6643a) {
                    this.f6653k = 0;
                }
            } catch (Throwable th) {
                throw th;
            }
            return i2 > 0 ? this.f6644b[this.f6653k] : ((long) this.f6645c[i3]) + this.f6644b[i3];
        }

        public synchronized boolean g(C0633A c0633a, b bVar) {
            C0639e c0639e;
            if (this.f6651i == 0) {
                return false;
            }
            long[] jArr = this.f6647e;
            int i2 = this.f6653k;
            c0633a.f5412e = jArr[i2];
            c0633a.f5413f = this.f6648f[i2];
            c0633a.f5410c = this.f6645c[i2];
            c0633a.f5411d = this.f6646d[i2];
            bVar.f6655a = this.f6644b[i2];
            bVar.f6656b = this.f6649g[i2];
            if (c0633a.g() && (c0639e = this.f6650h[this.f6653k]) != null) {
                c0633a.f5408a.c(c0639e.f5515f, c0639e.f5513d, c0639e.f5514e, c0639e.f5511b, c0639e.f5510a, c0639e.f5512c);
                MediaCodec.CryptoInfo cryptoInfoA = c0633a.f5408a.a();
                C0639e c0639e2 = this.f6650h[this.f6653k];
                cryptoInfoA.setPattern(new MediaCodec.CryptoInfo.Pattern(c0639e2.f5516g, c0639e2.f5517h));
            }
            return true;
        }

        public synchronized long h(long j2) {
            try {
                if (this.f6651i != 0) {
                    long[] jArr = this.f6647e;
                    int i2 = this.f6653k;
                    if (j2 >= jArr[i2]) {
                        int i3 = this.f6654l;
                        if (i3 == 0) {
                            i3 = this.f6643a;
                        }
                        if (j2 > jArr[i3 - 1]) {
                            return -1L;
                        }
                        int i4 = 0;
                        int i5 = -1;
                        while (i2 != this.f6654l && this.f6647e[i2] <= j2) {
                            if ((this.f6646d[i2] & 1) != 0) {
                                i5 = i4;
                            }
                            i2 = (i2 + 1) % this.f6643a;
                            i4++;
                        }
                        if (i5 == -1) {
                            return -1L;
                        }
                        this.f6651i -= i5;
                        int i6 = (this.f6653k + i5) % this.f6643a;
                        this.f6653k = i6;
                        this.f6652j += i5;
                        return this.f6644b[i6];
                    }
                }
                return -1L;
            } finally {
            }
        }
    }

    private static final class b {

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

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

        private b() {
        }
    }

    public k(InterfaceC0147b interfaceC0147b) {
        this.f6633a = interfaceC0147b;
        int iG = interfaceC0147b.g();
        this.f6634b = iG;
        this.f6635c = new a();
        this.f6636d = new LinkedBlockingDeque();
        this.f6637e = new b();
        this.f6638f = new t(32);
        this.f6642j = iG;
    }

    private void g(long j2) {
        int i2 = ((int) (j2 - this.f6639g)) / this.f6634b;
        for (int i3 = 0; i3 < i2; i3++) {
            this.f6633a.e((C0146a) this.f6636d.remove());
            this.f6639g += (long) this.f6634b;
        }
    }

    private void h(long j2) {
        int i2 = (int) (j2 - this.f6639g);
        int i3 = this.f6634b;
        int i4 = i2 / i3;
        int i5 = i2 % i3;
        int size = this.f6636d.size() - i4;
        int i6 = size - 1;
        if (i5 != 0) {
            size = i6;
        }
        for (int i7 = 0; i7 < size; i7++) {
            this.f6633a.e((C0146a) this.f6636d.removeLast());
        }
        this.f6641i = (C0146a) this.f6636d.peekLast();
        if (i5 == 0) {
            i5 = this.f6634b;
        }
        this.f6642j = i5;
    }

    private static void i(t tVar, int i2) {
        if (tVar.d() < i2) {
            tVar.z(new byte[i2], i2);
        }
    }

    private int n(int i2) {
        if (this.f6642j == this.f6634b) {
            this.f6642j = 0;
            C0146a c0146aC = this.f6633a.c();
            this.f6641i = c0146aC;
            this.f6636d.add(c0146aC);
        }
        return Math.min(i2, this.f6634b - this.f6642j);
    }

    private void o(long j2, ByteBuffer byteBuffer, int i2) {
        while (i2 > 0) {
            g(j2);
            int i3 = (int) (j2 - this.f6639g);
            int iMin = Math.min(i2, this.f6634b - i3);
            C0146a c0146a = (C0146a) this.f6636d.peek();
            if (c0146a != null) {
                byteBuffer.put(c0146a.f1920a, c0146a.a(i3), iMin);
                j2 += (long) iMin;
                i2 -= iMin;
            } else {
                Log.e("RollingSampleBuffer", "readData: dataQueue is null!");
            }
        }
    }

    private void p(long j2, byte[] bArr, int i2) {
        int i3 = 0;
        while (i3 < i2) {
            g(j2);
            int i4 = (int) (j2 - this.f6639g);
            int iMin = Math.min(i2 - i3, this.f6634b - i4);
            C0146a c0146a = (C0146a) this.f6636d.peek();
            if (c0146a != null) {
                System.arraycopy(c0146a.f1920a, c0146a.a(i4), bArr, i3, iMin);
                j2 += (long) iMin;
                i3 += iMin;
            } else {
                Log.e("RollingSampleBuffer", "readData: dataQueue is null!");
            }
        }
    }

    private void q(C0633A c0633a, b bVar) {
        long j2 = bVar.f6655a;
        int iW = 1;
        p(j2, this.f6638f.f3092a, 1);
        long j3 = j2 + 1;
        byte b2 = this.f6638f.f3092a[0];
        boolean z2 = (b2 & SDKKeyboardCode.FUNCTION_KEY_BOARD_VOLUME_DOWN) != 0;
        int i2 = b2 & SDKKeyboardCode.FUNCTION_KEY_BOARD_VOLUME_UP;
        C0639e c0639e = c0633a.f5408a;
        if (c0639e.f5510a == null) {
            c0639e.f5510a = new byte[16];
        }
        p(j3, c0639e.f5510a, i2);
        long j4 = j3 + ((long) i2);
        if (z2) {
            p(j4, this.f6638f.f3092a, 2);
            j4 += 2;
            this.f6638f.B(0);
            iW = this.f6638f.w();
        }
        int i3 = iW;
        C0639e c0639e2 = c0633a.f5408a;
        int[] iArr = c0639e2.f5513d;
        if (iArr == null || iArr.length < i3) {
            iArr = new int[i3];
        }
        int[] iArr2 = iArr;
        int[] iArr3 = c0639e2.f5514e;
        if (iArr3 == null || iArr3.length < i3) {
            iArr3 = new int[i3];
        }
        int[] iArr4 = iArr3;
        if (z2) {
            int i4 = i3 * 6;
            i(this.f6638f, i4);
            p(j4, this.f6638f.f3092a, i4);
            j4 += (long) i4;
            this.f6638f.B(0);
            for (int i5 = 0; i5 < i3; i5++) {
                iArr2[i5] = this.f6638f.w();
                iArr4[i5] = this.f6638f.u();
            }
        } else {
            iArr2[0] = 0;
            iArr4[0] = c0633a.f5410c - ((int) (j4 - bVar.f6655a));
        }
        C0639e c0639e3 = c0633a.f5408a;
        c0639e3.c(i3, iArr2, iArr4, bVar.f6656b, c0639e3.f5510a, 1);
        long j5 = bVar.f6655a;
        int i6 = (int) (j4 - j5);
        bVar.f6655a = j5 + ((long) i6);
        c0633a.f5410c -= i6;
    }

    public int a(a0.f fVar, int i2, boolean z2) throws EOFException {
        int iN = n(i2);
        C0146a c0146a = this.f6641i;
        int i3 = fVar.read(c0146a.f1920a, c0146a.a(this.f6642j), iN);
        if (i3 == -1) {
            if (z2) {
                return -1;
            }
            throw new EOFException();
        }
        this.f6642j += i3;
        this.f6640h += (long) i3;
        return i3;
    }

    public int b(f fVar, int i2, boolean z2) throws EOFException {
        int iN = n(i2);
        C0146a c0146a = this.f6641i;
        int i3 = fVar.read(c0146a.f1920a, c0146a.a(this.f6642j), iN);
        if (i3 == -1) {
            if (z2) {
                return -1;
            }
            throw new EOFException();
        }
        this.f6642j += i3;
        this.f6640h += (long) i3;
        return i3;
    }

    public void c(t tVar, int i2) {
        while (i2 > 0) {
            int iN = n(i2);
            C0146a c0146a = this.f6641i;
            tVar.e(c0146a.f1920a, c0146a.a(this.f6642j), iN);
            this.f6642j += iN;
            this.f6640h += (long) iN;
            i2 -= iN;
        }
    }

    public void d() {
        this.f6635c.a();
        InterfaceC0147b interfaceC0147b = this.f6633a;
        LinkedBlockingDeque linkedBlockingDeque = this.f6636d;
        interfaceC0147b.b((C0146a[]) linkedBlockingDeque.toArray(new C0146a[linkedBlockingDeque.size()]));
        this.f6636d.clear();
        this.f6639g = 0L;
        this.f6640h = 0L;
        this.f6641i = null;
        this.f6642j = this.f6634b;
    }

    public void e(long j2, int i2, long j3, int i3, byte[] bArr) {
        this.f6635c.b(j2, i2, j3, i3, bArr);
    }

    public void f(int i2) {
        long jC = this.f6635c.c(i2);
        this.f6640h = jC;
        h(jC);
    }

    public int j() {
        return this.f6635c.d();
    }

    public int k() {
        return this.f6635c.e();
    }

    public long l() {
        return this.f6640h;
    }

    public boolean m(C0633A c0633a) {
        return this.f6635c.g(c0633a, this.f6637e);
    }

    public boolean r(C0633A c0633a) {
        if (!this.f6635c.g(c0633a, this.f6637e)) {
            return false;
        }
        if (c0633a.f()) {
            q(c0633a, this.f6637e);
        }
        c0633a.c(c0633a.f5410c);
        o(this.f6637e.f6655a, c0633a.f5409b, c0633a.f5410c);
        g(this.f6635c.f());
        return true;
    }

    public void s() {
        g(this.f6635c.f());
    }

    public boolean t(long j2) {
        long jH = this.f6635c.h(j2);
        if (jH == -1) {
            return false;
        }
        g(jH);
        return true;
    }
}
