package androidx.media3.container;

import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class ParsableNalUnitBitArray {
    private int bitOffset;
    private int byteLimit;
    private int byteOffset;
    private byte[] data;

    public ParsableNalUnitBitArray(byte[] bArr, int i6, int i7) {
        reset(bArr, i6, i7);
    }

    private void assertValidOffset() {
        int i6;
        int i7 = this.byteOffset;
        Assertions.checkState(i7 >= 0 && (i7 < (i6 = this.byteLimit) || (i7 == i6 && this.bitOffset == 0)));
    }

    private int readExpGolombCodeNum() {
        int i6 = 0;
        while (!readBit()) {
            i6++;
        }
        return ((1 << i6) - 1) + (i6 > 0 ? readBits(i6) : 0);
    }

    private boolean shouldSkipByte(int i6) {
        if (2 > i6 || i6 >= this.byteLimit) {
            return false;
        }
        byte[] bArr = this.data;
        return bArr[i6] == 3 && bArr[i6 + (-2)] == 0 && bArr[i6 - 1] == 0;
    }

    public boolean canReadBits(int i6) {
        int i7 = this.byteOffset;
        int i8 = i6 / 8;
        int i9 = i7 + i8;
        int i10 = (this.bitOffset + i6) - (i8 * 8);
        if (i10 > 7) {
            i9++;
            i10 -= 8;
        }
        while (true) {
            i7++;
            if (i7 > i9 || i9 >= this.byteLimit) {
                break;
            }
            if (shouldSkipByte(i7)) {
                i9++;
                i7 += 2;
            }
        }
        int i11 = this.byteLimit;
        if (i9 >= i11) {
            return i9 == i11 && i10 == 0;
        }
        return true;
    }

    public boolean canReadExpGolombCodedNum() {
        int i6 = this.byteOffset;
        int i7 = this.bitOffset;
        int i8 = 0;
        while (this.byteOffset < this.byteLimit && !readBit()) {
            i8++;
        }
        boolean z6 = this.byteOffset == this.byteLimit;
        this.byteOffset = i6;
        this.bitOffset = i7;
        return !z6 && canReadBits((i8 * 2) + 1);
    }

    public boolean readBit() {
        boolean z6 = (this.data[this.byteOffset] & (128 >> this.bitOffset)) != 0;
        skipBit();
        return z6;
    }

    public int readBits(int i6) {
        int i7;
        this.bitOffset += i6;
        int i8 = 0;
        while (true) {
            i7 = this.bitOffset;
            if (i7 <= 8) {
                break;
            }
            int i9 = i7 - 8;
            this.bitOffset = i9;
            byte[] bArr = this.data;
            int i10 = this.byteOffset;
            i8 |= (bArr[i10] & 255) << i9;
            if (!shouldSkipByte(i10 + 1)) {
                i = 1;
            }
            this.byteOffset = i10 + i;
        }
        byte[] bArr2 = this.data;
        int i11 = this.byteOffset;
        int i12 = ((-1) >>> (32 - i6)) & (i8 | ((bArr2[i11] & 255) >> (8 - i7)));
        if (i7 == 8) {
            this.bitOffset = 0;
            this.byteOffset = i11 + (shouldSkipByte(i11 + 1) ? 2 : 1);
        }
        assertValidOffset();
        return i12;
    }

    public int readSignedExpGolombCodedInt() {
        int expGolombCodeNum = readExpGolombCodeNum();
        return ((expGolombCodeNum + 1) / 2) * (expGolombCodeNum % 2 == 0 ? -1 : 1);
    }

    public int readUnsignedExpGolombCodedInt() {
        return readExpGolombCodeNum();
    }

    public void reset(byte[] bArr, int i6, int i7) {
        this.data = bArr;
        this.byteOffset = i6;
        this.byteLimit = i7;
        this.bitOffset = 0;
        assertValidOffset();
    }

    public void skipBit() {
        int i6 = this.bitOffset + 1;
        this.bitOffset = i6;
        if (i6 == 8) {
            this.bitOffset = 0;
            int i7 = this.byteOffset;
            this.byteOffset = i7 + (shouldSkipByte(i7 + 1) ? 2 : 1);
        }
        assertValidOffset();
    }

    public void skipBits(int i6) {
        int i7 = this.byteOffset;
        int i8 = i6 / 8;
        int i9 = i7 + i8;
        this.byteOffset = i9;
        int i10 = (i6 - (i8 * 8)) + this.bitOffset;
        this.bitOffset = i10;
        if (i10 > 7) {
            this.byteOffset = i9 + 1;
            this.bitOffset = i10 - 8;
        }
        while (true) {
            i7++;
            if (i7 > this.byteOffset) {
                assertValidOffset();
                return;
            } else if (shouldSkipByte(i7)) {
                this.byteOffset++;
                i7 += 2;
            }
        }
    }
}
