package androidx.media3.decoder.midi;

import androidx.media3.common.ParserException;
import androidx.media3.common.util.ParsableByteArray;

/* JADX INFO: loaded from: classes.dex */
final class TrackEvent {
    public static final int DATA_FIELD_UNSET = Integer.MIN_VALUE;
    private static final int META_END_OF_TRACK = 47;
    private static final int META_EVENT_STATUS = 255;
    private static final int META_TEMPO_CHANGE = 81;
    public static final int MIDI_MESSAGE_LENGTH_BYTES = 3;
    private static final int SYSEX_BEGIN_STATUS = 240;
    private static final int SYSEX_END_STATUS = 247;
    private static final int TICKS_UNSET = -1;
    private int data1;
    private int data2;
    public long elapsedTimeDeltaTicks;
    public int eventDecoderSizeBytes;
    public int eventFileSizeBytes;
    private boolean isPopulated;
    public int statusByte;
    public int timestampSize;
    public long usPerQuarterNote;
    private static final int[] CHANNEL_BYTE_LENGTHS = {3, 3, 3, 3, 2, 2, 3};
    private static final int[] SYSTEM_BYTE_LENGTHS = {1, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

    public TrackEvent() {
        reset();
    }

    private static int getMidiMessageLengthBytes(int i2) {
        if (i2 < 128 || i2 > 255) {
            return 0;
        }
        return i2 >= 240 ? SYSTEM_BYTE_LENGTHS[i2 & 15] : CHANNEL_BYTE_LENGTHS[(i2 >> 4) & 7];
    }

    private static int readVariableLengthInt(ParsableByteArray parsableByteArray) {
        int i2 = 0;
        int i3 = 0;
        do {
            int unsignedByte = parsableByteArray.readUnsignedByte();
            i2 = (i2 << 7) | (unsignedByte & 127);
            i3++;
            if ((unsignedByte & 128) == 0) {
                break;
            }
        } while (i3 <= 4);
        return i2;
    }

    public boolean isMetaEvent() {
        return this.statusByte == 255;
    }

    public boolean isMidiEvent() {
        int i2 = this.statusByte;
        return (i2 == 255 || i2 == 240) ? false : true;
    }

    public boolean isNoteChannelEvent() {
        int i2 = this.statusByte >>> 4;
        return isMidiEvent() && (i2 == 8 || i2 == 9);
    }

    public boolean isPopulated() {
        return this.isPopulated;
    }

    /* JADX INFO: Thrown type has an unknown type hierarchy: androidx.media3.common.ParserException */
    public boolean populateFrom(ParsableByteArray parsableByteArray, int i2) throws ParserException {
        reset();
        int position = parsableByteArray.getPosition();
        if (parsableByteArray.bytesLeft() < 2) {
            return false;
        }
        this.elapsedTimeDeltaTicks = readVariableLengthInt(parsableByteArray);
        this.timestampSize = parsableByteArray.getPosition() - position;
        int unsignedByte = parsableByteArray.readUnsignedByte();
        this.eventDecoderSizeBytes = 1;
        if (unsignedByte == 240) {
            this.statusByte = unsignedByte;
            while (parsableByteArray.readUnsignedByte() != 247) {
            }
        } else if (unsignedByte == 255) {
            int unsignedByte2 = parsableByteArray.readUnsignedByte();
            int variableLengthInt = readVariableLengthInt(parsableByteArray);
            this.statusByte = unsignedByte;
            if (unsignedByte2 == 47) {
                parsableByteArray.setPosition(position);
                reset();
                return false;
            }
            if (unsignedByte2 != 81) {
                parsableByteArray.skipBytes(variableLengthInt);
            } else {
                long unsignedInt24 = parsableByteArray.readUnsignedInt24();
                this.usPerQuarterNote = unsignedInt24;
                if (unsignedInt24 <= 0) {
                    throw ParserException.createForUnsupportedContainerFeature("Tempo event data value must be a non-zero positive value. Parsed value: " + this.usPerQuarterNote);
                }
                parsableByteArray.skipBytes(variableLengthInt - 3);
            }
        } else {
            boolean z2 = unsignedByte < 128;
            if (!z2) {
                i2 = unsignedByte;
            } else {
                if (i2 == Integer.MIN_VALUE) {
                    throw ParserException.createForMalformedContainer("Running status in the first event.", (Throwable) null);
                }
                this.data1 = unsignedByte;
                this.eventDecoderSizeBytes = 1 + 1;
            }
            int midiMessageLengthBytes = getMidiMessageLengthBytes(i2);
            if (!z2 && midiMessageLengthBytes > this.eventDecoderSizeBytes) {
                this.data1 = parsableByteArray.readUnsignedByte();
                this.eventDecoderSizeBytes++;
            }
            if (midiMessageLengthBytes > this.eventDecoderSizeBytes) {
                this.data2 = parsableByteArray.readUnsignedByte();
                this.eventDecoderSizeBytes++;
            }
            this.statusByte = i2;
        }
        this.eventFileSizeBytes = parsableByteArray.getPosition() - position;
        parsableByteArray.setPosition(position);
        this.isPopulated = true;
        return true;
    }

    public void reset() {
        this.isPopulated = false;
        this.timestampSize = -1;
        this.statusByte = Integer.MIN_VALUE;
        this.data1 = Integer.MIN_VALUE;
        this.data2 = Integer.MIN_VALUE;
        this.elapsedTimeDeltaTicks = -1L;
        this.eventFileSizeBytes = -1;
        this.eventDecoderSizeBytes = -1;
        this.usPerQuarterNote = -9223372036854775807L;
    }

    public void writeTo(byte[] bArr) {
        bArr[0] = (byte) this.statusByte;
        bArr[1] = (byte) this.data1;
        bArr[2] = (byte) this.data2;
    }
}
