diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AviExtractor.java b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AviExtractor.java index d4a97d065e..f2c88fe5f7 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AviExtractor.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/avi/AviExtractor.java @@ -31,6 +31,7 @@ import com.google.android.exoplayer2.util.MimeTypes; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -449,8 +450,8 @@ public class AviExtractor implements Extractor { final int size = indexByteBuffer.getInt(); if ((flags & AVIIF_KEYFRAME) == AVIIF_KEYFRAME) { if (chunkHandler.isVideo()) { - int indexSize = seekIndexes[videoId].getSize(); - if (indexSize == 0 || chunkHandler.chunks - seekIndexes[videoId].get(indexSize - 1) >= chunksPerKeyFrame) { + int indexSize = seekIndexes[videoId].size; + if (indexSize == 0 || chunkHandler.chunks - seekIndexes[videoId].array[indexSize - 1] >= chunksPerKeyFrame) { keyFrameOffsetsDiv2.add(offset / 2); for (@Nullable ChunkHandler seekTrack : chunkHandlers) { if (seekTrack != null) { @@ -469,13 +470,17 @@ public class AviExtractor implements Extractor { if (videoTrack.chunks == keyFrameCounts[videoTrack.getId()]) { videoTrack.setKeyFrames(ChunkHandler.ALL_KEY_FRAMES); } else { - videoTrack.setKeyFrames(seekIndexes[videoId].getArray()); + videoTrack.setKeyFrames(seekIndexes[videoId].pack()); } //Work-around a bug where the offset is from the start of the file, not "movi" final long seekOffset = firstEntry.getInt(8) > moviOffset ? 0L : moviOffset; + int[][] seekIndexArrays = new int[seekIndexes.length][]; + for (int i=0;i= size) { - throw new ArrayIndexOutOfBoundsException(index + ">=" + size); - } - return array[index]; - } - - public int getSize() { - return size; - } - - public void pack() { - if (size != array.length) { - array = Arrays.copyOf(array, size); - } - } - - protected void grow() { - int increase = Math.max(array.length /4, 1); - array = Arrays.copyOf(array, increase + array.length + size); - } - - public int[] getArray() { - pack(); - return array; - } - - /** - * Only works if values are in sequential order - */ - public int indexOf(int v) { - return Arrays.binarySearch(array, v); - } -} diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviExtractorTest.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviExtractorTest.java index 9605e208c2..013fc3afe3 100644 --- a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviExtractorTest.java +++ b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/AviExtractorTest.java @@ -594,4 +594,18 @@ public class AviExtractorTest { final FakeTrackOutput fakeTrackOutput = (FakeTrackOutput) chunkHandler.trackOutput; Assert.assertEquals(size, fakeTrackOutput.getSampleData(0).length); } + + @Test + public void unboundIntArray_add_givenExceedsCapacity() { + final AviExtractor.UnboundedIntArray unboundedIntArray = new AviExtractor.UnboundedIntArray(); + final int testLen = unboundedIntArray.array.length + 1; + for (int i=0; i < testLen; i++) { + unboundedIntArray.add(i); + } + + Assert.assertEquals(testLen, unboundedIntArray.size); + for (int i=0; i < testLen; i++) { + Assert.assertEquals(i, unboundedIntArray.array[i]); + } + } } \ No newline at end of file diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/DataHelper.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/DataHelper.java index cfd86f24d6..c78abae9b8 100644 --- a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/DataHelper.java +++ b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/DataHelper.java @@ -118,14 +118,12 @@ public class DataHelper { public static AviSeekMap getAviSeekMap() { final int[] keyFrameOffsetsDiv2= {4, 1024}; - final UnboundedIntArray videoArray = new UnboundedIntArray(); - videoArray.add(0); - videoArray.add(4); - final UnboundedIntArray audioArray = new UnboundedIntArray(); - audioArray.add(0); - audioArray.add(128); + final int[] videoArray = new int[2]; + videoArray[1] = 4; + final int[] audioArray = new int[2]; + audioArray[1] = 128; return new AviSeekMap(0, 100L, 8, keyFrameOffsetsDiv2, - new UnboundedIntArray[]{videoArray, audioArray}, MOVI_OFFSET); + new int[][]{videoArray, audioArray}, MOVI_OFFSET); } private static void putIndex(final ByteBuffer byteBuffer, int chunkId, int flags, int offset, diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/UnboundedIntArrayTest.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/UnboundedIntArrayTest.java deleted file mode 100644 index 7f4251e31f..0000000000 --- a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/avi/UnboundedIntArrayTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.extractor.avi; - -import org.junit.Assert; -import org.junit.Test; - -public class UnboundedIntArrayTest { - @Test - public void add_givenInt() { - final UnboundedIntArray unboundedIntArray = new UnboundedIntArray(); - unboundedIntArray.add(4); - Assert.assertEquals(1, unboundedIntArray.getSize()); - Assert.assertEquals(unboundedIntArray.getArray()[0], 4); - } - - @Test - public void indexOf_givenOrderSet() { - final UnboundedIntArray unboundedIntArray = new UnboundedIntArray(); - unboundedIntArray.add(2); - unboundedIntArray.add(4); - unboundedIntArray.add(5); - unboundedIntArray.add(8); - Assert.assertEquals(2, unboundedIntArray.indexOf(5)); - Assert.assertTrue(unboundedIntArray.indexOf(6) < 0); - } - - @Test - public void grow_givenSizeOfOne() { - final UnboundedIntArray unboundedIntArray = new UnboundedIntArray(1); - unboundedIntArray.add(0); - Assert.assertEquals(1, unboundedIntArray.getSize()); - unboundedIntArray.add(1); - Assert.assertTrue(unboundedIntArray.getSize() > 1); - } - - @Test - public void pack_givenSizeOfOne() { - final UnboundedIntArray unboundedIntArray = new UnboundedIntArray(8); - unboundedIntArray.add(1); - unboundedIntArray.add(2); - Assert.assertEquals(8, unboundedIntArray.array.length); - unboundedIntArray.pack(); - Assert.assertEquals(2, unboundedIntArray.array.length); - } - - @Test - public void illegalArgument_givenNegativeSize() { - try { - new UnboundedIntArray(-1); - Assert.fail(); - } catch (IllegalArgumentException e) { - //Intentionally blank - } - } - - @Test - public void get_givenValidIndex() { - final UnboundedIntArray unboundedIntArray = new UnboundedIntArray(4); - unboundedIntArray.add(1); - unboundedIntArray.add(2); - Assert.assertEquals(1, unboundedIntArray.get(0)); - } - - @Test - public void get_givenOutOfBounds() { - final UnboundedIntArray unboundedIntArray = new UnboundedIntArray(4); - try { - unboundedIntArray.get(0); - Assert.fail(); - } catch (ArrayIndexOutOfBoundsException e) { - //Intentionally blank - } - } -}