package q1;

import androidx.media3.common.util.Assertions;

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

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

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

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

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

    public x(byte[] bArr, int i10, int i11) {
        this.f32170a = bArr;
        this.f32172c = i10;
        this.f32171b = i11;
        a();
    }

    public final void a() {
        int i10;
        int i11 = this.f32172c;
        Assertions.checkState(i11 >= 0 && (i11 < (i10 = this.f32171b) || (i11 == i10 && this.f32173d == 0)));
    }

    public final void b() {
        int i10 = this.f32173d;
        if (i10 > 0) {
            l(8 - i10);
        }
    }

    public final boolean c(int i10) {
        int i11 = this.f32172c;
        int i12 = i10 / 8;
        int i13 = i11 + i12;
        int i14 = (this.f32173d + i10) - (i12 * 8);
        if (i14 > 7) {
            i13++;
            i14 -= 8;
        }
        while (true) {
            i11++;
            if (i11 > i13 || i13 >= this.f32171b) {
                break;
            }
            if (j(i11)) {
                i13++;
                i11 += 2;
            }
        }
        int i15 = this.f32171b;
        if (i13 >= i15) {
            return i13 == i15 && i14 == 0;
        }
        return true;
    }

    public final boolean d() {
        int i10 = this.f32172c;
        int i11 = this.f32173d;
        int i12 = 0;
        while (this.f32172c < this.f32171b && !e()) {
            i12++;
        }
        boolean z10 = this.f32172c == this.f32171b;
        this.f32172c = i10;
        this.f32173d = i11;
        return !z10 && c((i12 * 2) + 1);
    }

    public final boolean e() {
        boolean z10 = (this.f32170a[this.f32172c] & (128 >> this.f32173d)) != 0;
        k();
        return z10;
    }

    public final int f(int i10) {
        int i11;
        this.f32173d += i10;
        int i12 = 0;
        while (true) {
            i11 = this.f32173d;
            if (i11 <= 8) {
                break;
            }
            int i13 = i11 - 8;
            this.f32173d = i13;
            byte[] bArr = this.f32170a;
            int i14 = this.f32172c;
            i12 |= (bArr[i14] & 255) << i13;
            if (!j(i14 + 1)) {
                i = 1;
            }
            this.f32172c = i14 + i;
        }
        byte[] bArr2 = this.f32170a;
        int i15 = this.f32172c;
        int i16 = ((-1) >>> (32 - i10)) & (i12 | ((bArr2[i15] & 255) >> (8 - i11)));
        if (i11 == 8) {
            this.f32173d = 0;
            this.f32172c = i15 + (j(i15 + 1) ? 2 : 1);
        }
        a();
        return i16;
    }

    public final int g() {
        int i10 = 0;
        while (!e()) {
            i10++;
        }
        return ((1 << i10) - 1) + (i10 > 0 ? f(i10) : 0);
    }

    public final int h() {
        int iG = g();
        return ((iG + 1) / 2) * (iG % 2 == 0 ? -1 : 1);
    }

    public final int i() {
        return g();
    }

    public final boolean j(int i10) {
        if (2 <= i10 && i10 < this.f32171b) {
            byte[] bArr = this.f32170a;
            if (bArr[i10] == 3 && bArr[i10 - 2] == 0 && bArr[i10 - 1] == 0) {
                return true;
            }
        }
        return false;
    }

    public final void k() {
        int i10 = this.f32173d + 1;
        this.f32173d = i10;
        if (i10 == 8) {
            this.f32173d = 0;
            int i11 = this.f32172c;
            this.f32172c = i11 + (j(i11 + 1) ? 2 : 1);
        }
        a();
    }

    public final void l(int i10) {
        int i11 = this.f32172c;
        int i12 = i10 / 8;
        int i13 = i11 + i12;
        this.f32172c = i13;
        int i14 = (i10 - (i12 * 8)) + this.f32173d;
        this.f32173d = i14;
        if (i14 > 7) {
            this.f32172c = i13 + 1;
            this.f32173d = i14 - 8;
        }
        while (true) {
            i11++;
            if (i11 > this.f32172c) {
                a();
                return;
            } else if (j(i11)) {
                this.f32172c++;
                i11 += 2;
            }
        }
    }
}
