package androidx.media3.extractor;

import androidx.media3.common.util.Assertions;
import com.google.android.gms.internal.ads.bb1;
import com.google.android.gms.internal.ads.ih1;
import com.google.android.gms.internal.ads.p01;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f3326a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Object f3327b;

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

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

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

    public b1(byte[] bArr, int i10) {
        this.f3326a = 2;
        this.f3327b = bArr;
        this.f3330e = i10;
    }

    public final void a(Object obj) {
        Object[] objArr = (Object[]) this.f3327b;
        int i10 = this.f3329d;
        objArr[i10] = obj;
        int i11 = this.f3330e & (i10 + 1);
        this.f3329d = i11;
        int i12 = this.f3328c;
        if (i11 == i12) {
            int length = objArr.length;
            int i13 = length - i12;
            int i14 = length << 1;
            if (i14 < 0) {
                throw new RuntimeException("Max array capacity exceeded");
            }
            Object[] objArr2 = new Object[i14];
            kotlin.collections.q.R(objArr, 0, objArr2, i12, length);
            kotlin.collections.q.R((Object[]) this.f3327b, i13, objArr2, 0, this.f3328c);
            this.f3327b = objArr2;
            this.f3328c = 0;
            this.f3329d = length;
            this.f3330e = i14 - 1;
        }
    }

    public final boolean b() {
        boolean z10 = (((((byte[]) this.f3327b)[this.f3329d] & 255) >> this.f3330e) & 1) == 1;
        d(1);
        return z10;
    }

    public final int c(int i10) {
        int i11 = this.f3329d;
        int iMin = Math.min(i10, 8 - this.f3330e);
        int i12 = i11 + 1;
        int i13 = ((((byte[]) this.f3327b)[i11] & 255) >> this.f3330e) & (255 >> (8 - iMin));
        while (iMin < i10) {
            i13 |= (((byte[]) this.f3327b)[i12] & 255) << iMin;
            iMin += 8;
            i12++;
        }
        int i14 = i13 & ((-1) >>> (32 - i10));
        d(i10);
        return i14;
    }

    public final void d(int i10) {
        int i11;
        int i12 = i10 / 8;
        int i13 = this.f3329d + i12;
        this.f3329d = i13;
        int i14 = (i10 - (i12 * 8)) + this.f3330e;
        this.f3330e = i14;
        boolean z10 = true;
        if (i14 > 7) {
            this.f3329d = i13 + 1;
            this.f3330e = i14 - 8;
        }
        int i15 = this.f3329d;
        if (i15 < 0 || (i15 >= (i11 = this.f3328c) && (i15 != i11 || this.f3330e != 0))) {
            z10 = false;
        }
        Assertions.checkState(z10);
    }

    public final void e(p01 p01Var) {
        byte[] bArr = p01Var.f15225a;
        int i10 = p01Var.f15227c;
        this.f3327b = bArr;
        this.f3328c = 0;
        this.f3329d = 0;
        this.f3330e = i10;
        l(p01Var.f15226b * 8);
    }

    public final boolean f() {
        int i10 = (((byte[]) this.f3327b)[this.f3329d] & 255) >> this.f3330e;
        i(1);
        return 1 == (i10 & 1);
    }

    public final int g(int i10) {
        int i11 = this.f3329d;
        int i12 = 8 - this.f3330e;
        int i13 = i11 + 1;
        byte[] bArr = (byte[]) this.f3327b;
        int iMin = Math.min(i10, i12);
        int i14 = ((bArr[i11] & 255) >> this.f3330e) & (255 >> (8 - iMin));
        while (iMin < i10) {
            i14 |= (bArr[i13] & 255) << iMin;
            iMin += 8;
            i13++;
        }
        int i15 = i14 & ((-1) >>> (32 - i10));
        i(i10);
        return i15;
    }

    public final int h() {
        return ((this.f3330e - this.f3328c) * 8) - this.f3329d;
    }

    public final void i(int i10) {
        int i11;
        int i12 = i10 / 8;
        int i13 = this.f3329d + i12;
        this.f3329d = i13;
        int i14 = (i10 - (i12 * 8)) + this.f3330e;
        this.f3330e = i14;
        if (i14 > 7) {
            i13++;
            this.f3329d = i13;
            i14 -= 8;
            this.f3330e = i14;
        }
        boolean z10 = false;
        if (i13 >= 0 && (i13 < (i11 = this.f3328c) || (i13 == i11 && i14 == 0))) {
            z10 = true;
        }
        ih1.b1(z10);
    }

    public final int j() {
        switch (this.f3326a) {
            case 1:
                return (this.f3329d * 8) + this.f3330e;
            default:
                return (this.f3328c * 8) + this.f3329d;
        }
    }

    public final int k() {
        ih1.b1(this.f3329d == 0);
        return this.f3328c;
    }

    public final void l(int i10) {
        int i11 = i10 / 8;
        this.f3328c = i11;
        this.f3329d = i10 - (i11 * 8);
        v();
    }

    public final void m() {
        int i10 = this.f3329d + 1;
        this.f3329d = i10;
        if (i10 == 8) {
            this.f3329d = 0;
            this.f3328c++;
        }
        v();
    }

    public final void n(int i10) {
        int i11 = i10 / 8;
        int i12 = this.f3328c + i11;
        this.f3328c = i12;
        int i13 = (i10 - (i11 * 8)) + this.f3329d;
        this.f3329d = i13;
        if (i13 > 7) {
            this.f3328c = i12 + 1;
            this.f3329d = i13 - 8;
        }
        v();
    }

    public final boolean o() {
        int i10 = ((byte[]) this.f3327b)[this.f3328c] & (128 >> this.f3329d);
        m();
        return i10 != 0;
    }

    public final int p(int i10) {
        int i11;
        if (i10 == 0) {
            return 0;
        }
        this.f3329d += i10;
        int i12 = 0;
        while (true) {
            i11 = this.f3329d;
            if (i11 <= 8) {
                break;
            }
            int i13 = i11 - 8;
            this.f3329d = i13;
            byte[] bArr = (byte[]) this.f3327b;
            int i14 = this.f3328c;
            this.f3328c = i14 + 1;
            i12 |= (bArr[i14] & 255) << i13;
        }
        byte[] bArr2 = (byte[]) this.f3327b;
        int i15 = this.f3328c;
        int i16 = i12 | ((bArr2[i15] & 255) >> (8 - i11));
        int i17 = 32 - i10;
        if (i11 == 8) {
            this.f3329d = 0;
            this.f3328c = i15 + 1;
        }
        int i18 = ((-1) >>> i17) & i16;
        v();
        return i18;
    }

    public final long q(int i10) {
        if (i10 <= 32) {
            int iP = p(i10);
            String str = bb1.f9474a;
            return 4294967295L & ((long) iP);
        }
        int iP2 = p(i10 - 32);
        int iP3 = p(32);
        String str2 = bb1.f9474a;
        return (4294967295L & ((long) iP3)) | ((((long) iP2) & 4294967295L) << 32);
    }

    public final void r(byte[] bArr, int i10) {
        int i11;
        int i12 = 0;
        while (true) {
            i11 = i10 >> 3;
            if (i12 >= i11) {
                break;
            }
            byte[] bArr2 = (byte[]) this.f3327b;
            int i13 = this.f3328c;
            int i14 = i13 + 1;
            this.f3328c = i14;
            byte b10 = bArr2[i13];
            int i15 = this.f3329d;
            byte b11 = (byte) (b10 << i15);
            bArr[i12] = b11;
            bArr[i12] = (byte) (((bArr2[i14] & 255) >> (8 - i15)) | b11);
            i12++;
        }
        int i16 = i10 & 7;
        if (i16 == 0) {
            return;
        }
        byte b12 = (byte) (bArr[i11] & (255 >> i16));
        bArr[i11] = b12;
        int i17 = this.f3329d;
        if (i17 + i16 > 8) {
            byte[] bArr3 = (byte[]) this.f3327b;
            int i18 = this.f3328c;
            this.f3328c = i18 + 1;
            b12 = (byte) (b12 | ((bArr3[i18] & 255) << i17));
            bArr[i11] = b12;
            i17 -= 8;
        }
        int i19 = i17 + i16;
        this.f3329d = i19;
        byte[] bArr4 = (byte[]) this.f3327b;
        int i20 = this.f3328c;
        bArr[i11] = (byte) (((byte) (((255 & bArr4[i20]) >> (8 - i19)) << (8 - i16))) | b12);
        if (i19 == 8) {
            this.f3329d = 0;
            this.f3328c = i20 + 1;
        }
        v();
    }

    public final void s() {
        if (this.f3329d == 0) {
            return;
        }
        this.f3329d = 0;
        this.f3328c++;
        v();
    }

    public final void t(int i10, byte[] bArr) {
        ih1.b1(this.f3329d == 0);
        System.arraycopy((byte[]) this.f3327b, this.f3328c, bArr, 0, i10);
        this.f3328c += i10;
        v();
    }

    public final void u(int i10) {
        ih1.b1(this.f3329d == 0);
        this.f3328c += i10;
        v();
    }

    public final void v() {
        int i10;
        int i11 = this.f3328c;
        boolean z10 = false;
        if (i11 >= 0 && (i11 < (i10 = this.f3330e) || (i11 == i10 && this.f3329d == 0))) {
            z10 = true;
        }
        ih1.b1(z10);
    }

    public b1() {
        this.f3326a = 2;
        this.f3327b = bb1.f9475b;
    }

    public b1(int i10) {
        this.f3326a = 3;
        if (i10 < 1) {
            throw new IllegalArgumentException("capacity must be >= 1".toString());
        }
        if (i10 <= 1073741824) {
            i10 = Integer.bitCount(i10) != 1 ? Integer.highestOneBit(i10 - 1) << 1 : i10;
            this.f3330e = i10 - 1;
            this.f3327b = new Object[i10];
            return;
        }
        throw new IllegalArgumentException("capacity must be <= 2^30".toString());
    }

    public b1(byte[] bArr, int i10, int i11) {
        this.f3326a = i10;
        if (i10 != 1) {
            this.f3327b = bArr;
            this.f3328c = bArr.length;
        } else {
            this.f3327b = bArr;
            this.f3328c = bArr.length;
        }
    }
}
