Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.video.cts;
     18 
     19 import com.android.compatibility.common.util.MediaUtils;
     20 
     21 import android.media.MediaCodec;
     22 import android.media.MediaCodecInfo;
     23 import android.media.MediaCodecInfo.CodecCapabilities;
     24 import android.media.MediaCodecInfo.CodecProfileLevel;
     25 import android.media.MediaCodecInfo.VideoCapabilities;
     26 import android.media.MediaCodecList;
     27 import android.media.MediaFormat;
     28 import android.util.Log;
     29 import android.util.Range;
     30 
     31 import java.io.IOException;
     32 
     33 /**
     34  * Utility class for getting codec information like bit rate, fps, and etc.
     35  * Uses public member variables instead of methods as this code is only for video benchmarking.
     36  */
     37 public class CodecInfo {
     38     /** bit rate in bps */
     39     public int mBitRate = 0;
     40     /** Frame rate */
     41     public int mFps = 0;
     42     /** if codec is supporting YUV semiplanar format */
     43     public boolean mSupportSemiPlanar = false;
     44     /** if codec is supporting YUV planar format */
     45     public boolean mSupportPlanar = false;
     46 
     47     private static final String TAG = "CodecInfo";
     48     private static final String VIDEO_AVC = MediaFormat.MIMETYPE_VIDEO_AVC;
     49     /**
     50      * Check if given codec with given (w,h) is supported.
     51      * @param codecName codec name
     52      * @param mimeType codec type in mime format like MediaFormat.MIMETYPE_VIDEO_AVC
     53      * @param w video width
     54      * @param h video height
     55      * @return null if the configuration is not supported.
     56      */
     57     public static CodecInfo getSupportedFormatInfo(
     58             String codecName, String mimeType, int w, int h, int maxFps) {
     59         MediaCodec codec;
     60         try {
     61             codec = MediaCodec.createByCodecName(codecName);
     62         } catch (IOException e) {
     63             return null;
     64         }
     65 
     66         CodecCapabilities cap = codec.getCodecInfo().getCapabilitiesForType(mimeType);
     67         if (cap.colorFormats.length == 0) {
     68             Log.w(TAG, "no supported color format");
     69             codec.release();
     70             return null;
     71         }
     72 
     73         CodecInfo info = new CodecInfo();
     74         for (int color : cap.colorFormats) {
     75             if (color == CodecCapabilities.COLOR_FormatYUV420SemiPlanar) {
     76                 info.mSupportSemiPlanar = true;
     77             }
     78             if (color == CodecCapabilities.COLOR_FormatYUV420Planar) {
     79                 info.mSupportPlanar = true;
     80             }
     81         }
     82         printIntArray("supported colors", cap.colorFormats);
     83 
     84         MediaFormat format = MediaFormat.createVideoFormat(mimeType, w, h);
     85         MediaUtils.setMaxEncoderFrameAndBitrates(cap.getVideoCapabilities(), format, maxFps);
     86         info.mFps = format.getInteger(MediaFormat.KEY_FRAME_RATE);
     87         info.mBitRate = format.getInteger(MediaFormat.KEY_BIT_RATE);
     88 
     89         codec.release();
     90         return info;
     91     }
     92 
     93     // for debugging
     94     private static void printIntArray(String msg, int[] data) {
     95         StringBuilder builder = new StringBuilder();
     96         builder.append(msg);
     97         builder.append(":");
     98         for (int e : data) {
     99             builder.append(Integer.toHexString(e));
    100             builder.append(",");
    101         }
    102         builder.deleteCharAt(builder.length() - 1);
    103         Log.i(TAG, builder.toString());
    104     }
    105 }
    106