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

153 lines
6.2 KiB
Java

package androidx.media3.extractor.ts;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.TimestampAdjuster;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.ExtractorInput;
import androidx.media3.extractor.PositionHolder;
import java.io.IOException;
/* loaded from: classes2.dex */
final class PsDurationReader {
private static final String TAG = "PsDurationReader";
private static final int TIMESTAMP_SEARCH_BYTES = 20000;
private boolean isDurationRead;
private boolean isFirstScrValueRead;
private boolean isLastScrValueRead;
private final TimestampAdjuster scrTimestampAdjuster = new TimestampAdjuster(0);
private long firstScrValue = -9223372036854775807L;
private long lastScrValue = -9223372036854775807L;
private long durationUs = -9223372036854775807L;
private final ParsableByteArray packetBuffer = new ParsableByteArray();
public long getDurationUs() {
return this.durationUs;
}
public TimestampAdjuster getScrTimestampAdjuster() {
return this.scrTimestampAdjuster;
}
public boolean isDurationReadFinished() {
return this.isDurationRead;
}
public int readDuration(ExtractorInput extractorInput, PositionHolder positionHolder) throws IOException {
if (!this.isLastScrValueRead) {
return readLastScrValue(extractorInput, positionHolder);
}
if (this.lastScrValue == -9223372036854775807L) {
return finishReadDuration(extractorInput);
}
if (!this.isFirstScrValueRead) {
return readFirstScrValue(extractorInput, positionHolder);
}
long j = this.firstScrValue;
if (j == -9223372036854775807L) {
return finishReadDuration(extractorInput);
}
long adjustTsTimestamp = this.scrTimestampAdjuster.adjustTsTimestamp(this.lastScrValue) - this.scrTimestampAdjuster.adjustTsTimestamp(j);
this.durationUs = adjustTsTimestamp;
if (adjustTsTimestamp < 0) {
Log.w(TAG, "Invalid duration: " + this.durationUs + ". Using TIME_UNSET instead.");
this.durationUs = -9223372036854775807L;
}
return finishReadDuration(extractorInput);
}
public static long readScrValueFromPack(ParsableByteArray parsableByteArray) {
int position = parsableByteArray.getPosition();
if (parsableByteArray.bytesLeft() < 9) {
return -9223372036854775807L;
}
byte[] bArr = new byte[9];
parsableByteArray.readBytes(bArr, 0, 9);
parsableByteArray.setPosition(position);
if (checkMarkerBits(bArr)) {
return readScrValueFromPackHeader(bArr);
}
return -9223372036854775807L;
}
private int finishReadDuration(ExtractorInput extractorInput) {
this.packetBuffer.reset(Util.EMPTY_BYTE_ARRAY);
this.isDurationRead = true;
extractorInput.resetPeekPosition();
return 0;
}
private int readFirstScrValue(ExtractorInput extractorInput, PositionHolder positionHolder) throws IOException {
int min = (int) Math.min(20000L, extractorInput.getLength());
long j = 0;
if (extractorInput.getPosition() != j) {
positionHolder.position = j;
return 1;
}
this.packetBuffer.reset(min);
extractorInput.resetPeekPosition();
extractorInput.peekFully(this.packetBuffer.getData(), 0, min);
this.firstScrValue = readFirstScrValueFromBuffer(this.packetBuffer);
this.isFirstScrValueRead = true;
return 0;
}
private long readFirstScrValueFromBuffer(ParsableByteArray parsableByteArray) {
int limit = parsableByteArray.limit();
for (int position = parsableByteArray.getPosition(); position < limit - 3; position++) {
if (peekIntAtPosition(parsableByteArray.getData(), position) == 442) {
parsableByteArray.setPosition(position + 4);
long readScrValueFromPack = readScrValueFromPack(parsableByteArray);
if (readScrValueFromPack != -9223372036854775807L) {
return readScrValueFromPack;
}
}
}
return -9223372036854775807L;
}
private int readLastScrValue(ExtractorInput extractorInput, PositionHolder positionHolder) throws IOException {
long length = extractorInput.getLength();
int min = (int) Math.min(20000L, length);
long j = length - min;
if (extractorInput.getPosition() != j) {
positionHolder.position = j;
return 1;
}
this.packetBuffer.reset(min);
extractorInput.resetPeekPosition();
extractorInput.peekFully(this.packetBuffer.getData(), 0, min);
this.lastScrValue = readLastScrValueFromBuffer(this.packetBuffer);
this.isLastScrValueRead = true;
return 0;
}
private long readLastScrValueFromBuffer(ParsableByteArray parsableByteArray) {
int position = parsableByteArray.getPosition();
for (int limit = parsableByteArray.limit() - 4; limit >= position; limit--) {
if (peekIntAtPosition(parsableByteArray.getData(), limit) == 442) {
parsableByteArray.setPosition(limit + 4);
long readScrValueFromPack = readScrValueFromPack(parsableByteArray);
if (readScrValueFromPack != -9223372036854775807L) {
return readScrValueFromPack;
}
}
}
return -9223372036854775807L;
}
private int peekIntAtPosition(byte[] bArr, int i) {
return ((bArr[i] & 255) << 24) | ((bArr[i + 1] & 255) << 16) | ((bArr[i + 2] & 255) << 8) | (bArr[i + 3] & 255);
}
private static boolean checkMarkerBits(byte[] bArr) {
return (bArr[0] & 196) == 68 && (bArr[2] & 4) == 4 && (bArr[4] & 4) == 4 && (bArr[5] & 1) == 1 && (bArr[8] & 3) == 3;
}
private static long readScrValueFromPackHeader(byte[] bArr) {
byte b = bArr[0];
long j = (((b & 56) >> 3) << 30) | ((b & 3) << 28) | ((bArr[1] & 255) << 20);
byte b2 = bArr[2];
return j | (((b2 & 248) >> 3) << 15) | ((b2 & 3) << 13) | ((bArr[3] & 255) << 5) | ((bArr[4] & 248) >> 3);
}
}