package x2;

import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.TimestampAdjuster;
import androidx.media3.common.util.Util;

/* JADX INFO: loaded from: classes.dex */
public final class g0 implements p0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final f0 f34441a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ParsableByteArray f34442b = new ParsableByteArray(32);

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

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

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

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

    public g0(f0 f0Var) {
        this.f34441a = f0Var;
    }

    @Override // x2.p0
    public final void a() {
        this.f34446f = true;
    }

    @Override // x2.p0
    public final void b(TimestampAdjuster timestampAdjuster, androidx.media3.extractor.b0 b0Var, o0 o0Var) {
        this.f34441a.b(timestampAdjuster, b0Var, o0Var);
        this.f34446f = true;
    }

    @Override // x2.p0
    public final void c(int i10, ParsableByteArray parsableByteArray) {
        boolean z10 = (i10 & 1) != 0;
        int position = z10 ? parsableByteArray.getPosition() + parsableByteArray.readUnsignedByte() : -1;
        if (this.f34446f) {
            if (!z10) {
                return;
            }
            this.f34446f = false;
            parsableByteArray.setPosition(position);
            this.f34444d = 0;
        }
        while (parsableByteArray.bytesLeft() > 0) {
            int i11 = this.f34444d;
            ParsableByteArray parsableByteArray2 = this.f34442b;
            if (i11 < 3) {
                if (i11 == 0) {
                    int unsignedByte = parsableByteArray.readUnsignedByte();
                    parsableByteArray.setPosition(parsableByteArray.getPosition() - 1);
                    if (unsignedByte == 255) {
                        this.f34446f = true;
                        return;
                    }
                }
                int iMin = Math.min(parsableByteArray.bytesLeft(), 3 - this.f34444d);
                parsableByteArray.readBytes(parsableByteArray2.getData(), this.f34444d, iMin);
                int i12 = this.f34444d + iMin;
                this.f34444d = i12;
                if (i12 == 3) {
                    parsableByteArray2.setPosition(0);
                    parsableByteArray2.setLimit(3);
                    parsableByteArray2.skipBytes(1);
                    int unsignedByte2 = parsableByteArray2.readUnsignedByte();
                    int unsignedByte3 = parsableByteArray2.readUnsignedByte();
                    this.f34445e = (unsignedByte2 & 128) != 0;
                    this.f34443c = (((unsignedByte2 & 15) << 8) | unsignedByte3) + 3;
                    int iCapacity = parsableByteArray2.capacity();
                    int i13 = this.f34443c;
                    if (iCapacity < i13) {
                        parsableByteArray2.ensureCapacity(Math.min(4098, Math.max(i13, parsableByteArray2.capacity() * 2)));
                    }
                }
            } else {
                int iMin2 = Math.min(parsableByteArray.bytesLeft(), this.f34443c - this.f34444d);
                parsableByteArray.readBytes(parsableByteArray2.getData(), this.f34444d, iMin2);
                int i14 = this.f34444d + iMin2;
                this.f34444d = i14;
                int i15 = this.f34443c;
                if (i14 != i15) {
                    continue;
                } else {
                    if (!this.f34445e) {
                        parsableByteArray2.setLimit(i15);
                    } else {
                        if (Util.crc32(parsableByteArray2.getData(), 0, this.f34443c, -1) != 0) {
                            this.f34446f = true;
                            return;
                        }
                        parsableByteArray2.setLimit(this.f34443c - 4);
                    }
                    parsableByteArray2.setPosition(0);
                    this.f34441a.c(parsableByteArray2);
                    this.f34444d = 0;
                }
            }
        }
    }
}
