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

129 lines
5.2 KiB
Java

package androidx.media3.extractor.ogg;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.ExtractorInput;
import androidx.media3.extractor.FlacFrameReader;
import androidx.media3.extractor.FlacMetadataReader;
import androidx.media3.extractor.FlacSeekTableSeekMap;
import androidx.media3.extractor.FlacStreamMetadata;
import androidx.media3.extractor.SeekMap;
import androidx.media3.extractor.ogg.StreamReader;
import java.util.Arrays;
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;
/* loaded from: classes2.dex */
final class FlacReader extends StreamReader {
private static final byte AUDIO_PACKET_TYPE = -1;
private static final int FRAME_HEADER_SAMPLE_NUMBER_OFFSET = 4;
private FlacOggSeeker flacOggSeeker;
private FlacStreamMetadata streamMetadata;
public static boolean verifyBitstreamType(ParsableByteArray parsableByteArray) {
return parsableByteArray.bytesLeft() >= 5 && parsableByteArray.readUnsignedByte() == 127 && parsableByteArray.readUnsignedInt() == 1179402563;
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // androidx.media3.extractor.ogg.StreamReader
public void reset(boolean z) {
super.reset(z);
if (z) {
this.streamMetadata = null;
this.flacOggSeeker = null;
}
}
private static boolean isAudioPacket(byte[] bArr) {
return bArr[0] == -1;
}
@Override // androidx.media3.extractor.ogg.StreamReader
protected long preparePayload(ParsableByteArray parsableByteArray) {
if (isAudioPacket(parsableByteArray.getData())) {
return getFlacFrameBlockSize(parsableByteArray);
}
return -1L;
}
@Override // androidx.media3.extractor.ogg.StreamReader
@EnsuresNonNullIf(expression = {"#3.format"}, result = false)
protected boolean readHeaders(ParsableByteArray parsableByteArray, long j, StreamReader.SetupData setupData) {
byte[] data = parsableByteArray.getData();
FlacStreamMetadata flacStreamMetadata = this.streamMetadata;
if (flacStreamMetadata == null) {
FlacStreamMetadata flacStreamMetadata2 = new FlacStreamMetadata(data, 17);
this.streamMetadata = flacStreamMetadata2;
setupData.format = flacStreamMetadata2.getFormat(Arrays.copyOfRange(data, 9, parsableByteArray.limit()), null);
return true;
}
if ((data[0] & Byte.MAX_VALUE) == 3) {
FlacStreamMetadata.SeekTable readSeekTableMetadataBlock = FlacMetadataReader.readSeekTableMetadataBlock(parsableByteArray);
FlacStreamMetadata copyWithSeekTable = flacStreamMetadata.copyWithSeekTable(readSeekTableMetadataBlock);
this.streamMetadata = copyWithSeekTable;
this.flacOggSeeker = new FlacOggSeeker(copyWithSeekTable, readSeekTableMetadataBlock);
return true;
}
if (!isAudioPacket(data)) {
return true;
}
FlacOggSeeker flacOggSeeker = this.flacOggSeeker;
if (flacOggSeeker != null) {
flacOggSeeker.setFirstFrameOffset(j);
setupData.oggSeeker = this.flacOggSeeker;
}
Assertions.checkNotNull(setupData.format);
return false;
}
private int getFlacFrameBlockSize(ParsableByteArray parsableByteArray) {
int i = (parsableByteArray.getData()[2] & 255) >> 4;
if (i == 6 || i == 7) {
parsableByteArray.skipBytes(4);
parsableByteArray.readUtf8EncodedLong();
}
int readFrameBlockSizeSamplesFromKey = FlacFrameReader.readFrameBlockSizeSamplesFromKey(parsableByteArray, i);
parsableByteArray.setPosition(0);
return readFrameBlockSizeSamplesFromKey;
}
/* loaded from: classes2.dex */
private static final class FlacOggSeeker implements OggSeeker {
private long firstFrameOffset = -1;
private long pendingSeekGranule = -1;
private FlacStreamMetadata.SeekTable seekTable;
private FlacStreamMetadata streamMetadata;
@Override // androidx.media3.extractor.ogg.OggSeeker
public long read(ExtractorInput extractorInput) {
long j = this.pendingSeekGranule;
if (j < 0) {
return -1L;
}
long j2 = -(j + 2);
this.pendingSeekGranule = -1L;
return j2;
}
public void setFirstFrameOffset(long j) {
this.firstFrameOffset = j;
}
public FlacOggSeeker(FlacStreamMetadata flacStreamMetadata, FlacStreamMetadata.SeekTable seekTable) {
this.streamMetadata = flacStreamMetadata;
this.seekTable = seekTable;
}
@Override // androidx.media3.extractor.ogg.OggSeeker
public void startSeek(long j) {
long[] jArr = this.seekTable.pointSampleNumbers;
this.pendingSeekGranule = jArr[Util.binarySearchFloor(jArr, j, true, true)];
}
@Override // androidx.media3.extractor.ogg.OggSeeker
public SeekMap createSeekMap() {
Assertions.checkState(this.firstFrameOffset != -1);
return new FlacSeekTableSeekMap(this.streamMetadata, this.firstFrameOffset);
}
}
}