Refactor to ChunkHander, add Mp3ChunkHandler

This commit is contained in:
Dustin 2022-02-04 12:37:24 -07:00
parent aee15f6c70
commit 5b952294f6
20 changed files with 372 additions and 240 deletions

View file

@ -565,4 +565,22 @@ public final class ParsableByteArray {
position += length;
return value;
}
/**
* The data from the end of the buffer is copied to the front
* The limit() because the bytesLeft() and position is zero
*/
public void compact() {
if (bytesLeft() == 0) {
limit = 0;
} else {
final ByteBuffer byteBuffer = ByteBuffer.wrap(data);
byteBuffer.limit(limit);
byteBuffer.position(position);
byteBuffer.compact();
byteBuffer.flip();
limit = byteBuffer.limit();
}
position = 0;
}
}

View file

@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.extractor.avi;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.extractor.ExtractorInput;
@ -27,7 +28,7 @@ import java.io.IOException;
* Corrects the time and PAR for H264 streams
* AVC is very rare in AVI due to the rise of the mp4 container
*/
public class AvcChunkPeeker extends NalChunkPeeker {
public class AvcChunkHandler extends NalChunkPeeker {
private static final int NAL_TYPE_MASK = 0x1f;
private static final int NAL_TYPE_IDR = 5; //I Frame
private static final int NAL_TYPE_SEI = 6;
@ -35,22 +36,21 @@ public class AvcChunkPeeker extends NalChunkPeeker {
private static final int NAL_TYPE_PPS = 8;
private static final int NAL_TYPE_AUD = 9;
private final PicCountClock picCountClock;
private final Format.Builder formatBuilder;
private final TrackOutput trackOutput;
private float pixelWidthHeightRatio = 1f;
private NalUnitUtil.SpsData spsData;
public AvcChunkPeeker(Format.Builder formatBuilder, TrackOutput trackOutput, LinearClock clock) {
super(16);
public AvcChunkHandler(int id, @NonNull TrackOutput trackOutput,
@NonNull ChunkClock clock, Format.Builder formatBuilder) {
super(id, trackOutput, new PicCountClock(clock.durationUs, clock.chunks), 16);
this.formatBuilder = formatBuilder;
this.trackOutput = trackOutput;
picCountClock = new PicCountClock(clock.durationUs, clock.length);
}
@NonNull
@Override
public PicCountClock getClock() {
return picCountClock;
return (PicCountClock) clock;
}
@Override
@ -84,13 +84,13 @@ public class AvcChunkPeeker extends NalChunkPeeker {
if (spsData.picOrderCountType == 0) {
int picOrderCountLsb = in.readBits(spsData.picOrderCntLsbLength);
//Log.d("Test", "FrameNum: " + frame + " cnt=" + picOrderCountLsb);
picCountClock.setPicCount(picOrderCountLsb);
getClock().setPicCount(picOrderCountLsb);
return;
} else if (spsData.picOrderCountType == 2) {
picCountClock.setPicCount(frameNum);
getClock().setPicCount(frameNum);
return;
}
picCountClock.setIndex(picCountClock.getIndex());
clock.setIndex(clock.getIndex());
}
@VisibleForTesting
@ -99,10 +99,10 @@ public class AvcChunkPeeker extends NalChunkPeeker {
nalTypeOffset = seekNextNal(input, spsStart);
spsData = NalUnitUtil.parseSpsNalUnitPayload(buffer, spsStart, pos);
if (spsData.picOrderCountType == 0) {
picCountClock.setMaxPicCount(1 << spsData.picOrderCntLsbLength, 2);
getClock().setMaxPicCount(1 << spsData.picOrderCntLsbLength, 2);
} else if (spsData.picOrderCountType == 2) {
//Plus one because we double the frame number
picCountClock.setMaxPicCount(1 << spsData.frameNumLength, 1);
getClock().setMaxPicCount(1 << spsData.frameNumLength, 1);
}
if (spsData.pixelWidthHeightRatio != pixelWidthHeightRatio) {
pixelWidthHeightRatio = spsData.pixelWidthHeightRatio;
@ -124,7 +124,7 @@ public class AvcChunkPeeker extends NalChunkPeeker {
updatePicCountClock(nalTypeOffset);
return;
case NAL_TYPE_IDR:
picCountClock.syncIndexes();
getClock().syncIndexes();
return;
case NAL_TYPE_AUD:
case NAL_TYPE_SEI:

View file

@ -103,7 +103,7 @@ public class AviExtractor implements Extractor {
@VisibleForTesting
static final int STATE_READ_IDX1 = 2;
@VisibleForTesting
static final int STATE_READ_SAMPLES = 3;
static final int STATE_READ_CHUNKS = 3;
@VisibleForTesting
static final int STATE_SEEK_START = 4;
@ -127,9 +127,9 @@ public class AviExtractor implements Extractor {
private AviHeaderBox aviHeader;
private long durationUs = C.TIME_UNSET;
/**
* AviTracks by StreamId
* ChunkHandlers by StreamId
*/
private AviTrack[] aviTracks = new AviTrack[0];
private ChunkHandler[] chunkHandlers = new ChunkHandler[0];
//At the start of the movi tag
private long moviOffset;
private long moviEnd;
@ -137,7 +137,7 @@ public class AviExtractor implements Extractor {
AviSeekMap aviSeekMap;
//Set if a chunk is only partially read
private transient AviTrack chunkHandler;
private transient ChunkHandler chunkHandler;
/**
*
@ -218,7 +218,7 @@ public class AviExtractor implements Extractor {
}
@VisibleForTesting
AviTrack parseStream(final ListBox streamList, int streamId) {
ChunkHandler parseStream(final ListBox streamList, int streamId) {
final StreamHeaderBox streamHeader = streamList.getChild(StreamHeaderBox.class);
final StreamFormatBox streamFormat = streamList.getChild(StreamFormatBox.class);
if (streamHeader == null) {
@ -243,7 +243,8 @@ public class AviExtractor implements Extractor {
if (streamName != null) {
builder.setLabel(streamName.getName());
}
final AviTrack aviTrack;
final ChunkClock clock = new ChunkClock(durationUs, length);
final ChunkHandler chunkHandler;
if (streamHeader.isVideo()) {
final VideoFormat videoFormat = streamFormat.getVideoFormat();
final String mimeType = videoFormat.getMimeType();
@ -257,17 +258,12 @@ public class AviExtractor implements Extractor {
builder.setFrameRate(streamHeader.getFrameRate());
builder.setSampleMimeType(mimeType);
final LinearClock clock = new LinearClock(durationUs, length);
aviTrack = new AviTrack(streamId, AviTrack.CHUNK_TYPE_VIDEO, clock, trackOutput);
if (MimeTypes.VIDEO_H264.equals(mimeType)) {
final AvcChunkPeeker avcChunkPeeker = new AvcChunkPeeker(builder, trackOutput, clock);
aviTrack.setClock(avcChunkPeeker.getClock());
aviTrack.setChunkPeeker(avcChunkPeeker);
chunkHandler = new AvcChunkHandler(streamId, trackOutput, clock, builder);
} else if (MimeTypes.VIDEO_MP4V.equals(mimeType)) {
chunkHandler = new Mp4vChunkHandler(streamId, trackOutput, clock, builder);
} else {
if (MimeTypes.VIDEO_MP4V.equals(mimeType)) {
aviTrack.setChunkPeeker(new Mp4vChunkPeeker(builder, trackOutput));
}
chunkHandler = new ChunkHandler(streamId, ChunkHandler.TYPE_VIDEO, trackOutput, clock);
}
trackOutput.format(builder.build());
this.durationUs = durationUs;
@ -294,13 +290,18 @@ public class AviExtractor implements Extractor {
builder.setInitializationData(Collections.singletonList(audioFormat.getCodecData()));
}
trackOutput.format(builder.build());
aviTrack = new AviTrack(streamId, AviTrack.CHUNK_TYPE_AUDIO,
new LinearClock(durationUs, length), trackOutput);
aviTrack.setKeyFrames(AviTrack.ALL_KEY_FRAMES);
if (MimeTypes.AUDIO_MPEG.equals(mimeType)) {
chunkHandler = new Mp3ChunkHandler(streamId, trackOutput, clock,
audioFormat.getSamplesPerSecond());
} else {
chunkHandler = new ChunkHandler(streamId, ChunkHandler.TYPE_AUDIO,
trackOutput, clock);
}
chunkHandler.setKeyFrames(ChunkHandler.ALL_KEY_FRAMES);
}else {
aviTrack = null;
chunkHandler = null;
}
return aviTrack;
return chunkHandler;
}
private int readTracks(ExtractorInput input) throws IOException {
@ -312,7 +313,7 @@ public class AviExtractor implements Extractor {
if (aviHeader == null) {
throw new IOException("AviHeader not found");
}
aviTracks = new AviTrack[aviHeader.getStreams()];
chunkHandlers = new ChunkHandler[aviHeader.getStreams()];
//This is usually wrong, so it will be overwritten by video if present
durationUs = aviHeader.getTotalFrames() * (long)aviHeader.getMicroSecPerFrame();
@ -320,7 +321,7 @@ public class AviExtractor implements Extractor {
for (Box box : headerList.getChildren()) {
if (box instanceof ListBox && ((ListBox) box).getListType() == ListBox.TYPE_STRL) {
final ListBox streamList = (ListBox) box;
aviTracks[streamId] = parseStream(streamList, streamId);
chunkHandlers[streamId] = parseStream(streamList, streamId);
streamId++;
}
}
@ -356,32 +357,32 @@ public class AviExtractor implements Extractor {
}
@VisibleForTesting
AviTrack getVideoTrack() {
for (@Nullable AviTrack aviTrack : aviTracks) {
if (aviTrack != null && aviTrack.isVideo()) {
return aviTrack;
ChunkHandler getVideoTrack() {
for (@Nullable ChunkHandler chunkHandler : chunkHandlers) {
if (chunkHandler != null && chunkHandler.isVideo()) {
return chunkHandler;
}
}
return null;
}
void fixTimings(final int[] keyFrameCounts, final long videoDuration) {
for (final AviTrack aviTrack : aviTracks) {
if (aviTrack != null) {
if (aviTrack.isAudio()) {
final long durationUs = aviTrack.getClock().durationUs;
i("Audio #" + aviTrack.getId() + " chunks: " + aviTrack.chunks + " us=" + durationUs +
" size=" + aviTrack.size);
final LinearClock linearClock = aviTrack.getClock();
for (final ChunkHandler chunkHandler : chunkHandlers) {
if (chunkHandler != null) {
if (chunkHandler.isAudio()) {
final long durationUs = chunkHandler.getClock().durationUs;
i("Audio #" + chunkHandler.getId() + " chunks: " + chunkHandler.chunks + " us=" + durationUs +
" size=" + chunkHandler.size);
final ChunkClock linearClock = chunkHandler.getClock();
//If the audio track duration is off from the video by >5 % recalc using video
if ((durationUs - videoDuration) / (float)videoDuration > .05f) {
w("Audio #" + aviTrack.getId() + " duration is off using videoDuration");
w("Audio #" + chunkHandler.getId() + " duration is off using videoDuration");
linearClock.setDuration(videoDuration);
}
linearClock.setLength(aviTrack.chunks);
if (aviTrack.chunks != keyFrameCounts[aviTrack.getId()]) {
w("Audio is not all key frames chunks=" + aviTrack.chunks + " keyFrames=" +
keyFrameCounts[aviTrack.getId()]);
linearClock.setChunks(chunkHandler.chunks);
if (chunkHandler.chunks != keyFrameCounts[chunkHandler.getId()]) {
w("Audio is not all key frames chunks=" + chunkHandler.chunks + " keyFrames=" +
keyFrameCounts[chunkHandler.getId()]);
}
}
}
@ -392,7 +393,7 @@ public class AviExtractor implements Extractor {
* Reads the index and sets the keyFrames and creates the SeekMap
*/
void readIdx1(ExtractorInput input, int remaining) throws IOException {
final AviTrack videoTrack = getVideoTrack();
final ChunkHandler videoTrack = getVideoTrack();
if (videoTrack == null) {
output.seekMap(new SeekMap.Unseekable(getDuration()));
w("No video track found");
@ -412,8 +413,8 @@ public class AviExtractor implements Extractor {
//These are ints/2
final UnboundedIntArray keyFrameOffsetsDiv2 = new UnboundedIntArray();
final int[] keyFrameCounts = new int[aviTracks.length];
final UnboundedIntArray[] seekIndexes = new UnboundedIntArray[aviTracks.length];
final int[] keyFrameCounts = new int[chunkHandlers.length];
final UnboundedIntArray[] seekIndexes = new UnboundedIntArray[chunkHandlers.length];
for (int i=0;i<seekIndexes.length;i++) {
seekIndexes[i] = new UnboundedIntArray();
}
@ -435,8 +436,8 @@ public class AviExtractor implements Extractor {
count += 1;
}
tagMap.put(chunkId, count);
final AviTrack aviTrack = getAviTrack(chunkId);
if (aviTrack == null) {
final ChunkHandler chunkHandler = getChunkHandler(chunkId);
if (chunkHandler == null) {
if (chunkId != AviExtractor.REC_) {
w("Unknown Track Type: " + toString(chunkId));
}
@ -447,26 +448,26 @@ public class AviExtractor implements Extractor {
final int offset = indexByteBuffer.getInt();
final int size = indexByteBuffer.getInt();
if ((flags & AVIIF_KEYFRAME) == AVIIF_KEYFRAME) {
if (aviTrack.isVideo()) {
if (chunkHandler.isVideo()) {
int indexSize = seekIndexes[videoId].getSize();
if (indexSize == 0 || aviTrack.chunks - seekIndexes[videoId].get(indexSize - 1) >= chunksPerKeyFrame) {
if (indexSize == 0 || chunkHandler.chunks - seekIndexes[videoId].get(indexSize - 1) >= chunksPerKeyFrame) {
keyFrameOffsetsDiv2.add(offset / 2);
for (AviTrack seekTrack : aviTracks) {
for (ChunkHandler seekTrack : chunkHandlers) {
if (seekTrack != null) {
seekIndexes[seekTrack.getId()].add(seekTrack.chunks);
}
}
}
}
keyFrameCounts[aviTrack.getId()]++;
keyFrameCounts[chunkHandler.getId()]++;
}
aviTrack.chunks++;
aviTrack.size+=size;
chunkHandler.chunks++;
chunkHandler.size+=size;
}
indexByteBuffer.compact();
}
if (videoTrack.chunks == keyFrameCounts[videoTrack.getId()]) {
videoTrack.setKeyFrames(AviTrack.ALL_KEY_FRAMES);
videoTrack.setKeyFrames(ChunkHandler.ALL_KEY_FRAMES);
} else {
videoTrack.setKeyFrames(seekIndexes[videoId].getArray());
}
@ -485,16 +486,16 @@ public class AviExtractor implements Extractor {
@Nullable
@VisibleForTesting
AviTrack getAviTrack(int chunkId) {
for (AviTrack aviTrack : aviTracks) {
if (aviTrack != null && aviTrack.handlesChunkId(chunkId)) {
return aviTrack;
ChunkHandler getChunkHandler(int chunkId) {
for (ChunkHandler chunkHandler : chunkHandlers) {
if (chunkHandler != null && chunkHandler.handlesChunkId(chunkId)) {
return chunkHandler;
}
}
return null;
}
int readSamples(@NonNull ExtractorInput input) throws IOException {
int readChunks(@NonNull ExtractorInput input) throws IOException {
if (chunkHandler != null) {
if (chunkHandler.resume(input)) {
chunkHandler = null;
@ -527,21 +528,21 @@ public class AviExtractor implements Extractor {
alignInput(input);
return RESULT_CONTINUE;
}
final AviTrack aviTrack = getAviTrack(chunkId);
if (aviTrack == null) {
final ChunkHandler chunkHandler = getChunkHandler(chunkId);
if (chunkHandler == null) {
input.skipFully(size);
alignInput(input);
w("Unknown tag=" + toString(chunkId) + " pos=" + (input.getPosition() - 8)
+ " size=" + size + " moviEnd=" + moviEnd);
return RESULT_CONTINUE;
}
if (aviTrack.newChunk(chunkId, size, input)) {
if (chunkHandler.newChunk(chunkId, size, input)) {
alignInput(input);
} else {
chunkHandler = aviTrack;
this.chunkHandler = chunkHandler;
}
}
if (input.getPosition() == input.getLength()) {
if (input.getPosition() >= moviEnd) {
return C.RESULT_END_OF_INPUT;
}
return RESULT_CONTINUE;
@ -550,10 +551,10 @@ public class AviExtractor implements Extractor {
@Override
public int read(@NonNull ExtractorInput input, @NonNull PositionHolder seekPosition) throws IOException {
switch (state) {
case STATE_READ_SAMPLES:
return readSamples(input);
case STATE_READ_CHUNKS:
return readChunks(input);
case STATE_SEEK_START:
state = STATE_READ_SAMPLES;
state = STATE_READ_CHUNKS;
seekPosition.position = moviOffset + 4;
return RESULT_SEEK;
case STATE_READ_TRACKS:
@ -573,7 +574,7 @@ public class AviExtractor implements Extractor {
output.seekMap(new SeekMap.Unseekable(getDuration()));
}
seekPosition.position = moviOffset + 4;
state = STATE_READ_SAMPLES;
state = STATE_READ_CHUNKS;
return RESULT_SEEK;
}
}
@ -591,15 +592,15 @@ public class AviExtractor implements Extractor {
}
} else {
if (aviSeekMap != null) {
aviSeekMap.setFrames(position, timeUs, aviTracks);
aviSeekMap.setFrames(position, timeUs, chunkHandlers);
}
}
}
void resetClocks() {
for (@Nullable AviTrack aviTrack : aviTracks) {
if (aviTrack != null) {
aviTrack.getClock().setIndex(0);
for (@Nullable ChunkHandler chunkHandler : chunkHandlers) {
if (chunkHandler != null) {
chunkHandler.getClock().setIndex(0);
}
}
}
@ -610,8 +611,8 @@ public class AviExtractor implements Extractor {
}
@VisibleForTesting
void setAviTracks(AviTrack[] aviTracks) {
this.aviTracks = aviTracks;
void setChunkHandlers(ChunkHandler[] chunkHandlers) {
this.chunkHandlers = chunkHandlers;
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
@ -626,13 +627,13 @@ public class AviExtractor implements Extractor {
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
AviTrack getChunkHandler() {
ChunkHandler getChunkHandler() {
return chunkHandler;
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
void setChunkHandler(final AviTrack aviTrack) {
chunkHandler = aviTrack;
void setChunkHandler(final ChunkHandler chunkHandler) {
this.chunkHandler = chunkHandler;
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)

View file

@ -99,17 +99,18 @@ public class AviSeekMap implements SeekMap {
//Log.d(AviExtractor.TAG, "SeekPoint: us=" + outUs + " pos=" + position);
}
public void setFrames(final long position, final long timeUs, final AviTrack[] aviTracks) {
public void setFrames(final long position, final long timeUs, final ChunkHandler[] chunkHandlers) {
final int index = Arrays.binarySearch(keyFrameOffsetsDiv2, (int)((position - seekOffset) / 2));
if (index < 0) {
throw new IllegalArgumentException("Position: " + position);
}
for (int i=0;i<aviTracks.length;i++) {
final AviTrack aviTrack = aviTracks[i];
final LinearClock clock = aviTrack.getClock();
clock.setIndex(seekIndexes[i][index]);
// Log.d(AviExtractor.TAG, "Frame: " + (aviTrack.isVideo()? 'V' : 'A') + " us=" + clock.getUs() + " frame=" + clock.getIndex() + " key=" + aviTrack.isKeyFrame());
for (int i=0;i<chunkHandlers.length;i++) {
final ChunkHandler chunkHandler = chunkHandlers[i];
if (chunkHandler != null) {
// Log.d(AviExtractor.TAG, "Frame: " + (chunkHandler.isVideo()? 'V' : 'A') + " us=" + clock.getUs() + " frame=" + clock.getIndex() + " key=" + chunkHandler.isKeyFrame());
chunkHandler.setIndex(seekIndexes[i][index]);
}
}
}
}

View file

@ -18,23 +18,23 @@ package com.google.android.exoplayer2.extractor.avi;
/**
* A clock that is linearly derived from the current chunk index of a given stream
*/
public class LinearClock {
public class ChunkClock {
long durationUs;
int length;
int chunks;
int index;
public LinearClock(long durationUs, int length) {
public ChunkClock(long durationUs, int chunks) {
this.durationUs = durationUs;
this.length = length;
this.chunks = chunks;
}
public void setDuration(long durationUs) {
this.durationUs = durationUs;
}
public void setLength(int length) {
this.length = length;
public void setChunks(int length) {
this.chunks = length;
}
public int getIndex() {
@ -55,6 +55,6 @@ public class LinearClock {
long getUs(int index) {
//Doing this the hard way lessens round errors
return durationUs * index / length;
return durationUs * index / chunks;
}
}

View file

@ -16,11 +16,11 @@
package com.google.android.exoplayer2.extractor.avi;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.util.Log;
import java.io.IOException;
import java.util.Arrays;
@ -28,13 +28,13 @@ import java.util.Arrays;
* Collection of info about a track.
* This acts a bridge between AVI and ExoPlayer structures
*/
public class AviTrack {
public class ChunkHandler {
public static final int[] ALL_KEY_FRAMES = new int[0];
public static int CHUNK_TYPE_VIDEO = ('d' << 16) | ('c' << 24);
public static int CHUNK_TYPE_AUDIO = ('w' << 16) | ('b' << 24);
public static int TYPE_VIDEO = ('d' << 16) | ('c' << 24);
public static int TYPE_AUDIO = ('w' << 16) | ('b' << 24);
@NonNull
LinearClock clock;
ChunkClock clock;
@NonNull
final TrackOutput trackOutput;
@ -42,9 +42,6 @@ public class AviTrack {
final int chunkId;
final int chunkIdAlt;
@Nullable
ChunkPeeker chunkPeeker;
int chunks;
int size;
@ -63,8 +60,7 @@ public class AviTrack {
return ('0' + tens) | (('0' + ones) << 8);
}
AviTrack(int id, int chunkType, @NonNull LinearClock clock,
@NonNull TrackOutput trackOutput) {
ChunkHandler(int id, int chunkType, @NonNull TrackOutput trackOutput, @NonNull ChunkClock clock) {
this.chunkId = getChunkIdLower(id) | chunkType;
this.clock = clock;
this.trackOutput = trackOutput;
@ -80,18 +76,14 @@ public class AviTrack {
}
@NonNull
public LinearClock getClock() {
public ChunkClock getClock() {
return clock;
}
public void setClock(@NonNull LinearClock clock) {
public void setClock(@NonNull ChunkClock clock) {
this.clock = clock;
}
public void setChunkPeeker(ChunkPeeker chunkPeeker) {
this.chunkPeeker = chunkPeeker;
}
/**
*
* @param keyFrames null means all key frames
@ -105,17 +97,14 @@ public class AviTrack {
}
public boolean isVideo() {
return (chunkId & CHUNK_TYPE_VIDEO) == CHUNK_TYPE_VIDEO;
return (chunkId & TYPE_VIDEO) == TYPE_VIDEO;
}
public boolean isAudio() {
return (chunkId & CHUNK_TYPE_AUDIO) == CHUNK_TYPE_AUDIO;
return (chunkId & TYPE_AUDIO) == TYPE_AUDIO;
}
public boolean newChunk(int tag, int size, ExtractorInput input) throws IOException {
if (chunkPeeker != null) {
chunkPeeker.peek(input, size);
}
final int remaining = size - trackOutput.sampleData(input, size, false);
if (remaining == 0) {
done(size);
@ -152,7 +141,7 @@ public class AviTrack {
trackOutput.sampleMetadata(
clock.getUs(), (isKeyFrame() ? C.BUFFER_FLAG_KEY_FRAME : 0), size, 0, null);
}
final LinearClock clock = getClock();
final ChunkClock clock = getClock();
//Log.d(AviExtractor.TAG, "Frame: " + (isVideo()? 'V' : 'A') + " us=" + clock.getUs() + " size=" + size + " frame=" + clock.getIndex() + " key=" + isKeyFrame());
clock.advance();
}
@ -160,4 +149,11 @@ public class AviTrack {
public int getId() {
return ((chunkId >> 8) & 0xf) + ( chunkId & 0xf) * 10;
}
/**
* A seek occurred
*/
public void setIndex(int index) {
getClock().setIndex(index);
}
}

View file

@ -1,26 +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 com.google.android.exoplayer2.extractor.ExtractorInput;
import java.io.IOException;
/**
* Peeks for import data in the chunk stream.
*/
public interface ChunkPeeker {
void peek(ExtractorInput input, final int size) throws IOException;
}

View file

@ -0,0 +1,133 @@
/*
* 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 androidx.annotation.NonNull;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.audio.MpegAudioUtil;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.ParsableByteArray;
import java.io.IOException;
public class Mp3ChunkHandler extends ChunkHandler {
private final MpegAudioUtil.Header header = new MpegAudioUtil.Header();
private final ParsableByteArray scratch = new ParsableByteArray(0);
private final int fps;
private int frameRemaining;
private long us = 0L;
Mp3ChunkHandler(int id, @NonNull TrackOutput trackOutput, @NonNull ChunkClock clock, int fps) {
super(id, TYPE_AUDIO, trackOutput, clock);
this.fps = fps;
}
@Override
public boolean newChunk(int tag, int size, ExtractorInput input) throws IOException {
if (size == 0) {
clock.advance();
syncUs();
//Log.d(AviExtractor.TAG, "Blank Frame: us=" + us);
return true;
}
chunkRemaining = size;
if (process(input)) {
//If we scratch is the entire buffer, we didn't find a MP3 header, so just dump the chunk
if (scratch.limit() == size) {
scratch.setPosition(0);
trackOutput.sampleData(scratch, size);
scratch.reset(0);
}
clock.advance();
return true;
}
return false;
}
@Override
boolean resume(ExtractorInput input) throws IOException {
if (process(input)) {
clock.advance();
return true;
}
return false;
}
int readScratch(ExtractorInput input, int bytes) throws IOException {
final int toRead = Math.min(bytes, chunkRemaining);
final int read = input.read(scratch.getData(), scratch.limit(), toRead);
if (read == C.RESULT_END_OF_INPUT) {
return read;
}
chunkRemaining -= read;
scratch.setLimit(scratch.limit() + read);
return read;
}
private boolean findFrame(ExtractorInput input) throws IOException {
scratch.reset(0);
scratch.ensureCapacity(scratch.limit() + chunkRemaining);
int toRead = 4;
while (chunkRemaining > 0 && readScratch(input, toRead) != C.RESULT_END_OF_INPUT) {
readScratch(input, toRead);
while (scratch.bytesLeft() >= 4) {
if (header.setForHeaderData(scratch.readInt())) {
scratch.skipBytes(-4);
return true;
}
scratch.skipBytes(-3);
}
toRead = Math.min(chunkRemaining, 128);
}
return false;
}
private boolean process(ExtractorInput input) throws IOException {
if (frameRemaining == 0) {
if (findFrame(input)) {
final int scratchBytes = scratch.bytesLeft();
trackOutput.sampleData(scratch, scratchBytes);
frameRemaining = header.frameSize - scratchBytes;
} else {
return chunkRemaining == 0;
}
}
final int bytes = trackOutput.sampleData(input, Math.min(frameRemaining, chunkRemaining), false);
if (bytes == C.RESULT_END_OF_INPUT) {
return true;
}
frameRemaining -= bytes;
if (frameRemaining == 0) {
trackOutput.sampleMetadata(us, C.BUFFER_FLAG_KEY_FRAME, header.frameSize, 0, null);
//Log.d(AviExtractor.TAG, "MP3: us=" + us);
us += header.samplesPerFrame * C.MICROS_PER_SECOND / fps;
}
chunkRemaining -= bytes;
return chunkRemaining == 0;
}
@Override
public void setIndex(int index) {
super.setIndex(index);
syncUs();
}
private void syncUs() {
us = clock.getUs();
frameRemaining = 0;
}
}

View file

@ -26,7 +26,7 @@ import java.io.IOException;
/**
* Peeks an MP4V stream looking for pixelWidthHeightRatio data
*/
public class Mp4vChunkPeeker extends NalChunkPeeker {
public class Mp4vChunkHandler extends NalChunkPeeker {
@VisibleForTesting
static final byte SEQUENCE_START_CODE = (byte)0xb0;
@VisibleForTesting
@ -36,15 +36,14 @@ public class Mp4vChunkPeeker extends NalChunkPeeker {
static final int Extended_PAR = 0xf;
private final Format.Builder formatBuilder;
private final TrackOutput trackOutput;
@VisibleForTesting()
float pixelWidthHeightRatio = 1f;
public Mp4vChunkPeeker(@NonNull Format.Builder formatBuilder, @NonNull TrackOutput trackOutput) {
super(5);
public Mp4vChunkHandler(int id, @NonNull TrackOutput trackOutput,
@NonNull ChunkClock clock, @NonNull Format.Builder formatBuilder) {
super(id, trackOutput, clock, 5);
this.formatBuilder = formatBuilder;
this.trackOutput = trackOutput;
}
@Override

View file

@ -15,7 +15,9 @@
*/
package com.google.android.exoplayer2.extractor.avi;
import androidx.annotation.NonNull;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.TrackOutput;
import java.io.IOException;
import java.util.Arrays;
@ -23,7 +25,7 @@ import java.util.Arrays;
* Generic base class for NAL (0x00 0x00 0x01) chunk headers
* Theses are used by AVC and MP4V (XVID)
*/
public abstract class NalChunkPeeker implements ChunkPeeker {
public abstract class NalChunkPeeker extends ChunkHandler {
private static final int SEEK_PEEK_SIZE = 256;
private final int peekSize;
@ -31,6 +33,15 @@ public abstract class NalChunkPeeker implements ChunkPeeker {
transient byte[] buffer;
transient int pos;
NalChunkPeeker(int id, @NonNull TrackOutput trackOutput,
@NonNull ChunkClock clock, int peakSize) {
super(id, TYPE_VIDEO, trackOutput, clock);
if (peakSize < 5) {
throw new IllegalArgumentException("Peak size must at least be 5");
}
this.peekSize = peakSize;
}
abstract void processChunk(ExtractorInput input, int nalTypeOffset) throws IOException;
/**
@ -100,15 +111,13 @@ public abstract class NalChunkPeeker implements ChunkPeeker {
return -1;
}
public NalChunkPeeker(int peakSize) {
if (peakSize < 5) {
throw new IllegalArgumentException("Peak size must at least be 5");
}
this.peekSize = peakSize;
}
abstract boolean skip(byte nalType);
public boolean newChunk(int tag, int size, ExtractorInput input) throws IOException {
peek(input, size);
return super.newChunk(tag, size, input);
}
public void peek(ExtractorInput input, final int size) throws IOException {
buffer = new byte[peekSize];
if (!input.peekFully(buffer, 0, peekSize, true)) {

View file

@ -20,7 +20,7 @@ import androidx.annotation.VisibleForTesting;
/**
* Properly calculates the frame time for H264 frames using PicCount
*/
public class PicCountClock extends LinearClock {
public class PicCountClock extends ChunkClock {
//The frame as a calculated from the picCount
private int picIndex;
private int lastPicCount;

View file

@ -39,13 +39,13 @@ public class AvcChunkPeekerTest {
(byte)0xFE,(byte)0x9E,0x10,00,00};
private FakeTrackOutput fakeTrackOutput;
private AvcChunkPeeker avcChunkPeeker;
private AvcChunkHandler avcChunkPeeker;
@Before
public void before() {
fakeTrackOutput = new FakeTrackOutput(false);
avcChunkPeeker = new AvcChunkPeeker(FORMAT_BUILDER_AVC, fakeTrackOutput,
new LinearClock(10_000_000L, 24 * 10));
avcChunkPeeker = new AvcChunkHandler(0, fakeTrackOutput,
new ChunkClock(10_000_000L, 24 * 10), FORMAT_BUILDER_AVC);
}
private void peekStreamHeader() throws IOException {

View file

@ -108,26 +108,26 @@ public class AviExtractorRoboTest {
Assert.assertEquals(AviExtractor.STATE_FIND_MOVI, aviExtractor.state);
final AviTrack aviTrack = aviExtractor.getVideoTrack();
Assert.assertEquals(aviTrack.getClock().durationUs, streamHeaderBox.getDurationUs());
final ChunkHandler chunkHandler = aviExtractor.getVideoTrack();
Assert.assertEquals(chunkHandler.getClock().durationUs, streamHeaderBox.getDurationUs());
}
@Test
public void readSamples_fragmentedChunk() throws IOException {
AviExtractor aviExtractor = AviExtractorTest.setupVideoAviExtractor();
final AviTrack aviTrack = aviExtractor.getVideoTrack();
final ChunkHandler chunkHandler = aviExtractor.getVideoTrack();
final int size = 24 + 16;
final ByteBuffer byteBuffer = AviExtractor.allocate(size + 8);
byteBuffer.putInt(aviTrack.chunkId);
byteBuffer.putInt(chunkHandler.chunkId);
byteBuffer.putInt(size);
final ExtractorInput chunk = new FakeExtractorInput.Builder().setData(byteBuffer.array()).
setSimulatePartialReads(true).build();
Assert.assertEquals(Extractor.RESULT_CONTINUE, aviExtractor.read(chunk, new PositionHolder()));
Assert.assertEquals(Extractor.RESULT_END_OF_INPUT, aviExtractor.read(chunk, new PositionHolder()));
Assert.assertEquals(Extractor.RESULT_CONTINUE, aviExtractor.read(chunk, new PositionHolder()));
final FakeTrackOutput fakeTrackOutput = (FakeTrackOutput) aviTrack.trackOutput;
final FakeTrackOutput fakeTrackOutput = (FakeTrackOutput) chunkHandler.trackOutput;
Assert.assertEquals(size, fakeTrackOutput.getSampleData(0).length);
}
}

View file

@ -165,7 +165,7 @@ public class AviExtractorTest {
Assert.assertEquals(1, AviExtractor.getStreamId('0' | ('1' << 8) | ('d' << 16) | ('c' << 24)));
}
private void assertIdx1(AviSeekMap aviSeekMap, AviTrack videoTrack, int keyFrames,
private void assertIdx1(AviSeekMap aviSeekMap, ChunkHandler videoTrack, int keyFrames,
int keyFrameRate) {
Assert.assertEquals(keyFrames, videoTrack.keyFrames.length);
@ -192,9 +192,9 @@ public class AviExtractorTest {
final int keyFrameRate = 3 * DataHelper.FPS; // Keyframe every 3 seconds
final int keyFrames = secs * DataHelper.FPS / keyFrameRate;
final ByteBuffer idx1 = DataHelper.getIndex(secs, keyFrameRate);
final AviTrack videoTrack = DataHelper.getVideoAviTrack(secs);
final AviTrack audioTrack = DataHelper.getAudioAviTrack(secs);
aviExtractor.setAviTracks(new AviTrack[]{videoTrack, audioTrack});
final ChunkHandler videoTrack = DataHelper.getVideoChunkHandler(secs);
final ChunkHandler audioTrack = DataHelper.getAudioChunkHandler(secs);
aviExtractor.setChunkHandlers(new ChunkHandler[]{videoTrack, audioTrack});
aviExtractor.setAviHeader(DataHelper.createAviHeaderBox());
aviExtractor.state = AviExtractor.STATE_READ_IDX1;
aviExtractor.setMovi(DataHelper.MOVI_OFFSET, 128*1024);
@ -213,7 +213,7 @@ public class AviExtractorTest {
final AviSeekMap aviSeekMap = aviExtractor.aviSeekMap;
assertIdx1(aviSeekMap, videoTrack, keyFrames, keyFrameRate);
Assert.assertEquals(AviExtractor.STATE_READ_SAMPLES, aviExtractor.state);
Assert.assertEquals(AviExtractor.STATE_READ_CHUNKS, aviExtractor.state);
Assert.assertEquals(DataHelper.MOVI_OFFSET + 4, positionHolder.position);
}
@ -225,8 +225,8 @@ public class AviExtractorTest {
final int secs = 9;
final int keyFrameRate = 3 * DataHelper.FPS; // Keyframe every 3 seconds
final ByteBuffer idx1 = DataHelper.getIndex(secs, keyFrameRate);
final AviTrack audioTrack = DataHelper.getAudioAviTrack(secs);
aviExtractor.setAviTracks(new AviTrack[]{audioTrack});
final ChunkHandler audioTrack = DataHelper.getAudioChunkHandler(secs);
aviExtractor.setChunkHandlers(new ChunkHandler[]{audioTrack});
final FakeExtractorInput fakeExtractorInput = new FakeExtractorInput.Builder()
.setData(idx1.array()).build();
@ -250,9 +250,9 @@ public class AviExtractorTest {
junk.putInt(0);
idx1.flip();
junk.put(idx1);
final AviTrack videoTrack = DataHelper.getVideoAviTrack(secs);
final AviTrack audioTrack = DataHelper.getAudioAviTrack(secs);
aviExtractor.setAviTracks(new AviTrack[]{videoTrack, audioTrack});
final ChunkHandler videoTrack = DataHelper.getVideoChunkHandler(secs);
final ChunkHandler audioTrack = DataHelper.getAudioChunkHandler(secs);
aviExtractor.setChunkHandlers(new ChunkHandler[]{videoTrack, audioTrack});
final FakeExtractorInput fakeExtractorInput = new FakeExtractorInput.Builder().
setData(junk.array()).build();
@ -268,16 +268,16 @@ public class AviExtractorTest {
aviExtractor.init(fakeExtractorOutput);
final int secs = 4;
final ByteBuffer idx1 = DataHelper.getIndex(secs, 1);
final AviTrack videoTrack = DataHelper.getVideoAviTrack(secs);
final AviTrack audioTrack = DataHelper.getAudioAviTrack(secs);
aviExtractor.setAviTracks(new AviTrack[]{videoTrack, audioTrack});
final ChunkHandler videoTrack = DataHelper.getVideoChunkHandler(secs);
final ChunkHandler audioTrack = DataHelper.getAudioChunkHandler(secs);
aviExtractor.setChunkHandlers(new ChunkHandler[]{videoTrack, audioTrack});
final FakeExtractorInput fakeExtractorInput = new FakeExtractorInput.Builder().
setData(idx1.array()).build();
aviExtractor.readIdx1(fakeExtractorInput, (int) fakeExtractorInput.getLength());
//We should be throttled to 2 key frame per second
Assert.assertSame(AviTrack.ALL_KEY_FRAMES, videoTrack.keyFrames);
Assert.assertSame(ChunkHandler.ALL_KEY_FRAMES, videoTrack.keyFrames);
}
@Test
@ -389,12 +389,12 @@ public class AviExtractorTest {
final FakeExtractorOutput fakeExtractorOutput = new FakeExtractorOutput();
aviExtractor.init(fakeExtractorOutput);
final AviTrack aviTrack = DataHelper.getVideoAviTrack(9);
aviExtractor.setAviTracks(new AviTrack[]{aviTrack});
final ChunkHandler chunkHandler = DataHelper.getVideoChunkHandler(9);
aviExtractor.setChunkHandlers(new ChunkHandler[]{chunkHandler});
final Format format = new Format.Builder().setSampleMimeType(MimeTypes.VIDEO_MP4V).build();
aviTrack.trackOutput.format(format);
chunkHandler.trackOutput.format(format);
aviExtractor.state = AviExtractor.STATE_READ_SAMPLES;
aviExtractor.state = AviExtractor.STATE_READ_CHUNKS;
aviExtractor.setMovi(DataHelper.MOVI_OFFSET, 128*1024);
return aviExtractor;
}
@ -403,9 +403,9 @@ public class AviExtractorTest {
public void readSamples_givenAtEndOfInput() throws IOException {
AviExtractor aviExtractor = setupVideoAviExtractor();
aviExtractor.setMovi(0, 0);
final AviTrack aviTrack = aviExtractor.getVideoTrack();
final ChunkHandler chunkHandler = aviExtractor.getVideoTrack();
final ByteBuffer byteBuffer = AviExtractor.allocate(32);
byteBuffer.putInt(aviTrack.chunkId);
byteBuffer.putInt(chunkHandler.chunkId);
byteBuffer.putInt(24);
final ExtractorInput input = new FakeExtractorInput.Builder().setData(byteBuffer.array()).build();
@ -415,47 +415,47 @@ public class AviExtractorTest {
@Test
public void readSamples_completeChunk() throws IOException {
AviExtractor aviExtractor = setupVideoAviExtractor();
final AviTrack aviTrack = aviExtractor.getVideoTrack();
final ChunkHandler chunkHandler = aviExtractor.getVideoTrack();
final ByteBuffer byteBuffer = AviExtractor.allocate(32);
byteBuffer.putInt(aviTrack.chunkId);
byteBuffer.putInt(chunkHandler.chunkId);
byteBuffer.putInt(24);
final ExtractorInput input = new FakeExtractorInput.Builder().setData(byteBuffer.array())
.build();
Assert.assertEquals(Extractor.RESULT_END_OF_INPUT, aviExtractor.read(input, new PositionHolder()));
Assert.assertEquals(Extractor.RESULT_CONTINUE, aviExtractor.read(input, new PositionHolder()));
final FakeTrackOutput fakeTrackOutput = (FakeTrackOutput) aviTrack.trackOutput;
final FakeTrackOutput fakeTrackOutput = (FakeTrackOutput) chunkHandler.trackOutput;
Assert.assertEquals(24, fakeTrackOutput.getSampleData(0).length);
}
@Test
public void readSamples_givenLeadingZeros() throws IOException {
AviExtractor aviExtractor = setupVideoAviExtractor();
final AviTrack aviTrack = aviExtractor.getVideoTrack();
final ChunkHandler chunkHandler = aviExtractor.getVideoTrack();
final ByteBuffer byteBuffer = AviExtractor.allocate(48);
byteBuffer.position(16);
byteBuffer.putInt(aviTrack.chunkId);
byteBuffer.putInt(chunkHandler.chunkId);
byteBuffer.putInt(24);
final ExtractorInput input = new FakeExtractorInput.Builder().setData(byteBuffer.array())
.build();
Assert.assertEquals(Extractor.RESULT_END_OF_INPUT, aviExtractor.read(input, new PositionHolder()));
Assert.assertEquals(Extractor.RESULT_CONTINUE, aviExtractor.read(input, new PositionHolder()));
final FakeTrackOutput fakeTrackOutput = (FakeTrackOutput) aviTrack.trackOutput;
final FakeTrackOutput fakeTrackOutput = (FakeTrackOutput) chunkHandler.trackOutput;
Assert.assertEquals(24, fakeTrackOutput.getSampleData(0).length);
}
@Test
public void seek_givenPosition0() throws IOException {
final AviExtractor aviExtractor = setupVideoAviExtractor();
final AviTrack aviTrack = aviExtractor.getVideoTrack();
aviExtractor.setChunkHandler(aviTrack);
aviTrack.getClock().setIndex(10);
final ChunkHandler chunkHandler = aviExtractor.getVideoTrack();
aviExtractor.setChunkHandler(chunkHandler);
chunkHandler.getClock().setIndex(10);
aviExtractor.seek(0L, 0L);
Assert.assertNull(aviExtractor.getChunkHandler());
Assert.assertEquals(0, aviTrack.getClock().getIndex());
Assert.assertEquals(0, chunkHandler.getClock().getIndex());
Assert.assertEquals(aviExtractor.state, AviExtractor.STATE_SEEK_START);
@ -470,18 +470,18 @@ public class AviExtractorTest {
final AviExtractor aviExtractor = setupVideoAviExtractor();
final AviSeekMap aviSeekMap = DataHelper.getAviSeekMap();
aviExtractor.aviSeekMap = aviSeekMap;
final AviTrack aviTrack = aviExtractor.getVideoTrack();
final ChunkHandler chunkHandler = aviExtractor.getVideoTrack();
final long position = DataHelper.MOVI_OFFSET + aviSeekMap.keyFrameOffsetsDiv2[1] * 2L;
aviExtractor.seek(position, 0L);
Assert.assertEquals(aviSeekMap.seekIndexes[aviTrack.getId()][1], aviTrack.getClock().getIndex());
Assert.assertEquals(aviSeekMap.seekIndexes[chunkHandler.getId()][1], chunkHandler.getClock().getIndex());
}
@Test
public void getAviTrack_givenListWithNull() {
public void getChunkHandler_givenListWithNull() {
final AviExtractor aviExtractor = new AviExtractor();
final AviTrack aviTrack = DataHelper.getAudioAviTrack(9);
aviExtractor.setAviTracks(new AviTrack[]{null, aviTrack});
Assert.assertSame(aviTrack, aviExtractor.getAviTrack(aviTrack.chunkId));
final ChunkHandler chunkHandler = DataHelper.getAudioChunkHandler(9);
aviExtractor.setChunkHandlers(new ChunkHandler[]{null, chunkHandler});
Assert.assertSame(chunkHandler, aviExtractor.getChunkHandler(chunkHandler.chunkId));
}
@Test

View file

@ -27,22 +27,22 @@ public class AviSeekMapTest {
final AviSeekMap aviSeekMap = DataHelper.getAviSeekMap();
final long position = aviSeekMap.keyFrameOffsetsDiv2[1] * 2L + aviSeekMap.seekOffset;
final int secs = 4;
final AviTrack[] aviTracks = new AviTrack[]{DataHelper.getVideoAviTrack(secs),
DataHelper.getAudioAviTrack(secs)};
final ChunkHandler[] chunkHandlers = new ChunkHandler[]{DataHelper.getVideoChunkHandler(secs),
DataHelper.getAudioChunkHandler(secs)};
aviSeekMap.setFrames(position, C.MICROS_PER_SECOND, aviTracks);
for (int i=0;i<aviTracks.length;i++) {
Assert.assertEquals(aviSeekMap.seekIndexes[i][1], aviTracks[i].getClock().getIndex());
aviSeekMap.setFrames(position, C.MICROS_PER_SECOND, chunkHandlers);
for (int i=0;i<chunkHandlers.length;i++) {
Assert.assertEquals(aviSeekMap.seekIndexes[i][1], chunkHandlers[i].getClock().getIndex());
}
}
@Test
public void setFrames_givenBadPosition() {
final AviSeekMap aviSeekMap = DataHelper.getAviSeekMap();
final AviTrack[] aviTracks = new AviTrack[2];
final ChunkHandler[] chunkHandlers = new ChunkHandler[2];
try {
aviSeekMap.setFrames(1L, C.MICROS_PER_SECOND, aviTracks);
aviSeekMap.setFrames(1L, C.MICROS_PER_SECOND, chunkHandlers);
Assert.fail();
} catch (IllegalArgumentException e) {
//Intentionally blank

View file

@ -18,13 +18,13 @@ package com.google.android.exoplayer2.extractor.avi;
import org.junit.Assert;
import org.junit.Test;
public class AviTrackTest {
public class ChunkHandlerTest {
@Test
public void setClock_givenLinearClock() {
final LinearClock linearClock = new LinearClock(1_000_000L, 30);
final AviTrack aviTrack = DataHelper.getVideoAviTrack(1);
aviTrack.setClock(linearClock);
final ChunkClock linearClock = new ChunkClock(1_000_000L, 30);
final ChunkHandler chunkHandler = DataHelper.getVideoChunkHandler(1);
chunkHandler.setClock(linearClock);
Assert.assertSame(linearClock, aviTrack.getClock());
Assert.assertSame(linearClock, chunkHandler.getClock());
}
}

View file

@ -17,7 +17,6 @@ package com.google.android.exoplayer2.extractor.avi;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.FakeTrackOutput;
import com.google.android.exoplayer2.testutil.TestUtil;
import java.io.IOException;
@ -104,18 +103,16 @@ public class DataHelper {
return byteBuffer;
}
public static AviTrack getVideoAviTrack(int sec) {
public static ChunkHandler getVideoChunkHandler(int sec) {
final FakeTrackOutput fakeTrackOutput = new FakeTrackOutput(false);
return new AviTrack(0, AviTrack.CHUNK_TYPE_VIDEO,
new LinearClock(sec * 1_000_000L, sec * FPS),
fakeTrackOutput);
return new ChunkHandler(0, ChunkHandler.TYPE_VIDEO, fakeTrackOutput,
new ChunkClock(sec * 1_000_000L, sec * FPS));
}
public static AviTrack getAudioAviTrack(int sec) {
public static ChunkHandler getAudioChunkHandler(int sec) {
final FakeTrackOutput fakeTrackOutput = new FakeTrackOutput(false);
return new AviTrack(AUDIO_ID, AviTrack.CHUNK_TYPE_AUDIO,
new LinearClock(sec * 1_000_000L, sec * FPS * AUDIO_PER_VIDEO),
fakeTrackOutput);
return new ChunkHandler(AUDIO_ID, ChunkHandler.TYPE_AUDIO, fakeTrackOutput,
new ChunkClock(sec * 1_000_000L, sec * FPS * AUDIO_PER_VIDEO));
}
public static AviSeekMap getAviSeekMap() {
@ -153,8 +150,8 @@ public class DataHelper {
*/
public static ByteBuffer getIndex(final int secs, final int keyFrameRate, int offset) {
final int videoFrames = secs * FPS;
final int videoChunkId = AviTrack.CHUNK_TYPE_VIDEO | AviTrack.getChunkIdLower(0);
final int audioChunkId = AviTrack.CHUNK_TYPE_AUDIO | AviTrack.getChunkIdLower(1);
final int videoChunkId = ChunkHandler.TYPE_VIDEO | ChunkHandler.getChunkIdLower(0);
final int audioChunkId = ChunkHandler.TYPE_AUDIO | ChunkHandler.getChunkIdLower(1);
final ByteBuffer byteBuffer = AviExtractor.allocate((videoFrames + videoFrames*AUDIO_PER_VIDEO) * 16);
for (int v=0;v<videoFrames;v++) {

View file

@ -24,7 +24,7 @@ import org.junit.Test;
public class LinearClockTest {
@Test
public void advance() {
final LinearClock linearClock = new LinearClock(1_000L, 10);
final ChunkClock linearClock = new ChunkClock(1_000L, 10);
linearClock.setIndex(2);
Assert.assertEquals(200, linearClock.getUs());
linearClock.advance();

View file

@ -16,12 +16,13 @@
package com.google.android.exoplayer2.extractor.avi;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.testutil.FakeTrackOutput;
import java.io.IOException;
public class MockNalChunkPeeker extends NalChunkPeeker {
private boolean skip;
public MockNalChunkPeeker(int peakSize, boolean skip) {
super(peakSize);
super(0, new FakeTrackOutput(false), new ChunkClock(1_000_000L, 24), peakSize);
this.skip = skip;
}

View file

@ -32,7 +32,7 @@ import org.junit.runner.RunWith;
public class Mp4vChunkPeekerTest {
private ByteBuffer makeSequence() {
return DataHelper.appendNal(AviExtractor.allocate(32),Mp4vChunkPeeker.SEQUENCE_START_CODE);
return DataHelper.appendNal(AviExtractor.allocate(32), Mp4vChunkHandler.SEQUENCE_START_CODE);
}
@Test
@ -42,7 +42,8 @@ public class Mp4vChunkPeekerTest {
final Format.Builder formatBuilder = new Format.Builder();
final FakeExtractorInput input = new FakeExtractorInput.Builder().setData(byteBuffer.array())
.build();
final Mp4vChunkPeeker mp4vChunkPeeker = new Mp4vChunkPeeker(formatBuilder, fakeTrackOutput);
final Mp4vChunkHandler mp4vChunkPeeker = new Mp4vChunkHandler(0, fakeTrackOutput,
new ChunkClock(1_000_000L, 24), formatBuilder);
mp4vChunkPeeker.peek(input, (int) input.getLength());
Assert.assertEquals(1f, mp4vChunkPeeker.pixelWidthHeightRatio, 0.01);
}
@ -54,7 +55,8 @@ public class Mp4vChunkPeekerTest {
final Context context = ApplicationProvider.getApplicationContext();
final byte[] bytes = TestUtil.getByteArray(context, "extractordumps/avi/mp4v_sequence.dump");
final FakeExtractorInput input = new FakeExtractorInput.Builder().setData(bytes).build();
final Mp4vChunkPeeker mp4vChunkPeeker = new Mp4vChunkPeeker(formatBuilder, fakeTrackOutput);
final Mp4vChunkHandler mp4vChunkPeeker = new Mp4vChunkHandler(0, fakeTrackOutput,
new ChunkClock(1_000_000L, 24), formatBuilder);
mp4vChunkPeeker.peek(input, (int) input.getLength());
Assert.assertEquals(1.2121212, mp4vChunkPeeker.pixelWidthHeightRatio, 0.01);
@ -64,14 +66,14 @@ public class Mp4vChunkPeekerTest {
public void peek_givenCustomAspectRatio() throws IOException {
ByteBuffer byteBuffer = makeSequence();
byteBuffer.putInt(0x5555);
DataHelper.appendNal(byteBuffer, (byte)Mp4vChunkPeeker.LAYER_START_CODE);
DataHelper.appendNal(byteBuffer, (byte) Mp4vChunkHandler.LAYER_START_CODE);
BitBuffer bitBuffer = new BitBuffer();
bitBuffer.push(false); //random_accessible_vol
bitBuffer.push(8, 8); //video_object_type_indication
bitBuffer.push(true); // is_object_layer_identifier
bitBuffer.push(7, 7); // video_object_layer_verid, video_object_layer_priority
bitBuffer.push(4, Mp4vChunkPeeker.Extended_PAR);
bitBuffer.push(4, Mp4vChunkHandler.Extended_PAR);
bitBuffer.push(8, 16);
bitBuffer.push(8, 9);
final byte bytes[] = bitBuffer.getBytes();
@ -81,7 +83,8 @@ public class Mp4vChunkPeekerTest {
final Format.Builder formatBuilder = new Format.Builder();
final FakeExtractorInput input = new FakeExtractorInput.Builder().setData(byteBuffer.array())
.build();
final Mp4vChunkPeeker mp4vChunkPeeker = new Mp4vChunkPeeker(formatBuilder, fakeTrackOutput);
final Mp4vChunkHandler mp4vChunkPeeker = new Mp4vChunkHandler(0, fakeTrackOutput,
new ChunkClock(1_000_000L, 24), formatBuilder);
mp4vChunkPeeker.peek(input, (int) input.getLength());
Assert.assertEquals(16f/9f, mp4vChunkPeeker.pixelWidthHeightRatio, 0.01);
}