# EncoderUtil

Artifact: [androidx.media3:media3-transformer](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/transformer/EncoderUtil.java+class:androidx.media3.transformer.EncoderUtil)

*** ** * ** ***


```
@UnstableApi
public final class EncoderUtil
```

<br />

*** ** * ** ***

Utility methods for `https://developer.android.com/reference/android/media/MediaCodec.html` encoders.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#LEVEL_UNSET() = -1` A value to indicate the encoding level is not set. |

| ### Public methods |
|---|---|
| `synchronized static void` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#clearCachedEncoders()()` Clears the cached list of encoders. |
| `static int` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#findHighestSupportedEncodingLevel(android.media.MediaCodecInfo,java.lang.String,int)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType, int profile )` Finds the highest supported encoding level given a profile. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableSet.html<https://developer.android.com/reference/java/lang/Integer.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#findSupportedEncodingProfiles(android.media.MediaCodecInfo,java.lang.String)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType )` Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableSet.html` of supported `https://developer.android.com/reference/android/media/MediaCodecInfo.CodecProfileLevel.html` for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html` and `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `static int` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getClosestSupportedSampleRate(android.media.MediaCodecInfo,java.lang.String,int)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType, int requestedSampleRate )` Returns the sample rate supported by the provided `https://developer.android.com/reference/android/media/MediaCodecInfo.html` that is closest to the provided sample rate for a given `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Integer.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getCodecProfilesForHdrFormat(java.lang.String,int)( https://developer.android.com/reference/java/lang/String.html mimeType, @https://developer.android.com/reference/androidx/media3/common/C.ColorTransfer int colorTransfer )` Returns the `https://developer.android.com/reference/android/media/MediaCodecInfo.CodecProfileLevel.html#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` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getMaxSupportedInstances(android.media.MediaCodecInfo,java.lang.String)(https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType)` Returns the number of max number of the supported concurrent codec instances. |
| `static https://developer.android.com/reference/android/util/Range.html<https://developer.android.com/reference/java/lang/Integer.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedBitrateRange(android.media.MediaCodecInfo,java.lang.String)(https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType)` Returns the range of supported bitrates for the given `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Integer.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedColorFormats(android.media.MediaCodecInfo,java.lang.String)(https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType)` Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html` of supported `https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities.html#colorFormats` for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html` and `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `synchronized static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/android/media/MediaCodecInfo.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedEncoders(java.lang.String)(https://developer.android.com/reference/java/lang/String.html mimeType)` Returns a list of `https://developer.android.com/reference/android/media/MediaCodecInfo.html` that support the given `mimeType`, or an empty list if there is none. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/android/media/MediaCodecInfo.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedEncodersForHdrEditing(java.lang.String,androidx.media3.common.ColorInfo)( https://developer.android.com/reference/java/lang/String.html mimeType, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/ColorInfo colorInfo )` Returns a list of `https://developer.android.com/reference/android/media/MediaCodecInfo.html` 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 https://developer.android.com/reference/android/util/Range.html<https://developer.android.com/reference/java/lang/Integer.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedHeights(android.media.MediaCodecInfo,java.lang.String,int)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType, int width )` Returns a `https://developer.android.com/reference/android/util/Range.html` of supported heights for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html`, `https://developer.android.com/reference/androidx/media3/common/MimeTypes` and `width`. |
| `synchronized static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableSet.html<https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedMimeTypes()()` Returns a list of `https://developer.android.com/reference/androidx/media3/common/MimeTypes` that can be encoded. |
| `static @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/util/Size.html` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedResolution(android.media.MediaCodecInfo,java.lang.String,int,int)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType, int width, int height )` Finds an `https://developer.android.com/reference/android/media/MediaCodecInfo.html`'s supported resolution from a given resolution. |
| `static https://developer.android.com/reference/android/util/Pair.html<https://developer.android.com/reference/android/util/Range.html<https://developer.android.com/reference/java/lang/Integer.html>, https://developer.android.com/reference/android/util/Range.html<https://developer.android.com/reference/java/lang/Integer.html>>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedResolutionRanges(android.media.MediaCodecInfo,java.lang.String)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType )` Returns a `https://developer.android.com/reference/android/util/Pair.html` of supported width and height `https://developer.android.com/reference/android/util/Range.html` for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html` and `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#isBitrateModeSupported(android.media.MediaCodecInfo,java.lang.String,int)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType, int bitrateMode )` Returns whether the bitrate mode is supported by the encoder. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#isFeatureSupported(android.media.MediaCodecInfo,java.lang.String,java.lang.String)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType, https://developer.android.com/reference/java/lang/String.html featureName )` Returns whether a given feature is supported. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#isHardwareAccelerated(android.media.MediaCodecInfo,java.lang.String)(https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType)` Checks if a `https://developer.android.com/reference/android/media/MediaCodecInfo.html` is hardware-accelerated. |
| `static boolean` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 33) https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#isHdrEditingSupported(android.media.MediaCodecInfo,java.lang.String,androidx.media3.common.ColorInfo)( https://developer.android.com/reference/android/media/MediaCodecInfo.html mediaCodecInfo, https://developer.android.com/reference/java/lang/String.html mimeType, https://developer.android.com/reference/androidx/media3/common/ColorInfo colorInfo )` Returns whether HDR editing with the given `https://developer.android.com/reference/androidx/media3/common/ColorInfo` is supported by the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html`. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#isSizeSupported(android.media.MediaCodecInfo,java.lang.String,int,int)( https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo, https://developer.android.com/reference/java/lang/String.html mimeType, int width, int height )` Returns whether the `https://developer.android.com/reference/android/media/MediaCodecInfo.html` supports the given resolution for a specific `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `static https://developer.android.com/reference/android/util/Pair.html<https://developer.android.com/reference/java/lang/Integer.html, https://developer.android.com/reference/java/lang/Integer.html>` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#parseTemporalLayeringSchema(java.lang.String)(https://developer.android.com/reference/java/lang/String.html schema)` Parses a `https://developer.android.com/reference/android/media/MediaFormat.html#KEY_TEMPORAL_LAYERING` schema string into a `https://developer.android.com/reference/android/util/Pair.html` of the `https://developer.android.com/reference/androidx/media3/transformer/VideoEncoderSettings#numNonBidirectionalTemporalLayers()` and the `https://developer.android.com/reference/androidx/media3/transformer/VideoEncoderSettings#numBidirectionalTemporalLayers()`. |
| `static https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#toTemporalLayeringSchema(int,int)( int numNonBidirectionalLayers, int numBidirectionalLayers )` Returns the `https://developer.android.com/reference/android/media/MediaFormat.html#KEY_TEMPORAL_LAYERING` schema string for the given temporal layer counts. |

## 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.

### findHighestSupportedEncodingLevel

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

Finds the highest supported encoding level given a profile.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo` | The `https://developer.android.com/reference/android/media/MediaCodecInfo.html`. |
| `https://developer.android.com/reference/java/lang/String.html mimeType` | The `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `int profile` | The encoding profile. |

| Returns |
|---|---|
| `int` | The highest supported encoding level, as documented in `https://developer.android.com/reference/android/media/MediaCodecInfo.CodecProfileLevel.html`, or `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#LEVEL_UNSET()` if the profile is not supported. |

### findSupportedEncodingProfiles

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

Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableSet.html` of supported `https://developer.android.com/reference/android/media/MediaCodecInfo.CodecProfileLevel.html` for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html` and `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

### getClosestSupportedSampleRate

```
public static int getClosestSupportedSampleRate(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int requestedSampleRate
)
```

Returns the sample rate supported by the provided `https://developer.android.com/reference/android/media/MediaCodecInfo.html` that is closest to the provided sample rate for a given `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

### getCodecProfilesForHdrFormat

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

Returns the `https://developer.android.com/reference/android/media/MediaCodecInfo.CodecProfileLevel.html#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

```
public static int getMaxSupportedInstances(MediaCodecInfo encoderInfo, String mimeType)
```

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

### getSupportedBitrateRange

```
public static Range<Integer> getSupportedBitrateRange(MediaCodecInfo encoderInfo, String mimeType)
```

Returns the range of supported bitrates for the given `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

### getSupportedColorFormats

```
public static ImmutableList<Integer> getSupportedColorFormats(MediaCodecInfo encoderInfo, String mimeType)
```

Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html` of supported `https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities.html#colorFormats` for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html` and `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

### getSupportedEncoders

```
synchronized public static ImmutableList<MediaCodecInfo> getSupportedEncoders(String mimeType)
```

Returns a list of `https://developer.android.com/reference/android/media/MediaCodecInfo.html` that support the given `mimeType`, or an empty list if there is none.

### getSupportedEncodersForHdrEditing

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

Returns a list of `https://developer.android.com/reference/android/media/MediaCodecInfo.html` 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<Integer> getSupportedHeights(
    MediaCodecInfo encoderInfo,
    String mimeType,
    int width
)
```

Returns a `https://developer.android.com/reference/android/util/Range.html` of supported heights for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html`, `https://developer.android.com/reference/androidx/media3/common/MimeTypes` and `width`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | When the width is not in the range of `https://developer.android.com/reference/androidx/media3/transformer/EncoderUtil#getSupportedResolutionRanges(android.media.MediaCodecInfo,java.lang.String)`. |

### getSupportedMimeTypes

```
synchronized public static ImmutableSet<String> getSupportedMimeTypes()
```

Returns a list of `https://developer.android.com/reference/androidx/media3/common/MimeTypes` that can be encoded.

### getSupportedResolution

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

Finds an `https://developer.android.com/reference/android/media/MediaCodecInfo.html`'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 `https://developer.android.com/reference/android/media/MediaCodecInfo.html`.

The resolution will be adjusted to be within the `https://developer.android.com/reference/android/media/MediaCodecInfo.html`'s range of supported resolutions, and will be aligned to the `https://developer.android.com/reference/android/media/MediaCodecInfo.html`'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 |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodecInfo.html encoderInfo` | The `https://developer.android.com/reference/android/media/MediaCodecInfo.html` of the encoder. |
| `https://developer.android.com/reference/java/lang/String.html mimeType` | The output `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `int width` | The original width. |
| `int height` | The original height. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/util/Size.html` | A `https://developer.android.com/reference/android/util/Size.html`, or `null` if unable to find a fallback. |

### getSupportedResolutionRanges

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

Returns a `https://developer.android.com/reference/android/util/Pair.html` of supported width and height `https://developer.android.com/reference/android/util/Range.html` for the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html` and `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

### 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 `https://developer.android.com/reference/android/media/MediaCodecInfo.html` is hardware-accelerated.

### isHdrEditingSupported

```
@RequiresApi(value = 33)
public static boolean isHdrEditingSupported(
    MediaCodecInfo mediaCodecInfo,
    String mimeType,
    ColorInfo colorInfo
)
```

Returns whether HDR editing with the given `https://developer.android.com/reference/androidx/media3/common/ColorInfo` is supported by the given `https://developer.android.com/reference/android/media/MediaCodecInfo.html`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodecInfo.html mediaCodecInfo` | The encoder. |
| `https://developer.android.com/reference/java/lang/String.html mimeType` | The MIME type of the video stream. |
| `https://developer.android.com/reference/androidx/media3/common/ColorInfo colorInfo` | The color info. |

### isSizeSupported

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

Returns whether the `https://developer.android.com/reference/android/media/MediaCodecInfo.html` supports the given resolution for a specific `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

### parseTemporalLayeringSchema

```
public static Pair<Integer, Integer> parseTemporalLayeringSchema(String schema)
```

Parses a `https://developer.android.com/reference/android/media/MediaFormat.html#KEY_TEMPORAL_LAYERING` schema string into a `https://developer.android.com/reference/android/util/Pair.html` of the `https://developer.android.com/reference/androidx/media3/transformer/VideoEncoderSettings#numNonBidirectionalTemporalLayers()` and the `https://developer.android.com/reference/androidx/media3/transformer/VideoEncoderSettings#numBidirectionalTemporalLayers()`.

This method only supports `"none"`, which is parsed as zero layers, and the `
"android.generic.N[+M]"` schemas defined by `https://developer.android.com/reference/android/media/MediaFormat.html#KEY_TEMPORAL_LAYERING`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html schema` | The temporal layering schema string. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | If the schema is a `"webrtc.*"` schema. |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | If the schema is malformed or a layer count does not fit in an int. |

### toTemporalLayeringSchema

```
public static String toTemporalLayeringSchema(
    int numNonBidirectionalLayers,
    int numBidirectionalLayers
)
```

Returns the `https://developer.android.com/reference/android/media/MediaFormat.html#KEY_TEMPORAL_LAYERING` schema string for the given temporal layer counts.

Returns `"none"` if `numNonBidirectionalLayers` is zero, `
"android.generic.N"` if `numBidirectionalLayers` is zero, and `
"android.generic.N+M"` otherwise.

| Parameters |
|---|---|
| `int numNonBidirectionalLayers` | The number of non-bidirectional temporal layers. Must be non-negative. |
| `int numBidirectionalLayers` | The number of bidirectional temporal layers. Must be non-negative if `numNonBidirectionalLayers` is positive, and zero or `https://developer.android.com/reference/androidx/media3/common/Format#NO_VALUE()` if it is zero. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | If `numNonBidirectionalLayers` is negative, if it is positive and `numBidirectionalLayers` is negative, or if it is zero and ` numBidirectionalLayers` is neither zero nor `https://developer.android.com/reference/androidx/media3/common/Format#NO_VALUE()`. |