Move atom parsing logic from Atom to AtomParsers

This is groundwork to moving `Atom` to the `container` library, which
we want to do before making it public (so it can be used by `muxer` in
future).

PiperOrigin-RevId: 662453520
This commit is contained in:
ibaker 2024-08-13 03:47:52 -07:00 committed by Copybara-Service
parent 96f2c7ece7
commit 9dfd72b6c6
6 changed files with 31 additions and 31 deletions

View File

@ -586,16 +586,6 @@ import java.util.List;
} }
} }
/** Parses the version number out of the additional integer component of a full atom. */
public static int parseFullAtomVersion(int fullAtomInt) {
return 0x000000FF & (fullAtomInt >> 24);
}
/** Parses the atom flags out of the additional integer component of a full atom. */
public static int parseFullAtomFlags(int fullAtomInt) {
return 0x00FFFFFF & fullAtomInt;
}
/** /**
* Converts a numeric atom type to the corresponding four character string. * Converts a numeric atom type to the corresponding four character string.
* *

View File

@ -104,6 +104,16 @@ import java.util.Objects;
/** The magic signature for an Opus Identification header, as defined in RFC-7845. */ /** The magic signature for an Opus Identification header, as defined in RFC-7845. */
private static final byte[] opusMagic = Util.getUtf8Bytes("OpusHead"); private static final byte[] opusMagic = Util.getUtf8Bytes("OpusHead");
/** Parses the version number out of the additional integer component of a full atom. */
public static int parseFullAtomVersion(int fullAtomInt) {
return 0x000000FF & (fullAtomInt >> 24);
}
/** Parses the atom flags out of the additional integer component of a full atom. */
public static int parseFullAtomFlags(int fullAtomInt) {
return 0x00FFFFFF & fullAtomInt;
}
/** /**
* Parse the trak atoms in a moov atom (defined in ISO/IEC 14496-12). * Parse the trak atoms in a moov atom (defined in ISO/IEC 14496-12).
* *
@ -198,7 +208,7 @@ import java.util.Objects;
public static Mp4TimestampData parseMvhd(ParsableByteArray mvhd) { public static Mp4TimestampData parseMvhd(ParsableByteArray mvhd) {
mvhd.setPosition(Atom.HEADER_SIZE); mvhd.setPosition(Atom.HEADER_SIZE);
int fullAtom = mvhd.readInt(); int fullAtom = mvhd.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = parseFullAtomVersion(fullAtom);
long creationTimestampSeconds; long creationTimestampSeconds;
long modificationTimestampSeconds; long modificationTimestampSeconds;
if (version == 0) { if (version == 0) {
@ -840,7 +850,7 @@ import java.util.Objects;
private static TkhdData parseTkhd(ParsableByteArray tkhd) { private static TkhdData parseTkhd(ParsableByteArray tkhd) {
tkhd.setPosition(Atom.HEADER_SIZE); tkhd.setPosition(Atom.HEADER_SIZE);
int fullAtom = tkhd.readInt(); int fullAtom = tkhd.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = parseFullAtomVersion(fullAtom);
tkhd.skipBytes(version == 0 ? 8 : 16); tkhd.skipBytes(version == 0 ? 8 : 16);
int trackId = tkhd.readInt(); int trackId = tkhd.readInt();
@ -927,7 +937,7 @@ import java.util.Objects;
private static Pair<Long, String> parseMdhd(ParsableByteArray mdhd) { private static Pair<Long, String> parseMdhd(ParsableByteArray mdhd) {
mdhd.setPosition(Atom.HEADER_SIZE); mdhd.setPosition(Atom.HEADER_SIZE);
int fullAtom = mdhd.readInt(); int fullAtom = mdhd.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = parseFullAtomVersion(fullAtom);
mdhd.skipBytes(version == 0 ? 8 : 16); mdhd.skipBytes(version == 0 ? 8 : 16);
long timescale = mdhd.readUnsignedInt(); long timescale = mdhd.readUnsignedInt();
mdhd.skipBytes(version == 0 ? 4 : 8); mdhd.skipBytes(version == 0 ? 4 : 8);
@ -1636,7 +1646,7 @@ import java.util.Objects;
ParsableByteArray elstData = elstAtom.data; ParsableByteArray elstData = elstAtom.data;
elstData.setPosition(Atom.HEADER_SIZE); elstData.setPosition(Atom.HEADER_SIZE);
int fullAtom = elstData.readInt(); int fullAtom = elstData.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = parseFullAtomVersion(fullAtom);
int entryCount = elstData.readUnsignedIntToInt(); int entryCount = elstData.readUnsignedIntToInt();
long[] editListDurations = new long[entryCount]; long[] editListDurations = new long[entryCount];
long[] editListMediaTimes = new long[entryCount]; long[] editListMediaTimes = new long[entryCount];
@ -2188,7 +2198,7 @@ import java.util.Objects;
int childAtomType = parent.readInt(); int childAtomType = parent.readInt();
if (childAtomType == Atom.TYPE_tenc) { if (childAtomType == Atom.TYPE_tenc) {
int fullAtom = parent.readInt(); int fullAtom = parent.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = parseFullAtomVersion(fullAtom);
parent.skipBytes(1); // reserved = 0. parent.skipBytes(1); // reserved = 0.
int defaultCryptByteBlock = 0; int defaultCryptByteBlock = 0;
int defaultSkipByteBlock = 0; int defaultSkipByteBlock = 0;

View File

@ -777,7 +777,7 @@ public class FragmentedMp4Extractor implements Extractor {
} }
atom.setPosition(Atom.HEADER_SIZE); atom.setPosition(Atom.HEADER_SIZE);
int fullAtom = atom.readInt(); int fullAtom = atom.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = AtomParsers.parseFullAtomVersion(fullAtom);
String schemeIdUri; String schemeIdUri;
String value; String value;
long timescale; long timescale;
@ -883,7 +883,7 @@ public class FragmentedMp4Extractor implements Extractor {
private static long parseMehd(ParsableByteArray mehd) { private static long parseMehd(ParsableByteArray mehd) {
mehd.setPosition(Atom.HEADER_SIZE); mehd.setPosition(Atom.HEADER_SIZE);
int fullAtom = mehd.readInt(); int fullAtom = mehd.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = AtomParsers.parseFullAtomVersion(fullAtom);
return version == 0 ? mehd.readUnsignedInt() : mehd.readUnsignedLongToLong(); return version == 0 ? mehd.readUnsignedInt() : mehd.readUnsignedLongToLong();
} }
@ -1005,7 +1005,7 @@ public class FragmentedMp4Extractor implements Extractor {
int vectorSize = encryptionBox.perSampleIvSize; int vectorSize = encryptionBox.perSampleIvSize;
saiz.setPosition(Atom.HEADER_SIZE); saiz.setPosition(Atom.HEADER_SIZE);
int fullAtom = saiz.readInt(); int fullAtom = saiz.readInt();
int flags = Atom.parseFullAtomFlags(fullAtom); int flags = AtomParsers.parseFullAtomFlags(fullAtom);
if ((flags & 0x01) == 1) { if ((flags & 0x01) == 1) {
saiz.skipBytes(8); saiz.skipBytes(8);
} }
@ -1049,7 +1049,7 @@ public class FragmentedMp4Extractor implements Extractor {
private static void parseSaio(ParsableByteArray saio, TrackFragment out) throws ParserException { private static void parseSaio(ParsableByteArray saio, TrackFragment out) throws ParserException {
saio.setPosition(Atom.HEADER_SIZE); saio.setPosition(Atom.HEADER_SIZE);
int fullAtom = saio.readInt(); int fullAtom = saio.readInt();
int flags = Atom.parseFullAtomFlags(fullAtom); int flags = AtomParsers.parseFullAtomFlags(fullAtom);
if ((flags & 0x01) == 1) { if ((flags & 0x01) == 1) {
saio.skipBytes(8); saio.skipBytes(8);
} }
@ -1061,7 +1061,7 @@ public class FragmentedMp4Extractor implements Extractor {
"Unexpected saio entry count: " + entryCount, /* cause= */ null); "Unexpected saio entry count: " + entryCount, /* cause= */ null);
} }
int version = Atom.parseFullAtomVersion(fullAtom); int version = AtomParsers.parseFullAtomVersion(fullAtom);
out.auxiliaryDataPosition += out.auxiliaryDataPosition +=
version == 0 ? saio.readUnsignedInt() : saio.readUnsignedLongToLong(); version == 0 ? saio.readUnsignedInt() : saio.readUnsignedLongToLong();
} }
@ -1083,7 +1083,7 @@ public class FragmentedMp4Extractor implements Extractor {
ParsableByteArray tfhd, SparseArray<TrackBundle> trackBundles, boolean haveSideloadedTrack) { ParsableByteArray tfhd, SparseArray<TrackBundle> trackBundles, boolean haveSideloadedTrack) {
tfhd.setPosition(Atom.HEADER_SIZE); tfhd.setPosition(Atom.HEADER_SIZE);
int fullAtom = tfhd.readInt(); int fullAtom = tfhd.readInt();
int atomFlags = Atom.parseFullAtomFlags(fullAtom); int atomFlags = AtomParsers.parseFullAtomFlags(fullAtom);
int trackId = tfhd.readInt(); int trackId = tfhd.readInt();
@Nullable @Nullable
TrackBundle trackBundle = TrackBundle trackBundle =
@ -1132,7 +1132,7 @@ public class FragmentedMp4Extractor implements Extractor {
private static long parseTfdt(ParsableByteArray tfdt) { private static long parseTfdt(ParsableByteArray tfdt) {
tfdt.setPosition(Atom.HEADER_SIZE); tfdt.setPosition(Atom.HEADER_SIZE);
int fullAtom = tfdt.readInt(); int fullAtom = tfdt.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = AtomParsers.parseFullAtomVersion(fullAtom);
return version == 1 ? tfdt.readUnsignedLongToLong() : tfdt.readUnsignedInt(); return version == 1 ? tfdt.readUnsignedLongToLong() : tfdt.readUnsignedInt();
} }
@ -1175,7 +1175,7 @@ public class FragmentedMp4Extractor implements Extractor {
throws ParserException { throws ParserException {
trun.setPosition(Atom.HEADER_SIZE); trun.setPosition(Atom.HEADER_SIZE);
int fullAtom = trun.readInt(); int fullAtom = trun.readInt();
int atomFlags = Atom.parseFullAtomFlags(fullAtom); int atomFlags = AtomParsers.parseFullAtomFlags(fullAtom);
Track track = trackBundle.moovSampleTable.track; Track track = trackBundle.moovSampleTable.track;
TrackFragment fragment = trackBundle.fragment; TrackFragment fragment = trackBundle.fragment;
@ -1286,7 +1286,7 @@ public class FragmentedMp4Extractor implements Extractor {
throws ParserException { throws ParserException {
senc.setPosition(Atom.HEADER_SIZE + offset); senc.setPosition(Atom.HEADER_SIZE + offset);
int fullAtom = senc.readInt(); int fullAtom = senc.readInt();
int flags = Atom.parseFullAtomFlags(fullAtom); int flags = AtomParsers.parseFullAtomFlags(fullAtom);
if ((flags & 0x01 /* override_track_encryption_box_parameters */) != 0) { if ((flags & 0x01 /* override_track_encryption_box_parameters */) != 0) {
// TODO: Implement this. // TODO: Implement this.
@ -1339,7 +1339,7 @@ public class FragmentedMp4Extractor implements Extractor {
} }
sbgp.setPosition(Atom.HEADER_SIZE); sbgp.setPosition(Atom.HEADER_SIZE);
int sbgpVersion = Atom.parseFullAtomVersion(sbgp.readInt()); int sbgpVersion = AtomParsers.parseFullAtomVersion(sbgp.readInt());
sbgp.skipBytes(4); // grouping_type == seig. sbgp.skipBytes(4); // grouping_type == seig.
if (sbgpVersion == 1) { if (sbgpVersion == 1) {
sbgp.skipBytes(4); // grouping_type_parameter. sbgp.skipBytes(4); // grouping_type_parameter.
@ -1350,7 +1350,7 @@ public class FragmentedMp4Extractor implements Extractor {
} }
sgpd.setPosition(Atom.HEADER_SIZE); sgpd.setPosition(Atom.HEADER_SIZE);
int sgpdVersion = Atom.parseFullAtomVersion(sgpd.readInt()); int sgpdVersion = AtomParsers.parseFullAtomVersion(sgpd.readInt());
sgpd.skipBytes(4); // grouping_type == seig. sgpd.skipBytes(4); // grouping_type == seig.
if (sgpdVersion == 1) { if (sgpdVersion == 1) {
if (sgpd.readUnsignedInt() == 0) { if (sgpd.readUnsignedInt() == 0) {
@ -1407,7 +1407,7 @@ public class FragmentedMp4Extractor implements Extractor {
throws ParserException { throws ParserException {
atom.setPosition(Atom.HEADER_SIZE); atom.setPosition(Atom.HEADER_SIZE);
int fullAtom = atom.readInt(); int fullAtom = atom.readInt();
int version = Atom.parseFullAtomVersion(fullAtom); int version = AtomParsers.parseFullAtomVersion(fullAtom);
atom.skipBytes(4); atom.skipBytes(4);
long timescale = atom.readUnsignedInt(); long timescale = atom.readUnsignedInt();

View File

@ -350,7 +350,7 @@ import com.google.common.collect.ImmutableList;
int atomType = data.readInt(); int atomType = data.readInt();
if (atomType == Atom.TYPE_data) { if (atomType == Atom.TYPE_data) {
int fullVersionInt = data.readInt(); int fullVersionInt = data.readInt();
int flags = Atom.parseFullAtomFlags(fullVersionInt); int flags = AtomParsers.parseFullAtomFlags(fullVersionInt);
@Nullable String mimeType = flags == 13 ? "image/jpeg" : flags == 14 ? "image/png" : null; @Nullable String mimeType = flags == 13 ? "image/jpeg" : flags == 14 ? "image/png" : null;
if (mimeType == null) { if (mimeType == null) {
Log.w(TAG, "Unrecognized cover art flags: " + flags); Log.w(TAG, "Unrecognized cover art flags: " + flags);

View File

@ -175,7 +175,7 @@ public final class PsshAtomUtil {
Log.w(TAG, "Atom type is not pssh: " + atomType); Log.w(TAG, "Atom type is not pssh: " + atomType);
return null; return null;
} }
int atomVersion = Atom.parseFullAtomVersion(atomData.readInt()); int atomVersion = AtomParsers.parseFullAtomVersion(atomData.readInt());
if (atomVersion > 1) { if (atomVersion > 1) {
Log.w(TAG, "Unsupported pssh version: " + atomVersion); Log.w(TAG, "Unsupported pssh version: " + atomVersion);
return null; return null;

View File

@ -17,8 +17,8 @@ package androidx.media3.extractor.mp4;
import static androidx.media3.common.C.WIDEVINE_UUID; import static androidx.media3.common.C.WIDEVINE_UUID;
import static androidx.media3.extractor.mp4.Atom.TYPE_pssh; import static androidx.media3.extractor.mp4.Atom.TYPE_pssh;
import static androidx.media3.extractor.mp4.Atom.parseFullAtomFlags; import static androidx.media3.extractor.mp4.AtomParsers.parseFullAtomFlags;
import static androidx.media3.extractor.mp4.Atom.parseFullAtomVersion; import static androidx.media3.extractor.mp4.AtomParsers.parseFullAtomVersion;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import androidx.media3.common.C; import androidx.media3.common.C;