CodecSpecificDataUtil
@UnstableApi
public final class CodecSpecificDataUtil
Provides utilities for handling various types of codec-specific data.
Summary
Nested types |
|---|
public final class CodecSpecificDataUtil.MediaCodecProfileAndLevelA tuple of profile and level constants for |
Public methods |
|
|---|---|
static String |
buildApvCodecString(byte[] initializationData)Returns an APV codec string based on the provided initialization data. |
static String |
buildAvcCodecString(Builds an RFC 6381 AVC codec string using the provided parameters. |
static List<byte[]> |
buildCea708InitializationData(boolean isWideAspectRatio)Returns initialization data for formats with MIME type |
static String |
buildDolbyVisionCodecString(int profile, int level)Builds a Dolby Vision codec string using profile and level. |
static byte[] |
buildDolbyVisionInitializationData(int profile, int level)Returns initialization data for Dolby Vision according to Dolby Vision ISO MediaFormat (section 2.2) specification. |
static String |
buildH263CodecString(int profile, int level)Builds an RFC 6381 H263 codec string using profile and level. |
static String |
buildHevcCodecString(Builds an RFC 6381 HEVC codec string using the provided parameters. |
static @Nullable String |
buildIamfCodecString(byte[] initializationData)Builds an RFC 6381 IAMF codec string from the given |
static byte[] |
buildNalUnit(byte[] data, int offset, int length)Constructs a NAL unit consisting of the NAL start code followed by the specified data. |
static @Nullable byte[] |
Builds VP9 CodecPrivate from the uncompressed header of the first frame. |
static ImmutableList<byte[]> |
buildVp9CodecPrivateInitializationData(Returns initialization data in CodecPrivate format of VP9. |
static int |
calculateMediaCodecDolbyVisionLevel(Calculates the Dolby Vision level based on resolution and frame rate. |
static int |
dolbyVisionConstantToLevelNumber(int levelConstant)Returns Dolby Vision level number corresponding to the level constant. |
static int |
dolbyVisionConstantToProfileNumber(int profileConstant)Returns Dolby Vision profile number corresponding to the profile constant. |
static @Nullable Pair<Integer, Integer> |
getCodecProfileAndLevel(Format format)Returns profile and level (as defined by |
static @Nullable String |
Returns a Dolby Vision base layer codec MIME type of the provided |
static @Nullable CodecSpecificDataUtil.MediaCodecProfileAndLevel |
getHevcProfileAndLevel(Returns Hevc profile and level corresponding to the codec description string (as defined by RFC 6381) and it |
static @Nullable CodecSpecificDataUtil.MediaCodecProfileAndLevel |
getMediaCodecProfileAndLevel(Format format)Returns the profile and level corresponding to the codec description string (as defined by RFC 6381) of the given format. |
static byte[] |
getOpusInitializationData(Format format)Returns initialization data for Opus according to RFC 7845: 5.1. |
static Pair<Integer, Integer> |
getVideoResolutionFromMpeg4VideoConfig(byte[] videoSpecificConfig)Parses an MPEG-4 Visual configuration information, as defined in ISO/IEC14496-2. |
static ByteBuffer |
getVorbisInitializationData(Format format)Creates the Vorbis initialization data (CodecPrivate). |
static int |
h263ConstToLevelNumber(int levelConst)Maps Android MediaCodec constants to H.263 Level numbers. |
static int |
h263ConstToProfileNumber(int profileConst)Maps Android MediaCodec constants to H.263 Profile numbers. |
static int[] |
parseAlacAudioSpecificConfig(byte[] audioSpecificConfig)Parses an ALAC AudioSpecificConfig (i.e. an ALACSpecificConfig). |
static boolean |
parseCea708InitializationData(List<byte[]> initializationData)Returns whether the CEA-708 closed caption service with the given initialization data is formatted for displays with 16:9 aspect ratio. |
static @Nullable byte[][] |
splitNalUnits(byte[] data)Splits an array of NAL units. |
Public methods
buildApvCodecString
public static String buildApvCodecString(byte[] initializationData)
Returns an APV codec string based on the provided initialization data.
Reference: Codecs Parameter in APV ISO Base Media File Format
| Parameters | |
|---|---|
byte[] initializationData |
The initialization data containing profile, level, and band information. |
| Returns | |
|---|---|
String |
The generated APV codec string. |
buildAvcCodecString
public static String buildAvcCodecString(
int profileIdc,
int constraintsFlagsAndReservedZero2Bits,
int levelIdc
)
Builds an RFC 6381 AVC codec string using the provided parameters.
| Parameters | |
|---|---|
int profileIdc |
The encoding profile. |
int constraintsFlagsAndReservedZero2Bits |
The constraint flags followed by the reserved zero 2 bits, all contained in the least significant byte of the integer. |
int levelIdc |
The encoding level. |
| Returns | |
|---|---|
String |
An RFC 6381 AVC codec string built using the provided parameters. |
buildCea708InitializationData
public static List<byte[]> buildCea708InitializationData(boolean isWideAspectRatio)
Returns initialization data for formats with MIME type APPLICATION_CEA708.
| Parameters | |
|---|---|
boolean isWideAspectRatio |
Whether the CEA-708 closed caption service is formatted for displays with 16:9 aspect ratio. |
| Returns | |
|---|---|
List<byte[]> |
Initialization data for formats with MIME type |
buildDolbyVisionCodecString
public static String buildDolbyVisionCodecString(int profile, int level)
Builds a Dolby Vision codec string using profile and level.
Reference: href="https://professionalsupport.dolby.com/s/article/What-is-Dolby-Vision-Profile?language=en_US", >, Dolby Vision Profile and Level (section 2.3)
buildDolbyVisionInitializationData
public static byte[] buildDolbyVisionInitializationData(int profile, int level)
Returns initialization data for Dolby Vision according to Dolby Vision ISO MediaFormat (section 2.2) specification.
| Parameters | |
|---|---|
int profile |
The Dolby Vision codec profile. This is the integer profile, not the |
int level |
The Dolby Vision codec level. This is the integer level, not the |
buildH263CodecString
public static String buildH263CodecString(int profile, int level)
Builds an RFC 6381 H263 codec string using profile and level.
buildHevcCodecString
public static String buildHevcCodecString(
int generalProfileSpace,
boolean generalTierFlag,
int generalProfileIdc,
int generalProfileCompatibilityFlags,
int[] constraintBytes,
int generalLevelIdc
)
Builds an RFC 6381 HEVC codec string using the provided parameters.
buildIamfCodecString
public static @Nullable String buildIamfCodecString(byte[] initializationData)
Builds an RFC 6381 IAMF codec string from the given initializationData.
The format is defined by the IAMF Codec Parameters String specification.
buildNalUnit
public static byte[] buildNalUnit(byte[] data, int offset, int length)
Constructs a NAL unit consisting of the NAL start code followed by the specified data.
| Parameters | |
|---|---|
byte[] data |
An array containing the data that should follow the NAL start code. |
int offset |
The start offset into |
int length |
The number of bytes to copy from |
| Returns | |
|---|---|
byte[] |
The constructed NAL unit. |
buildVp9CodecPrivateFromUncompressedHeader
public static @Nullable byte[] buildVp9CodecPrivateFromUncompressedHeader(ByteBuffer sample)
Builds VP9 CodecPrivate from the uncompressed header of the first frame.
This method parses the uncompressed header of the first frame to extract the VP9 profile, bit depth, and chroma subsampling.
Refer to 6.2 of the VP9 bitstream specification and VP9 in ISO Media File Format for more details.
| Parameters | |
|---|---|
ByteBuffer sample |
The first frame's data. |
| Returns | |
|---|---|
@Nullable byte[] |
The CodecPrivate data for VP9 if parsing is successful, |
buildVp9CodecPrivateInitializationData
public static ImmutableList<byte[]> buildVp9CodecPrivateInitializationData(
byte profile,
byte level,
byte bitDepth,
byte chromaSubsampling
)
Returns initialization data in CodecPrivate format of VP9.
Each feature of VP9 CodecPrivate is defined by the binary format of ID (1 byte), length (1 byte), and data (1 byte). See href="https://www.webmproject.org/docs/container/#vp9-codec-feature-metadata-codecprivate", >, CodecPrivate format of VP9 for more details.
| Parameters | |
|---|---|
byte profile |
The VP9 codec profile. |
byte level |
The VP9 codec level. |
byte bitDepth |
The bit depth of the luma and color components. |
byte chromaSubsampling |
The chroma subsampling. |
calculateMediaCodecDolbyVisionLevel
public static int calculateMediaCodecDolbyVisionLevel(
int width,
int height,
float frameRate
)
Calculates the Dolby Vision level based on resolution and frame rate.
Refer to Dolby Vision profiles and levels for more details.
| Parameters | |
|---|---|
int width |
The video width in pixels. |
int height |
The video height in pixels. |
float frameRate |
The video frame rate in frames per second. |
| Returns | |
|---|---|
int |
The Android MediaCodec constants for the Dolby Vision level, or -1 if it could not be determined. |
dolbyVisionConstantToLevelNumber
public static int dolbyVisionConstantToLevelNumber(int levelConstant)
Returns Dolby Vision level number corresponding to the level constant.
| Parameters | |
|---|---|
int levelConstant |
The Dolby Vision level constant. |
| Returns | |
|---|---|
int |
The Dolby Vision level number. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the level constant is not recognized. |
dolbyVisionConstantToProfileNumber
public static int dolbyVisionConstantToProfileNumber(int profileConstant)
Returns Dolby Vision profile number corresponding to the profile constant.
| Parameters | |
|---|---|
int profileConstant |
The Dolby Vision profile constant. |
| Returns | |
|---|---|
int |
The Dolby Vision profile number. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the profile constant is not recognized. |
getCodecProfileAndLevel
public static @Nullable Pair<Integer, Integer> getCodecProfileAndLevel(Format format)
Returns profile and level (as defined by MediaCodecInfo.CodecProfileLevel) corresponding to the codec description string (as defined by RFC 6381) of the given format.
Consider using getMediaCodecProfileAndLevel instead in order to distinguish between:
- The
formatdoesn't contain profile and level info, or it is malformed. - The
formatcontains profile and level info, but it cannot be represented with constants fromMediaCodecInfo.CodecProfileLevel.
| Parameters | |
|---|---|
Format format |
Media format with a codec description string, as defined by RFC 6381. |
getDolbyVisionBaseLayerMimeType
public static @Nullable String getDolbyVisionBaseLayerMimeType(Format format)
Returns a Dolby Vision base layer codec MIME type of the provided Format.
| Parameters | |
|---|---|
Format format |
The media format. |
getHevcProfileAndLevel
public static @Nullable CodecSpecificDataUtil.MediaCodecProfileAndLevel getHevcProfileAndLevel(
String codec,
String[] parts,
@Nullable ColorInfo colorInfo
)
Returns Hevc profile and level corresponding to the codec description string (as defined by RFC 6381) and it ColorInfo.
| Parameters | |
|---|---|
String codec |
The codec description string (as defined by RFC 6381). |
String[] parts |
The codec string split by ".". |
@Nullable ColorInfo colorInfo |
The |
| Returns | |
|---|---|
@Nullable CodecSpecificDataUtil.MediaCodecProfileAndLevel |
A pair (profile constant, level constant) if profile and level are recognized, or |
getMediaCodecProfileAndLevel
public static @Nullable CodecSpecificDataUtil.MediaCodecProfileAndLevel getMediaCodecProfileAndLevel(Format format)
Returns the profile and level corresponding to the codec description string (as defined by RFC 6381) of the given format.
| Parameters | |
|---|---|
Format format |
Media format with a codec description string, as defined by RFC 6381. |
| Returns | |
|---|---|
@Nullable CodecSpecificDataUtil.MediaCodecProfileAndLevel |
|
getOpusInitializationData
public static byte[] getOpusInitializationData(Format format)
Returns initialization data for Opus according to RFC 7845: 5.1.
| Returns | |
|---|---|
byte[] |
A byte array containing the Opus initialization data. |
getVideoResolutionFromMpeg4VideoConfig
public static Pair<Integer, Integer> getVideoResolutionFromMpeg4VideoConfig(byte[] videoSpecificConfig)
Parses an MPEG-4 Visual configuration information, as defined in ISO/IEC14496-2.
| Parameters | |
|---|---|
byte[] videoSpecificConfig |
A byte array containing the MPEG-4 Visual configuration information to parse. |
getVorbisInitializationData
public static ByteBuffer getVorbisInitializationData(Format format)
Creates the Vorbis initialization data (CodecPrivate).
The format is as follows:
- Byte 1: The number of packets minus one. This is 2 for Vorbis, representing the Identification, Comment, and Setup headers.
- Bytes 2..n: The lengths of the first two packets (Identification and Comment headers), encoded using Xiph lacing. The length of the final packet (Setup header) is not explicitly stored.
- Bytes n+1 onwards: The Vorbis identification header, Vorbis comment header and the codec setup header.
| Returns | |
|---|---|
ByteBuffer |
A |
h263ConstToLevelNumber
public static int h263ConstToLevelNumber(int levelConst)
Maps Android MediaCodec constants to H.263 Level numbers.
| Parameters | |
|---|---|
int levelConst |
The MediaCodec H.263 level constant. |
| Returns | |
|---|---|
int |
The H.263 level number, or -1 if the constant is not recognized. |
h263ConstToProfileNumber
public static int h263ConstToProfileNumber(int profileConst)
Maps Android MediaCodec constants to H.263 Profile numbers.
| Parameters | |
|---|---|
int profileConst |
The MediaCodec H.263 profile constant. |
| Returns | |
|---|---|
int |
The H.263 profile number, or -1 if the constant is not recognized. |
parseAlacAudioSpecificConfig
public static int[] parseAlacAudioSpecificConfig(byte[] audioSpecificConfig)
Parses an ALAC AudioSpecificConfig (i.e. an ALACSpecificConfig).
| Parameters | |
|---|---|
byte[] audioSpecificConfig |
A byte array containing the AudioSpecificConfig to parse. |
| Returns | |
|---|---|
int[] |
An int array consisting of the sample rate in Hz, the channel count and the bit depth. |
parseCea708InitializationData
public static boolean parseCea708InitializationData(List<byte[]> initializationData)
Returns whether the CEA-708 closed caption service with the given initialization data is formatted for displays with 16:9 aspect ratio.
| Parameters | |
|---|---|
List<byte[]> initializationData |
The initialization data to parse. |
| Returns | |
|---|---|
boolean |
Whether the CEA-708 closed caption service is formatted for displays with 16:9 aspect ratio. |
splitNalUnits
public static @Nullable byte[][] splitNalUnits(byte[] data)
Splits an array of NAL units.
If the input consists of NAL start code delimited units, then the returned array consists of the split NAL units, each of which is still prefixed with the NAL start code. For any other input, null is returned.
| Parameters | |
|---|---|
byte[] data |
An array of data. |
| Returns | |
|---|---|
@Nullable byte[][] |
The individual NAL units, or null if the input did not consist of NAL start code delimited units. |