Rabbit-R1/android (non root)/java/sources/androidx/media3/extractor/ts/Ac4Reader.java
2024-05-21 17:08:36 -04:00

152 lines
6.2 KiB
Java

package androidx.media3.extractor.ts;
import androidx.media3.common.Format;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ParsableBitArray;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.extractor.Ac4Util;
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 Ac4Reader implements ElementaryStreamReader {
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 boolean hasCRC;
private final ParsableBitArray headerScratchBits;
private final ParsableByteArray headerScratchBytes;
private final String language;
private boolean lastByteWasAC;
private TrackOutput output;
private long sampleDurationUs;
private int sampleSize;
private int state;
private long timeUs;
@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.lastByteWasAC = false;
this.hasCRC = false;
this.timeUs = -9223372036854775807L;
}
public Ac4Reader() {
this(null);
}
public Ac4Reader(String str) {
ParsableBitArray parsableBitArray = new ParsableBitArray(new byte[16]);
this.headerScratchBits = parsableBitArray;
this.headerScratchBytes = new ParsableByteArray(parsableBitArray.data);
this.state = 0;
this.bytesRead = 0;
this.lastByteWasAC = false;
this.hasCRC = false;
this.timeUs = -9223372036854775807L;
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 if (continueRead(parsableByteArray, this.headerScratchBytes.getData(), 16)) {
parseHeader();
this.headerScratchBytes.setPosition(0);
this.output.sampleData(this.headerScratchBytes, 16);
this.state = 2;
}
} else if (skipToNextSync(parsableByteArray)) {
this.state = 1;
this.headerScratchBytes.getData()[0] = -84;
this.headerScratchBytes.getData()[1] = (byte) (this.hasCRC ? 65 : 64);
this.bytesRead = 2;
}
}
}
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) {
int readUnsignedByte;
while (true) {
if (parsableByteArray.bytesLeft() <= 0) {
return false;
}
if (!this.lastByteWasAC) {
this.lastByteWasAC = parsableByteArray.readUnsignedByte() == 172;
} else {
readUnsignedByte = parsableByteArray.readUnsignedByte();
this.lastByteWasAC = readUnsignedByte == 172;
if (readUnsignedByte == 64 || readUnsignedByte == 65) {
break;
}
}
}
this.hasCRC = readUnsignedByte == 65;
return true;
}
@RequiresNonNull({"output"})
private void parseHeader() {
this.headerScratchBits.setPosition(0);
Ac4Util.SyncFrameInfo parseAc4SyncframeInfo = Ac4Util.parseAc4SyncframeInfo(this.headerScratchBits);
if (this.format == null || parseAc4SyncframeInfo.channelCount != this.format.channelCount || parseAc4SyncframeInfo.sampleRate != this.format.sampleRate || !"audio/ac4".equals(this.format.sampleMimeType)) {
Format build = new Format.Builder().setId(this.formatId).setSampleMimeType("audio/ac4").setChannelCount(parseAc4SyncframeInfo.channelCount).setSampleRate(parseAc4SyncframeInfo.sampleRate).setLanguage(this.language).build();
this.format = build;
this.output.format(build);
}
this.sampleSize = parseAc4SyncframeInfo.frameSize;
this.sampleDurationUs = (parseAc4SyncframeInfo.sampleCount * 1000000) / this.format.sampleRate;
}
}