Commit 202cc67f authored by Marcin Wiącek's avatar Marcin Wiącek Committed by Commit Bot

Replace enum HWEncoderProperties with @IntDef

@IntDef/@StringDef annotation are preferred way for declaring
set of String/int values

1. they need less space in APK than enum, see
https://developer.android.com/topic/performance/reduce-apk-size#remove-enums
2. they give more control over allowed values than "static final" values

Patch is updating HWEncoderProperties to @IntDef with form common with other Chrome @IntDef:

1. with @IntDef first, @Retention second
   and related @interface third
2. with values inside @interface
3. with @Retention(RetentionPolicy.SOURCE)
4. without "static final" in the @interface
5. with NUM_ENTRIES

BUG=919666

Change-Id: I3dac5daa74145708436f63c6a8af0f50136addc3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1545928
Commit-Queue: Marcin Wiącek <marcin@mwiacek.com>
Reviewed-by: default avatarJohn Rummell <jrummell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#646607}
parent 1c1defc5
...@@ -15,6 +15,8 @@ import android.media.MediaCodecList; ...@@ -15,6 +15,8 @@ import android.media.MediaCodecList;
import android.media.MediaCrypto; import android.media.MediaCrypto;
import android.media.MediaFormat; import android.media.MediaFormat;
import android.os.Build; import android.os.Build;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
...@@ -22,6 +24,8 @@ import org.chromium.base.annotations.JNINamespace; ...@@ -22,6 +24,8 @@ import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex; import org.chromium.base.annotations.MainDex;
import org.chromium.base.compat.ApiHelperForN; import org.chromium.base.compat.ApiHelperForN;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
...@@ -495,46 +499,72 @@ class MediaCodecUtil { ...@@ -495,46 +499,72 @@ class MediaCodecUtil {
} }
// List of supported HW encoders. // List of supported HW encoders.
private static enum HWEncoderProperties { @IntDef({HWEncoder.QcomVp8, HWEncoder.QcomH264, HWEncoder.ExynosVp8, HWEncoder.ExynosH264,
QcomVp8(MimeTypes.VIDEO_VP8, "OMX.qcom.", Build.VERSION_CODES.KITKAT, HWEncoder.MediatekH264})
BitrateAdjuster.Type.NO_ADJUSTMENT), @Retention(RetentionPolicy.SOURCE)
QcomH264(MimeTypes.VIDEO_H264, "OMX.qcom.", Build.VERSION_CODES.KITKAT, public @interface HWEncoder {
BitrateAdjuster.Type.NO_ADJUSTMENT), int QcomVp8 = 0;
ExynosVp8(MimeTypes.VIDEO_VP8, "OMX.Exynos.", Build.VERSION_CODES.M, int QcomH264 = 1;
BitrateAdjuster.Type.NO_ADJUSTMENT), int ExynosVp8 = 2;
ExynosH264(MimeTypes.VIDEO_H264, "OMX.Exynos.", Build.VERSION_CODES.LOLLIPOP, int ExynosH264 = 3;
BitrateAdjuster.Type.FRAMERATE_ADJUSTMENT), int MediatekH264 = 4;
MediatekH264(MimeTypes.VIDEO_H264, "OMX.MTK.", Build.VERSION_CODES.O_MR1, int NUM_ENTRIES = 5;
BitrateAdjuster.Type.FRAMERATE_ADJUSTMENT); }
private final String mMime;
private final String mPrefix;
private final int mMinSDK;
private final @BitrateAdjuster.Type int mBitrateAdjuster;
private HWEncoderProperties(
String mime, String prefix, int minSDK, @BitrateAdjuster.Type int bitrateAdjuster) {
this.mMime = mime;
this.mPrefix = prefix;
this.mMinSDK = minSDK;
this.mBitrateAdjuster = bitrateAdjuster;
}
public String getMime() { private static String getMimeForHWEncoder(@HWEncoder int decoder) {
return mMime; switch (decoder) {
case HWEncoder.QcomVp8:
case HWEncoder.ExynosVp8:
return MimeTypes.VIDEO_VP8;
case HWEncoder.QcomH264:
case HWEncoder.ExynosH264:
case HWEncoder.MediatekH264:
return MimeTypes.VIDEO_H264;
} }
return "";
}
public String getPrefix() { private static String getPrefixForHWEncoder(@HWEncoder int decoder) {
return mPrefix; switch (decoder) {
case HWEncoder.QcomVp8:
case HWEncoder.QcomH264:
return "OMX.qcom.";
case HWEncoder.ExynosVp8:
case HWEncoder.ExynosH264:
return "OMX.Exynos.";
case HWEncoder.MediatekH264:
return "OMX.MTK.";
} }
return "";
}
public int getMinSDK() { private static int getMinSDKForHWEncoder(@HWEncoder int decoder) {
return mMinSDK; switch (decoder) {
} case HWEncoder.QcomVp8:
case HWEncoder.QcomH264:
return Build.VERSION_CODES.KITKAT;
case HWEncoder.ExynosVp8:
return Build.VERSION_CODES.M;
case HWEncoder.ExynosH264:
return Build.VERSION_CODES.LOLLIPOP;
case HWEncoder.MediatekH264:
return Build.VERSION_CODES.O_MR1;
}
return -1;
}
public @BitrateAdjuster.Type int getBitrateAdjuster() { private static @BitrateAdjuster.Type int getBitrateAdjusterTypeForHWEncoder(
return mBitrateAdjuster; @HWEncoder int decoder) {
} switch (decoder) {
case HWEncoder.QcomVp8:
case HWEncoder.QcomH264:
case HWEncoder.ExynosVp8:
return BitrateAdjuster.Type.NO_ADJUSTMENT;
case HWEncoder.ExynosH264:
case HWEncoder.MediatekH264:
return BitrateAdjuster.Type.FRAMERATE_ADJUSTMENT;
}
return -1;
} }
// List of devices with poor H.264 encoder quality. // List of devices with poor H.264 encoder quality.
...@@ -553,15 +583,15 @@ class MediaCodecUtil { ...@@ -553,15 +583,15 @@ class MediaCodecUtil {
// if we cannot create the codec. // if we cannot create the codec.
CodecCreationInfo result = new CodecCreationInfo(); CodecCreationInfo result = new CodecCreationInfo();
HWEncoderProperties encoderProperties = findHWEncoder(mime); @Nullable
if (encoderProperties == null) { @HWEncoder
return result; Integer encoderProperties = findHWEncoder(mime);
} if (encoderProperties == null) return result;
try { try {
result.mediaCodec = MediaCodec.createEncoderByType(mime); result.mediaCodec = MediaCodec.createEncoderByType(mime);
result.supportsAdaptivePlayback = false; result.supportsAdaptivePlayback = false;
result.bitrateAdjuster = encoderProperties.getBitrateAdjuster(); result.bitrateAdjuster = getBitrateAdjusterTypeForHWEncoder(encoderProperties);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Failed to create MediaCodec: %s", mime, e); Log.e(TAG, "Failed to create MediaCodec: %s", mime, e);
} }
...@@ -591,11 +621,7 @@ class MediaCodecUtil { ...@@ -591,11 +621,7 @@ class MediaCodecUtil {
} }
} }
if (findHWEncoder(mime) == null) { return !(findHWEncoder(mime) == null);
return false;
}
return true;
} }
/** /**
...@@ -616,9 +642,9 @@ class MediaCodecUtil { ...@@ -616,9 +642,9 @@ class MediaCodecUtil {
/** /**
* Find HW encoder with given MIME type. * Find HW encoder with given MIME type.
* @param mime MIME type of the media. * @param mime MIME type of the media.
* @return HWEncoderProperties object. * @return HWEncoder or null if not found.
*/ */
private static HWEncoderProperties findHWEncoder(String mime) { private static @Nullable @HWEncoder Integer findHWEncoder(String mime) {
MediaCodecListHelper codecListHelper = new MediaCodecListHelper(); MediaCodecListHelper codecListHelper = new MediaCodecListHelper();
for (MediaCodecInfo info : codecListHelper) { for (MediaCodecInfo info : codecListHelper) {
if (!info.isEncoder() || isSoftwareCodec(info.getName())) continue; if (!info.isEncoder() || isSoftwareCodec(info.getName())) continue;
...@@ -636,11 +662,12 @@ class MediaCodecUtil { ...@@ -636,11 +662,12 @@ class MediaCodecUtil {
} }
// Check if this is supported HW encoder. // Check if this is supported HW encoder.
for (HWEncoderProperties codecProperties : HWEncoderProperties.values()) { for (@HWEncoder int codecProperties = 0; codecProperties < HWEncoder.NUM_ENTRIES;
if (!mime.equalsIgnoreCase(codecProperties.getMime())) continue; codecProperties++) {
if (!mime.equalsIgnoreCase(getMimeForHWEncoder(codecProperties))) continue;
if (encoderName.startsWith(codecProperties.getPrefix())) { if (encoderName.startsWith(getPrefixForHWEncoder(codecProperties))) {
if (Build.VERSION.SDK_INT < codecProperties.getMinSDK()) { if (Build.VERSION.SDK_INT < getMinSDKForHWEncoder(codecProperties)) {
Log.w(TAG, "Codec " + encoderName + " is disabled due to SDK version " Log.w(TAG, "Codec " + encoderName + " is disabled due to SDK version "
+ Build.VERSION.SDK_INT); + Build.VERSION.SDK_INT);
continue; continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment