Home » Multimedia » Identify media encoder / decoder (codec) information of Android device

Identify media encoder / decoder (codec) information of Android device

This post details, how you can obtain the media encoder and decoder ( codec) information of your android Android device. First we will show, how you can use media codec API’s to get this information.

You just follow the steps from “Android demo application with button click event” to create the demo application and then Modify MainActivity.java as below,

package com.demoapp.clickbutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.os.Build;
import android.widget.Button;

import com.demoapp.clickbutton.R;

import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaCodecInfo.CodecCapabilities;
import android.media.MediaCodecInfo.VideoCapabilities;
import android.media.MediaFormat;


public class MainActivity extends AppCompatActivity {

    Button btn;
    private static final String TAG = "lynxbee:";

    private void printSupportedCodecInfo() {
        int numCodecs = MediaCodecList.getCodecCount();
        for (int i = 0; i < numCodecs; i++) {
                MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);

                /*if (codecInfo.isEncoder()) {
                        continue;
                }*/

                Log.d(TAG, "--------------------------------");
                Log.d(TAG, "getName: " + codecInfo.getName() + " isEncoder: " + codecInfo.isEncoder());

                String[] types = codecInfo.getSupportedTypes();
                for (int j = 0; j < types.length; j++) {
                        Log.d(TAG, "Type: " + types[j]);
                        MediaCodecInfo.CodecCapabilities cap = codecInfo.getCapabilitiesForType(types[j]);
                        Log.d(TAG, "default format = " + cap.getDefaultFormat().toString());

                        if (types[j].startsWith("video")) {
                                MediaCodecInfo.VideoCapabilities vcap = cap.getVideoCapabilities();

                                Log.d(TAG, "getBitrateRange: " + vcap.getBitrateRange());
                                Log.d(TAG, "getHeightAlignment: " + vcap.getHeightAlignment());
                                Log.d(TAG, "getWidthAlignment: " + vcap.getWidthAlignment());

                                Log.d(TAG, "isSizeSupported:640*480: " + vcap.isSizeSupported(640, 480));
                                Log.d(TAG, "isSizeSupported:720*1280: " + vcap.isSizeSupported(720, 1280));
                                Log.d(TAG, "isSizeSupported:1080*1920: " + vcap.isSizeSupported(1080, 1920));

                                Log.d(TAG, "getSupportedWidths: " + vcap.getSupportedWidths());
                                Log.d(TAG, "getSupportedHeights: " + vcap.getSupportedHeights());
                                Log.d(TAG, "getSupportedFrameRates: " + vcap.getSupportedFrameRates());

                                int width = 176;
                                int height = 144;
                                int maxWidth = vcap.getSupportedWidths().getUpper();
                                int maxHeight = vcap.getSupportedHeightsFor(width).getUpper();
                                int maxRate = vcap.getSupportedFrameRatesFor(width, height).getUpper().intValue();
                                int bitrate = vcap.getBitrateRange().clamp(
                                        (int)(vcap.getBitrateRange().getUpper() / Math.sqrt(
                                                (double)maxWidth * maxHeight / width / height)));

                                Log.i(TAG, "reasonable bitrate for " + width + "x" + height + "@" + maxRate + " " + types[j] + " = " + bitrate);
                                Log.d(TAG, "areSizeAndRateSupported: " + vcap.areSizeAndRateSupported(width, height, maxRate));
                        }
                }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "Build.TYPE :: " + Build.TYPE);
		printSupportedCodecInfo();
            }
        });
    }
}

Now, when you compile this application, install and open this application and click on “ClickButton”, it will print the codec information in Logcat, which you can filter as,

$ adb logcat | grep lynxbee

If you are looking for only decoder information, just uncomment below code to skip printing encoder information

if (codecInfo.isEncoder()) {
    continue;
}

You can also use “Media Codec Info” application from playstore https://play.google.com/store/apps/details?id=net.tyniw.mediacodecinfo.application&hl=en

Leave a Comment