Search in sources :

Example 21 with MediaCodecInfo

use of android.media.MediaCodecInfo in project LanSoEditor_advance by LanSoSdk.

the class VideoEditor method isSupportNV21ColorFormat.

public static boolean isSupportNV21ColorFormat() {
    if (isSupportNV21) {
        return true;
    }
    MediaCodecInfo codecInfo = selectCodec(MIME_TYPE_AVC);
    if (codecInfo == null) {
        return false;
    }
    isSupportNV21 = selectColorFormat(codecInfo, MIME_TYPE_AVC);
    return isSupportNV21;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo)

Example 22 with MediaCodecInfo

use of android.media.MediaCodecInfo in project LanSoEditor_advance by LanSoSdk.

the class VideoEditor method selectCodec.

private static MediaCodecInfo selectCodec(String mimeType) {
    int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        if (!codecInfo.isEncoder()) {
            continue;
        }
        String[] types = codecInfo.getSupportedTypes();
        for (int j = 0; j < types.length; j++) {
            if (types[j].equalsIgnoreCase(mimeType)) {
                return codecInfo;
            }
        }
    }
    return null;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo)

Example 23 with MediaCodecInfo

use of android.media.MediaCodecInfo in project android_packages_apps_Snap by LineageOS.

the class SettingsManager method getSupportedVideoSize.

private List<String> getSupportedVideoSize(int cameraId) {
    StreamConfigurationMap map = mCharacteristics.get(cameraId).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
    Size[] sizes = map.getOutputSizes(MediaRecorder.class);
    boolean isHeifEnabled = getSavePictureFormat() == HEIF_FORMAT;
    VideoCapabilities heifCap = null;
    if (isHeifEnabled) {
        MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
        for (MediaCodecInfo info : list.getCodecInfos()) {
            if (info.isEncoder() && info.getName().contains("heic")) {
                heifCap = info.getCapabilitiesForType(MediaFormat.MIMETYPE_IMAGE_ANDROID_HEIC).getVideoCapabilities();
                Log.d(TAG, "supported heif height range =" + heifCap.getSupportedHeights().toString() + " width range =" + heifCap.getSupportedWidths().toString());
            }
        }
    }
    List<String> res = new ArrayList<>();
    for (int i = 0; i < sizes.length; i++) {
        if (isHeifEnabled && heifCap != null) {
            if (!heifCap.getSupportedWidths().contains(sizes[i].getWidth()) || !heifCap.getSupportedHeights().contains(sizes[i].getHeight())) {
                continue;
            }
        }
        if (CameraSettings.VIDEO_QUALITY_TABLE.containsKey(sizes[i].toString())) {
            Integer profile = CameraSettings.VIDEO_QUALITY_TABLE.get(sizes[i].toString());
            if (profile != null && CamcorderProfile.hasProfile(cameraId, profile)) {
                res.add(sizes[i].toString());
            }
        }
    }
    return res;
}
Also used : Size(android.util.Size) MediaCodecInfo(android.media.MediaCodecInfo) MediaCodecList(android.media.MediaCodecList) ArrayList(java.util.ArrayList) StreamConfigurationMap(android.hardware.camera2.params.StreamConfigurationMap) VideoCapabilities(android.media.MediaCodecInfo.VideoCapabilities) Point(android.graphics.Point)

Example 24 with MediaCodecInfo

use of android.media.MediaCodecInfo in project android_packages_apps_Snap by LineageOS.

the class SettingsManager method getSupportedPictureSize.

private List<String> getSupportedPictureSize(int cameraId) {
    StreamConfigurationMap map = mCharacteristics.get(cameraId).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
    Size[] sizes = map.getOutputSizes(ImageFormat.JPEG);
    List<String> res = new ArrayList<>();
    boolean isDeepportrait = getDeepportraitEnabled();
    boolean isHeifEnabled = getSavePictureFormat() == HEIF_FORMAT;
    if (getQcfaPrefEnabled() && getIsSupportedQcfa(cameraId)) {
        res.add(getSupportedQcfaDimension(cameraId));
    }
    VideoCapabilities heifCap = null;
    if (isHeifEnabled) {
        MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
        for (MediaCodecInfo info : list.getCodecInfos()) {
            if (info.isEncoder() && info.getName().contains("heic")) {
                heifCap = info.getCapabilitiesForType(MediaFormat.MIMETYPE_IMAGE_ANDROID_HEIC).getVideoCapabilities();
                Log.d(TAG, "supported heif height range =" + heifCap.getSupportedHeights().toString() + " width range =" + heifCap.getSupportedWidths().toString());
            }
        }
    }
    if (sizes != null) {
        for (int i = 0; i < sizes.length; i++) {
            if (isHeifEnabled && heifCap != null) {
                if (!heifCap.getSupportedWidths().contains(sizes[i].getWidth()) || !heifCap.getSupportedHeights().contains(sizes[i].getHeight())) {
                    continue;
                }
            }
            if (isDeepportrait && (Math.min(sizes[i].getWidth(), sizes[i].getHeight()) < 720 || Math.max(sizes[i].getWidth(), sizes[i].getHeight()) <= 1024)) {
                // some reslutions are not supported in deepportrait
                continue;
            }
            res.add(sizes[i].toString());
        }
    }
    Size[] highResSizes = map.getHighResolutionOutputSizes(ImageFormat.JPEG);
    if (highResSizes != null) {
        for (int i = 0; i < highResSizes.length; i++) {
            if (isHeifEnabled && heifCap != null) {
                if (!heifCap.getSupportedWidths().contains(highResSizes[i].getWidth()) || !heifCap.getSupportedHeights().contains(highResSizes[i].getHeight())) {
                    continue;
                }
            }
            res.add(highResSizes[i].toString());
        }
    }
    return res;
}
Also used : Size(android.util.Size) MediaCodecInfo(android.media.MediaCodecInfo) ArrayList(java.util.ArrayList) MediaCodecList(android.media.MediaCodecList) StreamConfigurationMap(android.hardware.camera2.params.StreamConfigurationMap) VideoCapabilities(android.media.MediaCodecInfo.VideoCapabilities) Point(android.graphics.Point)

Example 25 with MediaCodecInfo

use of android.media.MediaCodecInfo in project Telegram-FOSS by Telegram-FOSS-Team.

the class SendMessagesHelper method createCompressionSettings.

private static VideoEditedInfo createCompressionSettings(String videoPath) {
    int[] params = new int[AnimatedFileDrawable.PARAM_NUM_COUNT];
    AnimatedFileDrawable.getVideoInfo(videoPath, params);
    if (params[AnimatedFileDrawable.PARAM_NUM_SUPPORTED_VIDEO_CODEC] == 0) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("video hasn't avc1 atom");
        }
        return null;
    }
    int originalBitrate = MediaController.getVideoBitrate(videoPath);
    if (originalBitrate == -1) {
        originalBitrate = params[AnimatedFileDrawable.PARAM_NUM_BITRATE];
    }
    int bitrate = originalBitrate;
    float videoDuration = params[AnimatedFileDrawable.PARAM_NUM_DURATION];
    long videoFramesSize = params[AnimatedFileDrawable.PARAM_NUM_VIDEO_FRAME_SIZE];
    long audioFramesSize = params[AnimatedFileDrawable.PARAM_NUM_AUDIO_FRAME_SIZE];
    int videoFramerate = params[AnimatedFileDrawable.PARAM_NUM_FRAMERATE];
    if (Build.VERSION.SDK_INT < 18) {
        try {
            MediaCodecInfo codecInfo = MediaController.selectCodec(MediaController.VIDEO_MIME_TYPE);
            if (codecInfo == null) {
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("no codec info for " + MediaController.VIDEO_MIME_TYPE);
                }
                return null;
            } else {
                String name = codecInfo.getName();
                if (name.equals("OMX.google.h264.encoder") || name.equals("OMX.ST.VFM.H264Enc") || name.equals("OMX.Exynos.avc.enc") || name.equals("OMX.MARVELL.VIDEO.HW.CODA7542ENCODER") || name.equals("OMX.MARVELL.VIDEO.H264ENCODER") || name.equals("OMX.k3.video.encoder.avc") || name.equals("OMX.TI.DUCATI1.VIDEO.H264E")) {
                    if (BuildVars.LOGS_ENABLED) {
                        FileLog.d("unsupported encoder = " + name);
                    }
                    return null;
                } else {
                    if (MediaController.selectColorFormat(codecInfo, MediaController.VIDEO_MIME_TYPE) == 0) {
                        if (BuildVars.LOGS_ENABLED) {
                            FileLog.d("no color format for " + MediaController.VIDEO_MIME_TYPE);
                        }
                        return null;
                    }
                }
            }
        } catch (Exception e) {
            return null;
        }
    }
    VideoEditedInfo videoEditedInfo = new VideoEditedInfo();
    videoEditedInfo.startTime = -1;
    videoEditedInfo.endTime = -1;
    videoEditedInfo.bitrate = bitrate;
    videoEditedInfo.originalPath = videoPath;
    videoEditedInfo.framerate = videoFramerate;
    videoEditedInfo.estimatedDuration = (long) Math.ceil(videoDuration);
    videoEditedInfo.resultWidth = videoEditedInfo.originalWidth = params[AnimatedFileDrawable.PARAM_NUM_WIDTH];
    videoEditedInfo.resultHeight = videoEditedInfo.originalHeight = params[AnimatedFileDrawable.PARAM_NUM_HEIGHT];
    videoEditedInfo.rotationValue = params[AnimatedFileDrawable.PARAM_NUM_ROTATION];
    videoEditedInfo.originalDuration = (long) (videoDuration * 1000);
    int compressionsCount;
    float maxSize = Math.max(videoEditedInfo.originalWidth, videoEditedInfo.originalHeight);
    if (maxSize > 1280) {
        compressionsCount = 4;
    } else if (maxSize > 854) {
        compressionsCount = 3;
    } else if (maxSize > 640) {
        compressionsCount = 2;
    } else {
        compressionsCount = 1;
    }
    int selectedCompression = Math.round(DownloadController.getInstance(UserConfig.selectedAccount).getMaxVideoBitrate() / (100f / compressionsCount));
    if (selectedCompression > compressionsCount) {
        selectedCompression = compressionsCount;
    }
    boolean needCompress = false;
    if (selectedCompression != compressionsCount - 1 || Math.max(videoEditedInfo.originalWidth, videoEditedInfo.originalHeight) > 1280) {
        needCompress = true;
        switch(selectedCompression) {
            case 1:
                maxSize = 432.0f;
                break;
            case 2:
                maxSize = 640.0f;
                break;
            case 3:
                maxSize = 848.0f;
                break;
            default:
                maxSize = 1280.0f;
                break;
        }
        float scale = videoEditedInfo.originalWidth > videoEditedInfo.originalHeight ? maxSize / videoEditedInfo.originalWidth : maxSize / videoEditedInfo.originalHeight;
        videoEditedInfo.resultWidth = Math.round(videoEditedInfo.originalWidth * scale / 2) * 2;
        videoEditedInfo.resultHeight = Math.round(videoEditedInfo.originalHeight * scale / 2) * 2;
    }
    bitrate = MediaController.makeVideoBitrate(videoEditedInfo.originalHeight, videoEditedInfo.originalWidth, originalBitrate, videoEditedInfo.resultHeight, videoEditedInfo.resultWidth);
    if (!needCompress) {
        videoEditedInfo.resultWidth = videoEditedInfo.originalWidth;
        videoEditedInfo.resultHeight = videoEditedInfo.originalHeight;
        videoEditedInfo.bitrate = bitrate;
        videoEditedInfo.estimatedSize = (int) (audioFramesSize + videoDuration / 1000.0f * bitrate / 8);
    } else {
        videoEditedInfo.bitrate = bitrate;
        videoEditedInfo.estimatedSize = (int) (audioFramesSize + videoFramesSize);
        videoEditedInfo.estimatedSize += videoEditedInfo.estimatedSize / (32 * 1024) * 16;
    }
    if (videoEditedInfo.estimatedSize == 0) {
        videoEditedInfo.estimatedSize = 1;
    }
    return videoEditedInfo;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) Point(org.telegram.ui.Components.Point)

Aggregations

MediaCodecInfo (android.media.MediaCodecInfo)102 ArrayList (java.util.ArrayList)32 SuppressLint (android.annotation.SuppressLint)19 MediaCodecList (android.media.MediaCodecList)18 MediaFormat (android.media.MediaFormat)16 CodecCapabilities (android.media.MediaCodecInfo.CodecCapabilities)15 IOException (java.io.IOException)13 TargetApi (android.annotation.TargetApi)10 Point (android.graphics.Point)9 VideoCapabilities (android.media.MediaCodecInfo.VideoCapabilities)6 Size (android.util.Size)6 Nullable (androidx.annotation.Nullable)6 HashSet (java.util.HashSet)6 Test (org.junit.Test)6 Config (org.robolectric.annotation.Config)5 SparseIntArray (android.util.SparseIntArray)4 TreeMap (java.util.TreeMap)4 MediaCodec (android.media.MediaCodec)3 Bundle (android.os.Bundle)3 NonNull (androidx.annotation.NonNull)3