Commit 1ca382d1 by Marc Baechinger

Merge pull request #9915 from dburckh:avi

PiperOrigin-RevId: 455094147
parents 8b65c6a0 0b629e4b
Showing with 840 additions and 3 deletions
......@@ -42,7 +42,7 @@ public final class FileTypes {
@Target(TYPE_USE)
@IntDef({
UNKNOWN, AC3, AC4, ADTS, AMR, FLAC, FLV, MATROSKA, MP3, MP4, OGG, PS, TS, WAV, WEBVTT, JPEG,
MIDI
MIDI, AVI
})
public @interface Type {}
/** Unknown file type. */
......@@ -79,6 +79,8 @@ public final class FileTypes {
public static final int JPEG = 14;
/** File type for the MIDI format. */
public static final int MIDI = 15;
/** File type for the AVI format. */
public static final int AVI = 16;
@VisibleForTesting /* package */ static final String HEADER_CONTENT_TYPE = "Content-Type";
......@@ -114,6 +116,7 @@ public final class FileTypes {
private static final String EXTENSION_WEBVTT = ".webvtt";
private static final String EXTENSION_JPG = ".jpg";
private static final String EXTENSION_JPEG = ".jpeg";
private static final String EXTENSION_AVI = ".avi";
private FileTypes() {}
......@@ -177,6 +180,8 @@ public final class FileTypes {
return FileTypes.WEBVTT;
case MimeTypes.IMAGE_JPEG:
return FileTypes.JPEG;
case MimeTypes.VIDEO_AVI:
return FileTypes.AVI;
default:
return FileTypes.UNKNOWN;
}
......@@ -242,6 +247,8 @@ public final class FileTypes {
return FileTypes.WEBVTT;
} else if (filename.endsWith(EXTENSION_JPG) || filename.endsWith(EXTENSION_JPEG)) {
return FileTypes.JPEG;
} else if (filename.endsWith(EXTENSION_AVI)) {
return FileTypes.AVI;
} else {
return FileTypes.UNKNOWN;
}
......
......@@ -54,6 +54,10 @@ public final class MimeTypes {
public static final String VIDEO_FLV = BASE_TYPE_VIDEO + "/x-flv";
public static final String VIDEO_DOLBY_VISION = BASE_TYPE_VIDEO + "/dolby-vision";
public static final String VIDEO_OGG = BASE_TYPE_VIDEO + "/ogg";
public static final String VIDEO_AVI = BASE_TYPE_VIDEO + "/x-msvideo";
public static final String VIDEO_MJPEG = BASE_TYPE_VIDEO + "/mjpeg";
public static final String VIDEO_MP42 = BASE_TYPE_VIDEO + "/mp42";
public static final String VIDEO_MP43 = BASE_TYPE_VIDEO + "/mp43";
public static final String VIDEO_UNKNOWN = BASE_TYPE_VIDEO + "/x-unknown";
// audio/ MIME types
......
......@@ -24,6 +24,7 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.extractor.amr.AmrExtractor;
import com.google.android.exoplayer2.extractor.avi.AviExtractor;
import com.google.android.exoplayer2.extractor.flac.FlacExtractor;
import com.google.android.exoplayer2.extractor.flv.FlvExtractor;
import com.google.android.exoplayer2.extractor.jpeg.JpegExtractor;
......@@ -101,8 +102,11 @@ public final class DefaultExtractorsFactory implements ExtractorsFactory {
FileTypes.AC3,
FileTypes.AC4,
FileTypes.MP3,
FileTypes.JPEG,
// The following extractors are not part of the optimized ordering, and were appended
// without further analysis.
FileTypes.AVI,
FileTypes.MIDI,
FileTypes.JPEG,
};
private static final ExtensionLoader FLAC_EXTENSION_LOADER =
......@@ -307,7 +311,8 @@ public final class DefaultExtractorsFactory implements ExtractorsFactory {
@Override
public synchronized Extractor[] createExtractors(
Uri uri, Map<String, List<String>> responseHeaders) {
List<Extractor> extractors = new ArrayList<>(/* initialCapacity= */ 14);
List<Extractor> extractors =
new ArrayList<>(/* initialCapacity= */ DEFAULT_EXTRACTOR_ORDER.length);
@FileTypes.Type
int responseHeadersInferredFileType = inferFileTypeFromResponseHeaders(responseHeaders);
......@@ -410,6 +415,9 @@ public final class DefaultExtractorsFactory implements ExtractorsFactory {
extractors.add(midiExtractor);
}
break;
case FileTypes.AVI:
extractors.add(new AviExtractor());
break;
case FileTypes.WEBVTT:
case FileTypes.UNKNOWN:
default:
......
/*
* Copyright 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;
/**
* A chunk, as defined in the AVI spec.
*
* <p>See https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference.
*/
/* package */ interface AviChunk {
/** Returns the chunk type fourcc. */
int getType();
}
/*
* Copyright 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.util.ParsableByteArray;
/** Wrapper around the AVIMAINHEADER structure */
/* package */ final class AviMainHeaderChunk implements AviChunk {
private static final int AVIF_HAS_INDEX = 0x10;
public static AviMainHeaderChunk parseFrom(ParsableByteArray body) {
int microSecPerFrame = body.readLittleEndianInt();
body.skipBytes(8); // Skip dwMaxBytesPerSec (4 bytes), dwPaddingGranularity (4 bytes).
int flags = body.readLittleEndianInt();
int totalFrames = body.readLittleEndianInt();
body.skipBytes(4); // dwInitialFrames (4 bytes).
int streams = body.readLittleEndianInt();
body.skipBytes(12); // dwSuggestedBufferSize (4 bytes), dwWidth (4 bytes), dwHeight (4 bytes).
return new AviMainHeaderChunk(microSecPerFrame, flags, totalFrames, streams);
}
public final int frameDurationUs;
public final int flags;
public final int totalFrames;
public final int streams;
private AviMainHeaderChunk(int frameDurationUs, int flags, int totalFrames, int streams) {
this.frameDurationUs = frameDurationUs;
this.flags = flags;
this.totalFrames = totalFrames;
this.streams = streams;
}
@Override
public int getType() {
return AviExtractor.FOURCC_avih;
}
public boolean hasIndex() {
return (flags & AVIF_HAS_INDEX) == AVIF_HAS_INDEX;
}
}
/*
* Copyright 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.C;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util;
/** Parses and holds information from the AVISTREAMHEADER structure. */
/* package */ final class AviStreamHeaderChunk implements AviChunk {
private static final String TAG = "AviStreamHeaderChunk";
public static AviStreamHeaderChunk parseFrom(ParsableByteArray body) {
int streamType = body.readLittleEndianInt();
body.skipBytes(12); // fccHandler (4 bytes), dwFlags (4 bytes), wPriority (2 bytes),
// wLanguage (2 bytes).
int initialFrames = body.readLittleEndianInt();
int scale = body.readLittleEndianInt();
int rate = body.readLittleEndianInt();
body.skipBytes(4); // dwStart (4 bytes).
int length = body.readLittleEndianInt();
int suggestedBufferSize = body.readLittleEndianInt();
body.skipBytes(8); // dwQuality (4 bytes), dwSampleSize (4 bytes).
return new AviStreamHeaderChunk(
streamType, initialFrames, scale, rate, length, suggestedBufferSize);
}
public final int streamType;
public final int initialFrames;
public final int scale;
public final int rate;
public final int length;
public final int suggestedBufferSize;
private AviStreamHeaderChunk(
int streamType, int initialFrames, int scale, int rate, int length, int suggestedBufferSize) {
this.streamType = streamType;
this.initialFrames = initialFrames;
this.scale = scale;
this.rate = rate;
this.length = length;
this.suggestedBufferSize = suggestedBufferSize;
}
@Override
public int getType() {
return AviExtractor.FOURCC_strh;
}
public @C.TrackType int getTrackType() {
switch (streamType) {
case AviExtractor.FOURCC_auds:
return C.TRACK_TYPE_AUDIO;
case AviExtractor.FOURCC_vids:
return C.TRACK_TYPE_VIDEO;
case AviExtractor.FOURCC_txts:
return C.TRACK_TYPE_TEXT;
default:
Log.w(TAG, "Found unsupported streamType fourCC: " + Integer.toHexString(streamType));
return C.TRACK_TYPE_UNKNOWN;
}
}
public float getFrameRate() {
return rate / (float) scale;
}
public long getDurationUs() {
return Util.scaleLargeTimestamp(
/* timestamp= */ length,
/* multiplier= */ C.MICROS_PER_SECOND * scale,
/* divisor= */ rate);
}
}
/*
* Copyright 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 static java.lang.annotation.ElementType.TYPE_USE;
import androidx.annotation.IntDef;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.extractor.SeekPoint;
import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
/** Reads chunks holding sample data. */
/* package */ final class ChunkReader {
/** Parser states. */
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({
CHUNK_TYPE_VIDEO_COMPRESSED,
CHUNK_TYPE_VIDEO_UNCOMPRESSED,
CHUNK_TYPE_AUDIO,
})
private @interface ChunkType {}
private static final int INITIAL_INDEX_SIZE = 512;
private static final int CHUNK_TYPE_VIDEO_COMPRESSED = ('d' << 16) | ('c' << 24);
private static final int CHUNK_TYPE_VIDEO_UNCOMPRESSED = ('d' << 16) | ('b' << 24);
private static final int CHUNK_TYPE_AUDIO = ('w' << 16) | ('b' << 24);
protected final TrackOutput trackOutput;
/** The chunk id fourCC (example: `01wb`), as defined in the index and the movi. */
private final int chunkId;
/** Secondary chunk id. Bad muxers sometimes use an uncompressed video id (db) for key frames */
private final int alternativeChunkId;
private final long durationUs;
private final int streamHeaderChunkCount;
private int currentChunkSize;
private int bytesRemainingInCurrentChunk;
/** Number of chunks as calculated by the index */
private int currentChunkIndex;
private int indexChunkCount;
private int indexSize;
private long[] keyFrameOffsets;
private int[] keyFrameIndices;
public ChunkReader(
int id,
@C.TrackType int trackType,
long durationnUs,
int streamHeaderChunkCount,
TrackOutput trackOutput) {
Assertions.checkArgument(trackType == C.TRACK_TYPE_AUDIO || trackType == C.TRACK_TYPE_VIDEO);
this.durationUs = durationnUs;
this.streamHeaderChunkCount = streamHeaderChunkCount;
this.trackOutput = trackOutput;
@ChunkType
int chunkType =
trackType == C.TRACK_TYPE_VIDEO ? CHUNK_TYPE_VIDEO_COMPRESSED : CHUNK_TYPE_AUDIO;
chunkId = getChunkIdFourCc(id, chunkType);
alternativeChunkId =
trackType == C.TRACK_TYPE_VIDEO ? getChunkIdFourCc(id, CHUNK_TYPE_VIDEO_UNCOMPRESSED) : -1;
keyFrameOffsets = new long[INITIAL_INDEX_SIZE];
keyFrameIndices = new int[INITIAL_INDEX_SIZE];
}
public void appendKeyFrameToIndex(long offset) {
if (indexSize == keyFrameIndices.length) {
keyFrameOffsets = Arrays.copyOf(keyFrameOffsets, keyFrameOffsets.length * 3 / 2);
keyFrameIndices = Arrays.copyOf(keyFrameIndices, keyFrameIndices.length * 3 / 2);
}
keyFrameOffsets[indexSize] = offset;
keyFrameIndices[indexSize] = indexChunkCount;
indexSize++;
}
public void advanceCurrentChunk() {
currentChunkIndex++;
}
public long getCurrentChunkTimestampUs() {
return getChunkTimestampUs(currentChunkIndex);
}
public long getFrameDurationUs() {
return getChunkTimestampUs(/* chunkIndex= */ 1);
}
public void incrementIndexChunkCount() {
indexChunkCount++;
}
public void compactIndex() {
keyFrameOffsets = Arrays.copyOf(keyFrameOffsets, indexSize);
keyFrameIndices = Arrays.copyOf(keyFrameIndices, indexSize);
}
public boolean handlesChunkId(int chunkId) {
return this.chunkId == chunkId || alternativeChunkId == chunkId;
}
public boolean isCurrentFrameAKeyFrame() {
return Arrays.binarySearch(keyFrameIndices, currentChunkIndex) >= 0;
}
public boolean isVideo() {
return (chunkId & CHUNK_TYPE_VIDEO_COMPRESSED) == CHUNK_TYPE_VIDEO_COMPRESSED;
}
public boolean isAudio() {
return (chunkId & CHUNK_TYPE_AUDIO) == CHUNK_TYPE_AUDIO;
}
/** Prepares for parsing a chunk with the given {@code size}. */
public void onChunkStart(int size) {
currentChunkSize = size;
bytesRemainingInCurrentChunk = size;
}
/**
* Provides data associated to the current chunk and returns whether the full chunk has been
* parsed.
*/
public boolean onChunkData(ExtractorInput input) throws IOException {
bytesRemainingInCurrentChunk -=
trackOutput.sampleData(input, bytesRemainingInCurrentChunk, false);
boolean done = bytesRemainingInCurrentChunk == 0;
if (done) {
if (currentChunkSize > 0) {
trackOutput.sampleMetadata(
getCurrentChunkTimestampUs(),
(isCurrentFrameAKeyFrame() ? C.BUFFER_FLAG_KEY_FRAME : 0),
currentChunkSize,
0,
null);
}
advanceCurrentChunk();
}
return done;
}
public void seekToPosition(long position) {
if (indexSize == 0) {
currentChunkIndex = 0;
} else {
int index =
Util.binarySearchFloor(
keyFrameOffsets, position, /* inclusive= */ true, /* stayInBounds= */ true);
currentChunkIndex = keyFrameIndices[index];
}
}
public SeekMap.SeekPoints getSeekPoints(long timeUs) {
int targetFrameIndex = (int) (timeUs / getFrameDurationUs());
int keyFrameIndex =
Util.binarySearchFloor(
keyFrameIndices, targetFrameIndex, /* inclusive= */ true, /* stayInBounds= */ true);
if (keyFrameIndices[keyFrameIndex] == targetFrameIndex) {
return new SeekMap.SeekPoints(getSeekPoint(keyFrameIndex));
}
// The target frame is not a key frame, we look for the two closest ones.
SeekPoint precedingKeyFrameSeekPoint = getSeekPoint(keyFrameIndex);
if (keyFrameIndex + 1 < keyFrameOffsets.length) {
return new SeekMap.SeekPoints(precedingKeyFrameSeekPoint, getSeekPoint(keyFrameIndex + 1));
} else {
return new SeekMap.SeekPoints(precedingKeyFrameSeekPoint);
}
}
private long getChunkTimestampUs(int chunkIndex) {
return durationUs * chunkIndex / streamHeaderChunkCount;
}
private SeekPoint getSeekPoint(int keyFrameIndex) {
return new SeekPoint(
keyFrameIndices[keyFrameIndex] * getFrameDurationUs(), keyFrameOffsets[keyFrameIndex]);
}
private static int getChunkIdFourCc(int streamId, @ChunkType int chunkType) {
int tens = streamId / 10;
int ones = streamId % 10;
return (('0' + ones) << 8) | ('0' + tens) | chunkType;
}
}
/*
* Copyright 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.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.common.collect.ImmutableList;
/** Represents an AVI LIST. */
/* package */ final class ListChunk implements AviChunk {
public static ListChunk parseFrom(int listType, ParsableByteArray body) {
ImmutableList.Builder<AviChunk> builder = new ImmutableList.Builder<>();
int listBodyEndPosition = body.limit();
@C.TrackType int currentTrackType = C.TRACK_TYPE_NONE;
while (body.bytesLeft() > 8) {
int type = body.readLittleEndianInt();
int size = body.readLittleEndianInt();
int innerBoxBodyEndPosition = body.getPosition() + size;
body.setLimit(innerBoxBodyEndPosition);
@Nullable AviChunk aviChunk;
if (type == AviExtractor.FOURCC_LIST) {
int innerListType = body.readLittleEndianInt();
aviChunk = parseFrom(innerListType, body);
} else {
aviChunk = createBox(type, currentTrackType, body);
}
if (aviChunk != null) {
if (aviChunk.getType() == AviExtractor.FOURCC_strh) {
currentTrackType = ((AviStreamHeaderChunk) aviChunk).getTrackType();
}
builder.add(aviChunk);
}
body.setPosition(innerBoxBodyEndPosition);
body.setLimit(listBodyEndPosition);
}
return new ListChunk(listType, builder.build());
}
public final ImmutableList<AviChunk> children;
private final int type;
private ListChunk(int type, ImmutableList<AviChunk> children) {
this.type = type;
this.children = children;
}
@Override
public int getType() {
return type;
}
@Nullable
@SuppressWarnings("unchecked")
public <T extends AviChunk> T getChild(Class<T> c) {
for (AviChunk aviChunk : children) {
if (aviChunk.getClass() == c) {
return (T) aviChunk;
}
}
return null;
}
@Nullable
private static AviChunk createBox(
int chunkType, @C.TrackType int trackType, ParsableByteArray body) {
switch (chunkType) {
case AviExtractor.FOURCC_avih:
return AviMainHeaderChunk.parseFrom(body);
case AviExtractor.FOURCC_strh:
return AviStreamHeaderChunk.parseFrom(body);
case AviExtractor.FOURCC_strf:
return StreamFormatChunk.parseFrom(trackType, body);
case AviExtractor.FOURCC_strn:
return StreamNameChunk.parseFrom(body);
default:
return null;
}
}
}
/*
* Copyright 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.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
/** Holds the {@link Format} information contained in an STRF chunk. */
/* package */ final class StreamFormatChunk implements AviChunk {
private static final String TAG = "StreamFormatChunk";
@Nullable
public static AviChunk parseFrom(int trackType, ParsableByteArray body) {
if (trackType == C.TRACK_TYPE_VIDEO) {
return parseBitmapInfoHeader(body);
} else if (trackType == C.TRACK_TYPE_AUDIO) {
return parseWaveFormatEx(body);
} else {
Log.w(
TAG,
"Ignoring strf box for unsupported track type: " + Util.getTrackTypeString(trackType));
return null;
}
}
public final Format format;
public StreamFormatChunk(Format format) {
this.format = format;
}
@Override
public int getType() {
return AviExtractor.FOURCC_strf;
}
@Nullable
private static AviChunk parseBitmapInfoHeader(ParsableByteArray body) {
body.skipBytes(4); // biSize.
int width = body.readLittleEndianInt();
int height = body.readLittleEndianInt();
body.skipBytes(4); // biPlanes (2 bytes), biBitCount (2 bytes).
int compression = body.readLittleEndianInt();
String mimeType = getMimeTypeFromCompression(compression);
if (mimeType == null) {
Log.w(TAG, "Ignoring track with unsupported compression " + compression);
return null;
}
Format.Builder formatBuilder = new Format.Builder();
formatBuilder.setWidth(width).setHeight(height).setSampleMimeType(mimeType);
return new StreamFormatChunk(formatBuilder.build());
}
// Syntax defined by the WAVEFORMATEX structure. See
// https://docs.microsoft.com/en-us/previous-versions/dd757713(v=vs.85).
@Nullable
private static AviChunk parseWaveFormatEx(ParsableByteArray body) {
int formatTag = body.readLittleEndianUnsignedShort();
@Nullable String mimeType = getMimeTypeFromTag(formatTag);
if (mimeType == null) {
Log.w(TAG, "Ignoring track with unsupported format tag " + formatTag);
return null;
}
int channelCount = body.readLittleEndianUnsignedShort();
int samplesPerSecond = body.readLittleEndianInt();
body.skipBytes(6); // averageBytesPerSecond (4 bytes), nBlockAlign (2 bytes).
int bitsPerSample = body.readUnsignedShort();
int pcmEncoding = Util.getPcmEncoding(bitsPerSample);
int cbSize = body.readLittleEndianUnsignedShort();
byte[] codecData = new byte[cbSize];
body.readBytes(codecData, /* offset= */ 0, codecData.length);
Format.Builder formatBuilder = new Format.Builder();
formatBuilder
.setSampleMimeType(mimeType)
.setChannelCount(channelCount)
.setSampleRate(samplesPerSecond);
if (MimeTypes.AUDIO_RAW.equals(mimeType) && pcmEncoding != C.ENCODING_INVALID) {
formatBuilder.setPcmEncoding(pcmEncoding);
}
if (MimeTypes.AUDIO_AAC.equals(mimeType) && codecData.length > 0) {
formatBuilder.setInitializationData(ImmutableList.of(codecData));
}
return new StreamFormatChunk(formatBuilder.build());
}
@Nullable
private static String getMimeTypeFromTag(int tag) {
switch (tag) {
case 0x1: // WAVE_FORMAT_PCM
return MimeTypes.AUDIO_RAW;
case 0x55: // WAVE_FORMAT_MPEGLAYER3
return MimeTypes.AUDIO_MPEG;
case 0xff: // WAVE_FORMAT_AAC
return MimeTypes.AUDIO_AAC;
case 0x2000: // WAVE_FORMAT_DVM - AC3
return MimeTypes.AUDIO_AC3;
case 0x2001: // WAVE_FORMAT_DTS2
return MimeTypes.AUDIO_DTS;
default:
return null;
}
}
@Nullable
private static String getMimeTypeFromCompression(int compression) {
switch (compression) {
case 0x3234504d: // MP42
return MimeTypes.VIDEO_MP42;
case 0x3334504d: // MP43
return MimeTypes.VIDEO_MP43;
case 0x34363248: // H264
case 0x31637661: // avc1
case 0x31435641: // AVC1
return MimeTypes.VIDEO_H264;
case 0x44495633: // 3VID
case 0x78766964: // divx
case 0x58564944: // DIVX
case 0x30355844: // DX50
case 0x34504d46: // FMP4
case 0x64697678: // xvid
case 0x44495658: // XVID
return MimeTypes.VIDEO_MP4V;
case 0x47504a4d: // MJPG
case 0x67706a6d: // mjpg
return MimeTypes.VIDEO_MJPEG;
default:
return null;
}
}
}
/*
* Copyright 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.util.ParsableByteArray;
/** Parses and contains the name from the STRN chunk. */
/* package */ final class StreamNameChunk implements AviChunk {
public static StreamNameChunk parseFrom(ParsableByteArray body) {
return new StreamNameChunk(body.readString(body.bytesLeft()));
}
public final String name;
private StreamNameChunk(String name) {
this.name = name;
}
@Override
public int getType() {
return AviExtractor.FOURCC_strn;
}
}
/*
* Copyright 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.
*/
@NonNullApi
package com.google.android.exoplayer2.extractor.avi;
import com.google.android.exoplayer2.util.NonNullApi;
......@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.extractor.amr.AmrExtractor;
import com.google.android.exoplayer2.extractor.avi.AviExtractor;
import com.google.android.exoplayer2.extractor.flac.FlacExtractor;
import com.google.android.exoplayer2.extractor.flv.FlvExtractor;
import com.google.android.exoplayer2.extractor.jpeg.JpegExtractor;
......@@ -70,6 +71,7 @@ public final class DefaultExtractorsFactoryTest {
Ac3Extractor.class,
Ac4Extractor.class,
Mp3Extractor.class,
AviExtractor.class,
JpegExtractor.class)
.inOrder();
}
......@@ -112,6 +114,7 @@ public final class DefaultExtractorsFactoryTest {
AdtsExtractor.class,
Ac3Extractor.class,
Ac4Extractor.class,
AviExtractor.class,
JpegExtractor.class)
.inOrder();
}
......
/*
* Copyright 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.testutil.ExtractorAsserts;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.ParameterizedRobolectricTestRunner.Parameter;
import org.robolectric.ParameterizedRobolectricTestRunner.Parameters;
/** Tests for {@link AviExtractor}. */
@RunWith(ParameterizedRobolectricTestRunner.class)
public final class AviExtractorTest {
@Parameters(name = "{0}")
public static ImmutableList<ExtractorAsserts.SimulationConfig> params() {
return ExtractorAsserts.configs();
}
@Parameter public ExtractorAsserts.SimulationConfig simulationConfig;
@Test
public void aviSample() throws Exception {
ExtractorAsserts.assertBehavior(AviExtractor::new, "media/avi/sample.avi", simulationConfig);
}
}
seekMap:
isSeekable = true
duration = 4080000
getPosition(0) = [[timeUs=0, position=9996]]
getPosition(1) = [[timeUs=0, position=9996]]
getPosition(2040000) = [[timeUs=1835152, position=160708], [timeUs=2335648, position=198850]]
getPosition(4080000) = [[timeUs=3837136, position=308434]]
numberOfTracks = 2
track 0:
total output bytes = 17833
sample count = 6
format 0:
id = 0
sampleMimeType = video/mp4v-es
maxInputSize = 6761
width = 960
height = 400
sample 0:
time = 3837166
flags = 1
data = length 5356, hash 190A3CAE
sample 1:
time = 3878874
flags = 0
data = length 3172, hash 538FA2AE
sample 2:
time = 3920582
flags = 0
data = length 2393, hash 525B26D6
sample 3:
time = 3962291
flags = 0
data = length 2307, hash C894745F
sample 4:
time = 4003999
flags = 0
data = length 2490, hash 800FED70
sample 5:
time = 4045707
flags = 0
data = length 2115, hash A2512D3
track 1:
total output bytes = 3840
sample count = 10
format 0:
id = 1
sampleMimeType = audio/mpeg
maxInputSize = 384
channelCount = 2
sampleRate = 48000
sample 0:
time = 3816000
flags = 1
data = length 384, hash 69E3E157
sample 1:
time = 3840000
flags = 1
data = length 384, hash AC90ADEC
sample 2:
time = 3864000
flags = 1
data = length 384, hash 6A333A56
sample 3:
time = 3888000
flags = 1
data = length 384, hash 493D75A3
sample 4:
time = 3912000
flags = 1
data = length 384, hash 53FE2A9E
sample 5:
time = 3936000
flags = 1
data = length 384, hash 65D6147C
sample 6:
time = 3960000
flags = 1
data = length 384, hash 5E744FB2
sample 7:
time = 3984000
flags = 1
data = length 384, hash 68AEB7CA
sample 8:
time = 4008000
flags = 1
data = length 384, hash AC2972C
sample 9:
time = 4032000
flags = 1
data = length 384, hash E2A06CB9
tracksEnded = true
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