Refactor audioEsdsBox to esdsBox

Since the muxer supported only AAC audio codec, the esdsBox was unconditionally created within the audioSampleEntry. This CL refactors the box creation logic by moving it to the codecSpecificBox method. This is to make adding support for new audio codecs easier.

PiperOrigin-RevId: 650130935
(cherry picked from commit a2693553698bf75a6874f2a5bd7fc8b855232372)
This commit is contained in:
Googler 2024-07-07 23:10:43 -07:00 committed by Tianyi Feng
parent ae5a7e54ae
commit f2d3072d1a

View File

@ -509,17 +509,11 @@ import java.util.Locale;
/** Returns an audio sample entry box based on the MIME type. */ /** Returns an audio sample entry box based on the MIME type. */
public static ByteBuffer audioSampleEntry(Format format) { public static ByteBuffer audioSampleEntry(Format format) {
String mimeType = checkNotNull(format.sampleMimeType); String fourcc = codecSpecificFourcc(format);
checkArgument(mimeType.equals(MimeTypes.AUDIO_AAC), "Unsupported audio format: " + mimeType); ByteBuffer codecSpecificBox = codecSpecificBox(format);
String fourcc = "mp4a";
checkArgument(!format.initializationData.isEmpty(), "csd-0 not found in the format."); ByteBuffer contents =
ByteBuffer.allocate(codecSpecificBox.remaining() + MAX_FIXED_LEAF_BOX_SIZE);
byte[] csd0 = format.initializationData.get(0);
checkArgument(csd0.length > 0, "csd-0 is empty.");
ByteBuffer csd0ByteBuffer = ByteBuffer.wrap(csd0);
ByteBuffer contents = ByteBuffer.allocate(csd0ByteBuffer.limit() + MAX_FIXED_LEAF_BOX_SIZE);
contents.putInt(0x00); // reserved contents.putInt(0x00); // reserved
contents.putShort((short) 0x0); // reserved contents.putShort((short) 0x0); // reserved
@ -536,7 +530,7 @@ import java.util.Locale;
int sampleRate = format.sampleRate; int sampleRate = format.sampleRate;
contents.putInt(sampleRate << 16); contents.putInt(sampleRate << 16);
contents.put(audioEsdsBox(csd0ByteBuffer, format.peakBitrate, format.averageBitrate)); contents.put(codecSpecificBox);
contents.flip(); contents.flip();
return BoxUtils.wrapIntoBox(fourcc, contents); return BoxUtils.wrapIntoBox(fourcc, contents);
@ -546,14 +540,16 @@ import java.util.Locale;
public static ByteBuffer codecSpecificBox(Format format) { public static ByteBuffer codecSpecificBox(Format format) {
String mimeType = checkNotNull(format.sampleMimeType); String mimeType = checkNotNull(format.sampleMimeType);
switch (mimeType) { switch (mimeType) {
case "video/avc": case MimeTypes.AUDIO_AAC:
return esdsBox(format);
case MimeTypes.VIDEO_H264:
return avcCBox(format); return avcCBox(format);
case "video/hevc": case MimeTypes.VIDEO_H265:
return hvcCBox(format); return hvcCBox(format);
case "video/av01": case MimeTypes.VIDEO_AV1:
return av1CBox(format); return av1CBox(format);
default: default:
throw new IllegalArgumentException("Unsupported video format: " + mimeType); throw new IllegalArgumentException("Unsupported format: " + mimeType);
} }
} }
@ -1318,24 +1314,33 @@ import java.util.Locale;
return BoxUtils.wrapIntoBox("colr", contents); return BoxUtils.wrapIntoBox("colr", contents);
} }
/** Returns video codec specific fourcc. */ /** Returns codec specific fourcc. */
private static String codecSpecificFourcc(Format format) { private static String codecSpecificFourcc(Format format) {
String mimeType = checkNotNull(format.sampleMimeType); String mimeType = checkNotNull(format.sampleMimeType);
switch (mimeType) { switch (mimeType) {
case "video/avc": case MimeTypes.AUDIO_AAC:
return "mp4a";
case MimeTypes.VIDEO_H264:
return "avc1"; return "avc1";
case "video/hevc": case MimeTypes.VIDEO_H265:
return "hvc1"; return "hvc1";
case "video/av01": case MimeTypes.VIDEO_AV1:
return "av01"; return "av01";
default: default:
throw new IllegalArgumentException("Unsupported video format: " + mimeType); throw new IllegalArgumentException("Unsupported format: " + mimeType);
} }
} }
/** Returns the esds box. */ /** Returns the esds box. */
private static ByteBuffer audioEsdsBox( private static ByteBuffer esdsBox(Format format) {
ByteBuffer csd0ByteBuffer, int peakBitrate, int averageBitrate) { checkArgument(!format.initializationData.isEmpty(), "csd-0 not found in the format.");
byte[] csd0 = format.initializationData.get(0);
checkArgument(csd0.length > 0, "csd-0 is empty.");
ByteBuffer csd0ByteBuffer = ByteBuffer.wrap(csd0);
int peakBitrate = format.peakBitrate;
int averageBitrate = format.averageBitrate;
int csd0Size = csd0ByteBuffer.limit(); int csd0Size = csd0ByteBuffer.limit();
ByteBuffer contents = ByteBuffer.allocate(csd0Size + MAX_FIXED_LEAF_BOX_SIZE); ByteBuffer contents = ByteBuffer.allocate(csd0Size + MAX_FIXED_LEAF_BOX_SIZE);