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

152 lines
5.7 KiB
Java

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.ExtractorOutput;
import androidx.media3.extractor.MpegAudioUtil;
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 MpegAudioReader implements ElementaryStreamReader {
private static final int HEADER_SIZE = 4;
private static final int STATE_FINDING_HEADER = 0;
private static final int STATE_READING_FRAME = 2;
private static final int STATE_READING_HEADER = 1;
private String formatId;
private int frameBytesRead;
private long frameDurationUs;
private int frameSize;
private boolean hasOutputFormat;
private final MpegAudioUtil.Header header;
private final ParsableByteArray headerScratch;
private final String language;
private boolean lastByteWasFF;
private TrackOutput output;
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.frameBytesRead = 0;
this.lastByteWasFF = false;
this.timeUs = -9223372036854775807L;
}
public MpegAudioReader() {
this(null);
}
public MpegAudioReader(String str) {
this.state = 0;
ParsableByteArray parsableByteArray = new ParsableByteArray(4);
this.headerScratch = parsableByteArray;
parsableByteArray.getData()[0] = -1;
this.header = new MpegAudioUtil.Header();
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) {
findHeader(parsableByteArray);
} else if (i == 1) {
readHeaderRemainder(parsableByteArray);
} else if (i == 2) {
readFrameRemainder(parsableByteArray);
} else {
throw new IllegalStateException();
}
}
}
private void findHeader(ParsableByteArray parsableByteArray) {
byte[] data = parsableByteArray.getData();
int limit = parsableByteArray.limit();
for (int position = parsableByteArray.getPosition(); position < limit; position++) {
byte b = data[position];
boolean z = (b & 255) == 255;
boolean z2 = this.lastByteWasFF && (b & 224) == 224;
this.lastByteWasFF = z;
if (z2) {
parsableByteArray.setPosition(position + 1);
this.lastByteWasFF = false;
this.headerScratch.getData()[1] = data[position];
this.frameBytesRead = 2;
this.state = 1;
return;
}
}
parsableByteArray.setPosition(limit);
}
@RequiresNonNull({"output"})
private void readHeaderRemainder(ParsableByteArray parsableByteArray) {
int min = Math.min(parsableByteArray.bytesLeft(), 4 - this.frameBytesRead);
parsableByteArray.readBytes(this.headerScratch.getData(), this.frameBytesRead, min);
int i = this.frameBytesRead + min;
this.frameBytesRead = i;
if (i < 4) {
return;
}
this.headerScratch.setPosition(0);
if (!this.header.setForHeaderData(this.headerScratch.readInt())) {
this.frameBytesRead = 0;
this.state = 1;
return;
}
this.frameSize = this.header.frameSize;
if (!this.hasOutputFormat) {
this.frameDurationUs = (this.header.samplesPerFrame * 1000000) / this.header.sampleRate;
this.output.format(new Format.Builder().setId(this.formatId).setSampleMimeType(this.header.mimeType).setMaxInputSize(4096).setChannelCount(this.header.channels).setSampleRate(this.header.sampleRate).setLanguage(this.language).build());
this.hasOutputFormat = true;
}
this.headerScratch.setPosition(0);
this.output.sampleData(this.headerScratch, 4);
this.state = 2;
}
@RequiresNonNull({"output"})
private void readFrameRemainder(ParsableByteArray parsableByteArray) {
int min = Math.min(parsableByteArray.bytesLeft(), this.frameSize - this.frameBytesRead);
this.output.sampleData(parsableByteArray, min);
int i = this.frameBytesRead + min;
this.frameBytesRead = i;
int i2 = this.frameSize;
if (i < i2) {
return;
}
long j = this.timeUs;
if (j != -9223372036854775807L) {
this.output.sampleMetadata(j, 1, i2, 0, null);
this.timeUs += this.frameDurationUs;
}
this.frameBytesRead = 0;
this.state = 0;
}
}