From 5f76e0fa04a42fdaf81ebee367802fcd2872cad4 Mon Sep 17 00:00:00 2001 From: Dustin Date: Mon, 31 Jan 2022 10:52:12 -0700 Subject: [PATCH] AvcChunkPeeker tests --- .../extractor/avi/AvcChunkPeeker.java | 7 +- .../extractor/avi/PicCountClock.java | 12 ++++ .../extractor/avi/AvcChunkPeekerTest.java | 65 +++++++++++++++++++ .../extractor/avi/AviTrackTest.java | 15 +++++ .../exoplayer2/extractor/avi/BitBuffer.java | 13 ++++ 5 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeekerTest.java create mode 100644 library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviTrackTest.java diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeeker.java b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeeker.java index 950823da2a..9b26c2d1da 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeeker.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeeker.java @@ -34,7 +34,7 @@ public class AvcChunkPeeker extends NalChunkPeeker { picCountClock = new PicCountClock(clock.durationUs, clock.length); } - public LinearClock getClock() { + public PicCountClock getClock() { return picCountClock; } @@ -131,4 +131,9 @@ public class AvcChunkPeeker extends NalChunkPeeker { compact(); } } + + @VisibleForTesting(otherwise = VisibleForTesting.NONE) + public NalUnitUtil.SpsData getSpsData() { + return spsData; + } } diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/PicCountClock.java b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/PicCountClock.java index 050bdf59f9..55743a83db 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/PicCountClock.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/PicCountClock.java @@ -1,5 +1,7 @@ package com.google.android.exoplayer2.extractor.avi; +import androidx.annotation.VisibleForTesting; + /** * Properly calculates the frame time for H264 frames using PicCount */ @@ -60,4 +62,14 @@ public class PicCountClock extends LinearClock { public long getUs() { return getUs(picIndex); } + + @VisibleForTesting(otherwise = VisibleForTesting.NONE) + int getMaxPicCount() { + return maxPicCount; + } + + @VisibleForTesting(otherwise = VisibleForTesting.NONE) + int getLastPicCount() { + return lastPicCount; + } } diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeekerTest.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeekerTest.java new file mode 100644 index 0000000000..0e28dbc375 --- /dev/null +++ b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AvcChunkPeekerTest.java @@ -0,0 +1,65 @@ +package com.google.android.exoplayer2.extractor.avi; + +import android.content.Context; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.android.exoplayer2.Format; +import com.google.android.exoplayer2.testutil.FakeExtractorInput; +import com.google.android.exoplayer2.testutil.FakeTrackOutput; +import com.google.android.exoplayer2.testutil.TestUtil; +import com.google.android.exoplayer2.util.MimeTypes; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class AvcChunkPeekerTest { + private static final Format.Builder FORMAT_BUILDER_AVC = new Format.Builder(). + setSampleMimeType(MimeTypes.VIDEO_H264). + setWidth(1280).setHeight(720).setFrameRate(24000f/1001f); + + private static final byte[] P_SLICE = {00,00,00,01,0x41,(byte)0x9A,0x13,0x36,0x21,0x3A,0x5F, + (byte)0xFE,(byte)0x9E,0x10,00,00}; + + private FakeTrackOutput fakeTrackOutput; + private AvcChunkPeeker avcChunkPeeker; + + @Before + public void before() { + fakeTrackOutput = new FakeTrackOutput(false); + avcChunkPeeker = new AvcChunkPeeker(FORMAT_BUILDER_AVC, fakeTrackOutput, + new LinearClock(10_000_000L, 24 * 10)); + } + + private void peekStreamHeader() throws IOException { + final Context context = ApplicationProvider.getApplicationContext(); + final byte[] bytes = + TestUtil.getByteArray(context,"extractordumps/avi/avc_sei_sps_pps_ird.dump"); + + final FakeExtractorInput input = new FakeExtractorInput.Builder().setData(bytes).build(); + + avcChunkPeeker.peek(input, bytes.length); + } + + @Test + public void peek_givenStreamHeader() throws IOException { + peekStreamHeader(); + final PicCountClock picCountClock = avcChunkPeeker.getClock(); + Assert.assertEquals(64, picCountClock.getMaxPicCount()); + Assert.assertEquals(0, avcChunkPeeker.getSpsData().picOrderCountType); + Assert.assertEquals(1.18f, fakeTrackOutput.lastFormat.pixelWidthHeightRatio, 0.01f); + } + + @Test + public void peek_givenStreamHeaderAndPSlice() throws IOException { + peekStreamHeader(); + final PicCountClock picCountClock = avcChunkPeeker.getClock(); + final FakeExtractorInput input = new FakeExtractorInput.Builder().setData(P_SLICE).build(); + + avcChunkPeeker.peek(input, P_SLICE.length); + + Assert.assertEquals(12, picCountClock.getLastPicCount()); + } +} diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviTrackTest.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviTrackTest.java new file mode 100644 index 0000000000..5b9afcfac9 --- /dev/null +++ b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviTrackTest.java @@ -0,0 +1,15 @@ +package com.google.android.exoplayer2.extractor.avi; + +import org.junit.Assert; +import org.junit.Test; + +public class AviTrackTest { + @Test + public void setClock_givenLinearClock() { + final LinearClock linearClock = new LinearClock(1_000_000L, 30); + final AviTrack aviTrack = DataHelper.getVideoAviTrack(1); + aviTrack.setClock(linearClock); + + Assert.assertSame(linearClock, aviTrack.getClock()); + } +} diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/BitBuffer.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/BitBuffer.java index 4775d93872..66b24ada1b 100644 --- a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/BitBuffer.java +++ b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/BitBuffer.java @@ -30,6 +30,19 @@ public class BitBuffer { work |= (value & 0xffffffffL); } + public void pushExpGolomb(final int i) { + if (i == 0) { + push(true); + } + int v = i + 1; + int zeroBits = 0; + while ((v >>>=1) > 1) { + zeroBits++; + } + final int bits = zeroBits * 2 + 1; + push(bits, i + 1); + } + public byte[] getBytes() { //Byte align grow(8 - bits % 8);