Search in sources :

Example 6 with ParsableByteArray

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);
}
Also used : ParsableByteArray(androidx.media3.common.util.ParsableByteArray)

Example 7 with ParsableByteArray

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;
}
Also used : ParsableByteArray(androidx.media3.common.util.ParsableByteArray)

Example 8 with ParsableByteArray

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);
    }
}
Also used : ParsableByteArray(androidx.media3.common.util.ParsableByteArray)

Example 9 with ParsableByteArray

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);
}
Also used : ParsableByteArray(androidx.media3.common.util.ParsableByteArray) CommentHeader(androidx.media3.extractor.VorbisUtil.CommentHeader)

Example 10 with ParsableByteArray

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;
        }
    }
}
Also used : AacUtil(androidx.media3.extractor.AacUtil) Format(androidx.media3.common.Format)

Aggregations

ParsableByteArray (androidx.media3.common.util.ParsableByteArray)96 Test (org.junit.Test)38 Nullable (androidx.annotation.Nullable)34 Format (androidx.media3.common.Format)9 ArrayList (java.util.ArrayList)9 Metadata (androidx.media3.common.Metadata)7 FakeExtractorInput (androidx.media3.test.utils.FakeExtractorInput)7 SeekPoint (androidx.media3.extractor.SeekPoint)5 Cue (androidx.media3.common.text.Cue)4 SampleNumberHolder (androidx.media3.extractor.FlacFrameReader.SampleNumberHolder)4 FlacStreamMetadataHolder (androidx.media3.extractor.FlacMetadataReader.FlacStreamMetadataHolder)4 ByteBuffer (java.nio.ByteBuffer)4 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)4 ParsableBitArray (androidx.media3.common.util.ParsableBitArray)3 Mesh (androidx.media3.exoplayer.video.spherical.Projection.Mesh)3 SubMesh (androidx.media3.exoplayer.video.spherical.Projection.SubMesh)3 TrackOutput (androidx.media3.extractor.TrackOutput)3 SmtaMetadataEntry (androidx.media3.extractor.metadata.mp4.SmtaMetadataEntry)3 LeafAtom (androidx.media3.extractor.mp4.Atom.LeafAtom)3 EnsuresNonNullIf (org.checkerframework.checker.nullness.qual.EnsuresNonNullIf)3