@UnstableApi
public final class EncoderUtil


Utility methods for MediaCodec encoders.

Summary

Constants

static final int

A value to indicate the encoding level is not set.

Public methods

synchronized static void

Clears the cached list of encoders.

static @Nullable String
findCodecForFormat(MediaFormat format, boolean isDecoder)

Finds a MediaCodec that supports the MediaFormat, or null if none is found.

static int
findHighestSupportedEncodingLevel(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int profile
)

Finds the highest supported encoding level given a profile.

static ImmutableSet<Integer>
findSupportedEncodingProfiles(
    MediaCodecInfo encoderInfo,
    String mimeType
)

Returns a set of supported encoding profiles for the given encoder and MIME type.

static ImmutableList<Integer>
getCodecProfilesForHdrFormat(
    String mimeType,
    @C.ColorTransfer int colorTransfer
)

Returns the profile constants that can be used to encode the given HDR format, if supported by the device (this method does not check device capabilities).

static int
@RequiresApi(value = 23)
getMaxSupportedInstances(MediaCodecInfo encoderInfo, String mimeType)

Returns the number of max number of the supported concurrent codec instances.

static Range<Integer>

Returns the range of supported bitrates for the given MIME type.

static ImmutableList<Integer>

Returns a list of supported color formats for the given encoder and MIME type.

synchronized static ImmutableList<MediaCodecInfo>

Returns a list of encoders that support the given mimeType, or an empty list if there is none.

static ImmutableList<MediaCodecInfo>
getSupportedEncodersForHdrEditing(
    String mimeType,
    @Nullable ColorInfo colorInfo
)

Returns a list of encoders that support HDR editing for the given mimeType and ColorInfo, or an empty list if the format is unknown or not supported for HDR encoding.

static Range<Integer>
getSupportedHeights(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int width
)

Returns a Range of supported heights for the given encoder, MIME type and width.

synchronized static ImmutableSet<String>

Returns a list of MIME types that can be encoded.

static @Nullable Size
getSupportedResolution(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int width,
    int height
)

Finds an encoder's supported resolution from a given resolution.

static Pair<Range<Integer>, Range<Integer>>
getSupportedResolutionRanges(
    MediaCodecInfo encoderInfo,
    String mimeType
)

Returns a Pair of supported width and height ranges for the given encoder and MIME type.

static boolean
isBitrateModeSupported(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int bitrateMode
)

Returns whether the bitrate mode is supported by the encoder.

static boolean
isFeatureSupported(
    MediaCodecInfo encoderInfo,
    String mimeType,
    String featureName
)

Returns whether a given feature is supported.

static boolean
isHardwareAccelerated(MediaCodecInfo encoderInfo, String mimeType)

Checks if a codec is hardware-accelerated.

static boolean
isSizeSupported(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int width,
    int height
)

Returns whether the encoder supports the given resolution.

Constants

LEVEL_UNSET

public static final int LEVEL_UNSET = -1

A value to indicate the encoding level is not set.

Public methods

clearCachedEncoders

@VisibleForTesting
synchronized public static void clearCachedEncoders()

Clears the cached list of encoders.

findCodecForFormat

public static @Nullable String findCodecForFormat(MediaFormat format, boolean isDecoder)

Finds a MediaCodec that supports the MediaFormat, or null if none is found.

findHighestSupportedEncodingLevel

public static int findHighestSupportedEncodingLevel(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int profile
)

Finds the highest supported encoding level given a profile.

Parameters
MediaCodecInfo encoderInfo

The encoderInfo.

String mimeType

The MIME type.

int profile

The encoding profile.

Returns
int

The highest supported encoding level, as documented in , or LEVEL_UNSET if the profile is not supported.

findSupportedEncodingProfiles

public static ImmutableSet<IntegerfindSupportedEncodingProfiles(
    MediaCodecInfo encoderInfo,
    String mimeType
)

Returns a set of supported encoding profiles for the given encoder and MIME type.

getCodecProfilesForHdrFormat

public static ImmutableList<IntegergetCodecProfilesForHdrFormat(
    String mimeType,
    @C.ColorTransfer int colorTransfer
)

Returns the profile constants that can be used to encode the given HDR format, if supported by the device (this method does not check device capabilities). If multiple profiles are returned, they are ordered by expected level of compatibility, with the most widely compatible profile first.

getMaxSupportedInstances

@RequiresApi(value = 23)
public static int getMaxSupportedInstances(MediaCodecInfo encoderInfo, String mimeType)

Returns the number of max number of the supported concurrent codec instances.

getSupportedBitrateRange

public static Range<IntegergetSupportedBitrateRange(MediaCodecInfo encoderInfo, String mimeType)

Returns the range of supported bitrates for the given MIME type.

getSupportedColorFormats

public static ImmutableList<IntegergetSupportedColorFormats(MediaCodecInfo encoderInfo, String mimeType)

Returns a list of supported color formats for the given encoder and MIME type.

getSupportedEncoders

synchronized public static ImmutableList<MediaCodecInfogetSupportedEncoders(String mimeType)

Returns a list of encoders that support the given mimeType, or an empty list if there is none.

getSupportedEncodersForHdrEditing

public static ImmutableList<MediaCodecInfogetSupportedEncodersForHdrEditing(
    String mimeType,
    @Nullable ColorInfo colorInfo
)

Returns a list of encoders that support HDR editing for the given mimeType and ColorInfo, or an empty list if the format is unknown or not supported for HDR encoding.

getSupportedHeights

public static Range<IntegergetSupportedHeights(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int width
)

Returns a Range of supported heights for the given encoder, MIME type and width.

Throws
java.lang.IllegalArgumentException

When the width is not in the range of supported widths.

getSupportedMimeTypes

synchronized public static ImmutableSet<StringgetSupportedMimeTypes()

Returns a list of MIME types that can be encoded.

getSupportedResolution

public static @Nullable Size getSupportedResolution(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int width,
    int height
)

Finds an encoder's supported resolution from a given resolution.

The input resolution is returned, if it (after aligning to the encoder's requirement) is supported by the encoder.

The resolution will be adjusted to be within the encoder's range of supported resolutions, and will be aligned to the encoder's alignment requirement. The adjustment process takes into account the original aspect ratio. But the fixed resolution may not preserve the original aspect ratio, depending on the encoder's required size alignment.

Parameters
MediaCodecInfo encoderInfo

The MediaCodecInfo of the encoder.

String mimeType

The output MIME type.

int width

The original width.

int height

The original height.

Returns
@Nullable Size

A supported resolution, or null if unable to find a fallback.

getSupportedResolutionRanges

public static Pair<Range<Integer>, Range<Integer>> getSupportedResolutionRanges(
    MediaCodecInfo encoderInfo,
    String mimeType
)

Returns a Pair of supported width and height ranges for the given encoder and MIME type.

isBitrateModeSupported

public static boolean isBitrateModeSupported(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int bitrateMode
)

Returns whether the bitrate mode is supported by the encoder.

isFeatureSupported

public static boolean isFeatureSupported(
    MediaCodecInfo encoderInfo,
    String mimeType,
    String featureName
)

Returns whether a given feature is supported.

isHardwareAccelerated

public static boolean isHardwareAccelerated(MediaCodecInfo encoderInfo, String mimeType)

Checks if a codec is hardware-accelerated.

isSizeSupported

public static boolean isSizeSupported(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int width,
    int height
)

Returns whether the encoder supports the given resolution.