package androidx.media3.extractor.ogg; import androidx.media3.common.Format; import androidx.media3.common.Metadata; import androidx.media3.common.ParserException; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.ParsableByteArray; import androidx.media3.extractor.OpusUtil; import androidx.media3.extractor.VorbisUtil; import androidx.media3.extractor.ogg.StreamReader; import com.google.common.collect.ImmutableList; import java.util.Arrays; import java.util.List; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; /* loaded from: classes2.dex */ final class OpusReader extends StreamReader { private boolean firstCommentHeaderSeen; private static final byte[] OPUS_ID_HEADER_SIGNATURE = {79, 112, 117, 115, 72, 101, 97, 100}; private static final byte[] OPUS_COMMENT_HEADER_SIGNATURE = {79, 112, 117, 115, 84, 97, 103, 115}; public static boolean verifyBitstreamType(ParsableByteArray parsableByteArray) { return peekPacketStartsWith(parsableByteArray, OPUS_ID_HEADER_SIGNATURE); } /* JADX INFO: Access modifiers changed from: protected */ @Override // androidx.media3.extractor.ogg.StreamReader public void reset(boolean z) { super.reset(z); if (z) { this.firstCommentHeaderSeen = false; } } @Override // androidx.media3.extractor.ogg.StreamReader protected long preparePayload(ParsableByteArray parsableByteArray) { return convertTimeToGranule(OpusUtil.getPacketDurationUs(parsableByteArray.getData())); } @Override // androidx.media3.extractor.ogg.StreamReader @EnsuresNonNullIf(expression = {"#3.format"}, result = false) protected boolean readHeaders(ParsableByteArray parsableByteArray, long j, StreamReader.SetupData setupData) throws ParserException { if (peekPacketStartsWith(parsableByteArray, OPUS_ID_HEADER_SIGNATURE)) { byte[] copyOf = Arrays.copyOf(parsableByteArray.getData(), parsableByteArray.limit()); int channelCount = OpusUtil.getChannelCount(copyOf); List buildInitializationData = OpusUtil.buildInitializationData(copyOf); if (setupData.format != null) { return true; } setupData.format = new Format.Builder().setSampleMimeType("audio/opus").setChannelCount(channelCount).setSampleRate(48000).setInitializationData(buildInitializationData).build(); return true; } byte[] bArr = OPUS_COMMENT_HEADER_SIGNATURE; if (peekPacketStartsWith(parsableByteArray, bArr)) { Assertions.checkStateNotNull(setupData.format); if (this.firstCommentHeaderSeen) { return true; } this.firstCommentHeaderSeen = true; parsableByteArray.skipBytes(bArr.length); Metadata parseVorbisComments = VorbisUtil.parseVorbisComments(ImmutableList.copyOf(VorbisUtil.readVorbisCommentHeader(parsableByteArray, false, false).comments)); if (parseVorbisComments == null) { return true; } setupData.format = setupData.format.buildUpon().setMetadata(parseVorbisComments.copyWithAppendedEntriesFrom(setupData.format.metadata)).build(); return true; } Assertions.checkStateNotNull(setupData.format); return false; } private static boolean peekPacketStartsWith(ParsableByteArray parsableByteArray, byte[] bArr) { if (parsableByteArray.bytesLeft() < bArr.length) { return false; } int position = parsableByteArray.getPosition(); byte[] bArr2 = new byte[bArr.length]; parsableByteArray.readBytes(bArr2, 0, bArr.length); parsableByteArray.setPosition(position); return Arrays.equals(bArr2, bArr); } }