Commit 1e78ee82 by olly Committed by Oliver Woodman

WAV extractor fixes.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=118021575
parent 0ee45550
...@@ -72,6 +72,7 @@ import java.io.IOException; ...@@ -72,6 +72,7 @@ import java.io.IOException;
throw new ParserException( throw new ParserException(
"Second chunk in RIFF WAV should be format; got: " + formatChunkHeader.id); "Second chunk in RIFF WAV should be format; got: " + formatChunkHeader.id);
} }
Assertions.checkState(formatChunkHeader.size >= 16);
input.peekFully(scratch.data, 0, 16); input.peekFully(scratch.data, 0, 16);
scratch.setPosition(0); scratch.setPosition(0);
...@@ -95,18 +96,14 @@ import java.io.IOException; ...@@ -95,18 +96,14 @@ import java.io.IOException;
return null; return null;
} }
if (type == TYPE_PCM) { if (type != TYPE_PCM && type != TYPE_WAVE_FORMAT_EXTENSIBLE) {
Assertions.checkState(formatChunkHeader.size == 16);
// No more data to read.
} else if (type == TYPE_WAVE_FORMAT_EXTENSIBLE) {
Assertions.checkState(formatChunkHeader.size == 40);
// Skip extensionSize, validBitsPerSample, channelMask, subFormatGuid.
input.advancePeekPosition(2 + 2 + 4 + 16);
} else {
Log.e(TAG, "Unsupported WAV format type: " + type); Log.e(TAG, "Unsupported WAV format type: " + type);
return null; return null;
} }
// If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ...
input.advancePeekPosition((int) formatChunkHeader.size - 16);
return new WavHeader( return new WavHeader(
numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample); numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample);
} }
...@@ -118,7 +115,7 @@ import java.io.IOException; ...@@ -118,7 +115,7 @@ import java.io.IOException;
* If an exception is thrown, the input position will be left pointing to a chunk header. * If an exception is thrown, the input position will be left pointing to a chunk header.
* *
* @param input Input stream to skip to the data chunk in. Its peek position must be pointing to * @param input Input stream to skip to the data chunk in. Its peek position must be pointing to
* a valid chunk header that is not the RIFF chunk. * a valid chunk header.
* @param wavHeader WAV header to populate with data bounds. * @param wavHeader WAV header to populate with data bounds.
* @throws IOException If reading from the input fails. * @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from input. * @throws InterruptedException If interrupted while reading from input.
...@@ -135,6 +132,10 @@ import java.io.IOException; ...@@ -135,6 +132,10 @@ import java.io.IOException;
while (chunkHeader.id != Util.getIntegerCodeForString("data")) { while (chunkHeader.id != Util.getIntegerCodeForString("data")) {
Log.w(TAG, "Ignoring unknown WAV chunk: " + chunkHeader.id); Log.w(TAG, "Ignoring unknown WAV chunk: " + chunkHeader.id);
long bytesToSkip = ChunkHeader.SIZE_IN_BYTES + chunkHeader.size; long bytesToSkip = ChunkHeader.SIZE_IN_BYTES + chunkHeader.size;
// Override size of RIFF chunk, since it describes its size as the entire file.
if (chunkHeader.id == Util.getIntegerCodeForString("RIFF")) {
bytesToSkip = ChunkHeader.SIZE_IN_BYTES + 4;
}
if (bytesToSkip > Integer.MAX_VALUE) { if (bytesToSkip > Integer.MAX_VALUE) {
throw new ParserException("Chunk is too large (~2GB+) to skip; id: " + chunkHeader.id); throw new ParserException("Chunk is too large (~2GB+) to skip; id: " + chunkHeader.id);
} }
......
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