WavExtractor: Skip to data start position if position reset to 0

PiperOrigin-RevId: 260970865
This commit is contained in:
olly 2019-07-31 19:54:12 +01:00 committed by Oliver Woodman
parent c373ff0a1c
commit 6d20a5cf0c
3 changed files with 23 additions and 9 deletions

View File

@ -87,6 +87,8 @@ public final class WavExtractor implements Extractor {
if (!wavHeader.hasDataBounds()) { if (!wavHeader.hasDataBounds()) {
WavHeaderReader.skipToData(input, wavHeader); WavHeaderReader.skipToData(input, wavHeader);
extractorOutput.seekMap(wavHeader); extractorOutput.seekMap(wavHeader);
} else if (input.getPosition() == 0) {
input.skipFully(wavHeader.getDataStartPosition());
} }
long dataLimit = wavHeader.getDataLimit(); long dataLimit = wavHeader.getDataLimit();

View File

@ -37,9 +37,9 @@ import com.google.android.exoplayer2.util.Util;
@C.PcmEncoding @C.PcmEncoding
private final int encoding; private final int encoding;
/** Offset to the start of sample data. */ /** Position of the start of the sample data, in bytes. */
private long dataStartPosition; private int dataStartPosition;
/** Total size in bytes of the sample data. */ /** Total size of the sample data, in bytes. */
private long dataSize; private long dataSize;
public WavHeader(int numChannels, int sampleRateHz, int averageBytesPerSecond, int blockAlignment, public WavHeader(int numChannels, int sampleRateHz, int averageBytesPerSecond, int blockAlignment,
@ -50,6 +50,7 @@ import com.google.android.exoplayer2.util.Util;
this.blockAlignment = blockAlignment; this.blockAlignment = blockAlignment;
this.bitsPerSample = bitsPerSample; this.bitsPerSample = bitsPerSample;
this.encoding = encoding; this.encoding = encoding;
dataStartPosition = C.POSITION_UNSET;
} }
// Data bounds. // Data bounds.
@ -57,22 +58,33 @@ import com.google.android.exoplayer2.util.Util;
/** /**
* Sets the data start position and size in bytes of sample data in this WAV. * Sets the data start position and size in bytes of sample data in this WAV.
* *
* @param dataStartPosition The data start position in bytes. * @param dataStartPosition The position of the start of the sample data, in bytes.
* @param dataSize The data size in bytes. * @param dataSize The total size of the sample data, in bytes.
*/ */
public void setDataBounds(long dataStartPosition, long dataSize) { public void setDataBounds(int dataStartPosition, long dataSize) {
this.dataStartPosition = dataStartPosition; this.dataStartPosition = dataStartPosition;
this.dataSize = dataSize; this.dataSize = dataSize;
} }
/** Returns the data limit, or {@link C#POSITION_UNSET} if the data bounds have not been set. */ /**
* Returns the position of the start of the sample data, in bytes, or {@link C#POSITION_UNSET} if
* the data bounds have not been set.
*/
public int getDataStartPosition() {
return dataStartPosition;
}
/**
* Returns the limit of the sample data, in bytes, or {@link C#POSITION_UNSET} if the data bounds
* have not been set.
*/
public long getDataLimit() { public long getDataLimit() {
return hasDataBounds() ? (dataStartPosition + dataSize) : C.POSITION_UNSET; return hasDataBounds() ? (dataStartPosition + dataSize) : C.POSITION_UNSET;
} }
/** Returns whether the data start position and size have been set. */ /** Returns whether the data start position and size have been set. */
public boolean hasDataBounds() { public boolean hasDataBounds() {
return dataStartPosition != 0 && dataSize != 0; return dataStartPosition != C.POSITION_UNSET;
} }
// SeekMap implementation. // SeekMap implementation.

View File

@ -139,7 +139,7 @@ import java.io.IOException;
// Skip past the "data" header. // Skip past the "data" header.
input.skipFully(ChunkHeader.SIZE_IN_BYTES); input.skipFully(ChunkHeader.SIZE_IN_BYTES);
wavHeader.setDataBounds(input.getPosition(), chunkHeader.size); wavHeader.setDataBounds((int) input.getPosition(), chunkHeader.size);
} }
private WavHeaderReader() { private WavHeaderReader() {