package androidx.media3.extractor.ts; import androidx.media3.common.Format; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.ParsableByteArray; import androidx.media3.extractor.DtsUtil; import androidx.media3.extractor.ExtractorOutput; import androidx.media3.extractor.TrackOutput; import androidx.media3.extractor.ts.TsPayloadReader; import org.checkerframework.checker.nullness.qual.RequiresNonNull; /* loaded from: classes2.dex */ public final class DtsReader implements ElementaryStreamReader { private static final int HEADER_SIZE = 18; private static final int STATE_FINDING_SYNC = 0; private static final int STATE_READING_HEADER = 1; private static final int STATE_READING_SAMPLE = 2; private int bytesRead; private Format format; private String formatId; private final String language; private TrackOutput output; private long sampleDurationUs; private int sampleSize; private int syncBytes; private final ParsableByteArray headerScratchBytes = new ParsableByteArray(new byte[18]); private int state = 0; private long timeUs = -9223372036854775807L; @Override // androidx.media3.extractor.ts.ElementaryStreamReader public void packetFinished() { } @Override // androidx.media3.extractor.ts.ElementaryStreamReader public void packetStarted(long j, int i) { if (j != -9223372036854775807L) { this.timeUs = j; } } @Override // androidx.media3.extractor.ts.ElementaryStreamReader public void seek() { this.state = 0; this.bytesRead = 0; this.syncBytes = 0; this.timeUs = -9223372036854775807L; } public DtsReader(String str) { this.language = str; } @Override // androidx.media3.extractor.ts.ElementaryStreamReader public void createTracks(ExtractorOutput extractorOutput, TsPayloadReader.TrackIdGenerator trackIdGenerator) { trackIdGenerator.generateNewId(); this.formatId = trackIdGenerator.getFormatId(); this.output = extractorOutput.track(trackIdGenerator.getTrackId(), 1); } @Override // androidx.media3.extractor.ts.ElementaryStreamReader public void consume(ParsableByteArray parsableByteArray) { Assertions.checkStateNotNull(this.output); while (parsableByteArray.bytesLeft() > 0) { int i = this.state; if (i != 0) { if (i != 1) { if (i == 2) { int min = Math.min(parsableByteArray.bytesLeft(), this.sampleSize - this.bytesRead); this.output.sampleData(parsableByteArray, min); int i2 = this.bytesRead + min; this.bytesRead = i2; int i3 = this.sampleSize; if (i2 == i3) { long j = this.timeUs; if (j != -9223372036854775807L) { this.output.sampleMetadata(j, 1, i3, 0, null); this.timeUs += this.sampleDurationUs; } this.state = 0; } } else { throw new IllegalStateException(); } } else if (continueRead(parsableByteArray, this.headerScratchBytes.getData(), 18)) { parseHeader(); this.headerScratchBytes.setPosition(0); this.output.sampleData(this.headerScratchBytes, 18); this.state = 2; } } else if (skipToNextSync(parsableByteArray)) { this.state = 1; } } } private boolean continueRead(ParsableByteArray parsableByteArray, byte[] bArr, int i) { int min = Math.min(parsableByteArray.bytesLeft(), i - this.bytesRead); parsableByteArray.readBytes(bArr, this.bytesRead, min); int i2 = this.bytesRead + min; this.bytesRead = i2; return i2 == i; } private boolean skipToNextSync(ParsableByteArray parsableByteArray) { while (parsableByteArray.bytesLeft() > 0) { int i = this.syncBytes << 8; this.syncBytes = i; int readUnsignedByte = i | parsableByteArray.readUnsignedByte(); this.syncBytes = readUnsignedByte; if (DtsUtil.isSyncWord(readUnsignedByte)) { byte[] data = this.headerScratchBytes.getData(); int i2 = this.syncBytes; data[0] = (byte) ((i2 >> 24) & 255); data[1] = (byte) ((i2 >> 16) & 255); data[2] = (byte) ((i2 >> 8) & 255); data[3] = (byte) (i2 & 255); this.bytesRead = 4; this.syncBytes = 0; return true; } } return false; } @RequiresNonNull({"output"}) private void parseHeader() { byte[] data = this.headerScratchBytes.getData(); if (this.format == null) { Format parseDtsFormat = DtsUtil.parseDtsFormat(data, this.formatId, this.language, null); this.format = parseDtsFormat; this.output.format(parseDtsFormat); } this.sampleSize = DtsUtil.getDtsFrameSize(data); this.sampleDurationUs = (int) ((DtsUtil.parseDtsAudioSampleCount(data) * 1000000) / this.format.sampleRate); } }