use of androidx.media3.common.util.ParsableByteArray in project media by androidx.
the class FlacFrameReader method checkFrameHeaderFromPeek.
/**
* Checks whether the given FLAC frame header is valid and, if so, writes the frame first sample
* number in {@code sampleNumberHolder}.
*
* <p>The {@code input} peek position is left unchanged.
*
* @param input The input to get the data from, whose peek position must correspond to the frame
* header.
* @param flacStreamMetadata The stream metadata.
* @param frameStartMarker The frame start marker of the stream.
* @param sampleNumberHolder The holder used to contain the sample number.
* @return Whether the frame header is valid.
*/
public static boolean checkFrameHeaderFromPeek(ExtractorInput input, FlacStreamMetadata flacStreamMetadata, int frameStartMarker, SampleNumberHolder sampleNumberHolder) throws IOException {
long originalPeekPosition = input.getPeekPosition();
byte[] frameStartBytes = new byte[2];
input.peekFully(frameStartBytes, 0, 2);
int frameStart = (frameStartBytes[0] & 0xFF) << 8 | (frameStartBytes[1] & 0xFF);
if (frameStart != frameStartMarker) {
input.resetPeekPosition();
input.advancePeekPosition((int) (originalPeekPosition - input.getPosition()));
return false;
}
ParsableByteArray scratch = new ParsableByteArray(FlacConstants.MAX_FRAME_HEADER_SIZE);
System.arraycopy(frameStartBytes, /* srcPos= */
0, scratch.getData(), /* destPos= */
0, /* length= */
2);
int totalBytesPeeked = ExtractorUtil.peekToLength(input, scratch.getData(), 2, FlacConstants.MAX_FRAME_HEADER_SIZE - 2);
scratch.setLimit(totalBytesPeeked);
input.resetPeekPosition();
input.advancePeekPosition((int) (originalPeekPosition - input.getPosition()));
return checkAndReadFrameHeader(scratch, flacStreamMetadata, frameStartMarker, sampleNumberHolder);
}
use of androidx.media3.common.util.ParsableByteArray in project media by androidx.
the class FlacMetadataReader method checkAndPeekStreamMarker.
/**
* Peeks the FLAC stream marker.
*
* @param input Input stream to peek the stream marker from.
* @return Whether the data peeked is the FLAC stream marker.
* @throws IOException If peeking from the input fails. In this case, the peek position is left
* unchanged.
*/
public static boolean checkAndPeekStreamMarker(ExtractorInput input) throws IOException {
ParsableByteArray scratch = new ParsableByteArray(FlacConstants.STREAM_MARKER_SIZE);
input.peekFully(scratch.getData(), 0, FlacConstants.STREAM_MARKER_SIZE);
return scratch.readUnsignedInt() == STREAM_MARKER;
}
use of androidx.media3.common.util.ParsableByteArray in project media by androidx.
the class FlacMetadataReader method readStreamMarker.
/**
* Reads the FLAC stream marker.
*
* @param input Input stream to read the stream marker from.
* @throws ParserException If an error occurs parsing the stream marker. In this case, the
* position of {@code input} is advanced by {@link FlacConstants#STREAM_MARKER_SIZE} bytes.
* @throws IOException If reading from the input fails. In this case, the position is left
* unchanged.
*/
public static void readStreamMarker(ExtractorInput input) throws IOException {
ParsableByteArray scratch = new ParsableByteArray(FlacConstants.STREAM_MARKER_SIZE);
input.readFully(scratch.getData(), 0, FlacConstants.STREAM_MARKER_SIZE);
if (scratch.readUnsignedInt() != STREAM_MARKER) {
throw ParserException.createForMalformedContainer("Failed to read FLAC stream marker.", /* cause= */
null);
}
}
use of androidx.media3.common.util.ParsableByteArray in project media by androidx.
the class FlacMetadataReader method readVorbisCommentMetadataBlock.
private static List<String> readVorbisCommentMetadataBlock(ExtractorInput input, int length) throws IOException {
ParsableByteArray scratch = new ParsableByteArray(length);
input.readFully(scratch.getData(), 0, length);
scratch.skipBytes(FlacConstants.METADATA_BLOCK_HEADER_SIZE);
CommentHeader commentHeader = VorbisUtil.readVorbisCommentHeader(scratch, /* hasMetadataHeader= */
false, /* hasFramingBit= */
false);
return Arrays.asList(commentHeader.comments);
}
use of androidx.media3.common.util.ParsableByteArray in project media by androidx.
the class AudioTagPayloadReader method parsePayload.
@Override
protected boolean parsePayload(ParsableByteArray data, long timeUs) throws ParserException {
if (audioFormat == AUDIO_FORMAT_MP3) {
int sampleSize = data.bytesLeft();
output.sampleData(data, sampleSize);
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
return true;
} else {
int packetType = data.readUnsignedByte();
if (packetType == AAC_PACKET_TYPE_SEQUENCE_HEADER && !hasOutputFormat) {
// Parse the sequence header.
byte[] audioSpecificConfig = new byte[data.bytesLeft()];
data.readBytes(audioSpecificConfig, 0, audioSpecificConfig.length);
AacUtil.Config aacConfig = AacUtil.parseAudioSpecificConfig(audioSpecificConfig);
Format format = new Format.Builder().setSampleMimeType(MimeTypes.AUDIO_AAC).setCodecs(aacConfig.codecs).setChannelCount(aacConfig.channelCount).setSampleRate(aacConfig.sampleRateHz).setInitializationData(Collections.singletonList(audioSpecificConfig)).build();
output.format(format);
hasOutputFormat = true;
return false;
} else if (audioFormat != AUDIO_FORMAT_AAC || packetType == AAC_PACKET_TYPE_AAC_RAW) {
int sampleSize = data.bytesLeft();
output.sampleData(data, sampleSize);
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
return true;
} else {
return false;
}
}
}
Aggregations