Skip to content

Commit

Permalink
Skip AnnexB to Avcc conversion for AV1 video format
Browse files Browse the repository at this point in the history
The AV1 video stream does not contain NAL unit which is a concept
specific to H.264/H.265.
This was not caught before muxer does an in place replacement of
`NAL start code` with `NAL length`. In the absence of `NAL start code`
it was automatically a no-op.

PiperOrigin-RevId: 617193317
  • Loading branch information
SheenaChhabra authored and Copybara-Service committed Mar 19, 2024
1 parent 078cd22 commit 112066d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.muxer;

import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableList;
import java.nio.ByteBuffer;

Expand Down Expand Up @@ -95,6 +96,15 @@ public static ByteBuffer stripEmulationPrevention(ByteBuffer input) {
return output;
}

/**
* Returns whether the sample of the given MIME type will contain NAL units in Annex-B format
* (ISO/IEC 14496-10 Annex B, which uses start codes to delineate NAL units).
*/
public static boolean doesSampleContainAnnexBNalUnits(String sampleMimeType) {
return sampleMimeType.equals(MimeTypes.VIDEO_H264)
|| sampleMimeType.equals(MimeTypes.VIDEO_H265);
}

private static ByteBuffer getBytes(ByteBuffer buf, int offset, int length) {
ByteBuffer result = buf.duplicate();
result.position(offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.muxer;

import static com.google.android.exoplayer2.muxer.AnnexBUtils.doesSampleContainAnnexBNalUnits;
import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState;
Expand All @@ -24,7 +25,6 @@
import android.media.MediaCodec.BufferInfo;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.muxer.Mp4Muxer.TrackToken;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.Range;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -303,7 +303,7 @@ private void flushPending(Track track) throws IOException {

// Convert the H.264/H.265 samples from Annex-B format (output by MediaCodec) to
// Avcc format (required by MP4 container).
if (MimeTypes.isVideo(track.format.sampleMimeType)) {
if (doesSampleContainAnnexBNalUnits(checkNotNull(track.format.sampleMimeType))) {
annexBToAvccConverter.process(currentSampleByteBuffer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.muxer;

import static com.google.android.exoplayer2.muxer.AnnexBUtils.doesSampleContainAnnexBNalUnits;
import static com.google.android.exoplayer2.muxer.Boxes.BOX_HEADER_SIZE;
import static com.google.android.exoplayer2.muxer.Boxes.MFHD_BOX_CONTENT_SIZE;
import static com.google.android.exoplayer2.muxer.Boxes.TFHD_BOX_CONTENT_SIZE;
Expand Down Expand Up @@ -253,7 +254,7 @@ private void writeMdatBox() throws IOException {

// Convert the H.264/H.265 samples from Annex-B format (output by MediaCodec) to
// Avcc format (required by MP4 container).
if (MimeTypes.isVideo(currentTrack.format.sampleMimeType)) {
if (doesSampleContainAnnexBNalUnits(checkNotNull(currentTrack.format.sampleMimeType))) {
annexBToAvccConverter.process(currentSampleByteBuffer);
}
bytesWritten += output.write(currentSampleByteBuffer);
Expand Down

0 comments on commit 112066d

Please sign in to comment.