add readSignedInt24 in ParsableByteArray

parent 5895884c
...@@ -79,9 +79,7 @@ import com.google.android.exoplayer2.video.AvcConfig; ...@@ -79,9 +79,7 @@ import com.google.android.exoplayer2.video.AvcConfig;
@Override @Override
protected void parsePayload(ParsableByteArray data, long timeUs) throws ParserException { protected void parsePayload(ParsableByteArray data, long timeUs) throws ParserException {
int packetType = data.readUnsignedByte(); int packetType = data.readUnsignedByte();
int compositionTimeMs = data.readUnsignedInt24(); int compositionTimeMs = data.readSignedInt24();
// compositionTimeMs is signed int 24, change unsigned int 24 to signed int 24
compositionTimeMs = (compositionTimeMs & 0x800000L) >>> 23 == 1 ? (compositionTimeMs | 0xff000000) : compositionTimeMs;
timeUs += compositionTimeMs * 1000L; timeUs += compositionTimeMs * 1000L;
// Parse avc sequence header in case this was not done before. // Parse avc sequence header in case this was not done before.
......
...@@ -257,6 +257,14 @@ public final class ParsableByteArray { ...@@ -257,6 +257,14 @@ public final class ParsableByteArray {
} }
/** /**
* Reads the next three bytes as an signed value.
*/
public int readSignedInt24() {
int ui24 = readUnsignedInt24();
return (ui24 & 0x800000L) >>> 23 == 1 ? (ui24 | 0xff000000) : ui24;
}
/**
* Reads the next three bytes as a signed value in little endian order. * Reads the next three bytes as a signed value in little endian order.
*/ */
public int readLittleEndianInt24() { public int readLittleEndianInt24() {
......
...@@ -335,6 +335,22 @@ public final class ParsableByteArrayTest { ...@@ -335,6 +335,22 @@ public final class ParsableByteArrayTest {
} }
@Test @Test
public void testReadPositiveSignedInt24() {
byte[] data = { 0x01, 0x02, (byte) 0xFF };
ParsableByteArray byteArray = new ParsableByteArray(data);
assertThat(byteArray.readSignedInt24()).isEqualTo(0x0102FF);
assertThat(byteArray.getPosition()).isEqualTo(3);
}
@Test
public void testReadNegativeSignedInt24() {
byte[] data = { (byte)0xFF, 0x02, (byte) 0x01 };
ParsableByteArray byteArray = new ParsableByteArray(data);
assertThat(byteArray.readSignedInt24()).isEqualTo(0xFFFF0201);
assertThat(byteArray.getPosition()).isEqualTo(3);
}
@Test
public void testReadLittleEndianUnsignedShort() { public void testReadLittleEndianUnsignedShort() {
ParsableByteArray byteArray = new ParsableByteArray(new byte[] { ParsableByteArray byteArray = new ParsableByteArray(new byte[] {
0x01, (byte) 0xFF, 0x02, (byte) 0xFF 0x01, (byte) 0xFF, 0x02, (byte) 0xFF
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment