| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import android.annotation.TargetApi; |
| 14 | import android.media.MediaCodecInfo; |
| 15 | import android.media.MediaCodecInfo.CodecCapabilities; |
| 16 | import android.os.Build; |
| 17 | import androidx.annotation.Nullable; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 |
| 21 | /** Container class for static constants and helpers used with MediaCodec. */ |
| 22 | // We are forced to use the old API because we want to support API level < 21. |
| 23 | @SuppressWarnings("deprecation") |
| 24 | class MediaCodecUtils { |
| 25 | private static final String TAG = "MediaCodecUtils"; |
| 26 |
| 27 | // Prefixes for supported hardware encoder/decoder component names. |
| 28 | static final String EXYNOS_PREFIX = "OMX.Exynos."; |
| 29 | static final String INTEL_PREFIX = "OMX.Intel."; |
| 30 | static final String NVIDIA_PREFIX = "OMX.Nvidia."; |
| 31 | static final String QCOM_PREFIX = "OMX.qcom."; |
| 32 | static final String[] SOFTWARE_IMPLEMENTATION_PREFIXES = { |
| 33 | "OMX.google.", "OMX.SEC.", "c2.android"}; |
| 34 |
| 35 | // NV12 color format supported by QCOM codec, but not declared in MediaCodec - |
| 36 | // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h |
| 37 | static final int COLOR_QCOM_FORMATYVU420PackedSemiPlanar32m4ka = 0x7FA30C01; |
| 38 | static final int COLOR_QCOM_FORMATYVU420PackedSemiPlanar16m4ka = 0x7FA30C02; |
| 39 | static final int COLOR_QCOM_FORMATYVU420PackedSemiPlanar64x32Tile2m8ka = 0x7FA30C03; |
| 40 | static final int COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04; |
| 41 |
| 42 | // Color formats supported by hardware decoder - in order of preference. |
| 43 | static final int[] DECODER_COLOR_FORMATS = new int[] {CodecCapabilities.COLOR_FormatYUV420Planar, |
| 44 | CodecCapabilities.COLOR_FormatYUV420SemiPlanar, |
| 45 | CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar, |
| 46 | MediaCodecUtils.COLOR_QCOM_FORMATYVU420PackedSemiPlanar32m4ka, |
| 47 | MediaCodecUtils.COLOR_QCOM_FORMATYVU420PackedSemiPlanar16m4ka, |
| 48 | MediaCodecUtils.COLOR_QCOM_FORMATYVU420PackedSemiPlanar64x32Tile2m8ka, |
| 49 | MediaCodecUtils.COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m}; |
| 50 |
| 51 | // Color formats supported by hardware encoder - in order of preference. |
| 52 | static final int[] ENCODER_COLOR_FORMATS = { |
| 53 | MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar, |
| 54 | MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar, |
| 55 | MediaCodecInfo.CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar, |
| 56 | MediaCodecUtils.COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m}; |
| 57 |
| 58 | // Color formats supported by texture mode encoding - in order of preference. |
| 59 | static final int[] TEXTURE_COLOR_FORMATS = |
| 60 | new int[] {MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface}; |
| 61 |
| 62 | static @Nullable Integer selectColorFormat( |
| 63 | int[] supportedColorFormats, CodecCapabilities capabilities) { |
| 64 | for (int supportedColorFormat : supportedColorFormats) { |
| 65 | for (int codecColorFormat : capabilities.colorFormats) { |
| 66 | if (codecColorFormat == supportedColorFormat) { |
| 67 | return codecColorFormat; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return null; |
| 72 | } |
| 73 |
| 74 | static boolean codecSupportsType(MediaCodecInfo info, VideoCodecMimeType type) { |
| 75 | for (String mimeType : info.getSupportedTypes()) { |
| 76 | if (type.mimeType().equals(mimeType)) { |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 |
| 83 | static Map<String, String> getCodecProperties(VideoCodecMimeType type, boolean highProfile) { |
| 84 | switch (type) { |
| 85 | case VP8: |
| 86 | case VP9: |
| 87 | case AV1: |
| 88 | case H265: |
| 89 | return new HashMap<String, String>(); |
| 90 | case H264: |
| 91 | return H264Utils.getDefaultH264Params(highProfile); |
| 92 | default: |
| 93 | throw new IllegalArgumentException("Unsupported codec: " + type); |
| 94 | } |
| 95 | } |
| 96 |
| 97 | static boolean isHardwareAccelerated(MediaCodecInfo info) { |
| 98 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 99 | return isHardwareAcceleratedQOrHigher(info); |
| 100 | } |
| 101 | return !isSoftwareOnly(info); |
| 102 | } |
| 103 |
| 104 | @TargetApi(29) |
| 105 | private static boolean isHardwareAcceleratedQOrHigher(android.media.MediaCodecInfo codecInfo) { |
| 106 | return codecInfo.isHardwareAccelerated(); |
| 107 | } |
| 108 |
| 109 | static boolean isSoftwareOnly(android.media.MediaCodecInfo codecInfo) { |
| 110 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 111 | return isSoftwareOnlyQOrHigher(codecInfo); |
| 112 | } |
| 113 | String name = codecInfo.getName(); |
| 114 | for (String prefix : SOFTWARE_IMPLEMENTATION_PREFIXES) { |
| 115 | if (name.startsWith(prefix)) { |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 |
| 122 | @TargetApi(29) |
| 123 | private static boolean isSoftwareOnlyQOrHigher(android.media.MediaCodecInfo codecInfo) { |
| 124 | return codecInfo.isSoftwareOnly(); |
| 125 | } |
| 126 |
| 127 | private MediaCodecUtils() { |
| 128 | // This class should not be instantiated. |
| 129 | } |
| 130 | } |
| 131 |