Java Code Examples for com.google.android.exoplayer2.util.ParsableByteArray#readLittleEndianUnsignedIntToInt()
The following examples show how to use
com.google.android.exoplayer2.util.ParsableByteArray#readLittleEndianUnsignedIntToInt() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WavHeaderReader.java From MediaSDK with Apache License 2.0 | 4 votes |
/** * Peeks and returns a {@code WavHeader}. * * @param input Input stream to peek the WAV header from. * @throws ParserException If the input file is an incorrect RIFF WAV. * @throws IOException If peeking from the input fails. * @throws InterruptedException If interrupted while peeking from input. * @return A new {@code WavHeader} peeked from {@code input}, or null if the input is not a * supported WAV format. */ @Nullable public static WavHeader peek(ExtractorInput input) throws IOException, InterruptedException { Assertions.checkNotNull(input); // Allocate a scratch buffer large enough to store the format chunk. ParsableByteArray scratch = new ParsableByteArray(16); // Attempt to read the RIFF chunk. ChunkHeader chunkHeader = ChunkHeader.peek(input, scratch); if (chunkHeader.id != WavUtil.RIFF_FOURCC) { return null; } input.peekFully(scratch.data, 0, 4); scratch.setPosition(0); int riffFormat = scratch.readInt(); if (riffFormat != WavUtil.WAVE_FOURCC) { Log.e(TAG, "Unsupported RIFF format: " + riffFormat); return null; } // Skip chunks until we find the format chunk. chunkHeader = ChunkHeader.peek(input, scratch); while (chunkHeader.id != WavUtil.FMT_FOURCC) { input.advancePeekPosition((int) chunkHeader.size); chunkHeader = ChunkHeader.peek(input, scratch); } Assertions.checkState(chunkHeader.size >= 16); input.peekFully(scratch.data, 0, 16); scratch.setPosition(0); int type = scratch.readLittleEndianUnsignedShort(); int numChannels = scratch.readLittleEndianUnsignedShort(); int sampleRateHz = scratch.readLittleEndianUnsignedIntToInt(); int averageBytesPerSecond = scratch.readLittleEndianUnsignedIntToInt(); int blockAlignment = scratch.readLittleEndianUnsignedShort(); int bitsPerSample = scratch.readLittleEndianUnsignedShort(); int expectedBlockAlignment = numChannels * bitsPerSample / 8; if (blockAlignment != expectedBlockAlignment) { throw new ParserException("Expected block alignment: " + expectedBlockAlignment + "; got: " + blockAlignment); } @C.PcmEncoding int encoding = WavUtil.getEncodingForType(type, bitsPerSample); if (encoding == C.ENCODING_INVALID) { Log.e(TAG, "Unsupported WAV format: " + bitsPerSample + " bit/sample, type " + type); return null; } // If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ... input.advancePeekPosition((int) chunkHeader.size - 16); return new WavHeader( numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding); }
Example 2
Source File: WavHeaderReader.java From TelePlus-Android with GNU General Public License v2.0 | 4 votes |
/** * Peeks and returns a {@code WavHeader}. * * @param input Input stream to peek the WAV header from. * @throws ParserException If the input file is an incorrect RIFF WAV. * @throws IOException If peeking from the input fails. * @throws InterruptedException If interrupted while peeking from input. * @return A new {@code WavHeader} peeked from {@code input}, or null if the input is not a * supported WAV format. */ public static WavHeader peek(ExtractorInput input) throws IOException, InterruptedException { Assertions.checkNotNull(input); // Allocate a scratch buffer large enough to store the format chunk. ParsableByteArray scratch = new ParsableByteArray(16); // Attempt to read the RIFF chunk. ChunkHeader chunkHeader = ChunkHeader.peek(input, scratch); if (chunkHeader.id != Util.getIntegerCodeForString("RIFF")) { return null; } input.peekFully(scratch.data, 0, 4); scratch.setPosition(0); int riffFormat = scratch.readInt(); if (riffFormat != Util.getIntegerCodeForString("WAVE")) { Log.e(TAG, "Unsupported RIFF format: " + riffFormat); return null; } // Skip chunks until we find the format chunk. chunkHeader = ChunkHeader.peek(input, scratch); while (chunkHeader.id != Util.getIntegerCodeForString("fmt ")) { input.advancePeekPosition((int) chunkHeader.size); chunkHeader = ChunkHeader.peek(input, scratch); } Assertions.checkState(chunkHeader.size >= 16); input.peekFully(scratch.data, 0, 16); scratch.setPosition(0); int type = scratch.readLittleEndianUnsignedShort(); int numChannels = scratch.readLittleEndianUnsignedShort(); int sampleRateHz = scratch.readLittleEndianUnsignedIntToInt(); int averageBytesPerSecond = scratch.readLittleEndianUnsignedIntToInt(); int blockAlignment = scratch.readLittleEndianUnsignedShort(); int bitsPerSample = scratch.readLittleEndianUnsignedShort(); int expectedBlockAlignment = numChannels * bitsPerSample / 8; if (blockAlignment != expectedBlockAlignment) { throw new ParserException("Expected block alignment: " + expectedBlockAlignment + "; got: " + blockAlignment); } @C.PcmEncoding int encoding; switch (type) { case TYPE_PCM: case TYPE_WAVE_FORMAT_EXTENSIBLE: encoding = Util.getPcmEncoding(bitsPerSample); break; case TYPE_FLOAT: encoding = bitsPerSample == 32 ? C.ENCODING_PCM_FLOAT : C.ENCODING_INVALID; break; case TYPE_A_LAW: encoding = C.ENCODING_PCM_A_LAW; break; case TYPE_MU_LAW: encoding = C.ENCODING_PCM_MU_LAW; break; default: Log.e(TAG, "Unsupported WAV format type: " + type); return null; } if (encoding == C.ENCODING_INVALID) { Log.e(TAG, "Unsupported WAV bit depth " + bitsPerSample + " for type " + type); return null; } // If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ... input.advancePeekPosition((int) chunkHeader.size - 16); return new WavHeader(numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding); }
Example 3
Source File: WavHeaderReader.java From TelePlus-Android with GNU General Public License v2.0 | 4 votes |
/** * Peeks and returns a {@code WavHeader}. * * @param input Input stream to peek the WAV header from. * @throws ParserException If the input file is an incorrect RIFF WAV. * @throws IOException If peeking from the input fails. * @throws InterruptedException If interrupted while peeking from input. * @return A new {@code WavHeader} peeked from {@code input}, or null if the input is not a * supported WAV format. */ public static WavHeader peek(ExtractorInput input) throws IOException, InterruptedException { Assertions.checkNotNull(input); // Allocate a scratch buffer large enough to store the format chunk. ParsableByteArray scratch = new ParsableByteArray(16); // Attempt to read the RIFF chunk. ChunkHeader chunkHeader = ChunkHeader.peek(input, scratch); if (chunkHeader.id != Util.getIntegerCodeForString("RIFF")) { return null; } input.peekFully(scratch.data, 0, 4); scratch.setPosition(0); int riffFormat = scratch.readInt(); if (riffFormat != Util.getIntegerCodeForString("WAVE")) { Log.e(TAG, "Unsupported RIFF format: " + riffFormat); return null; } // Skip chunks until we find the format chunk. chunkHeader = ChunkHeader.peek(input, scratch); while (chunkHeader.id != Util.getIntegerCodeForString("fmt ")) { input.advancePeekPosition((int) chunkHeader.size); chunkHeader = ChunkHeader.peek(input, scratch); } Assertions.checkState(chunkHeader.size >= 16); input.peekFully(scratch.data, 0, 16); scratch.setPosition(0); int type = scratch.readLittleEndianUnsignedShort(); int numChannels = scratch.readLittleEndianUnsignedShort(); int sampleRateHz = scratch.readLittleEndianUnsignedIntToInt(); int averageBytesPerSecond = scratch.readLittleEndianUnsignedIntToInt(); int blockAlignment = scratch.readLittleEndianUnsignedShort(); int bitsPerSample = scratch.readLittleEndianUnsignedShort(); int expectedBlockAlignment = numChannels * bitsPerSample / 8; if (blockAlignment != expectedBlockAlignment) { throw new ParserException("Expected block alignment: " + expectedBlockAlignment + "; got: " + blockAlignment); } @C.PcmEncoding int encoding; switch (type) { case TYPE_PCM: case TYPE_WAVE_FORMAT_EXTENSIBLE: encoding = Util.getPcmEncoding(bitsPerSample); break; case TYPE_FLOAT: encoding = bitsPerSample == 32 ? C.ENCODING_PCM_FLOAT : C.ENCODING_INVALID; break; case TYPE_A_LAW: encoding = C.ENCODING_PCM_A_LAW; break; case TYPE_MU_LAW: encoding = C.ENCODING_PCM_MU_LAW; break; default: Log.e(TAG, "Unsupported WAV format type: " + type); return null; } if (encoding == C.ENCODING_INVALID) { Log.e(TAG, "Unsupported WAV bit depth " + bitsPerSample + " for type " + type); return null; } // If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ... input.advancePeekPosition((int) chunkHeader.size - 16); return new WavHeader(numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding); }
Example 4
Source File: WavHeaderReader.java From K-Sonic with MIT License | 4 votes |
/** * Peeks and returns a {@code WavHeader}. * * @param input Input stream to peek the WAV header from. * @throws ParserException If the input file is an incorrect RIFF WAV. * @throws IOException If peeking from the input fails. * @throws InterruptedException If interrupted while peeking from input. * @return A new {@code WavHeader} peeked from {@code input}, or null if the input is not a * supported WAV format. */ public static WavHeader peek(ExtractorInput input) throws IOException, InterruptedException { Assertions.checkNotNull(input); // Allocate a scratch buffer large enough to store the format chunk. ParsableByteArray scratch = new ParsableByteArray(16); // Attempt to read the RIFF chunk. ChunkHeader chunkHeader = ChunkHeader.peek(input, scratch); if (chunkHeader.id != Util.getIntegerCodeForString("RIFF")) { return null; } input.peekFully(scratch.data, 0, 4); scratch.setPosition(0); int riffFormat = scratch.readInt(); if (riffFormat != Util.getIntegerCodeForString("WAVE")) { Log.e(TAG, "Unsupported RIFF format: " + riffFormat); return null; } // Skip chunks until we find the format chunk. chunkHeader = ChunkHeader.peek(input, scratch); while (chunkHeader.id != Util.getIntegerCodeForString("fmt ")) { input.advancePeekPosition((int) chunkHeader.size); chunkHeader = ChunkHeader.peek(input, scratch); } Assertions.checkState(chunkHeader.size >= 16); input.peekFully(scratch.data, 0, 16); scratch.setPosition(0); int type = scratch.readLittleEndianUnsignedShort(); int numChannels = scratch.readLittleEndianUnsignedShort(); int sampleRateHz = scratch.readLittleEndianUnsignedIntToInt(); int averageBytesPerSecond = scratch.readLittleEndianUnsignedIntToInt(); int blockAlignment = scratch.readLittleEndianUnsignedShort(); int bitsPerSample = scratch.readLittleEndianUnsignedShort(); int expectedBlockAlignment = numChannels * bitsPerSample / 8; if (blockAlignment != expectedBlockAlignment) { throw new ParserException("Expected block alignment: " + expectedBlockAlignment + "; got: " + blockAlignment); } @C.PcmEncoding int encoding = Util.getPcmEncoding(bitsPerSample); if (encoding == C.ENCODING_INVALID) { Log.e(TAG, "Unsupported WAV bit depth: " + bitsPerSample); return null; } if (type != TYPE_PCM && type != TYPE_WAVE_FORMAT_EXTENSIBLE) { Log.e(TAG, "Unsupported WAV format type: " + type); return null; } // If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ... input.advancePeekPosition((int) chunkHeader.size - 16); return new WavHeader(numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding); }
Example 5
Source File: WavHeaderReader.java From Telegram-FOSS with GNU General Public License v2.0 | 4 votes |
/** * Peeks and returns a {@code WavHeader}. * * @param input Input stream to peek the WAV header from. * @throws ParserException If the input file is an incorrect RIFF WAV. * @throws IOException If peeking from the input fails. * @throws InterruptedException If interrupted while peeking from input. * @return A new {@code WavHeader} peeked from {@code input}, or null if the input is not a * supported WAV format. */ public static WavHeader peek(ExtractorInput input) throws IOException, InterruptedException { Assertions.checkNotNull(input); // Allocate a scratch buffer large enough to store the format chunk. ParsableByteArray scratch = new ParsableByteArray(16); // Attempt to read the RIFF chunk. ChunkHeader chunkHeader = ChunkHeader.peek(input, scratch); if (chunkHeader.id != WavUtil.RIFF_FOURCC) { return null; } input.peekFully(scratch.data, 0, 4); scratch.setPosition(0); int riffFormat = scratch.readInt(); if (riffFormat != WavUtil.WAVE_FOURCC) { Log.e(TAG, "Unsupported RIFF format: " + riffFormat); return null; } // Skip chunks until we find the format chunk. chunkHeader = ChunkHeader.peek(input, scratch); while (chunkHeader.id != WavUtil.FMT_FOURCC) { input.advancePeekPosition((int) chunkHeader.size); chunkHeader = ChunkHeader.peek(input, scratch); } Assertions.checkState(chunkHeader.size >= 16); input.peekFully(scratch.data, 0, 16); scratch.setPosition(0); int type = scratch.readLittleEndianUnsignedShort(); int numChannels = scratch.readLittleEndianUnsignedShort(); int sampleRateHz = scratch.readLittleEndianUnsignedIntToInt(); int averageBytesPerSecond = scratch.readLittleEndianUnsignedIntToInt(); int blockAlignment = scratch.readLittleEndianUnsignedShort(); int bitsPerSample = scratch.readLittleEndianUnsignedShort(); int expectedBlockAlignment = numChannels * bitsPerSample / 8; if (blockAlignment != expectedBlockAlignment) { throw new ParserException("Expected block alignment: " + expectedBlockAlignment + "; got: " + blockAlignment); } @C.PcmEncoding int encoding = WavUtil.getEncodingForType(type, bitsPerSample); if (encoding == C.ENCODING_INVALID) { Log.e(TAG, "Unsupported WAV format: " + bitsPerSample + " bit/sample, type " + type); return null; } // If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ... input.advancePeekPosition((int) chunkHeader.size - 16); return new WavHeader( numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding); }
Example 6
Source File: WavHeaderReader.java From Telegram with GNU General Public License v2.0 | 4 votes |
/** * Peeks and returns a {@code WavHeader}. * * @param input Input stream to peek the WAV header from. * @throws ParserException If the input file is an incorrect RIFF WAV. * @throws IOException If peeking from the input fails. * @throws InterruptedException If interrupted while peeking from input. * @return A new {@code WavHeader} peeked from {@code input}, or null if the input is not a * supported WAV format. */ public static WavHeader peek(ExtractorInput input) throws IOException, InterruptedException { Assertions.checkNotNull(input); // Allocate a scratch buffer large enough to store the format chunk. ParsableByteArray scratch = new ParsableByteArray(16); // Attempt to read the RIFF chunk. ChunkHeader chunkHeader = ChunkHeader.peek(input, scratch); if (chunkHeader.id != WavUtil.RIFF_FOURCC) { return null; } input.peekFully(scratch.data, 0, 4); scratch.setPosition(0); int riffFormat = scratch.readInt(); if (riffFormat != WavUtil.WAVE_FOURCC) { Log.e(TAG, "Unsupported RIFF format: " + riffFormat); return null; } // Skip chunks until we find the format chunk. chunkHeader = ChunkHeader.peek(input, scratch); while (chunkHeader.id != WavUtil.FMT_FOURCC) { input.advancePeekPosition((int) chunkHeader.size); chunkHeader = ChunkHeader.peek(input, scratch); } Assertions.checkState(chunkHeader.size >= 16); input.peekFully(scratch.data, 0, 16); scratch.setPosition(0); int type = scratch.readLittleEndianUnsignedShort(); int numChannels = scratch.readLittleEndianUnsignedShort(); int sampleRateHz = scratch.readLittleEndianUnsignedIntToInt(); int averageBytesPerSecond = scratch.readLittleEndianUnsignedIntToInt(); int blockAlignment = scratch.readLittleEndianUnsignedShort(); int bitsPerSample = scratch.readLittleEndianUnsignedShort(); int expectedBlockAlignment = numChannels * bitsPerSample / 8; if (blockAlignment != expectedBlockAlignment) { throw new ParserException("Expected block alignment: " + expectedBlockAlignment + "; got: " + blockAlignment); } @C.PcmEncoding int encoding = WavUtil.getEncodingForType(type, bitsPerSample); if (encoding == C.ENCODING_INVALID) { Log.e(TAG, "Unsupported WAV format: " + bitsPerSample + " bit/sample, type " + type); return null; } // If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ... input.advancePeekPosition((int) chunkHeader.size - 16); return new WavHeader( numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding); }